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