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