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