Line data Source code
1 : /* Functions related to invoking -*- C++ -*- methods and overloaded functions.
2 : Copyright (C) 1987-2026 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 "decl.h"
45 : #include "c-family/c-type-mismatch.h"
46 : #include "tristate.h"
47 : #include "tree-pretty-print-markup.h"
48 : #include "contracts.h" // maybe_contract_wrap_call
49 :
50 : /* The various kinds of conversion. */
51 :
52 : enum conversion_kind {
53 : ck_identity,
54 : ck_lvalue,
55 : ck_fnptr,
56 : ck_qual,
57 : ck_std,
58 : ck_ptr,
59 : ck_pmem,
60 : ck_base,
61 : ck_ref_bind,
62 : ck_user,
63 : ck_ambig,
64 : ck_list,
65 : ck_aggr,
66 : ck_rvalue,
67 : /* When LOOKUP_SHORTCUT_BAD_CONVS is set, we may return a conversion of
68 : this kind whenever we know the true conversion is either bad or outright
69 : invalid, but we don't want to attempt to compute the bad conversion (for
70 : sake of avoiding unnecessary instantiation). bad_p should always be set
71 : for these. */
72 : ck_deferred_bad,
73 : };
74 :
75 : /* The rank of the conversion. Order of the enumerals matters; better
76 : conversions should come earlier in the list. */
77 :
78 : enum conversion_rank {
79 : cr_identity,
80 : cr_exact,
81 : cr_promotion,
82 : cr_std,
83 : cr_pbool,
84 : cr_user,
85 : cr_ellipsis,
86 : cr_bad
87 : };
88 :
89 : /* An implicit conversion sequence, in the sense of [over.best.ics].
90 : The first conversion to be performed is at the end of the chain.
91 : That conversion is always a cr_identity conversion. */
92 :
93 : struct conversion {
94 : /* The kind of conversion represented by this step. */
95 : conversion_kind kind;
96 : /* The rank of this conversion. */
97 : conversion_rank rank;
98 : BOOL_BITFIELD user_conv_p : 1;
99 : BOOL_BITFIELD ellipsis_p : 1;
100 : BOOL_BITFIELD this_p : 1;
101 : /* True if this conversion would be permitted with a bending of
102 : language standards, e.g. disregarding pointer qualifiers or
103 : converting integers to pointers. */
104 : BOOL_BITFIELD bad_p : 1;
105 : /* If KIND is ck_ref_bind or ck_base, true to indicate that a
106 : temporary should be created to hold the result of the
107 : conversion. If KIND is ck_ambig or ck_user, true means force
108 : copy-initialization. */
109 : BOOL_BITFIELD need_temporary_p : 1;
110 : /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion
111 : from a pointer-to-derived to pointer-to-base is being performed. */
112 : BOOL_BITFIELD base_p : 1;
113 : /* If KIND is ck_ref_bind, true when either an lvalue reference is
114 : being bound to an lvalue expression or an rvalue reference is
115 : being bound to an rvalue expression. If KIND is ck_rvalue or ck_base,
116 : true when we are treating an lvalue as an rvalue (12.8p33). If
117 : ck_identity, we will be binding a reference directly or decaying to
118 : a pointer. */
119 : BOOL_BITFIELD rvaluedness_matches_p: 1;
120 : BOOL_BITFIELD check_narrowing: 1;
121 : /* Whether check_narrowing should only check TREE_CONSTANTs; used
122 : in build_converted_constant_expr. */
123 : BOOL_BITFIELD check_narrowing_const_only: 1;
124 : /* True if this conversion is taking place in a copy-initialization context
125 : and we should only consider converting constructors. Only set in
126 : ck_base and ck_rvalue. */
127 : BOOL_BITFIELD copy_init_p : 1;
128 : /* The type of the expression resulting from the conversion. */
129 : tree type;
130 : union {
131 : /* The next conversion in the chain. Since the conversions are
132 : arranged from outermost to innermost, the NEXT conversion will
133 : actually be performed before this conversion. This variant is
134 : used only when KIND is neither ck_identity, ck_aggr, ck_ambig nor
135 : ck_list. Please use the next_conversion function instead
136 : of using this field directly. */
137 : conversion *next;
138 : /* The expression at the beginning of the conversion chain. This
139 : variant is used only if KIND is ck_identity, ck_aggr, or ck_ambig.
140 : You can use conv_get_original_expr to get this expression. */
141 : tree expr;
142 : /* The array of conversions for an initializer_list, so this
143 : variant is used only when KIN D is ck_list. */
144 : conversion **list;
145 : } u;
146 : /* The function candidate corresponding to this conversion
147 : sequence. This field is only used if KIND is ck_user. */
148 : struct z_candidate *cand;
149 : };
150 :
151 : #define CONVERSION_RANK(NODE) \
152 : ((NODE)->bad_p ? cr_bad \
153 : : (NODE)->ellipsis_p ? cr_ellipsis \
154 : : (NODE)->user_conv_p ? cr_user \
155 : : (NODE)->rank)
156 :
157 : #define BAD_CONVERSION_RANK(NODE) \
158 : ((NODE)->ellipsis_p ? cr_ellipsis \
159 : : (NODE)->user_conv_p ? cr_user \
160 : : (NODE)->rank)
161 :
162 : static struct obstack conversion_obstack;
163 : static bool conversion_obstack_initialized;
164 : struct rejection_reason;
165 :
166 : static struct z_candidate * tourney (struct z_candidate *, tsubst_flags_t);
167 : static int equal_functions (tree, tree);
168 : static int joust (struct z_candidate *, struct z_candidate *, bool,
169 : tsubst_flags_t);
170 : static int compare_ics (conversion *, conversion *);
171 : static void maybe_warn_class_memaccess (location_t, tree,
172 : const vec<tree, va_gc> *);
173 : static tree build_over_call (struct z_candidate *, int, tsubst_flags_t);
174 : static tree convert_like (conversion *, tree, tsubst_flags_t);
175 : static tree convert_like_with_context (conversion *, tree, tree, int,
176 : tsubst_flags_t);
177 : static void op_error (const op_location_t &, enum tree_code, enum tree_code,
178 : tree, tree, tree, bool);
179 : static struct z_candidate *build_user_type_conversion_1 (tree, tree, int,
180 : tsubst_flags_t);
181 : static void print_z_candidate (location_t, const char *, struct z_candidate *);
182 : static void print_z_candidates (location_t, struct z_candidate *,
183 : tristate = tristate::unknown ());
184 : static tree build_this (tree);
185 : static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
186 : static bool any_strictly_viable (struct z_candidate *);
187 : static struct z_candidate *add_template_candidate
188 : (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
189 : tree, tree, tree, int, unification_kind_t, bool, tsubst_flags_t);
190 : static struct z_candidate *add_template_candidate_real
191 : (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
192 : tree, tree, tree, int, tree, unification_kind_t, bool, tsubst_flags_t);
193 : static bool is_complete (tree);
194 : static struct z_candidate *add_conv_candidate
195 : (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, tree,
196 : tree, tsubst_flags_t);
197 : static struct z_candidate *add_function_candidate
198 : (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *, tree,
199 : tree, int, conversion**, bool, tsubst_flags_t);
200 : static conversion *implicit_conversion (tree, tree, tree, bool, int,
201 : tsubst_flags_t);
202 : static conversion *reference_binding (tree, tree, tree, bool, int,
203 : tsubst_flags_t);
204 : static conversion *build_conv (conversion_kind, tree, conversion *);
205 : static conversion *build_list_conv (tree, tree, int, tsubst_flags_t);
206 : static conversion *next_conversion (conversion *);
207 : static bool is_subseq (conversion *, conversion *);
208 : static conversion *maybe_handle_ref_bind (conversion **);
209 : static void maybe_handle_implicit_object (conversion **);
210 : static struct z_candidate *add_candidate
211 : (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, size_t,
212 : conversion **, tree, tree, int, struct rejection_reason *, int);
213 : static tree source_type (conversion *);
214 : static void add_warning (struct z_candidate *, struct z_candidate *);
215 : static conversion *direct_reference_binding (tree, conversion *);
216 : static bool promoted_arithmetic_type_p (tree);
217 : static conversion *conditional_conversion (tree, tree, tsubst_flags_t);
218 : static char *name_as_c_string (tree, tree, bool *);
219 : static tree prep_operand (tree);
220 : static void add_candidates (tree, tree, const vec<tree, va_gc> *, tree, tree,
221 : bool, tree, tree, int, struct z_candidate **,
222 : tsubst_flags_t);
223 : static conversion *merge_conversion_sequences (conversion *, conversion *);
224 : static tree build_temp (tree, tree, int, enum diagnostics::kind *,
225 : tsubst_flags_t);
226 : static conversion *build_identity_conv (tree, tree);
227 : static inline bool conv_binds_to_array_of_unknown_bound (conversion *);
228 : static bool conv_is_prvalue (conversion *);
229 : static tree prevent_lifetime_extension (tree);
230 :
231 : /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
232 : NAME can take many forms... */
233 :
234 : bool
235 3065135 : check_dtor_name (tree basetype, tree name)
236 : {
237 : /* Just accept something we've already complained about. */
238 3065135 : if (name == error_mark_node)
239 : return true;
240 :
241 3065135 : if (TREE_CODE (name) == TYPE_DECL)
242 84 : name = TREE_TYPE (name);
243 3065051 : else if (TYPE_P (name))
244 : /* OK */;
245 26 : else if (identifier_p (name))
246 : {
247 26 : if ((MAYBE_CLASS_TYPE_P (basetype)
248 0 : || TREE_CODE (basetype) == ENUMERAL_TYPE)
249 26 : && name == constructor_name (basetype))
250 : return true;
251 :
252 : /* Otherwise lookup the name, it could be an unrelated typedef
253 : of the correct type. */
254 6 : name = lookup_name (name, LOOK_want::TYPE);
255 6 : if (!name)
256 : return false;
257 3 : name = TREE_TYPE (name);
258 3 : if (name == error_mark_node)
259 : return false;
260 : }
261 : else
262 : {
263 : /* In the case of:
264 :
265 : template <class T> struct S { ~S(); };
266 : int i;
267 : i.~S();
268 :
269 : NAME will be a class template. */
270 0 : gcc_assert (DECL_CLASS_TEMPLATE_P (name));
271 : return false;
272 : }
273 :
274 3065109 : return same_type_p (TYPE_MAIN_VARIANT (basetype), TYPE_MAIN_VARIANT (name));
275 : }
276 :
277 : /* We want the address of a function or method. We avoid creating a
278 : pointer-to-member function. */
279 :
280 : tree
281 323753752 : build_addr_func (tree function, tsubst_flags_t complain)
282 : {
283 323753752 : tree type = TREE_TYPE (function);
284 :
285 : /* We have to do these by hand to avoid real pointer to member
286 : functions. */
287 323753752 : if (TREE_CODE (type) == METHOD_TYPE)
288 : {
289 51207544 : if (TREE_CODE (function) == OFFSET_REF)
290 : {
291 72 : tree object = build_address (TREE_OPERAND (function, 0));
292 72 : return get_member_function_from_ptrfunc (&object,
293 72 : TREE_OPERAND (function, 1),
294 : complain);
295 : }
296 51207472 : function = build_address (function);
297 : }
298 272546208 : else if (TREE_CODE (function) == FUNCTION_DECL
299 388660729 : && DECL_IMMEDIATE_FUNCTION_P (function))
300 1598662 : function = build_address (function);
301 : else
302 270947546 : function = decay_conversion (function, complain, /*reject_builtin=*/false);
303 :
304 : return function;
305 : }
306 :
307 : /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
308 : POINTER_TYPE to those. Note, pointer to member function types
309 : (TYPE_PTRMEMFUNC_P) must be handled by our callers. There are
310 : two variants. build_call_a is the primitive taking an array of
311 : arguments, while build_call_n is a wrapper that handles varargs. */
312 :
313 : tree
314 162391 : build_call_n (tree function, int n, ...)
315 : {
316 162391 : if (n == 0)
317 0 : return build_call_a (function, 0, NULL);
318 : else
319 : {
320 162391 : tree *argarray = XALLOCAVEC (tree, n);
321 162391 : va_list ap;
322 162391 : int i;
323 :
324 162391 : va_start (ap, n);
325 325520 : for (i = 0; i < n; i++)
326 163129 : argarray[i] = va_arg (ap, tree);
327 162391 : va_end (ap);
328 162391 : return build_call_a (function, n, argarray);
329 : }
330 : }
331 :
332 : /* Update various flags in cfun and the call itself based on what is being
333 : called. Split out of build_call_a so that bot_manip can use it too. */
334 :
335 : void
336 155963002 : set_flags_from_callee (tree call)
337 : {
338 : /* Handle both CALL_EXPRs and AGGR_INIT_EXPRs. */
339 155963002 : tree decl = cp_get_callee_fndecl_nofold (call);
340 :
341 : /* We check both the decl and the type; a function may be known not to
342 : throw without being declared throw(). */
343 155963002 : bool nothrow = decl && TREE_NOTHROW (decl);
344 155963002 : tree callee = cp_get_callee (call);
345 155963002 : if (callee)
346 155962994 : nothrow |= TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (callee)));
347 8 : else if (TREE_CODE (call) == CALL_EXPR
348 8 : && internal_fn_flags (CALL_EXPR_IFN (call)) & ECF_NOTHROW)
349 : nothrow = true;
350 :
351 155963002 : if (cfun && cp_function_chain && !cp_unevaluated_operand)
352 : {
353 94415575 : if (!nothrow && at_function_scope_p ())
354 23203005 : cp_function_chain->can_throw = 1;
355 :
356 94415575 : if (decl && TREE_THIS_VOLATILE (decl))
357 2870437 : current_function_returns_abnormally = 1;
358 : }
359 :
360 155963002 : TREE_NOTHROW (call) = nothrow;
361 155963002 : }
362 :
363 : tree
364 151918115 : build_call_a (tree function, int n, tree *argarray)
365 : {
366 151918115 : tree decl;
367 151918115 : tree result_type;
368 151918115 : tree fntype;
369 151918115 : int i;
370 :
371 151918115 : function = build_addr_func (function, tf_warning_or_error);
372 :
373 151918115 : gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
374 151918115 : fntype = TREE_TYPE (TREE_TYPE (function));
375 151918115 : gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype));
376 151918115 : result_type = TREE_TYPE (fntype);
377 : /* An rvalue has no cv-qualifiers. */
378 151918115 : if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
379 90063486 : result_type = cv_unqualified (result_type);
380 :
381 151918115 : function = build_call_array_loc (input_location,
382 : result_type, function, n, argarray);
383 151918115 : set_flags_from_callee (function);
384 :
385 151918115 : decl = get_callee_fndecl (function);
386 :
387 151918115 : if (decl && !TREE_USED (decl))
388 : {
389 : /* We invoke build_call directly for several library
390 : functions. These may have been declared normally if
391 : we're building libgcc, so we can't just check
392 : DECL_ARTIFICIAL. */
393 191686 : gcc_assert (DECL_ARTIFICIAL (decl)
394 : || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
395 : "__", 2));
396 191686 : mark_used (decl);
397 : }
398 :
399 151918115 : require_complete_eh_spec_types (fntype, decl);
400 :
401 437381658 : TREE_HAS_CONSTRUCTOR (function) = (decl && DECL_CONSTRUCTOR_P (decl));
402 :
403 : /* Don't pass empty class objects by value. This is useful
404 : for tags in STL, which are used to control overload resolution.
405 : We don't need to handle other cases of copying empty classes. */
406 151918115 : if (!decl || !fndecl_built_in_p (decl))
407 308179835 : for (i = 0; i < n; i++)
408 : {
409 163601849 : tree arg = CALL_EXPR_ARG (function, i);
410 163601849 : if (is_empty_class (TREE_TYPE (arg))
411 163601849 : && simple_empty_class_p (TREE_TYPE (arg), arg, INIT_EXPR))
412 : {
413 5226928 : while (TREE_CODE (arg) == TARGET_EXPR)
414 : /* We're disconnecting the initializer from its target,
415 : don't create a temporary. */
416 2612508 : arg = TARGET_EXPR_INITIAL (arg);
417 2614420 : suppress_warning (arg, OPT_Wunused_result);
418 2614420 : tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg));
419 2614420 : arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t);
420 2614420 : CALL_EXPR_ARG (function, i) = arg;
421 : }
422 : }
423 :
424 151918115 : return function;
425 : }
426 :
427 : /* New overloading code. */
428 :
429 : struct z_candidate;
430 :
431 : struct candidate_warning {
432 : z_candidate *loser;
433 : candidate_warning *next;
434 : };
435 :
436 : /* Information for providing diagnostics about why overloading failed. */
437 :
438 : enum rejection_reason_code {
439 : rr_none,
440 : rr_arity,
441 : rr_explicit_conversion,
442 : rr_template_conversion,
443 : rr_arg_conversion,
444 : rr_bad_arg_conversion,
445 : rr_template_unification,
446 : rr_invalid_copy,
447 : rr_inherited_ctor,
448 : rr_constraint_failure,
449 : rr_ignored,
450 : };
451 :
452 : struct conversion_info {
453 : /* The index of the argument, 0-based. */
454 : int n_arg;
455 : /* The actual argument or its type. */
456 : tree from;
457 : /* The type of the parameter. */
458 : tree to_type;
459 : /* The location of the argument. */
460 : location_t loc;
461 : };
462 :
463 : struct rejection_reason {
464 : enum rejection_reason_code code;
465 : union {
466 : /* Information about an arity mismatch. */
467 : struct {
468 : /* The expected number of arguments. */
469 : int expected;
470 : /* The actual number of arguments in the call. */
471 : int actual;
472 : /* Whether EXPECTED should be treated as a lower bound. */
473 : bool least_p;
474 : } arity;
475 : /* Information about an argument conversion mismatch. */
476 : struct conversion_info conversion;
477 : /* Same, but for bad argument conversions. */
478 : struct conversion_info bad_conversion;
479 : /* Information about template unification failures. These are the
480 : parameters passed to fn_type_unification. */
481 : struct {
482 : tree tmpl;
483 : tree explicit_targs;
484 : int num_targs;
485 : const tree *args;
486 : unsigned int nargs;
487 : tree return_type;
488 : unification_kind_t strict;
489 : int flags;
490 : } template_unification;
491 : /* Information about template instantiation failures. These are the
492 : parameters passed to instantiate_template. */
493 : struct {
494 : tree tmpl;
495 : tree targs;
496 : } template_instantiation;
497 : } u;
498 : };
499 :
500 : struct z_candidate {
501 : /* The FUNCTION_DECL that will be called if this candidate is
502 : selected by overload resolution. */
503 : tree fn;
504 : /* If not NULL_TREE, the first argument to use when calling this
505 : function. */
506 : tree first_arg;
507 : /* The rest of the arguments to use when calling this function. If
508 : there are no further arguments this may be NULL or it may be an
509 : empty vector. */
510 : const vec<tree, va_gc> *args;
511 : /* The implicit conversion sequences for each of the arguments to
512 : FN. */
513 : conversion **convs;
514 : /* The number of implicit conversion sequences. */
515 : size_t num_convs;
516 : /* If FN is a user-defined conversion, the standard conversion
517 : sequence from the type returned by FN to the desired destination
518 : type. */
519 : conversion *second_conv;
520 : struct rejection_reason *reason;
521 : /* If FN is a member function, the binfo indicating the path used to
522 : qualify the name of FN at the call site. This path is used to
523 : determine whether or not FN is accessible if it is selected by
524 : overload resolution. The DECL_CONTEXT of FN will always be a
525 : (possibly improper) base of this binfo. */
526 : tree access_path;
527 : /* If FN is a non-static member function, the binfo indicating the
528 : subobject to which the `this' pointer should be converted if FN
529 : is selected by overload resolution. The type pointed to by
530 : the `this' pointer must correspond to the most derived class
531 : indicated by the CONVERSION_PATH. */
532 : tree conversion_path;
533 : tree template_decl;
534 : tree explicit_targs;
535 : candidate_warning *warnings;
536 : z_candidate *next;
537 : int viable;
538 :
539 : /* The flags active in add_candidate. */
540 : int flags;
541 :
542 62248990 : bool rewritten () const { return (flags & LOOKUP_REWRITTEN); }
543 983308850 : bool reversed () const { return (flags & LOOKUP_REVERSED); }
544 : };
545 :
546 : /* Returns true iff T is a null pointer constant in the sense of
547 : [conv.ptr]. */
548 :
549 : bool
550 121319696 : null_ptr_cst_p (tree t)
551 : {
552 121319696 : tree type = TREE_TYPE (t);
553 :
554 : /* [conv.ptr]
555 :
556 : A null pointer constant is an integer literal ([lex.icon]) with value
557 : zero or a prvalue of type std::nullptr_t. */
558 121319696 : if (NULLPTR_TYPE_P (type))
559 : return true;
560 :
561 112641743 : if (cxx_dialect >= cxx11)
562 : {
563 112258683 : STRIP_ANY_LOCATION_WRAPPER (t);
564 :
565 : /* Core issue 903 says only literal 0 is a null pointer constant. */
566 112258683 : if (TREE_CODE (t) == INTEGER_CST
567 12887504 : && !TREE_OVERFLOW (t)
568 12887504 : && TREE_CODE (type) == INTEGER_TYPE
569 12318415 : && integer_zerop (t)
570 120465475 : && !char_type_p (type))
571 : return true;
572 : }
573 383060 : else if (CP_INTEGRAL_TYPE_P (type))
574 : {
575 73537 : t = fold_non_dependent_expr (t, tf_none);
576 73537 : STRIP_NOPS (t);
577 73537 : if (integer_zerop (t) && !TREE_OVERFLOW (t))
578 : return true;
579 : }
580 :
581 : return false;
582 : }
583 :
584 : /* Returns true iff T is a null member pointer value (4.11). */
585 :
586 : bool
587 95414711 : null_member_pointer_value_p (tree t)
588 : {
589 95414711 : tree type = TREE_TYPE (t);
590 95414711 : if (!type)
591 : return false;
592 95234067 : else if (TYPE_PTRMEMFUNC_P (type))
593 1267 : return (TREE_CODE (t) == CONSTRUCTOR
594 688 : && CONSTRUCTOR_NELTS (t)
595 1952 : && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value));
596 95232800 : else if (TYPE_PTRDATAMEM_P (type))
597 5992 : return integer_all_onesp (t);
598 : else
599 : return false;
600 : }
601 :
602 : /* Returns nonzero if PARMLIST consists of only default parms,
603 : ellipsis, and/or undeduced parameter packs. */
604 :
605 : bool
606 684585139 : sufficient_parms_p (const_tree parmlist)
607 : {
608 693720677 : for (; parmlist && parmlist != void_list_node;
609 9135538 : parmlist = TREE_CHAIN (parmlist))
610 211113891 : if (!TREE_PURPOSE (parmlist)
611 211113891 : && !PACK_EXPANSION_P (TREE_VALUE (parmlist)))
612 : return false;
613 : return true;
614 : }
615 :
616 : /* Allocate N bytes of memory from the conversion obstack. The memory
617 : is zeroed before being returned. */
618 :
619 : static void *
620 7427894425 : conversion_obstack_alloc (size_t n)
621 : {
622 7427894425 : void *p;
623 7427894425 : if (!conversion_obstack_initialized)
624 : {
625 84416 : gcc_obstack_init (&conversion_obstack);
626 84416 : conversion_obstack_initialized = true;
627 : }
628 7427894425 : p = obstack_alloc (&conversion_obstack, n);
629 7427894425 : memset (p, 0, n);
630 7427894425 : return p;
631 : }
632 :
633 : /* RAII class to discard anything added to conversion_obstack. */
634 :
635 : struct conversion_obstack_sentinel
636 : {
637 : void *p;
638 2407816636 : conversion_obstack_sentinel (): p (conversion_obstack_alloc (0)) {}
639 1203894764 : ~conversion_obstack_sentinel () { obstack_free (&conversion_obstack, p); }
640 : };
641 :
642 : /* Allocate rejection reasons. */
643 :
644 : static struct rejection_reason *
645 716135200 : alloc_rejection (enum rejection_reason_code code)
646 : {
647 716135200 : struct rejection_reason *p;
648 0 : p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
649 716135200 : p->code = code;
650 716135200 : return p;
651 : }
652 :
653 : static struct rejection_reason *
654 182460565 : arity_rejection (tree first_arg, int expected, int actual, bool least_p = false)
655 : {
656 145154937 : struct rejection_reason *r = alloc_rejection (rr_arity);
657 182460565 : int adjust = first_arg != NULL_TREE;
658 182460565 : r->u.arity.expected = expected - adjust;
659 182460565 : r->u.arity.actual = actual - adjust;
660 182460565 : r->u.arity.least_p = least_p;
661 182460565 : return r;
662 : }
663 :
664 : static struct rejection_reason *
665 153335977 : arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to,
666 : location_t loc)
667 : {
668 6615800 : struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
669 153335977 : int adjust = first_arg != NULL_TREE;
670 153335977 : r->u.conversion.n_arg = n_arg - adjust;
671 153335977 : r->u.conversion.from = from;
672 153335977 : r->u.conversion.to_type = to;
673 153335977 : r->u.conversion.loc = loc;
674 153335977 : return r;
675 : }
676 :
677 : static struct rejection_reason *
678 20936827 : bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to,
679 : location_t loc)
680 : {
681 2018 : struct rejection_reason *r = alloc_rejection (rr_bad_arg_conversion);
682 20936827 : int adjust = first_arg != NULL_TREE;
683 20936827 : r->u.bad_conversion.n_arg = n_arg - adjust;
684 20936827 : r->u.bad_conversion.from = from;
685 20936827 : r->u.bad_conversion.to_type = to;
686 20936827 : r->u.bad_conversion.loc = loc;
687 20936827 : return r;
688 : }
689 :
690 : static struct rejection_reason *
691 2630 : explicit_conversion_rejection (tree from, tree to)
692 : {
693 2630 : struct rejection_reason *r = alloc_rejection (rr_explicit_conversion);
694 2630 : r->u.conversion.n_arg = 0;
695 2630 : r->u.conversion.from = from;
696 2630 : r->u.conversion.to_type = to;
697 2630 : r->u.conversion.loc = UNKNOWN_LOCATION;
698 2630 : return r;
699 : }
700 :
701 : static struct rejection_reason *
702 21 : template_conversion_rejection (tree from, tree to)
703 : {
704 21 : struct rejection_reason *r = alloc_rejection (rr_template_conversion);
705 21 : r->u.conversion.n_arg = 0;
706 21 : r->u.conversion.from = from;
707 21 : r->u.conversion.to_type = to;
708 21 : r->u.conversion.loc = UNKNOWN_LOCATION;
709 21 : return r;
710 : }
711 :
712 : static struct rejection_reason *
713 357972850 : template_unification_rejection (tree tmpl, tree explicit_targs, tree targs,
714 : const tree *args, unsigned int nargs,
715 : tree return_type, unification_kind_t strict,
716 : int flags)
717 : {
718 357972850 : size_t args_n_bytes = sizeof (*args) * nargs;
719 357972850 : tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes);
720 357972850 : struct rejection_reason *r = alloc_rejection (rr_template_unification);
721 357972850 : r->u.template_unification.tmpl = tmpl;
722 357972850 : r->u.template_unification.explicit_targs = explicit_targs;
723 357972850 : r->u.template_unification.num_targs = TREE_VEC_LENGTH (targs);
724 : /* Copy args to our own storage. */
725 357972850 : memcpy (args1, args, args_n_bytes);
726 357972850 : r->u.template_unification.args = args1;
727 357972850 : r->u.template_unification.nargs = nargs;
728 357972850 : r->u.template_unification.return_type = return_type;
729 357972850 : r->u.template_unification.strict = strict;
730 357972850 : r->u.template_unification.flags = flags;
731 357972850 : return r;
732 : }
733 :
734 : static struct rejection_reason *
735 110 : template_unification_error_rejection (void)
736 : {
737 0 : return alloc_rejection (rr_template_unification);
738 : }
739 :
740 : static struct rejection_reason *
741 667928 : invalid_copy_with_fn_template_rejection (void)
742 : {
743 0 : struct rejection_reason *r = alloc_rejection (rr_invalid_copy);
744 667928 : return r;
745 : }
746 :
747 : static struct rejection_reason *
748 599010 : inherited_ctor_rejection (void)
749 : {
750 0 : struct rejection_reason *r = alloc_rejection (rr_inherited_ctor);
751 599010 : return r;
752 : }
753 :
754 : /* Build a constraint failure record. */
755 :
756 : static struct rejection_reason *
757 159282 : constraint_failure (void)
758 : {
759 0 : struct rejection_reason *r = alloc_rejection (rr_constraint_failure);
760 159282 : return r;
761 : }
762 :
763 : /* Dynamically allocate a conversion. */
764 :
765 : static conversion *
766 2571104929 : alloc_conversion (conversion_kind kind)
767 : {
768 2571104929 : conversion *c;
769 0 : c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
770 2571104929 : c->kind = kind;
771 2571104929 : return c;
772 : }
773 :
774 : /* Make sure that all memory on the conversion obstack has been
775 : freed. */
776 :
777 : void
778 96570 : validate_conversion_obstack (void)
779 : {
780 96570 : if (conversion_obstack_initialized)
781 84085 : gcc_assert ((obstack_next_free (&conversion_obstack)
782 : == obstack_base (&conversion_obstack)));
783 96570 : }
784 :
785 : /* Dynamically allocate an array of N conversions. */
786 :
787 : static conversion **
788 956228820 : alloc_conversions (size_t n)
789 : {
790 0 : return (conversion **) conversion_obstack_alloc (n * sizeof (conversion *));
791 : }
792 :
793 : /* True iff the active member of conversion::u for code CODE is NEXT. */
794 :
795 : static inline bool
796 1407476924 : has_next (conversion_kind code)
797 : {
798 1324599316 : return !(code == ck_identity
799 1407476924 : || code == ck_ambig
800 : || code == ck_list
801 1324663906 : || code == ck_aggr
802 1407476924 : || code == ck_deferred_bad);
803 : }
804 :
805 : static conversion *
806 760479595 : build_conv (conversion_kind code, tree type, conversion *from)
807 : {
808 760479595 : conversion *t;
809 760479595 : conversion_rank rank = CONVERSION_RANK (from);
810 :
811 : /* Only call this function for conversions that use u.next. */
812 760479595 : gcc_assert (from == NULL || has_next (code));
813 :
814 : /* Note that the caller is responsible for filling in t->cand for
815 : user-defined conversions. */
816 760479595 : t = alloc_conversion (code);
817 760479595 : t->type = type;
818 760479595 : t->u.next = from;
819 :
820 760479595 : switch (code)
821 : {
822 225424946 : case ck_ptr:
823 225424946 : case ck_pmem:
824 225424946 : case ck_base:
825 225424946 : case ck_std:
826 225424946 : if (rank < cr_std)
827 760479595 : rank = cr_std;
828 : break;
829 :
830 51594353 : case ck_qual:
831 51594353 : case ck_fnptr:
832 51594353 : if (rank < cr_exact)
833 760479595 : rank = cr_exact;
834 : break;
835 :
836 : default:
837 : break;
838 : }
839 760479595 : t->rank = rank;
840 760479595 : t->user_conv_p = (code == ck_user || from->user_conv_p);
841 760479595 : t->bad_p = from->bad_p;
842 760479595 : t->base_p = false;
843 760479595 : return t;
844 : }
845 :
846 : /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
847 : specialization of std::initializer_list<T>, if such a conversion is
848 : possible. */
849 :
850 : static conversion *
851 82841 : build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
852 : {
853 82841 : tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
854 82841 : unsigned len = CONSTRUCTOR_NELTS (ctor);
855 82841 : conversion **subconvs = alloc_conversions (len);
856 82841 : conversion *t;
857 82841 : unsigned i;
858 82841 : tree val;
859 :
860 : /* Within a list-initialization we can have more user-defined
861 : conversions. */
862 82841 : flags &= ~LOOKUP_NO_CONVERSION;
863 : /* But no narrowing conversions. */
864 82841 : flags |= LOOKUP_NO_NARROWING;
865 :
866 : /* Can't make an array of these types. */
867 82841 : if (TYPE_REF_P (elttype)
868 82832 : || TREE_CODE (elttype) == FUNCTION_TYPE
869 82832 : || VOID_TYPE_P (elttype))
870 : return NULL;
871 :
872 286134 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
873 : {
874 214797 : if (TREE_CODE (val) == RAW_DATA_CST)
875 : {
876 51 : tree elt
877 51 : = build_int_cst (TREE_TYPE (val), RAW_DATA_UCHAR_ELT (val, 0));
878 51 : conversion *sub
879 51 : = implicit_conversion (elttype, TREE_TYPE (val), elt,
880 : false, flags, complain);
881 51 : conversion *next;
882 51 : if (sub == NULL)
883 : return NULL;
884 : /* For conversion to initializer_list<unsigned char> or
885 : initializer_list<char> or initializer_list<signed char>
886 : we can optimize and keep RAW_DATA_CST with adjusted
887 : type if we report narrowing errors if needed.
888 : Use just one subconversion for that case. */
889 69 : if (sub->kind == ck_std
890 24 : && sub->type
891 24 : && (TREE_CODE (sub->type) == INTEGER_TYPE
892 6 : || is_byte_access_type (sub->type))
893 18 : && TYPE_PRECISION (sub->type) == CHAR_BIT
894 18 : && (next = next_conversion (sub))
895 69 : && next->kind == ck_identity)
896 : {
897 18 : subconvs[i] = sub;
898 18 : continue;
899 : }
900 : /* Otherwise. build separate subconv for each RAW_DATA_CST
901 : byte. Wrap those into an artificial ck_list which convert_like
902 : will then handle. */
903 33 : conversion **subsubconvs = alloc_conversions (RAW_DATA_LENGTH (val));
904 33 : unsigned int j;
905 33 : subsubconvs[0] = sub;
906 15849 : for (j = 1; j < (unsigned) RAW_DATA_LENGTH (val); ++j)
907 : {
908 15816 : elt = build_int_cst (TREE_TYPE (val),
909 15816 : RAW_DATA_UCHAR_ELT (val, j));
910 15816 : sub = implicit_conversion (elttype, TREE_TYPE (val), elt,
911 : false, flags, complain);
912 15816 : if (sub == NULL)
913 : return NULL;
914 15816 : subsubconvs[j] = sub;
915 : }
916 :
917 33 : t = alloc_conversion (ck_list);
918 33 : t->type = type;
919 33 : t->u.list = subsubconvs;
920 33 : t->rank = cr_exact;
921 15882 : for (j = 0; j < (unsigned) RAW_DATA_LENGTH (val); ++j)
922 : {
923 15849 : sub = subsubconvs[j];
924 15849 : if (sub->rank > t->rank)
925 6 : t->rank = sub->rank;
926 15849 : if (sub->user_conv_p)
927 12063 : t->user_conv_p = true;
928 15849 : if (sub->bad_p)
929 0 : t->bad_p = true;
930 : }
931 33 : subconvs[i] = t;
932 33 : continue;
933 33 : }
934 :
935 214746 : conversion *sub
936 214746 : = implicit_conversion (elttype, TREE_TYPE (val), val,
937 : false, flags, complain);
938 214746 : if (sub == NULL)
939 : return NULL;
940 :
941 203251 : subconvs[i] = sub;
942 : }
943 :
944 71337 : t = alloc_conversion (ck_list);
945 71337 : t->type = type;
946 71337 : t->u.list = subconvs;
947 71337 : t->rank = cr_exact;
948 :
949 256449 : for (i = 0; i < len; ++i)
950 : {
951 185112 : conversion *sub = subconvs[i];
952 185112 : if (sub->rank > t->rank)
953 58889 : t->rank = sub->rank;
954 185112 : if (sub->user_conv_p)
955 10136 : t->user_conv_p = true;
956 185112 : if (sub->bad_p)
957 58904 : t->bad_p = true;
958 : }
959 :
960 : return t;
961 : }
962 :
963 : /* Return the next conversion of the conversion chain (if applicable),
964 : or NULL otherwise. Please use this function instead of directly
965 : accessing fields of struct conversion. */
966 :
967 : static conversion *
968 564255290 : next_conversion (conversion *conv)
969 : {
970 564255290 : if (conv == NULL
971 564255290 : || !has_next (conv->kind))
972 : return NULL;
973 552822131 : return conv->u.next;
974 : }
975 :
976 : /* Strip to the first ck_user, ck_ambig, ck_list, ck_aggr or ck_identity
977 : encountered. */
978 :
979 : static conversion *
980 1375795 : strip_standard_conversion (conversion *conv)
981 : {
982 1375795 : while (conv
983 1383367 : && conv->kind != ck_user
984 1418965 : && has_next (conv->kind))
985 7572 : conv = next_conversion (conv);
986 1375795 : return conv;
987 : }
988 :
989 : /* Subroutine of build_aggr_conv: check whether FROM is a valid aggregate
990 : initializer for array type ATYPE. */
991 :
992 : static bool
993 25275 : can_convert_array (tree atype, tree from, int flags, tsubst_flags_t complain)
994 : {
995 25275 : tree elttype = TREE_TYPE (atype);
996 25275 : unsigned i;
997 :
998 25275 : if (TREE_CODE (from) == CONSTRUCTOR)
999 : {
1000 28274 : for (i = 0; i < CONSTRUCTOR_NELTS (from); ++i)
1001 : {
1002 3044 : tree val = CONSTRUCTOR_ELT (from, i)->value;
1003 3044 : bool ok;
1004 3044 : if (TREE_CODE (elttype) == ARRAY_TYPE)
1005 18 : ok = can_convert_array (elttype, val, flags, complain);
1006 : else
1007 3026 : ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags,
1008 : complain);
1009 3044 : if (!ok)
1010 : return false;
1011 : }
1012 : return true;
1013 : }
1014 :
1015 45 : if (char_type_p (TYPE_MAIN_VARIANT (elttype))
1016 45 : && TREE_CODE (tree_strip_any_location_wrapper (from)) == STRING_CST)
1017 45 : return array_string_literal_compatible_p (atype, from);
1018 :
1019 : /* No other valid way to aggregate initialize an array. */
1020 : return false;
1021 : }
1022 :
1023 : /* Helper for build_aggr_conv. Return true if FIELD is in PSET, or if
1024 : FIELD has ANON_AGGR_TYPE_P and any initializable field in there recursively
1025 : is in PSET. */
1026 :
1027 : static bool
1028 773519 : field_in_pset (hash_set<tree, true> &pset, tree field)
1029 : {
1030 773519 : if (pset.contains (field))
1031 : return true;
1032 1355 : if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1033 0 : for (field = TYPE_FIELDS (TREE_TYPE (field));
1034 0 : field; field = DECL_CHAIN (field))
1035 : {
1036 0 : field = next_aggregate_field (field);
1037 0 : if (field == NULL_TREE)
1038 : break;
1039 0 : if (field_in_pset (pset, field))
1040 : return true;
1041 : }
1042 : return false;
1043 : }
1044 :
1045 : /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
1046 : aggregate class, if such a conversion is possible. */
1047 :
1048 : static conversion *
1049 1232055 : build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
1050 : {
1051 1232055 : unsigned HOST_WIDE_INT i = 0;
1052 1232055 : conversion *c;
1053 1232055 : tree field = next_aggregate_field (TYPE_FIELDS (type));
1054 1232055 : tree empty_ctor = NULL_TREE;
1055 1232055 : hash_set<tree, true> pset;
1056 :
1057 : /* We already called reshape_init in implicit_conversion, but it might not
1058 : have done anything in the case of parenthesized aggr init. */
1059 :
1060 : /* The conversions within the init-list aren't affected by the enclosing
1061 : context; they're always simple copy-initialization. */
1062 1232055 : flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1063 :
1064 : /* For designated initializers, verify that each initializer is convertible
1065 : to corresponding TREE_TYPE (ce->index) and mark those FIELD_DECLs as
1066 : visited. In the following loop then ignore already visited
1067 : FIELD_DECLs. */
1068 1232055 : tree idx, val;
1069 2004219 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, val)
1070 : {
1071 772239 : if (!idx)
1072 : break;
1073 :
1074 772196 : gcc_checking_assert (TREE_CODE (idx) == FIELD_DECL);
1075 :
1076 772196 : tree ftype = TREE_TYPE (idx);
1077 772196 : bool ok;
1078 :
1079 772196 : if (TREE_CODE (ftype) == ARRAY_TYPE)
1080 1078 : ok = can_convert_array (ftype, val, flags, complain);
1081 : else
1082 771118 : ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
1083 : complain);
1084 :
1085 772196 : if (!ok)
1086 : return NULL;
1087 :
1088 : /* For unions, there should be just one initializer. */
1089 772187 : if (TREE_CODE (type) == UNION_TYPE)
1090 : {
1091 : field = NULL_TREE;
1092 : i = 1;
1093 : break;
1094 : }
1095 772164 : pset.add (idx);
1096 : }
1097 :
1098 2070412 : for (; field; field = next_aggregate_field (DECL_CHAIN (field)))
1099 : {
1100 838623 : tree ftype = TREE_TYPE (field);
1101 838623 : bool ok;
1102 :
1103 838623 : if (!pset.is_empty () && field_in_pset (pset, field))
1104 772164 : continue;
1105 66459 : if (i < CONSTRUCTOR_NELTS (ctor))
1106 : {
1107 29 : constructor_elt *ce = CONSTRUCTOR_ELT (ctor, i);
1108 29 : gcc_checking_assert (!ce->index);
1109 29 : val = ce->value;
1110 29 : ++i;
1111 : }
1112 66430 : else if (DECL_INITIAL (field))
1113 15395 : val = get_nsdmi (field, /*ctor*/false, complain);
1114 51035 : else if (TYPE_REF_P (ftype))
1115 : /* Value-initialization of reference is ill-formed. */
1116 : return NULL;
1117 : else
1118 : {
1119 51029 : if (empty_ctor == NULL_TREE)
1120 38137 : empty_ctor = build_constructor (init_list_type_node, NULL);
1121 : val = empty_ctor;
1122 : }
1123 :
1124 66453 : if (TREE_CODE (ftype) == ARRAY_TYPE)
1125 24179 : ok = can_convert_array (ftype, val, flags, complain);
1126 : else
1127 42274 : ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
1128 : complain);
1129 :
1130 66453 : if (!ok)
1131 : return NULL;
1132 :
1133 66442 : if (TREE_CODE (type) == UNION_TYPE)
1134 : break;
1135 : }
1136 :
1137 1232029 : if (i < CONSTRUCTOR_NELTS (ctor))
1138 : return NULL;
1139 :
1140 1232010 : c = alloc_conversion (ck_aggr);
1141 1232010 : c->type = type;
1142 1232010 : c->rank = cr_exact;
1143 1232010 : c->user_conv_p = true;
1144 1232010 : c->check_narrowing = true;
1145 1232010 : c->u.expr = ctor;
1146 1232010 : return c;
1147 1232055 : }
1148 :
1149 : /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
1150 : array type, if such a conversion is possible. */
1151 :
1152 : static conversion *
1153 1667 : build_array_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
1154 : {
1155 1667 : conversion *c;
1156 1667 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1157 1667 : tree elttype = TREE_TYPE (type);
1158 1667 : bool bad = false;
1159 1667 : bool user = false;
1160 1667 : enum conversion_rank rank = cr_exact;
1161 :
1162 : /* We might need to propagate the size from the element to the array. */
1163 1667 : complete_type (type);
1164 :
1165 1667 : if (TYPE_DOMAIN (type)
1166 1667 : && !variably_modified_type_p (TYPE_DOMAIN (type), NULL_TREE))
1167 : {
1168 1593 : unsigned HOST_WIDE_INT alen = tree_to_uhwi (array_type_nelts_top (type));
1169 1593 : if (alen < len)
1170 : return NULL;
1171 : }
1172 :
1173 1630 : flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1174 :
1175 6990 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
1176 : {
1177 2776 : conversion *sub
1178 2776 : = implicit_conversion (elttype, TREE_TYPE (e.value), e.value,
1179 : false, flags, complain);
1180 2776 : if (sub == NULL)
1181 : return NULL;
1182 :
1183 2186 : if (sub->rank > rank)
1184 265 : rank = sub->rank;
1185 2186 : if (sub->user_conv_p)
1186 23 : user = true;
1187 2186 : if (sub->bad_p)
1188 196 : bad = true;
1189 : }
1190 :
1191 1040 : c = alloc_conversion (ck_aggr);
1192 1040 : c->type = type;
1193 1040 : c->rank = rank;
1194 1040 : c->user_conv_p = user;
1195 1040 : c->bad_p = bad;
1196 1040 : c->u.expr = ctor;
1197 1040 : return c;
1198 : }
1199 :
1200 : /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
1201 : complex type, if such a conversion is possible. */
1202 :
1203 : static conversion *
1204 55072 : build_complex_conv (tree type, tree ctor, int flags,
1205 : tsubst_flags_t complain)
1206 : {
1207 55072 : conversion *c;
1208 55072 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1209 55072 : tree elttype = TREE_TYPE (type);
1210 55072 : bool bad = false;
1211 55072 : bool user = false;
1212 55072 : enum conversion_rank rank = cr_exact;
1213 :
1214 55072 : if (len != 2)
1215 : return NULL;
1216 :
1217 55032 : flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1218 :
1219 275160 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
1220 : {
1221 110064 : conversion *sub
1222 110064 : = implicit_conversion (elttype, TREE_TYPE (e.value), e.value,
1223 : false, flags, complain);
1224 110064 : if (sub == NULL)
1225 : return NULL;
1226 :
1227 110064 : if (sub->rank > rank)
1228 44 : rank = sub->rank;
1229 110064 : if (sub->user_conv_p)
1230 0 : user = true;
1231 110064 : if (sub->bad_p)
1232 0 : bad = true;
1233 : }
1234 :
1235 55032 : c = alloc_conversion (ck_aggr);
1236 55032 : c->type = type;
1237 55032 : c->rank = rank;
1238 55032 : c->user_conv_p = user;
1239 55032 : c->bad_p = bad;
1240 55032 : c->u.expr = ctor;
1241 55032 : return c;
1242 : }
1243 :
1244 : /* Build a representation of the identity conversion from EXPR to
1245 : itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
1246 :
1247 : static conversion *
1248 1801874315 : build_identity_conv (tree type, tree expr)
1249 : {
1250 1801874315 : conversion *c;
1251 :
1252 0 : c = alloc_conversion (ck_identity);
1253 1801874315 : c->type = type;
1254 1801874315 : c->u.expr = expr;
1255 :
1256 1801874315 : return c;
1257 : }
1258 :
1259 : /* Converting from EXPR to TYPE was ambiguous in the sense that there
1260 : were multiple user-defined conversions to accomplish the job.
1261 : Build a conversion that indicates that ambiguity. */
1262 :
1263 : static conversion *
1264 1339 : build_ambiguous_conv (tree type, tree expr)
1265 : {
1266 1339 : conversion *c;
1267 :
1268 0 : c = alloc_conversion (ck_ambig);
1269 1339 : c->type = type;
1270 1339 : c->u.expr = expr;
1271 :
1272 1339 : return c;
1273 : }
1274 :
1275 : tree
1276 3238148085 : strip_top_quals (tree t)
1277 : {
1278 3238148085 : if (TREE_CODE (t) == ARRAY_TYPE)
1279 : return t;
1280 3232140259 : return cp_build_qualified_type (t, 0);
1281 : }
1282 :
1283 : /* Returns the standard conversion path (see [conv]) from type FROM to type
1284 : TO, if any. For proper handling of null pointer constants, you must
1285 : also pass the expression EXPR to convert from. If C_CAST_P is true,
1286 : this conversion is coming from a C-style cast. */
1287 :
1288 : static conversion *
1289 1443774299 : standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
1290 : int flags, tsubst_flags_t complain)
1291 : {
1292 1443774299 : enum tree_code fcode, tcode;
1293 1443774299 : conversion *conv;
1294 1443774299 : bool fromref = false;
1295 1443774299 : tree qualified_to;
1296 :
1297 1443774299 : to = non_reference (to);
1298 1443774299 : if (TYPE_REF_P (from))
1299 : {
1300 77718 : fromref = true;
1301 77718 : from = TREE_TYPE (from);
1302 : }
1303 1443774299 : qualified_to = to;
1304 1443774299 : to = strip_top_quals (to);
1305 1443774299 : from = strip_top_quals (from);
1306 :
1307 1443774299 : if (expr && type_unknown_p (expr))
1308 : {
1309 321541 : if (TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
1310 : {
1311 34878 : tsubst_flags_t tflags = tf_conv;
1312 34878 : expr = instantiate_type (to, expr, tflags);
1313 34878 : if (expr == error_mark_node)
1314 : return NULL;
1315 21302 : from = TREE_TYPE (expr);
1316 : }
1317 286663 : else if (TREE_CODE (to) == BOOLEAN_TYPE)
1318 : {
1319 : /* Necessary for eg, TEMPLATE_ID_EXPRs (c++/50961). */
1320 6587 : expr = resolve_nondeduced_context (expr, complain);
1321 6587 : from = TREE_TYPE (expr);
1322 : }
1323 : }
1324 :
1325 1443760723 : fcode = TREE_CODE (from);
1326 1443760723 : tcode = TREE_CODE (to);
1327 :
1328 1443760723 : conv = build_identity_conv (from, expr);
1329 1443760723 : if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
1330 : {
1331 6028060 : from = type_decays_to (from);
1332 6028060 : fcode = TREE_CODE (from);
1333 : /* Tell convert_like that we're using the address. */
1334 6028060 : conv->rvaluedness_matches_p = true;
1335 6028060 : conv = build_conv (ck_lvalue, from, conv);
1336 : }
1337 : /* Wrapping a ck_rvalue around a class prvalue (as a result of using
1338 : obvalue_p) seems odd, since it's already a prvalue, but that's how we
1339 : express the copy constructor call required by copy-initialization. */
1340 1437732663 : else if (fromref || (expr && obvalue_p (expr)))
1341 : {
1342 338273676 : if (expr)
1343 : {
1344 338195983 : tree bitfield_type;
1345 338195983 : bitfield_type = is_bitfield_expr_with_lowered_type (expr);
1346 338195983 : if (bitfield_type)
1347 : {
1348 2271083 : from = strip_top_quals (bitfield_type);
1349 2271083 : fcode = TREE_CODE (from);
1350 : }
1351 : }
1352 338273676 : conv = build_conv (ck_rvalue, from, conv);
1353 : /* If we're performing copy-initialization, remember to skip
1354 : explicit constructors. */
1355 338273676 : if (flags & LOOKUP_ONLYCONVERTING)
1356 289401309 : conv->copy_init_p = true;
1357 : }
1358 :
1359 : /* Allow conversion between `__complex__' data types. */
1360 1443760723 : if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
1361 : {
1362 : /* The standard conversion sequence to convert FROM to TO is
1363 : the standard conversion sequence to perform componentwise
1364 : conversion. */
1365 2234501 : conversion *part_conv = standard_conversion
1366 2234501 : (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags,
1367 : complain);
1368 :
1369 2234501 : if (!part_conv)
1370 : conv = NULL;
1371 2234501 : else if (part_conv->kind == ck_identity)
1372 : /* Leave conv alone. */;
1373 : else
1374 : {
1375 229356 : conv = build_conv (part_conv->kind, to, conv);
1376 229356 : conv->rank = part_conv->rank;
1377 : }
1378 :
1379 2234501 : return conv;
1380 : }
1381 :
1382 1441526222 : if (same_type_p (from, to))
1383 : {
1384 874422762 : if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue)
1385 16375853 : conv->type = qualified_to;
1386 858046909 : else if (from != to)
1387 : /* Use TO in order to not lose TO in diagnostics. */
1388 76124111 : conv->type = to;
1389 874422762 : return conv;
1390 : }
1391 :
1392 : /* [conv.ptr]
1393 : A null pointer constant can be converted to a pointer type; ... A
1394 : null pointer constant of integral type can be converted to an
1395 : rvalue of type std::nullptr_t. */
1396 365469861 : if ((tcode == POINTER_TYPE || TYPE_PTRMEM_P (to)
1397 365382553 : || NULLPTR_TYPE_P (to))
1398 572300030 : && ((expr && null_ptr_cst_p (expr))
1399 201448613 : || NULLPTR_TYPE_P (from)))
1400 5381560 : conv = build_conv (ck_std, to, conv);
1401 561721900 : else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
1402 561164982 : || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
1403 : {
1404 : /* For backwards brain damage compatibility, allow interconversion of
1405 : pointers and integers with a pedwarn. */
1406 2065674 : conv = build_conv (ck_std, to, conv);
1407 2065674 : conv->bad_p = true;
1408 : }
1409 559656226 : else if (UNSCOPED_ENUM_P (to) && fcode == INTEGER_TYPE)
1410 : {
1411 : /* For backwards brain damage compatibility, allow interconversion of
1412 : enums and integers with a pedwarn. */
1413 350373 : conv = build_conv (ck_std, to, conv);
1414 350373 : conv->bad_p = true;
1415 : }
1416 559305853 : else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
1417 368877030 : || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from)))
1418 : {
1419 1700 : tree to_pointee;
1420 1700 : tree from_pointee;
1421 :
1422 1700 : if (tcode == POINTER_TYPE)
1423 : {
1424 190428823 : to_pointee = TREE_TYPE (to);
1425 190428823 : from_pointee = TREE_TYPE (from);
1426 :
1427 : /* Since this is the target of a pointer, it can't have function
1428 : qualifiers, so any TYPE_QUALS must be for attributes const or
1429 : noreturn. Strip them. */
1430 190428823 : if (TREE_CODE (to_pointee) == FUNCTION_TYPE
1431 190428823 : && TYPE_QUALS (to_pointee))
1432 9 : to_pointee = build_qualified_type (to_pointee, TYPE_UNQUALIFIED);
1433 190428823 : if (TREE_CODE (from_pointee) == FUNCTION_TYPE
1434 190428823 : && TYPE_QUALS (from_pointee))
1435 36 : from_pointee = build_qualified_type (from_pointee, TYPE_UNQUALIFIED);
1436 : }
1437 : else
1438 : {
1439 1700 : to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
1440 1700 : from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
1441 : }
1442 :
1443 190430523 : if (tcode == POINTER_TYPE
1444 190430523 : && same_type_ignoring_top_level_qualifiers_p (from_pointee,
1445 : to_pointee))
1446 : ;
1447 134525959 : else if (VOID_TYPE_P (to_pointee)
1448 4408994 : && !TYPE_PTRDATAMEM_P (from)
1449 4408994 : && TREE_CODE (from_pointee) != FUNCTION_TYPE)
1450 : {
1451 4408669 : tree nfrom = TREE_TYPE (from);
1452 : /* Don't try to apply restrict to void. */
1453 4408669 : int quals = cp_type_quals (nfrom) & ~TYPE_QUAL_RESTRICT;
1454 4408669 : from_pointee = cp_build_qualified_type (void_type_node, quals);
1455 4408669 : from = build_pointer_type (from_pointee);
1456 4408669 : conv = build_conv (ck_ptr, from, conv);
1457 4408669 : }
1458 130117290 : else if (TYPE_PTRDATAMEM_P (from))
1459 : {
1460 1700 : tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
1461 1700 : tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
1462 :
1463 1700 : if (same_type_p (fbase, tbase))
1464 : /* No base conversion needed. */;
1465 589 : else if (DERIVED_FROM_P (fbase, tbase)
1466 990 : && (same_type_ignoring_top_level_qualifiers_p
1467 401 : (from_pointee, to_pointee)))
1468 : {
1469 377 : from = build_ptrmem_type (tbase, from_pointee);
1470 377 : conv = build_conv (ck_pmem, from, conv);
1471 : }
1472 : else
1473 212 : return NULL;
1474 : }
1475 63344004 : else if (CLASS_TYPE_P (from_pointee)
1476 63341569 : && CLASS_TYPE_P (to_pointee)
1477 : /* [conv.ptr]
1478 :
1479 : An rvalue of type "pointer to cv D," where D is a
1480 : class type, can be converted to an rvalue of type
1481 : "pointer to cv B," where B is a base class (clause
1482 : _class.derived_) of D. If B is an inaccessible
1483 : (clause _class.access_) or ambiguous
1484 : (_class.member.lookup_) base class of D, a program
1485 : that necessitates this conversion is ill-formed.
1486 : Therefore, we use DERIVED_FROM_P, and do not check
1487 : access or uniqueness. */
1488 192669551 : && DERIVED_FROM_P (to_pointee, from_pointee))
1489 : {
1490 5832529 : from_pointee
1491 5832529 : = cp_build_qualified_type (to_pointee,
1492 : cp_type_quals (from_pointee));
1493 5832529 : from = build_pointer_type (from_pointee);
1494 5832529 : conv = build_conv (ck_ptr, from, conv);
1495 5832529 : conv->base_p = true;
1496 : }
1497 :
1498 190430311 : if (same_type_p (from, to))
1499 : /* OK */;
1500 182725509 : else if (c_cast_p && comp_ptr_ttypes_const (to, from, bounds_either))
1501 : /* In a C-style cast, we ignore CV-qualification because we
1502 : are allowed to perform a static_cast followed by a
1503 : const_cast. */
1504 623 : conv = build_conv (ck_qual, to, conv);
1505 182724886 : else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
1506 51349598 : conv = build_conv (ck_qual, to, conv);
1507 131375288 : else if (expr && string_conv_p (to, expr, 0))
1508 : /* converting from string constant to char *. */
1509 204 : conv = build_conv (ck_qual, to, conv);
1510 131375084 : else if (fnptr_conv_p (to, from))
1511 227884 : conv = build_conv (ck_fnptr, to, conv);
1512 : /* Allow conversions among compatible ObjC pointer types (base
1513 : conversions have been already handled above). */
1514 131147200 : else if (c_dialect_objc ()
1515 131147200 : && objc_compare_types (to, from, -4, NULL_TREE))
1516 0 : conv = build_conv (ck_ptr, to, conv);
1517 131147200 : else if (ptr_reasonably_similar (to_pointee, from_pointee))
1518 : {
1519 8021067 : conv = build_conv (ck_ptr, to, conv);
1520 8021067 : conv->bad_p = true;
1521 : }
1522 : else
1523 : return NULL;
1524 :
1525 261947255 : from = to;
1526 : }
1527 368875330 : else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
1528 : {
1529 83450 : tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
1530 83450 : tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
1531 83450 : tree fbase = class_of_this_parm (fromfn);
1532 83450 : tree tbase = class_of_this_parm (tofn);
1533 :
1534 : /* If FBASE and TBASE are equivalent but incomplete, DERIVED_FROM_P
1535 : yields false. But a pointer to member of incomplete class is OK. */
1536 83450 : if (!same_type_p (fbase, tbase) && !DERIVED_FROM_P (fbase, tbase))
1537 : return NULL;
1538 :
1539 83289 : tree fstat = static_fn_type (fromfn);
1540 83289 : tree tstat = static_fn_type (tofn);
1541 83289 : if (same_type_p (tstat, fstat)
1542 83289 : || fnptr_conv_p (tstat, fstat))
1543 : /* OK */;
1544 : else
1545 : return NULL;
1546 :
1547 83021 : if (!same_type_p (fbase, tbase))
1548 : {
1549 82952 : from = build_memfn_type (fstat,
1550 : tbase,
1551 : cp_type_quals (tbase),
1552 : type_memfn_rqual (tofn));
1553 82952 : from = build_ptrmemfunc_type (build_pointer_type (from));
1554 82952 : conv = build_conv (ck_pmem, from, conv);
1555 82952 : conv->base_p = true;
1556 : }
1557 83021 : if (fnptr_conv_p (tstat, fstat))
1558 69 : conv = build_conv (ck_fnptr, to, conv);
1559 : }
1560 368791880 : else if (tcode == BOOLEAN_TYPE)
1561 : {
1562 : /* [conv.bool]
1563 :
1564 : A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
1565 : to member type can be converted to a prvalue of type bool. ...
1566 : For direct-initialization (8.5 [dcl.init]), a prvalue of type
1567 : std::nullptr_t can be converted to a prvalue of type bool; */
1568 6111524 : if (ARITHMETIC_TYPE_P (from)
1569 6054875 : || UNSCOPED_ENUM_P (from)
1570 3967097 : || fcode == POINTER_TYPE
1571 2789519 : || TYPE_PTRMEM_P (from)
1572 14319337 : || NULLPTR_TYPE_P (from))
1573 : {
1574 8740821 : conv = build_conv (ck_std, to, conv);
1575 8740821 : if (fcode == POINTER_TYPE
1576 7563243 : || TYPE_PTRDATAMEM_P (from)
1577 7563099 : || (TYPE_PTRMEMFUNC_P (from)
1578 77 : && conv->rank < cr_pbool)
1579 16303843 : || NULLPTR_TYPE_P (from))
1580 1177879 : conv->rank = cr_pbool;
1581 8740821 : if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING))
1582 38 : conv->bad_p = true;
1583 8740821 : if (flags & LOOKUP_NO_NARROWING)
1584 30909 : conv->check_narrowing = true;
1585 8740821 : return conv;
1586 : }
1587 :
1588 : return NULL;
1589 : }
1590 : /* We don't check for ENUMERAL_TYPE here because there are no standard
1591 : conversions to enum type. */
1592 : /* As an extension, allow conversion to complex type. */
1593 357261841 : else if (ARITHMETIC_TYPE_P (to))
1594 : {
1595 29255761 : if (! (INTEGRAL_CODE_P (fcode)
1596 22958718 : || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL)))
1597 216894866 : || SCOPED_ENUM_P (from))
1598 : return NULL;
1599 :
1600 : /* If we're parsing an enum with no fixed underlying type, we're
1601 : dealing with an incomplete type, which renders the conversion
1602 : ill-formed. */
1603 186324964 : if (!COMPLETE_TYPE_P (from))
1604 : return NULL;
1605 :
1606 186324958 : conv = build_conv (ck_std, to, conv);
1607 :
1608 186324958 : tree underlying_type = NULL_TREE;
1609 186324958 : if (TREE_CODE (from) == ENUMERAL_TYPE
1610 186324958 : && ENUM_FIXED_UNDERLYING_TYPE_P (from))
1611 1633710 : underlying_type = ENUM_UNDERLYING_TYPE (from);
1612 :
1613 : /* Give this a better rank if it's a promotion.
1614 :
1615 : To handle CWG 1601, also bump the rank if we are converting
1616 : an enumeration with a fixed underlying type to the underlying
1617 : type. */
1618 186324958 : if ((same_type_p (to, type_promotes_to (from))
1619 174916681 : || (underlying_type && same_type_p (to, underlying_type)))
1620 186324977 : && next_conversion (conv)->rank <= cr_promotion)
1621 11408296 : conv->rank = cr_promotion;
1622 :
1623 : /* A prvalue of floating-point type can be converted to a prvalue of
1624 : another floating-point type with a greater or equal conversion
1625 : rank ([conv.rank]). A prvalue of standard floating-point type can
1626 : be converted to a prvalue of another standard floating-point type.
1627 : For backwards compatibility with handling __float128 and other
1628 : non-standard floating point types, allow all implicit floating
1629 : point conversions if neither type is extended floating-point
1630 : type and if at least one of them is, fail if they have unordered
1631 : conversion rank or from has higher conversion rank. */
1632 186324958 : if (fcode == REAL_TYPE
1633 186324958 : && tcode == REAL_TYPE
1634 15491773 : && (extended_float_type_p (from)
1635 14929909 : || extended_float_type_p (to))
1636 189143080 : && cp_compare_floating_point_conversion_ranks (from, to) >= 2)
1637 1299582 : conv->bad_p = true;
1638 : }
1639 163325693 : else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
1640 163325693 : && vector_types_convertible_p (from, to, false))
1641 4884 : return build_conv (ck_std, to, conv);
1642 163320809 : else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
1643 65628863 : && is_properly_derived_from (from, to))
1644 : {
1645 437491 : if (conv->kind == ck_rvalue)
1646 437440 : conv = next_conversion (conv);
1647 437491 : conv = build_conv (ck_base, to, conv);
1648 : /* The derived-to-base conversion indicates the initialization
1649 : of a parameter with base type from an object of a derived
1650 : type. A temporary object is created to hold the result of
1651 : the conversion unless we're binding directly to a reference. */
1652 437491 : conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
1653 : /* If we're performing copy-initialization, remember to skip
1654 : explicit constructors. */
1655 437491 : if (flags & LOOKUP_ONLYCONVERTING)
1656 437424 : conv->copy_init_p = true;
1657 : }
1658 : else
1659 162883318 : return NULL;
1660 :
1661 261947255 : if (flags & LOOKUP_NO_NARROWING)
1662 91632918 : conv->check_narrowing = true;
1663 :
1664 : return conv;
1665 : }
1666 :
1667 : /* Returns nonzero if T1 is reference-related to T2.
1668 :
1669 : This is considered when a reference to T1 is initialized by a T2. */
1670 :
1671 : bool
1672 258541586 : reference_related_p (tree t1, tree t2)
1673 : {
1674 258541586 : if (t1 == error_mark_node || t2 == error_mark_node)
1675 : return false;
1676 :
1677 258541582 : t1 = TYPE_MAIN_VARIANT (t1);
1678 258541582 : t2 = TYPE_MAIN_VARIANT (t2);
1679 :
1680 : /* [dcl.init.ref]
1681 :
1682 : Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
1683 : to "cv2 T2" if T1 is similar to T2, or T1 is a base class of T2. */
1684 258541582 : return (similar_type_p (t1, t2)
1685 258541582 : || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1686 64191092 : && DERIVED_FROM_P (t1, t2)));
1687 : }
1688 :
1689 : /* Returns nonzero if T1 is reference-compatible with T2. */
1690 :
1691 : bool
1692 229255569 : reference_compatible_p (tree t1, tree t2)
1693 : {
1694 : /* [dcl.init.ref]
1695 :
1696 : "cv1 T1" is reference compatible with "cv2 T2" if
1697 : a prvalue of type "pointer to cv2 T2" can be converted to the type
1698 : "pointer to cv1 T1" via a standard conversion sequence. */
1699 229255569 : tree ptype1 = build_pointer_type (t1);
1700 229255569 : tree ptype2 = build_pointer_type (t2);
1701 229255569 : conversion *conv = standard_conversion (ptype1, ptype2, NULL_TREE,
1702 : /*c_cast_p=*/false, 0, tf_none);
1703 229255569 : if (!conv || conv->bad_p)
1704 129128513 : return false;
1705 : return true;
1706 : }
1707 :
1708 : /* Return true if converting FROM to TO would involve a qualification
1709 : conversion. */
1710 :
1711 : static bool
1712 110460830 : involves_qualification_conversion_p (tree to, tree from)
1713 : {
1714 : /* If we're not convering a pointer to another one, we won't get
1715 : a qualification conversion. */
1716 110460830 : if (!((TYPE_PTR_P (to) && TYPE_PTR_P (from))
1717 2400 : || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from))))
1718 : return false;
1719 :
1720 4401941 : conversion *conv = standard_conversion (to, from, NULL_TREE,
1721 : /*c_cast_p=*/false, 0, tf_none);
1722 8787923 : for (conversion *t = conv; t; t = next_conversion (t))
1723 4401957 : if (t->kind == ck_qual)
1724 : return true;
1725 :
1726 : return false;
1727 : }
1728 :
1729 : /* Return true if HANDLER is a match for exception object with EXCEPT_TYPE as
1730 : per [except.handle]/3. */
1731 :
1732 : bool
1733 1970 : handler_match_for_exception_type (tree handler, tree except_type)
1734 : {
1735 1970 : tree handler_type = HANDLER_TYPE (handler);
1736 1970 : if (handler_type == NULL_TREE)
1737 : return true; /* ... */
1738 1937 : if (same_type_ignoring_top_level_qualifiers_p (handler_type, except_type))
1739 : return true;
1740 292 : if (CLASS_TYPE_P (except_type) && CLASS_TYPE_P (handler_type))
1741 : {
1742 80 : base_kind b_kind;
1743 80 : tree binfo = lookup_base (except_type, handler_type, ba_check, &b_kind,
1744 : tf_none);
1745 80 : if (binfo && binfo != error_mark_node)
1746 37 : return true;
1747 : }
1748 255 : if (TYPE_PTR_P (handler_type) || TYPE_PTRDATAMEM_P (handler_type))
1749 : {
1750 60 : if (TREE_CODE (except_type) == NULLPTR_TYPE)
1751 : return true;
1752 54 : if ((TYPE_PTR_P (handler_type) && TYPE_PTR_P (except_type))
1753 9 : || (TYPE_PTRDATAMEM_P (handler_type)
1754 0 : && TYPE_PTRDATAMEM_P (except_type)))
1755 : {
1756 45 : conversion *conv
1757 45 : = standard_conversion (handler_type, except_type, NULL_TREE,
1758 : /*c_cast_p=*/false, 0, tf_none);
1759 45 : if (conv && !conv->bad_p)
1760 : {
1761 33 : for (conversion *t = conv; t; t = next_conversion (t))
1762 22 : switch (t->kind)
1763 : {
1764 22 : case ck_ptr:
1765 22 : case ck_fnptr:
1766 22 : case ck_qual:
1767 22 : case ck_identity:
1768 22 : break;
1769 : default:
1770 : return false;
1771 : }
1772 : return true;
1773 : }
1774 : }
1775 : }
1776 : return false;
1777 : }
1778 :
1779 : /* A reference of the indicated TYPE is being bound directly to the
1780 : expression represented by the implicit conversion sequence CONV.
1781 : Return a conversion sequence for this binding. */
1782 :
1783 : static conversion *
1784 114005059 : direct_reference_binding (tree type, conversion *conv)
1785 : {
1786 114005059 : tree t;
1787 :
1788 114005059 : gcc_assert (TYPE_REF_P (type));
1789 114005059 : gcc_assert (!TYPE_REF_P (conv->type));
1790 :
1791 114005059 : t = TREE_TYPE (type);
1792 :
1793 114005059 : if (conv->kind == ck_identity)
1794 : /* Mark the identity conv as to not decay to rvalue. */
1795 114005059 : conv->rvaluedness_matches_p = true;
1796 :
1797 : /* [over.ics.rank]
1798 :
1799 : When a parameter of reference type binds directly
1800 : (_dcl.init.ref_) to an argument expression, the implicit
1801 : conversion sequence is the identity conversion, unless the
1802 : argument expression has a type that is a derived class of the
1803 : parameter type, in which case the implicit conversion sequence is
1804 : a derived-to-base Conversion.
1805 :
1806 : If the parameter binds directly to the result of applying a
1807 : conversion function to the argument expression, the implicit
1808 : conversion sequence is a user-defined conversion sequence
1809 : (_over.ics.user_), with the second standard conversion sequence
1810 : either an identity conversion or, if the conversion function
1811 : returns an entity of a type that is a derived class of the
1812 : parameter type, a derived-to-base conversion. */
1813 114005059 : if (is_properly_derived_from (conv->type, t))
1814 : {
1815 : /* Represent the derived-to-base conversion. */
1816 3544229 : conv = build_conv (ck_base, t, conv);
1817 : /* We will actually be binding to the base-class subobject in
1818 : the derived class, so we mark this conversion appropriately.
1819 : That way, convert_like knows not to generate a temporary. */
1820 3544229 : conv->need_temporary_p = false;
1821 : }
1822 110460830 : else if (involves_qualification_conversion_p (t, conv->type))
1823 : /* Represent the qualification conversion. After DR 2352
1824 : #1 and #2 were indistinguishable conversion sequences:
1825 :
1826 : void f(int*); // #1
1827 : void f(const int* const &); // #2
1828 : void g(int* p) { f(p); }
1829 :
1830 : because the types "int *" and "const int *const" are
1831 : reference-related and we were binding both directly and they
1832 : had the same rank. To break it up, we add a ck_qual under the
1833 : ck_ref_bind so that conversion sequence ranking chooses #1.
1834 :
1835 : We strip_top_quals here which is also what standard_conversion
1836 : does. Failure to do so would confuse comp_cv_qual_signature
1837 : into thinking that in
1838 :
1839 : void f(const int * const &); // #1
1840 : void f(const int *); // #2
1841 : int *x;
1842 : f(x);
1843 :
1844 : #2 is a better match than #1 even though they're ambiguous (97296). */
1845 15975 : conv = build_conv (ck_qual, strip_top_quals (t), conv);
1846 :
1847 114005059 : return build_conv (ck_ref_bind, type, conv);
1848 : }
1849 :
1850 : /* Returns the conversion path from type FROM to reference type TO for
1851 : purposes of reference binding. For lvalue binding, either pass a
1852 : reference type to FROM or an lvalue expression to EXPR. If the
1853 : reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1854 : the conversion returned. If C_CAST_P is true, this
1855 : conversion is coming from a C-style cast. */
1856 :
1857 : static conversion *
1858 228465479 : reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags,
1859 : tsubst_flags_t complain)
1860 : {
1861 228465479 : conversion *conv = NULL;
1862 228465479 : conversion *bad_direct_conv = nullptr;
1863 228465479 : tree to = TREE_TYPE (rto);
1864 228465479 : tree from = rfrom;
1865 228465479 : tree tfrom;
1866 228465479 : bool related_p;
1867 228465479 : bool compatible_p;
1868 228465479 : cp_lvalue_kind gl_kind;
1869 228465479 : bool is_lvalue;
1870 :
1871 228465479 : if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1872 : {
1873 49 : expr = instantiate_type (to, expr, tf_none);
1874 49 : if (expr == error_mark_node)
1875 : return NULL;
1876 49 : from = TREE_TYPE (expr);
1877 : }
1878 :
1879 228465479 : bool copy_list_init = false;
1880 228465479 : bool single_list_conv = false;
1881 228465479 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1882 : {
1883 88000 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
1884 : /* DR 1288: Otherwise, if the initializer list has a single element
1885 : of type E and ... [T's] referenced type is reference-related to E,
1886 : the object or reference is initialized from that element...
1887 :
1888 : ??? With P0388R4, we should bind 't' directly to U{}:
1889 : using U = A[2];
1890 : A (&&t)[] = {U{}};
1891 : because A[] and A[2] are reference-related. But we don't do it
1892 : because grok_reference_init has deduced the array size (to 1), and
1893 : A[1] and A[2] aren't reference-related. */
1894 88000 : if (CONSTRUCTOR_NELTS (expr) == 1
1895 47673 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
1896 : {
1897 3056 : tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1898 3056 : if (error_operand_p (elt))
1899 : return NULL;
1900 3051 : tree etype = TREE_TYPE (elt);
1901 3051 : if (reference_related_p (to, etype))
1902 : {
1903 603 : expr = elt;
1904 603 : from = etype;
1905 603 : goto skip;
1906 : }
1907 2448 : else if (CLASS_TYPE_P (etype) && TYPE_HAS_CONVERSION (etype))
1908 : /* CWG1996: jason's proposed drafting adds "or initializing T from E
1909 : would bind directly". We check that in the direct binding with
1910 : conversion code below. */
1911 : single_list_conv = true;
1912 : }
1913 : /* Otherwise, if T is a reference type, a prvalue temporary of the type
1914 : referenced by T is copy-list-initialized, and the reference is bound
1915 : to that temporary. */
1916 : copy_list_init = true;
1917 228465474 : skip:;
1918 : }
1919 :
1920 228465474 : if (TYPE_REF_P (from))
1921 : {
1922 57680 : from = TREE_TYPE (from);
1923 57680 : if (!TYPE_REF_IS_RVALUE (rfrom)
1924 57680 : || TREE_CODE (from) == FUNCTION_TYPE)
1925 : gl_kind = clk_ordinary;
1926 : else
1927 : gl_kind = clk_rvalueref;
1928 : }
1929 228407794 : else if (expr)
1930 225147430 : gl_kind = lvalue_kind (expr);
1931 3005415 : else if (CLASS_TYPE_P (from)
1932 3260364 : || TREE_CODE (from) == ARRAY_TYPE)
1933 : gl_kind = clk_class;
1934 : else
1935 : gl_kind = clk_none;
1936 :
1937 : /* Don't allow a class prvalue when LOOKUP_NO_TEMP_BIND. */
1938 228465474 : if ((flags & LOOKUP_NO_TEMP_BIND)
1939 3388067 : && (gl_kind & clk_class))
1940 : gl_kind = clk_none;
1941 :
1942 : /* Same mask as real_lvalue_p. */
1943 225849098 : is_lvalue = gl_kind && !(gl_kind & (clk_rvalueref|clk_class));
1944 :
1945 194663949 : tfrom = from;
1946 194663949 : if ((gl_kind & clk_bitfield) != 0)
1947 1620781 : tfrom = unlowered_expr_type (expr);
1948 :
1949 : /* Figure out whether or not the types are reference-related and
1950 : reference compatible. We have to do this after stripping
1951 : references from FROM. */
1952 228465474 : related_p = reference_related_p (to, tfrom);
1953 : /* If this is a C cast, first convert to an appropriately qualified
1954 : type, so that we can later do a const_cast to the desired type. */
1955 228465474 : if (related_p && c_cast_p
1956 228465474 : && !at_least_as_qualified_p (to, tfrom))
1957 194 : to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1958 228465474 : compatible_p = reference_compatible_p (to, tfrom);
1959 :
1960 : /* Directly bind reference when target expression's type is compatible with
1961 : the reference and expression is an lvalue. In DR391, the wording in
1962 : [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1963 : const and rvalue references to rvalues of compatible class type.
1964 : We should also do direct bindings for non-class xvalues. */
1965 228465474 : if ((related_p || compatible_p) && gl_kind)
1966 : {
1967 : /* [dcl.init.ref]
1968 :
1969 : If the initializer expression
1970 :
1971 : -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1972 : is reference-compatible with "cv2 T2,"
1973 :
1974 : the reference is bound directly to the initializer expression
1975 : lvalue.
1976 :
1977 : [...]
1978 : If the initializer expression is an rvalue, with T2 a class type,
1979 : and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1980 : is bound to the object represented by the rvalue or to a sub-object
1981 : within that object. */
1982 :
1983 101207928 : conv = build_identity_conv (tfrom, expr);
1984 101207928 : conv = direct_reference_binding (rto, conv);
1985 :
1986 101207928 : if (TYPE_REF_P (rfrom))
1987 : /* Handle rvalue reference to function properly. */
1988 16025 : conv->rvaluedness_matches_p
1989 16025 : = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom));
1990 : else
1991 101191903 : conv->rvaluedness_matches_p
1992 101191903 : = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
1993 :
1994 101207928 : if ((gl_kind & clk_bitfield) != 0
1995 101207928 : || ((gl_kind & clk_packed) != 0 && !TYPE_PACKED (to)))
1996 : /* For the purposes of overload resolution, we ignore the fact
1997 : this expression is a bitfield or packed field. (In particular,
1998 : [over.ics.ref] says specifically that a function with a
1999 : non-const reference parameter is viable even if the
2000 : argument is a bitfield.)
2001 :
2002 : However, when we actually call the function we must create
2003 : a temporary to which to bind the reference. If the
2004 : reference is volatile, or isn't const, then we cannot make
2005 : a temporary, so we just issue an error when the conversion
2006 : actually occurs. */
2007 110 : conv->need_temporary_p = true;
2008 :
2009 : /* Don't allow binding of lvalues (other than function lvalues) to
2010 : rvalue references. */
2011 73290083 : if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
2012 110669535 : && TREE_CODE (to) != FUNCTION_TYPE)
2013 9459985 : conv->bad_p = true;
2014 :
2015 : /* Nor the reverse. */
2016 27917845 : if (!is_lvalue && !TYPE_REF_IS_RVALUE (rto)
2017 : /* Unless it's really a C++20 lvalue being treated as an xvalue.
2018 : But in C++23, such an expression is just an xvalue, not a special
2019 : lvalue, so the binding is once again ill-formed. */
2020 17361852 : && !(cxx_dialect <= cxx20
2021 14701330 : && (gl_kind & clk_implicit_rval))
2022 16273892 : && (!CP_TYPE_CONST_NON_VOLATILE_P (to)
2023 16164423 : || (flags & LOOKUP_NO_RVAL_BIND))
2024 101317397 : && TREE_CODE (to) != FUNCTION_TYPE)
2025 109469 : conv->bad_p = true;
2026 :
2027 101207928 : if (!compatible_p)
2028 6173084 : conv->bad_p = true;
2029 :
2030 101207928 : return conv;
2031 : }
2032 : /* [class.conv.fct] A conversion function is never used to convert a
2033 : (possibly cv-qualified) object to the (possibly cv-qualified) same
2034 : object type (or a reference to it), to a (possibly cv-qualified) base
2035 : class of that type (or a reference to it).... */
2036 4304453 : else if (!related_p
2037 122953093 : && !(flags & LOOKUP_NO_CONVERSION)
2038 45902889 : && (CLASS_TYPE_P (from) || single_list_conv))
2039 : {
2040 16848084 : tree rexpr = expr;
2041 16848084 : if (single_list_conv)
2042 88 : rexpr = CONSTRUCTOR_ELT (expr, 0)->value;
2043 :
2044 : /* [dcl.init.ref]
2045 :
2046 : If the initializer expression
2047 :
2048 : -- has a class type (i.e., T2 is a class type) can be
2049 : implicitly converted to an lvalue of type "cv3 T3," where
2050 : "cv1 T1" is reference-compatible with "cv3 T3". (this
2051 : conversion is selected by enumerating the applicable
2052 : conversion functions (_over.match.ref_) and choosing the
2053 : best one through overload resolution. (_over.match_).
2054 :
2055 : the reference is bound to the lvalue result of the conversion
2056 : in the second case. */
2057 16848084 : z_candidate *cand = build_user_type_conversion_1 (rto, rexpr, flags,
2058 : complain);
2059 16848084 : if (cand)
2060 : {
2061 13355 : if (!cand->second_conv->bad_p)
2062 : return cand->second_conv;
2063 :
2064 : /* Direct reference binding wasn't successful and yielded a bad
2065 : conversion. Proceed with trying to go through a temporary
2066 : instead, and if that also fails then we'll return this bad
2067 : conversion rather than no conversion for sake of better
2068 : diagnostics. */
2069 : bad_direct_conv = cand->second_conv;
2070 : }
2071 : }
2072 :
2073 : /* From this point on, we conceptually need temporaries, even if we
2074 : elide them. Only the cases above are "direct bindings". */
2075 127244583 : if (flags & LOOKUP_NO_TEMP_BIND)
2076 : return bad_direct_conv ? bad_direct_conv : nullptr;
2077 :
2078 : /* [over.ics.rank]
2079 :
2080 : When a parameter of reference type is not bound directly to an
2081 : argument expression, the conversion sequence is the one required
2082 : to convert the argument expression to the underlying type of the
2083 : reference according to _over.best.ics_. Conceptually, this
2084 : conversion sequence corresponds to copy-initializing a temporary
2085 : of the underlying type with the argument expression. Any
2086 : difference in top-level cv-qualification is subsumed by the
2087 : initialization itself and does not constitute a conversion. */
2088 :
2089 124078889 : bool maybe_valid_p = true;
2090 :
2091 : /* [dcl.init.ref]
2092 :
2093 : Otherwise, the reference shall be an lvalue reference to a
2094 : non-volatile const type, or the reference shall be an rvalue
2095 : reference. */
2096 164283027 : if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto))
2097 : maybe_valid_p = false;
2098 :
2099 : /* [dcl.init.ref]
2100 :
2101 : Otherwise, a temporary of type "cv1 T1" is created and
2102 : initialized from the initializer expression using the rules for a
2103 : non-reference copy initialization. If T1 is reference-related to
2104 : T2, cv1 must be the same cv-qualification as, or greater
2105 : cv-qualification than, cv2; otherwise, the program is ill-formed. */
2106 124078889 : if (related_p && !at_least_as_qualified_p (to, from))
2107 : maybe_valid_p = false;
2108 :
2109 : /* We try below to treat an invalid reference binding as a bad conversion
2110 : to improve diagnostics, but doing so may cause otherwise unnecessary
2111 : instantiations that can lead to a hard error. So during the first pass
2112 : of overload resolution wherein we shortcut bad conversions, instead just
2113 : produce a special conversion indicating a second pass is necessary if
2114 : there's no strictly viable candidate. */
2115 124078889 : if (!maybe_valid_p && (flags & LOOKUP_SHORTCUT_BAD_CONVS))
2116 : {
2117 7390557 : if (bad_direct_conv)
2118 : return bad_direct_conv;
2119 :
2120 7390228 : conv = alloc_conversion (ck_deferred_bad);
2121 7390228 : conv->bad_p = true;
2122 7390228 : return conv;
2123 : }
2124 :
2125 : /* We're generating a temporary now, but don't bind any more in the
2126 : conversion (specifically, don't slice the temporary returned by a
2127 : conversion operator). */
2128 116688332 : flags |= LOOKUP_NO_TEMP_BIND;
2129 :
2130 : /* Core issue 899: When [copy-]initializing a temporary to be bound
2131 : to the first parameter of a copy constructor (12.8) called with
2132 : a single argument in the context of direct-initialization,
2133 : explicit conversion functions are also considered.
2134 :
2135 : So don't set LOOKUP_ONLYCONVERTING in that case. */
2136 116688332 : if (!(flags & LOOKUP_COPY_PARM))
2137 98618232 : flags |= LOOKUP_ONLYCONVERTING;
2138 :
2139 116688332 : if (!conv)
2140 116688332 : conv = implicit_conversion (to, from, expr, c_cast_p,
2141 : flags, complain);
2142 116688332 : if (!conv)
2143 : return bad_direct_conv ? bad_direct_conv : nullptr;
2144 :
2145 11194569 : if (conv->user_conv_p)
2146 : {
2147 7456109 : if (copy_list_init)
2148 : /* Remember this was copy-list-initialization. */
2149 61019 : conv->need_temporary_p = true;
2150 :
2151 : /* If initializing the temporary used a conversion function,
2152 : recalculate the second conversion sequence. */
2153 21504311 : for (conversion *t = conv; t; t = next_conversion (t))
2154 14467739 : if (t->kind == ck_user
2155 7429366 : && c_cast_p && !maybe_valid_p)
2156 : {
2157 12 : if (complain & tf_warning)
2158 12 : warning (OPT_Wcast_user_defined,
2159 : "casting %qT to %qT does not use %qD",
2160 12 : from, rto, t->cand->fn);
2161 : /* Don't let recalculation try to make this valid. */
2162 : break;
2163 : }
2164 14467727 : else if (t->kind == ck_user
2165 14467727 : && DECL_CONV_FN_P (t->cand->fn))
2166 : {
2167 419525 : tree ftype = TREE_TYPE (TREE_TYPE (t->cand->fn));
2168 : /* A prvalue of non-class type is cv-unqualified. */
2169 419525 : if (!TYPE_REF_P (ftype) && !CLASS_TYPE_P (ftype))
2170 1640 : ftype = cv_unqualified (ftype);
2171 419525 : int sflags = (flags|LOOKUP_NO_CONVERSION)&~LOOKUP_NO_TEMP_BIND;
2172 419525 : conversion *new_second
2173 419525 : = reference_binding (rto, ftype, NULL_TREE, c_cast_p,
2174 : sflags, complain);
2175 419525 : if (!new_second)
2176 : return bad_direct_conv ? bad_direct_conv : nullptr;
2177 419525 : conv = merge_conversion_sequences (t, new_second);
2178 419525 : gcc_assert (maybe_valid_p || conv->bad_p);
2179 : return conv;
2180 : }
2181 : }
2182 :
2183 10775044 : conv = build_conv (ck_ref_bind, rto, conv);
2184 : /* This reference binding, unlike those above, requires the
2185 : creation of a temporary. */
2186 10775044 : conv->need_temporary_p = true;
2187 10775044 : conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
2188 10775044 : conv->bad_p |= !maybe_valid_p;
2189 :
2190 10775044 : return conv;
2191 : }
2192 :
2193 : /* Returns the implicit conversion sequence (see [over.ics]) from type
2194 : FROM to type TO. The optional expression EXPR may affect the
2195 : conversion. FLAGS are the usual overloading flags. If C_CAST_P is
2196 : true, this conversion is coming from a C-style cast. */
2197 :
2198 : static conversion *
2199 1429849434 : implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
2200 : int flags, tsubst_flags_t complain)
2201 : {
2202 1429849434 : conversion *conv;
2203 :
2204 1429849434 : if (from == error_mark_node || to == error_mark_node
2205 1429848202 : || expr == error_mark_node)
2206 : return NULL;
2207 :
2208 : /* Other flags only apply to the primary function in overload
2209 : resolution, or after we've chosen one. */
2210 1429848202 : flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM
2211 : |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_NO_NARROWING
2212 : |LOOKUP_PROTECT|LOOKUP_NO_NON_INTEGRAL|LOOKUP_SHORTCUT_BAD_CONVS);
2213 :
2214 : /* FIXME: actually we don't want warnings either, but we can't just
2215 : have 'complain &= ~(tf_warning|tf_error)' because it would cause
2216 : the regression of, eg, g++.old-deja/g++.benjamin/16077.C.
2217 : We really ought not to issue that warning until we've committed
2218 : to that conversion. */
2219 1429848202 : complain &= ~tf_error;
2220 :
2221 : /* Call reshape_init early to remove redundant braces. */
2222 1429848202 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr) && CLASS_TYPE_P (to))
2223 : {
2224 2214403 : to = complete_type (to);
2225 2214403 : if (!COMPLETE_TYPE_P (to))
2226 : return nullptr;
2227 2214379 : if (!CLASSTYPE_NON_AGGREGATE (to))
2228 : {
2229 1232352 : expr = reshape_init (to, expr, complain);
2230 1232352 : if (expr == error_mark_node)
2231 : return nullptr;
2232 1232112 : from = TREE_TYPE (expr);
2233 : }
2234 : }
2235 :
2236 : /* An argument should have gone through convert_from_reference. */
2237 1429847938 : gcc_checking_assert (!expr || !TYPE_REF_P (from));
2238 :
2239 1429847938 : if (TYPE_REF_P (to))
2240 221965695 : conv = reference_binding (to, from, expr, c_cast_p, flags, complain);
2241 : else
2242 1207882243 : conv = standard_conversion (to, from, expr, c_cast_p, flags, complain);
2243 :
2244 1429847938 : if (conv)
2245 : return conv;
2246 :
2247 282187500 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
2248 : {
2249 4514057 : if (is_std_init_list (to) && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2250 82841 : return build_list_conv (to, expr, flags, complain);
2251 :
2252 : /* As an extension, allow list-initialization of _Complex. */
2253 4348366 : if (TREE_CODE (to) == COMPLEX_TYPE
2254 4403447 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2255 : {
2256 55072 : conv = build_complex_conv (to, expr, flags, complain);
2257 55072 : if (conv)
2258 : return conv;
2259 : }
2260 :
2261 : /* Allow conversion from an initializer-list with one element to a
2262 : scalar type. */
2263 4293334 : if (SCALAR_TYPE_P (to))
2264 : {
2265 2137917 : int nelts = CONSTRUCTOR_NELTS (expr);
2266 332281 : tree elt;
2267 :
2268 332281 : if (nelts == 0)
2269 1805636 : elt = build_value_init (to, tf_none);
2270 332281 : else if (nelts == 1 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2271 331502 : elt = CONSTRUCTOR_ELT (expr, 0)->value;
2272 : else
2273 779 : elt = error_mark_node;
2274 :
2275 2137917 : conv = implicit_conversion (to, TREE_TYPE (elt), elt,
2276 : c_cast_p, flags, complain);
2277 2137917 : if (conv)
2278 : {
2279 2136175 : conv->check_narrowing = true;
2280 2136175 : if (BRACE_ENCLOSED_INITIALIZER_P (elt))
2281 : /* Too many levels of braces, i.e. '{{1}}'. */
2282 16 : conv->bad_p = true;
2283 2136175 : return conv;
2284 : }
2285 : }
2286 2155417 : else if (TREE_CODE (to) == ARRAY_TYPE)
2287 1667 : return build_array_conv (to, expr, flags, complain);
2288 : }
2289 :
2290 279911785 : if (expr != NULL_TREE
2291 273295475 : && (MAYBE_CLASS_TYPE_P (from)
2292 145232349 : || MAYBE_CLASS_TYPE_P (to))
2293 478424203 : && (flags & LOOKUP_NO_CONVERSION) == 0)
2294 : {
2295 74602381 : struct z_candidate *cand;
2296 :
2297 51019388 : if (CLASS_TYPE_P (to)
2298 51019198 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
2299 76733305 : && !CLASSTYPE_NON_AGGREGATE (complete_type (to)))
2300 1232055 : return build_aggr_conv (to, expr, flags, complain);
2301 :
2302 73370326 : cand = build_user_type_conversion_1 (to, expr, flags, complain);
2303 73370326 : if (cand)
2304 : {
2305 885748 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
2306 876365 : && CONSTRUCTOR_NELTS (expr) == 1
2307 56981 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr)
2308 13914137 : && !is_list_ctor (cand->fn))
2309 : {
2310 : /* "If C is not an initializer-list constructor and the
2311 : initializer list has a single element of type cv U, where U is
2312 : X or a class derived from X, the implicit conversion sequence
2313 : has Exact Match rank if U is X, or Conversion rank if U is
2314 : derived from X." */
2315 56613 : tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
2316 56613 : tree elttype = TREE_TYPE (elt);
2317 56613 : if (reference_related_p (to, elttype))
2318 79 : return implicit_conversion (to, elttype, elt,
2319 79 : c_cast_p, flags, complain);
2320 : }
2321 13857077 : conv = cand->second_conv;
2322 : }
2323 :
2324 : /* We used to try to bind a reference to a temporary here, but that
2325 : is now handled after the recursive call to this function at the end
2326 : of reference_binding. */
2327 73370247 : return conv;
2328 : }
2329 :
2330 : return NULL;
2331 : }
2332 :
2333 : /* Like implicit_conversion, but return NULL if the conversion is bad.
2334 :
2335 : This is not static so that check_non_deducible_conversion can call it within
2336 : add_template_candidate_real as part of overload resolution; it should not be
2337 : called outside of overload resolution. */
2338 :
2339 : conversion *
2340 6392300 : good_conversion (tree to, tree from, tree expr,
2341 : int flags, tsubst_flags_t complain)
2342 : {
2343 6392300 : conversion *c = implicit_conversion (to, from, expr, /*cast*/false,
2344 : flags, complain);
2345 6392300 : if (c && c->bad_p)
2346 2609577 : c = NULL;
2347 6392300 : return c;
2348 : }
2349 :
2350 : /* Add a new entry to the list of candidates. Used by the add_*_candidate
2351 : functions. ARGS will not be changed until a single candidate is
2352 : selected. */
2353 :
2354 : static struct z_candidate *
2355 994100666 : add_candidate (struct z_candidate **candidates,
2356 : tree fn, tree first_arg, const vec<tree, va_gc> *args,
2357 : size_t num_convs, conversion **convs,
2358 : tree access_path, tree conversion_path,
2359 : int viable, struct rejection_reason *reason,
2360 : int flags)
2361 : {
2362 994100666 : struct z_candidate *cand = (struct z_candidate *)
2363 994100666 : conversion_obstack_alloc (sizeof (struct z_candidate));
2364 :
2365 994100666 : cand->fn = fn;
2366 994100666 : cand->first_arg = first_arg;
2367 994100666 : cand->args = args;
2368 994100666 : cand->convs = convs;
2369 994100666 : cand->num_convs = num_convs;
2370 994100666 : cand->access_path = access_path;
2371 994100666 : cand->conversion_path = conversion_path;
2372 994100666 : cand->viable = viable;
2373 994100666 : cand->reason = reason;
2374 994100666 : cand->next = *candidates;
2375 994100666 : cand->flags = flags;
2376 994100666 : *candidates = cand;
2377 :
2378 994100666 : if (convs && cand->reversed ())
2379 : /* Swap the conversions for comparison in joust; we'll swap them back
2380 : before build_over_call. */
2381 80184128 : std::swap (convs[0], convs[1]);
2382 :
2383 994100666 : return cand;
2384 : }
2385 :
2386 : /* FN is a function from the overload set that we outright didn't even
2387 : consider (for some reason); add it to the list as an non-viable "ignored"
2388 : candidate. */
2389 :
2390 : static z_candidate *
2391 628443196 : add_ignored_candidate (z_candidate **candidates, tree fn)
2392 : {
2393 : /* No need to dynamically allocate these. */
2394 628443196 : static const rejection_reason reason_ignored = { rr_ignored, {} };
2395 :
2396 628443196 : struct z_candidate *cand = (struct z_candidate *)
2397 628438686 : conversion_obstack_alloc (sizeof (struct z_candidate));
2398 :
2399 628443196 : cand->fn = fn;
2400 628443196 : cand->reason = const_cast<rejection_reason *> (&reason_ignored);
2401 628443196 : cand->next = *candidates;
2402 628443196 : *candidates = cand;
2403 :
2404 628443196 : return cand;
2405 : }
2406 :
2407 : /* True iff CAND is a candidate added by add_ignored_candidate. */
2408 :
2409 : static bool
2410 559980837 : ignored_candidate_p (const z_candidate *cand)
2411 : {
2412 559974782 : return cand->reason && cand->reason->code == rr_ignored;
2413 : }
2414 :
2415 : /* Return the number of remaining arguments in the parameter list
2416 : beginning with ARG. */
2417 :
2418 : int
2419 145158398 : remaining_arguments (tree arg)
2420 : {
2421 145158398 : int n;
2422 :
2423 252589341 : for (n = 0; arg != NULL_TREE && arg != void_list_node;
2424 107430943 : arg = TREE_CHAIN (arg))
2425 107430943 : n++;
2426 :
2427 145158398 : return n;
2428 : }
2429 :
2430 : /* [over.match.copy]: When initializing a temporary object (12.2) to be bound
2431 : to the first parameter of a constructor where the parameter is of type
2432 : "reference to possibly cv-qualified T" and the constructor is called with a
2433 : single argument in the context of direct-initialization of an object of type
2434 : "cv2 T", explicit conversion functions are also considered.
2435 :
2436 : So set LOOKUP_COPY_PARM to let reference_binding know that
2437 : it's being called in that context. */
2438 :
2439 : int
2440 415360782 : conv_flags (int i, int nargs, tree fn, tree arg, int flags)
2441 : {
2442 415360782 : int lflags = flags;
2443 415360782 : tree t;
2444 430033918 : if (i == 0 && nargs == 1 && DECL_CONSTRUCTOR_P (fn)
2445 139074223 : && (t = FUNCTION_FIRST_USER_PARMTYPE (fn))
2446 554435005 : && (same_type_ignoring_top_level_qualifiers_p
2447 139074223 : (non_reference (TREE_VALUE (t)), DECL_CONTEXT (fn))))
2448 : {
2449 108338777 : if (!(flags & LOOKUP_ONLYCONVERTING))
2450 33077896 : lflags |= LOOKUP_COPY_PARM;
2451 108338777 : if ((flags & LOOKUP_LIST_INIT_CTOR)
2452 108338777 : && BRACE_ENCLOSED_INITIALIZER_P (arg))
2453 1477 : lflags |= LOOKUP_NO_CONVERSION;
2454 : }
2455 : else
2456 307022005 : lflags |= LOOKUP_ONLYCONVERTING;
2457 :
2458 415360782 : return lflags;
2459 : }
2460 :
2461 : /* Build an appropriate 'this' conversion for the method FN and class
2462 : type CTYPE from the value ARG (having type ARGTYPE) to the type PARMTYPE.
2463 : This function modifies PARMTYPE, ARGTYPE and ARG. */
2464 :
2465 : static conversion *
2466 89903846 : build_this_conversion (tree fn, tree ctype,
2467 : tree& parmtype, tree& argtype, tree& arg,
2468 : int flags, tsubst_flags_t complain)
2469 : {
2470 179807692 : gcc_assert (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
2471 : && !DECL_CONSTRUCTOR_P (fn));
2472 :
2473 : /* The type of the implicit object parameter ('this') for
2474 : overload resolution is not always the same as for the
2475 : function itself; conversion functions are considered to
2476 : be members of the class being converted, and functions
2477 : introduced by a using-declaration are considered to be
2478 : members of the class that uses them.
2479 :
2480 : Since build_over_call ignores the ICS for the `this'
2481 : parameter, we can just change the parm type. */
2482 89903846 : parmtype = cp_build_qualified_type (ctype,
2483 89903846 : cp_type_quals (TREE_TYPE (parmtype)));
2484 89903846 : bool this_p = true;
2485 89903846 : if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn)))
2486 : {
2487 : /* If the function has a ref-qualifier, the implicit
2488 : object parameter has reference type. */
2489 156849 : bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn));
2490 156849 : parmtype = cp_build_reference_type (parmtype, rv);
2491 : /* The special handling of 'this' conversions in compare_ics
2492 : does not apply if there is a ref-qualifier. */
2493 156849 : this_p = false;
2494 : }
2495 : else
2496 : {
2497 89746997 : parmtype = build_pointer_type (parmtype);
2498 : /* We don't use build_this here because we don't want to
2499 : capture the object argument until we've chosen a
2500 : non-static member function. */
2501 89746997 : arg = build_address (arg);
2502 89746997 : argtype = lvalue_type (arg);
2503 : }
2504 89903846 : flags |= LOOKUP_ONLYCONVERTING;
2505 89903846 : conversion *t = implicit_conversion (parmtype, argtype, arg,
2506 : /*c_cast_p=*/false, flags, complain);
2507 89903846 : t->this_p = this_p;
2508 89903846 : return t;
2509 : }
2510 :
2511 : /* Create an overload candidate for the function or method FN called
2512 : with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
2513 : FLAGS is passed on to implicit_conversion.
2514 :
2515 : This does not change ARGS.
2516 :
2517 : CTYPE, if non-NULL, is the type we want to pretend this function
2518 : comes from for purposes of overload resolution.
2519 :
2520 : SHORTCUT_BAD_CONVS controls how we handle "bad" argument conversions.
2521 : If true, we stop computing conversions upon seeing the first bad
2522 : conversion. This is used by add_candidates to avoid computing
2523 : more conversions than necessary in the presence of a strictly viable
2524 : candidate, while preserving the defacto behavior of overload resolution
2525 : when it turns out there are only non-strictly viable candidates. */
2526 :
2527 : static struct z_candidate *
2528 588990914 : add_function_candidate (struct z_candidate **candidates,
2529 : tree fn, tree ctype, tree first_arg,
2530 : const vec<tree, va_gc> *args, tree access_path,
2531 : tree conversion_path, int flags,
2532 : conversion **convs,
2533 : bool shortcut_bad_convs,
2534 : tsubst_flags_t complain)
2535 : {
2536 588990914 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
2537 588990914 : int i, len;
2538 588990914 : tree parmnode;
2539 588990914 : tree orig_first_arg = first_arg;
2540 588990914 : int skip;
2541 588990914 : int viable = 1;
2542 588990914 : struct rejection_reason *reason = NULL;
2543 :
2544 : /* The `this', `in_chrg' and VTT arguments to constructors are not
2545 : considered in overload resolution. */
2546 1177981828 : if (DECL_CONSTRUCTOR_P (fn))
2547 : {
2548 270163463 : if (ctor_omit_inherited_parms (fn))
2549 : /* Bring back parameters omitted from an inherited ctor. */
2550 150 : parmlist = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
2551 : else
2552 270163388 : parmlist = skip_artificial_parms_for (fn, parmlist);
2553 270163463 : skip = num_artificial_parms_for (fn);
2554 270163463 : if (skip > 0 && first_arg != NULL_TREE)
2555 : {
2556 270163463 : --skip;
2557 270163463 : first_arg = NULL_TREE;
2558 : }
2559 : }
2560 : else
2561 : skip = 0;
2562 :
2563 1139661725 : len = vec_safe_length (args) - skip + (first_arg != NULL_TREE ? 1 : 0);
2564 588990914 : if (!convs)
2565 512679586 : convs = alloc_conversions (len);
2566 :
2567 : /* 13.3.2 - Viable functions [over.match.viable]
2568 : First, to be a viable function, a candidate function shall have enough
2569 : parameters to agree in number with the arguments in the list.
2570 :
2571 : We need to check this first; otherwise, checking the ICSes might cause
2572 : us to produce an ill-formed template instantiation. */
2573 :
2574 588990914 : parmnode = parmlist;
2575 1204710285 : for (i = 0; i < len; ++i)
2576 : {
2577 687352752 : if (parmnode == NULL_TREE || parmnode == void_list_node)
2578 : break;
2579 615719371 : parmnode = TREE_CHAIN (parmnode);
2580 : }
2581 :
2582 588990914 : if ((i < len && parmnode)
2583 588990914 : || !sufficient_parms_p (parmnode))
2584 : {
2585 145154937 : int remaining = remaining_arguments (parmnode);
2586 145154937 : viable = 0;
2587 145154937 : reason = arity_rejection (first_arg, i + remaining, len);
2588 : }
2589 :
2590 : /* An inherited constructor (12.6.3 [class.inhctor.init]) that has a first
2591 : parameter of type "reference to cv C" (including such a constructor
2592 : instantiated from a template) is excluded from the set of candidate
2593 : functions when used to construct an object of type D with an argument list
2594 : containing a single argument if C is reference-related to D. */
2595 539339160 : if (viable && len == 1 && parmlist && DECL_CONSTRUCTOR_P (fn)
2596 141280336 : && flag_new_inheriting_ctors
2597 730262565 : && DECL_INHERITED_CTOR (fn))
2598 : {
2599 844767 : tree ptype = non_reference (TREE_VALUE (parmlist));
2600 844767 : tree dtype = DECL_CONTEXT (fn);
2601 1689534 : tree btype = DECL_INHERITED_CTOR_BASE (fn);
2602 844767 : if (reference_related_p (ptype, dtype)
2603 844767 : && reference_related_p (btype, ptype))
2604 : {
2605 599010 : viable = false;
2606 599010 : reason = inherited_ctor_rejection ();
2607 : }
2608 : }
2609 :
2610 : /* Second, for a function to be viable, its constraints must be
2611 : satisfied. */
2612 588990914 : if (flag_concepts && viable && !constraints_satisfied_p (fn))
2613 : {
2614 159273 : reason = constraint_failure ();
2615 159273 : viable = false;
2616 : }
2617 :
2618 : /* When looking for a function from a subobject from an implicit
2619 : copy/move constructor/operator=, don't consider anything that takes (a
2620 : reference to) an unrelated type. See c++/44909 and core 1092. */
2621 588990914 : if (viable && parmlist && (flags & LOOKUP_DEFAULTED))
2622 : {
2623 75382576 : if (DECL_CONSTRUCTOR_P (fn))
2624 : i = 1;
2625 19633109 : else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2626 19633109 : && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
2627 : i = 2;
2628 : else
2629 : i = 0;
2630 37691288 : if (i && len == i)
2631 : {
2632 20741320 : parmnode = chain_index (i-1, parmlist);
2633 20741320 : if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
2634 : ctype))
2635 4197549 : viable = 0;
2636 : }
2637 :
2638 : /* This only applies at the top level. */
2639 33493739 : flags &= ~LOOKUP_DEFAULTED;
2640 : }
2641 :
2642 588990914 : if (! viable)
2643 150110769 : goto out;
2644 :
2645 438880145 : if (shortcut_bad_convs)
2646 438750755 : flags |= LOOKUP_SHORTCUT_BAD_CONVS;
2647 : else
2648 129390 : flags &= ~LOOKUP_SHORTCUT_BAD_CONVS;
2649 :
2650 : /* Third, for F to be a viable function, there shall exist for each
2651 : argument an implicit conversion sequence that converts that argument
2652 : to the corresponding parameter of F. */
2653 :
2654 438880145 : parmnode = parmlist;
2655 :
2656 756074509 : for (i = 0; i < len; ++i)
2657 : {
2658 482963937 : tree argtype, to_type;
2659 482963937 : tree arg;
2660 :
2661 482963937 : if (parmnode == void_list_node)
2662 : break;
2663 :
2664 482963937 : if (convs[i])
2665 : {
2666 : /* Already set during deduction. */
2667 5637293 : parmnode = TREE_CHAIN (parmnode);
2668 5637293 : continue;
2669 : }
2670 :
2671 477326644 : if (i == 0 && first_arg != NULL_TREE)
2672 86455258 : arg = first_arg;
2673 : else
2674 390871386 : arg = const_cast<tree> (
2675 748198665 : (*args)[i + skip - (first_arg != NULL_TREE ? 1 : 0)]);
2676 477326644 : argtype = lvalue_type (arg);
2677 :
2678 477326644 : conversion *t;
2679 477326644 : if (parmnode)
2680 : {
2681 472182626 : tree parmtype = TREE_VALUE (parmnode);
2682 472182626 : if (i == 0
2683 381771700 : && DECL_IOBJ_MEMBER_FUNCTION_P (fn)
2684 925166110 : && !DECL_CONSTRUCTOR_P (fn))
2685 86446260 : t = build_this_conversion (fn, ctype, parmtype, argtype, arg,
2686 : flags, complain);
2687 : else
2688 : {
2689 385736366 : int lflags = conv_flags (i, len-skip, fn, arg, flags);
2690 385736366 : t = implicit_conversion (parmtype, argtype, arg,
2691 : /*c_cast_p=*/false, lflags, complain);
2692 : }
2693 472182626 : to_type = parmtype;
2694 472182626 : parmnode = TREE_CHAIN (parmnode);
2695 : }
2696 : else
2697 : {
2698 5144018 : t = build_identity_conv (argtype, arg);
2699 5144018 : t->ellipsis_p = true;
2700 5144018 : to_type = argtype;
2701 : }
2702 :
2703 477326644 : convs[i] = t;
2704 477326644 : if (! t)
2705 : {
2706 144899303 : viable = 0;
2707 144899303 : reason = arg_conversion_rejection (first_arg, i, argtype, to_type,
2708 144899303 : EXPR_LOCATION (arg));
2709 144899303 : break;
2710 : }
2711 :
2712 332427341 : if (t->bad_p)
2713 : {
2714 20901834 : viable = -1;
2715 20901834 : reason = bad_arg_conversion_rejection (first_arg, i, arg, to_type,
2716 20901834 : EXPR_LOCATION (arg));
2717 20901834 : if (shortcut_bad_convs)
2718 : break;
2719 : }
2720 : }
2721 :
2722 273110572 : out:
2723 588990914 : return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
2724 588990914 : access_path, conversion_path, viable, reason, flags);
2725 : }
2726 :
2727 : /* Create an overload candidate for the conversion function FN which will
2728 : be invoked for expression OBJ, producing a pointer-to-function which
2729 : will in turn be called with the argument list FIRST_ARG/ARGLIST,
2730 : and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
2731 : passed on to implicit_conversion.
2732 :
2733 : Actually, we don't really care about FN; we care about the type it
2734 : converts to. There may be multiple conversion functions that will
2735 : convert to that type, and we rely on build_user_type_conversion_1 to
2736 : choose the best one; so when we create our candidate, we record the type
2737 : instead of the function. */
2738 :
2739 : static struct z_candidate *
2740 95862 : add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
2741 : const vec<tree, va_gc> *arglist,
2742 : tree access_path, tree conversion_path,
2743 : tsubst_flags_t complain)
2744 : {
2745 95862 : tree totype = TREE_TYPE (TREE_TYPE (fn));
2746 95862 : int i, len, viable, flags;
2747 95862 : tree parmlist, parmnode;
2748 95862 : conversion **convs;
2749 95862 : struct rejection_reason *reason;
2750 :
2751 191886 : for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
2752 96024 : parmlist = TREE_TYPE (parmlist);
2753 95862 : parmlist = TYPE_ARG_TYPES (parmlist);
2754 :
2755 95862 : len = vec_safe_length (arglist) + 1;
2756 95862 : convs = alloc_conversions (len);
2757 95862 : parmnode = parmlist;
2758 95862 : viable = 1;
2759 95862 : flags = LOOKUP_IMPLICIT;
2760 95862 : reason = NULL;
2761 :
2762 : /* Don't bother looking up the same type twice. */
2763 95862 : if (*candidates && (*candidates)->fn == totype)
2764 : return NULL;
2765 :
2766 95847 : if (!constraints_satisfied_p (fn))
2767 : {
2768 9 : reason = constraint_failure ();
2769 9 : viable = 0;
2770 9 : return add_candidate (candidates, fn, obj, arglist, len, convs,
2771 9 : access_path, conversion_path, viable, reason, flags);
2772 : }
2773 :
2774 313349 : for (i = 0; i < len; ++i)
2775 : {
2776 217676 : tree arg, argtype, convert_type = NULL_TREE;
2777 217676 : conversion *t;
2778 :
2779 217676 : if (i == 0)
2780 : arg = obj;
2781 : else
2782 121838 : arg = (*arglist)[i - 1];
2783 217676 : argtype = lvalue_type (arg);
2784 :
2785 217676 : if (i == 0)
2786 : {
2787 95838 : t = build_identity_conv (argtype, NULL_TREE);
2788 95838 : t = build_conv (ck_user, totype, t);
2789 : /* Leave the 'cand' field null; we'll figure out the conversion in
2790 : convert_like if this candidate is chosen. */
2791 95838 : convert_type = totype;
2792 : }
2793 121838 : else if (parmnode == void_list_node)
2794 : break;
2795 121742 : else if (parmnode)
2796 : {
2797 121733 : t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
2798 : /*c_cast_p=*/false, flags, complain);
2799 121733 : convert_type = TREE_VALUE (parmnode);
2800 : }
2801 : else
2802 : {
2803 9 : t = build_identity_conv (argtype, arg);
2804 9 : t->ellipsis_p = true;
2805 9 : convert_type = argtype;
2806 : }
2807 :
2808 217580 : convs[i] = t;
2809 217580 : if (! t)
2810 : break;
2811 :
2812 217511 : if (t->bad_p)
2813 : {
2814 26 : viable = -1;
2815 69 : reason = bad_arg_conversion_rejection (NULL_TREE, i, arg, convert_type,
2816 26 : EXPR_LOCATION (arg));
2817 : }
2818 :
2819 217511 : if (i == 0)
2820 95838 : continue;
2821 :
2822 121673 : if (parmnode)
2823 121664 : parmnode = TREE_CHAIN (parmnode);
2824 : }
2825 :
2826 95838 : if (i < len
2827 95838 : || ! sufficient_parms_p (parmnode))
2828 : {
2829 189 : int remaining = remaining_arguments (parmnode);
2830 189 : viable = 0;
2831 189 : reason = arity_rejection (NULL_TREE, i + remaining, len);
2832 : }
2833 :
2834 95838 : return add_candidate (candidates, totype, obj, arglist, len, convs,
2835 95838 : access_path, conversion_path, viable, reason, flags);
2836 : }
2837 :
2838 : static void
2839 9034777 : build_builtin_candidate (struct z_candidate **candidates, tree fnname,
2840 : tree type1, tree type2, const vec<tree,va_gc> &args,
2841 : tree *argtypes, int flags, tsubst_flags_t complain)
2842 : {
2843 9034777 : conversion *t;
2844 9034777 : conversion **convs;
2845 9034777 : size_t num_convs;
2846 9034777 : int viable = 1;
2847 9034777 : tree types[2];
2848 9034777 : struct rejection_reason *reason = NULL;
2849 :
2850 9034777 : types[0] = type1;
2851 9034777 : types[1] = type2;
2852 :
2853 9034777 : num_convs = args.length ();
2854 9034777 : convs = alloc_conversions (num_convs);
2855 :
2856 : /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2857 : conversion ops are allowed. We handle that here by just checking for
2858 : boolean_type_node because other operators don't ask for it. COND_EXPR
2859 : also does contextual conversion to bool for the first operand, but we
2860 : handle that in build_conditional_expr, and type1 here is operand 2. */
2861 9034777 : if (type1 != boolean_type_node)
2862 8490997 : flags |= LOOKUP_ONLYCONVERTING;
2863 :
2864 26398304 : for (unsigned i = 0; i < 2 && i < num_convs; ++i)
2865 : {
2866 17363527 : t = implicit_conversion (types[i], argtypes[i], args[i],
2867 : /*c_cast_p=*/false, flags, complain);
2868 17363527 : if (! t)
2869 : {
2870 1820874 : viable = 0;
2871 : /* We need something for printing the candidate. */
2872 1820874 : t = build_identity_conv (types[i], NULL_TREE);
2873 1820874 : reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2874 1820874 : types[i], EXPR_LOCATION (args[i]));
2875 : }
2876 15542653 : else if (t->bad_p)
2877 : {
2878 174 : viable = 0;
2879 174 : reason = bad_arg_conversion_rejection (NULL_TREE, i, args[i],
2880 : types[i],
2881 174 : EXPR_LOCATION (args[i]));
2882 : }
2883 17363527 : convs[i] = t;
2884 : }
2885 :
2886 : /* For COND_EXPR we rearranged the arguments; undo that now. */
2887 9034777 : if (num_convs == 3)
2888 : {
2889 144 : convs[2] = convs[1];
2890 144 : convs[1] = convs[0];
2891 144 : t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
2892 : /*c_cast_p=*/false, flags,
2893 : complain);
2894 144 : if (t)
2895 144 : convs[0] = t;
2896 : else
2897 : {
2898 0 : viable = 0;
2899 0 : reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2],
2900 : boolean_type_node,
2901 0 : EXPR_LOCATION (args[2]));
2902 : }
2903 : }
2904 :
2905 9034777 : add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
2906 : num_convs, convs,
2907 : /*access_path=*/NULL_TREE,
2908 : /*conversion_path=*/NULL_TREE,
2909 : viable, reason, flags);
2910 9034777 : }
2911 :
2912 : static bool
2913 9 : is_complete (tree t)
2914 : {
2915 9 : return COMPLETE_TYPE_P (complete_type (t));
2916 : }
2917 :
2918 : /* Returns nonzero if TYPE is a promoted arithmetic type. */
2919 :
2920 : static bool
2921 2916 : promoted_arithmetic_type_p (tree type)
2922 : {
2923 : /* [over.built]
2924 :
2925 : In this section, the term promoted integral type is used to refer
2926 : to those integral types which are preserved by integral promotion
2927 : (including e.g. int and long but excluding e.g. char).
2928 : Similarly, the term promoted arithmetic type refers to promoted
2929 : integral types plus floating types. */
2930 2916 : return ((CP_INTEGRAL_TYPE_P (type)
2931 218 : && same_type_p (type_promotes_to (type), type))
2932 2916 : || SCALAR_FLOAT_TYPE_P (type));
2933 : }
2934 :
2935 : /* Create any builtin operator overload candidates for the operator in
2936 : question given the converted operand types TYPE1 and TYPE2. The other
2937 : args are passed through from add_builtin_candidates to
2938 : build_builtin_candidate.
2939 :
2940 : TYPE1 and TYPE2 may not be permissible, and we must filter them.
2941 : If CODE is requires candidates operands of the same type of the kind
2942 : of which TYPE1 and TYPE2 are, we add both candidates
2943 : CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2944 :
2945 : static void
2946 13317974 : add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
2947 : enum tree_code code2, tree fnname, tree type1,
2948 : tree type2, vec<tree,va_gc> &args, tree *argtypes,
2949 : int flags, tsubst_flags_t complain)
2950 : {
2951 13317974 : switch (code)
2952 : {
2953 21 : case POSTINCREMENT_EXPR:
2954 21 : case POSTDECREMENT_EXPR:
2955 21 : args[1] = integer_zero_node;
2956 21 : type2 = integer_type_node;
2957 21 : break;
2958 : default:
2959 : break;
2960 : }
2961 :
2962 13317974 : switch (code)
2963 : {
2964 :
2965 : /* 4 For every pair (T, VQ), where T is an arithmetic type other than bool,
2966 : and VQ is either volatile or empty, there exist candidate operator
2967 : functions of the form
2968 : VQ T& operator++(VQ T&);
2969 : T operator++(VQ T&, int);
2970 : 5 For every pair (T, VQ), where T is an arithmetic type other than bool,
2971 : and VQ is either volatile or empty, there exist candidate operator
2972 : functions of the form
2973 : VQ T& operator--(VQ T&);
2974 : T operator--(VQ T&, int);
2975 : 6 For every pair (T, VQ), where T is a cv-qualified or cv-unqualified object
2976 : type, and VQ is either volatile or empty, there exist candidate operator
2977 : functions of the form
2978 : T*VQ& operator++(T*VQ&);
2979 : T*VQ& operator--(T*VQ&);
2980 : T* operator++(T*VQ&, int);
2981 : T* operator--(T*VQ&, int); */
2982 :
2983 18 : case POSTDECREMENT_EXPR:
2984 18 : case PREDECREMENT_EXPR:
2985 18 : if (TREE_CODE (type1) == BOOLEAN_TYPE)
2986 : return;
2987 : /* FALLTHRU */
2988 39 : case POSTINCREMENT_EXPR:
2989 39 : case PREINCREMENT_EXPR:
2990 : /* P0002R1, Remove deprecated operator++(bool) added "other than bool"
2991 : to p4. */
2992 39 : if (TREE_CODE (type1) == BOOLEAN_TYPE && cxx_dialect >= cxx17)
2993 : return;
2994 33 : if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
2995 : {
2996 24 : type1 = build_reference_type (type1);
2997 24 : break;
2998 : }
2999 : return;
3000 :
3001 : /* 7 For every cv-qualified or cv-unqualified object type T, there
3002 : exist candidate operator functions of the form
3003 :
3004 : T& operator*(T*);
3005 :
3006 :
3007 : 8 For every function type T that does not have cv-qualifiers or
3008 : a ref-qualifier, there exist candidate operator functions of the form
3009 : T& operator*(T*); */
3010 :
3011 108718 : case INDIRECT_REF:
3012 108718 : if (TYPE_PTR_P (type1)
3013 108718 : && (TYPE_PTROB_P (type1)
3014 12 : || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
3015 : break;
3016 : return;
3017 :
3018 : /* 9 For every type T, there exist candidate operator functions of the form
3019 : T* operator+(T*);
3020 :
3021 : 10 For every floating-point or promoted integral type T, there exist
3022 : candidate operator functions of the form
3023 : T operator+(T);
3024 : T operator-(T); */
3025 :
3026 320 : case UNARY_PLUS_EXPR: /* unary + */
3027 320 : if (TYPE_PTR_P (type1))
3028 : break;
3029 : /* FALLTHRU */
3030 195559 : case NEGATE_EXPR:
3031 195559 : if (ARITHMETIC_TYPE_P (type1))
3032 : break;
3033 : return;
3034 :
3035 : /* 11 For every promoted integral type T, there exist candidate operator
3036 : functions of the form
3037 : T operator~(T); */
3038 :
3039 152491 : case BIT_NOT_EXPR:
3040 152491 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
3041 : break;
3042 : return;
3043 :
3044 : /* 12 For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, C1
3045 : is the same type as C2 or is a derived class of C2, and T is an object
3046 : type or a function type there exist candidate operator functions of the
3047 : form
3048 : CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
3049 : where CV12 is the union of CV1 and CV2. */
3050 :
3051 28 : case MEMBER_REF:
3052 28 : if (TYPE_PTR_P (type1) && TYPE_PTRMEM_P (type2))
3053 : {
3054 28 : tree c1 = TREE_TYPE (type1);
3055 28 : tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
3056 :
3057 25 : if (CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
3058 50 : && (TYPE_PTRMEMFUNC_P (type2)
3059 9 : || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
3060 : break;
3061 : }
3062 : return;
3063 :
3064 : /* 13 For every pair of types L and R, where each of L and R is a floating-point
3065 : or promoted integral type, there exist candidate operator functions of the
3066 : form
3067 : LR operator*(L, R);
3068 : LR operator/(L, R);
3069 : LR operator+(L, R);
3070 : LR operator-(L, R);
3071 : bool operator<(L, R);
3072 : bool operator>(L, R);
3073 : bool operator<=(L, R);
3074 : bool operator>=(L, R);
3075 : bool operator==(L, R);
3076 : bool operator!=(L, R);
3077 : where LR is the result of the usual arithmetic conversions between
3078 : types L and R.
3079 :
3080 : 14 For every integral type T there exists a candidate operator function of
3081 : the form
3082 :
3083 : std::strong_ordering operator<=>(T, T);
3084 :
3085 : 15 For every pair of floating-point types L and R, there exists a candidate
3086 : operator function of the form
3087 :
3088 : std::partial_ordering operator<=>(L, R);
3089 :
3090 : 16 For every cv-qualified or cv-unqualified object type T there exist
3091 : candidate operator functions of the form
3092 : T* operator+(T*, std::ptrdiff_t);
3093 : T& operator[](T*, std::ptrdiff_t);
3094 : T* operator-(T*, std::ptrdiff_t);
3095 : T* operator+(std::ptrdiff_t, T*);
3096 : T& operator[](std::ptrdiff_t, T*);
3097 :
3098 : 17 For every T, where T is a pointer to object type, there exist candidate
3099 : operator functions of the form
3100 : std::ptrdiff_t operator-(T, T);
3101 :
3102 : 18 For every T, where T is an enumeration type or a pointer type, there
3103 : exist candidate operator functions of the form
3104 : bool operator<(T, T);
3105 : bool operator>(T, T);
3106 : bool operator<=(T, T);
3107 : bool operator>=(T, T);
3108 : bool operator==(T, T);
3109 : bool operator!=(T, T);
3110 : R operator<=>(T, T);
3111 :
3112 : where R is the result type specified in [expr.spaceship].
3113 :
3114 : 19 For every T, where T is a pointer-to-member type or std::nullptr_t,
3115 : there exist candidate operator functions of the form
3116 : bool operator==(T, T);
3117 : bool operator!=(T, T); */
3118 :
3119 192985 : case MINUS_EXPR:
3120 192985 : if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
3121 : break;
3122 13 : if (TYPE_PTROB_P (type1)
3123 192971 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3124 : {
3125 4 : type2 = ptrdiff_type_node;
3126 4 : break;
3127 : }
3128 : /* FALLTHRU */
3129 842544 : case MULT_EXPR:
3130 842544 : case TRUNC_DIV_EXPR:
3131 842544 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3132 : break;
3133 : return;
3134 :
3135 : /* This isn't exactly what's specified above for operator<=>, but it's
3136 : close enough. In particular, we don't care about the return type
3137 : specified above; it doesn't participate in overload resolution and it
3138 : doesn't affect the semantics of the built-in operator. */
3139 6372409 : case SPACESHIP_EXPR:
3140 6372409 : case EQ_EXPR:
3141 6372409 : case NE_EXPR:
3142 207985 : if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
3143 6580394 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)))
3144 : break;
3145 6372375 : if (NULLPTR_TYPE_P (type1) && NULLPTR_TYPE_P (type2))
3146 : break;
3147 6372369 : if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (args[1]))
3148 : {
3149 : type2 = type1;
3150 : break;
3151 : }
3152 6372366 : if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (args[0]))
3153 : {
3154 : type1 = type2;
3155 : break;
3156 : }
3157 : /* Fall through. */
3158 7056121 : case LT_EXPR:
3159 7056121 : case GT_EXPR:
3160 7056121 : case LE_EXPR:
3161 7056121 : case GE_EXPR:
3162 7056121 : case MAX_EXPR:
3163 7056121 : case MIN_EXPR:
3164 7056121 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3165 : break;
3166 5433146 : if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3167 : break;
3168 5429468 : if (TREE_CODE (type1) == ENUMERAL_TYPE
3169 3880317 : && TREE_CODE (type2) == ENUMERAL_TYPE)
3170 : break;
3171 2649878 : if (TYPE_PTR_P (type1)
3172 2649878 : && null_ptr_cst_p (args[1]))
3173 : {
3174 : type2 = type1;
3175 : break;
3176 : }
3177 2649859 : if (null_ptr_cst_p (args[0])
3178 2649859 : && TYPE_PTR_P (type2))
3179 : {
3180 : type1 = type2;
3181 : break;
3182 : }
3183 : return;
3184 :
3185 221131 : case PLUS_EXPR:
3186 221131 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3187 : break;
3188 : /* FALLTHRU */
3189 166388 : case ARRAY_REF:
3190 166388 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
3191 : {
3192 6 : type1 = ptrdiff_type_node;
3193 6 : break;
3194 : }
3195 166382 : if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3196 : {
3197 36089 : type2 = ptrdiff_type_node;
3198 36089 : break;
3199 : }
3200 : return;
3201 :
3202 : /* 18For every pair of promoted integral types L and R, there exist candi-
3203 : date operator functions of the form
3204 : LR operator%(L, R);
3205 : LR operator&(L, R);
3206 : LR operator^(L, R);
3207 : LR operator|(L, R);
3208 : L operator<<(L, R);
3209 : L operator>>(L, R);
3210 : where LR is the result of the usual arithmetic conversions between
3211 : types L and R. */
3212 :
3213 2957396 : case TRUNC_MOD_EXPR:
3214 2957396 : case BIT_AND_EXPR:
3215 2957396 : case BIT_IOR_EXPR:
3216 2957396 : case BIT_XOR_EXPR:
3217 2957396 : case LSHIFT_EXPR:
3218 2957396 : case RSHIFT_EXPR:
3219 2957396 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3220 : break;
3221 : return;
3222 :
3223 : /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
3224 : type, VQ is either volatile or empty, and R is a promoted arithmetic
3225 : type, there exist candidate operator functions of the form
3226 : VQ L& operator=(VQ L&, R);
3227 : VQ L& operator*=(VQ L&, R);
3228 : VQ L& operator/=(VQ L&, R);
3229 : VQ L& operator+=(VQ L&, R);
3230 : VQ L& operator-=(VQ L&, R);
3231 :
3232 : 20For every pair T, VQ), where T is any type and VQ is either volatile
3233 : or empty, there exist candidate operator functions of the form
3234 : T*VQ& operator=(T*VQ&, T*);
3235 :
3236 : 21For every pair T, VQ), where T is a pointer to member type and VQ is
3237 : either volatile or empty, there exist candidate operator functions of
3238 : the form
3239 : VQ T& operator=(VQ T&, T);
3240 :
3241 : 22For every triple T, VQ, I), where T is a cv-qualified or cv-
3242 : unqualified complete object type, VQ is either volatile or empty, and
3243 : I is a promoted integral type, there exist candidate operator func-
3244 : tions of the form
3245 : T*VQ& operator+=(T*VQ&, I);
3246 : T*VQ& operator-=(T*VQ&, I);
3247 :
3248 : 23For every triple L, VQ, R), where L is an integral or enumeration
3249 : type, VQ is either volatile or empty, and R is a promoted integral
3250 : type, there exist candidate operator functions of the form
3251 :
3252 : VQ L& operator%=(VQ L&, R);
3253 : VQ L& operator<<=(VQ L&, R);
3254 : VQ L& operator>>=(VQ L&, R);
3255 : VQ L& operator&=(VQ L&, R);
3256 : VQ L& operator^=(VQ L&, R);
3257 : VQ L& operator|=(VQ L&, R); */
3258 :
3259 1736113 : case MODIFY_EXPR:
3260 1736113 : switch (code2)
3261 : {
3262 100052 : case PLUS_EXPR:
3263 100052 : case MINUS_EXPR:
3264 100052 : if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3265 : {
3266 0 : type2 = ptrdiff_type_node;
3267 0 : break;
3268 : }
3269 : /* FALLTHRU */
3270 100057 : case MULT_EXPR:
3271 100057 : case TRUNC_DIV_EXPR:
3272 100057 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3273 : break;
3274 : return;
3275 :
3276 1636056 : case TRUNC_MOD_EXPR:
3277 1636056 : case BIT_AND_EXPR:
3278 1636056 : case BIT_IOR_EXPR:
3279 1636056 : case BIT_XOR_EXPR:
3280 1636056 : case LSHIFT_EXPR:
3281 1636056 : case RSHIFT_EXPR:
3282 1636056 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3283 : break;
3284 : return;
3285 :
3286 0 : case NOP_EXPR:
3287 0 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3288 : break;
3289 0 : if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
3290 0 : || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3291 0 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
3292 0 : || ((TYPE_PTRMEMFUNC_P (type1)
3293 0 : || TYPE_PTR_P (type1))
3294 0 : && null_ptr_cst_p (args[1])))
3295 : {
3296 : type2 = type1;
3297 : break;
3298 : }
3299 : return;
3300 :
3301 0 : default:
3302 0 : gcc_unreachable ();
3303 : }
3304 1400015 : type1 = build_reference_type (type1);
3305 1400015 : break;
3306 :
3307 2721 : case COND_EXPR:
3308 : /* [over.built]
3309 :
3310 : For every pair of promoted arithmetic types L and R, there
3311 : exist candidate operator functions of the form
3312 :
3313 : LR operator?(bool, L, R);
3314 :
3315 : where LR is the result of the usual arithmetic conversions
3316 : between types L and R.
3317 :
3318 : For every type T, where T is a pointer or pointer-to-member
3319 : type, there exist candidate operator functions of the form T
3320 : operator?(bool, T, T); */
3321 :
3322 2721 : if (promoted_arithmetic_type_p (type1)
3323 2721 : && promoted_arithmetic_type_p (type2))
3324 : /* That's OK. */
3325 : break;
3326 :
3327 : /* Otherwise, the types should be pointers. */
3328 2698 : if (!((TYPE_PTR_OR_PTRMEM_P (type1) || null_ptr_cst_p (args[0]))
3329 337 : && (TYPE_PTR_OR_PTRMEM_P (type2) || null_ptr_cst_p (args[1]))))
3330 2577 : return;
3331 :
3332 : /* We don't check that the two types are the same; the logic
3333 : below will actually create two candidates; one in which both
3334 : parameter types are TYPE1, and one in which both parameter
3335 : types are TYPE2. */
3336 : break;
3337 :
3338 3 : case REALPART_EXPR:
3339 3 : case IMAGPART_EXPR:
3340 3 : if (ARITHMETIC_TYPE_P (type1))
3341 : break;
3342 : return;
3343 :
3344 0 : default:
3345 0 : gcc_unreachable ();
3346 : }
3347 :
3348 : /* Make sure we don't create builtin candidates with dependent types. */
3349 8490873 : bool u1 = uses_template_parms (type1);
3350 8490873 : bool u2 = type2 ? uses_template_parms (type2) : false;
3351 8490860 : if (u1 || u2)
3352 : {
3353 : /* Try to recover if one of the types is non-dependent. But if
3354 : there's only one type, there's nothing we can do. */
3355 19 : if (!type2)
3356 : return;
3357 : /* And we lose if both are dependent. */
3358 16 : if (u1 && u2)
3359 : return;
3360 : /* Or if they have different forms. */
3361 9 : if (TREE_CODE (type1) != TREE_CODE (type2))
3362 : return;
3363 :
3364 9 : if (u1 && !u2)
3365 : type1 = type2;
3366 6 : else if (u2 && !u1)
3367 8490863 : type2 = type1;
3368 : }
3369 :
3370 : /* If we're dealing with two pointer types or two enumeral types,
3371 : we need candidates for both of them. */
3372 8265083 : if (type2 && !same_type_p (type1, type2)
3373 1863005 : && TREE_CODE (type1) == TREE_CODE (type2)
3374 8917475 : && (TYPE_REF_P (type1)
3375 426612 : || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3376 426501 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
3377 426483 : || TYPE_PTRMEMFUNC_P (type1)
3378 426483 : || MAYBE_CLASS_TYPE_P (type1)
3379 426483 : || TREE_CODE (type1) == ENUMERAL_TYPE))
3380 : {
3381 223 : if (TYPE_PTR_OR_PTRMEM_P (type1))
3382 : {
3383 129 : tree cptype = composite_pointer_type (input_location,
3384 : type1, type2,
3385 : error_mark_node,
3386 : error_mark_node,
3387 : CPO_CONVERSION,
3388 : tf_none);
3389 129 : if (cptype != error_mark_node)
3390 : {
3391 89 : build_builtin_candidate
3392 89 : (candidates, fnname, cptype, cptype, args, argtypes,
3393 : flags, complain);
3394 89 : return;
3395 : }
3396 : }
3397 :
3398 134 : build_builtin_candidate
3399 134 : (candidates, fnname, type1, type1, args, argtypes, flags, complain);
3400 134 : build_builtin_candidate
3401 134 : (candidates, fnname, type2, type2, args, argtypes, flags, complain);
3402 134 : return;
3403 : }
3404 :
3405 8490640 : build_builtin_candidate
3406 8490640 : (candidates, fnname, type1, type2, args, argtypes, flags, complain);
3407 : }
3408 :
3409 : tree
3410 805836252 : type_decays_to (tree type)
3411 : {
3412 805836252 : if (TREE_CODE (type) == ARRAY_TYPE)
3413 6063349 : return build_pointer_type (TREE_TYPE (type));
3414 799772903 : if (TREE_CODE (type) == FUNCTION_TYPE)
3415 31873 : return build_pointer_type (type);
3416 : return type;
3417 : }
3418 :
3419 : /* There are three conditions of builtin candidates:
3420 :
3421 : 1) bool-taking candidates. These are the same regardless of the input.
3422 : 2) pointer-pair taking candidates. These are generated for each type
3423 : one of the input types converts to.
3424 : 3) arithmetic candidates. According to the standard, we should generate
3425 : all of these, but I'm trying not to...
3426 :
3427 : Here we generate a superset of the possible candidates for this particular
3428 : case. That is a subset of the full set the standard defines, plus some
3429 : other cases which the standard disallows. add_builtin_candidate will
3430 : filter out the invalid set. */
3431 :
3432 : static void
3433 23277756 : add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
3434 : enum tree_code code2, tree fnname,
3435 : vec<tree, va_gc> *argv,
3436 : int flags, tsubst_flags_t complain)
3437 : {
3438 23277756 : int ref1;
3439 23277756 : int enum_p = 0;
3440 23277756 : tree type, argtypes[3], t;
3441 : /* TYPES[i] is the set of possible builtin-operator parameter types
3442 : we will consider for the Ith argument. */
3443 23277756 : vec<tree, va_gc> *types[2];
3444 23277756 : unsigned ix;
3445 23277756 : vec<tree, va_gc> &args = *argv;
3446 23277756 : unsigned len = args.length ();
3447 :
3448 64998651 : for (unsigned i = 0; i < len; ++i)
3449 : {
3450 41720895 : if (args[i])
3451 41720895 : argtypes[i] = unlowered_expr_type (args[i]);
3452 : else
3453 0 : argtypes[i] = NULL_TREE;
3454 : }
3455 :
3456 23277756 : switch (code)
3457 : {
3458 : /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
3459 : and VQ is either volatile or empty, there exist candidate operator
3460 : functions of the form
3461 : VQ T& operator++(VQ T&); */
3462 :
3463 : case POSTINCREMENT_EXPR:
3464 : case PREINCREMENT_EXPR:
3465 : case POSTDECREMENT_EXPR:
3466 : case PREDECREMENT_EXPR:
3467 : case MODIFY_EXPR:
3468 : ref1 = 1;
3469 : break;
3470 :
3471 : /* 24There also exist candidate operator functions of the form
3472 : bool operator!(bool);
3473 : bool operator&&(bool, bool);
3474 : bool operator||(bool, bool); */
3475 :
3476 480247 : case TRUTH_NOT_EXPR:
3477 480247 : build_builtin_candidate
3478 480247 : (candidates, fnname, boolean_type_node,
3479 : NULL_TREE, args, argtypes, flags, complain);
3480 13146445 : return;
3481 :
3482 63533 : case TRUTH_ORIF_EXPR:
3483 63533 : case TRUTH_ANDIF_EXPR:
3484 63533 : build_builtin_candidate
3485 63533 : (candidates, fnname, boolean_type_node,
3486 : boolean_type_node, args, argtypes, flags, complain);
3487 63533 : return;
3488 :
3489 : case ADDR_EXPR:
3490 : case COMPOUND_EXPR:
3491 : case COMPONENT_REF:
3492 : case CO_AWAIT_EXPR:
3493 : return;
3494 :
3495 5916873 : case COND_EXPR:
3496 5916873 : case EQ_EXPR:
3497 5916873 : case NE_EXPR:
3498 5916873 : case LT_EXPR:
3499 5916873 : case LE_EXPR:
3500 5916873 : case GT_EXPR:
3501 5916873 : case GE_EXPR:
3502 5916873 : case SPACESHIP_EXPR:
3503 5916873 : enum_p = 1;
3504 : /* Fall through. */
3505 :
3506 : default:
3507 : ref1 = 0;
3508 : }
3509 :
3510 20638929 : types[0] = make_tree_vector ();
3511 20638929 : types[1] = make_tree_vector ();
3512 :
3513 20638929 : if (len == 3)
3514 693 : len = 2;
3515 42191232 : for (unsigned i = 0; i < len; ++i)
3516 : {
3517 31579674 : if (MAYBE_CLASS_TYPE_P (argtypes[i]))
3518 : {
3519 14339239 : tree convs;
3520 :
3521 14339239 : if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
3522 : return;
3523 :
3524 10981317 : convs = lookup_conversions (argtypes[i]);
3525 :
3526 10981317 : if (code == COND_EXPR)
3527 : {
3528 1212 : if (lvalue_p (args[i]))
3529 885 : vec_safe_push (types[i], build_reference_type (argtypes[i]));
3530 :
3531 1212 : vec_safe_push (types[i], TYPE_MAIN_VARIANT (argtypes[i]));
3532 : }
3533 :
3534 10980105 : else if (! convs)
3535 : return;
3536 :
3537 10605098 : for (; convs; convs = TREE_CHAIN (convs))
3538 : {
3539 6293230 : type = TREE_TYPE (convs);
3540 :
3541 7788780 : if (i == 0 && ref1
3542 6293230 : && (!TYPE_REF_P (type)
3543 1319 : || CP_TYPE_CONST_P (TREE_TYPE (type))))
3544 1495550 : continue;
3545 :
3546 4797680 : if (code == COND_EXPR && TYPE_REF_P (type))
3547 23 : vec_safe_push (types[i], type);
3548 :
3549 4797680 : type = non_reference (type);
3550 4797680 : if (i != 0 || ! ref1)
3551 : {
3552 4797641 : type = cv_unqualified (type_decays_to (type));
3553 4797641 : if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
3554 328 : vec_safe_push (types[i], type);
3555 4797641 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3556 2714976 : type = type_promotes_to (type);
3557 : }
3558 :
3559 4797680 : if (! vec_member (type, types[i]))
3560 4793785 : vec_safe_push (types[i], type);
3561 : }
3562 : }
3563 : else
3564 : {
3565 17240435 : if (code == COND_EXPR && lvalue_p (args[i]))
3566 90 : vec_safe_push (types[i], build_reference_type (argtypes[i]));
3567 17240435 : type = non_reference (argtypes[i]);
3568 17240435 : if (i != 0 || ! ref1)
3569 : {
3570 15504423 : type = cv_unqualified (type_decays_to (type));
3571 15504423 : if (enum_p && UNSCOPED_ENUM_P (type))
3572 1846337 : vec_safe_push (types[i], type);
3573 15504423 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3574 8818079 : type = type_promotes_to (type);
3575 : }
3576 17240435 : vec_safe_push (types[i], type);
3577 : }
3578 : }
3579 :
3580 : /* Run through the possible parameter types of both arguments,
3581 : creating candidates with those parameter types. */
3582 21747444 : FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t)
3583 : {
3584 11135886 : unsigned jx;
3585 11135886 : tree u;
3586 :
3587 11135886 : if (!types[1]->is_empty ())
3588 23997009 : FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u)
3589 12861123 : add_builtin_candidate
3590 12861123 : (candidates, code, code2, fnname, t,
3591 : u, args, argtypes, flags, complain);
3592 : else
3593 456851 : add_builtin_candidate
3594 456851 : (candidates, code, code2, fnname, t,
3595 : NULL_TREE, args, argtypes, flags, complain);
3596 : }
3597 :
3598 10611558 : release_tree_vector (types[0]);
3599 10611558 : release_tree_vector (types[1]);
3600 : }
3601 :
3602 :
3603 : /* If TMPL can be successfully instantiated as indicated by
3604 : EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
3605 :
3606 : TMPL is the template. EXPLICIT_TARGS are any explicit template
3607 : arguments. ARGLIST is the arguments provided at the call-site.
3608 : This does not change ARGLIST. The RETURN_TYPE is the desired type
3609 : for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
3610 : as for add_function_candidate. If an OBJ is supplied, FLAGS and
3611 : CTYPE are ignored, and OBJ is as for add_conv_candidate.
3612 :
3613 : SHORTCUT_BAD_CONVS is as in add_function_candidate. */
3614 :
3615 : static struct z_candidate*
3616 472308526 : add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
3617 : tree ctype, tree explicit_targs, tree first_arg,
3618 : const vec<tree, va_gc> *arglist, tree return_type,
3619 : tree access_path, tree conversion_path,
3620 : int flags, tree obj, unification_kind_t strict,
3621 : bool shortcut_bad_convs, tsubst_flags_t complain)
3622 : {
3623 472308526 : int ntparms = DECL_NTPARMS (tmpl);
3624 472308526 : tree targs = make_tree_vec (ntparms);
3625 472308526 : unsigned int len = vec_safe_length (arglist);
3626 472308526 : unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
3627 472308526 : unsigned int skip_without_in_chrg = 0;
3628 472308526 : tree first_arg_without_in_chrg = first_arg;
3629 472308526 : tree *args_without_in_chrg;
3630 472308526 : unsigned int nargs_without_in_chrg;
3631 472308526 : unsigned int ia, ix;
3632 472308526 : tree arg;
3633 472308526 : struct z_candidate *cand;
3634 472308526 : tree fn;
3635 472308526 : struct rejection_reason *reason = NULL;
3636 472308526 : int errs;
3637 472308526 : conversion **convs = NULL;
3638 :
3639 : /* We don't do deduction on the in-charge parameter, the VTT
3640 : parameter or 'this'. */
3641 472308526 : if (DECL_IOBJ_MEMBER_FUNCTION_P (tmpl))
3642 : {
3643 48565067 : if (first_arg_without_in_chrg != NULL_TREE)
3644 : first_arg_without_in_chrg = NULL_TREE;
3645 12 : else if (return_type && strict == DEDUCE_CALL)
3646 : /* We're deducing for a call to the result of a template conversion
3647 : function, so the args don't contain 'this'; leave them alone. */;
3648 : else
3649 472308526 : ++skip_without_in_chrg;
3650 : }
3651 :
3652 472308526 : if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
3653 472308526 : || DECL_BASE_CONSTRUCTOR_P (tmpl))
3654 473243534 : && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
3655 : {
3656 177 : if (first_arg_without_in_chrg != NULL_TREE)
3657 : first_arg_without_in_chrg = NULL_TREE;
3658 : else
3659 177 : ++skip_without_in_chrg;
3660 : }
3661 :
3662 472308526 : if (len < skip_without_in_chrg)
3663 0 : return add_ignored_candidate (candidates, tmpl);
3664 :
3665 516962501 : if (DECL_CONSTRUCTOR_P (tmpl) && nargs == 2
3666 512009812 : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (first_arg),
3667 39701286 : TREE_TYPE ((*arglist)[0])))
3668 : {
3669 : /* 12.8/6 says, "A declaration of a constructor for a class X is
3670 : ill-formed if its first parameter is of type (optionally cv-qualified)
3671 : X and either there are no other parameters or else all other
3672 : parameters have default arguments. A member function template is never
3673 : instantiated to produce such a constructor signature."
3674 :
3675 : So if we're trying to copy an object of the containing class, don't
3676 : consider a template constructor that has a first parameter type that
3677 : is just a template parameter, as we would deduce a signature that we
3678 : would then reject in the code below. */
3679 2083522 : if (tree firstparm = FUNCTION_FIRST_USER_PARMTYPE (tmpl))
3680 : {
3681 2083522 : firstparm = TREE_VALUE (firstparm);
3682 2083522 : if (PACK_EXPANSION_P (firstparm))
3683 1946 : firstparm = PACK_EXPANSION_PATTERN (firstparm);
3684 2083522 : if (TREE_CODE (firstparm) == TEMPLATE_TYPE_PARM)
3685 : {
3686 667354 : gcc_assert (!explicit_targs);
3687 667354 : reason = invalid_copy_with_fn_template_rejection ();
3688 667354 : goto fail;
3689 : }
3690 : }
3691 : }
3692 :
3693 471641172 : nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
3694 471641172 : + (len - skip_without_in_chrg));
3695 471641172 : args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
3696 471641172 : ia = 0;
3697 471641172 : if (first_arg_without_in_chrg != NULL_TREE)
3698 : {
3699 6304 : args_without_in_chrg[ia] = first_arg_without_in_chrg;
3700 6304 : ++ia;
3701 : }
3702 797071575 : for (ix = skip_without_in_chrg;
3703 1268712747 : vec_safe_iterate (arglist, ix, &arg);
3704 : ++ix)
3705 : {
3706 797071575 : args_without_in_chrg[ia] = arg;
3707 797071575 : ++ia;
3708 : }
3709 471641172 : gcc_assert (ia == nargs_without_in_chrg);
3710 :
3711 471641172 : if (!obj)
3712 : {
3713 : /* Check that there's no obvious arity mismatch before proceeding with
3714 : deduction. This avoids substituting explicit template arguments
3715 : into the template or e.g. derived-to-base parm/arg unification
3716 : (which could result in an error outside the immediate context) when
3717 : the resulting candidate would be unviable anyway. */
3718 471641160 : int min_arity = 0, max_arity = 0;
3719 471641160 : tree parms = TYPE_ARG_TYPES (TREE_TYPE (tmpl));
3720 471641160 : parms = skip_artificial_parms_for (tmpl, parms);
3721 1779056810 : for (; parms != void_list_node; parms = TREE_CHAIN (parms))
3722 : {
3723 1677247507 : if (!parms || PACK_EXPANSION_P (TREE_VALUE (parms)))
3724 : {
3725 : max_arity = -1;
3726 : break;
3727 : }
3728 835774490 : if (TREE_PURPOSE (parms))
3729 : /* A parameter with a default argument. */
3730 9219073 : ++max_arity;
3731 : else
3732 826555417 : ++min_arity, ++max_arity;
3733 : }
3734 471641160 : if (ia < (unsigned)min_arity)
3735 : {
3736 : /* Too few arguments. */
3737 32533597 : reason = arity_rejection (NULL_TREE, min_arity, ia,
3738 : /*least_p=*/(max_arity == -1));
3739 32533597 : goto fail;
3740 : }
3741 439107563 : else if (max_arity != -1 && ia > (unsigned)max_arity)
3742 : {
3743 : /* Too many arguments. */
3744 4771842 : reason = arity_rejection (NULL_TREE, max_arity, ia);
3745 4771842 : goto fail;
3746 : }
3747 :
3748 434335721 : convs = alloc_conversions (nargs);
3749 :
3750 434335721 : if (shortcut_bad_convs
3751 434331100 : && DECL_IOBJ_MEMBER_FUNCTION_P (tmpl)
3752 501955163 : && !DECL_CONSTRUCTOR_P (tmpl))
3753 : {
3754 : /* Check the 'this' conversion before proceeding with deduction.
3755 : This is effectively an extension of the DR 1391 resolution
3756 : that we perform in check_non_deducible_conversions, though it's
3757 : convenient to do this extra check here instead of there. */
3758 3457586 : tree parmtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (tmpl)));
3759 3457586 : tree argtype = lvalue_type (first_arg);
3760 3457586 : tree arg = first_arg;
3761 3457586 : conversion *t = build_this_conversion (tmpl, ctype,
3762 : parmtype, argtype, arg,
3763 : flags, complain);
3764 3457586 : convs[0] = t;
3765 3457586 : if (t->bad_p)
3766 : {
3767 32801 : reason = bad_arg_conversion_rejection (first_arg, 0,
3768 : arg, parmtype,
3769 32801 : EXPR_LOCATION (arg));
3770 32801 : goto fail;
3771 : }
3772 : }
3773 : }
3774 :
3775 434302932 : errs = errorcount+sorrycount;
3776 868592313 : fn = fn_type_unification (tmpl, explicit_targs, targs,
3777 : args_without_in_chrg,
3778 : nargs_without_in_chrg,
3779 : return_type, strict, flags, convs,
3780 434302932 : false, complain & tf_decltype);
3781 :
3782 434289381 : if (fn == error_mark_node)
3783 : {
3784 : /* Don't repeat unification later if it already resulted in errors. */
3785 357972960 : if (errorcount+sorrycount == errs)
3786 357972850 : reason = template_unification_rejection (tmpl, explicit_targs,
3787 : targs, args_without_in_chrg,
3788 : nargs_without_in_chrg,
3789 : return_type, strict, flags);
3790 : else
3791 110 : reason = template_unification_error_rejection ();
3792 357972960 : goto fail;
3793 : }
3794 :
3795 : /* Now the explicit specifier might have been deduced; check if this
3796 : declaration is explicit. If it is and we're ignoring non-converting
3797 : constructors, don't add this function to the set of candidates. */
3798 76316421 : if (((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
3799 : == LOOKUP_ONLYCONVERTING)
3800 76316421 : && DECL_NONCONVERTING_P (fn))
3801 4510 : return add_ignored_candidate (candidates, fn);
3802 :
3803 152623822 : if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
3804 : {
3805 4436037 : tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
3806 8872047 : if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
3807 : ctype))
3808 : {
3809 : /* We're trying to produce a constructor with a prohibited signature,
3810 : as discussed above; handle here any cases we didn't catch then,
3811 : such as X(X<T>). */
3812 574 : reason = invalid_copy_with_fn_template_rejection ();
3813 574 : goto fail;
3814 : }
3815 : }
3816 :
3817 76311337 : if (obj != NULL_TREE)
3818 : /* Aha, this is a conversion function. */
3819 9 : cand = add_conv_candidate (candidates, fn, obj, arglist,
3820 : access_path, conversion_path, complain);
3821 : else
3822 76311328 : cand = add_function_candidate (candidates, fn, ctype,
3823 : first_arg, arglist, access_path,
3824 : conversion_path, flags, convs,
3825 : shortcut_bad_convs, complain);
3826 76311337 : if (DECL_TI_TEMPLATE (fn) != tmpl)
3827 : /* This situation can occur if a member template of a template
3828 : class is specialized. Then, instantiate_template might return
3829 : an instantiation of the specialization, in which case the
3830 : DECL_TI_TEMPLATE field will point at the original
3831 : specialization. For example:
3832 :
3833 : template <class T> struct S { template <class U> void f(U);
3834 : template <> void f(int) {}; };
3835 : S<double> sd;
3836 : sd.f(3);
3837 :
3838 : Here, TMPL will be template <class U> S<double>::f(U).
3839 : And, instantiate template will give us the specialization
3840 : template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
3841 : for this will point at template <class T> template <> S<T>::f(int),
3842 : so that we can find the definition. For the purposes of
3843 : overload resolution, however, we want the original TMPL. */
3844 5125517 : cand->template_decl = build_template_info (tmpl, targs);
3845 : else
3846 71185820 : cand->template_decl = DECL_TEMPLATE_INFO (fn);
3847 76311337 : cand->explicit_targs = explicit_targs;
3848 :
3849 76311337 : return cand;
3850 395979128 : fail:
3851 395979128 : int viable = (reason->code == rr_bad_arg_conversion ? -1 : 0);
3852 395979128 : return add_candidate (candidates, tmpl, first_arg, arglist, nargs, convs,
3853 395979128 : access_path, conversion_path, viable, reason, flags);
3854 : }
3855 :
3856 :
3857 : static struct z_candidate *
3858 472308514 : add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
3859 : tree explicit_targs, tree first_arg,
3860 : const vec<tree, va_gc> *arglist, tree return_type,
3861 : tree access_path, tree conversion_path, int flags,
3862 : unification_kind_t strict, bool shortcut_bad_convs,
3863 : tsubst_flags_t complain)
3864 : {
3865 472308514 : return
3866 0 : add_template_candidate_real (candidates, tmpl, ctype,
3867 : explicit_targs, first_arg, arglist,
3868 : return_type, access_path, conversion_path,
3869 : flags, NULL_TREE, strict, shortcut_bad_convs,
3870 472294963 : complain);
3871 : }
3872 :
3873 : /* Create an overload candidate for the conversion function template TMPL,
3874 : returning RETURN_TYPE, which will be invoked for expression OBJ to produce a
3875 : pointer-to-function which will in turn be called with the argument list
3876 : ARGLIST, and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
3877 : passed on to implicit_conversion. */
3878 :
3879 : static struct z_candidate *
3880 12 : add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
3881 : tree obj,
3882 : const vec<tree, va_gc> *arglist,
3883 : tree return_type, tree access_path,
3884 : tree conversion_path, tsubst_flags_t complain)
3885 : {
3886 12 : return
3887 12 : add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
3888 : NULL_TREE, arglist, return_type, access_path,
3889 : conversion_path, 0, obj, DEDUCE_CALL,
3890 12 : /*shortcut_bad_convs=*/false, complain);
3891 : }
3892 :
3893 : /* The CANDS are the set of candidates that were considered for
3894 : overload resolution. Sort CANDS so that the strictly viable
3895 : candidates appear first, followed by non-strictly viable candidates,
3896 : followed by non-viable candidates. Returns the first candidate
3897 : in this sorted list. If any of the candidates were viable, set
3898 : *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
3899 : considered viable only if it is strictly viable when setting
3900 : *ANY_VIABLE_P. */
3901 :
3902 : static struct z_candidate*
3903 300019917 : splice_viable (struct z_candidate *cands,
3904 : bool strict_p,
3905 : bool *any_viable_p)
3906 : {
3907 300019917 : z_candidate *strictly_viable = nullptr;
3908 300019917 : z_candidate **strictly_viable_tail = &strictly_viable;
3909 :
3910 300019917 : z_candidate *non_strictly_viable = nullptr;
3911 300019917 : z_candidate **non_strictly_viable_tail = &non_strictly_viable;
3912 :
3913 300019917 : z_candidate *non_viable = nullptr;
3914 300019917 : z_candidate **non_viable_tail = &non_viable;
3915 :
3916 300019917 : z_candidate *non_viable_ignored = nullptr;
3917 300019917 : z_candidate **non_viable_ignored_tail = &non_viable_ignored;
3918 :
3919 : /* Be strict inside templates, since build_over_call won't actually
3920 : do the conversions to get pedwarns. */
3921 300019917 : if (processing_template_decl)
3922 55954319 : strict_p = true;
3923 :
3924 1148956887 : for (z_candidate *cand = cands; cand; cand = cand->next)
3925 : {
3926 848936970 : if (!strict_p
3927 292636307 : && (cand->viable == 1 || TREE_CODE (cand->fn) == TEMPLATE_DECL))
3928 : /* Be strict in the presence of a viable candidate. Also if
3929 : there are template candidates, so that we get deduction errors
3930 : for them instead of silently preferring a bad conversion. */
3931 757085531 : strict_p = true;
3932 :
3933 : /* Move this candidate to the appropriate list according to
3934 : its viability. */
3935 848936970 : auto& tail = (cand->viable == 1 ? strictly_viable_tail
3936 : : cand->viable == -1 ? non_strictly_viable_tail
3937 1355550795 : : ignored_candidate_p (cand) ? non_viable_ignored_tail
3938 1355550795 : : non_viable_tail);
3939 848936970 : *tail = cand;
3940 848936970 : tail = &cand->next;
3941 : }
3942 :
3943 600039834 : *any_viable_p = (strictly_viable != nullptr
3944 300019917 : || (!strict_p && non_strictly_viable != nullptr));
3945 :
3946 : /* Combine the lists. */
3947 300019917 : *non_viable_ignored_tail = nullptr;
3948 300019917 : *non_viable_tail = non_viable_ignored;
3949 300019917 : *non_strictly_viable_tail = non_viable;
3950 300019917 : *strictly_viable_tail = non_strictly_viable;
3951 :
3952 300019917 : return strictly_viable;
3953 : }
3954 :
3955 : static bool
3956 286740635 : any_strictly_viable (struct z_candidate *cands)
3957 : {
3958 393326594 : for (; cands; cands = cands->next)
3959 114954456 : if (cands->viable == 1)
3960 : return true;
3961 : return false;
3962 : }
3963 :
3964 : /* OBJ is being used in an expression like "OBJ.f (...)". In other
3965 : words, it is about to become the "this" pointer for a member
3966 : function call. Take the address of the object. */
3967 :
3968 : static tree
3969 63266490 : build_this (tree obj)
3970 : {
3971 : /* In a template, we are only concerned about the type of the
3972 : expression, so we can take a shortcut. */
3973 63266490 : if (processing_template_decl)
3974 74009 : return build_address (obj);
3975 :
3976 63192481 : return cp_build_addr_expr (obj, tf_warning_or_error);
3977 : }
3978 :
3979 : /* Returns true iff functions are equivalent. Equivalent functions are
3980 : not '==' only if one is a function-local extern function or if
3981 : both are extern "C". */
3982 :
3983 : static inline int
3984 4566334 : equal_functions (tree fn1, tree fn2)
3985 : {
3986 4566334 : if (TREE_CODE (fn1) != TREE_CODE (fn2))
3987 : return 0;
3988 4550989 : if (TREE_CODE (fn1) == TEMPLATE_DECL)
3989 36044 : return fn1 == fn2;
3990 9029872 : if (DECL_LOCAL_DECL_P (fn1) || DECL_LOCAL_DECL_P (fn2)
3991 9029869 : || DECL_EXTERN_C_FUNCTION_P (fn1))
3992 2909407 : return decls_match (fn1, fn2);
3993 1605538 : return fn1 == fn2;
3994 : }
3995 :
3996 : /* Print information about a candidate FN being rejected due to INFO. */
3997 :
3998 : static void
3999 5301 : print_conversion_rejection (location_t loc, struct conversion_info *info,
4000 : tree fn)
4001 : {
4002 5301 : tree from = info->from;
4003 5301 : if (!TYPE_P (from))
4004 4361 : from = lvalue_type (from);
4005 5301 : if (info->n_arg == -1)
4006 : {
4007 : /* Conversion of implicit `this' argument failed. */
4008 24 : if (!TYPE_P (info->from))
4009 : /* A bad conversion for 'this' must be discarding cv-quals. */
4010 24 : inform (loc, "passing %qT as %<this%> "
4011 : "argument discards qualifiers",
4012 : from);
4013 : else
4014 0 : inform (loc, "no known conversion for implicit "
4015 : "%<this%> parameter from %qH to %qI",
4016 : from, info->to_type);
4017 : }
4018 5277 : else if (!TYPE_P (info->from))
4019 : {
4020 4337 : if (info->n_arg >= 0)
4021 4337 : inform (loc, "conversion of argument %d would be ill-formed:",
4022 : info->n_arg + 1);
4023 4337 : iloc_sentinel ils = loc;
4024 4337 : perform_implicit_conversion (info->to_type, info->from,
4025 : tf_warning_or_error);
4026 4337 : }
4027 940 : else if (info->n_arg == -2)
4028 : /* Conversion of conversion function return value failed. */
4029 33 : inform (loc, "no known conversion from %qH to %qI",
4030 : from, info->to_type);
4031 : else
4032 : {
4033 907 : if (TREE_CODE (fn) == FUNCTION_DECL)
4034 751 : loc = get_fndecl_argument_location (fn, info->n_arg);
4035 907 : inform (loc, "no known conversion for argument %d from %qH to %qI",
4036 907 : info->n_arg + 1, from, info->to_type);
4037 : }
4038 5301 : }
4039 :
4040 : /* Print information about a candidate with WANT parameters and we found
4041 : HAVE. */
4042 :
4043 : static void
4044 1450 : print_arity_information (location_t loc, unsigned int have, unsigned int want,
4045 : bool least_p)
4046 : {
4047 1450 : if (least_p)
4048 41 : inform_n (loc, want,
4049 : "candidate expects at least %d argument, %d provided",
4050 : "candidate expects at least %d arguments, %d provided",
4051 : want, have);
4052 : else
4053 1409 : inform_n (loc, want,
4054 : "candidate expects %d argument, %d provided",
4055 : "candidate expects %d arguments, %d provided",
4056 : want, have);
4057 1450 : }
4058 :
4059 : /* Print information about one overload candidate CANDIDATE. MSGSTR
4060 : is the text to print before the candidate itself.
4061 :
4062 : NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
4063 : to have been run through gettext by the caller. This wart makes
4064 : life simpler in print_z_candidates and for the translators. */
4065 :
4066 : static void
4067 13188 : print_z_candidate (location_t loc, const char *msgstr,
4068 : struct z_candidate *candidate)
4069 : {
4070 13188 : const char *msg = (msgstr == NULL
4071 13188 : ? ""
4072 13188 : : ACONCAT ((_(msgstr), " ", NULL)));
4073 13188 : tree fn = candidate->fn;
4074 13188 : if (flag_new_inheriting_ctors)
4075 13161 : fn = strip_inheriting_ctors (fn);
4076 13188 : location_t cloc = location_of (fn);
4077 :
4078 13188 : if (identifier_p (fn))
4079 : {
4080 214 : cloc = loc;
4081 214 : if (candidate->num_convs == 3)
4082 0 : inform (cloc, "%s%<%D(%T, %T, %T)%> (built-in)", msg, fn,
4083 0 : candidate->convs[0]->type,
4084 0 : candidate->convs[1]->type,
4085 0 : candidate->convs[2]->type);
4086 214 : else if (candidate->num_convs == 2)
4087 190 : inform (cloc, "%s%<%D(%T, %T)%> (built-in)", msg, fn,
4088 190 : candidate->convs[0]->type,
4089 190 : candidate->convs[1]->type);
4090 : else
4091 24 : inform (cloc, "%s%<%D(%T)%> (built-in)", msg, fn,
4092 24 : candidate->convs[0]->type);
4093 : }
4094 12974 : else if (TYPE_P (fn))
4095 14 : inform (cloc, "%s%qT (conversion)", msg, fn);
4096 12960 : else if (candidate->viable == -1)
4097 4369 : inform (cloc, "%s%#qD (near match)", msg, fn);
4098 8591 : else if (ignored_candidate_p (candidate))
4099 6 : inform (cloc, "%s%#qD (ignored)", msg, fn);
4100 8585 : else if (DECL_DELETED_FN (fn))
4101 60 : inform (cloc, "%s%#qD (deleted)", msg, fn);
4102 8525 : else if (candidate->reversed ())
4103 300 : inform (cloc, "%s%#qD (reversed)", msg, fn);
4104 8225 : else if (candidate->rewritten ())
4105 150 : inform (cloc, "%s%#qD (rewritten)", msg, fn);
4106 : else
4107 8075 : inform (cloc, "%s%#qD", msg, fn);
4108 :
4109 13188 : auto_diagnostic_nesting_level sentinel;
4110 :
4111 13188 : if (fn != candidate->fn)
4112 : {
4113 57 : cloc = location_of (candidate->fn);
4114 57 : inform (cloc, "inherited here");
4115 : }
4116 :
4117 : /* Give the user some information about why this candidate failed. */
4118 13188 : if (candidate->reason != NULL)
4119 : {
4120 10099 : struct rejection_reason *r = candidate->reason;
4121 :
4122 10099 : switch (r->code)
4123 : {
4124 1450 : case rr_arity:
4125 1450 : print_arity_information (cloc, r->u.arity.actual,
4126 1450 : r->u.arity.expected,
4127 1450 : r->u.arity.least_p);
4128 1450 : break;
4129 913 : case rr_arg_conversion:
4130 913 : print_conversion_rejection (cloc, &r->u.conversion, fn);
4131 913 : break;
4132 4388 : case rr_bad_arg_conversion:
4133 4388 : print_conversion_rejection (cloc, &r->u.bad_conversion, fn);
4134 4388 : break;
4135 11 : case rr_explicit_conversion:
4136 11 : inform (cloc, "return type %qT of explicit conversion function "
4137 : "cannot be converted to %qT with a qualification "
4138 : "conversion", r->u.conversion.from,
4139 : r->u.conversion.to_type);
4140 11 : break;
4141 6 : case rr_template_conversion:
4142 6 : inform (cloc, "conversion from return type %qT of template "
4143 : "conversion function specialization to %qT is not an "
4144 : "exact match", r->u.conversion.from,
4145 : r->u.conversion.to_type);
4146 6 : break;
4147 3198 : case rr_template_unification:
4148 : /* We use template_unification_error_rejection if unification caused
4149 : actual non-SFINAE errors, in which case we don't need to repeat
4150 : them here. */
4151 3198 : if (r->u.template_unification.tmpl == NULL_TREE)
4152 : {
4153 45 : inform (cloc, "substitution of deduced template arguments "
4154 : "resulted in errors seen above");
4155 45 : break;
4156 : }
4157 : /* Re-run template unification with diagnostics. */
4158 3153 : inform (cloc, "template argument deduction/substitution failed:");
4159 3153 : {
4160 3153 : auto_diagnostic_nesting_level sentinel;
4161 3153 : fn_type_unification (r->u.template_unification.tmpl,
4162 : r->u.template_unification.explicit_targs,
4163 : (make_tree_vec
4164 : (r->u.template_unification.num_targs)),
4165 : r->u.template_unification.args,
4166 : r->u.template_unification.nargs,
4167 : r->u.template_unification.return_type,
4168 : r->u.template_unification.strict,
4169 : r->u.template_unification.flags,
4170 : NULL, true, false);
4171 3153 : }
4172 3153 : break;
4173 2 : case rr_invalid_copy:
4174 2 : inform (cloc,
4175 : "a constructor taking a single argument of its own "
4176 : "class type is invalid");
4177 2 : break;
4178 93 : case rr_constraint_failure:
4179 93 : diagnose_constraints (cloc, fn, NULL_TREE);
4180 93 : break;
4181 32 : case rr_inherited_ctor:
4182 32 : inform (cloc, "an inherited constructor is not a candidate for "
4183 : "initialization from an expression of the same or derived "
4184 : "type");
4185 32 : break;
4186 : case rr_ignored:
4187 : break;
4188 0 : case rr_none:
4189 0 : default:
4190 : /* This candidate didn't have any issues or we failed to
4191 : handle a particular code. Either way... */
4192 0 : gcc_unreachable ();
4193 : }
4194 : }
4195 13188 : }
4196 :
4197 : /* Print information about each overload candidate in CANDIDATES,
4198 : which is assumed to have gone through splice_viable and tourney
4199 : (if splice_viable succeeded). */
4200 :
4201 : static void
4202 5465 : print_z_candidates (location_t loc, struct z_candidate *candidates,
4203 : tristate only_viable_p /* = tristate::unknown () */)
4204 : {
4205 5465 : struct z_candidate *cand1;
4206 5465 : struct z_candidate **cand2;
4207 :
4208 5465 : if (!candidates)
4209 1007 : return;
4210 :
4211 : /* Remove non-viable deleted candidates. */
4212 4458 : cand1 = candidates;
4213 20221 : for (cand2 = &cand1; *cand2; )
4214 : {
4215 15763 : if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
4216 11396 : && !(*cand2)->viable
4217 19522 : && DECL_DELETED_FN ((*cand2)->fn))
4218 102 : *cand2 = (*cand2)->next;
4219 : else
4220 15661 : cand2 = &(*cand2)->next;
4221 : }
4222 : /* ...if there are any non-deleted ones. */
4223 4458 : if (cand1)
4224 4452 : candidates = cand1;
4225 :
4226 : /* There may be duplicates in the set of candidates. We put off
4227 : checking this condition as long as possible, since we have no way
4228 : to eliminate duplicates from a set of functions in less than n^2
4229 : time. Now we are about to emit an error message, so it is more
4230 : permissible to go slowly. */
4231 19819 : for (cand1 = candidates; cand1; cand1 = cand1->next)
4232 : {
4233 15361 : tree fn = cand1->fn;
4234 : /* Skip builtin candidates and conversion functions. */
4235 15361 : if (!DECL_P (fn))
4236 288 : continue;
4237 15073 : cand2 = &cand1->next;
4238 143175 : while (*cand2)
4239 : {
4240 128102 : if (DECL_P ((*cand2)->fn)
4241 128102 : && equal_functions (fn, (*cand2)->fn))
4242 306 : *cand2 = (*cand2)->next;
4243 : else
4244 127796 : cand2 = &(*cand2)->next;
4245 : }
4246 : }
4247 :
4248 : /* Unless otherwise specified, if there's a (strictly) viable candidate
4249 : then we assume we're being called as part of diagnosing ambiguity, in
4250 : which case we want to print only viable candidates since non-viable
4251 : candidates couldn't have contributed to the ambiguity. */
4252 4458 : if (only_viable_p.is_unknown ())
4253 4449 : only_viable_p = candidates->viable == 1;
4254 :
4255 4458 : auto_diagnostic_nesting_level sentinel;
4256 :
4257 4458 : int num_candidates = 0;
4258 17534 : for (auto iter = candidates; iter; iter = iter->next)
4259 : {
4260 13417 : if (only_viable_p.is_true () && iter->viable != 1)
4261 : break;
4262 13076 : ++num_candidates;
4263 : }
4264 :
4265 4458 : inform_num_candidates (loc, num_candidates);
4266 :
4267 4458 : auto_diagnostic_nesting_level sentinel2;
4268 :
4269 4458 : int candidate_idx = 0;
4270 21959 : for (; candidates; candidates = candidates->next)
4271 : {
4272 13413 : if (only_viable_p.is_true () && candidates->viable != 1)
4273 : break;
4274 13107 : if (ignored_candidate_p (candidates) && !flag_diagnostics_all_candidates)
4275 : {
4276 29 : inform (loc, "some candidates omitted; "
4277 : "use %<-fdiagnostics-all-candidates%> to display them");
4278 29 : break;
4279 : }
4280 13043 : pretty_printer pp;
4281 13043 : pp_printf (&pp, N_("candidate %i:"), candidate_idx + 1);
4282 13043 : const char *const msgstr = pp_formatted_text (&pp);
4283 13043 : print_z_candidate (loc, msgstr, candidates);
4284 13043 : ++candidate_idx;
4285 13043 : }
4286 4458 : }
4287 :
4288 : /* USER_SEQ is a user-defined conversion sequence, beginning with a
4289 : USER_CONV. STD_SEQ is the standard conversion sequence applied to
4290 : the result of the conversion function to convert it to the final
4291 : desired type. Merge the two sequences into a single sequence,
4292 : and return the merged sequence. */
4293 :
4294 : static conversion *
4295 14315540 : merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
4296 : {
4297 14315540 : conversion **t;
4298 14315540 : bool bad = user_seq->bad_p;
4299 :
4300 14315540 : gcc_assert (user_seq->kind == ck_user);
4301 :
4302 : /* Find the end of the second conversion sequence. */
4303 15031121 : for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
4304 : {
4305 : /* The entire sequence is a user-conversion sequence. */
4306 715581 : (*t)->user_conv_p = true;
4307 715581 : if (bad)
4308 2884 : (*t)->bad_p = true;
4309 : }
4310 :
4311 14315540 : if ((*t)->rvaluedness_matches_p)
4312 : /* We're binding a reference directly to the result of the conversion.
4313 : build_user_type_conversion_1 stripped the REFERENCE_TYPE from the return
4314 : type, but we want it back. */
4315 431261 : user_seq->type = TREE_TYPE (TREE_TYPE (user_seq->cand->fn));
4316 :
4317 : /* Replace the identity conversion with the user conversion
4318 : sequence. */
4319 14315540 : *t = user_seq;
4320 :
4321 14315540 : return std_seq;
4322 : }
4323 :
4324 : /* Handle overload resolution for initializing an object of class type from
4325 : an initializer list. First we look for a suitable constructor that
4326 : takes a std::initializer_list; if we don't find one, we then look for a
4327 : non-list constructor.
4328 :
4329 : Parameters are as for add_candidates, except that the arguments are in
4330 : the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
4331 : the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
4332 :
4333 : static void
4334 2421320 : add_list_candidates (tree fns, tree first_arg,
4335 : const vec<tree, va_gc> *args, tree totype,
4336 : tree explicit_targs, bool template_only,
4337 : tree conversion_path, tree access_path,
4338 : int flags,
4339 : struct z_candidate **candidates,
4340 : tsubst_flags_t complain)
4341 : {
4342 2421320 : gcc_assert (*candidates == NULL);
4343 :
4344 : /* We're looking for a ctor for list-initialization. */
4345 2421320 : flags |= LOOKUP_LIST_INIT_CTOR;
4346 : /* And we don't allow narrowing conversions. We also use this flag to
4347 : avoid the copy constructor call for copy-list-initialization. */
4348 2421320 : flags |= LOOKUP_NO_NARROWING;
4349 :
4350 4842640 : unsigned nart = num_artificial_parms_for (OVL_FIRST (fns)) - 1;
4351 2421320 : tree init_list = (*args)[nart];
4352 :
4353 : /* Always use the default constructor if the list is empty (DR 990). */
4354 2421320 : if (CONSTRUCTOR_NELTS (init_list) == 0
4355 2421320 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
4356 : ;
4357 2194377 : else if (CONSTRUCTOR_IS_DESIGNATED_INIT (init_list)
4358 2194377 : && !CP_AGGREGATE_TYPE_P (totype))
4359 : {
4360 48 : if (complain & tf_error)
4361 12 : error ("designated initializers cannot be used with a "
4362 : "non-aggregate type %qT", totype);
4363 7048 : return;
4364 : }
4365 : /* If the class has a list ctor, try passing the list as a single
4366 : argument first, but only consider list ctors. */
4367 2194329 : else if (TYPE_HAS_LIST_CTOR (totype))
4368 : {
4369 76999 : flags |= LOOKUP_LIST_ONLY;
4370 76999 : add_candidates (fns, first_arg, args, NULL_TREE,
4371 : explicit_targs, template_only, conversion_path,
4372 : access_path, flags, candidates, complain);
4373 153998 : if (any_strictly_viable (*candidates))
4374 : return;
4375 : }
4376 :
4377 : /* Expand the CONSTRUCTOR into a new argument vec. */
4378 2414272 : vec<tree, va_gc> *new_args;
4379 4600892 : vec_alloc (new_args, nart + CONSTRUCTOR_NELTS (init_list));
4380 2414275 : for (unsigned i = 0; i < nart; ++i)
4381 3 : new_args->quick_push ((*args)[i]);
4382 2414272 : new_args = append_ctor_to_tree_vector (new_args, init_list);
4383 :
4384 : /* We aren't looking for list-ctors anymore. */
4385 2414272 : flags &= ~LOOKUP_LIST_ONLY;
4386 : /* We allow more user-defined conversions within an init-list. */
4387 2414272 : flags &= ~LOOKUP_NO_CONVERSION;
4388 :
4389 2414272 : add_candidates (fns, first_arg, new_args, NULL_TREE,
4390 : explicit_targs, template_only, conversion_path,
4391 : access_path, flags, candidates, complain);
4392 : }
4393 :
4394 : /* Given C(std::initializer_list<A>), return A. */
4395 :
4396 : static tree
4397 1393 : list_ctor_element_type (tree fn)
4398 : {
4399 1393 : gcc_checking_assert (is_list_ctor (fn));
4400 :
4401 1393 : tree parm = FUNCTION_FIRST_USER_PARMTYPE (fn);
4402 1393 : parm = non_reference (TREE_VALUE (parm));
4403 1393 : return TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
4404 : }
4405 :
4406 : /* If EXPR is a braced-init-list where the elements all decay to the same type,
4407 : return that type. */
4408 :
4409 : static tree
4410 1438 : braced_init_element_type (tree expr)
4411 : {
4412 1438 : if (TREE_CODE (expr) == CONSTRUCTOR
4413 1438 : && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
4414 0 : return TREE_TYPE (TREE_TYPE (expr));
4415 1438 : if (!BRACE_ENCLOSED_INITIALIZER_P (expr))
4416 : return NULL_TREE;
4417 :
4418 1438 : tree elttype = NULL_TREE;
4419 14462 : for (constructor_elt &e: CONSTRUCTOR_ELTS (expr))
4420 : {
4421 10332 : tree type = TREE_TYPE (e.value);
4422 10332 : type = type_decays_to (type);
4423 10332 : if (!elttype)
4424 : elttype = type;
4425 8913 : else if (!same_type_p (type, elttype))
4426 : return NULL_TREE;
4427 : }
4428 : return elttype;
4429 : }
4430 :
4431 : /* True iff EXPR contains any temporaries with non-trivial destruction.
4432 :
4433 : ??? Also ignore classes with non-trivial but no-op destruction other than
4434 : std::allocator? */
4435 :
4436 : static bool
4437 191 : has_non_trivial_temporaries (tree expr)
4438 : {
4439 191 : auto_vec<tree*> temps;
4440 191 : cp_walk_tree_without_duplicates (&expr, find_temps_r, &temps);
4441 380 : for (tree *p : temps)
4442 : {
4443 63 : tree t = TREE_TYPE (*p);
4444 63 : if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (t)
4445 63 : && !is_std_allocator (t))
4446 : return true;
4447 : }
4448 : return false;
4449 191 : }
4450 :
4451 : /* Return number of initialized elements in CTOR. */
4452 :
4453 : unsigned HOST_WIDE_INT
4454 253 : count_ctor_elements (tree ctor)
4455 : {
4456 253 : unsigned HOST_WIDE_INT len = 0;
4457 2076 : for (constructor_elt &e: CONSTRUCTOR_ELTS (ctor))
4458 1317 : if (TREE_CODE (e.value) == RAW_DATA_CST)
4459 9 : len += RAW_DATA_LENGTH (e.value);
4460 : else
4461 1308 : ++len;
4462 253 : return len;
4463 : }
4464 :
4465 : /* We're initializing an array of ELTTYPE from INIT. If it seems useful,
4466 : return INIT as an array (of its own type) so the caller can initialize the
4467 : target array in a loop. */
4468 :
4469 : static tree
4470 11453 : maybe_init_list_as_array (tree elttype, tree init)
4471 : {
4472 : /* Only do this if the array can go in rodata but not once converted. */
4473 11453 : if (!TYPE_NON_AGGREGATE_CLASS (elttype))
4474 : return NULL_TREE;
4475 1438 : tree init_elttype = braced_init_element_type (init);
4476 1438 : if (!init_elttype || !SCALAR_TYPE_P (init_elttype) || !TREE_CONSTANT (init))
4477 : return NULL_TREE;
4478 :
4479 : /* Check with a stub expression to weed out special cases, and check whether
4480 : we call the same function for direct-init as copy-list-init. */
4481 317 : conversion_obstack_sentinel cos;
4482 317 : init_elttype = cp_build_qualified_type (init_elttype, TYPE_QUAL_CONST);
4483 317 : tree arg = build_stub_object (init_elttype);
4484 317 : conversion *c = implicit_conversion (elttype, init_elttype, arg, false,
4485 : LOOKUP_NORMAL, tf_none);
4486 317 : if (c && c->kind == ck_rvalue)
4487 0 : c = next_conversion (c);
4488 311 : if (!c || c->kind != ck_user)
4489 : return NULL_TREE;
4490 : /* Check that we actually can perform the conversion. */
4491 308 : if (convert_like (c, arg, tf_none) == error_mark_node)
4492 : /* Let the normal code give the error. */
4493 : return NULL_TREE;
4494 :
4495 : /* A glvalue initializer might be significant to a reference constructor
4496 : or conversion operator. */
4497 302 : if (!DECL_CONSTRUCTOR_P (c->cand->fn)
4498 302 : || (TYPE_REF_P (TREE_VALUE
4499 : (FUNCTION_FIRST_USER_PARMTYPE (c->cand->fn)))))
4500 240 : for (auto &ce : CONSTRUCTOR_ELTS (init))
4501 108 : if (non_mergeable_glvalue_p (ce.value))
4502 : return NULL_TREE;
4503 :
4504 299 : tree first = CONSTRUCTOR_ELT (init, 0)->value;
4505 299 : conversion *fc = implicit_conversion (elttype, init_elttype, first, false,
4506 : LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING,
4507 : tf_none);
4508 299 : if (fc && fc->kind == ck_rvalue)
4509 48 : fc = next_conversion (fc);
4510 299 : if (!fc || fc->kind != ck_user || fc->cand->fn != c->cand->fn)
4511 : return NULL_TREE;
4512 257 : first = convert_like (fc, first, tf_none);
4513 257 : if (first == error_mark_node)
4514 : /* Let the normal code give the error. */
4515 : return NULL_TREE;
4516 :
4517 : /* Don't do this if the conversion would be constant. */
4518 254 : first = maybe_constant_init (first);
4519 254 : if (TREE_CONSTANT (first))
4520 : return NULL_TREE;
4521 :
4522 : /* We can't do this if the conversion creates temporaries that need
4523 : to live until the whole array is initialized. */
4524 191 : if (has_non_trivial_temporaries (first))
4525 : return NULL_TREE;
4526 :
4527 : /* We can't do this if copying from the initializer_list would be
4528 : ill-formed. */
4529 191 : tree copy_argtypes = make_tree_vec (1);
4530 191 : TREE_VEC_ELT (copy_argtypes, 0)
4531 191 : = cp_build_qualified_type (elttype, TYPE_QUAL_CONST);
4532 191 : if (!is_xible (INIT_EXPR, elttype, copy_argtypes))
4533 : return NULL_TREE;
4534 :
4535 185 : unsigned HOST_WIDE_INT len = count_ctor_elements (init);
4536 185 : tree arr = build_array_of_n_type (init_elttype, len);
4537 185 : arr = finish_compound_literal (arr, init, tf_none);
4538 185 : DECL_MERGEABLE (TARGET_EXPR_SLOT (arr)) = true;
4539 185 : return arr;
4540 317 : }
4541 :
4542 : /* If we were going to call e.g. vector(initializer_list<string>) starting
4543 : with a list of string-literals (which is inefficient, see PR105838),
4544 : instead build an array of const char* and pass it to the range constructor.
4545 : But only do this for standard library types, where we can assume the
4546 : transformation makes sense.
4547 :
4548 : Really the container classes should have initializer_list<U> constructors to
4549 : get the same effect more simply; this is working around that lack. */
4550 :
4551 : static tree
4552 13896015 : maybe_init_list_as_range (tree fn, tree expr)
4553 : {
4554 13896015 : if (!processing_template_decl
4555 13388890 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
4556 857713 : && is_list_ctor (fn)
4557 13897561 : && decl_in_std_namespace_p (fn))
4558 : {
4559 1393 : tree to = list_ctor_element_type (fn);
4560 1393 : if (tree init = maybe_init_list_as_array (to, expr))
4561 : {
4562 53 : tree begin = decay_conversion (TARGET_EXPR_SLOT (init), tf_none);
4563 53 : tree nelts = array_type_nelts_top (TREE_TYPE (init));
4564 53 : tree end = cp_build_binary_op (input_location, PLUS_EXPR, begin,
4565 : nelts, tf_none);
4566 53 : begin = cp_build_compound_expr (init, begin, tf_none);
4567 53 : return build_constructor_va (init_list_type_node, 2,
4568 53 : NULL_TREE, begin, NULL_TREE, end);
4569 : }
4570 : }
4571 :
4572 : return NULL_TREE;
4573 : }
4574 :
4575 : /* Returns the best overload candidate to perform the requested
4576 : conversion. This function is used for three the overloading situations
4577 : described in [over.match.copy], [over.match.conv], and [over.match.ref].
4578 : If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
4579 : per [dcl.init.ref], so we ignore temporary bindings. */
4580 :
4581 : static struct z_candidate *
4582 90245460 : build_user_type_conversion_1 (tree totype, tree expr, int flags,
4583 : tsubst_flags_t complain)
4584 : {
4585 90245460 : struct z_candidate *candidates, *cand;
4586 90245460 : tree fromtype;
4587 90245460 : tree ctors = NULL_TREE;
4588 90245460 : tree conv_fns = NULL_TREE;
4589 90245460 : conversion *conv = NULL;
4590 90245460 : tree first_arg = NULL_TREE;
4591 90245460 : vec<tree, va_gc> *args = NULL;
4592 90245460 : bool any_viable_p;
4593 90245460 : int convflags;
4594 :
4595 90245460 : if (!expr)
4596 : return NULL;
4597 :
4598 90245454 : fromtype = TREE_TYPE (expr);
4599 :
4600 : /* We represent conversion within a hierarchy using RVALUE_CONV and
4601 : BASE_CONV, as specified by [over.best.ics]; these become plain
4602 : constructor calls, as specified in [dcl.init]. */
4603 90245454 : gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
4604 : || !DERIVED_FROM_P (totype, fromtype));
4605 :
4606 90245454 : if (CLASS_TYPE_P (totype))
4607 : /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
4608 : creating a garbage BASELINK; constructors can't be inherited. */
4609 49813920 : ctors = get_class_binding (totype, complete_ctor_identifier);
4610 :
4611 90245454 : tree to_nonref = non_reference (totype);
4612 90245454 : if (MAYBE_CLASS_TYPE_P (fromtype))
4613 : {
4614 62892812 : if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
4615 62865247 : (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
4616 51469762 : && DERIVED_FROM_P (to_nonref, fromtype)))
4617 : {
4618 : /* [class.conv.fct] A conversion function is never used to
4619 : convert a (possibly cv-qualified) object to the (possibly
4620 : cv-qualified) same object type (or a reference to it), to a
4621 : (possibly cv-qualified) base class of that type (or a
4622 : reference to it)... */
4623 : }
4624 : else
4625 62865244 : conv_fns = lookup_conversions (fromtype);
4626 : }
4627 :
4628 90245454 : candidates = 0;
4629 90245454 : flags |= LOOKUP_NO_CONVERSION;
4630 90245454 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4631 898922 : flags |= LOOKUP_NO_NARROWING;
4632 : /* Prevent add_candidates from treating a non-strictly viable candidate
4633 : as unviable. */
4634 90245454 : complain |= tf_conv;
4635 :
4636 : /* It's OK to bind a temporary for converting constructor arguments, but
4637 : not in converting the return value of a conversion operator. */
4638 90245454 : convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION
4639 90245454 : | (flags & LOOKUP_NO_NARROWING));
4640 90245454 : flags &= ~LOOKUP_NO_TEMP_BIND;
4641 :
4642 90245454 : if (ctors)
4643 : {
4644 49602954 : int ctorflags = flags;
4645 :
4646 49602954 : first_arg = build_dummy_object (totype);
4647 :
4648 : /* We should never try to call the abstract or base constructor
4649 : from here. */
4650 348170652 : gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_FIRST (ctors))
4651 : && !DECL_HAS_VTT_PARM_P (OVL_FIRST (ctors)));
4652 :
4653 49602954 : args = make_tree_vector_single (expr);
4654 49602954 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4655 : {
4656 : /* List-initialization. */
4657 898922 : add_list_candidates (ctors, first_arg, args, totype, NULL_TREE,
4658 898922 : false, TYPE_BINFO (totype), TYPE_BINFO (totype),
4659 : ctorflags, &candidates, complain);
4660 : }
4661 : else
4662 : {
4663 48704032 : add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
4664 48704032 : TYPE_BINFO (totype), TYPE_BINFO (totype),
4665 : ctorflags, &candidates, complain);
4666 : }
4667 :
4668 272727162 : for (cand = candidates; cand; cand = cand->next)
4669 : {
4670 223124208 : cand->second_conv = build_identity_conv (totype, NULL_TREE);
4671 :
4672 : /* If totype isn't a reference, and LOOKUP_ONLYCONVERTING is
4673 : set, then this is copy-initialization. In that case, "The
4674 : result of the call is then used to direct-initialize the
4675 : object that is the destination of the copy-initialization."
4676 : [dcl.init]
4677 :
4678 : We represent this in the conversion sequence with an
4679 : rvalue conversion, which means a constructor call. */
4680 223124208 : if (!TYPE_REF_P (totype)
4681 223124208 : && cxx_dialect < cxx17
4682 429745 : && (flags & LOOKUP_ONLYCONVERTING)
4683 372357 : && !(convflags & LOOKUP_NO_TEMP_BIND))
4684 116419 : cand->second_conv
4685 116419 : = build_conv (ck_rvalue, totype, cand->second_conv);
4686 : }
4687 : }
4688 :
4689 90245454 : if (conv_fns)
4690 : {
4691 28522165 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4692 0 : first_arg = CONSTRUCTOR_ELT (expr, 0)->value;
4693 : else
4694 90245454 : first_arg = expr;
4695 : }
4696 :
4697 123383462 : for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
4698 : {
4699 33138008 : tree conversion_path = TREE_PURPOSE (conv_fns);
4700 33138008 : struct z_candidate *old_candidates;
4701 :
4702 : /* If LOOKUP_NO_CONVERSION, don't consider a conversion function that
4703 : would need an addional user-defined conversion, i.e. if the return
4704 : type differs in class-ness from the desired type. So we avoid
4705 : considering operator bool when calling a copy constructor.
4706 :
4707 : This optimization avoids the failure in PR97600, and is allowed by
4708 : [temp.inst]/9: "If the function selected by overload resolution can be
4709 : determined without instantiating a class template definition, it is
4710 : unspecified whether that instantiation actually takes place." */
4711 33138008 : tree convtype = non_reference (TREE_TYPE (conv_fns));
4712 49002154 : if ((flags & LOOKUP_NO_CONVERSION)
4713 33138008 : && !WILDCARD_TYPE_P (convtype)
4714 62926320 : && (CLASS_TYPE_P (to_nonref)
4715 31463160 : != CLASS_TYPE_P (convtype)))
4716 15864146 : continue;
4717 :
4718 : /* If we are called to convert to a reference type, we are trying to
4719 : find a direct binding, so don't even consider temporaries. If
4720 : we don't find a direct binding, the caller will try again to
4721 : look for a temporary binding. */
4722 17273862 : if (TYPE_REF_P (totype))
4723 4855929 : convflags |= LOOKUP_NO_TEMP_BIND;
4724 :
4725 17273862 : old_candidates = candidates;
4726 17273862 : add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
4727 : NULL_TREE, false,
4728 17273862 : conversion_path, TYPE_BINFO (fromtype),
4729 : flags, &candidates, complain);
4730 :
4731 34547719 : for (cand = candidates; cand != old_candidates; cand = cand->next)
4732 : {
4733 17273857 : if (cand->viable == 0)
4734 : /* Already rejected, don't change to -1. */
4735 6595125 : continue;
4736 :
4737 10678732 : tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
4738 10678732 : conversion *ics
4739 10678732 : = implicit_conversion (totype,
4740 : rettype,
4741 : 0,
4742 : /*c_cast_p=*/false, convflags,
4743 : complain);
4744 :
4745 : /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
4746 : copy-initialization. In that case, "The result of the
4747 : call is then used to direct-initialize the object that is
4748 : the destination of the copy-initialization." [dcl.init]
4749 :
4750 : We represent this in the conversion sequence with an
4751 : rvalue conversion, which means a constructor call. But
4752 : don't add a second rvalue conversion if there's already
4753 : one there. Which there really shouldn't be, but it's
4754 : harmless since we'd add it here anyway. */
4755 4062932 : if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
4756 11338092 : && !(convflags & LOOKUP_NO_TEMP_BIND))
4757 242620 : ics = build_conv (ck_rvalue, totype, ics);
4758 :
4759 10678732 : cand->second_conv = ics;
4760 :
4761 10678732 : if (!ics)
4762 : {
4763 6615800 : cand->viable = 0;
4764 13229773 : cand->reason = arg_conversion_rejection (NULL_TREE, -2,
4765 : rettype, totype,
4766 6615800 : EXPR_LOCATION (expr));
4767 : }
4768 14765 : else if (TYPE_REF_P (totype) && !ics->rvaluedness_matches_p
4769 : /* Limit this to non-templates for now (PR90546). */
4770 1359 : && !cand->template_decl
4771 4064286 : && TREE_CODE (TREE_TYPE (totype)) != FUNCTION_TYPE)
4772 : {
4773 : /* If we are called to convert to a reference type, we are trying
4774 : to find a direct binding per [over.match.ref], so rvaluedness
4775 : must match for non-functions. */
4776 1348 : cand->viable = 0;
4777 : }
4778 4061584 : else if (DECL_NONCONVERTING_P (cand->fn)
4779 4061584 : && ics->rank > cr_exact)
4780 : {
4781 : /* 13.3.1.5: For direct-initialization, those explicit
4782 : conversion functions that are not hidden within S and
4783 : yield type T or a type that can be converted to type T
4784 : with a qualification conversion (4.4) are also candidate
4785 : functions. */
4786 : /* 13.3.1.6 doesn't have a parallel restriction, but it should;
4787 : I've raised this issue with the committee. --jason 9/2011 */
4788 2630 : cand->viable = -1;
4789 2630 : cand->reason = explicit_conversion_rejection (rettype, totype);
4790 : }
4791 4058954 : else if (cand->viable == 1 && ics->bad_p)
4792 : {
4793 1992 : cand->viable = -1;
4794 1992 : cand->reason
4795 3984 : = bad_arg_conversion_rejection (NULL_TREE, -2,
4796 : rettype, totype,
4797 1992 : EXPR_LOCATION (expr));
4798 : }
4799 4056962 : else if (primary_template_specialization_p (cand->fn)
4800 4056962 : && ics->rank > cr_exact)
4801 : {
4802 : /* 13.3.3.1.2: If the user-defined conversion is specified by
4803 : a specialization of a conversion function template, the
4804 : second standard conversion sequence shall have exact match
4805 : rank. */
4806 21 : cand->viable = -1;
4807 21 : cand->reason = template_conversion_rejection (rettype, totype);
4808 : }
4809 : }
4810 : }
4811 :
4812 90245454 : candidates = splice_viable (candidates, false, &any_viable_p);
4813 90245454 : if (!any_viable_p)
4814 : {
4815 76348100 : if (args)
4816 39095700 : release_tree_vector (args);
4817 76348100 : return NULL;
4818 : }
4819 :
4820 13897354 : cand = tourney (candidates, complain);
4821 13897354 : if (cand == NULL)
4822 : {
4823 1339 : if (complain & tf_error)
4824 : {
4825 71 : auto_diagnostic_group d;
4826 74 : error_at (cp_expr_loc_or_input_loc (expr),
4827 : "conversion from %qH to %qI is ambiguous",
4828 : fromtype, totype);
4829 71 : print_z_candidates (location_of (expr), candidates);
4830 71 : }
4831 :
4832 1339 : cand = candidates; /* any one will do */
4833 1339 : cand->second_conv = build_ambiguous_conv (totype, expr);
4834 1339 : cand->second_conv->user_conv_p = true;
4835 2678 : if (!any_strictly_viable (candidates))
4836 12 : cand->second_conv->bad_p = true;
4837 1339 : if (flags & LOOKUP_ONLYCONVERTING)
4838 1266 : cand->second_conv->need_temporary_p = true;
4839 : /* If there are viable candidates, don't set ICS_BAD_FLAG; an
4840 : ambiguous conversion is no worse than another user-defined
4841 : conversion. */
4842 :
4843 1339 : return cand;
4844 : }
4845 :
4846 : /* Maybe pass { } as iterators instead of an initializer_list. */
4847 13896015 : if (tree iters = maybe_init_list_as_range (cand->fn, expr))
4848 106 : if (z_candidate *cand2
4849 53 : = build_user_type_conversion_1 (totype, iters, flags, tf_none))
4850 50 : if (cand2->viable == 1 && !is_list_ctor (cand2->fn))
4851 : {
4852 : cand = cand2;
4853 : expr = iters;
4854 : }
4855 :
4856 13896015 : tree convtype;
4857 27792030 : if (!DECL_CONSTRUCTOR_P (cand->fn))
4858 4049424 : convtype = non_reference (TREE_TYPE (TREE_TYPE (cand->fn)));
4859 9846591 : else if (cand->second_conv->kind == ck_rvalue)
4860 : /* DR 5: [in the first step of copy-initialization]...if the function
4861 : is a constructor, the call initializes a temporary of the
4862 : cv-unqualified version of the destination type. */
4863 11919 : convtype = cv_unqualified (totype);
4864 : else
4865 : convtype = totype;
4866 : /* Build the user conversion sequence. */
4867 13896015 : conv = build_conv
4868 13896015 : (ck_user,
4869 : convtype,
4870 13896015 : build_identity_conv (TREE_TYPE (expr), expr));
4871 13896015 : conv->cand = cand;
4872 13896015 : if (cand->viable == -1)
4873 7282 : conv->bad_p = true;
4874 :
4875 : /* Remember that this was a list-initialization. */
4876 13896015 : if (flags & LOOKUP_NO_NARROWING)
4877 3212788 : conv->check_narrowing = true;
4878 :
4879 : /* Combine it with the second conversion sequence. */
4880 13896015 : cand->second_conv = merge_conversion_sequences (conv,
4881 : cand->second_conv);
4882 :
4883 13896015 : return cand;
4884 : }
4885 :
4886 : /* Wrapper for above. */
4887 :
4888 : tree
4889 26941 : build_user_type_conversion (tree totype, tree expr, int flags,
4890 : tsubst_flags_t complain)
4891 : {
4892 26941 : struct z_candidate *cand;
4893 26941 : tree ret;
4894 :
4895 26941 : auto_cond_timevar tv (TV_OVERLOAD);
4896 :
4897 26941 : conversion_obstack_sentinel cos;
4898 :
4899 26941 : cand = build_user_type_conversion_1 (totype, expr, flags, complain);
4900 :
4901 26941 : if (cand)
4902 : {
4903 26737 : if (cand->second_conv->kind == ck_ambig)
4904 65 : ret = error_mark_node;
4905 : else
4906 : {
4907 26672 : expr = convert_like (cand->second_conv, expr, complain);
4908 26672 : ret = convert_from_reference (expr);
4909 : }
4910 : }
4911 : else
4912 : ret = NULL_TREE;
4913 :
4914 53882 : return ret;
4915 26941 : }
4916 :
4917 : /* Give a helpful diagnostic when implicit_conversion fails. */
4918 :
4919 : static void
4920 668 : implicit_conversion_error (location_t loc, tree type, tree expr,
4921 : int flags)
4922 : {
4923 668 : tsubst_flags_t complain = tf_warning_or_error;
4924 :
4925 : /* If expr has unknown type, then it is an overloaded function.
4926 : Call instantiate_type to get good error messages. */
4927 668 : if (TREE_TYPE (expr) == unknown_type_node)
4928 58 : instantiate_type (type, expr, complain);
4929 610 : else if (invalid_nonstatic_memfn_p (loc, expr, complain))
4930 : /* We gave an error. */;
4931 70 : else if (BRACE_ENCLOSED_INITIALIZER_P (expr)
4932 67 : && CONSTRUCTOR_IS_DESIGNATED_INIT (expr)
4933 623 : && !CP_AGGREGATE_TYPE_P (type))
4934 18 : error_at (loc, "designated initializers cannot be used with a "
4935 : "non-aggregate type %qT", type);
4936 : else
4937 : {
4938 587 : auto_diagnostic_group d;
4939 587 : if (is_stub_object (expr))
4940 : /* The expression is generated by a trait check, we don't have
4941 : a useful location to highlight the label. */
4942 17 : error_at (loc, "could not convert %qH to %qI",
4943 17 : TREE_TYPE (expr), type);
4944 : else
4945 : {
4946 570 : range_label_for_type_mismatch label (TREE_TYPE (expr), type);
4947 570 : gcc_rich_location rich_loc (loc, &label,
4948 570 : highlight_colors::percent_h);
4949 570 : error_at (&rich_loc, "could not convert %qE from %qH to %qI",
4950 570 : expr, TREE_TYPE (expr), type);
4951 570 : }
4952 587 : maybe_show_nonconverting_candidate (type, TREE_TYPE (expr), expr, flags);
4953 587 : }
4954 668 : }
4955 :
4956 : /* Worker for build_converted_constant_expr. */
4957 :
4958 : static tree
4959 297968755 : build_converted_constant_expr_internal (tree type, tree expr,
4960 : int flags, tsubst_flags_t complain)
4961 : {
4962 297968755 : conversion *conv;
4963 297968755 : tree t;
4964 297968755 : location_t loc = cp_expr_loc_or_input_loc (expr);
4965 :
4966 297968755 : if (error_operand_p (expr))
4967 57 : return error_mark_node;
4968 :
4969 297968698 : conversion_obstack_sentinel cos;
4970 :
4971 297968698 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
4972 : /*c_cast_p=*/false, flags, complain);
4973 :
4974 : /* A converted constant expression of type T is an expression, implicitly
4975 : converted to type T, where the converted expression is a constant
4976 : expression and the implicit conversion sequence contains only
4977 :
4978 : * user-defined conversions,
4979 : * lvalue-to-rvalue conversions (7.1),
4980 : * array-to-pointer conversions (7.2),
4981 : * function-to-pointer conversions (7.3),
4982 : * qualification conversions (7.5),
4983 : * integral promotions (7.6),
4984 : * integral conversions (7.8) other than narrowing conversions (11.6.4),
4985 : * null pointer conversions (7.11) from std::nullptr_t,
4986 : * null member pointer conversions (7.12) from std::nullptr_t, and
4987 : * function pointer conversions (7.13),
4988 :
4989 : and where the reference binding (if any) binds directly. */
4990 :
4991 297968698 : for (conversion *c = conv;
4992 345927105 : c && c->kind != ck_identity;
4993 47958407 : c = next_conversion (c))
4994 : {
4995 47958407 : switch (c->kind)
4996 : {
4997 : /* A conversion function is OK. If it isn't constexpr, we'll
4998 : complain later that the argument isn't constant. */
4999 : case ck_user:
5000 : /* List-initialization is OK. */
5001 : case ck_aggr:
5002 : /* The lvalue-to-rvalue conversion is OK. */
5003 : case ck_rvalue:
5004 : /* Array-to-pointer and function-to-pointer. */
5005 : case ck_lvalue:
5006 : /* Function pointer conversions. */
5007 : case ck_fnptr:
5008 : /* Qualification conversions. */
5009 : case ck_qual:
5010 : break;
5011 :
5012 547 : case ck_ref_bind:
5013 547 : if (c->need_temporary_p)
5014 : {
5015 0 : if (complain & tf_error)
5016 0 : error_at (loc, "initializing %qH with %qI in converted "
5017 : "constant expression does not bind directly",
5018 0 : type, next_conversion (c)->type);
5019 : conv = NULL;
5020 : }
5021 : break;
5022 :
5023 8786104 : case ck_base:
5024 8786104 : case ck_pmem:
5025 8786104 : case ck_ptr:
5026 8786104 : case ck_std:
5027 8786104 : t = next_conversion (c)->type;
5028 8786104 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (t)
5029 8786029 : && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5030 : /* Integral promotion or conversion. */
5031 : break;
5032 106 : if (NULLPTR_TYPE_P (t))
5033 : /* Conversion from nullptr to pointer or pointer-to-member. */
5034 : break;
5035 :
5036 106 : if (complain & tf_error)
5037 80 : error_at (loc, "conversion from %qH to %qI in a "
5038 : "converted constant expression", t, type);
5039 : /* fall through. */
5040 :
5041 : default:
5042 : conv = NULL;
5043 : break;
5044 : }
5045 : }
5046 :
5047 : /* Avoid confusing convert_nontype_argument by introducing
5048 : a redundant conversion to the same reference type. */
5049 297968046 : if (conv && conv->kind == ck_ref_bind
5050 297969245 : && REFERENCE_REF_P (expr))
5051 : {
5052 301 : tree ref = TREE_OPERAND (expr, 0);
5053 301 : if (same_type_p (type, TREE_TYPE (ref)))
5054 : return ref;
5055 : }
5056 :
5057 297968419 : if (conv)
5058 : {
5059 : /* Don't copy a class in a template. */
5060 29407 : if (CLASS_TYPE_P (type) && conv->kind == ck_rvalue
5061 297982730 : && processing_template_decl)
5062 155 : conv = next_conversion (conv);
5063 :
5064 : /* Issuing conversion warnings for value-dependent expressions is
5065 : likely too noisy. */
5066 297967767 : warning_sentinel w (warn_conversion);
5067 297967767 : conv->check_narrowing = true;
5068 297967767 : conv->check_narrowing_const_only = true;
5069 297967767 : expr = convert_like (conv, expr, complain);
5070 297967767 : }
5071 : else
5072 : {
5073 652 : if (complain & tf_error)
5074 189 : implicit_conversion_error (loc, type, expr, flags);
5075 652 : expr = error_mark_node;
5076 : }
5077 :
5078 : return expr;
5079 297968698 : }
5080 :
5081 : /* Subroutine of convert_nontype_argument.
5082 :
5083 : EXPR is an expression used in a context that requires a converted
5084 : constant-expression, such as a template non-type parameter. Do any
5085 : necessary conversions (that are permitted for converted
5086 : constant-expressions) to convert it to the desired type.
5087 :
5088 : This function doesn't consider explicit conversion functions. If
5089 : you mean to use "a contextually converted constant expression of type
5090 : bool", use build_converted_constant_bool_expr.
5091 :
5092 : If conversion is successful, returns the converted expression;
5093 : otherwise, returns error_mark_node. */
5094 :
5095 : tree
5096 169681029 : build_converted_constant_expr (tree type, tree expr, tsubst_flags_t complain)
5097 : {
5098 169681029 : return build_converted_constant_expr_internal (type, expr, LOOKUP_IMPLICIT,
5099 169681029 : complain);
5100 : }
5101 :
5102 : /* Used to create "a contextually converted constant expression of type
5103 : bool". This differs from build_converted_constant_expr in that it
5104 : also considers explicit conversion functions. */
5105 :
5106 : tree
5107 128287726 : build_converted_constant_bool_expr (tree expr, tsubst_flags_t complain)
5108 : {
5109 128287726 : return build_converted_constant_expr_internal (boolean_type_node, expr,
5110 128287726 : LOOKUP_NORMAL, complain);
5111 : }
5112 :
5113 : /* Do any initial processing on the arguments to a function call. */
5114 :
5115 : vec<tree, va_gc> *
5116 190190739 : resolve_args (vec<tree, va_gc> *args, tsubst_flags_t complain)
5117 : {
5118 190190739 : unsigned int ix;
5119 190190739 : tree arg;
5120 :
5121 354561167 : FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
5122 : {
5123 164371068 : if (error_operand_p (arg))
5124 : return NULL;
5125 164370565 : else if (VOID_TYPE_P (TREE_TYPE (arg)))
5126 : {
5127 110 : if (complain & tf_error)
5128 12 : error_at (cp_expr_loc_or_input_loc (arg),
5129 : "invalid use of void expression");
5130 110 : return NULL;
5131 : }
5132 164370455 : else if (invalid_nonstatic_memfn_p (EXPR_LOCATION (arg), arg, complain))
5133 : return NULL;
5134 :
5135 : /* Force auto deduction now. Omit tf_warning to avoid redundant
5136 : deprecated warning on deprecated-14.C. */
5137 164370428 : if (!mark_single_function (arg, complain & ~tf_warning))
5138 : return NULL;
5139 : }
5140 : return args;
5141 : }
5142 :
5143 : /* Perform overload resolution on FN, which is called with the ARGS.
5144 :
5145 : Return the candidate function selected by overload resolution, or
5146 : NULL if the event that overload resolution failed. In the case
5147 : that overload resolution fails, *CANDIDATES will be the set of
5148 : candidates considered, and ANY_VIABLE_P will be set to true or
5149 : false to indicate whether or not any of the candidates were
5150 : viable.
5151 :
5152 : The ARGS should already have gone through RESOLVE_ARGS before this
5153 : function is called. */
5154 :
5155 : static struct z_candidate *
5156 89488769 : perform_overload_resolution (tree fn,
5157 : const vec<tree, va_gc> *args,
5158 : struct z_candidate **candidates,
5159 : bool *any_viable_p, tsubst_flags_t complain)
5160 : {
5161 89488769 : struct z_candidate *cand;
5162 89488769 : tree explicit_targs;
5163 89488769 : int template_only;
5164 :
5165 89488769 : auto_cond_timevar tv (TV_OVERLOAD);
5166 :
5167 89488769 : explicit_targs = NULL_TREE;
5168 89488769 : template_only = 0;
5169 :
5170 89488769 : *candidates = NULL;
5171 89488769 : *any_viable_p = true;
5172 :
5173 : /* Check FN. */
5174 89488769 : gcc_assert (OVL_P (fn) || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
5175 :
5176 89488769 : if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
5177 : {
5178 30104492 : explicit_targs = TREE_OPERAND (fn, 1);
5179 30104492 : fn = TREE_OPERAND (fn, 0);
5180 30104492 : template_only = 1;
5181 : }
5182 :
5183 : /* Add the various candidate functions. */
5184 89488769 : add_candidates (fn, NULL_TREE, args, NULL_TREE,
5185 : explicit_targs, template_only,
5186 : /*conversion_path=*/NULL_TREE,
5187 : /*access_path=*/NULL_TREE,
5188 : LOOKUP_NORMAL,
5189 : candidates, complain & ~tf_any_viable);
5190 :
5191 89475251 : *candidates = splice_viable (*candidates, false, any_viable_p);
5192 89475251 : if (*any_viable_p)
5193 : {
5194 89311852 : if (complain & tf_any_viable)
5195 : cand = *candidates;
5196 : else
5197 89311672 : cand = tourney (*candidates, complain);
5198 : }
5199 : else
5200 : cand = NULL;
5201 :
5202 178950502 : return cand;
5203 89475251 : }
5204 :
5205 : /* Print an error message about being unable to build a call to FN with
5206 : ARGS. ANY_VIABLE_P indicates whether any candidate functions could
5207 : be located; CANDIDATES is a possibly empty list of such
5208 : functions. */
5209 :
5210 : static void
5211 2915 : print_error_for_call_failure (tree fn, const vec<tree, va_gc> *args,
5212 : struct z_candidate *candidates)
5213 : {
5214 2915 : tree targs = NULL_TREE;
5215 2915 : if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
5216 : {
5217 516 : targs = TREE_OPERAND (fn, 1);
5218 516 : fn = TREE_OPERAND (fn, 0);
5219 : }
5220 5830 : tree name = OVL_NAME (fn);
5221 2915 : location_t loc = location_of (name);
5222 2915 : if (targs)
5223 516 : name = lookup_template_function (name, targs);
5224 :
5225 2915 : auto_diagnostic_group d;
5226 5830 : if (!any_strictly_viable (candidates))
5227 2439 : error_at (loc, "no matching function for call to %<%D(%A)%>",
5228 : name, build_tree_list_vec (args));
5229 : else
5230 476 : error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous",
5231 : name, build_tree_list_vec (args));
5232 2915 : if (candidates)
5233 2915 : print_z_candidates (loc, candidates);
5234 2915 : }
5235 :
5236 : /* Perform overload resolution on the set of deduction guides DGUIDES
5237 : using ARGS. Returns the selected deduction guide, or error_mark_node
5238 : if overload resolution fails. */
5239 :
5240 : tree
5241 29657 : perform_dguide_overload_resolution (tree dguides, const vec<tree, va_gc> *args,
5242 : tsubst_flags_t complain)
5243 : {
5244 29657 : z_candidate *candidates;
5245 29657 : bool any_viable_p;
5246 29657 : tree result;
5247 :
5248 59314 : gcc_assert (deduction_guide_p (OVL_FIRST (dguides)));
5249 :
5250 29657 : conversion_obstack_sentinel cos;
5251 :
5252 29657 : z_candidate *cand = perform_overload_resolution (dguides, args, &candidates,
5253 : &any_viable_p, complain);
5254 29657 : if (!cand)
5255 : {
5256 903 : if (complain & tf_error)
5257 193 : print_error_for_call_failure (dguides, args, candidates);
5258 903 : result = error_mark_node;
5259 : }
5260 : else
5261 28754 : result = cand->fn;
5262 :
5263 59314 : return result;
5264 29657 : }
5265 :
5266 : /* Return an expression for a call to FN (a namespace-scope function,
5267 : or a static member function) with the ARGS. This may change
5268 : ARGS. */
5269 :
5270 : tree
5271 88452659 : build_new_function_call (tree fn, vec<tree, va_gc> **args,
5272 : tsubst_flags_t complain)
5273 : {
5274 88452659 : struct z_candidate *candidates, *cand;
5275 88452659 : bool any_viable_p;
5276 88452659 : tree result;
5277 :
5278 88452659 : if (args != NULL && *args != NULL)
5279 : {
5280 88452659 : *args = resolve_args (*args, complain & ~tf_any_viable);
5281 88452659 : if (*args == NULL)
5282 362 : return error_mark_node;
5283 : }
5284 :
5285 88452297 : if (flag_tm)
5286 3544 : tm_malloc_replacement (fn);
5287 :
5288 88452297 : conversion_obstack_sentinel cos;
5289 :
5290 88452297 : cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
5291 : complain);
5292 :
5293 88438779 : if (!cand)
5294 : {
5295 172780 : if (complain & tf_error)
5296 : {
5297 : // If there is a single (non-viable) function candidate,
5298 : // let the error be diagnosed by cp_build_function_call_vec.
5299 3136 : if (!any_viable_p && candidates && ! candidates->next
5300 1362 : && TREE_CODE (candidates->fn) == FUNCTION_DECL
5301 : /* A template-id callee consisting of a single (ignored)
5302 : non-template candidate needs to be diagnosed the
5303 : ordinary way. */
5304 433 : && (TREE_CODE (fn) != TEMPLATE_ID_EXPR
5305 31 : || candidates->template_decl))
5306 423 : return cp_build_function_call_vec (candidates->fn, args, complain);
5307 :
5308 : // Otherwise, emit notes for non-viable candidates.
5309 2713 : print_error_for_call_failure (fn, *args, candidates);
5310 : }
5311 172357 : result = error_mark_node;
5312 : }
5313 88265999 : else if (complain & tf_any_viable)
5314 180 : return void_node;
5315 : else
5316 88265819 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
5317 :
5318 88438176 : if (flag_coroutines
5319 86366737 : && result
5320 86366737 : && TREE_CODE (result) == CALL_EXPR
5321 142216947 : && DECL_BUILT_IN_CLASS (TREE_OPERAND (CALL_EXPR_FN (result), 0))
5322 53778771 : == BUILT_IN_NORMAL)
5323 7515731 : result = coro_validate_builtin_call (result);
5324 :
5325 : return result;
5326 88438779 : }
5327 :
5328 : /* Build a call to a global operator new. FNNAME is the name of the
5329 : operator (either "operator new" or "operator new[]") and ARGS are
5330 : the arguments provided. This may change ARGS. *SIZE points to the
5331 : total number of bytes required by the allocation, and is updated if
5332 : that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
5333 : be used. If this function determines that no cookie should be
5334 : used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK
5335 : is not NULL_TREE, it is evaluated before calculating the final
5336 : array size, and if it fails, the array size is replaced with
5337 : (size_t)-1 (usually triggering a std::bad_alloc exception). If FN
5338 : is non-NULL, it will be set, upon return, to the allocation
5339 : function called. */
5340 :
5341 : tree
5342 1006792 : build_operator_new_call (tree fnname, vec<tree, va_gc> **args,
5343 : tree *size, tree *cookie_size,
5344 : tree align_arg, tree size_check,
5345 : tree *fn, tsubst_flags_t complain)
5346 : {
5347 1006792 : tree original_size = *size;
5348 1006792 : tree fns;
5349 1006792 : struct z_candidate *candidates;
5350 1006792 : struct z_candidate *cand = NULL;
5351 1006792 : bool any_viable_p;
5352 :
5353 1006792 : if (fn)
5354 1005354 : *fn = NULL_TREE;
5355 : /* Set to (size_t)-1 if the size check fails. */
5356 1006792 : if (size_check != NULL_TREE)
5357 : {
5358 20309 : tree errval = TYPE_MAX_VALUE (sizetype);
5359 20309 : if (cxx_dialect >= cxx11 && flag_exceptions)
5360 19914 : errval = throw_bad_array_new_length ();
5361 20309 : *size = fold_build3 (COND_EXPR, sizetype, size_check,
5362 : original_size, errval);
5363 : }
5364 1006792 : vec_safe_insert (*args, 0, *size);
5365 1006792 : *args = resolve_args (*args, complain);
5366 1006792 : if (*args == NULL)
5367 0 : return error_mark_node;
5368 :
5369 1006792 : conversion_obstack_sentinel cos;
5370 :
5371 : /* Based on:
5372 :
5373 : [expr.new]
5374 :
5375 : If this lookup fails to find the name, or if the allocated type
5376 : is not a class type, the allocation function's name is looked
5377 : up in the global scope.
5378 :
5379 : we disregard block-scope declarations of "operator new". */
5380 1006792 : fns = lookup_qualified_name (global_namespace, fnname);
5381 :
5382 1006792 : if (align_arg)
5383 : {
5384 9068 : vec<tree, va_gc>* align_args
5385 9068 : = vec_copy_and_insert (*args, align_arg, 1);
5386 9068 : cand = perform_overload_resolution (fns, align_args, &candidates,
5387 : &any_viable_p, tf_none);
5388 9068 : if (cand)
5389 9045 : *args = align_args;
5390 : /* If no aligned allocation function matches, try again without the
5391 : alignment. */
5392 : }
5393 :
5394 : /* Figure out what function is being called. */
5395 9045 : if (!cand)
5396 997747 : cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p,
5397 : complain);
5398 :
5399 : /* If no suitable function could be found, issue an error message
5400 : and give up. */
5401 1006792 : if (!cand)
5402 : {
5403 9 : if (complain & tf_error)
5404 9 : print_error_for_call_failure (fns, *args, candidates);
5405 9 : return error_mark_node;
5406 : }
5407 :
5408 : /* If a cookie is required, add some extra space. Whether
5409 : or not a cookie is required cannot be determined until
5410 : after we know which function was called. */
5411 1006783 : if (*cookie_size)
5412 : {
5413 268 : bool use_cookie = true;
5414 268 : tree arg_types;
5415 :
5416 268 : arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
5417 : /* Skip the size_t parameter. */
5418 268 : arg_types = TREE_CHAIN (arg_types);
5419 : /* Check the remaining parameters (if any). */
5420 268 : if (arg_types
5421 268 : && TREE_CHAIN (arg_types) == void_list_node
5422 331 : && same_type_p (TREE_VALUE (arg_types),
5423 : ptr_type_node))
5424 48 : use_cookie = false;
5425 : /* If we need a cookie, adjust the number of bytes allocated. */
5426 268 : if (use_cookie)
5427 : {
5428 : /* Update the total size. */
5429 220 : *size = size_binop (PLUS_EXPR, original_size, *cookie_size);
5430 220 : if (size_check)
5431 : {
5432 : /* Set to (size_t)-1 if the size check fails. */
5433 47 : gcc_assert (size_check != NULL_TREE);
5434 47 : *size = fold_build3 (COND_EXPR, sizetype, size_check,
5435 : *size, TYPE_MAX_VALUE (sizetype));
5436 : }
5437 : /* Update the argument list to reflect the adjusted size. */
5438 220 : (**args)[0] = *size;
5439 : }
5440 : else
5441 48 : *cookie_size = NULL_TREE;
5442 : }
5443 :
5444 : /* Tell our caller which function we decided to call. */
5445 1006783 : if (fn)
5446 1005345 : *fn = cand->fn;
5447 :
5448 : /* Build the CALL_EXPR. */
5449 1006783 : tree ret = build_over_call (cand, LOOKUP_NORMAL, complain);
5450 :
5451 : /* Set this flag for all callers of this function. In addition to
5452 : new-expressions, this is called for allocating coroutine state; treat
5453 : that as an implicit new-expression. */
5454 1006783 : tree call = extract_call_expr (ret);
5455 1006783 : if (TREE_CODE (call) == CALL_EXPR)
5456 1006783 : CALL_FROM_NEW_OR_DELETE_P (call) = 1;
5457 :
5458 : return ret;
5459 1006792 : }
5460 :
5461 : /* Evaluate side-effects from OBJ before evaluating call
5462 : to FN in RESULT expression.
5463 : This is for expressions of the form `obj->fn(...)'
5464 : where `fn' turns out to be a static member function and
5465 : `obj' needs to be evaluated. `fn' could be also static operator[]
5466 : or static operator(), in which cases the source expression
5467 : would be `obj[...]' or `obj(...)'. */
5468 :
5469 : tree
5470 75490532 : keep_unused_object_arg (tree result, tree obj, tree fn)
5471 : {
5472 75490532 : if (result == NULL_TREE
5473 75490532 : || result == error_mark_node
5474 74538076 : || DECL_OBJECT_MEMBER_FUNCTION_P (fn)
5475 78032799 : || !TREE_SIDE_EFFECTS (obj))
5476 : return result;
5477 :
5478 : /* But avoid the implicit lvalue-rvalue conversion when `obj' is
5479 : volatile. */
5480 84264 : tree a = obj;
5481 84264 : if (TREE_THIS_VOLATILE (a))
5482 55902 : a = build_this (a);
5483 84264 : if (TREE_SIDE_EFFECTS (a))
5484 28371 : return cp_build_compound_expr (a, result, tf_error);
5485 : return result;
5486 : }
5487 :
5488 : /* Build a new call to operator(). This may change ARGS. */
5489 :
5490 : tree
5491 2153437 : build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
5492 : {
5493 2153437 : struct z_candidate *candidates = 0, *cand;
5494 2153437 : tree fns, convs, first_mem_arg = NULL_TREE;
5495 2153437 : bool any_viable_p;
5496 2153437 : tree result = NULL_TREE;
5497 :
5498 2153437 : auto_cond_timevar tv (TV_OVERLOAD);
5499 :
5500 2153437 : obj = mark_lvalue_use (obj);
5501 :
5502 2153437 : if (error_operand_p (obj))
5503 0 : return error_mark_node;
5504 :
5505 2153437 : tree type = TREE_TYPE (obj);
5506 :
5507 2153437 : obj = prep_operand (obj);
5508 :
5509 2153437 : if (TYPE_PTRMEMFUNC_P (type))
5510 : {
5511 0 : if (complain & tf_error)
5512 : /* It's no good looking for an overloaded operator() on a
5513 : pointer-to-member-function. */
5514 0 : error ("pointer-to-member function %qE cannot be called without "
5515 : "an object; consider using %<.*%> or %<->*%>", obj);
5516 0 : return error_mark_node;
5517 : }
5518 :
5519 2153437 : if (TYPE_BINFO (type))
5520 : {
5521 2153419 : fns = lookup_fnfields (TYPE_BINFO (type), call_op_identifier, 1, complain);
5522 2153419 : if (fns == error_mark_node)
5523 : return error_mark_node;
5524 : }
5525 : else
5526 : fns = NULL_TREE;
5527 :
5528 2153427 : if (args != NULL && *args != NULL)
5529 : {
5530 2153427 : *args = resolve_args (*args, complain);
5531 2153427 : if (*args == NULL)
5532 109 : return error_mark_node;
5533 : }
5534 :
5535 2153318 : conversion_obstack_sentinel cos;
5536 :
5537 2153318 : if (fns)
5538 : {
5539 2150440 : first_mem_arg = obj;
5540 :
5541 2150440 : add_candidates (BASELINK_FUNCTIONS (fns),
5542 : first_mem_arg, *args, NULL_TREE,
5543 : NULL_TREE, false,
5544 2150440 : BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
5545 : LOOKUP_NORMAL, &candidates, complain);
5546 : }
5547 :
5548 2153318 : bool any_call_ops = candidates != nullptr;
5549 :
5550 2153318 : convs = lookup_conversions (type);
5551 :
5552 2303306 : for (; convs; convs = TREE_CHAIN (convs))
5553 : {
5554 149988 : tree totype = TREE_TYPE (convs);
5555 :
5556 130411 : if (TYPE_PTRFN_P (totype)
5557 19580 : || TYPE_REFFN_P (totype)
5558 169524 : || (TYPE_REF_P (totype)
5559 1038 : && TYPE_PTRFN_P (TREE_TYPE (totype))))
5560 261228 : for (tree fn : ovl_range (TREE_VALUE (convs)))
5561 : {
5562 130614 : if (DECL_NONCONVERTING_P (fn))
5563 3 : continue;
5564 :
5565 130611 : if (TREE_CODE (fn) == TEMPLATE_DECL)
5566 : {
5567 : /* Making this work broke PR 71117 and 85118, so until the
5568 : committee resolves core issue 2189, let's disable this
5569 : candidate if there are any call operators. */
5570 34758 : if (any_call_ops)
5571 34746 : continue;
5572 :
5573 12 : add_template_conv_candidate
5574 12 : (&candidates, fn, obj, *args, totype,
5575 : /*access_path=*/NULL_TREE,
5576 : /*conversion_path=*/NULL_TREE, complain);
5577 : }
5578 : else
5579 95853 : add_conv_candidate (&candidates, fn, obj,
5580 : *args, /*conversion_path=*/NULL_TREE,
5581 : /*access_path=*/NULL_TREE, complain);
5582 : }
5583 : }
5584 :
5585 : /* Be strict here because if we choose a bad conversion candidate, the
5586 : errors we get won't mention the call context. */
5587 2153318 : candidates = splice_viable (candidates, true, &any_viable_p);
5588 2153318 : if (!any_viable_p)
5589 : {
5590 27623 : if (complain & tf_error)
5591 : {
5592 280 : auto_diagnostic_group d;
5593 280 : error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
5594 : build_tree_list_vec (*args));
5595 280 : print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
5596 280 : }
5597 27623 : result = error_mark_node;
5598 : }
5599 : else
5600 : {
5601 2125695 : cand = tourney (candidates, complain);
5602 2125695 : if (cand == 0)
5603 : {
5604 17 : if (complain & tf_error)
5605 : {
5606 6 : auto_diagnostic_group d;
5607 12 : error ("call of %<(%T) (%A)%> is ambiguous",
5608 6 : TREE_TYPE (obj), build_tree_list_vec (*args));
5609 6 : print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
5610 6 : }
5611 17 : result = error_mark_node;
5612 : }
5613 2125678 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL
5614 2125619 : && DECL_OVERLOADED_OPERATOR_P (cand->fn)
5615 4251297 : && DECL_OVERLOADED_OPERATOR_IS (cand->fn, CALL_EXPR))
5616 : {
5617 2125619 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
5618 : /* In an expression of the form `a()' where cand->fn
5619 : which is operator() turns out to be a static member function,
5620 : `a' is none-the-less evaluated. */
5621 2125619 : result = keep_unused_object_arg (result, obj, cand->fn);
5622 : }
5623 : else
5624 : {
5625 59 : if (TREE_CODE (cand->fn) == FUNCTION_DECL)
5626 0 : obj = convert_like_with_context (cand->convs[0], obj, cand->fn,
5627 : -1, complain);
5628 : else
5629 : {
5630 59 : gcc_checking_assert (TYPE_P (cand->fn));
5631 59 : obj = convert_like (cand->convs[0], obj, complain);
5632 : }
5633 59 : obj = convert_from_reference (obj);
5634 59 : result = cp_build_function_call_vec (obj, args, complain);
5635 : }
5636 : }
5637 :
5638 2153318 : return result;
5639 2153437 : }
5640 :
5641 : /* Subroutine for preparing format strings suitable for the error
5642 : function. It concatenates a prefix (controlled by MATCH), ERRMSG,
5643 : and SUFFIX. */
5644 :
5645 : static const char *
5646 1465 : concat_op_error_string (bool match, const char *errmsg, const char *suffix)
5647 : {
5648 1465 : return concat (match
5649 : ? G_("ambiguous overload for ")
5650 : : G_("no match for "),
5651 0 : errmsg, suffix, nullptr);
5652 : }
5653 :
5654 : /* Called by op_error to prepare format strings suitable for the error
5655 : function. It concatenates a prefix (controlled by MATCH), ERRMSG,
5656 : and a suffix (controlled by NTYPES). */
5657 :
5658 : static const char *
5659 1444 : op_error_string (const char *errmsg, int ntypes, bool match)
5660 : {
5661 1444 : const char *suffix;
5662 0 : if (ntypes == 3)
5663 : suffix = G_(" (operand types are %qT, %qT, and %qT)");
5664 0 : else if (ntypes == 2)
5665 : suffix = G_(" (operand types are %qT and %qT)");
5666 : else
5667 0 : suffix = G_(" (operand type is %qT)");
5668 0 : return concat_op_error_string (match, errmsg, suffix);
5669 : }
5670 :
5671 : /* Similar to op_error_string, but a special-case for binary ops that
5672 : use %e for the args, rather than %qT. */
5673 :
5674 : static const char *
5675 21 : binop_error_string (const char *errmsg, bool match)
5676 : {
5677 21 : return concat_op_error_string (match, errmsg,
5678 21 : G_(" (operand types are %e and %e)"));
5679 : }
5680 :
5681 : static void
5682 1465 : op_error (const op_location_t &loc,
5683 : enum tree_code code, enum tree_code code2,
5684 : tree arg1, tree arg2, tree arg3, bool match)
5685 : {
5686 1465 : bool assop = code == MODIFY_EXPR;
5687 1465 : const char *opname = OVL_OP_INFO (assop, assop ? code2 : code)->name;
5688 :
5689 1465 : switch (code)
5690 : {
5691 0 : case COND_EXPR:
5692 0 : if (flag_diagnostics_show_caret)
5693 0 : error_at (loc, op_error_string (G_("ternary %<operator?:%>"),
5694 : 3, match),
5695 0 : TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
5696 : else
5697 0 : error_at (loc, op_error_string (G_("ternary %<operator?:%> "
5698 : "in %<%E ? %E : %E%>"), 3, match),
5699 : arg1, arg2, arg3,
5700 0 : TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
5701 : break;
5702 :
5703 0 : case POSTINCREMENT_EXPR:
5704 0 : case POSTDECREMENT_EXPR:
5705 0 : if (flag_diagnostics_show_caret)
5706 0 : error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
5707 0 : opname, TREE_TYPE (arg1));
5708 : else
5709 0 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
5710 : 1, match),
5711 0 : opname, arg1, opname, TREE_TYPE (arg1));
5712 : break;
5713 :
5714 13 : case ARRAY_REF:
5715 13 : if (flag_diagnostics_show_caret)
5716 0 : error_at (loc, op_error_string (G_("%<operator[]%>"), 2, match),
5717 0 : TREE_TYPE (arg1), TREE_TYPE (arg2));
5718 : else
5719 26 : error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
5720 : 2, match),
5721 13 : arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2));
5722 : break;
5723 :
5724 6 : case REALPART_EXPR:
5725 6 : case IMAGPART_EXPR:
5726 6 : if (flag_diagnostics_show_caret)
5727 0 : error_at (loc, op_error_string (G_("%qs"), 1, match),
5728 0 : opname, TREE_TYPE (arg1));
5729 : else
5730 12 : error_at (loc, op_error_string (G_("%qs in %<%s %E%>"), 1, match),
5731 6 : opname, opname, arg1, TREE_TYPE (arg1));
5732 : break;
5733 :
5734 0 : case CO_AWAIT_EXPR:
5735 0 : if (flag_diagnostics_show_caret)
5736 0 : error_at (loc, op_error_string (G_("%<operator %s%>"), 1, match),
5737 0 : opname, TREE_TYPE (arg1));
5738 : else
5739 0 : error_at (loc, op_error_string (G_("%<operator %s%> in %<%s%E%>"),
5740 : 1, match),
5741 0 : opname, opname, arg1, TREE_TYPE (arg1));
5742 : break;
5743 :
5744 1446 : default:
5745 1446 : if (arg2)
5746 1348 : if (flag_diagnostics_show_caret)
5747 : {
5748 21 : binary_op_rich_location richloc (loc, arg1, arg2, true);
5749 21 : pp_markup::element_quoted_type element_0
5750 21 : (TREE_TYPE (arg1), highlight_colors::lhs);
5751 21 : pp_markup::element_quoted_type element_1
5752 21 : (TREE_TYPE (arg2), highlight_colors::rhs);
5753 21 : error_at (&richloc,
5754 : binop_error_string (G_("%<operator%s%>"), match),
5755 : opname, &element_0, &element_1);
5756 21 : }
5757 : else
5758 2654 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
5759 : 2, match),
5760 : opname, arg1, opname, arg2,
5761 1327 : TREE_TYPE (arg1), TREE_TYPE (arg2));
5762 : else
5763 98 : if (flag_diagnostics_show_caret)
5764 0 : error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
5765 0 : opname, TREE_TYPE (arg1));
5766 : else
5767 196 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
5768 : 1, match),
5769 98 : opname, opname, arg1, TREE_TYPE (arg1));
5770 : break;
5771 : }
5772 1465 : }
5773 :
5774 : /* Return the implicit conversion sequence that could be used to
5775 : convert E1 to E2 in [expr.cond]. */
5776 :
5777 : static conversion *
5778 517414 : conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
5779 : {
5780 517414 : tree t1 = non_reference (TREE_TYPE (e1));
5781 517414 : tree t2 = non_reference (TREE_TYPE (e2));
5782 517414 : conversion *conv;
5783 517414 : bool good_base;
5784 :
5785 : /* [expr.cond]
5786 :
5787 : If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
5788 : implicitly converted (clause _conv_) to the type "lvalue reference to
5789 : T2", subject to the constraint that in the conversion the
5790 : reference must bind directly (_dcl.init.ref_) to an lvalue.
5791 :
5792 : If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
5793 : implicitly converted to the type "rvalue reference to T2", subject to
5794 : the constraint that the reference must bind directly. */
5795 517414 : if (glvalue_p (e2))
5796 : {
5797 489572 : tree rtype = cp_build_reference_type (t2, !lvalue_p (e2));
5798 489572 : conv = implicit_conversion (rtype,
5799 : t1,
5800 : e1,
5801 : /*c_cast_p=*/false,
5802 : LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
5803 : |LOOKUP_ONLYCONVERTING,
5804 : complain);
5805 489572 : if (conv && !conv->bad_p)
5806 : return conv;
5807 : }
5808 :
5809 : /* If E2 is a prvalue or if neither of the conversions above can be done
5810 : and at least one of the operands has (possibly cv-qualified) class
5811 : type: */
5812 412973 : if (!CLASS_TYPE_P (t1) && !CLASS_TYPE_P (t2))
5813 : return NULL;
5814 :
5815 : /* [expr.cond]
5816 :
5817 : If E1 and E2 have class type, and the underlying class types are
5818 : the same or one is a base class of the other: E1 can be converted
5819 : to match E2 if the class of T2 is the same type as, or a base
5820 : class of, the class of T1, and the cv-qualification of T2 is the
5821 : same cv-qualification as, or a greater cv-qualification than, the
5822 : cv-qualification of T1. If the conversion is applied, E1 is
5823 : changed to an rvalue of type T2 that still refers to the original
5824 : source class object (or the appropriate subobject thereof). */
5825 184353 : if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
5826 369903 : && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
5827 : {
5828 55339 : if (good_base && at_least_as_qualified_p (t2, t1))
5829 : {
5830 27571 : conv = build_identity_conv (t1, e1);
5831 27571 : if (!same_type_p (TYPE_MAIN_VARIANT (t1),
5832 : TYPE_MAIN_VARIANT (t2)))
5833 6 : conv = build_conv (ck_base, t2, conv);
5834 : else
5835 27565 : conv = build_conv (ck_rvalue, t2, conv);
5836 27571 : return conv;
5837 : }
5838 : else
5839 27768 : return NULL;
5840 : }
5841 : else
5842 : /* [expr.cond]
5843 :
5844 : Otherwise: E1 can be converted to match E2 if E1 can be implicitly
5845 : converted to the type that expression E2 would have if E2 were
5846 : converted to an rvalue (or the type it has, if E2 is an rvalue). */
5847 254610 : return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
5848 254610 : LOOKUP_IMPLICIT, complain);
5849 : }
5850 :
5851 : /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
5852 : arguments to the conditional expression. */
5853 :
5854 : tree
5855 7463575 : build_conditional_expr (const op_location_t &loc,
5856 : tree arg1, tree arg2, tree arg3,
5857 : tsubst_flags_t complain)
5858 : {
5859 7463575 : tree arg2_type;
5860 7463575 : tree arg3_type;
5861 7463575 : tree result = NULL_TREE;
5862 7463575 : tree result_type = NULL_TREE;
5863 7463575 : tree semantic_result_type = NULL_TREE;
5864 7463575 : bool is_glvalue = true;
5865 7463575 : struct z_candidate *candidates = 0;
5866 7463575 : struct z_candidate *cand;
5867 7463575 : tree orig_arg2, orig_arg3;
5868 :
5869 7463575 : auto_cond_timevar tv (TV_OVERLOAD);
5870 :
5871 : /* As a G++ extension, the second argument to the conditional can be
5872 : omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
5873 : c'.) If the second operand is omitted, make sure it is
5874 : calculated only once. */
5875 7463575 : if (!arg2)
5876 : {
5877 380 : if (complain & tf_error)
5878 374 : pedwarn (loc, OPT_Wpedantic,
5879 : "ISO C++ forbids omitting the middle term of "
5880 : "a %<?:%> expression");
5881 :
5882 380 : if ((complain & tf_warning) && !truth_value_p (TREE_CODE (arg1)))
5883 332 : warn_for_omitted_condop (loc, arg1);
5884 :
5885 : /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
5886 380 : if (glvalue_p (arg1))
5887 : {
5888 89 : arg1 = cp_stabilize_reference (arg1);
5889 89 : arg2 = arg1 = prevent_lifetime_extension (arg1);
5890 : }
5891 291 : else if (TREE_CODE (arg1) == TARGET_EXPR)
5892 : /* arg1 can't be a prvalue result of the conditional
5893 : expression, since it needs to be materialized for the
5894 : conversion to bool, so treat it as an xvalue in arg2. */
5895 6 : arg2 = move (TARGET_EXPR_SLOT (arg1));
5896 285 : else if (TREE_CODE (arg1) == EXCESS_PRECISION_EXPR)
5897 3 : arg2 = arg1 = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (arg1),
5898 3 : cp_save_expr (TREE_OPERAND (arg1, 0)));
5899 : else
5900 282 : arg2 = arg1 = cp_save_expr (arg1);
5901 : }
5902 :
5903 : /* If something has already gone wrong, just pass that fact up the
5904 : tree. */
5905 7463575 : if (error_operand_p (arg1)
5906 7463486 : || error_operand_p (arg2)
5907 14927012 : || error_operand_p (arg3))
5908 180 : return error_mark_node;
5909 :
5910 7463395 : conversion_obstack_sentinel cos;
5911 :
5912 7463395 : orig_arg2 = arg2;
5913 7463395 : orig_arg3 = arg3;
5914 :
5915 7463395 : if (gnu_vector_type_p (TREE_TYPE (arg1))
5916 7463395 : && (VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1))
5917 3 : || VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (arg1))))
5918 : {
5919 1435 : tree arg1_type = TREE_TYPE (arg1);
5920 :
5921 : /* If arg1 is another cond_expr choosing between -1 and 0,
5922 : then we can use its comparison. It may help to avoid
5923 : additional comparison, produce more accurate diagnostics
5924 : and enables folding. */
5925 1435 : if (TREE_CODE (arg1) == VEC_COND_EXPR
5926 1253 : && integer_minus_onep (TREE_OPERAND (arg1, 1))
5927 2688 : && integer_zerop (TREE_OPERAND (arg1, 2)))
5928 1253 : arg1 = TREE_OPERAND (arg1, 0);
5929 :
5930 1435 : arg1 = force_rvalue (arg1, complain);
5931 1435 : arg2 = force_rvalue (arg2, complain);
5932 1435 : arg3 = force_rvalue (arg3, complain);
5933 :
5934 : /* force_rvalue can return error_mark on valid arguments. */
5935 1435 : if (error_operand_p (arg1)
5936 1435 : || error_operand_p (arg2)
5937 2870 : || error_operand_p (arg3))
5938 0 : return error_mark_node;
5939 :
5940 1435 : arg2_type = TREE_TYPE (arg2);
5941 1435 : arg3_type = TREE_TYPE (arg3);
5942 :
5943 1435 : if (!VECTOR_TYPE_P (arg2_type)
5944 312 : && !VECTOR_TYPE_P (arg3_type))
5945 : {
5946 : /* Rely on the error messages of the scalar version. */
5947 294 : tree scal = build_conditional_expr (loc, integer_one_node,
5948 : orig_arg2, orig_arg3, complain);
5949 294 : if (scal == error_mark_node)
5950 : return error_mark_node;
5951 294 : tree stype = TREE_TYPE (scal);
5952 294 : tree ctype = TREE_TYPE (arg1_type);
5953 294 : if (TYPE_SIZE (stype) != TYPE_SIZE (ctype)
5954 294 : || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype)))
5955 : {
5956 9 : if (complain & tf_error)
5957 9 : error_at (loc, "inferred scalar type %qT is not an integer or "
5958 : "floating-point type of the same size as %qT", stype,
5959 9 : COMPARISON_CLASS_P (arg1)
5960 9 : ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0)))
5961 : : ctype);
5962 9 : return error_mark_node;
5963 : }
5964 :
5965 285 : tree vtype = build_opaque_vector_type (stype,
5966 285 : TYPE_VECTOR_SUBPARTS (arg1_type));
5967 : /* We could pass complain & tf_warning to unsafe_conversion_p,
5968 : but the warnings (like Wsign-conversion) have already been
5969 : given by the scalar build_conditional_expr_1. We still check
5970 : unsafe_conversion_p to forbid truncating long long -> float. */
5971 285 : if (unsafe_conversion_p (stype, arg2, NULL_TREE, false))
5972 : {
5973 0 : if (complain & tf_error)
5974 0 : error_at (loc, "conversion of scalar %qH to vector %qI "
5975 : "involves truncation", arg2_type, vtype);
5976 0 : return error_mark_node;
5977 : }
5978 285 : if (unsafe_conversion_p (stype, arg3, NULL_TREE, false))
5979 : {
5980 3 : if (complain & tf_error)
5981 3 : error_at (loc, "conversion of scalar %qH to vector %qI "
5982 : "involves truncation", arg3_type, vtype);
5983 3 : return error_mark_node;
5984 : }
5985 :
5986 282 : arg2 = cp_convert (stype, arg2, complain);
5987 282 : arg2 = save_expr (arg2);
5988 282 : arg2 = build_vector_from_val (vtype, arg2);
5989 282 : arg2_type = vtype;
5990 282 : arg3 = cp_convert (stype, arg3, complain);
5991 282 : arg3 = save_expr (arg3);
5992 282 : arg3 = build_vector_from_val (vtype, arg3);
5993 282 : arg3_type = vtype;
5994 : }
5995 :
5996 2828 : if ((gnu_vector_type_p (arg2_type) && !VECTOR_TYPE_P (arg3_type))
5997 2750 : || (gnu_vector_type_p (arg3_type) && !VECTOR_TYPE_P (arg2_type)))
5998 : {
5999 96 : enum stv_conv convert_flag =
6000 96 : scalar_to_vector (loc, VEC_COND_EXPR, arg2, arg3,
6001 : complain & tf_error);
6002 :
6003 96 : switch (convert_flag)
6004 : {
6005 0 : case stv_error:
6006 0 : return error_mark_node;
6007 15 : case stv_firstarg:
6008 15 : {
6009 15 : arg2 = save_expr (arg2);
6010 15 : arg2 = convert (TREE_TYPE (arg3_type), arg2);
6011 15 : arg2 = build_vector_from_val (arg3_type, arg2);
6012 15 : arg2_type = TREE_TYPE (arg2);
6013 15 : break;
6014 : }
6015 72 : case stv_secondarg:
6016 72 : {
6017 72 : arg3 = save_expr (arg3);
6018 72 : arg3 = convert (TREE_TYPE (arg2_type), arg3);
6019 72 : arg3 = build_vector_from_val (arg2_type, arg3);
6020 72 : arg3_type = TREE_TYPE (arg3);
6021 72 : break;
6022 : }
6023 : default:
6024 : break;
6025 : }
6026 : }
6027 :
6028 1423 : if (!gnu_vector_type_p (arg2_type)
6029 1420 : || !gnu_vector_type_p (arg3_type)
6030 1414 : || !same_type_p (arg2_type, arg3_type)
6031 1414 : || maybe_ne (TYPE_VECTOR_SUBPARTS (arg1_type),
6032 1414 : TYPE_VECTOR_SUBPARTS (arg2_type))
6033 2837 : || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type))
6034 : {
6035 9 : if (complain & tf_error)
6036 6 : error_at (loc,
6037 : "incompatible vector types in conditional expression: "
6038 6 : "%qT, %qT and %qT", TREE_TYPE (arg1),
6039 6 : TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3));
6040 9 : return error_mark_node;
6041 : }
6042 :
6043 1414 : if (!COMPARISON_CLASS_P (arg1))
6044 : {
6045 179 : tree cmp_type = truth_type_for (arg1_type);
6046 179 : arg1 = build2 (NE_EXPR, cmp_type, arg1, build_zero_cst (arg1_type));
6047 : }
6048 1414 : return build3_loc (loc, VEC_COND_EXPR, arg2_type, arg1, arg2, arg3);
6049 : }
6050 :
6051 : /* [expr.cond]
6052 :
6053 : The first expression is implicitly converted to bool (clause
6054 : _conv_). */
6055 7461960 : arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
6056 : LOOKUP_NORMAL);
6057 7461960 : if (error_operand_p (arg1))
6058 25 : return error_mark_node;
6059 :
6060 7461935 : arg2_type = unlowered_expr_type (arg2);
6061 7461935 : arg3_type = unlowered_expr_type (arg3);
6062 :
6063 7461935 : if ((TREE_CODE (arg2) == EXCESS_PRECISION_EXPR
6064 7461917 : || TREE_CODE (arg3) == EXCESS_PRECISION_EXPR)
6065 45 : && (TREE_CODE (arg2_type) == INTEGER_TYPE
6066 : || SCALAR_FLOAT_TYPE_P (arg2_type)
6067 : || TREE_CODE (arg2_type) == COMPLEX_TYPE)
6068 45 : && (TREE_CODE (arg3_type) == INTEGER_TYPE
6069 : || SCALAR_FLOAT_TYPE_P (arg3_type)
6070 : || TREE_CODE (arg3_type) == COMPLEX_TYPE))
6071 : {
6072 45 : semantic_result_type
6073 45 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
6074 45 : if (semantic_result_type == error_mark_node)
6075 : {
6076 0 : tree t1 = arg2_type;
6077 0 : tree t2 = arg3_type;
6078 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6079 0 : t1 = TREE_TYPE (t1);
6080 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6081 0 : t2 = TREE_TYPE (t2);
6082 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
6083 : && SCALAR_FLOAT_TYPE_P (t2)
6084 : && (extended_float_type_p (t1)
6085 : || extended_float_type_p (t2))
6086 : && cp_compare_floating_point_conversion_ranks
6087 : (t1, t2) == 3);
6088 0 : if (complain & tf_error)
6089 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
6090 : "have unordered conversion rank",
6091 : arg2_type, arg3_type);
6092 0 : return error_mark_node;
6093 : }
6094 45 : if (TREE_CODE (arg2) == EXCESS_PRECISION_EXPR)
6095 : {
6096 18 : arg2 = TREE_OPERAND (arg2, 0);
6097 18 : arg2_type = TREE_TYPE (arg2);
6098 : }
6099 45 : if (TREE_CODE (arg3) == EXCESS_PRECISION_EXPR)
6100 : {
6101 27 : arg3 = TREE_OPERAND (arg3, 0);
6102 27 : arg3_type = TREE_TYPE (arg3);
6103 : }
6104 : }
6105 :
6106 : /* [expr.cond]
6107 :
6108 : If either the second or the third operand has type (possibly
6109 : cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
6110 : array-to-pointer (_conv.array_), and function-to-pointer
6111 : (_conv.func_) standard conversions are performed on the second
6112 : and third operands. */
6113 7461935 : if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
6114 : {
6115 : /* 'void' won't help in resolving an overloaded expression on the
6116 : other side, so require it to resolve by itself. */
6117 115648 : if (arg2_type == unknown_type_node)
6118 : {
6119 9 : arg2 = resolve_nondeduced_context_or_error (arg2, complain);
6120 9 : arg2_type = TREE_TYPE (arg2);
6121 : }
6122 115648 : if (arg3_type == unknown_type_node)
6123 : {
6124 0 : arg3 = resolve_nondeduced_context_or_error (arg3, complain);
6125 0 : arg3_type = TREE_TYPE (arg3);
6126 : }
6127 :
6128 : /* [expr.cond]
6129 :
6130 : One of the following shall hold:
6131 :
6132 : --The second or the third operand (but not both) is a
6133 : throw-expression (_except.throw_); the result is of the type
6134 : and value category of the other.
6135 :
6136 : --Both the second and the third operands have type void; the
6137 : result is of type void and is a prvalue. */
6138 115648 : if (TREE_CODE (arg2) == THROW_EXPR
6139 81 : && TREE_CODE (arg3) != THROW_EXPR)
6140 : {
6141 69 : result_type = arg3_type;
6142 69 : is_glvalue = glvalue_p (arg3);
6143 : }
6144 115579 : else if (TREE_CODE (arg2) != THROW_EXPR
6145 115567 : && TREE_CODE (arg3) == THROW_EXPR)
6146 : {
6147 171 : result_type = arg2_type;
6148 171 : is_glvalue = glvalue_p (arg2);
6149 : }
6150 115408 : else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
6151 : {
6152 115379 : result_type = void_type_node;
6153 115379 : is_glvalue = false;
6154 : }
6155 : else
6156 : {
6157 29 : if (complain & tf_error)
6158 : {
6159 18 : if (VOID_TYPE_P (arg2_type))
6160 9 : error_at (cp_expr_loc_or_loc (arg3, loc),
6161 : "second operand to the conditional operator "
6162 : "is of type %<void%>, but the third operand is "
6163 : "neither a throw-expression nor of type %<void%>");
6164 : else
6165 9 : error_at (cp_expr_loc_or_loc (arg2, loc),
6166 : "third operand to the conditional operator "
6167 : "is of type %<void%>, but the second operand is "
6168 : "neither a throw-expression nor of type %<void%>");
6169 : }
6170 29 : return error_mark_node;
6171 : }
6172 :
6173 115619 : goto valid_operands;
6174 : }
6175 : /* [expr.cond]
6176 :
6177 : Otherwise, if the second and third operand have different types,
6178 : and either has (possibly cv-qualified) class type, or if both are
6179 : glvalues of the same value category and the same type except for
6180 : cv-qualification, an attempt is made to convert each of those operands
6181 : to the type of the other. */
6182 7346287 : else if (!same_type_p (arg2_type, arg3_type)
6183 7346287 : && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)
6184 1478502 : || (same_type_ignoring_top_level_qualifiers_p (arg2_type,
6185 : arg3_type)
6186 424118 : && glvalue_p (arg2) && glvalue_p (arg3)
6187 103094 : && lvalue_p (arg2) == lvalue_p (arg3))))
6188 : {
6189 258707 : conversion *conv2;
6190 258707 : conversion *conv3;
6191 258707 : bool converted = false;
6192 :
6193 258707 : conv2 = conditional_conversion (arg2, arg3, complain);
6194 258707 : conv3 = conditional_conversion (arg3, arg2, complain);
6195 :
6196 : /* [expr.cond]
6197 :
6198 : If both can be converted, or one can be converted but the
6199 : conversion is ambiguous, the program is ill-formed. If
6200 : neither can be converted, the operands are left unchanged and
6201 : further checking is performed as described below. If exactly
6202 : one conversion is possible, that conversion is applied to the
6203 : chosen operand and the converted operand is used in place of
6204 : the original operand for the remainder of this section. */
6205 258707 : if ((conv2 && !conv2->bad_p
6206 61989 : && conv3 && !conv3->bad_p)
6207 61970 : || (conv2 && conv2->kind == ck_ambig)
6208 258592 : || (conv3 && conv3->kind == ck_ambig))
6209 : {
6210 115 : if (complain & tf_error)
6211 : {
6212 6 : error_at (loc, "operands to %<?:%> have different types "
6213 : "%qT and %qT",
6214 : arg2_type, arg3_type);
6215 6 : if (conv2 && !conv2->bad_p && conv3 && !conv3->bad_p)
6216 3 : inform (loc, " and each type can be converted to the other");
6217 3 : else if (conv2 && conv2->kind == ck_ambig)
6218 3 : convert_like (conv2, arg2, complain);
6219 : else
6220 0 : convert_like (conv3, arg3, complain);
6221 : }
6222 115 : result = error_mark_node;
6223 : }
6224 258592 : else if (conv2 && !conv2->bad_p)
6225 : {
6226 61874 : arg2 = convert_like (conv2, arg2, complain);
6227 61874 : arg2 = convert_from_reference (arg2);
6228 61874 : arg2_type = TREE_TYPE (arg2);
6229 : /* Even if CONV2 is a valid conversion, the result of the
6230 : conversion may be invalid. For example, if ARG3 has type
6231 : "volatile X", and X does not have a copy constructor
6232 : accepting a "volatile X&", then even if ARG2 can be
6233 : converted to X, the conversion will fail. */
6234 61874 : if (error_operand_p (arg2))
6235 4 : result = error_mark_node;
6236 4 : converted = true;
6237 : }
6238 196718 : else if (conv3 && !conv3->bad_p)
6239 : {
6240 196025 : arg3 = convert_like (conv3, arg3, complain);
6241 196025 : arg3 = convert_from_reference (arg3);
6242 196025 : arg3_type = TREE_TYPE (arg3);
6243 196025 : if (error_operand_p (arg3))
6244 0 : result = error_mark_node;
6245 0 : converted = true;
6246 : }
6247 :
6248 119 : if (result)
6249 : return result;
6250 :
6251 : /* If, after the conversion, both operands have class type,
6252 : treat the cv-qualification of both operands as if it were the
6253 : union of the cv-qualification of the operands.
6254 :
6255 : The standard is not clear about what to do in this
6256 : circumstance. For example, if the first operand has type
6257 : "const X" and the second operand has a user-defined
6258 : conversion to "volatile X", what is the type of the second
6259 : operand after this step? Making it be "const X" (matching
6260 : the first operand) seems wrong, as that discards the
6261 : qualification without actually performing a copy. Leaving it
6262 : as "volatile X" seems wrong as that will result in the
6263 : conditional expression failing altogether, even though,
6264 : according to this step, the one operand could be converted to
6265 : the type of the other. */
6266 258588 : if (converted
6267 257895 : && CLASS_TYPE_P (arg2_type)
6268 288273 : && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
6269 0 : arg2_type = arg3_type =
6270 0 : cp_build_qualified_type (arg2_type,
6271 0 : cp_type_quals (arg2_type)
6272 0 : | cp_type_quals (arg3_type));
6273 : }
6274 :
6275 : /* [expr.cond]
6276 :
6277 : If the second and third operands are glvalues of the same value
6278 : category and have the same type, the result is of that type and
6279 : value category. */
6280 10862744 : if (((lvalue_p (arg2) && lvalue_p (arg3))
6281 4429857 : || (xvalue_p (arg2) && xvalue_p (arg3)))
6282 10309674 : && same_type_p (arg2_type, arg3_type))
6283 : {
6284 2873029 : result_type = arg2_type;
6285 2873029 : goto valid_operands;
6286 : }
6287 :
6288 : /* [expr.cond]
6289 :
6290 : Otherwise, the result is an rvalue. If the second and third
6291 : operand do not have the same type, and either has (possibly
6292 : cv-qualified) class type, overload resolution is used to
6293 : determine the conversions (if any) to be applied to the operands
6294 : (_over.match.oper_, _over.built_). */
6295 4473139 : is_glvalue = false;
6296 4473139 : if (!same_type_p (arg2_type, arg3_type)
6297 4473139 : && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
6298 : {
6299 693 : releasing_vec args;
6300 693 : conversion *conv;
6301 693 : bool any_viable_p;
6302 :
6303 : /* Rearrange the arguments so that add_builtin_candidate only has
6304 : to know about two args. In build_builtin_candidate, the
6305 : arguments are unscrambled. */
6306 693 : args->quick_push (arg2);
6307 693 : args->quick_push (arg3);
6308 693 : args->quick_push (arg1);
6309 693 : add_builtin_candidates (&candidates,
6310 : COND_EXPR,
6311 : NOP_EXPR,
6312 : ovl_op_identifier (false, COND_EXPR),
6313 : args,
6314 : LOOKUP_NORMAL, complain);
6315 :
6316 : /* [expr.cond]
6317 :
6318 : If the overload resolution fails, the program is
6319 : ill-formed. */
6320 693 : candidates = splice_viable (candidates, false, &any_viable_p);
6321 693 : if (!any_viable_p)
6322 : {
6323 577 : if (complain & tf_error)
6324 22 : error_at (loc, "operands to %<?:%> have different types %qT and %qT",
6325 : arg2_type, arg3_type);
6326 577 : return error_mark_node;
6327 : }
6328 116 : cand = tourney (candidates, complain);
6329 116 : if (!cand)
6330 : {
6331 0 : if (complain & tf_error)
6332 : {
6333 0 : auto_diagnostic_group d;
6334 0 : op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, false);
6335 0 : print_z_candidates (loc, candidates);
6336 0 : }
6337 0 : return error_mark_node;
6338 : }
6339 :
6340 : /* [expr.cond]
6341 :
6342 : Otherwise, the conversions thus determined are applied, and
6343 : the converted operands are used in place of the original
6344 : operands for the remainder of this section. */
6345 116 : conv = cand->convs[0];
6346 116 : arg1 = convert_like (conv, arg1, complain);
6347 116 : conv = cand->convs[1];
6348 116 : arg2 = convert_like (conv, arg2, complain);
6349 116 : arg2_type = TREE_TYPE (arg2);
6350 116 : conv = cand->convs[2];
6351 116 : arg3 = convert_like (conv, arg3, complain);
6352 116 : arg3_type = TREE_TYPE (arg3);
6353 693 : }
6354 :
6355 : /* [expr.cond]
6356 :
6357 : Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
6358 : and function-to-pointer (_conv.func_) standard conversions are
6359 : performed on the second and third operands.
6360 :
6361 : We need to force the lvalue-to-rvalue conversion here for class types,
6362 : so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
6363 : that isn't wrapped with a TARGET_EXPR plays havoc with exception
6364 : regions. */
6365 :
6366 4472562 : arg2 = force_rvalue (arg2, complain);
6367 4472562 : if (!CLASS_TYPE_P (arg2_type))
6368 4366475 : arg2_type = TREE_TYPE (arg2);
6369 :
6370 4472562 : arg3 = force_rvalue (arg3, complain);
6371 4472562 : if (!CLASS_TYPE_P (arg3_type))
6372 4366475 : arg3_type = TREE_TYPE (arg3);
6373 :
6374 4472562 : if (arg2 == error_mark_node || arg3 == error_mark_node)
6375 : return error_mark_node;
6376 :
6377 : /* [expr.cond]
6378 :
6379 : After those conversions, one of the following shall hold:
6380 :
6381 : --The second and third operands have the same type; the result is of
6382 : that type. */
6383 4472514 : if (same_type_p (arg2_type, arg3_type))
6384 : result_type = arg2_type;
6385 : /* [expr.cond]
6386 :
6387 : --The second and third operands have arithmetic or enumeration
6388 : type; the usual arithmetic conversions are performed to bring
6389 : them to a common type, and the result is of that type. */
6390 22877 : else if ((ARITHMETIC_TYPE_P (arg2_type)
6391 22167 : || UNSCOPED_ENUM_P (arg2_type))
6392 967065 : && (ARITHMETIC_TYPE_P (arg3_type)
6393 183 : || UNSCOPED_ENUM_P (arg3_type)))
6394 : {
6395 : /* A conditional expression between a floating-point
6396 : type and an integer type should convert the integer type to
6397 : the evaluation format of the floating-point type, with
6398 : possible excess precision. */
6399 944074 : tree eptype2 = arg2_type;
6400 944074 : tree eptype3 = arg3_type;
6401 944074 : tree eptype;
6402 710 : if (ANY_INTEGRAL_TYPE_P (arg2_type)
6403 944077 : && (eptype = excess_precision_type (arg3_type)) != NULL_TREE)
6404 : {
6405 5 : eptype3 = eptype;
6406 5 : if (!semantic_result_type)
6407 5 : semantic_result_type
6408 5 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
6409 : }
6410 402 : else if (ANY_INTEGRAL_TYPE_P (arg3_type)
6411 944069 : && (eptype = excess_precision_type (arg2_type)) != NULL_TREE)
6412 : {
6413 23 : eptype2 = eptype;
6414 23 : if (!semantic_result_type)
6415 23 : semantic_result_type
6416 23 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
6417 : }
6418 944074 : result_type = type_after_usual_arithmetic_conversions (eptype2,
6419 : eptype3);
6420 944074 : if (result_type == error_mark_node)
6421 : {
6422 0 : tree t1 = eptype2;
6423 0 : tree t2 = eptype3;
6424 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6425 0 : t1 = TREE_TYPE (t1);
6426 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6427 0 : t2 = TREE_TYPE (t2);
6428 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
6429 : && SCALAR_FLOAT_TYPE_P (t2)
6430 : && (extended_float_type_p (t1)
6431 : || extended_float_type_p (t2))
6432 : && cp_compare_floating_point_conversion_ranks
6433 : (t1, t2) == 3);
6434 0 : if (complain & tf_error)
6435 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
6436 : "have unordered conversion rank",
6437 : eptype2, eptype3);
6438 0 : return error_mark_node;
6439 : }
6440 944074 : if (semantic_result_type == error_mark_node)
6441 : {
6442 0 : tree t1 = arg2_type;
6443 0 : tree t2 = arg3_type;
6444 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6445 0 : t1 = TREE_TYPE (t1);
6446 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6447 0 : t2 = TREE_TYPE (t2);
6448 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
6449 : && SCALAR_FLOAT_TYPE_P (t2)
6450 : && (extended_float_type_p (t1)
6451 : || extended_float_type_p (t2))
6452 : && cp_compare_floating_point_conversion_ranks
6453 : (t1, t2) == 3);
6454 0 : if (complain & tf_error)
6455 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
6456 : "have unordered conversion rank",
6457 : arg2_type, arg3_type);
6458 0 : return error_mark_node;
6459 : }
6460 :
6461 944074 : if (complain & tf_warning)
6462 893316 : do_warn_double_promotion (result_type, arg2_type, arg3_type,
6463 : "implicit conversion from %qH to %qI to "
6464 : "match other result of conditional",
6465 : loc);
6466 :
6467 944074 : if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
6468 98 : && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
6469 : {
6470 34 : tree stripped_orig_arg2 = tree_strip_any_location_wrapper (orig_arg2);
6471 34 : tree stripped_orig_arg3 = tree_strip_any_location_wrapper (orig_arg3);
6472 34 : if (TREE_CODE (stripped_orig_arg2) == CONST_DECL
6473 23 : && TREE_CODE (stripped_orig_arg3) == CONST_DECL
6474 57 : && (DECL_CONTEXT (stripped_orig_arg2)
6475 23 : == DECL_CONTEXT (stripped_orig_arg3)))
6476 : /* Two enumerators from the same enumeration can have different
6477 : types when the enumeration is still being defined. */;
6478 28 : else if (complain & (cxx_dialect >= cxx26
6479 28 : ? tf_warning_or_error : tf_warning))
6480 43 : emit_diagnostic ((cxx_dialect >= cxx26
6481 : ? diagnostics::kind::pedwarn
6482 : : diagnostics::kind::warning),
6483 25 : loc, OPT_Wenum_compare, "enumerated mismatch "
6484 : "in conditional expression: %qT vs %qT",
6485 : arg2_type, arg3_type);
6486 3 : else if (cxx_dialect >= cxx26)
6487 1 : return error_mark_node;
6488 : }
6489 944040 : else if ((((complain & (cxx_dialect >= cxx26
6490 944040 : ? tf_warning_or_error : tf_warning))
6491 896201 : && warn_deprecated_enum_float_conv)
6492 63233 : || (cxx_dialect >= cxx26
6493 1335 : && (complain & tf_warning_or_error) == 0))
6494 882043 : && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
6495 36 : && SCALAR_FLOAT_TYPE_P (arg3_type))
6496 882032 : || (SCALAR_FLOAT_TYPE_P (arg2_type)
6497 232 : && TREE_CODE (arg3_type) == ENUMERAL_TYPE)))
6498 : {
6499 19 : if (cxx_dialect >= cxx26 && (complain & tf_warning_or_error) == 0)
6500 2 : return error_mark_node;
6501 17 : if (cxx_dialect >= cxx26 && TREE_CODE (arg2_type) == ENUMERAL_TYPE)
6502 4 : pedwarn (loc, OPT_Wdeprecated_enum_float_conversion,
6503 : "conditional expression between enumeration type "
6504 : "%qT and floating-point type %qT", arg2_type, arg3_type);
6505 13 : else if (cxx_dialect >= cxx26)
6506 3 : pedwarn (loc, OPT_Wdeprecated_enum_float_conversion,
6507 : "conditional expression between floating-point type "
6508 : "%qT and enumeration type %qT", arg2_type, arg3_type);
6509 10 : else if (TREE_CODE (arg2_type) == ENUMERAL_TYPE)
6510 6 : warning_at (loc, OPT_Wdeprecated_enum_float_conversion,
6511 : "conditional expression between enumeration type "
6512 : "%qT and floating-point type %qT is deprecated",
6513 : arg2_type, arg3_type);
6514 : else
6515 4 : warning_at (loc, OPT_Wdeprecated_enum_float_conversion,
6516 : "conditional expression between floating-point "
6517 : "type %qT and enumeration type %qT is deprecated",
6518 : arg2_type, arg3_type);
6519 : }
6520 934295 : else if ((extra_warnings || warn_enum_conversion)
6521 944026 : && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
6522 15 : && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
6523 9719 : || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
6524 8 : && !same_type_p (arg2_type,
6525 : type_promotes_to (arg3_type)))))
6526 : {
6527 17 : if (complain & tf_warning)
6528 : {
6529 12 : enum opt_code opt = (warn_enum_conversion
6530 17 : ? OPT_Wenum_conversion
6531 : : OPT_Wextra);
6532 17 : warning_at (loc, opt, "enumerated and "
6533 : "non-enumerated type in conditional expression");
6534 : }
6535 : }
6536 :
6537 944071 : arg2 = perform_implicit_conversion (result_type, arg2, complain);
6538 944071 : arg3 = perform_implicit_conversion (result_type, arg3, complain);
6539 : }
6540 : /* [expr.cond]
6541 :
6542 : --The second and third operands have pointer type, or one has
6543 : pointer type and the other is a null pointer constant; pointer
6544 : conversions (_conv.ptr_) and qualification conversions
6545 : (_conv.qual_) are performed to bring them to their composite
6546 : pointer type (_expr.rel_). The result is of the composite
6547 : pointer type.
6548 :
6549 : --The second and third operands have pointer to member type, or
6550 : one has pointer to member type and the other is a null pointer
6551 : constant; pointer to member conversions (_conv.mem_) and
6552 : qualification conversions (_conv.qual_) are performed to bring
6553 : them to a common type, whose cv-qualification shall match the
6554 : cv-qualification of either the second or the third operand.
6555 : The result is of the common type. */
6556 22183 : else if ((null_ptr_cst_p (arg2)
6557 163 : && TYPE_PTR_OR_PTRMEM_P (arg3_type))
6558 22020 : || (null_ptr_cst_p (arg3)
6559 21075 : && TYPE_PTR_OR_PTRMEM_P (arg2_type))
6560 945 : || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
6561 89 : || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type))
6562 22255 : || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
6563 : {
6564 22129 : result_type = composite_pointer_type (loc,
6565 : arg2_type, arg3_type, arg2,
6566 : arg3, CPO_CONDITIONAL_EXPR,
6567 : complain);
6568 22129 : if (result_type == error_mark_node)
6569 : return error_mark_node;
6570 22103 : arg2 = perform_implicit_conversion (result_type, arg2, complain);
6571 22103 : arg3 = perform_implicit_conversion (result_type, arg3, complain);
6572 : }
6573 :
6574 4472431 : if (!result_type)
6575 : {
6576 54 : if (complain & tf_error)
6577 3 : error_at (loc, "operands to %<?:%> have different types %qT and %qT",
6578 : arg2_type, arg3_type);
6579 54 : return error_mark_node;
6580 : }
6581 :
6582 4472431 : if (arg2 == error_mark_node || arg3 == error_mark_node)
6583 : return error_mark_node;
6584 :
6585 4472428 : valid_operands:
6586 7461076 : if (processing_template_decl && is_glvalue)
6587 : {
6588 : /* Let lvalue_kind know this was a glvalue. */
6589 109937 : tree arg = (result_type == arg2_type ? arg2 : arg3);
6590 109937 : result_type = cp_build_reference_type (result_type, xvalue_p (arg));
6591 : }
6592 :
6593 7461076 : result = build3_loc (loc, COND_EXPR, result_type, arg1, arg2, arg3);
6594 :
6595 : /* If the ARG2 and ARG3 are the same and don't have side-effects,
6596 : warn here, because the COND_EXPR will be turned into ARG2. */
6597 7461076 : if (warn_duplicated_branches
6598 153 : && (complain & tf_warning)
6599 7461229 : && (arg2 == arg3 || operand_equal_p (arg2, arg3,
6600 : OEP_ADDRESS_OF_SAME_FIELD)))
6601 57 : warning_at (EXPR_LOCATION (result), OPT_Wduplicated_branches,
6602 : "this condition has identical branches");
6603 :
6604 : /* We can't use result_type below, as fold might have returned a
6605 : throw_expr. */
6606 :
6607 7461076 : if (!is_glvalue)
6608 : {
6609 : /* Expand both sides into the same slot, hopefully the target of
6610 : the ?: expression. We used to check for TARGET_EXPRs here,
6611 : but now we sometimes wrap them in NOP_EXPRs so the test would
6612 : fail. */
6613 4587987 : if (CLASS_TYPE_P (TREE_TYPE (result)))
6614 : {
6615 106124 : result = get_target_expr (result, complain);
6616 : /* Tell gimplify_modify_expr_rhs not to strip this in
6617 : assignment context: we want both arms to initialize
6618 : the same temporary. */
6619 106124 : TARGET_EXPR_NO_ELIDE (result) = true;
6620 : }
6621 : /* If this expression is an rvalue, but might be mistaken for an
6622 : lvalue, we must add a NON_LVALUE_EXPR. */
6623 4587987 : result = rvalue (result);
6624 4587987 : if (semantic_result_type)
6625 73 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type,
6626 : result);
6627 : }
6628 : else
6629 : {
6630 2873089 : result = force_paren_expr (result);
6631 2873089 : gcc_assert (semantic_result_type == NULL_TREE);
6632 : }
6633 :
6634 : return result;
6635 7463575 : }
6636 :
6637 : /* OPERAND is an operand to an expression. Perform necessary steps
6638 : required before using it. If OPERAND is NULL_TREE, NULL_TREE is
6639 : returned. */
6640 :
6641 : static tree
6642 616672976 : prep_operand (tree operand)
6643 : {
6644 616672976 : if (operand)
6645 : {
6646 712328110 : if (CLASS_TYPE_P (TREE_TYPE (operand))
6647 381156129 : && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
6648 : /* Make sure the template type is instantiated now. */
6649 10020591 : instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
6650 : }
6651 :
6652 616672976 : return operand;
6653 : }
6654 :
6655 : /* True iff CONV represents a conversion sequence which no other can be better
6656 : than under [over.ics.rank]: in other words, a "conversion" to the exact same
6657 : type (including binding to a reference to the same type). This is stronger
6658 : than the standard's "identity" category, which also includes reference
6659 : bindings that add cv-qualifiers or change rvalueness. */
6660 :
6661 : static bool
6662 217976144 : perfect_conversion_p (conversion *conv)
6663 : {
6664 217976144 : if (CONVERSION_RANK (conv) != cr_identity)
6665 : return false;
6666 156477831 : if (conv->kind == ck_ref_bind)
6667 : {
6668 40702683 : if (!conv->rvaluedness_matches_p)
6669 : return false;
6670 28125345 : if (!same_type_p (TREE_TYPE (conv->type),
6671 : next_conversion (conv)->type))
6672 : return false;
6673 : }
6674 140282374 : if (conv->check_narrowing)
6675 : /* Brace elision is imperfect. */
6676 : return false;
6677 : return true;
6678 : }
6679 :
6680 : /* True if CAND represents a perfect match, i.e. all perfect conversions, so no
6681 : other candidate can be a better match. Since the template/non-template
6682 : tiebreaker comes immediately after the conversion comparison in
6683 : [over.match.best], a perfect non-template candidate is better than all
6684 : templates. */
6685 :
6686 : static bool
6687 512679586 : perfect_candidate_p (z_candidate *cand)
6688 : {
6689 512679586 : if (cand->viable < 1)
6690 : return false;
6691 : /* CWG1402 makes an implicitly deleted move op worse than other
6692 : candidates. */
6693 201588049 : if (DECL_DELETED_FN (cand->fn) && DECL_DEFAULTED_FN (cand->fn)
6694 200637885 : && move_fn_p (cand->fn))
6695 : return false;
6696 199046587 : int len = cand->num_convs;
6697 339328914 : for (int i = 0; i < len; ++i)
6698 217976144 : if (!perfect_conversion_p (cand->convs[i]))
6699 : return false;
6700 121352770 : if (conversion *conv = cand->second_conv)
6701 0 : if (!perfect_conversion_p (conv))
6702 : return false;
6703 : return true;
6704 : }
6705 :
6706 : /* True iff one of CAND's argument conversions is missing. */
6707 :
6708 : static bool
6709 20903071 : missing_conversion_p (const z_candidate *cand)
6710 : {
6711 37762264 : for (unsigned i = 0; i < cand->num_convs; ++i)
6712 : {
6713 25143204 : conversion *conv = cand->convs[i];
6714 25143204 : if (!conv)
6715 : return true;
6716 24248867 : if (conv->kind == ck_deferred_bad)
6717 : {
6718 : /* We don't know whether this conversion is outright invalid or
6719 : just bad, so conservatively assume it's missing. */
6720 7389674 : gcc_checking_assert (conv->bad_p);
6721 : return true;
6722 : }
6723 : }
6724 : return false;
6725 : }
6726 :
6727 : /* Add each of the viable functions in FNS (a FUNCTION_DECL or
6728 : OVERLOAD) to the CANDIDATES, returning an updated list of
6729 : CANDIDATES. The ARGS are the arguments provided to the call;
6730 : if FIRST_ARG is non-null it is the implicit object argument,
6731 : otherwise the first element of ARGS is used if needed. The
6732 : EXPLICIT_TARGS are explicit template arguments provided.
6733 : TEMPLATE_ONLY is true if only template functions should be
6734 : considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
6735 : add_function_candidate. */
6736 :
6737 : static void
6738 289973957 : add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args,
6739 : tree return_type,
6740 : tree explicit_targs, bool template_only,
6741 : tree conversion_path, tree access_path,
6742 : int flags,
6743 : struct z_candidate **candidates,
6744 : tsubst_flags_t complain)
6745 : {
6746 289973957 : tree ctype;
6747 289973957 : const vec<tree, va_gc> *non_static_args;
6748 289973957 : bool check_list_ctor = false;
6749 289973957 : bool check_converting = false;
6750 289973957 : unification_kind_t strict;
6751 289973957 : tree ne_context = NULL_TREE;
6752 289973957 : tree ne_fns = NULL_TREE;
6753 :
6754 289973957 : if (!fns)
6755 3314666 : return;
6756 :
6757 : /* Precalculate special handling of constructors and conversion ops. */
6758 286659291 : tree fn = OVL_FIRST (fns);
6759 286659291 : if (DECL_CONV_FN_P (fn))
6760 : {
6761 17275232 : check_list_ctor = false;
6762 17275232 : check_converting = (flags & LOOKUP_ONLYCONVERTING) != 0;
6763 17275232 : if (flags & LOOKUP_NO_CONVERSION)
6764 : /* We're doing return_type(x). */
6765 : strict = DEDUCE_CONV;
6766 : else
6767 : /* We're doing x.operator return_type(). */
6768 1370 : strict = DEDUCE_EXACT;
6769 : /* [over.match.funcs] For conversion functions, the function
6770 : is considered to be a member of the class of the implicit
6771 : object argument for the purpose of defining the type of
6772 : the implicit object parameter. */
6773 17275232 : ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg));
6774 : }
6775 : else
6776 : {
6777 538768118 : if (DECL_CONSTRUCTOR_P (fn))
6778 : {
6779 74370087 : check_list_ctor = (flags & LOOKUP_LIST_ONLY) != 0;
6780 : /* For list-initialization we consider explicit constructors
6781 : and complain if one is chosen. */
6782 74370087 : check_converting
6783 74370087 : = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
6784 : == LOOKUP_ONLYCONVERTING);
6785 : }
6786 269384059 : strict = DEDUCE_CALL;
6787 423776170 : ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
6788 : }
6789 :
6790 : /* P2468: Check if operator== is a rewrite target with first operand
6791 : (*args)[0]; for now just do the lookups. */
6792 286659291 : if ((flags & (LOOKUP_REWRITTEN | LOOKUP_REVERSED))
6793 286659291 : && DECL_OVERLOADED_OPERATOR_IS (fn, EQ_EXPR))
6794 : {
6795 6552272 : tree ne_name = ovl_op_identifier (false, NE_EXPR);
6796 6552272 : if (DECL_CLASS_SCOPE_P (fn))
6797 : {
6798 178969 : ne_context = DECL_CONTEXT (fn);
6799 178969 : ne_fns = lookup_fnfields (TREE_TYPE ((*args)[0]), ne_name,
6800 : 1, tf_none);
6801 178969 : if (ne_fns == error_mark_node || ne_fns == NULL_TREE)
6802 : ne_fns = NULL_TREE;
6803 : else
6804 18209 : ne_fns = BASELINK_FUNCTIONS (ne_fns);
6805 : }
6806 : else
6807 : {
6808 6373303 : ne_context = decl_namespace_context (fn);
6809 6373303 : ne_fns = lookup_qualified_name (ne_context, ne_name,
6810 : LOOK_want::NORMAL,
6811 : /*complain*/false);
6812 6373303 : if (ne_fns == error_mark_node
6813 2089312 : || !is_overloaded_fn (ne_fns))
6814 4283991 : ne_fns = NULL_TREE;
6815 : }
6816 : }
6817 :
6818 286659291 : if (first_arg)
6819 : non_static_args = args;
6820 : else
6821 : /* Delay creating the implicit this parameter until it is needed. */
6822 123101791 : non_static_args = NULL;
6823 :
6824 286659291 : bool seen_strictly_viable = any_strictly_viable (*candidates);
6825 : /* If there's a non-template perfect match, we don't need to consider
6826 : templates. So check non-templates first. This optimization is only
6827 : really needed for the defaulted copy constructor of tuple and the like
6828 : (96926), but it seems like we might as well enable it more generally. */
6829 286659291 : bool seen_perfect = false;
6830 286659291 : enum { templates, non_templates, either } which = either;
6831 286659291 : if (template_only)
6832 : which = templates;
6833 : else /*if (flags & LOOKUP_DEFAULTED)*/
6834 254016361 : which = non_templates;
6835 :
6836 : /* Template candidates that we'll potentially ignore if the
6837 : perfect candidate optimization succeeds. */
6838 286659291 : z_candidate *ignored_template_cands = nullptr;
6839 :
6840 : /* During overload resolution, we first consider each function under the
6841 : assumption that we'll eventually find a strictly viable candidate.
6842 : This allows us to circumvent our defacto behavior when checking
6843 : argument conversions and shortcut consideration of the candidate
6844 : upon encountering the first bad conversion. If this assumption
6845 : turns out to be false, and all candidates end up being non-strictly
6846 : viable, then we reconsider such candidates under the defacto behavior.
6847 : This trick is important for pruning member function overloads according
6848 : to their const/ref-qualifiers (since all 'this' conversions are at
6849 : worst bad) without breaking -fpermissive. */
6850 286659291 : z_candidate *bad_cands = nullptr;
6851 286659291 : bool shortcut_bad_convs = true;
6852 :
6853 419437821 : again:
6854 2459084014 : for (tree fn : lkp_range (fns))
6855 : {
6856 2039659744 : if (which == templates && TREE_CODE (fn) != TEMPLATE_DECL)
6857 : {
6858 299035426 : if (template_only)
6859 769379 : add_ignored_candidate (candidates, fn);
6860 299035426 : continue;
6861 : }
6862 1740624318 : if (which == non_templates && TREE_CODE (fn) == TEMPLATE_DECL)
6863 : {
6864 602380617 : add_ignored_candidate (&ignored_template_cands, fn);
6865 602380617 : continue;
6866 : }
6867 219406240 : if ((check_converting && DECL_NONCONVERTING_P (fn))
6868 1333600039 : || (check_list_ctor && !is_list_ctor (fn)))
6869 : {
6870 25288690 : add_ignored_candidate (candidates, fn);
6871 25288690 : continue;
6872 : }
6873 :
6874 1112955011 : tree fn_first_arg = NULL_TREE;
6875 1112955011 : const vec<tree, va_gc> *fn_args = args;
6876 :
6877 1112955011 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fn))
6878 : {
6879 : /* Figure out where the object arg comes from. If this
6880 : function is a non-static member and we didn't get an
6881 : implicit object argument, move it out of args. */
6882 405956371 : if (first_arg == NULL_TREE)
6883 : {
6884 8109843 : unsigned int ix;
6885 8109843 : tree arg;
6886 8109843 : vec<tree, va_gc> *tempvec;
6887 8109843 : vec_alloc (tempvec, args->length () - 1);
6888 21918612 : for (ix = 1; args->iterate (ix, &arg); ++ix)
6889 5698926 : tempvec->quick_push (arg);
6890 8109843 : non_static_args = tempvec;
6891 8109843 : first_arg = (*args)[0];
6892 : }
6893 :
6894 : fn_first_arg = first_arg;
6895 : fn_args = non_static_args;
6896 : }
6897 :
6898 : /* Don't bother reversing an operator with two identical parameters. */
6899 706998640 : else if (vec_safe_length (args) == 2 && (flags & LOOKUP_REVERSED))
6900 : {
6901 198265435 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
6902 198265435 : if (same_type_p (TREE_VALUE (parmlist),
6903 : TREE_VALUE (TREE_CHAIN (parmlist))))
6904 94161096 : continue;
6905 : }
6906 :
6907 : /* When considering reversed operator==, if there's a corresponding
6908 : operator!= in the same scope, it's not a rewrite target. */
6909 1018793915 : if (ne_context)
6910 : {
6911 141627861 : if (TREE_CODE (ne_context) == NAMESPACE_DECL)
6912 : {
6913 : /* With argument-dependent lookup, fns can span multiple
6914 : namespaces; make sure we look in the fn's namespace for a
6915 : corresponding operator!=. */
6916 141444841 : tree fn_ns = decl_namespace_context (fn);
6917 141444841 : if (fn_ns != ne_context)
6918 : {
6919 7684435 : ne_context = fn_ns;
6920 7684435 : tree ne_name = ovl_op_identifier (false, NE_EXPR);
6921 7684435 : ne_fns = lookup_qualified_name (ne_context, ne_name,
6922 : LOOK_want::NORMAL,
6923 : /*complain*/false);
6924 7684435 : if (ne_fns == error_mark_node
6925 4919051 : || !is_overloaded_fn (ne_fns))
6926 2765384 : ne_fns = NULL_TREE;
6927 : }
6928 : }
6929 141627861 : bool found = false;
6930 1526616867 : for (lkp_iterator ne (ne_fns); !found && ne; ++ne)
6931 1418794821 : if (0 && !ne.using_p ()
6932 : && DECL_NAMESPACE_SCOPE_P (fn)
6933 : && DECL_CONTEXT (*ne) != DECL_CONTEXT (fn))
6934 : /* ??? This kludge excludes inline namespace members for the H
6935 : test in spaceship-eq15.C, but I don't see why we would want
6936 : that behavior. Asked Core 2022-11-04. Disabling for now. */;
6937 1418794821 : else if (fns_correspond (fn, *ne))
6938 : {
6939 : found = true;
6940 : break;
6941 : }
6942 141627861 : if (found)
6943 33805815 : continue;
6944 : }
6945 :
6946 : /* Do not resolve any non-default function. Only the default version
6947 : is resolvable (for the target_version attribute semantics.) */
6948 984988100 : if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
6949 : && TREE_CODE (fn) == FUNCTION_DECL
6950 : && !is_function_default_version (fn))
6951 : {
6952 : add_ignored_candidate (candidates, fn);
6953 : continue;
6954 : }
6955 :
6956 984988100 : if (TREE_CODE (fn) == TEMPLATE_DECL)
6957 472308514 : add_template_candidate (candidates,
6958 : fn,
6959 : ctype,
6960 : explicit_targs,
6961 : fn_first_arg,
6962 : fn_args,
6963 : return_type,
6964 : access_path,
6965 : conversion_path,
6966 : flags,
6967 : strict,
6968 : shortcut_bad_convs,
6969 : complain);
6970 : else
6971 : {
6972 512679586 : add_function_candidate (candidates,
6973 : fn,
6974 : ctype,
6975 : fn_first_arg,
6976 : fn_args,
6977 : access_path,
6978 : conversion_path,
6979 : flags,
6980 : NULL,
6981 : shortcut_bad_convs,
6982 : complain);
6983 512679586 : if (perfect_candidate_p (*candidates))
6984 984974549 : seen_perfect = true;
6985 : }
6986 :
6987 984974549 : z_candidate *cand = *candidates;
6988 984974549 : if (cand->viable == 1)
6989 273110079 : seen_strictly_viable = true;
6990 :
6991 984974549 : if (cand->viable == -1
6992 20903564 : && shortcut_bad_convs
6993 1005877620 : && (missing_conversion_p (cand)
6994 12619060 : || TREE_CODE (cand->fn) == TEMPLATE_DECL))
6995 : {
6996 : /* This candidate has been tentatively marked non-strictly viable,
6997 : and we didn't compute all argument conversions for it (having
6998 : stopped at the first bad conversion). Move it to BAD_CANDS to
6999 : to fully reconsider later if we don't find any strictly viable
7000 : candidates. */
7001 8309667 : if (complain & (tf_error | tf_conv))
7002 : {
7003 8018313 : *candidates = cand->next;
7004 8018313 : cand->next = bad_cands;
7005 8018313 : bad_cands = cand;
7006 : }
7007 : else
7008 : /* But if we're in a SFINAE context, just mark this candidate as
7009 : unviable outright and avoid potentially reconsidering it.
7010 : This is safe to do because in a SFINAE context, performing a bad
7011 : conversion is always an error (even with -fpermissive), so a
7012 : non-strictly viable candidate is effectively unviable anyway. */
7013 291354 : cand->viable = 0;
7014 : }
7015 : }
7016 419424270 : if (which == non_templates && !seen_perfect)
7017 : {
7018 132668727 : which = templates;
7019 132668727 : ignored_template_cands = nullptr;
7020 132668727 : goto again;
7021 : }
7022 286755543 : else if (which == templates
7023 286755543 : && !seen_strictly_viable
7024 : && shortcut_bad_convs
7025 54282472 : && bad_cands)
7026 : {
7027 : /* None of the candidates are strictly viable, so consider again those
7028 : functions in BAD_CANDS, this time without shortcutting bad conversions
7029 : so that all their argument conversions are computed. */
7030 243665 : which = either;
7031 : fns = NULL_TREE;
7032 243665 : for (z_candidate *cand = bad_cands; cand; cand = cand->next)
7033 : {
7034 133862 : tree fn = cand->fn;
7035 133862 : if (tree ti = cand->template_decl)
7036 117 : fn = TI_TEMPLATE (ti);
7037 133862 : fns = ovl_make (fn, fns);
7038 : }
7039 109803 : shortcut_bad_convs = false;
7040 109803 : bad_cands = nullptr;
7041 109803 : goto again;
7042 : }
7043 :
7044 286645740 : if (complain & tf_error)
7045 : {
7046 : /* Remember any omitted candidates; we may want to print all candidates
7047 : as part of overload resolution failure diagnostics. */
7048 451326921 : for (z_candidate *omitted_cands : { ignored_template_cands, bad_cands })
7049 : {
7050 300884614 : z_candidate **omitted_cands_tail = &omitted_cands;
7051 346401485 : while (*omitted_cands_tail)
7052 45516871 : omitted_cands_tail = &(*omitted_cands_tail)->next;
7053 300884614 : *omitted_cands_tail = *candidates;
7054 300884614 : *candidates = omitted_cands;
7055 : }
7056 : }
7057 : }
7058 :
7059 : /* Returns 1 if P0145R2 says that the LHS of operator CODE is evaluated first,
7060 : -1 if the RHS is evaluated first, or 0 if the order is unspecified. */
7061 :
7062 : static int
7063 15802123 : op_is_ordered (tree_code code)
7064 : {
7065 15801719 : switch (code)
7066 : {
7067 : // 5. b @= a
7068 3609632 : case MODIFY_EXPR:
7069 3609632 : return (flag_strong_eval_order > 1 ? -1 : 0);
7070 :
7071 : // 6. a[b]
7072 452273 : case ARRAY_REF:
7073 451869 : return (flag_strong_eval_order > 1 ? 1 : 0);
7074 :
7075 : // 1. a.b
7076 : // Not overloadable (yet).
7077 : // 2. a->b
7078 : // Only one argument.
7079 : // 3. a->*b
7080 56860 : case MEMBER_REF:
7081 : // 7. a << b
7082 56860 : case LSHIFT_EXPR:
7083 : // 8. a >> b
7084 56860 : case RSHIFT_EXPR:
7085 : // a && b
7086 : // Predates P0145R3.
7087 56860 : case TRUTH_ANDIF_EXPR:
7088 : // a || b
7089 : // Predates P0145R3.
7090 56860 : case TRUTH_ORIF_EXPR:
7091 : // a , b
7092 : // Predates P0145R3.
7093 56860 : case COMPOUND_EXPR:
7094 56860 : return (flag_strong_eval_order ? 1 : 0);
7095 :
7096 : default:
7097 : return 0;
7098 : }
7099 : }
7100 :
7101 : /* Subroutine of build_new_op: Add to CANDIDATES all candidates for the
7102 : operator indicated by CODE/CODE2. This function calls itself recursively to
7103 : handle C++20 rewritten comparison operator candidates. Returns NULL_TREE
7104 : upon success, and error_mark_node if something went wrong that prevented
7105 : us from performing overload resolution (e.g. ambiguous member name lookup).
7106 :
7107 : LOOKUPS, if non-NULL, is the set of pertinent namespace-scope operator
7108 : overloads to consider. This parameter is used when instantiating a
7109 : dependent operator expression and has the same structure as
7110 : DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS. */
7111 :
7112 : static tree
7113 32880438 : add_operator_candidates (z_candidate **candidates,
7114 : tree_code code, tree_code code2,
7115 : vec<tree, va_gc> *arglist, tree lookups,
7116 : int flags, tsubst_flags_t complain)
7117 : {
7118 32880438 : z_candidate *start_candidates = *candidates;
7119 32880438 : bool ismodop = code2 != ERROR_MARK;
7120 32880438 : tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
7121 :
7122 : /* LOOKUP_REWRITTEN is set when we're looking for the == or <=> operator to
7123 : rewrite from, and also when we're looking for the e.g. < operator to use
7124 : on the result of <=>. In the latter case, we don't want the flag set in
7125 : the candidate, we just want to suppress looking for rewrites. */
7126 32880438 : bool rewritten = (flags & LOOKUP_REWRITTEN);
7127 32880438 : if (rewritten && code != EQ_EXPR && code != SPACESHIP_EXPR)
7128 861777 : flags &= ~LOOKUP_REWRITTEN;
7129 :
7130 30545600 : bool memonly = false;
7131 30545600 : switch (code)
7132 : {
7133 : /* =, ->, [], () must be non-static member functions. */
7134 6170128 : case MODIFY_EXPR:
7135 6170128 : if (code2 != NOP_EXPR)
7136 : break;
7137 : /* FALLTHRU */
7138 : case COMPONENT_REF:
7139 : case ARRAY_REF:
7140 : memonly = true;
7141 : break;
7142 :
7143 : default:
7144 : break;
7145 : }
7146 :
7147 : /* Add namespace-scope operators to the list of functions to
7148 : consider. */
7149 : if (!memonly)
7150 : {
7151 28817845 : tree fns;
7152 28817845 : if (!lookups)
7153 21612482 : fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE);
7154 : /* If LOOKUPS is non-NULL, then we're instantiating a dependent operator
7155 : expression, and LOOKUPS is the result of stage 1 name lookup. */
7156 7205363 : else if (tree found = purpose_member (fnname, lookups))
7157 1520159 : fns = TREE_VALUE (found);
7158 : else
7159 : fns = NULL_TREE;
7160 28817845 : fns = lookup_arg_dependent (fnname, fns, arglist);
7161 28817845 : add_candidates (fns, NULL_TREE, arglist, NULL_TREE,
7162 : NULL_TREE, false, NULL_TREE, NULL_TREE,
7163 : flags, candidates, complain);
7164 : }
7165 :
7166 : /* Add class-member operators to the candidate set. */
7167 32880405 : tree arg1_type = TREE_TYPE ((*arglist)[0]);
7168 32880405 : unsigned nargs = arglist->length () > 1 ? 2 : 1;
7169 28045092 : tree arg2_type = nargs > 1 ? TREE_TYPE ((*arglist)[1]) : NULL_TREE;
7170 32880405 : if (CLASS_TYPE_P (arg1_type))
7171 : {
7172 19014822 : tree fns = lookup_fnfields (arg1_type, fnname, 1, complain);
7173 19014822 : if (fns == error_mark_node)
7174 : return error_mark_node;
7175 19014810 : if (fns)
7176 : {
7177 8564063 : if (code == ARRAY_REF)
7178 : {
7179 454220 : vec<tree,va_gc> *restlist = make_tree_vector ();
7180 908440 : for (unsigned i = 1; i < nargs; ++i)
7181 454220 : vec_safe_push (restlist, (*arglist)[i]);
7182 454220 : z_candidate *save_cand = *candidates;
7183 908440 : add_candidates (BASELINK_FUNCTIONS (fns),
7184 454220 : (*arglist)[0], restlist, NULL_TREE,
7185 : NULL_TREE, false,
7186 454220 : BASELINK_BINFO (fns),
7187 454220 : BASELINK_ACCESS_BINFO (fns),
7188 : flags, candidates, complain);
7189 : /* Release the vec if we didn't add a candidate that uses it. */
7190 454816 : for (z_candidate *c = *candidates; c != save_cand; c = c->next)
7191 454816 : if (c->args == restlist)
7192 : {
7193 454220 : restlist = NULL;
7194 454220 : break;
7195 : }
7196 454220 : release_tree_vector (restlist);
7197 : }
7198 : else
7199 8109843 : add_candidates (BASELINK_FUNCTIONS (fns),
7200 : NULL_TREE, arglist, NULL_TREE,
7201 : NULL_TREE, false,
7202 8109843 : BASELINK_BINFO (fns),
7203 8109843 : BASELINK_ACCESS_BINFO (fns),
7204 : flags, candidates, complain);
7205 : }
7206 : }
7207 : /* Per [over.match.oper]3.2, if no operand has a class type, then
7208 : only non-member functions that have type T1 or reference to
7209 : cv-qualified-opt T1 for the first argument, if the first argument
7210 : has an enumeration type, or T2 or reference to cv-qualified-opt
7211 : T2 for the second argument, if the second argument has an
7212 : enumeration type. Filter out those that don't match. */
7213 13865583 : else if (! arg2_type || ! CLASS_TYPE_P (arg2_type))
7214 : {
7215 : struct z_candidate **candp, **next;
7216 :
7217 227015004 : for (candp = candidates; *candp != start_candidates; candp = next)
7218 : {
7219 213700127 : unsigned i;
7220 213700127 : z_candidate *cand = *candp;
7221 213700127 : next = &cand->next;
7222 :
7223 213700127 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
7224 :
7225 630364609 : for (i = 0; i < nargs; ++i)
7226 : {
7227 421642479 : tree parmtype = TREE_VALUE (parmlist);
7228 421642479 : tree argtype = unlowered_expr_type ((*arglist)[i]);
7229 :
7230 421642479 : if (TYPE_REF_P (parmtype))
7231 333244072 : parmtype = TREE_TYPE (parmtype);
7232 421642479 : if (TREE_CODE (argtype) == ENUMERAL_TYPE
7233 823185596 : && (same_type_ignoring_top_level_qualifiers_p
7234 401543117 : (argtype, parmtype)))
7235 : break;
7236 :
7237 416664482 : parmlist = TREE_CHAIN (parmlist);
7238 : }
7239 :
7240 : /* No argument has an appropriate type, so remove this
7241 : candidate function from the list. */
7242 213700127 : if (i == nargs)
7243 : {
7244 208722130 : *candp = cand->next;
7245 208722130 : next = candp;
7246 : }
7247 : }
7248 : }
7249 :
7250 32880393 : if (!rewritten)
7251 : {
7252 : /* The standard says to rewrite built-in candidates, too,
7253 : but there's no point. */
7254 23277063 : add_builtin_candidates (candidates, code, code2, fnname, arglist,
7255 : flags, complain);
7256 :
7257 : /* Maybe add C++20 rewritten comparison candidates. */
7258 23277063 : tree_code rewrite_code = ERROR_MARK;
7259 23277063 : if (cxx_dialect >= cxx20
7260 22978135 : && nargs == 2
7261 41541937 : && (OVERLOAD_TYPE_P (arg1_type) || OVERLOAD_TYPE_P (arg2_type)))
7262 18257254 : switch (code)
7263 : {
7264 1228134 : case LT_EXPR:
7265 1228134 : case LE_EXPR:
7266 1228134 : case GT_EXPR:
7267 1228134 : case GE_EXPR:
7268 1228134 : case SPACESHIP_EXPR:
7269 1228134 : rewrite_code = SPACESHIP_EXPR;
7270 1228134 : break;
7271 :
7272 : case NE_EXPR:
7273 : case EQ_EXPR:
7274 : rewrite_code = EQ_EXPR;
7275 : break;
7276 :
7277 : default:;
7278 : }
7279 :
7280 1228134 : if (rewrite_code)
7281 : {
7282 5854389 : tree r;
7283 5854389 : flags |= LOOKUP_REWRITTEN;
7284 5854389 : if (rewrite_code != code)
7285 : {
7286 : /* Add rewritten candidates in same order. */
7287 2886884 : r = add_operator_candidates (candidates, rewrite_code, ERROR_MARK,
7288 : arglist, lookups, flags, complain);
7289 2886884 : if (r == error_mark_node)
7290 : return error_mark_node;
7291 : }
7292 :
7293 5854383 : z_candidate *save_cand = *candidates;
7294 :
7295 : /* Add rewritten candidates in reverse order. */
7296 5854383 : flags |= LOOKUP_REVERSED;
7297 5854383 : vec<tree,va_gc> *revlist = make_tree_vector ();
7298 5854383 : revlist->quick_push ((*arglist)[1]);
7299 5854383 : revlist->quick_push ((*arglist)[0]);
7300 5854383 : r = add_operator_candidates (candidates, rewrite_code, ERROR_MARK,
7301 : revlist, lookups, flags, complain);
7302 5854383 : if (r == error_mark_node)
7303 : return error_mark_node;
7304 :
7305 : /* Release the vec if we didn't add a candidate that uses it. */
7306 6008852 : for (z_candidate *c = *candidates; c != save_cand; c = c->next)
7307 2778927 : if (c->args == revlist)
7308 : {
7309 : revlist = NULL;
7310 : break;
7311 : }
7312 5854383 : release_tree_vector (revlist);
7313 : }
7314 : }
7315 :
7316 : return NULL_TREE;
7317 : }
7318 :
7319 : tree
7320 204849499 : build_new_op (const op_location_t &loc, enum tree_code code, int flags,
7321 : tree arg1, tree arg2, tree arg3, tree lookups,
7322 : tree *overload, tsubst_flags_t complain)
7323 : {
7324 204849499 : struct z_candidate *candidates = 0, *cand;
7325 204849499 : releasing_vec arglist;
7326 204849499 : tree result = NULL_TREE;
7327 204849499 : bool result_valid_p = false;
7328 204849499 : enum tree_code code2 = ERROR_MARK;
7329 204849499 : enum tree_code code_orig_arg1 = ERROR_MARK;
7330 204849499 : enum tree_code code_orig_arg2 = ERROR_MARK;
7331 204849499 : bool strict_p;
7332 204849499 : bool any_viable_p;
7333 :
7334 204849499 : auto_cond_timevar tv (TV_OVERLOAD);
7335 :
7336 204849499 : if (error_operand_p (arg1)
7337 204845725 : || error_operand_p (arg2)
7338 409689199 : || error_operand_p (arg3))
7339 9799 : return error_mark_node;
7340 :
7341 204839700 : conversion_obstack_sentinel cos;
7342 :
7343 204839700 : bool ismodop = code == MODIFY_EXPR;
7344 204839700 : if (ismodop)
7345 : {
7346 10526573 : code2 = TREE_CODE (arg3);
7347 10526573 : arg3 = NULL_TREE;
7348 : }
7349 :
7350 204839700 : tree arg1_type = unlowered_expr_type (arg1);
7351 204839700 : tree arg2_type = arg2 ? unlowered_expr_type (arg2) : NULL_TREE;
7352 :
7353 204839700 : arg1 = prep_operand (arg1);
7354 :
7355 204839700 : switch (code)
7356 : {
7357 0 : case NEW_EXPR:
7358 0 : case VEC_NEW_EXPR:
7359 0 : case VEC_DELETE_EXPR:
7360 0 : case DELETE_EXPR:
7361 : /* Use build_operator_new_call and build_op_delete_call instead. */
7362 0 : gcc_unreachable ();
7363 :
7364 0 : case CALL_EXPR:
7365 : /* Use build_op_call instead. */
7366 0 : gcc_unreachable ();
7367 :
7368 16080443 : case TRUTH_ORIF_EXPR:
7369 16080443 : case TRUTH_ANDIF_EXPR:
7370 16080443 : case TRUTH_AND_EXPR:
7371 16080443 : case TRUTH_OR_EXPR:
7372 : /* These are saved for the sake of warn_logical_operator. */
7373 16080443 : code_orig_arg1 = TREE_CODE (arg1);
7374 16080443 : code_orig_arg2 = TREE_CODE (arg2);
7375 16080443 : break;
7376 47990719 : case GT_EXPR:
7377 47990719 : case LT_EXPR:
7378 47990719 : case GE_EXPR:
7379 47990719 : case LE_EXPR:
7380 47990719 : case EQ_EXPR:
7381 47990719 : case NE_EXPR:
7382 : /* These are saved for the sake of maybe_warn_bool_compare. */
7383 47990719 : code_orig_arg1 = TREE_CODE (arg1_type);
7384 47990719 : code_orig_arg2 = TREE_CODE (arg2_type);
7385 47990719 : break;
7386 :
7387 : default:
7388 : break;
7389 : }
7390 :
7391 204839700 : arg2 = prep_operand (arg2);
7392 204839700 : arg3 = prep_operand (arg3);
7393 :
7394 204839700 : if (code == COND_EXPR)
7395 : /* Use build_conditional_expr instead. */
7396 0 : gcc_unreachable ();
7397 204839700 : else if (! OVERLOAD_TYPE_P (arg1_type)
7398 385948025 : && (! arg2 || ! OVERLOAD_TYPE_P (arg2_type)))
7399 180700529 : goto builtin;
7400 :
7401 24139171 : if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
7402 : {
7403 314542 : arg2 = integer_zero_node;
7404 314542 : arg2_type = integer_type_node;
7405 : }
7406 :
7407 24139171 : arglist->quick_push (arg1);
7408 24139171 : if (arg2 != NULL_TREE)
7409 19303858 : arglist->quick_push (arg2);
7410 24139171 : if (arg3 != NULL_TREE)
7411 0 : arglist->quick_push (arg3);
7412 :
7413 24139171 : result = add_operator_candidates (&candidates, code, code2, arglist,
7414 : lookups, flags, complain);
7415 24139138 : if (result == error_mark_node)
7416 : return error_mark_node;
7417 :
7418 24139126 : switch (code)
7419 : {
7420 : case COMPOUND_EXPR:
7421 : case ADDR_EXPR:
7422 : /* For these, the built-in candidates set is empty
7423 : [over.match.oper]/3. We don't want non-strict matches
7424 : because exact matches are always possible with built-in
7425 : operators. The built-in candidate set for COMPONENT_REF
7426 : would be empty too, but since there are no such built-in
7427 : operators, we accept non-strict matches for them. */
7428 : strict_p = true;
7429 : break;
7430 :
7431 22296160 : default:
7432 22296160 : strict_p = false;
7433 22296160 : break;
7434 : }
7435 :
7436 24139126 : candidates = splice_viable (candidates, strict_p, &any_viable_p);
7437 24139126 : if (!any_viable_p)
7438 : {
7439 1882907 : switch (code)
7440 : {
7441 225 : case POSTINCREMENT_EXPR:
7442 225 : case POSTDECREMENT_EXPR:
7443 : /* Don't try anything fancy if we're not allowed to produce
7444 : errors. */
7445 225 : if (!(complain & tf_error))
7446 : return error_mark_node;
7447 :
7448 : /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
7449 : distinguish between prefix and postfix ++ and
7450 : operator++() was used for both, so we allow this with
7451 : -fpermissive. */
7452 : else
7453 : {
7454 42 : tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
7455 30 : const char *msg = (flag_permissive)
7456 42 : ? G_("no %<%D(int)%> declared for postfix %qs,"
7457 : " trying prefix operator instead")
7458 : : G_("no %<%D(int)%> declared for postfix %qs");
7459 42 : permerror (loc, msg, fnname, OVL_OP_INFO (false, code)->name);
7460 : }
7461 :
7462 42 : if (!flag_permissive)
7463 30 : return error_mark_node;
7464 :
7465 12 : if (code == POSTINCREMENT_EXPR)
7466 : code = PREINCREMENT_EXPR;
7467 : else
7468 0 : code = PREDECREMENT_EXPR;
7469 12 : result = build_new_op (loc, code, flags, arg1, NULL_TREE,
7470 : NULL_TREE, lookups, overload, complain);
7471 12 : break;
7472 :
7473 : /* The caller will deal with these. */
7474 : case ADDR_EXPR:
7475 : case COMPOUND_EXPR:
7476 : case COMPONENT_REF:
7477 : case CO_AWAIT_EXPR:
7478 : result = NULL_TREE;
7479 : result_valid_p = true;
7480 : break;
7481 :
7482 36915 : default:
7483 36915 : if (complain & tf_error)
7484 : {
7485 : /* If one of the arguments of the operator represents
7486 : an invalid use of member function pointer, try to report
7487 : a meaningful error ... */
7488 1345 : if (invalid_nonstatic_memfn_p (loc, arg1, tf_error)
7489 1342 : || invalid_nonstatic_memfn_p (loc, arg2, tf_error)
7490 2681 : || invalid_nonstatic_memfn_p (loc, arg3, tf_error))
7491 : /* We displayed the error message. */;
7492 : else
7493 : {
7494 : /* ... Otherwise, report the more generic
7495 : "no matching operator found" error */
7496 1336 : auto_diagnostic_group d;
7497 1336 : op_error (loc, code, code2, arg1, arg2, arg3, false);
7498 1336 : print_z_candidates (loc, candidates);
7499 1336 : }
7500 : }
7501 36915 : result = error_mark_node;
7502 36915 : break;
7503 : }
7504 : }
7505 : else
7506 : {
7507 22256219 : cand = tourney (candidates, complain);
7508 22256219 : if (cand == 0)
7509 : {
7510 184 : if (complain & tf_error)
7511 : {
7512 129 : auto_diagnostic_group d;
7513 129 : op_error (loc, code, code2, arg1, arg2, arg3, true);
7514 129 : print_z_candidates (loc, candidates);
7515 129 : }
7516 184 : result = error_mark_node;
7517 184 : if (overload)
7518 165 : *overload = error_mark_node;
7519 : }
7520 22256035 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
7521 : {
7522 18359797 : if (overload)
7523 : {
7524 14141530 : if (cand->rewritten ())
7525 : /* build_min_non_dep_op_overload needs to know whether the
7526 : candidate is rewritten/reversed. */
7527 1697254 : *overload = build_tree_list (build_int_cst (integer_type_node,
7528 1697254 : cand->flags),
7529 : cand->fn);
7530 : else
7531 12444276 : *overload = cand->fn;
7532 : }
7533 :
7534 18359797 : if (resolve_args (arglist, complain) == NULL)
7535 4 : result = error_mark_node;
7536 : else
7537 : {
7538 18359793 : tsubst_flags_t ocomplain = complain;
7539 18359793 : if (cand->rewritten ())
7540 : /* We'll wrap this call in another one. */
7541 1697683 : ocomplain &= ~tf_decltype;
7542 18359793 : if (cand->reversed ())
7543 : {
7544 : /* We swapped these in add_candidate, swap them back now. */
7545 16688 : std::swap (cand->convs[0], cand->convs[1]);
7546 16688 : if (cand->fn == current_function_decl)
7547 1 : warning_at (loc, 0, "in C++20 this comparison calls the "
7548 : "current function recursively with reversed "
7549 : "arguments");
7550 : }
7551 18359793 : result = build_over_call (cand, LOOKUP_NORMAL, ocomplain);
7552 : }
7553 :
7554 34167581 : if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn))
7555 : /* There won't be a CALL_EXPR. */;
7556 15804140 : else if (result && result != error_mark_node)
7557 : {
7558 15801719 : tree call = extract_call_expr (result);
7559 15801719 : CALL_EXPR_OPERATOR_SYNTAX (call) = true;
7560 :
7561 : /* Specify evaluation order as per P0145R2. */
7562 15801719 : CALL_EXPR_ORDERED_ARGS (call) = false;
7563 15801719 : switch (op_is_ordered (code))
7564 : {
7565 3582401 : case -1:
7566 3582401 : CALL_EXPR_REVERSE_ARGS (call) = true;
7567 3582401 : break;
7568 :
7569 507658 : case 1:
7570 507658 : CALL_EXPR_ORDERED_ARGS (call) = true;
7571 507658 : break;
7572 :
7573 : default:
7574 : break;
7575 : }
7576 : }
7577 :
7578 : /* If this was a C++20 rewritten comparison, adjust the result. */
7579 18359794 : if (cand->rewritten ())
7580 : {
7581 1697683 : switch (code)
7582 : {
7583 7431 : case EQ_EXPR:
7584 7431 : gcc_checking_assert (cand->reversed ());
7585 834665 : gcc_fallthrough ();
7586 834665 : case NE_EXPR:
7587 834665 : if (result == error_mark_node)
7588 : ;
7589 : /* If a rewritten operator== candidate is selected by
7590 : overload resolution for an operator @, its return type
7591 : shall be cv bool.... */
7592 834663 : else if (TREE_CODE (TREE_TYPE (result)) != BOOLEAN_TYPE)
7593 : {
7594 38 : if (complain & tf_error)
7595 : {
7596 6 : auto_diagnostic_group d;
7597 6 : error_at (loc, "return type of %qD is not %qs",
7598 : cand->fn, "bool");
7599 6 : inform (loc, "used as rewritten candidate for "
7600 : "comparison of %qT and %qT",
7601 : arg1_type, arg2_type);
7602 6 : }
7603 38 : result = error_mark_node;
7604 : }
7605 834625 : else if (code == NE_EXPR)
7606 : /* !(y == x) or !(x == y) */
7607 827215 : result = build1_loc (loc, TRUTH_NOT_EXPR,
7608 : boolean_type_node, result);
7609 : break;
7610 :
7611 : /* If a rewritten operator<=> candidate is selected by
7612 : overload resolution for an operator @, x @ y is
7613 : interpreted as 0 @ (y <=> x) if the selected candidate is
7614 : a synthesized candidate with reversed order of parameters,
7615 : or (x <=> y) @ 0 otherwise, using the selected rewritten
7616 : operator<=> candidate. */
7617 587 : case SPACESHIP_EXPR:
7618 587 : if (!cand->reversed ())
7619 : /* We're in the build_new_op call below for an outer
7620 : reversed call; we don't need to do anything more. */
7621 : break;
7622 862726 : gcc_fallthrough ();
7623 862726 : case LT_EXPR:
7624 862726 : case LE_EXPR:
7625 862726 : case GT_EXPR:
7626 862726 : case GE_EXPR:
7627 862726 : {
7628 862726 : tree lhs = result;
7629 862726 : tree rhs = integer_zero_node;
7630 862726 : if (cand->reversed ())
7631 1990 : std::swap (lhs, rhs);
7632 862726 : warning_sentinel ws (warn_zero_as_null_pointer_constant);
7633 862726 : result = build_new_op (loc, code,
7634 : LOOKUP_NORMAL|LOOKUP_REWRITTEN,
7635 : lhs, rhs, NULL_TREE, lookups,
7636 : NULL, complain);
7637 862726 : }
7638 862726 : break;
7639 :
7640 0 : default:
7641 0 : gcc_unreachable ();
7642 : }
7643 : }
7644 :
7645 : /* In an expression of the form `a[]' where cand->fn
7646 : which is operator[] turns out to be a static member function,
7647 : `a' is none-the-less evaluated. */
7648 18359794 : if (code == ARRAY_REF)
7649 454204 : result = keep_unused_object_arg (result, arg1, cand->fn);
7650 : }
7651 : else
7652 : {
7653 : /* Give any warnings we noticed during overload resolution. */
7654 3896238 : if (cand->warnings && (complain & tf_warning))
7655 : {
7656 : struct candidate_warning *w;
7657 0 : for (w = cand->warnings; w; w = w->next)
7658 0 : joust (cand, w->loser, 1, complain);
7659 : }
7660 :
7661 : /* Check for comparison of different enum types. */
7662 3896238 : switch (code)
7663 : {
7664 3049402 : case GT_EXPR:
7665 3049402 : case LT_EXPR:
7666 3049402 : case GE_EXPR:
7667 3049402 : case LE_EXPR:
7668 3049402 : case EQ_EXPR:
7669 3049402 : case NE_EXPR:
7670 3049402 : if (TREE_CODE (arg1_type) == ENUMERAL_TYPE
7671 2853840 : && TREE_CODE (arg2_type) == ENUMERAL_TYPE
7672 5828613 : && (TYPE_MAIN_VARIANT (arg1_type)
7673 2779211 : != TYPE_MAIN_VARIANT (arg2_type)))
7674 : {
7675 79 : if (cxx_dialect >= cxx26
7676 24 : && (complain & tf_warning_or_error) == 0)
7677 1 : result = error_mark_node;
7678 78 : else if (cxx_dialect >= cxx26 || (complain & tf_warning))
7679 129 : emit_diagnostic ((cxx_dialect >= cxx26
7680 : ? diagnostics::kind::pedwarn
7681 : : diagnostics::kind::warning),
7682 76 : loc, OPT_Wenum_compare,
7683 : "comparison between %q#T and %q#T",
7684 : arg1_type, arg2_type);
7685 : }
7686 : break;
7687 : default:
7688 : break;
7689 : }
7690 :
7691 : /* "If a built-in candidate is selected by overload resolution, the
7692 : operands of class type are converted to the types of the
7693 : corresponding parameters of the selected operation function,
7694 : except that the second standard conversion sequence of a
7695 : user-defined conversion sequence (12.3.3.1.2) is not applied." */
7696 3896238 : conversion *conv = cand->convs[0];
7697 3896238 : if (conv->user_conv_p)
7698 : {
7699 48740 : conv = strip_standard_conversion (conv);
7700 48740 : arg1 = convert_like (conv, arg1, complain);
7701 : }
7702 :
7703 3896238 : if (arg2)
7704 : {
7705 3417699 : conv = cand->convs[1];
7706 3417699 : if (conv->user_conv_p)
7707 : {
7708 9772 : conv = strip_standard_conversion (conv);
7709 9772 : arg2 = convert_like (conv, arg2, complain);
7710 : }
7711 : }
7712 :
7713 3896238 : if (arg3)
7714 : {
7715 0 : conv = cand->convs[2];
7716 0 : if (conv->user_conv_p)
7717 : {
7718 0 : conv = strip_standard_conversion (conv);
7719 0 : arg3 = convert_like (conv, arg3, complain);
7720 : }
7721 : }
7722 : }
7723 : }
7724 :
7725 24138910 : if (result || result_valid_p)
7726 : return result;
7727 :
7728 3896237 : builtin:
7729 184596766 : switch (code)
7730 : {
7731 4356876 : case MODIFY_EXPR:
7732 4356876 : return cp_build_modify_expr (loc, arg1, code2, arg2, complain);
7733 :
7734 19117794 : case INDIRECT_REF:
7735 19117794 : return cp_build_indirect_ref (loc, arg1, RO_UNARY_STAR, complain);
7736 :
7737 16079315 : case TRUTH_ANDIF_EXPR:
7738 16079315 : case TRUTH_ORIF_EXPR:
7739 16079315 : case TRUTH_AND_EXPR:
7740 16079315 : case TRUTH_OR_EXPR:
7741 16079315 : if ((complain & tf_warning) && !processing_template_decl)
7742 7156331 : warn_logical_operator (loc, code, boolean_type_node,
7743 : code_orig_arg1, arg1,
7744 : code_orig_arg2, arg2);
7745 : /* Fall through. */
7746 59741538 : case GT_EXPR:
7747 59741538 : case LT_EXPR:
7748 59741538 : case GE_EXPR:
7749 59741538 : case LE_EXPR:
7750 59741538 : case EQ_EXPR:
7751 59741538 : case NE_EXPR:
7752 59741538 : if ((complain & tf_warning)
7753 54040003 : && ((code_orig_arg1 == BOOLEAN_TYPE)
7754 54040003 : ^ (code_orig_arg2 == BOOLEAN_TYPE)))
7755 25699 : maybe_warn_bool_compare (loc, code, arg1, arg2);
7756 54040003 : if (complain & tf_warning && warn_tautological_compare)
7757 441191 : warn_tautological_cmp (loc, code, arg1, arg2);
7758 : /* Fall through. */
7759 126520049 : case SPACESHIP_EXPR:
7760 126520049 : case PLUS_EXPR:
7761 126520049 : case MINUS_EXPR:
7762 126520049 : case MULT_EXPR:
7763 126520049 : case TRUNC_DIV_EXPR:
7764 126520049 : case MAX_EXPR:
7765 126520049 : case MIN_EXPR:
7766 126520049 : case LSHIFT_EXPR:
7767 126520049 : case RSHIFT_EXPR:
7768 126520049 : case TRUNC_MOD_EXPR:
7769 126520049 : case BIT_AND_EXPR:
7770 126520049 : case BIT_IOR_EXPR:
7771 126520049 : case BIT_XOR_EXPR:
7772 126520049 : return cp_build_binary_op (loc, code, arg1, arg2, complain);
7773 :
7774 29444659 : case UNARY_PLUS_EXPR:
7775 29444659 : case NEGATE_EXPR:
7776 29444659 : case BIT_NOT_EXPR:
7777 29444659 : case TRUTH_NOT_EXPR:
7778 29444659 : case PREINCREMENT_EXPR:
7779 29444659 : case POSTINCREMENT_EXPR:
7780 29444659 : case PREDECREMENT_EXPR:
7781 29444659 : case POSTDECREMENT_EXPR:
7782 29444659 : case REALPART_EXPR:
7783 29444659 : case IMAGPART_EXPR:
7784 29444659 : case ABS_EXPR:
7785 29444659 : case CO_AWAIT_EXPR:
7786 29444659 : return cp_build_unary_op (code, arg1, false, complain);
7787 :
7788 2250262 : case ARRAY_REF:
7789 2250262 : return cp_build_array_ref (input_location, arg1, arg2, complain);
7790 :
7791 754 : case MEMBER_REF:
7792 754 : return build_m_component_ref (cp_build_indirect_ref (loc, arg1,
7793 : RO_ARROW_STAR,
7794 : complain),
7795 754 : arg2, complain);
7796 :
7797 : /* The caller will deal with these. */
7798 : case ADDR_EXPR:
7799 : case COMPONENT_REF:
7800 : case COMPOUND_EXPR:
7801 : return NULL_TREE;
7802 :
7803 0 : default:
7804 0 : gcc_unreachable ();
7805 : }
7806 : return NULL_TREE;
7807 204849463 : }
7808 :
7809 : /* Build a new call to operator[]. This may change ARGS. */
7810 :
7811 : tree
7812 439 : build_op_subscript (const op_location_t &loc, tree obj,
7813 : vec<tree, va_gc> **args, tree *overload,
7814 : tsubst_flags_t complain)
7815 : {
7816 439 : struct z_candidate *candidates = 0, *cand;
7817 439 : tree fns, first_mem_arg = NULL_TREE;
7818 439 : bool any_viable_p;
7819 439 : tree result = NULL_TREE;
7820 :
7821 439 : auto_cond_timevar tv (TV_OVERLOAD);
7822 :
7823 439 : obj = mark_lvalue_use (obj);
7824 :
7825 439 : if (error_operand_p (obj))
7826 0 : return error_mark_node;
7827 :
7828 439 : tree type = TREE_TYPE (obj);
7829 :
7830 439 : obj = prep_operand (obj);
7831 :
7832 439 : if (TYPE_BINFO (type))
7833 : {
7834 439 : fns = lookup_fnfields (TYPE_BINFO (type), ovl_op_identifier (ARRAY_REF),
7835 : 1, complain);
7836 439 : if (fns == error_mark_node)
7837 : return error_mark_node;
7838 : }
7839 : else
7840 : fns = NULL_TREE;
7841 :
7842 439 : if (args != NULL && *args != NULL)
7843 : {
7844 439 : *args = resolve_args (*args, complain);
7845 439 : if (*args == NULL)
7846 0 : return error_mark_node;
7847 : }
7848 :
7849 439 : conversion_obstack_sentinel cos;
7850 :
7851 439 : if (fns)
7852 : {
7853 437 : first_mem_arg = obj;
7854 :
7855 437 : add_candidates (BASELINK_FUNCTIONS (fns),
7856 : first_mem_arg, *args, NULL_TREE,
7857 : NULL_TREE, false,
7858 437 : BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
7859 : LOOKUP_NORMAL, &candidates, complain);
7860 : }
7861 :
7862 : /* Be strict here because if we choose a bad conversion candidate, the
7863 : errors we get won't mention the call context. */
7864 439 : candidates = splice_viable (candidates, true, &any_viable_p);
7865 439 : if (!any_viable_p)
7866 : {
7867 35 : if (complain & tf_error)
7868 : {
7869 1 : auto_diagnostic_group d;
7870 2 : error ("no match for call to %<%T::operator[] (%A)%>",
7871 1 : TREE_TYPE (obj), build_tree_list_vec (*args));
7872 1 : print_z_candidates (loc, candidates);
7873 1 : }
7874 35 : result = error_mark_node;
7875 : }
7876 : else
7877 : {
7878 404 : cand = tourney (candidates, complain);
7879 404 : if (cand == 0)
7880 : {
7881 0 : if (complain & tf_error)
7882 : {
7883 0 : auto_diagnostic_group d;
7884 0 : error ("call of %<%T::operator[] (%A)%> is ambiguous",
7885 0 : TREE_TYPE (obj), build_tree_list_vec (*args));
7886 0 : print_z_candidates (loc, candidates);
7887 0 : }
7888 0 : result = error_mark_node;
7889 : }
7890 404 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL
7891 404 : && DECL_OVERLOADED_OPERATOR_P (cand->fn)
7892 808 : && DECL_OVERLOADED_OPERATOR_IS (cand->fn, ARRAY_REF))
7893 : {
7894 404 : if (overload)
7895 404 : *overload = cand->fn;
7896 404 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
7897 808 : if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn))
7898 : /* There won't be a CALL_EXPR. */;
7899 404 : else if (result && result != error_mark_node)
7900 : {
7901 404 : tree call = extract_call_expr (result);
7902 404 : CALL_EXPR_OPERATOR_SYNTAX (call) = true;
7903 :
7904 : /* Specify evaluation order as per P0145R2. */
7905 404 : CALL_EXPR_ORDERED_ARGS (call) = op_is_ordered (ARRAY_REF) == 1;
7906 : }
7907 :
7908 : /* In an expression of the form `a[]' where cand->fn
7909 : which is operator[] turns out to be a static member function,
7910 : `a' is none-the-less evaluated. */
7911 404 : result = keep_unused_object_arg (result, obj, cand->fn);
7912 : }
7913 : else
7914 0 : gcc_unreachable ();
7915 : }
7916 :
7917 439 : return result;
7918 439 : }
7919 :
7920 : /* CALL was returned by some call-building function; extract the actual
7921 : CALL_EXPR from any bits that have been tacked on, e.g. by
7922 : convert_from_reference. */
7923 :
7924 : tree
7925 43349372 : extract_call_expr (tree call)
7926 : {
7927 43349803 : while (TREE_CODE (call) == COMPOUND_EXPR)
7928 431 : call = TREE_OPERAND (call, 1);
7929 43349372 : if (REFERENCE_REF_P (call))
7930 10269983 : call = TREE_OPERAND (call, 0);
7931 43349372 : if (TREE_CODE (call) == TARGET_EXPR)
7932 2906772 : call = TARGET_EXPR_INITIAL (call);
7933 :
7934 43349372 : if (TREE_CODE (call) != CALL_EXPR
7935 843495 : && TREE_CODE (call) != AGGR_INIT_EXPR
7936 744520 : && call != error_mark_node)
7937 744502 : return NULL_TREE;
7938 : return call;
7939 : }
7940 :
7941 : /* Returns true if FN has two parameters, of which the second has type
7942 : size_t. */
7943 :
7944 : static bool
7945 638073 : second_parm_is_size_t (tree fn)
7946 : {
7947 638073 : tree t = FUNCTION_ARG_CHAIN (fn);
7948 638073 : if (!t || !same_type_p (TREE_VALUE (t), size_type_node))
7949 638058 : return false;
7950 15 : t = TREE_CHAIN (t);
7951 15 : if (t == void_list_node)
7952 : return true;
7953 : return false;
7954 : }
7955 :
7956 : /* True if T, an allocation function, has std::align_val_t as its second
7957 : argument. */
7958 :
7959 : bool
7960 328732 : aligned_allocation_fn_p (tree t)
7961 : {
7962 328732 : if (!aligned_new_threshold)
7963 : return false;
7964 :
7965 319126 : tree a = FUNCTION_ARG_CHAIN (t);
7966 319126 : return (a && same_type_p (TREE_VALUE (a), align_type_node));
7967 : }
7968 :
7969 : /* True if T is std::destroying_delete_t. */
7970 :
7971 : static bool
7972 5388441 : std_destroying_delete_t_p (tree t)
7973 : {
7974 5388441 : return (TYPE_CONTEXT (t) == std_node
7975 5388441 : && id_equal (TYPE_IDENTIFIER (t), "destroying_delete_t"));
7976 : }
7977 :
7978 : /* A deallocation function with at least two parameters whose second parameter
7979 : type is of type std::destroying_delete_t is a destroying operator delete. A
7980 : destroying operator delete shall be a class member function named operator
7981 : delete. [ Note: Array deletion cannot use a destroying operator
7982 : delete. --end note ] */
7983 :
7984 : tree
7985 5388456 : destroying_delete_p (tree t)
7986 : {
7987 5388456 : tree a = TYPE_ARG_TYPES (TREE_TYPE (t));
7988 5388456 : if (!a || !TREE_CHAIN (a))
7989 : return NULL_TREE;
7990 5388441 : tree type = TREE_VALUE (TREE_CHAIN (a));
7991 5388441 : return std_destroying_delete_t_p (type) ? type : NULL_TREE;
7992 : }
7993 :
7994 : struct dealloc_info
7995 : {
7996 : bool sized;
7997 : bool aligned;
7998 : tree destroying;
7999 : };
8000 :
8001 : /* Returns true iff T, an element of an OVERLOAD chain, is a usual deallocation
8002 : function (3.7.4.2 [basic.stc.dynamic.deallocation]). If so, and DI is
8003 : non-null, also set *DI. */
8004 :
8005 : static bool
8006 3669068 : usual_deallocation_fn_p (tree t, dealloc_info *di)
8007 : {
8008 3669068 : if (di) *di = dealloc_info();
8009 :
8010 : /* A template instance is never a usual deallocation function,
8011 : regardless of its signature. */
8012 3669068 : if (TREE_CODE (t) == TEMPLATE_DECL
8013 3669068 : || primary_template_specialization_p (t))
8014 12 : return false;
8015 :
8016 : /* A usual deallocation function is a deallocation function whose parameters
8017 : after the first are
8018 : - optionally, a parameter of type std::destroying_delete_t, then
8019 : - optionally, a parameter of type std::size_t, then
8020 : - optionally, a parameter of type std::align_val_t. */
8021 3669056 : bool global = DECL_NAMESPACE_SCOPE_P (t);
8022 3669056 : tree chain = FUNCTION_ARG_CHAIN (t);
8023 3669056 : if (chain && destroying_delete_p (t))
8024 : {
8025 78 : if (di) di->destroying = TREE_VALUE (chain);
8026 78 : chain = TREE_CHAIN (chain);
8027 : }
8028 3669056 : if (chain
8029 3669053 : && (!global || flag_sized_deallocation)
8030 7323184 : && same_type_p (TREE_VALUE (chain), size_type_node))
8031 : {
8032 1056428 : if (di) di->sized = true;
8033 1056428 : chain = TREE_CHAIN (chain);
8034 : }
8035 3669053 : if (chain && aligned_new_threshold
8036 7322151 : && same_type_p (TREE_VALUE (chain), align_type_node))
8037 : {
8038 1569406 : if (di) di->aligned = true;
8039 1569406 : chain = TREE_CHAIN (chain);
8040 : }
8041 3669056 : return (chain == void_list_node);
8042 : }
8043 :
8044 : /* Just return whether FN is a usual deallocation function. */
8045 :
8046 : bool
8047 8828 : usual_deallocation_fn_p (tree fn)
8048 : {
8049 8828 : return usual_deallocation_fn_p (fn, NULL);
8050 : }
8051 :
8052 : /* Build a call to operator delete. This has to be handled very specially,
8053 : because the restrictions on what signatures match are different from all
8054 : other call instances. For a normal delete, only a delete taking (void *)
8055 : or (void *, size_t) is accepted. For a placement delete, only an exact
8056 : match with the placement new is accepted.
8057 :
8058 : CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
8059 : ADDR is the pointer to be deleted.
8060 : SIZE is the size of the memory block to be deleted.
8061 : GLOBAL_P is true if the delete-expression should not consider
8062 : class-specific delete operators.
8063 : CORO_P is true if the allocation is for a coroutine, where the two argument
8064 : usual deallocation should be chosen in preference to the single argument
8065 : version in a class context.
8066 : PLACEMENT is the corresponding placement new call, or NULL_TREE.
8067 :
8068 : If this call to "operator delete" is being generated as part to
8069 : deallocate memory allocated via a new-expression (as per [expr.new]
8070 : which requires that if the initialization throws an exception then
8071 : we call a deallocation function), then ALLOC_FN is the allocation
8072 : function. */
8073 :
8074 : static tree
8075 1172019 : build_op_delete_call_1 (enum tree_code code, tree addr, tree size,
8076 : bool global_p, bool coro_p, tree placement,
8077 : tree alloc_fn, tsubst_flags_t complain)
8078 : {
8079 1172019 : tree fn = NULL_TREE;
8080 1172019 : tree fns, fnname, type, t;
8081 1172019 : dealloc_info di_fn = { };
8082 :
8083 1172019 : if (addr == error_mark_node)
8084 : return error_mark_node;
8085 :
8086 1172019 : type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
8087 :
8088 1172019 : fnname = ovl_op_identifier (false, code);
8089 :
8090 815911 : if (CLASS_TYPE_P (type)
8091 815875 : && COMPLETE_TYPE_P (complete_type (type))
8092 1987871 : && !global_p)
8093 : /* In [class.free]
8094 :
8095 : If the result of the lookup is ambiguous or inaccessible, or if
8096 : the lookup selects a placement deallocation function, the
8097 : program is ill-formed.
8098 :
8099 : Therefore, we ask lookup_fnfields to complain about ambiguity. */
8100 : {
8101 480159 : fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1, complain);
8102 480159 : if (fns == error_mark_node)
8103 : return error_mark_node;
8104 : }
8105 : else
8106 : fns = NULL_TREE;
8107 :
8108 480156 : if (fns == NULL_TREE)
8109 1170959 : fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE);
8110 :
8111 : /* Strip const and volatile from addr. */
8112 1172016 : tree oaddr = addr;
8113 1172016 : addr = cp_convert (ptr_type_node, addr, complain);
8114 :
8115 1172016 : tree excluded_destroying = NULL_TREE;
8116 :
8117 1172016 : if (placement)
8118 : {
8119 : /* "A declaration of a placement deallocation function matches the
8120 : declaration of a placement allocation function if it has the same
8121 : number of parameters and, after parameter transformations (8.3.5),
8122 : all parameter types except the first are identical."
8123 :
8124 : So we build up the function type we want and ask instantiate_type
8125 : to get it for us. */
8126 638938 : t = FUNCTION_ARG_CHAIN (alloc_fn);
8127 638938 : t = tree_cons (NULL_TREE, ptr_type_node, t);
8128 638938 : t = build_function_type (void_type_node, t);
8129 :
8130 638938 : fn = instantiate_type (t, fns, tf_none);
8131 638938 : if (fn == error_mark_node)
8132 : return NULL_TREE;
8133 :
8134 638073 : fn = MAYBE_BASELINK_FUNCTIONS (fn);
8135 :
8136 : /* "If the lookup finds the two-parameter form of a usual deallocation
8137 : function (3.7.4.2) and that function, considered as a placement
8138 : deallocation function, would have been selected as a match for the
8139 : allocation function, the program is ill-formed." */
8140 638073 : if (second_parm_is_size_t (fn))
8141 : {
8142 9 : const char *const msg1
8143 : = G_("exception cleanup for this placement new selects "
8144 : "non-placement %<operator delete%>");
8145 9 : const char *const msg2
8146 : = G_("%qD is a usual (non-placement) deallocation "
8147 : "function in C++14 (or with %<-fsized-deallocation%>)");
8148 :
8149 : /* But if the class has an operator delete (void *), then that is
8150 : the usual deallocation function, so we shouldn't complain
8151 : about using the operator delete (void *, size_t). */
8152 9 : if (DECL_CLASS_SCOPE_P (fn))
8153 18 : for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns)))
8154 : {
8155 9 : if (usual_deallocation_fn_p (elt)
8156 9 : && FUNCTION_ARG_CHAIN (elt) == void_list_node)
8157 3 : goto ok;
8158 : }
8159 : /* Before C++14 a two-parameter global deallocation function is
8160 : always a placement deallocation function, but warn if
8161 : -Wc++14-compat. */
8162 3 : else if (!flag_sized_deallocation)
8163 : {
8164 1 : if (complain & tf_warning)
8165 : {
8166 1 : auto_diagnostic_group d;
8167 1 : if (warning (OPT_Wc__14_compat, msg1))
8168 1 : inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
8169 1 : }
8170 1 : goto ok;
8171 : }
8172 :
8173 5 : if (complain & tf_warning_or_error)
8174 : {
8175 5 : auto_diagnostic_group d;
8176 5 : if (permerror (input_location, msg1))
8177 : {
8178 : /* Only mention C++14 for namespace-scope delete. */
8179 5 : if (DECL_NAMESPACE_SCOPE_P (fn))
8180 2 : inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
8181 : else
8182 3 : inform (DECL_SOURCE_LOCATION (fn),
8183 : "%qD is a usual (non-placement) deallocation "
8184 : "function", fn);
8185 : }
8186 5 : }
8187 : else
8188 0 : return error_mark_node;
8189 1171151 : ok:;
8190 : }
8191 : }
8192 : else
8193 : /* "Any non-placement deallocation function matches a non-placement
8194 : allocation function. If the lookup finds a single matching
8195 : deallocation function, that function will be called; otherwise, no
8196 : deallocation function will be called." */
8197 4726396 : for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns)))
8198 : {
8199 3660240 : dealloc_info di_elt;
8200 3660240 : if (usual_deallocation_fn_p (elt, &di_elt))
8201 : {
8202 : /* If we're called for an EH cleanup in a new-expression, we can't
8203 : use a destroying delete; the exception was thrown before the
8204 : object was constructed. */
8205 2111212 : if (alloc_fn && di_elt.destroying)
8206 : {
8207 13 : excluded_destroying = elt;
8208 1077084 : continue;
8209 : }
8210 :
8211 2111199 : if (!fn)
8212 : {
8213 533053 : fn = elt;
8214 533053 : di_fn = di_elt;
8215 533053 : continue;
8216 : }
8217 :
8218 : /* -- If any of the deallocation functions is a destroying
8219 : operator delete, all deallocation functions that are not
8220 : destroying operator deletes are eliminated from further
8221 : consideration. */
8222 1578146 : if (di_elt.destroying != di_fn.destroying)
8223 : {
8224 12 : if (di_elt.destroying)
8225 : {
8226 6 : fn = elt;
8227 6 : di_fn = di_elt;
8228 : }
8229 12 : continue;
8230 : }
8231 :
8232 : /* -- If the type has new-extended alignment, a function with a
8233 : parameter of type std::align_val_t is preferred; otherwise a
8234 : function without such a parameter is preferred. If exactly one
8235 : preferred function is found, that function is selected and the
8236 : selection process terminates. If more than one preferred
8237 : function is found, all non-preferred functions are eliminated
8238 : from further consideration. */
8239 1578134 : if (aligned_new_threshold)
8240 : {
8241 1577867 : bool want_align = type_has_new_extended_alignment (type);
8242 1577867 : if (di_elt.aligned != di_fn.aligned)
8243 : {
8244 544006 : if (want_align == di_elt.aligned)
8245 : {
8246 507893 : fn = elt;
8247 507893 : di_fn = di_elt;
8248 : }
8249 544006 : continue;
8250 : }
8251 : }
8252 :
8253 : /* -- If the deallocation functions have class scope, the one
8254 : without a parameter of type std::size_t is selected. */
8255 1034128 : bool want_size;
8256 1034128 : if (DECL_CLASS_SCOPE_P (fn) && !coro_p)
8257 : want_size = false;
8258 :
8259 : /* -- If the type is complete and if, for the second alternative
8260 : (delete array) only, the operand is a pointer to a class type
8261 : with a non-trivial destructor or a (possibly multi-dimensional)
8262 : array thereof, the function with a parameter of type std::size_t
8263 : is selected.
8264 :
8265 : -- Otherwise, it is unspecified whether a deallocation function
8266 : with a parameter of type std::size_t is selected. */
8267 : else
8268 : {
8269 1034020 : want_size = COMPLETE_TYPE_P (type);
8270 1034020 : if (code == VEC_DELETE_EXPR
8271 1034020 : && !TYPE_VEC_NEW_USES_COOKIE (type))
8272 : /* We need a cookie to determine the array size. */
8273 : want_size = false;
8274 : }
8275 1034128 : gcc_assert (di_fn.sized != di_elt.sized);
8276 1034128 : if (want_size == di_elt.sized)
8277 : {
8278 44274 : fn = elt;
8279 44274 : di_fn = di_elt;
8280 : }
8281 : }
8282 : }
8283 :
8284 : /* If we have a matching function, call it. */
8285 1171151 : if (fn)
8286 : {
8287 1171126 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8288 :
8289 : /* If the FN is a member function, make sure that it is
8290 : accessible. */
8291 1171126 : if (BASELINK_P (fns))
8292 990 : perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn,
8293 : complain);
8294 :
8295 : /* Core issue 901: It's ok to new a type with deleted delete. */
8296 1171126 : if (DECL_DELETED_FN (fn) && alloc_fn)
8297 : return NULL_TREE;
8298 :
8299 1171120 : tree ret;
8300 1171120 : if (placement)
8301 : {
8302 : /* The placement args might not be suitable for overload
8303 : resolution at this point, so build the call directly. */
8304 638073 : int nargs = call_expr_nargs (placement);
8305 638073 : tree *argarray = XALLOCAVEC (tree, nargs);
8306 638073 : int i;
8307 638073 : argarray[0] = addr;
8308 1276197 : for (i = 1; i < nargs; i++)
8309 638124 : argarray[i] = CALL_EXPR_ARG (placement, i);
8310 638073 : if (!mark_used (fn, complain) && !(complain & tf_error))
8311 0 : return error_mark_node;
8312 638073 : ret = build_cxx_call (fn, nargs, argarray, complain);
8313 : }
8314 : else
8315 : {
8316 533047 : tree destroying = di_fn.destroying;
8317 533047 : if (destroying)
8318 : {
8319 : /* Strip const and volatile from addr but retain the type of the
8320 : object. */
8321 25 : tree rtype = TREE_TYPE (TREE_TYPE (oaddr));
8322 25 : rtype = cv_unqualified (rtype);
8323 25 : rtype = TYPE_POINTER_TO (rtype);
8324 25 : addr = cp_convert (rtype, oaddr, complain);
8325 25 : destroying = build_functional_cast (input_location,
8326 : destroying, NULL_TREE,
8327 : complain);
8328 : }
8329 :
8330 533047 : releasing_vec args;
8331 533047 : args->quick_push (addr);
8332 533047 : if (destroying)
8333 25 : args->quick_push (destroying);
8334 533047 : if (di_fn.sized)
8335 495338 : args->quick_push (size);
8336 533047 : if (di_fn.aligned)
8337 : {
8338 18060 : tree al = build_int_cst (align_type_node, TYPE_ALIGN_UNIT (type));
8339 18060 : args->quick_push (al);
8340 : }
8341 533047 : ret = cp_build_function_call_vec (fn, &args, complain);
8342 533047 : }
8343 :
8344 : /* Set this flag for all callers of this function. In addition to
8345 : delete-expressions, this is called for deallocating coroutine state;
8346 : treat that as an implicit delete-expression. This is also called for
8347 : the delete if the constructor throws in a new-expression, and for a
8348 : deleting destructor (which implements a delete-expression). */
8349 : /* But leave this flag off for destroying delete to avoid wrong
8350 : assumptions in the optimizers. */
8351 1171120 : tree call = extract_call_expr (ret);
8352 1171120 : if (TREE_CODE (call) == CALL_EXPR && !destroying_delete_p (fn))
8353 1171089 : CALL_FROM_NEW_OR_DELETE_P (call) = 1;
8354 :
8355 1171120 : return ret;
8356 : }
8357 :
8358 : /* If there's only a destroying delete that we can't use because the
8359 : object isn't constructed yet, and we used global new, use global
8360 : delete as well. */
8361 25 : if (excluded_destroying
8362 25 : && DECL_NAMESPACE_SCOPE_P (alloc_fn))
8363 7 : return build_op_delete_call (code, addr, size, true, placement,
8364 7 : alloc_fn, complain);
8365 :
8366 : /* [expr.new]
8367 :
8368 : If no unambiguous matching deallocation function can be found,
8369 : propagating the exception does not cause the object's memory to
8370 : be freed. */
8371 18 : if (alloc_fn)
8372 : {
8373 15 : if ((complain & tf_warning)
8374 15 : && !placement)
8375 : {
8376 15 : bool w = warning (0,
8377 : "no corresponding deallocation function for %qD",
8378 : alloc_fn);
8379 15 : if (w && excluded_destroying)
8380 3 : inform (DECL_SOURCE_LOCATION (excluded_destroying), "destroying "
8381 : "delete %qD cannot be used to release the allocated memory"
8382 : " if the initialization throws because the object is not "
8383 : "constructed yet", excluded_destroying);
8384 : }
8385 15 : return NULL_TREE;
8386 : }
8387 :
8388 3 : if (complain & tf_error)
8389 3 : error ("no suitable %<operator %s%> for %qT",
8390 3 : OVL_OP_INFO (false, code)->name, type);
8391 3 : return error_mark_node;
8392 : }
8393 :
8394 : /* Arguments as per build_op_delete_call_1 (). */
8395 :
8396 : tree
8397 1168796 : build_op_delete_call (enum tree_code code, tree addr, tree size, bool global_p,
8398 : tree placement, tree alloc_fn, tsubst_flags_t complain)
8399 : {
8400 1168796 : return build_op_delete_call_1 (code, addr, size, global_p, /*coro_p*/false,
8401 1168796 : placement, alloc_fn, complain);
8402 : }
8403 :
8404 : /* Arguments as per build_op_delete_call_1 (). */
8405 :
8406 : tree
8407 3223 : build_coroutine_op_delete_call (enum tree_code code, tree addr, tree size,
8408 : bool global_p, tree placement, tree alloc_fn,
8409 : tsubst_flags_t complain)
8410 : {
8411 3223 : return build_op_delete_call_1 (code, addr, size, global_p, /*coro_p*/true,
8412 3223 : placement, alloc_fn, complain);
8413 : }
8414 :
8415 : /* Issue diagnostics about a disallowed access of DECL, using DIAG_DECL
8416 : in the diagnostics.
8417 :
8418 : If ISSUE_ERROR is true, then issue an error about the access, followed
8419 : by a note showing the declaration. Otherwise, just show the note.
8420 :
8421 : DIAG_DECL and DIAG_LOCATION will almost always be the same.
8422 : DIAG_LOCATION is just another DECL. NO_ACCESS_REASON is an optional
8423 : parameter used to specify why DECL wasn't accessible (e.g. ak_private
8424 : would be because DECL was private). If not using NO_ACCESS_REASON,
8425 : then it must be ak_none, and the access failure reason will be
8426 : figured out by looking at the protection of DECL. */
8427 :
8428 : void
8429 1179 : complain_about_access (tree decl, tree diag_decl, tree diag_location,
8430 : bool issue_error, access_kind no_access_reason)
8431 : {
8432 : /* If we have not already figured out why DECL is inaccessible... */
8433 1179 : if (no_access_reason == ak_none)
8434 : {
8435 : /* Examine the access of DECL to find out why. */
8436 997 : if (TREE_PRIVATE (decl))
8437 : no_access_reason = ak_private;
8438 225 : else if (TREE_PROTECTED (decl))
8439 : no_access_reason = ak_protected;
8440 : }
8441 :
8442 : /* Now generate an error message depending on calculated access. */
8443 227 : if (no_access_reason == ak_private)
8444 : {
8445 954 : if (issue_error)
8446 940 : error ("%q#D is private within this context", diag_decl);
8447 954 : inform (DECL_SOURCE_LOCATION (diag_location), "declared private here");
8448 : }
8449 225 : else if (no_access_reason == ak_protected)
8450 : {
8451 180 : if (issue_error)
8452 166 : error ("%q#D is protected within this context", diag_decl);
8453 180 : inform (DECL_SOURCE_LOCATION (diag_location), "declared protected here");
8454 : }
8455 : /* Couldn't figure out why DECL is inaccesible, so just say it's
8456 : inaccessible. */
8457 : else
8458 : {
8459 45 : if (issue_error)
8460 45 : error ("%q#D is inaccessible within this context", diag_decl);
8461 45 : inform (DECL_SOURCE_LOCATION (diag_decl), "declared here");
8462 : }
8463 1179 : }
8464 :
8465 : /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
8466 : bitwise or of LOOKUP_* values. If any errors are warnings are
8467 : generated, set *DIAGNOSTIC_FN to "error" or "warning",
8468 : respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
8469 : to NULL. */
8470 :
8471 : static tree
8472 13026284 : build_temp (tree expr, tree type, int flags,
8473 : enum diagnostics::kind *diagnostic_kind, tsubst_flags_t complain)
8474 : {
8475 13026284 : int savew, savee;
8476 :
8477 13026284 : *diagnostic_kind = diagnostics::kind::unspecified;
8478 :
8479 : /* If the source is a packed field, calling the copy constructor will require
8480 : binding the field to the reference parameter to the copy constructor, and
8481 : we'll end up with an infinite loop. If we can use a bitwise copy, then
8482 : do that now. */
8483 13026284 : if ((lvalue_kind (expr) & clk_packed)
8484 6 : && CLASS_TYPE_P (TREE_TYPE (expr))
8485 13026290 : && !type_has_nontrivial_copy_init (TREE_TYPE (expr)))
8486 3 : return get_target_expr (expr, complain);
8487 :
8488 : /* In decltype, we might have decided not to wrap this call in a TARGET_EXPR.
8489 : But it turns out to be a subexpression, so perform temporary
8490 : materialization now. */
8491 13026281 : if (TREE_CODE (expr) == CALL_EXPR
8492 6 : && CLASS_TYPE_P (type)
8493 13026287 : && same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
8494 3 : expr = build_cplus_new (type, expr, complain);
8495 :
8496 13026281 : savew = warningcount + werrorcount, savee = errorcount;
8497 13026281 : releasing_vec args (make_tree_vector_single (expr));
8498 13026281 : expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
8499 : &args, type, flags, complain);
8500 13026281 : if (warningcount + werrorcount > savew)
8501 0 : *diagnostic_kind = diagnostics::kind::warning;
8502 13026281 : else if (errorcount > savee)
8503 77 : *diagnostic_kind = diagnostics::kind::error;
8504 13026281 : return expr;
8505 13026281 : }
8506 :
8507 : /* Get any location for EXPR, falling back to input_location.
8508 :
8509 : If the result is in a system header and is the virtual location for
8510 : a token coming from the expansion of a macro, unwind it to the
8511 : location of the expansion point of the macro (e.g. to avoid the
8512 : diagnostic being suppressed for expansions of NULL where "NULL" is
8513 : in a system header). */
8514 :
8515 : static location_t
8516 2299678 : get_location_for_expr_unwinding_for_system_header (tree expr)
8517 : {
8518 2299678 : location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
8519 2299678 : loc = expansion_point_location_if_in_system_header (loc);
8520 2299678 : return loc;
8521 : }
8522 :
8523 : /* Perform warnings about peculiar, but valid, conversions from/to NULL.
8524 : Also handle a subset of zero as null warnings.
8525 : EXPR is implicitly converted to type TOTYPE.
8526 : FN and ARGNUM are used for diagnostics. */
8527 :
8528 : static void
8529 512719628 : conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
8530 : {
8531 : /* Issue warnings about peculiar, but valid, uses of NULL. */
8532 512719628 : if (TREE_CODE (totype) != BOOLEAN_TYPE
8533 309679291 : && ARITHMETIC_TYPE_P (totype)
8534 689579767 : && null_node_p (expr))
8535 : {
8536 94 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8537 94 : if (fn)
8538 : {
8539 33 : auto_diagnostic_group d;
8540 33 : if (warning_at (loc, OPT_Wconversion_null,
8541 : "passing NULL to non-pointer argument %P of %qD",
8542 : argnum, fn))
8543 27 : inform (get_fndecl_argument_location (fn, argnum),
8544 : "declared here");
8545 33 : }
8546 : else
8547 61 : warning_at (loc, OPT_Wconversion_null,
8548 : "converting to non-pointer type %qT from NULL", totype);
8549 : }
8550 :
8551 : /* Issue warnings if "false" is converted to a NULL pointer */
8552 512719534 : else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
8553 512719534 : && TYPE_PTR_P (totype))
8554 : {
8555 7 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8556 7 : if (fn)
8557 : {
8558 3 : auto_diagnostic_group d;
8559 3 : if (warning_at (loc, OPT_Wconversion_null,
8560 : "converting %<false%> to pointer type for argument "
8561 : "%P of %qD", argnum, fn))
8562 3 : inform (get_fndecl_argument_location (fn, argnum),
8563 : "declared here");
8564 3 : }
8565 : else
8566 4 : warning_at (loc, OPT_Wconversion_null,
8567 : "converting %<false%> to pointer type %qT", totype);
8568 : }
8569 : /* Handle zero as null pointer warnings for cases other
8570 : than EQ_EXPR and NE_EXPR */
8571 472836638 : else if ((TYPE_PTR_OR_PTRMEM_P (totype) || NULLPTR_TYPE_P (totype))
8572 513024021 : && null_ptr_cst_p (expr))
8573 : {
8574 2299577 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8575 2299577 : maybe_warn_zero_as_null_pointer_constant (expr, loc);
8576 : }
8577 512719628 : }
8578 :
8579 : /* We gave a diagnostic during a conversion. If this was in the second
8580 : standard conversion sequence of a user-defined conversion sequence, say
8581 : which user-defined conversion. */
8582 :
8583 : static void
8584 5216 : maybe_print_user_conv_context (conversion *convs)
8585 : {
8586 5216 : if (convs->user_conv_p)
8587 164 : for (conversion *t = convs; t; t = next_conversion (t))
8588 140 : if (t->kind == ck_user)
8589 : {
8590 43 : print_z_candidate (0, N_(" after user-defined conversion:"),
8591 : t->cand);
8592 43 : break;
8593 : }
8594 5216 : }
8595 :
8596 : /* Locate the parameter with the given index within FNDECL.
8597 : ARGNUM is zero based, -1 indicates the `this' argument of a method.
8598 : Return the location of the FNDECL itself if there are problems. */
8599 :
8600 : location_t
8601 8663432 : get_fndecl_argument_location (tree fndecl, int argnum)
8602 : {
8603 : /* The locations of implicitly-declared functions are likely to be
8604 : more meaningful than those of their parameters. */
8605 8663432 : if (DECL_ARTIFICIAL (fndecl))
8606 297 : return DECL_SOURCE_LOCATION (fndecl);
8607 :
8608 8663135 : if (argnum == -1)
8609 49 : return DECL_SOURCE_LOCATION (fndecl);
8610 :
8611 8663086 : int i;
8612 8663086 : tree param;
8613 :
8614 : /* Locate param by index within DECL_ARGUMENTS (fndecl). */
8615 8663086 : for (i = 0, param = FUNCTION_FIRST_USER_PARM (fndecl);
8616 21732626 : i < argnum && param;
8617 13069540 : i++, param = TREE_CHAIN (param))
8618 : ;
8619 :
8620 : /* If something went wrong (e.g. if we have a builtin and thus no arguments),
8621 : return the location of FNDECL. */
8622 8663086 : if (param == NULL)
8623 38 : return DECL_SOURCE_LOCATION (fndecl);
8624 :
8625 8663048 : return DECL_SOURCE_LOCATION (param);
8626 : }
8627 :
8628 : /* If FNDECL is non-NULL, issue a note highlighting ARGNUM
8629 : within its declaration (or the fndecl itself if something went
8630 : wrong). */
8631 :
8632 : void
8633 6932 : maybe_inform_about_fndecl_for_bogus_argument_init (tree fn, int argnum,
8634 : const char *highlight_color)
8635 : {
8636 6932 : if (fn)
8637 : {
8638 1098 : gcc_rich_location richloc (get_fndecl_argument_location (fn, argnum));
8639 1098 : richloc.set_highlight_color (highlight_color);
8640 1098 : inform (&richloc,
8641 : "initializing argument %P of %qD", argnum, fn);
8642 1098 : }
8643 6932 : }
8644 :
8645 : /* Maybe warn about C++20 Conversions to arrays of unknown bound. C is
8646 : the conversion, EXPR is the expression we're converting. */
8647 :
8648 : static void
8649 36614976 : maybe_warn_array_conv (location_t loc, conversion *c, tree expr)
8650 : {
8651 36614976 : if (cxx_dialect >= cxx20)
8652 : return;
8653 :
8654 433281 : tree type = TREE_TYPE (expr);
8655 433281 : type = strip_pointer_operator (type);
8656 :
8657 433281 : if (TREE_CODE (type) != ARRAY_TYPE
8658 433281 : || TYPE_DOMAIN (type) == NULL_TREE)
8659 : return;
8660 :
8661 1851 : if (pedantic && conv_binds_to_array_of_unknown_bound (c))
8662 10 : pedwarn (loc, OPT_Wc__20_extensions,
8663 : "conversions to arrays of unknown bound "
8664 : "are only available with %<-std=c++20%> or %<-std=gnu++20%>");
8665 : }
8666 :
8667 : /* We call this recursively in convert_like_internal. */
8668 : static tree convert_like (conversion *, tree, tree, int, bool, bool, bool,
8669 : tsubst_flags_t);
8670 :
8671 : /* Adjust the result EXPR of a conversion to the expected type TOTYPE, which
8672 : must be equivalent but might be a typedef. */
8673 :
8674 : static tree
8675 846146248 : maybe_adjust_type_name (tree type, tree expr, conversion_kind kind)
8676 : {
8677 846146248 : if (expr == error_mark_node
8678 846146197 : || processing_template_decl)
8679 : return expr;
8680 :
8681 741599369 : tree etype = TREE_TYPE (expr);
8682 741599369 : if (etype == type)
8683 : return expr;
8684 :
8685 100102154 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p (etype, type)
8686 : || is_bitfield_expr_with_lowered_type (expr)
8687 : || seen_error ());
8688 :
8689 97468930 : if (SCALAR_TYPE_P (type)
8690 191937314 : && (kind == ck_rvalue
8691 : /* ??? We should be able to do this for ck_identity of more prvalue
8692 : expressions, but checking !obvalue_p here breaks, so for now let's
8693 : just handle NON_LVALUE_EXPR (such as the location wrapper for a
8694 : literal). Maybe we want to express already-rvalue in the
8695 : conversion somehow? */
8696 84860255 : || TREE_CODE (expr) == NON_LVALUE_EXPR))
8697 11062259 : expr = build_nop (type, expr);
8698 :
8699 : return expr;
8700 : }
8701 :
8702 : /* Perform the conversions in CONVS on the expression EXPR. FN and
8703 : ARGNUM are used for diagnostics. ARGNUM is zero based, -1
8704 : indicates the `this' argument of a method. INNER is nonzero when
8705 : being called to continue a conversion chain. It is negative when a
8706 : reference binding will be applied, positive otherwise. If
8707 : ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
8708 : conversions will be emitted if appropriate. If C_CAST_P is true,
8709 : this conversion is coming from a C-style cast; in that case,
8710 : conversions to inaccessible bases are permitted. */
8711 :
8712 : static tree
8713 1032109828 : convert_like_internal (conversion *convs, tree expr, tree fn, int argnum,
8714 : bool issue_conversion_warnings, bool c_cast_p,
8715 : bool nested_p, tsubst_flags_t complain)
8716 : {
8717 1032109828 : tree totype = convs->type;
8718 1032109828 : enum diagnostics::kind diag_kind;
8719 1032109828 : int flags;
8720 1032109828 : location_t loc = cp_expr_loc_or_input_loc (expr);
8721 1032109828 : const bool stub_object_p = is_stub_object (expr);
8722 :
8723 1032109828 : if (convs->bad_p && !(complain & tf_error))
8724 42990 : return error_mark_node;
8725 :
8726 1032066838 : gcc_checking_assert (!TYPE_REF_P (TREE_TYPE (expr)));
8727 :
8728 1032066838 : if (convs->bad_p
8729 6905 : && convs->kind != ck_user
8730 6811 : && convs->kind != ck_list
8731 6805 : && convs->kind != ck_ambig
8732 6805 : && (convs->kind != ck_ref_bind
8733 5224 : || (convs->user_conv_p && next_conversion (convs)->bad_p))
8734 1602 : && (convs->kind != ck_rvalue
8735 15 : || SCALAR_TYPE_P (totype))
8736 1032068431 : && convs->kind != ck_base)
8737 : {
8738 1593 : int complained = 0;
8739 1593 : conversion *t = convs;
8740 :
8741 : /* Give a helpful error if this is bad because of excess braces. */
8742 46 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8743 46 : && SCALAR_TYPE_P (totype)
8744 34 : && CONSTRUCTOR_NELTS (expr) > 0
8745 1627 : && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
8746 : {
8747 11 : complained = permerror (loc, "too many braces around initializer "
8748 : "for %qT", totype);
8749 33 : while (BRACE_ENCLOSED_INITIALIZER_P (expr)
8750 55 : && CONSTRUCTOR_NELTS (expr) == 1)
8751 22 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
8752 : }
8753 :
8754 : /* Give a helpful error if this is bad because a conversion to bool
8755 : from std::nullptr_t requires direct-initialization. */
8756 1593 : if (NULLPTR_TYPE_P (TREE_TYPE (expr))
8757 1593 : && TREE_CODE (totype) == BOOLEAN_TYPE)
8758 15 : complained = permerror (loc, "converting to %qH from %qI requires "
8759 : "direct-initialization",
8760 15 : totype, TREE_TYPE (expr));
8761 :
8762 1593 : if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (expr))
8763 72 : && SCALAR_FLOAT_TYPE_P (totype)
8764 1665 : && (extended_float_type_p (TREE_TYPE (expr))
8765 18 : || extended_float_type_p (totype)))
8766 72 : switch (cp_compare_floating_point_conversion_ranks (TREE_TYPE (expr),
8767 : totype))
8768 : {
8769 69 : case 2:
8770 69 : if (pedwarn (loc, OPT_Wnarrowing, "ISO C++ does not allow "
8771 : "converting to %qH from %qI with greater "
8772 69 : "conversion rank", totype, TREE_TYPE (expr)))
8773 1593 : complained = 1;
8774 15 : else if (!complained)
8775 15 : complained = -1;
8776 : break;
8777 3 : case 3:
8778 3 : if (pedwarn (loc, OPT_Wnarrowing, "ISO C++ does not allow "
8779 : "converting to %qH from %qI with unordered "
8780 3 : "conversion rank", totype, TREE_TYPE (expr)))
8781 : complained = 1;
8782 0 : else if (!complained)
8783 15 : complained = -1;
8784 : break;
8785 : default:
8786 : break;
8787 : }
8788 :
8789 3202 : for (; t ; t = next_conversion (t))
8790 : {
8791 3193 : if (t->kind == ck_user && t->cand->reason)
8792 : {
8793 52 : auto_diagnostic_group d;
8794 52 : complained = permerror (loc, "invalid user-defined conversion "
8795 52 : "from %qH to %qI", TREE_TYPE (expr),
8796 : totype);
8797 52 : if (complained)
8798 : {
8799 52 : auto_diagnostic_nesting_level sentinel;
8800 52 : print_z_candidate (loc, N_("candidate is:"), t->cand);
8801 52 : }
8802 52 : expr = convert_like (t, expr, fn, argnum,
8803 : /*issue_conversion_warnings=*/false,
8804 : /*c_cast_p=*/false, /*nested_p=*/true,
8805 : complain);
8806 52 : break;
8807 52 : }
8808 3141 : else if (t->kind == ck_user || !t->bad_p)
8809 : {
8810 1532 : expr = convert_like (t, expr, fn, argnum,
8811 : /*issue_conversion_warnings=*/false,
8812 : /*c_cast_p=*/false, /*nested_p=*/true,
8813 : complain);
8814 1532 : if (t->bad_p)
8815 0 : complained = 1;
8816 : break;
8817 : }
8818 1609 : else if (t->kind == ck_ambig)
8819 0 : return convert_like (t, expr, fn, argnum,
8820 : /*issue_conversion_warnings=*/false,
8821 : /*c_cast_p=*/false, /*nested_p=*/true,
8822 0 : complain);
8823 1609 : else if (t->kind == ck_identity)
8824 : break;
8825 : }
8826 1593 : if (!complained && stub_object_p)
8827 : {
8828 : /* An error diagnosed within a trait, don't give extra labels. */
8829 12 : error_at (loc, "invalid conversion from %qH to %qI",
8830 6 : TREE_TYPE (expr), totype);
8831 6 : complained = 1;
8832 : }
8833 1587 : else if (!complained && expr != error_mark_node)
8834 : {
8835 1435 : range_label_for_type_mismatch label (TREE_TYPE (expr), totype);
8836 1435 : gcc_rich_location richloc (loc, &label, highlight_colors::percent_h);
8837 1435 : complained = permerror (&richloc,
8838 : "invalid conversion from %qH to %qI",
8839 1435 : TREE_TYPE (expr), totype);
8840 1435 : if (complained)
8841 1380 : maybe_emit_indirection_note (loc, expr, totype);
8842 1435 : }
8843 1593 : if (convs->kind == ck_ref_bind)
8844 21 : expr = convert_to_reference (totype, expr, CONV_IMPLICIT,
8845 : LOOKUP_NORMAL, NULL_TREE,
8846 : complain);
8847 : else
8848 1572 : expr = cp_convert (totype, expr, complain);
8849 1593 : if (complained == 1)
8850 1521 : maybe_inform_about_fndecl_for_bogus_argument_init
8851 1521 : (fn, argnum, highlight_colors::percent_i);
8852 1593 : return expr;
8853 : }
8854 :
8855 1032065245 : if (issue_conversion_warnings && (complain & tf_warning))
8856 512719628 : conversion_null_warnings (totype, expr, fn, argnum);
8857 :
8858 1032065245 : switch (convs->kind)
8859 : {
8860 6188725 : case ck_user:
8861 6188725 : {
8862 6188725 : struct z_candidate *cand = convs->cand;
8863 :
8864 6188725 : if (cand == NULL)
8865 : /* We chose the surrogate function from add_conv_candidate, now we
8866 : actually need to build the conversion. */
8867 56 : cand = build_user_type_conversion_1 (totype, expr,
8868 : LOOKUP_NO_CONVERSION, complain);
8869 :
8870 6188725 : tree convfn = cand->fn;
8871 :
8872 : /* When converting from an init list we consider explicit
8873 : constructors, but actually trying to call one is an error. */
8874 6653997 : if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
8875 9766 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
8876 : /* Unless this is for direct-list-initialization. */
8877 9763 : && (!CONSTRUCTOR_IS_DIRECT_INIT (expr) || convs->need_temporary_p)
8878 : /* And in C++98 a default constructor can't be explicit. */
8879 6188961 : && cxx_dialect >= cxx11)
8880 : {
8881 235 : if (!(complain & tf_error))
8882 53 : return error_mark_node;
8883 182 : location_t loc = location_of (expr);
8884 182 : if (CONSTRUCTOR_NELTS (expr) == 0
8885 182 : && FUNCTION_FIRST_USER_PARMTYPE (convfn) != void_list_node)
8886 : {
8887 7 : auto_diagnostic_group d;
8888 7 : if (pedwarn (loc, 0, "converting to %qT from initializer list "
8889 : "would use explicit constructor %qD",
8890 : totype, convfn))
8891 : {
8892 7 : inform (DECL_SOURCE_LOCATION (convfn), "%qD declared here",
8893 : convfn);
8894 7 : inform (loc, "in C++11 and above a default constructor "
8895 : "can be explicit");
8896 : }
8897 7 : }
8898 : else
8899 : {
8900 175 : auto_diagnostic_group d;
8901 175 : error ("converting to %qT from initializer list would use "
8902 : "explicit constructor %qD", totype, convfn);
8903 175 : inform (DECL_SOURCE_LOCATION (convfn), "%qD declared here",
8904 : convfn);
8905 175 : }
8906 : }
8907 :
8908 : /* If we're initializing from {}, it's value-initialization. */
8909 835004 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8910 834996 : && CONSTRUCTOR_NELTS (expr) == 0
8911 142055 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype)
8912 6330700 : && !processing_template_decl)
8913 : {
8914 142028 : if (abstract_virtuals_error (NULL_TREE, totype, complain))
8915 15 : return error_mark_node;
8916 142013 : expr = build_value_init (totype, complain);
8917 142013 : expr = get_target_expr (expr, complain);
8918 142013 : if (expr != error_mark_node)
8919 141873 : TARGET_EXPR_LIST_INIT_P (expr) = true;
8920 142013 : return expr;
8921 : }
8922 :
8923 : /* We don't know here whether EXPR is being used as an lvalue or
8924 : rvalue, but we know it's read. */
8925 6046644 : mark_exp_read (expr);
8926 :
8927 : /* Give the conversion call the location of EXPR rather than the
8928 : location of the context that caused the conversion. */
8929 6046644 : iloc_sentinel ils (loc);
8930 :
8931 : /* Pass LOOKUP_NO_CONVERSION so rvalue/base handling knows not to allow
8932 : any more UDCs. */
8933 6046644 : expr = build_over_call (cand, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
8934 : complain);
8935 :
8936 : /* If this is a constructor or a function returning an aggr type,
8937 : we need to build up a TARGET_EXPR. */
8938 12093288 : if (DECL_CONSTRUCTOR_P (convfn))
8939 : {
8940 2705016 : expr = build_cplus_new (totype, expr, complain);
8941 :
8942 : /* Remember that this was list-initialization. */
8943 2705016 : if (convs->check_narrowing && expr != error_mark_node)
8944 718858 : TARGET_EXPR_LIST_INIT_P (expr) = true;
8945 : }
8946 :
8947 6046644 : return expr;
8948 6046644 : }
8949 724739667 : case ck_identity:
8950 724739667 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
8951 : {
8952 1063123 : int nelts = CONSTRUCTOR_NELTS (expr);
8953 165215 : if (nelts == 0)
8954 897908 : expr = build_value_init (totype, complain);
8955 165215 : else if (nelts == 1)
8956 165215 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
8957 : else
8958 0 : gcc_unreachable ();
8959 : }
8960 724739667 : expr = mark_use (expr, /*rvalue_p=*/!convs->rvaluedness_matches_p,
8961 : /*read_p=*/true, UNKNOWN_LOCATION,
8962 : /*reject_builtin=*/true);
8963 :
8964 724739667 : if (type_unknown_p (expr))
8965 18976 : expr = instantiate_type (totype, expr, complain);
8966 724739667 : if (!nested_p && TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8967 51078 : expr = cp_convert (totype, TREE_OPERAND (expr, 0), complain);
8968 724739667 : if (expr == null_node
8969 724739667 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype))
8970 : /* If __null has been converted to an integer type, we do not want to
8971 : continue to warn about uses of EXPR as an integer, rather than as a
8972 : pointer. */
8973 147998 : expr = build_int_cst (totype, 0);
8974 724739667 : return maybe_adjust_type_name (totype, expr, convs->kind);
8975 47 : case ck_ambig:
8976 : /* We leave bad_p off ck_ambig because overload resolution considers
8977 : it valid, it just fails when we try to perform it. So we need to
8978 : check complain here, too. */
8979 47 : if (complain & tf_error)
8980 : {
8981 : /* Call build_user_type_conversion again for the error. */
8982 3 : int flags = (convs->need_temporary_p
8983 47 : ? LOOKUP_IMPLICIT : LOOKUP_NORMAL);
8984 47 : build_user_type_conversion (totype, convs->u.expr, flags, complain);
8985 47 : gcc_assert (seen_error ());
8986 47 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
8987 : }
8988 47 : return error_mark_node;
8989 :
8990 10060 : case ck_list:
8991 10060 : {
8992 : /* Conversion to std::initializer_list<T>. */
8993 10060 : tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
8994 10060 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (expr);
8995 10060 : tree array;
8996 :
8997 10060 : if (tree init = maybe_init_list_as_array (elttype, expr))
8998 : {
8999 132 : elttype
9000 132 : = cp_build_qualified_type (elttype, (cp_type_quals (elttype)
9001 : | TYPE_QUAL_CONST));
9002 132 : tree index_type = TYPE_DOMAIN (TREE_TYPE (init));
9003 132 : array = build_cplus_array_type (elttype, index_type);
9004 132 : len = TREE_INT_CST_LOW (TYPE_MAX_VALUE (index_type)) + 1;
9005 132 : array = build_vec_init_expr (array, init, complain);
9006 132 : array = get_target_expr (array);
9007 132 : array = cp_build_addr_expr (array, complain);
9008 : }
9009 9928 : else if (len)
9010 : {
9011 9546 : tree val;
9012 9546 : unsigned ix;
9013 9546 : tree new_ctor = build_constructor (init_list_type_node, NULL);
9014 :
9015 : /* Convert all the elements. */
9016 69964 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
9017 : {
9018 60433 : if (TREE_CODE (val) == RAW_DATA_CST)
9019 : {
9020 : /* For conversion to initializer_list<unsigned char> or
9021 : initializer_list<char> or initializer_list<signed char>
9022 : we can optimize and keep RAW_DATA_CST with adjusted
9023 : type if we report narrowing errors if needed, for
9024 : others this converts each element separately. */
9025 48 : if (convs->u.list[ix]->kind == ck_std)
9026 : {
9027 18 : tree et = convs->u.list[ix]->type;
9028 18 : conversion *next = next_conversion (convs->u.list[ix]);
9029 18 : gcc_assert (et
9030 : && (TREE_CODE (et) == INTEGER_TYPE
9031 : || is_byte_access_type (et))
9032 : && TYPE_PRECISION (et) == CHAR_BIT
9033 : && next
9034 : && next->kind == ck_identity);
9035 18 : if (!TYPE_UNSIGNED (et)
9036 : /* For RAW_DATA_CST, TREE_TYPE (val) can be
9037 : either integer_type_node (when it has been
9038 : created by the lexer from CPP_EMBED) or
9039 : after digestion/conversion some integral
9040 : type with CHAR_BIT precision. For int with
9041 : precision higher than CHAR_BIT or unsigned char
9042 : diagnose narrowing conversions from
9043 : that int/unsigned char to signed char if any
9044 : byte has most significant bit set. */
9045 18 : && (TYPE_UNSIGNED (TREE_TYPE (val))
9046 12 : || (TYPE_PRECISION (TREE_TYPE (val))
9047 : > CHAR_BIT)))
9048 2460 : for (int i = 0; i < RAW_DATA_LENGTH (val); ++i)
9049 : {
9050 2454 : if (RAW_DATA_SCHAR_ELT (val, i) >= 0)
9051 2448 : continue;
9052 6 : else if (complain & tf_error)
9053 : {
9054 6 : location_t loc
9055 6 : = cp_expr_loc_or_input_loc (val);
9056 6 : int savederrorcount = errorcount;
9057 18 : permerror_opt (loc, OPT_Wnarrowing,
9058 : "narrowing conversion of "
9059 : "%qd from %qH to %qI",
9060 6 : RAW_DATA_UCHAR_ELT (val, i),
9061 6 : TREE_TYPE (val), et);
9062 6 : if (errorcount != savederrorcount)
9063 6 : return error_mark_node;
9064 : }
9065 : else
9066 0 : return error_mark_node;
9067 : }
9068 12 : tree sub = copy_node (val);
9069 12 : TREE_TYPE (sub) = et;
9070 12 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor),
9071 : NULL_TREE, sub);
9072 : }
9073 : else
9074 : {
9075 30 : conversion *conv = convs->u.list[ix];
9076 30 : gcc_assert (conv->kind == ck_list);
9077 15495 : for (int i = 0; i < RAW_DATA_LENGTH (val); ++i)
9078 : {
9079 15465 : tree elt
9080 15465 : = build_int_cst (TREE_TYPE (val),
9081 15465 : RAW_DATA_UCHAR_ELT (val, i));
9082 15465 : tree sub
9083 15465 : = convert_like (conv->u.list[i], elt,
9084 : fn, argnum, false, false,
9085 : /*nested_p=*/true, complain);
9086 15465 : if (sub == error_mark_node)
9087 : return sub;
9088 15465 : if (!check_narrowing (TREE_TYPE (sub), elt,
9089 : complain))
9090 0 : return error_mark_node;
9091 15465 : tree nc = new_ctor;
9092 15465 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (nc),
9093 : NULL_TREE, sub);
9094 15465 : if (!TREE_CONSTANT (sub))
9095 11679 : TREE_CONSTANT (new_ctor) = false;
9096 : }
9097 : }
9098 42 : len += RAW_DATA_LENGTH (val) - 1;
9099 42 : continue;
9100 42 : }
9101 60385 : tree sub = convert_like (convs->u.list[ix], val, fn,
9102 : argnum, false, false,
9103 : /*nested_p=*/true, complain);
9104 60385 : if (sub == error_mark_node)
9105 : return sub;
9106 1591 : if (!BRACE_ENCLOSED_INITIALIZER_P (val)
9107 60396 : && !check_narrowing (TREE_TYPE (sub), val, complain))
9108 0 : return error_mark_node;
9109 60376 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor),
9110 : NULL_TREE, sub);
9111 60376 : if (!TREE_CONSTANT (sub))
9112 10520 : TREE_CONSTANT (new_ctor) = false;
9113 : }
9114 : /* Build up the array. */
9115 9531 : elttype
9116 9531 : = cp_build_qualified_type (elttype, (cp_type_quals (elttype)
9117 : | TYPE_QUAL_CONST));
9118 9531 : array = build_array_of_n_type (elttype, len);
9119 9531 : array = finish_compound_literal (array, new_ctor, complain);
9120 : /* This is dubious now, should be blessed by P2752. */
9121 9531 : DECL_MERGEABLE (TARGET_EXPR_SLOT (array)) = true;
9122 9531 : array = cp_build_addr_expr (array, complain);
9123 : }
9124 : else
9125 382 : array = nullptr_node;
9126 :
9127 10045 : array = cp_convert (build_pointer_type (elttype), array, complain);
9128 10045 : if (array == error_mark_node)
9129 : return error_mark_node;
9130 :
9131 : /* Build up the initializer_list object. Note: fail gracefully
9132 : if the object cannot be completed because, for example, no
9133 : definition is provided (c++/80956). */
9134 10045 : totype = complete_type_or_maybe_complain (totype, NULL_TREE, complain);
9135 10045 : if (!totype)
9136 0 : return error_mark_node;
9137 10045 : tree field = next_aggregate_field (TYPE_FIELDS (totype));
9138 10045 : vec<constructor_elt, va_gc> *vec = NULL;
9139 10045 : CONSTRUCTOR_APPEND_ELT (vec, field, array);
9140 10045 : field = next_aggregate_field (DECL_CHAIN (field));
9141 10045 : CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
9142 10045 : tree new_ctor = build_constructor (totype, vec);
9143 10045 : return get_target_expr (new_ctor, complain);
9144 : }
9145 :
9146 1202184 : case ck_aggr:
9147 1202184 : if (TREE_CODE (totype) == COMPLEX_TYPE)
9148 : {
9149 27516 : tree real = CONSTRUCTOR_ELT (expr, 0)->value;
9150 27516 : tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
9151 27516 : real = perform_implicit_conversion (TREE_TYPE (totype),
9152 : real, complain);
9153 27516 : imag = perform_implicit_conversion (TREE_TYPE (totype),
9154 : imag, complain);
9155 27516 : expr = build2 (COMPLEX_EXPR, totype, real, imag);
9156 27516 : return expr;
9157 : }
9158 1174668 : expr = reshape_init (totype, expr, complain);
9159 1174668 : expr = get_target_expr (digest_init (totype, expr, complain),
9160 : complain);
9161 1174668 : if (expr != error_mark_node)
9162 1174657 : TARGET_EXPR_LIST_INIT_P (expr) = true;
9163 : return expr;
9164 :
9165 299924562 : default:
9166 299924562 : break;
9167 299924562 : };
9168 :
9169 299924562 : conversion *nc = next_conversion (convs);
9170 299924562 : if (convs->kind == ck_ref_bind && nc->kind == ck_qual
9171 7490 : && !convs->need_temporary_p)
9172 : /* direct_reference_binding might have inserted a ck_qual under
9173 : this ck_ref_bind for the benefit of conversion sequence ranking.
9174 : Don't actually perform that conversion. */
9175 5651 : nc = next_conversion (nc);
9176 :
9177 299924562 : expr = convert_like (nc, expr, fn, argnum,
9178 : convs->kind == ck_ref_bind
9179 299924562 : ? issue_conversion_warnings : false,
9180 : c_cast_p, /*nested_p=*/true, complain & ~tf_no_cleanup);
9181 299924562 : if (expr == error_mark_node)
9182 : return error_mark_node;
9183 :
9184 299924089 : switch (convs->kind)
9185 : {
9186 134224286 : case ck_rvalue:
9187 134224286 : expr = decay_conversion (expr, complain);
9188 134224286 : if (expr == error_mark_node)
9189 : {
9190 15 : if (complain & tf_error)
9191 : {
9192 12 : auto_diagnostic_group d;
9193 12 : maybe_print_user_conv_context (convs);
9194 12 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9195 12 : }
9196 15 : return error_mark_node;
9197 : }
9198 :
9199 134224271 : if ((complain & tf_warning) && fn
9200 38575904 : && warn_suggest_attribute_format)
9201 : {
9202 706 : tree rhstype = TREE_TYPE (expr);
9203 706 : const enum tree_code coder = TREE_CODE (rhstype);
9204 706 : const enum tree_code codel = TREE_CODE (totype);
9205 706 : if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
9206 250 : && coder == codel
9207 956 : && check_missing_format_attribute (totype, rhstype))
9208 6 : warning (OPT_Wsuggest_attribute_format,
9209 : "argument of function call might be a candidate "
9210 : "for a format attribute");
9211 : }
9212 :
9213 134224271 : if (! MAYBE_CLASS_TYPE_P (totype))
9214 121406581 : return maybe_adjust_type_name (totype, expr, convs->kind);
9215 :
9216 : /* Don't introduce copies when passing arguments along to the inherited
9217 : constructor. */
9218 12817690 : if (current_function_decl
9219 10134384 : && flag_new_inheriting_ctors
9220 33085978 : && DECL_INHERITED_CTOR (current_function_decl))
9221 : return expr;
9222 :
9223 12813993 : if (TREE_CODE (expr) == TARGET_EXPR
9224 12813993 : && TARGET_EXPR_LIST_INIT_P (expr))
9225 : /* Copy-list-initialization doesn't actually involve a copy. */
9226 : return expr;
9227 :
9228 : /* Fall through. */
9229 14734234 : case ck_base:
9230 14734234 : if (convs->kind == ck_base && !convs->need_temporary_p)
9231 : {
9232 : /* We are going to bind a reference directly to a base-class
9233 : subobject of EXPR. */
9234 : /* Build an expression for `*((base*) &expr)'. */
9235 1707950 : expr = convert_to_base (expr, totype,
9236 1707950 : !c_cast_p, /*nonnull=*/true, complain);
9237 1707950 : return expr;
9238 : }
9239 :
9240 : /* Copy-initialization where the cv-unqualified version of the source
9241 : type is the same class as, or a derived class of, the class of the
9242 : destination [is treated as direct-initialization]. [dcl.init] */
9243 13026284 : flags = LOOKUP_NORMAL;
9244 : /* This conversion is being done in the context of a user-defined
9245 : conversion (i.e. the second step of copy-initialization), so
9246 : don't allow any more. */
9247 13026284 : if (convs->user_conv_p)
9248 248200 : flags |= LOOKUP_NO_CONVERSION;
9249 : /* We might be performing a conversion of the argument
9250 : to the user-defined conversion, i.e., not a conversion of the
9251 : result of the user-defined conversion. In which case we skip
9252 : explicit constructors. */
9253 13026284 : if (convs->copy_init_p)
9254 12768639 : flags |= LOOKUP_ONLYCONVERTING;
9255 13026284 : expr = build_temp (expr, totype, flags, &diag_kind, complain);
9256 13026284 : if (diag_kind != diagnostics::kind::unspecified && complain)
9257 : {
9258 77 : auto_diagnostic_group d;
9259 77 : maybe_print_user_conv_context (convs);
9260 77 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9261 77 : }
9262 :
9263 13026284 : return build_cplus_new (totype, expr, complain);
9264 :
9265 55119238 : case ck_ref_bind:
9266 55119238 : {
9267 55119238 : tree ref_type = totype;
9268 :
9269 55119238 : if (convs->bad_p && !next_conversion (convs)->bad_p)
9270 : {
9271 5127 : tree extype = TREE_TYPE (expr);
9272 5127 : auto_diagnostic_group d;
9273 5127 : if (TYPE_REF_IS_RVALUE (ref_type)
9274 5127 : && lvalue_p (expr))
9275 1638 : error_at (loc, "cannot bind rvalue reference of type %qH to "
9276 : "lvalue of type %qI", totype, extype);
9277 6132 : else if (!TYPE_REF_IS_RVALUE (ref_type) && !lvalue_p (expr)
9278 4953 : && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
9279 : {
9280 1215 : conversion *next = next_conversion (convs);
9281 1215 : if (next->kind == ck_std)
9282 : {
9283 59 : next = next_conversion (next);
9284 59 : error_at (loc, "cannot bind non-const lvalue reference of "
9285 : "type %qH to a value of type %qI",
9286 : totype, next->type);
9287 : }
9288 1156 : else if (!CP_TYPE_CONST_P (TREE_TYPE (ref_type)))
9289 815 : error_at (loc, "cannot bind non-const lvalue reference of "
9290 : "type %qH to an rvalue of type %qI", totype, extype);
9291 : else // extype is volatile
9292 341 : error_at (loc, "cannot bind lvalue reference of type "
9293 : "%qH to an rvalue of type %qI", totype,
9294 : extype);
9295 : }
9296 2274 : else if (!reference_compatible_p (TREE_TYPE (totype), extype))
9297 : {
9298 : /* If we're converting from T[] to T[N], don't talk
9299 : about discarding qualifiers. (Converting from T[N] to
9300 : T[] is allowed by P0388R4.) */
9301 2274 : if (TREE_CODE (extype) == ARRAY_TYPE
9302 27 : && TYPE_DOMAIN (extype) == NULL_TREE
9303 15 : && TREE_CODE (TREE_TYPE (totype)) == ARRAY_TYPE
9304 2289 : && TYPE_DOMAIN (TREE_TYPE (totype)) != NULL_TREE)
9305 15 : error_at (loc, "cannot bind reference of type %qH to %qI "
9306 : "due to different array bounds", totype, extype);
9307 : else
9308 2259 : error_at (loc, "binding reference of type %qH to %qI "
9309 : "discards qualifiers", totype, extype);
9310 : }
9311 : else
9312 0 : gcc_unreachable ();
9313 5127 : maybe_print_user_conv_context (convs);
9314 5127 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9315 :
9316 5127 : return error_mark_node;
9317 5127 : }
9318 55114111 : else if (complain & tf_warning)
9319 35258865 : maybe_warn_array_conv (loc, convs, expr);
9320 :
9321 : /* If necessary, create a temporary.
9322 :
9323 : VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
9324 : that need temporaries, even when their types are reference
9325 : compatible with the type of reference being bound, so the
9326 : upcoming call to cp_build_addr_expr doesn't fail. */
9327 55114111 : if (convs->need_temporary_p
9328 52726625 : || TREE_CODE (expr) == CONSTRUCTOR
9329 52726530 : || TREE_CODE (expr) == VA_ARG_EXPR)
9330 : {
9331 : /* Otherwise, a temporary of type "cv1 T1" is created and
9332 : initialized from the initializer expression using the rules
9333 : for a non-reference copy-initialization (8.5). */
9334 :
9335 2387581 : tree type = TREE_TYPE (ref_type);
9336 2387581 : cp_lvalue_kind lvalue = lvalue_kind (expr);
9337 :
9338 2387581 : gcc_assert (similar_type_p (type, next_conversion (convs)->type));
9339 2387581 : if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
9340 3234055 : && !TYPE_REF_IS_RVALUE (ref_type))
9341 : {
9342 : /* If the reference is volatile or non-const, we
9343 : cannot create a temporary. */
9344 105 : if (complain & tf_error)
9345 : {
9346 103 : if (lvalue & clk_bitfield)
9347 30 : error_at (loc, "cannot bind bit-field %qE to %qT",
9348 : expr, ref_type);
9349 73 : else if (lvalue & clk_packed)
9350 9 : error_at (loc, "cannot bind packed field %qE to %qT",
9351 : expr, ref_type);
9352 : else
9353 64 : error_at (loc, "cannot bind rvalue %qE to %qT",
9354 : expr, ref_type);
9355 : }
9356 105 : return error_mark_node;
9357 : }
9358 : /* If the source is a packed field, and we must use a copy
9359 : constructor, then building the target expr will require
9360 : binding the field to the reference parameter to the
9361 : copy constructor, and we'll end up with an infinite
9362 : loop. If we can use a bitwise copy, then we'll be
9363 : OK. */
9364 2387476 : if ((lvalue & clk_packed)
9365 20 : && CLASS_TYPE_P (type)
9366 2387493 : && type_has_nontrivial_copy_init (type))
9367 : {
9368 6 : error_at (loc, "cannot bind packed field %qE to %qT",
9369 : expr, ref_type);
9370 6 : return error_mark_node;
9371 : }
9372 2387470 : if (lvalue & clk_bitfield)
9373 : {
9374 24 : expr = convert_bitfield_to_declared_type (expr);
9375 24 : expr = fold_convert (type, expr);
9376 : }
9377 :
9378 : /* Creating &TARGET_EXPR<> in a template would break when
9379 : tsubsting the expression, so use an IMPLICIT_CONV_EXPR
9380 : instead. This can happen even when there's no class
9381 : involved, e.g., when converting an integer to a reference
9382 : type. */
9383 2387470 : if (processing_template_decl)
9384 9338 : return build1 (IMPLICIT_CONV_EXPR, totype, expr);
9385 2378132 : expr = build_target_expr_with_type (expr, type, complain);
9386 : }
9387 :
9388 : /* Take the address of the thing to which we will bind the
9389 : reference. */
9390 55104662 : expr = cp_build_addr_expr (expr, complain);
9391 55104662 : if (expr == error_mark_node)
9392 : return error_mark_node;
9393 :
9394 : /* Convert it to a pointer to the type referred to by the
9395 : reference. This will adjust the pointer if a derived to
9396 : base conversion is being performed. */
9397 55104662 : expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
9398 : expr, complain);
9399 : /* Convert the pointer to the desired reference type. */
9400 55104662 : return build_nop (ref_type, expr);
9401 : }
9402 :
9403 2668469 : case ck_lvalue:
9404 2668469 : return decay_conversion (expr, complain);
9405 :
9406 225578 : case ck_fnptr:
9407 : /* ??? Should the address of a transaction-safe pointer point to the TM
9408 : clone, and this conversion look up the primary function? */
9409 225578 : return build_nop (totype, expr);
9410 :
9411 1542306 : case ck_qual:
9412 : /* Warn about deprecated conversion if appropriate. */
9413 1542306 : if (complain & tf_warning)
9414 : {
9415 1356111 : string_conv_p (totype, expr, 1);
9416 1356111 : maybe_warn_array_conv (loc, convs, expr);
9417 : }
9418 : break;
9419 :
9420 2905785 : case ck_ptr:
9421 2905785 : if (convs->base_p)
9422 206998 : expr = convert_to_base (expr, totype, !c_cast_p,
9423 : /*nonnull=*/false, complain);
9424 2905785 : return build_nop (totype, expr);
9425 :
9426 27844 : case ck_pmem:
9427 27844 : return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
9428 27844 : c_cast_p, complain);
9429 :
9430 : default:
9431 : break;
9432 : }
9433 :
9434 102806197 : if (convs->check_narrowing
9435 156473453 : && !check_narrowing (totype, expr, complain,
9436 53667256 : convs->check_narrowing_const_only))
9437 441 : return error_mark_node;
9438 :
9439 205611512 : warning_sentinel w (warn_zero_as_null_pointer_constant);
9440 102805756 : if (issue_conversion_warnings)
9441 80751223 : expr = cp_convert_and_check (totype, expr, complain);
9442 : else
9443 : {
9444 22054533 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
9445 36 : expr = TREE_OPERAND (expr, 0);
9446 22054533 : expr = cp_convert (totype, expr, complain);
9447 : }
9448 :
9449 102805756 : return expr;
9450 : }
9451 :
9452 : /* Return true if converting FROM to TO is unsafe in a template. */
9453 :
9454 : static bool
9455 1890350 : conv_unsafe_in_template_p (tree to, tree from)
9456 : {
9457 : /* Converting classes involves TARGET_EXPR. */
9458 1890350 : if (CLASS_TYPE_P (to) || CLASS_TYPE_P (from))
9459 : return true;
9460 :
9461 : /* Converting real to integer produces FIX_TRUNC_EXPR which tsubst
9462 : doesn't handle. */
9463 1475086 : if (SCALAR_FLOAT_TYPE_P (from) && INTEGRAL_OR_ENUMERATION_TYPE_P (to))
9464 : return true;
9465 :
9466 : /* Converting integer to real isn't a trivial conversion, either. */
9467 1475083 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (from) && SCALAR_FLOAT_TYPE_P (to))
9468 3 : return true;
9469 :
9470 : return false;
9471 : }
9472 :
9473 : /* Wrapper for convert_like_internal that handles creating
9474 : IMPLICIT_CONV_EXPR. */
9475 :
9476 : static tree
9477 1032525089 : convert_like (conversion *convs, tree expr, tree fn, int argnum,
9478 : bool issue_conversion_warnings, bool c_cast_p, bool nested_p,
9479 : tsubst_flags_t complain)
9480 : {
9481 : /* Creating &TARGET_EXPR<> in a template breaks when substituting,
9482 : and creating a CALL_EXPR in a template breaks in finish_call_expr
9483 : so use an IMPLICIT_CONV_EXPR for this conversion. We would have
9484 : created such codes e.g. when calling a user-defined conversion
9485 : function. */
9486 1032525089 : tree conv_expr = NULL_TREE;
9487 1032525089 : if (processing_template_decl
9488 105727488 : && convs->kind != ck_identity
9489 1034415439 : && conv_unsafe_in_template_p (convs->type, TREE_TYPE (expr)))
9490 : {
9491 415270 : conv_expr = build1 (IMPLICIT_CONV_EXPR, convs->type, expr);
9492 415270 : if (convs->kind != ck_ref_bind)
9493 46150 : conv_expr = convert_from_reference (conv_expr);
9494 415270 : if (!convs->bad_p)
9495 : return conv_expr;
9496 : /* Do the normal processing to give the bad_p errors. But we still
9497 : need to return the IMPLICIT_CONV_EXPR, unless we're returning
9498 : error_mark_node. */
9499 : }
9500 1032109828 : expr = convert_like_internal (convs, expr, fn, argnum,
9501 : issue_conversion_warnings, c_cast_p,
9502 : nested_p, complain);
9503 1032109828 : if (expr == error_mark_node)
9504 : return error_mark_node;
9505 1032059481 : return conv_expr ? conv_expr : expr;
9506 : }
9507 :
9508 : /* Convenience wrapper for convert_like. */
9509 :
9510 : static inline tree
9511 570612450 : convert_like (conversion *convs, tree expr, tsubst_flags_t complain)
9512 : {
9513 570612450 : return convert_like (convs, expr, NULL_TREE, 0,
9514 : /*issue_conversion_warnings=*/true,
9515 570612450 : /*c_cast_p=*/false, /*nested_p=*/false, complain);
9516 : }
9517 :
9518 : /* Convenience wrapper for convert_like. */
9519 :
9520 : static inline tree
9521 123645826 : convert_like_with_context (conversion *convs, tree expr, tree fn, int argnum,
9522 : tsubst_flags_t complain)
9523 : {
9524 0 : return convert_like (convs, expr, fn, argnum,
9525 : /*issue_conversion_warnings=*/true,
9526 : /*c_cast_p=*/false, /*nested_p=*/false, complain);
9527 : }
9528 :
9529 : /* ARG is being passed to a varargs function. Perform any conversions
9530 : required. Return the converted value. */
9531 :
9532 : tree
9533 1340456 : convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
9534 : {
9535 1340456 : tree arg_type = TREE_TYPE (arg);
9536 1340456 : location_t loc = cp_expr_loc_or_input_loc (arg);
9537 :
9538 : /* [expr.call]
9539 :
9540 : If the argument has integral or enumeration type that is subject
9541 : to the integral promotions (_conv.prom_), or a floating-point
9542 : type that is subject to the floating-point promotion
9543 : (_conv.fpprom_), the value of the argument is converted to the
9544 : promoted type before the call. */
9545 1340456 : if (SCALAR_FLOAT_TYPE_P (arg_type)
9546 47942 : && (TYPE_PRECISION (arg_type)
9547 47942 : < TYPE_PRECISION (double_type_node))
9548 11836 : && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type))
9549 1351614 : && !extended_float_type_p (arg_type))
9550 : {
9551 11154 : if ((complain & tf_warning)
9552 11151 : && warn_double_promotion && !c_inhibit_evaluation_warnings)
9553 3 : warning_at (loc, OPT_Wdouble_promotion,
9554 : "implicit conversion from %qH to %qI when passing "
9555 : "argument to function",
9556 : arg_type, double_type_node);
9557 11154 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
9558 3 : arg = TREE_OPERAND (arg, 0);
9559 11154 : arg = mark_rvalue_use (arg);
9560 11154 : arg = convert_to_real_nofold (double_type_node, arg);
9561 : }
9562 1329302 : else if (NULLPTR_TYPE_P (arg_type))
9563 : {
9564 75 : arg = mark_rvalue_use (arg);
9565 75 : if (TREE_SIDE_EFFECTS (arg))
9566 : {
9567 6 : warning_sentinel w(warn_unused_result);
9568 6 : arg = cp_build_compound_expr (arg, null_pointer_node, complain);
9569 6 : }
9570 : else
9571 69 : arg = null_pointer_node;
9572 : }
9573 1329227 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
9574 : {
9575 877635 : if (SCOPED_ENUM_P (arg_type))
9576 : {
9577 56 : tree prom = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg,
9578 : complain);
9579 56 : prom = cp_perform_integral_promotions (prom, complain);
9580 109 : if (abi_version_crosses (6)
9581 30 : && TYPE_MODE (TREE_TYPE (prom)) != TYPE_MODE (arg_type)
9582 86 : && (complain & tf_warning))
9583 60 : warning_at (loc, OPT_Wabi, "scoped enum %qT passed through %<...%>"
9584 : " as %qT before %<-fabi-version=6%>, %qT after",
9585 : arg_type,
9586 30 : TREE_TYPE (prom), ENUM_UNDERLYING_TYPE (arg_type));
9587 56 : if (!abi_version_at_least (6))
9588 1340456 : arg = prom;
9589 : }
9590 : else
9591 877579 : arg = cp_perform_integral_promotions (arg, complain);
9592 : }
9593 : else
9594 : /* [expr.call]
9595 :
9596 : The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
9597 : standard conversions are performed. */
9598 451592 : arg = decay_conversion (arg, complain);
9599 :
9600 1340456 : arg = require_complete_type (arg, complain);
9601 1340456 : arg_type = TREE_TYPE (arg);
9602 :
9603 1340456 : if (arg != error_mark_node
9604 : /* In a template (or ill-formed code), we can have an incomplete type
9605 : even after require_complete_type, in which case we don't know
9606 : whether it has trivial copy or not. */
9607 1340444 : && COMPLETE_TYPE_P (arg_type)
9608 2680900 : && !cp_unevaluated_operand)
9609 : {
9610 : /* [expr.call] 5.2.2/7:
9611 : Passing a potentially-evaluated argument of class type (Clause 9)
9612 : with a non-trivial copy constructor or a non-trivial destructor
9613 : with no corresponding parameter is conditionally-supported, with
9614 : implementation-defined semantics.
9615 :
9616 : We support it as pass-by-invisible-reference, just like a normal
9617 : value parameter.
9618 :
9619 : If the call appears in the context of a sizeof expression,
9620 : it is not potentially-evaluated. */
9621 639084 : if (type_has_nontrivial_copy_init (arg_type)
9622 639084 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type))
9623 : {
9624 35 : arg = force_rvalue (arg, complain);
9625 35 : if (complain & tf_warning)
9626 35 : warning (OPT_Wconditionally_supported,
9627 : "passing objects of non-trivially-copyable "
9628 : "type %q#T through %<...%> is conditionally supported",
9629 : arg_type);
9630 35 : return build1 (ADDR_EXPR, build_reference_type (arg_type), arg);
9631 : }
9632 : /* Build up a real lvalue-to-rvalue conversion in case the
9633 : copy constructor is trivial but not callable. */
9634 639049 : else if (CLASS_TYPE_P (arg_type))
9635 44156 : force_rvalue (arg, complain);
9636 :
9637 : }
9638 :
9639 : return arg;
9640 : }
9641 :
9642 : /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
9643 :
9644 : tree
9645 31142 : build_x_va_arg (location_t loc, tree expr, tree type)
9646 : {
9647 31142 : if (processing_template_decl)
9648 : {
9649 39 : tree r = build_min (VA_ARG_EXPR, type, expr);
9650 39 : SET_EXPR_LOCATION (r, loc);
9651 39 : return r;
9652 : }
9653 :
9654 31103 : type = complete_type_or_else (type, NULL_TREE);
9655 :
9656 31103 : if (expr == error_mark_node || !type)
9657 : return error_mark_node;
9658 :
9659 31082 : expr = mark_lvalue_use (expr);
9660 :
9661 31082 : if (TYPE_REF_P (type))
9662 : {
9663 6 : error ("cannot receive reference type %qT through %<...%>", type);
9664 6 : return error_mark_node;
9665 : }
9666 :
9667 31076 : if (type_has_nontrivial_copy_init (type)
9668 31076 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
9669 : {
9670 : /* conditionally-supported behavior [expr.call] 5.2.2/7. Let's treat
9671 : it as pass by invisible reference. */
9672 12 : warning_at (loc, OPT_Wconditionally_supported,
9673 : "receiving objects of non-trivially-copyable type %q#T "
9674 : "through %<...%> is conditionally-supported", type);
9675 :
9676 12 : tree ref = cp_build_reference_type (type, false);
9677 12 : expr = build_va_arg (loc, expr, ref);
9678 12 : return convert_from_reference (expr);
9679 : }
9680 :
9681 31064 : tree ret = build_va_arg (loc, expr, type);
9682 31064 : if (CLASS_TYPE_P (type))
9683 : /* Wrap the VA_ARG_EXPR in a TARGET_EXPR now so other code doesn't need to
9684 : know how to handle it. */
9685 12094 : ret = get_target_expr (ret);
9686 : return ret;
9687 : }
9688 :
9689 : /* TYPE has been given to va_arg. Apply the default conversions which
9690 : would have happened when passed via ellipsis. Return the promoted
9691 : type, or the passed type if there is no change. */
9692 :
9693 : tree
9694 2584002 : cxx_type_promotes_to (tree type)
9695 : {
9696 2584002 : tree promote;
9697 :
9698 : /* Perform the array-to-pointer and function-to-pointer
9699 : conversions. */
9700 2584002 : type = type_decays_to (type);
9701 :
9702 2584002 : promote = type_promotes_to (type);
9703 2584002 : if (same_type_p (type, promote))
9704 2583978 : promote = type;
9705 :
9706 2584002 : return promote;
9707 : }
9708 :
9709 : /* ARG is a default argument expression being passed to a parameter of
9710 : the indicated TYPE, which is a parameter to FN. PARMNUM is the
9711 : zero-based argument number. Do any required conversions. Return
9712 : the converted value. */
9713 :
9714 : static GTY(()) vec<tree, va_gc> *default_arg_context;
9715 : void
9716 7585923 : push_defarg_context (tree fn)
9717 7585923 : { vec_safe_push (default_arg_context, fn); }
9718 :
9719 : void
9720 7585923 : pop_defarg_context (void)
9721 7585923 : { default_arg_context->pop (); }
9722 :
9723 : tree
9724 1027063 : convert_default_arg (tree type, tree arg, tree fn, int parmnum,
9725 : tsubst_flags_t complain)
9726 : {
9727 1027063 : int i;
9728 1027063 : tree t;
9729 :
9730 : /* See through clones. */
9731 1027063 : fn = DECL_ORIGIN (fn);
9732 : /* And inheriting ctors. */
9733 1027063 : if (flag_new_inheriting_ctors)
9734 1026423 : fn = strip_inheriting_ctors (fn);
9735 :
9736 : /* Detect recursion. */
9737 1028992 : FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t)
9738 1935 : if (t == fn)
9739 : {
9740 6 : if (complain & tf_error)
9741 6 : error ("recursive evaluation of default argument for %q#D", fn);
9742 6 : return error_mark_node;
9743 : }
9744 :
9745 : /* If the ARG is an unparsed default argument expression, the
9746 : conversion cannot be performed. */
9747 1027057 : if (TREE_CODE (arg) == DEFERRED_PARSE)
9748 : {
9749 15 : if (complain & tf_error)
9750 15 : error ("call to %qD uses the default argument for parameter %P, which "
9751 : "is not yet defined", fn, parmnum);
9752 15 : return error_mark_node;
9753 : }
9754 :
9755 1027042 : push_defarg_context (fn);
9756 :
9757 1027042 : if (fn && DECL_TEMPLATE_INFO (fn))
9758 742623 : arg = tsubst_default_argument (fn, parmnum, type, arg, complain);
9759 :
9760 : /* Due to:
9761 :
9762 : [dcl.fct.default]
9763 :
9764 : The names in the expression are bound, and the semantic
9765 : constraints are checked, at the point where the default
9766 : expressions appears.
9767 :
9768 : we must not perform access checks here. */
9769 1027042 : push_deferring_access_checks (dk_no_check);
9770 : /* We must make a copy of ARG, in case subsequent processing
9771 : alters any part of it. */
9772 1027042 : arg = break_out_target_exprs (arg, /*clear location*/true);
9773 :
9774 1027042 : arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
9775 : ICR_DEFAULT_ARGUMENT, fn, parmnum,
9776 : complain);
9777 1027042 : arg = convert_for_arg_passing (type, arg, complain);
9778 1027042 : pop_deferring_access_checks();
9779 :
9780 1027042 : pop_defarg_context ();
9781 :
9782 1027042 : return arg;
9783 : }
9784 :
9785 : /* Returns the type which will really be used for passing an argument of
9786 : type TYPE. */
9787 :
9788 : tree
9789 638922087 : type_passed_as (tree type)
9790 : {
9791 : /* Pass classes with copy ctors by invisible reference. */
9792 638922087 : if (TREE_ADDRESSABLE (type))
9793 593427 : type = build_reference_type (type);
9794 :
9795 638922087 : return type;
9796 : }
9797 :
9798 : /* Actually perform the appropriate conversion. */
9799 :
9800 : tree
9801 129253046 : convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
9802 : {
9803 129253046 : tree bitfield_type;
9804 :
9805 : /* If VAL is a bitfield, then -- since it has already been converted
9806 : to TYPE -- it cannot have a precision greater than TYPE.
9807 :
9808 : If it has a smaller precision, we must widen it here. For
9809 : example, passing "int f:3;" to a function expecting an "int" will
9810 : not result in any conversion before this point.
9811 :
9812 : If the precision is the same we must not risk widening. For
9813 : example, the COMPONENT_REF for a 32-bit "long long" bitfield will
9814 : often have type "int", even though the C++ type for the field is
9815 : "long long". If the value is being passed to a function
9816 : expecting an "int", then no conversions will be required. But,
9817 : if we call convert_bitfield_to_declared_type, the bitfield will
9818 : be converted to "long long". */
9819 129253046 : bitfield_type = is_bitfield_expr_with_lowered_type (val);
9820 129253046 : if (bitfield_type
9821 129253046 : && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
9822 0 : val = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type), val);
9823 :
9824 129253046 : if (val == error_mark_node)
9825 : ;
9826 : /* Pass classes with copy ctors by invisible reference. */
9827 129249957 : else if (TREE_ADDRESSABLE (type))
9828 199666 : val = build1 (ADDR_EXPR, build_reference_type (type), val);
9829 129253046 : if (complain & tf_warning)
9830 121862186 : maybe_warn_parm_abi (type, cp_expr_loc_or_input_loc (val));
9831 :
9832 98055267 : if (complain & tf_warning)
9833 98055267 : warn_for_address_of_packed_member (type, val);
9834 :
9835 : /* gimplify_arg elides TARGET_EXPRs that initialize a function argument,
9836 : unless the initializer is a CONSTRUCTOR. In that case, we fail to
9837 : elide the copy anyway. See that function for more information. */
9838 7087548 : if (SIMPLE_TARGET_EXPR_P (val)
9839 135043819 : && TREE_CODE (TARGET_EXPR_INITIAL (val)) != CONSTRUCTOR)
9840 3304173 : set_target_expr_eliding (val);
9841 :
9842 129253046 : return val;
9843 : }
9844 :
9845 : /* Returns non-zero iff FN is a function with magic varargs, i.e. ones for
9846 : which just decay_conversion or no conversions at all should be done.
9847 : This is true for some builtins which don't act like normal functions.
9848 : Return 2 if just decay_conversion and removal of excess precision should
9849 : be done, 1 if just decay_conversion. Return 3 for special treatment of
9850 : the 3rd argument for __builtin_*_overflow_p. Return 4 for special
9851 : treatment of the 1st argument for
9852 : __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g. */
9853 :
9854 : int
9855 154728690 : magic_varargs_p (tree fn)
9856 : {
9857 154728690 : if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
9858 5550427 : switch (DECL_FUNCTION_CODE (fn))
9859 : {
9860 : case BUILT_IN_CLASSIFY_TYPE:
9861 : case BUILT_IN_CONSTANT_P:
9862 : case BUILT_IN_NEXT_ARG:
9863 : case BUILT_IN_VA_START:
9864 : return 1;
9865 :
9866 20842 : case BUILT_IN_ADD_OVERFLOW_P:
9867 20842 : case BUILT_IN_SUB_OVERFLOW_P:
9868 20842 : case BUILT_IN_MUL_OVERFLOW_P:
9869 20842 : return 3;
9870 :
9871 181024 : case BUILT_IN_ISFINITE:
9872 181024 : case BUILT_IN_ISINF:
9873 181024 : case BUILT_IN_ISINF_SIGN:
9874 181024 : case BUILT_IN_ISNAN:
9875 181024 : case BUILT_IN_ISNORMAL:
9876 181024 : case BUILT_IN_FPCLASSIFY:
9877 181024 : return 2;
9878 :
9879 93347 : case BUILT_IN_CLZG:
9880 93347 : case BUILT_IN_CTZG:
9881 93347 : case BUILT_IN_CLRSBG:
9882 93347 : case BUILT_IN_FFSG:
9883 93347 : case BUILT_IN_PARITYG:
9884 93347 : case BUILT_IN_POPCOUNTG:
9885 93347 : return 4;
9886 :
9887 5060534 : default:
9888 5060534 : return lookup_attribute ("type generic",
9889 10121068 : TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
9890 : }
9891 :
9892 : return 0;
9893 : }
9894 :
9895 : /* Returns the decl of the dispatcher function if FN is a function version. */
9896 :
9897 : tree
9898 240 : get_function_version_dispatcher (tree fn)
9899 : {
9900 240 : tree dispatcher_decl = NULL;
9901 :
9902 240 : if (DECL_LOCAL_DECL_P (fn))
9903 9 : fn = DECL_LOCAL_DECL_ALIAS (fn);
9904 :
9905 240 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
9906 : && DECL_FUNCTION_VERSIONED (fn));
9907 :
9908 240 : gcc_assert (targetm.get_function_versions_dispatcher);
9909 240 : dispatcher_decl = targetm.get_function_versions_dispatcher (fn);
9910 :
9911 240 : if (dispatcher_decl == NULL)
9912 : {
9913 9 : error_at (input_location, "use of multiversioned function "
9914 : "without a default");
9915 9 : return NULL;
9916 : }
9917 :
9918 231 : retrofit_lang_decl (dispatcher_decl);
9919 231 : gcc_assert (dispatcher_decl != NULL);
9920 : return dispatcher_decl;
9921 : }
9922 :
9923 : /* fn is a function version dispatcher that is marked used. Mark all the
9924 : semantically identical function versions it will dispatch as used. */
9925 :
9926 : void
9927 159 : mark_versions_used (tree fn)
9928 : {
9929 159 : struct cgraph_node *node;
9930 159 : struct cgraph_function_version_info *node_v;
9931 159 : struct cgraph_function_version_info *it_v;
9932 :
9933 159 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9934 :
9935 159 : node = cgraph_node::get (fn);
9936 159 : if (node == NULL)
9937 : return;
9938 :
9939 159 : gcc_assert (node->dispatcher_function);
9940 :
9941 159 : node_v = node->function_version ();
9942 159 : if (node_v == NULL)
9943 : return;
9944 :
9945 : /* All semantically identical versions are chained. Traverse and mark each
9946 : one of them as used. */
9947 159 : it_v = node_v->next;
9948 1143 : while (it_v != NULL)
9949 : {
9950 984 : mark_used (it_v->this_node->decl);
9951 984 : it_v = it_v->next;
9952 : }
9953 : }
9954 :
9955 : /* Build a call to "the copy constructor" for the type of A, even if it
9956 : wouldn't be selected by normal overload resolution. Used for
9957 : diagnostics. */
9958 :
9959 : static tree
9960 3 : call_copy_ctor (tree a, tsubst_flags_t complain)
9961 : {
9962 3 : tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (a));
9963 3 : tree binfo = TYPE_BINFO (ctype);
9964 3 : tree copy = get_copy_ctor (ctype, complain);
9965 3 : copy = build_baselink (binfo, binfo, copy, NULL_TREE);
9966 3 : tree ob = build_dummy_object (ctype);
9967 3 : releasing_vec args (make_tree_vector_single (a));
9968 3 : tree r = build_new_method_call (ob, copy, &args, NULL_TREE,
9969 : LOOKUP_NORMAL, NULL, complain);
9970 3 : return r;
9971 3 : }
9972 :
9973 : /* Return the base constructor corresponding to COMPLETE_CTOR or NULL_TREE. */
9974 :
9975 : static tree
9976 42 : base_ctor_for (tree complete_ctor)
9977 : {
9978 42 : tree clone;
9979 42 : FOR_EACH_CLONE (clone, DECL_CLONED_FUNCTION (complete_ctor))
9980 42 : if (DECL_BASE_CONSTRUCTOR_P (clone))
9981 : return clone;
9982 : return NULL_TREE;
9983 : }
9984 :
9985 : /* Try to make EXP suitable to be used as the initializer for a base subobject,
9986 : and return whether we were successful. EXP must have already been cleared
9987 : by unsafe_copy_elision_p{,_opt}. */
9988 :
9989 : static bool
9990 213 : make_base_init_ok (tree exp)
9991 : {
9992 213 : if (TREE_CODE (exp) == TARGET_EXPR)
9993 213 : exp = TARGET_EXPR_INITIAL (exp);
9994 216 : while (TREE_CODE (exp) == COMPOUND_EXPR)
9995 3 : exp = TREE_OPERAND (exp, 1);
9996 213 : if (TREE_CODE (exp) == COND_EXPR)
9997 : {
9998 3 : bool ret = make_base_init_ok (TREE_OPERAND (exp, 2));
9999 3 : if (tree op1 = TREE_OPERAND (exp, 1))
10000 : {
10001 3 : bool r1 = make_base_init_ok (op1);
10002 : /* If unsafe_copy_elision_p was false, the arms should match. */
10003 3 : gcc_assert (r1 == ret);
10004 : }
10005 3 : return ret;
10006 : }
10007 210 : if (TREE_CODE (exp) != AGGR_INIT_EXPR)
10008 : /* A trivial copy is OK. */
10009 : return true;
10010 60 : if (!AGGR_INIT_VIA_CTOR_P (exp))
10011 : /* unsafe_copy_elision_p_opt must have said this is OK. */
10012 : return true;
10013 42 : tree fn = cp_get_callee_fndecl_nofold (exp);
10014 42 : if (DECL_BASE_CONSTRUCTOR_P (fn))
10015 : return true;
10016 42 : gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (fn));
10017 42 : fn = base_ctor_for (fn);
10018 42 : if (!fn || DECL_HAS_VTT_PARM_P (fn))
10019 : /* The base constructor has more parameters, so we can't just change the
10020 : call target. It would be possible to splice in the appropriate
10021 : arguments, but probably not worth the complexity. */
10022 : return false;
10023 33 : mark_used (fn);
10024 33 : AGGR_INIT_EXPR_FN (exp) = build_address (fn);
10025 33 : return true;
10026 : }
10027 :
10028 : /* Return 2 if T refers to a base, 1 if a potentially-overlapping field,
10029 : neither of which can be used for return by invisible reference. We avoid
10030 : doing C++17 mandatory copy elision for either of these cases.
10031 :
10032 : This returns non-zero even if the type of T has no tail padding that other
10033 : data could be allocated into, because that depends on the particular ABI.
10034 : unsafe_copy_elision_p_opt does consider whether there is padding. */
10035 :
10036 : int
10037 39261940 : unsafe_return_slot_p (tree t)
10038 : {
10039 : /* Check empty bases separately, they don't have fields. */
10040 39261940 : if (is_empty_base_ref (t))
10041 : return 2;
10042 :
10043 : /* A delegating constructor might be used to initialize a base. */
10044 38806881 : if (current_function_decl
10045 52613660 : && DECL_CONSTRUCTOR_P (current_function_decl)
10046 42704933 : && (t == current_class_ref
10047 3733758 : || tree_strip_nop_conversions (t) == current_class_ptr))
10048 212658 : return 2;
10049 :
10050 38594223 : STRIP_NOPS (t);
10051 38594223 : if (TREE_CODE (t) == ADDR_EXPR)
10052 62300 : t = TREE_OPERAND (t, 0);
10053 38594223 : if (TREE_CODE (t) == COMPONENT_REF)
10054 3274854 : t = TREE_OPERAND (t, 1);
10055 38594223 : if (TREE_CODE (t) != FIELD_DECL)
10056 : return false;
10057 3314569 : if (!CLASS_TYPE_P (TREE_TYPE (t)))
10058 : /* The middle-end will do the right thing for scalar types. */
10059 : return false;
10060 2886558 : if (DECL_FIELD_IS_BASE (t))
10061 : return 2;
10062 2108100 : if (lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (t)))
10063 : return 1;
10064 : return 0;
10065 : }
10066 :
10067 : /* True IFF EXP is a prvalue that represents return by invisible reference. */
10068 :
10069 : static bool
10070 267491 : init_by_return_slot_p (tree exp)
10071 : {
10072 : /* Copy elision only happens with a TARGET_EXPR. */
10073 267494 : if (TREE_CODE (exp) != TARGET_EXPR)
10074 : return false;
10075 14242 : tree init = TARGET_EXPR_INITIAL (exp);
10076 : /* build_compound_expr pushes COMPOUND_EXPR inside TARGET_EXPR. */
10077 14248 : while (TREE_CODE (init) == COMPOUND_EXPR)
10078 6 : init = TREE_OPERAND (init, 1);
10079 14242 : if (TREE_CODE (init) == COND_EXPR)
10080 : {
10081 : /* We'll end up copying from each of the arms of the COND_EXPR directly
10082 : into the target, so look at them. */
10083 6 : if (tree op = TREE_OPERAND (init, 1))
10084 6 : if (init_by_return_slot_p (op))
10085 : return true;
10086 3 : return init_by_return_slot_p (TREE_OPERAND (init, 2));
10087 : }
10088 14236 : return (TREE_CODE (init) == AGGR_INIT_EXPR
10089 14236 : && !AGGR_INIT_VIA_CTOR_P (init));
10090 : }
10091 :
10092 : /* We can't elide a copy from a function returning by value to a
10093 : potentially-overlapping subobject, as the callee might clobber tail padding.
10094 : Return true iff this could be that case.
10095 :
10096 : Places that use this function (or _opt) to decide to elide a copy should
10097 : probably use make_safe_copy_elision instead. */
10098 :
10099 : bool
10100 2135492 : unsafe_copy_elision_p (tree target, tree exp)
10101 : {
10102 2135492 : return unsafe_return_slot_p (target) && init_by_return_slot_p (exp);
10103 : }
10104 :
10105 : /* As above, but for optimization allow more cases that are actually safe. */
10106 :
10107 : static bool
10108 7470258 : unsafe_copy_elision_p_opt (tree target, tree exp)
10109 : {
10110 7470258 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
10111 : /* It's safe to elide the copy for a class with no tail padding. */
10112 7470258 : if (!is_empty_class (type)
10113 7470258 : && tree_int_cst_equal (TYPE_SIZE (type), CLASSTYPE_SIZE (type)))
10114 : return false;
10115 2095777 : return unsafe_copy_elision_p (target, exp);
10116 : }
10117 :
10118 : /* Try to make EXP suitable to be used as the initializer for TARGET,
10119 : and return whether we were successful. */
10120 :
10121 : bool
10122 23 : make_safe_copy_elision (tree target, tree exp)
10123 : {
10124 23 : int uns = unsafe_return_slot_p (target);
10125 23 : if (!uns)
10126 : return true;
10127 23 : if (init_by_return_slot_p (exp))
10128 : return false;
10129 23 : if (uns == 1)
10130 : return true;
10131 19 : return make_base_init_ok (exp);
10132 : }
10133 :
10134 : /* True IFF the result of the conversion C is a prvalue. */
10135 :
10136 : static bool
10137 13157312 : conv_is_prvalue (conversion *c)
10138 : {
10139 13157312 : if (c->kind == ck_rvalue)
10140 : return true;
10141 13157312 : if (c->kind == ck_base && c->need_temporary_p)
10142 : return true;
10143 13157312 : if (c->kind == ck_user && !TYPE_REF_P (c->type))
10144 : return true;
10145 13027729 : if (c->kind == ck_identity && c->u.expr
10146 12791899 : && TREE_CODE (c->u.expr) == TARGET_EXPR)
10147 56 : return true;
10148 :
10149 : return false;
10150 : }
10151 :
10152 : /* True iff C is a conversion that binds a reference to a prvalue. */
10153 :
10154 : static bool
10155 13158368 : conv_binds_ref_to_prvalue (conversion *c)
10156 : {
10157 13158368 : if (c->kind != ck_ref_bind)
10158 : return false;
10159 13158368 : if (c->need_temporary_p)
10160 : return true;
10161 :
10162 13157312 : return conv_is_prvalue (next_conversion (c));
10163 : }
10164 :
10165 : /* True iff EXPR represents a (subobject of a) temporary. */
10166 :
10167 : static bool
10168 14091 : expr_represents_temporary_p (tree expr)
10169 : {
10170 14195 : while (handled_component_p (expr))
10171 104 : expr = TREE_OPERAND (expr, 0);
10172 14091 : return TREE_CODE (expr) == TARGET_EXPR;
10173 : }
10174 :
10175 : /* True iff C is a conversion that binds a reference to a temporary.
10176 : This is a superset of conv_binds_ref_to_prvalue: here we're also
10177 : interested in xvalues. */
10178 :
10179 : static bool
10180 13365 : conv_binds_ref_to_temporary (conversion *c)
10181 : {
10182 13365 : if (conv_binds_ref_to_prvalue (c))
10183 : return true;
10184 12938 : if (c->kind != ck_ref_bind)
10185 : return false;
10186 12938 : c = next_conversion (c);
10187 : /* This is the case for
10188 : struct Base {};
10189 : struct Derived : Base {};
10190 : const Base& b(Derived{});
10191 : where we bind 'b' to the Base subobject of a temporary object of type
10192 : Derived. The subobject is an xvalue; the whole object is a prvalue.
10193 :
10194 : The ck_base doesn't have to be present for cases like X{}.m. */
10195 12938 : if (c->kind == ck_base)
10196 8 : c = next_conversion (c);
10197 12870 : if (c->kind == ck_identity && c->u.expr
10198 25808 : && expr_represents_temporary_p (c->u.expr))
10199 : return true;
10200 : return false;
10201 : }
10202 :
10203 : /* Return tristate::TS_TRUE if converting EXPR to a reference type TYPE binds
10204 : the reference to a temporary. Return tristate::TS_FALSE if converting
10205 : EXPR to a reference type TYPE doesn't bind the reference to a temporary. If
10206 : the conversion is invalid or bad, return tristate::TS_UNKNOWN. DIRECT_INIT_P
10207 : says whether the conversion should be done in direct- or copy-initialization
10208 : context. */
10209 :
10210 : tristate
10211 13866 : ref_conv_binds_to_temporary (tree type, tree expr, bool direct_init_p/*=false*/)
10212 : {
10213 13866 : gcc_assert (TYPE_REF_P (type));
10214 :
10215 13866 : conversion_obstack_sentinel cos;
10216 :
10217 13866 : const int flags = direct_init_p ? LOOKUP_NORMAL : LOOKUP_IMPLICIT;
10218 13866 : conversion *conv = implicit_conversion (type, TREE_TYPE (expr), expr,
10219 : /*c_cast_p=*/false, flags, tf_none);
10220 13866 : if (!conv || conv->bad_p)
10221 501 : return tristate::unknown ();
10222 :
10223 13365 : if (conv_binds_ref_to_temporary (conv))
10224 : {
10225 : /* Actually perform the conversion to check access control. */
10226 433 : if (convert_like (conv, expr, tf_none) != error_mark_node)
10227 421 : return tristate (true);
10228 : else
10229 12 : return tristate::unknown ();
10230 : }
10231 :
10232 12932 : return tristate (false);
10233 13866 : }
10234 :
10235 : /* Call the trivial destructor for INSTANCE, which can be either an lvalue of
10236 : class type or a pointer to class type. If NO_PTR_DEREF is true and
10237 : INSTANCE has pointer type, clobber the pointer rather than what it points
10238 : to. */
10239 :
10240 : tree
10241 3212568 : build_trivial_dtor_call (tree instance, bool no_ptr_deref)
10242 : {
10243 3212568 : gcc_assert (!is_dummy_object (instance));
10244 :
10245 3212568 : if (!flag_lifetime_dse)
10246 : {
10247 58 : no_clobber:
10248 77 : return fold_convert (void_type_node, instance);
10249 : }
10250 :
10251 3254711 : if (INDIRECT_TYPE_P (TREE_TYPE (instance))
10252 3212510 : && (!no_ptr_deref || TYPE_REF_P (TREE_TYPE (instance))))
10253 : {
10254 3059788 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
10255 19 : goto no_clobber;
10256 3059769 : instance = cp_build_fold_indirect_ref (instance);
10257 : }
10258 :
10259 : /* A trivial destructor should still clobber the object. */
10260 3212491 : tree clobber = build_clobber (TREE_TYPE (instance), CLOBBER_OBJECT_END);
10261 3212491 : return build2 (MODIFY_EXPR, void_type_node,
10262 3212491 : instance, clobber);
10263 : }
10264 :
10265 : /* Return true if in an immediate function context, or an unevaluated operand,
10266 : or a default argument/member initializer, or a subexpression of an immediate
10267 : invocation. */
10268 :
10269 : bool
10270 9940755 : in_immediate_context ()
10271 : {
10272 9940755 : return (cp_unevaluated_operand != 0
10273 8746945 : || (current_function_decl != NULL_TREE
10274 14457538 : && DECL_IMMEDIATE_FUNCTION_P (current_function_decl))
10275 : /* DR 2631: default args and DMI aren't immediately evaluated.
10276 : Return true here so immediate_invocation_p returns false. */
10277 8528232 : || current_binding_level->kind == sk_function_parms
10278 8526889 : || current_binding_level->kind == sk_template_parms
10279 8494721 : || parsing_nsdmi ()
10280 18433754 : || in_consteval_if_p);
10281 : }
10282 :
10283 : /* Return true if a call to FN with number of arguments NARGS
10284 : is an immediate invocation. */
10285 :
10286 : bool
10287 193660626 : immediate_invocation_p (tree fn)
10288 : {
10289 193660626 : return (TREE_CODE (fn) == FUNCTION_DECL
10290 193660626 : && DECL_IMMEDIATE_FUNCTION_P (fn)
10291 196264045 : && !in_immediate_context ());
10292 : }
10293 :
10294 : /* Subroutine of the various build_*_call functions. Overload resolution
10295 : has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
10296 : ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
10297 : bitmask of various LOOKUP_* flags which apply to the call itself. */
10298 :
10299 : static tree
10300 209786146 : build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
10301 : {
10302 209786146 : tree fn = cand->fn;
10303 209786146 : const vec<tree, va_gc> *args = cand->args;
10304 209786146 : tree first_arg = cand->first_arg;
10305 209786146 : conversion **convs = cand->convs;
10306 209786146 : tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
10307 209786146 : int parmlen;
10308 209786146 : tree val;
10309 209786146 : int nargs;
10310 209786146 : tree *argarray;
10311 209786146 : bool already_used = false;
10312 :
10313 : /* In a template, there is no need to perform all of the work that
10314 : is normally done. We are only interested in the type of the call
10315 : expression, i.e., the return type of the function. Any semantic
10316 : errors will be deferred until the template is instantiated. */
10317 209786146 : if (processing_template_decl)
10318 : {
10319 29509226 : if (undeduced_auto_decl (fn))
10320 11387 : mark_used (fn, complain);
10321 : else
10322 : /* Otherwise set TREE_USED for the benefit of -Wunused-function.
10323 : See PR80598. */
10324 29497839 : TREE_USED (fn) = 1;
10325 :
10326 29509226 : tree return_type = TREE_TYPE (TREE_TYPE (fn));
10327 29509226 : tree callee;
10328 29509226 : if (first_arg == NULL_TREE)
10329 : {
10330 21534644 : callee = build_addr_func (fn, complain);
10331 21534644 : if (callee == error_mark_node)
10332 : return error_mark_node;
10333 : }
10334 : else
10335 : {
10336 7974582 : callee = build_baselink (cand->conversion_path, cand->access_path,
10337 : fn, NULL_TREE);
10338 7974582 : callee = build_min (COMPONENT_REF, TREE_TYPE (fn),
10339 : first_arg, callee, NULL_TREE);
10340 : }
10341 :
10342 29509226 : tree expr = build_call_vec (return_type, callee, args);
10343 29509226 : SET_EXPR_LOCATION (expr, input_location);
10344 29509226 : if (TREE_THIS_VOLATILE (fn) && cfun)
10345 2476972 : current_function_returns_abnormally = 1;
10346 29509226 : if (TREE_DEPRECATED (fn)
10347 29509226 : && warning_suppressed_at (input_location,
10348 : OPT_Wdeprecated_declarations))
10349 : /* Make the expr consistent with the location. */
10350 19220 : TREE_NO_WARNING (expr) = true;
10351 29509226 : if (immediate_invocation_p (fn))
10352 : {
10353 193517 : tree obj_arg = NULL_TREE;
10354 387034 : if (DECL_CONSTRUCTOR_P (fn))
10355 3 : obj_arg = first_arg;
10356 : /* Look through *(const T *)&obj. */
10357 3 : if (obj_arg && INDIRECT_REF_P (obj_arg))
10358 : {
10359 3 : tree addr = TREE_OPERAND (obj_arg, 0);
10360 3 : STRIP_NOPS (addr);
10361 3 : if (TREE_CODE (addr) == ADDR_EXPR)
10362 : {
10363 0 : tree typeo = TREE_TYPE (obj_arg);
10364 0 : tree typei = TREE_TYPE (TREE_OPERAND (addr, 0));
10365 0 : if (same_type_ignoring_top_level_qualifiers_p (typeo, typei))
10366 0 : obj_arg = TREE_OPERAND (addr, 0);
10367 : }
10368 : }
10369 193517 : fold_non_dependent_expr (expr, complain,
10370 : /*manifestly_const_eval=*/true,
10371 : obj_arg);
10372 : }
10373 29509226 : return convert_from_reference (expr);
10374 : }
10375 :
10376 : /* Give any warnings we noticed during overload resolution. */
10377 180276920 : if (cand->warnings && (complain & tf_warning))
10378 : {
10379 : struct candidate_warning *w;
10380 98 : for (w = cand->warnings; w; w = w->next)
10381 49 : joust (cand, w->loser, 1, complain);
10382 : }
10383 :
10384 : /* Core issue 2327: P0135 doesn't say how to handle the case where the
10385 : argument to the copy constructor ends up being a prvalue after
10386 : conversion. Let's do the normal processing, but pretend we aren't
10387 : actually using the copy constructor. */
10388 180276920 : bool force_elide = false;
10389 180276920 : if (cxx_dialect >= cxx17
10390 178273526 : && cand->num_convs == 1
10391 101583414 : && DECL_COMPLETE_CONSTRUCTOR_P (fn)
10392 36390996 : && (DECL_COPY_CONSTRUCTOR_P (fn)
10393 19906372 : || DECL_MOVE_CONSTRUCTOR_P (fn))
10394 13211648 : && !unsafe_return_slot_p (first_arg)
10395 193421869 : && conv_binds_ref_to_prvalue (convs[0]))
10396 : {
10397 130265 : force_elide = true;
10398 130265 : goto not_really_used;
10399 : }
10400 :
10401 : /* OK, we're actually calling this inherited constructor; set its deletedness
10402 : appropriately. We can get away with doing this here because calling is
10403 : the only way to refer to a constructor. */
10404 360293310 : if (DECL_INHERITED_CTOR (fn)
10405 27063645 : && !deduce_inheriting_ctor (fn))
10406 : {
10407 2 : if (complain & tf_error)
10408 2 : mark_used (fn);
10409 2 : return error_mark_node;
10410 : }
10411 :
10412 : /* Make =delete work with SFINAE. */
10413 180146653 : if (DECL_DELETED_FN (fn))
10414 : {
10415 994610 : if (complain & tf_error)
10416 : {
10417 2260 : mark_used (fn);
10418 2260 : if (cand->next)
10419 : {
10420 2061 : if (flag_diagnostics_all_candidates)
10421 9 : print_z_candidates (input_location, cand, /*only_viable_p=*/false);
10422 : else
10423 2052 : inform (input_location,
10424 : "use %<-fdiagnostics-all-candidates%> to display "
10425 : "considered candidates");
10426 : }
10427 : }
10428 994610 : return error_mark_node;
10429 : }
10430 :
10431 179152043 : if (DECL_FUNCTION_MEMBER_P (fn))
10432 : {
10433 99332106 : tree access_fn;
10434 : /* If FN is a template function, two cases must be considered.
10435 : For example:
10436 :
10437 : struct A {
10438 : protected:
10439 : template <class T> void f();
10440 : };
10441 : template <class T> struct B {
10442 : protected:
10443 : void g();
10444 : };
10445 : struct C : A, B<int> {
10446 : using A::f; // #1
10447 : using B<int>::g; // #2
10448 : };
10449 :
10450 : In case #1 where `A::f' is a member template, DECL_ACCESS is
10451 : recorded in the primary template but not in its specialization.
10452 : We check access of FN using its primary template.
10453 :
10454 : In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
10455 : because it is a member of class template B, DECL_ACCESS is
10456 : recorded in the specialization `B<int>::g'. We cannot use its
10457 : primary template because `B<T>::g' and `B<int>::g' may have
10458 : different access. */
10459 99332106 : if (DECL_TEMPLATE_INFO (fn)
10460 99332106 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
10461 7243028 : access_fn = DECL_TI_TEMPLATE (fn);
10462 : else
10463 : access_fn = fn;
10464 99332106 : if (!perform_or_defer_access_check (cand->access_path, access_fn,
10465 : fn, complain))
10466 20337 : return error_mark_node;
10467 : }
10468 :
10469 : /* If we're checking for implicit delete, don't bother with argument
10470 : conversions. */
10471 179131706 : if (flags & LOOKUP_SPECULATIVE)
10472 : {
10473 24530687 : if (cand->viable == 1)
10474 : return fn;
10475 121 : else if (!(complain & tf_error))
10476 : /* Reject bad conversions now. */
10477 106 : return error_mark_node;
10478 : /* else continue to get conversion error. */
10479 : }
10480 :
10481 154601019 : not_really_used:
10482 :
10483 : /* N3276 magic doesn't apply to nested calls. */
10484 154731299 : tsubst_flags_t decltype_flag = (complain & tf_decltype);
10485 154731299 : complain &= ~tf_decltype;
10486 : /* No-Cleanup doesn't apply to nested calls either. */
10487 154731299 : tsubst_flags_t no_cleanup_complain = complain;
10488 154731299 : complain &= ~tf_no_cleanup;
10489 :
10490 : /* Find maximum size of vector to hold converted arguments. */
10491 154731299 : parmlen = list_length (parm);
10492 290179763 : nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0);
10493 154731299 : if (parmlen > nargs)
10494 : nargs = parmlen;
10495 154731299 : argarray = XALLOCAVEC (tree, nargs);
10496 :
10497 309462595 : in_consteval_if_p_temp_override icip;
10498 : /* If the call is immediate function invocation, make sure
10499 : taking address of immediate functions is allowed in its arguments. */
10500 154731299 : if (immediate_invocation_p (STRIP_TEMPLATE (fn)))
10501 1367996 : in_consteval_if_p = true;
10502 :
10503 154731299 : int argarray_size = 0;
10504 154731299 : unsigned int arg_index = 0;
10505 154731299 : int conv_index = 0;
10506 154731299 : int param_index = 0;
10507 154731299 : tree parmd = DECL_ARGUMENTS (fn);
10508 :
10509 217287034 : auto consume_object_arg = [&arg_index, &first_arg, args]()
10510 : {
10511 62555735 : if (!first_arg)
10512 0 : return (*args)[arg_index++];
10513 62555735 : tree object_arg = first_arg;
10514 62555735 : first_arg = NULL_TREE;
10515 62555735 : return object_arg;
10516 154731299 : };
10517 :
10518 : /* The implicit parameters to a constructor are not considered by overload
10519 : resolution, and must be of the proper type. */
10520 154731299 : if (DECL_CONSTRUCTOR_P (fn))
10521 : {
10522 19097032 : tree object_arg = consume_object_arg ();
10523 19097032 : argarray[argarray_size++] = build_this (object_arg);
10524 19097032 : parm = TREE_CHAIN (parm);
10525 19097032 : if (parmd)
10526 19097032 : parmd = DECL_CHAIN (parmd);
10527 : /* We should never try to call the abstract constructor. */
10528 19097032 : gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
10529 :
10530 19097032 : if (DECL_HAS_VTT_PARM_P (fn))
10531 : {
10532 17030 : argarray[argarray_size++] = (*args)[arg_index];
10533 17030 : ++arg_index;
10534 17030 : parm = TREE_CHAIN (parm);
10535 17030 : if (parmd)
10536 17030 : parmd = DECL_CHAIN (parmd);
10537 : }
10538 : }
10539 : /* Bypass access control for 'this' parameter. */
10540 135634267 : else if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
10541 : {
10542 43452559 : tree arg = build_this (consume_object_arg ());
10543 43452559 : tree argtype = TREE_TYPE (arg);
10544 :
10545 43452559 : if (arg == error_mark_node)
10546 : return error_mark_node;
10547 43452556 : if (convs[conv_index++]->bad_p)
10548 : {
10549 1097 : if (complain & tf_error)
10550 : {
10551 103 : auto_diagnostic_group d;
10552 103 : if (permerror (input_location, "passing %qT as %<this%> "
10553 : "argument discards qualifiers",
10554 103 : TREE_TYPE (argtype)))
10555 100 : inform (DECL_SOURCE_LOCATION (fn), " in call to %qD", fn);
10556 103 : }
10557 : else
10558 : return error_mark_node;
10559 : }
10560 :
10561 : /* The class where FN is defined. */
10562 43451562 : tree ctx = DECL_CONTEXT (fn);
10563 :
10564 : /* See if the function member or the whole class type is declared
10565 : final and the call can be devirtualized. */
10566 43451562 : if (DECL_FINAL_P (fn) || CLASSTYPE_FINAL (ctx))
10567 125721 : flags |= LOOKUP_NONVIRTUAL;
10568 :
10569 : /* [class.mfct.non-static]: If a non-static member function of a class
10570 : X is called for an object that is not of type X, or of a type
10571 : derived from X, the behavior is undefined.
10572 :
10573 : So we can assume that anything passed as 'this' is non-null, and
10574 : optimize accordingly. */
10575 : /* Check that the base class is accessible. */
10576 43451562 : if (!accessible_base_p (TREE_TYPE (argtype),
10577 43451562 : BINFO_TYPE (cand->conversion_path), true))
10578 : {
10579 33 : if (complain & tf_error)
10580 30 : error ("%qT is not an accessible base of %qT",
10581 30 : BINFO_TYPE (cand->conversion_path),
10582 30 : TREE_TYPE (argtype));
10583 : else
10584 3 : return error_mark_node;
10585 : }
10586 : /* If fn was found by a using declaration, the conversion path
10587 : will be to the derived class, not the base declaring fn. We
10588 : must convert to the base. */
10589 43451559 : tree base_binfo = cand->conversion_path;
10590 43451559 : if (BINFO_TYPE (base_binfo) != ctx)
10591 : {
10592 114141 : base_binfo = lookup_base (base_binfo, ctx, ba_unique, NULL, complain);
10593 114141 : if (base_binfo == error_mark_node)
10594 : return error_mark_node;
10595 : }
10596 :
10597 : /* If we know the dynamic type of the object, look up the final overrider
10598 : in the BINFO. */
10599 44311851 : if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
10600 43926982 : && resolves_to_fixed_type_p (arg))
10601 : {
10602 1069 : tree ov = lookup_vfn_in_binfo (DECL_VINDEX (fn), base_binfo);
10603 :
10604 : /* And unwind base_binfo to match. If we don't find the type we're
10605 : looking for in BINFO_INHERITANCE_CHAIN, we're looking at diamond
10606 : inheritance; for now do a normal virtual call in that case. */
10607 1069 : tree octx = DECL_CONTEXT (ov);
10608 1069 : tree obinfo = base_binfo;
10609 2213 : while (obinfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (obinfo), octx))
10610 39 : obinfo = BINFO_INHERITANCE_CHAIN (obinfo);
10611 1069 : if (obinfo)
10612 : {
10613 1066 : fn = ov;
10614 1066 : base_binfo = obinfo;
10615 1066 : flags |= LOOKUP_NONVIRTUAL;
10616 : }
10617 : }
10618 :
10619 43451553 : tree converted_arg = build_base_path (PLUS_EXPR, arg,
10620 : base_binfo, 1, complain);
10621 :
10622 43451553 : argarray[argarray_size++] = converted_arg;
10623 43451553 : parm = TREE_CHAIN (parm);
10624 43451553 : if (parmd)
10625 43451553 : parmd = DECL_CHAIN (parmd);
10626 : }
10627 :
10628 278376119 : auto handle_arg = [fn, flags](tree type,
10629 : tree arg,
10630 : int const param_index,
10631 : conversion *conv,
10632 : tsubst_flags_t const arg_complain)
10633 : {
10634 : /* Set user_conv_p on the argument conversions, so rvalue/base handling
10635 : knows not to allow any more UDCs. This needs to happen after we
10636 : process cand->warnings. */
10637 123645826 : if (flags & LOOKUP_NO_CONVERSION)
10638 3467018 : conv->user_conv_p = true;
10639 :
10640 123645826 : if (arg_complain & tf_warning)
10641 92846563 : maybe_warn_pessimizing_move (arg, type, /*return_p=*/false);
10642 :
10643 123645826 : tree val = convert_like_with_context (conv, arg, fn,
10644 : param_index, arg_complain);
10645 123645826 : val = convert_for_arg_passing (type, val, arg_complain);
10646 123645826 : return val;
10647 154730293 : };
10648 :
10649 279401562 : auto handle_indeterminate_arg = [](tree parmd, tree val)
10650 : {
10651 124671269 : if (parmd
10652 124671269 : && lookup_attribute (NULL, "indeterminate", DECL_ATTRIBUTES (parmd)))
10653 : {
10654 48 : STRIP_NOPS (val);
10655 48 : if (TREE_CODE (val) == ADDR_EXPR
10656 48 : && TREE_CODE (TREE_OPERAND (val, 0)) == TARGET_EXPR)
10657 : {
10658 48 : val = TARGET_EXPR_SLOT (TREE_OPERAND (val, 0));
10659 48 : if (auto_var_p (val) && DECL_ARTIFICIAL (val))
10660 : {
10661 48 : tree id = get_identifier ("indeterminate");
10662 48 : DECL_ATTRIBUTES (val)
10663 96 : = tree_cons (build_tree_list (NULL_TREE, id), NULL_TREE,
10664 48 : DECL_ATTRIBUTES (val));
10665 : }
10666 : }
10667 : }
10668 124671269 : };
10669 :
10670 154730293 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
10671 : {
10672 6144 : gcc_assert (cand->num_convs > 0);
10673 6144 : tree object_arg = consume_object_arg ();
10674 6144 : val = handle_arg (TREE_VALUE (parm),
10675 : object_arg,
10676 : param_index++,
10677 6144 : convs[conv_index++],
10678 : complain);
10679 :
10680 6144 : if (val == error_mark_node)
10681 : return error_mark_node;
10682 : else
10683 : {
10684 6032 : argarray[argarray_size++] = val;
10685 6032 : handle_indeterminate_arg (parmd, val);
10686 : }
10687 6032 : parm = TREE_CHAIN (parm);
10688 6032 : if (parmd)
10689 6032 : parmd = DECL_CHAIN (parmd);
10690 : }
10691 :
10692 154730181 : gcc_assert (first_arg == NULL_TREE);
10693 278368471 : for (; arg_index < vec_safe_length (args) && parm;
10694 123638290 : parm = TREE_CHAIN (parm), ++arg_index, ++param_index, ++conv_index)
10695 : {
10696 123639682 : tree current_arg = (*args)[arg_index];
10697 :
10698 : /* If the argument is NULL and used to (implicitly) instantiate a
10699 : template function (and bind one of the template arguments to
10700 : the type of 'long int'), we don't want to warn about passing NULL
10701 : to non-pointer argument.
10702 : For example, if we have this template function:
10703 :
10704 : template<typename T> void func(T x) {}
10705 :
10706 : we want to warn (when -Wconversion is enabled) in this case:
10707 :
10708 : void foo() {
10709 : func<int>(NULL);
10710 : }
10711 :
10712 : but not in this case:
10713 :
10714 : void foo() {
10715 : func(NULL);
10716 : }
10717 : */
10718 123639682 : bool conversion_warning = !(null_node_p (current_arg)
10719 46387 : && DECL_TEMPLATE_INFO (fn)
10720 61 : && cand->template_decl
10721 30 : && !cand->explicit_targs);
10722 :
10723 : /* Also don't warn about (x <=> y) < 0 (c++/100903). */
10724 123639667 : if (conversion_warning
10725 123639667 : && integer_zerop (current_arg)
10726 9809481 : && warn_zero_as_null_pointer_constant
10727 195 : && !warning_enabled_at (DECL_SOURCE_LOCATION (fn),
10728 195 : OPT_Wzero_as_null_pointer_constant))
10729 : conversion_warning = false;
10730 :
10731 135 : tsubst_flags_t const arg_complain
10732 123639562 : = conversion_warning ? complain : complain & ~tf_warning;
10733 :
10734 123639682 : val = handle_arg (TREE_VALUE (parm),
10735 : current_arg,
10736 : param_index,
10737 123639682 : convs[conv_index],
10738 : arg_complain);
10739 :
10740 123639682 : if (val == error_mark_node)
10741 : return error_mark_node;
10742 : else
10743 : {
10744 123638290 : argarray[argarray_size++] = val;
10745 123638290 : handle_indeterminate_arg (parmd, val);
10746 : }
10747 123638290 : if (parmd)
10748 113018841 : parmd = DECL_CHAIN (parmd);
10749 : }
10750 :
10751 : /* Default arguments */
10752 155755736 : for (; parm && parm != void_list_node;
10753 1026947 : parm = TREE_CHAIN (parm), param_index++)
10754 : {
10755 1027067 : if (TREE_VALUE (parm) == error_mark_node)
10756 : return error_mark_node;
10757 2054110 : val = convert_default_arg (TREE_VALUE (parm),
10758 1027055 : TREE_PURPOSE (parm),
10759 : fn, param_index,
10760 : complain);
10761 1027055 : if (val == error_mark_node)
10762 : return error_mark_node;
10763 1026947 : argarray[argarray_size++] = val;
10764 1026947 : handle_indeterminate_arg (parmd, val);
10765 1026947 : if (parmd)
10766 1026947 : parmd = DECL_CHAIN (parmd);
10767 : }
10768 :
10769 : /* Ellipsis */
10770 154728669 : int magic = magic_varargs_p (fn);
10771 157454922 : for (; arg_index < vec_safe_length (args); ++arg_index)
10772 : {
10773 2726274 : tree a = (*args)[arg_index];
10774 2726274 : if ((magic == 3 && arg_index == 2) || (magic == 4 && arg_index == 0))
10775 : {
10776 : /* Do no conversions for certain magic varargs. */
10777 114153 : a = mark_type_use (a);
10778 114153 : if (TREE_CODE (a) == FUNCTION_DECL && reject_gcc_builtin (a))
10779 0 : return error_mark_node;
10780 : }
10781 2612121 : else if (magic != 0)
10782 : {
10783 : /* Don't truncate excess precision to the semantic type. */
10784 1272108 : if (magic == 1 && TREE_CODE (a) == EXCESS_PRECISION_EXPR)
10785 84 : a = TREE_OPERAND (a, 0);
10786 : /* For other magic varargs only do decay_conversion. */
10787 1272108 : a = decay_conversion (a, complain);
10788 : }
10789 1340013 : else if (DECL_CONSTRUCTOR_P (fn)
10790 336 : && vec_safe_length (args) == 1
10791 1340173 : && same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (fn),
10792 160 : TREE_TYPE (a)))
10793 : {
10794 : /* Avoid infinite recursion trying to call A(...). */
10795 3 : if (complain & tf_error)
10796 : /* Try to call the actual copy constructor for a good error. */
10797 3 : call_copy_ctor (a, complain);
10798 3 : return error_mark_node;
10799 : }
10800 : else
10801 1340010 : a = convert_arg_to_ellipsis (a, complain);
10802 2726271 : if (a == error_mark_node)
10803 : return error_mark_node;
10804 2726253 : argarray[argarray_size++] = a;
10805 : }
10806 :
10807 154728648 : gcc_assert (argarray_size <= nargs);
10808 154728648 : nargs = argarray_size;
10809 154728648 : icip.reset ();
10810 :
10811 : /* Avoid performing argument transformation if warnings are disabled.
10812 : When tf_warning is set and at least one of the warnings is active
10813 : the check_function_arguments function might warn about something. */
10814 :
10815 154728648 : bool warned_p = false;
10816 154728648 : if ((complain & tf_warning)
10817 105552609 : && (warn_nonnull
10818 103597814 : || warn_format
10819 103597811 : || warn_suggest_attribute_format
10820 103597715 : || warn_restrict))
10821 : {
10822 1957141 : tree *fargs = (!nargs ? argarray
10823 1616051 : : (tree *) alloca (nargs * sizeof (tree)));
10824 5953886 : for (int j = 0; j < nargs; j++)
10825 : {
10826 : /* For -Wformat undo the implicit passing by hidden reference
10827 : done by convert_arg_to_ellipsis. */
10828 3996745 : if (TREE_CODE (argarray[j]) == ADDR_EXPR
10829 3996745 : && TYPE_REF_P (TREE_TYPE (argarray[j])))
10830 1488 : fargs[j] = TREE_OPERAND (argarray[j], 0);
10831 : else
10832 3995257 : fargs[j] = argarray[j];
10833 : }
10834 :
10835 1957141 : warned_p = check_function_arguments (input_location, fn, TREE_TYPE (fn),
10836 : nargs, fargs, NULL,
10837 : cp_comp_parm_types);
10838 : }
10839 :
10840 309457296 : if (DECL_INHERITED_CTOR (fn))
10841 : {
10842 : /* Check for passing ellipsis arguments to an inherited constructor. We
10843 : could handle this by open-coding the inherited constructor rather than
10844 : defining it, but let's not bother now. */
10845 60841 : if (!cp_unevaluated_operand
10846 60580 : && cand->num_convs
10847 60580 : && cand->convs[cand->num_convs-1]->ellipsis_p)
10848 : {
10849 15 : if (complain & tf_error)
10850 : {
10851 15 : sorry ("passing arguments to ellipsis of inherited constructor "
10852 : "%qD", cand->fn);
10853 15 : inform (DECL_SOURCE_LOCATION (cand->fn), "declared here");
10854 : }
10855 15 : return error_mark_node;
10856 : }
10857 :
10858 : /* A base constructor inheriting from a virtual base doesn't get the
10859 : inherited arguments, just this and __vtt. */
10860 60826 : if (ctor_omit_inherited_parms (fn))
10861 154728633 : nargs = 2;
10862 : }
10863 :
10864 : /* Avoid actually calling copy constructors and copy assignment operators,
10865 : if possible. */
10866 :
10867 154728633 : if (!force_elide
10868 154598618 : && (!flag_elide_constructors
10869 : /* It's unsafe to elide the operation when handling
10870 : a noexcept-expression, it may evaluate to the wrong
10871 : value (c++/53025, c++/96090). */
10872 154598414 : || cp_noexcept_operand != 0))
10873 : /* Do things the hard way. */;
10874 152257503 : else if (cand->num_convs == 1
10875 232807645 : && (DECL_COPY_CONSTRUCTOR_P (fn)
10876 151447458 : || DECL_MOVE_CONSTRUCTOR_P (fn)))
10877 : {
10878 7470258 : tree targ;
10879 7470258 : tree arg = argarray[num_artificial_parms_for (fn)];
10880 7470258 : tree fa = argarray[0];
10881 7470258 : bool trivial = trivial_fn_p (fn);
10882 :
10883 : /* Pull out the real argument, disregarding const-correctness. */
10884 7470258 : targ = arg;
10885 : /* Strip the reference binding for the constructor parameter. */
10886 0 : if (CONVERT_EXPR_P (targ)
10887 7470258 : && TYPE_REF_P (TREE_TYPE (targ)))
10888 7470258 : targ = TREE_OPERAND (targ, 0);
10889 : /* But don't strip any other reference bindings; binding a temporary to a
10890 : reference prevents copy elision. */
10891 12405824 : while ((CONVERT_EXPR_P (targ)
10892 10450172 : && !TYPE_REF_P (TREE_TYPE (targ)))
10893 26046533 : || TREE_CODE (targ) == NON_LVALUE_EXPR)
10894 9383936 : targ = TREE_OPERAND (targ, 0);
10895 7470258 : if (TREE_CODE (targ) == ADDR_EXPR)
10896 : {
10897 2219431 : targ = TREE_OPERAND (targ, 0);
10898 2219431 : if (!same_type_ignoring_top_level_qualifiers_p
10899 2219431 : (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
10900 : targ = NULL_TREE;
10901 : }
10902 : else
10903 : targ = NULL_TREE;
10904 :
10905 2218982 : if (targ)
10906 : arg = targ;
10907 : else
10908 5251276 : arg = cp_build_fold_indirect_ref (arg);
10909 :
10910 : /* In C++17 we shouldn't be copying a TARGET_EXPR except into a
10911 : potentially-overlapping subobject. */
10912 7470258 : if (CHECKING_P && cxx_dialect >= cxx17)
10913 7391839 : gcc_assert (TREE_CODE (arg) != TARGET_EXPR
10914 : || force_elide
10915 : /* It's from binding the ref parm to a packed field. */
10916 : || convs[0]->need_temporary_p
10917 : || seen_error ()
10918 : /* See unsafe_copy_elision_p. */
10919 : || unsafe_return_slot_p (fa));
10920 :
10921 7470258 : bool unsafe = unsafe_copy_elision_p_opt (fa, arg);
10922 7470258 : bool eliding_temp = (TREE_CODE (arg) == TARGET_EXPR && !unsafe);
10923 :
10924 : /* [class.copy]: the copy constructor is implicitly defined even if the
10925 : implementation elided its use. But don't warn about deprecation when
10926 : eliding a temporary, as then no copy is actually performed. */
10927 7470258 : warning_sentinel s (warn_deprecated_copy, eliding_temp);
10928 7470258 : if (force_elide)
10929 : /* The language says this isn't called. */;
10930 7340243 : else if (!trivial)
10931 : {
10932 1275644 : if (!mark_used (fn, complain) && !(complain & tf_error))
10933 0 : return error_mark_node;
10934 : already_used = true;
10935 : }
10936 : else
10937 6064599 : cp_handle_deprecated_or_unavailable (fn, complain);
10938 :
10939 190560 : if (eliding_temp && DECL_BASE_CONSTRUCTOR_P (fn)
10940 7470446 : && !make_base_init_ok (arg))
10941 : unsafe = true;
10942 :
10943 : /* If we're creating a temp and we already have one, don't create a
10944 : new one. If we're not creating a temp but we get one, use
10945 : INIT_EXPR to collapse the temp into our target. Otherwise, if the
10946 : ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
10947 : temp or an INIT_EXPR otherwise. */
10948 7470258 : if (is_dummy_object (fa))
10949 : {
10950 5458805 : if (TREE_CODE (arg) == TARGET_EXPR)
10951 : return arg;
10952 5298403 : else if (trivial)
10953 4765668 : return force_target_expr (DECL_CONTEXT (fn), arg, complain);
10954 : }
10955 2011453 : else if ((trivial || TREE_CODE (arg) == TARGET_EXPR)
10956 1274971 : && !unsafe)
10957 : {
10958 1274883 : tree to = cp_build_fold_indirect_ref (fa);
10959 1274883 : val = cp_build_init_expr (to, arg);
10960 1274883 : return val;
10961 : }
10962 7470258 : }
10963 144787245 : else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
10964 2993763 : && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR)
10965 146822406 : && trivial_fn_p (fn))
10966 : {
10967 1346491 : tree to = cp_build_fold_indirect_ref (argarray[0]);
10968 1346491 : tree type = TREE_TYPE (to);
10969 1346491 : tree as_base = CLASSTYPE_AS_BASE (type);
10970 1346491 : tree arg = argarray[1];
10971 1346491 : location_t loc = cp_expr_loc_or_input_loc (arg);
10972 :
10973 1346491 : if (is_really_empty_class (type, /*ignore_vptr*/true))
10974 : {
10975 : /* Avoid copying empty classes, but ensure op= returns an lvalue even
10976 : if the object argument isn't one. */
10977 227786 : to = force_lvalue (to, complain);
10978 227786 : val = build2 (COMPOUND_EXPR, type, arg, to);
10979 227786 : suppress_warning (val, OPT_Wunused);
10980 : }
10981 1118705 : else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
10982 : {
10983 855940 : if (is_std_init_list (type)
10984 855940 : && conv_binds_ref_to_prvalue (convs[1]))
10985 3 : warning_at (loc, OPT_Winit_list_lifetime,
10986 : "assignment from temporary %<initializer_list%> does "
10987 : "not extend the lifetime of the underlying array");
10988 855940 : arg = cp_build_fold_indirect_ref (arg);
10989 855940 : val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
10990 : }
10991 : else
10992 : {
10993 : /* We must only copy the non-tail padding parts. */
10994 262765 : tree arg0, arg2, t;
10995 262765 : tree array_type, alias_set;
10996 :
10997 262765 : arg2 = TYPE_SIZE_UNIT (as_base);
10998 : /* Ensure op= returns an lvalue even if the object argument isn't
10999 : one. */
11000 262765 : to = force_lvalue (to, complain);
11001 262765 : to = cp_stabilize_reference (to);
11002 262765 : arg0 = cp_build_addr_expr (to, complain);
11003 :
11004 262765 : array_type = build_array_type (unsigned_char_type_node,
11005 : build_index_type
11006 : (size_binop (MINUS_EXPR,
11007 : arg2, size_int (1))));
11008 262765 : alias_set = build_int_cst (build_pointer_type (type), 0);
11009 262765 : t = build2 (MODIFY_EXPR, void_type_node,
11010 : build2 (MEM_REF, array_type, arg0, alias_set),
11011 : build2 (MEM_REF, array_type, arg, alias_set));
11012 262765 : val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
11013 262765 : suppress_warning (val, OPT_Wunused);
11014 : }
11015 :
11016 1346491 : cp_handle_deprecated_or_unavailable (fn, complain);
11017 :
11018 1346491 : return val;
11019 : }
11020 143440754 : else if (trivial_fn_p (fn))
11021 : {
11022 7508118 : if (DECL_DESTRUCTOR_P (fn))
11023 3044022 : return build_trivial_dtor_call (argarray[0]);
11024 710037 : else if (default_ctor_p (fn))
11025 : {
11026 710037 : if (is_dummy_object (argarray[0]))
11027 265474 : return force_target_expr (DECL_CONTEXT (fn), void_node,
11028 265474 : no_cleanup_complain);
11029 : else
11030 444563 : return cp_build_fold_indirect_ref (argarray[0]);
11031 : }
11032 : }
11033 :
11034 143427130 : gcc_assert (!force_elide);
11035 :
11036 143427130 : if (!already_used
11037 143427130 : && !mark_used (fn, complain))
11038 788 : return error_mark_node;
11039 :
11040 : /* Warn if the built-in writes to an object of a non-trivial type. */
11041 143426339 : if (warn_class_memaccess
11042 1987654 : && vec_safe_length (args) >= 2
11043 144386659 : && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
11044 69612 : maybe_warn_class_memaccess (input_location, fn, args);
11045 :
11046 143426339 : if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
11047 : {
11048 474363 : tree t;
11049 474363 : tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
11050 474363 : DECL_CONTEXT (fn),
11051 : ba_any, NULL, complain);
11052 474363 : gcc_assert (binfo && binfo != error_mark_node);
11053 :
11054 474363 : argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
11055 : complain);
11056 474363 : if (TREE_SIDE_EFFECTS (argarray[0]))
11057 45584 : argarray[0] = save_expr (argarray[0]);
11058 474363 : t = build_pointer_type (TREE_TYPE (fn));
11059 474363 : fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
11060 474363 : TREE_TYPE (fn) = t;
11061 : }
11062 : else
11063 : {
11064 : /* If FN is marked deprecated or unavailable, then we've already
11065 : issued a diagnostic from mark_used above, so avoid redundantly
11066 : issuing another one from build_addr_func. */
11067 142951976 : auto w = make_temp_override (deprecated_state,
11068 142951976 : UNAVAILABLE_DEPRECATED_SUPPRESS);
11069 :
11070 142951976 : fn = build_addr_func (fn, complain);
11071 142951976 : if (fn == error_mark_node)
11072 0 : return error_mark_node;
11073 :
11074 : /* We're actually invoking the function. (Immediate functions get an
11075 : & when invoking it even though the user didn't use &.) */
11076 142951976 : ADDR_EXPR_DENOTES_CALL_P (fn) = true;
11077 142951976 : }
11078 :
11079 143426339 : tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag);
11080 143426339 : if (call == error_mark_node)
11081 : return call;
11082 143425488 : if (cand->flags & LOOKUP_LIST_INIT_CTOR)
11083 : {
11084 1932583 : tree c = extract_call_expr (call);
11085 : /* build_new_op will clear this when appropriate. */
11086 1932583 : CALL_EXPR_ORDERED_ARGS (c) = true;
11087 : }
11088 143425488 : if (warned_p)
11089 : {
11090 253 : tree c = extract_call_expr (call);
11091 253 : if (TREE_CODE (c) == CALL_EXPR)
11092 253 : suppress_warning (c /* Suppress all warnings. */);
11093 : }
11094 143425235 : else if (TREE_DEPRECATED (fn)
11095 143425235 : && warning_suppressed_at (input_location,
11096 : OPT_Wdeprecated_declarations))
11097 : {
11098 0 : tree c = extract_call_expr (call);
11099 0 : if (TREE_CODE (c) == CALL_EXPR)
11100 0 : TREE_NO_WARNING (c) = true;
11101 : }
11102 :
11103 : return call;
11104 : }
11105 :
11106 : namespace
11107 : {
11108 :
11109 : /* Return the DECL of the first non-static subobject of class TYPE
11110 : that satisfies the predicate PRED or null if none can be found. */
11111 :
11112 : template <class Predicate>
11113 : tree
11114 3556 : first_non_static_field (tree type, Predicate pred)
11115 : {
11116 3556 : if (!type || !CLASS_TYPE_P (type))
11117 : return NULL_TREE;
11118 :
11119 71398 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
11120 : {
11121 68436 : if (TREE_CODE (field) != FIELD_DECL)
11122 43330 : continue;
11123 25106 : if (TREE_STATIC (field))
11124 0 : continue;
11125 25106 : if (pred (field))
11126 : return field;
11127 : }
11128 :
11129 2962 : int i = 0;
11130 :
11131 3082 : for (tree base_binfo, binfo = TYPE_BINFO (type);
11132 3082 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11133 : {
11134 126 : tree base = TREE_TYPE (base_binfo);
11135 126 : if (pred (base))
11136 : return base;
11137 120 : if (tree field = first_non_static_field (base, pred))
11138 : return field;
11139 : }
11140 :
11141 : return NULL_TREE;
11142 : }
11143 :
11144 : struct NonPublicField
11145 : {
11146 24356 : bool operator() (const_tree t) const
11147 : {
11148 24356 : return DECL_P (t) && (TREE_PRIVATE (t) || TREE_PROTECTED (t));
11149 : }
11150 : };
11151 :
11152 : /* Return the DECL of the first non-public subobject of class TYPE
11153 : or null if none can be found. */
11154 :
11155 : static inline tree
11156 3262 : first_non_public_field (tree type)
11157 : {
11158 3262 : return first_non_static_field (type, NonPublicField ());
11159 : }
11160 :
11161 : struct NonTrivialField
11162 : {
11163 876 : bool operator() (const_tree t) const
11164 : {
11165 876 : return !trivial_type_p (DECL_P (t) ? TREE_TYPE (t) : t);
11166 : }
11167 : };
11168 :
11169 : /* Return the DECL of the first non-trivial subobject of class TYPE
11170 : or null if none can be found. */
11171 :
11172 : static inline tree
11173 174 : first_non_trivial_field (tree type)
11174 : {
11175 174 : return first_non_static_field (type, NonTrivialField ());
11176 : }
11177 :
11178 : } /* unnamed namespace */
11179 :
11180 : /* Return true if all copy and move assignment operator overloads for
11181 : class TYPE are trivial and at least one of them is not deleted and,
11182 : when ACCESS is set, accessible. Return false otherwise. Set
11183 : HASASSIGN to true when the TYPE has a (not necessarily trivial)
11184 : copy or move assignment. */
11185 :
11186 : static bool
11187 5021 : has_trivial_copy_assign_p (tree type, bool access, bool *hasassign)
11188 : {
11189 5021 : tree fns = get_class_binding (type, assign_op_identifier);
11190 5021 : bool all_trivial = true;
11191 :
11192 : /* Iterate over overloads of the assignment operator, checking
11193 : accessible copy assignments for triviality. */
11194 :
11195 16515 : for (tree f : ovl_range (fns))
11196 : {
11197 : /* Skip operators that aren't copy assignments. */
11198 8571 : if (!copy_fn_p (f))
11199 3046 : continue;
11200 :
11201 5525 : bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
11202 5813 : || accessible_p (TYPE_BINFO (type), f, true));
11203 :
11204 : /* Skip template assignment operators and deleted functions. */
11205 5525 : if (TREE_CODE (f) != FUNCTION_DECL || DECL_DELETED_FN (f))
11206 694 : continue;
11207 :
11208 4831 : if (accessible)
11209 4579 : *hasassign = true;
11210 :
11211 4579 : if (!accessible || !trivial_fn_p (f))
11212 : all_trivial = false;
11213 :
11214 : /* Break early when both properties have been determined. */
11215 4831 : if (*hasassign && !all_trivial)
11216 : break;
11217 : }
11218 :
11219 : /* Return true if they're all trivial and one of the expressions
11220 : TYPE() = TYPE() or TYPE() = (TYPE&)() is valid. */
11221 5021 : tree ref = cp_build_reference_type (type, false);
11222 5021 : return (all_trivial
11223 5021 : && (is_trivially_xible (MODIFY_EXPR, type, type)
11224 333 : || is_trivially_xible (MODIFY_EXPR, type, ref)));
11225 : }
11226 :
11227 : /* Return true if all copy and move ctor overloads for class TYPE are
11228 : trivial and at least one of them is not deleted and, when ACCESS is
11229 : set, accessible. Return false otherwise. Set each element of HASCTOR[]
11230 : to true when the TYPE has a (not necessarily trivial) default and copy
11231 : (or move) ctor, respectively. */
11232 :
11233 : static bool
11234 5021 : has_trivial_copy_p (tree type, bool access, bool hasctor[2])
11235 : {
11236 5021 : tree fns = get_class_binding (type, complete_ctor_identifier);
11237 5021 : bool all_trivial = true;
11238 :
11239 25141 : for (tree f : ovl_range (fns))
11240 : {
11241 : /* Skip template constructors. */
11242 12602 : if (TREE_CODE (f) != FUNCTION_DECL)
11243 105 : continue;
11244 :
11245 12497 : bool cpy_or_move_ctor_p = copy_fn_p (f);
11246 :
11247 : /* Skip ctors other than default, copy, and move. */
11248 12497 : if (!cpy_or_move_ctor_p && !default_ctor_p (f))
11249 2757 : continue;
11250 :
11251 9740 : if (DECL_DELETED_FN (f))
11252 306 : continue;
11253 :
11254 9434 : bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
11255 9626 : || accessible_p (TYPE_BINFO (type), f, true));
11256 :
11257 9314 : if (accessible)
11258 9314 : hasctor[cpy_or_move_ctor_p] = true;
11259 :
11260 9434 : if (cpy_or_move_ctor_p && (!accessible || !trivial_fn_p (f)))
11261 : all_trivial = false;
11262 :
11263 : /* Break early when both properties have been determined. */
11264 9434 : if (hasctor[0] && hasctor[1] && !all_trivial)
11265 : break;
11266 : }
11267 :
11268 5021 : return all_trivial;
11269 : }
11270 :
11271 : /* Issue a warning on a call to the built-in function FNDECL if it is
11272 : a raw memory write whose destination is not an object of (something
11273 : like) trivial or standard layout type with a non-deleted assignment
11274 : and copy ctor. Detects const correctness violations, corrupting
11275 : references, virtual table pointers, and bypassing non-trivial
11276 : assignments. */
11277 :
11278 : static void
11279 69612 : maybe_warn_class_memaccess (location_t loc, tree fndecl,
11280 : const vec<tree, va_gc> *args)
11281 : {
11282 : /* Except for bcopy where it's second, the destination pointer is
11283 : the first argument for all functions handled here. Compute
11284 : the index of the destination and source arguments. */
11285 69612 : unsigned dstidx = DECL_FUNCTION_CODE (fndecl) == BUILT_IN_BCOPY;
11286 69612 : unsigned srcidx = !dstidx;
11287 :
11288 69612 : tree dest = (*args)[dstidx];
11289 69612 : if (!TREE_TYPE (dest)
11290 69612 : || (TREE_CODE (TREE_TYPE (dest)) != ARRAY_TYPE
11291 68447 : && !INDIRECT_TYPE_P (TREE_TYPE (dest))))
11292 66237 : return;
11293 :
11294 22439 : tree srctype = NULL_TREE;
11295 :
11296 : /* Determine the type of the pointed-to object and whether it's
11297 : a complete class type. */
11298 22439 : tree desttype = TREE_TYPE (TREE_TYPE (dest));
11299 :
11300 22439 : if (!desttype || !COMPLETE_TYPE_P (desttype) || !CLASS_TYPE_P (desttype))
11301 : return;
11302 :
11303 : /* Check to see if the raw memory call is made by a non-static member
11304 : function with THIS as the destination argument for the destination
11305 : type. If so, and if the class has no non-trivial bases or members,
11306 : be more permissive. */
11307 5123 : if (current_function_decl
11308 5123 : && DECL_OBJECT_MEMBER_FUNCTION_P (current_function_decl)
11309 5390 : && is_object_parameter (tree_strip_nop_conversions (dest)))
11310 : {
11311 204 : tree ctx = DECL_CONTEXT (current_function_decl);
11312 204 : bool special = same_type_ignoring_top_level_qualifiers_p (ctx, desttype);
11313 204 : tree binfo = TYPE_BINFO (ctx);
11314 :
11315 204 : if (special
11316 204 : && !BINFO_VTABLE (binfo)
11317 378 : && !first_non_trivial_field (desttype))
11318 : return;
11319 : }
11320 :
11321 : /* True if the class is trivial. */
11322 5021 : bool trivial = trivial_type_p (desttype);
11323 :
11324 : /* Set to true if DESTYPE has an accessible copy assignment. */
11325 5021 : bool hasassign = false;
11326 : /* True if all of the class' overloaded copy assignment operators
11327 : are all trivial (and not deleted) and at least one of them is
11328 : accessible. */
11329 5021 : bool trivassign = has_trivial_copy_assign_p (desttype, true, &hasassign);
11330 :
11331 : /* Set to true if DESTTYPE has an accessible default and copy ctor,
11332 : respectively. */
11333 5021 : bool hasctors[2] = { false, false };
11334 :
11335 : /* True if all of the class' overloaded copy constructors are all
11336 : trivial (and not deleted) and at least one of them is accessible. */
11337 5021 : bool trivcopy = has_trivial_copy_p (desttype, true, hasctors);
11338 :
11339 : /* Set FLD to the first private/protected member of the class. */
11340 5021 : tree fld = trivial ? first_non_public_field (desttype) : NULL_TREE;
11341 :
11342 : /* The warning format string. */
11343 5021 : const char *warnfmt = NULL;
11344 : /* A suggested alternative to offer instead of the raw memory call.
11345 : Empty string when none can be come up with. */
11346 5021 : const char *suggest = "";
11347 5021 : bool warned = false;
11348 :
11349 5021 : switch (DECL_FUNCTION_CODE (fndecl))
11350 : {
11351 560 : case BUILT_IN_MEMSET:
11352 560 : if (!integer_zerop (maybe_constant_value ((*args)[1])))
11353 : {
11354 : /* Diagnose setting non-copy-assignable or non-trivial types,
11355 : or types with a private member, to (potentially) non-zero
11356 : bytes. Since the value of the bytes being written is unknown,
11357 : suggest using assignment instead (if one exists). Also warn
11358 : for writes into objects for which zero-initialization doesn't
11359 : mean all bits clear (pointer-to-member data, where null is all
11360 : bits set). Since the value being written is (most likely)
11361 : non-zero, simply suggest assignment (but not copy assignment). */
11362 196 : suggest = "; use assignment instead";
11363 196 : if (!trivassign)
11364 : warnfmt = G_("%qD writing to an object of type %#qT with "
11365 : "no trivial copy-assignment");
11366 125 : else if (!trivial)
11367 : warnfmt = G_("%qD writing to an object of non-trivial type %#qT%s");
11368 85 : else if (fld)
11369 : {
11370 24 : const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
11371 24 : warned = warning_at (loc, OPT_Wclass_memaccess,
11372 : "%qD writing to an object of type %#qT with "
11373 : "%qs member %qD",
11374 : fndecl, desttype, access, fld);
11375 : }
11376 61 : else if (!zero_init_p (desttype))
11377 : warnfmt = G_("%qD writing to an object of type %#qT containing "
11378 : "a pointer to data member%s");
11379 :
11380 : break;
11381 : }
11382 : /* Fall through. */
11383 :
11384 481 : case BUILT_IN_BZERO:
11385 : /* Similarly to the above, diagnose clearing non-trivial or non-
11386 : standard layout objects, or objects of types with no assignmenmt.
11387 : Since the value being written is known to be zero, suggest either
11388 : copy assignment, copy ctor, or default ctor as an alternative,
11389 : depending on what's available. */
11390 :
11391 481 : if (hasassign && hasctors[0])
11392 : suggest = G_("; use assignment or value-initialization instead");
11393 49 : else if (hasassign)
11394 : suggest = G_("; use assignment instead");
11395 31 : else if (hasctors[0])
11396 16 : suggest = G_("; use value-initialization instead");
11397 :
11398 481 : if (!trivassign)
11399 : warnfmt = G_("%qD clearing an object of type %#qT with "
11400 : "no trivial copy-assignment%s");
11401 374 : else if (!trivial)
11402 : warnfmt = G_("%qD clearing an object of non-trivial type %#qT%s");
11403 304 : else if (!zero_init_p (desttype))
11404 : warnfmt = G_("%qD clearing an object of type %#qT containing "
11405 : "a pointer-to-member%s");
11406 : break;
11407 :
11408 2437 : case BUILT_IN_BCOPY:
11409 2437 : case BUILT_IN_MEMCPY:
11410 2437 : case BUILT_IN_MEMMOVE:
11411 2437 : case BUILT_IN_MEMPCPY:
11412 : /* Determine the type of the source object. */
11413 2437 : srctype = TREE_TYPE ((*args)[srcidx]);
11414 2437 : if (!srctype || !INDIRECT_TYPE_P (srctype))
11415 0 : srctype = void_type_node;
11416 : else
11417 2437 : srctype = TREE_TYPE (srctype);
11418 :
11419 : /* Since it's impossible to determine wheter the byte copy is
11420 : being used in place of assignment to an existing object or
11421 : as a substitute for initialization, assume it's the former.
11422 : Determine the best alternative to use instead depending on
11423 : what's not deleted. */
11424 2437 : if (hasassign && hasctors[1])
11425 : suggest = G_("; use copy-assignment or copy-initialization instead");
11426 488 : else if (hasassign)
11427 : suggest = G_("; use copy-assignment instead");
11428 341 : else if (hasctors[1])
11429 290 : suggest = G_("; use copy-initialization instead");
11430 :
11431 2437 : if (!trivassign)
11432 : warnfmt = G_("%qD writing to an object of type %#qT with no trivial "
11433 : "copy-assignment%s");
11434 1639 : else if (!trivially_copyable_p (desttype))
11435 : warnfmt = G_("%qD writing to an object of non-trivially copyable "
11436 : "type %#qT%s");
11437 1315 : else if (!trivcopy)
11438 : warnfmt = G_("%qD writing to an object with a deleted copy constructor");
11439 :
11440 1315 : else if (!trivial
11441 211 : && !VOID_TYPE_P (srctype)
11442 160 : && !is_byte_access_type (srctype)
11443 1412 : && !same_type_ignoring_top_level_qualifiers_p (desttype,
11444 : srctype))
11445 : {
11446 : /* Warn when copying into a non-trivial object from an object
11447 : of a different type other than void or char. */
11448 54 : warned = warning_at (loc, OPT_Wclass_memaccess,
11449 : "%qD copying an object of non-trivial type "
11450 : "%#qT from an array of %#qT",
11451 : fndecl, desttype, srctype);
11452 : }
11453 1261 : else if (fld
11454 432 : && !VOID_TYPE_P (srctype)
11455 384 : && !is_byte_access_type (srctype)
11456 1549 : && !same_type_ignoring_top_level_qualifiers_p (desttype,
11457 : srctype))
11458 : {
11459 216 : const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
11460 216 : warned = warning_at (loc, OPT_Wclass_memaccess,
11461 : "%qD copying an object of type %#qT with "
11462 : "%qs member %qD from an array of %#qT; use "
11463 : "assignment or copy-initialization instead",
11464 : fndecl, desttype, access, fld, srctype);
11465 : }
11466 1045 : else if (!trivial && vec_safe_length (args) > 2)
11467 : {
11468 157 : tree sz = maybe_constant_value ((*args)[2]);
11469 157 : if (!tree_fits_uhwi_p (sz))
11470 : break;
11471 :
11472 : /* Finally, warn on partial copies. */
11473 99 : unsigned HOST_WIDE_INT typesize
11474 99 : = tree_to_uhwi (TYPE_SIZE_UNIT (desttype));
11475 99 : if (typesize == 0)
11476 : break;
11477 96 : if (unsigned HOST_WIDE_INT partial = tree_to_uhwi (sz) % typesize)
11478 12 : warned = warning_at (loc, OPT_Wclass_memaccess,
11479 : (typesize - partial > 1
11480 : ? G_("%qD writing to an object of "
11481 : "a non-trivial type %#qT leaves %wu "
11482 : "bytes unchanged")
11483 : : G_("%qD writing to an object of "
11484 : "a non-trivial type %#qT leaves %wu "
11485 : "byte unchanged")),
11486 : fndecl, desttype, typesize - partial);
11487 : }
11488 : break;
11489 :
11490 261 : case BUILT_IN_REALLOC:
11491 :
11492 261 : if (!trivially_copyable_p (desttype))
11493 : warnfmt = G_("%qD moving an object of non-trivially copyable type "
11494 : "%#qT; use %<new%> and %<delete%> instead");
11495 138 : else if (!trivcopy)
11496 : warnfmt = G_("%qD moving an object of type %#qT with deleted copy "
11497 : "constructor; use %<new%> and %<delete%> instead");
11498 135 : else if (!get_dtor (desttype, tf_none))
11499 : warnfmt = G_("%qD moving an object of type %#qT with deleted "
11500 : "destructor");
11501 126 : else if (!trivial)
11502 : {
11503 33 : tree sz = maybe_constant_value ((*args)[1]);
11504 33 : if (TREE_CODE (sz) == INTEGER_CST
11505 33 : && tree_int_cst_lt (sz, TYPE_SIZE_UNIT (desttype)))
11506 : /* Finally, warn on reallocation into insufficient space. */
11507 7 : warned = warning_at (loc, OPT_Wclass_memaccess,
11508 : "%qD moving an object of non-trivial type "
11509 : "%#qT and size %E into a region of size %E",
11510 7 : fndecl, desttype, TYPE_SIZE_UNIT (desttype),
11511 : sz);
11512 : }
11513 : break;
11514 :
11515 : default:
11516 : return;
11517 : }
11518 :
11519 310 : if (warnfmt)
11520 : {
11521 1569 : if (suggest)
11522 1569 : warned = warning_at (loc, OPT_Wclass_memaccess,
11523 : warnfmt, fndecl, desttype, suggest);
11524 : else
11525 : warned = warning_at (loc, OPT_Wclass_memaccess,
11526 : warnfmt, fndecl, desttype);
11527 : }
11528 :
11529 3375 : if (warned)
11530 1879 : inform (location_of (desttype), "%#qT declared here", desttype);
11531 : }
11532 :
11533 : /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
11534 : If FN is the result of resolving an overloaded target built-in,
11535 : ORIG_FNDECL is the original function decl, otherwise it is null.
11536 : This function performs no overload resolution, conversion, or other
11537 : high-level operations. */
11538 :
11539 : tree
11540 151680029 : build_cxx_call (tree fn, int nargs, tree *argarray,
11541 : tsubst_flags_t complain, tree orig_fndecl)
11542 : {
11543 151680029 : tree fndecl;
11544 :
11545 : /* Remember roughly where this call is. */
11546 151680029 : location_t loc = cp_expr_loc_or_input_loc (fn);
11547 151680029 : fn = build_call_a (fn, nargs, argarray);
11548 151680029 : SET_EXPR_LOCATION (fn, loc);
11549 :
11550 151680029 : fndecl = get_callee_fndecl (fn);
11551 151680029 : if (!orig_fndecl)
11552 151680029 : orig_fndecl = fndecl;
11553 :
11554 : /* Check that arguments to builtin functions match the expectations. */
11555 151680029 : if (fndecl
11556 145494180 : && !processing_template_decl
11557 297121635 : && fndecl_built_in_p (fndecl))
11558 : {
11559 : int i;
11560 :
11561 : /* We need to take care that values to BUILT_IN_NORMAL
11562 : are reduced. */
11563 21002905 : for (i = 0; i < nargs; i++)
11564 13816877 : argarray[i] = maybe_constant_value (argarray[i]);
11565 :
11566 7186028 : if (!check_builtin_function_arguments (EXPR_LOCATION (fn), vNULL, fndecl,
11567 : orig_fndecl, nargs, argarray,
11568 : complain & tf_error))
11569 804 : return error_mark_node;
11570 7185224 : else if (fndecl_built_in_p (fndecl, BUILT_IN_CLEAR_PADDING))
11571 : {
11572 12884 : tree arg0 = argarray[0];
11573 12884 : STRIP_NOPS (arg0);
11574 12884 : if (TREE_CODE (arg0) == ADDR_EXPR
11575 264 : && DECL_P (TREE_OPERAND (arg0, 0))
11576 13127 : && same_type_ignoring_top_level_qualifiers_p
11577 243 : (TREE_TYPE (TREE_TYPE (argarray[0])),
11578 243 : TREE_TYPE (TREE_TYPE (arg0))))
11579 : /* For __builtin_clear_padding (&var) we know the type
11580 : is for a complete object, so there is no risk in clearing
11581 : padding that is reused in some derived class member. */;
11582 12648 : else if (!trivially_copyable_p (TREE_TYPE (TREE_TYPE (argarray[0]))))
11583 : {
11584 18 : error_at (EXPR_LOC_OR_LOC (argarray[0], input_location),
11585 : "argument %u in call to function %qE "
11586 : "has pointer to a non-trivially-copyable type (%qT)",
11587 18 : 1, fndecl, TREE_TYPE (argarray[0]));
11588 18 : return error_mark_node;
11589 : }
11590 : }
11591 : }
11592 :
11593 151679207 : if (VOID_TYPE_P (TREE_TYPE (fn)))
11594 35286735 : return maybe_contract_wrap_call (fndecl, fn);
11595 :
11596 : /* 5.2.2/11: If a function call is a prvalue of object type: if the
11597 : function call is either the operand of a decltype-specifier or the
11598 : right operand of a comma operator that is the operand of a
11599 : decltype-specifier, a temporary object is not introduced for the
11600 : prvalue. The type of the prvalue may be incomplete. */
11601 116392472 : if (!(complain & tf_decltype))
11602 : {
11603 98643594 : fn = require_complete_type (fn, complain);
11604 98643594 : if (fn == error_mark_node)
11605 : return error_mark_node;
11606 98643568 : fn = maybe_contract_wrap_call (fndecl, fn);
11607 :
11608 98643568 : if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
11609 : {
11610 11388600 : fn = build_cplus_new (TREE_TYPE (fn), fn, complain);
11611 11388600 : maybe_warn_parm_abi (TREE_TYPE (fn), loc);
11612 : }
11613 : }
11614 : else
11615 17748878 : fn = maybe_contract_wrap_call (fndecl, fn);
11616 116392446 : return convert_from_reference (fn);
11617 : }
11618 :
11619 : /* Returns the value to use for the in-charge parameter when making a
11620 : call to a function with the indicated NAME.
11621 :
11622 : FIXME:Can't we find a neater way to do this mapping? */
11623 :
11624 : tree
11625 33624 : in_charge_arg_for_name (tree name)
11626 : {
11627 33624 : if (IDENTIFIER_CTOR_P (name))
11628 : {
11629 19256 : if (name == complete_ctor_identifier)
11630 9628 : return integer_one_node;
11631 9628 : gcc_checking_assert (name == base_ctor_identifier);
11632 : }
11633 : else
11634 : {
11635 14368 : if (name == complete_dtor_identifier)
11636 7184 : return integer_two_node;
11637 7184 : else if (name == deleting_dtor_identifier)
11638 : /* The deleting dtor should now be handled by
11639 : build_delete_destructor_body. */
11640 0 : gcc_unreachable ();
11641 7184 : gcc_checking_assert (name == base_dtor_identifier);
11642 : }
11643 :
11644 16812 : return integer_zero_node;
11645 : }
11646 :
11647 : /* We've built up a constructor call RET. Complain if it delegates to the
11648 : constructor we're currently compiling. */
11649 :
11650 : static void
11651 349312 : check_self_delegation (tree ret)
11652 : {
11653 349312 : if (TREE_CODE (ret) == TARGET_EXPR)
11654 0 : ret = TARGET_EXPR_INITIAL (ret);
11655 349312 : tree fn = cp_get_callee_fndecl_nofold (ret);
11656 349312 : if (fn && DECL_ABSTRACT_ORIGIN (fn) == current_function_decl)
11657 46 : error ("constructor delegates to itself");
11658 349312 : }
11659 :
11660 : /* Build a call to a constructor, destructor, or an assignment
11661 : operator for INSTANCE, an expression with class type. NAME
11662 : indicates the special member function to call; *ARGS are the
11663 : arguments. ARGS may be NULL. This may change ARGS. BINFO
11664 : indicates the base of INSTANCE that is to be passed as the `this'
11665 : parameter to the member function called.
11666 :
11667 : FLAGS are the LOOKUP_* flags to use when processing the call.
11668 :
11669 : If NAME indicates a complete object constructor, INSTANCE may be
11670 : NULL_TREE. In this case, the caller will call build_cplus_new to
11671 : store the newly constructed object into a VAR_DECL. */
11672 :
11673 : tree
11674 37222850 : build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
11675 : tree binfo, int flags, tsubst_flags_t complain)
11676 : {
11677 37222850 : tree fns;
11678 : /* The type of the subobject to be constructed or destroyed. */
11679 37222850 : tree class_type;
11680 37222850 : vec<tree, va_gc> *allocated = NULL;
11681 37222850 : tree ret;
11682 :
11683 37222850 : gcc_assert (IDENTIFIER_CDTOR_P (name) || name == assign_op_identifier);
11684 :
11685 37222850 : if (error_operand_p (instance))
11686 0 : return error_mark_node;
11687 :
11688 37222850 : if (IDENTIFIER_DTOR_P (name))
11689 : {
11690 10365228 : gcc_assert (args == NULL || vec_safe_is_empty (*args));
11691 10365228 : if (!type_build_dtor_call (TREE_TYPE (instance)))
11692 : /* Shortcut to avoid lazy destructor declaration. */
11693 989 : return build_trivial_dtor_call (instance);
11694 : }
11695 :
11696 37221861 : if (TYPE_P (binfo))
11697 : {
11698 : /* Resolve the name. */
11699 29676621 : if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
11700 24 : return error_mark_node;
11701 :
11702 29676597 : binfo = TYPE_BINFO (binfo);
11703 : }
11704 :
11705 29676597 : gcc_assert (binfo != NULL_TREE);
11706 :
11707 37221837 : class_type = BINFO_TYPE (binfo);
11708 :
11709 : /* Handle the special case where INSTANCE is NULL_TREE. */
11710 37221837 : if (name == complete_ctor_identifier && !instance)
11711 19497474 : instance = build_dummy_object (class_type);
11712 : else
11713 : {
11714 : /* Convert to the base class, if necessary. */
11715 17724363 : if (!same_type_ignoring_top_level_qualifiers_p
11716 17724363 : (TREE_TYPE (instance), BINFO_TYPE (binfo)))
11717 : {
11718 1563019 : if (IDENTIFIER_CDTOR_P (name))
11719 : /* For constructors and destructors, either the base is
11720 : non-virtual, or it is virtual but we are doing the
11721 : conversion from a constructor or destructor for the
11722 : complete object. In either case, we can convert
11723 : statically. */
11724 1543792 : instance = convert_to_base_statically (instance, binfo);
11725 : else
11726 : {
11727 : /* However, for assignment operators, we must convert
11728 : dynamically if the base is virtual. */
11729 19227 : gcc_checking_assert (name == assign_op_identifier);
11730 19227 : instance = build_base_path (PLUS_EXPR, instance,
11731 : binfo, /*nonnull=*/1, complain);
11732 : }
11733 : }
11734 : }
11735 :
11736 37221837 : gcc_assert (instance != NULL_TREE);
11737 :
11738 : /* In C++17, "If the initializer expression is a prvalue and the
11739 : cv-unqualified version of the source type is the same class as the class
11740 : of the destination, the initializer expression is used to initialize the
11741 : destination object." Handle that here to avoid doing overload
11742 : resolution. */
11743 37221837 : if (cxx_dialect >= cxx17
11744 37025326 : && args && vec_safe_length (*args) == 1
11745 59238521 : && !unsafe_return_slot_p (instance))
11746 : {
11747 20784574 : tree arg = (**args)[0];
11748 :
11749 1631666 : if (BRACE_ENCLOSED_INITIALIZER_P (arg)
11750 1631459 : && !TYPE_HAS_LIST_CTOR (class_type)
11751 1565111 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (arg)
11752 22349671 : && CONSTRUCTOR_NELTS (arg) == 1)
11753 1121572 : arg = CONSTRUCTOR_ELT (arg, 0)->value;
11754 :
11755 20784574 : if ((TREE_CODE (arg) == TARGET_EXPR
11756 10061864 : || TREE_CODE (arg) == CONSTRUCTOR)
11757 32017452 : && (same_type_ignoring_top_level_qualifiers_p
11758 11232878 : (class_type, TREE_TYPE (arg))))
11759 : {
11760 10136822 : if (is_dummy_object (instance))
11761 : return arg;
11762 214675 : else if (TREE_CODE (arg) == TARGET_EXPR)
11763 214675 : TARGET_EXPR_DIRECT_INIT_P (arg) = true;
11764 :
11765 214675 : if ((complain & tf_error)
11766 214673 : && (flags & LOOKUP_DELEGATING_CONS))
11767 0 : check_self_delegation (arg);
11768 : /* Avoid change of behavior on Wunused-var-2.C. */
11769 214675 : instance = mark_lvalue_use (instance);
11770 214675 : return cp_build_init_expr (instance, arg);
11771 : }
11772 : }
11773 :
11774 27085015 : fns = lookup_fnfields (binfo, name, 1, complain);
11775 :
11776 : /* When making a call to a constructor or destructor for a subobject
11777 : that uses virtual base classes, pass down a pointer to a VTT for
11778 : the subobject. */
11779 27085015 : if ((name == base_ctor_identifier
11780 24816818 : || name == base_dtor_identifier)
11781 28628873 : && CLASSTYPE_VBASECLASSES (class_type))
11782 : {
11783 46904 : tree vtt;
11784 46904 : tree sub_vtt;
11785 :
11786 : /* If the current function is a complete object constructor
11787 : or destructor, then we fetch the VTT directly.
11788 : Otherwise, we look it up using the VTT we were given. */
11789 46904 : vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
11790 46904 : vtt = decay_conversion (vtt, complain);
11791 46904 : if (vtt == error_mark_node)
11792 0 : return error_mark_node;
11793 46904 : vtt = build_if_in_charge (vtt, current_vtt_parm);
11794 46904 : if (BINFO_SUBVTT_INDEX (binfo))
11795 46752 : sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
11796 : else
11797 152 : sub_vtt = vtt;
11798 :
11799 46904 : if (args == NULL)
11800 : {
11801 29870 : allocated = make_tree_vector ();
11802 29870 : args = &allocated;
11803 : }
11804 :
11805 46904 : vec_safe_insert (*args, 0, sub_vtt);
11806 : }
11807 :
11808 54170030 : ret = build_new_method_call (instance, fns, args,
11809 27085015 : TYPE_BINFO (BINFO_TYPE (binfo)),
11810 : flags, /*fn=*/NULL,
11811 : complain);
11812 :
11813 27085015 : if (allocated != NULL)
11814 29870 : release_tree_vector (allocated);
11815 :
11816 27085015 : if ((complain & tf_error)
11817 24293298 : && (flags & LOOKUP_DELEGATING_CONS)
11818 349398 : && name == complete_ctor_identifier)
11819 349312 : check_self_delegation (ret);
11820 :
11821 : return ret;
11822 : }
11823 :
11824 : /* Return the NAME, as a C string. The NAME indicates a function that
11825 : is a member of TYPE. *FREE_P is set to true if the caller must
11826 : free the memory returned.
11827 :
11828 : Rather than go through all of this, we should simply set the names
11829 : of constructors and destructors appropriately, and dispense with
11830 : ctor_identifier, dtor_identifier, etc. */
11831 :
11832 : static char *
11833 91 : name_as_c_string (tree name, tree type, bool *free_p)
11834 : {
11835 91 : const char *pretty_name;
11836 :
11837 : /* Assume that we will not allocate memory. */
11838 91 : *free_p = false;
11839 : /* Constructors and destructors are special. */
11840 91 : if (IDENTIFIER_CDTOR_P (name))
11841 : {
11842 42 : pretty_name
11843 42 : = identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type)));
11844 : /* For a destructor, add the '~'. */
11845 42 : if (IDENTIFIER_DTOR_P (name))
11846 : {
11847 0 : pretty_name = concat ("~", pretty_name, NULL);
11848 : /* Remember that we need to free the memory allocated. */
11849 0 : *free_p = true;
11850 : }
11851 : }
11852 49 : else if (IDENTIFIER_CONV_OP_P (name))
11853 : {
11854 0 : pretty_name = concat ("operator ",
11855 0 : type_as_string_translate (TREE_TYPE (name),
11856 : TFF_PLAIN_IDENTIFIER),
11857 : NULL);
11858 : /* Remember that we need to free the memory allocated. */
11859 0 : *free_p = true;
11860 : }
11861 : else
11862 49 : pretty_name = identifier_to_locale (IDENTIFIER_POINTER (name));
11863 :
11864 91 : return const_cast<char *> (pretty_name);
11865 : }
11866 :
11867 : /* If CANDIDATES contains exactly one candidate, return it, otherwise
11868 : return NULL. */
11869 :
11870 : static z_candidate *
11871 677 : single_z_candidate (z_candidate *candidates)
11872 : {
11873 0 : if (candidates == NULL)
11874 : return NULL;
11875 :
11876 665 : if (candidates->next)
11877 0 : return NULL;
11878 :
11879 : return candidates;
11880 : }
11881 :
11882 : /* If CANDIDATE is invalid due to a bad argument type, return the
11883 : pertinent conversion_info.
11884 :
11885 : Otherwise, return NULL. */
11886 :
11887 : static const conversion_info *
11888 336 : maybe_get_bad_conversion_for_unmatched_call (const z_candidate *candidate)
11889 : {
11890 : /* Must be an rr_arg_conversion or rr_bad_arg_conversion. */
11891 336 : rejection_reason *r = candidate->reason;
11892 :
11893 336 : if (r == NULL)
11894 : return NULL;
11895 :
11896 336 : switch (r->code)
11897 : {
11898 : default:
11899 : return NULL;
11900 :
11901 50 : case rr_arg_conversion:
11902 50 : return &r->u.conversion;
11903 :
11904 6 : case rr_bad_arg_conversion:
11905 6 : return &r->u.bad_conversion;
11906 : }
11907 : }
11908 :
11909 : /* Issue an error and note complaining about a bad argument type at a
11910 : callsite with a single candidate FNDECL.
11911 :
11912 : ARG_LOC is the location of the argument (or UNKNOWN_LOCATION, in which
11913 : case input_location is used).
11914 : FROM_TYPE is the type of the actual argument; TO_TYPE is the type of
11915 : the formal parameter. */
11916 :
11917 : void
11918 148 : complain_about_bad_argument (location_t arg_loc,
11919 : tree from_type, tree to_type,
11920 : tree fndecl, int parmnum)
11921 : {
11922 148 : auto_diagnostic_group d;
11923 148 : range_label_for_type_mismatch rhs_label (from_type, to_type);
11924 148 : range_label *label = &rhs_label;
11925 148 : if (arg_loc == UNKNOWN_LOCATION)
11926 : {
11927 6 : arg_loc = input_location;
11928 6 : label = NULL;
11929 : }
11930 148 : gcc_rich_location richloc (arg_loc, label, highlight_colors::percent_h);
11931 148 : error_at (&richloc,
11932 : "cannot convert %qH to %qI",
11933 : from_type, to_type);
11934 148 : maybe_inform_about_fndecl_for_bogus_argument_init
11935 148 : (fndecl,
11936 : parmnum,
11937 : highlight_colors::percent_i);
11938 148 : }
11939 :
11940 : /* Subroutine of build_new_method_call_1, for where there are no viable
11941 : candidates for the call. */
11942 :
11943 : static void
11944 683 : complain_about_no_candidates_for_method_call (tree instance,
11945 : z_candidate *candidates,
11946 : tree explicit_targs,
11947 : tree basetype,
11948 : tree optype, tree name,
11949 : bool skip_first_for_error,
11950 : vec<tree, va_gc> *user_args)
11951 : {
11952 683 : auto_diagnostic_group d;
11953 683 : if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
11954 0 : cxx_incomplete_type_error (instance, basetype);
11955 683 : else if (optype)
11956 6 : error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
11957 : basetype, optype, build_tree_list_vec (user_args),
11958 6 : TREE_TYPE (instance));
11959 : else
11960 : {
11961 : /* Special-case for when there's a single candidate that's failing
11962 : due to a bad argument type. */
11963 677 : if (z_candidate *candidate = single_z_candidate (candidates))
11964 336 : if (const conversion_info *conv
11965 336 : = maybe_get_bad_conversion_for_unmatched_call (candidate))
11966 : {
11967 56 : tree from_type = conv->from;
11968 56 : if (!TYPE_P (conv->from))
11969 6 : from_type = lvalue_type (conv->from);
11970 56 : complain_about_bad_argument (conv->loc,
11971 56 : from_type, conv->to_type,
11972 56 : candidate->fn, conv->n_arg);
11973 56 : return;
11974 : }
11975 :
11976 621 : tree arglist = build_tree_list_vec (user_args);
11977 621 : tree errname = name;
11978 621 : bool twiddle = false;
11979 621 : if (IDENTIFIER_CDTOR_P (errname))
11980 : {
11981 319 : twiddle = IDENTIFIER_DTOR_P (errname);
11982 319 : errname = constructor_name (basetype);
11983 : }
11984 621 : if (explicit_targs)
11985 47 : errname = lookup_template_function (errname, explicit_targs);
11986 621 : if (skip_first_for_error)
11987 3 : arglist = TREE_CHAIN (arglist);
11988 1242 : error ("no matching function for call to %<%T::%s%E(%A)%#V%>",
11989 621 : basetype, &"~"[!twiddle], errname, arglist,
11990 621 : TREE_TYPE (instance));
11991 : }
11992 627 : print_z_candidates (location_of (name), candidates);
11993 683 : }
11994 :
11995 : /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
11996 : be set, upon return, to the function called. ARGS may be NULL.
11997 : This may change ARGS. */
11998 :
11999 : tree
12000 95527228 : build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args,
12001 : tree conversion_path, int flags,
12002 : tree *fn_p, tsubst_flags_t complain)
12003 : {
12004 95527228 : struct z_candidate *candidates = 0, *cand;
12005 95527228 : tree explicit_targs = NULL_TREE;
12006 95527228 : tree basetype = NULL_TREE;
12007 95527228 : tree access_binfo;
12008 95527228 : tree optype;
12009 95527228 : tree first_mem_arg = NULL_TREE;
12010 95527228 : tree name;
12011 95527228 : bool skip_first_for_error;
12012 95527228 : vec<tree, va_gc> *user_args;
12013 95527228 : tree call;
12014 95527228 : tree fn;
12015 95527228 : int template_only = 0;
12016 95527228 : bool any_viable_p;
12017 95527228 : tree orig_instance;
12018 95527228 : tree orig_fns;
12019 95527228 : vec<tree, va_gc> *orig_args = NULL;
12020 :
12021 95527228 : auto_cond_timevar tv (TV_OVERLOAD);
12022 :
12023 95527228 : gcc_assert (instance != NULL_TREE);
12024 :
12025 : /* We don't know what function we're going to call, yet. */
12026 95527228 : if (fn_p)
12027 26919920 : *fn_p = NULL_TREE;
12028 :
12029 95527228 : if (error_operand_p (instance)
12030 95527228 : || !fns || error_operand_p (fns))
12031 1410021 : return error_mark_node;
12032 :
12033 94117207 : if (!BASELINK_P (fns))
12034 : {
12035 0 : if (complain & tf_error)
12036 0 : error ("call to non-function %qD", fns);
12037 0 : return error_mark_node;
12038 : }
12039 :
12040 94117207 : orig_instance = instance;
12041 94117207 : orig_fns = fns;
12042 :
12043 : /* Dismantle the baselink to collect all the information we need. */
12044 94117207 : if (!conversion_path)
12045 41536281 : conversion_path = BASELINK_BINFO (fns);
12046 94117207 : access_binfo = BASELINK_ACCESS_BINFO (fns);
12047 94117207 : optype = BASELINK_OPTYPE (fns);
12048 94117207 : fns = BASELINK_FUNCTIONS (fns);
12049 94117207 : if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12050 : {
12051 2538460 : explicit_targs = TREE_OPERAND (fns, 1);
12052 2538460 : fns = TREE_OPERAND (fns, 0);
12053 2538460 : template_only = 1;
12054 : }
12055 94117207 : gcc_assert (OVL_P (fns));
12056 94117207 : fn = OVL_FIRST (fns);
12057 94117207 : name = DECL_NAME (fn);
12058 :
12059 94117207 : basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
12060 94117207 : gcc_assert (CLASS_TYPE_P (basetype));
12061 :
12062 94117207 : user_args = args == NULL ? NULL : *args;
12063 : /* Under DR 147 A::A() is an invalid constructor call,
12064 : not a functional cast. */
12065 94117207 : if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
12066 : {
12067 54 : if (! (complain & tf_error))
12068 0 : return error_mark_node;
12069 :
12070 54 : basetype = DECL_CONTEXT (fn);
12071 54 : name = constructor_name (basetype);
12072 54 : auto_diagnostic_group d;
12073 54 : if (permerror (input_location,
12074 : "cannot call constructor %<%T::%D%> directly",
12075 : basetype, name))
12076 54 : inform (input_location, "for a function-style cast, remove the "
12077 : "redundant %<::%D%>", name);
12078 54 : call = build_functional_cast (input_location, basetype,
12079 : build_tree_list_vec (user_args),
12080 : complain);
12081 54 : return call;
12082 54 : }
12083 :
12084 94117153 : if (processing_template_decl)
12085 7513523 : orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
12086 :
12087 : /* Process the argument list. */
12088 93980215 : if (args != NULL && *args != NULL)
12089 : {
12090 80182327 : *args = resolve_args (*args, complain);
12091 80182327 : if (*args == NULL)
12092 153 : return error_mark_node;
12093 : user_args = *args;
12094 : }
12095 :
12096 : /* Consider the object argument to be used even if we end up selecting a
12097 : static member function. */
12098 94117000 : instance = mark_type_use (instance);
12099 :
12100 : /* Figure out whether to skip the first argument for the error
12101 : message we will display to users if an error occurs. We don't
12102 : want to display any compiler-generated arguments. The "this"
12103 : pointer hasn't been added yet. However, we must remove the VTT
12104 : pointer if this is a call to a base-class constructor or
12105 : destructor. */
12106 94117000 : skip_first_for_error = false;
12107 94117000 : if (IDENTIFIER_CDTOR_P (name))
12108 : {
12109 : /* Callers should explicitly indicate whether they want to ctor
12110 : the complete object or just the part without virtual bases. */
12111 49980207 : gcc_assert (name != ctor_identifier);
12112 :
12113 : /* Remove the VTT pointer, if present. */
12114 47712016 : if ((name == base_ctor_identifier || name == base_dtor_identifier)
12115 51524065 : && CLASSTYPE_VBASECLASSES (basetype))
12116 : skip_first_for_error = true;
12117 :
12118 : /* It's OK to call destructors and constructors on cv-qualified
12119 : objects. Therefore, convert the INSTANCE to the unqualified
12120 : type, if necessary. */
12121 49980207 : if (!same_type_p (basetype, TREE_TYPE (instance)))
12122 : {
12123 660997 : instance = build_this (instance);
12124 660997 : instance = build_nop (build_pointer_type (basetype), instance);
12125 660997 : instance = build_fold_indirect_ref (instance);
12126 : }
12127 : }
12128 : else
12129 88273586 : gcc_assert (!DECL_DESTRUCTOR_P (fn) && !DECL_CONSTRUCTOR_P (fn));
12130 :
12131 : /* For the overload resolution we need to find the actual `this`
12132 : that would be captured if the call turns out to be to a
12133 : non-static member function. Do not actually capture it at this
12134 : point. */
12135 188234000 : if (DECL_CONSTRUCTOR_P (fn))
12136 : /* Constructors don't use the enclosing 'this'. */
12137 : first_mem_arg = instance;
12138 : else
12139 69308454 : first_mem_arg = maybe_resolve_dummy (instance, false);
12140 :
12141 94117000 : conversion_obstack_sentinel cos;
12142 :
12143 : /* The number of arguments artificial parms in ARGS; we subtract one because
12144 : there's no 'this' in ARGS. */
12145 94117000 : unsigned skip = num_artificial_parms_for (fn) - 1;
12146 :
12147 : /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
12148 : initializer, not T({ }). */
12149 94117000 : if (DECL_CONSTRUCTOR_P (fn)
12150 21228319 : && vec_safe_length (user_args) > skip
12151 113200760 : && DIRECT_LIST_INIT_P ((*user_args)[skip]))
12152 : {
12153 1633762 : tree init_list = (*user_args)[skip];
12154 1633762 : tree init = NULL_TREE;
12155 :
12156 1633762 : gcc_assert (user_args->length () == skip + 1
12157 : && !(flags & LOOKUP_ONLYCONVERTING));
12158 :
12159 : /* If the initializer list has no elements and T is a class type with
12160 : a default constructor, the object is value-initialized. Handle
12161 : this here so we don't need to handle it wherever we use
12162 : build_special_member_call. */
12163 1633762 : if (CONSTRUCTOR_NELTS (init_list) == 0
12164 176829 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
12165 : /* For a user-provided default constructor, use the normal
12166 : mechanisms so that protected access works. */
12167 176829 : && type_has_non_user_provided_default_constructor (basetype)
12168 1568259 : && !processing_template_decl)
12169 111323 : init = build_value_init (basetype, complain);
12170 :
12171 : /* If BASETYPE is an aggregate, we need to do aggregate
12172 : initialization. */
12173 1522439 : else if (CP_AGGREGATE_TYPE_P (basetype))
12174 : {
12175 41 : init = reshape_init (basetype, init_list, complain);
12176 41 : init = digest_init (basetype, init, complain);
12177 : }
12178 :
12179 111364 : if (init)
12180 : {
12181 111364 : if (is_dummy_object (instance))
12182 31438 : return get_target_expr (init, complain);
12183 79926 : return cp_build_init_expr (instance, init);
12184 : }
12185 :
12186 : /* Otherwise go ahead with overload resolution. */
12187 1522398 : add_list_candidates (fns, first_mem_arg, user_args,
12188 : basetype, explicit_targs, template_only,
12189 : conversion_path, access_binfo, flags,
12190 : &candidates, complain);
12191 : }
12192 : else
12193 92483238 : add_candidates (fns, first_mem_arg, user_args, optype,
12194 : explicit_targs, template_only, conversion_path,
12195 : access_binfo, flags, &candidates, complain);
12196 :
12197 94005636 : any_viable_p = false;
12198 94005636 : candidates = splice_viable (candidates, false, &any_viable_p);
12199 :
12200 94005636 : if (!any_viable_p)
12201 : {
12202 : /* [dcl.init], 17.6.2.2:
12203 :
12204 : Otherwise, if no constructor is viable, the destination type is
12205 : a (possibly cv-qualified) aggregate class A, and the initializer
12206 : is a parenthesized expression-list, the object is initialized as
12207 : follows...
12208 :
12209 : We achieve this by building up a CONSTRUCTOR, as for list-init,
12210 : and setting CONSTRUCTOR_IS_PAREN_INIT to distinguish between
12211 : the two. */
12212 24014 : if (DECL_CONSTRUCTOR_P (fn)
12213 17960 : && !(flags & LOOKUP_ONLYCONVERTING)
12214 17936 : && cxx_dialect >= cxx20
12215 17322 : && CP_AGGREGATE_TYPE_P (basetype)
12216 24014 : && !vec_safe_is_empty (user_args))
12217 : {
12218 : /* Create a CONSTRUCTOR from ARGS, e.g. {1, 2} from <1, 2>. */
12219 928 : tree ctor = build_constructor_from_vec (init_list_type_node,
12220 : user_args);
12221 928 : CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true;
12222 928 : CONSTRUCTOR_IS_PAREN_INIT (ctor) = true;
12223 928 : if (is_dummy_object (instance))
12224 : return ctor;
12225 : else
12226 : {
12227 612 : ctor = digest_init (basetype, ctor, complain);
12228 612 : if (ctor == error_mark_node)
12229 : return error_mark_node;
12230 419 : return cp_build_init_expr (instance, ctor);
12231 : }
12232 : }
12233 23086 : if (complain & tf_error)
12234 683 : complain_about_no_candidates_for_method_call (instance, candidates,
12235 : explicit_targs, basetype,
12236 : optype, name,
12237 : skip_first_for_error,
12238 : user_args);
12239 23086 : call = error_mark_node;
12240 : }
12241 : else
12242 : {
12243 93981622 : cand = tourney (candidates, complain);
12244 93981622 : if (cand == 0)
12245 : {
12246 187 : char *pretty_name;
12247 187 : bool free_p;
12248 187 : tree arglist;
12249 :
12250 187 : if (complain & tf_error)
12251 : {
12252 91 : pretty_name = name_as_c_string (name, basetype, &free_p);
12253 91 : arglist = build_tree_list_vec (user_args);
12254 91 : if (skip_first_for_error)
12255 0 : arglist = TREE_CHAIN (arglist);
12256 91 : auto_diagnostic_group d;
12257 182 : if (!any_strictly_viable (candidates))
12258 13 : error ("no matching function for call to %<%s(%A)%>",
12259 : pretty_name, arglist);
12260 : else
12261 78 : error ("call of overloaded %<%s(%A)%> is ambiguous",
12262 : pretty_name, arglist);
12263 91 : print_z_candidates (location_of (name), candidates);
12264 91 : if (free_p)
12265 0 : free (pretty_name);
12266 91 : }
12267 187 : call = error_mark_node;
12268 187 : if (fn_p)
12269 84 : *fn_p = error_mark_node;
12270 : }
12271 : else
12272 : {
12273 93981435 : fn = cand->fn;
12274 93981435 : call = NULL_TREE;
12275 :
12276 93981435 : if (!(flags & LOOKUP_NONVIRTUAL)
12277 71909061 : && DECL_PURE_VIRTUAL_P (fn)
12278 199256 : && instance == current_class_ref
12279 94137376 : && (complain & tf_warning))
12280 : {
12281 : /* This is not an error, it is runtime undefined
12282 : behavior. */
12283 155941 : if (!current_function_decl)
12284 3 : warning (0, "pure virtual %q#D called from "
12285 : "non-static data member initializer", fn);
12286 155938 : else if (DECL_CONSTRUCTOR_P (current_function_decl)
12287 155938 : || DECL_DESTRUCTOR_P (current_function_decl))
12288 9 : warning (0, (DECL_CONSTRUCTOR_P (current_function_decl)
12289 : ? G_("pure virtual %q#D called from constructor")
12290 : : G_("pure virtual %q#D called from destructor")),
12291 : fn);
12292 : }
12293 :
12294 108021213 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fn)
12295 159885252 : && !DECL_CONSTRUCTOR_P (fn)
12296 149244975 : && is_dummy_object (instance))
12297 : {
12298 49559 : instance = maybe_resolve_dummy (instance, true);
12299 49559 : if (instance == error_mark_node)
12300 : call = error_mark_node;
12301 49559 : else if (!is_dummy_object (instance))
12302 : {
12303 : /* We captured 'this' in the current lambda now that
12304 : we know we really need it. */
12305 49170 : cand->first_arg = instance;
12306 : }
12307 389 : else if (current_class_ptr && any_dependent_bases_p ())
12308 : /* We can't tell until instantiation time whether we can use
12309 : *this as the implicit object argument. */;
12310 : else
12311 : {
12312 351 : if (complain & tf_error)
12313 60 : error ("cannot call member function %qD without object",
12314 : fn);
12315 351 : call = error_mark_node;
12316 : }
12317 : }
12318 :
12319 93981435 : if (call != error_mark_node)
12320 : {
12321 : /* Now we know what function is being called. */
12322 93981084 : if (fn_p)
12323 25496902 : *fn_p = fn;
12324 : /* Build the actual CALL_EXPR. */
12325 93981084 : call = build_over_call (cand, flags, complain);
12326 :
12327 : /* Suppress warnings for if (my_struct.operator= (x)) where
12328 : my_struct is implicitly converted to bool. */
12329 93981084 : if (TREE_CODE (call) == MODIFY_EXPR)
12330 3062539 : suppress_warning (call, OPT_Wparentheses);
12331 :
12332 : /* In an expression of the form `a->f()' where `f' turns
12333 : out to be a static member function, `a' is
12334 : none-the-less evaluated. */
12335 93981084 : if (!is_dummy_object (instance))
12336 72910290 : call = keep_unused_object_arg (call, instance, fn);
12337 93981084 : if (call != error_mark_node
12338 186052416 : && DECL_DESTRUCTOR_P (cand->fn)
12339 119151955 : && !VOID_TYPE_P (TREE_TYPE (call)))
12340 : /* An explicit call of the form "x->~X()" has type
12341 : "void". However, on platforms where destructors
12342 : return "this" (i.e., those where
12343 : targetm.cxx.cdtor_returns_this is true), such calls
12344 : will appear to have a return value of pointer type
12345 : to the low-level call machinery. We do not want to
12346 : change the low-level machinery, since we want to be
12347 : able to optimize "delete f()" on such platforms as
12348 : "operator delete(~X(f()))" (rather than generating
12349 : "t = f(), ~X(t), operator delete (t)"). */
12350 14603568 : call = build_nop (void_type_node, call);
12351 : }
12352 : }
12353 : }
12354 :
12355 94004708 : if (processing_template_decl && call != error_mark_node)
12356 : {
12357 7513471 : bool cast_to_void = false;
12358 :
12359 7513471 : if (TREE_CODE (call) == COMPOUND_EXPR)
12360 8 : call = TREE_OPERAND (call, 1);
12361 7513463 : else if (TREE_CODE (call) == NOP_EXPR)
12362 : {
12363 0 : cast_to_void = true;
12364 0 : call = TREE_OPERAND (call, 0);
12365 : }
12366 7513471 : if (INDIRECT_REF_P (call))
12367 580335 : call = TREE_OPERAND (call, 0);
12368 :
12369 : /* Prune all but the selected function from the original overload
12370 : set so that we can avoid some duplicate work at instantiation time. */
12371 7513471 : if (really_overloaded_fn (fns))
12372 : {
12373 1801882 : if (DECL_TEMPLATE_INFO (fn)
12374 1801882 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
12375 : {
12376 : /* Use the selected template, not the specialization, so that
12377 : this looks like an actual lookup result for sake of
12378 : filter_memfn_lookup. */
12379 :
12380 267733 : if (OVL_SINGLE_P (fns))
12381 : /* If the original overload set consists of a single function
12382 : template, this isn't beneficial. */
12383 229426 : goto skip_prune;
12384 :
12385 38307 : fn = ovl_make (DECL_TI_TEMPLATE (fn));
12386 38307 : if (template_only)
12387 28989 : fn = lookup_template_function (fn, explicit_targs);
12388 : }
12389 1572456 : orig_fns = copy_node (orig_fns);
12390 1572456 : BASELINK_FUNCTIONS (orig_fns) = fn;
12391 1572456 : BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (orig_fns) = true;
12392 : }
12393 :
12394 5711589 : skip_prune:
12395 7513471 : call = (build_min_non_dep_call_vec
12396 7513471 : (call,
12397 7513471 : build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
12398 : orig_instance, orig_fns, NULL_TREE),
12399 : orig_args));
12400 7513471 : SET_EXPR_LOCATION (call, input_location);
12401 7513471 : call = convert_from_reference (call);
12402 7513471 : if (cast_to_void)
12403 0 : call = build_nop (void_type_node, call);
12404 : }
12405 :
12406 94004708 : if (orig_args != NULL)
12407 7376579 : release_tree_vector (orig_args);
12408 :
12409 : return call;
12410 95527228 : }
12411 :
12412 : /* Returns true iff standard conversion sequence ICS1 is a proper
12413 : subsequence of ICS2. */
12414 :
12415 : static bool
12416 72065736 : is_subseq (conversion *ics1, conversion *ics2)
12417 : {
12418 : /* We can assume that a conversion of the same code
12419 : between the same types indicates a subsequence since we only get
12420 : here if the types we are converting from are the same. */
12421 :
12422 72065736 : while (ics1->kind == ck_rvalue
12423 84094458 : || ics1->kind == ck_lvalue)
12424 12028722 : ics1 = next_conversion (ics1);
12425 :
12426 : while (1)
12427 : {
12428 95380062 : while (ics2->kind == ck_rvalue
12429 95380062 : || ics2->kind == ck_lvalue)
12430 12028722 : ics2 = next_conversion (ics2);
12431 :
12432 83351340 : if (ics2->kind == ck_user
12433 83351340 : || !has_next (ics2->kind))
12434 : /* At this point, ICS1 cannot be a proper subsequence of
12435 : ICS2. We can get a USER_CONV when we are comparing the
12436 : second standard conversion sequence of two user conversion
12437 : sequences. */
12438 : return false;
12439 :
12440 11290018 : ics2 = next_conversion (ics2);
12441 :
12442 11290018 : while (ics2->kind == ck_rvalue
12443 17778836 : || ics2->kind == ck_lvalue)
12444 6488818 : ics2 = next_conversion (ics2);
12445 :
12446 11290018 : if (ics2->kind == ics1->kind
12447 4459 : && same_type_p (ics2->type, ics1->type)
12448 11294432 : && (ics1->kind == ck_identity
12449 4414 : || same_type_p (next_conversion (ics2)->type,
12450 : next_conversion (ics1)->type)))
12451 4414 : return true;
12452 : }
12453 : }
12454 :
12455 : /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
12456 : be any _TYPE nodes. */
12457 :
12458 : bool
12459 359582745 : is_properly_derived_from (tree derived, tree base)
12460 : {
12461 359582745 : if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
12462 : return false;
12463 :
12464 : /* We only allow proper derivation here. The DERIVED_FROM_P macro
12465 : considers every class derived from itself. */
12466 340647096 : return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
12467 340647096 : && DERIVED_FROM_P (base, derived));
12468 : }
12469 :
12470 : /* We build the ICS for an implicit object parameter as a pointer
12471 : conversion sequence. However, such a sequence should be compared
12472 : as if it were a reference conversion sequence. If ICS is the
12473 : implicit conversion sequence for an implicit object parameter,
12474 : modify it accordingly. */
12475 :
12476 : static void
12477 153466382 : maybe_handle_implicit_object (conversion **ics)
12478 : {
12479 153466382 : if ((*ics)->this_p)
12480 : {
12481 : /* [over.match.funcs]
12482 :
12483 : For non-static member functions, the type of the
12484 : implicit object parameter is "reference to cv X"
12485 : where X is the class of which the function is a
12486 : member and cv is the cv-qualification on the member
12487 : function declaration. */
12488 12797131 : conversion *t = *ics;
12489 12797131 : tree reference_type;
12490 :
12491 : /* The `this' parameter is a pointer to a class type. Make the
12492 : implicit conversion talk about a reference to that same class
12493 : type. */
12494 12797131 : reference_type = TREE_TYPE (t->type);
12495 12797131 : reference_type = build_reference_type (reference_type);
12496 :
12497 12797131 : if (t->kind == ck_qual)
12498 3218252 : t = next_conversion (t);
12499 12797131 : if (t->kind == ck_ptr)
12500 586091 : t = next_conversion (t);
12501 12797131 : t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
12502 12797131 : t = direct_reference_binding (reference_type, t);
12503 12797131 : t->this_p = 1;
12504 12797131 : t->rvaluedness_matches_p = 0;
12505 12797131 : *ics = t;
12506 : }
12507 153466382 : }
12508 :
12509 : /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
12510 : and return the initial reference binding conversion. Otherwise,
12511 : leave *ICS unchanged and return NULL. */
12512 :
12513 : static conversion *
12514 153466382 : maybe_handle_ref_bind (conversion **ics)
12515 : {
12516 153466382 : if ((*ics)->kind == ck_ref_bind)
12517 : {
12518 57679403 : conversion *old_ics = *ics;
12519 57679403 : *ics = next_conversion (old_ics);
12520 57679403 : (*ics)->user_conv_p = old_ics->user_conv_p;
12521 57679403 : return old_ics;
12522 : }
12523 :
12524 : return NULL;
12525 : }
12526 :
12527 : /* Get the expression at the beginning of the conversion chain C. */
12528 :
12529 : static tree
12530 51 : conv_get_original_expr (conversion *c)
12531 : {
12532 60 : for (; c; c = next_conversion (c))
12533 60 : if (c->kind == ck_identity || c->kind == ck_ambig || c->kind == ck_aggr)
12534 51 : return c->u.expr;
12535 : return NULL_TREE;
12536 : }
12537 :
12538 : /* Return a tree representing the number of elements initialized by the
12539 : list-initialization C. The caller must check that C converts to an
12540 : array type. */
12541 :
12542 : static tree
12543 126 : nelts_initialized_by_list_init (conversion *c)
12544 : {
12545 : /* If the array we're converting to has a dimension, we'll use that. */
12546 126 : if (TYPE_DOMAIN (c->type))
12547 84 : return array_type_nelts_top (c->type);
12548 : else
12549 : {
12550 : /* Otherwise, we look at how many elements the constructor we're
12551 : initializing from has. */
12552 42 : tree ctor = conv_get_original_expr (c);
12553 72 : return size_int (CONSTRUCTOR_NELTS (ctor));
12554 : }
12555 : }
12556 :
12557 : /* True iff C is a conversion that binds a reference or a pointer to
12558 : an array of unknown bound. */
12559 :
12560 : static inline bool
12561 28855132 : conv_binds_to_array_of_unknown_bound (conversion *c)
12562 : {
12563 : /* ck_ref_bind won't have the reference stripped. */
12564 28855132 : tree type = non_reference (c->type);
12565 : /* ck_qual won't have the pointer stripped. */
12566 28855132 : type = strip_pointer_operator (type);
12567 28855132 : return (TREE_CODE (type) == ARRAY_TYPE
12568 28855132 : && TYPE_DOMAIN (type) == NULL_TREE);
12569 : }
12570 :
12571 : /* Compare two implicit conversion sequences according to the rules set out in
12572 : [over.ics.rank]. Return values:
12573 :
12574 : 1: ics1 is better than ics2
12575 : -1: ics2 is better than ics1
12576 : 0: ics1 and ics2 are indistinguishable */
12577 :
12578 : static int
12579 76733232 : compare_ics (conversion *ics1, conversion *ics2)
12580 : {
12581 76733232 : tree from_type1;
12582 76733232 : tree from_type2;
12583 76733232 : tree to_type1;
12584 76733232 : tree to_type2;
12585 76733232 : tree deref_from_type1 = NULL_TREE;
12586 76733232 : tree deref_from_type2 = NULL_TREE;
12587 76733232 : tree deref_to_type1 = NULL_TREE;
12588 76733232 : tree deref_to_type2 = NULL_TREE;
12589 76733232 : conversion_rank rank1, rank2;
12590 :
12591 : /* REF_BINDING is nonzero if the result of the conversion sequence
12592 : is a reference type. In that case REF_CONV is the reference
12593 : binding conversion. */
12594 76733232 : conversion *ref_conv1;
12595 76733232 : conversion *ref_conv2;
12596 :
12597 : /* Compare badness before stripping the reference conversion. */
12598 76733232 : if (ics1->bad_p > ics2->bad_p)
12599 : return -1;
12600 76733218 : else if (ics1->bad_p < ics2->bad_p)
12601 : return 1;
12602 :
12603 : /* Handle implicit object parameters. */
12604 76733191 : maybe_handle_implicit_object (&ics1);
12605 76733191 : maybe_handle_implicit_object (&ics2);
12606 :
12607 : /* Handle reference parameters. */
12608 76733191 : ref_conv1 = maybe_handle_ref_bind (&ics1);
12609 76733191 : ref_conv2 = maybe_handle_ref_bind (&ics2);
12610 :
12611 : /* List-initialization sequence L1 is a better conversion sequence than
12612 : list-initialization sequence L2 if L1 converts to
12613 : std::initializer_list<X> for some X and L2 does not. */
12614 76733191 : if (ics1->kind == ck_list && ics2->kind != ck_list)
12615 : return 1;
12616 76732402 : if (ics2->kind == ck_list && ics1->kind != ck_list)
12617 : return -1;
12618 :
12619 : /* [over.ics.rank]
12620 :
12621 : When comparing the basic forms of implicit conversion sequences (as
12622 : defined in _over.best.ics_)
12623 :
12624 : --a standard conversion sequence (_over.ics.scs_) is a better
12625 : conversion sequence than a user-defined conversion sequence
12626 : or an ellipsis conversion sequence, and
12627 :
12628 : --a user-defined conversion sequence (_over.ics.user_) is a
12629 : better conversion sequence than an ellipsis conversion sequence
12630 : (_over.ics.ellipsis_). */
12631 : /* Use BAD_CONVERSION_RANK because we already checked for a badness
12632 : mismatch. If both ICS are bad, we try to make a decision based on
12633 : what would have happened if they'd been good. This is not an
12634 : extension, we'll still give an error when we build up the call; this
12635 : just helps us give a more helpful error message. */
12636 76732156 : rank1 = BAD_CONVERSION_RANK (ics1);
12637 76732156 : rank2 = BAD_CONVERSION_RANK (ics2);
12638 :
12639 76732156 : if (rank1 > rank2)
12640 : return -1;
12641 66228330 : else if (rank1 < rank2)
12642 : return 1;
12643 :
12644 36432270 : if (ics1->ellipsis_p)
12645 : /* Both conversions are ellipsis conversions. */
12646 : return 0;
12647 :
12648 : /* User-defined conversion sequence U1 is a better conversion sequence
12649 : than another user-defined conversion sequence U2 if they contain the
12650 : same user-defined conversion operator or constructor and if the sec-
12651 : ond standard conversion sequence of U1 is better than the second
12652 : standard conversion sequence of U2. */
12653 :
12654 : /* Handle list-conversion with the same code even though it isn't always
12655 : ranked as a user-defined conversion and it doesn't have a second
12656 : standard conversion sequence; it will still have the desired effect.
12657 : Specifically, we need to do the reference binding comparison at the
12658 : end of this function. */
12659 :
12660 36432224 : if (ics1->user_conv_p || ics1->kind == ck_list
12661 35773656 : || ics1->kind == ck_aggr || ics2->kind == ck_aggr)
12662 : {
12663 658637 : conversion *t1 = strip_standard_conversion (ics1);
12664 658637 : conversion *t2 = strip_standard_conversion (ics2);
12665 :
12666 658637 : if (!t1 || !t2 || t1->kind != t2->kind)
12667 : return 0;
12668 658618 : else if (t1->kind == ck_user)
12669 : {
12670 644625 : tree f1 = t1->cand ? t1->cand->fn : t1->type;
12671 644625 : tree f2 = t2->cand ? t2->cand->fn : t2->type;
12672 644625 : if (f1 != f2)
12673 : return 0;
12674 : }
12675 : /* List-initialization sequence L1 is a better conversion sequence than
12676 : list-initialization sequence L2 if
12677 :
12678 : -- L1 and L2 convert to arrays of the same element type, and either
12679 : the number of elements n1 initialized by L1 is less than the number
12680 : of elements n2 initialized by L2, or n1=n2 and L2 converts to an array
12681 : of unknown bound and L1 does not. (Added in CWG 1307 and extended by
12682 : P0388R4.) */
12683 13993 : else if (t1->kind == ck_aggr
12684 13160 : && TREE_CODE (t1->type) == ARRAY_TYPE
12685 66 : && TREE_CODE (t2->type) == ARRAY_TYPE
12686 14059 : && same_type_p (TREE_TYPE (t1->type), TREE_TYPE (t2->type)))
12687 : {
12688 63 : tree n1 = nelts_initialized_by_list_init (t1);
12689 63 : tree n2 = nelts_initialized_by_list_init (t2);
12690 63 : if (tree_int_cst_lt (n1, n2))
12691 : return 1;
12692 24 : else if (tree_int_cst_lt (n2, n1))
12693 : return -1;
12694 : /* The n1 == n2 case. */
12695 24 : bool c1 = conv_binds_to_array_of_unknown_bound (t1);
12696 24 : bool c2 = conv_binds_to_array_of_unknown_bound (t2);
12697 24 : if (c1 && !c2)
12698 : return -1;
12699 6 : else if (!c1 && c2)
12700 : return 1;
12701 : else
12702 : return 0;
12703 : }
12704 : else
12705 : {
12706 : /* For ambiguous or aggregate conversions, use the target type as
12707 : a proxy for the conversion function. */
12708 13930 : if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
12709 : return 0;
12710 : }
12711 :
12712 : /* We can just fall through here, after setting up
12713 : FROM_TYPE1 and FROM_TYPE2. */
12714 655854 : from_type1 = t1->type;
12715 655854 : from_type2 = t2->type;
12716 655854 : }
12717 : else
12718 : {
12719 : conversion *t1;
12720 : conversion *t2;
12721 :
12722 : /* We're dealing with two standard conversion sequences.
12723 :
12724 : [over.ics.rank]
12725 :
12726 : Standard conversion sequence S1 is a better conversion
12727 : sequence than standard conversion sequence S2 if
12728 :
12729 : --S1 is a proper subsequence of S2 (comparing the conversion
12730 : sequences in the canonical form defined by _over.ics.scs_,
12731 : excluding any Lvalue Transformation; the identity
12732 : conversion sequence is considered to be a subsequence of
12733 : any non-identity conversion sequence */
12734 :
12735 : t1 = ics1;
12736 50764441 : while (t1->kind != ck_identity)
12737 14990854 : t1 = next_conversion (t1);
12738 35773587 : from_type1 = t1->type;
12739 :
12740 35773587 : t2 = ics2;
12741 50662947 : while (t2->kind != ck_identity)
12742 14889360 : t2 = next_conversion (t2);
12743 35773587 : from_type2 = t2->type;
12744 : }
12745 :
12746 : /* One sequence can only be a subsequence of the other if they start with
12747 : the same type. They can start with different types when comparing the
12748 : second standard conversion sequence in two user-defined conversion
12749 : sequences. */
12750 36429441 : if (same_type_p (from_type1, from_type2))
12751 : {
12752 36035046 : if (is_subseq (ics1, ics2))
12753 : return 1;
12754 36030690 : if (is_subseq (ics2, ics1))
12755 : return -1;
12756 : }
12757 :
12758 : /* [over.ics.rank]
12759 :
12760 : Or, if not that,
12761 :
12762 : --the rank of S1 is better than the rank of S2 (by the rules
12763 : defined below):
12764 :
12765 : Standard conversion sequences are ordered by their ranks: an Exact
12766 : Match is a better conversion than a Promotion, which is a better
12767 : conversion than a Conversion.
12768 :
12769 : Two conversion sequences with the same rank are indistinguishable
12770 : unless one of the following rules applies:
12771 :
12772 : --A conversion that does not a convert a pointer, pointer to member,
12773 : or std::nullptr_t to bool is better than one that does.
12774 :
12775 : The ICS_STD_RANK automatically handles the pointer-to-bool rule,
12776 : so that we do not have to check it explicitly. */
12777 36425027 : if (ics1->rank < ics2->rank)
12778 : return 1;
12779 36424947 : else if (ics2->rank < ics1->rank)
12780 : return -1;
12781 :
12782 36424947 : to_type1 = ics1->type;
12783 36424947 : to_type2 = ics2->type;
12784 :
12785 : /* A conversion from scalar arithmetic type to complex is worse than a
12786 : conversion between scalar arithmetic types. */
12787 36424947 : if (same_type_p (from_type1, from_type2)
12788 36030552 : && ARITHMETIC_TYPE_P (from_type1)
12789 7358309 : && ARITHMETIC_TYPE_P (to_type1)
12790 7358142 : && ARITHMETIC_TYPE_P (to_type2)
12791 36424947 : && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
12792 7358100 : != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
12793 : {
12794 106 : if (TREE_CODE (to_type1) == COMPLEX_TYPE)
12795 : return -1;
12796 : else
12797 : return 1;
12798 : }
12799 :
12800 36424841 : {
12801 : /* A conversion in either direction between floating-point type FP1 and
12802 : floating-point type FP2 is better than a conversion in the same
12803 : direction between FP1 and arithmetic type T3 if
12804 : - the floating-point conversion rank of FP1 is equal to the rank of
12805 : FP2, and
12806 : - T3 is not a floating-point type, or T3 is a floating-point type
12807 : whose rank is not equal to the rank of FP1, or the floating-point
12808 : conversion subrank of FP2 is greater than the subrank of T3. */
12809 36424841 : tree fp1 = from_type1;
12810 36424841 : tree fp2 = to_type1;
12811 36424841 : tree fp3 = from_type2;
12812 36424841 : tree t3 = to_type2;
12813 36424841 : int ret = 1;
12814 36424841 : if (TYPE_MAIN_VARIANT (fp2) == TYPE_MAIN_VARIANT (t3))
12815 : {
12816 31794408 : std::swap (fp1, fp2);
12817 31794408 : std::swap (fp3, t3);
12818 : }
12819 36424841 : if (TYPE_MAIN_VARIANT (fp1) == TYPE_MAIN_VARIANT (fp3)
12820 36424756 : && SCALAR_FLOAT_TYPE_P (fp1)
12821 : /* Only apply this rule if at least one of the 3 types is
12822 : extended floating-point type, otherwise keep them as
12823 : before for compatibility reasons with types like __float128.
12824 : float, double and long double alone have different conversion
12825 : ranks and so when just those 3 types are involved, this
12826 : rule doesn't trigger. */
12827 40576755 : && (extended_float_type_p (fp1)
12828 4097165 : || (SCALAR_FLOAT_TYPE_P (fp2) && extended_float_type_p (fp2))
12829 4028465 : || (SCALAR_FLOAT_TYPE_P (t3) && extended_float_type_p (t3))))
12830 : {
12831 123449 : if (TREE_CODE (fp2) != REAL_TYPE)
12832 : {
12833 24448 : ret = -ret;
12834 24448 : std::swap (fp2, t3);
12835 : }
12836 123449 : if (SCALAR_FLOAT_TYPE_P (fp2))
12837 : {
12838 : /* cp_compare_floating_point_conversion_ranks returns -1, 0 or 1
12839 : if the conversion rank is equal (-1 or 1 if the subrank is
12840 : different). */
12841 110088 : if (IN_RANGE (cp_compare_floating_point_conversion_ranks (fp1,
12842 : fp2),
12843 : -1, 1))
12844 : {
12845 : /* Conversion ranks of FP1 and FP2 are equal. */
12846 71582 : if (TREE_CODE (t3) != REAL_TYPE
12847 71582 : || !IN_RANGE (cp_compare_floating_point_conversion_ranks
12848 : (fp1, t3),
12849 : -1, 1))
12850 : /* FP1 <-> FP2 conversion is better. */
12851 71112 : return ret;
12852 470 : int c = cp_compare_floating_point_conversion_ranks (fp2, t3);
12853 470 : gcc_assert (IN_RANGE (c, -1, 1));
12854 470 : if (c == 1)
12855 : /* Conversion subrank of FP2 is greater than subrank of T3.
12856 : FP1 <-> FP2 conversion is better. */
12857 : return ret;
12858 470 : else if (c == -1)
12859 : /* Conversion subrank of FP2 is less than subrank of T3.
12860 : FP1 <-> T3 conversion is better. */
12861 0 : return -ret;
12862 : }
12863 38506 : else if (SCALAR_FLOAT_TYPE_P (t3)
12864 38506 : && IN_RANGE (cp_compare_floating_point_conversion_ranks
12865 : (fp1, t3),
12866 : -1, 1))
12867 : /* Conversion ranks of FP1 and FP2 are not equal, conversion
12868 : ranks of FP1 and T3 are equal.
12869 : FP1 <-> T3 conversion is better. */
12870 7739 : return -ret;
12871 : }
12872 : }
12873 : }
12874 :
12875 36345990 : if (TYPE_PTR_P (from_type1)
12876 3866066 : && TYPE_PTR_P (from_type2)
12877 3866050 : && TYPE_PTR_P (to_type1)
12878 3866026 : && TYPE_PTR_P (to_type2))
12879 : {
12880 3866026 : deref_from_type1 = TREE_TYPE (from_type1);
12881 3866026 : deref_from_type2 = TREE_TYPE (from_type2);
12882 3866026 : deref_to_type1 = TREE_TYPE (to_type1);
12883 3866026 : deref_to_type2 = TREE_TYPE (to_type2);
12884 : }
12885 : /* The rules for pointers to members A::* are just like the rules
12886 : for pointers A*, except opposite: if B is derived from A then
12887 : A::* converts to B::*, not vice versa. For that reason, we
12888 : switch the from_ and to_ variables here. */
12889 58 : else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2)
12890 58 : && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2))
12891 32479964 : || (TYPE_PTRMEMFUNC_P (from_type1)
12892 437 : && TYPE_PTRMEMFUNC_P (from_type2)
12893 437 : && TYPE_PTRMEMFUNC_P (to_type1)
12894 437 : && TYPE_PTRMEMFUNC_P (to_type2)))
12895 : {
12896 495 : deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
12897 495 : deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
12898 495 : deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
12899 495 : deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
12900 : }
12901 :
12902 3866521 : if (deref_from_type1 != NULL_TREE
12903 3866521 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
12904 294092 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
12905 : {
12906 : /* This was one of the pointer or pointer-like conversions.
12907 :
12908 : [over.ics.rank]
12909 :
12910 : --If class B is derived directly or indirectly from class A,
12911 : conversion of B* to A* is better than conversion of B* to
12912 : void*, and conversion of A* to void* is better than
12913 : conversion of B* to void*. */
12914 294092 : if (VOID_TYPE_P (deref_to_type1)
12915 57 : && VOID_TYPE_P (deref_to_type2))
12916 : {
12917 14 : if (is_properly_derived_from (deref_from_type1,
12918 : deref_from_type2))
12919 : return -1;
12920 14 : else if (is_properly_derived_from (deref_from_type2,
12921 : deref_from_type1))
12922 : return 1;
12923 : }
12924 294078 : else if (VOID_TYPE_P (deref_to_type1)
12925 294035 : || VOID_TYPE_P (deref_to_type2))
12926 : {
12927 47 : if (same_type_p (deref_from_type1, deref_from_type2))
12928 : {
12929 47 : if (VOID_TYPE_P (deref_to_type2))
12930 : {
12931 4 : if (is_properly_derived_from (deref_from_type1,
12932 : deref_to_type1))
12933 : return 1;
12934 : }
12935 : /* We know that DEREF_TO_TYPE1 is `void' here. */
12936 43 : else if (is_properly_derived_from (deref_from_type1,
12937 : deref_to_type2))
12938 : return -1;
12939 : }
12940 : }
12941 294031 : else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
12942 294031 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
12943 : {
12944 : /* [over.ics.rank]
12945 :
12946 : --If class B is derived directly or indirectly from class A
12947 : and class C is derived directly or indirectly from B,
12948 :
12949 : --conversion of C* to B* is better than conversion of C* to
12950 : A*,
12951 :
12952 : --conversion of B* to A* is better than conversion of C* to
12953 : A* */
12954 294031 : if (same_type_p (deref_from_type1, deref_from_type2))
12955 : {
12956 294025 : if (is_properly_derived_from (deref_to_type1,
12957 : deref_to_type2))
12958 : return 1;
12959 294025 : else if (is_properly_derived_from (deref_to_type2,
12960 : deref_to_type1))
12961 : return -1;
12962 : }
12963 6 : else if (same_type_p (deref_to_type1, deref_to_type2))
12964 : {
12965 6 : if (is_properly_derived_from (deref_from_type2,
12966 : deref_from_type1))
12967 : return 1;
12968 0 : else if (is_properly_derived_from (deref_from_type1,
12969 : deref_from_type2))
12970 : return -1;
12971 : }
12972 : }
12973 : }
12974 72103796 : else if (CLASS_TYPE_P (non_reference (from_type1))
12975 59027041 : && same_type_p (from_type1, from_type2))
12976 : {
12977 22638621 : tree from = non_reference (from_type1);
12978 :
12979 : /* [over.ics.rank]
12980 :
12981 : --binding of an expression of type C to a reference of type
12982 : B& is better than binding an expression of type C to a
12983 : reference of type A&
12984 :
12985 : --conversion of C to B is better than conversion of C to A, */
12986 22638621 : if (is_properly_derived_from (from, to_type1)
12987 22638621 : && is_properly_derived_from (from, to_type2))
12988 : {
12989 913205 : if (is_properly_derived_from (to_type1, to_type2))
12990 : return 1;
12991 909590 : else if (is_properly_derived_from (to_type2, to_type1))
12992 : return -1;
12993 : }
12994 : }
12995 26826554 : else if (CLASS_TYPE_P (non_reference (to_type1))
12996 13749799 : && same_type_p (to_type1, to_type2))
12997 : {
12998 6 : tree to = non_reference (to_type1);
12999 :
13000 : /* [over.ics.rank]
13001 :
13002 : --binding of an expression of type B to a reference of type
13003 : A& is better than binding an expression of type C to a
13004 : reference of type A&,
13005 :
13006 : --conversion of B to A is better than conversion of C to A */
13007 6 : if (is_properly_derived_from (from_type1, to)
13008 6 : && is_properly_derived_from (from_type2, to))
13009 : {
13010 3 : if (is_properly_derived_from (from_type2, from_type1))
13011 : return 1;
13012 3 : else if (is_properly_derived_from (from_type1, from_type2))
13013 : return -1;
13014 : }
13015 : }
13016 :
13017 : /* [over.ics.rank]
13018 :
13019 : --S1 and S2 differ only in their qualification conversion and yield
13020 : similar types T1 and T2 (_conv.qual_), respectively, and the cv-
13021 : qualification signature of type T1 is a proper subset of the cv-
13022 : qualification signature of type T2 */
13023 36261134 : if (ics1->kind == ck_qual
13024 351 : && ics2->kind == ck_qual
13025 36261485 : && same_type_p (from_type1, from_type2))
13026 : {
13027 351 : int result = comp_cv_qual_signature (to_type1, to_type2);
13028 351 : if (result != 0)
13029 : return result;
13030 : }
13031 :
13032 : /* [over.ics.rank]
13033 :
13034 : --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
13035 : to an implicit object parameter of a non-static member function
13036 : declared without a ref-qualifier, and either S1 binds an lvalue
13037 : reference to an lvalue and S2 binds an rvalue reference or S1 binds an
13038 : rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x
13039 : draft standard, 13.3.3.2)
13040 :
13041 : --S1 and S2 are reference bindings (_dcl.init.ref_), and the
13042 : types to which the references refer are the same type except for
13043 : top-level cv-qualifiers, and the type to which the reference
13044 : initialized by S2 refers is more cv-qualified than the type to
13045 : which the reference initialized by S1 refers.
13046 :
13047 : DR 1328 [over.match.best]: the context is an initialization by
13048 : conversion function for direct reference binding (13.3.1.6) of a
13049 : reference to function type, the return type of F1 is the same kind of
13050 : reference (i.e. lvalue or rvalue) as the reference being initialized,
13051 : and the return type of F2 is not. */
13052 :
13053 36261048 : if (ref_conv1 && ref_conv2)
13054 : {
13055 17164919 : if (!ref_conv1->this_p && !ref_conv2->this_p
13056 16934447 : && (ref_conv1->rvaluedness_matches_p
13057 16934447 : != ref_conv2->rvaluedness_matches_p)
13058 32570744 : && (same_type_p (ref_conv1->type, ref_conv2->type)
13059 9071638 : || (TYPE_REF_IS_RVALUE (ref_conv1->type)
13060 9071638 : != TYPE_REF_IS_RVALUE (ref_conv2->type))))
13061 : {
13062 9071366 : if (ref_conv1->bad_p
13063 9071366 : && !same_type_p (TREE_TYPE (ref_conv1->type),
13064 : TREE_TYPE (ref_conv2->type)))
13065 : /* Don't prefer a bad conversion that drops cv-quals to a bad
13066 : conversion with the wrong rvalueness. */
13067 : return 0;
13068 9069907 : return (ref_conv1->rvaluedness_matches_p
13069 9069907 : - ref_conv2->rvaluedness_matches_p);
13070 : }
13071 :
13072 14427737 : if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
13073 : {
13074 : /* Per P0388R4:
13075 :
13076 : void f (int(&)[]), // (1)
13077 : f (int(&)[1]), // (2)
13078 : f (int*); // (3)
13079 :
13080 : (2) is better than (1), but (3) should be equal to (1) and to
13081 : (2). For that reason we don't use ck_qual for (1) which would
13082 : give it the cr_exact rank while (3) remains ck_identity.
13083 : Therefore we compare (1) and (2) here. For (1) we'll have
13084 :
13085 : ck_ref_bind <- ck_identity
13086 : int[] & int[1]
13087 :
13088 : so to handle this we must look at ref_conv. */
13089 14427306 : bool c1 = conv_binds_to_array_of_unknown_bound (ref_conv1);
13090 14427306 : bool c2 = conv_binds_to_array_of_unknown_bound (ref_conv2);
13091 14427306 : if (c1 && !c2)
13092 : return -1;
13093 14427300 : else if (!c1 && c2)
13094 : return 1;
13095 :
13096 14427300 : int q1 = cp_type_quals (TREE_TYPE (ref_conv1->type));
13097 14427300 : int q2 = cp_type_quals (TREE_TYPE (ref_conv2->type));
13098 14427300 : if (ref_conv1->bad_p)
13099 : {
13100 : /* Prefer the one that drops fewer cv-quals. */
13101 1606 : tree ftype = next_conversion (ref_conv1)->type;
13102 1606 : int fquals = cp_type_quals (ftype);
13103 1606 : q1 ^= fquals;
13104 1606 : q2 ^= fquals;
13105 : }
13106 14427300 : return comp_cv_qualification (q2, q1);
13107 : }
13108 : }
13109 :
13110 : /* [over.ics.rank]
13111 :
13112 : Per CWG 1601:
13113 : -- A conversion that promotes an enumeration whose underlying type
13114 : is fixed to its underlying type is better than one that promotes to
13115 : the promoted underlying type, if the two are different. */
13116 12762376 : if (ics1->rank == cr_promotion
13117 145 : && ics2->rank == cr_promotion
13118 145 : && UNSCOPED_ENUM_P (from_type1)
13119 27 : && ENUM_FIXED_UNDERLYING_TYPE_P (from_type1)
13120 12762400 : && same_type_p (from_type1, from_type2))
13121 : {
13122 24 : tree utype = ENUM_UNDERLYING_TYPE (from_type1);
13123 24 : tree prom = type_promotes_to (from_type1);
13124 24 : if (!same_type_p (utype, prom))
13125 : {
13126 12 : if (same_type_p (to_type1, utype)
13127 12 : && same_type_p (to_type2, prom))
13128 : return 1;
13129 6 : else if (same_type_p (to_type2, utype)
13130 6 : && same_type_p (to_type1, prom))
13131 : return -1;
13132 : }
13133 : }
13134 :
13135 : /* Neither conversion sequence is better than the other. */
13136 : return 0;
13137 : }
13138 :
13139 : /* The source type for this standard conversion sequence. */
13140 :
13141 : static tree
13142 9 : source_type (conversion *t)
13143 : {
13144 9 : return strip_standard_conversion (t)->type;
13145 : }
13146 :
13147 : /* Note a warning about preferring WINNER to LOSER. We do this by storing
13148 : a pointer to LOSER and re-running joust to produce the warning if WINNER
13149 : is actually used. */
13150 :
13151 : static void
13152 446 : add_warning (struct z_candidate *winner, struct z_candidate *loser)
13153 : {
13154 446 : candidate_warning *cw = (candidate_warning *)
13155 0 : conversion_obstack_alloc (sizeof (candidate_warning));
13156 446 : cw->loser = loser;
13157 446 : cw->next = winner->warnings;
13158 446 : winner->warnings = cw;
13159 446 : }
13160 :
13161 : /* CAND is a constructor candidate in joust in C++17 and up. If it copies a
13162 : prvalue returned from a conversion function, return true. Otherwise, return
13163 : false. */
13164 :
13165 : static bool
13166 795010 : joust_maybe_elide_copy (z_candidate *cand)
13167 : {
13168 795010 : tree fn = cand->fn;
13169 1995836 : if (!DECL_COPY_CONSTRUCTOR_P (fn) && !DECL_MOVE_CONSTRUCTOR_P (fn))
13170 405735 : return false;
13171 389275 : conversion *conv = cand->convs[0];
13172 389275 : if (conv->kind == ck_ambig)
13173 : return false;
13174 389273 : gcc_checking_assert (conv->kind == ck_ref_bind);
13175 389273 : conv = next_conversion (conv);
13176 389273 : if (conv->kind == ck_user && !TYPE_REF_P (conv->type))
13177 : {
13178 122 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
13179 : (conv->type, DECL_CONTEXT (fn)));
13180 122 : z_candidate *uc = conv->cand;
13181 122 : if (DECL_CONV_FN_P (uc->fn))
13182 : return true;
13183 : }
13184 : return false;
13185 : }
13186 :
13187 : /* Return the class that CAND's implicit object parameter refers to. */
13188 :
13189 : static tree
13190 284467 : class_of_implicit_object (z_candidate *cand)
13191 : {
13192 284467 : if (!DECL_IOBJ_MEMBER_FUNCTION_P (cand->fn))
13193 : return NULL_TREE;
13194 :
13195 : /* "For conversion functions that are implicit object member functions,
13196 : the function is considered to be a member of the class of the implied
13197 : object argument for the purpose of defining the type of the implicit
13198 : object parameter." */
13199 284467 : if (DECL_CONV_FN_P (cand->fn))
13200 0 : return TYPE_MAIN_VARIANT (TREE_TYPE (cand->first_arg));
13201 :
13202 : /* "For non-conversion functions that are implicit object member
13203 : functions nominated by a using-declaration in a derived class, the
13204 : function is considered to be a member of the derived class for the
13205 : purpose of defining the type of the implicit object parameter."
13206 :
13207 : That derived class is reflected in the conversion_path binfo. */
13208 284467 : return BINFO_TYPE (cand->conversion_path);
13209 : }
13210 :
13211 : /* Return whether the first parameter of C1 matches the second parameter
13212 : of C2. */
13213 :
13214 : static bool
13215 536407 : reversed_match (z_candidate *c1, z_candidate *c2)
13216 : {
13217 536407 : tree fn1 = c1->fn;
13218 536407 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (c2->fn));
13219 536407 : tree parm2 = TREE_VALUE (TREE_CHAIN (parms2));
13220 536407 : if (DECL_IOBJ_MEMBER_FUNCTION_P (fn1))
13221 : {
13222 284467 : tree ctx = class_of_implicit_object (c1);
13223 284467 : return iobj_parm_corresponds_to (fn1, parm2, ctx);
13224 : }
13225 : else
13226 : {
13227 251940 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn1));
13228 251940 : tree parm1 = TREE_VALUE (parms1);
13229 251940 : return same_type_p (parm1, parm2);
13230 : }
13231 : }
13232 :
13233 : /* True if the defining declarations of the two candidates have equivalent
13234 : parameters. MATCH_KIND controls whether we're trying to compare the
13235 : original declarations (for a warning) or the actual candidates. */
13236 :
13237 : enum class pmatch { original, current };
13238 :
13239 : static bool
13240 4664630 : cand_parms_match (z_candidate *c1, z_candidate *c2, pmatch match_kind)
13241 : {
13242 4664630 : tree fn1 = c1->fn;
13243 4664630 : tree fn2 = c2->fn;
13244 4664630 : bool reversed = (match_kind == pmatch::current
13245 4664630 : && c1->reversed () != c2->reversed ());
13246 4664630 : if (fn1 == fn2 && !reversed)
13247 : return true;
13248 4664381 : if (identifier_p (fn1) || identifier_p (fn2))
13249 : return false;
13250 4664364 : if (match_kind == pmatch::original)
13251 : {
13252 : /* We don't look at c1->template_decl because that's only set for
13253 : primary templates, not e.g. non-template member functions of
13254 : class templates. */
13255 22 : tree t1 = most_general_template (fn1);
13256 22 : tree t2 = most_general_template (fn2);
13257 22 : if (t1 || t2)
13258 : {
13259 15 : if (!t1 || !t2)
13260 : return false;
13261 15 : if (t1 == t2)
13262 : return true;
13263 3 : fn1 = DECL_TEMPLATE_RESULT (t1);
13264 3 : fn2 = DECL_TEMPLATE_RESULT (t2);
13265 : }
13266 : }
13267 :
13268 4664352 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn1));
13269 4664352 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (fn2));
13270 :
13271 9120042 : if (DECL_FUNCTION_MEMBER_P (fn1)
13272 4664765 : && DECL_FUNCTION_MEMBER_P (fn2))
13273 : {
13274 209061 : tree base1 = DECL_CONTEXT (strip_inheriting_ctors (fn1));
13275 209061 : tree base2 = DECL_CONTEXT (strip_inheriting_ctors (fn2));
13276 209061 : if (base1 != base2)
13277 142693 : return false;
13278 :
13279 208884 : if (reversed)
13280 142516 : return (reversed_match (c1, c2)
13281 142516 : && reversed_match (c2, c1));
13282 :
13283 : /* Use object_parms_correspond to simplify comparing iobj/xobj/static
13284 : member functions. */
13285 66368 : if (!object_parms_correspond (fn1, fn2, base1))
13286 : return false;
13287 :
13288 : /* We just compared the object parameters, if they don't correspond
13289 : we already returned false. */
13290 199104 : auto skip_parms = [] (tree fn, tree parms)
13291 : {
13292 132736 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
13293 680 : return TREE_CHAIN (parms);
13294 : else
13295 132056 : return skip_artificial_parms_for (fn, parms);
13296 : };
13297 66368 : parms1 = skip_parms (fn1, parms1);
13298 66368 : parms2 = skip_parms (fn2, parms2);
13299 : }
13300 4455291 : else if (reversed)
13301 125951 : return (reversed_match (c1, c2)
13302 125951 : && reversed_match (c2, c1));
13303 4395708 : return compparms (parms1, parms2);
13304 : }
13305 :
13306 : /* True iff FN is a copy or move constructor or assignment operator. */
13307 :
13308 : static bool
13309 19199244 : sfk_copy_or_move (tree fn)
13310 : {
13311 19199244 : if (TREE_CODE (fn) != FUNCTION_DECL)
13312 : return false;
13313 19199154 : special_function_kind sfk = special_function_p (fn);
13314 19199154 : return sfk >= sfk_copy_constructor && sfk <= sfk_move_assignment;
13315 : }
13316 :
13317 : /* Compare two candidates for overloading as described in
13318 : [over.match.best]. Return values:
13319 :
13320 : 1: cand1 is better than cand2
13321 : -1: cand2 is better than cand1
13322 : 0: cand1 and cand2 are indistinguishable */
13323 :
13324 : static int
13325 72813599 : joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
13326 : tsubst_flags_t complain)
13327 : {
13328 72813599 : int winner = 0;
13329 72813599 : int off1 = 0, off2 = 0;
13330 72813599 : size_t i;
13331 72813599 : size_t len;
13332 :
13333 : /* Candidates that involve bad conversions are always worse than those
13334 : that don't. */
13335 72813599 : if (cand1->viable > cand2->viable)
13336 : return 1;
13337 58765407 : if (cand1->viable < cand2->viable)
13338 : return -1;
13339 :
13340 : /* If we have two pseudo-candidates for conversions to the same type,
13341 : or two candidates for the same function, arbitrarily pick one. */
13342 58765407 : if (cand1->fn == cand2->fn
13343 1785525 : && cand1->reversed () == cand2->reversed ()
13344 59555804 : && (IS_TYPE_OR_DECL_P (cand1->fn)))
13345 : return 1;
13346 :
13347 : /* Prefer a non-deleted function over an implicitly deleted move
13348 : constructor or assignment operator. This differs slightly from the
13349 : wording for issue 1402 (which says the move op is ignored by overload
13350 : resolution), but this way produces better error messages. */
13351 58765407 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13352 54596832 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13353 113354826 : && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn))
13354 : {
13355 2318423 : if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn)
13356 1938725 : && move_fn_p (cand1->fn))
13357 : return -1;
13358 3490620 : if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn)
13359 2846042 : && move_fn_p (cand2->fn))
13360 : return 1;
13361 : }
13362 :
13363 : /* a viable function F1
13364 : is defined to be a better function than another viable function F2 if
13365 : for all arguments i, ICSi(F1) is not a worse conversion sequence than
13366 : ICSi(F2), and then */
13367 :
13368 : /* for some argument j, ICSj(F1) is a better conversion sequence than
13369 : ICSj(F2) */
13370 :
13371 : /* For comparing static and non-static member functions, we ignore
13372 : the implicit object parameter of the non-static function. The
13373 : standard says to pretend that the static function has an object
13374 : parm, but that won't work with operator overloading. */
13375 58751795 : len = cand1->num_convs;
13376 58751795 : if (len != cand2->num_convs)
13377 : {
13378 441 : int static_1 = (TREE_CODE (cand1->fn) == FUNCTION_DECL
13379 441 : && DECL_STATIC_FUNCTION_P (cand1->fn));
13380 441 : int static_2 = (TREE_CODE (cand2->fn) == FUNCTION_DECL
13381 441 : && DECL_STATIC_FUNCTION_P (cand2->fn));
13382 :
13383 441 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13384 139 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13385 139 : && DECL_CONSTRUCTOR_P (cand1->fn)
13386 447 : && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
13387 : /* We're comparing a near-match list constructor and a near-match
13388 : non-list constructor. Just treat them as unordered. */
13389 : return 0;
13390 :
13391 435 : gcc_assert (static_1 != static_2);
13392 :
13393 435 : if (static_1)
13394 : {
13395 : /* C++23 [over.best.ics.general] says:
13396 : When the parameter is the implicit object parameter of a static
13397 : member function, the implicit conversion sequence is a standard
13398 : conversion sequence that is neither better nor worse than any
13399 : other standard conversion sequence. */
13400 46 : if (CONVERSION_RANK (cand2->convs[0]) >= cr_user)
13401 0 : winner = 1;
13402 : off2 = 1;
13403 : }
13404 : else
13405 : {
13406 389 : if (CONVERSION_RANK (cand1->convs[0]) >= cr_user)
13407 302 : winner = -1;
13408 389 : off1 = 1;
13409 389 : --len;
13410 : }
13411 : }
13412 :
13413 135433048 : for (i = 0; i < len; ++i)
13414 : {
13415 76682505 : conversion *t1 = cand1->convs[i + off1];
13416 76682505 : conversion *t2 = cand2->convs[i + off2];
13417 76682505 : int comp = compare_ics (t1, t2);
13418 :
13419 76682505 : if (comp != 0)
13420 : {
13421 53025989 : if ((complain & tf_warning)
13422 43888253 : && warn_sign_promo
13423 82 : && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
13424 : == cr_std + cr_promotion)
13425 50 : && t1->kind == ck_std
13426 31 : && t2->kind == ck_std
13427 31 : && TREE_CODE (t1->type) == INTEGER_TYPE
13428 31 : && TREE_CODE (t2->type) == INTEGER_TYPE
13429 31 : && (TYPE_PRECISION (t1->type)
13430 31 : == TYPE_PRECISION (t2->type))
13431 53026020 : && (TYPE_UNSIGNED (next_conversion (t1)->type)
13432 0 : || (TREE_CODE (next_conversion (t1)->type)
13433 : == ENUMERAL_TYPE)))
13434 : {
13435 31 : tree type = next_conversion (t1)->type;
13436 31 : tree type1, type2;
13437 31 : struct z_candidate *w, *l;
13438 31 : if (comp > 0)
13439 : type1 = t1->type, type2 = t2->type,
13440 : w = cand1, l = cand2;
13441 : else
13442 8 : type1 = t2->type, type2 = t1->type,
13443 8 : w = cand2, l = cand1;
13444 :
13445 31 : if (warn)
13446 : {
13447 9 : warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
13448 : type, type1, type2);
13449 9 : warning (OPT_Wsign_promo, " in call to %qD", w->fn);
13450 : }
13451 : else
13452 22 : add_warning (w, l);
13453 : }
13454 :
13455 53025989 : if (winner && comp != winner)
13456 : {
13457 : /* Ambiguity between normal and reversed comparison operators
13458 : with the same parameter types. P2468 decided not to go with
13459 : this approach to resolving the ambiguity, so pedwarn. */
13460 1246 : if ((complain & tf_warning_or_error)
13461 690 : && (cand1->reversed () != cand2->reversed ())
13462 1534 : && cand_parms_match (cand1, cand2, pmatch::original))
13463 : {
13464 273 : struct z_candidate *w, *l;
13465 273 : if (cand2->reversed ())
13466 : winner = 1, w = cand1, l = cand2;
13467 : else
13468 251 : winner = -1, w = cand2, l = cand1;
13469 273 : if (warn)
13470 : {
13471 22 : auto_diagnostic_group d;
13472 22 : if (pedwarn (input_location, 0,
13473 : "C++20 says that these are ambiguous, "
13474 : "even though the second is reversed:"))
13475 : {
13476 22 : print_z_candidate (input_location,
13477 : N_("candidate 1:"), w);
13478 22 : print_z_candidate (input_location,
13479 : N_("candidate 2:"), l);
13480 22 : if (w->fn == l->fn
13481 17 : && DECL_IOBJ_MEMBER_FUNCTION_P (w->fn)
13482 37 : && (type_memfn_quals (TREE_TYPE (w->fn))
13483 15 : & TYPE_QUAL_CONST) == 0)
13484 : {
13485 : /* Suggest adding const to
13486 : struct A { bool operator==(const A&); }; */
13487 12 : tree parmtype
13488 12 : = FUNCTION_FIRST_USER_PARMTYPE (w->fn);
13489 12 : parmtype = TREE_VALUE (parmtype);
13490 12 : if (TYPE_REF_P (parmtype)
13491 12 : && TYPE_READONLY (TREE_TYPE (parmtype))
13492 24 : && (same_type_ignoring_top_level_qualifiers_p
13493 12 : (TREE_TYPE (parmtype),
13494 12 : DECL_CONTEXT (w->fn))))
13495 12 : inform (DECL_SOURCE_LOCATION (w->fn),
13496 : "try making the operator a %<const%> "
13497 : "member function");
13498 : }
13499 : }
13500 22 : }
13501 : else
13502 251 : add_warning (w, l);
13503 273 : return winner;
13504 : }
13505 :
13506 973 : winner = 0;
13507 973 : goto tweak;
13508 : }
13509 : winner = comp;
13510 : }
13511 : }
13512 :
13513 : /* warn about confusing overload resolution for user-defined conversions,
13514 : either between a constructor and a conversion op, or between two
13515 : conversion ops. */
13516 58750543 : if ((complain & tf_warning)
13517 : /* In C++17, the constructor might have been elided, which means that
13518 : an originally null ->second_conv could become non-null. */
13519 48085817 : && winner && warn_conversion && cand1->second_conv && cand2->second_conv
13520 54 : && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
13521 58750570 : && winner != compare_ics (cand1->second_conv, cand2->second_conv))
13522 : {
13523 27 : struct z_candidate *w, *l;
13524 27 : bool give_warning = false;
13525 :
13526 27 : if (winner == 1)
13527 : w = cand1, l = cand2;
13528 : else
13529 6 : w = cand2, l = cand1;
13530 :
13531 : /* We don't want to complain about `X::operator T1 ()'
13532 : beating `X::operator T2 () const', when T2 is a no less
13533 : cv-qualified version of T1. */
13534 27 : if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
13535 39 : && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
13536 : {
13537 6 : tree t = TREE_TYPE (TREE_TYPE (l->fn));
13538 6 : tree f = TREE_TYPE (TREE_TYPE (w->fn));
13539 :
13540 6 : if (TREE_CODE (t) == TREE_CODE (f) && INDIRECT_TYPE_P (t))
13541 : {
13542 6 : t = TREE_TYPE (t);
13543 6 : f = TREE_TYPE (f);
13544 : }
13545 6 : if (!comp_ptr_ttypes (t, f))
13546 : give_warning = true;
13547 : }
13548 : else
13549 : give_warning = true;
13550 :
13551 : if (!give_warning)
13552 : /*NOP*/;
13553 21 : else if (warn)
13554 : {
13555 9 : tree source = source_type (w->convs[0]);
13556 9 : if (INDIRECT_TYPE_P (source))
13557 6 : source = TREE_TYPE (source);
13558 9 : auto_diagnostic_group d;
13559 9 : if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
13560 18 : && warning (OPT_Wconversion, " for conversion from %qH to %qI",
13561 9 : source, w->second_conv->type))
13562 : {
13563 9 : inform (input_location, " because conversion sequence "
13564 : "for the argument is better");
13565 : }
13566 9 : }
13567 : else
13568 12 : add_warning (w, l);
13569 : }
13570 :
13571 58750543 : if (winner)
13572 : return winner;
13573 :
13574 : /* DR 495 moved this tiebreaker above the template ones. */
13575 : /* or, if not that,
13576 : the context is an initialization by user-defined conversion (see
13577 : _dcl.init_ and _over.match.user_) and the standard conversion
13578 : sequence from the return type of F1 to the destination type (i.e.,
13579 : the type of the entity being initialized) is a better conversion
13580 : sequence than the standard conversion sequence from the return type
13581 : of F2 to the destination type. */
13582 :
13583 9599764 : if (cand1->second_conv)
13584 : {
13585 50700 : winner = compare_ics (cand1->second_conv, cand2->second_conv);
13586 50700 : if (winner)
13587 : return winner;
13588 : }
13589 :
13590 : /* CWG2735 (PR109247): A copy/move ctor/op= for which its operand uses an
13591 : explicit conversion (due to list-initialization) is worse. */
13592 9599622 : {
13593 9599622 : z_candidate *sp = nullptr;
13594 9599622 : if (sfk_copy_or_move (cand1->fn))
13595 536 : sp = cand1;
13596 9599622 : if (sfk_copy_or_move (cand2->fn))
13597 819175 : sp = sp ? nullptr : cand2;
13598 9599573 : if (sp)
13599 : {
13600 1639226 : conversion *conv = sp->convs[!DECL_CONSTRUCTOR_P (sp->fn)];
13601 819613 : if (conv->user_conv_p)
13602 1330 : for (; conv; conv = next_conversion (conv))
13603 1196 : if (conv->kind == ck_user
13604 531 : && DECL_P (conv->cand->fn)
13605 1727 : && DECL_NONCONVERTING_P (conv->cand->fn))
13606 403 : return (sp == cand1) ? -1 : 1;
13607 : }
13608 : }
13609 :
13610 : /* DR2327: C++17 copy elision in [over.match.ctor] (direct-init) context.
13611 : The standard currently says that only constructors are candidates, but if
13612 : one copies a prvalue returned by a conversion function we prefer that.
13613 :
13614 : Clang does something similar, as discussed at
13615 : http://lists.isocpp.org/core/2017/10/3166.php
13616 : http://lists.isocpp.org/core/2019/03/5721.php */
13617 6393098 : if (len == 1 && cxx_dialect >= cxx17
13618 6378629 : && DECL_P (cand1->fn)
13619 6378629 : && DECL_COMPLETE_CONSTRUCTOR_P (cand1->fn)
13620 10348936 : && !(cand1->flags & LOOKUP_ONLYCONVERTING))
13621 : {
13622 397505 : bool elided1 = joust_maybe_elide_copy (cand1);
13623 397505 : bool elided2 = joust_maybe_elide_copy (cand2);
13624 397505 : winner = elided1 - elided2;
13625 397505 : if (winner)
13626 : return winner;
13627 : }
13628 :
13629 : /* or, if not that,
13630 : F1 is a non-template function and F2 is a template function
13631 : specialization. */
13632 :
13633 9599208 : if (!cand1->template_decl && cand2->template_decl)
13634 : return 1;
13635 9545425 : else if (cand1->template_decl && !cand2->template_decl)
13636 : return -1;
13637 :
13638 : /* or, if not that,
13639 : F1 and F2 are template functions and the function template for F1 is
13640 : more specialized than the template for F2 according to the partial
13641 : ordering rules. */
13642 :
13643 8497549 : if (cand1->template_decl && cand2->template_decl)
13644 : {
13645 3821041 : winner = more_specialized_fn
13646 3821041 : (TI_TEMPLATE (cand1->template_decl),
13647 3821041 : TI_TEMPLATE (cand2->template_decl),
13648 : /* [temp.func.order]: The presence of unused ellipsis and default
13649 : arguments has no effect on the partial ordering of function
13650 : templates. add_function_candidate() will not have
13651 : counted the "this" argument for constructors. */
13652 7642082 : cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
13653 3821041 : if (winner)
13654 : return winner;
13655 : }
13656 :
13657 : /* F1 and F2 are non-template functions and
13658 : - they have the same non-object-parameter-type-lists ([dcl.fct]), and
13659 : - if they are member functions, both are direct members of the same
13660 : class, and
13661 : - if both are non-static member functions, they have the same types for
13662 : their object parameters, and
13663 : - F1 is more constrained than F2 according to the partial ordering of
13664 : constraints described in [temp.constr.order]. */
13665 5683926 : if (flag_concepts && DECL_P (cand1->fn) && DECL_P (cand2->fn)
13666 5683878 : && !cand1->template_decl && !cand2->template_decl
13667 10360783 : && cand_parms_match (cand1, cand2, pmatch::current))
13668 : {
13669 384933 : winner = more_constrained (cand1->fn, cand2->fn);
13670 384933 : if (winner)
13671 : return winner;
13672 : }
13673 :
13674 : /* F2 is a rewritten candidate (12.4.1.2) and F1 is not, or F1 and F2 are
13675 : rewritten candidates, and F2 is a synthesized candidate with reversed
13676 : order of parameters and F1 is not. */
13677 5689824 : if (cand1->rewritten ())
13678 : {
13679 1182394 : if (!cand2->rewritten ())
13680 : return -1;
13681 745544 : if (!cand1->reversed () && cand2->reversed ())
13682 : return 1;
13683 745544 : if (cand1->reversed () && !cand2->reversed ())
13684 : return -1;
13685 : }
13686 4507430 : else if (cand2->rewritten ())
13687 : return 1;
13688 :
13689 4443541 : if (deduction_guide_p (cand1->fn))
13690 : {
13691 3144 : gcc_assert (deduction_guide_p (cand2->fn));
13692 :
13693 : /* F1 and F2 are generated from class template argument deduction for a
13694 : class D, and F2 is generated from inheriting constructors from a base
13695 : class of D while F1 is not, and for each explicit function argument,
13696 : the corresponding parameters of F1 and F2 are either both ellipses or
13697 : have the same type. */
13698 3144 : bool inherited1 = inherited_guide_p (cand1->fn);
13699 3144 : bool inherited2 = inherited_guide_p (cand2->fn);
13700 3144 : if (int diff = inherited2 - inherited1)
13701 : {
13702 52 : for (i = 0; i < len; ++i)
13703 : {
13704 15 : conversion *t1 = cand1->convs[i + off1];
13705 15 : conversion *t2 = cand2->convs[i + off2];
13706 : /* ??? It seems the ellipses part of this tiebreaker isn't
13707 : needed since a mismatch should have broken the tie earlier
13708 : during ICS comparison. */
13709 15 : gcc_checking_assert (t1->ellipsis_p == t2->ellipsis_p);
13710 15 : if (!same_type_p (t1->type, t2->type))
13711 : break;
13712 : }
13713 39 : if (i == len)
13714 : return diff;
13715 : }
13716 :
13717 : /* F1 is generated from a deduction-guide (13.3.1.8) and F2 is not */
13718 : /* We distinguish between candidates from an explicit deduction guide and
13719 : candidates built from a constructor based on DECL_ARTIFICIAL. */
13720 3107 : int art1 = DECL_ARTIFICIAL (cand1->fn);
13721 3107 : int art2 = DECL_ARTIFICIAL (cand2->fn);
13722 3107 : if (art1 != art2)
13723 1102 : return art2 - art1;
13724 :
13725 2005 : if (art1)
13726 : {
13727 : /* Prefer the special copy guide over a declared copy/move
13728 : constructor. */
13729 2002 : if (copy_guide_p (cand1->fn))
13730 : return 1;
13731 81 : if (copy_guide_p (cand2->fn))
13732 : return -1;
13733 :
13734 : /* Prefer a candidate generated from a non-template constructor. */
13735 26 : int tg1 = template_guide_p (cand1->fn);
13736 26 : int tg2 = template_guide_p (cand2->fn);
13737 26 : if (tg1 != tg2)
13738 6 : return tg2 - tg1;
13739 : }
13740 : }
13741 :
13742 : /* F1 is a constructor for a class D, F2 is a constructor for a base class B
13743 : of D, and for all arguments the corresponding parameters of F1 and F2 have
13744 : the same type (CWG 2273/2277). */
13745 13321105 : if (DECL_INHERITED_CTOR (cand1->fn) || DECL_INHERITED_CTOR (cand2->fn))
13746 : {
13747 87 : tree base1 = DECL_CONTEXT (strip_inheriting_ctors (cand1->fn));
13748 87 : tree base2 = DECL_CONTEXT (strip_inheriting_ctors (cand2->fn));
13749 :
13750 87 : bool used1 = false;
13751 87 : bool used2 = false;
13752 87 : if (base1 == base2)
13753 : /* No difference. */;
13754 87 : else if (DERIVED_FROM_P (base1, base2))
13755 : used1 = true;
13756 18 : else if (DERIVED_FROM_P (base2, base1))
13757 : used2 = true;
13758 :
13759 78 : if (int diff = used2 - used1)
13760 : {
13761 105 : for (i = 0; i < len; ++i)
13762 : {
13763 36 : conversion *t1 = cand1->convs[i + off1];
13764 36 : conversion *t2 = cand2->convs[i + off2];
13765 36 : if (!same_type_p (t1->type, t2->type))
13766 : break;
13767 : }
13768 78 : if (i == len)
13769 : return diff;
13770 : }
13771 : }
13772 :
13773 : /* Check whether we can discard a builtin candidate, either because we
13774 : have two identical ones or matching builtin and non-builtin candidates.
13775 :
13776 : (Pedantically in the latter case the builtin which matched the user
13777 : function should not be added to the overload set, but we spot it here.
13778 :
13779 : [over.match.oper]
13780 : ... the builtin candidates include ...
13781 : - do not have the same parameter type list as any non-template
13782 : non-member candidate. */
13783 :
13784 4440351 : if (identifier_p (cand1->fn) || identifier_p (cand2->fn))
13785 : {
13786 93 : for (i = 0; i < len; ++i)
13787 69 : if (!same_type_p (cand1->convs[i]->type,
13788 : cand2->convs[i]->type))
13789 : break;
13790 43 : if (i == cand1->num_convs)
13791 : {
13792 24 : if (cand1->fn == cand2->fn)
13793 : /* Two built-in candidates; arbitrarily pick one. */
13794 : return 1;
13795 16412696 : else if (identifier_p (cand1->fn))
13796 : /* cand1 is built-in; prefer cand2. */
13797 : return -1;
13798 : else
13799 : /* cand2 is built-in; prefer cand1. */
13800 : return 1;
13801 : }
13802 : }
13803 :
13804 : /* For candidates of a multi-versioned function, make the version with
13805 : the highest priority win. This version will be checked for dispatching
13806 : first. If this version can be inlined into the caller, the front-end
13807 : will simply make a direct call to this function. */
13808 :
13809 4440327 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13810 4440305 : && DECL_FUNCTION_VERSIONED (cand1->fn)
13811 917 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13812 4441244 : && DECL_FUNCTION_VERSIONED (cand2->fn))
13813 : {
13814 917 : tree f1 = TREE_TYPE (cand1->fn);
13815 917 : tree f2 = TREE_TYPE (cand2->fn);
13816 917 : tree p1 = TYPE_ARG_TYPES (f1);
13817 917 : tree p2 = TYPE_ARG_TYPES (f2);
13818 :
13819 : /* Check if cand1->fn and cand2->fn are versions of the same function. It
13820 : is possible that cand1->fn and cand2->fn are function versions but of
13821 : different functions. Check types to see if they are versions of the same
13822 : function. */
13823 917 : if (compparms (p1, p2)
13824 917 : && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
13825 : {
13826 : /* Always make the version with the higher priority, more
13827 : specialized, win. */
13828 917 : gcc_assert (targetm.compare_version_priority);
13829 917 : if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
13830 : return 1;
13831 : else
13832 : return -1;
13833 : }
13834 : }
13835 :
13836 : /* If the two function declarations represent the same function (this can
13837 : happen with declarations in multiple scopes and arg-dependent lookup),
13838 : arbitrarily choose one. But first make sure the default args we're
13839 : using match. */
13840 4439388 : if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
13841 8878798 : && equal_functions (cand1->fn, cand2->fn))
13842 : {
13843 33 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
13844 33 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
13845 :
13846 66 : gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
13847 :
13848 47 : for (i = 0; i < len; ++i)
13849 : {
13850 : /* Don't crash if the fn is variadic. */
13851 14 : if (!parms1)
13852 : break;
13853 14 : parms1 = TREE_CHAIN (parms1);
13854 14 : parms2 = TREE_CHAIN (parms2);
13855 : }
13856 :
13857 33 : if (off1)
13858 0 : parms1 = TREE_CHAIN (parms1);
13859 33 : else if (off2)
13860 0 : parms2 = TREE_CHAIN (parms2);
13861 :
13862 64 : for (; parms1; ++i)
13863 : {
13864 74 : if (!cp_tree_equal (TREE_PURPOSE (parms1),
13865 37 : TREE_PURPOSE (parms2)))
13866 : {
13867 6 : if (warn)
13868 : {
13869 3 : if (complain & tf_error)
13870 : {
13871 3 : auto_diagnostic_group d;
13872 3 : if (permerror (input_location,
13873 : "default argument mismatch in "
13874 : "overload resolution"))
13875 : {
13876 3 : inform (DECL_SOURCE_LOCATION (cand1->fn),
13877 : " candidate 1: %q#F", cand1->fn);
13878 3 : inform (DECL_SOURCE_LOCATION (cand2->fn),
13879 : " candidate 2: %q#F", cand2->fn);
13880 : }
13881 3 : }
13882 : else
13883 : return 0;
13884 : }
13885 : else
13886 3 : add_warning (cand1, cand2);
13887 : break;
13888 : }
13889 31 : parms1 = TREE_CHAIN (parms1);
13890 31 : parms2 = TREE_CHAIN (parms2);
13891 : }
13892 :
13893 33 : return 1;
13894 : }
13895 :
13896 4440350 : tweak:
13897 :
13898 : /* Extension: If the worst conversion for one candidate is better than the
13899 : worst conversion for the other, take the first. */
13900 4440350 : if (!pedantic && (complain & tf_warning_or_error))
13901 : {
13902 : conversion_rank rank1 = cr_identity, rank2 = cr_identity;
13903 9437136 : struct z_candidate *w = 0, *l = 0;
13904 :
13905 9437136 : for (i = 0; i < len; ++i)
13906 : {
13907 5191775 : if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
13908 4170944 : rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
13909 5191775 : if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
13910 4170961 : rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
13911 : }
13912 4245361 : if (rank1 < rank2)
13913 56 : winner = 1, w = cand1, l = cand2;
13914 4245361 : if (rank1 > rank2)
13915 : winner = -1, w = cand2, l = cand1;
13916 4245253 : if (winner)
13917 : {
13918 : /* Don't choose a deleted function over ambiguity. */
13919 164 : if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
13920 : return 0;
13921 164 : if (warn)
13922 : {
13923 6 : auto_diagnostic_group d;
13924 6 : if (pedwarn (input_location, 0,
13925 : "ISO C++ says that these are ambiguous, even "
13926 : "though the worst conversion for the first is "
13927 : "better than the worst conversion for the second:"))
13928 : {
13929 3 : print_z_candidate (input_location, N_("candidate 1:"), w);
13930 3 : print_z_candidate (input_location, N_("candidate 2:"), l);
13931 : }
13932 6 : }
13933 : else
13934 158 : add_warning (w, l);
13935 164 : return winner;
13936 : }
13937 : }
13938 :
13939 : gcc_assert (!winner);
13940 : return 0;
13941 : }
13942 :
13943 : /* Given a list of candidates for overloading, find the best one, if any.
13944 : This algorithm has a worst case of O(2n) (winner is last), and a best
13945 : case of O(n/2) (totally ambiguous); much better than a sorting
13946 : algorithm. The candidates list is assumed to be sorted according
13947 : to viability (via splice_viable). */
13948 :
13949 : static struct z_candidate *
13950 221573082 : tourney (struct z_candidate *candidates, tsubst_flags_t complain)
13951 : {
13952 221573082 : struct z_candidate **champ = &candidates, **challenger;
13953 221573082 : int fate;
13954 221573082 : struct z_candidate *previous_worse_champ = nullptr;
13955 :
13956 : /* Walk through the list once, comparing each current champ to the next
13957 : candidate, knocking out a candidate or two with each comparison. */
13958 :
13959 284131321 : for (challenger = &candidates->next; *challenger && (*challenger)->viable; )
13960 : {
13961 62568382 : fate = joust (*champ, *challenger, 0, complain);
13962 62568382 : if (fate == 1)
13963 40340166 : challenger = &(*challenger)->next;
13964 22228216 : else if (fate == -1)
13965 : {
13966 17789730 : previous_worse_champ = *champ;
13967 17789730 : champ = challenger;
13968 17789730 : challenger = &(*challenger)->next;
13969 : }
13970 : else
13971 : {
13972 4438486 : previous_worse_champ = nullptr;
13973 4438486 : champ = &(*challenger)->next;
13974 4438486 : if (!*champ || !(*champ)->viable
13975 4428437 : || (*champ)->viable < (*challenger)->viable)
13976 : {
13977 : champ = nullptr;
13978 : break;
13979 : }
13980 4428343 : challenger = &(*champ)->next;
13981 : }
13982 : }
13983 :
13984 : /* Make sure the champ is better than all the candidates it hasn't yet
13985 : been compared to. */
13986 :
13987 221573082 : if (champ)
13988 26901153 : for (challenger = &candidates;
13989 248464092 : challenger != champ;
13990 26901153 : challenger = &(*challenger)->next)
13991 : {
13992 26903053 : if (*challenger == previous_worse_champ)
13993 : /* We already know this candidate is worse than the champ. */
13994 16657885 : continue;
13995 10245168 : fate = joust (*champ, *challenger, 0, complain);
13996 10245168 : if (fate != 1)
13997 : {
13998 : champ = nullptr;
13999 : break;
14000 : }
14001 : }
14002 :
14003 221562939 : if (!champ)
14004 12043 : return nullptr;
14005 :
14006 : /* Move the champ to the front of the candidate list. */
14007 :
14008 221561039 : if (champ != &candidates)
14009 : {
14010 18240134 : z_candidate *saved_champ = *champ;
14011 18240134 : *champ = saved_champ->next;
14012 18240134 : saved_champ->next = candidates;
14013 18240134 : candidates = saved_champ;
14014 : }
14015 :
14016 221561039 : return candidates;
14017 : }
14018 :
14019 : /* Returns nonzero if things of type FROM can be converted to TO. */
14020 :
14021 : bool
14022 4196202 : can_convert (tree to, tree from, tsubst_flags_t complain)
14023 : {
14024 4196202 : tree arg = NULL_TREE;
14025 : /* implicit_conversion only considers user-defined conversions
14026 : if it has an expression for the call argument list. */
14027 4196202 : if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to))
14028 109 : arg = build_stub_object (from);
14029 4196202 : return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain);
14030 : }
14031 :
14032 : /* Returns nonzero if things of type FROM can be converted to TO with a
14033 : standard conversion. */
14034 :
14035 : bool
14036 255 : can_convert_standard (tree to, tree from, tsubst_flags_t complain)
14037 : {
14038 255 : return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
14039 : }
14040 :
14041 : /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
14042 :
14043 : bool
14044 5054421 : can_convert_arg (tree to, tree from, tree arg, int flags,
14045 : tsubst_flags_t complain)
14046 : {
14047 5054421 : conversion *t;
14048 5054421 : bool ok_p;
14049 :
14050 5054421 : conversion_obstack_sentinel cos;
14051 : /* We want to discard any access checks done for this test,
14052 : as we might not be in the appropriate access context and
14053 : we'll do the check again when we actually perform the
14054 : conversion. */
14055 5054421 : push_deferring_access_checks (dk_deferred);
14056 :
14057 : /* Handle callers like check_local_shadow forgetting to
14058 : convert_from_reference. */
14059 5054421 : if (TYPE_REF_P (from) && arg)
14060 : {
14061 52 : arg = convert_from_reference (arg);
14062 52 : from = TREE_TYPE (arg);
14063 : }
14064 :
14065 5054421 : t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
14066 : flags, complain);
14067 5054421 : ok_p = (t && !t->bad_p);
14068 :
14069 : /* Discard the access checks now. */
14070 5054421 : pop_deferring_access_checks ();
14071 :
14072 10108842 : return ok_p;
14073 5054421 : }
14074 :
14075 : /* Like can_convert_arg, but allows dubious conversions as well. */
14076 :
14077 : bool
14078 172914876 : can_convert_arg_bad (tree to, tree from, tree arg, int flags,
14079 : tsubst_flags_t complain)
14080 : {
14081 172914876 : conversion *t;
14082 :
14083 172914876 : conversion_obstack_sentinel cos;
14084 : /* Try to perform the conversion. */
14085 172914876 : t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
14086 : flags, complain);
14087 :
14088 345829752 : return t != NULL;
14089 172914876 : }
14090 :
14091 : /* Return an IMPLICIT_CONV_EXPR from EXPR to TYPE with bits set from overload
14092 : resolution FLAGS. */
14093 :
14094 : tree
14095 13848160 : build_implicit_conv_flags (tree type, tree expr, int flags)
14096 : {
14097 : /* In a template, we are only concerned about determining the
14098 : type of non-dependent expressions, so we do not have to
14099 : perform the actual conversion. But for initializers, we
14100 : need to be able to perform it at instantiation
14101 : (or instantiate_non_dependent_expr) time. */
14102 13848160 : expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
14103 13848160 : if (!(flags & LOOKUP_ONLYCONVERTING))
14104 6305371 : IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
14105 13848160 : if (flags & LOOKUP_NO_NARROWING)
14106 22153 : IMPLICIT_CONV_EXPR_BRACED_INIT (expr) = true;
14107 13848160 : return expr;
14108 : }
14109 :
14110 : /* Convert EXPR to TYPE. Return the converted expression.
14111 :
14112 : Note that we allow bad conversions here because by the time we get to
14113 : this point we are committed to doing the conversion. If we end up
14114 : doing a bad conversion, convert_like will complain. */
14115 :
14116 : tree
14117 280287709 : perform_implicit_conversion_flags (tree type, tree expr,
14118 : tsubst_flags_t complain, int flags)
14119 : {
14120 280287709 : conversion *conv;
14121 280287709 : location_t loc = cp_expr_loc_or_input_loc (expr);
14122 :
14123 280287709 : if (error_operand_p (expr))
14124 444 : return error_mark_node;
14125 :
14126 280287265 : conversion_obstack_sentinel cos;
14127 :
14128 280287265 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
14129 : /*c_cast_p=*/false,
14130 : flags, complain);
14131 :
14132 280287265 : if (!conv)
14133 : {
14134 237020 : if (complain & tf_error)
14135 479 : implicit_conversion_error (loc, type, expr, flags);
14136 237020 : expr = error_mark_node;
14137 : }
14138 280050245 : else if (processing_template_decl && conv->kind != ck_identity)
14139 13829934 : expr = build_implicit_conv_flags (type, expr, flags);
14140 : else
14141 : {
14142 : /* Give a conversion call the same location as expr. */
14143 266220311 : iloc_sentinel il (loc);
14144 266220311 : expr = convert_like (conv, expr, complain);
14145 266220311 : }
14146 :
14147 280287265 : return expr;
14148 280287265 : }
14149 :
14150 : tree
14151 12369489 : perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
14152 : {
14153 12369489 : return perform_implicit_conversion_flags (type, expr, complain,
14154 12369489 : LOOKUP_IMPLICIT);
14155 : }
14156 :
14157 : /* Convert EXPR to TYPE (as a direct-initialization) if that is
14158 : permitted. If the conversion is valid, the converted expression is
14159 : returned. Otherwise, NULL_TREE is returned, except in the case
14160 : that TYPE is a class type; in that case, an error is issued. If
14161 : C_CAST_P is true, then this direct-initialization is taking
14162 : place as part of a static_cast being attempted as part of a C-style
14163 : cast. */
14164 :
14165 : tree
14166 46503426 : perform_direct_initialization_if_possible (tree type,
14167 : tree expr,
14168 : bool c_cast_p,
14169 : tsubst_flags_t complain)
14170 : {
14171 46503426 : conversion *conv;
14172 :
14173 46503426 : if (type == error_mark_node || error_operand_p (expr))
14174 : return error_mark_node;
14175 : /* [dcl.init]
14176 :
14177 : If the destination type is a (possibly cv-qualified) class type:
14178 :
14179 : -- If the initialization is direct-initialization ...,
14180 : constructors are considered.
14181 :
14182 : -- If overload resolution is successful, the selected constructor
14183 : is called to initialize the object, with the initializer expression
14184 : or expression-list as its argument(s).
14185 :
14186 : -- Otherwise, if no constructor is viable, the destination type is
14187 : a (possibly cv-qualified) aggregate class A, and the initializer is
14188 : a parenthesized expression-list, the object is initialized as
14189 : follows... */
14190 46503426 : if (CLASS_TYPE_P (type))
14191 : {
14192 3006283 : releasing_vec args (make_tree_vector_single (expr));
14193 3006283 : expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
14194 : &args, type, LOOKUP_NORMAL, complain);
14195 3006283 : return build_cplus_new (type, expr, complain);
14196 3006283 : }
14197 :
14198 43497143 : conversion_obstack_sentinel cos;
14199 :
14200 43497143 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
14201 : c_cast_p,
14202 : LOOKUP_NORMAL, complain);
14203 43497143 : if (!conv || conv->bad_p)
14204 : expr = NULL_TREE;
14205 38929483 : else if (processing_template_decl && conv->kind != ck_identity)
14206 : {
14207 : /* In a template, we are only concerned about determining the
14208 : type of non-dependent expressions, so we do not have to
14209 : perform the actual conversion. But for initializers, we
14210 : need to be able to perform it at instantiation
14211 : (or instantiate_non_dependent_expr) time. */
14212 664666 : expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
14213 664666 : IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
14214 : }
14215 : else
14216 38264817 : expr = convert_like (conv, expr, NULL_TREE, 0,
14217 : /*issue_conversion_warnings=*/false,
14218 : c_cast_p, /*nested_p=*/false, complain);
14219 :
14220 43497143 : return expr;
14221 43497143 : }
14222 :
14223 : /* When initializing a reference that lasts longer than a full-expression,
14224 : this special rule applies:
14225 :
14226 : [class.temporary]
14227 :
14228 : The temporary to which the reference is bound or the temporary
14229 : that is the complete object to which the reference is bound
14230 : persists for the lifetime of the reference.
14231 :
14232 : The temporaries created during the evaluation of the expression
14233 : initializing the reference, except the temporary to which the
14234 : reference is bound, are destroyed at the end of the
14235 : full-expression in which they are created.
14236 :
14237 : In that case, we store the converted expression into a new
14238 : VAR_DECL in a new scope.
14239 :
14240 : However, we want to be careful not to create temporaries when
14241 : they are not required. For example, given:
14242 :
14243 : struct B {};
14244 : struct D : public B {};
14245 : D f();
14246 : const B& b = f();
14247 :
14248 : there is no need to copy the return value from "f"; we can just
14249 : extend its lifetime. Similarly, given:
14250 :
14251 : struct S {};
14252 : struct T { operator S(); };
14253 : T t;
14254 : const S& s = t;
14255 :
14256 : we can extend the lifetime of the return value of the conversion
14257 : operator.
14258 :
14259 : The next several functions are involved in this lifetime extension. */
14260 :
14261 : /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
14262 : reference is being bound to a temporary. Create and return a new
14263 : VAR_DECL with the indicated TYPE; this variable will store the value to
14264 : which the reference is bound. */
14265 :
14266 : tree
14267 7642 : make_temporary_var_for_ref_to_temp (tree decl, tree type)
14268 : {
14269 7642 : tree var = create_temporary_var (type);
14270 :
14271 : /* Register the variable. */
14272 7642 : if (VAR_P (decl)
14273 7642 : && (TREE_STATIC (decl) || CP_DECL_THREAD_LOCAL_P (decl)))
14274 : {
14275 : /* Namespace-scope or local static; give it a mangled name. */
14276 :
14277 : /* If an initializer is visible to multiple translation units, those
14278 : translation units must agree on the addresses of the
14279 : temporaries. Therefore the temporaries must be given a consistent name
14280 : and vague linkage. The mangled name of a temporary is the name of the
14281 : non-temporary object in whose initializer they appear, prefixed with
14282 : GR and suffixed with a sequence number mangled using the usual rules
14283 : for a seq-id. Temporaries are numbered with a pre-order, depth-first,
14284 : left-to-right walk of the complete initializer. */
14285 758 : copy_linkage (var, decl);
14286 :
14287 758 : tree name = mangle_ref_init_variable (decl);
14288 758 : DECL_NAME (var) = name;
14289 758 : SET_DECL_ASSEMBLER_NAME (var, name);
14290 :
14291 : /* Set the context to make the variable mergeable in modules. */
14292 758 : DECL_CONTEXT (var) = current_scope ();
14293 : }
14294 : else
14295 : /* Create a new cleanup level if necessary. */
14296 6884 : maybe_push_cleanup_level (type);
14297 :
14298 7642 : return pushdecl (var);
14299 : }
14300 :
14301 : static tree extend_temps_r (tree *, int *, void *);
14302 :
14303 : /* EXPR is the initializer for a variable DECL of reference or
14304 : std::initializer_list type. Create, push and return a new VAR_DECL
14305 : for the initializer so that it will live as long as DECL. Any
14306 : cleanup for the new variable is returned through CLEANUP, and the
14307 : code to initialize the new variable is returned through INITP. */
14308 :
14309 : static tree
14310 7642 : set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
14311 : tree *initp, tree *cond_guard,
14312 : void *walk_data)
14313 : {
14314 7642 : tree init;
14315 7642 : tree type;
14316 7642 : tree var;
14317 :
14318 : /* Create the temporary variable. */
14319 7642 : type = TREE_TYPE (expr);
14320 7642 : var = make_temporary_var_for_ref_to_temp (decl, type);
14321 7642 : layout_decl (var, 0);
14322 : /* If the rvalue is the result of a function call it will be
14323 : a TARGET_EXPR. If it is some other construct (such as a
14324 : member access expression where the underlying object is
14325 : itself the result of a function call), turn it into a
14326 : TARGET_EXPR here. It is important that EXPR be a
14327 : TARGET_EXPR below since otherwise the INIT_EXPR will
14328 : attempt to make a bitwise copy of EXPR to initialize
14329 : VAR. */
14330 7642 : if (TREE_CODE (expr) != TARGET_EXPR)
14331 0 : expr = get_target_expr (expr);
14332 : else
14333 : {
14334 7642 : if (TREE_ADDRESSABLE (expr))
14335 7559 : TREE_ADDRESSABLE (var) = 1;
14336 7642 : if (DECL_MERGEABLE (TARGET_EXPR_SLOT (expr)))
14337 549 : DECL_MERGEABLE (var) = true;
14338 : }
14339 :
14340 7642 : if (TREE_CODE (decl) == FIELD_DECL
14341 7642 : && extra_warnings && !warning_suppressed_p (decl))
14342 : {
14343 3 : warning (OPT_Wextra, "a temporary bound to %qD only persists "
14344 : "until the constructor exits", decl);
14345 3 : suppress_warning (decl);
14346 : }
14347 :
14348 : /* Recursively extend temps in this initializer. The recursion needs to come
14349 : after creating the variable to conform to the mangling ABI, and before
14350 : maybe_constant_init because the extension might change its result. */
14351 7642 : if (walk_data)
14352 1481 : cp_walk_tree (&TARGET_EXPR_INITIAL (expr), extend_temps_r,
14353 : walk_data, nullptr);
14354 : else
14355 6161 : TARGET_EXPR_INITIAL (expr)
14356 12322 : = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups,
14357 : cond_guard);
14358 :
14359 : /* Any reference temp has a non-trivial initializer. */
14360 7642 : DECL_NONTRIVIALLY_INITIALIZED_P (var) = true;
14361 :
14362 : /* If the initializer is constant, put it in DECL_INITIAL so we get
14363 : static initialization and use in constant expressions. */
14364 7642 : init = maybe_constant_init (expr, var, /*manifestly_const_eval=*/true);
14365 : /* As in store_init_value. */
14366 7642 : init = cp_fully_fold (init);
14367 7642 : if (TREE_CONSTANT (init))
14368 : {
14369 1120 : if (literal_type_p (type)
14370 1094 : && CP_TYPE_CONST_NON_VOLATILE_P (type)
14371 1784 : && !TYPE_HAS_MUTABLE_P (type))
14372 : {
14373 : /* 5.19 says that a constant expression can include an
14374 : lvalue-rvalue conversion applied to "a glvalue of literal type
14375 : that refers to a non-volatile temporary object initialized
14376 : with a constant expression". Rather than try to communicate
14377 : that this VAR_DECL is a temporary, just mark it constexpr. */
14378 661 : DECL_DECLARED_CONSTEXPR_P (var) = true;
14379 661 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
14380 661 : TREE_CONSTANT (var) = true;
14381 661 : TREE_READONLY (var) = true;
14382 : }
14383 1120 : DECL_INITIAL (var) = init;
14384 1120 : init = NULL_TREE;
14385 : }
14386 : else
14387 : /* Create the INIT_EXPR that will initialize the temporary
14388 : variable. */
14389 6522 : init = split_nonconstant_init (var, expr);
14390 7642 : if (at_function_scope_p ())
14391 : {
14392 6974 : add_decl_expr (var);
14393 :
14394 6974 : if (TREE_STATIC (var))
14395 90 : init = add_stmt_to_compound (init, register_dtor_fn (var));
14396 : else
14397 : {
14398 : /* ??? Instead of rebuilding the cleanup, we could replace the slot
14399 : with var in TARGET_EXPR_CLEANUP (expr). */
14400 6884 : tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
14401 6884 : if (cleanup)
14402 : {
14403 2819 : if (cond_guard && cleanup != error_mark_node)
14404 : {
14405 23 : if (*cond_guard == NULL_TREE)
14406 : {
14407 23 : *cond_guard = build_local_temp (boolean_type_node);
14408 23 : add_decl_expr (*cond_guard);
14409 23 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION,
14410 : *cond_guard, NOP_EXPR,
14411 : boolean_false_node,
14412 : tf_warning_or_error);
14413 23 : finish_expr_stmt (set);
14414 : }
14415 23 : cleanup = build3 (COND_EXPR, void_type_node,
14416 : *cond_guard, cleanup, NULL_TREE);
14417 : }
14418 2819 : if (flag_exceptions && TREE_CODE (TREE_TYPE (var)) != ARRAY_TYPE)
14419 : {
14420 : /* The normal cleanup for this extended variable isn't pushed
14421 : until cp_finish_decl, so we need to retain a TARGET_EXPR
14422 : to clean it up in case a later initializer throws
14423 : (g++.dg/eh/ref-temp3.C).
14424 :
14425 : We don't do this for array temporaries because they have
14426 : the array cleanup region from build_vec_init.
14427 :
14428 : Unlike maybe_push_temp_cleanup, we don't actually need a
14429 : flag, but a TARGET_EXPR needs a TARGET_EXPR_SLOT.
14430 : Perhaps this could use WITH_CLEANUP_EXPR instead, but
14431 : gimplify.cc doesn't handle that, and front-end handling
14432 : was removed in r8-1725 and r8-1818.
14433 :
14434 : Alternately it might be preferable to flatten an
14435 : initialization with extended temps into a sequence of
14436 : (non-full-expression) statements, so we could immediately
14437 : push_cleanup here for only a single cleanup region, but we
14438 : don't have a mechanism for that in the front-end, only the
14439 : gimplifier. */
14440 2711 : tree targ = get_internal_target_expr (boolean_true_node);
14441 2711 : TARGET_EXPR_CLEANUP (targ) = cleanup;
14442 2711 : CLEANUP_EH_ONLY (targ) = true;
14443 : /* Don't actually initialize the bool. */
14444 2711 : init = (!init ? void_node
14445 2685 : : convert_to_void (init, ICV_STATEMENT, tf_none));
14446 2711 : TARGET_EXPR_INITIAL (targ) = init;
14447 2711 : init = targ;
14448 : }
14449 2819 : vec_safe_push (*cleanups, cleanup);
14450 : }
14451 : }
14452 :
14453 : /* We must be careful to destroy the temporary only
14454 : after its initialization has taken place. If the
14455 : initialization throws an exception, then the
14456 : destructor should not be run. We cannot simply
14457 : transform INIT into something like:
14458 :
14459 : (INIT, ({ CLEANUP_STMT; }))
14460 :
14461 : because emit_local_var always treats the
14462 : initializer as a full-expression. Thus, the
14463 : destructor would run too early; it would run at the
14464 : end of initializing the reference variable, rather
14465 : than at the end of the block enclosing the
14466 : reference variable.
14467 :
14468 : The solution is to pass back a cleanup expression
14469 : which the caller is responsible for attaching to
14470 : the statement tree. */
14471 : }
14472 : else
14473 : {
14474 : /* Don't output reflection variables. */
14475 668 : if (consteval_only_p (var))
14476 1 : DECL_EXTERNAL (var) = true;
14477 :
14478 668 : rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
14479 668 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
14480 : {
14481 40 : if (CP_DECL_THREAD_LOCAL_P (var))
14482 12 : tls_aggregates = tree_cons (NULL_TREE, var,
14483 : tls_aggregates);
14484 : else
14485 28 : static_aggregates = tree_cons (NULL_TREE, var,
14486 : static_aggregates);
14487 : }
14488 : else
14489 : /* Check whether the dtor is callable. */
14490 628 : cxx_maybe_build_cleanup (var, tf_warning_or_error);
14491 : }
14492 : /* Avoid -Wunused-variable warning (c++/38958). */
14493 7642 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
14494 7642 : && VAR_P (decl))
14495 2790 : TREE_USED (decl) = DECL_READ_P (decl) = true;
14496 :
14497 7642 : *initp = init;
14498 7642 : return var;
14499 : }
14500 :
14501 : /* Convert EXPR to the indicated reference TYPE, in a way suitable for
14502 : initializing a variable of that TYPE. */
14503 :
14504 : tree
14505 6080326 : initialize_reference (tree type, tree expr,
14506 : int flags, tsubst_flags_t complain)
14507 : {
14508 6080326 : conversion *conv;
14509 6080326 : location_t loc = cp_expr_loc_or_input_loc (expr);
14510 :
14511 6080326 : if (type == error_mark_node || error_operand_p (expr))
14512 : return error_mark_node;
14513 :
14514 6080255 : conversion_obstack_sentinel cos;
14515 :
14516 6080255 : conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
14517 : flags, complain);
14518 : /* If this conversion failed, we're in C++20, and we have something like
14519 : A& a(b) where A is an aggregate, try again, this time as A& a{b}. */
14520 6080255 : if ((!conv || conv->bad_p)
14521 677 : && (flags & LOOKUP_AGGREGATE_PAREN_INIT))
14522 : {
14523 4 : tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
14524 4 : CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
14525 4 : CONSTRUCTOR_IS_PAREN_INIT (e) = true;
14526 4 : conversion *c = reference_binding (type, TREE_TYPE (e), e,
14527 : /*c_cast_p=*/false, flags, complain);
14528 : /* If this worked, use it. */
14529 4 : if (c && !c->bad_p)
14530 : expr = e, conv = c;
14531 : }
14532 6080850 : if (!conv || conv->bad_p)
14533 : {
14534 674 : if (complain & tf_error)
14535 : {
14536 352 : if (conv)
14537 303 : convert_like (conv, expr, complain);
14538 49 : else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
14539 17 : && !TYPE_REF_IS_RVALUE (type)
14540 66 : && !lvalue_p (expr))
14541 6 : error_at (loc, "invalid initialization of non-const reference of "
14542 : "type %qH from an rvalue of type %qI",
14543 6 : type, TREE_TYPE (expr));
14544 : else
14545 43 : error_at (loc, "invalid initialization of reference of type "
14546 : "%qH from expression of type %qI", type,
14547 43 : TREE_TYPE (expr));
14548 : }
14549 674 : return error_mark_node;
14550 : }
14551 :
14552 6079581 : if (conv->kind == ck_ref_bind)
14553 : /* Perform the conversion. */
14554 6079578 : expr = convert_like (conv, expr, complain);
14555 3 : else if (conv->kind == ck_ambig)
14556 : /* We gave an error in build_user_type_conversion_1. */
14557 3 : expr = error_mark_node;
14558 : else
14559 0 : gcc_unreachable ();
14560 :
14561 : return expr;
14562 6080255 : }
14563 :
14564 : /* Return true if T is std::pair<const T&, const T&>. */
14565 :
14566 : static bool
14567 614481 : std_pair_ref_ref_p (tree t)
14568 : {
14569 : /* First, check if we have std::pair. */
14570 57287 : if (!NON_UNION_CLASS_TYPE_P (t)
14571 671308 : || !CLASSTYPE_TEMPLATE_INSTANTIATION (t))
14572 : return false;
14573 20615 : tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (t));
14574 20615 : if (!decl_in_std_namespace_p (tdecl))
14575 : return false;
14576 15426 : tree name = DECL_NAME (tdecl);
14577 15426 : if (!name || !id_equal (name, "pair"))
14578 : return false;
14579 :
14580 : /* Now see if the template arguments are both const T&. */
14581 363 : tree args = CLASSTYPE_TI_ARGS (t);
14582 363 : if (TREE_VEC_LENGTH (args) != 2)
14583 : return false;
14584 423 : for (int i = 0; i < 2; i++)
14585 453 : if (!TYPE_REF_OBJ_P (TREE_VEC_ELT (args, i))
14586 453 : || !CP_TYPE_CONST_P (TREE_TYPE (TREE_VEC_ELT (args, i))))
14587 333 : return false;
14588 :
14589 : return true;
14590 : }
14591 :
14592 : /* Return true if a class T has a reference member. */
14593 :
14594 : static bool
14595 216 : class_has_reference_member_p (tree t)
14596 : {
14597 216 : for (tree fields = TYPE_FIELDS (t);
14598 3380 : fields;
14599 3164 : fields = DECL_CHAIN (fields))
14600 3227 : if (TREE_CODE (fields) == FIELD_DECL
14601 196 : && !DECL_ARTIFICIAL (fields)
14602 3394 : && TYPE_REF_P (TREE_TYPE (fields)))
14603 : return true;
14604 : return false;
14605 : }
14606 :
14607 : /* A wrapper for the above suitable as a callback for dfs_walk_once. */
14608 :
14609 : static tree
14610 216 : class_has_reference_member_p_r (tree binfo, void *)
14611 : {
14612 216 : return (class_has_reference_member_p (BINFO_TYPE (binfo))
14613 216 : ? integer_one_node : NULL_TREE);
14614 : }
14615 :
14616 :
14617 : /* Return true if T (either a class or a function) has been marked as
14618 : not-dangling. */
14619 :
14620 : static bool
14621 1554 : no_dangling_p (tree t)
14622 : {
14623 1554 : t = lookup_attribute ("no_dangling", TYPE_ATTRIBUTES (t));
14624 1554 : if (!t)
14625 : return false;
14626 :
14627 75 : t = TREE_VALUE (t);
14628 75 : if (!t)
14629 : return true;
14630 :
14631 51 : t = build_converted_constant_bool_expr (TREE_VALUE (t), tf_warning_or_error);
14632 51 : t = cxx_constant_value (t);
14633 51 : return t == boolean_true_node;
14634 : }
14635 :
14636 : /* Return true if a class CTYPE is either std::reference_wrapper or
14637 : std::ref_view, or a reference wrapper class. We consider a class
14638 : a reference wrapper class if it has a reference member. We no
14639 : longer check that it has a constructor taking the same reference type
14640 : since that approach still generated too many false positives. */
14641 :
14642 : static bool
14643 446 : reference_like_class_p (tree ctype)
14644 : {
14645 446 : if (!CLASS_TYPE_P (ctype))
14646 : return false;
14647 :
14648 314 : if (no_dangling_p (ctype))
14649 : return true;
14650 :
14651 : /* Also accept a std::pair<const T&, const T&>. */
14652 290 : if (std_pair_ref_ref_p (ctype))
14653 : return true;
14654 :
14655 275 : tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (ctype));
14656 275 : if (decl_in_std_namespace_p (tdecl))
14657 : {
14658 38 : tree name = DECL_NAME (tdecl);
14659 38 : if (name
14660 38 : && (id_equal (name, "reference_wrapper")
14661 26 : || id_equal (name, "span")
14662 11 : || id_equal (name, "ref_view")))
14663 : return true;
14664 : }
14665 :
14666 : /* Avoid warning if CTYPE looks like std::span: it has a T* member and
14667 : a trivial destructor. For example,
14668 :
14669 : template<typename T>
14670 : struct Span {
14671 : T* data_;
14672 : std::size len_;
14673 : };
14674 :
14675 : is considered std::span-like. */
14676 248 : if (NON_UNION_CLASS_TYPE_P (ctype) && TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
14677 228 : for (tree field = next_aggregate_field (TYPE_FIELDS (ctype));
14678 367 : field; field = next_aggregate_field (DECL_CHAIN (field)))
14679 199 : if (TYPE_PTR_P (TREE_TYPE (field)))
14680 : return true;
14681 :
14682 : /* Some classes, such as std::tuple, have the reference member in its
14683 : (non-direct) base class. */
14684 188 : if (dfs_walk_once (TYPE_BINFO (ctype), class_has_reference_member_p_r,
14685 : nullptr, nullptr))
14686 : return true;
14687 :
14688 : return false;
14689 : }
14690 :
14691 : /* Helper for maybe_warn_dangling_reference to find a problematic temporary
14692 : in EXPR (as outlined in maybe_warn_dangling_reference), or NULL_TREE
14693 : if none found. For instance:
14694 :
14695 : const S& s = S().self(); // S()
14696 : const int& r = (42, f(1)); // temporary for passing 1 to f
14697 : const int& t = b ? f(1) : f(2); // temporary for 1
14698 : const int& u = b ? f(1) : f(g); // temporary for 1
14699 : const int& v = b ? f(g) : f(2); // temporary for 2
14700 : const int& w = b ? f(g) : f(g); // NULL_TREE
14701 : const int& y = (f(1), 42); // NULL_TREE
14702 : const int& z = f(f(1)); // temporary for 1
14703 :
14704 : EXPR is the initializer. If ARG_P is true, we're processing an argument
14705 : to a function; the point is to distinguish between, for example,
14706 :
14707 : Ref::inner (&TARGET_EXPR <D.2839, F::foo (fm)>)
14708 :
14709 : where we shouldn't warn, and
14710 :
14711 : Ref::inner (&TARGET_EXPR <D.2908, F::foo (&TARGET_EXPR <...>)>)
14712 :
14713 : where we should warn (Ref is a reference_like_class_p so we see through
14714 : it. */
14715 :
14716 : static tree
14717 3868 : do_warn_dangling_reference (tree expr, bool arg_p)
14718 : {
14719 3868 : STRIP_NOPS (expr);
14720 :
14721 3868 : if (arg_p && expr_represents_temporary_p (expr))
14722 : {
14723 : /* An attempt to reduce the number of -Wdangling-reference
14724 : false positives concerning reference wrappers (c++/107532).
14725 : When we encounter a reference_like_class_p, we don't warn
14726 : just yet; instead, we keep recursing to see if there were
14727 : any temporaries behind the reference-wrapper class. */
14728 : tree e = expr;
14729 354 : while (handled_component_p (e))
14730 15 : e = TREE_OPERAND (e, 0);
14731 339 : tree type = TREE_TYPE (e);
14732 : /* If the temporary represents a lambda, we don't really know
14733 : what's going on here. */
14734 461 : if (!reference_like_class_p (type) && !LAMBDA_TYPE_P (type))
14735 : return expr;
14736 : }
14737 :
14738 3636 : switch (TREE_CODE (expr))
14739 : {
14740 1817 : case CALL_EXPR:
14741 1817 : {
14742 1817 : tree fndecl = cp_get_callee_fndecl_nofold (expr);
14743 1817 : if (!fndecl
14744 1817 : || warning_suppressed_p (fndecl, OPT_Wdangling_reference)
14745 1806 : || !warning_enabled_at (DECL_SOURCE_LOCATION (fndecl),
14746 1806 : OPT_Wdangling_reference)
14747 : /* Don't emit a false positive for:
14748 : std::vector<int> v = ...;
14749 : std::vector<int>::const_iterator it = v.begin();
14750 : const int &r = *it++;
14751 : because R refers to one of the int elements of V, not to
14752 : a temporary object. Member operator* may return a reference
14753 : but probably not to one of its arguments. */
14754 1447 : || (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl)
14755 849 : && DECL_OVERLOADED_OPERATOR_P (fndecl)
14756 316 : && DECL_OVERLOADED_OPERATOR_IS (fndecl, INDIRECT_REF))
14757 3057 : || no_dangling_p (TREE_TYPE (fndecl)))
14758 601 : return NULL_TREE;
14759 :
14760 1216 : tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
14761 : /* If the function doesn't return a reference, don't warn. This
14762 : can be e.g.
14763 : const int& z = std::min({1, 2, 3, 4, 5, 6, 7});
14764 : which doesn't dangle: std::min here returns an int.
14765 :
14766 : If the function returns a std::pair<const T&, const T&>, we
14767 : warn, to detect e.g.
14768 : std::pair<const int&, const int&> v = std::minmax(1, 2);
14769 : which also creates a dangling reference, because std::minmax
14770 : returns std::pair<const T&, const T&>(b, a). */
14771 1216 : if (!(TYPE_REF_OBJ_P (rettype) || reference_like_class_p (rettype)))
14772 : return NULL_TREE;
14773 :
14774 : /* Here we're looking to see if any of the arguments is a temporary
14775 : initializing a reference parameter. */
14776 1644 : for (int i = 0; i < call_expr_nargs (expr); ++i)
14777 : {
14778 1238 : tree arg = CALL_EXPR_ARG (expr, i);
14779 : /* Check that this argument initializes a reference, except for
14780 : the argument initializing the object of a member function. */
14781 1238 : if (!DECL_IOBJ_MEMBER_FUNCTION_P (fndecl)
14782 1238 : && !TYPE_REF_P (TREE_TYPE (arg)))
14783 130 : continue;
14784 1108 : STRIP_NOPS (arg);
14785 1108 : if (TREE_CODE (arg) == ADDR_EXPR)
14786 723 : arg = TREE_OPERAND (arg, 0);
14787 : /* Recurse to see if the argument is a temporary. It could also
14788 : be another call taking a temporary and returning it and
14789 : initializing this reference parameter. */
14790 1108 : if ((arg = do_warn_dangling_reference (arg, /*arg_p=*/true)))
14791 : {
14792 : /* If we know the temporary could not bind to the return type,
14793 : don't warn. This is for scalars and empty classes only
14794 : because for other classes we can't be sure we are not
14795 : returning its sub-object. */
14796 277 : if ((SCALAR_TYPE_P (TREE_TYPE (arg))
14797 152 : || is_empty_class (TREE_TYPE (arg)))
14798 152 : && TYPE_REF_P (rettype)
14799 137 : && !reference_related_p (TREE_TYPE (rettype),
14800 137 : TREE_TYPE (arg)))
14801 21 : continue;
14802 256 : return arg;
14803 : }
14804 : /* Don't warn about member functions like:
14805 : std::any a(...);
14806 : S& s = a.emplace<S>({0}, 0);
14807 : which construct a new object and return a reference to it, but
14808 : we still want to detect:
14809 : struct S { const S& self () { return *this; } };
14810 : const S& s = S().self();
14811 : where 's' dangles. If we've gotten here, the object this function
14812 : is invoked on is not a temporary. */
14813 831 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl))
14814 : break;
14815 : }
14816 : return NULL_TREE;
14817 : }
14818 28 : case COMPOUND_EXPR:
14819 28 : return do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p);
14820 31 : case COND_EXPR:
14821 31 : if (tree t = do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p))
14822 : return t;
14823 16 : return do_warn_dangling_reference (TREE_OPERAND (expr, 2), arg_p);
14824 0 : case PAREN_EXPR:
14825 0 : return do_warn_dangling_reference (TREE_OPERAND (expr, 0), arg_p);
14826 122 : case TARGET_EXPR:
14827 122 : return do_warn_dangling_reference (TARGET_EXPR_INITIAL (expr), arg_p);
14828 : default:
14829 : return NULL_TREE;
14830 : }
14831 : }
14832 :
14833 : /* Implement -Wdangling-reference, to detect cases like
14834 :
14835 : int n = 1;
14836 : const int& r = std::max(n - 1, n + 1); // r is dangling
14837 :
14838 : This creates temporaries from the arguments, returns a reference to
14839 : one of the temporaries, but both temporaries are destroyed at the end
14840 : of the full expression.
14841 :
14842 : This works by checking if a reference is initialized with a function
14843 : that returns a reference, and at least one parameter of the function
14844 : is a reference that is bound to a temporary. It assumes that such a
14845 : function actually returns one of its arguments.
14846 :
14847 : DECL is the reference being initialized, INIT is the initializer. */
14848 :
14849 : static void
14850 102506435 : maybe_warn_dangling_reference (const_tree decl, tree init)
14851 : {
14852 102506435 : if (!warn_dangling_reference)
14853 : return;
14854 616739 : tree type = TREE_TYPE (decl);
14855 : /* Only warn if what we're initializing has type T&& or const T&, or
14856 : std::pair<const T&, const T&>. (A non-const lvalue reference can't
14857 : bind to a temporary.) */
14858 1230930 : if (!((TYPE_REF_OBJ_P (type)
14859 5420 : && (TYPE_REF_IS_RVALUE (type)
14860 4988 : || CP_TYPE_CONST_P (TREE_TYPE (type))))
14861 614191 : || std_pair_ref_ref_p (type)))
14862 : return;
14863 : /* Don't suppress the diagnostic just because the call comes from
14864 : a system header. If the DECL is not in a system header, or if
14865 : -Wsystem-headers was provided, warn. */
14866 2563 : auto wsh
14867 2563 : = make_temp_override (global_dc->m_warn_system_headers,
14868 2563 : (!in_system_header_at (DECL_SOURCE_LOCATION (decl))
14869 2563 : || global_dc->m_warn_system_headers));
14870 2563 : if (tree call = do_warn_dangling_reference (init, /*arg_p=*/false))
14871 : {
14872 211 : auto_diagnostic_group d;
14873 211 : if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wdangling_reference,
14874 : "possibly dangling reference to a temporary"))
14875 211 : inform (EXPR_LOCATION (call), "%qT temporary created here",
14876 211 : TREE_TYPE (call));
14877 211 : }
14878 2563 : }
14879 :
14880 : /* If *P is an xvalue expression, prevent temporary lifetime extension if it
14881 : gets used to initialize a reference. */
14882 :
14883 : static tree
14884 89 : prevent_lifetime_extension (tree t)
14885 : {
14886 89 : tree *p = &t;
14887 89 : while (TREE_CODE (*p) == COMPOUND_EXPR)
14888 0 : p = &TREE_OPERAND (*p, 1);
14889 98 : while (handled_component_p (*p))
14890 9 : p = &TREE_OPERAND (*p, 0);
14891 : /* Change a TARGET_EXPR from prvalue to xvalue. */
14892 89 : if (TREE_CODE (*p) == TARGET_EXPR)
14893 3 : *p = build2 (COMPOUND_EXPR, TREE_TYPE (*p), *p,
14894 3 : move (TARGET_EXPR_SLOT (*p)));
14895 89 : return t;
14896 : }
14897 :
14898 : /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
14899 : which is bound either to a reference or a std::initializer_list. */
14900 :
14901 : static tree
14902 665216 : extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups,
14903 : tree *cond_guard)
14904 : {
14905 : /* CWG1299 (C++20): The temporary object to which the reference is bound or
14906 : the temporary object that is the complete object of a subobject to which
14907 : the reference is bound persists for the lifetime of the reference if the
14908 : glvalue to which the reference is bound was obtained through one of the
14909 : following:
14910 : - a temporary materialization conversion ([conv.rval]),
14911 : - ( expression ), where expression is one of these expressions,
14912 : - subscripting ([expr.sub]) of an array operand, where that operand is one
14913 : of these expressions,
14914 : - a class member access ([expr.ref]) using the . operator where the left
14915 : operand is one of these expressions and the right operand designates a
14916 : non-static data member of non-reference type,
14917 : - a pointer-to-member operation ([expr.mptr.oper]) using the .* operator
14918 : where the left operand is one of these expressions and the right operand
14919 : is a pointer to data member of non-reference type,
14920 : - a const_cast ([expr.const.cast]), static_cast ([expr.static.cast]),
14921 : dynamic_cast ([expr.dynamic.cast]), or reinterpret_cast
14922 : ([expr.reinterpret.cast]) converting, without a user-defined conversion,
14923 : a glvalue operand that is one of these expressions to a glvalue that
14924 : refers to the object designated by the operand, or to its complete
14925 : object or a subobject thereof,
14926 : - a conditional expression ([expr.cond]) that is a glvalue where the
14927 : second or third operand is one of these expressions, or
14928 : - a comma expression ([expr.comma]) that is a glvalue where the right
14929 : operand is one of these expressions. */
14930 :
14931 : /* FIXME several cases are still handled wrong (101572, 81420). */
14932 :
14933 665216 : tree sub = init;
14934 665216 : tree *p;
14935 665216 : STRIP_NOPS (sub);
14936 665216 : if (TREE_CODE (sub) == COMPOUND_EXPR)
14937 : {
14938 242 : TREE_OPERAND (sub, 1)
14939 242 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups,
14940 : cond_guard);
14941 242 : return init;
14942 : }
14943 664974 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14944 664974 : && TYPE_PTRDATAMEM_P (TREE_TYPE (tree_strip_nop_conversions
14945 : (TREE_OPERAND (sub, 1)))))
14946 : {
14947 : /* A pointer-to-member operation. */
14948 6 : TREE_OPERAND (sub, 0)
14949 6 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 0), cleanups,
14950 : cond_guard);
14951 6 : return init;
14952 : }
14953 664968 : if (TREE_CODE (sub) == COND_EXPR)
14954 : {
14955 22432 : tree cur_cond_guard = NULL_TREE;
14956 22432 : if (TREE_OPERAND (sub, 1))
14957 22432 : TREE_OPERAND (sub, 1)
14958 44864 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups,
14959 : &cur_cond_guard);
14960 22432 : if (cur_cond_guard)
14961 : {
14962 9 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard,
14963 : NOP_EXPR, boolean_true_node,
14964 : tf_warning_or_error);
14965 9 : TREE_OPERAND (sub, 1)
14966 18 : = cp_build_compound_expr (set, TREE_OPERAND (sub, 1),
14967 : tf_warning_or_error);
14968 : }
14969 22432 : cur_cond_guard = NULL_TREE;
14970 22432 : if (TREE_OPERAND (sub, 2))
14971 22432 : TREE_OPERAND (sub, 2)
14972 44864 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 2), cleanups,
14973 : &cur_cond_guard);
14974 22432 : if (cur_cond_guard)
14975 : {
14976 12 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard,
14977 : NOP_EXPR, boolean_true_node,
14978 : tf_warning_or_error);
14979 12 : TREE_OPERAND (sub, 2)
14980 24 : = cp_build_compound_expr (set, TREE_OPERAND (sub, 2),
14981 : tf_warning_or_error);
14982 : }
14983 22432 : return init;
14984 : }
14985 642536 : if (TREE_CODE (sub) != ADDR_EXPR)
14986 : return init;
14987 : /* Deal with binding to a subobject. */
14988 196621 : for (p = &TREE_OPERAND (sub, 0);
14989 210392 : TREE_CODE (*p) == COMPONENT_REF || TREE_CODE (*p) == ARRAY_REF; )
14990 13771 : p = &TREE_OPERAND (*p, 0);
14991 196621 : if (TREE_CODE (*p) == TARGET_EXPR)
14992 : {
14993 6161 : tree subinit = NULL_TREE;
14994 6161 : *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit,
14995 : cond_guard, nullptr);
14996 6161 : recompute_tree_invariant_for_addr_expr (sub);
14997 6161 : if (init != sub)
14998 6143 : init = fold_convert (TREE_TYPE (init), sub);
14999 6161 : if (subinit)
15000 5174 : init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
15001 : }
15002 : return init;
15003 : }
15004 :
15005 : /* Data for extend_temps_r, mostly matching the parameters of
15006 : extend_ref_init_temps. */
15007 :
15008 : struct extend_temps_data
15009 : {
15010 : tree decl;
15011 : tree init;
15012 : vec<tree, va_gc> **cleanups;
15013 : tree* cond_guard;
15014 : hash_set<tree> *pset; // For avoiding redundant walk_tree.
15015 : hash_map<tree, tree> *var_map; // For remapping extended temps.
15016 : };
15017 :
15018 : /* Tree walk function for extend_all_temps. Generally parallel to
15019 : extend_ref_init_temps_1, but adapted for walk_tree. */
15020 :
15021 : tree
15022 70726 : extend_temps_r (tree *tp, int *walk_subtrees, void *data)
15023 : {
15024 70726 : extend_temps_data *d = (extend_temps_data *)data;
15025 :
15026 70726 : if (TREE_CODE (*tp) == VAR_DECL)
15027 : {
15028 8994 : if (tree *r = d->var_map->get (*tp))
15029 0 : *tp = *r;
15030 8994 : return NULL_TREE;
15031 : }
15032 :
15033 61717 : if (TYPE_P (*tp) || TREE_CODE (*tp) == CLEANUP_POINT_EXPR
15034 123448 : || d->pset->add (*tp))
15035 : {
15036 3605 : *walk_subtrees = 0;
15037 3605 : return NULL_TREE;
15038 : }
15039 :
15040 58127 : if (TREE_CODE (*tp) == COND_EXPR)
15041 : {
15042 8 : cp_walk_tree (&TREE_OPERAND (*tp, 0), extend_temps_r, d, nullptr);
15043 :
15044 24 : auto walk_arm = [d](tree &op)
15045 : {
15046 16 : tree cur_cond_guard = NULL_TREE;
15047 16 : auto ov = make_temp_override (d->cond_guard, &cur_cond_guard);
15048 16 : cp_walk_tree (&op, extend_temps_r, d, nullptr);
15049 16 : if (cur_cond_guard)
15050 : {
15051 2 : tree set = build2 (MODIFY_EXPR, boolean_type_node,
15052 : cur_cond_guard, boolean_true_node);
15053 2 : op = cp_build_compound_expr (set, op, tf_none);
15054 : }
15055 24 : };
15056 8 : walk_arm (TREE_OPERAND (*tp, 1));
15057 8 : walk_arm (TREE_OPERAND (*tp, 2));
15058 :
15059 8 : *walk_subtrees = 0;
15060 8 : return NULL_TREE;
15061 : }
15062 :
15063 58119 : tree *p = tp;
15064 :
15065 58119 : if (TREE_CODE (*tp) == ADDR_EXPR)
15066 13238 : for (p = &TREE_OPERAND (*tp, 0);
15067 14342 : TREE_CODE (*p) == COMPONENT_REF || TREE_CODE (*p) == ARRAY_REF; )
15068 1104 : p = &TREE_OPERAND (*p, 0);
15069 :
15070 58119 : if (TREE_CODE (*p) == TARGET_EXPR
15071 : /* An eliding TARGET_EXPR isn't a temporary at all. */
15072 2374 : && !TARGET_EXPR_ELIDING_P (*p)
15073 : /* A TARGET_EXPR with TARGET_EXPR_INTERNAL_P is an artificial variable
15074 : used during initialization that need not be extended. */
15075 59912 : && !TARGET_EXPR_INTERNAL_P (*p))
15076 : {
15077 : /* A CLEANUP_EH_ONLY expr should also have TARGET_EXPR_INTERNAL_P. */
15078 1481 : gcc_checking_assert (!CLEANUP_EH_ONLY (*p));
15079 :
15080 1481 : tree subinit = NULL_TREE;
15081 1481 : tree slot = TARGET_EXPR_SLOT (*p);
15082 1481 : *p = set_up_extended_ref_temp (d->decl, *p, d->cleanups, &subinit,
15083 : d->cond_guard, d);
15084 1481 : if (TREE_CODE (*tp) == ADDR_EXPR)
15085 1467 : recompute_tree_invariant_for_addr_expr (*tp);
15086 1481 : if (subinit)
15087 1374 : *tp = cp_build_compound_expr (subinit, *tp, tf_none);
15088 1481 : d->var_map->put (slot, *p);
15089 : }
15090 :
15091 : return NULL_TREE;
15092 : }
15093 :
15094 : /* Extend all the temporaries in a for-range-initializer. */
15095 :
15096 : static tree
15097 14251 : extend_all_temps (tree decl, tree init, vec<tree, va_gc> **cleanups)
15098 : {
15099 14251 : hash_set<tree> pset;
15100 14251 : hash_map<tree, tree> map;
15101 14251 : gcc_assert (!TREE_STATIC (decl));
15102 14251 : extend_temps_data d = { decl, init, cleanups, nullptr, &pset, &map };
15103 14251 : cp_walk_tree (&init, extend_temps_r, &d, nullptr);
15104 14251 : return init;
15105 14251 : }
15106 :
15107 : /* INIT is part of the initializer for DECL. If there are any
15108 : reference or initializer lists being initialized, extend their
15109 : lifetime to match that of DECL. */
15110 :
15111 : tree
15112 104818708 : extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups,
15113 : tree *cond_guard)
15114 : {
15115 104818708 : tree type = TREE_TYPE (init);
15116 104818708 : if (processing_template_decl)
15117 : return init;
15118 :
15119 : /* P2718R0 - in C++23 for-range-initializer, extend all temps. */
15120 102520686 : if (DECL_NAME (decl) == for_range__identifier
15121 88548 : && flag_range_for_ext_temps
15122 : /* Iterating expansion statement decl is static right now, but that
15123 : could change depending on CWG3044 and CWG3043. */
15124 102534963 : && !TREE_STATIC (decl))
15125 : {
15126 14251 : gcc_checking_assert (!cond_guard);
15127 14251 : return extend_all_temps (decl, init, cleanups);
15128 : }
15129 :
15130 102506435 : maybe_warn_dangling_reference (decl, init);
15131 :
15132 102506435 : if (TYPE_REF_P (type))
15133 619511 : init = extend_ref_init_temps_1 (decl, init, cleanups, cond_guard);
15134 : else
15135 : {
15136 101886924 : tree ctor = init;
15137 101886924 : if (TREE_CODE (ctor) == TARGET_EXPR)
15138 1394782 : ctor = TARGET_EXPR_INITIAL (ctor);
15139 101886924 : if (TREE_CODE (ctor) == CONSTRUCTOR)
15140 : {
15141 : /* [dcl.init] When initializing an aggregate from a parenthesized list
15142 : of values... a temporary object bound to a reference does not have
15143 : its lifetime extended. */
15144 5061723 : if (CONSTRUCTOR_IS_PAREN_INIT (ctor))
15145 : return init;
15146 :
15147 5061352 : if (is_std_init_list (type))
15148 : {
15149 : /* The temporary array underlying a std::initializer_list
15150 : is handled like a reference temporary. */
15151 593 : tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
15152 593 : array = extend_ref_init_temps_1 (decl, array, cleanups,
15153 : cond_guard);
15154 593 : CONSTRUCTOR_ELT (ctor, 0)->value = array;
15155 : }
15156 : else
15157 : {
15158 5060759 : unsigned i;
15159 5060759 : constructor_elt *p;
15160 5060759 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
15161 59134693 : FOR_EACH_VEC_SAFE_ELT (elts, i, p)
15162 54073934 : p->value = extend_ref_init_temps (decl, p->value, cleanups,
15163 : cond_guard);
15164 : }
15165 5061352 : recompute_constructor_flags (ctor);
15166 5061352 : if (decl_maybe_constant_var_p (decl) && TREE_CONSTANT (ctor))
15167 2744100 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
15168 : }
15169 : }
15170 :
15171 : return init;
15172 : }
15173 :
15174 : /* Returns true iff an initializer for TYPE could contain temporaries that
15175 : need to be extended because they are bound to references or
15176 : std::initializer_list. */
15177 :
15178 : bool
15179 5287 : type_has_extended_temps (tree type)
15180 : {
15181 5287 : type = strip_array_types (type);
15182 5287 : if (TYPE_REF_P (type))
15183 : return true;
15184 5230 : if (CLASS_TYPE_P (type))
15185 : {
15186 2844 : if (is_std_init_list (type))
15187 : return true;
15188 2841 : for (tree f = next_aggregate_field (TYPE_FIELDS (type));
15189 6702 : f; f = next_aggregate_field (DECL_CHAIN (f)))
15190 3990 : if (type_has_extended_temps (TREE_TYPE (f)))
15191 : return true;
15192 : }
15193 : return false;
15194 : }
15195 :
15196 : /* Returns true iff TYPE is some variant of std::initializer_list. */
15197 :
15198 : bool
15199 172299830 : is_std_init_list (tree type)
15200 : {
15201 172299830 : if (!TYPE_P (type))
15202 : return false;
15203 172299807 : if (cxx_dialect == cxx98)
15204 : return false;
15205 : /* Look through typedefs. */
15206 171897803 : type = TYPE_MAIN_VARIANT (type);
15207 137633999 : return (CLASS_TYPE_P (type)
15208 137495581 : && CP_TYPE_CONTEXT (type) == std_node
15209 248813265 : && init_list_identifier == DECL_NAME (TYPE_NAME (type)));
15210 : }
15211 :
15212 : /* Returns true iff DECL is a list constructor: i.e. a constructor which
15213 : will accept an argument list of a single std::initializer_list<T>. */
15214 :
15215 : bool
15216 150326559 : is_list_ctor (tree decl)
15217 : {
15218 150326559 : tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
15219 150326559 : tree arg;
15220 :
15221 150326559 : if (!args || args == void_list_node)
15222 : return false;
15223 :
15224 121015669 : arg = non_reference (TREE_VALUE (args));
15225 121015669 : if (!is_std_init_list (arg))
15226 : return false;
15227 :
15228 2182239 : args = TREE_CHAIN (args);
15229 :
15230 3716992 : if (args && args != void_list_node && !TREE_PURPOSE (args))
15231 : /* There are more non-defaulted parms. */
15232 : return false;
15233 :
15234 : return true;
15235 : }
15236 :
15237 : /* We know that can_convert_arg_bad already said "no" when trying to convert
15238 : FROM to TO with ARG and FLAGS. Try to figure out if it was because
15239 : an explicit conversion function was skipped when looking for a way to
15240 : perform the conversion. At this point we've already printed an error. */
15241 :
15242 : void
15243 2056 : maybe_show_nonconverting_candidate (tree to, tree from, tree arg, int flags)
15244 : {
15245 2056 : if (!(flags & LOOKUP_ONLYCONVERTING))
15246 289 : return;
15247 :
15248 1767 : conversion_obstack_sentinel cos;
15249 1767 : conversion *c = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
15250 : flags & ~LOOKUP_ONLYCONVERTING, tf_none);
15251 1767 : if (c && !c->bad_p && c->user_conv_p)
15252 : /* Ay, the conversion would have worked in direct-init context. */
15253 778 : for (; c; c = next_conversion (c))
15254 521 : if (c->kind == ck_user
15255 254 : && DECL_P (c->cand->fn)
15256 775 : && DECL_NONCONVERTING_P (c->cand->fn))
15257 250 : inform (DECL_SOURCE_LOCATION (c->cand->fn), "explicit conversion "
15258 : "function was not considered");
15259 1767 : }
15260 :
15261 : /* We're converting EXPR to TYPE. If that conversion involves a conversion
15262 : function and we're binding EXPR to a reference parameter of that function,
15263 : return true. */
15264 :
15265 : bool
15266 171 : conv_binds_to_reference_parm_p (tree type, tree expr)
15267 : {
15268 171 : conversion_obstack_sentinel cos;
15269 171 : conversion *c = implicit_conversion (type, TREE_TYPE (expr), expr,
15270 : /*c_cast_p=*/false, LOOKUP_NORMAL,
15271 : tf_none);
15272 171 : if (c && !c->bad_p && c->user_conv_p)
15273 117 : for (; c; c = next_conversion (c))
15274 81 : if (c->kind == ck_user)
15275 244 : for (z_candidate *cand = c->cand; cand; cand = cand->next)
15276 208 : if (cand->viable == 1)
15277 81 : for (size_t i = 0; i < cand->num_convs; ++i)
15278 45 : if (cand->convs[i]->kind == ck_ref_bind
15279 45 : && conv_get_original_expr (cand->convs[i]) == expr)
15280 : return true;
15281 :
15282 : return false;
15283 171 : }
15284 :
15285 : #include "gt-cp-call.h"
|