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