[c++]: Unconfuse lookup_name_real API a bit
[gcc.git] / gcc / cp / call.c
1 /* Functions related to invoking -*- C++ -*- methods and overloaded functions.
2 Copyright (C) 1987-2020 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 };
64
65 /* The rank of the conversion. Order of the enumerals matters; better
66 conversions should come earlier in the list. */
67
68 enum conversion_rank {
69 cr_identity,
70 cr_exact,
71 cr_promotion,
72 cr_std,
73 cr_pbool,
74 cr_user,
75 cr_ellipsis,
76 cr_bad
77 };
78
79 /* An implicit conversion sequence, in the sense of [over.best.ics].
80 The first conversion to be performed is at the end of the chain.
81 That conversion is always a cr_identity conversion. */
82
83 struct conversion {
84 /* The kind of conversion represented by this step. */
85 conversion_kind kind;
86 /* The rank of this conversion. */
87 conversion_rank rank;
88 BOOL_BITFIELD user_conv_p : 1;
89 BOOL_BITFIELD ellipsis_p : 1;
90 BOOL_BITFIELD this_p : 1;
91 /* True if this conversion would be permitted with a bending of
92 language standards, e.g. disregarding pointer qualifiers or
93 converting integers to pointers. */
94 BOOL_BITFIELD bad_p : 1;
95 /* If KIND is ck_ref_bind or ck_base, true to indicate that a
96 temporary should be created to hold the result of the
97 conversion. If KIND is ck_ambig or ck_user, true means force
98 copy-initialization. */
99 BOOL_BITFIELD need_temporary_p : 1;
100 /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion
101 from a pointer-to-derived to pointer-to-base is being performed. */
102 BOOL_BITFIELD base_p : 1;
103 /* If KIND is ck_ref_bind, true when either an lvalue reference is
104 being bound to an lvalue expression or an rvalue reference is
105 being bound to an rvalue expression. If KIND is ck_rvalue or ck_base,
106 true when we are treating an lvalue as an rvalue (12.8p33). If
107 ck_identity, we will be binding a reference directly or decaying to
108 a pointer. */
109 BOOL_BITFIELD rvaluedness_matches_p: 1;
110 BOOL_BITFIELD check_narrowing: 1;
111 /* Whether check_narrowing should only check TREE_CONSTANTs; used
112 in build_converted_constant_expr. */
113 BOOL_BITFIELD check_narrowing_const_only: 1;
114 /* True if this conversion is taking place in a copy-initialization context
115 and we should only consider converting constructors. Only set in
116 ck_base and ck_rvalue. */
117 BOOL_BITFIELD copy_init_p : 1;
118 /* The type of the expression resulting from the conversion. */
119 tree type;
120 union {
121 /* The next conversion in the chain. Since the conversions are
122 arranged from outermost to innermost, the NEXT conversion will
123 actually be performed before this conversion. This variant is
124 used only when KIND is neither ck_identity, ck_aggr, ck_ambig nor
125 ck_list. Please use the next_conversion function instead
126 of using this field directly. */
127 conversion *next;
128 /* The expression at the beginning of the conversion chain. This
129 variant is used only if KIND is ck_identity, ck_aggr, or ck_ambig.
130 You can use conv_get_original_expr to get this expression. */
131 tree expr;
132 /* The array of conversions for an initializer_list, so this
133 variant is used only when KIN D is ck_list. */
134 conversion **list;
135 } u;
136 /* The function candidate corresponding to this conversion
137 sequence. This field is only used if KIND is ck_user. */
138 struct z_candidate *cand;
139 };
140
141 #define CONVERSION_RANK(NODE) \
142 ((NODE)->bad_p ? cr_bad \
143 : (NODE)->ellipsis_p ? cr_ellipsis \
144 : (NODE)->user_conv_p ? cr_user \
145 : (NODE)->rank)
146
147 #define BAD_CONVERSION_RANK(NODE) \
148 ((NODE)->ellipsis_p ? cr_ellipsis \
149 : (NODE)->user_conv_p ? cr_user \
150 : (NODE)->rank)
151
152 static struct obstack conversion_obstack;
153 static bool conversion_obstack_initialized;
154 struct rejection_reason;
155
156 static struct z_candidate * tourney (struct z_candidate *, tsubst_flags_t);
157 static int equal_functions (tree, tree);
158 static int joust (struct z_candidate *, struct z_candidate *, bool,
159 tsubst_flags_t);
160 static int compare_ics (conversion *, conversion *);
161 static void maybe_warn_class_memaccess (location_t, tree,
162 const vec<tree, va_gc> *);
163 static tree build_over_call (struct z_candidate *, int, tsubst_flags_t);
164 static tree convert_like (conversion *, tree, tsubst_flags_t);
165 static tree convert_like_with_context (conversion *, tree, tree, int,
166 tsubst_flags_t);
167 static void op_error (const op_location_t &, enum tree_code, enum tree_code,
168 tree, tree, tree, bool);
169 static struct z_candidate *build_user_type_conversion_1 (tree, tree, int,
170 tsubst_flags_t);
171 static void print_z_candidate (location_t, const char *, struct z_candidate *);
172 static void print_z_candidates (location_t, struct z_candidate *);
173 static tree build_this (tree);
174 static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
175 static bool any_strictly_viable (struct z_candidate *);
176 static struct z_candidate *add_template_candidate
177 (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
178 tree, tree, tree, int, unification_kind_t, tsubst_flags_t);
179 static struct z_candidate *add_template_candidate_real
180 (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
181 tree, tree, tree, int, tree, unification_kind_t, tsubst_flags_t);
182 static bool is_complete (tree);
183 static struct z_candidate *add_conv_candidate
184 (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, tree,
185 tree, tsubst_flags_t);
186 static struct z_candidate *add_function_candidate
187 (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *, tree,
188 tree, int, conversion**, tsubst_flags_t);
189 static conversion *implicit_conversion (tree, tree, tree, bool, int,
190 tsubst_flags_t);
191 static conversion *reference_binding (tree, tree, tree, bool, int,
192 tsubst_flags_t);
193 static conversion *build_conv (conversion_kind, tree, conversion *);
194 static conversion *build_list_conv (tree, tree, int, tsubst_flags_t);
195 static conversion *next_conversion (conversion *);
196 static bool is_subseq (conversion *, conversion *);
197 static conversion *maybe_handle_ref_bind (conversion **);
198 static void maybe_handle_implicit_object (conversion **);
199 static struct z_candidate *add_candidate
200 (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, size_t,
201 conversion **, tree, tree, int, struct rejection_reason *, int);
202 static tree source_type (conversion *);
203 static void add_warning (struct z_candidate *, struct z_candidate *);
204 static conversion *direct_reference_binding (tree, conversion *);
205 static bool promoted_arithmetic_type_p (tree);
206 static conversion *conditional_conversion (tree, tree, tsubst_flags_t);
207 static char *name_as_c_string (tree, tree, bool *);
208 static tree prep_operand (tree);
209 static void add_candidates (tree, tree, const vec<tree, va_gc> *, tree, tree,
210 bool, tree, tree, int, struct z_candidate **,
211 tsubst_flags_t);
212 static conversion *merge_conversion_sequences (conversion *, conversion *);
213 static tree build_temp (tree, tree, int, diagnostic_t *, tsubst_flags_t);
214 static conversion *build_identity_conv (tree, tree);
215 static inline bool conv_binds_to_array_of_unknown_bound (conversion *);
216 static tree prevent_lifetime_extension (tree);
217
218 /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
219 NAME can take many forms... */
220
221 bool
222 check_dtor_name (tree basetype, tree name)
223 {
224 /* Just accept something we've already complained about. */
225 if (name == error_mark_node)
226 return true;
227
228 if (TREE_CODE (name) == TYPE_DECL)
229 name = TREE_TYPE (name);
230 else if (TYPE_P (name))
231 /* OK */;
232 else if (identifier_p (name))
233 {
234 if ((MAYBE_CLASS_TYPE_P (basetype)
235 || TREE_CODE (basetype) == ENUMERAL_TYPE)
236 && name == constructor_name (basetype))
237 return true;
238 else
239 name = get_type_value (name);
240 }
241 else
242 {
243 /* In the case of:
244
245 template <class T> struct S { ~S(); };
246 int i;
247 i.~S();
248
249 NAME will be a class template. */
250 gcc_assert (DECL_CLASS_TEMPLATE_P (name));
251 return false;
252 }
253
254 if (!name || name == error_mark_node)
255 return false;
256 return same_type_p (TYPE_MAIN_VARIANT (basetype), TYPE_MAIN_VARIANT (name));
257 }
258
259 /* We want the address of a function or method. We avoid creating a
260 pointer-to-member function. */
261
262 tree
263 build_addr_func (tree function, tsubst_flags_t complain)
264 {
265 tree type = TREE_TYPE (function);
266
267 /* We have to do these by hand to avoid real pointer to member
268 functions. */
269 if (TREE_CODE (type) == METHOD_TYPE)
270 {
271 if (TREE_CODE (function) == OFFSET_REF)
272 {
273 tree object = build_address (TREE_OPERAND (function, 0));
274 return get_member_function_from_ptrfunc (&object,
275 TREE_OPERAND (function, 1),
276 complain);
277 }
278 function = build_address (function);
279 }
280 else if (TREE_CODE (function) == FUNCTION_DECL
281 && DECL_IMMEDIATE_FUNCTION_P (function))
282 function = build_address (function);
283 else
284 function = decay_conversion (function, complain, /*reject_builtin=*/false);
285
286 return function;
287 }
288
289 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
290 POINTER_TYPE to those. Note, pointer to member function types
291 (TYPE_PTRMEMFUNC_P) must be handled by our callers. There are
292 two variants. build_call_a is the primitive taking an array of
293 arguments, while build_call_n is a wrapper that handles varargs. */
294
295 tree
296 build_call_n (tree function, int n, ...)
297 {
298 if (n == 0)
299 return build_call_a (function, 0, NULL);
300 else
301 {
302 tree *argarray = XALLOCAVEC (tree, n);
303 va_list ap;
304 int i;
305
306 va_start (ap, n);
307 for (i = 0; i < n; i++)
308 argarray[i] = va_arg (ap, tree);
309 va_end (ap);
310 return build_call_a (function, n, argarray);
311 }
312 }
313
314 /* Update various flags in cfun and the call itself based on what is being
315 called. Split out of build_call_a so that bot_manip can use it too. */
316
317 void
318 set_flags_from_callee (tree call)
319 {
320 /* Handle both CALL_EXPRs and AGGR_INIT_EXPRs. */
321 tree decl = cp_get_callee_fndecl_nofold (call);
322
323 /* We check both the decl and the type; a function may be known not to
324 throw without being declared throw(). */
325 bool nothrow = decl && TREE_NOTHROW (decl);
326 tree callee = cp_get_callee (call);
327 if (callee)
328 nothrow |= TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (callee)));
329 else if (TREE_CODE (call) == CALL_EXPR
330 && internal_fn_flags (CALL_EXPR_IFN (call)) & ECF_NOTHROW)
331 nothrow = true;
332
333 if (cfun && cp_function_chain && !cp_unevaluated_operand)
334 {
335 if (!nothrow && at_function_scope_p ())
336 cp_function_chain->can_throw = 1;
337
338 if (decl && TREE_THIS_VOLATILE (decl))
339 current_function_returns_abnormally = 1;
340 }
341
342 TREE_NOTHROW (call) = nothrow;
343 }
344
345 tree
346 build_call_a (tree function, int n, tree *argarray)
347 {
348 tree decl;
349 tree result_type;
350 tree fntype;
351 int i;
352
353 function = build_addr_func (function, tf_warning_or_error);
354
355 gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
356 fntype = TREE_TYPE (TREE_TYPE (function));
357 gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype));
358 result_type = TREE_TYPE (fntype);
359 /* An rvalue has no cv-qualifiers. */
360 if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
361 result_type = cv_unqualified (result_type);
362
363 function = build_call_array_loc (input_location,
364 result_type, function, n, argarray);
365 set_flags_from_callee (function);
366
367 decl = get_callee_fndecl (function);
368
369 if (decl && !TREE_USED (decl))
370 {
371 /* We invoke build_call directly for several library
372 functions. These may have been declared normally if
373 we're building libgcc, so we can't just check
374 DECL_ARTIFICIAL. */
375 gcc_assert (DECL_ARTIFICIAL (decl)
376 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
377 "__", 2));
378 mark_used (decl);
379 }
380
381 require_complete_eh_spec_types (fntype, decl);
382
383 TREE_HAS_CONSTRUCTOR (function) = (decl && DECL_CONSTRUCTOR_P (decl));
384
385 /* Don't pass empty class objects by value. This is useful
386 for tags in STL, which are used to control overload resolution.
387 We don't need to handle other cases of copying empty classes. */
388 if (!decl || !fndecl_built_in_p (decl))
389 for (i = 0; i < n; i++)
390 {
391 tree arg = CALL_EXPR_ARG (function, i);
392 if (is_empty_class (TREE_TYPE (arg))
393 && simple_empty_class_p (TREE_TYPE (arg), arg, INIT_EXPR))
394 {
395 while (TREE_CODE (arg) == TARGET_EXPR)
396 /* We're disconnecting the initializer from its target,
397 don't create a temporary. */
398 arg = TARGET_EXPR_INITIAL (arg);
399 tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg));
400 arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t);
401 CALL_EXPR_ARG (function, i) = arg;
402 }
403 }
404
405 return function;
406 }
407
408 /* New overloading code. */
409
410 struct z_candidate;
411
412 struct candidate_warning {
413 z_candidate *loser;
414 candidate_warning *next;
415 };
416
417 /* Information for providing diagnostics about why overloading failed. */
418
419 enum rejection_reason_code {
420 rr_none,
421 rr_arity,
422 rr_explicit_conversion,
423 rr_template_conversion,
424 rr_arg_conversion,
425 rr_bad_arg_conversion,
426 rr_template_unification,
427 rr_invalid_copy,
428 rr_inherited_ctor,
429 rr_constraint_failure
430 };
431
432 struct conversion_info {
433 /* The index of the argument, 0-based. */
434 int n_arg;
435 /* The actual argument or its type. */
436 tree from;
437 /* The type of the parameter. */
438 tree to_type;
439 /* The location of the argument. */
440 location_t loc;
441 };
442
443 struct rejection_reason {
444 enum rejection_reason_code code;
445 union {
446 /* Information about an arity mismatch. */
447 struct {
448 /* The expected number of arguments. */
449 int expected;
450 /* The actual number of arguments in the call. */
451 int actual;
452 /* Whether the call was a varargs call. */
453 bool call_varargs_p;
454 } arity;
455 /* Information about an argument conversion mismatch. */
456 struct conversion_info conversion;
457 /* Same, but for bad argument conversions. */
458 struct conversion_info bad_conversion;
459 /* Information about template unification failures. These are the
460 parameters passed to fn_type_unification. */
461 struct {
462 tree tmpl;
463 tree explicit_targs;
464 int num_targs;
465 const tree *args;
466 unsigned int nargs;
467 tree return_type;
468 unification_kind_t strict;
469 int flags;
470 } template_unification;
471 /* Information about template instantiation failures. These are the
472 parameters passed to instantiate_template. */
473 struct {
474 tree tmpl;
475 tree targs;
476 } template_instantiation;
477 } u;
478 };
479
480 struct z_candidate {
481 /* The FUNCTION_DECL that will be called if this candidate is
482 selected by overload resolution. */
483 tree fn;
484 /* If not NULL_TREE, the first argument to use when calling this
485 function. */
486 tree first_arg;
487 /* The rest of the arguments to use when calling this function. If
488 there are no further arguments this may be NULL or it may be an
489 empty vector. */
490 const vec<tree, va_gc> *args;
491 /* The implicit conversion sequences for each of the arguments to
492 FN. */
493 conversion **convs;
494 /* The number of implicit conversion sequences. */
495 size_t num_convs;
496 /* If FN is a user-defined conversion, the standard conversion
497 sequence from the type returned by FN to the desired destination
498 type. */
499 conversion *second_conv;
500 struct rejection_reason *reason;
501 /* If FN is a member function, the binfo indicating the path used to
502 qualify the name of FN at the call site. This path is used to
503 determine whether or not FN is accessible if it is selected by
504 overload resolution. The DECL_CONTEXT of FN will always be a
505 (possibly improper) base of this binfo. */
506 tree access_path;
507 /* If FN is a non-static member function, the binfo indicating the
508 subobject to which the `this' pointer should be converted if FN
509 is selected by overload resolution. The type pointed to by
510 the `this' pointer must correspond to the most derived class
511 indicated by the CONVERSION_PATH. */
512 tree conversion_path;
513 tree template_decl;
514 tree explicit_targs;
515 candidate_warning *warnings;
516 z_candidate *next;
517 int viable;
518
519 /* The flags active in add_candidate. */
520 int flags;
521
522 bool rewritten () { return (flags & LOOKUP_REWRITTEN); }
523 bool reversed () { return (flags & LOOKUP_REVERSED); }
524 };
525
526 /* Returns true iff T is a null pointer constant in the sense of
527 [conv.ptr]. */
528
529 bool
530 null_ptr_cst_p (tree t)
531 {
532 tree type = TREE_TYPE (t);
533
534 /* [conv.ptr]
535
536 A null pointer constant is an integer literal ([lex.icon]) with value
537 zero or a prvalue of type std::nullptr_t. */
538 if (NULLPTR_TYPE_P (type))
539 return true;
540
541 if (cxx_dialect >= cxx11)
542 {
543 STRIP_ANY_LOCATION_WRAPPER (t);
544
545 /* Core issue 903 says only literal 0 is a null pointer constant. */
546 if (TREE_CODE (t) == INTEGER_CST
547 && !TREE_OVERFLOW (t)
548 && TREE_CODE (type) == INTEGER_TYPE
549 && integer_zerop (t)
550 && !char_type_p (type))
551 return true;
552 }
553 else if (CP_INTEGRAL_TYPE_P (type))
554 {
555 t = fold_non_dependent_expr (t, tf_none);
556 STRIP_NOPS (t);
557 if (integer_zerop (t) && !TREE_OVERFLOW (t))
558 return true;
559 }
560
561 return false;
562 }
563
564 /* Returns true iff T is a null member pointer value (4.11). */
565
566 bool
567 null_member_pointer_value_p (tree t)
568 {
569 tree type = TREE_TYPE (t);
570 if (!type)
571 return false;
572 else if (TYPE_PTRMEMFUNC_P (type))
573 return (TREE_CODE (t) == CONSTRUCTOR
574 && CONSTRUCTOR_NELTS (t)
575 && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value));
576 else if (TYPE_PTRDATAMEM_P (type))
577 return integer_all_onesp (t);
578 else
579 return false;
580 }
581
582 /* Returns nonzero if PARMLIST consists of only default parms,
583 ellipsis, and/or undeduced parameter packs. */
584
585 bool
586 sufficient_parms_p (const_tree parmlist)
587 {
588 for (; parmlist && parmlist != void_list_node;
589 parmlist = TREE_CHAIN (parmlist))
590 if (!TREE_PURPOSE (parmlist)
591 && !PACK_EXPANSION_P (TREE_VALUE (parmlist)))
592 return false;
593 return true;
594 }
595
596 /* Allocate N bytes of memory from the conversion obstack. The memory
597 is zeroed before being returned. */
598
599 static void *
600 conversion_obstack_alloc (size_t n)
601 {
602 void *p;
603 if (!conversion_obstack_initialized)
604 {
605 gcc_obstack_init (&conversion_obstack);
606 conversion_obstack_initialized = true;
607 }
608 p = obstack_alloc (&conversion_obstack, n);
609 memset (p, 0, n);
610 return p;
611 }
612
613 /* Allocate rejection reasons. */
614
615 static struct rejection_reason *
616 alloc_rejection (enum rejection_reason_code code)
617 {
618 struct rejection_reason *p;
619 p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
620 p->code = code;
621 return p;
622 }
623
624 static struct rejection_reason *
625 arity_rejection (tree first_arg, int expected, int actual)
626 {
627 struct rejection_reason *r = alloc_rejection (rr_arity);
628 int adjust = first_arg != NULL_TREE;
629 r->u.arity.expected = expected - adjust;
630 r->u.arity.actual = actual - adjust;
631 return r;
632 }
633
634 static struct rejection_reason *
635 arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to,
636 location_t loc)
637 {
638 struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
639 int adjust = first_arg != NULL_TREE;
640 r->u.conversion.n_arg = n_arg - adjust;
641 r->u.conversion.from = from;
642 r->u.conversion.to_type = to;
643 r->u.conversion.loc = loc;
644 return r;
645 }
646
647 static struct rejection_reason *
648 bad_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_bad_arg_conversion);
652 int adjust = first_arg != NULL_TREE;
653 r->u.bad_conversion.n_arg = n_arg - adjust;
654 r->u.bad_conversion.from = from;
655 r->u.bad_conversion.to_type = to;
656 r->u.bad_conversion.loc = loc;
657 return r;
658 }
659
660 static struct rejection_reason *
661 explicit_conversion_rejection (tree from, tree to)
662 {
663 struct rejection_reason *r = alloc_rejection (rr_explicit_conversion);
664 r->u.conversion.n_arg = 0;
665 r->u.conversion.from = from;
666 r->u.conversion.to_type = to;
667 r->u.conversion.loc = UNKNOWN_LOCATION;
668 return r;
669 }
670
671 static struct rejection_reason *
672 template_conversion_rejection (tree from, tree to)
673 {
674 struct rejection_reason *r = alloc_rejection (rr_template_conversion);
675 r->u.conversion.n_arg = 0;
676 r->u.conversion.from = from;
677 r->u.conversion.to_type = to;
678 r->u.conversion.loc = UNKNOWN_LOCATION;
679 return r;
680 }
681
682 static struct rejection_reason *
683 template_unification_rejection (tree tmpl, tree explicit_targs, tree targs,
684 const tree *args, unsigned int nargs,
685 tree return_type, unification_kind_t strict,
686 int flags)
687 {
688 size_t args_n_bytes = sizeof (*args) * nargs;
689 tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes);
690 struct rejection_reason *r = alloc_rejection (rr_template_unification);
691 r->u.template_unification.tmpl = tmpl;
692 r->u.template_unification.explicit_targs = explicit_targs;
693 r->u.template_unification.num_targs = TREE_VEC_LENGTH (targs);
694 /* Copy args to our own storage. */
695 memcpy (args1, args, args_n_bytes);
696 r->u.template_unification.args = args1;
697 r->u.template_unification.nargs = nargs;
698 r->u.template_unification.return_type = return_type;
699 r->u.template_unification.strict = strict;
700 r->u.template_unification.flags = flags;
701 return r;
702 }
703
704 static struct rejection_reason *
705 template_unification_error_rejection (void)
706 {
707 return alloc_rejection (rr_template_unification);
708 }
709
710 static struct rejection_reason *
711 invalid_copy_with_fn_template_rejection (void)
712 {
713 struct rejection_reason *r = alloc_rejection (rr_invalid_copy);
714 return r;
715 }
716
717 static struct rejection_reason *
718 inherited_ctor_rejection (void)
719 {
720 struct rejection_reason *r = alloc_rejection (rr_inherited_ctor);
721 return r;
722 }
723
724 /* Build a constraint failure record. */
725
726 static struct rejection_reason *
727 constraint_failure (void)
728 {
729 struct rejection_reason *r = alloc_rejection (rr_constraint_failure);
730 return r;
731 }
732
733 /* Dynamically allocate a conversion. */
734
735 static conversion *
736 alloc_conversion (conversion_kind kind)
737 {
738 conversion *c;
739 c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
740 c->kind = kind;
741 return c;
742 }
743
744 /* Make sure that all memory on the conversion obstack has been
745 freed. */
746
747 void
748 validate_conversion_obstack (void)
749 {
750 if (conversion_obstack_initialized)
751 gcc_assert ((obstack_next_free (&conversion_obstack)
752 == obstack_base (&conversion_obstack)));
753 }
754
755 /* Dynamically allocate an array of N conversions. */
756
757 static conversion **
758 alloc_conversions (size_t n)
759 {
760 return (conversion **) conversion_obstack_alloc (n * sizeof (conversion *));
761 }
762
763 static conversion *
764 build_conv (conversion_kind code, tree type, conversion *from)
765 {
766 conversion *t;
767 conversion_rank rank = CONVERSION_RANK (from);
768
769 /* Note that the caller is responsible for filling in t->cand for
770 user-defined conversions. */
771 t = alloc_conversion (code);
772 t->type = type;
773 t->u.next = from;
774
775 switch (code)
776 {
777 case ck_ptr:
778 case ck_pmem:
779 case ck_base:
780 case ck_std:
781 if (rank < cr_std)
782 rank = cr_std;
783 break;
784
785 case ck_qual:
786 case ck_fnptr:
787 if (rank < cr_exact)
788 rank = cr_exact;
789 break;
790
791 default:
792 break;
793 }
794 t->rank = rank;
795 t->user_conv_p = (code == ck_user || from->user_conv_p);
796 t->bad_p = from->bad_p;
797 t->base_p = false;
798 return t;
799 }
800
801 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
802 specialization of std::initializer_list<T>, if such a conversion is
803 possible. */
804
805 static conversion *
806 build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
807 {
808 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
809 unsigned len = CONSTRUCTOR_NELTS (ctor);
810 conversion **subconvs = alloc_conversions (len);
811 conversion *t;
812 unsigned i;
813 tree val;
814
815 /* Within a list-initialization we can have more user-defined
816 conversions. */
817 flags &= ~LOOKUP_NO_CONVERSION;
818 /* But no narrowing conversions. */
819 flags |= LOOKUP_NO_NARROWING;
820
821 /* Can't make an array of these types. */
822 if (TYPE_REF_P (elttype)
823 || TREE_CODE (elttype) == FUNCTION_TYPE
824 || VOID_TYPE_P (elttype))
825 return NULL;
826
827 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
828 {
829 conversion *sub
830 = implicit_conversion (elttype, TREE_TYPE (val), val,
831 false, flags, complain);
832 if (sub == NULL)
833 return NULL;
834
835 subconvs[i] = sub;
836 }
837
838 t = alloc_conversion (ck_list);
839 t->type = type;
840 t->u.list = subconvs;
841 t->rank = cr_exact;
842
843 for (i = 0; i < len; ++i)
844 {
845 conversion *sub = subconvs[i];
846 if (sub->rank > t->rank)
847 t->rank = sub->rank;
848 if (sub->user_conv_p)
849 t->user_conv_p = true;
850 if (sub->bad_p)
851 t->bad_p = true;
852 }
853
854 return t;
855 }
856
857 /* Return the next conversion of the conversion chain (if applicable),
858 or NULL otherwise. Please use this function instead of directly
859 accessing fields of struct conversion. */
860
861 static conversion *
862 next_conversion (conversion *conv)
863 {
864 if (conv == NULL
865 || conv->kind == ck_identity
866 || conv->kind == ck_ambig
867 || conv->kind == ck_list
868 || conv->kind == ck_aggr)
869 return NULL;
870 return conv->u.next;
871 }
872
873 /* Strip to the first ck_user, ck_ambig, ck_list, ck_aggr or ck_identity
874 encountered. */
875
876 static conversion *
877 strip_standard_conversion (conversion *conv)
878 {
879 while (conv
880 && conv->kind != ck_user
881 && conv->kind != ck_ambig
882 && conv->kind != ck_list
883 && conv->kind != ck_aggr
884 && conv->kind != ck_identity)
885 conv = next_conversion (conv);
886 return conv;
887 }
888
889 /* Subroutine of build_aggr_conv: check whether CTOR, a braced-init-list,
890 is a valid aggregate initializer for array type ATYPE. */
891
892 static bool
893 can_convert_array (tree atype, tree ctor, int flags, tsubst_flags_t complain)
894 {
895 unsigned i;
896 tree elttype = TREE_TYPE (atype);
897 for (i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
898 {
899 tree val = CONSTRUCTOR_ELT (ctor, i)->value;
900 bool ok;
901 if (TREE_CODE (elttype) == ARRAY_TYPE
902 && TREE_CODE (val) == CONSTRUCTOR)
903 ok = can_convert_array (elttype, val, flags, complain);
904 else
905 ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags,
906 complain);
907 if (!ok)
908 return false;
909 }
910 return true;
911 }
912
913 /* Helper for build_aggr_conv. Return true if FIELD is in PSET, or if
914 FIELD has ANON_AGGR_TYPE_P and any initializable field in there recursively
915 is in PSET. */
916
917 static bool
918 field_in_pset (hash_set<tree, true> &pset, tree field)
919 {
920 if (pset.contains (field))
921 return true;
922 if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
923 for (field = TYPE_FIELDS (TREE_TYPE (field));
924 field; field = DECL_CHAIN (field))
925 {
926 field = next_initializable_field (field);
927 if (field == NULL_TREE)
928 break;
929 if (field_in_pset (pset, field))
930 return true;
931 }
932 return false;
933 }
934
935 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
936 aggregate class, if such a conversion is possible. */
937
938 static conversion *
939 build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
940 {
941 unsigned HOST_WIDE_INT i = 0;
942 conversion *c;
943 tree field = next_initializable_field (TYPE_FIELDS (type));
944 tree empty_ctor = NULL_TREE;
945 hash_set<tree, true> pset;
946
947 /* We already called reshape_init in implicit_conversion. */
948
949 /* The conversions within the init-list aren't affected by the enclosing
950 context; they're always simple copy-initialization. */
951 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
952
953 /* For designated initializers, verify that each initializer is convertible
954 to corresponding TREE_TYPE (ce->index) and mark those FIELD_DECLs as
955 visited. In the following loop then ignore already visited
956 FIELD_DECLs. */
957 if (CONSTRUCTOR_IS_DESIGNATED_INIT (ctor))
958 {
959 tree idx, val;
960 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, val)
961 {
962 if (idx && TREE_CODE (idx) == FIELD_DECL)
963 {
964 tree ftype = TREE_TYPE (idx);
965 bool ok;
966
967 if (TREE_CODE (ftype) == ARRAY_TYPE
968 && TREE_CODE (val) == CONSTRUCTOR)
969 ok = can_convert_array (ftype, val, flags, complain);
970 else
971 ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
972 complain);
973
974 if (!ok)
975 return NULL;
976 /* For unions, there should be just one initializer. */
977 if (TREE_CODE (type) == UNION_TYPE)
978 {
979 field = NULL_TREE;
980 i = 1;
981 break;
982 }
983 pset.add (idx);
984 }
985 else
986 return NULL;
987 }
988 }
989
990 for (; field; field = next_initializable_field (DECL_CHAIN (field)))
991 {
992 tree ftype = TREE_TYPE (field);
993 tree val;
994 bool ok;
995
996 if (!pset.is_empty () && field_in_pset (pset, field))
997 continue;
998 if (i < CONSTRUCTOR_NELTS (ctor))
999 {
1000 val = CONSTRUCTOR_ELT (ctor, i)->value;
1001 ++i;
1002 }
1003 else if (DECL_INITIAL (field))
1004 val = get_nsdmi (field, /*ctor*/false, complain);
1005 else if (TYPE_REF_P (ftype))
1006 /* Value-initialization of reference is ill-formed. */
1007 return NULL;
1008 else
1009 {
1010 if (empty_ctor == NULL_TREE)
1011 empty_ctor = build_constructor (init_list_type_node, NULL);
1012 val = empty_ctor;
1013 }
1014
1015 if (TREE_CODE (ftype) == ARRAY_TYPE
1016 && TREE_CODE (val) == CONSTRUCTOR)
1017 ok = can_convert_array (ftype, val, flags, complain);
1018 else
1019 ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
1020 complain);
1021
1022 if (!ok)
1023 return NULL;
1024
1025 if (TREE_CODE (type) == UNION_TYPE)
1026 break;
1027 }
1028
1029 if (i < CONSTRUCTOR_NELTS (ctor))
1030 return NULL;
1031
1032 c = alloc_conversion (ck_aggr);
1033 c->type = type;
1034 c->rank = cr_exact;
1035 c->user_conv_p = true;
1036 c->check_narrowing = true;
1037 c->u.expr = ctor;
1038 return c;
1039 }
1040
1041 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
1042 array type, if such a conversion is possible. */
1043
1044 static conversion *
1045 build_array_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
1046 {
1047 conversion *c;
1048 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1049 tree elttype = TREE_TYPE (type);
1050 unsigned i;
1051 tree val;
1052 bool bad = false;
1053 bool user = false;
1054 enum conversion_rank rank = cr_exact;
1055
1056 /* We might need to propagate the size from the element to the array. */
1057 complete_type (type);
1058
1059 if (TYPE_DOMAIN (type)
1060 && !variably_modified_type_p (TYPE_DOMAIN (type), NULL_TREE))
1061 {
1062 unsigned HOST_WIDE_INT alen = tree_to_uhwi (array_type_nelts_top (type));
1063 if (alen < len)
1064 return NULL;
1065 }
1066
1067 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1068
1069 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
1070 {
1071 conversion *sub
1072 = implicit_conversion (elttype, TREE_TYPE (val), val,
1073 false, flags, complain);
1074 if (sub == NULL)
1075 return NULL;
1076
1077 if (sub->rank > rank)
1078 rank = sub->rank;
1079 if (sub->user_conv_p)
1080 user = true;
1081 if (sub->bad_p)
1082 bad = true;
1083 }
1084
1085 c = alloc_conversion (ck_aggr);
1086 c->type = type;
1087 c->rank = rank;
1088 c->user_conv_p = user;
1089 c->bad_p = bad;
1090 c->u.expr = ctor;
1091 return c;
1092 }
1093
1094 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
1095 complex type, if such a conversion is possible. */
1096
1097 static conversion *
1098 build_complex_conv (tree type, tree ctor, int flags,
1099 tsubst_flags_t complain)
1100 {
1101 conversion *c;
1102 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1103 tree elttype = TREE_TYPE (type);
1104 unsigned i;
1105 tree val;
1106 bool bad = false;
1107 bool user = false;
1108 enum conversion_rank rank = cr_exact;
1109
1110 if (len != 2)
1111 return NULL;
1112
1113 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1114
1115 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
1116 {
1117 conversion *sub
1118 = implicit_conversion (elttype, TREE_TYPE (val), val,
1119 false, flags, complain);
1120 if (sub == NULL)
1121 return NULL;
1122
1123 if (sub->rank > rank)
1124 rank = sub->rank;
1125 if (sub->user_conv_p)
1126 user = true;
1127 if (sub->bad_p)
1128 bad = true;
1129 }
1130
1131 c = alloc_conversion (ck_aggr);
1132 c->type = type;
1133 c->rank = rank;
1134 c->user_conv_p = user;
1135 c->bad_p = bad;
1136 c->u.expr = ctor;
1137 return c;
1138 }
1139
1140 /* Build a representation of the identity conversion from EXPR to
1141 itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
1142
1143 static conversion *
1144 build_identity_conv (tree type, tree expr)
1145 {
1146 conversion *c;
1147
1148 c = alloc_conversion (ck_identity);
1149 c->type = type;
1150 c->u.expr = expr;
1151
1152 return c;
1153 }
1154
1155 /* Converting from EXPR to TYPE was ambiguous in the sense that there
1156 were multiple user-defined conversions to accomplish the job.
1157 Build a conversion that indicates that ambiguity. */
1158
1159 static conversion *
1160 build_ambiguous_conv (tree type, tree expr)
1161 {
1162 conversion *c;
1163
1164 c = alloc_conversion (ck_ambig);
1165 c->type = type;
1166 c->u.expr = expr;
1167
1168 return c;
1169 }
1170
1171 tree
1172 strip_top_quals (tree t)
1173 {
1174 if (TREE_CODE (t) == ARRAY_TYPE)
1175 return t;
1176 return cp_build_qualified_type (t, 0);
1177 }
1178
1179 /* Returns the standard conversion path (see [conv]) from type FROM to type
1180 TO, if any. For proper handling of null pointer constants, you must
1181 also pass the expression EXPR to convert from. If C_CAST_P is true,
1182 this conversion is coming from a C-style cast. */
1183
1184 static conversion *
1185 standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
1186 int flags, tsubst_flags_t complain)
1187 {
1188 enum tree_code fcode, tcode;
1189 conversion *conv;
1190 bool fromref = false;
1191 tree qualified_to;
1192
1193 to = non_reference (to);
1194 if (TYPE_REF_P (from))
1195 {
1196 fromref = true;
1197 from = TREE_TYPE (from);
1198 }
1199 qualified_to = to;
1200 to = strip_top_quals (to);
1201 from = strip_top_quals (from);
1202
1203 if (expr && type_unknown_p (expr))
1204 {
1205 if (TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
1206 {
1207 tsubst_flags_t tflags = tf_conv;
1208 expr = instantiate_type (to, expr, tflags);
1209 if (expr == error_mark_node)
1210 return NULL;
1211 from = TREE_TYPE (expr);
1212 }
1213 else if (TREE_CODE (to) == BOOLEAN_TYPE)
1214 {
1215 /* Necessary for eg, TEMPLATE_ID_EXPRs (c++/50961). */
1216 expr = resolve_nondeduced_context (expr, complain);
1217 from = TREE_TYPE (expr);
1218 }
1219 }
1220
1221 fcode = TREE_CODE (from);
1222 tcode = TREE_CODE (to);
1223
1224 conv = build_identity_conv (from, expr);
1225 if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
1226 {
1227 from = type_decays_to (from);
1228 fcode = TREE_CODE (from);
1229 /* Tell convert_like that we're using the address. */
1230 conv->rvaluedness_matches_p = true;
1231 conv = build_conv (ck_lvalue, from, conv);
1232 }
1233 /* Wrapping a ck_rvalue around a class prvalue (as a result of using
1234 obvalue_p) seems odd, since it's already a prvalue, but that's how we
1235 express the copy constructor call required by copy-initialization. */
1236 else if (fromref || (expr && obvalue_p (expr)))
1237 {
1238 if (expr)
1239 {
1240 tree bitfield_type;
1241 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
1242 if (bitfield_type)
1243 {
1244 from = strip_top_quals (bitfield_type);
1245 fcode = TREE_CODE (from);
1246 }
1247 }
1248 conv = build_conv (ck_rvalue, from, conv);
1249 if (flags & LOOKUP_PREFER_RVALUE)
1250 /* Tell convert_like to set LOOKUP_PREFER_RVALUE. */
1251 conv->rvaluedness_matches_p = true;
1252 /* If we're performing copy-initialization, remember to skip
1253 explicit constructors. */
1254 if (flags & LOOKUP_ONLYCONVERTING)
1255 conv->copy_init_p = true;
1256 }
1257
1258 /* Allow conversion between `__complex__' data types. */
1259 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
1260 {
1261 /* The standard conversion sequence to convert FROM to TO is
1262 the standard conversion sequence to perform componentwise
1263 conversion. */
1264 conversion *part_conv = standard_conversion
1265 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags,
1266 complain);
1267
1268 if (part_conv)
1269 {
1270 conv = build_conv (part_conv->kind, to, conv);
1271 conv->rank = part_conv->rank;
1272 }
1273 else
1274 conv = NULL;
1275
1276 return conv;
1277 }
1278
1279 if (same_type_p (from, to))
1280 {
1281 if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue)
1282 conv->type = qualified_to;
1283 return conv;
1284 }
1285
1286 /* [conv.ptr]
1287 A null pointer constant can be converted to a pointer type; ... A
1288 null pointer constant of integral type can be converted to an
1289 rvalue of type std::nullptr_t. */
1290 if ((tcode == POINTER_TYPE || TYPE_PTRMEM_P (to)
1291 || NULLPTR_TYPE_P (to))
1292 && ((expr && null_ptr_cst_p (expr))
1293 || NULLPTR_TYPE_P (from)))
1294 conv = build_conv (ck_std, to, conv);
1295 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
1296 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
1297 {
1298 /* For backwards brain damage compatibility, allow interconversion of
1299 pointers and integers with a pedwarn. */
1300 conv = build_conv (ck_std, to, conv);
1301 conv->bad_p = true;
1302 }
1303 else if (UNSCOPED_ENUM_P (to) && fcode == INTEGER_TYPE)
1304 {
1305 /* For backwards brain damage compatibility, allow interconversion of
1306 enums and integers with a pedwarn. */
1307 conv = build_conv (ck_std, to, conv);
1308 conv->bad_p = true;
1309 }
1310 else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
1311 || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from)))
1312 {
1313 tree to_pointee;
1314 tree from_pointee;
1315
1316 if (tcode == POINTER_TYPE)
1317 {
1318 to_pointee = TREE_TYPE (to);
1319 from_pointee = TREE_TYPE (from);
1320
1321 /* Since this is the target of a pointer, it can't have function
1322 qualifiers, so any TYPE_QUALS must be for attributes const or
1323 noreturn. Strip them. */
1324 if (TREE_CODE (to_pointee) == FUNCTION_TYPE
1325 && TYPE_QUALS (to_pointee))
1326 to_pointee = build_qualified_type (to_pointee, TYPE_UNQUALIFIED);
1327 if (TREE_CODE (from_pointee) == FUNCTION_TYPE
1328 && TYPE_QUALS (from_pointee))
1329 from_pointee = build_qualified_type (from_pointee, TYPE_UNQUALIFIED);
1330 }
1331 else
1332 {
1333 to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
1334 from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
1335 }
1336
1337 if (tcode == POINTER_TYPE
1338 && same_type_ignoring_top_level_qualifiers_p (from_pointee,
1339 to_pointee))
1340 ;
1341 else if (VOID_TYPE_P (to_pointee)
1342 && !TYPE_PTRDATAMEM_P (from)
1343 && TREE_CODE (from_pointee) != FUNCTION_TYPE)
1344 {
1345 tree nfrom = TREE_TYPE (from);
1346 /* Don't try to apply restrict to void. */
1347 int quals = cp_type_quals (nfrom) & ~TYPE_QUAL_RESTRICT;
1348 from_pointee = cp_build_qualified_type (void_type_node, quals);
1349 from = build_pointer_type (from_pointee);
1350 conv = build_conv (ck_ptr, from, conv);
1351 }
1352 else if (TYPE_PTRDATAMEM_P (from))
1353 {
1354 tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
1355 tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
1356
1357 if (same_type_p (fbase, tbase))
1358 /* No base conversion needed. */;
1359 else if (DERIVED_FROM_P (fbase, tbase)
1360 && (same_type_ignoring_top_level_qualifiers_p
1361 (from_pointee, to_pointee)))
1362 {
1363 from = build_ptrmem_type (tbase, from_pointee);
1364 conv = build_conv (ck_pmem, from, conv);
1365 }
1366 else
1367 return NULL;
1368 }
1369 else if (CLASS_TYPE_P (from_pointee)
1370 && CLASS_TYPE_P (to_pointee)
1371 /* [conv.ptr]
1372
1373 An rvalue of type "pointer to cv D," where D is a
1374 class type, can be converted to an rvalue of type
1375 "pointer to cv B," where B is a base class (clause
1376 _class.derived_) of D. If B is an inaccessible
1377 (clause _class.access_) or ambiguous
1378 (_class.member.lookup_) base class of D, a program
1379 that necessitates this conversion is ill-formed.
1380 Therefore, we use DERIVED_FROM_P, and do not check
1381 access or uniqueness. */
1382 && DERIVED_FROM_P (to_pointee, from_pointee))
1383 {
1384 from_pointee
1385 = cp_build_qualified_type (to_pointee,
1386 cp_type_quals (from_pointee));
1387 from = build_pointer_type (from_pointee);
1388 conv = build_conv (ck_ptr, from, conv);
1389 conv->base_p = true;
1390 }
1391
1392 if (same_type_p (from, to))
1393 /* OK */;
1394 else if (c_cast_p && comp_ptr_ttypes_const (to, from, bounds_either))
1395 /* In a C-style cast, we ignore CV-qualification because we
1396 are allowed to perform a static_cast followed by a
1397 const_cast. */
1398 conv = build_conv (ck_qual, to, conv);
1399 else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
1400 conv = build_conv (ck_qual, to, conv);
1401 else if (expr && string_conv_p (to, expr, 0))
1402 /* converting from string constant to char *. */
1403 conv = build_conv (ck_qual, to, conv);
1404 else if (fnptr_conv_p (to, from))
1405 conv = build_conv (ck_fnptr, to, conv);
1406 /* Allow conversions among compatible ObjC pointer types (base
1407 conversions have been already handled above). */
1408 else if (c_dialect_objc ()
1409 && objc_compare_types (to, from, -4, NULL_TREE))
1410 conv = build_conv (ck_ptr, to, conv);
1411 else if (ptr_reasonably_similar (to_pointee, from_pointee))
1412 {
1413 conv = build_conv (ck_ptr, to, conv);
1414 conv->bad_p = true;
1415 }
1416 else
1417 return NULL;
1418
1419 from = to;
1420 }
1421 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
1422 {
1423 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
1424 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
1425 tree fbase = class_of_this_parm (fromfn);
1426 tree tbase = class_of_this_parm (tofn);
1427
1428 if (!DERIVED_FROM_P (fbase, tbase))
1429 return NULL;
1430
1431 tree fstat = static_fn_type (fromfn);
1432 tree tstat = static_fn_type (tofn);
1433 if (same_type_p (tstat, fstat)
1434 || fnptr_conv_p (tstat, fstat))
1435 /* OK */;
1436 else
1437 return NULL;
1438
1439 if (!same_type_p (fbase, tbase))
1440 {
1441 from = build_memfn_type (fstat,
1442 tbase,
1443 cp_type_quals (tbase),
1444 type_memfn_rqual (tofn));
1445 from = build_ptrmemfunc_type (build_pointer_type (from));
1446 conv = build_conv (ck_pmem, from, conv);
1447 conv->base_p = true;
1448 }
1449 if (fnptr_conv_p (tstat, fstat))
1450 conv = build_conv (ck_fnptr, to, conv);
1451 }
1452 else if (tcode == BOOLEAN_TYPE)
1453 {
1454 /* [conv.bool]
1455
1456 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
1457 to member type can be converted to a prvalue of type bool. ...
1458 For direct-initialization (8.5 [dcl.init]), a prvalue of type
1459 std::nullptr_t can be converted to a prvalue of type bool; */
1460 if (ARITHMETIC_TYPE_P (from)
1461 || UNSCOPED_ENUM_P (from)
1462 || fcode == POINTER_TYPE
1463 || TYPE_PTRMEM_P (from)
1464 || NULLPTR_TYPE_P (from))
1465 {
1466 conv = build_conv (ck_std, to, conv);
1467 if (fcode == POINTER_TYPE
1468 || TYPE_PTRDATAMEM_P (from)
1469 || (TYPE_PTRMEMFUNC_P (from)
1470 && conv->rank < cr_pbool)
1471 || NULLPTR_TYPE_P (from))
1472 conv->rank = cr_pbool;
1473 if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING))
1474 conv->bad_p = true;
1475 if (flags & LOOKUP_NO_NARROWING)
1476 conv->check_narrowing = true;
1477 return conv;
1478 }
1479
1480 return NULL;
1481 }
1482 /* We don't check for ENUMERAL_TYPE here because there are no standard
1483 conversions to enum type. */
1484 /* As an extension, allow conversion to complex type. */
1485 else if (ARITHMETIC_TYPE_P (to))
1486 {
1487 if (! (INTEGRAL_CODE_P (fcode)
1488 || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL)))
1489 || SCOPED_ENUM_P (from))
1490 return NULL;
1491
1492 /* If we're parsing an enum with no fixed underlying type, we're
1493 dealing with an incomplete type, which renders the conversion
1494 ill-formed. */
1495 if (!COMPLETE_TYPE_P (from))
1496 return NULL;
1497
1498 conv = build_conv (ck_std, to, conv);
1499
1500 tree underlying_type = NULL_TREE;
1501 if (TREE_CODE (from) == ENUMERAL_TYPE
1502 && ENUM_FIXED_UNDERLYING_TYPE_P (from))
1503 underlying_type = ENUM_UNDERLYING_TYPE (from);
1504
1505 /* Give this a better rank if it's a promotion.
1506
1507 To handle CWG 1601, also bump the rank if we are converting
1508 an enumeration with a fixed underlying type to the underlying
1509 type. */
1510 if ((same_type_p (to, type_promotes_to (from))
1511 || (underlying_type && same_type_p (to, underlying_type)))
1512 && next_conversion (conv)->rank <= cr_promotion)
1513 conv->rank = cr_promotion;
1514 }
1515 else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
1516 && vector_types_convertible_p (from, to, false))
1517 return build_conv (ck_std, to, conv);
1518 else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
1519 && is_properly_derived_from (from, to))
1520 {
1521 if (conv->kind == ck_rvalue)
1522 conv = next_conversion (conv);
1523 conv = build_conv (ck_base, to, conv);
1524 /* The derived-to-base conversion indicates the initialization
1525 of a parameter with base type from an object of a derived
1526 type. A temporary object is created to hold the result of
1527 the conversion unless we're binding directly to a reference. */
1528 conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
1529 if (flags & LOOKUP_PREFER_RVALUE)
1530 /* Tell convert_like to set LOOKUP_PREFER_RVALUE. */
1531 conv->rvaluedness_matches_p = true;
1532 /* If we're performing copy-initialization, remember to skip
1533 explicit constructors. */
1534 if (flags & LOOKUP_ONLYCONVERTING)
1535 conv->copy_init_p = true;
1536 }
1537 else
1538 return NULL;
1539
1540 if (flags & LOOKUP_NO_NARROWING)
1541 conv->check_narrowing = true;
1542
1543 return conv;
1544 }
1545
1546 /* Returns nonzero if T1 is reference-related to T2. */
1547
1548 bool
1549 reference_related_p (tree t1, tree t2)
1550 {
1551 if (t1 == error_mark_node || t2 == error_mark_node)
1552 return false;
1553
1554 t1 = TYPE_MAIN_VARIANT (t1);
1555 t2 = TYPE_MAIN_VARIANT (t2);
1556
1557 /* [dcl.init.ref]
1558
1559 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
1560 to "cv2 T2" if T1 is similar to T2, or T1 is a base class of T2. */
1561 return (similar_type_p (t1, t2)
1562 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1563 && DERIVED_FROM_P (t1, t2)));
1564 }
1565
1566 /* Returns nonzero if T1 is reference-compatible with T2. */
1567
1568 bool
1569 reference_compatible_p (tree t1, tree t2)
1570 {
1571 /* [dcl.init.ref]
1572
1573 "cv1 T1" is reference compatible with "cv2 T2" if
1574 a prvalue of type "pointer to cv2 T2" can be converted to the type
1575 "pointer to cv1 T1" via a standard conversion sequence. */
1576 tree ptype1 = build_pointer_type (t1);
1577 tree ptype2 = build_pointer_type (t2);
1578 conversion *conv = standard_conversion (ptype1, ptype2, NULL_TREE,
1579 /*c_cast_p=*/false, 0, tf_none);
1580 if (!conv || conv->bad_p)
1581 return false;
1582 return true;
1583 }
1584
1585 /* Return true if converting FROM to TO would involve a qualification
1586 conversion. */
1587
1588 static bool
1589 involves_qualification_conversion_p (tree to, tree from)
1590 {
1591 /* If we're not convering a pointer to another one, we won't get
1592 a qualification conversion. */
1593 if (!((TYPE_PTR_P (to) && TYPE_PTR_P (from))
1594 || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from))))
1595 return false;
1596
1597 conversion *conv = standard_conversion (to, from, NULL_TREE,
1598 /*c_cast_p=*/false, 0, tf_none);
1599 for (conversion *t = conv; t; t = next_conversion (t))
1600 if (t->kind == ck_qual)
1601 return true;
1602
1603 return false;
1604 }
1605
1606 /* A reference of the indicated TYPE is being bound directly to the
1607 expression represented by the implicit conversion sequence CONV.
1608 Return a conversion sequence for this binding. */
1609
1610 static conversion *
1611 direct_reference_binding (tree type, conversion *conv)
1612 {
1613 tree t;
1614
1615 gcc_assert (TYPE_REF_P (type));
1616 gcc_assert (!TYPE_REF_P (conv->type));
1617
1618 t = TREE_TYPE (type);
1619
1620 if (conv->kind == ck_identity)
1621 /* Mark the identity conv as to not decay to rvalue. */
1622 conv->rvaluedness_matches_p = true;
1623
1624 /* [over.ics.rank]
1625
1626 When a parameter of reference type binds directly
1627 (_dcl.init.ref_) to an argument expression, the implicit
1628 conversion sequence is the identity conversion, unless the
1629 argument expression has a type that is a derived class of the
1630 parameter type, in which case the implicit conversion sequence is
1631 a derived-to-base Conversion.
1632
1633 If the parameter binds directly to the result of applying a
1634 conversion function to the argument expression, the implicit
1635 conversion sequence is a user-defined conversion sequence
1636 (_over.ics.user_), with the second standard conversion sequence
1637 either an identity conversion or, if the conversion function
1638 returns an entity of a type that is a derived class of the
1639 parameter type, a derived-to-base conversion. */
1640 if (is_properly_derived_from (conv->type, t))
1641 {
1642 /* Represent the derived-to-base conversion. */
1643 conv = build_conv (ck_base, t, conv);
1644 /* We will actually be binding to the base-class subobject in
1645 the derived class, so we mark this conversion appropriately.
1646 That way, convert_like knows not to generate a temporary. */
1647 conv->need_temporary_p = false;
1648 }
1649 else if (involves_qualification_conversion_p (t, conv->type))
1650 /* Represent the qualification conversion. After DR 2352
1651 #1 and #2 were indistinguishable conversion sequences:
1652
1653 void f(int*); // #1
1654 void f(const int* const &); // #2
1655 void g(int* p) { f(p); }
1656
1657 because the types "int *" and "const int *const" are
1658 reference-related and we were binding both directly and they
1659 had the same rank. To break it up, we add a ck_qual under the
1660 ck_ref_bind so that conversion sequence ranking chooses #1. */
1661 conv = build_conv (ck_qual, t, conv);
1662
1663 return build_conv (ck_ref_bind, type, conv);
1664 }
1665
1666 /* Returns the conversion path from type FROM to reference type TO for
1667 purposes of reference binding. For lvalue binding, either pass a
1668 reference type to FROM or an lvalue expression to EXPR. If the
1669 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1670 the conversion returned. If C_CAST_P is true, this
1671 conversion is coming from a C-style cast. */
1672
1673 static conversion *
1674 reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags,
1675 tsubst_flags_t complain)
1676 {
1677 conversion *conv = NULL;
1678 tree to = TREE_TYPE (rto);
1679 tree from = rfrom;
1680 tree tfrom;
1681 bool related_p;
1682 bool compatible_p;
1683 cp_lvalue_kind gl_kind;
1684 bool is_lvalue;
1685
1686 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1687 {
1688 expr = instantiate_type (to, expr, tf_none);
1689 if (expr == error_mark_node)
1690 return NULL;
1691 from = TREE_TYPE (expr);
1692 }
1693
1694 bool copy_list_init = false;
1695 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1696 {
1697 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
1698 /* DR 1288: Otherwise, if the initializer list has a single element
1699 of type E and ... [T's] referenced type is reference-related to E,
1700 the object or reference is initialized from that element...
1701
1702 ??? With P0388R4, we should bind 't' directly to U{}:
1703 using U = A[2];
1704 A (&&t)[] = {U{}};
1705 because A[] and A[2] are reference-related. But we don't do it
1706 because grok_reference_init has deduced the array size (to 1), and
1707 A[1] and A[2] aren't reference-related. */
1708 if (CONSTRUCTOR_NELTS (expr) == 1)
1709 {
1710 tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1711 if (error_operand_p (elt))
1712 return NULL;
1713 tree etype = TREE_TYPE (elt);
1714 if (reference_related_p (to, etype))
1715 {
1716 expr = elt;
1717 from = etype;
1718 goto skip;
1719 }
1720 }
1721 /* Otherwise, if T is a reference type, a prvalue temporary of the type
1722 referenced by T is copy-list-initialized, and the reference is bound
1723 to that temporary. */
1724 copy_list_init = true;
1725 skip:;
1726 }
1727
1728 if (TYPE_REF_P (from))
1729 {
1730 from = TREE_TYPE (from);
1731 if (!TYPE_REF_IS_RVALUE (rfrom)
1732 || TREE_CODE (from) == FUNCTION_TYPE)
1733 gl_kind = clk_ordinary;
1734 else
1735 gl_kind = clk_rvalueref;
1736 }
1737 else if (expr)
1738 gl_kind = lvalue_kind (expr);
1739 else if (CLASS_TYPE_P (from)
1740 || TREE_CODE (from) == ARRAY_TYPE)
1741 gl_kind = clk_class;
1742 else
1743 gl_kind = clk_none;
1744
1745 /* Don't allow a class prvalue when LOOKUP_NO_TEMP_BIND. */
1746 if ((flags & LOOKUP_NO_TEMP_BIND)
1747 && (gl_kind & clk_class))
1748 gl_kind = clk_none;
1749
1750 /* Same mask as real_lvalue_p. */
1751 is_lvalue = gl_kind && !(gl_kind & (clk_rvalueref|clk_class));
1752
1753 tfrom = from;
1754 if ((gl_kind & clk_bitfield) != 0)
1755 tfrom = unlowered_expr_type (expr);
1756
1757 /* Figure out whether or not the types are reference-related and
1758 reference compatible. We have to do this after stripping
1759 references from FROM. */
1760 related_p = reference_related_p (to, tfrom);
1761 /* If this is a C cast, first convert to an appropriately qualified
1762 type, so that we can later do a const_cast to the desired type. */
1763 if (related_p && c_cast_p
1764 && !at_least_as_qualified_p (to, tfrom))
1765 to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1766 compatible_p = reference_compatible_p (to, tfrom);
1767
1768 /* Directly bind reference when target expression's type is compatible with
1769 the reference and expression is an lvalue. In DR391, the wording in
1770 [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1771 const and rvalue references to rvalues of compatible class type.
1772 We should also do direct bindings for non-class xvalues. */
1773 if ((related_p || compatible_p) && gl_kind)
1774 {
1775 /* [dcl.init.ref]
1776
1777 If the initializer expression
1778
1779 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1780 is reference-compatible with "cv2 T2,"
1781
1782 the reference is bound directly to the initializer expression
1783 lvalue.
1784
1785 [...]
1786 If the initializer expression is an rvalue, with T2 a class type,
1787 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1788 is bound to the object represented by the rvalue or to a sub-object
1789 within that object. */
1790
1791 conv = build_identity_conv (tfrom, expr);
1792 conv = direct_reference_binding (rto, conv);
1793
1794 if (TYPE_REF_P (rfrom))
1795 /* Handle rvalue reference to function properly. */
1796 conv->rvaluedness_matches_p
1797 = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom));
1798 else
1799 conv->rvaluedness_matches_p
1800 = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
1801
1802 if ((gl_kind & clk_bitfield) != 0
1803 || ((gl_kind & clk_packed) != 0 && !TYPE_PACKED (to)))
1804 /* For the purposes of overload resolution, we ignore the fact
1805 this expression is a bitfield or packed field. (In particular,
1806 [over.ics.ref] says specifically that a function with a
1807 non-const reference parameter is viable even if the
1808 argument is a bitfield.)
1809
1810 However, when we actually call the function we must create
1811 a temporary to which to bind the reference. If the
1812 reference is volatile, or isn't const, then we cannot make
1813 a temporary, so we just issue an error when the conversion
1814 actually occurs. */
1815 conv->need_temporary_p = true;
1816
1817 /* Don't allow binding of lvalues (other than function lvalues) to
1818 rvalue references. */
1819 if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
1820 && TREE_CODE (to) != FUNCTION_TYPE)
1821 conv->bad_p = true;
1822
1823 /* Nor the reverse. */
1824 if (!is_lvalue && !TYPE_REF_IS_RVALUE (rto)
1825 /* Unless it's really an lvalue. */
1826 && !(cxx_dialect >= cxx20
1827 && (gl_kind & clk_implicit_rval))
1828 && (!CP_TYPE_CONST_NON_VOLATILE_P (to)
1829 || (flags & LOOKUP_NO_RVAL_BIND))
1830 && TREE_CODE (to) != FUNCTION_TYPE)
1831 conv->bad_p = true;
1832
1833 if (!compatible_p)
1834 conv->bad_p = true;
1835
1836 return conv;
1837 }
1838 /* [class.conv.fct] A conversion function is never used to convert a
1839 (possibly cv-qualified) object to the (possibly cv-qualified) same
1840 object type (or a reference to it), to a (possibly cv-qualified) base
1841 class of that type (or a reference to it).... */
1842 else if (CLASS_TYPE_P (from) && !related_p
1843 && !(flags & LOOKUP_NO_CONVERSION))
1844 {
1845 /* [dcl.init.ref]
1846
1847 If the initializer expression
1848
1849 -- has a class type (i.e., T2 is a class type) can be
1850 implicitly converted to an lvalue of type "cv3 T3," where
1851 "cv1 T1" is reference-compatible with "cv3 T3". (this
1852 conversion is selected by enumerating the applicable
1853 conversion functions (_over.match.ref_) and choosing the
1854 best one through overload resolution. (_over.match_).
1855
1856 the reference is bound to the lvalue result of the conversion
1857 in the second case. */
1858 z_candidate *cand = build_user_type_conversion_1 (rto, expr, flags,
1859 complain);
1860 if (cand)
1861 return cand->second_conv;
1862 }
1863
1864 /* From this point on, we conceptually need temporaries, even if we
1865 elide them. Only the cases above are "direct bindings". */
1866 if (flags & LOOKUP_NO_TEMP_BIND)
1867 return NULL;
1868
1869 /* [over.ics.rank]
1870
1871 When a parameter of reference type is not bound directly to an
1872 argument expression, the conversion sequence is the one required
1873 to convert the argument expression to the underlying type of the
1874 reference according to _over.best.ics_. Conceptually, this
1875 conversion sequence corresponds to copy-initializing a temporary
1876 of the underlying type with the argument expression. Any
1877 difference in top-level cv-qualification is subsumed by the
1878 initialization itself and does not constitute a conversion. */
1879
1880 /* [dcl.init.ref]
1881
1882 Otherwise, the reference shall be an lvalue reference to a
1883 non-volatile const type, or the reference shall be an rvalue
1884 reference.
1885
1886 We try below to treat this as a bad conversion to improve diagnostics,
1887 but if TO is an incomplete class, we need to reject this conversion
1888 now to avoid unnecessary instantiation. */
1889 if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto)
1890 && !COMPLETE_TYPE_P (to))
1891 return NULL;
1892
1893 /* We're generating a temporary now, but don't bind any more in the
1894 conversion (specifically, don't slice the temporary returned by a
1895 conversion operator). */
1896 flags |= LOOKUP_NO_TEMP_BIND;
1897
1898 /* Core issue 899: When [copy-]initializing a temporary to be bound
1899 to the first parameter of a copy constructor (12.8) called with
1900 a single argument in the context of direct-initialization,
1901 explicit conversion functions are also considered.
1902
1903 So don't set LOOKUP_ONLYCONVERTING in that case. */
1904 if (!(flags & LOOKUP_COPY_PARM))
1905 flags |= LOOKUP_ONLYCONVERTING;
1906
1907 if (!conv)
1908 conv = implicit_conversion (to, from, expr, c_cast_p,
1909 flags, complain);
1910 if (!conv)
1911 return NULL;
1912
1913 if (conv->user_conv_p)
1914 {
1915 if (copy_list_init)
1916 /* Remember this was copy-list-initialization. */
1917 conv->need_temporary_p = true;
1918
1919 /* If initializing the temporary used a conversion function,
1920 recalculate the second conversion sequence. */
1921 for (conversion *t = conv; t; t = next_conversion (t))
1922 if (t->kind == ck_user
1923 && DECL_CONV_FN_P (t->cand->fn))
1924 {
1925 tree ftype = TREE_TYPE (TREE_TYPE (t->cand->fn));
1926 /* A prvalue of non-class type is cv-unqualified. */
1927 if (!TYPE_REF_P (ftype) && !CLASS_TYPE_P (ftype))
1928 ftype = cv_unqualified (ftype);
1929 int sflags = (flags|LOOKUP_NO_CONVERSION)&~LOOKUP_NO_TEMP_BIND;
1930 conversion *new_second
1931 = reference_binding (rto, ftype, NULL_TREE, c_cast_p,
1932 sflags, complain);
1933 if (!new_second)
1934 return NULL;
1935 return merge_conversion_sequences (t, new_second);
1936 }
1937 }
1938
1939 conv = build_conv (ck_ref_bind, rto, conv);
1940 /* This reference binding, unlike those above, requires the
1941 creation of a temporary. */
1942 conv->need_temporary_p = true;
1943 conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
1944
1945 /* [dcl.init.ref]
1946
1947 Otherwise, the reference shall be an lvalue reference to a
1948 non-volatile const type, or the reference shall be an rvalue
1949 reference. */
1950 if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto))
1951 conv->bad_p = true;
1952
1953 /* [dcl.init.ref]
1954
1955 Otherwise, a temporary of type "cv1 T1" is created and
1956 initialized from the initializer expression using the rules for a
1957 non-reference copy initialization. If T1 is reference-related to
1958 T2, cv1 must be the same cv-qualification as, or greater
1959 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1960 if (related_p && !at_least_as_qualified_p (to, from))
1961 conv->bad_p = true;
1962
1963 return conv;
1964 }
1965
1966 /* Returns the implicit conversion sequence (see [over.ics]) from type
1967 FROM to type TO. The optional expression EXPR may affect the
1968 conversion. FLAGS are the usual overloading flags. If C_CAST_P is
1969 true, this conversion is coming from a C-style cast. */
1970
1971 static conversion *
1972 implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
1973 int flags, tsubst_flags_t complain)
1974 {
1975 conversion *conv;
1976
1977 if (from == error_mark_node || to == error_mark_node
1978 || expr == error_mark_node)
1979 return NULL;
1980
1981 /* Other flags only apply to the primary function in overload
1982 resolution, or after we've chosen one. */
1983 flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM
1984 |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_PREFER_RVALUE
1985 |LOOKUP_NO_NARROWING|LOOKUP_PROTECT|LOOKUP_NO_NON_INTEGRAL);
1986
1987 /* FIXME: actually we don't want warnings either, but we can't just
1988 have 'complain &= ~(tf_warning|tf_error)' because it would cause
1989 the regression of, eg, g++.old-deja/g++.benjamin/16077.C.
1990 We really ought not to issue that warning until we've committed
1991 to that conversion. */
1992 complain &= ~tf_error;
1993
1994 /* Call reshape_init early to remove redundant braces. */
1995 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr)
1996 && CLASS_TYPE_P (to)
1997 && COMPLETE_TYPE_P (complete_type (to))
1998 && !CLASSTYPE_NON_AGGREGATE (to))
1999 {
2000 expr = reshape_init (to, expr, complain);
2001 if (expr == error_mark_node)
2002 return NULL;
2003 from = TREE_TYPE (expr);
2004 }
2005
2006 if (TYPE_REF_P (to))
2007 conv = reference_binding (to, from, expr, c_cast_p, flags, complain);
2008 else
2009 conv = standard_conversion (to, from, expr, c_cast_p, flags, complain);
2010
2011 if (conv)
2012 return conv;
2013
2014 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
2015 {
2016 if (is_std_init_list (to) && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2017 return build_list_conv (to, expr, flags, complain);
2018
2019 /* As an extension, allow list-initialization of _Complex. */
2020 if (TREE_CODE (to) == COMPLEX_TYPE
2021 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2022 {
2023 conv = build_complex_conv (to, expr, flags, complain);
2024 if (conv)
2025 return conv;
2026 }
2027
2028 /* Allow conversion from an initializer-list with one element to a
2029 scalar type. */
2030 if (SCALAR_TYPE_P (to))
2031 {
2032 int nelts = CONSTRUCTOR_NELTS (expr);
2033 tree elt;
2034
2035 if (nelts == 0)
2036 elt = build_value_init (to, tf_none);
2037 else if (nelts == 1 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2038 elt = CONSTRUCTOR_ELT (expr, 0)->value;
2039 else
2040 elt = error_mark_node;
2041
2042 conv = implicit_conversion (to, TREE_TYPE (elt), elt,
2043 c_cast_p, flags, complain);
2044 if (conv)
2045 {
2046 conv->check_narrowing = true;
2047 if (BRACE_ENCLOSED_INITIALIZER_P (elt))
2048 /* Too many levels of braces, i.e. '{{1}}'. */
2049 conv->bad_p = true;
2050 return conv;
2051 }
2052 }
2053 else if (TREE_CODE (to) == ARRAY_TYPE)
2054 return build_array_conv (to, expr, flags, complain);
2055 }
2056
2057 if (expr != NULL_TREE
2058 && (MAYBE_CLASS_TYPE_P (from)
2059 || MAYBE_CLASS_TYPE_P (to))
2060 && (flags & LOOKUP_NO_CONVERSION) == 0)
2061 {
2062 struct z_candidate *cand;
2063
2064 if (CLASS_TYPE_P (to)
2065 && BRACE_ENCLOSED_INITIALIZER_P (expr)
2066 && !CLASSTYPE_NON_AGGREGATE (complete_type (to)))
2067 return build_aggr_conv (to, expr, flags, complain);
2068
2069 cand = build_user_type_conversion_1 (to, expr, flags, complain);
2070 if (cand)
2071 {
2072 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
2073 && CONSTRUCTOR_NELTS (expr) == 1
2074 && !is_list_ctor (cand->fn))
2075 {
2076 /* "If C is not an initializer-list constructor and the
2077 initializer list has a single element of type cv U, where U is
2078 X or a class derived from X, the implicit conversion sequence
2079 has Exact Match rank if U is X, or Conversion rank if U is
2080 derived from X." */
2081 tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
2082 tree elttype = TREE_TYPE (elt);
2083 if (reference_related_p (to, elttype))
2084 return implicit_conversion (to, elttype, elt,
2085 c_cast_p, flags, complain);
2086 }
2087 conv = cand->second_conv;
2088 }
2089
2090 /* We used to try to bind a reference to a temporary here, but that
2091 is now handled after the recursive call to this function at the end
2092 of reference_binding. */
2093 return conv;
2094 }
2095
2096 return NULL;
2097 }
2098
2099 /* Like implicit_conversion, but return NULL if the conversion is bad.
2100
2101 This is not static so that check_non_deducible_conversion can call it within
2102 add_template_candidate_real as part of overload resolution; it should not be
2103 called outside of overload resolution. */
2104
2105 conversion *
2106 good_conversion (tree to, tree from, tree expr,
2107 int flags, tsubst_flags_t complain)
2108 {
2109 conversion *c = implicit_conversion (to, from, expr, /*cast*/false,
2110 flags, complain);
2111 if (c && c->bad_p)
2112 c = NULL;
2113 return c;
2114 }
2115
2116 /* Add a new entry to the list of candidates. Used by the add_*_candidate
2117 functions. ARGS will not be changed until a single candidate is
2118 selected. */
2119
2120 static struct z_candidate *
2121 add_candidate (struct z_candidate **candidates,
2122 tree fn, tree first_arg, const vec<tree, va_gc> *args,
2123 size_t num_convs, conversion **convs,
2124 tree access_path, tree conversion_path,
2125 int viable, struct rejection_reason *reason,
2126 int flags)
2127 {
2128 struct z_candidate *cand = (struct z_candidate *)
2129 conversion_obstack_alloc (sizeof (struct z_candidate));
2130
2131 cand->fn = fn;
2132 cand->first_arg = first_arg;
2133 cand->args = args;
2134 cand->convs = convs;
2135 cand->num_convs = num_convs;
2136 cand->access_path = access_path;
2137 cand->conversion_path = conversion_path;
2138 cand->viable = viable;
2139 cand->reason = reason;
2140 cand->next = *candidates;
2141 cand->flags = flags;
2142 *candidates = cand;
2143
2144 if (convs && cand->reversed ())
2145 /* Swap the conversions for comparison in joust; we'll swap them back
2146 before build_over_call. */
2147 std::swap (convs[0], convs[1]);
2148
2149 return cand;
2150 }
2151
2152 /* Return the number of remaining arguments in the parameter list
2153 beginning with ARG. */
2154
2155 int
2156 remaining_arguments (tree arg)
2157 {
2158 int n;
2159
2160 for (n = 0; arg != NULL_TREE && arg != void_list_node;
2161 arg = TREE_CHAIN (arg))
2162 n++;
2163
2164 return n;
2165 }
2166
2167 /* [over.match.copy]: When initializing a temporary object (12.2) to be bound
2168 to the first parameter of a constructor where the parameter is of type
2169 "reference to possibly cv-qualified T" and the constructor is called with a
2170 single argument in the context of direct-initialization of an object of type
2171 "cv2 T", explicit conversion functions are also considered.
2172
2173 So set LOOKUP_COPY_PARM to let reference_binding know that
2174 it's being called in that context. */
2175
2176 int
2177 conv_flags (int i, int nargs, tree fn, tree arg, int flags)
2178 {
2179 int lflags = flags;
2180 tree t;
2181 if (i == 0 && nargs == 1 && DECL_CONSTRUCTOR_P (fn)
2182 && (t = FUNCTION_FIRST_USER_PARMTYPE (fn))
2183 && (same_type_ignoring_top_level_qualifiers_p
2184 (non_reference (TREE_VALUE (t)), DECL_CONTEXT (fn))))
2185 {
2186 if (!(flags & LOOKUP_ONLYCONVERTING))
2187 lflags |= LOOKUP_COPY_PARM;
2188 if ((flags & LOOKUP_LIST_INIT_CTOR)
2189 && BRACE_ENCLOSED_INITIALIZER_P (arg))
2190 lflags |= LOOKUP_NO_CONVERSION;
2191 }
2192 else
2193 lflags |= LOOKUP_ONLYCONVERTING;
2194
2195 return lflags;
2196 }
2197
2198 /* Create an overload candidate for the function or method FN called
2199 with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
2200 FLAGS is passed on to implicit_conversion.
2201
2202 This does not change ARGS.
2203
2204 CTYPE, if non-NULL, is the type we want to pretend this function
2205 comes from for purposes of overload resolution. */
2206
2207 static struct z_candidate *
2208 add_function_candidate (struct z_candidate **candidates,
2209 tree fn, tree ctype, tree first_arg,
2210 const vec<tree, va_gc> *args, tree access_path,
2211 tree conversion_path, int flags,
2212 conversion **convs,
2213 tsubst_flags_t complain)
2214 {
2215 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
2216 int i, len;
2217 tree parmnode;
2218 tree orig_first_arg = first_arg;
2219 int skip;
2220 int viable = 1;
2221 struct rejection_reason *reason = NULL;
2222
2223 /* At this point we should not see any functions which haven't been
2224 explicitly declared, except for friend functions which will have
2225 been found using argument dependent lookup. */
2226 gcc_assert (!DECL_ANTICIPATED (fn) || DECL_HIDDEN_FRIEND_P (fn));
2227
2228 /* The `this', `in_chrg' and VTT arguments to constructors are not
2229 considered in overload resolution. */
2230 if (DECL_CONSTRUCTOR_P (fn))
2231 {
2232 if (ctor_omit_inherited_parms (fn))
2233 /* Bring back parameters omitted from an inherited ctor. */
2234 parmlist = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
2235 else
2236 parmlist = skip_artificial_parms_for (fn, parmlist);
2237 skip = num_artificial_parms_for (fn);
2238 if (skip > 0 && first_arg != NULL_TREE)
2239 {
2240 --skip;
2241 first_arg = NULL_TREE;
2242 }
2243 }
2244 else
2245 skip = 0;
2246
2247 len = vec_safe_length (args) - skip + (first_arg != NULL_TREE ? 1 : 0);
2248 if (!convs)
2249 convs = alloc_conversions (len);
2250
2251 /* 13.3.2 - Viable functions [over.match.viable]
2252 First, to be a viable function, a candidate function shall have enough
2253 parameters to agree in number with the arguments in the list.
2254
2255 We need to check this first; otherwise, checking the ICSes might cause
2256 us to produce an ill-formed template instantiation. */
2257
2258 parmnode = parmlist;
2259 for (i = 0; i < len; ++i)
2260 {
2261 if (parmnode == NULL_TREE || parmnode == void_list_node)
2262 break;
2263 parmnode = TREE_CHAIN (parmnode);
2264 }
2265
2266 if ((i < len && parmnode)
2267 || !sufficient_parms_p (parmnode))
2268 {
2269 int remaining = remaining_arguments (parmnode);
2270 viable = 0;
2271 reason = arity_rejection (first_arg, i + remaining, len);
2272 }
2273
2274 /* An inherited constructor (12.6.3 [class.inhctor.init]) that has a first
2275 parameter of type "reference to cv C" (including such a constructor
2276 instantiated from a template) is excluded from the set of candidate
2277 functions when used to construct an object of type D with an argument list
2278 containing a single argument if C is reference-related to D. */
2279 if (viable && len == 1 && parmlist && DECL_CONSTRUCTOR_P (fn)
2280 && flag_new_inheriting_ctors
2281 && DECL_INHERITED_CTOR (fn))
2282 {
2283 tree ptype = non_reference (TREE_VALUE (parmlist));
2284 tree dtype = DECL_CONTEXT (fn);
2285 tree btype = DECL_INHERITED_CTOR_BASE (fn);
2286 if (reference_related_p (ptype, dtype)
2287 && reference_related_p (btype, ptype))
2288 {
2289 viable = false;
2290 reason = inherited_ctor_rejection ();
2291 }
2292 }
2293
2294 /* Second, for a function to be viable, its constraints must be
2295 satisfied. */
2296 if (flag_concepts && viable && !constraints_satisfied_p (fn))
2297 {
2298 reason = constraint_failure ();
2299 viable = false;
2300 }
2301
2302 /* When looking for a function from a subobject from an implicit
2303 copy/move constructor/operator=, don't consider anything that takes (a
2304 reference to) an unrelated type. See c++/44909 and core 1092. */
2305 if (viable && parmlist && (flags & LOOKUP_DEFAULTED))
2306 {
2307 if (DECL_CONSTRUCTOR_P (fn))
2308 i = 1;
2309 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2310 && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
2311 i = 2;
2312 else
2313 i = 0;
2314 if (i && len == i)
2315 {
2316 parmnode = chain_index (i-1, parmlist);
2317 if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
2318 ctype))
2319 viable = 0;
2320 }
2321
2322 /* This only applies at the top level. */
2323 flags &= ~LOOKUP_DEFAULTED;
2324 }
2325
2326 if (! viable)
2327 goto out;
2328
2329 /* Third, for F to be a viable function, there shall exist for each
2330 argument an implicit conversion sequence that converts that argument
2331 to the corresponding parameter of F. */
2332
2333 parmnode = parmlist;
2334
2335 for (i = 0; i < len; ++i)
2336 {
2337 tree argtype, to_type;
2338 tree arg;
2339 conversion *t;
2340 int is_this;
2341
2342 if (parmnode == void_list_node)
2343 break;
2344
2345 if (convs[i])
2346 {
2347 /* Already set during deduction. */
2348 parmnode = TREE_CHAIN (parmnode);
2349 continue;
2350 }
2351
2352 if (i == 0 && first_arg != NULL_TREE)
2353 arg = first_arg;
2354 else
2355 arg = CONST_CAST_TREE (
2356 (*args)[i + skip - (first_arg != NULL_TREE ? 1 : 0)]);
2357 argtype = lvalue_type (arg);
2358
2359 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2360 && ! DECL_CONSTRUCTOR_P (fn));
2361
2362 if (parmnode)
2363 {
2364 tree parmtype = TREE_VALUE (parmnode);
2365
2366 parmnode = TREE_CHAIN (parmnode);
2367
2368 /* The type of the implicit object parameter ('this') for
2369 overload resolution is not always the same as for the
2370 function itself; conversion functions are considered to
2371 be members of the class being converted, and functions
2372 introduced by a using-declaration are considered to be
2373 members of the class that uses them.
2374
2375 Since build_over_call ignores the ICS for the `this'
2376 parameter, we can just change the parm type. */
2377 if (ctype && is_this)
2378 {
2379 parmtype = cp_build_qualified_type
2380 (ctype, cp_type_quals (TREE_TYPE (parmtype)));
2381 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn)))
2382 {
2383 /* If the function has a ref-qualifier, the implicit
2384 object parameter has reference type. */
2385 bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn));
2386 parmtype = cp_build_reference_type (parmtype, rv);
2387 /* The special handling of 'this' conversions in compare_ics
2388 does not apply if there is a ref-qualifier. */
2389 is_this = false;
2390 }
2391 else
2392 {
2393 parmtype = build_pointer_type (parmtype);
2394 /* We don't use build_this here because we don't want to
2395 capture the object argument until we've chosen a
2396 non-static member function. */
2397 arg = build_address (arg);
2398 argtype = lvalue_type (arg);
2399 }
2400 }
2401
2402 int lflags = conv_flags (i, len-skip, fn, arg, flags);
2403
2404 t = implicit_conversion (parmtype, argtype, arg,
2405 /*c_cast_p=*/false, lflags, complain);
2406 to_type = parmtype;
2407 }
2408 else
2409 {
2410 t = build_identity_conv (argtype, arg);
2411 t->ellipsis_p = true;
2412 to_type = argtype;
2413 }
2414
2415 if (t && is_this)
2416 t->this_p = true;
2417
2418 convs[i] = t;
2419 if (! t)
2420 {
2421 viable = 0;
2422 reason = arg_conversion_rejection (first_arg, i, argtype, to_type,
2423 EXPR_LOCATION (arg));
2424 break;
2425 }
2426
2427 if (t->bad_p)
2428 {
2429 viable = -1;
2430 reason = bad_arg_conversion_rejection (first_arg, i, arg, to_type,
2431 EXPR_LOCATION (arg));
2432
2433 }
2434 }
2435
2436 out:
2437 return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
2438 access_path, conversion_path, viable, reason, flags);
2439 }
2440
2441 /* Create an overload candidate for the conversion function FN which will
2442 be invoked for expression OBJ, producing a pointer-to-function which
2443 will in turn be called with the argument list FIRST_ARG/ARGLIST,
2444 and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
2445 passed on to implicit_conversion.
2446
2447 Actually, we don't really care about FN; we care about the type it
2448 converts to. There may be multiple conversion functions that will
2449 convert to that type, and we rely on build_user_type_conversion_1 to
2450 choose the best one; so when we create our candidate, we record the type
2451 instead of the function. */
2452
2453 static struct z_candidate *
2454 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
2455 const vec<tree, va_gc> *arglist,
2456 tree access_path, tree conversion_path,
2457 tsubst_flags_t complain)
2458 {
2459 tree totype = TREE_TYPE (TREE_TYPE (fn));
2460 int i, len, viable, flags;
2461 tree parmlist, parmnode;
2462 conversion **convs;
2463 struct rejection_reason *reason;
2464
2465 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
2466 parmlist = TREE_TYPE (parmlist);
2467 parmlist = TYPE_ARG_TYPES (parmlist);
2468
2469 len = vec_safe_length (arglist) + 1;
2470 convs = alloc_conversions (len);
2471 parmnode = parmlist;
2472 viable = 1;
2473 flags = LOOKUP_IMPLICIT;
2474 reason = NULL;
2475
2476 /* Don't bother looking up the same type twice. */
2477 if (*candidates && (*candidates)->fn == totype)
2478 return NULL;
2479
2480 for (i = 0; i < len; ++i)
2481 {
2482 tree arg, argtype, convert_type = NULL_TREE;
2483 conversion *t;
2484
2485 if (i == 0)
2486 arg = obj;
2487 else
2488 arg = (*arglist)[i - 1];
2489 argtype = lvalue_type (arg);
2490
2491 if (i == 0)
2492 {
2493 t = build_identity_conv (argtype, NULL_TREE);
2494 t = build_conv (ck_user, totype, t);
2495 /* Leave the 'cand' field null; we'll figure out the conversion in
2496 convert_like if this candidate is chosen. */
2497 convert_type = totype;
2498 }
2499 else if (parmnode == void_list_node)
2500 break;
2501 else if (parmnode)
2502 {
2503 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
2504 /*c_cast_p=*/false, flags, complain);
2505 convert_type = TREE_VALUE (parmnode);
2506 }
2507 else
2508 {
2509 t = build_identity_conv (argtype, arg);
2510 t->ellipsis_p = true;
2511 convert_type = argtype;
2512 }
2513
2514 convs[i] = t;
2515 if (! t)
2516 break;
2517
2518 if (t->bad_p)
2519 {
2520 viable = -1;
2521 reason = bad_arg_conversion_rejection (NULL_TREE, i, arg, convert_type,
2522 EXPR_LOCATION (arg));
2523 }
2524
2525 if (i == 0)
2526 continue;
2527
2528 if (parmnode)
2529 parmnode = TREE_CHAIN (parmnode);
2530 }
2531
2532 if (i < len
2533 || ! sufficient_parms_p (parmnode))
2534 {
2535 int remaining = remaining_arguments (parmnode);
2536 viable = 0;
2537 reason = arity_rejection (NULL_TREE, i + remaining, len);
2538 }
2539
2540 return add_candidate (candidates, totype, obj, arglist, len, convs,
2541 access_path, conversion_path, viable, reason, flags);
2542 }
2543
2544 static void
2545 build_builtin_candidate (struct z_candidate **candidates, tree fnname,
2546 tree type1, tree type2, const vec<tree,va_gc> &args,
2547 tree *argtypes, int flags, tsubst_flags_t complain)
2548 {
2549 conversion *t;
2550 conversion **convs;
2551 size_t num_convs;
2552 int viable = 1;
2553 tree types[2];
2554 struct rejection_reason *reason = NULL;
2555
2556 types[0] = type1;
2557 types[1] = type2;
2558
2559 num_convs = args.length ();
2560 convs = alloc_conversions (num_convs);
2561
2562 /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2563 conversion ops are allowed. We handle that here by just checking for
2564 boolean_type_node because other operators don't ask for it. COND_EXPR
2565 also does contextual conversion to bool for the first operand, but we
2566 handle that in build_conditional_expr, and type1 here is operand 2. */
2567 if (type1 != boolean_type_node)
2568 flags |= LOOKUP_ONLYCONVERTING;
2569
2570 for (unsigned i = 0; i < 2 && i < num_convs; ++i)
2571 {
2572 t = implicit_conversion (types[i], argtypes[i], args[i],
2573 /*c_cast_p=*/false, flags, complain);
2574 if (! t)
2575 {
2576 viable = 0;
2577 /* We need something for printing the candidate. */
2578 t = build_identity_conv (types[i], NULL_TREE);
2579 reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2580 types[i], EXPR_LOCATION (args[i]));
2581 }
2582 else if (t->bad_p)
2583 {
2584 viable = 0;
2585 reason = bad_arg_conversion_rejection (NULL_TREE, i, args[i],
2586 types[i],
2587 EXPR_LOCATION (args[i]));
2588 }
2589 convs[i] = t;
2590 }
2591
2592 /* For COND_EXPR we rearranged the arguments; undo that now. */
2593 if (num_convs == 3)
2594 {
2595 convs[2] = convs[1];
2596 convs[1] = convs[0];
2597 t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
2598 /*c_cast_p=*/false, flags,
2599 complain);
2600 if (t)
2601 convs[0] = t;
2602 else
2603 {
2604 viable = 0;
2605 reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2],
2606 boolean_type_node,
2607 EXPR_LOCATION (args[2]));
2608 }
2609 }
2610
2611 add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
2612 num_convs, convs,
2613 /*access_path=*/NULL_TREE,
2614 /*conversion_path=*/NULL_TREE,
2615 viable, reason, flags);
2616 }
2617
2618 static bool
2619 is_complete (tree t)
2620 {
2621 return COMPLETE_TYPE_P (complete_type (t));
2622 }
2623
2624 /* Returns nonzero if TYPE is a promoted arithmetic type. */
2625
2626 static bool
2627 promoted_arithmetic_type_p (tree type)
2628 {
2629 /* [over.built]
2630
2631 In this section, the term promoted integral type is used to refer
2632 to those integral types which are preserved by integral promotion
2633 (including e.g. int and long but excluding e.g. char).
2634 Similarly, the term promoted arithmetic type refers to promoted
2635 integral types plus floating types. */
2636 return ((CP_INTEGRAL_TYPE_P (type)
2637 && same_type_p (type_promotes_to (type), type))
2638 || TREE_CODE (type) == REAL_TYPE);
2639 }
2640
2641 /* Create any builtin operator overload candidates for the operator in
2642 question given the converted operand types TYPE1 and TYPE2. The other
2643 args are passed through from add_builtin_candidates to
2644 build_builtin_candidate.
2645
2646 TYPE1 and TYPE2 may not be permissible, and we must filter them.
2647 If CODE is requires candidates operands of the same type of the kind
2648 of which TYPE1 and TYPE2 are, we add both candidates
2649 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2650
2651 static void
2652 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
2653 enum tree_code code2, tree fnname, tree type1,
2654 tree type2, vec<tree,va_gc> &args, tree *argtypes,
2655 int flags, tsubst_flags_t complain)
2656 {
2657 switch (code)
2658 {
2659 case POSTINCREMENT_EXPR:
2660 case POSTDECREMENT_EXPR:
2661 args[1] = integer_zero_node;
2662 type2 = integer_type_node;
2663 break;
2664 default:
2665 break;
2666 }
2667
2668 switch (code)
2669 {
2670
2671 /* 4 For every pair (T, VQ), where T is an arithmetic type other than bool,
2672 and VQ is either volatile or empty, there exist candidate operator
2673 functions of the form
2674 VQ T& operator++(VQ T&);
2675 T operator++(VQ T&, int);
2676 5 For every pair (T, VQ), where T is an arithmetic type other than bool,
2677 and VQ is either volatile or empty, there exist candidate operator
2678 functions of the form
2679 VQ T& operator--(VQ T&);
2680 T operator--(VQ T&, int);
2681 6 For every pair (T, VQ), where T is a cv-qualified or cv-unqualified object
2682 type, and VQ is either volatile or empty, there exist candidate operator
2683 functions of the form
2684 T*VQ& operator++(T*VQ&);
2685 T*VQ& operator--(T*VQ&);
2686 T* operator++(T*VQ&, int);
2687 T* operator--(T*VQ&, int); */
2688
2689 case POSTDECREMENT_EXPR:
2690 case PREDECREMENT_EXPR:
2691 if (TREE_CODE (type1) == BOOLEAN_TYPE)
2692 return;
2693 /* FALLTHRU */
2694 case POSTINCREMENT_EXPR:
2695 case PREINCREMENT_EXPR:
2696 /* P0002R1, Remove deprecated operator++(bool) added "other than bool"
2697 to p4. */
2698 if (TREE_CODE (type1) == BOOLEAN_TYPE && cxx_dialect >= cxx17)
2699 return;
2700 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
2701 {
2702 type1 = build_reference_type (type1);
2703 break;
2704 }
2705 return;
2706
2707 /* 7 For every cv-qualified or cv-unqualified object type T, there
2708 exist candidate operator functions of the form
2709
2710 T& operator*(T*);
2711
2712
2713 8 For every function type T that does not have cv-qualifiers or
2714 a ref-qualifier, there exist candidate operator functions of the form
2715 T& operator*(T*); */
2716
2717 case INDIRECT_REF:
2718 if (TYPE_PTR_P (type1)
2719 && (TYPE_PTROB_P (type1)
2720 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
2721 break;
2722 return;
2723
2724 /* 9 For every type T, there exist candidate operator functions of the form
2725 T* operator+(T*);
2726
2727 10 For every floating-point or promoted integral type T, there exist
2728 candidate operator functions of the form
2729 T operator+(T);
2730 T operator-(T); */
2731
2732 case UNARY_PLUS_EXPR: /* unary + */
2733 if (TYPE_PTR_P (type1))
2734 break;
2735 /* FALLTHRU */
2736 case NEGATE_EXPR:
2737 if (ARITHMETIC_TYPE_P (type1))
2738 break;
2739 return;
2740
2741 /* 11 For every promoted integral type T, there exist candidate operator
2742 functions of the form
2743 T operator~(T); */
2744
2745 case BIT_NOT_EXPR:
2746 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
2747 break;
2748 return;
2749
2750 /* 12 For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, C1
2751 is the same type as C2 or is a derived class of C2, and T is an object
2752 type or a function type there exist candidate operator functions of the
2753 form
2754 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
2755 where CV12 is the union of CV1 and CV2. */
2756
2757 case MEMBER_REF:
2758 if (TYPE_PTR_P (type1) && TYPE_PTRMEM_P (type2))
2759 {
2760 tree c1 = TREE_TYPE (type1);
2761 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
2762
2763 if (MAYBE_CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
2764 && (TYPE_PTRMEMFUNC_P (type2)
2765 || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
2766 break;
2767 }
2768 return;
2769
2770 /* 13 For every pair of types L and R, where each of L and R is a floating-point
2771 or promoted integral type, there exist candidate operator functions of the
2772 form
2773 LR operator*(L, R);
2774 LR operator/(L, R);
2775 LR operator+(L, R);
2776 LR operator-(L, R);
2777 bool operator<(L, R);
2778 bool operator>(L, R);
2779 bool operator<=(L, R);
2780 bool operator>=(L, R);
2781 bool operator==(L, R);
2782 bool operator!=(L, R);
2783 where LR is the result of the usual arithmetic conversions between
2784 types L and R.
2785
2786 14 For every integral type T there exists a candidate operator function of
2787 the form
2788
2789 std::strong_ordering operator<=>(T, T);
2790
2791 15 For every pair of floating-point types L and R, there exists a candidate
2792 operator function of the form
2793
2794 std::partial_ordering operator<=>(L, R);
2795
2796 16 For every cv-qualified or cv-unqualified object type T there exist
2797 candidate operator functions of the form
2798 T* operator+(T*, std::ptrdiff_t);
2799 T& operator[](T*, std::ptrdiff_t);
2800 T* operator-(T*, std::ptrdiff_t);
2801 T* operator+(std::ptrdiff_t, T*);
2802 T& operator[](std::ptrdiff_t, T*);
2803
2804 17 For every T, where T is a pointer to object type, there exist candidate
2805 operator functions of the form
2806 std::ptrdiff_t operator-(T, T);
2807
2808 18 For every T, where T is an enumeration type or a pointer type, there
2809 exist candidate operator functions of the form
2810 bool operator<(T, T);
2811 bool operator>(T, T);
2812 bool operator<=(T, T);
2813 bool operator>=(T, T);
2814 bool operator==(T, T);
2815 bool operator!=(T, T);
2816 R operator<=>(T, T);
2817
2818 where R is the result type specified in [expr.spaceship].
2819
2820 19 For every T, where T is a pointer-to-member type or std::nullptr_t,
2821 there exist candidate operator functions of the form
2822 bool operator==(T, T);
2823 bool operator!=(T, T); */
2824
2825 case MINUS_EXPR:
2826 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
2827 break;
2828 if (TYPE_PTROB_P (type1)
2829 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2830 {
2831 type2 = ptrdiff_type_node;
2832 break;
2833 }
2834 /* FALLTHRU */
2835 case MULT_EXPR:
2836 case TRUNC_DIV_EXPR:
2837 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2838 break;
2839 return;
2840
2841 /* This isn't exactly what's specified above for operator<=>, but it's
2842 close enough. In particular, we don't care about the return type
2843 specified above; it doesn't participate in overload resolution and it
2844 doesn't affect the semantics of the built-in operator. */
2845 case SPACESHIP_EXPR:
2846 case EQ_EXPR:
2847 case NE_EXPR:
2848 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2849 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)))
2850 break;
2851 if (NULLPTR_TYPE_P (type1) && NULLPTR_TYPE_P (type2))
2852 break;
2853 if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (args[1]))
2854 {
2855 type2 = type1;
2856 break;
2857 }
2858 if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (args[0]))
2859 {
2860 type1 = type2;
2861 break;
2862 }
2863 /* Fall through. */
2864 case LT_EXPR:
2865 case GT_EXPR:
2866 case LE_EXPR:
2867 case GE_EXPR:
2868 case MAX_EXPR:
2869 case MIN_EXPR:
2870 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2871 break;
2872 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2873 break;
2874 if (TREE_CODE (type1) == ENUMERAL_TYPE
2875 && TREE_CODE (type2) == ENUMERAL_TYPE)
2876 break;
2877 if (TYPE_PTR_P (type1)
2878 && null_ptr_cst_p (args[1]))
2879 {
2880 type2 = type1;
2881 break;
2882 }
2883 if (null_ptr_cst_p (args[0])
2884 && TYPE_PTR_P (type2))
2885 {
2886 type1 = type2;
2887 break;
2888 }
2889 return;
2890
2891 case PLUS_EXPR:
2892 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2893 break;
2894 /* FALLTHRU */
2895 case ARRAY_REF:
2896 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
2897 {
2898 type1 = ptrdiff_type_node;
2899 break;
2900 }
2901 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2902 {
2903 type2 = ptrdiff_type_node;
2904 break;
2905 }
2906 return;
2907
2908 /* 18For every pair of promoted integral types L and R, there exist candi-
2909 date operator functions of the form
2910 LR operator%(L, R);
2911 LR operator&(L, R);
2912 LR operator^(L, R);
2913 LR operator|(L, R);
2914 L operator<<(L, R);
2915 L operator>>(L, R);
2916 where LR is the result of the usual arithmetic conversions between
2917 types L and R. */
2918
2919 case TRUNC_MOD_EXPR:
2920 case BIT_AND_EXPR:
2921 case BIT_IOR_EXPR:
2922 case BIT_XOR_EXPR:
2923 case LSHIFT_EXPR:
2924 case RSHIFT_EXPR:
2925 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2926 break;
2927 return;
2928
2929 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
2930 type, VQ is either volatile or empty, and R is a promoted arithmetic
2931 type, there exist candidate operator functions of the form
2932 VQ L& operator=(VQ L&, R);
2933 VQ L& operator*=(VQ L&, R);
2934 VQ L& operator/=(VQ L&, R);
2935 VQ L& operator+=(VQ L&, R);
2936 VQ L& operator-=(VQ L&, R);
2937
2938 20For every pair T, VQ), where T is any type and VQ is either volatile
2939 or empty, there exist candidate operator functions of the form
2940 T*VQ& operator=(T*VQ&, T*);
2941
2942 21For every pair T, VQ), where T is a pointer to member type and VQ is
2943 either volatile or empty, there exist candidate operator functions of
2944 the form
2945 VQ T& operator=(VQ T&, T);
2946
2947 22For every triple T, VQ, I), where T is a cv-qualified or cv-
2948 unqualified complete object type, VQ is either volatile or empty, and
2949 I is a promoted integral type, there exist candidate operator func-
2950 tions of the form
2951 T*VQ& operator+=(T*VQ&, I);
2952 T*VQ& operator-=(T*VQ&, I);
2953
2954 23For every triple L, VQ, R), where L is an integral or enumeration
2955 type, VQ is either volatile or empty, and R is a promoted integral
2956 type, there exist candidate operator functions of the form
2957
2958 VQ L& operator%=(VQ L&, R);
2959 VQ L& operator<<=(VQ L&, R);
2960 VQ L& operator>>=(VQ L&, R);
2961 VQ L& operator&=(VQ L&, R);
2962 VQ L& operator^=(VQ L&, R);
2963 VQ L& operator|=(VQ L&, R); */
2964
2965 case MODIFY_EXPR:
2966 switch (code2)
2967 {
2968 case PLUS_EXPR:
2969 case MINUS_EXPR:
2970 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2971 {
2972 type2 = ptrdiff_type_node;
2973 break;
2974 }
2975 /* FALLTHRU */
2976 case MULT_EXPR:
2977 case TRUNC_DIV_EXPR:
2978 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2979 break;
2980 return;
2981
2982 case TRUNC_MOD_EXPR:
2983 case BIT_AND_EXPR:
2984 case BIT_IOR_EXPR:
2985 case BIT_XOR_EXPR:
2986 case LSHIFT_EXPR:
2987 case RSHIFT_EXPR:
2988 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2989 break;
2990 return;
2991
2992 case NOP_EXPR:
2993 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2994 break;
2995 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2996 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2997 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
2998 || ((TYPE_PTRMEMFUNC_P (type1)
2999 || TYPE_PTR_P (type1))
3000 && null_ptr_cst_p (args[1])))
3001 {
3002 type2 = type1;
3003 break;
3004 }
3005 return;
3006
3007 default:
3008 gcc_unreachable ();
3009 }
3010 type1 = build_reference_type (type1);
3011 break;
3012
3013 case COND_EXPR:
3014 /* [over.built]
3015
3016 For every pair of promoted arithmetic types L and R, there
3017 exist candidate operator functions of the form
3018
3019 LR operator?(bool, L, R);
3020
3021 where LR is the result of the usual arithmetic conversions
3022 between types L and R.
3023
3024 For every type T, where T is a pointer or pointer-to-member
3025 type, there exist candidate operator functions of the form T
3026 operator?(bool, T, T); */
3027
3028 if (promoted_arithmetic_type_p (type1)
3029 && promoted_arithmetic_type_p (type2))
3030 /* That's OK. */
3031 break;
3032
3033 /* Otherwise, the types should be pointers. */
3034 if (!TYPE_PTR_OR_PTRMEM_P (type1) || !TYPE_PTR_OR_PTRMEM_P (type2))
3035 return;
3036
3037 /* We don't check that the two types are the same; the logic
3038 below will actually create two candidates; one in which both
3039 parameter types are TYPE1, and one in which both parameter
3040 types are TYPE2. */
3041 break;
3042
3043 case REALPART_EXPR:
3044 case IMAGPART_EXPR:
3045 if (ARITHMETIC_TYPE_P (type1))
3046 break;
3047 return;
3048
3049 default:
3050 gcc_unreachable ();
3051 }
3052
3053 /* Make sure we don't create builtin candidates with dependent types. */
3054 bool u1 = uses_template_parms (type1);
3055 bool u2 = type2 ? uses_template_parms (type2) : false;
3056 if (u1 || u2)
3057 {
3058 /* Try to recover if one of the types is non-dependent. But if
3059 there's only one type, there's nothing we can do. */
3060 if (!type2)
3061 return;
3062 /* And we lose if both are dependent. */
3063 if (u1 && u2)
3064 return;
3065 /* Or if they have different forms. */
3066 if (TREE_CODE (type1) != TREE_CODE (type2))
3067 return;
3068
3069 if (u1 && !u2)
3070 type1 = type2;
3071 else if (u2 && !u1)
3072 type2 = type1;
3073 }
3074
3075 /* If we're dealing with two pointer types or two enumeral types,
3076 we need candidates for both of them. */
3077 if (type2 && !same_type_p (type1, type2)
3078 && TREE_CODE (type1) == TREE_CODE (type2)
3079 && (TYPE_REF_P (type1)
3080 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3081 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
3082 || TYPE_PTRMEMFUNC_P (type1)
3083 || MAYBE_CLASS_TYPE_P (type1)
3084 || TREE_CODE (type1) == ENUMERAL_TYPE))
3085 {
3086 if (TYPE_PTR_OR_PTRMEM_P (type1))
3087 {
3088 tree cptype = composite_pointer_type (input_location,
3089 type1, type2,
3090 error_mark_node,
3091 error_mark_node,
3092 CPO_CONVERSION,
3093 tf_none);
3094 if (cptype != error_mark_node)
3095 {
3096 build_builtin_candidate
3097 (candidates, fnname, cptype, cptype, args, argtypes,
3098 flags, complain);
3099 return;
3100 }
3101 }
3102
3103 build_builtin_candidate
3104 (candidates, fnname, type1, type1, args, argtypes, flags, complain);
3105 build_builtin_candidate
3106 (candidates, fnname, type2, type2, args, argtypes, flags, complain);
3107 return;
3108 }
3109
3110 build_builtin_candidate
3111 (candidates, fnname, type1, type2, args, argtypes, flags, complain);
3112 }
3113
3114 tree
3115 type_decays_to (tree type)
3116 {
3117 if (TREE_CODE (type) == ARRAY_TYPE)
3118 return build_pointer_type (TREE_TYPE (type));
3119 if (TREE_CODE (type) == FUNCTION_TYPE)
3120 return build_pointer_type (type);
3121 return type;
3122 }
3123
3124 /* There are three conditions of builtin candidates:
3125
3126 1) bool-taking candidates. These are the same regardless of the input.
3127 2) pointer-pair taking candidates. These are generated for each type
3128 one of the input types converts to.
3129 3) arithmetic candidates. According to the standard, we should generate
3130 all of these, but I'm trying not to...
3131
3132 Here we generate a superset of the possible candidates for this particular
3133 case. That is a subset of the full set the standard defines, plus some
3134 other cases which the standard disallows. add_builtin_candidate will
3135 filter out the invalid set. */
3136
3137 static void
3138 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
3139 enum tree_code code2, tree fnname,
3140 vec<tree, va_gc> *argv,
3141 int flags, tsubst_flags_t complain)
3142 {
3143 int ref1;
3144 int enum_p = 0;
3145 tree type, argtypes[3], t;
3146 /* TYPES[i] is the set of possible builtin-operator parameter types
3147 we will consider for the Ith argument. */
3148 vec<tree, va_gc> *types[2];
3149 unsigned ix;
3150 vec<tree, va_gc> &args = *argv;
3151 unsigned len = args.length ();
3152
3153 for (unsigned i = 0; i < len; ++i)
3154 {
3155 if (args[i])
3156 argtypes[i] = unlowered_expr_type (args[i]);
3157 else
3158 argtypes[i] = NULL_TREE;
3159 }
3160
3161 switch (code)
3162 {
3163 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
3164 and VQ is either volatile or empty, there exist candidate operator
3165 functions of the form
3166 VQ T& operator++(VQ T&); */
3167
3168 case POSTINCREMENT_EXPR:
3169 case PREINCREMENT_EXPR:
3170 case POSTDECREMENT_EXPR:
3171 case PREDECREMENT_EXPR:
3172 case MODIFY_EXPR:
3173 ref1 = 1;
3174 break;
3175
3176 /* 24There also exist candidate operator functions of the form
3177 bool operator!(bool);
3178 bool operator&&(bool, bool);
3179 bool operator||(bool, bool); */
3180
3181 case TRUTH_NOT_EXPR:
3182 build_builtin_candidate
3183 (candidates, fnname, boolean_type_node,
3184 NULL_TREE, args, argtypes, flags, complain);
3185 return;
3186
3187 case TRUTH_ORIF_EXPR:
3188 case TRUTH_ANDIF_EXPR:
3189 build_builtin_candidate
3190 (candidates, fnname, boolean_type_node,
3191 boolean_type_node, args, argtypes, flags, complain);
3192 return;
3193
3194 case ADDR_EXPR:
3195 case COMPOUND_EXPR:
3196 case COMPONENT_REF:
3197 case CO_AWAIT_EXPR:
3198 return;
3199
3200 case COND_EXPR:
3201 case EQ_EXPR:
3202 case NE_EXPR:
3203 case LT_EXPR:
3204 case LE_EXPR:
3205 case GT_EXPR:
3206 case GE_EXPR:
3207 case SPACESHIP_EXPR:
3208 enum_p = 1;
3209 /* Fall through. */
3210
3211 default:
3212 ref1 = 0;
3213 }
3214
3215 types[0] = make_tree_vector ();
3216 types[1] = make_tree_vector ();
3217
3218 if (len == 3)
3219 len = 2;
3220 for (unsigned i = 0; i < len; ++i)
3221 {
3222 if (MAYBE_CLASS_TYPE_P (argtypes[i]))
3223 {
3224 tree convs;
3225
3226 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
3227 return;
3228
3229 convs = lookup_conversions (argtypes[i]);
3230
3231 if (code == COND_EXPR)
3232 {
3233 if (lvalue_p (args[i]))
3234 vec_safe_push (types[i], build_reference_type (argtypes[i]));
3235
3236 vec_safe_push (types[i], TYPE_MAIN_VARIANT (argtypes[i]));
3237 }
3238
3239 else if (! convs)
3240 return;
3241
3242 for (; convs; convs = TREE_CHAIN (convs))
3243 {
3244 type = TREE_TYPE (convs);
3245
3246 if (i == 0 && ref1
3247 && (!TYPE_REF_P (type)
3248 || CP_TYPE_CONST_P (TREE_TYPE (type))))
3249 continue;
3250
3251 if (code == COND_EXPR && TYPE_REF_P (type))
3252 vec_safe_push (types[i], type);
3253
3254 type = non_reference (type);
3255 if (i != 0 || ! ref1)
3256 {
3257 type = cv_unqualified (type_decays_to (type));
3258 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
3259 vec_safe_push (types[i], type);
3260 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3261 type = type_promotes_to (type);
3262 }
3263
3264 if (! vec_member (type, types[i]))
3265 vec_safe_push (types[i], type);
3266 }
3267 }
3268 else
3269 {
3270 if (code == COND_EXPR && lvalue_p (args[i]))
3271 vec_safe_push (types[i], build_reference_type (argtypes[i]));
3272 type = non_reference (argtypes[i]);
3273 if (i != 0 || ! ref1)
3274 {
3275 type = cv_unqualified (type_decays_to (type));
3276 if (enum_p && UNSCOPED_ENUM_P (type))
3277 vec_safe_push (types[i], type);
3278 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3279 type = type_promotes_to (type);
3280 }
3281 vec_safe_push (types[i], type);
3282 }
3283 }
3284
3285 /* Run through the possible parameter types of both arguments,
3286 creating candidates with those parameter types. */
3287 FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t)
3288 {
3289 unsigned jx;
3290 tree u;
3291
3292 if (!types[1]->is_empty ())
3293 FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u)
3294 add_builtin_candidate
3295 (candidates, code, code2, fnname, t,
3296 u, args, argtypes, flags, complain);
3297 else
3298 add_builtin_candidate
3299 (candidates, code, code2, fnname, t,
3300 NULL_TREE, args, argtypes, flags, complain);
3301 }
3302
3303 release_tree_vector (types[0]);
3304 release_tree_vector (types[1]);
3305 }
3306
3307
3308 /* If TMPL can be successfully instantiated as indicated by
3309 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
3310
3311 TMPL is the template. EXPLICIT_TARGS are any explicit template
3312 arguments. ARGLIST is the arguments provided at the call-site.
3313 This does not change ARGLIST. The RETURN_TYPE is the desired type
3314 for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
3315 as for add_function_candidate. If an OBJ is supplied, FLAGS and
3316 CTYPE are ignored, and OBJ is as for add_conv_candidate. */
3317
3318 static struct z_candidate*
3319 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
3320 tree ctype, tree explicit_targs, tree first_arg,
3321 const vec<tree, va_gc> *arglist, tree return_type,
3322 tree access_path, tree conversion_path,
3323 int flags, tree obj, unification_kind_t strict,
3324 tsubst_flags_t complain)
3325 {
3326 int ntparms = DECL_NTPARMS (tmpl);
3327 tree targs = make_tree_vec (ntparms);
3328 unsigned int len = vec_safe_length (arglist);
3329 unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
3330 unsigned int skip_without_in_chrg = 0;
3331 tree first_arg_without_in_chrg = first_arg;
3332 tree *args_without_in_chrg;
3333 unsigned int nargs_without_in_chrg;
3334 unsigned int ia, ix;
3335 tree arg;
3336 struct z_candidate *cand;
3337 tree fn;
3338 struct rejection_reason *reason = NULL;
3339 int errs;
3340 conversion **convs = NULL;
3341
3342 /* We don't do deduction on the in-charge parameter, the VTT
3343 parameter or 'this'. */
3344 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
3345 {
3346 if (first_arg_without_in_chrg != NULL_TREE)
3347 first_arg_without_in_chrg = NULL_TREE;
3348 else if (return_type && strict == DEDUCE_CALL)
3349 /* We're deducing for a call to the result of a template conversion
3350 function, so the args don't contain 'this'; leave them alone. */;
3351 else
3352 ++skip_without_in_chrg;
3353 }
3354
3355 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
3356 || DECL_BASE_CONSTRUCTOR_P (tmpl))
3357 && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
3358 {
3359 if (first_arg_without_in_chrg != NULL_TREE)
3360 first_arg_without_in_chrg = NULL_TREE;
3361 else
3362 ++skip_without_in_chrg;
3363 }
3364
3365 if (len < skip_without_in_chrg)
3366 return NULL;
3367
3368 if (DECL_CONSTRUCTOR_P (tmpl) && nargs == 2
3369 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (first_arg),
3370 TREE_TYPE ((*arglist)[0])))
3371 {
3372 /* 12.8/6 says, "A declaration of a constructor for a class X is
3373 ill-formed if its first parameter is of type (optionally cv-qualified)
3374 X and either there are no other parameters or else all other
3375 parameters have default arguments. A member function template is never
3376 instantiated to produce such a constructor signature."
3377
3378 So if we're trying to copy an object of the containing class, don't
3379 consider a template constructor that has a first parameter type that
3380 is just a template parameter, as we would deduce a signature that we
3381 would then reject in the code below. */
3382 if (tree firstparm = FUNCTION_FIRST_USER_PARMTYPE (tmpl))
3383 {
3384 firstparm = TREE_VALUE (firstparm);
3385 if (PACK_EXPANSION_P (firstparm))
3386 firstparm = PACK_EXPANSION_PATTERN (firstparm);
3387 if (TREE_CODE (firstparm) == TEMPLATE_TYPE_PARM)
3388 {
3389 gcc_assert (!explicit_targs);
3390 reason = invalid_copy_with_fn_template_rejection ();
3391 goto fail;
3392 }
3393 }
3394 }
3395
3396 nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
3397 + (len - skip_without_in_chrg));
3398 args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
3399 ia = 0;
3400 if (first_arg_without_in_chrg != NULL_TREE)
3401 {
3402 args_without_in_chrg[ia] = first_arg_without_in_chrg;
3403 ++ia;
3404 }
3405 for (ix = skip_without_in_chrg;
3406 vec_safe_iterate (arglist, ix, &arg);
3407 ++ix)
3408 {
3409 args_without_in_chrg[ia] = arg;
3410 ++ia;
3411 }
3412 gcc_assert (ia == nargs_without_in_chrg);
3413
3414 errs = errorcount+sorrycount;
3415 if (!obj)
3416 convs = alloc_conversions (nargs);
3417 fn = fn_type_unification (tmpl, explicit_targs, targs,
3418 args_without_in_chrg,
3419 nargs_without_in_chrg,
3420 return_type, strict, flags, convs,
3421 false, complain & tf_decltype);
3422
3423 if (fn == error_mark_node)
3424 {
3425 /* Don't repeat unification later if it already resulted in errors. */
3426 if (errorcount+sorrycount == errs)
3427 reason = template_unification_rejection (tmpl, explicit_targs,
3428 targs, args_without_in_chrg,
3429 nargs_without_in_chrg,
3430 return_type, strict, flags);
3431 else
3432 reason = template_unification_error_rejection ();
3433 goto fail;
3434 }
3435
3436 /* Now the explicit specifier might have been deduced; check if this
3437 declaration is explicit. If it is and we're ignoring non-converting
3438 constructors, don't add this function to the set of candidates. */
3439 if ((flags & LOOKUP_ONLYCONVERTING) && DECL_NONCONVERTING_P (fn))
3440 return NULL;
3441
3442 if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
3443 {
3444 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
3445 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
3446 ctype))
3447 {
3448 /* We're trying to produce a constructor with a prohibited signature,
3449 as discussed above; handle here any cases we didn't catch then,
3450 such as X(X<T>). */
3451 reason = invalid_copy_with_fn_template_rejection ();
3452 goto fail;
3453 }
3454 }
3455
3456 if (obj != NULL_TREE)
3457 /* Aha, this is a conversion function. */
3458 cand = add_conv_candidate (candidates, fn, obj, arglist,
3459 access_path, conversion_path, complain);
3460 else
3461 cand = add_function_candidate (candidates, fn, ctype,
3462 first_arg, arglist, access_path,
3463 conversion_path, flags, convs, complain);
3464 if (DECL_TI_TEMPLATE (fn) != tmpl)
3465 /* This situation can occur if a member template of a template
3466 class is specialized. Then, instantiate_template might return
3467 an instantiation of the specialization, in which case the
3468 DECL_TI_TEMPLATE field will point at the original
3469 specialization. For example:
3470
3471 template <class T> struct S { template <class U> void f(U);
3472 template <> void f(int) {}; };
3473 S<double> sd;
3474 sd.f(3);
3475
3476 Here, TMPL will be template <class U> S<double>::f(U).
3477 And, instantiate template will give us the specialization
3478 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
3479 for this will point at template <class T> template <> S<T>::f(int),
3480 so that we can find the definition. For the purposes of
3481 overload resolution, however, we want the original TMPL. */
3482 cand->template_decl = build_template_info (tmpl, targs);
3483 else
3484 cand->template_decl = DECL_TEMPLATE_INFO (fn);
3485 cand->explicit_targs = explicit_targs;
3486
3487 return cand;
3488 fail:
3489 return add_candidate (candidates, tmpl, first_arg, arglist, nargs, NULL,
3490 access_path, conversion_path, 0, reason, flags);
3491 }
3492
3493
3494 static struct z_candidate *
3495 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
3496 tree explicit_targs, tree first_arg,
3497 const vec<tree, va_gc> *arglist, tree return_type,
3498 tree access_path, tree conversion_path, int flags,
3499 unification_kind_t strict, tsubst_flags_t complain)
3500 {
3501 return
3502 add_template_candidate_real (candidates, tmpl, ctype,
3503 explicit_targs, first_arg, arglist,
3504 return_type, access_path, conversion_path,
3505 flags, NULL_TREE, strict, complain);
3506 }
3507
3508 /* Create an overload candidate for the conversion function template TMPL,
3509 returning RETURN_TYPE, which will be invoked for expression OBJ to produce a
3510 pointer-to-function which will in turn be called with the argument list
3511 ARGLIST, and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
3512 passed on to implicit_conversion. */
3513
3514 static struct z_candidate *
3515 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
3516 tree obj,
3517 const vec<tree, va_gc> *arglist,
3518 tree return_type, tree access_path,
3519 tree conversion_path, tsubst_flags_t complain)
3520 {
3521 /* Making this work broke PR 71117 and 85118, so until the committee resolves
3522 core issue 2189, let's disable this candidate if there are any call
3523 operators. */
3524 if (*candidates)
3525 return NULL;
3526
3527 return
3528 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
3529 NULL_TREE, arglist, return_type, access_path,
3530 conversion_path, 0, obj, DEDUCE_CALL,
3531 complain);
3532 }
3533
3534 /* The CANDS are the set of candidates that were considered for
3535 overload resolution. Return the set of viable candidates, or CANDS
3536 if none are viable. If any of the candidates were viable, set
3537 *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
3538 considered viable only if it is strictly viable. */
3539
3540 static struct z_candidate*
3541 splice_viable (struct z_candidate *cands,
3542 bool strict_p,
3543 bool *any_viable_p)
3544 {
3545 struct z_candidate *viable;
3546 struct z_candidate **last_viable;
3547 struct z_candidate **cand;
3548 bool found_strictly_viable = false;
3549
3550 /* Be strict inside templates, since build_over_call won't actually
3551 do the conversions to get pedwarns. */
3552 if (processing_template_decl)
3553 strict_p = true;
3554
3555 viable = NULL;
3556 last_viable = &viable;
3557 *any_viable_p = false;
3558
3559 cand = &cands;
3560 while (*cand)
3561 {
3562 struct z_candidate *c = *cand;
3563 if (!strict_p
3564 && (c->viable == 1 || TREE_CODE (c->fn) == TEMPLATE_DECL))
3565 {
3566 /* Be strict in the presence of a viable candidate. Also if
3567 there are template candidates, so that we get deduction errors
3568 for them instead of silently preferring a bad conversion. */
3569 strict_p = true;
3570 if (viable && !found_strictly_viable)
3571 {
3572 /* Put any spliced near matches back onto the main list so
3573 that we see them if there is no strict match. */
3574 *any_viable_p = false;
3575 *last_viable = cands;
3576 cands = viable;
3577 viable = NULL;
3578 last_viable = &viable;
3579 }
3580 }
3581
3582 if (strict_p ? c->viable == 1 : c->viable)
3583 {
3584 *last_viable = c;
3585 *cand = c->next;
3586 c->next = NULL;
3587 last_viable = &c->next;
3588 *any_viable_p = true;
3589 if (c->viable == 1)
3590 found_strictly_viable = true;
3591 }
3592 else
3593 cand = &c->next;
3594 }
3595
3596 return viable ? viable : cands;
3597 }
3598
3599 static bool
3600 any_strictly_viable (struct z_candidate *cands)
3601 {
3602 for (; cands; cands = cands->next)
3603 if (cands->viable == 1)
3604 return true;
3605 return false;
3606 }
3607
3608 /* OBJ is being used in an expression like "OBJ.f (...)". In other
3609 words, it is about to become the "this" pointer for a member
3610 function call. Take the address of the object. */
3611
3612 static tree
3613 build_this (tree obj)
3614 {
3615 /* In a template, we are only concerned about the type of the
3616 expression, so we can take a shortcut. */
3617 if (processing_template_decl)
3618 return build_address (obj);
3619
3620 return cp_build_addr_expr (obj, tf_warning_or_error);
3621 }
3622
3623 /* Returns true iff functions are equivalent. Equivalent functions are
3624 not '==' only if one is a function-local extern function or if
3625 both are extern "C". */
3626
3627 static inline int
3628 equal_functions (tree fn1, tree fn2)
3629 {
3630 if (TREE_CODE (fn1) != TREE_CODE (fn2))
3631 return 0;
3632 if (TREE_CODE (fn1) == TEMPLATE_DECL)
3633 return fn1 == fn2;
3634 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
3635 || DECL_EXTERN_C_FUNCTION_P (fn1))
3636 return decls_match (fn1, fn2);
3637 return fn1 == fn2;
3638 }
3639
3640 /* Print information about a candidate FN being rejected due to INFO. */
3641
3642 static void
3643 print_conversion_rejection (location_t loc, struct conversion_info *info,
3644 tree fn)
3645 {
3646 tree from = info->from;
3647 if (!TYPE_P (from))
3648 from = lvalue_type (from);
3649 if (info->n_arg == -1)
3650 {
3651 /* Conversion of implicit `this' argument failed. */
3652 if (!TYPE_P (info->from))
3653 /* A bad conversion for 'this' must be discarding cv-quals. */
3654 inform (loc, " passing %qT as %<this%> "
3655 "argument discards qualifiers",
3656 from);
3657 else
3658 inform (loc, " no known conversion for implicit "
3659 "%<this%> parameter from %qH to %qI",
3660 from, info->to_type);
3661 }
3662 else if (!TYPE_P (info->from))
3663 {
3664 if (info->n_arg >= 0)
3665 inform (loc, " conversion of argument %d would be ill-formed:",
3666 info->n_arg + 1);
3667 perform_implicit_conversion (info->to_type, info->from,
3668 tf_warning_or_error);
3669 }
3670 else if (info->n_arg == -2)
3671 /* Conversion of conversion function return value failed. */
3672 inform (loc, " no known conversion from %qH to %qI",
3673 from, info->to_type);
3674 else
3675 {
3676 if (TREE_CODE (fn) == FUNCTION_DECL)
3677 loc = get_fndecl_argument_location (fn, info->n_arg);
3678 inform (loc, " no known conversion for argument %d from %qH to %qI",
3679 info->n_arg + 1, from, info->to_type);
3680 }
3681 }
3682
3683 /* Print information about a candidate with WANT parameters and we found
3684 HAVE. */
3685
3686 static void
3687 print_arity_information (location_t loc, unsigned int have, unsigned int want)
3688 {
3689 inform_n (loc, want,
3690 " candidate expects %d argument, %d provided",
3691 " candidate expects %d arguments, %d provided",
3692 want, have);
3693 }
3694
3695 /* Print information about one overload candidate CANDIDATE. MSGSTR
3696 is the text to print before the candidate itself.
3697
3698 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
3699 to have been run through gettext by the caller. This wart makes
3700 life simpler in print_z_candidates and for the translators. */
3701
3702 static void
3703 print_z_candidate (location_t loc, const char *msgstr,
3704 struct z_candidate *candidate)
3705 {
3706 const char *msg = (msgstr == NULL
3707 ? ""
3708 : ACONCAT ((_(msgstr), " ", NULL)));
3709 tree fn = candidate->fn;
3710 if (flag_new_inheriting_ctors)
3711 fn = strip_inheriting_ctors (fn);
3712 location_t cloc = location_of (fn);
3713
3714 if (identifier_p (fn))
3715 {
3716 cloc = loc;
3717 if (candidate->num_convs == 3)
3718 inform (cloc, "%s%<%D(%T, %T, %T)%> (built-in)", msg, fn,
3719 candidate->convs[0]->type,
3720 candidate->convs[1]->type,
3721 candidate->convs[2]->type);
3722 else if (candidate->num_convs == 2)
3723 inform (cloc, "%s%<%D(%T, %T)%> (built-in)", msg, fn,
3724 candidate->convs[0]->type,
3725 candidate->convs[1]->type);
3726 else
3727 inform (cloc, "%s%<%D(%T)%> (built-in)", msg, fn,
3728 candidate->convs[0]->type);
3729 }
3730 else if (TYPE_P (fn))
3731 inform (cloc, "%s%qT (conversion)", msg, fn);
3732 else if (candidate->viable == -1)
3733 inform (cloc, "%s%#qD (near match)", msg, fn);
3734 else if (DECL_DELETED_FN (fn))
3735 inform (cloc, "%s%#qD (deleted)", msg, fn);
3736 else if (candidate->reversed ())
3737 inform (cloc, "%s%#qD (reversed)", msg, fn);
3738 else if (candidate->rewritten ())
3739 inform (cloc, "%s%#qD (rewritten)", msg, fn);
3740 else
3741 inform (cloc, "%s%#qD", msg, fn);
3742 if (fn != candidate->fn)
3743 {
3744 cloc = location_of (candidate->fn);
3745 inform (cloc, " inherited here");
3746 }
3747 /* Give the user some information about why this candidate failed. */
3748 if (candidate->reason != NULL)
3749 {
3750 struct rejection_reason *r = candidate->reason;
3751
3752 switch (r->code)
3753 {
3754 case rr_arity:
3755 print_arity_information (cloc, r->u.arity.actual,
3756 r->u.arity.expected);
3757 break;
3758 case rr_arg_conversion:
3759 print_conversion_rejection (cloc, &r->u.conversion, fn);
3760 break;
3761 case rr_bad_arg_conversion:
3762 print_conversion_rejection (cloc, &r->u.bad_conversion, fn);
3763 break;
3764 case rr_explicit_conversion:
3765 inform (cloc, " return type %qT of explicit conversion function "
3766 "cannot be converted to %qT with a qualification "
3767 "conversion", r->u.conversion.from,
3768 r->u.conversion.to_type);
3769 break;
3770 case rr_template_conversion:
3771 inform (cloc, " conversion from return type %qT of template "
3772 "conversion function specialization to %qT is not an "
3773 "exact match", r->u.conversion.from,
3774 r->u.conversion.to_type);
3775 break;
3776 case rr_template_unification:
3777 /* We use template_unification_error_rejection if unification caused
3778 actual non-SFINAE errors, in which case we don't need to repeat
3779 them here. */
3780 if (r->u.template_unification.tmpl == NULL_TREE)
3781 {
3782 inform (cloc, " substitution of deduced template arguments "
3783 "resulted in errors seen above");
3784 break;
3785 }
3786 /* Re-run template unification with diagnostics. */
3787 inform (cloc, " template argument deduction/substitution failed:");
3788 fn_type_unification (r->u.template_unification.tmpl,
3789 r->u.template_unification.explicit_targs,
3790 (make_tree_vec
3791 (r->u.template_unification.num_targs)),
3792 r->u.template_unification.args,
3793 r->u.template_unification.nargs,
3794 r->u.template_unification.return_type,
3795 r->u.template_unification.strict,
3796 r->u.template_unification.flags,
3797 NULL, true, false);
3798 break;
3799 case rr_invalid_copy:
3800 inform (cloc,
3801 " a constructor taking a single argument of its own "
3802 "class type is invalid");
3803 break;
3804 case rr_constraint_failure:
3805 diagnose_constraints (cloc, fn, NULL_TREE);
3806 break;
3807 case rr_inherited_ctor:
3808 inform (cloc, " an inherited constructor is not a candidate for "
3809 "initialization from an expression of the same or derived "
3810 "type");
3811 break;
3812 case rr_none:
3813 default:
3814 /* This candidate didn't have any issues or we failed to
3815 handle a particular code. Either way... */
3816 gcc_unreachable ();
3817 }
3818 }
3819 }
3820
3821 static void
3822 print_z_candidates (location_t loc, struct z_candidate *candidates)
3823 {
3824 struct z_candidate *cand1;
3825 struct z_candidate **cand2;
3826
3827 if (!candidates)
3828 return;
3829
3830 /* Remove non-viable deleted candidates. */
3831 cand1 = candidates;
3832 for (cand2 = &cand1; *cand2; )
3833 {
3834 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
3835 && !(*cand2)->viable
3836 && DECL_DELETED_FN ((*cand2)->fn))
3837 *cand2 = (*cand2)->next;
3838 else
3839 cand2 = &(*cand2)->next;
3840 }
3841 /* ...if there are any non-deleted ones. */
3842 if (cand1)
3843 candidates = cand1;
3844
3845 /* There may be duplicates in the set of candidates. We put off
3846 checking this condition as long as possible, since we have no way
3847 to eliminate duplicates from a set of functions in less than n^2
3848 time. Now we are about to emit an error message, so it is more
3849 permissible to go slowly. */
3850 for (cand1 = candidates; cand1; cand1 = cand1->next)
3851 {
3852 tree fn = cand1->fn;
3853 /* Skip builtin candidates and conversion functions. */
3854 if (!DECL_P (fn))
3855 continue;
3856 cand2 = &cand1->next;
3857 while (*cand2)
3858 {
3859 if (DECL_P ((*cand2)->fn)
3860 && equal_functions (fn, (*cand2)->fn))
3861 *cand2 = (*cand2)->next;
3862 else
3863 cand2 = &(*cand2)->next;
3864 }
3865 }
3866
3867 for (; candidates; candidates = candidates->next)
3868 print_z_candidate (loc, N_("candidate:"), candidates);
3869 }
3870
3871 /* USER_SEQ is a user-defined conversion sequence, beginning with a
3872 USER_CONV. STD_SEQ is the standard conversion sequence applied to
3873 the result of the conversion function to convert it to the final
3874 desired type. Merge the two sequences into a single sequence,
3875 and return the merged sequence. */
3876
3877 static conversion *
3878 merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
3879 {
3880 conversion **t;
3881 bool bad = user_seq->bad_p;
3882
3883 gcc_assert (user_seq->kind == ck_user);
3884
3885 /* Find the end of the second conversion sequence. */
3886 for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
3887 {
3888 /* The entire sequence is a user-conversion sequence. */
3889 (*t)->user_conv_p = true;
3890 if (bad)
3891 (*t)->bad_p = true;
3892 }
3893
3894 if ((*t)->rvaluedness_matches_p)
3895 /* We're binding a reference directly to the result of the conversion.
3896 build_user_type_conversion_1 stripped the REFERENCE_TYPE from the return
3897 type, but we want it back. */
3898 user_seq->type = TREE_TYPE (TREE_TYPE (user_seq->cand->fn));
3899
3900 /* Replace the identity conversion with the user conversion
3901 sequence. */
3902 *t = user_seq;
3903
3904 return std_seq;
3905 }
3906
3907 /* Handle overload resolution for initializing an object of class type from
3908 an initializer list. First we look for a suitable constructor that
3909 takes a std::initializer_list; if we don't find one, we then look for a
3910 non-list constructor.
3911
3912 Parameters are as for add_candidates, except that the arguments are in
3913 the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
3914 the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
3915
3916 static void
3917 add_list_candidates (tree fns, tree first_arg,
3918 const vec<tree, va_gc> *args, tree totype,
3919 tree explicit_targs, bool template_only,
3920 tree conversion_path, tree access_path,
3921 int flags,
3922 struct z_candidate **candidates,
3923 tsubst_flags_t complain)
3924 {
3925 gcc_assert (*candidates == NULL);
3926
3927 /* We're looking for a ctor for list-initialization. */
3928 flags |= LOOKUP_LIST_INIT_CTOR;
3929 /* And we don't allow narrowing conversions. We also use this flag to
3930 avoid the copy constructor call for copy-list-initialization. */
3931 flags |= LOOKUP_NO_NARROWING;
3932
3933 unsigned nart = num_artificial_parms_for (OVL_FIRST (fns)) - 1;
3934 tree init_list = (*args)[nart];
3935
3936 /* Always use the default constructor if the list is empty (DR 990). */
3937 if (CONSTRUCTOR_NELTS (init_list) == 0
3938 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
3939 ;
3940 /* If the class has a list ctor, try passing the list as a single
3941 argument first, but only consider list ctors. */
3942 else if (TYPE_HAS_LIST_CTOR (totype))
3943 {
3944 flags |= LOOKUP_LIST_ONLY;
3945 add_candidates (fns, first_arg, args, NULL_TREE,
3946 explicit_targs, template_only, conversion_path,
3947 access_path, flags, candidates, complain);
3948 if (any_strictly_viable (*candidates))
3949 return;
3950 }
3951 else if (CONSTRUCTOR_IS_DESIGNATED_INIT (init_list)
3952 && !CP_AGGREGATE_TYPE_P (totype))
3953 {
3954 if (complain & tf_error)
3955 error ("designated initializers cannot be used with a "
3956 "non-aggregate type %qT", totype);
3957 return;
3958 }
3959
3960 /* Expand the CONSTRUCTOR into a new argument vec. */
3961 vec<tree, va_gc> *new_args;
3962 vec_alloc (new_args, nart + CONSTRUCTOR_NELTS (init_list));
3963 for (unsigned i = 0; i < nart; ++i)
3964 new_args->quick_push ((*args)[i]);
3965 for (unsigned i = 0; i < CONSTRUCTOR_NELTS (init_list); ++i)
3966 new_args->quick_push (CONSTRUCTOR_ELT (init_list, i)->value);
3967
3968 /* We aren't looking for list-ctors anymore. */
3969 flags &= ~LOOKUP_LIST_ONLY;
3970 /* We allow more user-defined conversions within an init-list. */
3971 flags &= ~LOOKUP_NO_CONVERSION;
3972
3973 add_candidates (fns, first_arg, new_args, NULL_TREE,
3974 explicit_targs, template_only, conversion_path,
3975 access_path, flags, candidates, complain);
3976 }
3977
3978 /* Returns the best overload candidate to perform the requested
3979 conversion. This function is used for three the overloading situations
3980 described in [over.match.copy], [over.match.conv], and [over.match.ref].
3981 If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
3982 per [dcl.init.ref], so we ignore temporary bindings. */
3983
3984 static struct z_candidate *
3985 build_user_type_conversion_1 (tree totype, tree expr, int flags,
3986 tsubst_flags_t complain)
3987 {
3988 struct z_candidate *candidates, *cand;
3989 tree fromtype;
3990 tree ctors = NULL_TREE;
3991 tree conv_fns = NULL_TREE;
3992 conversion *conv = NULL;
3993 tree first_arg = NULL_TREE;
3994 vec<tree, va_gc> *args = NULL;
3995 bool any_viable_p;
3996 int convflags;
3997
3998 if (!expr)
3999 return NULL;
4000
4001 fromtype = TREE_TYPE (expr);
4002
4003 /* We represent conversion within a hierarchy using RVALUE_CONV and
4004 BASE_CONV, as specified by [over.best.ics]; these become plain
4005 constructor calls, as specified in [dcl.init]. */
4006 gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
4007 || !DERIVED_FROM_P (totype, fromtype));
4008
4009 if (CLASS_TYPE_P (totype))
4010 /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
4011 creating a garbage BASELINK; constructors can't be inherited. */
4012 ctors = get_class_binding (totype, complete_ctor_identifier);
4013
4014 if (MAYBE_CLASS_TYPE_P (fromtype))
4015 {
4016 tree to_nonref = non_reference (totype);
4017 if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
4018 (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
4019 && DERIVED_FROM_P (to_nonref, fromtype)))
4020 {
4021 /* [class.conv.fct] A conversion function is never used to
4022 convert a (possibly cv-qualified) object to the (possibly
4023 cv-qualified) same object type (or a reference to it), to a
4024 (possibly cv-qualified) base class of that type (or a
4025 reference to it)... */
4026 }
4027 else
4028 conv_fns = lookup_conversions (fromtype);
4029 }
4030
4031 candidates = 0;
4032 flags |= LOOKUP_NO_CONVERSION;
4033 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4034 flags |= LOOKUP_NO_NARROWING;
4035
4036 /* It's OK to bind a temporary for converting constructor arguments, but
4037 not in converting the return value of a conversion operator. */
4038 convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION
4039 | (flags & LOOKUP_NO_NARROWING));
4040 flags &= ~LOOKUP_NO_TEMP_BIND;
4041
4042 if (ctors)
4043 {
4044 int ctorflags = flags;
4045
4046 first_arg = build_dummy_object (totype);
4047
4048 /* We should never try to call the abstract or base constructor
4049 from here. */
4050 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_FIRST (ctors))
4051 && !DECL_HAS_VTT_PARM_P (OVL_FIRST (ctors)));
4052
4053 args = make_tree_vector_single (expr);
4054 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4055 {
4056 /* List-initialization. */
4057 add_list_candidates (ctors, first_arg, args, totype, NULL_TREE,
4058 false, TYPE_BINFO (totype), TYPE_BINFO (totype),
4059 ctorflags, &candidates, complain);
4060 }
4061 else
4062 {
4063 add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
4064 TYPE_BINFO (totype), TYPE_BINFO (totype),
4065 ctorflags, &candidates, complain);
4066 }
4067
4068 for (cand = candidates; cand; cand = cand->next)
4069 {
4070 cand->second_conv = build_identity_conv (totype, NULL_TREE);
4071
4072 /* If totype isn't a reference, and LOOKUP_NO_TEMP_BIND isn't
4073 set, then this is copy-initialization. In that case, "The
4074 result of the call is then used to direct-initialize the
4075 object that is the destination of the copy-initialization."
4076 [dcl.init]
4077
4078 We represent this in the conversion sequence with an
4079 rvalue conversion, which means a constructor call. */
4080 if (!TYPE_REF_P (totype)
4081 && !(convflags & LOOKUP_NO_TEMP_BIND))
4082 cand->second_conv
4083 = build_conv (ck_rvalue, totype, cand->second_conv);
4084 }
4085 }
4086
4087 if (conv_fns)
4088 {
4089 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4090 first_arg = CONSTRUCTOR_ELT (expr, 0)->value;
4091 else
4092 first_arg = expr;
4093 }
4094
4095 for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
4096 {
4097 tree conversion_path = TREE_PURPOSE (conv_fns);
4098 struct z_candidate *old_candidates;
4099
4100 /* If we are called to convert to a reference type, we are trying to
4101 find a direct binding, so don't even consider temporaries. If
4102 we don't find a direct binding, the caller will try again to
4103 look for a temporary binding. */
4104 if (TYPE_REF_P (totype))
4105 convflags |= LOOKUP_NO_TEMP_BIND;
4106
4107 old_candidates = candidates;
4108 add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
4109 NULL_TREE, false,
4110 conversion_path, TYPE_BINFO (fromtype),
4111 flags, &candidates, complain);
4112
4113 for (cand = candidates; cand != old_candidates; cand = cand->next)
4114 {
4115 if (cand->viable == 0)
4116 /* Already rejected, don't change to -1. */
4117 continue;
4118
4119 tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
4120 conversion *ics
4121 = implicit_conversion (totype,
4122 rettype,
4123 0,
4124 /*c_cast_p=*/false, convflags,
4125 complain);
4126
4127 /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
4128 copy-initialization. In that case, "The result of the
4129 call is then used to direct-initialize the object that is
4130 the destination of the copy-initialization." [dcl.init]
4131
4132 We represent this in the conversion sequence with an
4133 rvalue conversion, which means a constructor call. But
4134 don't add a second rvalue conversion if there's already
4135 one there. Which there really shouldn't be, but it's
4136 harmless since we'd add it here anyway. */
4137 if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
4138 && !(convflags & LOOKUP_NO_TEMP_BIND))
4139 ics = build_conv (ck_rvalue, totype, ics);
4140
4141 cand->second_conv = ics;
4142
4143 if (!ics)
4144 {
4145 cand->viable = 0;
4146 cand->reason = arg_conversion_rejection (NULL_TREE, -2,
4147 rettype, totype,
4148 EXPR_LOCATION (expr));
4149 }
4150 else if (TYPE_REF_P (totype) && !ics->rvaluedness_matches_p
4151 /* Limit this to non-templates for now (PR90546). */
4152 && !cand->template_decl
4153 && TREE_CODE (TREE_TYPE (totype)) != FUNCTION_TYPE)
4154 {
4155 /* If we are called to convert to a reference type, we are trying
4156 to find a direct binding per [over.match.ref], so rvaluedness
4157 must match for non-functions. */
4158 cand->viable = 0;
4159 }
4160 else if (DECL_NONCONVERTING_P (cand->fn)
4161 && ics->rank > cr_exact)
4162 {
4163 /* 13.3.1.5: For direct-initialization, those explicit
4164 conversion functions that are not hidden within S and
4165 yield type T or a type that can be converted to type T
4166 with a qualification conversion (4.4) are also candidate
4167 functions. */
4168 /* 13.3.1.6 doesn't have a parallel restriction, but it should;
4169 I've raised this issue with the committee. --jason 9/2011 */
4170 cand->viable = -1;
4171 cand->reason = explicit_conversion_rejection (rettype, totype);
4172 }
4173 else if (cand->viable == 1 && ics->bad_p)
4174 {
4175 cand->viable = -1;
4176 cand->reason
4177 = bad_arg_conversion_rejection (NULL_TREE, -2,
4178 rettype, totype,
4179 EXPR_LOCATION (expr));
4180 }
4181 else if (primary_template_specialization_p (cand->fn)
4182 && ics->rank > cr_exact)
4183 {
4184 /* 13.3.3.1.2: If the user-defined conversion is specified by
4185 a specialization of a conversion function template, the
4186 second standard conversion sequence shall have exact match
4187 rank. */
4188 cand->viable = -1;
4189 cand->reason = template_conversion_rejection (rettype, totype);
4190 }
4191 }
4192 }
4193
4194 candidates = splice_viable (candidates, false, &any_viable_p);
4195 if (!any_viable_p)
4196 {
4197 if (args)
4198 release_tree_vector (args);
4199 return NULL;
4200 }
4201
4202 cand = tourney (candidates, complain);
4203 if (cand == NULL)
4204 {
4205 if (complain & tf_error)
4206 {
4207 auto_diagnostic_group d;
4208 error_at (cp_expr_loc_or_input_loc (expr),
4209 "conversion from %qH to %qI is ambiguous",
4210 fromtype, totype);
4211 print_z_candidates (location_of (expr), candidates);
4212 }
4213
4214 cand = candidates; /* any one will do */
4215 cand->second_conv = build_ambiguous_conv (totype, expr);
4216 cand->second_conv->user_conv_p = true;
4217 if (!any_strictly_viable (candidates))
4218 cand->second_conv->bad_p = true;
4219 if (flags & LOOKUP_ONLYCONVERTING)
4220 cand->second_conv->need_temporary_p = true;
4221 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
4222 ambiguous conversion is no worse than another user-defined
4223 conversion. */
4224
4225 return cand;
4226 }
4227
4228 tree convtype;
4229 if (!DECL_CONSTRUCTOR_P (cand->fn))
4230 convtype = non_reference (TREE_TYPE (TREE_TYPE (cand->fn)));
4231 else if (cand->second_conv->kind == ck_rvalue)
4232 /* DR 5: [in the first step of copy-initialization]...if the function
4233 is a constructor, the call initializes a temporary of the
4234 cv-unqualified version of the destination type. */
4235 convtype = cv_unqualified (totype);
4236 else
4237 convtype = totype;
4238 /* Build the user conversion sequence. */
4239 conv = build_conv
4240 (ck_user,
4241 convtype,
4242 build_identity_conv (TREE_TYPE (expr), expr));
4243 conv->cand = cand;
4244 if (cand->viable == -1)
4245 conv->bad_p = true;
4246
4247 /* We're performing the maybe-rvalue overload resolution and
4248 a conversion function is in play. Reject converting the return
4249 value of the conversion function to a base class. */
4250 if ((flags & LOOKUP_PREFER_RVALUE) && !DECL_CONSTRUCTOR_P (cand->fn))
4251 for (conversion *t = cand->second_conv; t; t = next_conversion (t))
4252 if (t->kind == ck_base)
4253 return NULL;
4254
4255 /* Remember that this was a list-initialization. */
4256 if (flags & LOOKUP_NO_NARROWING)
4257 conv->check_narrowing = true;
4258
4259 /* Combine it with the second conversion sequence. */
4260 cand->second_conv = merge_conversion_sequences (conv,
4261 cand->second_conv);
4262
4263 return cand;
4264 }
4265
4266 /* Wrapper for above. */
4267
4268 tree
4269 build_user_type_conversion (tree totype, tree expr, int flags,
4270 tsubst_flags_t complain)
4271 {
4272 struct z_candidate *cand;
4273 tree ret;
4274
4275 bool subtime = timevar_cond_start (TV_OVERLOAD);
4276 cand = build_user_type_conversion_1 (totype, expr, flags, complain);
4277
4278 if (cand)
4279 {
4280 if (cand->second_conv->kind == ck_ambig)
4281 ret = error_mark_node;
4282 else
4283 {
4284 expr = convert_like (cand->second_conv, expr, complain);
4285 ret = convert_from_reference (expr);
4286 }
4287 }
4288 else
4289 ret = NULL_TREE;
4290
4291 timevar_cond_stop (TV_OVERLOAD, subtime);
4292 return ret;
4293 }
4294
4295 /* Give a helpful diagnostic when implicit_conversion fails. */
4296
4297 static void
4298 implicit_conversion_error (location_t loc, tree type, tree expr)
4299 {
4300 tsubst_flags_t complain = tf_warning_or_error;
4301
4302 /* If expr has unknown type, then it is an overloaded function.
4303 Call instantiate_type to get good error messages. */
4304 if (TREE_TYPE (expr) == unknown_type_node)
4305 instantiate_type (type, expr, complain);
4306 else if (invalid_nonstatic_memfn_p (loc, expr, complain))
4307 /* We gave an error. */;
4308 else if (BRACE_ENCLOSED_INITIALIZER_P (expr)
4309 && CONSTRUCTOR_IS_DESIGNATED_INIT (expr)
4310 && !CP_AGGREGATE_TYPE_P (type))
4311 error_at (loc, "designated initializers cannot be used with a "
4312 "non-aggregate type %qT", type);
4313 else
4314 {
4315 range_label_for_type_mismatch label (TREE_TYPE (expr), type);
4316 gcc_rich_location rich_loc (loc, &label);
4317 error_at (&rich_loc, "could not convert %qE from %qH to %qI",
4318 expr, TREE_TYPE (expr), type);
4319 }
4320 }
4321
4322 /* Worker for build_converted_constant_expr. */
4323
4324 static tree
4325 build_converted_constant_expr_internal (tree type, tree expr,
4326 int flags, tsubst_flags_t complain)
4327 {
4328 conversion *conv;
4329 void *p;
4330 tree t;
4331 location_t loc = cp_expr_loc_or_input_loc (expr);
4332
4333 if (error_operand_p (expr))
4334 return error_mark_node;
4335
4336 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4337 p = conversion_obstack_alloc (0);
4338
4339 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
4340 /*c_cast_p=*/false, flags, complain);
4341
4342 /* A converted constant expression of type T is an expression, implicitly
4343 converted to type T, where the converted expression is a constant
4344 expression and the implicit conversion sequence contains only
4345
4346 * user-defined conversions,
4347 * lvalue-to-rvalue conversions (7.1),
4348 * array-to-pointer conversions (7.2),
4349 * function-to-pointer conversions (7.3),
4350 * qualification conversions (7.5),
4351 * integral promotions (7.6),
4352 * integral conversions (7.8) other than narrowing conversions (11.6.4),
4353 * null pointer conversions (7.11) from std::nullptr_t,
4354 * null member pointer conversions (7.12) from std::nullptr_t, and
4355 * function pointer conversions (7.13),
4356
4357 and where the reference binding (if any) binds directly. */
4358
4359 for (conversion *c = conv;
4360 c && c->kind != ck_identity;
4361 c = next_conversion (c))
4362 {
4363 switch (c->kind)
4364 {
4365 /* A conversion function is OK. If it isn't constexpr, we'll
4366 complain later that the argument isn't constant. */
4367 case ck_user:
4368 /* List-initialization is OK. */
4369 case ck_aggr:
4370 /* The lvalue-to-rvalue conversion is OK. */
4371 case ck_rvalue:
4372 /* Array-to-pointer and function-to-pointer. */
4373 case ck_lvalue:
4374 /* Function pointer conversions. */
4375 case ck_fnptr:
4376 /* Qualification conversions. */
4377 case ck_qual:
4378 break;
4379
4380 case ck_ref_bind:
4381 if (c->need_temporary_p)
4382 {
4383 if (complain & tf_error)
4384 error_at (loc, "initializing %qH with %qI in converted "
4385 "constant expression does not bind directly",
4386 type, next_conversion (c)->type);
4387 conv = NULL;
4388 }
4389 break;
4390
4391 case ck_base:
4392 case ck_pmem:
4393 case ck_ptr:
4394 case ck_std:
4395 t = next_conversion (c)->type;
4396 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t)
4397 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4398 /* Integral promotion or conversion. */
4399 break;
4400 if (NULLPTR_TYPE_P (t))
4401 /* Conversion from nullptr to pointer or pointer-to-member. */
4402 break;
4403
4404 if (complain & tf_error)
4405 error_at (loc, "conversion from %qH to %qI in a "
4406 "converted constant expression", t, type);
4407 /* fall through. */
4408
4409 default:
4410 conv = NULL;
4411 break;
4412 }
4413 }
4414
4415 /* Avoid confusing convert_nontype_argument by introducing
4416 a redundant conversion to the same reference type. */
4417 if (conv && conv->kind == ck_ref_bind
4418 && REFERENCE_REF_P (expr))
4419 {
4420 tree ref = TREE_OPERAND (expr, 0);
4421 if (same_type_p (type, TREE_TYPE (ref)))
4422 return ref;
4423 }
4424
4425 if (conv)
4426 {
4427 /* Don't copy a class in a template. */
4428 if (CLASS_TYPE_P (type) && conv->kind == ck_rvalue
4429 && processing_template_decl)
4430 conv = next_conversion (conv);
4431
4432 conv->check_narrowing = true;
4433 conv->check_narrowing_const_only = true;
4434 expr = convert_like (conv, expr, complain);
4435 }
4436 else
4437 {
4438 if (complain & tf_error)
4439 implicit_conversion_error (loc, type, expr);
4440 expr = error_mark_node;
4441 }
4442
4443 /* Free all the conversions we allocated. */
4444 obstack_free (&conversion_obstack, p);
4445
4446 return expr;
4447 }
4448
4449 /* Subroutine of convert_nontype_argument.
4450
4451 EXPR is an expression used in a context that requires a converted
4452 constant-expression, such as a template non-type parameter. Do any
4453 necessary conversions (that are permitted for converted
4454 constant-expressions) to convert it to the desired type.
4455
4456 This function doesn't consider explicit conversion functions. If
4457 you mean to use "a contextually converted constant expression of type
4458 bool", use build_converted_constant_bool_expr.
4459
4460 If conversion is successful, returns the converted expression;
4461 otherwise, returns error_mark_node. */
4462
4463 tree
4464 build_converted_constant_expr (tree type, tree expr, tsubst_flags_t complain)
4465 {
4466 return build_converted_constant_expr_internal (type, expr, LOOKUP_IMPLICIT,
4467 complain);
4468 }
4469
4470 /* Used to create "a contextually converted constant expression of type
4471 bool". This differs from build_converted_constant_expr in that it
4472 also considers explicit conversion functions. */
4473
4474 tree
4475 build_converted_constant_bool_expr (tree expr, tsubst_flags_t complain)
4476 {
4477 return build_converted_constant_expr_internal (boolean_type_node, expr,
4478 LOOKUP_NORMAL, complain);
4479 }
4480
4481 /* Do any initial processing on the arguments to a function call. */
4482
4483 vec<tree, va_gc> *
4484 resolve_args (vec<tree, va_gc> *args, tsubst_flags_t complain)
4485 {
4486 unsigned int ix;
4487 tree arg;
4488
4489 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
4490 {
4491 if (error_operand_p (arg))
4492 return NULL;
4493 else if (VOID_TYPE_P (TREE_TYPE (arg)))
4494 {
4495 if (complain & tf_error)
4496 error_at (cp_expr_loc_or_input_loc (arg),
4497 "invalid use of void expression");
4498 return NULL;
4499 }
4500 else if (invalid_nonstatic_memfn_p (EXPR_LOCATION (arg), arg, complain))
4501 return NULL;
4502 }
4503 return args;
4504 }
4505
4506 /* Perform overload resolution on FN, which is called with the ARGS.
4507
4508 Return the candidate function selected by overload resolution, or
4509 NULL if the event that overload resolution failed. In the case
4510 that overload resolution fails, *CANDIDATES will be the set of
4511 candidates considered, and ANY_VIABLE_P will be set to true or
4512 false to indicate whether or not any of the candidates were
4513 viable.
4514
4515 The ARGS should already have gone through RESOLVE_ARGS before this
4516 function is called. */
4517
4518 static struct z_candidate *
4519 perform_overload_resolution (tree fn,
4520 const vec<tree, va_gc> *args,
4521 struct z_candidate **candidates,
4522 bool *any_viable_p, tsubst_flags_t complain)
4523 {
4524 struct z_candidate *cand;
4525 tree explicit_targs;
4526 int template_only;
4527
4528 bool subtime = timevar_cond_start (TV_OVERLOAD);
4529
4530 explicit_targs = NULL_TREE;
4531 template_only = 0;
4532
4533 *candidates = NULL;
4534 *any_viable_p = true;
4535
4536 /* Check FN. */
4537 gcc_assert (OVL_P (fn) || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
4538
4539 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4540 {
4541 explicit_targs = TREE_OPERAND (fn, 1);
4542 fn = TREE_OPERAND (fn, 0);
4543 template_only = 1;
4544 }
4545
4546 /* Add the various candidate functions. */
4547 add_candidates (fn, NULL_TREE, args, NULL_TREE,
4548 explicit_targs, template_only,
4549 /*conversion_path=*/NULL_TREE,
4550 /*access_path=*/NULL_TREE,
4551 LOOKUP_NORMAL,
4552 candidates, complain);
4553
4554 *candidates = splice_viable (*candidates, false, any_viable_p);
4555 if (*any_viable_p)
4556 cand = tourney (*candidates, complain);
4557 else
4558 cand = NULL;
4559
4560 timevar_cond_stop (TV_OVERLOAD, subtime);
4561 return cand;
4562 }
4563
4564 /* Print an error message about being unable to build a call to FN with
4565 ARGS. ANY_VIABLE_P indicates whether any candidate functions could
4566 be located; CANDIDATES is a possibly empty list of such
4567 functions. */
4568
4569 static void
4570 print_error_for_call_failure (tree fn, vec<tree, va_gc> *args,
4571 struct z_candidate *candidates)
4572 {
4573 tree targs = NULL_TREE;
4574 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4575 {
4576 targs = TREE_OPERAND (fn, 1);
4577 fn = TREE_OPERAND (fn, 0);
4578 }
4579 tree name = OVL_NAME (fn);
4580 location_t loc = location_of (name);
4581 if (targs)
4582 name = lookup_template_function (name, targs);
4583
4584 auto_diagnostic_group d;
4585 if (!any_strictly_viable (candidates))
4586 error_at (loc, "no matching function for call to %<%D(%A)%>",
4587 name, build_tree_list_vec (args));
4588 else
4589 error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous",
4590 name, build_tree_list_vec (args));
4591 if (candidates)
4592 print_z_candidates (loc, candidates);
4593 }
4594
4595 /* Return an expression for a call to FN (a namespace-scope function,
4596 or a static member function) with the ARGS. This may change
4597 ARGS. */
4598
4599 tree
4600 build_new_function_call (tree fn, vec<tree, va_gc> **args,
4601 tsubst_flags_t complain)
4602 {
4603 struct z_candidate *candidates, *cand;
4604 bool any_viable_p;
4605 void *p;
4606 tree result;
4607
4608 if (args != NULL && *args != NULL)
4609 {
4610 *args = resolve_args (*args, complain);
4611 if (*args == NULL)
4612 return error_mark_node;
4613 }
4614
4615 if (flag_tm)
4616 tm_malloc_replacement (fn);
4617
4618 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4619 p = conversion_obstack_alloc (0);
4620
4621 cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
4622 complain);
4623
4624 if (!cand)
4625 {
4626 if (complain & tf_error)
4627 {
4628 // If there is a single (non-viable) function candidate,
4629 // let the error be diagnosed by cp_build_function_call_vec.
4630 if (!any_viable_p && candidates && ! candidates->next
4631 && (TREE_CODE (candidates->fn) == FUNCTION_DECL))
4632 return cp_build_function_call_vec (candidates->fn, args, complain);
4633
4634 // Otherwise, emit notes for non-viable candidates.
4635 print_error_for_call_failure (fn, *args, candidates);
4636 }
4637 result = error_mark_node;
4638 }
4639 else
4640 {
4641 result = build_over_call (cand, LOOKUP_NORMAL, complain);
4642 }
4643
4644 if (flag_coroutines
4645 && result
4646 && TREE_CODE (result) == CALL_EXPR
4647 && DECL_BUILT_IN_CLASS (TREE_OPERAND (CALL_EXPR_FN (result), 0))
4648 == BUILT_IN_NORMAL)
4649 result = coro_validate_builtin_call (result);
4650
4651 /* Free all the conversions we allocated. */
4652 obstack_free (&conversion_obstack, p);
4653
4654 return result;
4655 }
4656
4657 /* Build a call to a global operator new. FNNAME is the name of the
4658 operator (either "operator new" or "operator new[]") and ARGS are
4659 the arguments provided. This may change ARGS. *SIZE points to the
4660 total number of bytes required by the allocation, and is updated if
4661 that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
4662 be used. If this function determines that no cookie should be
4663 used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK
4664 is not NULL_TREE, it is evaluated before calculating the final
4665 array size, and if it fails, the array size is replaced with
4666 (size_t)-1 (usually triggering a std::bad_alloc exception). If FN
4667 is non-NULL, it will be set, upon return, to the allocation
4668 function called. */
4669
4670 tree
4671 build_operator_new_call (tree fnname, vec<tree, va_gc> **args,
4672 tree *size, tree *cookie_size,
4673 tree align_arg, tree size_check,
4674 tree *fn, tsubst_flags_t complain)
4675 {
4676 tree original_size = *size;
4677 tree fns;
4678 struct z_candidate *candidates;
4679 struct z_candidate *cand = NULL;
4680 bool any_viable_p;
4681
4682 if (fn)
4683 *fn = NULL_TREE;
4684 /* Set to (size_t)-1 if the size check fails. */
4685 if (size_check != NULL_TREE)
4686 {
4687 tree errval = TYPE_MAX_VALUE (sizetype);
4688 if (cxx_dialect >= cxx11 && flag_exceptions)
4689 errval = throw_bad_array_new_length ();
4690 *size = fold_build3 (COND_EXPR, sizetype, size_check,
4691 original_size, errval);
4692 }
4693 vec_safe_insert (*args, 0, *size);
4694 *args = resolve_args (*args, complain);
4695 if (*args == NULL)
4696 return error_mark_node;
4697
4698 /* Based on:
4699
4700 [expr.new]
4701
4702 If this lookup fails to find the name, or if the allocated type
4703 is not a class type, the allocation function's name is looked
4704 up in the global scope.
4705
4706 we disregard block-scope declarations of "operator new". */
4707 fns = lookup_name_real (fnname, LOOK_where::NAMESPACE, 0, 0, 0);
4708 fns = lookup_arg_dependent (fnname, fns, *args);
4709
4710 if (align_arg)
4711 {
4712 vec<tree, va_gc>* align_args
4713 = vec_copy_and_insert (*args, align_arg, 1);
4714 cand = perform_overload_resolution (fns, align_args, &candidates,
4715 &any_viable_p, tf_none);
4716 if (cand)
4717 *args = align_args;
4718 /* If no aligned allocation function matches, try again without the
4719 alignment. */
4720 }
4721
4722 /* Figure out what function is being called. */
4723 if (!cand)
4724 cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p,
4725 complain);
4726
4727 /* If no suitable function could be found, issue an error message
4728 and give up. */
4729 if (!cand)
4730 {
4731 if (complain & tf_error)
4732 print_error_for_call_failure (fns, *args, candidates);
4733 return error_mark_node;
4734 }
4735
4736 /* If a cookie is required, add some extra space. Whether
4737 or not a cookie is required cannot be determined until
4738 after we know which function was called. */
4739 if (*cookie_size)
4740 {
4741 bool use_cookie = true;
4742 tree arg_types;
4743
4744 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
4745 /* Skip the size_t parameter. */
4746 arg_types = TREE_CHAIN (arg_types);
4747 /* Check the remaining parameters (if any). */
4748 if (arg_types
4749 && TREE_CHAIN (arg_types) == void_list_node
4750 && same_type_p (TREE_VALUE (arg_types),
4751 ptr_type_node))
4752 use_cookie = false;
4753 /* If we need a cookie, adjust the number of bytes allocated. */
4754 if (use_cookie)
4755 {
4756 /* Update the total size. */
4757 *size = size_binop (PLUS_EXPR, original_size, *cookie_size);
4758 if (size_check)
4759 {
4760 /* Set to (size_t)-1 if the size check fails. */
4761 gcc_assert (size_check != NULL_TREE);
4762 *size = fold_build3 (COND_EXPR, sizetype, size_check,
4763 *size, TYPE_MAX_VALUE (sizetype));
4764 }
4765 /* Update the argument list to reflect the adjusted size. */
4766 (**args)[0] = *size;
4767 }
4768 else
4769 *cookie_size = NULL_TREE;
4770 }
4771
4772 /* Tell our caller which function we decided to call. */
4773 if (fn)
4774 *fn = cand->fn;
4775
4776 /* Build the CALL_EXPR. */
4777 return build_over_call (cand, LOOKUP_NORMAL, complain);
4778 }
4779
4780 /* Build a new call to operator(). This may change ARGS. */
4781
4782 static tree
4783 build_op_call_1 (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
4784 {
4785 struct z_candidate *candidates = 0, *cand;
4786 tree fns, convs, first_mem_arg = NULL_TREE;
4787 bool any_viable_p;
4788 tree result = NULL_TREE;
4789 void *p;
4790
4791 obj = mark_lvalue_use (obj);
4792
4793 if (error_operand_p (obj))
4794 return error_mark_node;
4795
4796 tree type = TREE_TYPE (obj);
4797
4798 obj = prep_operand (obj);
4799
4800 if (TYPE_PTRMEMFUNC_P (type))
4801 {
4802 if (complain & tf_error)
4803 /* It's no good looking for an overloaded operator() on a
4804 pointer-to-member-function. */
4805 error ("pointer-to-member function %qE cannot be called without "
4806 "an object; consider using %<.*%> or %<->*%>", obj);
4807 return error_mark_node;
4808 }
4809
4810 if (TYPE_BINFO (type))
4811 {
4812 fns = lookup_fnfields (TYPE_BINFO (type), call_op_identifier, 1, complain);
4813 if (fns == error_mark_node)
4814 return error_mark_node;
4815 }
4816 else
4817 fns = NULL_TREE;
4818
4819 if (args != NULL && *args != NULL)
4820 {
4821 *args = resolve_args (*args, complain);
4822 if (*args == NULL)
4823 return error_mark_node;
4824 }
4825
4826 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4827 p = conversion_obstack_alloc (0);
4828
4829 if (fns)
4830 {
4831 first_mem_arg = obj;
4832
4833 add_candidates (BASELINK_FUNCTIONS (fns),
4834 first_mem_arg, *args, NULL_TREE,
4835 NULL_TREE, false,
4836 BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
4837 LOOKUP_NORMAL, &candidates, complain);
4838 }
4839
4840 convs = lookup_conversions (type);
4841
4842 for (; convs; convs = TREE_CHAIN (convs))
4843 {
4844 tree totype = TREE_TYPE (convs);
4845
4846 if (TYPE_PTRFN_P (totype)
4847 || TYPE_REFFN_P (totype)
4848 || (TYPE_REF_P (totype)
4849 && TYPE_PTRFN_P (TREE_TYPE (totype))))
4850 for (ovl_iterator iter (TREE_VALUE (convs)); iter; ++iter)
4851 {
4852 tree fn = *iter;
4853
4854 if (DECL_NONCONVERTING_P (fn))
4855 continue;
4856
4857 if (TREE_CODE (fn) == TEMPLATE_DECL)
4858 add_template_conv_candidate
4859 (&candidates, fn, obj, *args, totype,
4860 /*access_path=*/NULL_TREE,
4861 /*conversion_path=*/NULL_TREE, complain);
4862 else
4863 add_conv_candidate (&candidates, fn, obj,
4864 *args, /*conversion_path=*/NULL_TREE,
4865 /*access_path=*/NULL_TREE, complain);
4866 }
4867 }
4868
4869 /* Be strict here because if we choose a bad conversion candidate, the
4870 errors we get won't mention the call context. */
4871 candidates = splice_viable (candidates, true, &any_viable_p);
4872 if (!any_viable_p)
4873 {
4874 if (complain & tf_error)
4875 {
4876 auto_diagnostic_group d;
4877 error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
4878 build_tree_list_vec (*args));
4879 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4880 }
4881 result = error_mark_node;
4882 }
4883 else
4884 {
4885 cand = tourney (candidates, complain);
4886 if (cand == 0)
4887 {
4888 if (complain & tf_error)
4889 {
4890 auto_diagnostic_group d;
4891 error ("call of %<(%T) (%A)%> is ambiguous",
4892 TREE_TYPE (obj), build_tree_list_vec (*args));
4893 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
4894 }
4895 result = error_mark_node;
4896 }
4897 else if (TREE_CODE (cand->fn) == FUNCTION_DECL
4898 && DECL_OVERLOADED_OPERATOR_P (cand->fn)
4899 && DECL_OVERLOADED_OPERATOR_IS (cand->fn, CALL_EXPR))
4900 result = build_over_call (cand, LOOKUP_NORMAL, complain);
4901 else
4902 {
4903 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
4904 obj = convert_like_with_context (cand->convs[0], obj, cand->fn,
4905 -1, complain);
4906 else
4907 {
4908 gcc_checking_assert (TYPE_P (cand->fn));
4909 obj = convert_like (cand->convs[0], obj, complain);
4910 }
4911 obj = convert_from_reference (obj);
4912 result = cp_build_function_call_vec (obj, args, complain);
4913 }
4914 }
4915
4916 /* Free all the conversions we allocated. */
4917 obstack_free (&conversion_obstack, p);
4918
4919 return result;
4920 }
4921
4922 /* Wrapper for above. */
4923
4924 tree
4925 build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
4926 {
4927 tree ret;
4928 bool subtime = timevar_cond_start (TV_OVERLOAD);
4929 ret = build_op_call_1 (obj, args, complain);
4930 timevar_cond_stop (TV_OVERLOAD, subtime);
4931 return ret;
4932 }
4933
4934 /* Called by op_error to prepare format strings suitable for the error
4935 function. It concatenates a prefix (controlled by MATCH), ERRMSG,
4936 and a suffix (controlled by NTYPES). */
4937
4938 static const char *
4939 op_error_string (const char *errmsg, int ntypes, bool match)
4940 {
4941 const char *msg;
4942
4943 const char *msgp = concat (match ? G_("ambiguous overload for ")
4944 : G_("no match for "), errmsg, NULL);
4945
4946 if (ntypes == 3)
4947 msg = concat (msgp, G_(" (operand types are %qT, %qT, and %qT)"), NULL);
4948 else if (ntypes == 2)
4949 msg = concat (msgp, G_(" (operand types are %qT and %qT)"), NULL);
4950 else
4951 msg = concat (msgp, G_(" (operand type is %qT)"), NULL);
4952
4953 return msg;
4954 }
4955
4956 static void
4957 op_error (const op_location_t &loc,
4958 enum tree_code code, enum tree_code code2,
4959 tree arg1, tree arg2, tree arg3, bool match)
4960 {
4961 bool assop = code == MODIFY_EXPR;
4962 const char *opname = OVL_OP_INFO (assop, assop ? code2 : code)->name;
4963
4964 switch (code)
4965 {
4966 case COND_EXPR:
4967 if (flag_diagnostics_show_caret)
4968 error_at (loc, op_error_string (G_("ternary %<operator?:%>"),
4969 3, match),
4970 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4971 else
4972 error_at (loc, op_error_string (G_("ternary %<operator?:%> "
4973 "in %<%E ? %E : %E%>"), 3, match),
4974 arg1, arg2, arg3,
4975 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
4976 break;
4977
4978 case POSTINCREMENT_EXPR:
4979 case POSTDECREMENT_EXPR:
4980 if (flag_diagnostics_show_caret)
4981 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
4982 opname, TREE_TYPE (arg1));
4983 else
4984 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
4985 1, match),
4986 opname, arg1, opname, TREE_TYPE (arg1));
4987 break;
4988
4989 case ARRAY_REF:
4990 if (flag_diagnostics_show_caret)
4991 error_at (loc, op_error_string (G_("%<operator[]%>"), 2, match),
4992 TREE_TYPE (arg1), TREE_TYPE (arg2));
4993 else
4994 error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
4995 2, match),
4996 arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2));
4997 break;
4998
4999 case REALPART_EXPR:
5000 case IMAGPART_EXPR:
5001 if (flag_diagnostics_show_caret)
5002 error_at (loc, op_error_string (G_("%qs"), 1, match),
5003 opname, TREE_TYPE (arg1));
5004 else
5005 error_at (loc, op_error_string (G_("%qs in %<%s %E%>"), 1, match),
5006 opname, opname, arg1, TREE_TYPE (arg1));
5007 break;
5008
5009 case CO_AWAIT_EXPR:
5010 if (flag_diagnostics_show_caret)
5011 error_at (loc, op_error_string (G_("%<operator %s%>"), 1, match),
5012 opname, TREE_TYPE (arg1));
5013 else
5014 error_at (loc, op_error_string (G_("%<operator %s%> in %<%s%E%>"),
5015 1, match),
5016 opname, opname, arg1, TREE_TYPE (arg1));
5017 break;
5018
5019 default:
5020 if (arg2)
5021 if (flag_diagnostics_show_caret)
5022 {
5023 binary_op_rich_location richloc (loc, arg1, arg2, true);
5024 error_at (&richloc,
5025 op_error_string (G_("%<operator%s%>"), 2, match),
5026 opname, TREE_TYPE (arg1), TREE_TYPE (arg2));
5027 }
5028 else
5029 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
5030 2, match),
5031 opname, arg1, opname, arg2,
5032 TREE_TYPE (arg1), TREE_TYPE (arg2));
5033 else
5034 if (flag_diagnostics_show_caret)
5035 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
5036 opname, TREE_TYPE (arg1));
5037 else
5038 error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
5039 1, match),
5040 opname, opname, arg1, TREE_TYPE (arg1));
5041 break;
5042 }
5043 }
5044
5045 /* Return the implicit conversion sequence that could be used to
5046 convert E1 to E2 in [expr.cond]. */
5047
5048 static conversion *
5049 conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
5050 {
5051 tree t1 = non_reference (TREE_TYPE (e1));
5052 tree t2 = non_reference (TREE_TYPE (e2));
5053 conversion *conv;
5054 bool good_base;
5055
5056 /* [expr.cond]
5057
5058 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
5059 implicitly converted (clause _conv_) to the type "lvalue reference to
5060 T2", subject to the constraint that in the conversion the
5061 reference must bind directly (_dcl.init.ref_) to an lvalue.
5062
5063 If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
5064 implicitly converted to the type "rvalue reference to T2", subject to
5065 the constraint that the reference must bind directly. */
5066 if (glvalue_p (e2))
5067 {
5068 tree rtype = cp_build_reference_type (t2, !lvalue_p (e2));
5069 conv = implicit_conversion (rtype,
5070 t1,
5071 e1,
5072 /*c_cast_p=*/false,
5073 LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
5074 |LOOKUP_ONLYCONVERTING,
5075 complain);
5076 if (conv && !conv->bad_p)
5077 return conv;
5078 }
5079
5080 /* If E2 is a prvalue or if neither of the conversions above can be done
5081 and at least one of the operands has (possibly cv-qualified) class
5082 type: */
5083 if (!CLASS_TYPE_P (t1) && !CLASS_TYPE_P (t2))
5084 return NULL;
5085
5086 /* [expr.cond]
5087
5088 If E1 and E2 have class type, and the underlying class types are
5089 the same or one is a base class of the other: E1 can be converted
5090 to match E2 if the class of T2 is the same type as, or a base
5091 class of, the class of T1, and the cv-qualification of T2 is the
5092 same cv-qualification as, or a greater cv-qualification than, the
5093 cv-qualification of T1. If the conversion is applied, E1 is
5094 changed to an rvalue of type T2 that still refers to the original
5095 source class object (or the appropriate subobject thereof). */
5096 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
5097 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
5098 {
5099 if (good_base && at_least_as_qualified_p (t2, t1))
5100 {
5101 conv = build_identity_conv (t1, e1);
5102 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
5103 TYPE_MAIN_VARIANT (t2)))
5104 conv = build_conv (ck_base, t2, conv);
5105 else
5106 conv = build_conv (ck_rvalue, t2, conv);
5107 return conv;
5108 }
5109 else
5110 return NULL;
5111 }
5112 else
5113 /* [expr.cond]
5114
5115 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
5116 converted to the type that expression E2 would have if E2 were
5117 converted to an rvalue (or the type it has, if E2 is an rvalue). */
5118 return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
5119 LOOKUP_IMPLICIT, complain);
5120 }
5121
5122 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
5123 arguments to the conditional expression. */
5124
5125 static tree
5126 build_conditional_expr_1 (const op_location_t &loc,
5127 tree arg1, tree arg2, tree arg3,
5128 tsubst_flags_t complain)
5129 {
5130 tree arg2_type;
5131 tree arg3_type;
5132 tree result = NULL_TREE;
5133 tree result_type = NULL_TREE;
5134 bool is_glvalue = true;
5135 struct z_candidate *candidates = 0;
5136 struct z_candidate *cand;
5137 void *p;
5138 tree orig_arg2, orig_arg3;
5139
5140 /* As a G++ extension, the second argument to the conditional can be
5141 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
5142 c'.) If the second operand is omitted, make sure it is
5143 calculated only once. */
5144 if (!arg2)
5145 {
5146 if (complain & tf_error)
5147 pedwarn (loc, OPT_Wpedantic,
5148 "ISO C++ forbids omitting the middle term of "
5149 "a %<?:%> expression");
5150
5151 if ((complain & tf_warning) && !truth_value_p (TREE_CODE (arg1)))
5152 warn_for_omitted_condop (loc, arg1);
5153
5154 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
5155 if (glvalue_p (arg1))
5156 {
5157 arg1 = cp_stabilize_reference (arg1);
5158 arg2 = arg1 = prevent_lifetime_extension (arg1);
5159 }
5160 else
5161 arg2 = arg1 = cp_save_expr (arg1);
5162 }
5163
5164 /* If something has already gone wrong, just pass that fact up the
5165 tree. */
5166 if (error_operand_p (arg1)
5167 || error_operand_p (arg2)
5168 || error_operand_p (arg3))
5169 return error_mark_node;
5170
5171 orig_arg2 = arg2;
5172 orig_arg3 = arg3;
5173
5174 if (gnu_vector_type_p (TREE_TYPE (arg1))
5175 && VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1)))
5176 {
5177 tree arg1_type = TREE_TYPE (arg1);
5178
5179 /* If arg1 is another cond_expr choosing between -1 and 0,
5180 then we can use its comparison. It may help to avoid
5181 additional comparison, produce more accurate diagnostics
5182 and enables folding. */
5183 if (TREE_CODE (arg1) == VEC_COND_EXPR
5184 && integer_minus_onep (TREE_OPERAND (arg1, 1))
5185 && integer_zerop (TREE_OPERAND (arg1, 2)))
5186 arg1 = TREE_OPERAND (arg1, 0);
5187
5188 arg1 = force_rvalue (arg1, complain);
5189 arg2 = force_rvalue (arg2, complain);
5190 arg3 = force_rvalue (arg3, complain);
5191
5192 /* force_rvalue can return error_mark on valid arguments. */
5193 if (error_operand_p (arg1)
5194 || error_operand_p (arg2)
5195 || error_operand_p (arg3))
5196 return error_mark_node;
5197
5198 arg2_type = TREE_TYPE (arg2);
5199 arg3_type = TREE_TYPE (arg3);
5200
5201 if (!VECTOR_TYPE_P (arg2_type)
5202 && !VECTOR_TYPE_P (arg3_type))
5203 {
5204 /* Rely on the error messages of the scalar version. */
5205 tree scal = build_conditional_expr_1 (loc, integer_one_node,
5206 orig_arg2, orig_arg3, complain);
5207 if (scal == error_mark_node)
5208 return error_mark_node;
5209 tree stype = TREE_TYPE (scal);
5210 tree ctype = TREE_TYPE (arg1_type);
5211 if (TYPE_SIZE (stype) != TYPE_SIZE (ctype)
5212 || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype)))
5213 {
5214 if (complain & tf_error)
5215 error_at (loc, "inferred scalar type %qT is not an integer or "
5216 "floating-point type of the same size as %qT", stype,
5217 COMPARISON_CLASS_P (arg1)
5218 ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0)))
5219 : ctype);
5220 return error_mark_node;
5221 }
5222
5223 tree vtype = build_opaque_vector_type (stype,
5224 TYPE_VECTOR_SUBPARTS (arg1_type));
5225 /* We could pass complain & tf_warning to unsafe_conversion_p,
5226 but the warnings (like Wsign-conversion) have already been
5227 given by the scalar build_conditional_expr_1. We still check
5228 unsafe_conversion_p to forbid truncating long long -> float. */
5229 if (unsafe_conversion_p (stype, arg2, NULL_TREE, false))
5230 {
5231 if (complain & tf_error)
5232 error_at (loc, "conversion of scalar %qH to vector %qI "
5233 "involves truncation", arg2_type, vtype);
5234 return error_mark_node;
5235 }
5236 if (unsafe_conversion_p (stype, arg3, NULL_TREE, false))
5237 {
5238 if (complain & tf_error)
5239 error_at (loc, "conversion of scalar %qH to vector %qI "
5240 "involves truncation", arg3_type, vtype);
5241 return error_mark_node;
5242 }
5243
5244 arg2 = cp_convert (stype, arg2, complain);
5245 arg2 = save_expr (arg2);
5246 arg2 = build_vector_from_val (vtype, arg2);
5247 arg2_type = vtype;
5248 arg3 = cp_convert (stype, arg3, complain);
5249 arg3 = save_expr (arg3);
5250 arg3 = build_vector_from_val (vtype, arg3);
5251 arg3_type = vtype;
5252 }
5253
5254 if ((gnu_vector_type_p (arg2_type) && !VECTOR_TYPE_P (arg3_type))
5255 || (gnu_vector_type_p (arg3_type) && !VECTOR_TYPE_P (arg2_type)))
5256 {
5257 enum stv_conv convert_flag =
5258 scalar_to_vector (loc, VEC_COND_EXPR, arg2, arg3,
5259 complain & tf_error);
5260
5261 switch (convert_flag)
5262 {
5263 case stv_error:
5264 return error_mark_node;
5265 case stv_firstarg:
5266 {
5267 arg2 = save_expr (arg2);
5268 arg2 = convert (TREE_TYPE (arg3_type), arg2);
5269 arg2 = build_vector_from_val (arg3_type, arg2);
5270 arg2_type = TREE_TYPE (arg2);
5271 break;
5272 }
5273 case stv_secondarg:
5274 {
5275 arg3 = save_expr (arg3);
5276 arg3 = convert (TREE_TYPE (arg2_type), arg3);
5277 arg3 = build_vector_from_val (arg2_type, arg3);
5278 arg3_type = TREE_TYPE (arg3);
5279 break;
5280 }
5281 default:
5282 break;
5283 }
5284 }
5285
5286 if (!gnu_vector_type_p (arg2_type)
5287 || !gnu_vector_type_p (arg3_type)
5288 || !same_type_p (arg2_type, arg3_type)
5289 || maybe_ne (TYPE_VECTOR_SUBPARTS (arg1_type),
5290 TYPE_VECTOR_SUBPARTS (arg2_type))
5291 || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type))
5292 {
5293 if (complain & tf_error)
5294 error_at (loc,
5295 "incompatible vector types in conditional expression: "
5296 "%qT, %qT and %qT", TREE_TYPE (arg1),
5297 TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3));
5298 return error_mark_node;
5299 }
5300
5301 if (!COMPARISON_CLASS_P (arg1))
5302 {
5303 tree cmp_type = truth_type_for (arg1_type);
5304 arg1 = build2 (NE_EXPR, cmp_type, arg1, build_zero_cst (arg1_type));
5305 }
5306 return build3_loc (loc, VEC_COND_EXPR, arg2_type, arg1, arg2, arg3);
5307 }
5308
5309 /* [expr.cond]
5310
5311 The first expression is implicitly converted to bool (clause
5312 _conv_). */
5313 arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
5314 LOOKUP_NORMAL);
5315 if (error_operand_p (arg1))
5316 return error_mark_node;
5317
5318 /* [expr.cond]
5319
5320 If either the second or the third operand has type (possibly
5321 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
5322 array-to-pointer (_conv.array_), and function-to-pointer
5323 (_conv.func_) standard conversions are performed on the second
5324 and third operands. */
5325 arg2_type = unlowered_expr_type (arg2);
5326 arg3_type = unlowered_expr_type (arg3);
5327 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
5328 {
5329 /* 'void' won't help in resolving an overloaded expression on the
5330 other side, so require it to resolve by itself. */
5331 if (arg2_type == unknown_type_node)
5332 {
5333 arg2 = resolve_nondeduced_context_or_error (arg2, complain);
5334 arg2_type = TREE_TYPE (arg2);
5335 }
5336 if (arg3_type == unknown_type_node)
5337 {
5338 arg3 = resolve_nondeduced_context_or_error (arg3, complain);
5339 arg3_type = TREE_TYPE (arg3);
5340 }
5341
5342 /* [expr.cond]
5343
5344 One of the following shall hold:
5345
5346 --The second or the third operand (but not both) is a
5347 throw-expression (_except.throw_); the result is of the type
5348 and value category of the other.
5349
5350 --Both the second and the third operands have type void; the
5351 result is of type void and is a prvalue. */
5352 if (TREE_CODE (arg2) == THROW_EXPR
5353 && TREE_CODE (arg3) != THROW_EXPR)
5354 {
5355 result_type = arg3_type;
5356 is_glvalue = glvalue_p (arg3);
5357 }
5358 else if (TREE_CODE (arg2) != THROW_EXPR
5359 && TREE_CODE (arg3) == THROW_EXPR)
5360 {
5361 result_type = arg2_type;
5362 is_glvalue = glvalue_p (arg2);
5363 }
5364 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
5365 {
5366 result_type = void_type_node;
5367 is_glvalue = false;
5368 }
5369 else
5370 {
5371 if (complain & tf_error)
5372 {
5373 if (VOID_TYPE_P (arg2_type))
5374 error_at (cp_expr_loc_or_loc (arg3, loc),
5375 "second operand to the conditional operator "
5376 "is of type %<void%>, but the third operand is "
5377 "neither a throw-expression nor of type %<void%>");
5378 else
5379 error_at (cp_expr_loc_or_loc (arg2, loc),
5380 "third operand to the conditional operator "
5381 "is of type %<void%>, but the second operand is "
5382 "neither a throw-expression nor of type %<void%>");
5383 }
5384 return error_mark_node;
5385 }
5386
5387 goto valid_operands;
5388 }
5389 /* [expr.cond]
5390
5391 Otherwise, if the second and third operand have different types,
5392 and either has (possibly cv-qualified) class type, or if both are
5393 glvalues of the same value category and the same type except for
5394 cv-qualification, an attempt is made to convert each of those operands
5395 to the type of the other. */
5396 else if (!same_type_p (arg2_type, arg3_type)
5397 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)
5398 || (same_type_ignoring_top_level_qualifiers_p (arg2_type,
5399 arg3_type)
5400 && glvalue_p (arg2) && glvalue_p (arg3)
5401 && lvalue_p (arg2) == lvalue_p (arg3))))
5402 {
5403 conversion *conv2;
5404 conversion *conv3;
5405 bool converted = false;
5406
5407 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5408 p = conversion_obstack_alloc (0);
5409
5410 conv2 = conditional_conversion (arg2, arg3, complain);
5411 conv3 = conditional_conversion (arg3, arg2, complain);
5412
5413 /* [expr.cond]
5414
5415 If both can be converted, or one can be converted but the
5416 conversion is ambiguous, the program is ill-formed. If
5417 neither can be converted, the operands are left unchanged and
5418 further checking is performed as described below. If exactly
5419 one conversion is possible, that conversion is applied to the
5420 chosen operand and the converted operand is used in place of
5421 the original operand for the remainder of this section. */
5422 if ((conv2 && !conv2->bad_p
5423 && conv3 && !conv3->bad_p)
5424 || (conv2 && conv2->kind == ck_ambig)
5425 || (conv3 && conv3->kind == ck_ambig))
5426 {
5427 if (complain & tf_error)
5428 {
5429 error_at (loc, "operands to %<?:%> have different types "
5430 "%qT and %qT",
5431 arg2_type, arg3_type);
5432 if (conv2 && !conv2->bad_p && conv3 && !conv3->bad_p)
5433 inform (loc, " and each type can be converted to the other");
5434 else if (conv2 && conv2->kind == ck_ambig)
5435 convert_like (conv2, arg2, complain);
5436 else
5437 convert_like (conv3, arg3, complain);
5438 }
5439 result = error_mark_node;
5440 }
5441 else if (conv2 && !conv2->bad_p)
5442 {
5443 arg2 = convert_like (conv2, arg2, complain);
5444 arg2 = convert_from_reference (arg2);
5445 arg2_type = TREE_TYPE (arg2);
5446 /* Even if CONV2 is a valid conversion, the result of the
5447 conversion may be invalid. For example, if ARG3 has type
5448 "volatile X", and X does not have a copy constructor
5449 accepting a "volatile X&", then even if ARG2 can be
5450 converted to X, the conversion will fail. */
5451 if (error_operand_p (arg2))
5452 result = error_mark_node;
5453 converted = true;
5454 }
5455 else if (conv3 && !conv3->bad_p)
5456 {
5457 arg3 = convert_like (conv3, arg3, complain);
5458 arg3 = convert_from_reference (arg3);
5459 arg3_type = TREE_TYPE (arg3);
5460 if (error_operand_p (arg3))
5461 result = error_mark_node;
5462 converted = true;
5463 }
5464
5465 /* Free all the conversions we allocated. */
5466 obstack_free (&conversion_obstack, p);
5467
5468 if (result)
5469 return result;
5470
5471 /* If, after the conversion, both operands have class type,
5472 treat the cv-qualification of both operands as if it were the
5473 union of the cv-qualification of the operands.
5474
5475 The standard is not clear about what to do in this
5476 circumstance. For example, if the first operand has type
5477 "const X" and the second operand has a user-defined
5478 conversion to "volatile X", what is the type of the second
5479 operand after this step? Making it be "const X" (matching
5480 the first operand) seems wrong, as that discards the
5481 qualification without actually performing a copy. Leaving it
5482 as "volatile X" seems wrong as that will result in the
5483 conditional expression failing altogether, even though,
5484 according to this step, the one operand could be converted to
5485 the type of the other. */
5486 if (converted
5487 && CLASS_TYPE_P (arg2_type)
5488 && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
5489 arg2_type = arg3_type =
5490 cp_build_qualified_type (arg2_type,
5491 cp_type_quals (arg2_type)
5492 | cp_type_quals (arg3_type));
5493 }
5494
5495 /* [expr.cond]
5496
5497 If the second and third operands are glvalues of the same value
5498 category and have the same type, the result is of that type and
5499 value category. */
5500 if (((lvalue_p (arg2) && lvalue_p (arg3))
5501 || (xvalue_p (arg2) && xvalue_p (arg3)))
5502 && same_type_p (arg2_type, arg3_type))
5503 {
5504 result_type = arg2_type;
5505 arg2 = mark_lvalue_use (arg2);
5506 arg3 = mark_lvalue_use (arg3);
5507 goto valid_operands;
5508 }
5509
5510 /* [expr.cond]
5511
5512 Otherwise, the result is an rvalue. If the second and third
5513 operand do not have the same type, and either has (possibly
5514 cv-qualified) class type, overload resolution is used to
5515 determine the conversions (if any) to be applied to the operands
5516 (_over.match.oper_, _over.built_). */
5517 is_glvalue = false;
5518 if (!same_type_p (arg2_type, arg3_type)
5519 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
5520 {
5521 releasing_vec args;
5522 conversion *conv;
5523 bool any_viable_p;
5524
5525 /* Rearrange the arguments so that add_builtin_candidate only has
5526 to know about two args. In build_builtin_candidate, the
5527 arguments are unscrambled. */
5528 args->quick_push (arg2);
5529 args->quick_push (arg3);
5530 args->quick_push (arg1);
5531 add_builtin_candidates (&candidates,
5532 COND_EXPR,
5533 NOP_EXPR,
5534 ovl_op_identifier (false, COND_EXPR),
5535 args,
5536 LOOKUP_NORMAL, complain);
5537
5538 /* [expr.cond]
5539
5540 If the overload resolution fails, the program is
5541 ill-formed. */
5542 candidates = splice_viable (candidates, false, &any_viable_p);
5543 if (!any_viable_p)
5544 {
5545 if (complain & tf_error)
5546 error_at (loc, "operands to %<?:%> have different types %qT and %qT",
5547 arg2_type, arg3_type);
5548 return error_mark_node;
5549 }
5550 cand = tourney (candidates, complain);
5551 if (!cand)
5552 {
5553 if (complain & tf_error)
5554 {
5555 auto_diagnostic_group d;
5556 op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
5557 print_z_candidates (loc, candidates);
5558 }
5559 return error_mark_node;
5560 }
5561
5562 /* [expr.cond]
5563
5564 Otherwise, the conversions thus determined are applied, and
5565 the converted operands are used in place of the original
5566 operands for the remainder of this section. */
5567 conv = cand->convs[0];
5568 arg1 = convert_like (conv, arg1, complain);
5569 conv = cand->convs[1];
5570 arg2 = convert_like (conv, arg2, complain);
5571 arg2_type = TREE_TYPE (arg2);
5572 conv = cand->convs[2];
5573 arg3 = convert_like (conv, arg3, complain);
5574 arg3_type = TREE_TYPE (arg3);
5575 }
5576
5577 /* [expr.cond]
5578
5579 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
5580 and function-to-pointer (_conv.func_) standard conversions are
5581 performed on the second and third operands.
5582
5583 We need to force the lvalue-to-rvalue conversion here for class types,
5584 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
5585 that isn't wrapped with a TARGET_EXPR plays havoc with exception
5586 regions. */
5587
5588 arg2 = force_rvalue (arg2, complain);
5589 if (!CLASS_TYPE_P (arg2_type))
5590 arg2_type = TREE_TYPE (arg2);
5591
5592 arg3 = force_rvalue (arg3, complain);
5593 if (!CLASS_TYPE_P (arg3_type))
5594 arg3_type = TREE_TYPE (arg3);
5595
5596 if (arg2 == error_mark_node || arg3 == error_mark_node)
5597 return error_mark_node;
5598
5599 /* [expr.cond]
5600
5601 After those conversions, one of the following shall hold:
5602
5603 --The second and third operands have the same type; the result is of
5604 that type. */
5605 if (same_type_p (arg2_type, arg3_type))
5606 result_type = arg2_type;
5607 /* [expr.cond]
5608
5609 --The second and third operands have arithmetic or enumeration
5610 type; the usual arithmetic conversions are performed to bring
5611 them to a common type, and the result is of that type. */
5612 else if ((ARITHMETIC_TYPE_P (arg2_type)
5613 || UNSCOPED_ENUM_P (arg2_type))
5614 && (ARITHMETIC_TYPE_P (arg3_type)
5615 || UNSCOPED_ENUM_P (arg3_type)))
5616 {
5617 /* In this case, there is always a common type. */
5618 result_type = type_after_usual_arithmetic_conversions (arg2_type,
5619 arg3_type);
5620 if (complain & tf_warning)
5621 do_warn_double_promotion (result_type, arg2_type, arg3_type,
5622 "implicit conversion from %qH to %qI to "
5623 "match other result of conditional",
5624 loc);
5625
5626 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
5627 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
5628 {
5629 tree stripped_orig_arg2 = tree_strip_any_location_wrapper (orig_arg2);
5630 tree stripped_orig_arg3 = tree_strip_any_location_wrapper (orig_arg3);
5631 if (TREE_CODE (stripped_orig_arg2) == CONST_DECL
5632 && TREE_CODE (stripped_orig_arg3) == CONST_DECL
5633 && (DECL_CONTEXT (stripped_orig_arg2)
5634 == DECL_CONTEXT (stripped_orig_arg3)))
5635 /* Two enumerators from the same enumeration can have different
5636 types when the enumeration is still being defined. */;
5637 else if (complain & tf_warning)
5638 warning_at (loc, OPT_Wenum_compare, "enumerated mismatch "
5639 "in conditional expression: %qT vs %qT",
5640 arg2_type, arg3_type);
5641 }
5642 else if (extra_warnings
5643 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
5644 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
5645 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
5646 && !same_type_p (arg2_type,
5647 type_promotes_to (arg3_type)))))
5648 {
5649 if (complain & tf_warning)
5650 warning_at (loc, OPT_Wextra, "enumerated and non-enumerated "
5651 "type in conditional expression");
5652 }
5653
5654 arg2 = perform_implicit_conversion (result_type, arg2, complain);
5655 arg3 = perform_implicit_conversion (result_type, arg3, complain);
5656 }
5657 /* [expr.cond]
5658
5659 --The second and third operands have pointer type, or one has
5660 pointer type and the other is a null pointer constant; pointer
5661 conversions (_conv.ptr_) and qualification conversions
5662 (_conv.qual_) are performed to bring them to their composite
5663 pointer type (_expr.rel_). The result is of the composite
5664 pointer type.
5665
5666 --The second and third operands have pointer to member type, or
5667 one has pointer to member type and the other is a null pointer
5668 constant; pointer to member conversions (_conv.mem_) and
5669 qualification conversions (_conv.qual_) are performed to bring
5670 them to a common type, whose cv-qualification shall match the
5671 cv-qualification of either the second or the third operand.
5672 The result is of the common type. */
5673 else if ((null_ptr_cst_p (arg2)
5674 && TYPE_PTR_OR_PTRMEM_P (arg3_type))
5675 || (null_ptr_cst_p (arg3)
5676 && TYPE_PTR_OR_PTRMEM_P (arg2_type))
5677 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
5678 || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type))
5679 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
5680 {
5681 result_type = composite_pointer_type (loc,
5682 arg2_type, arg3_type, arg2,
5683 arg3, CPO_CONDITIONAL_EXPR,
5684 complain);
5685 if (result_type == error_mark_node)
5686 return error_mark_node;
5687 arg2 = perform_implicit_conversion (result_type, arg2, complain);
5688 arg3 = perform_implicit_conversion (result_type, arg3, complain);
5689 }
5690
5691 if (!result_type)
5692 {
5693 if (complain & tf_error)
5694 error_at (loc, "operands to %<?:%> have different types %qT and %qT",
5695 arg2_type, arg3_type);
5696 return error_mark_node;
5697 }
5698
5699 if (arg2 == error_mark_node || arg3 == error_mark_node)
5700 return error_mark_node;
5701
5702 valid_operands:
5703 if (processing_template_decl && is_glvalue)
5704 {
5705 /* Let lvalue_kind know this was a glvalue. */
5706 tree arg = (result_type == arg2_type ? arg2 : arg3);
5707 result_type = cp_build_reference_type (result_type, xvalue_p (arg));
5708 }
5709
5710 result = build3_loc (loc, COND_EXPR, result_type, arg1, arg2, arg3);
5711
5712 /* If the ARG2 and ARG3 are the same and don't have side-effects,
5713 warn here, because the COND_EXPR will be turned into ARG2. */
5714 if (warn_duplicated_branches
5715 && (complain & tf_warning)
5716 && (arg2 == arg3 || operand_equal_p (arg2, arg3, 0)))
5717 warning_at (EXPR_LOCATION (result), OPT_Wduplicated_branches,
5718 "this condition has identical branches");
5719
5720 /* We can't use result_type below, as fold might have returned a
5721 throw_expr. */
5722
5723 if (!is_glvalue)
5724 {
5725 /* Expand both sides into the same slot, hopefully the target of
5726 the ?: expression. We used to check for TARGET_EXPRs here,
5727 but now we sometimes wrap them in NOP_EXPRs so the test would
5728 fail. */
5729 if (CLASS_TYPE_P (TREE_TYPE (result)))
5730 result = get_target_expr_sfinae (result, complain);
5731 /* If this expression is an rvalue, but might be mistaken for an
5732 lvalue, we must add a NON_LVALUE_EXPR. */
5733 result = rvalue (result);
5734 }
5735 else
5736 result = force_paren_expr (result);
5737
5738 return result;
5739 }
5740
5741 /* Wrapper for above. */
5742
5743 tree
5744 build_conditional_expr (const op_location_t &loc,
5745 tree arg1, tree arg2, tree arg3,
5746 tsubst_flags_t complain)
5747 {
5748 tree ret;
5749 bool subtime = timevar_cond_start (TV_OVERLOAD);
5750 ret = build_conditional_expr_1 (loc, arg1, arg2, arg3, complain);
5751 timevar_cond_stop (TV_OVERLOAD, subtime);
5752 return ret;
5753 }
5754
5755 /* OPERAND is an operand to an expression. Perform necessary steps
5756 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
5757 returned. */
5758
5759 static tree
5760 prep_operand (tree operand)
5761 {
5762 if (operand)
5763 {
5764 if (CLASS_TYPE_P (TREE_TYPE (operand))
5765 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
5766 /* Make sure the template type is instantiated now. */
5767 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
5768 }
5769
5770 return operand;
5771 }
5772
5773 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
5774 OVERLOAD) to the CANDIDATES, returning an updated list of
5775 CANDIDATES. The ARGS are the arguments provided to the call;
5776 if FIRST_ARG is non-null it is the implicit object argument,
5777 otherwise the first element of ARGS is used if needed. The
5778 EXPLICIT_TARGS are explicit template arguments provided.
5779 TEMPLATE_ONLY is true if only template functions should be
5780 considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
5781 add_function_candidate. */
5782
5783 static void
5784 add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args,
5785 tree return_type,
5786 tree explicit_targs, bool template_only,
5787 tree conversion_path, tree access_path,
5788 int flags,
5789 struct z_candidate **candidates,
5790 tsubst_flags_t complain)
5791 {
5792 tree ctype;
5793 const vec<tree, va_gc> *non_static_args;
5794 bool check_list_ctor = false;
5795 bool check_converting = false;
5796 unification_kind_t strict;
5797
5798 if (!fns)
5799 return;
5800
5801 /* Precalculate special handling of constructors and conversion ops. */
5802 tree fn = OVL_FIRST (fns);
5803 if (DECL_CONV_FN_P (fn))
5804 {
5805 check_list_ctor = false;
5806 check_converting = (flags & LOOKUP_ONLYCONVERTING) != 0;
5807 if (flags & LOOKUP_NO_CONVERSION)
5808 /* We're doing return_type(x). */
5809 strict = DEDUCE_CONV;
5810 else
5811 /* We're doing x.operator return_type(). */
5812 strict = DEDUCE_EXACT;
5813 /* [over.match.funcs] For conversion functions, the function
5814 is considered to be a member of the class of the implicit
5815 object argument for the purpose of defining the type of
5816 the implicit object parameter. */
5817 ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg));
5818 }
5819 else
5820 {
5821 if (DECL_CONSTRUCTOR_P (fn))
5822 {
5823 check_list_ctor = (flags & LOOKUP_LIST_ONLY) != 0;
5824 /* For list-initialization we consider explicit constructors
5825 and complain if one is chosen. */
5826 check_converting
5827 = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
5828 == LOOKUP_ONLYCONVERTING);
5829 }
5830 strict = DEDUCE_CALL;
5831 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
5832 }
5833
5834 if (first_arg)
5835 non_static_args = args;
5836 else
5837 /* Delay creating the implicit this parameter until it is needed. */
5838 non_static_args = NULL;
5839
5840 for (lkp_iterator iter (fns); iter; ++iter)
5841 {
5842 fn = *iter;
5843
5844 if (check_converting && DECL_NONCONVERTING_P (fn))
5845 continue;
5846 if (check_list_ctor && !is_list_ctor (fn))
5847 continue;
5848
5849 tree fn_first_arg = NULL_TREE;
5850 const vec<tree, va_gc> *fn_args = args;
5851
5852 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
5853 {
5854 /* Figure out where the object arg comes from. If this
5855 function is a non-static member and we didn't get an
5856 implicit object argument, move it out of args. */
5857 if (first_arg == NULL_TREE)
5858 {
5859 unsigned int ix;
5860 tree arg;
5861 vec<tree, va_gc> *tempvec;
5862 vec_alloc (tempvec, args->length () - 1);
5863 for (ix = 1; args->iterate (ix, &arg); ++ix)
5864 tempvec->quick_push (arg);
5865 non_static_args = tempvec;
5866 first_arg = (*args)[0];
5867 }
5868
5869 fn_first_arg = first_arg;
5870 fn_args = non_static_args;
5871 }
5872
5873 /* Don't bother reversing an operator with two identical parameters. */
5874 else if (vec_safe_length (args) == 2 && (flags & LOOKUP_REVERSED))
5875 {
5876 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
5877 if (same_type_p (TREE_VALUE (parmlist),
5878 TREE_VALUE (TREE_CHAIN (parmlist))))
5879 continue;
5880 }
5881
5882 if (TREE_CODE (fn) == TEMPLATE_DECL)
5883 add_template_candidate (candidates,
5884 fn,
5885 ctype,
5886 explicit_targs,
5887 fn_first_arg,
5888 fn_args,
5889 return_type,
5890 access_path,
5891 conversion_path,
5892 flags,
5893 strict,
5894 complain);
5895 else if (!template_only)
5896 add_function_candidate (candidates,
5897 fn,
5898 ctype,
5899 fn_first_arg,
5900 fn_args,
5901 access_path,
5902 conversion_path,
5903 flags,
5904 NULL,
5905 complain);
5906 }
5907 }
5908
5909 /* Returns 1 if P0145R2 says that the LHS of operator CODE is evaluated first,
5910 -1 if the RHS is evaluated first, or 0 if the order is unspecified. */
5911
5912 static int
5913 op_is_ordered (tree_code code)
5914 {
5915 switch (code)
5916 {
5917 // 5. b @= a
5918 case MODIFY_EXPR:
5919 return (flag_strong_eval_order > 1 ? -1 : 0);
5920
5921 // 6. a[b]
5922 case ARRAY_REF:
5923 return (flag_strong_eval_order > 1 ? 1 : 0);
5924
5925 // 1. a.b
5926 // Not overloadable (yet).
5927 // 2. a->b
5928 // Only one argument.
5929 // 3. a->*b
5930 case MEMBER_REF:
5931 // 7. a << b
5932 case LSHIFT_EXPR:
5933 // 8. a >> b
5934 case RSHIFT_EXPR:
5935 return (flag_strong_eval_order ? 1 : 0);
5936
5937 default:
5938 return 0;
5939 }
5940 }
5941
5942 /* Subroutine of build_new_op_1: Add to CANDIDATES all candidates for the
5943 operator indicated by CODE/CODE2. This function calls itself recursively to
5944 handle C++20 rewritten comparison operator candidates. */
5945
5946 static tree
5947 add_operator_candidates (z_candidate **candidates,
5948 tree_code code, tree_code code2,
5949 vec<tree, va_gc> *arglist,
5950 int flags, tsubst_flags_t complain)
5951 {
5952 z_candidate *start_candidates = *candidates;
5953 bool ismodop = code2 != ERROR_MARK;
5954 tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
5955
5956 /* LOOKUP_REWRITTEN is set when we're looking for the == or <=> operator to
5957 rewrite from, and also when we're looking for the e.g. < operator to use
5958 on the result of <=>. In the latter case, we don't want the flag set in
5959 the candidate, we just want to suppress looking for rewrites. */
5960 bool rewritten = (flags & LOOKUP_REWRITTEN);
5961 if (rewritten && code != EQ_EXPR && code != SPACESHIP_EXPR)
5962 flags &= ~LOOKUP_REWRITTEN;
5963
5964 bool memonly = false;
5965 switch (code)
5966 {
5967 /* =, ->, [], () must be non-static member functions. */
5968 case MODIFY_EXPR:
5969 if (code2 != NOP_EXPR)
5970 break;
5971 /* FALLTHRU */
5972 case COMPONENT_REF:
5973 case ARRAY_REF:
5974 memonly = true;
5975 break;
5976
5977 default:
5978 break;
5979 }
5980
5981 /* Add namespace-scope operators to the list of functions to
5982 consider. */
5983 if (!memonly)
5984 {
5985 tree fns = lookup_name_real (fnname, LOOK_where::BLOCK_NAMESPACE,
5986 0, 0, 0);
5987 fns = lookup_arg_dependent (fnname, fns, arglist);
5988 add_candidates (fns, NULL_TREE, arglist, NULL_TREE,
5989 NULL_TREE, false, NULL_TREE, NULL_TREE,
5990 flags, candidates, complain);
5991 }
5992
5993 /* Add class-member operators to the candidate set. */
5994 tree arg1_type = TREE_TYPE ((*arglist)[0]);
5995 unsigned nargs = arglist->length () > 1 ? 2 : 1;
5996 tree arg2_type = nargs > 1 ? TREE_TYPE ((*arglist)[1]) : NULL_TREE;
5997 if (CLASS_TYPE_P (arg1_type))
5998 {
5999 tree fns = lookup_fnfields (arg1_type, fnname, 1, complain);
6000 if (fns == error_mark_node)
6001 return error_mark_node;
6002 if (fns)
6003 add_candidates (BASELINK_FUNCTIONS (fns),
6004 NULL_TREE, arglist, NULL_TREE,
6005 NULL_TREE, false,
6006 BASELINK_BINFO (fns),
6007 BASELINK_ACCESS_BINFO (fns),
6008 flags, candidates, complain);
6009 }
6010 /* Per [over.match.oper]3.2, if no operand has a class type, then
6011 only non-member functions that have type T1 or reference to
6012 cv-qualified-opt T1 for the first argument, if the first argument
6013 has an enumeration type, or T2 or reference to cv-qualified-opt
6014 T2 for the second argument, if the second argument has an
6015 enumeration type. Filter out those that don't match. */
6016 else if (! arg2_type || ! CLASS_TYPE_P (arg2_type))
6017 {
6018 struct z_candidate **candp, **next;
6019
6020 for (candp = candidates; *candp != start_candidates; candp = next)
6021 {
6022 unsigned i;
6023 z_candidate *cand = *candp;
6024 next = &cand->next;
6025
6026 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
6027
6028 for (i = 0; i < nargs; ++i)
6029 {
6030 tree parmtype = TREE_VALUE (parmlist);
6031 tree argtype = unlowered_expr_type ((*arglist)[i]);
6032
6033 if (TYPE_REF_P (parmtype))
6034 parmtype = TREE_TYPE (parmtype);
6035 if (TREE_CODE (argtype) == ENUMERAL_TYPE
6036 && (same_type_ignoring_top_level_qualifiers_p
6037 (argtype, parmtype)))
6038 break;
6039
6040 parmlist = TREE_CHAIN (parmlist);
6041 }
6042
6043 /* No argument has an appropriate type, so remove this
6044 candidate function from the list. */
6045 if (i == nargs)
6046 {
6047 *candp = cand->next;
6048 next = candp;
6049 }
6050 }
6051 }
6052
6053 if (!rewritten)
6054 {
6055 /* The standard says to rewrite built-in candidates, too,
6056 but there's no point. */
6057 add_builtin_candidates (candidates, code, code2, fnname, arglist,
6058 flags, complain);
6059
6060 /* Maybe add C++20 rewritten comparison candidates. */
6061 tree_code rewrite_code = ERROR_MARK;
6062 if (cxx_dialect >= cxx20
6063 && nargs == 2
6064 && (OVERLOAD_TYPE_P (arg1_type) || OVERLOAD_TYPE_P (arg2_type)))
6065 switch (code)
6066 {
6067 case LT_EXPR:
6068 case LE_EXPR:
6069 case GT_EXPR:
6070 case GE_EXPR:
6071 case SPACESHIP_EXPR:
6072 rewrite_code = SPACESHIP_EXPR;
6073 break;
6074
6075 case NE_EXPR:
6076 case EQ_EXPR:
6077 rewrite_code = EQ_EXPR;
6078 break;
6079
6080 default:;
6081 }
6082
6083 if (rewrite_code)
6084 {
6085 flags |= LOOKUP_REWRITTEN;
6086 if (rewrite_code != code)
6087 /* Add rewritten candidates in same order. */
6088 add_operator_candidates (candidates, rewrite_code, ERROR_MARK,
6089 arglist, flags, complain);
6090
6091 z_candidate *save_cand = *candidates;
6092
6093 /* Add rewritten candidates in reverse order. */
6094 flags |= LOOKUP_REVERSED;
6095 vec<tree,va_gc> *revlist = make_tree_vector ();
6096 revlist->quick_push ((*arglist)[1]);
6097 revlist->quick_push ((*arglist)[0]);
6098 add_operator_candidates (candidates, rewrite_code, ERROR_MARK,
6099 revlist, flags, complain);
6100
6101 /* Release the vec if we didn't add a candidate that uses it. */
6102 for (z_candidate *c = *candidates; c != save_cand; c = c->next)
6103 if (c->args == revlist)
6104 {
6105 revlist = NULL;
6106 break;
6107 }
6108 release_tree_vector (revlist);
6109 }
6110 }
6111
6112 return NULL_TREE;
6113 }
6114
6115 static tree
6116 build_new_op_1 (const op_location_t &loc, enum tree_code code, int flags,
6117 tree arg1, tree arg2, tree arg3, tree *overload,
6118 tsubst_flags_t complain)
6119 {
6120 struct z_candidate *candidates = 0, *cand;
6121 vec<tree, va_gc> *arglist;
6122 tree result = NULL_TREE;
6123 bool result_valid_p = false;
6124 enum tree_code code2 = ERROR_MARK;
6125 enum tree_code code_orig_arg1 = ERROR_MARK;
6126 enum tree_code code_orig_arg2 = ERROR_MARK;
6127 conversion *conv;
6128 void *p;
6129 bool strict_p;
6130 bool any_viable_p;
6131
6132 if (error_operand_p (arg1)
6133 || error_operand_p (arg2)
6134 || error_operand_p (arg3))
6135 return error_mark_node;
6136
6137 bool ismodop = code == MODIFY_EXPR;
6138 if (ismodop)
6139 {
6140 code2 = TREE_CODE (arg3);
6141 arg3 = NULL_TREE;
6142 }
6143
6144 tree arg1_type = unlowered_expr_type (arg1);
6145 tree arg2_type = arg2 ? unlowered_expr_type (arg2) : NULL_TREE;
6146
6147 arg1 = prep_operand (arg1);
6148
6149 switch (code)
6150 {
6151 case NEW_EXPR:
6152 case VEC_NEW_EXPR:
6153 case VEC_DELETE_EXPR:
6154 case DELETE_EXPR:
6155 /* Use build_op_new_call and build_op_delete_call instead. */
6156 gcc_unreachable ();
6157
6158 case CALL_EXPR:
6159 /* Use build_op_call instead. */
6160 gcc_unreachable ();
6161
6162 case TRUTH_ORIF_EXPR:
6163 case TRUTH_ANDIF_EXPR:
6164 case TRUTH_AND_EXPR:
6165 case TRUTH_OR_EXPR:
6166 /* These are saved for the sake of warn_logical_operator. */
6167 code_orig_arg1 = TREE_CODE (arg1);
6168 code_orig_arg2 = TREE_CODE (arg2);
6169 break;
6170 case GT_EXPR:
6171 case LT_EXPR:
6172 case GE_EXPR:
6173 case LE_EXPR:
6174 case EQ_EXPR:
6175 case NE_EXPR:
6176 /* These are saved for the sake of maybe_warn_bool_compare. */
6177 code_orig_arg1 = TREE_CODE (arg1_type);
6178 code_orig_arg2 = TREE_CODE (arg2_type);
6179 break;
6180
6181 default:
6182 break;
6183 }
6184
6185 arg2 = prep_operand (arg2);
6186 arg3 = prep_operand (arg3);
6187
6188 if (code == COND_EXPR)
6189 /* Use build_conditional_expr instead. */
6190 gcc_unreachable ();
6191 else if (! OVERLOAD_TYPE_P (arg1_type)
6192 && (! arg2 || ! OVERLOAD_TYPE_P (arg2_type)))
6193 goto builtin;
6194
6195 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
6196 {
6197 arg2 = integer_zero_node;
6198 arg2_type = integer_type_node;
6199 }
6200
6201 vec_alloc (arglist, 3);
6202 arglist->quick_push (arg1);
6203 if (arg2 != NULL_TREE)
6204 arglist->quick_push (arg2);
6205 if (arg3 != NULL_TREE)
6206 arglist->quick_push (arg3);
6207
6208 /* Get the high-water mark for the CONVERSION_OBSTACK. */
6209 p = conversion_obstack_alloc (0);
6210
6211 result = add_operator_candidates (&candidates, code, code2, arglist,
6212 flags, complain);
6213 if (result == error_mark_node)
6214 goto user_defined_result_ready;
6215
6216 switch (code)
6217 {
6218 case COMPOUND_EXPR:
6219 case ADDR_EXPR:
6220 /* For these, the built-in candidates set is empty
6221 [over.match.oper]/3. We don't want non-strict matches
6222 because exact matches are always possible with built-in
6223 operators. The built-in candidate set for COMPONENT_REF
6224 would be empty too, but since there are no such built-in
6225 operators, we accept non-strict matches for them. */
6226 strict_p = true;
6227 break;
6228
6229 default:
6230 strict_p = false;
6231 break;
6232 }
6233
6234 candidates = splice_viable (candidates, strict_p, &any_viable_p);
6235 if (!any_viable_p)
6236 {
6237 switch (code)
6238 {
6239 case POSTINCREMENT_EXPR:
6240 case POSTDECREMENT_EXPR:
6241 /* Don't try anything fancy if we're not allowed to produce
6242 errors. */
6243 if (!(complain & tf_error))
6244 return error_mark_node;
6245
6246 /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
6247 distinguish between prefix and postfix ++ and
6248 operator++() was used for both, so we allow this with
6249 -fpermissive. */
6250 else
6251 {
6252 tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
6253 const char *msg = (flag_permissive)
6254 ? G_("no %<%D(int)%> declared for postfix %qs,"
6255 " trying prefix operator instead")
6256 : G_("no %<%D(int)%> declared for postfix %qs");
6257 permerror (loc, msg, fnname, OVL_OP_INFO (false, code)->name);
6258 }
6259
6260 if (!flag_permissive)
6261 return error_mark_node;
6262
6263 if (code == POSTINCREMENT_EXPR)
6264 code = PREINCREMENT_EXPR;
6265 else
6266 code = PREDECREMENT_EXPR;
6267 result = build_new_op_1 (loc, code, flags, arg1, NULL_TREE,
6268 NULL_TREE, overload, complain);
6269 break;
6270
6271 /* The caller will deal with these. */
6272 case ADDR_EXPR:
6273 case COMPOUND_EXPR:
6274 case COMPONENT_REF:
6275 case CO_AWAIT_EXPR:
6276 result = NULL_TREE;
6277 result_valid_p = true;
6278 break;
6279
6280 default:
6281 if (complain & tf_error)
6282 {
6283 /* If one of the arguments of the operator represents
6284 an invalid use of member function pointer, try to report
6285 a meaningful error ... */
6286 if (invalid_nonstatic_memfn_p (loc, arg1, tf_error)
6287 || invalid_nonstatic_memfn_p (loc, arg2, tf_error)
6288 || invalid_nonstatic_memfn_p (loc, arg3, tf_error))
6289 /* We displayed the error message. */;
6290 else
6291 {
6292 /* ... Otherwise, report the more generic
6293 "no matching operator found" error */
6294 auto_diagnostic_group d;
6295 op_error (loc, code, code2, arg1, arg2, arg3, FALSE);
6296 print_z_candidates (loc, candidates);
6297 }
6298 }
6299 result = error_mark_node;
6300 break;
6301 }
6302 }
6303 else
6304 {
6305 cand = tourney (candidates, complain);
6306 if (cand == 0)
6307 {
6308 if (complain & tf_error)
6309 {
6310 auto_diagnostic_group d;
6311 op_error (loc, code, code2, arg1, arg2, arg3, TRUE);
6312 print_z_candidates (loc, candidates);
6313 }
6314 result = error_mark_node;
6315 }
6316 else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
6317 {
6318 if (overload)
6319 *overload = cand->fn;
6320
6321 if (resolve_args (arglist, complain) == NULL)
6322 result = error_mark_node;
6323 else
6324 {
6325 tsubst_flags_t ocomplain = complain;
6326 if (cand->rewritten ())
6327 /* We'll wrap this call in another one. */
6328 ocomplain &= ~tf_decltype;
6329 if (cand->reversed ())
6330 {
6331 /* We swapped these in add_candidate, swap them back now. */
6332 std::swap (cand->convs[0], cand->convs[1]);
6333 if (cand->fn == current_function_decl)
6334 warning_at (loc, 0, "in C++20 this comparison calls the "
6335 "current function recursively with reversed "
6336 "arguments");
6337 }
6338 result = build_over_call (cand, LOOKUP_NORMAL, ocomplain);
6339 }
6340
6341 if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn))
6342 /* There won't be a CALL_EXPR. */;
6343 else if (result && result != error_mark_node)
6344 {
6345 tree call = extract_call_expr (result);
6346 CALL_EXPR_OPERATOR_SYNTAX (call) = true;
6347
6348 if (processing_template_decl && DECL_HIDDEN_FRIEND_P (cand->fn))
6349 /* This prevents build_new_function_call from discarding this
6350 function during instantiation of the enclosing template. */
6351 KOENIG_LOOKUP_P (call) = 1;
6352
6353 /* Specify evaluation order as per P0145R2. */
6354 CALL_EXPR_ORDERED_ARGS (call) = false;
6355 switch (op_is_ordered (code))
6356 {
6357 case -1:
6358 CALL_EXPR_REVERSE_ARGS (call) = true;
6359 break;
6360
6361 case 1:
6362 CALL_EXPR_ORDERED_ARGS (call) = true;
6363 break;
6364
6365 default:
6366 break;
6367 }
6368 }
6369
6370 /* If this was a C++20 rewritten comparison, adjust the result. */
6371 if (cand->rewritten ())
6372 {
6373 /* FIXME build_min_non_dep_op_overload can't handle rewrites. */
6374 if (overload)
6375 *overload = NULL_TREE;
6376 switch (code)
6377 {
6378 case EQ_EXPR:
6379 gcc_checking_assert (cand->reversed ());
6380 gcc_fallthrough ();
6381 case NE_EXPR:
6382 /* If a rewritten operator== candidate is selected by
6383 overload resolution for an operator @, its return type
6384 shall be cv bool.... */
6385 if (TREE_CODE (TREE_TYPE (result)) != BOOLEAN_TYPE)
6386 {
6387 if (complain & tf_error)
6388 {
6389 auto_diagnostic_group d;
6390 error_at (loc, "return type of %qD is not %qs",
6391 cand->fn, "bool");
6392 inform (loc, "used as rewritten candidate for "
6393 "comparison of %qT and %qT",
6394 arg1_type, arg2_type);
6395 }
6396 result = error_mark_node;
6397 }
6398 else if (code == NE_EXPR)
6399 /* !(y == x) or !(x == y) */
6400 result = build1_loc (loc, TRUTH_NOT_EXPR,
6401 boolean_type_node, result);
6402 break;
6403
6404 /* If a rewritten operator<=> candidate is selected by
6405 overload resolution for an operator @, x @ y is
6406 interpreted as 0 @ (y <=> x) if the selected candidate is
6407 a synthesized candidate with reversed order of parameters,
6408 or (x <=> y) @ 0 otherwise, using the selected rewritten
6409 operator<=> candidate. */
6410 case SPACESHIP_EXPR:
6411 if (!cand->reversed ())
6412 /* We're in the build_new_op call below for an outer
6413 reversed call; we don't need to do anything more. */
6414 break;
6415 gcc_fallthrough ();
6416 case LT_EXPR:
6417 case LE_EXPR:
6418 case GT_EXPR:
6419 case GE_EXPR:
6420 {
6421 tree lhs = result;
6422 tree rhs = integer_zero_node;
6423 if (cand->reversed ())
6424 std::swap (lhs, rhs);
6425 warning_sentinel ws (warn_zero_as_null_pointer_constant);
6426 result = build_new_op (loc, code,
6427 LOOKUP_NORMAL|LOOKUP_REWRITTEN,
6428 lhs, rhs, NULL_TREE,
6429 NULL, complain);
6430 }
6431 break;
6432
6433 default:
6434 gcc_unreachable ();
6435 }
6436 }
6437 }
6438 else
6439 {
6440 /* Give any warnings we noticed during overload resolution. */
6441 if (cand->warnings && (complain & tf_warning))
6442 {
6443 struct candidate_warning *w;
6444 for (w = cand->warnings; w; w = w->next)
6445 joust (cand, w->loser, 1, complain);
6446 }
6447
6448 /* Check for comparison of different enum types. */
6449 switch (code)
6450 {
6451 case GT_EXPR:
6452 case LT_EXPR:
6453 case GE_EXPR:
6454 case LE_EXPR:
6455 case EQ_EXPR:
6456 case NE_EXPR:
6457 if (TREE_CODE (arg1_type) == ENUMERAL_TYPE
6458 && TREE_CODE (arg2_type) == ENUMERAL_TYPE
6459 && (TYPE_MAIN_VARIANT (arg1_type)
6460 != TYPE_MAIN_VARIANT (arg2_type))
6461 && (complain & tf_warning))
6462 warning_at (loc, OPT_Wenum_compare,
6463 "comparison between %q#T and %q#T",
6464 arg1_type, arg2_type);
6465 break;
6466 default:
6467 break;
6468 }
6469
6470 /* "If a built-in candidate is selected by overload resolution, the
6471 operands of class type are converted to the types of the
6472 corresponding parameters of the selected operation function,
6473 except that the second standard conversion sequence of a
6474 user-defined conversion sequence (12.3.3.1.2) is not applied." */
6475 conv = cand->convs[0];
6476 if (conv->user_conv_p)
6477 {
6478 conv = strip_standard_conversion (conv);
6479 arg1 = convert_like (conv, arg1, complain);
6480 }
6481
6482 if (arg2)
6483 {
6484 conv = cand->convs[1];
6485 if (conv->user_conv_p)
6486 {
6487 conv = strip_standard_conversion (conv);
6488 arg2 = convert_like (conv, arg2, complain);
6489 }
6490 }
6491
6492 if (arg3)
6493 {
6494 conv = cand->convs[2];
6495 if (conv->user_conv_p)
6496 {
6497 conv = strip_standard_conversion (conv);
6498 arg3 = convert_like (conv, arg3, complain);
6499 }
6500 }
6501 }
6502 }
6503
6504 user_defined_result_ready:
6505
6506 /* Free all the conversions we allocated. */
6507 obstack_free (&conversion_obstack, p);
6508
6509 if (result || result_valid_p)
6510 return result;
6511
6512 builtin:
6513 switch (code)
6514 {
6515 case MODIFY_EXPR:
6516 return cp_build_modify_expr (loc, arg1, code2, arg2, complain);
6517
6518 case INDIRECT_REF:
6519 return cp_build_indirect_ref (loc, arg1, RO_UNARY_STAR, complain);
6520
6521 case TRUTH_ANDIF_EXPR:
6522 case TRUTH_ORIF_EXPR:
6523 case TRUTH_AND_EXPR:
6524 case TRUTH_OR_EXPR:
6525 if (complain & tf_warning)
6526 warn_logical_operator (loc, code, boolean_type_node,
6527 code_orig_arg1, arg1,
6528 code_orig_arg2, arg2);
6529 /* Fall through. */
6530 case GT_EXPR:
6531 case LT_EXPR:
6532 case GE_EXPR:
6533 case LE_EXPR:
6534 case EQ_EXPR:
6535 case NE_EXPR:
6536 if ((complain & tf_warning)
6537 && ((code_orig_arg1 == BOOLEAN_TYPE)
6538 ^ (code_orig_arg2 == BOOLEAN_TYPE)))
6539 maybe_warn_bool_compare (loc, code, arg1, arg2);
6540 if (complain & tf_warning && warn_tautological_compare)
6541 warn_tautological_cmp (loc, code, arg1, arg2);
6542 /* Fall through. */
6543 case SPACESHIP_EXPR:
6544 case PLUS_EXPR:
6545 case MINUS_EXPR:
6546 case MULT_EXPR:
6547 case TRUNC_DIV_EXPR:
6548 case MAX_EXPR:
6549 case MIN_EXPR:
6550 case LSHIFT_EXPR:
6551 case RSHIFT_EXPR:
6552 case TRUNC_MOD_EXPR:
6553 case BIT_AND_EXPR:
6554 case BIT_IOR_EXPR:
6555 case BIT_XOR_EXPR:
6556 return cp_build_binary_op (loc, code, arg1, arg2, complain);
6557
6558 case UNARY_PLUS_EXPR:
6559 case NEGATE_EXPR:
6560 case BIT_NOT_EXPR:
6561 case TRUTH_NOT_EXPR:
6562 case PREINCREMENT_EXPR:
6563 case POSTINCREMENT_EXPR:
6564 case PREDECREMENT_EXPR:
6565 case POSTDECREMENT_EXPR:
6566 case REALPART_EXPR:
6567 case IMAGPART_EXPR:
6568 case ABS_EXPR:
6569 case CO_AWAIT_EXPR:
6570 return cp_build_unary_op (code, arg1, false, complain);
6571
6572 case ARRAY_REF:
6573 return cp_build_array_ref (input_location, arg1, arg2, complain);
6574
6575 case MEMBER_REF:
6576 return build_m_component_ref (cp_build_indirect_ref (loc, arg1,
6577 RO_ARROW_STAR,
6578 complain),
6579 arg2, complain);
6580
6581 /* The caller will deal with these. */
6582 case ADDR_EXPR:
6583 case COMPONENT_REF:
6584 case COMPOUND_EXPR:
6585 return NULL_TREE;
6586
6587 default:
6588 gcc_unreachable ();
6589 }
6590 return NULL_TREE;
6591 }
6592
6593 /* Wrapper for above. */
6594
6595 tree
6596 build_new_op (const op_location_t &loc, enum tree_code code, int flags,
6597 tree arg1, tree arg2, tree arg3,
6598 tree *overload, tsubst_flags_t complain)
6599 {
6600 tree ret;
6601 bool subtime = timevar_cond_start (TV_OVERLOAD);
6602 ret = build_new_op_1 (loc, code, flags, arg1, arg2, arg3,
6603 overload, complain);
6604 timevar_cond_stop (TV_OVERLOAD, subtime);
6605 return ret;
6606 }
6607
6608 /* CALL was returned by some call-building function; extract the actual
6609 CALL_EXPR from any bits that have been tacked on, e.g. by
6610 convert_from_reference. */
6611
6612 tree
6613 extract_call_expr (tree call)
6614 {
6615 while (TREE_CODE (call) == COMPOUND_EXPR)
6616 call = TREE_OPERAND (call, 1);
6617 if (REFERENCE_REF_P (call))
6618 call = TREE_OPERAND (call, 0);
6619 if (TREE_CODE (call) == TARGET_EXPR)
6620 call = TARGET_EXPR_INITIAL (call);
6621 if (cxx_dialect >= cxx20)
6622 switch (TREE_CODE (call))
6623 {
6624 /* C++20 rewritten comparison operators. */
6625 case TRUTH_NOT_EXPR:
6626 call = TREE_OPERAND (call, 0);
6627 break;
6628 case LT_EXPR:
6629 case LE_EXPR:
6630 case GT_EXPR:
6631 case GE_EXPR:
6632 case SPACESHIP_EXPR:
6633 {
6634 tree op0 = TREE_OPERAND (call, 0);
6635 if (integer_zerop (op0))
6636 call = TREE_OPERAND (call, 1);
6637 else
6638 call = op0;
6639 }
6640 break;
6641 default:;
6642 }
6643
6644 gcc_assert (TREE_CODE (call) == CALL_EXPR
6645 || TREE_CODE (call) == AGGR_INIT_EXPR
6646 || call == error_mark_node);
6647 return call;
6648 }
6649
6650 /* Returns true if FN has two parameters, of which the second has type
6651 size_t. */
6652
6653 static bool
6654 second_parm_is_size_t (tree fn)
6655 {
6656 tree t = FUNCTION_ARG_CHAIN (fn);
6657 if (!t || !same_type_p (TREE_VALUE (t), size_type_node))
6658 return false;
6659 t = TREE_CHAIN (t);
6660 if (t == void_list_node)
6661 return true;
6662 return false;
6663 }
6664
6665 /* True if T, an allocation function, has std::align_val_t as its second
6666 argument. */
6667
6668 bool
6669 aligned_allocation_fn_p (tree t)
6670 {
6671 if (!aligned_new_threshold)
6672 return false;
6673
6674 tree a = FUNCTION_ARG_CHAIN (t);
6675 return (a && same_type_p (TREE_VALUE (a), align_type_node));
6676 }
6677
6678 /* True if T is std::destroying_delete_t. */
6679
6680 static bool
6681 std_destroying_delete_t_p (tree t)
6682 {
6683 return (TYPE_CONTEXT (t) == std_node
6684 && id_equal (TYPE_IDENTIFIER (t), "destroying_delete_t"));
6685 }
6686
6687 /* A deallocation function with at least two parameters whose second parameter
6688 type is of type std::destroying_delete_t is a destroying operator delete. A
6689 destroying operator delete shall be a class member function named operator
6690 delete. [ Note: Array deletion cannot use a destroying operator
6691 delete. --end note ] */
6692
6693 tree
6694 destroying_delete_p (tree t)
6695 {
6696 tree a = TYPE_ARG_TYPES (TREE_TYPE (t));
6697 if (!a || !TREE_CHAIN (a))
6698 return NULL_TREE;
6699 tree type = TREE_VALUE (TREE_CHAIN (a));
6700 return std_destroying_delete_t_p (type) ? type : NULL_TREE;
6701 }
6702
6703 struct dealloc_info
6704 {
6705 bool sized;
6706 bool aligned;
6707 tree destroying;
6708 };
6709
6710 /* Returns true iff T, an element of an OVERLOAD chain, is a usual deallocation
6711 function (3.7.4.2 [basic.stc.dynamic.deallocation]). If so, and DI is
6712 non-null, also set *DI. */
6713
6714 static bool
6715 usual_deallocation_fn_p (tree t, dealloc_info *di)
6716 {
6717 if (di) *di = dealloc_info();
6718
6719 /* A template instance is never a usual deallocation function,
6720 regardless of its signature. */
6721 if (TREE_CODE (t) == TEMPLATE_DECL
6722 || primary_template_specialization_p (t))
6723 return false;
6724
6725 /* A usual deallocation function is a deallocation function whose parameters
6726 after the first are
6727 - optionally, a parameter of type std::destroying_delete_t, then
6728 - optionally, a parameter of type std::size_t, then
6729 - optionally, a parameter of type std::align_val_t. */
6730 bool global = DECL_NAMESPACE_SCOPE_P (t);
6731 tree chain = FUNCTION_ARG_CHAIN (t);
6732 if (chain && destroying_delete_p (t))
6733 {
6734 if (di) di->destroying = TREE_VALUE (chain);
6735 chain = TREE_CHAIN (chain);
6736 }
6737 if (chain
6738 && (!global || flag_sized_deallocation)
6739 && same_type_p (TREE_VALUE (chain), size_type_node))
6740 {
6741 if (di) di->sized = true;
6742 chain = TREE_CHAIN (chain);
6743 }
6744 if (chain && aligned_new_threshold
6745 && same_type_p (TREE_VALUE (chain), align_type_node))
6746 {
6747 if (di) di->aligned = true;
6748 chain = TREE_CHAIN (chain);
6749 }
6750 return (chain == void_list_node);
6751 }
6752
6753 /* Just return whether FN is a usual deallocation function. */
6754
6755 bool
6756 usual_deallocation_fn_p (tree fn)
6757 {
6758 return usual_deallocation_fn_p (fn, NULL);
6759 }
6760
6761 /* Build a call to operator delete. This has to be handled very specially,
6762 because the restrictions on what signatures match are different from all
6763 other call instances. For a normal delete, only a delete taking (void *)
6764 or (void *, size_t) is accepted. For a placement delete, only an exact
6765 match with the placement new is accepted.
6766
6767 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
6768 ADDR is the pointer to be deleted.
6769 SIZE is the size of the memory block to be deleted.
6770 GLOBAL_P is true if the delete-expression should not consider
6771 class-specific delete operators.
6772 PLACEMENT is the corresponding placement new call, or NULL_TREE.
6773
6774 If this call to "operator delete" is being generated as part to
6775 deallocate memory allocated via a new-expression (as per [expr.new]
6776 which requires that if the initialization throws an exception then
6777 we call a deallocation function), then ALLOC_FN is the allocation
6778 function. */
6779
6780 tree
6781 build_op_delete_call (enum tree_code code, tree addr, tree size,
6782 bool global_p, tree placement,
6783 tree alloc_fn, tsubst_flags_t complain)
6784 {
6785 tree fn = NULL_TREE;
6786 tree fns, fnname, type, t;
6787 dealloc_info di_fn = { };
6788
6789 if (addr == error_mark_node)
6790 return error_mark_node;
6791
6792 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
6793
6794 fnname = ovl_op_identifier (false, code);
6795
6796 if (CLASS_TYPE_P (type)
6797 && COMPLETE_TYPE_P (complete_type (type))
6798 && !global_p)
6799 /* In [class.free]
6800
6801 If the result of the lookup is ambiguous or inaccessible, or if
6802 the lookup selects a placement deallocation function, the
6803 program is ill-formed.
6804
6805 Therefore, we ask lookup_fnfields to complain about ambiguity. */
6806 {
6807 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1, complain);
6808 if (fns == error_mark_node)
6809 return error_mark_node;
6810 }
6811 else
6812 fns = NULL_TREE;
6813
6814 if (fns == NULL_TREE)
6815 fns = lookup_name_nonclass (fnname);
6816
6817 /* Strip const and volatile from addr. */
6818 tree oaddr = addr;
6819 addr = cp_convert (ptr_type_node, addr, complain);
6820
6821 if (placement)
6822 {
6823 /* "A declaration of a placement deallocation function matches the
6824 declaration of a placement allocation function if it has the same
6825 number of parameters and, after parameter transformations (8.3.5),
6826 all parameter types except the first are identical."
6827
6828 So we build up the function type we want and ask instantiate_type
6829 to get it for us. */
6830 t = FUNCTION_ARG_CHAIN (alloc_fn);
6831 t = tree_cons (NULL_TREE, ptr_type_node, t);
6832 t = build_function_type (void_type_node, t);
6833
6834 fn = instantiate_type (t, fns, tf_none);
6835 if (fn == error_mark_node)
6836 return NULL_TREE;
6837
6838 fn = MAYBE_BASELINK_FUNCTIONS (fn);
6839
6840 /* "If the lookup finds the two-parameter form of a usual deallocation
6841 function (3.7.4.2) and that function, considered as a placement
6842 deallocation function, would have been selected as a match for the
6843 allocation function, the program is ill-formed." */
6844 if (second_parm_is_size_t (fn))
6845 {
6846 const char *const msg1
6847 = G_("exception cleanup for this placement new selects "
6848 "non-placement %<operator delete%>");
6849 const char *const msg2
6850 = G_("%qD is a usual (non-placement) deallocation "
6851 "function in C++14 (or with %<-fsized-deallocation%>)");
6852
6853 /* But if the class has an operator delete (void *), then that is
6854 the usual deallocation function, so we shouldn't complain
6855 about using the operator delete (void *, size_t). */
6856 if (DECL_CLASS_SCOPE_P (fn))
6857 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns));
6858 iter; ++iter)
6859 {
6860 tree elt = *iter;
6861 if (usual_deallocation_fn_p (elt)
6862 && FUNCTION_ARG_CHAIN (elt) == void_list_node)
6863 goto ok;
6864 }
6865 /* Before C++14 a two-parameter global deallocation function is
6866 always a placement deallocation function, but warn if
6867 -Wc++14-compat. */
6868 else if (!flag_sized_deallocation)
6869 {
6870 if (complain & tf_warning)
6871 {
6872 auto_diagnostic_group d;
6873 if (warning (OPT_Wc__14_compat, msg1))
6874 inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
6875 }
6876 goto ok;
6877 }
6878
6879 if (complain & tf_warning_or_error)
6880 {
6881 auto_diagnostic_group d;
6882 if (permerror (input_location, msg1))
6883 {
6884 /* Only mention C++14 for namespace-scope delete. */
6885 if (DECL_NAMESPACE_SCOPE_P (fn))
6886 inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
6887 else
6888 inform (DECL_SOURCE_LOCATION (fn),
6889 "%qD is a usual (non-placement) deallocation "
6890 "function", fn);
6891 }
6892 }
6893 else
6894 return error_mark_node;
6895 ok:;
6896 }
6897 }
6898 else
6899 /* "Any non-placement deallocation function matches a non-placement
6900 allocation function. If the lookup finds a single matching
6901 deallocation function, that function will be called; otherwise, no
6902 deallocation function will be called." */
6903 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
6904 {
6905 tree elt = *iter;
6906 dealloc_info di_elt;
6907 if (usual_deallocation_fn_p (elt, &di_elt))
6908 {
6909 if (!fn)
6910 {
6911 fn = elt;
6912 di_fn = di_elt;
6913 continue;
6914 }
6915
6916 /* -- If any of the deallocation functions is a destroying
6917 operator delete, all deallocation functions that are not
6918 destroying operator deletes are eliminated from further
6919 consideration. */
6920 if (di_elt.destroying != di_fn.destroying)
6921 {
6922 if (di_elt.destroying)
6923 {
6924 fn = elt;
6925 di_fn = di_elt;
6926 }
6927 continue;
6928 }
6929
6930 /* -- If the type has new-extended alignment, a function with a
6931 parameter of type std::align_val_t is preferred; otherwise a
6932 function without such a parameter is preferred. If exactly one
6933 preferred function is found, that function is selected and the
6934 selection process terminates. If more than one preferred
6935 function is found, all non-preferred functions are eliminated
6936 from further consideration. */
6937 if (aligned_new_threshold)
6938 {
6939 bool want_align = type_has_new_extended_alignment (type);
6940 if (di_elt.aligned != di_fn.aligned)
6941 {
6942 if (want_align == di_elt.aligned)
6943 {
6944 fn = elt;
6945 di_fn = di_elt;
6946 }
6947 continue;
6948 }
6949 }
6950
6951 /* -- If the deallocation functions have class scope, the one
6952 without a parameter of type std::size_t is selected. */
6953 bool want_size;
6954 if (DECL_CLASS_SCOPE_P (fn))
6955 want_size = false;
6956
6957 /* -- If the type is complete and if, for the second alternative
6958 (delete array) only, the operand is a pointer to a class type
6959 with a non-trivial destructor or a (possibly multi-dimensional)
6960 array thereof, the function with a parameter of type std::size_t
6961 is selected.
6962
6963 -- Otherwise, it is unspecified whether a deallocation function
6964 with a parameter of type std::size_t is selected. */
6965 else
6966 {
6967 want_size = COMPLETE_TYPE_P (type);
6968 if (code == VEC_DELETE_EXPR
6969 && !TYPE_VEC_NEW_USES_COOKIE (type))
6970 /* We need a cookie to determine the array size. */
6971 want_size = false;
6972 }
6973 gcc_assert (di_fn.sized != di_elt.sized);
6974 if (want_size == di_elt.sized)
6975 {
6976 fn = elt;
6977 di_fn = di_elt;
6978 }
6979 }
6980 }
6981
6982 /* If we have a matching function, call it. */
6983 if (fn)
6984 {
6985 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
6986
6987 /* If the FN is a member function, make sure that it is
6988 accessible. */
6989 if (BASELINK_P (fns))
6990 perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn,
6991 complain);
6992
6993 /* Core issue 901: It's ok to new a type with deleted delete. */
6994 if (DECL_DELETED_FN (fn) && alloc_fn)
6995 return NULL_TREE;
6996
6997 if (placement)
6998 {
6999 /* The placement args might not be suitable for overload
7000 resolution at this point, so build the call directly. */
7001 int nargs = call_expr_nargs (placement);
7002 tree *argarray = XALLOCAVEC (tree, nargs);
7003 int i;
7004 argarray[0] = addr;
7005 for (i = 1; i < nargs; i++)
7006 argarray[i] = CALL_EXPR_ARG (placement, i);
7007 if (!mark_used (fn, complain) && !(complain & tf_error))
7008 return error_mark_node;
7009 return build_cxx_call (fn, nargs, argarray, complain);
7010 }
7011 else
7012 {
7013 tree destroying = di_fn.destroying;
7014 if (destroying)
7015 {
7016 /* Strip const and volatile from addr but retain the type of the
7017 object. */
7018 tree rtype = TREE_TYPE (TREE_TYPE (oaddr));
7019 rtype = cv_unqualified (rtype);
7020 rtype = TYPE_POINTER_TO (rtype);
7021 addr = cp_convert (rtype, oaddr, complain);
7022 destroying = build_functional_cast (input_location,
7023 destroying, NULL_TREE,
7024 complain);
7025 }
7026
7027 tree ret;
7028 releasing_vec args;
7029 args->quick_push (addr);
7030 if (destroying)
7031 args->quick_push (destroying);
7032 if (di_fn.sized)
7033 args->quick_push (size);
7034 if (di_fn.aligned)
7035 {
7036 tree al = build_int_cst (align_type_node, TYPE_ALIGN_UNIT (type));
7037 args->quick_push (al);
7038 }
7039 ret = cp_build_function_call_vec (fn, &args, complain);
7040 return ret;
7041 }
7042 }
7043
7044 /* [expr.new]
7045
7046 If no unambiguous matching deallocation function can be found,
7047 propagating the exception does not cause the object's memory to
7048 be freed. */
7049 if (alloc_fn)
7050 {
7051 if ((complain & tf_warning)
7052 && !placement)
7053 warning (0, "no corresponding deallocation function for %qD",
7054 alloc_fn);
7055 return NULL_TREE;
7056 }
7057
7058 if (complain & tf_error)
7059 error ("no suitable %<operator %s%> for %qT",
7060 OVL_OP_INFO (false, code)->name, type);
7061 return error_mark_node;
7062 }
7063
7064 /* Issue diagnostics about a disallowed access of DECL, using DIAG_DECL
7065 in the diagnostics.
7066
7067 If ISSUE_ERROR is true, then issue an error about the
7068 access, followed by a note showing the declaration.
7069 Otherwise, just show the note. */
7070
7071 void
7072 complain_about_access (tree decl, tree diag_decl, bool issue_error)
7073 {
7074 if (TREE_PRIVATE (decl))
7075 {
7076 if (issue_error)
7077 error ("%q#D is private within this context", diag_decl);
7078 inform (DECL_SOURCE_LOCATION (diag_decl),
7079 "declared private here");
7080 }
7081 else if (TREE_PROTECTED (decl))
7082 {
7083 if (issue_error)
7084 error ("%q#D is protected within this context", diag_decl);
7085 inform (DECL_SOURCE_LOCATION (diag_decl),
7086 "declared protected here");
7087 }
7088 else
7089 {
7090 if (issue_error)
7091 error ("%q#D is inaccessible within this context", diag_decl);
7092 inform (DECL_SOURCE_LOCATION (diag_decl), "declared here");
7093 }
7094 }
7095
7096 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
7097 bitwise or of LOOKUP_* values. If any errors are warnings are
7098 generated, set *DIAGNOSTIC_FN to "error" or "warning",
7099 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
7100 to NULL. */
7101
7102 static tree
7103 build_temp (tree expr, tree type, int flags,
7104 diagnostic_t *diagnostic_kind, tsubst_flags_t complain)
7105 {
7106 int savew, savee;
7107
7108 *diagnostic_kind = DK_UNSPECIFIED;
7109
7110 /* If the source is a packed field, calling the copy constructor will require
7111 binding the field to the reference parameter to the copy constructor, and
7112 we'll end up with an infinite loop. If we can use a bitwise copy, then
7113 do that now. */
7114 if ((lvalue_kind (expr) & clk_packed)
7115 && CLASS_TYPE_P (TREE_TYPE (expr))
7116 && !type_has_nontrivial_copy_init (TREE_TYPE (expr)))
7117 return get_target_expr_sfinae (expr, complain);
7118
7119 savew = warningcount + werrorcount, savee = errorcount;
7120 releasing_vec args (make_tree_vector_single (expr));
7121 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
7122 &args, type, flags, complain);
7123 if (warningcount + werrorcount > savew)
7124 *diagnostic_kind = DK_WARNING;
7125 else if (errorcount > savee)
7126 *diagnostic_kind = DK_ERROR;
7127 return expr;
7128 }
7129
7130 /* Get any location for EXPR, falling back to input_location.
7131
7132 If the result is in a system header and is the virtual location for
7133 a token coming from the expansion of a macro, unwind it to the
7134 location of the expansion point of the macro (e.g. to avoid the
7135 diagnostic being suppressed for expansions of NULL where "NULL" is
7136 in a system header). */
7137
7138 static location_t
7139 get_location_for_expr_unwinding_for_system_header (tree expr)
7140 {
7141 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
7142 loc = expansion_point_location_if_in_system_header (loc);
7143 return loc;
7144 }
7145
7146 /* Perform warnings about peculiar, but valid, conversions from/to NULL.
7147 Also handle a subset of zero as null warnings.
7148 EXPR is implicitly converted to type TOTYPE.
7149 FN and ARGNUM are used for diagnostics. */
7150
7151 static void
7152 conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
7153 {
7154 /* Issue warnings about peculiar, but valid, uses of NULL. */
7155 if (TREE_CODE (totype) != BOOLEAN_TYPE
7156 && ARITHMETIC_TYPE_P (totype)
7157 && null_node_p (expr))
7158 {
7159 location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
7160 if (fn)
7161 {
7162 auto_diagnostic_group d;
7163 if (warning_at (loc, OPT_Wconversion_null,
7164 "passing NULL to non-pointer argument %P of %qD",
7165 argnum, fn))
7166 inform (get_fndecl_argument_location (fn, argnum),
7167 " declared here");
7168 }
7169 else
7170 warning_at (loc, OPT_Wconversion_null,
7171 "converting to non-pointer type %qT from NULL", totype);
7172 }
7173
7174 /* Issue warnings if "false" is converted to a NULL pointer */
7175 else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
7176 && TYPE_PTR_P (totype))
7177 {
7178 location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
7179 if (fn)
7180 {
7181 auto_diagnostic_group d;
7182 if (warning_at (loc, OPT_Wconversion_null,
7183 "converting %<false%> to pointer type for argument "
7184 "%P of %qD", argnum, fn))
7185 inform (get_fndecl_argument_location (fn, argnum),
7186 " declared here");
7187 }
7188 else
7189 warning_at (loc, OPT_Wconversion_null,
7190 "converting %<false%> to pointer type %qT", totype);
7191 }
7192 /* Handle zero as null pointer warnings for cases other
7193 than EQ_EXPR and NE_EXPR */
7194 else if ((TYPE_PTR_OR_PTRMEM_P (totype) || NULLPTR_TYPE_P (totype))
7195 && null_ptr_cst_p (expr))
7196 {
7197 location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
7198 maybe_warn_zero_as_null_pointer_constant (expr, loc);
7199 }
7200 }
7201
7202 /* We gave a diagnostic during a conversion. If this was in the second
7203 standard conversion sequence of a user-defined conversion sequence, say
7204 which user-defined conversion. */
7205
7206 static void
7207 maybe_print_user_conv_context (conversion *convs)
7208 {
7209 if (convs->user_conv_p)
7210 for (conversion *t = convs; t; t = next_conversion (t))
7211 if (t->kind == ck_user)
7212 {
7213 print_z_candidate (0, N_(" after user-defined conversion:"),
7214 t->cand);
7215 break;
7216 }
7217 }
7218
7219 /* Locate the parameter with the given index within FNDECL.
7220 ARGNUM is zero based, -1 indicates the `this' argument of a method.
7221 Return the location of the FNDECL itself if there are problems. */
7222
7223 location_t
7224 get_fndecl_argument_location (tree fndecl, int argnum)
7225 {
7226 /* The locations of implicitly-declared functions are likely to be
7227 more meaningful than those of their parameters. */
7228 if (DECL_ARTIFICIAL (fndecl))
7229 return DECL_SOURCE_LOCATION (fndecl);
7230
7231 int i;
7232 tree param;
7233
7234 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
7235 for (i = 0, param = FUNCTION_FIRST_USER_PARM (fndecl);
7236 i < argnum && param;
7237 i++, param = TREE_CHAIN (param))
7238 ;
7239
7240 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
7241 return the location of FNDECL. */
7242 if (param == NULL)
7243 return DECL_SOURCE_LOCATION (fndecl);
7244
7245 return DECL_SOURCE_LOCATION (param);
7246 }
7247
7248 /* If FNDECL is non-NULL, issue a note highlighting ARGNUM
7249 within its declaration (or the fndecl itself if something went
7250 wrong). */
7251
7252 void
7253 maybe_inform_about_fndecl_for_bogus_argument_init (tree fn, int argnum)
7254 {
7255 if (fn)
7256 inform (get_fndecl_argument_location (fn, argnum),
7257 " initializing argument %P of %qD", argnum, fn);
7258 }
7259
7260 /* Maybe warn about C++20 Conversions to arrays of unknown bound. C is
7261 the conversion, EXPR is the expression we're converting. */
7262
7263 static void
7264 maybe_warn_array_conv (location_t loc, conversion *c, tree expr)
7265 {
7266 if (cxx_dialect >= cxx20)
7267 return;
7268
7269 tree type = TREE_TYPE (expr);
7270 type = strip_pointer_operator (type);
7271
7272 if (TREE_CODE (type) != ARRAY_TYPE
7273 || TYPE_DOMAIN (type) == NULL_TREE)
7274 return;
7275
7276 if (conv_binds_to_array_of_unknown_bound (c))
7277 pedwarn (loc, OPT_Wpedantic, "conversions to arrays of unknown bound "
7278 "are only available with %<-std=c++20%> or %<-std=gnu++20%>");
7279 }
7280
7281 /* We call this recursively in convert_like_internal. */
7282 static tree convert_like (conversion *, tree, tree, int, bool, bool,
7283 tsubst_flags_t);
7284
7285 /* Perform the conversions in CONVS on the expression EXPR. FN and
7286 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
7287 indicates the `this' argument of a method. INNER is nonzero when
7288 being called to continue a conversion chain. It is negative when a
7289 reference binding will be applied, positive otherwise. If
7290 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
7291 conversions will be emitted if appropriate. If C_CAST_P is true,
7292 this conversion is coming from a C-style cast; in that case,
7293 conversions to inaccessible bases are permitted. */
7294
7295 static tree
7296 convert_like_internal (conversion *convs, tree expr, tree fn, int argnum,
7297 bool issue_conversion_warnings, bool c_cast_p,
7298 tsubst_flags_t complain)
7299 {
7300 tree totype = convs->type;
7301 diagnostic_t diag_kind;
7302 int flags;
7303 location_t loc = cp_expr_loc_or_input_loc (expr);
7304
7305 if (convs->bad_p && !(complain & tf_error))
7306 return error_mark_node;
7307
7308 if (convs->bad_p
7309 && convs->kind != ck_user
7310 && convs->kind != ck_list
7311 && convs->kind != ck_ambig
7312 && (convs->kind != ck_ref_bind
7313 || (convs->user_conv_p && next_conversion (convs)->bad_p))
7314 && (convs->kind != ck_rvalue
7315 || SCALAR_TYPE_P (totype))
7316 && convs->kind != ck_base)
7317 {
7318 bool complained = false;
7319 conversion *t = convs;
7320
7321 /* Give a helpful error if this is bad because of excess braces. */
7322 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
7323 && SCALAR_TYPE_P (totype)
7324 && CONSTRUCTOR_NELTS (expr) > 0
7325 && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
7326 {
7327 complained = permerror (loc, "too many braces around initializer "
7328 "for %qT", totype);
7329 while (BRACE_ENCLOSED_INITIALIZER_P (expr)
7330 && CONSTRUCTOR_NELTS (expr) == 1)
7331 expr = CONSTRUCTOR_ELT (expr, 0)->value;
7332 }
7333
7334 /* Give a helpful error if this is bad because a conversion to bool
7335 from std::nullptr_t requires direct-initialization. */
7336 if (NULLPTR_TYPE_P (TREE_TYPE (expr))
7337 && TREE_CODE (totype) == BOOLEAN_TYPE)
7338 complained = permerror (loc, "converting to %qH from %qI requires "
7339 "direct-initialization",
7340 totype, TREE_TYPE (expr));
7341
7342 for (; t ; t = next_conversion (t))
7343 {
7344 if (t->kind == ck_user && t->cand->reason)
7345 {
7346 auto_diagnostic_group d;
7347 complained = permerror (loc, "invalid user-defined conversion "
7348 "from %qH to %qI", TREE_TYPE (expr),
7349 totype);
7350 if (complained)
7351 print_z_candidate (loc, N_("candidate is:"), t->cand);
7352 expr = convert_like (t, expr, fn, argnum,
7353 /*issue_conversion_warnings=*/false,
7354 /*c_cast_p=*/false, complain);
7355 if (convs->kind == ck_ref_bind)
7356 expr = convert_to_reference (totype, expr, CONV_IMPLICIT,
7357 LOOKUP_NORMAL, NULL_TREE,
7358 complain);
7359 else
7360 expr = cp_convert (totype, expr, complain);
7361 if (complained)
7362 maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
7363 return expr;
7364 }
7365 else if (t->kind == ck_user || !t->bad_p)
7366 {
7367 expr = convert_like (t, expr, fn, argnum,
7368 /*issue_conversion_warnings=*/false,
7369 /*c_cast_p=*/false, complain);
7370 break;
7371 }
7372 else if (t->kind == ck_ambig)
7373 return convert_like (t, expr, fn, argnum,
7374 /*issue_conversion_warnings=*/false,
7375 /*c_cast_p=*/false, complain);
7376 else if (t->kind == ck_identity)
7377 break;
7378 }
7379 if (!complained)
7380 {
7381 range_label_for_type_mismatch label (TREE_TYPE (expr), totype);
7382 gcc_rich_location richloc (loc, &label);
7383 complained = permerror (&richloc,
7384 "invalid conversion from %qH to %qI",
7385 TREE_TYPE (expr), totype);
7386 }
7387 if (complained)
7388 maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
7389
7390 return cp_convert (totype, expr, complain);
7391 }
7392
7393 if (issue_conversion_warnings && (complain & tf_warning))
7394 conversion_null_warnings (totype, expr, fn, argnum);
7395
7396 switch (convs->kind)
7397 {
7398 case ck_user:
7399 {
7400 struct z_candidate *cand = convs->cand;
7401
7402 if (cand == NULL)
7403 /* We chose the surrogate function from add_conv_candidate, now we
7404 actually need to build the conversion. */
7405 cand = build_user_type_conversion_1 (totype, expr,
7406 LOOKUP_NO_CONVERSION, complain);
7407
7408 tree convfn = cand->fn;
7409
7410 /* When converting from an init list we consider explicit
7411 constructors, but actually trying to call one is an error. */
7412 if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
7413 && BRACE_ENCLOSED_INITIALIZER_P (expr)
7414 /* Unless this is for direct-list-initialization. */
7415 && (!CONSTRUCTOR_IS_DIRECT_INIT (expr) || convs->need_temporary_p)
7416 /* And in C++98 a default constructor can't be explicit. */
7417 && cxx_dialect >= cxx11)
7418 {
7419 if (!(complain & tf_error))
7420 return error_mark_node;
7421 location_t loc = location_of (expr);
7422 if (CONSTRUCTOR_NELTS (expr) == 0
7423 && FUNCTION_FIRST_USER_PARMTYPE (convfn) != void_list_node)
7424 {
7425 auto_diagnostic_group d;
7426 if (pedwarn (loc, 0, "converting to %qT from initializer list "
7427 "would use explicit constructor %qD",
7428 totype, convfn))
7429 inform (loc, "in C++11 and above a default constructor "
7430 "can be explicit");
7431 }
7432 else
7433 error ("converting to %qT from initializer list would use "
7434 "explicit constructor %qD", totype, convfn);
7435 }
7436
7437 /* If we're initializing from {}, it's value-initialization. */
7438 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
7439 && CONSTRUCTOR_NELTS (expr) == 0
7440 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype)
7441 && !processing_template_decl)
7442 {
7443 bool direct = CONSTRUCTOR_IS_DIRECT_INIT (expr);
7444 if (abstract_virtuals_error_sfinae (NULL_TREE, totype, complain))
7445 return error_mark_node;
7446 expr = build_value_init (totype, complain);
7447 expr = get_target_expr_sfinae (expr, complain);
7448 if (expr != error_mark_node)
7449 {
7450 TARGET_EXPR_LIST_INIT_P (expr) = true;
7451 TARGET_EXPR_DIRECT_INIT_P (expr) = direct;
7452 }
7453 return expr;
7454 }
7455
7456 /* We don't know here whether EXPR is being used as an lvalue or
7457 rvalue, but we know it's read. */
7458 mark_exp_read (expr);
7459
7460 /* Pass LOOKUP_NO_CONVERSION so rvalue/base handling knows not to allow
7461 any more UDCs. */
7462 expr = build_over_call (cand, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
7463 complain);
7464
7465 /* If this is a constructor or a function returning an aggr type,
7466 we need to build up a TARGET_EXPR. */
7467 if (DECL_CONSTRUCTOR_P (convfn))
7468 {
7469 expr = build_cplus_new (totype, expr, complain);
7470
7471 /* Remember that this was list-initialization. */
7472 if (convs->check_narrowing && expr != error_mark_node)
7473 TARGET_EXPR_LIST_INIT_P (expr) = true;
7474 }
7475
7476 return expr;
7477 }
7478 case ck_identity:
7479 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
7480 {
7481 int nelts = CONSTRUCTOR_NELTS (expr);
7482 if (nelts == 0)
7483 expr = build_value_init (totype, complain);
7484 else if (nelts == 1)
7485 expr = CONSTRUCTOR_ELT (expr, 0)->value;
7486 else
7487 gcc_unreachable ();
7488 }
7489 expr = mark_use (expr, /*rvalue_p=*/!convs->rvaluedness_matches_p,
7490 /*read_p=*/true, UNKNOWN_LOCATION,
7491 /*reject_builtin=*/true);
7492
7493 if (type_unknown_p (expr))
7494 expr = instantiate_type (totype, expr, complain);
7495 if (expr == null_node
7496 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype))
7497 /* If __null has been converted to an integer type, we do not want to
7498 continue to warn about uses of EXPR as an integer, rather than as a
7499 pointer. */
7500 expr = build_int_cst (totype, 0);
7501 return expr;
7502 case ck_ambig:
7503 /* We leave bad_p off ck_ambig because overload resolution considers
7504 it valid, it just fails when we try to perform it. So we need to
7505 check complain here, too. */
7506 if (complain & tf_error)
7507 {
7508 /* Call build_user_type_conversion again for the error. */
7509 int flags = (convs->need_temporary_p
7510 ? LOOKUP_IMPLICIT : LOOKUP_NORMAL);
7511 build_user_type_conversion (totype, convs->u.expr, flags, complain);
7512 gcc_assert (seen_error ());
7513 maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
7514 }
7515 return error_mark_node;
7516
7517 case ck_list:
7518 {
7519 /* Conversion to std::initializer_list<T>. */
7520 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
7521 unsigned len = CONSTRUCTOR_NELTS (expr);
7522 tree array;
7523
7524 if (len)
7525 {
7526 tree val; unsigned ix;
7527
7528 tree new_ctor = build_constructor (init_list_type_node, NULL);
7529
7530 /* Convert all the elements. */
7531 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
7532 {
7533 tree sub = convert_like (convs->u.list[ix], val, fn,
7534 argnum, false, false, complain);
7535 if (sub == error_mark_node)
7536 return sub;
7537 if (!BRACE_ENCLOSED_INITIALIZER_P (val)
7538 && !check_narrowing (TREE_TYPE (sub), val, complain))
7539 return error_mark_node;
7540 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor),
7541 NULL_TREE, sub);
7542 if (!TREE_CONSTANT (sub))
7543 TREE_CONSTANT (new_ctor) = false;
7544 }
7545 /* Build up the array. */
7546 elttype = cp_build_qualified_type
7547 (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
7548 array = build_array_of_n_type (elttype, len);
7549 array = finish_compound_literal (array, new_ctor, complain);
7550 /* Take the address explicitly rather than via decay_conversion
7551 to avoid the error about taking the address of a temporary. */
7552 array = cp_build_addr_expr (array, complain);
7553 }
7554 else
7555 array = nullptr_node;
7556
7557 array = cp_convert (build_pointer_type (elttype), array, complain);
7558 if (array == error_mark_node)
7559 return error_mark_node;
7560
7561 /* Build up the initializer_list object. Note: fail gracefully
7562 if the object cannot be completed because, for example, no
7563 definition is provided (c++/80956). */
7564 totype = complete_type_or_maybe_complain (totype, NULL_TREE, complain);
7565 if (!totype)
7566 return error_mark_node;
7567 tree field = next_initializable_field (TYPE_FIELDS (totype));
7568 vec<constructor_elt, va_gc> *vec = NULL;
7569 CONSTRUCTOR_APPEND_ELT (vec, field, array);
7570 field = next_initializable_field (DECL_CHAIN (field));
7571 CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
7572 tree new_ctor = build_constructor (totype, vec);
7573 return get_target_expr_sfinae (new_ctor, complain);
7574 }
7575
7576 case ck_aggr:
7577 if (TREE_CODE (totype) == COMPLEX_TYPE)
7578 {
7579 tree real = CONSTRUCTOR_ELT (expr, 0)->value;
7580 tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
7581 real = perform_implicit_conversion (TREE_TYPE (totype),
7582 real, complain);
7583 imag = perform_implicit_conversion (TREE_TYPE (totype),
7584 imag, complain);
7585 expr = build2 (COMPLEX_EXPR, totype, real, imag);
7586 return expr;
7587 }
7588 expr = reshape_init (totype, expr, complain);
7589 expr = get_target_expr_sfinae (digest_init (totype, expr, complain),
7590 complain);
7591 if (expr != error_mark_node)
7592 TARGET_EXPR_LIST_INIT_P (expr) = true;
7593 return expr;
7594
7595 default:
7596 break;
7597 };
7598
7599 expr = convert_like (next_conversion (convs), expr, fn, argnum,
7600 convs->kind == ck_ref_bind
7601 ? issue_conversion_warnings : false,
7602 c_cast_p, complain);
7603 if (expr == error_mark_node)
7604 return error_mark_node;
7605
7606 switch (convs->kind)
7607 {
7608 case ck_rvalue:
7609 expr = decay_conversion (expr, complain);
7610 if (expr == error_mark_node)
7611 {
7612 if (complain & tf_error)
7613 {
7614 auto_diagnostic_group d;
7615 maybe_print_user_conv_context (convs);
7616 maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
7617 }
7618 return error_mark_node;
7619 }
7620
7621 if (! MAYBE_CLASS_TYPE_P (totype))
7622 return expr;
7623
7624 /* Don't introduce copies when passing arguments along to the inherited
7625 constructor. */
7626 if (current_function_decl
7627 && flag_new_inheriting_ctors
7628 && DECL_INHERITED_CTOR (current_function_decl))
7629 return expr;
7630
7631 if (TREE_CODE (expr) == TARGET_EXPR
7632 && TARGET_EXPR_LIST_INIT_P (expr))
7633 /* Copy-list-initialization doesn't actually involve a copy. */
7634 return expr;
7635
7636 /* Fall through. */
7637 case ck_base:
7638 if (convs->kind == ck_base && !convs->need_temporary_p)
7639 {
7640 /* We are going to bind a reference directly to a base-class
7641 subobject of EXPR. */
7642 /* Build an expression for `*((base*) &expr)'. */
7643 expr = convert_to_base (expr, totype,
7644 !c_cast_p, /*nonnull=*/true, complain);
7645 return expr;
7646 }
7647
7648 /* Copy-initialization where the cv-unqualified version of the source
7649 type is the same class as, or a derived class of, the class of the
7650 destination [is treated as direct-initialization]. [dcl.init] */
7651 flags = LOOKUP_NORMAL;
7652 /* This conversion is being done in the context of a user-defined
7653 conversion (i.e. the second step of copy-initialization), so
7654 don't allow any more. */
7655 if (convs->user_conv_p)
7656 flags |= LOOKUP_NO_CONVERSION;
7657 /* We might be performing a conversion of the argument
7658 to the user-defined conversion, i.e., not a conversion of the
7659 result of the user-defined conversion. In which case we skip
7660 explicit constructors. */
7661 if (convs->copy_init_p)
7662 flags |= LOOKUP_ONLYCONVERTING;
7663 if (convs->rvaluedness_matches_p)
7664 /* standard_conversion got LOOKUP_PREFER_RVALUE. */
7665 flags |= LOOKUP_PREFER_RVALUE;
7666 expr = build_temp (expr, totype, flags, &diag_kind, complain);
7667 if (diag_kind && complain)
7668 {
7669 auto_diagnostic_group d;
7670 maybe_print_user_conv_context (convs);
7671 maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
7672 }
7673
7674 return build_cplus_new (totype, expr, complain);
7675
7676 case ck_ref_bind:
7677 {
7678 tree ref_type = totype;
7679
7680 /* direct_reference_binding might have inserted a ck_qual under
7681 this ck_ref_bind for the benefit of conversion sequence ranking.
7682 Ignore the conversion; we'll create our own below. */
7683 if (next_conversion (convs)->kind == ck_qual
7684 && !convs->need_temporary_p)
7685 {
7686 gcc_assert (same_type_p (TREE_TYPE (expr),
7687 next_conversion (convs)->type));
7688 /* Strip the cast created by the ck_qual; cp_build_addr_expr
7689 below expects an lvalue. */
7690 STRIP_NOPS (expr);
7691 }
7692
7693 if (convs->bad_p && !next_conversion (convs)->bad_p)
7694 {
7695 tree extype = TREE_TYPE (expr);
7696 auto_diagnostic_group d;
7697 if (TYPE_REF_IS_RVALUE (ref_type)
7698 && lvalue_p (expr))
7699 error_at (loc, "cannot bind rvalue reference of type %qH to "
7700 "lvalue of type %qI", totype, extype);
7701 else if (!TYPE_REF_IS_RVALUE (ref_type) && !lvalue_p (expr)
7702 && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
7703 error_at (loc, "cannot bind non-const lvalue reference of "
7704 "type %qH to an rvalue of type %qI", totype, extype);
7705 else if (!reference_compatible_p (TREE_TYPE (totype), extype))
7706 {
7707 /* If we're converting from T[] to T[N], don't talk
7708 about discarding qualifiers. (Converting from T[N] to
7709 T[] is allowed by P0388R4.) */
7710 if (TREE_CODE (extype) == ARRAY_TYPE
7711 && TYPE_DOMAIN (extype) == NULL_TREE
7712 && TREE_CODE (TREE_TYPE (totype)) == ARRAY_TYPE
7713 && TYPE_DOMAIN (TREE_TYPE (totype)) != NULL_TREE)
7714 error_at (loc, "cannot bind reference of type %qH to %qI "
7715 "due to different array bounds", totype, extype);
7716 else
7717 error_at (loc, "binding reference of type %qH to %qI "
7718 "discards qualifiers", totype, extype);
7719 }
7720 else
7721 gcc_unreachable ();
7722 maybe_print_user_conv_context (convs);
7723 maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
7724
7725 return error_mark_node;
7726 }
7727 else if (complain & tf_warning)
7728 maybe_warn_array_conv (loc, convs, expr);
7729
7730 /* If necessary, create a temporary.
7731
7732 VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
7733 that need temporaries, even when their types are reference
7734 compatible with the type of reference being bound, so the
7735 upcoming call to cp_build_addr_expr doesn't fail. */
7736 if (convs->need_temporary_p
7737 || TREE_CODE (expr) == CONSTRUCTOR
7738 || TREE_CODE (expr) == VA_ARG_EXPR)
7739 {
7740 /* Otherwise, a temporary of type "cv1 T1" is created and
7741 initialized from the initializer expression using the rules
7742 for a non-reference copy-initialization (8.5). */
7743
7744 tree type = TREE_TYPE (ref_type);
7745 cp_lvalue_kind lvalue = lvalue_kind (expr);
7746
7747 gcc_assert (similar_type_p (type, next_conversion (convs)->type));
7748 if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
7749 && !TYPE_REF_IS_RVALUE (ref_type))
7750 {
7751 /* If the reference is volatile or non-const, we
7752 cannot create a temporary. */
7753 if (complain & tf_error)
7754 {
7755 if (lvalue & clk_bitfield)
7756 error_at (loc, "cannot bind bit-field %qE to %qT",
7757 expr, ref_type);
7758 else if (lvalue & clk_packed)
7759 error_at (loc, "cannot bind packed field %qE to %qT",
7760 expr, ref_type);
7761 else
7762 error_at (loc, "cannot bind rvalue %qE to %qT",
7763 expr, ref_type);
7764 }
7765 return error_mark_node;
7766 }
7767 /* If the source is a packed field, and we must use a copy
7768 constructor, then building the target expr will require
7769 binding the field to the reference parameter to the
7770 copy constructor, and we'll end up with an infinite
7771 loop. If we can use a bitwise copy, then we'll be
7772 OK. */
7773 if ((lvalue & clk_packed)
7774 && CLASS_TYPE_P (type)
7775 && type_has_nontrivial_copy_init (type))
7776 {
7777 error_at (loc, "cannot bind packed field %qE to %qT",
7778 expr, ref_type);
7779 return error_mark_node;
7780 }
7781 if (lvalue & clk_bitfield)
7782 {
7783 expr = convert_bitfield_to_declared_type (expr);
7784 expr = fold_convert (type, expr);
7785 }
7786
7787 /* Creating &TARGET_EXPR<> in a template would break when
7788 tsubsting the expression, so use an IMPLICIT_CONV_EXPR
7789 instead. This can happen even when there's no class
7790 involved, e.g., when converting an integer to a reference
7791 type. */
7792 if (processing_template_decl)
7793 return build1 (IMPLICIT_CONV_EXPR, totype, expr);
7794 expr = build_target_expr_with_type (expr, type, complain);
7795 }
7796
7797 /* Take the address of the thing to which we will bind the
7798 reference. */
7799 expr = cp_build_addr_expr (expr, complain);
7800 if (expr == error_mark_node)
7801 return error_mark_node;
7802
7803 /* Convert it to a pointer to the type referred to by the
7804 reference. This will adjust the pointer if a derived to
7805 base conversion is being performed. */
7806 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
7807 expr, complain);
7808 /* Convert the pointer to the desired reference type. */
7809 return build_nop (ref_type, expr);
7810 }
7811
7812 case ck_lvalue:
7813 return decay_conversion (expr, complain);
7814
7815 case ck_fnptr:
7816 /* ??? Should the address of a transaction-safe pointer point to the TM
7817 clone, and this conversion look up the primary function? */
7818 return build_nop (totype, expr);
7819
7820 case ck_qual:
7821 /* Warn about deprecated conversion if appropriate. */
7822 if (complain & tf_warning)
7823 {
7824 string_conv_p (totype, expr, 1);
7825 maybe_warn_array_conv (loc, convs, expr);
7826 }
7827 break;
7828
7829 case ck_ptr:
7830 if (convs->base_p)
7831 expr = convert_to_base (expr, totype, !c_cast_p,
7832 /*nonnull=*/false, complain);
7833 return build_nop (totype, expr);
7834
7835 case ck_pmem:
7836 return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
7837 c_cast_p, complain);
7838
7839 default:
7840 break;
7841 }
7842
7843 if (convs->check_narrowing
7844 && !check_narrowing (totype, expr, complain,
7845 convs->check_narrowing_const_only))
7846 return error_mark_node;
7847
7848 warning_sentinel w (warn_zero_as_null_pointer_constant);
7849 if (issue_conversion_warnings)
7850 expr = cp_convert_and_check (totype, expr, complain);
7851 else
7852 expr = cp_convert (totype, expr, complain);
7853
7854 return expr;
7855 }
7856
7857 /* Wrapper for convert_like_internal that handles creating
7858 IMPLICIT_CONV_EXPR. */
7859
7860 static tree
7861 convert_like (conversion *convs, tree expr, tree fn, int argnum,
7862 bool issue_conversion_warnings, bool c_cast_p,
7863 tsubst_flags_t complain)
7864 {
7865 /* Creating &TARGET_EXPR<> in a template breaks when substituting,
7866 and creating a CALL_EXPR in a template breaks in finish_call_expr
7867 so use an IMPLICIT_CONV_EXPR for this conversion. We would have
7868 created such codes e.g. when calling a user-defined conversion
7869 function. */
7870 tree conv_expr = NULL_TREE;
7871 if (processing_template_decl
7872 && convs->kind != ck_identity
7873 && (CLASS_TYPE_P (convs->type) || CLASS_TYPE_P (TREE_TYPE (expr))))
7874 {
7875 conv_expr = build1 (IMPLICIT_CONV_EXPR, convs->type, expr);
7876 if (convs->kind != ck_ref_bind)
7877 conv_expr = convert_from_reference (conv_expr);
7878 if (!convs->bad_p)
7879 return conv_expr;
7880 /* Do the normal processing to give the bad_p errors. But we still
7881 need to return the IMPLICIT_CONV_EXPR, unless we're returning
7882 error_mark_node. */
7883 }
7884 expr = convert_like_internal (convs, expr, fn, argnum,
7885 issue_conversion_warnings, c_cast_p, complain);
7886 if (expr == error_mark_node)
7887 return error_mark_node;
7888 return conv_expr ? conv_expr : expr;
7889 }
7890
7891 /* Convenience wrapper for convert_like. */
7892
7893 static inline tree
7894 convert_like (conversion *convs, tree expr, tsubst_flags_t complain)
7895 {
7896 return convert_like (convs, expr, NULL_TREE, 0,
7897 /*issue_conversion_warnings=*/true,
7898 /*c_cast_p=*/false, complain);
7899 }
7900
7901 /* Convenience wrapper for convert_like. */
7902
7903 static inline tree
7904 convert_like_with_context (conversion *convs, tree expr, tree fn, int argnum,
7905 tsubst_flags_t complain)
7906 {
7907 return convert_like (convs, expr, fn, argnum,
7908 /*issue_conversion_warnings=*/true,
7909 /*c_cast_p=*/false, complain);
7910 }
7911
7912 /* ARG is being passed to a varargs function. Perform any conversions
7913 required. Return the converted value. */
7914
7915 tree
7916 convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
7917 {
7918 tree arg_type = TREE_TYPE (arg);
7919 location_t loc = cp_expr_loc_or_input_loc (arg);
7920
7921 /* [expr.call]
7922
7923 If the argument has integral or enumeration type that is subject
7924 to the integral promotions (_conv.prom_), or a floating-point
7925 type that is subject to the floating-point promotion
7926 (_conv.fpprom_), the value of the argument is converted to the
7927 promoted type before the call. */
7928 if (TREE_CODE (arg_type) == REAL_TYPE
7929 && (TYPE_PRECISION (arg_type)
7930 < TYPE_PRECISION (double_type_node))
7931 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type)))
7932 {
7933 if ((complain & tf_warning)
7934 && warn_double_promotion && !c_inhibit_evaluation_warnings)
7935 warning_at (loc, OPT_Wdouble_promotion,
7936 "implicit conversion from %qH to %qI when passing "
7937 "argument to function",
7938 arg_type, double_type_node);
7939 arg = mark_rvalue_use (arg);
7940 arg = convert_to_real_nofold (double_type_node, arg);
7941 }
7942 else if (NULLPTR_TYPE_P (arg_type))
7943 {
7944 arg = mark_rvalue_use (arg);
7945 if (TREE_SIDE_EFFECTS (arg))
7946 arg = cp_build_compound_expr (arg, null_pointer_node, complain);
7947 else
7948 arg = null_pointer_node;
7949 }
7950 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
7951 {
7952 if (SCOPED_ENUM_P (arg_type))
7953 {
7954 tree prom = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg,
7955 complain);
7956 prom = cp_perform_integral_promotions (prom, complain);
7957 if (abi_version_crosses (6)
7958 && TYPE_MODE (TREE_TYPE (prom)) != TYPE_MODE (arg_type)
7959 && (complain & tf_warning))
7960 warning_at (loc, OPT_Wabi, "scoped enum %qT passed through %<...%>"
7961 " as %qT before %<-fabi-version=6%>, %qT after",
7962 arg_type,
7963 TREE_TYPE (prom), ENUM_UNDERLYING_TYPE (arg_type));
7964 if (!abi_version_at_least (6))
7965 arg = prom;
7966 }
7967 else
7968 arg = cp_perform_integral_promotions (arg, complain);
7969 }
7970 else
7971 /* [expr.call]
7972
7973 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7974 standard conversions are performed. */
7975 arg = decay_conversion (arg, complain);
7976
7977 arg = require_complete_type_sfinae (arg, complain);
7978 arg_type = TREE_TYPE (arg);
7979
7980 if (arg != error_mark_node
7981 /* In a template (or ill-formed code), we can have an incomplete type
7982 even after require_complete_type_sfinae, in which case we don't know
7983 whether it has trivial copy or not. */
7984 && COMPLETE_TYPE_P (arg_type)
7985 && !cp_unevaluated_operand)
7986 {
7987 /* [expr.call] 5.2.2/7:
7988 Passing a potentially-evaluated argument of class type (Clause 9)
7989 with a non-trivial copy constructor or a non-trivial destructor
7990 with no corresponding parameter is conditionally-supported, with
7991 implementation-defined semantics.
7992
7993 We support it as pass-by-invisible-reference, just like a normal
7994 value parameter.
7995
7996 If the call appears in the context of a sizeof expression,
7997 it is not potentially-evaluated. */
7998 if (type_has_nontrivial_copy_init (arg_type)
7999 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type))
8000 {
8001 arg = force_rvalue (arg, complain);
8002 if (complain & tf_warning)
8003 warning (OPT_Wconditionally_supported,
8004 "passing objects of non-trivially-copyable "
8005 "type %q#T through %<...%> is conditionally supported",
8006 arg_type);
8007 return build1 (ADDR_EXPR, build_reference_type (arg_type), arg);
8008 }
8009 /* Build up a real lvalue-to-rvalue conversion in case the
8010 copy constructor is trivial but not callable. */
8011 else if (CLASS_TYPE_P (arg_type))
8012 force_rvalue (arg, complain);
8013
8014 }
8015
8016 return arg;
8017 }
8018
8019 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
8020
8021 tree
8022 build_x_va_arg (location_t loc, tree expr, tree type)
8023 {
8024 if (processing_template_decl)
8025 {
8026 tree r = build_min (VA_ARG_EXPR, type, expr);
8027 SET_EXPR_LOCATION (r, loc);
8028 return r;
8029 }
8030
8031 type = complete_type_or_else (type, NULL_TREE);
8032
8033 if (expr == error_mark_node || !type)
8034 return error_mark_node;
8035
8036 expr = mark_lvalue_use (expr);
8037
8038 if (TYPE_REF_P (type))
8039 {
8040 error ("cannot receive reference type %qT through %<...%>", type);
8041 return error_mark_node;
8042 }
8043
8044 if (type_has_nontrivial_copy_init (type)
8045 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
8046 {
8047 /* conditionally-supported behavior [expr.call] 5.2.2/7. Let's treat
8048 it as pass by invisible reference. */
8049 warning_at (loc, OPT_Wconditionally_supported,
8050 "receiving objects of non-trivially-copyable type %q#T "
8051 "through %<...%> is conditionally-supported", type);
8052
8053 tree ref = cp_build_reference_type (type, false);
8054 expr = build_va_arg (loc, expr, ref);
8055 return convert_from_reference (expr);
8056 }
8057
8058 tree ret = build_va_arg (loc, expr, type);
8059 if (CLASS_TYPE_P (type))
8060 /* Wrap the VA_ARG_EXPR in a TARGET_EXPR now so other code doesn't need to
8061 know how to handle it. */
8062 ret = get_target_expr (ret);
8063 return ret;
8064 }
8065
8066 /* TYPE has been given to va_arg. Apply the default conversions which
8067 would have happened when passed via ellipsis. Return the promoted
8068 type, or the passed type if there is no change. */
8069
8070 tree
8071 cxx_type_promotes_to (tree type)
8072 {
8073 tree promote;
8074
8075 /* Perform the array-to-pointer and function-to-pointer
8076 conversions. */
8077 type = type_decays_to (type);
8078
8079 promote = type_promotes_to (type);
8080 if (same_type_p (type, promote))
8081 promote = type;
8082
8083 return promote;
8084 }
8085
8086 /* ARG is a default argument expression being passed to a parameter of
8087 the indicated TYPE, which is a parameter to FN. PARMNUM is the
8088 zero-based argument number. Do any required conversions. Return
8089 the converted value. */
8090
8091 static GTY(()) vec<tree, va_gc> *default_arg_context;
8092 void
8093 push_defarg_context (tree fn)
8094 { vec_safe_push (default_arg_context, fn); }
8095
8096 void
8097 pop_defarg_context (void)
8098 { default_arg_context->pop (); }
8099
8100 tree
8101 convert_default_arg (tree type, tree arg, tree fn, int parmnum,
8102 tsubst_flags_t complain)
8103 {
8104 int i;
8105 tree t;
8106
8107 /* See through clones. */
8108 fn = DECL_ORIGIN (fn);
8109 /* And inheriting ctors. */
8110 if (flag_new_inheriting_ctors)
8111 fn = strip_inheriting_ctors (fn);
8112
8113 /* Detect recursion. */
8114 FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t)
8115 if (t == fn)
8116 {
8117 if (complain & tf_error)
8118 error ("recursive evaluation of default argument for %q#D", fn);
8119 return error_mark_node;
8120 }
8121
8122 /* If the ARG is an unparsed default argument expression, the
8123 conversion cannot be performed. */
8124 if (TREE_CODE (arg) == DEFERRED_PARSE)
8125 {
8126 if (complain & tf_error)
8127 error ("call to %qD uses the default argument for parameter %P, which "
8128 "is not yet defined", fn, parmnum);
8129 return error_mark_node;
8130 }
8131
8132 push_defarg_context (fn);
8133
8134 if (fn && DECL_TEMPLATE_INFO (fn))
8135 arg = tsubst_default_argument (fn, parmnum, type, arg, complain);
8136
8137 /* Due to:
8138
8139 [dcl.fct.default]
8140
8141 The names in the expression are bound, and the semantic
8142 constraints are checked, at the point where the default
8143 expressions appears.
8144
8145 we must not perform access checks here. */
8146 push_deferring_access_checks (dk_no_check);
8147 /* We must make a copy of ARG, in case subsequent processing
8148 alters any part of it. */
8149 arg = break_out_target_exprs (arg, /*clear location*/true);
8150
8151 arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
8152 ICR_DEFAULT_ARGUMENT, fn, parmnum,
8153 complain);
8154 arg = convert_for_arg_passing (type, arg, complain);
8155 pop_deferring_access_checks();
8156
8157 pop_defarg_context ();
8158
8159 return arg;
8160 }
8161
8162 /* Returns the type which will really be used for passing an argument of
8163 type TYPE. */
8164
8165 tree
8166 type_passed_as (tree type)
8167 {
8168 /* Pass classes with copy ctors by invisible reference. */
8169 if (TREE_ADDRESSABLE (type))
8170 {
8171 type = build_reference_type (type);
8172 /* There are no other pointers to this temporary. */
8173 type = cp_build_qualified_type (type, TYPE_QUAL_RESTRICT);
8174 }
8175 else if (targetm.calls.promote_prototypes (NULL_TREE)
8176 && INTEGRAL_TYPE_P (type)
8177 && COMPLETE_TYPE_P (type)
8178 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
8179 type = integer_type_node;
8180
8181 return type;
8182 }
8183
8184 /* Actually perform the appropriate conversion. */
8185
8186 tree
8187 convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
8188 {
8189 tree bitfield_type;
8190
8191 /* If VAL is a bitfield, then -- since it has already been converted
8192 to TYPE -- it cannot have a precision greater than TYPE.
8193
8194 If it has a smaller precision, we must widen it here. For
8195 example, passing "int f:3;" to a function expecting an "int" will
8196 not result in any conversion before this point.
8197
8198 If the precision is the same we must not risk widening. For
8199 example, the COMPONENT_REF for a 32-bit "long long" bitfield will
8200 often have type "int", even though the C++ type for the field is
8201 "long long". If the value is being passed to a function
8202 expecting an "int", then no conversions will be required. But,
8203 if we call convert_bitfield_to_declared_type, the bitfield will
8204 be converted to "long long". */
8205 bitfield_type = is_bitfield_expr_with_lowered_type (val);
8206 if (bitfield_type
8207 && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
8208 val = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type), val);
8209
8210 if (val == error_mark_node)
8211 ;
8212 /* Pass classes with copy ctors by invisible reference. */
8213 else if (TREE_ADDRESSABLE (type))
8214 val = build1 (ADDR_EXPR, build_reference_type (type), val);
8215 else if (targetm.calls.promote_prototypes (NULL_TREE)
8216 && INTEGRAL_TYPE_P (type)
8217 && COMPLETE_TYPE_P (type)
8218 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
8219 val = cp_perform_integral_promotions (val, complain);
8220 if (complain & tf_warning)
8221 {
8222 if (warn_suggest_attribute_format)
8223 {
8224 tree rhstype = TREE_TYPE (val);
8225 const enum tree_code coder = TREE_CODE (rhstype);
8226 const enum tree_code codel = TREE_CODE (type);
8227 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8228 && coder == codel
8229 && check_missing_format_attribute (type, rhstype))
8230 warning (OPT_Wsuggest_attribute_format,
8231 "argument of function call might be a candidate "
8232 "for a format attribute");
8233 }
8234 maybe_warn_parm_abi (type, cp_expr_loc_or_input_loc (val));
8235 }
8236
8237 if (complain & tf_warning)
8238 warn_for_address_or_pointer_of_packed_member (type, val);
8239
8240 return val;
8241 }
8242
8243 /* Returns non-zero iff FN is a function with magic varargs, i.e. ones for
8244 which just decay_conversion or no conversions at all should be done.
8245 This is true for some builtins which don't act like normal functions.
8246 Return 2 if no conversions at all should be done, 1 if just
8247 decay_conversion. Return 3 for special treatment of the 3rd argument
8248 for __builtin_*_overflow_p. */
8249
8250 int
8251 magic_varargs_p (tree fn)
8252 {
8253 if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8254 switch (DECL_FUNCTION_CODE (fn))
8255 {
8256 case BUILT_IN_CLASSIFY_TYPE:
8257 case BUILT_IN_CONSTANT_P:
8258 case BUILT_IN_NEXT_ARG:
8259 case BUILT_IN_VA_START:
8260 return 1;
8261
8262 case BUILT_IN_ADD_OVERFLOW_P:
8263 case BUILT_IN_SUB_OVERFLOW_P:
8264 case BUILT_IN_MUL_OVERFLOW_P:
8265 return 3;
8266
8267 default:;
8268 return lookup_attribute ("type generic",
8269 TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
8270 }
8271
8272 return 0;
8273 }
8274
8275 /* Returns the decl of the dispatcher function if FN is a function version. */
8276
8277 tree
8278 get_function_version_dispatcher (tree fn)
8279 {
8280 tree dispatcher_decl = NULL;
8281
8282 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
8283 && DECL_FUNCTION_VERSIONED (fn));
8284
8285 gcc_assert (targetm.get_function_versions_dispatcher);
8286 dispatcher_decl = targetm.get_function_versions_dispatcher (fn);
8287
8288 if (dispatcher_decl == NULL)
8289 {
8290 error_at (input_location, "use of multiversioned function "
8291 "without a default");
8292 return NULL;
8293 }
8294
8295 retrofit_lang_decl (dispatcher_decl);
8296 gcc_assert (dispatcher_decl != NULL);
8297 return dispatcher_decl;
8298 }
8299
8300 /* fn is a function version dispatcher that is marked used. Mark all the
8301 semantically identical function versions it will dispatch as used. */
8302
8303 void
8304 mark_versions_used (tree fn)
8305 {
8306 struct cgraph_node *node;
8307 struct cgraph_function_version_info *node_v;
8308 struct cgraph_function_version_info *it_v;
8309
8310 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8311
8312 node = cgraph_node::get (fn);
8313 if (node == NULL)
8314 return;
8315
8316 gcc_assert (node->dispatcher_function);
8317
8318 node_v = node->function_version ();
8319 if (node_v == NULL)
8320 return;
8321
8322 /* All semantically identical versions are chained. Traverse and mark each
8323 one of them as used. */
8324 it_v = node_v->next;
8325 while (it_v != NULL)
8326 {
8327 mark_used (it_v->this_node->decl);
8328 it_v = it_v->next;
8329 }
8330 }
8331
8332 /* Build a call to "the copy constructor" for the type of A, even if it
8333 wouldn't be selected by normal overload resolution. Used for
8334 diagnostics. */
8335
8336 static tree
8337 call_copy_ctor (tree a, tsubst_flags_t complain)
8338 {
8339 tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (a));
8340 tree binfo = TYPE_BINFO (ctype);
8341 tree copy = get_copy_ctor (ctype, complain);
8342 copy = build_baselink (binfo, binfo, copy, NULL_TREE);
8343 tree ob = build_dummy_object (ctype);
8344 releasing_vec args (make_tree_vector_single (a));
8345 tree r = build_new_method_call (ob, copy, &args, NULL_TREE,
8346 LOOKUP_NORMAL, NULL, complain);
8347 return r;
8348 }
8349
8350 /* Return true iff T refers to a base field. */
8351
8352 static bool
8353 is_base_field_ref (tree t)
8354 {
8355 STRIP_NOPS (t);
8356 if (TREE_CODE (t) == ADDR_EXPR)
8357 t = TREE_OPERAND (t, 0);
8358 if (TREE_CODE (t) == COMPONENT_REF)
8359 t = TREE_OPERAND (t, 1);
8360 if (TREE_CODE (t) == FIELD_DECL)
8361 return DECL_FIELD_IS_BASE (t);
8362 return false;
8363 }
8364
8365 /* We can't elide a copy from a function returning by value to a base
8366 subobject, as the callee might clobber tail padding. Return true iff this
8367 could be that case. */
8368
8369 static bool
8370 unsafe_copy_elision_p (tree target, tree exp)
8371 {
8372 /* Copy elision only happens with a TARGET_EXPR. */
8373 if (TREE_CODE (exp) != TARGET_EXPR)
8374 return false;
8375 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
8376 /* It's safe to elide the copy for a class with no tail padding. */
8377 if (tree_int_cst_equal (TYPE_SIZE (type), CLASSTYPE_SIZE (type)))
8378 return false;
8379 /* It's safe to elide the copy if we aren't initializing a base object. */
8380 if (!is_base_field_ref (target))
8381 return false;
8382 tree init = TARGET_EXPR_INITIAL (exp);
8383 /* build_compound_expr pushes COMPOUND_EXPR inside TARGET_EXPR. */
8384 while (TREE_CODE (init) == COMPOUND_EXPR)
8385 init = TREE_OPERAND (init, 1);
8386 if (TREE_CODE (init) == COND_EXPR)
8387 {
8388 /* We'll end up copying from each of the arms of the COND_EXPR directly
8389 into the target, so look at them. */
8390 if (tree op = TREE_OPERAND (init, 1))
8391 if (unsafe_copy_elision_p (target, op))
8392 return true;
8393 return unsafe_copy_elision_p (target, TREE_OPERAND (init, 2));
8394 }
8395 return (TREE_CODE (init) == AGGR_INIT_EXPR
8396 && !AGGR_INIT_VIA_CTOR_P (init));
8397 }
8398
8399 /* True iff C is a conversion that binds a reference to a prvalue. */
8400
8401 static bool
8402 conv_binds_ref_to_prvalue (conversion *c)
8403 {
8404 if (c->kind != ck_ref_bind)
8405 return false;
8406 if (c->need_temporary_p)
8407 return true;
8408
8409 c = next_conversion (c);
8410
8411 if (c->kind == ck_rvalue)
8412 return true;
8413 if (c->kind == ck_user && !TYPE_REF_P (c->type))
8414 return true;
8415 if (c->kind == ck_identity && c->u.expr
8416 && TREE_CODE (c->u.expr) == TARGET_EXPR)
8417 return true;
8418
8419 return false;
8420 }
8421
8422 /* Call the trivial destructor for INSTANCE, which can be either an lvalue of
8423 class type or a pointer to class type. */
8424
8425 tree
8426 build_trivial_dtor_call (tree instance)
8427 {
8428 gcc_assert (!is_dummy_object (instance));
8429
8430 if (!flag_lifetime_dse)
8431 {
8432 no_clobber:
8433 return fold_convert (void_type_node, instance);
8434 }
8435
8436 if (INDIRECT_TYPE_P (TREE_TYPE (instance)))
8437 {
8438 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
8439 goto no_clobber;
8440 instance = cp_build_fold_indirect_ref (instance);
8441 }
8442
8443 /* A trivial destructor should still clobber the object. */
8444 tree clobber = build_clobber (TREE_TYPE (instance));
8445 return build2 (MODIFY_EXPR, void_type_node,
8446 instance, clobber);
8447 }
8448
8449 /* Subroutine of the various build_*_call functions. Overload resolution
8450 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
8451 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
8452 bitmask of various LOOKUP_* flags which apply to the call itself. */
8453
8454 static tree
8455 build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
8456 {
8457 tree fn = cand->fn;
8458 const vec<tree, va_gc> *args = cand->args;
8459 tree first_arg = cand->first_arg;
8460 conversion **convs = cand->convs;
8461 conversion *conv;
8462 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
8463 int parmlen;
8464 tree val;
8465 int i = 0;
8466 int j = 0;
8467 unsigned int arg_index = 0;
8468 int is_method = 0;
8469 int nargs;
8470 tree *argarray;
8471 bool already_used = false;
8472
8473 /* In a template, there is no need to perform all of the work that
8474 is normally done. We are only interested in the type of the call
8475 expression, i.e., the return type of the function. Any semantic
8476 errors will be deferred until the template is instantiated. */
8477 if (processing_template_decl)
8478 {
8479 tree expr, addr;
8480 tree return_type;
8481 const tree *argarray;
8482 unsigned int nargs;
8483
8484 if (undeduced_auto_decl (fn))
8485 mark_used (fn, complain);
8486 else
8487 /* Otherwise set TREE_USED for the benefit of -Wunused-function.
8488 See PR80598. */
8489 TREE_USED (fn) = 1;
8490
8491 return_type = TREE_TYPE (TREE_TYPE (fn));
8492 nargs = vec_safe_length (args);
8493 if (first_arg == NULL_TREE)
8494 argarray = args->address ();
8495 else
8496 {
8497 tree *alcarray;
8498 unsigned int ix;
8499 tree arg;
8500
8501 ++nargs;
8502 alcarray = XALLOCAVEC (tree, nargs);
8503 alcarray[0] = build_this (first_arg);
8504 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
8505 alcarray[ix + 1] = arg;
8506 argarray = alcarray;
8507 }
8508
8509 addr = build_addr_func (fn, complain);
8510 if (addr == error_mark_node)
8511 return error_mark_node;
8512 expr = build_call_array_loc (input_location, return_type,
8513 addr, nargs, argarray);
8514 if (TREE_THIS_VOLATILE (fn) && cfun)
8515 current_function_returns_abnormally = 1;
8516 if (TREE_CODE (fn) == FUNCTION_DECL
8517 && DECL_IMMEDIATE_FUNCTION_P (fn)
8518 && cp_unevaluated_operand == 0
8519 && (current_function_decl == NULL_TREE
8520 || !DECL_IMMEDIATE_FUNCTION_P (current_function_decl))
8521 && (current_binding_level->kind != sk_function_parms
8522 || !current_binding_level->immediate_fn_ctx_p))
8523 {
8524 tree obj_arg = NULL_TREE, exprimm = expr;
8525 if (DECL_CONSTRUCTOR_P (fn))
8526 obj_arg = first_arg;
8527 if (obj_arg
8528 && is_dummy_object (obj_arg)
8529 && !type_dependent_expression_p (obj_arg))
8530 {
8531 exprimm = build_cplus_new (DECL_CONTEXT (fn), expr, complain);
8532 obj_arg = NULL_TREE;
8533 }
8534 /* Look through *(const T *)&obj. */
8535 else if (obj_arg && TREE_CODE (obj_arg) == INDIRECT_REF)
8536 {
8537 tree addr = TREE_OPERAND (obj_arg, 0);
8538 STRIP_NOPS (addr);
8539 if (TREE_CODE (addr) == ADDR_EXPR)
8540 {
8541 tree typeo = TREE_TYPE (obj_arg);
8542 tree typei = TREE_TYPE (TREE_OPERAND (addr, 0));
8543 if (same_type_ignoring_top_level_qualifiers_p (typeo, typei))
8544 obj_arg = TREE_OPERAND (addr, 0);
8545 }
8546 }
8547 fold_non_dependent_expr (exprimm, complain,
8548 /*manifestly_const_eval=*/true,
8549 obj_arg);
8550 }
8551 return convert_from_reference (expr);
8552 }
8553
8554 /* Give any warnings we noticed during overload resolution. */
8555 if (cand->warnings && (complain & tf_warning))
8556 {
8557 struct candidate_warning *w;
8558 for (w = cand->warnings; w; w = w->next)
8559 joust (cand, w->loser, 1, complain);
8560 }
8561
8562 /* Core issue 2327: P0135 doesn't say how to handle the case where the
8563 argument to the copy constructor ends up being a prvalue after
8564 conversion. Let's do the normal processing, but pretend we aren't
8565 actually using the copy constructor. */
8566 bool force_elide = false;
8567 if (cxx_dialect >= cxx17
8568 && cand->num_convs == 1
8569 && DECL_COMPLETE_CONSTRUCTOR_P (fn)
8570 && (DECL_COPY_CONSTRUCTOR_P (fn)
8571 || DECL_MOVE_CONSTRUCTOR_P (fn))
8572 && conv_binds_ref_to_prvalue (convs[0]))
8573 {
8574 force_elide = true;
8575 goto not_really_used;
8576 }
8577
8578 /* OK, we're actually calling this inherited constructor; set its deletedness
8579 appropriately. We can get away with doing this here because calling is
8580 the only way to refer to a constructor. */
8581 if (DECL_INHERITED_CTOR (fn))
8582 deduce_inheriting_ctor (fn);
8583
8584 /* Make =delete work with SFINAE. */
8585 if (DECL_DELETED_FN (fn))
8586 {
8587 if (complain & tf_error)
8588 mark_used (fn);
8589 return error_mark_node;
8590 }
8591
8592 if (DECL_FUNCTION_MEMBER_P (fn))
8593 {
8594 tree access_fn;
8595 /* If FN is a template function, two cases must be considered.
8596 For example:
8597
8598 struct A {
8599 protected:
8600 template <class T> void f();
8601 };
8602 template <class T> struct B {
8603 protected:
8604 void g();
8605 };
8606 struct C : A, B<int> {
8607 using A::f; // #1
8608 using B<int>::g; // #2
8609 };
8610
8611 In case #1 where `A::f' is a member template, DECL_ACCESS is
8612 recorded in the primary template but not in its specialization.
8613 We check access of FN using its primary template.
8614
8615 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
8616 because it is a member of class template B, DECL_ACCESS is
8617 recorded in the specialization `B<int>::g'. We cannot use its
8618 primary template because `B<T>::g' and `B<int>::g' may have
8619 different access. */
8620 if (DECL_TEMPLATE_INFO (fn)
8621 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
8622 access_fn = DECL_TI_TEMPLATE (fn);
8623 else
8624 access_fn = fn;
8625 if (!perform_or_defer_access_check (cand->access_path, access_fn,
8626 fn, complain))
8627 return error_mark_node;
8628 }
8629
8630 /* If we're checking for implicit delete, don't bother with argument
8631 conversions. */
8632 if (flags & LOOKUP_SPECULATIVE)
8633 {
8634 if (cand->viable == 1)
8635 return fn;
8636 else if (!(complain & tf_error))
8637 /* Reject bad conversions now. */
8638 return error_mark_node;
8639 /* else continue to get conversion error. */
8640 }
8641
8642 not_really_used:
8643
8644 /* N3276 magic doesn't apply to nested calls. */
8645 tsubst_flags_t decltype_flag = (complain & tf_decltype);
8646 complain &= ~tf_decltype;
8647 /* No-Cleanup doesn't apply to nested calls either. */
8648 tsubst_flags_t no_cleanup_complain = complain;
8649 complain &= ~tf_no_cleanup;
8650
8651 /* Find maximum size of vector to hold converted arguments. */
8652 parmlen = list_length (parm);
8653 nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0);
8654 if (parmlen > nargs)
8655 nargs = parmlen;
8656 argarray = XALLOCAVEC (tree, nargs);
8657
8658 /* The implicit parameters to a constructor are not considered by overload
8659 resolution, and must be of the proper type. */
8660 if (DECL_CONSTRUCTOR_P (fn))
8661 {
8662 tree object_arg;
8663 if (first_arg != NULL_TREE)
8664 {
8665 object_arg = first_arg;
8666 first_arg = NULL_TREE;
8667 }
8668 else
8669 {
8670 object_arg = (*args)[arg_index];
8671 ++arg_index;
8672 }
8673 argarray[j++] = build_this (object_arg);
8674 parm = TREE_CHAIN (parm);
8675 /* We should never try to call the abstract constructor. */
8676 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
8677
8678 if (DECL_HAS_VTT_PARM_P (fn))
8679 {
8680 argarray[j++] = (*args)[arg_index];
8681 ++arg_index;
8682 parm = TREE_CHAIN (parm);
8683 }
8684
8685 if (cxx_dialect < cxx20
8686 && (cand->flags & LOOKUP_PREFER_RVALUE))
8687 {
8688 /* The implicit move specified in 15.8.3/3 fails "...if the type of
8689 the first parameter of the selected constructor is not an rvalue
8690 reference to the object's type (possibly cv-qualified)...." */
8691 gcc_assert (!(complain & tf_error));
8692 tree ptype = convs[0]->type;
8693 /* Allow calling a by-value converting constructor even though it
8694 isn't permitted by the above, because we've allowed it since GCC 5
8695 (PR58051) and it's allowed in C++20. But don't call a copy
8696 constructor. */
8697 if ((TYPE_REF_P (ptype) && !TYPE_REF_IS_RVALUE (ptype))
8698 || CONVERSION_RANK (convs[0]) > cr_exact)
8699 return error_mark_node;
8700 }
8701 }
8702 /* Bypass access control for 'this' parameter. */
8703 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
8704 {
8705 tree arg = build_this (first_arg != NULL_TREE
8706 ? first_arg
8707 : (*args)[arg_index]);
8708 tree argtype = TREE_TYPE (arg);
8709
8710 if (arg == error_mark_node)
8711 return error_mark_node;
8712
8713 if (convs[i]->bad_p)
8714 {
8715 if (complain & tf_error)
8716 {
8717 auto_diagnostic_group d;
8718 if (permerror (input_location, "passing %qT as %<this%> "
8719 "argument discards qualifiers",
8720 TREE_TYPE (argtype)))
8721 inform (DECL_SOURCE_LOCATION (fn), " in call to %qD", fn);
8722 }
8723 else
8724 return error_mark_node;
8725 }
8726
8727 /* The class where FN is defined. */
8728 tree ctx = DECL_CONTEXT (fn);
8729
8730 /* See if the function member or the whole class type is declared
8731 final and the call can be devirtualized. */
8732 if (DECL_FINAL_P (fn) || CLASSTYPE_FINAL (ctx))
8733 flags |= LOOKUP_NONVIRTUAL;
8734
8735 /* [class.mfct.non-static]: If a non-static member function of a class
8736 X is called for an object that is not of type X, or of a type
8737 derived from X, the behavior is undefined.
8738
8739 So we can assume that anything passed as 'this' is non-null, and
8740 optimize accordingly. */
8741 /* Check that the base class is accessible. */
8742 if (!accessible_base_p (TREE_TYPE (argtype),
8743 BINFO_TYPE (cand->conversion_path), true))
8744 {
8745 if (complain & tf_error)
8746 error ("%qT is not an accessible base of %qT",
8747 BINFO_TYPE (cand->conversion_path),
8748 TREE_TYPE (argtype));
8749 else
8750 return error_mark_node;
8751 }
8752 /* If fn was found by a using declaration, the conversion path
8753 will be to the derived class, not the base declaring fn. We
8754 must convert to the base. */
8755 tree base_binfo = cand->conversion_path;
8756 if (BINFO_TYPE (base_binfo) != ctx)
8757 {
8758 base_binfo = lookup_base (base_binfo, ctx, ba_unique, NULL, complain);
8759 if (base_binfo == error_mark_node)
8760 return error_mark_node;
8761 }
8762 tree converted_arg = build_base_path (PLUS_EXPR, arg,
8763 base_binfo, 1, complain);
8764
8765 /* If we know the dynamic type of the object, look up the final overrider
8766 in the BINFO. */
8767 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
8768 && resolves_to_fixed_type_p (arg))
8769 {
8770 fn = lookup_vfn_in_binfo (DECL_VINDEX (fn), base_binfo);
8771 flags |= LOOKUP_NONVIRTUAL;
8772 }
8773
8774 argarray[j++] = converted_arg;
8775 parm = TREE_CHAIN (parm);
8776 if (first_arg != NULL_TREE)
8777 first_arg = NULL_TREE;
8778 else
8779 ++arg_index;
8780 ++i;
8781 is_method = 1;
8782 }
8783
8784 gcc_assert (first_arg == NULL_TREE);
8785 for (; arg_index < vec_safe_length (args) && parm;
8786 parm = TREE_CHAIN (parm), ++arg_index, ++i)
8787 {
8788 tree type = TREE_VALUE (parm);
8789 tree arg = (*args)[arg_index];
8790 bool conversion_warning = true;
8791
8792 conv = convs[i];
8793
8794 /* If the argument is NULL and used to (implicitly) instantiate a
8795 template function (and bind one of the template arguments to
8796 the type of 'long int'), we don't want to warn about passing NULL
8797 to non-pointer argument.
8798 For example, if we have this template function:
8799
8800 template<typename T> void func(T x) {}
8801
8802 we want to warn (when -Wconversion is enabled) in this case:
8803
8804 void foo() {
8805 func<int>(NULL);
8806 }
8807
8808 but not in this case:
8809
8810 void foo() {
8811 func(NULL);
8812 }
8813 */
8814 if (null_node_p (arg)
8815 && DECL_TEMPLATE_INFO (fn)
8816 && cand->template_decl
8817 && !cand->explicit_targs)
8818 conversion_warning = false;
8819
8820 /* Set user_conv_p on the argument conversions, so rvalue/base handling
8821 knows not to allow any more UDCs. This needs to happen after we
8822 process cand->warnings. */
8823 if (flags & LOOKUP_NO_CONVERSION)
8824 conv->user_conv_p = true;
8825
8826 tsubst_flags_t arg_complain = complain;
8827 if (!conversion_warning)
8828 arg_complain &= ~tf_warning;
8829
8830 val = convert_like_with_context (conv, arg, fn, i - is_method,
8831 arg_complain);
8832 val = convert_for_arg_passing (type, val, arg_complain);
8833
8834 if (val == error_mark_node)
8835 return error_mark_node;
8836 else
8837 argarray[j++] = val;
8838 }
8839
8840 /* Default arguments */
8841 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
8842 {
8843 if (TREE_VALUE (parm) == error_mark_node)
8844 return error_mark_node;
8845 val = convert_default_arg (TREE_VALUE (parm),
8846 TREE_PURPOSE (parm),
8847 fn, i - is_method,
8848 complain);
8849 if (val == error_mark_node)
8850 return error_mark_node;
8851 argarray[j++] = val;
8852 }
8853
8854 /* Ellipsis */
8855 int magic = magic_varargs_p (fn);
8856 for (; arg_index < vec_safe_length (args); ++arg_index)
8857 {
8858 tree a = (*args)[arg_index];
8859 if ((magic == 3 && arg_index == 2) || magic == 2)
8860 {
8861 /* Do no conversions for certain magic varargs. */
8862 a = mark_type_use (a);
8863 if (TREE_CODE (a) == FUNCTION_DECL && reject_gcc_builtin (a))
8864 return error_mark_node;
8865 }
8866 else if (magic != 0)
8867 /* For other magic varargs only do decay_conversion. */
8868 a = decay_conversion (a, complain);
8869 else if (DECL_CONSTRUCTOR_P (fn)
8870 && same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (fn),
8871 TREE_TYPE (a)))
8872 {
8873 /* Avoid infinite recursion trying to call A(...). */
8874 if (complain & tf_error)
8875 /* Try to call the actual copy constructor for a good error. */
8876 call_copy_ctor (a, complain);
8877 return error_mark_node;
8878 }
8879 else
8880 a = convert_arg_to_ellipsis (a, complain);
8881 if (a == error_mark_node)
8882 return error_mark_node;
8883 argarray[j++] = a;
8884 }
8885
8886 gcc_assert (j <= nargs);
8887 nargs = j;
8888
8889 /* Avoid performing argument transformation if warnings are disabled.
8890 When tf_warning is set and at least one of the warnings is active
8891 the check_function_arguments function might warn about something. */
8892
8893 bool warned_p = false;
8894 if ((complain & tf_warning)
8895 && (warn_nonnull
8896 || warn_format
8897 || warn_suggest_attribute_format
8898 || warn_restrict))
8899 {
8900 tree *fargs = (!nargs ? argarray
8901 : (tree *) alloca (nargs * sizeof (tree)));
8902 for (j = 0; j < nargs; j++)
8903 {
8904 /* For -Wformat undo the implicit passing by hidden reference
8905 done by convert_arg_to_ellipsis. */
8906 if (TREE_CODE (argarray[j]) == ADDR_EXPR
8907 && TYPE_REF_P (TREE_TYPE (argarray[j])))
8908 fargs[j] = TREE_OPERAND (argarray[j], 0);
8909 else
8910 fargs[j] = argarray[j];
8911 }
8912
8913 warned_p = check_function_arguments (input_location, fn, TREE_TYPE (fn),
8914 nargs, fargs, NULL);
8915 }
8916
8917 if (DECL_INHERITED_CTOR (fn))
8918 {
8919 /* Check for passing ellipsis arguments to an inherited constructor. We
8920 could handle this by open-coding the inherited constructor rather than
8921 defining it, but let's not bother now. */
8922 if (!cp_unevaluated_operand
8923 && cand->num_convs
8924 && cand->convs[cand->num_convs-1]->ellipsis_p)
8925 {
8926 if (complain & tf_error)
8927 {
8928 sorry ("passing arguments to ellipsis of inherited constructor "
8929 "%qD", cand->fn);
8930 inform (DECL_SOURCE_LOCATION (cand->fn), "declared here");
8931 }
8932 return error_mark_node;
8933 }
8934
8935 /* A base constructor inheriting from a virtual base doesn't get the
8936 inherited arguments, just this and __vtt. */
8937 if (ctor_omit_inherited_parms (fn))
8938 nargs = 2;
8939 }
8940
8941 /* Avoid actually calling copy constructors and copy assignment operators,
8942 if possible. */
8943
8944 if (! flag_elide_constructors && !force_elide)
8945 /* Do things the hard way. */;
8946 else if (cand->num_convs == 1
8947 && (DECL_COPY_CONSTRUCTOR_P (fn)
8948 || DECL_MOVE_CONSTRUCTOR_P (fn))
8949 /* It's unsafe to elide the constructor when handling
8950 a noexcept-expression, it may evaluate to the wrong
8951 value (c++/53025). */
8952 && (force_elide || cp_noexcept_operand == 0))
8953 {
8954 tree targ;
8955 tree arg = argarray[num_artificial_parms_for (fn)];
8956 tree fa;
8957 bool trivial = trivial_fn_p (fn);
8958
8959 /* Pull out the real argument, disregarding const-correctness. */
8960 targ = arg;
8961 /* Strip the reference binding for the constructor parameter. */
8962 if (CONVERT_EXPR_P (targ)
8963 && TYPE_REF_P (TREE_TYPE (targ)))
8964 targ = TREE_OPERAND (targ, 0);
8965 /* But don't strip any other reference bindings; binding a temporary to a
8966 reference prevents copy elision. */
8967 while ((CONVERT_EXPR_P (targ)
8968 && !TYPE_REF_P (TREE_TYPE (targ)))
8969 || TREE_CODE (targ) == NON_LVALUE_EXPR)
8970 targ = TREE_OPERAND (targ, 0);
8971 if (TREE_CODE (targ) == ADDR_EXPR)
8972 {
8973 targ = TREE_OPERAND (targ, 0);
8974 if (!same_type_ignoring_top_level_qualifiers_p
8975 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
8976 targ = NULL_TREE;
8977 }
8978 else
8979 targ = NULL_TREE;
8980
8981 if (targ)
8982 arg = targ;
8983 else
8984 arg = cp_build_fold_indirect_ref (arg);
8985
8986 /* In C++17 we shouldn't be copying a TARGET_EXPR except into a base
8987 subobject. */
8988 if (CHECKING_P && cxx_dialect >= cxx17)
8989 gcc_assert (TREE_CODE (arg) != TARGET_EXPR
8990 || force_elide
8991 /* It's from binding the ref parm to a packed field. */
8992 || convs[0]->need_temporary_p
8993 || seen_error ()
8994 /* See unsafe_copy_elision_p. */
8995 || DECL_BASE_CONSTRUCTOR_P (fn));
8996
8997 fa = argarray[0];
8998 bool unsafe = unsafe_copy_elision_p (fa, arg);
8999 bool eliding_temp = (TREE_CODE (arg) == TARGET_EXPR && !unsafe);
9000
9001 /* [class.copy]: the copy constructor is implicitly defined even if the
9002 implementation elided its use. But don't warn about deprecation when
9003 eliding a temporary, as then no copy is actually performed. */
9004 warning_sentinel s (warn_deprecated_copy, eliding_temp);
9005 if (force_elide)
9006 /* The language says this isn't called. */;
9007 else if (!trivial)
9008 {
9009 if (!mark_used (fn, complain) && !(complain & tf_error))
9010 return error_mark_node;
9011 already_used = true;
9012 }
9013 else
9014 cp_warn_deprecated_use (fn, complain);
9015
9016 /* If we're creating a temp and we already have one, don't create a
9017 new one. If we're not creating a temp but we get one, use
9018 INIT_EXPR to collapse the temp into our target. Otherwise, if the
9019 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
9020 temp or an INIT_EXPR otherwise. */
9021 if (is_dummy_object (fa))
9022 {
9023 if (TREE_CODE (arg) == TARGET_EXPR)
9024 return arg;
9025 else if (trivial)
9026 return force_target_expr (DECL_CONTEXT (fn), arg, complain);
9027 }
9028 else if ((trivial || TREE_CODE (arg) == TARGET_EXPR)
9029 && !unsafe)
9030 {
9031 tree to = cp_build_fold_indirect_ref (fa);
9032 val = build2 (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
9033 return val;
9034 }
9035 }
9036 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
9037 && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR)
9038 && trivial_fn_p (fn))
9039 {
9040 tree to = cp_build_fold_indirect_ref (argarray[0]);
9041 tree type = TREE_TYPE (to);
9042 tree as_base = CLASSTYPE_AS_BASE (type);
9043 tree arg = argarray[1];
9044 location_t loc = cp_expr_loc_or_input_loc (arg);
9045
9046 if (is_really_empty_class (type, /*ignore_vptr*/true))
9047 {
9048 /* Avoid copying empty classes. */
9049 val = build2 (COMPOUND_EXPR, type, arg, to);
9050 TREE_NO_WARNING (val) = 1;
9051 }
9052 else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
9053 {
9054 if (is_std_init_list (type)
9055 && conv_binds_ref_to_prvalue (convs[1]))
9056 warning_at (loc, OPT_Winit_list_lifetime,
9057 "assignment from temporary %<initializer_list%> does "
9058 "not extend the lifetime of the underlying array");
9059 arg = cp_build_fold_indirect_ref (arg);
9060 val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
9061 }
9062 else
9063 {
9064 /* We must only copy the non-tail padding parts. */
9065 tree arg0, arg2, t;
9066 tree array_type, alias_set;
9067
9068 arg2 = TYPE_SIZE_UNIT (as_base);
9069 to = cp_stabilize_reference (to);
9070 arg0 = cp_build_addr_expr (to, complain);
9071
9072 array_type = build_array_type (unsigned_char_type_node,
9073 build_index_type
9074 (size_binop (MINUS_EXPR,
9075 arg2, size_int (1))));
9076 alias_set = build_int_cst (build_pointer_type (type), 0);
9077 t = build2 (MODIFY_EXPR, void_type_node,
9078 build2 (MEM_REF, array_type, arg0, alias_set),
9079 build2 (MEM_REF, array_type, arg, alias_set));
9080 val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
9081 TREE_NO_WARNING (val) = 1;
9082 }
9083
9084 cp_warn_deprecated_use (fn, complain);
9085
9086 return val;
9087 }
9088 else if (trivial_fn_p (fn))
9089 {
9090 if (DECL_DESTRUCTOR_P (fn))
9091 return build_trivial_dtor_call (argarray[0]);
9092 else if (default_ctor_p (fn))
9093 {
9094 if (is_dummy_object (argarray[0]))
9095 return force_target_expr (DECL_CONTEXT (fn), void_node,
9096 no_cleanup_complain);
9097 else
9098 return cp_build_fold_indirect_ref (argarray[0]);
9099 }
9100 }
9101
9102 gcc_assert (!force_elide);
9103
9104 if (!already_used
9105 && !mark_used (fn, complain))
9106 return error_mark_node;
9107
9108 /* Warn if the built-in writes to an object of a non-trivial type. */
9109 if (warn_class_memaccess
9110 && vec_safe_length (args) >= 2
9111 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
9112 maybe_warn_class_memaccess (input_location, fn, args);
9113
9114 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
9115 {
9116 tree t;
9117 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
9118 DECL_CONTEXT (fn),
9119 ba_any, NULL, complain);
9120 gcc_assert (binfo && binfo != error_mark_node);
9121
9122 argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
9123 complain);
9124 if (TREE_SIDE_EFFECTS (argarray[0]))
9125 argarray[0] = save_expr (argarray[0]);
9126 t = build_pointer_type (TREE_TYPE (fn));
9127 fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
9128 TREE_TYPE (fn) = t;
9129 }
9130 else
9131 {
9132 /* If FN is marked deprecated, then we've already issued a deprecated-use
9133 warning from mark_used above, so avoid redundantly issuing another one
9134 from build_addr_func. */
9135 warning_sentinel w (warn_deprecated_decl);
9136
9137 fn = build_addr_func (fn, complain);
9138 if (fn == error_mark_node)
9139 return error_mark_node;
9140 }
9141
9142 tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag);
9143 if (call == error_mark_node)
9144 return call;
9145 if (cand->flags & LOOKUP_LIST_INIT_CTOR)
9146 {
9147 tree c = extract_call_expr (call);
9148 /* build_new_op_1 will clear this when appropriate. */
9149 CALL_EXPR_ORDERED_ARGS (c) = true;
9150 }
9151 if (warned_p)
9152 {
9153 tree c = extract_call_expr (call);
9154 if (TREE_CODE (c) == CALL_EXPR)
9155 TREE_NO_WARNING (c) = 1;
9156 }
9157 if (TREE_CODE (fn) == ADDR_EXPR)
9158 {
9159 tree fndecl = STRIP_TEMPLATE (TREE_OPERAND (fn, 0));
9160 if (TREE_CODE (fndecl) == FUNCTION_DECL
9161 && DECL_IMMEDIATE_FUNCTION_P (fndecl)
9162 && cp_unevaluated_operand == 0
9163 && (current_function_decl == NULL_TREE
9164 || !DECL_IMMEDIATE_FUNCTION_P (current_function_decl))
9165 && (current_binding_level->kind != sk_function_parms
9166 || !current_binding_level->immediate_fn_ctx_p))
9167 {
9168 tree obj_arg = NULL_TREE;
9169 if (DECL_CONSTRUCTOR_P (fndecl))
9170 obj_arg = cand->first_arg ? cand->first_arg : (*args)[0];
9171 if (obj_arg && is_dummy_object (obj_arg))
9172 {
9173 call = build_cplus_new (DECL_CONTEXT (fndecl), call, complain);
9174 obj_arg = NULL_TREE;
9175 }
9176 /* Look through *(const T *)&obj. */
9177 else if (obj_arg && TREE_CODE (obj_arg) == INDIRECT_REF)
9178 {
9179 tree addr = TREE_OPERAND (obj_arg, 0);
9180 STRIP_NOPS (addr);
9181 if (TREE_CODE (addr) == ADDR_EXPR)
9182 {
9183 tree typeo = TREE_TYPE (obj_arg);
9184 tree typei = TREE_TYPE (TREE_OPERAND (addr, 0));
9185 if (same_type_ignoring_top_level_qualifiers_p (typeo, typei))
9186 obj_arg = TREE_OPERAND (addr, 0);
9187 }
9188 }
9189 call = cxx_constant_value (call, obj_arg);
9190 }
9191 }
9192 return call;
9193 }
9194
9195 namespace
9196 {
9197
9198 /* Return the DECL of the first non-static subobject of class TYPE
9199 that satisfies the predicate PRED or null if none can be found. */
9200
9201 template <class Predicate>
9202 tree
9203 first_non_static_field (tree type, Predicate pred)
9204 {
9205 if (!type || !CLASS_TYPE_P (type))
9206 return NULL_TREE;
9207
9208 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
9209 {
9210 if (TREE_CODE (field) != FIELD_DECL)
9211 continue;
9212 if (TREE_STATIC (field))
9213 continue;
9214 if (pred (field))
9215 return field;
9216 }
9217
9218 int i = 0;
9219
9220 for (tree base_binfo, binfo = TYPE_BINFO (type);
9221 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
9222 {
9223 tree base = TREE_TYPE (base_binfo);
9224 if (pred (base))
9225 return base;
9226 if (tree field = first_non_static_field (base, pred))
9227 return field;
9228 }
9229
9230 return NULL_TREE;
9231 }
9232
9233 struct NonPublicField
9234 {
9235 bool operator() (const_tree t)
9236 {
9237 return DECL_P (t) && (TREE_PRIVATE (t) || TREE_PROTECTED (t));
9238 }
9239 };
9240
9241 /* Return the DECL of the first non-public subobject of class TYPE
9242 or null if none can be found. */
9243
9244 static inline tree
9245 first_non_public_field (tree type)
9246 {
9247 return first_non_static_field (type, NonPublicField ());
9248 }
9249
9250 struct NonTrivialField
9251 {
9252 bool operator() (const_tree t)
9253 {
9254 return !trivial_type_p (DECL_P (t) ? TREE_TYPE (t) : t);
9255 }
9256 };
9257
9258 /* Return the DECL of the first non-trivial subobject of class TYPE
9259 or null if none can be found. */
9260
9261 static inline tree
9262 first_non_trivial_field (tree type)
9263 {
9264 return first_non_static_field (type, NonTrivialField ());
9265 }
9266
9267 } /* unnamed namespace */
9268
9269 /* Return true if all copy and move assignment operator overloads for
9270 class TYPE are trivial and at least one of them is not deleted and,
9271 when ACCESS is set, accessible. Return false otherwise. Set
9272 HASASSIGN to true when the TYPE has a (not necessarily trivial)
9273 copy or move assignment. */
9274
9275 static bool
9276 has_trivial_copy_assign_p (tree type, bool access, bool *hasassign)
9277 {
9278 tree fns = get_class_binding (type, assign_op_identifier);
9279 bool all_trivial = true;
9280
9281 /* Iterate over overloads of the assignment operator, checking
9282 accessible copy assignments for triviality. */
9283
9284 for (ovl_iterator oi (fns); oi; ++oi)
9285 {
9286 tree f = *oi;
9287
9288 /* Skip operators that aren't copy assignments. */
9289 if (!copy_fn_p (f))
9290 continue;
9291
9292 bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
9293 || accessible_p (TYPE_BINFO (type), f, true));
9294
9295 /* Skip template assignment operators and deleted functions. */
9296 if (TREE_CODE (f) != FUNCTION_DECL || DECL_DELETED_FN (f))
9297 continue;
9298
9299 if (accessible)
9300 *hasassign = true;
9301
9302 if (!accessible || !trivial_fn_p (f))
9303 all_trivial = false;
9304
9305 /* Break early when both properties have been determined. */
9306 if (*hasassign && !all_trivial)
9307 break;
9308 }
9309
9310 /* Return true if they're all trivial and one of the expressions
9311 TYPE() = TYPE() or TYPE() = (TYPE&)() is valid. */
9312 tree ref = cp_build_reference_type (type, false);
9313 return (all_trivial
9314 && (is_trivially_xible (MODIFY_EXPR, type, type)
9315 || is_trivially_xible (MODIFY_EXPR, type, ref)));
9316 }
9317
9318 /* Return true if all copy and move ctor overloads for class TYPE are
9319 trivial and at least one of them is not deleted and, when ACCESS is
9320 set, accessible. Return false otherwise. Set each element of HASCTOR[]
9321 to true when the TYPE has a (not necessarily trivial) default and copy
9322 (or move) ctor, respectively. */
9323
9324 static bool
9325 has_trivial_copy_p (tree type, bool access, bool hasctor[2])
9326 {
9327 tree fns = get_class_binding (type, complete_ctor_identifier);
9328 bool all_trivial = true;
9329
9330 for (ovl_iterator oi (fns); oi; ++oi)
9331 {
9332 tree f = *oi;
9333
9334 /* Skip template constructors. */
9335 if (TREE_CODE (f) != FUNCTION_DECL)
9336 continue;
9337
9338 bool cpy_or_move_ctor_p = copy_fn_p (f);
9339
9340 /* Skip ctors other than default, copy, and move. */
9341 if (!cpy_or_move_ctor_p && !default_ctor_p (f))
9342 continue;
9343
9344 if (DECL_DELETED_FN (f))
9345 continue;
9346
9347 bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
9348 || accessible_p (TYPE_BINFO (type), f, true));
9349
9350 if (accessible)
9351 hasctor[cpy_or_move_ctor_p] = true;
9352
9353 if (cpy_or_move_ctor_p && (!accessible || !trivial_fn_p (f)))
9354 all_trivial = false;
9355
9356 /* Break early when both properties have been determined. */
9357 if (hasctor[0] && hasctor[1] && !all_trivial)
9358 break;
9359 }
9360
9361 return all_trivial;
9362 }
9363
9364 /* Issue a warning on a call to the built-in function FNDECL if it is
9365 a raw memory write whose destination is not an object of (something
9366 like) trivial or standard layout type with a non-deleted assignment
9367 and copy ctor. Detects const correctness violations, corrupting
9368 references, virtual table pointers, and bypassing non-trivial
9369 assignments. */
9370
9371 static void
9372 maybe_warn_class_memaccess (location_t loc, tree fndecl,
9373 const vec<tree, va_gc> *args)
9374 {
9375 /* Except for bcopy where it's second, the destination pointer is
9376 the first argument for all functions handled here. Compute
9377 the index of the destination and source arguments. */
9378 unsigned dstidx = DECL_FUNCTION_CODE (fndecl) == BUILT_IN_BCOPY;
9379 unsigned srcidx = !dstidx;
9380
9381 tree dest = (*args)[dstidx];
9382 if (!TREE_TYPE (dest)
9383 || (TREE_CODE (TREE_TYPE (dest)) != ARRAY_TYPE
9384 && !INDIRECT_TYPE_P (TREE_TYPE (dest))))
9385 return;
9386
9387 tree srctype = NULL_TREE;
9388
9389 /* Determine the type of the pointed-to object and whether it's
9390 a complete class type. */
9391 tree desttype = TREE_TYPE (TREE_TYPE (dest));
9392
9393 if (!desttype || !COMPLETE_TYPE_P (desttype) || !CLASS_TYPE_P (desttype))
9394 return;
9395
9396 /* Check to see if the raw memory call is made by a non-static member
9397 function with THIS as the destination argument for the destination
9398 type. If so, and if the class has no non-trivial bases or members,
9399 be more permissive. */
9400 if (current_function_decl
9401 && DECL_NONSTATIC_MEMBER_FUNCTION_P (current_function_decl)
9402 && is_this_parameter (tree_strip_nop_conversions (dest)))
9403 {
9404 tree ctx = DECL_CONTEXT (current_function_decl);
9405 bool special = same_type_ignoring_top_level_qualifiers_p (ctx, desttype);
9406 tree binfo = TYPE_BINFO (ctx);
9407
9408 if (special
9409 && !BINFO_VTABLE (binfo)
9410 && !first_non_trivial_field (desttype))
9411 return;
9412 }
9413
9414 /* True if the class is trivial. */
9415 bool trivial = trivial_type_p (desttype);
9416
9417 /* Set to true if DESTYPE has an accessible copy assignment. */
9418 bool hasassign = false;
9419 /* True if all of the class' overloaded copy assignment operators
9420 are all trivial (and not deleted) and at least one of them is
9421 accessible. */
9422 bool trivassign = has_trivial_copy_assign_p (desttype, true, &hasassign);
9423
9424 /* Set to true if DESTTYPE has an accessible default and copy ctor,
9425 respectively. */
9426 bool hasctors[2] = { false, false };
9427
9428 /* True if all of the class' overloaded copy constructors are all
9429 trivial (and not deleted) and at least one of them is accessible. */
9430 bool trivcopy = has_trivial_copy_p (desttype, true, hasctors);
9431
9432 /* Set FLD to the first private/protected member of the class. */
9433 tree fld = trivial ? first_non_public_field (desttype) : NULL_TREE;
9434
9435 /* The warning format string. */
9436 const char *warnfmt = NULL;
9437 /* A suggested alternative to offer instead of the raw memory call.
9438 Empty string when none can be come up with. */
9439 const char *suggest = "";
9440 bool warned = false;
9441
9442 switch (DECL_FUNCTION_CODE (fndecl))
9443 {
9444 case BUILT_IN_MEMSET:
9445 if (!integer_zerop (maybe_constant_value ((*args)[1])))
9446 {
9447 /* Diagnose setting non-copy-assignable or non-trivial types,
9448 or types with a private member, to (potentially) non-zero
9449 bytes. Since the value of the bytes being written is unknown,
9450 suggest using assignment instead (if one exists). Also warn
9451 for writes into objects for which zero-initialization doesn't
9452 mean all bits clear (pointer-to-member data, where null is all
9453 bits set). Since the value being written is (most likely)
9454 non-zero, simply suggest assignment (but not copy assignment). */
9455 suggest = "; use assignment instead";
9456 if (!trivassign)
9457 warnfmt = G_("%qD writing to an object of type %#qT with "
9458 "no trivial copy-assignment");
9459 else if (!trivial)
9460 warnfmt = G_("%qD writing to an object of non-trivial type %#qT%s");
9461 else if (fld)
9462 {
9463 const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
9464 warned = warning_at (loc, OPT_Wclass_memaccess,
9465 "%qD writing to an object of type %#qT with "
9466 "%qs member %qD",
9467 fndecl, desttype, access, fld);
9468 }
9469 else if (!zero_init_p (desttype))
9470 warnfmt = G_("%qD writing to an object of type %#qT containing "
9471 "a pointer to data member%s");
9472
9473 break;
9474 }
9475 /* Fall through. */
9476
9477 case BUILT_IN_BZERO:
9478 /* Similarly to the above, diagnose clearing non-trivial or non-
9479 standard layout objects, or objects of types with no assignmenmt.
9480 Since the value being written is known to be zero, suggest either
9481 copy assignment, copy ctor, or default ctor as an alternative,
9482 depending on what's available. */
9483
9484 if (hasassign && hasctors[0])
9485 suggest = G_("; use assignment or value-initialization instead");
9486 else if (hasassign)
9487 suggest = G_("; use assignment instead");
9488 else if (hasctors[0])
9489 suggest = G_("; use value-initialization instead");
9490
9491 if (!trivassign)
9492 warnfmt = G_("%qD clearing an object of type %#qT with "
9493 "no trivial copy-assignment%s");
9494 else if (!trivial)
9495 warnfmt = G_("%qD clearing an object of non-trivial type %#qT%s");
9496 else if (!zero_init_p (desttype))
9497 warnfmt = G_("%qD clearing an object of type %#qT containing "
9498 "a pointer-to-member%s");
9499 break;
9500
9501 case BUILT_IN_BCOPY:
9502 case BUILT_IN_MEMCPY:
9503 case BUILT_IN_MEMMOVE:
9504 case BUILT_IN_MEMPCPY:
9505 /* Determine the type of the source object. */
9506 srctype = TREE_TYPE ((*args)[srcidx]);
9507 if (!srctype || !INDIRECT_TYPE_P (srctype))
9508 srctype = void_type_node;
9509 else
9510 srctype = TREE_TYPE (srctype);
9511
9512 /* Since it's impossible to determine wheter the byte copy is
9513 being used in place of assignment to an existing object or
9514 as a substitute for initialization, assume it's the former.
9515 Determine the best alternative to use instead depending on
9516 what's not deleted. */
9517 if (hasassign && hasctors[1])
9518 suggest = G_("; use copy-assignment or copy-initialization instead");
9519 else if (hasassign)
9520 suggest = G_("; use copy-assignment instead");
9521 else if (hasctors[1])
9522 suggest = G_("; use copy-initialization instead");
9523
9524 if (!trivassign)
9525 warnfmt = G_("%qD writing to an object of type %#qT with no trivial "
9526 "copy-assignment%s");
9527 else if (!trivially_copyable_p (desttype))
9528 warnfmt = G_("%qD writing to an object of non-trivially copyable "
9529 "type %#qT%s");
9530 else if (!trivcopy)
9531 warnfmt = G_("%qD writing to an object with a deleted copy constructor");
9532
9533 else if (!trivial
9534 && !VOID_TYPE_P (srctype)
9535 && !is_byte_access_type (srctype)
9536 && !same_type_ignoring_top_level_qualifiers_p (desttype,
9537 srctype))
9538 {
9539 /* Warn when copying into a non-trivial object from an object
9540 of a different type other than void or char. */
9541 warned = warning_at (loc, OPT_Wclass_memaccess,
9542 "%qD copying an object of non-trivial type "
9543 "%#qT from an array of %#qT",
9544 fndecl, desttype, srctype);
9545 }
9546 else if (fld
9547 && !VOID_TYPE_P (srctype)
9548 && !is_byte_access_type (srctype)
9549 && !same_type_ignoring_top_level_qualifiers_p (desttype,
9550 srctype))
9551 {
9552 const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
9553 warned = warning_at (loc, OPT_Wclass_memaccess,
9554 "%qD copying an object of type %#qT with "
9555 "%qs member %qD from an array of %#qT; use "
9556 "assignment or copy-initialization instead",
9557 fndecl, desttype, access, fld, srctype);
9558 }
9559 else if (!trivial && vec_safe_length (args) > 2)
9560 {
9561 tree sz = maybe_constant_value ((*args)[2]);
9562 if (!tree_fits_uhwi_p (sz))
9563 break;
9564
9565 /* Finally, warn on partial copies. */
9566 unsigned HOST_WIDE_INT typesize
9567 = tree_to_uhwi (TYPE_SIZE_UNIT (desttype));
9568 if (unsigned HOST_WIDE_INT partial = tree_to_uhwi (sz) % typesize)
9569 warned = warning_at (loc, OPT_Wclass_memaccess,
9570 (typesize - partial > 1
9571 ? G_("%qD writing to an object of "
9572 "a non-trivial type %#qT leaves %wu "
9573 "bytes unchanged")
9574 : G_("%qD writing to an object of "
9575 "a non-trivial type %#qT leaves %wu "
9576 "byte unchanged")),
9577 fndecl, desttype, typesize - partial);
9578 }
9579 break;
9580
9581 case BUILT_IN_REALLOC:
9582
9583 if (!trivially_copyable_p (desttype))
9584 warnfmt = G_("%qD moving an object of non-trivially copyable type "
9585 "%#qT; use %<new%> and %<delete%> instead");
9586 else if (!trivcopy)
9587 warnfmt = G_("%qD moving an object of type %#qT with deleted copy "
9588 "constructor; use %<new%> and %<delete%> instead");
9589 else if (!get_dtor (desttype, tf_none))
9590 warnfmt = G_("%qD moving an object of type %#qT with deleted "
9591 "destructor");
9592 else if (!trivial)
9593 {
9594 tree sz = maybe_constant_value ((*args)[1]);
9595 if (TREE_CODE (sz) == INTEGER_CST
9596 && tree_int_cst_lt (sz, TYPE_SIZE_UNIT (desttype)))
9597 /* Finally, warn on reallocation into insufficient space. */
9598 warned = warning_at (loc, OPT_Wclass_memaccess,
9599 "%qD moving an object of non-trivial type "
9600 "%#qT and size %E into a region of size %E",
9601 fndecl, desttype, TYPE_SIZE_UNIT (desttype),
9602 sz);
9603 }
9604 break;
9605
9606 default:
9607 return;
9608 }
9609
9610 if (warnfmt)
9611 {
9612 if (suggest)
9613 warned = warning_at (loc, OPT_Wclass_memaccess,
9614 warnfmt, fndecl, desttype, suggest);
9615 else
9616 warned = warning_at (loc, OPT_Wclass_memaccess,
9617 warnfmt, fndecl, desttype);
9618 }
9619
9620 if (warned)
9621 inform (location_of (desttype), "%#qT declared here", desttype);
9622 }
9623
9624 /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
9625 If FN is the result of resolving an overloaded target built-in,
9626 ORIG_FNDECL is the original function decl, otherwise it is null.
9627 This function performs no overload resolution, conversion, or other
9628 high-level operations. */
9629
9630 tree
9631 build_cxx_call (tree fn, int nargs, tree *argarray,
9632 tsubst_flags_t complain, tree orig_fndecl)
9633 {
9634 tree fndecl;
9635
9636 /* Remember roughly where this call is. */
9637 location_t loc = cp_expr_loc_or_input_loc (fn);
9638 fn = build_call_a (fn, nargs, argarray);
9639 SET_EXPR_LOCATION (fn, loc);
9640
9641 fndecl = get_callee_fndecl (fn);
9642 if (!orig_fndecl)
9643 orig_fndecl = fndecl;
9644
9645 /* Check that arguments to builtin functions match the expectations. */
9646 if (fndecl
9647 && !processing_template_decl
9648 && fndecl_built_in_p (fndecl))
9649 {
9650 int i;
9651
9652 /* We need to take care that values to BUILT_IN_NORMAL
9653 are reduced. */
9654 for (i = 0; i < nargs; i++)
9655 argarray[i] = maybe_constant_value (argarray[i]);
9656
9657 if (!check_builtin_function_arguments (EXPR_LOCATION (fn), vNULL, fndecl,
9658 orig_fndecl, nargs, argarray))
9659 return error_mark_node;
9660 }
9661
9662 if (VOID_TYPE_P (TREE_TYPE (fn)))
9663 return fn;
9664
9665 /* 5.2.2/11: If a function call is a prvalue of object type: if the
9666 function call is either the operand of a decltype-specifier or the
9667 right operand of a comma operator that is the operand of a
9668 decltype-specifier, a temporary object is not introduced for the
9669 prvalue. The type of the prvalue may be incomplete. */
9670 if (!(complain & tf_decltype))
9671 {
9672 fn = require_complete_type_sfinae (fn, complain);
9673 if (fn == error_mark_node)
9674 return error_mark_node;
9675
9676 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
9677 {
9678 fn = build_cplus_new (TREE_TYPE (fn), fn, complain);
9679 maybe_warn_parm_abi (TREE_TYPE (fn), loc);
9680 }
9681 }
9682 return convert_from_reference (fn);
9683 }
9684
9685 /* Returns the value to use for the in-charge parameter when making a
9686 call to a function with the indicated NAME.
9687
9688 FIXME:Can't we find a neater way to do this mapping? */
9689
9690 tree
9691 in_charge_arg_for_name (tree name)
9692 {
9693 if (IDENTIFIER_CTOR_P (name))
9694 {
9695 if (name == complete_ctor_identifier)
9696 return integer_one_node;
9697 gcc_checking_assert (name == base_ctor_identifier);
9698 }
9699 else
9700 {
9701 if (name == complete_dtor_identifier)
9702 return integer_two_node;
9703 else if (name == deleting_dtor_identifier)
9704 return integer_three_node;
9705 gcc_checking_assert (name == base_dtor_identifier);
9706 }
9707
9708 return integer_zero_node;
9709 }
9710
9711 /* We've built up a constructor call RET. Complain if it delegates to the
9712 constructor we're currently compiling. */
9713
9714 static void
9715 check_self_delegation (tree ret)
9716 {
9717 if (TREE_CODE (ret) == TARGET_EXPR)
9718 ret = TARGET_EXPR_INITIAL (ret);
9719 tree fn = cp_get_callee_fndecl_nofold (ret);
9720 if (fn && DECL_ABSTRACT_ORIGIN (fn) == current_function_decl)
9721 error ("constructor delegates to itself");
9722 }
9723
9724 /* Build a call to a constructor, destructor, or an assignment
9725 operator for INSTANCE, an expression with class type. NAME
9726 indicates the special member function to call; *ARGS are the
9727 arguments. ARGS may be NULL. This may change ARGS. BINFO
9728 indicates the base of INSTANCE that is to be passed as the `this'
9729 parameter to the member function called.
9730
9731 FLAGS are the LOOKUP_* flags to use when processing the call.
9732
9733 If NAME indicates a complete object constructor, INSTANCE may be
9734 NULL_TREE. In this case, the caller will call build_cplus_new to
9735 store the newly constructed object into a VAR_DECL. */
9736
9737 tree
9738 build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
9739 tree binfo, int flags, tsubst_flags_t complain)
9740 {
9741 tree fns;
9742 /* The type of the subobject to be constructed or destroyed. */
9743 tree class_type;
9744 vec<tree, va_gc> *allocated = NULL;
9745 tree ret;
9746
9747 gcc_assert (IDENTIFIER_CDTOR_P (name) || name == assign_op_identifier);
9748
9749 if (error_operand_p (instance))
9750 return error_mark_node;
9751
9752 if (IDENTIFIER_DTOR_P (name))
9753 {
9754 gcc_assert (args == NULL || vec_safe_is_empty (*args));
9755 if (!type_build_dtor_call (TREE_TYPE (instance)))
9756 /* Shortcut to avoid lazy destructor declaration. */
9757 return build_trivial_dtor_call (instance);
9758 }
9759
9760 if (TYPE_P (binfo))
9761 {
9762 /* Resolve the name. */
9763 if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
9764 return error_mark_node;
9765
9766 binfo = TYPE_BINFO (binfo);
9767 }
9768
9769 gcc_assert (binfo != NULL_TREE);
9770
9771 class_type = BINFO_TYPE (binfo);
9772
9773 /* Handle the special case where INSTANCE is NULL_TREE. */
9774 if (name == complete_ctor_identifier && !instance)
9775 instance = build_dummy_object (class_type);
9776 else
9777 {
9778 /* Convert to the base class, if necessary. */
9779 if (!same_type_ignoring_top_level_qualifiers_p
9780 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
9781 {
9782 if (IDENTIFIER_CDTOR_P (name))
9783 /* For constructors and destructors, either the base is
9784 non-virtual, or it is virtual but we are doing the
9785 conversion from a constructor or destructor for the
9786 complete object. In either case, we can convert
9787 statically. */
9788 instance = convert_to_base_statically (instance, binfo);
9789 else
9790 {
9791 /* However, for assignment operators, we must convert
9792 dynamically if the base is virtual. */
9793 gcc_checking_assert (name == assign_op_identifier);
9794 instance = build_base_path (PLUS_EXPR, instance,
9795 binfo, /*nonnull=*/1, complain);
9796 }
9797 }
9798 }
9799
9800 gcc_assert (instance != NULL_TREE);
9801
9802 /* In C++17, "If the initializer expression is a prvalue and the
9803 cv-unqualified version of the source type is the same class as the class
9804 of the destination, the initializer expression is used to initialize the
9805 destination object." Handle that here to avoid doing overload
9806 resolution. */
9807 if (cxx_dialect >= cxx17
9808 && args && vec_safe_length (*args) == 1
9809 && name == complete_ctor_identifier)
9810 {
9811 tree arg = (**args)[0];
9812
9813 if (BRACE_ENCLOSED_INITIALIZER_P (arg)
9814 && !TYPE_HAS_LIST_CTOR (class_type)
9815 && CONSTRUCTOR_NELTS (arg) == 1)
9816 arg = CONSTRUCTOR_ELT (arg, 0)->value;
9817
9818 if ((TREE_CODE (arg) == TARGET_EXPR
9819 || TREE_CODE (arg) == CONSTRUCTOR)
9820 && (same_type_ignoring_top_level_qualifiers_p
9821 (class_type, TREE_TYPE (arg))))
9822 {
9823 if (is_dummy_object (instance))
9824 return arg;
9825 else if (TREE_CODE (arg) == TARGET_EXPR)
9826 TARGET_EXPR_DIRECT_INIT_P (arg) = true;
9827
9828 if ((complain & tf_error)
9829 && (flags & LOOKUP_DELEGATING_CONS))
9830 check_self_delegation (arg);
9831 /* Avoid change of behavior on Wunused-var-2.C. */
9832 instance = mark_lvalue_use (instance);
9833 return build2 (INIT_EXPR, class_type, instance, arg);
9834 }
9835 }
9836
9837 fns = lookup_fnfields (binfo, name, 1, complain);
9838
9839 /* When making a call to a constructor or destructor for a subobject
9840 that uses virtual base classes, pass down a pointer to a VTT for
9841 the subobject. */
9842 if ((name == base_ctor_identifier
9843 || name == base_dtor_identifier)
9844 && CLASSTYPE_VBASECLASSES (class_type))
9845 {
9846 tree vtt;
9847 tree sub_vtt;
9848
9849 /* If the current function is a complete object constructor
9850 or destructor, then we fetch the VTT directly.
9851 Otherwise, we look it up using the VTT we were given. */
9852 vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
9853 vtt = decay_conversion (vtt, complain);
9854 if (vtt == error_mark_node)
9855 return error_mark_node;
9856 vtt = build_if_in_charge (vtt, current_vtt_parm);
9857 if (BINFO_SUBVTT_INDEX (binfo))
9858 sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
9859 else
9860 sub_vtt = vtt;
9861
9862 if (args == NULL)
9863 {
9864 allocated = make_tree_vector ();
9865 args = &allocated;
9866 }
9867
9868 vec_safe_insert (*args, 0, sub_vtt);
9869 }
9870
9871 ret = build_new_method_call (instance, fns, args,
9872 TYPE_BINFO (BINFO_TYPE (binfo)),
9873 flags, /*fn=*/NULL,
9874 complain);
9875
9876 if (allocated != NULL)
9877 release_tree_vector (allocated);
9878
9879 if ((complain & tf_error)
9880 && (flags & LOOKUP_DELEGATING_CONS)
9881 && name == complete_ctor_identifier)
9882 check_self_delegation (ret);
9883
9884 return ret;
9885 }
9886
9887 /* Return the NAME, as a C string. The NAME indicates a function that
9888 is a member of TYPE. *FREE_P is set to true if the caller must
9889 free the memory returned.
9890
9891 Rather than go through all of this, we should simply set the names
9892 of constructors and destructors appropriately, and dispense with
9893 ctor_identifier, dtor_identifier, etc. */
9894
9895 static char *
9896 name_as_c_string (tree name, tree type, bool *free_p)
9897 {
9898 const char *pretty_name;
9899
9900 /* Assume that we will not allocate memory. */
9901 *free_p = false;
9902 /* Constructors and destructors are special. */
9903 if (IDENTIFIER_CDTOR_P (name))
9904 {
9905 pretty_name
9906 = identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type)));
9907 /* For a destructor, add the '~'. */
9908 if (IDENTIFIER_DTOR_P (name))
9909 {
9910 pretty_name = concat ("~", pretty_name, NULL);
9911 /* Remember that we need to free the memory allocated. */
9912 *free_p = true;
9913 }
9914 }
9915 else if (IDENTIFIER_CONV_OP_P (name))
9916 {
9917 pretty_name = concat ("operator ",
9918 type_as_string_translate (TREE_TYPE (name),
9919 TFF_PLAIN_IDENTIFIER),
9920 NULL);
9921 /* Remember that we need to free the memory allocated. */
9922 *free_p = true;
9923 }
9924 else
9925 pretty_name = identifier_to_locale (IDENTIFIER_POINTER (name));
9926
9927 return CONST_CAST (char *, pretty_name);
9928 }
9929
9930 /* If CANDIDATES contains exactly one candidate, return it, otherwise
9931 return NULL. */
9932
9933 static z_candidate *
9934 single_z_candidate (z_candidate *candidates)
9935 {
9936 if (candidates == NULL)
9937 return NULL;
9938
9939 if (candidates->next)
9940 return NULL;
9941
9942 return candidates;
9943 }
9944
9945 /* If CANDIDATE is invalid due to a bad argument type, return the
9946 pertinent conversion_info.
9947
9948 Otherwise, return NULL. */
9949
9950 static const conversion_info *
9951 maybe_get_bad_conversion_for_unmatched_call (const z_candidate *candidate)
9952 {
9953 /* Must be an rr_arg_conversion or rr_bad_arg_conversion. */
9954 rejection_reason *r = candidate->reason;
9955
9956 if (r == NULL)
9957 return NULL;
9958
9959 switch (r->code)
9960 {
9961 default:
9962 return NULL;
9963
9964 case rr_arg_conversion:
9965 return &r->u.conversion;
9966
9967 case rr_bad_arg_conversion:
9968 return &r->u.bad_conversion;
9969 }
9970 }
9971
9972 /* Issue an error and note complaining about a bad argument type at a
9973 callsite with a single candidate FNDECL.
9974
9975 ARG_LOC is the location of the argument (or UNKNOWN_LOCATION, in which
9976 case input_location is used).
9977 FROM_TYPE is the type of the actual argument; TO_TYPE is the type of
9978 the formal parameter. */
9979
9980 void
9981 complain_about_bad_argument (location_t arg_loc,
9982 tree from_type, tree to_type,
9983 tree fndecl, int parmnum)
9984 {
9985 auto_diagnostic_group d;
9986 range_label_for_type_mismatch rhs_label (from_type, to_type);
9987 range_label *label = &rhs_label;
9988 if (arg_loc == UNKNOWN_LOCATION)
9989 {
9990 arg_loc = input_location;
9991 label = NULL;
9992 }
9993 gcc_rich_location richloc (arg_loc, label);
9994 error_at (&richloc,
9995 "cannot convert %qH to %qI",
9996 from_type, to_type);
9997 maybe_inform_about_fndecl_for_bogus_argument_init (fndecl,
9998 parmnum);
9999 }
10000
10001 /* Subroutine of build_new_method_call_1, for where there are no viable
10002 candidates for the call. */
10003
10004 static void
10005 complain_about_no_candidates_for_method_call (tree instance,
10006 z_candidate *candidates,
10007 tree explicit_targs,
10008 tree basetype,
10009 tree optype, tree name,
10010 bool skip_first_for_error,
10011 vec<tree, va_gc> *user_args)
10012 {
10013 auto_diagnostic_group d;
10014 if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
10015 cxx_incomplete_type_error (instance, basetype);
10016 else if (optype)
10017 error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
10018 basetype, optype, build_tree_list_vec (user_args),
10019 TREE_TYPE (instance));
10020 else
10021 {
10022 /* Special-case for when there's a single candidate that's failing
10023 due to a bad argument type. */
10024 if (z_candidate *candidate = single_z_candidate (candidates))
10025 if (const conversion_info *conv
10026 = maybe_get_bad_conversion_for_unmatched_call (candidate))
10027 {
10028 tree from_type = conv->from;
10029 if (!TYPE_P (conv->from))
10030 from_type = lvalue_type (conv->from);
10031 complain_about_bad_argument (conv->loc,
10032 from_type, conv->to_type,
10033 candidate->fn, conv->n_arg);
10034 return;
10035 }
10036
10037 tree arglist = build_tree_list_vec (user_args);
10038 tree errname = name;
10039 bool twiddle = false;
10040 if (IDENTIFIER_CDTOR_P (errname))
10041 {
10042 twiddle = IDENTIFIER_DTOR_P (errname);
10043 errname = constructor_name (basetype);
10044 }
10045 if (explicit_targs)
10046 errname = lookup_template_function (errname, explicit_targs);
10047 if (skip_first_for_error)
10048 arglist = TREE_CHAIN (arglist);
10049 error ("no matching function for call to %<%T::%s%E(%A)%#V%>",
10050 basetype, &"~"[!twiddle], errname, arglist,
10051 TREE_TYPE (instance));
10052 }
10053 print_z_candidates (location_of (name), candidates);
10054 }
10055
10056 /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
10057 be set, upon return, to the function called. ARGS may be NULL.
10058 This may change ARGS. */
10059
10060 static tree
10061 build_new_method_call_1 (tree instance, tree fns, vec<tree, va_gc> **args,
10062 tree conversion_path, int flags,
10063 tree *fn_p, tsubst_flags_t complain)
10064 {
10065 struct z_candidate *candidates = 0, *cand;
10066 tree explicit_targs = NULL_TREE;
10067 tree basetype = NULL_TREE;
10068 tree access_binfo;
10069 tree optype;
10070 tree first_mem_arg = NULL_TREE;
10071 tree name;
10072 bool skip_first_for_error;
10073 vec<tree, va_gc> *user_args;
10074 tree call;
10075 tree fn;
10076 int template_only = 0;
10077 bool any_viable_p;
10078 tree orig_instance;
10079 tree orig_fns;
10080 vec<tree, va_gc> *orig_args = NULL;
10081 void *p;
10082
10083 gcc_assert (instance != NULL_TREE);
10084
10085 /* We don't know what function we're going to call, yet. */
10086 if (fn_p)
10087 *fn_p = NULL_TREE;
10088
10089 if (error_operand_p (instance)
10090 || !fns || error_operand_p (fns))
10091 return error_mark_node;
10092
10093 if (!BASELINK_P (fns))
10094 {
10095 if (complain & tf_error)
10096 error ("call to non-function %qD", fns);
10097 return error_mark_node;
10098 }
10099
10100 orig_instance = instance;
10101 orig_fns = fns;
10102
10103 /* Dismantle the baselink to collect all the information we need. */
10104 if (!conversion_path)
10105 conversion_path = BASELINK_BINFO (fns);
10106 access_binfo = BASELINK_ACCESS_BINFO (fns);
10107 optype = BASELINK_OPTYPE (fns);
10108 fns = BASELINK_FUNCTIONS (fns);
10109 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
10110 {
10111 explicit_targs = TREE_OPERAND (fns, 1);
10112 fns = TREE_OPERAND (fns, 0);
10113 template_only = 1;
10114 }
10115 gcc_assert (OVL_P (fns));
10116 fn = OVL_FIRST (fns);
10117 name = DECL_NAME (fn);
10118
10119 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
10120 gcc_assert (CLASS_TYPE_P (basetype));
10121
10122 user_args = args == NULL ? NULL : *args;
10123 /* Under DR 147 A::A() is an invalid constructor call,
10124 not a functional cast. */
10125 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
10126 {
10127 if (! (complain & tf_error))
10128 return error_mark_node;
10129
10130 basetype = DECL_CONTEXT (fn);
10131 name = constructor_name (basetype);
10132 auto_diagnostic_group d;
10133 if (permerror (input_location,
10134 "cannot call constructor %<%T::%D%> directly",
10135 basetype, name))
10136 inform (input_location, "for a function-style cast, remove the "
10137 "redundant %<::%D%>", name);
10138 call = build_functional_cast (input_location, basetype,
10139 build_tree_list_vec (user_args),
10140 complain);
10141 return call;
10142 }
10143
10144 if (processing_template_decl)
10145 {
10146 orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
10147 instance = build_non_dependent_expr (instance);
10148 if (args != NULL)
10149 make_args_non_dependent (*args);
10150 }
10151
10152 /* Process the argument list. */
10153 if (args != NULL && *args != NULL)
10154 {
10155 *args = resolve_args (*args, complain);
10156 if (*args == NULL)
10157 return error_mark_node;
10158 user_args = *args;
10159 }
10160
10161 /* Consider the object argument to be used even if we end up selecting a
10162 static member function. */
10163 instance = mark_type_use (instance);
10164
10165 /* Figure out whether to skip the first argument for the error
10166 message we will display to users if an error occurs. We don't
10167 want to display any compiler-generated arguments. The "this"
10168 pointer hasn't been added yet. However, we must remove the VTT
10169 pointer if this is a call to a base-class constructor or
10170 destructor. */
10171 skip_first_for_error = false;
10172 if (IDENTIFIER_CDTOR_P (name))
10173 {
10174 /* Callers should explicitly indicate whether they want to ctor
10175 the complete object or just the part without virtual bases. */
10176 gcc_assert (name != ctor_identifier);
10177
10178 /* Remove the VTT pointer, if present. */
10179 if ((name == base_ctor_identifier || name == base_dtor_identifier)
10180 && CLASSTYPE_VBASECLASSES (basetype))
10181 skip_first_for_error = true;
10182
10183 /* It's OK to call destructors and constructors on cv-qualified
10184 objects. Therefore, convert the INSTANCE to the unqualified
10185 type, if necessary. */
10186 if (!same_type_p (basetype, TREE_TYPE (instance)))
10187 {
10188 instance = build_this (instance);
10189 instance = build_nop (build_pointer_type (basetype), instance);
10190 instance = build_fold_indirect_ref (instance);
10191 }
10192 }
10193 else
10194 gcc_assert (!DECL_DESTRUCTOR_P (fn) && !DECL_CONSTRUCTOR_P (fn));
10195
10196 /* For the overload resolution we need to find the actual `this`
10197 that would be captured if the call turns out to be to a
10198 non-static member function. Do not actually capture it at this
10199 point. */
10200 if (DECL_CONSTRUCTOR_P (fn))
10201 /* Constructors don't use the enclosing 'this'. */
10202 first_mem_arg = instance;
10203 else
10204 first_mem_arg = maybe_resolve_dummy (instance, false);
10205
10206 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10207 p = conversion_obstack_alloc (0);
10208
10209 /* The number of arguments artificial parms in ARGS; we subtract one because
10210 there's no 'this' in ARGS. */
10211 unsigned skip = num_artificial_parms_for (fn) - 1;
10212
10213 /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
10214 initializer, not T({ }). */
10215 if (DECL_CONSTRUCTOR_P (fn)
10216 && vec_safe_length (user_args) > skip
10217 && DIRECT_LIST_INIT_P ((*user_args)[skip]))
10218 {
10219 tree init_list = (*user_args)[skip];
10220 tree init = NULL_TREE;
10221
10222 gcc_assert (user_args->length () == skip + 1
10223 && !(flags & LOOKUP_ONLYCONVERTING));
10224
10225 /* If the initializer list has no elements and T is a class type with
10226 a default constructor, the object is value-initialized. Handle
10227 this here so we don't need to handle it wherever we use
10228 build_special_member_call. */
10229 if (CONSTRUCTOR_NELTS (init_list) == 0
10230 && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
10231 /* For a user-provided default constructor, use the normal
10232 mechanisms so that protected access works. */
10233 && type_has_non_user_provided_default_constructor (basetype)
10234 && !processing_template_decl)
10235 init = build_value_init (basetype, complain);
10236
10237 /* If BASETYPE is an aggregate, we need to do aggregate
10238 initialization. */
10239 else if (CP_AGGREGATE_TYPE_P (basetype))
10240 {
10241 init = reshape_init (basetype, init_list, complain);
10242 init = digest_init (basetype, init, complain);
10243 }
10244
10245 if (init)
10246 {
10247 if (is_dummy_object (instance))
10248 return get_target_expr_sfinae (init, complain);
10249 init = build2 (INIT_EXPR, TREE_TYPE (instance), instance, init);
10250 TREE_SIDE_EFFECTS (init) = true;
10251 return init;
10252 }
10253
10254 /* Otherwise go ahead with overload resolution. */
10255 add_list_candidates (fns, first_mem_arg, user_args,
10256 basetype, explicit_targs, template_only,
10257 conversion_path, access_binfo, flags,
10258 &candidates, complain);
10259 }
10260 else
10261 add_candidates (fns, first_mem_arg, user_args, optype,
10262 explicit_targs, template_only, conversion_path,
10263 access_binfo, flags, &candidates, complain);
10264
10265 any_viable_p = false;
10266 candidates = splice_viable (candidates, false, &any_viable_p);
10267
10268 if (!any_viable_p)
10269 {
10270 /* [dcl.init], 17.6.2.2:
10271
10272 Otherwise, if no constructor is viable, the destination type is
10273 a (possibly cv-qualified) aggregate class A, and the initializer
10274 is a parenthesized expression-list, the object is initialized as
10275 follows...
10276
10277 We achieve this by building up a CONSTRUCTOR, as for list-init,
10278 and setting CONSTRUCTOR_IS_PAREN_INIT to distinguish between
10279 the two. */
10280 if (DECL_CONSTRUCTOR_P (fn)
10281 && !(flags & LOOKUP_ONLYCONVERTING)
10282 && cxx_dialect >= cxx20
10283 && CP_AGGREGATE_TYPE_P (basetype)
10284 && !vec_safe_is_empty (user_args))
10285 {
10286 /* Create a CONSTRUCTOR from ARGS, e.g. {1, 2} from <1, 2>. */
10287 tree list = build_tree_list_vec (user_args);
10288 tree ctor = build_constructor_from_list (init_list_type_node, list);
10289 CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true;
10290 CONSTRUCTOR_IS_PAREN_INIT (ctor) = true;
10291 if (is_dummy_object (instance))
10292 return ctor;
10293 else
10294 {
10295 ctor = digest_init (basetype, ctor, complain);
10296 if (ctor == error_mark_node)
10297 return error_mark_node;
10298 ctor = build2 (INIT_EXPR, TREE_TYPE (instance), instance, ctor);
10299 TREE_SIDE_EFFECTS (ctor) = true;
10300 return ctor;
10301 }
10302 }
10303 if (complain & tf_error)
10304 complain_about_no_candidates_for_method_call (instance, candidates,
10305 explicit_targs, basetype,
10306 optype, name,
10307 skip_first_for_error,
10308 user_args);
10309 call = error_mark_node;
10310 }
10311 else
10312 {
10313 cand = tourney (candidates, complain);
10314 if (cand == 0)
10315 {
10316 char *pretty_name;
10317 bool free_p;
10318 tree arglist;
10319
10320 if (complain & tf_error)
10321 {
10322 pretty_name = name_as_c_string (name, basetype, &free_p);
10323 arglist = build_tree_list_vec (user_args);
10324 if (skip_first_for_error)
10325 arglist = TREE_CHAIN (arglist);
10326 auto_diagnostic_group d;
10327 if (!any_strictly_viable (candidates))
10328 error ("no matching function for call to %<%s(%A)%>",
10329 pretty_name, arglist);
10330 else
10331 error ("call of overloaded %<%s(%A)%> is ambiguous",
10332 pretty_name, arglist);
10333 print_z_candidates (location_of (name), candidates);
10334 if (free_p)
10335 free (pretty_name);
10336 }
10337 call = error_mark_node;
10338 }
10339 else
10340 {
10341 fn = cand->fn;
10342 call = NULL_TREE;
10343
10344 if (!(flags & LOOKUP_NONVIRTUAL)
10345 && DECL_PURE_VIRTUAL_P (fn)
10346 && instance == current_class_ref
10347 && (complain & tf_warning))
10348 {
10349 /* This is not an error, it is runtime undefined
10350 behavior. */
10351 if (!current_function_decl)
10352 warning (0, "pure virtual %q#D called from "
10353 "non-static data member initializer", fn);
10354 else if (DECL_CONSTRUCTOR_P (current_function_decl)
10355 || DECL_DESTRUCTOR_P (current_function_decl))
10356 warning (0, (DECL_CONSTRUCTOR_P (current_function_decl)
10357 ? G_("pure virtual %q#D called from constructor")
10358 : G_("pure virtual %q#D called from destructor")),
10359 fn);
10360 }
10361
10362 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
10363 && !DECL_CONSTRUCTOR_P (fn)
10364 && is_dummy_object (instance))
10365 {
10366 instance = maybe_resolve_dummy (instance, true);
10367 if (instance == error_mark_node)
10368 call = error_mark_node;
10369 else if (!is_dummy_object (instance))
10370 {
10371 /* We captured 'this' in the current lambda now that
10372 we know we really need it. */
10373 cand->first_arg = instance;
10374 }
10375 else if (any_dependent_bases_p ())
10376 /* We can't tell until instantiation time whether we can use
10377 *this as the implicit object argument. */;
10378 else
10379 {
10380 if (complain & tf_error)
10381 error ("cannot call member function %qD without object",
10382 fn);
10383 call = error_mark_node;
10384 }
10385 }
10386
10387 if (call != error_mark_node)
10388 {
10389 /* Now we know what function is being called. */
10390 if (fn_p)
10391 *fn_p = fn;
10392 /* Build the actual CALL_EXPR. */
10393 call = build_over_call (cand, flags, complain);
10394 /* In an expression of the form `a->f()' where `f' turns
10395 out to be a static member function, `a' is
10396 none-the-less evaluated. */
10397 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
10398 && !is_dummy_object (instance)
10399 && TREE_SIDE_EFFECTS (instance))
10400 {
10401 /* But avoid the implicit lvalue-rvalue conversion when 'a'
10402 is volatile. */
10403 tree a = instance;
10404 if (TREE_THIS_VOLATILE (a))
10405 a = build_this (a);
10406 call = build2 (COMPOUND_EXPR, TREE_TYPE (call), a, call);
10407 }
10408 else if (call != error_mark_node
10409 && DECL_DESTRUCTOR_P (cand->fn)
10410 && !VOID_TYPE_P (TREE_TYPE (call)))
10411 /* An explicit call of the form "x->~X()" has type
10412 "void". However, on platforms where destructors
10413 return "this" (i.e., those where
10414 targetm.cxx.cdtor_returns_this is true), such calls
10415 will appear to have a return value of pointer type
10416 to the low-level call machinery. We do not want to
10417 change the low-level machinery, since we want to be
10418 able to optimize "delete f()" on such platforms as
10419 "operator delete(~X(f()))" (rather than generating
10420 "t = f(), ~X(t), operator delete (t)"). */
10421 call = build_nop (void_type_node, call);
10422 }
10423 }
10424 }
10425
10426 if (processing_template_decl && call != error_mark_node)
10427 {
10428 bool cast_to_void = false;
10429
10430 if (TREE_CODE (call) == COMPOUND_EXPR)
10431 call = TREE_OPERAND (call, 1);
10432 else if (TREE_CODE (call) == NOP_EXPR)
10433 {
10434 cast_to_void = true;
10435 call = TREE_OPERAND (call, 0);
10436 }
10437 if (INDIRECT_REF_P (call))
10438 call = TREE_OPERAND (call, 0);
10439 call = (build_min_non_dep_call_vec
10440 (call,
10441 build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
10442 orig_instance, orig_fns, NULL_TREE),
10443 orig_args));
10444 SET_EXPR_LOCATION (call, input_location);
10445 call = convert_from_reference (call);
10446 if (cast_to_void)
10447 call = build_nop (void_type_node, call);
10448 }
10449
10450 /* Free all the conversions we allocated. */
10451 obstack_free (&conversion_obstack, p);
10452
10453 if (orig_args != NULL)
10454 release_tree_vector (orig_args);
10455
10456 return call;
10457 }
10458
10459 /* Wrapper for above. */
10460
10461 tree
10462 build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args,
10463 tree conversion_path, int flags,
10464 tree *fn_p, tsubst_flags_t complain)
10465 {
10466 tree ret;
10467 bool subtime = timevar_cond_start (TV_OVERLOAD);
10468 ret = build_new_method_call_1 (instance, fns, args, conversion_path, flags,
10469 fn_p, complain);
10470 timevar_cond_stop (TV_OVERLOAD, subtime);
10471 return ret;
10472 }
10473
10474 /* Returns true iff standard conversion sequence ICS1 is a proper
10475 subsequence of ICS2. */
10476
10477 static bool
10478 is_subseq (conversion *ics1, conversion *ics2)
10479 {
10480 /* We can assume that a conversion of the same code
10481 between the same types indicates a subsequence since we only get
10482 here if the types we are converting from are the same. */
10483
10484 while (ics1->kind == ck_rvalue
10485 || ics1->kind == ck_lvalue)
10486 ics1 = next_conversion (ics1);
10487
10488 while (1)
10489 {
10490 while (ics2->kind == ck_rvalue
10491 || ics2->kind == ck_lvalue)
10492 ics2 = next_conversion (ics2);
10493
10494 if (ics2->kind == ck_user
10495 || ics2->kind == ck_ambig
10496 || ics2->kind == ck_aggr
10497 || ics2->kind == ck_list
10498 || ics2->kind == ck_identity)
10499 /* At this point, ICS1 cannot be a proper subsequence of
10500 ICS2. We can get a USER_CONV when we are comparing the
10501 second standard conversion sequence of two user conversion
10502 sequences. */
10503 return false;
10504
10505 ics2 = next_conversion (ics2);
10506
10507 while (ics2->kind == ck_rvalue
10508 || ics2->kind == ck_lvalue)
10509 ics2 = next_conversion (ics2);
10510
10511 if (ics2->kind == ics1->kind
10512 && same_type_p (ics2->type, ics1->type)
10513 && (ics1->kind == ck_identity
10514 || same_type_p (next_conversion (ics2)->type,
10515 next_conversion (ics1)->type)))
10516 return true;
10517 }
10518 }
10519
10520 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
10521 be any _TYPE nodes. */
10522
10523 bool
10524 is_properly_derived_from (tree derived, tree base)
10525 {
10526 if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
10527 return false;
10528
10529 /* We only allow proper derivation here. The DERIVED_FROM_P macro
10530 considers every class derived from itself. */
10531 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
10532 && DERIVED_FROM_P (base, derived));
10533 }
10534
10535 /* We build the ICS for an implicit object parameter as a pointer
10536 conversion sequence. However, such a sequence should be compared
10537 as if it were a reference conversion sequence. If ICS is the
10538 implicit conversion sequence for an implicit object parameter,
10539 modify it accordingly. */
10540
10541 static void
10542 maybe_handle_implicit_object (conversion **ics)
10543 {
10544 if ((*ics)->this_p)
10545 {
10546 /* [over.match.funcs]
10547
10548 For non-static member functions, the type of the
10549 implicit object parameter is "reference to cv X"
10550 where X is the class of which the function is a
10551 member and cv is the cv-qualification on the member
10552 function declaration. */
10553 conversion *t = *ics;
10554 tree reference_type;
10555
10556 /* The `this' parameter is a pointer to a class type. Make the
10557 implicit conversion talk about a reference to that same class
10558 type. */
10559 reference_type = TREE_TYPE (t->type);
10560 reference_type = build_reference_type (reference_type);
10561
10562 if (t->kind == ck_qual)
10563 t = next_conversion (t);
10564 if (t->kind == ck_ptr)
10565 t = next_conversion (t);
10566 t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
10567 t = direct_reference_binding (reference_type, t);
10568 t->this_p = 1;
10569 t->rvaluedness_matches_p = 0;
10570 *ics = t;
10571 }
10572 }
10573
10574 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
10575 and return the initial reference binding conversion. Otherwise,
10576 leave *ICS unchanged and return NULL. */
10577
10578 static conversion *
10579 maybe_handle_ref_bind (conversion **ics)
10580 {
10581 if ((*ics)->kind == ck_ref_bind)
10582 {
10583 conversion *old_ics = *ics;
10584 *ics = next_conversion (old_ics);
10585 (*ics)->user_conv_p = old_ics->user_conv_p;
10586 return old_ics;
10587 }
10588
10589 return NULL;
10590 }
10591
10592 /* Get the expression at the beginning of the conversion chain C. */
10593
10594 static tree
10595 conv_get_original_expr (conversion *c)
10596 {
10597 for (; c; c = next_conversion (c))
10598 if (c->kind == ck_identity || c->kind == ck_ambig || c->kind == ck_aggr)
10599 return c->u.expr;
10600 return NULL_TREE;
10601 }
10602
10603 /* Return a tree representing the number of elements initialized by the
10604 list-initialization C. The caller must check that C converts to an
10605 array type. */
10606
10607 static tree
10608 nelts_initialized_by_list_init (conversion *c)
10609 {
10610 /* If the array we're converting to has a dimension, we'll use that. */
10611 if (TYPE_DOMAIN (c->type))
10612 return array_type_nelts_top (c->type);
10613 else
10614 {
10615 /* Otherwise, we look at how many elements the constructor we're
10616 initializing from has. */
10617 tree ctor = conv_get_original_expr (c);
10618 return size_int (CONSTRUCTOR_NELTS (ctor));
10619 }
10620 }
10621
10622 /* True iff C is a conversion that binds a reference or a pointer to
10623 an array of unknown bound. */
10624
10625 static inline bool
10626 conv_binds_to_array_of_unknown_bound (conversion *c)
10627 {
10628 /* ck_ref_bind won't have the reference stripped. */
10629 tree type = non_reference (c->type);
10630 /* ck_qual won't have the pointer stripped. */
10631 type = strip_pointer_operator (type);
10632 return (TREE_CODE (type) == ARRAY_TYPE
10633 && TYPE_DOMAIN (type) == NULL_TREE);
10634 }
10635
10636 /* Compare two implicit conversion sequences according to the rules set out in
10637 [over.ics.rank]. Return values:
10638
10639 1: ics1 is better than ics2
10640 -1: ics2 is better than ics1
10641 0: ics1 and ics2 are indistinguishable */
10642
10643 static int
10644 compare_ics (conversion *ics1, conversion *ics2)
10645 {
10646 tree from_type1;
10647 tree from_type2;
10648 tree to_type1;
10649 tree to_type2;
10650 tree deref_from_type1 = NULL_TREE;
10651 tree deref_from_type2 = NULL_TREE;
10652 tree deref_to_type1 = NULL_TREE;
10653 tree deref_to_type2 = NULL_TREE;
10654 conversion_rank rank1, rank2;
10655
10656 /* REF_BINDING is nonzero if the result of the conversion sequence
10657 is a reference type. In that case REF_CONV is the reference
10658 binding conversion. */
10659 conversion *ref_conv1;
10660 conversion *ref_conv2;
10661
10662 /* Compare badness before stripping the reference conversion. */
10663 if (ics1->bad_p > ics2->bad_p)
10664 return -1;
10665 else if (ics1->bad_p < ics2->bad_p)
10666 return 1;
10667
10668 /* Handle implicit object parameters. */
10669 maybe_handle_implicit_object (&ics1);
10670 maybe_handle_implicit_object (&ics2);
10671
10672 /* Handle reference parameters. */
10673 ref_conv1 = maybe_handle_ref_bind (&ics1);
10674 ref_conv2 = maybe_handle_ref_bind (&ics2);
10675
10676 /* List-initialization sequence L1 is a better conversion sequence than
10677 list-initialization sequence L2 if L1 converts to
10678 std::initializer_list<X> for some X and L2 does not. */
10679 if (ics1->kind == ck_list && ics2->kind != ck_list)
10680 return 1;
10681 if (ics2->kind == ck_list && ics1->kind != ck_list)
10682 return -1;
10683
10684 /* [over.ics.rank]
10685
10686 When comparing the basic forms of implicit conversion sequences (as
10687 defined in _over.best.ics_)
10688
10689 --a standard conversion sequence (_over.ics.scs_) is a better
10690 conversion sequence than a user-defined conversion sequence
10691 or an ellipsis conversion sequence, and
10692
10693 --a user-defined conversion sequence (_over.ics.user_) is a
10694 better conversion sequence than an ellipsis conversion sequence
10695 (_over.ics.ellipsis_). */
10696 /* Use BAD_CONVERSION_RANK because we already checked for a badness
10697 mismatch. If both ICS are bad, we try to make a decision based on
10698 what would have happened if they'd been good. This is not an
10699 extension, we'll still give an error when we build up the call; this
10700 just helps us give a more helpful error message. */
10701 rank1 = BAD_CONVERSION_RANK (ics1);
10702 rank2 = BAD_CONVERSION_RANK (ics2);
10703
10704 if (rank1 > rank2)
10705 return -1;
10706 else if (rank1 < rank2)
10707 return 1;
10708
10709 if (ics1->ellipsis_p)
10710 /* Both conversions are ellipsis conversions. */
10711 return 0;
10712
10713 /* User-defined conversion sequence U1 is a better conversion sequence
10714 than another user-defined conversion sequence U2 if they contain the
10715 same user-defined conversion operator or constructor and if the sec-
10716 ond standard conversion sequence of U1 is better than the second
10717 standard conversion sequence of U2. */
10718
10719 /* Handle list-conversion with the same code even though it isn't always
10720 ranked as a user-defined conversion and it doesn't have a second
10721 standard conversion sequence; it will still have the desired effect.
10722 Specifically, we need to do the reference binding comparison at the
10723 end of this function. */
10724
10725 if (ics1->user_conv_p || ics1->kind == ck_list
10726 || ics1->kind == ck_aggr || ics2->kind == ck_aggr)
10727 {
10728 conversion *t1 = strip_standard_conversion (ics1);
10729 conversion *t2 = strip_standard_conversion (ics2);
10730
10731 if (!t1 || !t2 || t1->kind != t2->kind)
10732 return 0;
10733 else if (t1->kind == ck_user)
10734 {
10735 tree f1 = t1->cand ? t1->cand->fn : t1->type;
10736 tree f2 = t2->cand ? t2->cand->fn : t2->type;
10737 if (f1 != f2)
10738 return 0;
10739 }
10740 /* List-initialization sequence L1 is a better conversion sequence than
10741 list-initialization sequence L2 if
10742
10743 -- L1 and L2 convert to arrays of the same element type, and either
10744 the number of elements n1 initialized by L1 is less than the number
10745 of elements n2 initialized by L2, or n1=n2 and L2 converts to an array
10746 of unknown bound and L1 does not. (Added in CWG 1307 and extended by
10747 P0388R4.) */
10748 else if (t1->kind == ck_aggr
10749 && TREE_CODE (t1->type) == ARRAY_TYPE
10750 && TREE_CODE (t2->type) == ARRAY_TYPE)
10751 {
10752 /* The type of the array elements must be the same. */
10753 if (!same_type_p (TREE_TYPE (t1->type), TREE_TYPE (t2->type)))
10754 return 0;
10755
10756 tree n1 = nelts_initialized_by_list_init (t1);
10757 tree n2 = nelts_initialized_by_list_init (t2);
10758 if (tree_int_cst_lt (n1, n2))
10759 return 1;
10760 else if (tree_int_cst_lt (n2, n1))
10761 return -1;
10762 /* The n1 == n2 case. */
10763 bool c1 = conv_binds_to_array_of_unknown_bound (t1);
10764 bool c2 = conv_binds_to_array_of_unknown_bound (t2);
10765 if (c1 && !c2)
10766 return -1;
10767 else if (!c1 && c2)
10768 return 1;
10769 else
10770 return 0;
10771 }
10772 else
10773 {
10774 /* For ambiguous or aggregate conversions, use the target type as
10775 a proxy for the conversion function. */
10776 if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
10777 return 0;
10778 }
10779
10780 /* We can just fall through here, after setting up
10781 FROM_TYPE1 and FROM_TYPE2. */
10782 from_type1 = t1->type;
10783 from_type2 = t2->type;
10784 }
10785 else
10786 {
10787 conversion *t1;
10788 conversion *t2;
10789
10790 /* We're dealing with two standard conversion sequences.
10791
10792 [over.ics.rank]
10793
10794 Standard conversion sequence S1 is a better conversion
10795 sequence than standard conversion sequence S2 if
10796
10797 --S1 is a proper subsequence of S2 (comparing the conversion
10798 sequences in the canonical form defined by _over.ics.scs_,
10799 excluding any Lvalue Transformation; the identity
10800 conversion sequence is considered to be a subsequence of
10801 any non-identity conversion sequence */
10802
10803 t1 = ics1;
10804 while (t1->kind != ck_identity)
10805 t1 = next_conversion (t1);
10806 from_type1 = t1->type;
10807
10808 t2 = ics2;
10809 while (t2->kind != ck_identity)
10810 t2 = next_conversion (t2);
10811 from_type2 = t2->type;
10812 }
10813
10814 /* One sequence can only be a subsequence of the other if they start with
10815 the same type. They can start with different types when comparing the
10816 second standard conversion sequence in two user-defined conversion
10817 sequences. */
10818 if (same_type_p (from_type1, from_type2))
10819 {
10820 if (is_subseq (ics1, ics2))
10821 return 1;
10822 if (is_subseq (ics2, ics1))
10823 return -1;
10824 }
10825
10826 /* [over.ics.rank]
10827
10828 Or, if not that,
10829
10830 --the rank of S1 is better than the rank of S2 (by the rules
10831 defined below):
10832
10833 Standard conversion sequences are ordered by their ranks: an Exact
10834 Match is a better conversion than a Promotion, which is a better
10835 conversion than a Conversion.
10836
10837 Two conversion sequences with the same rank are indistinguishable
10838 unless one of the following rules applies:
10839
10840 --A conversion that does not a convert a pointer, pointer to member,
10841 or std::nullptr_t to bool is better than one that does.
10842
10843 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
10844 so that we do not have to check it explicitly. */
10845 if (ics1->rank < ics2->rank)
10846 return 1;
10847 else if (ics2->rank < ics1->rank)
10848 return -1;
10849
10850 to_type1 = ics1->type;
10851 to_type2 = ics2->type;
10852
10853 /* A conversion from scalar arithmetic type to complex is worse than a
10854 conversion between scalar arithmetic types. */
10855 if (same_type_p (from_type1, from_type2)
10856 && ARITHMETIC_TYPE_P (from_type1)
10857 && ARITHMETIC_TYPE_P (to_type1)
10858 && ARITHMETIC_TYPE_P (to_type2)
10859 && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
10860 != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
10861 {
10862 if (TREE_CODE (to_type1) == COMPLEX_TYPE)
10863 return -1;
10864 else
10865 return 1;
10866 }
10867
10868 if (TYPE_PTR_P (from_type1)
10869 && TYPE_PTR_P (from_type2)
10870 && TYPE_PTR_P (to_type1)
10871 && TYPE_PTR_P (to_type2))
10872 {
10873 deref_from_type1 = TREE_TYPE (from_type1);
10874 deref_from_type2 = TREE_TYPE (from_type2);
10875 deref_to_type1 = TREE_TYPE (to_type1);
10876 deref_to_type2 = TREE_TYPE (to_type2);
10877 }
10878 /* The rules for pointers to members A::* are just like the rules
10879 for pointers A*, except opposite: if B is derived from A then
10880 A::* converts to B::*, not vice versa. For that reason, we
10881 switch the from_ and to_ variables here. */
10882 else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2)
10883 && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2))
10884 || (TYPE_PTRMEMFUNC_P (from_type1)
10885 && TYPE_PTRMEMFUNC_P (from_type2)
10886 && TYPE_PTRMEMFUNC_P (to_type1)
10887 && TYPE_PTRMEMFUNC_P (to_type2)))
10888 {
10889 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
10890 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
10891 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
10892 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
10893 }
10894
10895 if (deref_from_type1 != NULL_TREE
10896 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
10897 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
10898 {
10899 /* This was one of the pointer or pointer-like conversions.
10900
10901 [over.ics.rank]
10902
10903 --If class B is derived directly or indirectly from class A,
10904 conversion of B* to A* is better than conversion of B* to
10905 void*, and conversion of A* to void* is better than
10906 conversion of B* to void*. */
10907 if (VOID_TYPE_P (deref_to_type1)
10908 && VOID_TYPE_P (deref_to_type2))
10909 {
10910 if (is_properly_derived_from (deref_from_type1,
10911 deref_from_type2))
10912 return -1;
10913 else if (is_properly_derived_from (deref_from_type2,
10914 deref_from_type1))
10915 return 1;
10916 }
10917 else if (VOID_TYPE_P (deref_to_type1)
10918 || VOID_TYPE_P (deref_to_type2))
10919 {
10920 if (same_type_p (deref_from_type1, deref_from_type2))
10921 {
10922 if (VOID_TYPE_P (deref_to_type2))
10923 {
10924 if (is_properly_derived_from (deref_from_type1,
10925 deref_to_type1))
10926 return 1;
10927 }
10928 /* We know that DEREF_TO_TYPE1 is `void' here. */
10929 else if (is_properly_derived_from (deref_from_type1,
10930 deref_to_type2))
10931 return -1;
10932 }
10933 }
10934 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
10935 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
10936 {
10937 /* [over.ics.rank]
10938
10939 --If class B is derived directly or indirectly from class A
10940 and class C is derived directly or indirectly from B,
10941
10942 --conversion of C* to B* is better than conversion of C* to
10943 A*,
10944
10945 --conversion of B* to A* is better than conversion of C* to
10946 A* */
10947 if (same_type_p (deref_from_type1, deref_from_type2))
10948 {
10949 if (is_properly_derived_from (deref_to_type1,
10950 deref_to_type2))
10951 return 1;
10952 else if (is_properly_derived_from (deref_to_type2,
10953 deref_to_type1))
10954 return -1;
10955 }
10956 else if (same_type_p (deref_to_type1, deref_to_type2))
10957 {
10958 if (is_properly_derived_from (deref_from_type2,
10959 deref_from_type1))
10960 return 1;
10961 else if (is_properly_derived_from (deref_from_type1,
10962 deref_from_type2))
10963 return -1;
10964 }
10965 }
10966 }
10967 else if (CLASS_TYPE_P (non_reference (from_type1))
10968 && same_type_p (from_type1, from_type2))
10969 {
10970 tree from = non_reference (from_type1);
10971
10972 /* [over.ics.rank]
10973
10974 --binding of an expression of type C to a reference of type
10975 B& is better than binding an expression of type C to a
10976 reference of type A&
10977
10978 --conversion of C to B is better than conversion of C to A, */
10979 if (is_properly_derived_from (from, to_type1)
10980 && is_properly_derived_from (from, to_type2))
10981 {
10982 if (is_properly_derived_from (to_type1, to_type2))
10983 return 1;
10984 else if (is_properly_derived_from (to_type2, to_type1))
10985 return -1;
10986 }
10987 }
10988 else if (CLASS_TYPE_P (non_reference (to_type1))
10989 && same_type_p (to_type1, to_type2))
10990 {
10991 tree to = non_reference (to_type1);
10992
10993 /* [over.ics.rank]
10994
10995 --binding of an expression of type B to a reference of type
10996 A& is better than binding an expression of type C to a
10997 reference of type A&,
10998
10999 --conversion of B to A is better than conversion of C to A */
11000 if (is_properly_derived_from (from_type1, to)
11001 && is_properly_derived_from (from_type2, to))
11002 {
11003 if (is_properly_derived_from (from_type2, from_type1))
11004 return 1;
11005 else if (is_properly_derived_from (from_type1, from_type2))
11006 return -1;
11007 }
11008 }
11009
11010 /* [over.ics.rank]
11011
11012 --S1 and S2 differ only in their qualification conversion and yield
11013 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
11014 qualification signature of type T1 is a proper subset of the cv-
11015 qualification signature of type T2 */
11016 if (ics1->kind == ck_qual
11017 && ics2->kind == ck_qual
11018 && same_type_p (from_type1, from_type2))
11019 {
11020 int result = comp_cv_qual_signature (to_type1, to_type2);
11021 if (result != 0)
11022 return result;
11023 }
11024
11025 /* [over.ics.rank]
11026
11027 --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
11028 to an implicit object parameter of a non-static member function
11029 declared without a ref-qualifier, and either S1 binds an lvalue
11030 reference to an lvalue and S2 binds an rvalue reference or S1 binds an
11031 rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x
11032 draft standard, 13.3.3.2)
11033
11034 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
11035 types to which the references refer are the same type except for
11036 top-level cv-qualifiers, and the type to which the reference
11037 initialized by S2 refers is more cv-qualified than the type to
11038 which the reference initialized by S1 refers.
11039
11040 DR 1328 [over.match.best]: the context is an initialization by
11041 conversion function for direct reference binding (13.3.1.6) of a
11042 reference to function type, the return type of F1 is the same kind of
11043 reference (i.e. lvalue or rvalue) as the reference being initialized,
11044 and the return type of F2 is not. */
11045
11046 if (ref_conv1 && ref_conv2)
11047 {
11048 if (!ref_conv1->this_p && !ref_conv2->this_p
11049 && (ref_conv1->rvaluedness_matches_p
11050 != ref_conv2->rvaluedness_matches_p)
11051 && (same_type_p (ref_conv1->type, ref_conv2->type)
11052 || (TYPE_REF_IS_RVALUE (ref_conv1->type)
11053 != TYPE_REF_IS_RVALUE (ref_conv2->type))))
11054 {
11055 if (ref_conv1->bad_p
11056 && !same_type_p (TREE_TYPE (ref_conv1->type),
11057 TREE_TYPE (ref_conv2->type)))
11058 /* Don't prefer a bad conversion that drops cv-quals to a bad
11059 conversion with the wrong rvalueness. */
11060 return 0;
11061 return (ref_conv1->rvaluedness_matches_p
11062 - ref_conv2->rvaluedness_matches_p);
11063 }
11064
11065 if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
11066 {
11067 /* Per P0388R4:
11068
11069 void f (int(&)[]), // (1)
11070 f (int(&)[1]), // (2)
11071 f (int*); // (3)
11072
11073 (2) is better than (1), but (3) should be equal to (1) and to
11074 (2). For that reason we don't use ck_qual for (1) which would
11075 give it the cr_exact rank while (3) remains ck_identity.
11076 Therefore we compare (1) and (2) here. For (1) we'll have
11077
11078 ck_ref_bind <- ck_identity
11079 int[] & int[1]
11080
11081 so to handle this we must look at ref_conv. */
11082 bool c1 = conv_binds_to_array_of_unknown_bound (ref_conv1);
11083 bool c2 = conv_binds_to_array_of_unknown_bound (ref_conv2);
11084 if (c1 && !c2)
11085 return -1;
11086 else if (!c1 && c2)
11087 return 1;
11088
11089 int q1 = cp_type_quals (TREE_TYPE (ref_conv1->type));
11090 int q2 = cp_type_quals (TREE_TYPE (ref_conv2->type));
11091 if (ref_conv1->bad_p)
11092 {
11093 /* Prefer the one that drops fewer cv-quals. */
11094 tree ftype = next_conversion (ref_conv1)->type;
11095 int fquals = cp_type_quals (ftype);
11096 q1 ^= fquals;
11097 q2 ^= fquals;
11098 }
11099 return comp_cv_qualification (q2, q1);
11100 }
11101 }
11102
11103 /* [over.ics.rank]
11104
11105 Per CWG 1601:
11106 -- A conversion that promotes an enumeration whose underlying type
11107 is fixed to its underlying type is better than one that promotes to
11108 the promoted underlying type, if the two are different. */
11109 if (ics1->rank == cr_promotion
11110 && ics2->rank == cr_promotion
11111 && UNSCOPED_ENUM_P (from_type1)
11112 && ENUM_FIXED_UNDERLYING_TYPE_P (from_type1)
11113 && same_type_p (from_type1, from_type2))
11114 {
11115 tree utype = ENUM_UNDERLYING_TYPE (from_type1);
11116 tree prom = type_promotes_to (from_type1);
11117 if (!same_type_p (utype, prom))
11118 {
11119 if (same_type_p (to_type1, utype)
11120 && same_type_p (to_type2, prom))
11121 return 1;
11122 else if (same_type_p (to_type2, utype)
11123 && same_type_p (to_type1, prom))
11124 return -1;
11125 }
11126 }
11127
11128 /* Neither conversion sequence is better than the other. */
11129 return 0;
11130 }
11131
11132 /* The source type for this standard conversion sequence. */
11133
11134 static tree
11135 source_type (conversion *t)
11136 {
11137 return strip_standard_conversion (t)->type;
11138 }
11139
11140 /* Note a warning about preferring WINNER to LOSER. We do this by storing
11141 a pointer to LOSER and re-running joust to produce the warning if WINNER
11142 is actually used. */
11143
11144 static void
11145 add_warning (struct z_candidate *winner, struct z_candidate *loser)
11146 {
11147 candidate_warning *cw = (candidate_warning *)
11148 conversion_obstack_alloc (sizeof (candidate_warning));
11149 cw->loser = loser;
11150 cw->next = winner->warnings;
11151 winner->warnings = cw;
11152 }
11153
11154 /* CAND is a constructor candidate in joust in C++17 and up. If it copies a
11155 prvalue returned from a conversion function, replace CAND with the candidate
11156 for the conversion and return true. Otherwise, return false. */
11157
11158 static bool
11159 joust_maybe_elide_copy (z_candidate *&cand)
11160 {
11161 tree fn = cand->fn;
11162 if (!DECL_COPY_CONSTRUCTOR_P (fn) && !DECL_MOVE_CONSTRUCTOR_P (fn))
11163 return false;
11164 conversion *conv = cand->convs[0];
11165 gcc_checking_assert (conv->kind == ck_ref_bind);
11166 conv = next_conversion (conv);
11167 if (conv->kind == ck_user && !TYPE_REF_P (conv->type))
11168 {
11169 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
11170 (conv->type, DECL_CONTEXT (fn)));
11171 z_candidate *uc = conv->cand;
11172 if (DECL_CONV_FN_P (uc->fn))
11173 {
11174 cand = uc;
11175 return true;
11176 }
11177 }
11178 return false;
11179 }
11180
11181 /* True if the defining declarations of the two candidates have equivalent
11182 parameters. */
11183
11184 bool
11185 cand_parms_match (z_candidate *c1, z_candidate *c2)
11186 {
11187 tree fn1 = c1->fn;
11188 tree fn2 = c2->fn;
11189 if (fn1 == fn2)
11190 return true;
11191 if (identifier_p (fn1) || identifier_p (fn2))
11192 return false;
11193 /* We don't look at c1->template_decl because that's only set for primary
11194 templates, not e.g. non-template member functions of class templates. */
11195 tree t1 = most_general_template (fn1);
11196 tree t2 = most_general_template (fn2);
11197 if (t1 || t2)
11198 {
11199 if (!t1 || !t2)
11200 return false;
11201 if (t1 == t2)
11202 return true;
11203 fn1 = DECL_TEMPLATE_RESULT (t1);
11204 fn2 = DECL_TEMPLATE_RESULT (t2);
11205 }
11206 return compparms (TYPE_ARG_TYPES (TREE_TYPE (fn1)),
11207 TYPE_ARG_TYPES (TREE_TYPE (fn2)));
11208 }
11209
11210 /* Compare two candidates for overloading as described in
11211 [over.match.best]. Return values:
11212
11213 1: cand1 is better than cand2
11214 -1: cand2 is better than cand1
11215 0: cand1 and cand2 are indistinguishable */
11216
11217 static int
11218 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
11219 tsubst_flags_t complain)
11220 {
11221 int winner = 0;
11222 int off1 = 0, off2 = 0;
11223 size_t i;
11224 size_t len;
11225
11226 /* Candidates that involve bad conversions are always worse than those
11227 that don't. */
11228 if (cand1->viable > cand2->viable)
11229 return 1;
11230 if (cand1->viable < cand2->viable)
11231 return -1;
11232
11233 /* If we have two pseudo-candidates for conversions to the same type,
11234 or two candidates for the same function, arbitrarily pick one. */
11235 if (cand1->fn == cand2->fn
11236 && cand1->reversed () == cand2->reversed ()
11237 && (IS_TYPE_OR_DECL_P (cand1->fn)))
11238 return 1;
11239
11240 /* Prefer a non-deleted function over an implicitly deleted move
11241 constructor or assignment operator. This differs slightly from the
11242 wording for issue 1402 (which says the move op is ignored by overload
11243 resolution), but this way produces better error messages. */
11244 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
11245 && TREE_CODE (cand2->fn) == FUNCTION_DECL
11246 && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn))
11247 {
11248 if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn)
11249 && move_fn_p (cand1->fn))
11250 return -1;
11251 if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn)
11252 && move_fn_p (cand2->fn))
11253 return 1;
11254 }
11255
11256 /* a viable function F1
11257 is defined to be a better function than another viable function F2 if
11258 for all arguments i, ICSi(F1) is not a worse conversion sequence than
11259 ICSi(F2), and then */
11260
11261 /* for some argument j, ICSj(F1) is a better conversion sequence than
11262 ICSj(F2) */
11263
11264 /* For comparing static and non-static member functions, we ignore
11265 the implicit object parameter of the non-static function. The
11266 standard says to pretend that the static function has an object
11267 parm, but that won't work with operator overloading. */
11268 len = cand1->num_convs;
11269 if (len != cand2->num_convs)
11270 {
11271 int static_1 = DECL_STATIC_FUNCTION_P (cand1->fn);
11272 int static_2 = DECL_STATIC_FUNCTION_P (cand2->fn);
11273
11274 if (DECL_CONSTRUCTOR_P (cand1->fn)
11275 && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
11276 /* We're comparing a near-match list constructor and a near-match
11277 non-list constructor. Just treat them as unordered. */
11278 return 0;
11279
11280 gcc_assert (static_1 != static_2);
11281
11282 if (static_1)
11283 off2 = 1;
11284 else
11285 {
11286 off1 = 1;
11287 --len;
11288 }
11289 }
11290
11291 /* Handle C++17 copy elision in [over.match.ctor] (direct-init) context. The
11292 standard currently says that only constructors are candidates, but if one
11293 copies a prvalue returned by a conversion function we want to treat the
11294 conversion as the candidate instead.
11295
11296 Clang does something similar, as discussed at
11297 http://lists.isocpp.org/core/2017/10/3166.php
11298 http://lists.isocpp.org/core/2019/03/5721.php */
11299 int elided_tiebreaker = 0;
11300 if (len == 1 && cxx_dialect >= cxx17
11301 && DECL_P (cand1->fn)
11302 && DECL_COMPLETE_CONSTRUCTOR_P (cand1->fn)
11303 && !(cand1->flags & LOOKUP_ONLYCONVERTING))
11304 {
11305 bool elided1 = joust_maybe_elide_copy (cand1);
11306 bool elided2 = joust_maybe_elide_copy (cand2);
11307 /* As a tiebreaker below we will prefer a constructor to a conversion
11308 operator exposed this way. */
11309 elided_tiebreaker = elided2 - elided1;
11310 }
11311
11312 for (i = 0; i < len; ++i)
11313 {
11314 conversion *t1 = cand1->convs[i + off1];
11315 conversion *t2 = cand2->convs[i + off2];
11316 int comp = compare_ics (t1, t2);
11317
11318 if (comp != 0)
11319 {
11320 if ((complain & tf_warning)
11321 && warn_sign_promo
11322 && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
11323 == cr_std + cr_promotion)
11324 && t1->kind == ck_std
11325 && t2->kind == ck_std
11326 && TREE_CODE (t1->type) == INTEGER_TYPE
11327 && TREE_CODE (t2->type) == INTEGER_TYPE
11328 && (TYPE_PRECISION (t1->type)
11329 == TYPE_PRECISION (t2->type))
11330 && (TYPE_UNSIGNED (next_conversion (t1)->type)
11331 || (TREE_CODE (next_conversion (t1)->type)
11332 == ENUMERAL_TYPE)))
11333 {
11334 tree type = next_conversion (t1)->type;
11335 tree type1, type2;
11336 struct z_candidate *w, *l;
11337 if (comp > 0)
11338 type1 = t1->type, type2 = t2->type,
11339 w = cand1, l = cand2;
11340 else
11341 type1 = t2->type, type2 = t1->type,
11342 w = cand2, l = cand1;
11343
11344 if (warn)
11345 {
11346 warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
11347 type, type1, type2);
11348 warning (OPT_Wsign_promo, " in call to %qD", w->fn);
11349 }
11350 else
11351 add_warning (w, l);
11352 }
11353
11354 if (winner && comp != winner)
11355 {
11356 /* Ambiguity between normal and reversed comparison operators
11357 with the same parameter types; prefer the normal one. */
11358 if ((cand1->reversed () != cand2->reversed ())
11359 && cand_parms_match (cand1, cand2))
11360 return cand1->reversed () ? -1 : 1;
11361
11362 winner = 0;
11363 goto tweak;
11364 }
11365 winner = comp;
11366 }
11367 }
11368
11369 /* warn about confusing overload resolution for user-defined conversions,
11370 either between a constructor and a conversion op, or between two
11371 conversion ops. */
11372 if ((complain & tf_warning)
11373 /* In C++17, the constructor might have been elided, which means that
11374 an originally null ->second_conv could become non-null. */
11375 && winner && warn_conversion && cand1->second_conv && cand2->second_conv
11376 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
11377 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
11378 {
11379 struct z_candidate *w, *l;
11380 bool give_warning = false;
11381
11382 if (winner == 1)
11383 w = cand1, l = cand2;
11384 else
11385 w = cand2, l = cand1;
11386
11387 /* We don't want to complain about `X::operator T1 ()'
11388 beating `X::operator T2 () const', when T2 is a no less
11389 cv-qualified version of T1. */
11390 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
11391 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
11392 {
11393 tree t = TREE_TYPE (TREE_TYPE (l->fn));
11394 tree f = TREE_TYPE (TREE_TYPE (w->fn));
11395
11396 if (TREE_CODE (t) == TREE_CODE (f) && INDIRECT_TYPE_P (t))
11397 {
11398 t = TREE_TYPE (t);
11399 f = TREE_TYPE (f);
11400 }
11401 if (!comp_ptr_ttypes (t, f))
11402 give_warning = true;
11403 }
11404 else
11405 give_warning = true;
11406
11407 if (!give_warning)
11408 /*NOP*/;
11409 else if (warn)
11410 {
11411 tree source = source_type (w->convs[0]);
11412 if (INDIRECT_TYPE_P (source))
11413 source = TREE_TYPE (source);
11414 auto_diagnostic_group d;
11415 if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
11416 && warning (OPT_Wconversion, " for conversion from %qH to %qI",
11417 source, w->second_conv->type))
11418 {
11419 inform (input_location, " because conversion sequence "
11420 "for the argument is better");
11421 }
11422 }
11423 else
11424 add_warning (w, l);
11425 }
11426
11427 if (winner)
11428 return winner;
11429
11430 /* Put this tiebreaker first, so that we don't try to look at second_conv of
11431 a constructor candidate that doesn't have one. */
11432 if (elided_tiebreaker)
11433 return elided_tiebreaker;
11434
11435 /* DR 495 moved this tiebreaker above the template ones. */
11436 /* or, if not that,
11437 the context is an initialization by user-defined conversion (see
11438 _dcl.init_ and _over.match.user_) and the standard conversion
11439 sequence from the return type of F1 to the destination type (i.e.,
11440 the type of the entity being initialized) is a better conversion
11441 sequence than the standard conversion sequence from the return type
11442 of F2 to the destination type. */
11443
11444 if (cand1->second_conv)
11445 {
11446 winner = compare_ics (cand1->second_conv, cand2->second_conv);
11447 if (winner)
11448 return winner;
11449 }
11450
11451 /* or, if not that,
11452 F1 is a non-template function and F2 is a template function
11453 specialization. */
11454
11455 if (!cand1->template_decl && cand2->template_decl)
11456 return 1;
11457 else if (cand1->template_decl && !cand2->template_decl)
11458 return -1;
11459
11460 /* or, if not that,
11461 F1 and F2 are template functions and the function template for F1 is
11462 more specialized than the template for F2 according to the partial
11463 ordering rules. */
11464
11465 if (cand1->template_decl && cand2->template_decl)
11466 {
11467 winner = more_specialized_fn
11468 (TI_TEMPLATE (cand1->template_decl),
11469 TI_TEMPLATE (cand2->template_decl),
11470 /* [temp.func.order]: The presence of unused ellipsis and default
11471 arguments has no effect on the partial ordering of function
11472 templates. add_function_candidate() will not have
11473 counted the "this" argument for constructors. */
11474 cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
11475 if (winner)
11476 return winner;
11477 }
11478
11479 /* Concepts: F1 and F2 are non-template functions with the same
11480 parameter-type-lists, and F1 is more constrained than F2 according to the
11481 partial ordering of constraints described in 13.5.4. */
11482
11483 if (flag_concepts && DECL_P (cand1->fn) && DECL_P (cand2->fn)
11484 && !cand1->template_decl && !cand2->template_decl
11485 && cand_parms_match (cand1, cand2))
11486 {
11487 winner = more_constrained (cand1->fn, cand2->fn);
11488 if (winner)
11489 return winner;
11490 }
11491
11492 /* F2 is a rewritten candidate (12.4.1.2) and F1 is not, or F1 and F2 are
11493 rewritten candidates, and F2 is a synthesized candidate with reversed
11494 order of parameters and F1 is not. */
11495 if (cand1->rewritten ())
11496 {
11497 if (!cand2->rewritten ())
11498 return -1;
11499 if (!cand1->reversed () && cand2->reversed ())
11500 return 1;
11501 if (cand1->reversed () && !cand2->reversed ())
11502 return -1;
11503 }
11504 else if (cand2->rewritten ())
11505 return 1;
11506
11507 /* F1 is generated from a deduction-guide (13.3.1.8) and F2 is not */
11508 if (deduction_guide_p (cand1->fn))
11509 {
11510 gcc_assert (deduction_guide_p (cand2->fn));
11511 /* We distinguish between candidates from an explicit deduction guide and
11512 candidates built from a constructor based on DECL_ARTIFICIAL. */
11513 int art1 = DECL_ARTIFICIAL (cand1->fn);
11514 int art2 = DECL_ARTIFICIAL (cand2->fn);
11515 if (art1 != art2)
11516 return art2 - art1;
11517
11518 if (art1)
11519 {
11520 /* Prefer the special copy guide over a declared copy/move
11521 constructor. */
11522 if (copy_guide_p (cand1->fn))
11523 return 1;
11524 if (copy_guide_p (cand2->fn))
11525 return -1;
11526
11527 /* Prefer a candidate generated from a non-template constructor. */
11528 int tg1 = template_guide_p (cand1->fn);
11529 int tg2 = template_guide_p (cand2->fn);
11530 if (tg1 != tg2)
11531 return tg2 - tg1;
11532 }
11533 }
11534
11535 /* F1 is a member of a class D, F2 is a member of a base class B of D, and
11536 for all arguments the corresponding parameters of F1 and F2 have the same
11537 type (CWG 2273/2277). */
11538 if (DECL_P (cand1->fn) && DECL_CLASS_SCOPE_P (cand1->fn)
11539 && !DECL_CONV_FN_P (cand1->fn)
11540 && DECL_P (cand2->fn) && DECL_CLASS_SCOPE_P (cand2->fn)
11541 && !DECL_CONV_FN_P (cand2->fn))
11542 {
11543 tree base1 = DECL_CONTEXT (strip_inheriting_ctors (cand1->fn));
11544 tree base2 = DECL_CONTEXT (strip_inheriting_ctors (cand2->fn));
11545
11546 bool used1 = false;
11547 bool used2 = false;
11548 if (base1 == base2)
11549 /* No difference. */;
11550 else if (DERIVED_FROM_P (base1, base2))
11551 used1 = true;
11552 else if (DERIVED_FROM_P (base2, base1))
11553 used2 = true;
11554
11555 if (int diff = used2 - used1)
11556 {
11557 for (i = 0; i < len; ++i)
11558 {
11559 conversion *t1 = cand1->convs[i + off1];
11560 conversion *t2 = cand2->convs[i + off2];
11561 if (!same_type_p (t1->type, t2->type))
11562 break;
11563 }
11564 if (i == len)
11565 return diff;
11566 }
11567 }
11568
11569 /* Check whether we can discard a builtin candidate, either because we
11570 have two identical ones or matching builtin and non-builtin candidates.
11571
11572 (Pedantically in the latter case the builtin which matched the user
11573 function should not be added to the overload set, but we spot it here.
11574
11575 [over.match.oper]
11576 ... the builtin candidates include ...
11577 - do not have the same parameter type list as any non-template
11578 non-member candidate. */
11579
11580 if (identifier_p (cand1->fn) || identifier_p (cand2->fn))
11581 {
11582 for (i = 0; i < len; ++i)
11583 if (!same_type_p (cand1->convs[i]->type,
11584 cand2->convs[i]->type))
11585 break;
11586 if (i == cand1->num_convs)
11587 {
11588 if (cand1->fn == cand2->fn)
11589 /* Two built-in candidates; arbitrarily pick one. */
11590 return 1;
11591 else if (identifier_p (cand1->fn))
11592 /* cand1 is built-in; prefer cand2. */
11593 return -1;
11594 else
11595 /* cand2 is built-in; prefer cand1. */
11596 return 1;
11597 }
11598 }
11599
11600 /* For candidates of a multi-versioned function, make the version with
11601 the highest priority win. This version will be checked for dispatching
11602 first. If this version can be inlined into the caller, the front-end
11603 will simply make a direct call to this function. */
11604
11605 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
11606 && DECL_FUNCTION_VERSIONED (cand1->fn)
11607 && TREE_CODE (cand2->fn) == FUNCTION_DECL
11608 && DECL_FUNCTION_VERSIONED (cand2->fn))
11609 {
11610 tree f1 = TREE_TYPE (cand1->fn);
11611 tree f2 = TREE_TYPE (cand2->fn);
11612 tree p1 = TYPE_ARG_TYPES (f1);
11613 tree p2 = TYPE_ARG_TYPES (f2);
11614
11615 /* Check if cand1->fn and cand2->fn are versions of the same function. It
11616 is possible that cand1->fn and cand2->fn are function versions but of
11617 different functions. Check types to see if they are versions of the same
11618 function. */
11619 if (compparms (p1, p2)
11620 && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
11621 {
11622 /* Always make the version with the higher priority, more
11623 specialized, win. */
11624 gcc_assert (targetm.compare_version_priority);
11625 if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
11626 return 1;
11627 else
11628 return -1;
11629 }
11630 }
11631
11632 /* If the two function declarations represent the same function (this can
11633 happen with declarations in multiple scopes and arg-dependent lookup),
11634 arbitrarily choose one. But first make sure the default args we're
11635 using match. */
11636 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
11637 && equal_functions (cand1->fn, cand2->fn))
11638 {
11639 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
11640 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
11641
11642 gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
11643
11644 for (i = 0; i < len; ++i)
11645 {
11646 /* Don't crash if the fn is variadic. */
11647 if (!parms1)
11648 break;
11649 parms1 = TREE_CHAIN (parms1);
11650 parms2 = TREE_CHAIN (parms2);
11651 }
11652
11653 if (off1)
11654 parms1 = TREE_CHAIN (parms1);
11655 else if (off2)
11656 parms2 = TREE_CHAIN (parms2);
11657
11658 for (; parms1; ++i)
11659 {
11660 if (!cp_tree_equal (TREE_PURPOSE (parms1),
11661 TREE_PURPOSE (parms2)))
11662 {
11663 if (warn)
11664 {
11665 if (complain & tf_error)
11666 {
11667 auto_diagnostic_group d;
11668 if (permerror (input_location,
11669 "default argument mismatch in "
11670 "overload resolution"))
11671 {
11672 inform (DECL_SOURCE_LOCATION (cand1->fn),
11673 " candidate 1: %q#F", cand1->fn);
11674 inform (DECL_SOURCE_LOCATION (cand2->fn),
11675 " candidate 2: %q#F", cand2->fn);
11676 }
11677 }
11678 else
11679 return 0;
11680 }
11681 else
11682 add_warning (cand1, cand2);
11683 break;
11684 }
11685 parms1 = TREE_CHAIN (parms1);
11686 parms2 = TREE_CHAIN (parms2);
11687 }
11688
11689 return 1;
11690 }
11691
11692 tweak:
11693
11694 /* Extension: If the worst conversion for one candidate is better than the
11695 worst conversion for the other, take the first. */
11696 if (!pedantic && (complain & tf_warning_or_error))
11697 {
11698 conversion_rank rank1 = cr_identity, rank2 = cr_identity;
11699 struct z_candidate *w = 0, *l = 0;
11700
11701 for (i = 0; i < len; ++i)
11702 {
11703 if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
11704 rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
11705 if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
11706 rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
11707 }
11708 if (rank1 < rank2)
11709 winner = 1, w = cand1, l = cand2;
11710 if (rank1 > rank2)
11711 winner = -1, w = cand2, l = cand1;
11712 if (winner)
11713 {
11714 /* Don't choose a deleted function over ambiguity. */
11715 if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
11716 return 0;
11717 if (warn)
11718 {
11719 auto_diagnostic_group d;
11720 if (pedwarn (input_location, 0,
11721 "ISO C++ says that these are ambiguous, even "
11722 "though the worst conversion for the first is "
11723 "better than the worst conversion for the second:"))
11724 {
11725 print_z_candidate (input_location, N_("candidate 1:"), w);
11726 print_z_candidate (input_location, N_("candidate 2:"), l);
11727 }
11728 }
11729 else
11730 add_warning (w, l);
11731 return winner;
11732 }
11733 }
11734
11735 gcc_assert (!winner);
11736 return 0;
11737 }
11738
11739 /* Given a list of candidates for overloading, find the best one, if any.
11740 This algorithm has a worst case of O(2n) (winner is last), and a best
11741 case of O(n/2) (totally ambiguous); much better than a sorting
11742 algorithm. */
11743
11744 static struct z_candidate *
11745 tourney (struct z_candidate *candidates, tsubst_flags_t complain)
11746 {
11747 struct z_candidate *champ = candidates, *challenger;
11748 int fate;
11749 int champ_compared_to_predecessor = 0;
11750
11751 /* Walk through the list once, comparing each current champ to the next
11752 candidate, knocking out a candidate or two with each comparison. */
11753
11754 for (challenger = champ->next; challenger; )
11755 {
11756 fate = joust (champ, challenger, 0, complain);
11757 if (fate == 1)
11758 challenger = challenger->next;
11759 else
11760 {
11761 if (fate == 0)
11762 {
11763 champ = challenger->next;
11764 if (champ == 0)
11765 return NULL;
11766 champ_compared_to_predecessor = 0;
11767 }
11768 else
11769 {
11770 champ = challenger;
11771 champ_compared_to_predecessor = 1;
11772 }
11773
11774 challenger = champ->next;
11775 }
11776 }
11777
11778 /* Make sure the champ is better than all the candidates it hasn't yet
11779 been compared to. */
11780
11781 for (challenger = candidates;
11782 challenger != champ
11783 && !(champ_compared_to_predecessor && challenger->next == champ);
11784 challenger = challenger->next)
11785 {
11786 fate = joust (champ, challenger, 0, complain);
11787 if (fate != 1)
11788 return NULL;
11789 }
11790
11791 return champ;
11792 }
11793
11794 /* Returns nonzero if things of type FROM can be converted to TO. */
11795
11796 bool
11797 can_convert (tree to, tree from, tsubst_flags_t complain)
11798 {
11799 tree arg = NULL_TREE;
11800 /* implicit_conversion only considers user-defined conversions
11801 if it has an expression for the call argument list. */
11802 if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to))
11803 arg = build1 (CAST_EXPR, from, NULL_TREE);
11804 return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain);
11805 }
11806
11807 /* Returns nonzero if things of type FROM can be converted to TO with a
11808 standard conversion. */
11809
11810 bool
11811 can_convert_standard (tree to, tree from, tsubst_flags_t complain)
11812 {
11813 return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
11814 }
11815
11816 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
11817
11818 bool
11819 can_convert_arg (tree to, tree from, tree arg, int flags,
11820 tsubst_flags_t complain)
11821 {
11822 conversion *t;
11823 void *p;
11824 bool ok_p;
11825
11826 /* Get the high-water mark for the CONVERSION_OBSTACK. */
11827 p = conversion_obstack_alloc (0);
11828 /* We want to discard any access checks done for this test,
11829 as we might not be in the appropriate access context and
11830 we'll do the check again when we actually perform the
11831 conversion. */
11832 push_deferring_access_checks (dk_deferred);
11833
11834 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
11835 flags, complain);
11836 ok_p = (t && !t->bad_p);
11837
11838 /* Discard the access checks now. */
11839 pop_deferring_access_checks ();
11840 /* Free all the conversions we allocated. */
11841 obstack_free (&conversion_obstack, p);
11842
11843 return ok_p;
11844 }
11845
11846 /* Like can_convert_arg, but allows dubious conversions as well. */
11847
11848 bool
11849 can_convert_arg_bad (tree to, tree from, tree arg, int flags,
11850 tsubst_flags_t complain)
11851 {
11852 conversion *t;
11853 void *p;
11854
11855 /* Get the high-water mark for the CONVERSION_OBSTACK. */
11856 p = conversion_obstack_alloc (0);
11857 /* Try to perform the conversion. */
11858 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
11859 flags, complain);
11860 /* Free all the conversions we allocated. */
11861 obstack_free (&conversion_obstack, p);
11862
11863 return t != NULL;
11864 }
11865
11866 /* Convert EXPR to TYPE. Return the converted expression.
11867
11868 Note that we allow bad conversions here because by the time we get to
11869 this point we are committed to doing the conversion. If we end up
11870 doing a bad conversion, convert_like will complain. */
11871
11872 tree
11873 perform_implicit_conversion_flags (tree type, tree expr,
11874 tsubst_flags_t complain, int flags)
11875 {
11876 conversion *conv;
11877 void *p;
11878 location_t loc = cp_expr_loc_or_input_loc (expr);
11879
11880 if (TYPE_REF_P (type))
11881 expr = mark_lvalue_use (expr);
11882 else
11883 expr = mark_rvalue_use (expr);
11884
11885 if (error_operand_p (expr))
11886 return error_mark_node;
11887
11888 /* Get the high-water mark for the CONVERSION_OBSTACK. */
11889 p = conversion_obstack_alloc (0);
11890
11891 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
11892 /*c_cast_p=*/false,
11893 flags, complain);
11894
11895 if (!conv)
11896 {
11897 if (complain & tf_error)
11898 implicit_conversion_error (loc, type, expr);
11899 expr = error_mark_node;
11900 }
11901 else if (processing_template_decl && conv->kind != ck_identity)
11902 {
11903 /* In a template, we are only concerned about determining the
11904 type of non-dependent expressions, so we do not have to
11905 perform the actual conversion. But for initializers, we
11906 need to be able to perform it at instantiation
11907 (or instantiate_non_dependent_expr) time. */
11908 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
11909 if (!(flags & LOOKUP_ONLYCONVERTING))
11910 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
11911 if (flags & LOOKUP_NO_NARROWING)
11912 IMPLICIT_CONV_EXPR_BRACED_INIT (expr) = true;
11913 }
11914 else
11915 expr = convert_like (conv, expr, complain);
11916
11917 /* Free all the conversions we allocated. */
11918 obstack_free (&conversion_obstack, p);
11919
11920 return expr;
11921 }
11922
11923 tree
11924 perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
11925 {
11926 return perform_implicit_conversion_flags (type, expr, complain,
11927 LOOKUP_IMPLICIT);
11928 }
11929
11930 /* Convert EXPR to TYPE (as a direct-initialization) if that is
11931 permitted. If the conversion is valid, the converted expression is
11932 returned. Otherwise, NULL_TREE is returned, except in the case
11933 that TYPE is a class type; in that case, an error is issued. If
11934 C_CAST_P is true, then this direct-initialization is taking
11935 place as part of a static_cast being attempted as part of a C-style
11936 cast. */
11937
11938 tree
11939 perform_direct_initialization_if_possible (tree type,
11940 tree expr,
11941 bool c_cast_p,
11942 tsubst_flags_t complain)
11943 {
11944 conversion *conv;
11945 void *p;
11946
11947 if (type == error_mark_node || error_operand_p (expr))
11948 return error_mark_node;
11949 /* [dcl.init]
11950
11951 If the destination type is a (possibly cv-qualified) class type:
11952
11953 -- If the initialization is direct-initialization ...,
11954 constructors are considered.
11955
11956 -- If overload resolution is successful, the selected constructor
11957 is called to initialize the object, with the initializer expression
11958 or expression-list as its argument(s).
11959
11960 -- Otherwise, if no constructor is viable, the destination type is
11961 a (possibly cv-qualified) aggregate class A, and the initializer is
11962 a parenthesized expression-list, the object is initialized as
11963 follows... */
11964 if (CLASS_TYPE_P (type))
11965 {
11966 releasing_vec args (make_tree_vector_single (expr));
11967 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
11968 &args, type, LOOKUP_NORMAL, complain);
11969 return build_cplus_new (type, expr, complain);
11970 }
11971
11972 /* Get the high-water mark for the CONVERSION_OBSTACK. */
11973 p = conversion_obstack_alloc (0);
11974
11975 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
11976 c_cast_p,
11977 LOOKUP_NORMAL, complain);
11978 if (!conv || conv->bad_p)
11979 expr = NULL_TREE;
11980 else if (processing_template_decl && conv->kind != ck_identity)
11981 {
11982 /* In a template, we are only concerned about determining the
11983 type of non-dependent expressions, so we do not have to
11984 perform the actual conversion. But for initializers, we
11985 need to be able to perform it at instantiation
11986 (or instantiate_non_dependent_expr) time. */
11987 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
11988 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
11989 }
11990 else
11991 expr = convert_like (conv, expr, NULL_TREE, 0,
11992 /*issue_conversion_warnings=*/false,
11993 c_cast_p, complain);
11994
11995 /* Free all the conversions we allocated. */
11996 obstack_free (&conversion_obstack, p);
11997
11998 return expr;
11999 }
12000
12001 /* When initializing a reference that lasts longer than a full-expression,
12002 this special rule applies:
12003
12004 [class.temporary]
12005
12006 The temporary to which the reference is bound or the temporary
12007 that is the complete object to which the reference is bound
12008 persists for the lifetime of the reference.
12009
12010 The temporaries created during the evaluation of the expression
12011 initializing the reference, except the temporary to which the
12012 reference is bound, are destroyed at the end of the
12013 full-expression in which they are created.
12014
12015 In that case, we store the converted expression into a new
12016 VAR_DECL in a new scope.
12017
12018 However, we want to be careful not to create temporaries when
12019 they are not required. For example, given:
12020
12021 struct B {};
12022 struct D : public B {};
12023 D f();
12024 const B& b = f();
12025
12026 there is no need to copy the return value from "f"; we can just
12027 extend its lifetime. Similarly, given:
12028
12029 struct S {};
12030 struct T { operator S(); };
12031 T t;
12032 const S& s = t;
12033
12034 we can extend the lifetime of the return value of the conversion
12035 operator.
12036
12037 The next several functions are involved in this lifetime extension. */
12038
12039 /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
12040 reference is being bound to a temporary. Create and return a new
12041 VAR_DECL with the indicated TYPE; this variable will store the value to
12042 which the reference is bound. */
12043
12044 tree
12045 make_temporary_var_for_ref_to_temp (tree decl, tree type)
12046 {
12047 tree var = create_temporary_var (type);
12048
12049 /* Register the variable. */
12050 if (VAR_P (decl)
12051 && (TREE_STATIC (decl) || CP_DECL_THREAD_LOCAL_P (decl)))
12052 {
12053 /* Namespace-scope or local static; give it a mangled name. */
12054
12055 /* If an initializer is visible to multiple translation units, those
12056 translation units must agree on the addresses of the
12057 temporaries. Therefore the temporaries must be given a consistent name
12058 and vague linkage. The mangled name of a temporary is the name of the
12059 non-temporary object in whose initializer they appear, prefixed with
12060 GR and suffixed with a sequence number mangled using the usual rules
12061 for a seq-id. Temporaries are numbered with a pre-order, depth-first,
12062 left-to-right walk of the complete initializer. */
12063 copy_linkage (var, decl);
12064
12065 tree name = mangle_ref_init_variable (decl);
12066 DECL_NAME (var) = name;
12067 SET_DECL_ASSEMBLER_NAME (var, name);
12068 }
12069 else
12070 /* Create a new cleanup level if necessary. */
12071 maybe_push_cleanup_level (type);
12072
12073 return pushdecl (var);
12074 }
12075
12076 /* EXPR is the initializer for a variable DECL of reference or
12077 std::initializer_list type. Create, push and return a new VAR_DECL
12078 for the initializer so that it will live as long as DECL. Any
12079 cleanup for the new variable is returned through CLEANUP, and the
12080 code to initialize the new variable is returned through INITP. */
12081
12082 static tree
12083 set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
12084 tree *initp, tree *cond_guard)
12085 {
12086 tree init;
12087 tree type;
12088 tree var;
12089
12090 /* Create the temporary variable. */
12091 type = TREE_TYPE (expr);
12092 var = make_temporary_var_for_ref_to_temp (decl, type);
12093 layout_decl (var, 0);
12094 /* If the rvalue is the result of a function call it will be
12095 a TARGET_EXPR. If it is some other construct (such as a
12096 member access expression where the underlying object is
12097 itself the result of a function call), turn it into a
12098 TARGET_EXPR here. It is important that EXPR be a
12099 TARGET_EXPR below since otherwise the INIT_EXPR will
12100 attempt to make a bitwise copy of EXPR to initialize
12101 VAR. */
12102 if (TREE_CODE (expr) != TARGET_EXPR)
12103 expr = get_target_expr (expr);
12104
12105 if (TREE_CODE (decl) == FIELD_DECL
12106 && extra_warnings && !TREE_NO_WARNING (decl))
12107 {
12108 warning (OPT_Wextra, "a temporary bound to %qD only persists "
12109 "until the constructor exits", decl);
12110 TREE_NO_WARNING (decl) = true;
12111 }
12112
12113 /* Recursively extend temps in this initializer. */
12114 TARGET_EXPR_INITIAL (expr)
12115 = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups,
12116 cond_guard);
12117
12118 /* Any reference temp has a non-trivial initializer. */
12119 DECL_NONTRIVIALLY_INITIALIZED_P (var) = true;
12120
12121 /* If the initializer is constant, put it in DECL_INITIAL so we get
12122 static initialization and use in constant expressions. */
12123 init = maybe_constant_init (expr);
12124 /* As in store_init_value. */
12125 init = cp_fully_fold (init);
12126 if (TREE_CONSTANT (init))
12127 {
12128 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
12129 {
12130 /* 5.19 says that a constant expression can include an
12131 lvalue-rvalue conversion applied to "a glvalue of literal type
12132 that refers to a non-volatile temporary object initialized
12133 with a constant expression". Rather than try to communicate
12134 that this VAR_DECL is a temporary, just mark it constexpr. */
12135 DECL_DECLARED_CONSTEXPR_P (var) = true;
12136 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
12137 TREE_CONSTANT (var) = true;
12138 TREE_READONLY (var) = true;
12139 }
12140 DECL_INITIAL (var) = init;
12141 init = NULL_TREE;
12142 }
12143 else
12144 /* Create the INIT_EXPR that will initialize the temporary
12145 variable. */
12146 init = split_nonconstant_init (var, expr);
12147 if (at_function_scope_p ())
12148 {
12149 add_decl_expr (var);
12150
12151 if (TREE_STATIC (var))
12152 init = add_stmt_to_compound (init, register_dtor_fn (var));
12153 else
12154 {
12155 tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
12156 if (cleanup)
12157 {
12158 if (cond_guard && cleanup != error_mark_node)
12159 {
12160 if (*cond_guard == NULL_TREE)
12161 {
12162 *cond_guard = build_local_temp (boolean_type_node);
12163 add_decl_expr (*cond_guard);
12164 tree set = cp_build_modify_expr (UNKNOWN_LOCATION,
12165 *cond_guard, NOP_EXPR,
12166 boolean_false_node,
12167 tf_warning_or_error);
12168 finish_expr_stmt (set);
12169 }
12170 cleanup = build3 (COND_EXPR, void_type_node,
12171 *cond_guard, cleanup, NULL_TREE);
12172 }
12173 vec_safe_push (*cleanups, cleanup);
12174 }
12175 }
12176
12177 /* We must be careful to destroy the temporary only
12178 after its initialization has taken place. If the
12179 initialization throws an exception, then the
12180 destructor should not be run. We cannot simply
12181 transform INIT into something like:
12182
12183 (INIT, ({ CLEANUP_STMT; }))
12184
12185 because emit_local_var always treats the
12186 initializer as a full-expression. Thus, the
12187 destructor would run too early; it would run at the
12188 end of initializing the reference variable, rather
12189 than at the end of the block enclosing the
12190 reference variable.
12191
12192 The solution is to pass back a cleanup expression
12193 which the caller is responsible for attaching to
12194 the statement tree. */
12195 }
12196 else
12197 {
12198 rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
12199 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
12200 {
12201 if (CP_DECL_THREAD_LOCAL_P (var))
12202 tls_aggregates = tree_cons (NULL_TREE, var,
12203 tls_aggregates);
12204 else
12205 static_aggregates = tree_cons (NULL_TREE, var,
12206 static_aggregates);
12207 }
12208 else
12209 /* Check whether the dtor is callable. */
12210 cxx_maybe_build_cleanup (var, tf_warning_or_error);
12211 }
12212 /* Avoid -Wunused-variable warning (c++/38958). */
12213 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
12214 && VAR_P (decl))
12215 TREE_USED (decl) = DECL_READ_P (decl) = true;
12216
12217 *initp = init;
12218 return var;
12219 }
12220
12221 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
12222 initializing a variable of that TYPE. */
12223
12224 tree
12225 initialize_reference (tree type, tree expr,
12226 int flags, tsubst_flags_t complain)
12227 {
12228 conversion *conv;
12229 void *p;
12230 location_t loc = cp_expr_loc_or_input_loc (expr);
12231
12232 if (type == error_mark_node || error_operand_p (expr))
12233 return error_mark_node;
12234
12235 /* Get the high-water mark for the CONVERSION_OBSTACK. */
12236 p = conversion_obstack_alloc (0);
12237
12238 conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
12239 flags, complain);
12240 /* If this conversion failed, we're in C++20, and we have something like
12241 A& a(b) where A is an aggregate, try again, this time as A& a{b}. */
12242 if ((!conv || conv->bad_p)
12243 && (flags & LOOKUP_AGGREGATE_PAREN_INIT))
12244 {
12245 tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
12246 CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
12247 CONSTRUCTOR_IS_PAREN_INIT (e) = true;
12248 conversion *c = reference_binding (type, TREE_TYPE (e), e,
12249 /*c_cast_p=*/false, flags, complain);
12250 /* If this worked, use it. */
12251 if (c && !c->bad_p)
12252 expr = e, conv = c;
12253 }
12254 if (!conv || conv->bad_p)
12255 {
12256 if (complain & tf_error)
12257 {
12258 if (conv)
12259 convert_like (conv, expr, complain);
12260 else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
12261 && !TYPE_REF_IS_RVALUE (type)
12262 && !lvalue_p (expr))
12263 error_at (loc, "invalid initialization of non-const reference of "
12264 "type %qH from an rvalue of type %qI",
12265 type, TREE_TYPE (expr));
12266 else
12267 error_at (loc, "invalid initialization of reference of type "
12268 "%qH from expression of type %qI", type,
12269 TREE_TYPE (expr));
12270 }
12271 return error_mark_node;
12272 }
12273
12274 if (conv->kind == ck_ref_bind)
12275 /* Perform the conversion. */
12276 expr = convert_like (conv, expr, complain);
12277 else if (conv->kind == ck_ambig)
12278 /* We gave an error in build_user_type_conversion_1. */
12279 expr = error_mark_node;
12280 else
12281 gcc_unreachable ();
12282
12283 /* Free all the conversions we allocated. */
12284 obstack_free (&conversion_obstack, p);
12285
12286 return expr;
12287 }
12288
12289 /* If *P is an xvalue expression, prevent temporary lifetime extension if it
12290 gets used to initialize a reference. */
12291
12292 static tree
12293 prevent_lifetime_extension (tree t)
12294 {
12295 tree *p = &t;
12296 while (TREE_CODE (*p) == COMPOUND_EXPR)
12297 p = &TREE_OPERAND (*p, 1);
12298 while (handled_component_p (*p))
12299 p = &TREE_OPERAND (*p, 0);
12300 /* Change a TARGET_EXPR from prvalue to xvalue. */
12301 if (TREE_CODE (*p) == TARGET_EXPR)
12302 *p = build2 (COMPOUND_EXPR, TREE_TYPE (*p), *p,
12303 move (TARGET_EXPR_SLOT (*p)));
12304 return t;
12305 }
12306
12307 /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
12308 which is bound either to a reference or a std::initializer_list. */
12309
12310 static tree
12311 extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups,
12312 tree *cond_guard)
12313 {
12314 tree sub = init;
12315 tree *p;
12316 STRIP_NOPS (sub);
12317 if (TREE_CODE (sub) == COMPOUND_EXPR)
12318 {
12319 TREE_OPERAND (sub, 1)
12320 = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups,
12321 cond_guard);
12322 return init;
12323 }
12324 if (TREE_CODE (sub) == COND_EXPR)
12325 {
12326 tree cur_cond_guard = NULL_TREE;
12327 if (TREE_OPERAND (sub, 1))
12328 TREE_OPERAND (sub, 1)
12329 = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups,
12330 &cur_cond_guard);
12331 if (cur_cond_guard)
12332 {
12333 tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard,
12334 NOP_EXPR, boolean_true_node,
12335 tf_warning_or_error);
12336 TREE_OPERAND (sub, 1)
12337 = cp_build_compound_expr (set, TREE_OPERAND (sub, 1),
12338 tf_warning_or_error);
12339 }
12340 cur_cond_guard = NULL_TREE;
12341 if (TREE_OPERAND (sub, 2))
12342 TREE_OPERAND (sub, 2)
12343 = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 2), cleanups,
12344 &cur_cond_guard);
12345 if (cur_cond_guard)
12346 {
12347 tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard,
12348 NOP_EXPR, boolean_true_node,
12349 tf_warning_or_error);
12350 TREE_OPERAND (sub, 2)
12351 = cp_build_compound_expr (set, TREE_OPERAND (sub, 2),
12352 tf_warning_or_error);
12353 }
12354 return init;
12355 }
12356 if (TREE_CODE (sub) != ADDR_EXPR)
12357 return init;
12358 /* Deal with binding to a subobject. */
12359 for (p = &TREE_OPERAND (sub, 0);
12360 TREE_CODE (*p) == COMPONENT_REF || TREE_CODE (*p) == ARRAY_REF; )
12361 p = &TREE_OPERAND (*p, 0);
12362 if (TREE_CODE (*p) == TARGET_EXPR)
12363 {
12364 tree subinit = NULL_TREE;
12365 *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit, cond_guard);
12366 recompute_tree_invariant_for_addr_expr (sub);
12367 if (init != sub)
12368 init = fold_convert (TREE_TYPE (init), sub);
12369 if (subinit)
12370 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
12371 }
12372 return init;
12373 }
12374
12375 /* INIT is part of the initializer for DECL. If there are any
12376 reference or initializer lists being initialized, extend their
12377 lifetime to match that of DECL. */
12378
12379 tree
12380 extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups,
12381 tree *cond_guard)
12382 {
12383 tree type = TREE_TYPE (init);
12384 if (processing_template_decl)
12385 return init;
12386 if (TYPE_REF_P (type))
12387 init = extend_ref_init_temps_1 (decl, init, cleanups, cond_guard);
12388 else
12389 {
12390 tree ctor = init;
12391 if (TREE_CODE (ctor) == TARGET_EXPR)
12392 ctor = TARGET_EXPR_INITIAL (ctor);
12393 if (TREE_CODE (ctor) == CONSTRUCTOR)
12394 {
12395 /* [dcl.init] When initializing an aggregate from a parenthesized list
12396 of values... a temporary object bound to a reference does not have
12397 its lifetime extended. */
12398 if (CONSTRUCTOR_IS_PAREN_INIT (ctor))
12399 return init;
12400
12401 if (is_std_init_list (type))
12402 {
12403 /* The temporary array underlying a std::initializer_list
12404 is handled like a reference temporary. */
12405 tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
12406 array = extend_ref_init_temps_1 (decl, array, cleanups,
12407 cond_guard);
12408 CONSTRUCTOR_ELT (ctor, 0)->value = array;
12409 }
12410 else
12411 {
12412 unsigned i;
12413 constructor_elt *p;
12414 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
12415 FOR_EACH_VEC_SAFE_ELT (elts, i, p)
12416 p->value = extend_ref_init_temps (decl, p->value, cleanups,
12417 cond_guard);
12418 }
12419 recompute_constructor_flags (ctor);
12420 if (decl_maybe_constant_var_p (decl) && TREE_CONSTANT (ctor))
12421 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
12422 }
12423 }
12424
12425 return init;
12426 }
12427
12428 /* Returns true iff an initializer for TYPE could contain temporaries that
12429 need to be extended because they are bound to references or
12430 std::initializer_list. */
12431
12432 bool
12433 type_has_extended_temps (tree type)
12434 {
12435 type = strip_array_types (type);
12436 if (TYPE_REF_P (type))
12437 return true;
12438 if (CLASS_TYPE_P (type))
12439 {
12440 if (is_std_init_list (type))
12441 return true;
12442 for (tree f = next_initializable_field (TYPE_FIELDS (type));
12443 f; f = next_initializable_field (DECL_CHAIN (f)))
12444 if (type_has_extended_temps (TREE_TYPE (f)))
12445 return true;
12446 }
12447 return false;
12448 }
12449
12450 /* Returns true iff TYPE is some variant of std::initializer_list. */
12451
12452 bool
12453 is_std_init_list (tree type)
12454 {
12455 if (!TYPE_P (type))
12456 return false;
12457 if (cxx_dialect == cxx98)
12458 return false;
12459 /* Look through typedefs. */
12460 type = TYPE_MAIN_VARIANT (type);
12461 return (CLASS_TYPE_P (type)
12462 && CP_TYPE_CONTEXT (type) == std_node
12463 && init_list_identifier == DECL_NAME (TYPE_NAME (type)));
12464 }
12465
12466 /* Returns true iff DECL is a list constructor: i.e. a constructor which
12467 will accept an argument list of a single std::initializer_list<T>. */
12468
12469 bool
12470 is_list_ctor (tree decl)
12471 {
12472 tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
12473 tree arg;
12474
12475 if (!args || args == void_list_node)
12476 return false;
12477
12478 arg = non_reference (TREE_VALUE (args));
12479 if (!is_std_init_list (arg))
12480 return false;
12481
12482 args = TREE_CHAIN (args);
12483
12484 if (args && args != void_list_node && !TREE_PURPOSE (args))
12485 /* There are more non-defaulted parms. */
12486 return false;
12487
12488 return true;
12489 }
12490
12491 #include "gt-cp-call.h"