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 user_conv_p : 1;
99 : bool ellipsis_p : 1;
100 : bool 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 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 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 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 rvaluedness_matches_p: 1;
120 : bool check_narrowing: 1;
121 : /* Whether check_narrowing should only check TREE_CONSTANTs; used
122 : in build_converted_constant_expr. */
123 : bool 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 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 3217250 : check_dtor_name (tree basetype, tree name)
236 : {
237 : /* Just accept something we've already complained about. */
238 3217250 : if (name == error_mark_node)
239 : return true;
240 :
241 3217250 : if (TREE_CODE (name) == TYPE_DECL)
242 84 : name = TREE_TYPE (name);
243 3217166 : 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 3217224 : 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 363557710 : build_addr_func (tree function, tsubst_flags_t complain)
282 : {
283 363557710 : tree type = TREE_TYPE (function);
284 :
285 : /* We have to do these by hand to avoid real pointer to member
286 : functions. */
287 363557710 : if (TREE_CODE (type) == METHOD_TYPE)
288 : {
289 55536565 : if (TREE_CODE (function) == OFFSET_REF)
290 : {
291 73 : tree object = build_address (TREE_OPERAND (function, 0));
292 73 : return get_member_function_from_ptrfunc (&object,
293 73 : TREE_OPERAND (function, 1),
294 : complain);
295 : }
296 55536492 : function = build_address (function);
297 : }
298 308021145 : else if (TREE_CODE (function) == FUNCTION_DECL
299 439733262 : && DECL_IMMEDIATE_FUNCTION_P (function))
300 1924639 : function = build_address (function);
301 : else
302 306096506 : 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 171219 : build_call_n (tree function, int n, ...)
315 : {
316 171219 : if (n == 0)
317 0 : return build_call_a (function, 0, NULL);
318 : else
319 : {
320 171219 : tree *argarray = XALLOCAVEC (tree, n);
321 171219 : va_list ap;
322 171219 : int i;
323 :
324 171219 : va_start (ap, n);
325 343667 : for (i = 0; i < n; i++)
326 172448 : argarray[i] = va_arg (ap, tree);
327 171219 : va_end (ap);
328 171219 : 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 177449916 : set_flags_from_callee (tree call)
337 : {
338 : /* Handle both CALL_EXPRs and AGGR_INIT_EXPRs. */
339 177449916 : 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 177449916 : bool nothrow = decl && TREE_NOTHROW (decl);
344 177449916 : tree callee = cp_get_callee (call);
345 177449916 : if (callee)
346 177449908 : 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 177449916 : if (cfun && cp_function_chain && !cp_unevaluated_operand)
352 : {
353 101665812 : if (!nothrow && at_function_scope_p ())
354 25117335 : cp_function_chain->can_throw = 1;
355 :
356 101665812 : if (decl && TREE_THIS_VOLATILE (decl))
357 3053701 : current_function_returns_abnormally = 1;
358 : }
359 :
360 177449916 : TREE_NOTHROW (call) = nothrow;
361 177449916 : }
362 :
363 : tree
364 171382363 : build_call_a (tree function, int n, tree *argarray)
365 : {
366 171382363 : tree decl;
367 171382363 : tree result_type;
368 171382363 : tree fntype;
369 171382363 : int i;
370 :
371 171382363 : function = build_addr_func (function, tf_warning_or_error);
372 :
373 171382363 : gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
374 171382363 : fntype = TREE_TYPE (TREE_TYPE (function));
375 171382363 : gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype));
376 171382363 : result_type = TREE_TYPE (fntype);
377 : /* An rvalue has no cv-qualifiers. */
378 171382363 : if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
379 99753860 : result_type = cv_unqualified (result_type);
380 :
381 171382363 : function = build_call_array_loc (input_location,
382 : result_type, function, n, argarray);
383 171382363 : set_flags_from_callee (function);
384 :
385 171382363 : decl = get_callee_fndecl (function);
386 :
387 171382363 : 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 217063 : gcc_assert (DECL_ARTIFICIAL (decl)
394 : || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
395 : "__", 2));
396 217063 : mark_used (decl);
397 : }
398 :
399 171382363 : require_complete_eh_spec_types (fntype, decl);
400 :
401 494286437 : 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 171382363 : if (!decl || !fndecl_built_in_p (decl))
407 343780880 : for (i = 0; i < n; i++)
408 : {
409 180336788 : tree arg = CALL_EXPR_ARG (function, i);
410 180336788 : if (is_empty_class (TREE_TYPE (arg))
411 180336788 : && simple_empty_class_p (TREE_TYPE (arg), arg, INIT_EXPR))
412 : {
413 5697812 : while (TREE_CODE (arg) == TARGET_EXPR)
414 : /* We're disconnecting the initializer from its target,
415 : don't create a temporary. */
416 2847726 : arg = TARGET_EXPR_INITIAL (arg);
417 2850086 : suppress_warning (arg, OPT_Wunused_result);
418 2850086 : tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg));
419 2850086 : arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t);
420 2850086 : CALL_EXPR_ARG (function, i) = arg;
421 : }
422 : }
423 :
424 171382363 : 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 65850317 : bool rewritten () const { return (flags & LOOKUP_REWRITTEN); }
543 1062326650 : 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 131022440 : null_ptr_cst_p (tree t)
551 : {
552 131022440 : 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 131022440 : if (NULLPTR_TYPE_P (type))
559 : return true;
560 :
561 121838751 : if (cxx_dialect >= cxx11)
562 : {
563 121441895 : STRIP_ANY_LOCATION_WRAPPER (t);
564 :
565 : /* Core issue 903 says only literal 0 is a null pointer constant. */
566 121441895 : if (TREE_CODE (t) == INTEGER_CST
567 13835817 : && !TREE_OVERFLOW (t)
568 13835817 : && TREE_CODE (type) == INTEGER_TYPE
569 13226723 : && integer_zerop (t)
570 130233277 : && !char_type_p (type))
571 8791086 : return true;
572 : }
573 396856 : else if (CP_INTEGRAL_TYPE_P (type))
574 : {
575 76647 : t = fold_non_dependent_expr (t, tf_none);
576 76647 : STRIP_NOPS (t);
577 76647 : if (integer_zerop (t) && !TREE_OVERFLOW (t))
578 54973 : 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 102330394 : null_member_pointer_value_p (tree t)
588 : {
589 102330394 : tree type = TREE_TYPE (t);
590 102330394 : if (!type)
591 : return false;
592 102142291 : else if (TYPE_PTRMEMFUNC_P (type))
593 1109 : return (TREE_CODE (t) == CONSTRUCTOR
594 564 : && CONSTRUCTOR_NELTS (t)
595 1670 : && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value));
596 102141182 : else if (TYPE_PTRDATAMEM_P (type))
597 5967 : 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 739671066 : sufficient_parms_p (const_tree parmlist)
607 : {
608 749498652 : for (; parmlist && parmlist != void_list_node;
609 9827586 : parmlist = TREE_CHAIN (parmlist))
610 225472972 : if (!TREE_PURPOSE (parmlist)
611 225472972 : && !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 8034804062 : conversion_obstack_alloc (size_t n)
621 : {
622 8034804062 : void *p;
623 8034804062 : if (!conversion_obstack_initialized)
624 : {
625 87069 : gcc_obstack_init (&conversion_obstack);
626 87069 : conversion_obstack_initialized = true;
627 : }
628 8034804062 : p = obstack_alloc (&conversion_obstack, n);
629 8034804062 : memset (p, 0, n);
630 8034804062 : 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 2624810018 : conversion_obstack_sentinel (): p (conversion_obstack_alloc (0)) {}
639 1312391455 : ~conversion_obstack_sentinel () { obstack_free (&conversion_obstack, p); }
640 : };
641 :
642 : /* Allocate rejection reasons. */
643 :
644 : static struct rejection_reason *
645 769477423 : alloc_rejection (enum rejection_reason_code code)
646 : {
647 769477423 : struct rejection_reason *p;
648 0 : p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
649 769477423 : p->code = code;
650 769477423 : return p;
651 : }
652 :
653 : static struct rejection_reason *
654 196716721 : arity_rejection (tree first_arg, int expected, int actual, bool least_p = false)
655 : {
656 156732049 : struct rejection_reason *r = alloc_rejection (rr_arity);
657 196716721 : int adjust = first_arg != NULL_TREE;
658 196716721 : r->u.arity.expected = expected - adjust;
659 196716721 : r->u.arity.actual = actual - adjust;
660 196716721 : r->u.arity.least_p = least_p;
661 196716721 : return r;
662 : }
663 :
664 : static struct rejection_reason *
665 164226316 : arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to,
666 : location_t loc)
667 : {
668 7140982 : struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
669 164226316 : int adjust = first_arg != NULL_TREE;
670 164226316 : r->u.conversion.n_arg = n_arg - adjust;
671 164226316 : r->u.conversion.from = from;
672 164226316 : r->u.conversion.to_type = to;
673 164226316 : r->u.conversion.loc = loc;
674 164226316 : return r;
675 : }
676 :
677 : static struct rejection_reason *
678 22587866 : bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to,
679 : location_t loc)
680 : {
681 2603 : struct rejection_reason *r = alloc_rejection (rr_bad_arg_conversion);
682 22587866 : int adjust = first_arg != NULL_TREE;
683 22587866 : r->u.bad_conversion.n_arg = n_arg - adjust;
684 22587866 : r->u.bad_conversion.from = from;
685 22587866 : r->u.bad_conversion.to_type = to;
686 22587866 : r->u.bad_conversion.loc = loc;
687 22587866 : return r;
688 : }
689 :
690 : static struct rejection_reason *
691 2735 : explicit_conversion_rejection (tree from, tree to)
692 : {
693 2735 : struct rejection_reason *r = alloc_rejection (rr_explicit_conversion);
694 2735 : r->u.conversion.n_arg = 0;
695 2735 : r->u.conversion.from = from;
696 2735 : r->u.conversion.to_type = to;
697 2735 : r->u.conversion.loc = UNKNOWN_LOCATION;
698 2735 : 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 384180440 : 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 384180440 : size_t args_n_bytes = sizeof (*args) * nargs;
719 384180440 : tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes);
720 384180440 : struct rejection_reason *r = alloc_rejection (rr_template_unification);
721 384180440 : r->u.template_unification.tmpl = tmpl;
722 384180440 : r->u.template_unification.explicit_targs = explicit_targs;
723 384180440 : r->u.template_unification.num_targs = TREE_VEC_LENGTH (targs);
724 : /* Copy args to our own storage. */
725 384180440 : memcpy (args1, args, args_n_bytes);
726 384180440 : r->u.template_unification.args = args1;
727 384180440 : r->u.template_unification.nargs = nargs;
728 384180440 : r->u.template_unification.return_type = return_type;
729 384180440 : r->u.template_unification.strict = strict;
730 384180440 : r->u.template_unification.flags = flags;
731 384180440 : return r;
732 : }
733 :
734 : static struct rejection_reason *
735 116 : template_unification_error_rejection (void)
736 : {
737 0 : return alloc_rejection (rr_template_unification);
738 : }
739 :
740 : static struct rejection_reason *
741 757354 : invalid_copy_with_fn_template_rejection (void)
742 : {
743 0 : struct rejection_reason *r = alloc_rejection (rr_invalid_copy);
744 757354 : return r;
745 : }
746 :
747 : static struct rejection_reason *
748 807010 : inherited_ctor_rejection (void)
749 : {
750 0 : struct rejection_reason *r = alloc_rejection (rr_inherited_ctor);
751 807010 : return r;
752 : }
753 :
754 : /* Build a constraint failure record. */
755 :
756 : static struct rejection_reason *
757 198844 : constraint_failure (void)
758 : {
759 0 : struct rejection_reason *r = alloc_rejection (rr_constraint_failure);
760 198844 : return r;
761 : }
762 :
763 : /* Dynamically allocate a conversion. */
764 :
765 : static conversion *
766 2781474093 : alloc_conversion (conversion_kind kind)
767 : {
768 2781474093 : conversion *c;
769 0 : c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
770 2781474093 : c->kind = kind;
771 2781474093 : return c;
772 : }
773 :
774 : /* Make sure that all memory on the conversion obstack has been
775 : freed. */
776 :
777 : void
778 99470 : validate_conversion_obstack (void)
779 : {
780 99470 : if (conversion_obstack_initialized)
781 86735 : gcc_assert ((obstack_next_free (&conversion_obstack)
782 : == obstack_base (&conversion_obstack)));
783 99470 : }
784 :
785 : /* Dynamically allocate an array of N conversions. */
786 :
787 : static conversion **
788 1033571767 : 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 1523258262 : has_next (conversion_kind code)
797 : {
798 1431584555 : return !(code == ck_identity
799 1523258262 : || code == ck_ambig
800 : || code == ck_list
801 1431677604 : || code == ck_aggr
802 1523258262 : || code == ck_deferred_bad);
803 : }
804 :
805 : static conversion *
806 817419645 : build_conv (conversion_kind code, tree type, conversion *from)
807 : {
808 817419645 : conversion *t;
809 817419645 : conversion_rank rank = CONVERSION_RANK (from);
810 :
811 : /* Only call this function for conversions that use u.next. */
812 817419645 : 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 817419645 : t = alloc_conversion (code);
817 817419645 : t->type = type;
818 817419645 : t->u.next = from;
819 :
820 817419645 : switch (code)
821 : {
822 238977235 : case ck_ptr:
823 238977235 : case ck_pmem:
824 238977235 : case ck_base:
825 238977235 : case ck_std:
826 238977235 : if (rank < cr_std)
827 817419645 : rank = cr_std;
828 : break;
829 :
830 55276075 : case ck_qual:
831 55276075 : case ck_fnptr:
832 55276075 : if (rank < cr_exact)
833 817419645 : rank = cr_exact;
834 : break;
835 :
836 : default:
837 : break;
838 : }
839 817419645 : t->rank = rank;
840 817419645 : t->user_conv_p = (code == ck_user || from->user_conv_p);
841 817419645 : t->bad_p = from->bad_p;
842 817419645 : t->base_p = false;
843 817419645 : 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 97554 : build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
852 : {
853 97554 : tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
854 97554 : unsigned len = CONSTRUCTOR_NELTS (ctor);
855 97554 : conversion **subconvs = alloc_conversions (len);
856 97554 : conversion *t;
857 97554 : unsigned i;
858 97554 : tree val;
859 :
860 : /* Within a list-initialization we can have more user-defined
861 : conversions. */
862 97554 : flags &= ~LOOKUP_NO_CONVERSION;
863 : /* But no narrowing conversions. */
864 97554 : flags |= LOOKUP_NO_NARROWING;
865 :
866 : /* Can't make an array of these types. */
867 97554 : if (TYPE_REF_P (elttype)
868 97545 : || TREE_CODE (elttype) == FUNCTION_TYPE
869 97545 : || VOID_TYPE_P (elttype))
870 : return NULL;
871 :
872 362207 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
873 : {
874 278304 : 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 278253 : conversion *sub
936 278253 : = implicit_conversion (elttype, TREE_TYPE (val), val,
937 : false, flags, complain);
938 278253 : if (sub == NULL)
939 : return NULL;
940 :
941 264611 : subconvs[i] = sub;
942 : }
943 :
944 83903 : t = alloc_conversion (ck_list);
945 83903 : t->type = type;
946 83903 : t->u.list = subconvs;
947 83903 : t->rank = cr_exact;
948 :
949 329723 : for (i = 0; i < len; ++i)
950 : {
951 245820 : conversion *sub = subconvs[i];
952 245820 : if (sub->rank > t->rank)
953 63511 : t->rank = sub->rank;
954 245820 : if (sub->user_conv_p)
955 10366 : t->user_conv_p = true;
956 245820 : if (sub->bad_p)
957 63498 : 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 614415563 : next_conversion (conversion *conv)
969 : {
970 614415563 : if (conv == NULL
971 614415563 : || !has_next (conv->kind))
972 : return NULL;
973 601998909 : 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 1534598 : strip_standard_conversion (conversion *conv)
981 : {
982 1534598 : while (conv
983 1545048 : && conv->kind != ck_user
984 1587066 : && has_next (conv->kind))
985 10450 : conv = next_conversion (conv);
986 1534598 : 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 27894 : can_convert_array (tree atype, tree from, int flags, tsubst_flags_t complain)
994 : {
995 27906 : tree elttype = TREE_TYPE (atype);
996 27906 : unsigned i;
997 :
998 27906 : if (TREE_CODE (from) == CONSTRUCTOR)
999 : {
1000 31110 : for (i = 0; i < CONSTRUCTOR_NELTS (from); ++i)
1001 : {
1002 3261 : tree val = CONSTRUCTOR_ELT (from, i)->value;
1003 3261 : bool ok;
1004 3261 : if (TREE_CODE (elttype) == ARRAY_TYPE)
1005 18 : ok = can_convert_array (elttype, val, flags, complain);
1006 : else
1007 3243 : ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags,
1008 : complain);
1009 3261 : if (!ok)
1010 : return false;
1011 : }
1012 : return true;
1013 : }
1014 :
1015 57 : if (char_type_p (TYPE_MAIN_VARIANT (elttype))
1016 57 : && TREE_CODE (tree_strip_any_location_wrapper (from)) == STRING_CST)
1017 45 : return array_string_literal_compatible_p (atype, from);
1018 :
1019 12 : if (tree vi = (get_vec_init_expr (from)))
1020 12 : return can_convert_array (atype, VEC_INIT_EXPR_INIT (vi), flags, complain);
1021 :
1022 : /* No other valid way to aggregate initialize an array. */
1023 : return false;
1024 : }
1025 :
1026 : /* Helper for build_aggr_conv. Return true if FIELD is in PSET, or if
1027 : FIELD has ANON_AGGR_TYPE_P and any initializable field in there recursively
1028 : is in PSET. */
1029 :
1030 : static bool
1031 871005 : field_in_pset (hash_set<tree, true> &pset, tree field)
1032 : {
1033 871005 : if (pset.contains (field))
1034 : return true;
1035 2598 : if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1036 0 : for (field = TYPE_FIELDS (TREE_TYPE (field));
1037 0 : field; field = DECL_CHAIN (field))
1038 : {
1039 0 : field = next_aggregate_field (field);
1040 0 : if (field == NULL_TREE)
1041 : break;
1042 0 : if (field_in_pset (pset, field))
1043 : return true;
1044 : }
1045 : return false;
1046 : }
1047 :
1048 : /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
1049 : aggregate class, if such a conversion is possible. */
1050 :
1051 : static conversion *
1052 1370754 : build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
1053 : {
1054 1370754 : unsigned HOST_WIDE_INT i = 0;
1055 1370754 : conversion *c;
1056 1370754 : tree field = next_aggregate_field (TYPE_FIELDS (type));
1057 1370754 : tree empty_ctor = NULL_TREE;
1058 1370754 : hash_set<tree, true> pset;
1059 :
1060 : /* We already called reshape_init in implicit_conversion, but it might not
1061 : have done anything in the case of parenthesized aggr init. */
1062 :
1063 : /* The conversions within the init-list aren't affected by the enclosing
1064 : context; they're always simple copy-initialization. */
1065 1370754 : flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1066 :
1067 : /* For designated initializers, verify that each initializer is convertible
1068 : to corresponding TREE_TYPE (ce->index) and mark those FIELD_DECLs as
1069 : visited. In the following loop then ignore already visited
1070 : FIELD_DECLs. */
1071 1370754 : tree idx, val;
1072 3609915 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, val)
1073 : {
1074 868499 : if (!idx)
1075 : break;
1076 :
1077 868439 : gcc_checking_assert (TREE_CODE (idx) == FIELD_DECL);
1078 :
1079 868439 : tree ftype = TREE_TYPE (idx);
1080 868439 : bool ok;
1081 :
1082 868439 : if (TREE_CODE (ftype) == ARRAY_TYPE)
1083 1189 : ok = can_convert_array (ftype, val, flags, complain);
1084 : else
1085 867250 : ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
1086 : complain);
1087 :
1088 868439 : if (!ok)
1089 : return NULL;
1090 :
1091 : /* For unions, there should be just one initializer. */
1092 868430 : if (TREE_CODE (type) == UNION_TYPE)
1093 : {
1094 1370745 : field = NULL_TREE;
1095 1370745 : i = 1;
1096 : break;
1097 : }
1098 868407 : pset.add (idx);
1099 : }
1100 :
1101 2349582 : for (; field; field = next_aggregate_field (DECL_CHAIN (field)))
1102 : {
1103 979097 : tree ftype = TREE_TYPE (field);
1104 979097 : bool ok;
1105 :
1106 979097 : if (!pset.is_empty () && field_in_pset (pset, field))
1107 868407 : continue;
1108 110690 : if (i < CONSTRUCTOR_NELTS (ctor))
1109 : {
1110 39 : constructor_elt *ce = CONSTRUCTOR_ELT (ctor, i);
1111 39 : gcc_checking_assert (!ce->index);
1112 39 : val = ce->value;
1113 39 : ++i;
1114 : }
1115 110651 : else if (DECL_INITIAL (field))
1116 44935 : val = get_nsdmi (field, /*ctor*/false, complain);
1117 65716 : else if (TYPE_REF_P (ftype))
1118 : /* Value-initialization of reference is ill-formed. */
1119 : return NULL;
1120 : else
1121 : {
1122 65710 : if (empty_ctor == NULL_TREE)
1123 46885 : empty_ctor = build_constructor (init_list_type_node, NULL);
1124 : val = empty_ctor;
1125 : }
1126 :
1127 110684 : if (TREE_CODE (ftype) == ARRAY_TYPE)
1128 26687 : ok = can_convert_array (ftype, val, flags, complain);
1129 : else
1130 83997 : ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
1131 : complain);
1132 :
1133 110684 : if (!ok)
1134 : return NULL;
1135 :
1136 110671 : if (TREE_CODE (type) == UNION_TYPE)
1137 : break;
1138 : }
1139 :
1140 1370726 : if (i < CONSTRUCTOR_NELTS (ctor))
1141 : return NULL;
1142 :
1143 1370699 : c = alloc_conversion (ck_aggr);
1144 1370699 : c->type = type;
1145 1370699 : c->rank = cr_exact;
1146 1370699 : c->user_conv_p = true;
1147 1370699 : c->check_narrowing = true;
1148 1370699 : c->u.expr = ctor;
1149 1370699 : return c;
1150 1370754 : }
1151 :
1152 : /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
1153 : array type, if such a conversion is possible. */
1154 :
1155 : static conversion *
1156 1889 : build_array_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
1157 : {
1158 1889 : conversion *c;
1159 1889 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1160 1889 : tree elttype = TREE_TYPE (type);
1161 1889 : bool bad = false;
1162 1889 : bool user = false;
1163 1889 : enum conversion_rank rank = cr_exact;
1164 :
1165 : /* We might need to propagate the size from the element to the array. */
1166 1889 : complete_type (type);
1167 :
1168 1889 : if (TYPE_DOMAIN (type)
1169 1889 : && !variably_modified_type_p (TYPE_DOMAIN (type), NULL_TREE))
1170 : {
1171 1815 : unsigned HOST_WIDE_INT alen = tree_to_uhwi (array_type_nelts_top (type));
1172 1815 : if (alen < len)
1173 : return NULL;
1174 : }
1175 :
1176 1842 : flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1177 :
1178 7707 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
1179 : {
1180 3030 : conversion *sub
1181 3030 : = implicit_conversion (elttype, TREE_TYPE (e.value), e.value,
1182 : false, flags, complain);
1183 3030 : if (sub == NULL)
1184 : return NULL;
1185 :
1186 2267 : if (sub->rank > rank)
1187 287 : rank = sub->rank;
1188 2267 : if (sub->user_conv_p)
1189 27 : user = true;
1190 2267 : if (sub->bad_p)
1191 218 : bad = true;
1192 : }
1193 :
1194 1079 : c = alloc_conversion (ck_aggr);
1195 1079 : c->type = type;
1196 1079 : c->rank = rank;
1197 1079 : c->user_conv_p = user;
1198 1079 : c->bad_p = bad;
1199 1079 : c->u.expr = ctor;
1200 1079 : return c;
1201 : }
1202 :
1203 : /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
1204 : complex type, if such a conversion is possible. */
1205 :
1206 : static conversion *
1207 57208 : build_complex_conv (tree type, tree ctor, int flags,
1208 : tsubst_flags_t complain)
1209 : {
1210 57208 : conversion *c;
1211 57208 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1212 57208 : tree elttype = TREE_TYPE (type);
1213 57208 : bool bad = false;
1214 57208 : bool user = false;
1215 57208 : enum conversion_rank rank = cr_exact;
1216 :
1217 57208 : if (len != 2)
1218 : return NULL;
1219 :
1220 57168 : flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1221 :
1222 285840 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
1223 : {
1224 114336 : conversion *sub
1225 114336 : = implicit_conversion (elttype, TREE_TYPE (e.value), e.value,
1226 : false, flags, complain);
1227 114336 : if (sub == NULL)
1228 : return NULL;
1229 :
1230 114336 : if (sub->rank > rank)
1231 44 : rank = sub->rank;
1232 114336 : if (sub->user_conv_p)
1233 0 : user = true;
1234 114336 : if (sub->bad_p)
1235 0 : bad = true;
1236 : }
1237 :
1238 57168 : c = alloc_conversion (ck_aggr);
1239 57168 : c->type = type;
1240 57168 : c->rank = rank;
1241 57168 : c->user_conv_p = user;
1242 57168 : c->bad_p = bad;
1243 57168 : c->u.expr = ctor;
1244 57168 : return c;
1245 : }
1246 :
1247 : /* Build a representation of the identity conversion from EXPR to
1248 : itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
1249 :
1250 : static conversion *
1251 1954803464 : build_identity_conv (tree type, tree expr)
1252 : {
1253 1954803464 : conversion *c;
1254 :
1255 0 : c = alloc_conversion (ck_identity);
1256 1954803464 : c->type = type;
1257 1954803464 : c->u.expr = expr;
1258 :
1259 1954803464 : return c;
1260 : }
1261 :
1262 : /* Converting from EXPR to TYPE was ambiguous in the sense that there
1263 : were multiple user-defined conversions to accomplish the job.
1264 : Build a conversion that indicates that ambiguity. */
1265 :
1266 : static conversion *
1267 1346 : build_ambiguous_conv (tree type, tree expr)
1268 : {
1269 1346 : conversion *c;
1270 :
1271 0 : c = alloc_conversion (ck_ambig);
1272 1346 : c->type = type;
1273 1346 : c->u.expr = expr;
1274 :
1275 1346 : return c;
1276 : }
1277 :
1278 : tree
1279 3508302322 : strip_top_quals (tree t)
1280 : {
1281 3508302322 : if (TREE_CODE (t) == ARRAY_TYPE)
1282 : return t;
1283 3501478674 : return cp_build_qualified_type (t, 0);
1284 : }
1285 :
1286 : /* Returns the standard conversion path (see [conv]) from type FROM to type
1287 : TO, if any. For proper handling of null pointer constants, you must
1288 : also pass the expression EXPR to convert from. If C_CAST_P is true,
1289 : this conversion is coming from a C-style cast. */
1290 :
1291 : static conversion *
1292 1566972798 : standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
1293 : int flags, tsubst_flags_t complain)
1294 : {
1295 1566972798 : enum tree_code fcode, tcode;
1296 1566972798 : conversion *conv;
1297 1566972798 : bool fromref = false;
1298 1566972798 : tree qualified_to;
1299 :
1300 1566972798 : to = non_reference (to);
1301 1566972798 : if (TYPE_REF_P (from))
1302 : {
1303 80684 : fromref = true;
1304 80684 : from = TREE_TYPE (from);
1305 : }
1306 1566972798 : qualified_to = to;
1307 1566972798 : to = strip_top_quals (to);
1308 1566972798 : from = strip_top_quals (from);
1309 :
1310 1566972798 : if (expr && type_unknown_p (expr))
1311 : {
1312 348953 : if (TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
1313 : {
1314 38170 : tsubst_flags_t tflags = tf_conv;
1315 38170 : expr = instantiate_type (to, expr, tflags);
1316 38170 : if (expr == error_mark_node)
1317 : return NULL;
1318 23319 : from = TREE_TYPE (expr);
1319 : }
1320 310783 : else if (TREE_CODE (to) == BOOLEAN_TYPE)
1321 : {
1322 : /* Necessary for eg, TEMPLATE_ID_EXPRs (c++/50961). */
1323 7206 : expr = resolve_nondeduced_context (expr, complain);
1324 7206 : from = TREE_TYPE (expr);
1325 : }
1326 : }
1327 :
1328 1566957947 : fcode = TREE_CODE (from);
1329 1566957947 : tcode = TREE_CODE (to);
1330 :
1331 1566957947 : conv = build_identity_conv (from, expr);
1332 1566957947 : if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
1333 : {
1334 6846939 : from = type_decays_to (from);
1335 6846939 : fcode = TREE_CODE (from);
1336 : /* Tell convert_like that we're using the address. */
1337 6846939 : conv->rvaluedness_matches_p = true;
1338 6846939 : conv = build_conv (ck_lvalue, from, conv);
1339 : }
1340 : /* Wrapping a ck_rvalue around a class prvalue (as a result of using
1341 : obvalue_p) seems odd, since it's already a prvalue, but that's how we
1342 : express the copy constructor call required by copy-initialization. */
1343 1560111008 : else if (fromref || (expr && obvalue_p (expr)))
1344 : {
1345 363462618 : if (expr)
1346 : {
1347 363381946 : tree bitfield_type;
1348 363381946 : bitfield_type = is_bitfield_expr_with_lowered_type (expr);
1349 363381946 : if (bitfield_type)
1350 : {
1351 2915799 : from = strip_top_quals (bitfield_type);
1352 2915799 : fcode = TREE_CODE (from);
1353 : }
1354 : }
1355 363462618 : conv = build_conv (ck_rvalue, from, conv);
1356 : /* If we're performing copy-initialization, remember to skip
1357 : explicit constructors. */
1358 363462618 : if (flags & LOOKUP_ONLYCONVERTING)
1359 311389750 : conv->copy_init_p = true;
1360 : }
1361 :
1362 : /* Allow conversion between `__complex__' data types. */
1363 1566957947 : if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
1364 : {
1365 : /* The standard conversion sequence to convert FROM to TO is
1366 : the standard conversion sequence to perform componentwise
1367 : conversion. */
1368 2384341 : conversion *part_conv = standard_conversion
1369 2384341 : (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags,
1370 : complain);
1371 :
1372 2384341 : if (!part_conv)
1373 : conv = NULL;
1374 2384341 : else if (part_conv->kind == ck_identity)
1375 : /* Leave conv alone. */;
1376 : else
1377 : {
1378 278682 : conv = build_conv (part_conv->kind, to, conv);
1379 278682 : conv->rank = part_conv->rank;
1380 : }
1381 :
1382 : return conv;
1383 : }
1384 :
1385 1564573606 : if (same_type_p (from, to))
1386 : {
1387 960094812 : if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue)
1388 17782952 : conv->type = qualified_to;
1389 942311860 : else if (from != to)
1390 : /* Use TO in order to not lose TO in diagnostics. */
1391 82065961 : conv->type = to;
1392 : return conv;
1393 : }
1394 :
1395 : /* [conv.ptr]
1396 : A null pointer constant can be converted to a pointer type; ... A
1397 : null pointer constant of integral type can be converted to an
1398 : rvalue of type std::nullptr_t. */
1399 389202218 : if ((tcode == POINTER_TYPE || TYPE_PTRMEM_P (to)
1400 389110601 : || NULLPTR_TYPE_P (to))
1401 610413760 : && ((expr && null_ptr_cst_p (expr))
1402 215485399 : || NULLPTR_TYPE_P (from)))
1403 5726147 : conv = build_conv (ck_std, to, conv);
1404 598752647 : else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
1405 598125174 : || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
1406 : {
1407 : /* For backwards brain damage compatibility, allow interconversion of
1408 : pointers and integers with a pedwarn. */
1409 2147729 : conv = build_conv (ck_std, to, conv);
1410 2147729 : conv->bad_p = true;
1411 : }
1412 596604918 : else if (UNSCOPED_ENUM_P (to) && fcode == INTEGER_TYPE)
1413 : {
1414 : /* For backwards brain damage compatibility, allow interconversion of
1415 : enums and integers with a pedwarn. */
1416 364544 : conv = build_conv (ck_std, to, conv);
1417 364544 : conv->bad_p = true;
1418 : }
1419 596240374 : else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
1420 393045585 : || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from)))
1421 : {
1422 2765 : tree to_pointee;
1423 2765 : tree from_pointee;
1424 :
1425 2765 : if (tcode == POINTER_TYPE)
1426 : {
1427 203194789 : to_pointee = TREE_TYPE (to);
1428 203194789 : from_pointee = TREE_TYPE (from);
1429 :
1430 : /* Since this is the target of a pointer, it can't have function
1431 : qualifiers, so any TYPE_QUALS must be for attributes const or
1432 : noreturn. Strip them. */
1433 203194789 : if (TREE_CODE (to_pointee) == FUNCTION_TYPE
1434 203194789 : && TYPE_QUALS (to_pointee))
1435 9 : to_pointee = build_qualified_type (to_pointee, TYPE_UNQUALIFIED);
1436 203194789 : if (TREE_CODE (from_pointee) == FUNCTION_TYPE
1437 203194789 : && TYPE_QUALS (from_pointee))
1438 54 : from_pointee = build_qualified_type (from_pointee, TYPE_UNQUALIFIED);
1439 : }
1440 : else
1441 : {
1442 2765 : to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
1443 2765 : from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
1444 : }
1445 :
1446 203197554 : if (tcode == POINTER_TYPE
1447 203197554 : && same_type_ignoring_top_level_qualifiers_p (from_pointee,
1448 : to_pointee))
1449 : ;
1450 143232166 : else if (VOID_TYPE_P (to_pointee)
1451 4613080 : && !TYPE_PTRDATAMEM_P (from)
1452 4613080 : && TREE_CODE (from_pointee) != FUNCTION_TYPE)
1453 : {
1454 4612671 : tree nfrom = TREE_TYPE (from);
1455 : /* Don't try to apply restrict to void. */
1456 4612671 : int quals = cp_type_quals (nfrom) & ~TYPE_QUAL_RESTRICT;
1457 4612671 : from_pointee = cp_build_qualified_type (void_type_node, quals);
1458 4612671 : from = build_pointer_type (from_pointee);
1459 4612671 : conv = build_conv (ck_ptr, from, conv);
1460 4612671 : }
1461 138619495 : else if (TYPE_PTRDATAMEM_P (from))
1462 : {
1463 2765 : tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
1464 2765 : tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
1465 :
1466 2765 : if (same_type_p (fbase, tbase))
1467 : /* No base conversion needed. */;
1468 619 : else if (DERIVED_FROM_P (fbase, tbase)
1469 1026 : && (same_type_ignoring_top_level_qualifiers_p
1470 407 : (from_pointee, to_pointee)))
1471 : {
1472 383 : from = build_ptrmem_type (tbase, from_pointee);
1473 383 : conv = build_conv (ck_pmem, from, conv);
1474 : }
1475 : else
1476 : return NULL;
1477 : }
1478 67430411 : else if (CLASS_TYPE_P (from_pointee)
1479 67428038 : && CLASS_TYPE_P (to_pointee)
1480 : /* [conv.ptr]
1481 :
1482 : An rvalue of type "pointer to cv D," where D is a
1483 : class type, can be converted to an rvalue of type
1484 : "pointer to cv B," where B is a base class (clause
1485 : _class.derived_) of D. If B is an inaccessible
1486 : (clause _class.access_) or ambiguous
1487 : (_class.member.lookup_) base class of D, a program
1488 : that necessitates this conversion is ill-formed.
1489 : Therefore, we use DERIVED_FROM_P, and do not check
1490 : access or uniqueness. */
1491 205150989 : && DERIVED_FROM_P (to_pointee, from_pointee))
1492 : {
1493 6227618 : from_pointee
1494 6227618 : = cp_build_qualified_type (to_pointee,
1495 : cp_type_quals (from_pointee));
1496 6227618 : from = build_pointer_type (from_pointee);
1497 6227618 : conv = build_conv (ck_ptr, from, conv);
1498 6227618 : conv->base_p = true;
1499 : }
1500 :
1501 203197318 : if (same_type_p (from, to))
1502 : /* OK */;
1503 195030313 : else if (c_cast_p && comp_ptr_ttypes_const (to, from, bounds_either))
1504 : /* In a C-style cast, we ignore CV-qualification because we
1505 : are allowed to perform a static_cast followed by a
1506 : const_cast. */
1507 624 : conv = build_conv (ck_qual, to, conv);
1508 195029689 : else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
1509 55012799 : conv = build_conv (ck_qual, to, conv);
1510 140016890 : else if (expr && string_conv_p (to, expr, 0))
1511 : /* converting from string constant to char *. */
1512 204 : conv = build_conv (ck_qual, to, conv);
1513 140016686 : else if (fnptr_conv_p (to, from))
1514 244780 : conv = build_conv (ck_fnptr, to, conv);
1515 : /* Allow conversions among compatible ObjC pointer types (base
1516 : conversions have been already handled above). */
1517 139771906 : else if (c_dialect_objc ()
1518 139771906 : && objc_compare_types (to, from, -4, NULL_TREE))
1519 0 : conv = build_conv (ck_ptr, to, conv);
1520 139771906 : else if (ptr_reasonably_similar (to_pointee, from_pointee))
1521 : {
1522 8624777 : conv = build_conv (ck_ptr, to, conv);
1523 8624777 : conv->bad_p = true;
1524 : }
1525 : else
1526 : return NULL;
1527 :
1528 278060535 : from = to;
1529 : }
1530 393042820 : else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
1531 : {
1532 86469 : tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
1533 86469 : tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
1534 86469 : tree fbase = class_of_this_parm (fromfn);
1535 86469 : tree tbase = class_of_this_parm (tofn);
1536 :
1537 : /* If FBASE and TBASE are equivalent but incomplete, DERIVED_FROM_P
1538 : yields false. But a pointer to member of incomplete class is OK. */
1539 86469 : if (!same_type_p (fbase, tbase) && !DERIVED_FROM_P (fbase, tbase))
1540 : return NULL;
1541 :
1542 86308 : tree fstat = static_fn_type (fromfn);
1543 86308 : tree tstat = static_fn_type (tofn);
1544 86308 : if (same_type_p (tstat, fstat)
1545 86308 : || fnptr_conv_p (tstat, fstat))
1546 : /* OK */;
1547 : else
1548 : return NULL;
1549 :
1550 86040 : if (!same_type_p (fbase, tbase))
1551 : {
1552 85927 : tree first = to;
1553 85927 : if (!same_type_p (tstat, fstat))
1554 : {
1555 0 : first = build_memfn_type (fstat,
1556 : tbase,
1557 : cp_type_quals (tbase),
1558 : type_memfn_rqual (tofn));
1559 0 : first = build_ptrmemfunc_type (build_pointer_type (first));
1560 : }
1561 85927 : conv = build_conv (ck_pmem, first, conv);
1562 85927 : conv->base_p = true;
1563 : }
1564 86040 : if (fnptr_conv_p (tstat, fstat))
1565 113 : conv = build_conv (ck_fnptr, to, conv);
1566 : }
1567 392956351 : else if (tcode == BOOLEAN_TYPE)
1568 : {
1569 : /* [conv.bool]
1570 :
1571 : A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
1572 : to member type can be converted to a prvalue of type bool. ...
1573 : For direct-initialization (8.5 [dcl.init]), a prvalue of type
1574 : std::nullptr_t can be converted to a prvalue of type bool; */
1575 12432091 : if (ARITHMETIC_TYPE_P (from)
1576 2192953 : || UNSCOPED_ENUM_P (from)
1577 4244421 : || fcode == POINTER_TYPE
1578 2982682 : || TYPE_PTRMEM_P (from)
1579 2982459 : || NULLPTR_TYPE_P (from))
1580 : {
1581 9449714 : conv = build_conv (ck_std, to, conv);
1582 9449714 : if (fcode == POINTER_TYPE
1583 8187975 : || TYPE_PTRDATAMEM_P (from)
1584 8187829 : || (TYPE_PTRMEMFUNC_P (from)
1585 77 : && conv->rank < cr_pbool)
1586 17637466 : || NULLPTR_TYPE_P (from))
1587 1262044 : conv->rank = cr_pbool;
1588 9449714 : if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING))
1589 40 : conv->bad_p = true;
1590 9449714 : if (flags & LOOKUP_NO_NARROWING)
1591 31579 : conv->check_narrowing = true;
1592 : return conv;
1593 : }
1594 :
1595 : return NULL;
1596 : }
1597 : /* We don't check for ENUMERAL_TYPE here because there are no standard
1598 : conversions to enum type. */
1599 : /* As an extension, allow conversion to complex type. */
1600 380524260 : else if (ARITHMETIC_TYPE_P (to))
1601 : {
1602 31860053 : if (! (INTEGRAL_CODE_P (fcode)
1603 25089197 : || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL)))
1604 230563961 : || SCOPED_ENUM_P (from))
1605 : return NULL;
1606 :
1607 : /* If we're parsing an enum with no fixed underlying type, we're
1608 : dealing with an incomplete type, which renders the conversion
1609 : ill-formed. */
1610 197218381 : if (!COMPLETE_TYPE_P (from))
1611 : return NULL;
1612 :
1613 197218375 : conv = build_conv (ck_std, to, conv);
1614 :
1615 197218375 : tree underlying_type = NULL_TREE;
1616 197218375 : if (TREE_CODE (from) == ENUMERAL_TYPE
1617 197218375 : && ENUM_FIXED_UNDERLYING_TYPE_P (from))
1618 1714040 : underlying_type = ENUM_UNDERLYING_TYPE (from);
1619 :
1620 : /* Give this a better rank if it's a promotion.
1621 :
1622 : To handle CWG 1601, also bump the rank if we are converting
1623 : an enumeration with a fixed underlying type to the underlying
1624 : type. */
1625 197218375 : if ((same_type_p (to, type_promotes_to (from))
1626 185206763 : || (underlying_type && same_type_p (to, underlying_type)))
1627 197218394 : && next_conversion (conv)->rank <= cr_promotion)
1628 12011631 : conv->rank = cr_promotion;
1629 :
1630 : /* A prvalue of floating-point type can be converted to a prvalue of
1631 : another floating-point type with a greater or equal conversion
1632 : rank ([conv.rank]). A prvalue of standard floating-point type can
1633 : be converted to a prvalue of another standard floating-point type.
1634 : For backwards compatibility with handling __float128 and other
1635 : non-standard floating point types, allow all implicit floating
1636 : point conversions if neither type is extended floating-point
1637 : type and if at least one of them is, fail if they have unordered
1638 : conversion rank or from has higher conversion rank. */
1639 197218375 : if (fcode == REAL_TYPE
1640 197218375 : && tcode == REAL_TYPE
1641 17084680 : && (extended_float_type_p (from)
1642 16267025 : || extended_float_type_p (to))
1643 201143190 : && cp_compare_floating_point_conversion_ranks (from, to) >= 2)
1644 1800440 : conv->bad_p = true;
1645 : }
1646 175049496 : else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
1647 175049496 : && vector_types_convertible_p (from, to, false))
1648 4905 : return build_conv (ck_std, to, conv);
1649 175044591 : else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
1650 70008997 : && is_properly_derived_from (from, to))
1651 : {
1652 467511 : if (conv->kind == ck_rvalue)
1653 467460 : conv = next_conversion (conv);
1654 467511 : conv = build_conv (ck_base, to, conv);
1655 : /* The derived-to-base conversion indicates the initialization
1656 : of a parameter with base type from an object of a derived
1657 : type. A temporary object is created to hold the result of
1658 : the conversion unless we're binding directly to a reference. */
1659 467511 : conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
1660 : /* If we're performing copy-initialization, remember to skip
1661 : explicit constructors. */
1662 467511 : if (flags & LOOKUP_ONLYCONVERTING)
1663 467444 : conv->copy_init_p = true;
1664 : }
1665 : else
1666 : return NULL;
1667 :
1668 278060535 : if (flags & LOOKUP_NO_NARROWING)
1669 95603660 : conv->check_narrowing = true;
1670 :
1671 : return conv;
1672 : }
1673 :
1674 : /* Returns nonzero if T1 is reference-related to T2.
1675 :
1676 : This is considered when a reference to T1 is initialized by a T2. */
1677 :
1678 : bool
1679 280541205 : reference_related_p (tree t1, tree t2)
1680 : {
1681 280541205 : if (t1 == error_mark_node || t2 == error_mark_node)
1682 : return false;
1683 :
1684 280541201 : t1 = TYPE_MAIN_VARIANT (t1);
1685 280541201 : t2 = TYPE_MAIN_VARIANT (t2);
1686 :
1687 : /* [dcl.init.ref]
1688 :
1689 : Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
1690 : to "cv2 T2" if T1 is similar to T2, or T1 is a base class of T2. */
1691 280541201 : return (similar_type_p (t1, t2)
1692 280541201 : || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1693 68408057 : && DERIVED_FROM_P (t1, t2)));
1694 : }
1695 :
1696 : /* Returns nonzero if T1 is reference-compatible with T2. */
1697 :
1698 : bool
1699 248845968 : reference_compatible_p (tree t1, tree t2)
1700 : {
1701 : /* [dcl.init.ref]
1702 :
1703 : "cv1 T1" is reference compatible with "cv2 T2" if
1704 : a prvalue of type "pointer to cv2 T2" can be converted to the type
1705 : "pointer to cv1 T1" via a standard conversion sequence. */
1706 248845968 : tree ptype1 = build_pointer_type (t1);
1707 248845968 : tree ptype2 = build_pointer_type (t2);
1708 248845968 : conversion *conv = standard_conversion (ptype1, ptype2, NULL_TREE,
1709 : /*c_cast_p=*/false, 0, tf_none);
1710 248845968 : if (!conv || conv->bad_p)
1711 137599252 : return false;
1712 : return true;
1713 : }
1714 :
1715 : /* Return true if converting FROM to TO would involve a qualification
1716 : conversion. */
1717 :
1718 : static bool
1719 122065607 : involves_qualification_conversion_p (tree to, tree from)
1720 : {
1721 : /* If we're not converting a pointer to another one, we won't get
1722 : a qualification conversion. */
1723 122065607 : if (!((TYPE_PTR_P (to) && TYPE_PTR_P (from))
1724 3095 : || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from))))
1725 : return false;
1726 :
1727 4799323 : conversion *conv = standard_conversion (to, from, NULL_TREE,
1728 : /*c_cast_p=*/false, 0, tf_none);
1729 14380430 : for (conversion *t = conv; t; t = next_conversion (t))
1730 4799339 : if (t->kind == ck_qual)
1731 : return true;
1732 :
1733 : return false;
1734 : }
1735 :
1736 : /* Return true if HANDLER is a match for exception object with EXCEPT_TYPE as
1737 : per [except.handle]/3. */
1738 :
1739 : bool
1740 10604 : handler_match_for_exception_type (tree handler_type, tree except_type)
1741 : {
1742 10604 : if (handler_type && TREE_CODE (handler_type) == HANDLER)
1743 4229 : handler_type = TREE_TYPE (handler_type);
1744 4229 : if (handler_type == NULL_TREE)
1745 : return true; /* ... */
1746 10522 : if (same_type_ignoring_top_level_qualifiers_p (handler_type, except_type))
1747 : return true;
1748 6787 : if (CLASS_TYPE_P (except_type) && CLASS_TYPE_P (handler_type))
1749 3774 : return publicly_uniquely_derived_p (handler_type, except_type);
1750 3013 : if (TYPE_PTR_P (handler_type) || TYPE_PTRMEM_P (handler_type))
1751 : {
1752 813 : if (TREE_CODE (except_type) == NULLPTR_TYPE)
1753 : return true;
1754 748 : if ((TYPE_PTR_P (handler_type) && TYPE_PTR_P (except_type))
1755 80 : || (TYPE_PTRDATAMEM_P (handler_type)
1756 10 : && TYPE_PTRDATAMEM_P (except_type))
1757 844 : || (TYPE_PTRMEMFUNC_P (handler_type)
1758 16 : && TYPE_PTRMEMFUNC_P (except_type)))
1759 : {
1760 720 : conversion *conv
1761 720 : = standard_conversion (handler_type, except_type, NULL_TREE,
1762 : /*c_cast_p=*/false, 0, tf_none);
1763 720 : if (conv && !conv->bad_p)
1764 : {
1765 464 : for (conversion *t = conv; t; t = next_conversion (t))
1766 418 : switch (t->kind)
1767 : {
1768 336 : case ck_ptr:
1769 : /* ...not involving conversions to pointers to private or
1770 : protected or ambiguous classes, */
1771 336 : if (CLASS_TYPE_P (TREE_TYPE (handler_type)))
1772 320 : return (publicly_uniquely_derived_p
1773 320 : (TREE_TYPE (handler_type),
1774 640 : TREE_TYPE (except_type)));
1775 98 : gcc_fallthrough ();
1776 98 : case ck_fnptr:
1777 98 : case ck_qual:
1778 98 : case ck_identity:
1779 98 : break;
1780 : default:
1781 : return false;
1782 : }
1783 : return true;
1784 : }
1785 : }
1786 : }
1787 : return false;
1788 : }
1789 :
1790 : /* A reference of the indicated TYPE is being bound directly to the
1791 : expression represented by the implicit conversion sequence CONV.
1792 : Return a conversion sequence for this binding. */
1793 :
1794 : static conversion *
1795 125833853 : direct_reference_binding (tree type, conversion *conv)
1796 : {
1797 125833853 : tree t;
1798 :
1799 125833853 : gcc_assert (TYPE_REF_P (type));
1800 125833853 : gcc_assert (!TYPE_REF_P (conv->type));
1801 :
1802 125833853 : t = TREE_TYPE (type);
1803 :
1804 125833853 : if (conv->kind == ck_identity)
1805 : /* Mark the identity conv as to not decay to rvalue. */
1806 125833853 : conv->rvaluedness_matches_p = true;
1807 :
1808 : /* [over.ics.rank]
1809 :
1810 : When a parameter of reference type binds directly
1811 : (_dcl.init.ref_) to an argument expression, the implicit
1812 : conversion sequence is the identity conversion, unless the
1813 : argument expression has a type that is a derived class of the
1814 : parameter type, in which case the implicit conversion sequence is
1815 : a derived-to-base Conversion.
1816 :
1817 : If the parameter binds directly to the result of applying a
1818 : conversion function to the argument expression, the implicit
1819 : conversion sequence is a user-defined conversion sequence
1820 : (_over.ics.user_), with the second standard conversion sequence
1821 : either an identity conversion or, if the conversion function
1822 : returns an entity of a type that is a derived class of the
1823 : parameter type, a derived-to-base conversion. */
1824 125833853 : if (is_properly_derived_from (conv->type, t))
1825 : {
1826 : /* Represent the derived-to-base conversion. */
1827 3768246 : conv = build_conv (ck_base, t, conv);
1828 : /* We will actually be binding to the base-class subobject in
1829 : the derived class, so we mark this conversion appropriately.
1830 : That way, convert_like knows not to generate a temporary. */
1831 3768246 : conv->need_temporary_p = false;
1832 : }
1833 122065607 : else if (involves_qualification_conversion_p (t, conv->type))
1834 : /* Represent the qualification conversion. After DR 2352
1835 : #1 and #2 were indistinguishable conversion sequences:
1836 :
1837 : void f(int*); // #1
1838 : void f(const int* const &); // #2
1839 : void g(int* p) { f(p); }
1840 :
1841 : because the types "int *" and "const int *const" are
1842 : reference-related and we were binding both directly and they
1843 : had the same rank. To break it up, we add a ck_qual under the
1844 : ck_ref_bind so that conversion sequence ranking chooses #1.
1845 :
1846 : We strip_top_quals here which is also what standard_conversion
1847 : does. Failure to do so would confuse comp_cv_qual_signature
1848 : into thinking that in
1849 :
1850 : void f(const int * const &); // #1
1851 : void f(const int *); // #2
1852 : int *x;
1853 : f(x);
1854 :
1855 : #2 is a better match than #1 even though they're ambiguous (97296). */
1856 17555 : conv = build_conv (ck_qual, strip_top_quals (t), conv);
1857 :
1858 125833853 : return build_conv (ck_ref_bind, type, conv);
1859 : }
1860 :
1861 : /* Returns the conversion path from type FROM to reference type TO for
1862 : purposes of reference binding. For lvalue binding, either pass a
1863 : reference type to FROM or an lvalue expression to EXPR. If the
1864 : reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1865 : the conversion returned. If C_CAST_P is true, this
1866 : conversion is coming from a C-style cast. */
1867 :
1868 : static conversion *
1869 247987342 : reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags,
1870 : tsubst_flags_t complain)
1871 : {
1872 247987342 : conversion *conv = NULL;
1873 247987342 : conversion *bad_direct_conv = nullptr;
1874 247987342 : tree to = TREE_TYPE (rto);
1875 247987342 : tree from = rfrom;
1876 247987342 : tree tfrom;
1877 247987342 : bool related_p;
1878 247987342 : bool compatible_p;
1879 247987342 : cp_lvalue_kind gl_kind;
1880 247987342 : bool is_lvalue;
1881 :
1882 247987342 : if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1883 : {
1884 52 : expr = instantiate_type (to, expr, tf_none);
1885 52 : if (expr == error_mark_node)
1886 : return NULL;
1887 52 : from = TREE_TYPE (expr);
1888 : }
1889 :
1890 247987342 : bool copy_list_init = false;
1891 247987342 : bool single_list_conv = false;
1892 247987342 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1893 : {
1894 104581 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
1895 : /* DR 1288: Otherwise, if the initializer list has a single element
1896 : of type E and ... [T's] referenced type is reference-related to E,
1897 : the object or reference is initialized from that element...
1898 :
1899 : ??? With P0388R4, we should bind 't' directly to U{}:
1900 : using U = A[2];
1901 : A (&&t)[] = {U{}};
1902 : because A[] and A[2] are reference-related. But we don't do it
1903 : because grok_reference_init has deduced the array size (to 1), and
1904 : A[1] and A[2] aren't reference-related. */
1905 104581 : if (CONSTRUCTOR_NELTS (expr) == 1
1906 59722 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
1907 : {
1908 4342 : tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1909 4342 : if (error_operand_p (elt))
1910 : return NULL;
1911 4337 : tree etype = TREE_TYPE (elt);
1912 4337 : if (reference_related_p (to, etype))
1913 : {
1914 739 : expr = elt;
1915 739 : from = etype;
1916 739 : goto skip;
1917 : }
1918 3598 : else if (CLASS_TYPE_P (etype) && TYPE_HAS_CONVERSION (etype))
1919 : /* CWG1996: jason's proposed drafting adds "or initializing T from E
1920 : would bind directly". We check that in the direct binding with
1921 : conversion code below. */
1922 : single_list_conv = true;
1923 : }
1924 : /* Otherwise, if T is a reference type, a prvalue temporary of the type
1925 : referenced by T is copy-list-initialized, and the reference is bound
1926 : to that temporary. */
1927 : copy_list_init = true;
1928 247987337 : skip:;
1929 : }
1930 :
1931 247987337 : if (TYPE_REF_P (from))
1932 : {
1933 63996 : from = TREE_TYPE (from);
1934 63996 : if (!TYPE_REF_IS_RVALUE (rfrom)
1935 63996 : || TREE_CODE (from) == FUNCTION_TYPE)
1936 : gl_kind = clk_ordinary;
1937 : else
1938 247987337 : gl_kind = clk_rvalueref;
1939 : }
1940 247923341 : else if (expr)
1941 244442863 : gl_kind = lvalue_kind (expr);
1942 3194216 : else if (CLASS_TYPE_P (from)
1943 3480478 : || TREE_CODE (from) == ARRAY_TYPE)
1944 : gl_kind = clk_class;
1945 : else
1946 : gl_kind = clk_none;
1947 :
1948 : /* Don't allow a class prvalue when LOOKUP_NO_TEMP_BIND. */
1949 247987337 : if ((flags & LOOKUP_NO_TEMP_BIND)
1950 3594658 : && (gl_kind & clk_class))
1951 : gl_kind = clk_none;
1952 :
1953 : /* Same mask as real_lvalue_p. */
1954 245235055 : is_lvalue = gl_kind && !(gl_kind & (clk_rvalueref|clk_class));
1955 :
1956 212126826 : tfrom = from;
1957 212126826 : if ((gl_kind & clk_bitfield) != 0)
1958 2035069 : tfrom = unlowered_expr_type (expr);
1959 :
1960 : /* Figure out whether or not the types are reference-related and
1961 : reference compatible. We have to do this after stripping
1962 : references from FROM. */
1963 247987337 : related_p = reference_related_p (to, tfrom);
1964 : /* If this is a C cast, first convert to an appropriately qualified
1965 : type, so that we can later do a const_cast to the desired type. */
1966 247987337 : if (related_p && c_cast_p
1967 247987337 : && !at_least_as_qualified_p (to, tfrom))
1968 196 : to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1969 247987337 : compatible_p = reference_compatible_p (to, tfrom);
1970 :
1971 : /* Directly bind reference when target expression's type is compatible with
1972 : the reference and expression is an lvalue. In DR391, the wording in
1973 : [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1974 : const and rvalue references to rvalues of compatible class type.
1975 : We should also do direct bindings for non-class xvalues. */
1976 247987337 : if ((related_p || compatible_p) && gl_kind)
1977 : {
1978 : /* [dcl.init.ref]
1979 :
1980 : If the initializer expression
1981 :
1982 : -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1983 : is reference-compatible with "cv2 T2,"
1984 :
1985 : the reference is bound directly to the initializer expression
1986 : lvalue.
1987 :
1988 : [...]
1989 : If the initializer expression is an rvalue, with T2 a class type,
1990 : and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1991 : is bound to the object represented by the rvalue or to a sub-object
1992 : within that object. */
1993 :
1994 112518000 : conv = build_identity_conv (tfrom, expr);
1995 112518000 : conv = direct_reference_binding (rto, conv);
1996 :
1997 112518000 : if (TYPE_REF_P (rfrom))
1998 : /* Handle rvalue reference to function properly. */
1999 16419 : conv->rvaluedness_matches_p
2000 16419 : = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom));
2001 : else
2002 112501581 : conv->rvaluedness_matches_p
2003 112501581 : = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
2004 :
2005 112518000 : if ((gl_kind & clk_bitfield) != 0
2006 112518000 : || ((gl_kind & clk_packed) != 0 && !TYPE_PACKED (to)))
2007 : /* For the purposes of overload resolution, we ignore the fact
2008 : this expression is a bitfield or packed field. (In particular,
2009 : [over.ics.ref] says specifically that a function with a
2010 : non-const reference parameter is viable even if the
2011 : argument is a bitfield.)
2012 :
2013 : However, when we actually call the function we must create
2014 : a temporary to which to bind the reference. If the
2015 : reference is volatile, or isn't const, then we cannot make
2016 : a temporary, so we just issue an error when the conversion
2017 : actually occurs. */
2018 110 : conv->need_temporary_p = true;
2019 :
2020 : /* Don't allow binding of lvalues (other than function lvalues) to
2021 : rvalue references. */
2022 82607703 : if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
2023 122692952 : && TREE_CODE (to) != FUNCTION_TYPE)
2024 10172962 : conv->bad_p = true;
2025 :
2026 : /* Nor the reverse. */
2027 29910297 : if (!is_lvalue && !TYPE_REF_IS_RVALUE (rto)
2028 : /* Unless it's really a C++20 lvalue being treated as an xvalue.
2029 : But in C++23, such an expression is just an xvalue, not a special
2030 : lvalue, so the binding is once again ill-formed. */
2031 18523163 : && !(cxx_dialect <= cxx20
2032 14745821 : && (gl_kind & clk_implicit_rval))
2033 17423182 : && (!CP_TYPE_CONST_NON_VOLATILE_P (to)
2034 17305812 : || (flags & LOOKUP_NO_RVAL_BIND))
2035 112635370 : && TREE_CODE (to) != FUNCTION_TYPE)
2036 117370 : conv->bad_p = true;
2037 :
2038 112518000 : if (!compatible_p)
2039 6645353 : conv->bad_p = true;
2040 :
2041 : return conv;
2042 : }
2043 : /* [class.conv.fct] A conversion function is never used to convert a
2044 : (possibly cv-qualified) object to the (possibly cv-qualified) same
2045 : object type (or a reference to it), to a (possibly cv-qualified) base
2046 : class of that type (or a reference to it).... */
2047 4517799 : else if (!related_p
2048 130951538 : && !(flags & LOOKUP_NO_CONVERSION)
2049 48638999 : && (CLASS_TYPE_P (from) || single_list_conv))
2050 : {
2051 17891697 : tree rexpr = expr;
2052 17891697 : if (single_list_conv)
2053 144 : rexpr = CONSTRUCTOR_ELT (expr, 0)->value;
2054 :
2055 : /* [dcl.init.ref]
2056 :
2057 : If the initializer expression
2058 :
2059 : -- has a class type (i.e., T2 is a class type) can be
2060 : implicitly converted to an lvalue of type "cv3 T3," where
2061 : "cv1 T1" is reference-compatible with "cv3 T3". (this
2062 : conversion is selected by enumerating the applicable
2063 : conversion functions (_over.match.ref_) and choosing the
2064 : best one through overload resolution. (_over.match_).
2065 :
2066 : the reference is bound to the lvalue result of the conversion
2067 : in the second case. */
2068 17891697 : z_candidate *cand = build_user_type_conversion_1 (rto, rexpr, flags,
2069 : complain);
2070 17891697 : if (cand)
2071 : {
2072 12678 : if (!cand->second_conv->bad_p)
2073 : return cand->second_conv;
2074 :
2075 : /* Direct reference binding wasn't successful and yielded a bad
2076 : conversion. Proceed with trying to go through a temporary
2077 : instead, and if that also fails then we'll return this bad
2078 : conversion rather than no conversion for sake of better
2079 : diagnostics. */
2080 : bad_direct_conv = cand->second_conv;
2081 : }
2082 : }
2083 :
2084 : /* From this point on, we conceptually need temporaries, even if we
2085 : elide them. Only the cases above are "direct bindings". */
2086 135457196 : if (flags & LOOKUP_NO_TEMP_BIND)
2087 : return bad_direct_conv ? bad_direct_conv : nullptr;
2088 :
2089 : /* [over.ics.rank]
2090 :
2091 : When a parameter of reference type is not bound directly to an
2092 : argument expression, the conversion sequence is the one required
2093 : to convert the argument expression to the underlying type of the
2094 : reference according to _over.best.ics_. Conceptually, this
2095 : conversion sequence corresponds to copy-initializing a temporary
2096 : of the underlying type with the argument expression. Any
2097 : difference in top-level cv-qualification is subsumed by the
2098 : initialization itself and does not constitute a conversion. */
2099 :
2100 132097177 : bool maybe_valid_p = true;
2101 :
2102 : /* [dcl.init.ref]
2103 :
2104 : Otherwise, the reference shall be an lvalue reference to a
2105 : non-volatile const type, or the reference shall be an rvalue
2106 : reference. */
2107 174907808 : if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto))
2108 : maybe_valid_p = false;
2109 :
2110 : /* [dcl.init.ref]
2111 :
2112 : Otherwise, a temporary of type "cv1 T1" is created and
2113 : initialized from the initializer expression using the rules for a
2114 : non-reference copy initialization. If T1 is reference-related to
2115 : T2, cv1 must be the same cv-qualification as, or greater
2116 : cv-qualification than, cv2; otherwise, the program is ill-formed. */
2117 132097177 : if (related_p && !at_least_as_qualified_p (to, from))
2118 : maybe_valid_p = false;
2119 :
2120 : /* We try below to treat an invalid reference binding as a bad conversion
2121 : to improve diagnostics, but doing so may cause otherwise unnecessary
2122 : instantiations that can lead to a hard error. So during the first pass
2123 : of overload resolution wherein we shortcut bad conversions, instead just
2124 : produce a special conversion indicating a second pass is necessary if
2125 : there's no strictly viable candidate. */
2126 132097177 : if (!maybe_valid_p && (flags & LOOKUP_SHORTCUT_BAD_CONVS))
2127 : {
2128 7737223 : if (bad_direct_conv)
2129 : return bad_direct_conv;
2130 :
2131 7736756 : conv = alloc_conversion (ck_deferred_bad);
2132 7736756 : conv->bad_p = true;
2133 7736756 : return conv;
2134 : }
2135 :
2136 : /* We're generating a temporary now, but don't bind any more in the
2137 : conversion (specifically, don't slice the temporary returned by a
2138 : conversion operator). */
2139 124359954 : flags |= LOOKUP_NO_TEMP_BIND;
2140 :
2141 : /* Core issue 899: When [copy-]initializing a temporary to be bound
2142 : to the first parameter of a copy constructor (12.8) called with
2143 : a single argument in the context of direct-initialization,
2144 : explicit conversion functions are also considered.
2145 :
2146 : So don't set LOOKUP_ONLYCONVERTING in that case. */
2147 124359954 : if (!(flags & LOOKUP_COPY_PARM))
2148 105038020 : flags |= LOOKUP_ONLYCONVERTING;
2149 :
2150 124359954 : if (!conv)
2151 124359954 : conv = implicit_conversion (to, from, expr, c_cast_p,
2152 : flags, complain);
2153 124359954 : if (!conv)
2154 : return bad_direct_conv ? bad_direct_conv : nullptr;
2155 :
2156 11975002 : if (conv->user_conv_p)
2157 : {
2158 8059890 : if (copy_list_init)
2159 : /* Remember this was copy-list-initialization. */
2160 73894 : conv->need_temporary_p = true;
2161 :
2162 : /* If initializing the temporary used a conversion function,
2163 : recalculate the second conversion sequence. */
2164 23237176 : for (conversion *t = conv; t; t = next_conversion (t))
2165 15634912 : if (t->kind == ck_user
2166 8029952 : && c_cast_p && !maybe_valid_p)
2167 : {
2168 12 : if (complain & tf_warning)
2169 12 : warning (OPT_Wcast_user_defined,
2170 : "casting %qT to %qT does not use %qD",
2171 12 : from, rto, t->cand->fn);
2172 : /* Don't let recalculation try to make this valid. */
2173 : break;
2174 : }
2175 15634900 : else if (t->kind == ck_user
2176 15634900 : && DECL_CONV_FN_P (t->cand->fn))
2177 : {
2178 457614 : tree ftype = TREE_TYPE (TREE_TYPE (t->cand->fn));
2179 : /* A prvalue of non-class type is cv-unqualified. */
2180 457614 : if (!TYPE_REF_P (ftype) && !CLASS_TYPE_P (ftype))
2181 4174 : ftype = cv_unqualified (ftype);
2182 457614 : int sflags = (flags|LOOKUP_NO_CONVERSION)&~LOOKUP_NO_TEMP_BIND;
2183 457614 : conversion *new_second
2184 457614 : = reference_binding (rto, ftype, NULL_TREE, c_cast_p,
2185 : sflags, complain);
2186 457614 : if (!new_second)
2187 : return bad_direct_conv ? bad_direct_conv : nullptr;
2188 457614 : conv = merge_conversion_sequences (t, new_second);
2189 457614 : gcc_assert (maybe_valid_p || conv->bad_p);
2190 : return conv;
2191 : }
2192 : }
2193 :
2194 11517388 : conv = build_conv (ck_ref_bind, rto, conv);
2195 : /* This reference binding, unlike those above, requires the
2196 : creation of a temporary. */
2197 11517388 : conv->need_temporary_p = true;
2198 11517388 : conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
2199 11517388 : conv->bad_p |= !maybe_valid_p;
2200 :
2201 11517388 : return conv;
2202 : }
2203 :
2204 : /* Returns the implicit conversion sequence (see [over.ics]) from type
2205 : FROM to type TO. The optional expression EXPR may affect the
2206 : conversion. FLAGS are the usual overloading flags. If C_CAST_P is
2207 : true, this conversion is coming from a C-style cast. */
2208 :
2209 : static conversion *
2210 1551962181 : implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
2211 : int flags, tsubst_flags_t complain)
2212 : {
2213 1551962266 : conversion *conv;
2214 :
2215 1551962266 : if (from == error_mark_node || to == error_mark_node
2216 1551960957 : || expr == error_mark_node)
2217 : return NULL;
2218 :
2219 : /* Other flags only apply to the primary function in overload
2220 : resolution, or after we've chosen one. */
2221 1551960957 : flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM
2222 : |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_NO_NARROWING
2223 : |LOOKUP_PROTECT|LOOKUP_NO_NON_INTEGRAL|LOOKUP_SHORTCUT_BAD_CONVS);
2224 :
2225 : /* FIXME: actually we don't want warnings either, but we can't just
2226 : have 'complain &= ~(tf_warning|tf_error)' because it would cause
2227 : the regression of, eg, g++.old-deja/g++.benjamin/16077.C.
2228 : We really ought not to issue that warning until we've committed
2229 : to that conversion. */
2230 1551960957 : complain &= ~tf_error;
2231 :
2232 : /* Call reshape_init early to remove redundant braces. */
2233 1551960957 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr) && CLASS_TYPE_P (to))
2234 : {
2235 2414350 : to = complete_type (to);
2236 2414350 : if (!COMPLETE_TYPE_P (to))
2237 : return nullptr;
2238 2414326 : if (!CLASSTYPE_NON_AGGREGATE (to))
2239 : {
2240 1371056 : expr = reshape_init (to, expr, complain);
2241 1371056 : if (expr == error_mark_node)
2242 : return nullptr;
2243 1370813 : from = TREE_TYPE (expr);
2244 : }
2245 : }
2246 :
2247 : /* An argument should have gone through convert_from_reference. */
2248 1551960690 : gcc_checking_assert (!expr || !TYPE_REF_P (from));
2249 :
2250 1551960690 : if (TYPE_REF_P (to))
2251 241018244 : conv = reference_binding (to, from, expr, c_cast_p, flags, complain);
2252 : else
2253 1310942446 : conv = standard_conversion (to, from, expr, c_cast_p, flags, complain);
2254 :
2255 1551960690 : if (conv)
2256 : return conv;
2257 :
2258 301835184 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
2259 : {
2260 4938245 : if (is_std_init_list (to) && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2261 97554 : return build_list_conv (to, expr, flags, complain);
2262 :
2263 : /* As an extension, allow list-initialization of _Complex. */
2264 4743128 : if (TREE_CODE (to) == COMPLEX_TYPE
2265 4800345 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2266 : {
2267 57208 : conv = build_complex_conv (to, expr, flags, complain);
2268 57208 : if (conv)
2269 : return conv;
2270 : }
2271 :
2272 : /* Allow conversion from an initializer-list with one element to a
2273 : scalar type. */
2274 4685960 : if (SCALAR_TYPE_P (to))
2275 : {
2276 2343538 : int nelts = CONSTRUCTOR_NELTS (expr);
2277 363114 : tree elt;
2278 :
2279 363114 : if (nelts == 0)
2280 1980424 : elt = build_value_init (to, tf_none);
2281 363114 : else if (nelts == 1 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2282 362240 : elt = CONSTRUCTOR_ELT (expr, 0)->value;
2283 : else
2284 874 : elt = error_mark_node;
2285 :
2286 2343538 : conv = implicit_conversion (to, TREE_TYPE (elt), elt,
2287 : c_cast_p, flags, complain);
2288 2343538 : if (conv)
2289 : {
2290 2341546 : conv->check_narrowing = true;
2291 2341546 : if (BRACE_ENCLOSED_INITIALIZER_P (elt))
2292 : /* Too many levels of braces, i.e. '{{1}}'. */
2293 16 : conv->bad_p = true;
2294 : return conv;
2295 : }
2296 : }
2297 2342422 : else if (TREE_CODE (to) == ARRAY_TYPE)
2298 1889 : return build_array_conv (to, expr, flags, complain);
2299 : }
2300 :
2301 299337027 : if (expr != NULL_TREE
2302 292195534 : && (MAYBE_CLASS_TYPE_P (from)
2303 155346115 : || MAYBE_CLASS_TYPE_P (to))
2304 511520243 : && (flags & LOOKUP_NO_CONVERSION) == 0)
2305 : {
2306 79776989 : struct z_candidate *cand;
2307 :
2308 54448989 : if (CLASS_TYPE_P (to)
2309 54448939 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
2310 82093144 : && !CLASSTYPE_NON_AGGREGATE (complete_type (to)))
2311 1370754 : return build_aggr_conv (to, expr, flags, complain);
2312 :
2313 78406235 : cand = build_user_type_conversion_1 (to, expr, flags, complain);
2314 78406235 : if (cand)
2315 : {
2316 931135 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
2317 921384 : && CONSTRUCTOR_NELTS (expr) == 1
2318 59022 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr)
2319 15022608 : && !is_list_ctor (cand->fn))
2320 : {
2321 : /* "If C is not an initializer-list constructor and the
2322 : initializer list has a single element of type cv U, where U is
2323 : X or a class derived from X, the implicit conversion sequence
2324 : has Exact Match rank if U is X, or Conversion rank if U is
2325 : derived from X." */
2326 58622 : tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
2327 58622 : tree elttype = TREE_TYPE (elt);
2328 58622 : if (reference_related_p (to, elttype))
2329 : return implicit_conversion (to, elttype, elt,
2330 : c_cast_p, flags, complain);
2331 : }
2332 14963501 : conv = cand->second_conv;
2333 : }
2334 :
2335 : /* We used to try to bind a reference to a temporary here, but that
2336 : is now handled after the recursive call to this function at the end
2337 : of reference_binding. */
2338 : return conv;
2339 : }
2340 :
2341 : return NULL;
2342 : }
2343 :
2344 : /* Like implicit_conversion, but return NULL if the conversion is bad.
2345 :
2346 : This is not static so that check_non_deducible_conversion can call it within
2347 : add_template_candidate_real as part of overload resolution; it should not be
2348 : called outside of overload resolution. */
2349 :
2350 : conversion *
2351 7033822 : good_conversion (tree to, tree from, tree expr,
2352 : int flags, tsubst_flags_t complain)
2353 : {
2354 7033822 : conversion *c = implicit_conversion (to, from, expr, /*cast*/false,
2355 : flags, complain);
2356 7033822 : if (c && c->bad_p)
2357 2884780 : c = NULL;
2358 7033822 : return c;
2359 : }
2360 :
2361 : /* Add a new entry to the list of candidates. Used by the add_*_candidate
2362 : functions. ARGS will not be changed until a single candidate is
2363 : selected. */
2364 :
2365 : static struct z_candidate *
2366 1074196020 : add_candidate (struct z_candidate **candidates,
2367 : tree fn, tree first_arg, const vec<tree, va_gc> *args,
2368 : size_t num_convs, conversion **convs,
2369 : tree access_path, tree conversion_path,
2370 : int viable, struct rejection_reason *reason,
2371 : int flags)
2372 : {
2373 1074196020 : struct z_candidate *cand = (struct z_candidate *)
2374 1074196020 : conversion_obstack_alloc (sizeof (struct z_candidate));
2375 :
2376 1074196020 : cand->fn = fn;
2377 1074196020 : cand->first_arg = first_arg;
2378 1074196020 : cand->args = args;
2379 1074196020 : cand->convs = convs;
2380 1074196020 : cand->num_convs = num_convs;
2381 1074196020 : cand->access_path = access_path;
2382 1074196020 : cand->conversion_path = conversion_path;
2383 1074196020 : cand->viable = viable;
2384 1074196020 : cand->reason = reason;
2385 1074196020 : cand->next = *candidates;
2386 1074196020 : cand->flags = flags;
2387 1074196020 : *candidates = cand;
2388 :
2389 1074196020 : if (convs && cand->reversed ())
2390 : /* Swap the conversions for comparison in joust; we'll swap them back
2391 : before build_over_call. */
2392 86288644 : std::swap (convs[0], convs[1]);
2393 :
2394 1074196020 : return cand;
2395 : }
2396 :
2397 : /* FN is a function from the overload set that we outright didn't even
2398 : consider (for some reason); add it to the list as an non-viable "ignored"
2399 : candidate. */
2400 :
2401 : static z_candidate *
2402 679498800 : add_ignored_candidate (z_candidate **candidates, tree fn)
2403 : {
2404 : /* No need to dynamically allocate these. */
2405 679498800 : static const rejection_reason reason_ignored = { rr_ignored, {} };
2406 :
2407 679498800 : struct z_candidate *cand = (struct z_candidate *)
2408 679493343 : conversion_obstack_alloc (sizeof (struct z_candidate));
2409 :
2410 679498800 : cand->fn = fn;
2411 679498800 : cand->reason = const_cast<rejection_reason *> (&reason_ignored);
2412 679498800 : cand->next = *candidates;
2413 679498800 : *candidates = cand;
2414 :
2415 679498800 : return cand;
2416 : }
2417 :
2418 : /* True iff CAND is a candidate added by add_ignored_candidate. */
2419 :
2420 : static bool
2421 602408881 : ignored_candidate_p (const z_candidate *cand)
2422 : {
2423 602402774 : return cand->reason && cand->reason->code == rr_ignored;
2424 : }
2425 :
2426 : /* Return the number of remaining arguments in the parameter list
2427 : beginning with ARG. */
2428 :
2429 : int
2430 156736912 : remaining_arguments (tree arg)
2431 : {
2432 156736912 : int n;
2433 :
2434 273078739 : for (n = 0; arg != NULL_TREE && arg != void_list_node;
2435 116341827 : arg = TREE_CHAIN (arg))
2436 116341827 : n++;
2437 :
2438 156736912 : return n;
2439 : }
2440 :
2441 : /* [over.match.copy]: When initializing a temporary object (12.2) to be bound
2442 : to the first parameter of a constructor where the parameter is of type
2443 : "reference to possibly cv-qualified T" and the constructor is called with a
2444 : single argument in the context of direct-initialization of an object of type
2445 : "cv2 T", explicit conversion functions are also considered.
2446 :
2447 : So set LOOKUP_COPY_PARM to let reference_binding know that
2448 : it's being called in that context. */
2449 :
2450 : int
2451 451288177 : conv_flags (int i, int nargs, tree fn, tree arg, int flags)
2452 : {
2453 451288177 : int lflags = flags;
2454 451288177 : tree t;
2455 468723842 : if (i == 0 && nargs == 1 && DECL_CONSTRUCTOR_P (fn)
2456 149324603 : && (t = FUNCTION_FIRST_USER_PARMTYPE (fn))
2457 600612780 : && (same_type_ignoring_top_level_qualifiers_p
2458 149324603 : (non_reference (TREE_VALUE (t)), DECL_CONTEXT (fn))))
2459 : {
2460 115985441 : if (!(flags & LOOKUP_ONLYCONVERTING))
2461 35294589 : lflags |= LOOKUP_COPY_PARM;
2462 115985441 : if ((flags & LOOKUP_LIST_INIT_CTOR)
2463 115985441 : && BRACE_ENCLOSED_INITIALIZER_P (arg))
2464 1835 : lflags |= LOOKUP_NO_CONVERSION;
2465 : }
2466 : else
2467 335302736 : lflags |= LOOKUP_ONLYCONVERTING;
2468 :
2469 451288177 : return lflags;
2470 : }
2471 :
2472 : /* Build an appropriate 'this' conversion for the method FN and class
2473 : type CTYPE from the value ARG (having type ARGTYPE) to the type PARMTYPE.
2474 : This function modifies PARMTYPE, ARGTYPE and ARG. */
2475 :
2476 : static conversion *
2477 95672774 : build_this_conversion (tree fn, tree ctype,
2478 : tree& parmtype, tree& argtype, tree& arg,
2479 : int flags, tsubst_flags_t complain)
2480 : {
2481 191345548 : gcc_assert (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
2482 : && !DECL_CONSTRUCTOR_P (fn));
2483 :
2484 : /* The type of the implicit object parameter ('this') for
2485 : overload resolution is not always the same as for the
2486 : function itself; conversion functions are considered to
2487 : be members of the class being converted, and functions
2488 : introduced by a using-declaration are considered to be
2489 : members of the class that uses them.
2490 :
2491 : Since build_over_call ignores the ICS for the `this'
2492 : parameter, we can just change the parm type. */
2493 95672774 : parmtype = cp_build_qualified_type (ctype,
2494 95672774 : cp_type_quals (TREE_TYPE (parmtype)));
2495 95672774 : bool this_p = true;
2496 95672774 : if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn)))
2497 : {
2498 : /* If the function has a ref-qualifier, the implicit
2499 : object parameter has reference type. */
2500 167723 : bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn));
2501 167723 : parmtype = cp_build_reference_type (parmtype, rv);
2502 : /* The special handling of 'this' conversions in compare_ics
2503 : does not apply if there is a ref-qualifier. */
2504 167723 : this_p = false;
2505 : }
2506 : else
2507 : {
2508 95505051 : parmtype = build_pointer_type (parmtype);
2509 : /* We don't use build_this here because we don't want to
2510 : capture the object argument until we've chosen a
2511 : non-static member function. */
2512 95505051 : arg = build_address (arg);
2513 95505051 : argtype = lvalue_type (arg);
2514 : }
2515 95672774 : flags |= LOOKUP_ONLYCONVERTING;
2516 95672774 : conversion *t = implicit_conversion (parmtype, argtype, arg,
2517 : /*c_cast_p=*/false, flags, complain);
2518 95672774 : t->this_p = this_p;
2519 95672774 : return t;
2520 : }
2521 :
2522 : /* Create an overload candidate for the function or method FN called
2523 : with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
2524 : FLAGS is passed on to implicit_conversion.
2525 :
2526 : This does not change ARGS.
2527 :
2528 : CTYPE, if non-NULL, is the type we want to pretend this function
2529 : comes from for purposes of overload resolution.
2530 :
2531 : SHORTCUT_BAD_CONVS controls how we handle "bad" argument conversions.
2532 : If true, we stop computing conversions upon seeing the first bad
2533 : conversion. This is used by add_candidates to avoid computing
2534 : more conversions than necessary in the presence of a strictly viable
2535 : candidate, while preserving the defacto behavior of overload resolution
2536 : when it turns out there are only non-strictly viable candidates. */
2537 :
2538 : static struct z_candidate *
2539 639544973 : add_function_candidate (struct z_candidate **candidates,
2540 : tree fn, tree ctype, tree first_arg,
2541 : const vec<tree, va_gc> *args, tree access_path,
2542 : tree conversion_path, int flags,
2543 : conversion **convs,
2544 : bool shortcut_bad_convs,
2545 : tsubst_flags_t complain)
2546 : {
2547 639544973 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
2548 639544973 : int i, len;
2549 639544973 : tree parmnode;
2550 639544973 : tree orig_first_arg = first_arg;
2551 639544973 : int skip;
2552 639544973 : int viable = 1;
2553 639544973 : struct rejection_reason *reason = NULL;
2554 :
2555 : /* The `this', `in_chrg' and VTT arguments to constructors are not
2556 : considered in overload resolution. */
2557 1279089946 : if (DECL_CONSTRUCTOR_P (fn))
2558 : {
2559 290461099 : if (ctor_omit_inherited_parms (fn))
2560 : /* Bring back parameters omitted from an inherited ctor. */
2561 150 : parmlist = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
2562 : else
2563 290461024 : parmlist = skip_artificial_parms_for (fn, parmlist);
2564 290461099 : skip = num_artificial_parms_for (fn);
2565 290461099 : if (skip > 0 && first_arg != NULL_TREE)
2566 : {
2567 290461099 : --skip;
2568 290461099 : first_arg = NULL_TREE;
2569 : }
2570 : }
2571 : else
2572 : skip = 0;
2573 :
2574 1237666122 : len = vec_safe_length (args) - skip + (first_arg != NULL_TREE ? 1 : 0);
2575 639544973 : if (!convs)
2576 549343036 : convs = alloc_conversions (len);
2577 :
2578 : /* 13.3.2 - Viable functions [over.match.viable]
2579 : First, to be a viable function, a candidate function shall have enough
2580 : parameters to agree in number with the arguments in the list.
2581 :
2582 : We need to check this first; otherwise, checking the ICSes might cause
2583 : us to produce an ill-formed template instantiation. */
2584 :
2585 639544973 : parmnode = parmlist;
2586 1307623474 : for (i = 0; i < len; ++i)
2587 : {
2588 745450133 : if (parmnode == NULL_TREE || parmnode == void_list_node)
2589 : break;
2590 668078501 : parmnode = TREE_CHAIN (parmnode);
2591 : }
2592 :
2593 639544973 : if ((i < len && parmnode)
2594 639544973 : || !sufficient_parms_p (parmnode))
2595 : {
2596 156732049 : int remaining = remaining_arguments (parmnode);
2597 156732049 : viable = 0;
2598 156732049 : reason = arity_rejection (first_arg, i + remaining, len);
2599 : }
2600 :
2601 : /* An inherited constructor (12.6.3 [class.inhctor.init]) that has a first
2602 : parameter of type "reference to cv C" (including such a constructor
2603 : instantiated from a template) is excluded from the set of candidate
2604 : functions when used to construct an object of type D with an argument list
2605 : containing a single argument if C is reference-related to D. */
2606 584080505 : if (viable && len == 1 && parmlist && DECL_CONSTRUCTOR_P (fn)
2607 151789675 : && flag_new_inheriting_ctors
2608 791325963 : && DECL_INHERITED_CTOR (fn))
2609 : {
2610 1065645 : tree ptype = non_reference (TREE_VALUE (parmlist));
2611 1065645 : tree dtype = DECL_CONTEXT (fn);
2612 2131290 : tree btype = DECL_INHERITED_CTOR_BASE (fn);
2613 1065645 : if (reference_related_p (ptype, dtype)
2614 1065645 : && reference_related_p (btype, ptype))
2615 : {
2616 807010 : viable = false;
2617 807010 : reason = inherited_ctor_rejection ();
2618 : }
2619 : }
2620 :
2621 : /* Second, for a function to be viable, its constraints must be
2622 : satisfied. */
2623 639544973 : if (flag_concepts && viable && !constraints_satisfied_p (fn))
2624 : {
2625 198835 : reason = constraint_failure ();
2626 198835 : viable = false;
2627 : }
2628 :
2629 : /* When looking for a function from a subobject from an implicit
2630 : copy/move constructor/operator=, don't consider anything that takes (a
2631 : reference to) an unrelated type. See c++/44909 and core 1092. */
2632 639544973 : if (viable && parmlist && (flags & LOOKUP_DEFAULTED))
2633 : {
2634 80004098 : if (DECL_CONSTRUCTOR_P (fn))
2635 : i = 1;
2636 20833139 : else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2637 20833139 : && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
2638 : i = 2;
2639 : else
2640 : i = 0;
2641 40002049 : if (i && len == i)
2642 : {
2643 22075386 : parmnode = chain_index (i-1, parmlist);
2644 22075386 : if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
2645 : ctype))
2646 4469231 : viable = 0;
2647 : }
2648 :
2649 : /* This only applies at the top level. */
2650 35532818 : flags &= ~LOOKUP_DEFAULTED;
2651 : }
2652 :
2653 639544973 : if (! viable)
2654 162207125 : goto out;
2655 :
2656 477337848 : if (shortcut_bad_convs)
2657 477203691 : flags |= LOOKUP_SHORTCUT_BAD_CONVS;
2658 : else
2659 134157 : flags &= ~LOOKUP_SHORTCUT_BAD_CONVS;
2660 :
2661 : /* Third, for F to be a viable function, there shall exist for each
2662 : argument an implicit conversion sequence that converts that argument
2663 : to the corresponding parameter of F. */
2664 :
2665 477337848 : parmnode = parmlist;
2666 :
2667 822379694 : for (i = 0; i < len; ++i)
2668 : {
2669 522715963 : tree argtype, to_type;
2670 522715963 : tree arg;
2671 :
2672 522715963 : if (parmnode == void_list_node)
2673 : break;
2674 :
2675 522715963 : if (convs[i])
2676 : {
2677 : /* Already set during deduction. */
2678 6460863 : parmnode = TREE_CHAIN (parmnode);
2679 6460863 : continue;
2680 : }
2681 :
2682 516255100 : if (i == 0 && first_arg != NULL_TREE)
2683 91566515 : arg = first_arg;
2684 : else
2685 424688585 : arg = const_cast<tree> (
2686 812998812 : (*args)[i + skip - (first_arg != NULL_TREE ? 1 : 0)]);
2687 516255100 : argtype = lvalue_type (arg);
2688 :
2689 516255100 : conversion *t;
2690 516255100 : if (parmnode)
2691 : {
2692 510607180 : tree parmtype = TREE_VALUE (parmnode);
2693 510607180 : if (i == 0
2694 411732135 : && DECL_IOBJ_MEMBER_FUNCTION_P (fn)
2695 994375206 : && !DECL_CONSTRUCTOR_P (fn))
2696 91552990 : t = build_this_conversion (fn, ctype, parmtype, argtype, arg,
2697 : flags, complain);
2698 : else
2699 : {
2700 419054190 : int lflags = conv_flags (i, len-skip, fn, arg, flags);
2701 419054190 : t = implicit_conversion (parmtype, argtype, arg,
2702 : /*c_cast_p=*/false, lflags, complain);
2703 : }
2704 510607180 : to_type = parmtype;
2705 510607180 : parmnode = TREE_CHAIN (parmnode);
2706 : }
2707 : else
2708 : {
2709 5647920 : t = build_identity_conv (argtype, arg);
2710 5647920 : t->ellipsis_p = true;
2711 5647920 : to_type = argtype;
2712 : }
2713 :
2714 516255100 : convs[i] = t;
2715 516255100 : if (! t)
2716 : {
2717 155161321 : viable = 0;
2718 155161321 : reason = arg_conversion_rejection (first_arg, i, argtype, to_type,
2719 155161321 : EXPR_LOCATION (arg));
2720 155161321 : break;
2721 : }
2722 :
2723 361093779 : if (t->bad_p)
2724 : {
2725 22545574 : viable = -1;
2726 22545574 : reason = bad_arg_conversion_rejection (first_arg, i, arg, to_type,
2727 22545574 : EXPR_LOCATION (arg));
2728 22545574 : if (shortcut_bad_convs)
2729 : break;
2730 : }
2731 : }
2732 :
2733 299663731 : out:
2734 639544973 : return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
2735 639544973 : access_path, conversion_path, viable, reason, flags);
2736 : }
2737 :
2738 : /* Create an overload candidate for the conversion function FN which will
2739 : be invoked for expression OBJ, producing a pointer-to-function which
2740 : will in turn be called with the argument list FIRST_ARG/ARGLIST,
2741 : and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
2742 : passed on to implicit_conversion.
2743 :
2744 : Actually, we don't really care about FN; we care about the type it
2745 : converts to. There may be multiple conversion functions that will
2746 : convert to that type, and we rely on build_user_type_conversion_1 to
2747 : choose the best one; so when we create our candidate, we record the type
2748 : instead of the function. */
2749 :
2750 : static struct z_candidate *
2751 119417 : add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
2752 : const vec<tree, va_gc> *arglist,
2753 : tree access_path, tree conversion_path,
2754 : tsubst_flags_t complain)
2755 : {
2756 119417 : tree totype = TREE_TYPE (TREE_TYPE (fn));
2757 119417 : int i, len, viable, flags;
2758 119417 : tree parmlist, parmnode;
2759 119417 : conversion **convs;
2760 119417 : struct rejection_reason *reason;
2761 :
2762 239055 : for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
2763 119638 : parmlist = TREE_TYPE (parmlist);
2764 119417 : parmlist = TYPE_ARG_TYPES (parmlist);
2765 :
2766 119417 : len = vec_safe_length (arglist) + 1;
2767 119417 : convs = alloc_conversions (len);
2768 119417 : parmnode = parmlist;
2769 119417 : viable = 1;
2770 119417 : flags = LOOKUP_IMPLICIT;
2771 119417 : reason = NULL;
2772 :
2773 : /* Don't bother looking up the same type twice. */
2774 119417 : if (*candidates && (*candidates)->fn == totype)
2775 : return NULL;
2776 :
2777 119402 : if (!constraints_satisfied_p (fn))
2778 : {
2779 9 : reason = constraint_failure ();
2780 9 : viable = 0;
2781 9 : return add_candidate (candidates, fn, obj, arglist, len, convs,
2782 9 : access_path, conversion_path, viable, reason, flags);
2783 : }
2784 :
2785 379882 : for (i = 0; i < len; ++i)
2786 : {
2787 260789 : tree arg, argtype, convert_type = NULL_TREE;
2788 260789 : conversion *t;
2789 :
2790 260789 : if (i == 0)
2791 : arg = obj;
2792 : else
2793 141396 : arg = (*arglist)[i - 1];
2794 260789 : argtype = lvalue_type (arg);
2795 :
2796 260789 : if (i == 0)
2797 : {
2798 119393 : t = build_identity_conv (argtype, NULL_TREE);
2799 119393 : t = build_conv (ck_user, totype, t);
2800 : /* Leave the 'cand' field null; we'll figure out the conversion in
2801 : convert_like if this candidate is chosen. */
2802 119393 : convert_type = totype;
2803 : }
2804 141396 : else if (parmnode == void_list_node)
2805 : break;
2806 141169 : else if (parmnode)
2807 : {
2808 141160 : t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
2809 : /*c_cast_p=*/false, flags, complain);
2810 141160 : convert_type = TREE_VALUE (parmnode);
2811 : }
2812 : else
2813 : {
2814 9 : t = build_identity_conv (argtype, arg);
2815 9 : t->ellipsis_p = true;
2816 9 : convert_type = argtype;
2817 : }
2818 :
2819 260562 : convs[i] = t;
2820 260562 : if (! t)
2821 : break;
2822 :
2823 260489 : if (t->bad_p)
2824 : {
2825 27 : viable = -1;
2826 72 : reason = bad_arg_conversion_rejection (NULL_TREE, i, arg, convert_type,
2827 27 : EXPR_LOCATION (arg));
2828 : }
2829 :
2830 260489 : if (i == 0)
2831 119393 : continue;
2832 :
2833 141096 : if (parmnode)
2834 141087 : parmnode = TREE_CHAIN (parmnode);
2835 : }
2836 :
2837 119393 : if (i < len
2838 119393 : || ! sufficient_parms_p (parmnode))
2839 : {
2840 324 : int remaining = remaining_arguments (parmnode);
2841 324 : viable = 0;
2842 324 : reason = arity_rejection (NULL_TREE, i + remaining, len);
2843 : }
2844 :
2845 119393 : return add_candidate (candidates, totype, obj, arglist, len, convs,
2846 119393 : access_path, conversion_path, viable, reason, flags);
2847 : }
2848 :
2849 : static void
2850 9569950 : build_builtin_candidate (struct z_candidate **candidates, tree fnname,
2851 : tree type1, tree type2, const vec<tree,va_gc> &args,
2852 : tree *argtypes, int flags, tsubst_flags_t complain)
2853 : {
2854 9569950 : conversion *t;
2855 9569950 : conversion **convs;
2856 9569950 : size_t num_convs;
2857 9569950 : int viable = 1;
2858 9569950 : tree types[2];
2859 9569950 : struct rejection_reason *reason = NULL;
2860 :
2861 9569950 : types[0] = type1;
2862 9569950 : types[1] = type2;
2863 :
2864 9569950 : num_convs = args.length ();
2865 9569950 : convs = alloc_conversions (num_convs);
2866 :
2867 : /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2868 : conversion ops are allowed. We handle that here by just checking for
2869 : boolean_type_node because other operators don't ask for it. COND_EXPR
2870 : also does contextual conversion to bool for the first operand, but we
2871 : handle that in build_conditional_expr, and type1 here is operand 2. */
2872 9569950 : if (type1 != boolean_type_node)
2873 8999993 : flags |= LOOKUP_ONLYCONVERTING;
2874 :
2875 27969155 : for (unsigned i = 0; i < 2 && i < num_convs; ++i)
2876 : {
2877 18399205 : t = implicit_conversion (types[i], argtypes[i], args[i],
2878 : /*c_cast_p=*/false, flags, complain);
2879 18399205 : if (! t)
2880 : {
2881 1924013 : viable = 0;
2882 : /* We need something for printing the candidate. */
2883 1924013 : t = build_identity_conv (types[i], NULL_TREE);
2884 1924013 : reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2885 1924013 : types[i], EXPR_LOCATION (args[i]));
2886 : }
2887 16475192 : else if (t->bad_p)
2888 : {
2889 252 : viable = 0;
2890 252 : reason = bad_arg_conversion_rejection (NULL_TREE, i, args[i],
2891 : types[i],
2892 252 : EXPR_LOCATION (args[i]));
2893 : }
2894 18399205 : convs[i] = t;
2895 : }
2896 :
2897 : /* For COND_EXPR we rearranged the arguments; undo that now. */
2898 9569950 : if (num_convs == 3)
2899 : {
2900 159 : convs[2] = convs[1];
2901 159 : convs[1] = convs[0];
2902 159 : t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
2903 : /*c_cast_p=*/false, flags,
2904 : complain);
2905 159 : if (t)
2906 159 : convs[0] = t;
2907 : else
2908 : {
2909 0 : viable = 0;
2910 0 : reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2],
2911 : boolean_type_node,
2912 0 : EXPR_LOCATION (args[2]));
2913 : }
2914 : }
2915 :
2916 9569950 : add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
2917 : num_convs, convs,
2918 : /*access_path=*/NULL_TREE,
2919 : /*conversion_path=*/NULL_TREE,
2920 : viable, reason, flags);
2921 9569950 : }
2922 :
2923 : static bool
2924 12 : is_complete (tree t)
2925 : {
2926 12 : return COMPLETE_TYPE_P (complete_type (t));
2927 : }
2928 :
2929 : /* Returns nonzero if TYPE is a promoted arithmetic type. */
2930 :
2931 : static bool
2932 3139 : promoted_arithmetic_type_p (tree type)
2933 : {
2934 : /* [over.built]
2935 :
2936 : In this section, the term promoted integral type is used to refer
2937 : to those integral types which are preserved by integral promotion
2938 : (including e.g. int and long but excluding e.g. char).
2939 : Similarly, the term promoted arithmetic type refers to promoted
2940 : integral types plus floating types. */
2941 3139 : return ((CP_INTEGRAL_TYPE_P (type)
2942 239 : && same_type_p (type_promotes_to (type), type))
2943 3139 : || SCALAR_FLOAT_TYPE_P (type));
2944 : }
2945 :
2946 : /* Create any builtin operator overload candidates for the operator in
2947 : question given the converted operand types TYPE1 and TYPE2. The other
2948 : args are passed through from add_builtin_candidates to
2949 : build_builtin_candidate.
2950 :
2951 : TYPE1 and TYPE2 may not be permissible, and we must filter them.
2952 : If CODE is requires candidates operands of the same type of the kind
2953 : of which TYPE1 and TYPE2 are, we add both candidates
2954 : CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2955 :
2956 : static void
2957 14107102 : add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
2958 : enum tree_code code2, tree fnname, tree type1,
2959 : tree type2, vec<tree,va_gc> &args, tree *argtypes,
2960 : int flags, tsubst_flags_t complain)
2961 : {
2962 14107102 : switch (code)
2963 : {
2964 21 : case POSTINCREMENT_EXPR:
2965 21 : case POSTDECREMENT_EXPR:
2966 21 : args[1] = integer_zero_node;
2967 21 : type2 = integer_type_node;
2968 21 : break;
2969 : default:
2970 : break;
2971 : }
2972 :
2973 14107102 : switch (code)
2974 : {
2975 :
2976 : /* 4 For every pair (T, VQ), where T is an arithmetic type other than bool,
2977 : and VQ is either volatile or empty, there exist candidate operator
2978 : functions of the form
2979 : VQ T& operator++(VQ T&);
2980 : T operator++(VQ T&, int);
2981 : 5 For every pair (T, VQ), where T is an arithmetic type other than bool,
2982 : and VQ is either volatile or empty, there exist candidate operator
2983 : functions of the form
2984 : VQ T& operator--(VQ T&);
2985 : T operator--(VQ T&, int);
2986 : 6 For every pair (T, VQ), where T is a cv-qualified or cv-unqualified object
2987 : type, and VQ is either volatile or empty, there exist candidate operator
2988 : functions of the form
2989 : T*VQ& operator++(T*VQ&);
2990 : T*VQ& operator--(T*VQ&);
2991 : T* operator++(T*VQ&, int);
2992 : T* operator--(T*VQ&, int); */
2993 :
2994 18 : case POSTDECREMENT_EXPR:
2995 18 : case PREDECREMENT_EXPR:
2996 18 : if (TREE_CODE (type1) == BOOLEAN_TYPE)
2997 : return;
2998 : /* FALLTHRU */
2999 39 : case POSTINCREMENT_EXPR:
3000 39 : case PREINCREMENT_EXPR:
3001 : /* P0002R1, Remove deprecated operator++(bool) added "other than bool"
3002 : to p4. */
3003 39 : if (TREE_CODE (type1) == BOOLEAN_TYPE && cxx_dialect >= cxx17)
3004 : return;
3005 33 : if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
3006 : {
3007 24 : type1 = build_reference_type (type1);
3008 24 : break;
3009 : }
3010 : return;
3011 :
3012 : /* 7 For every cv-qualified or cv-unqualified object type T, there
3013 : exist candidate operator functions of the form
3014 :
3015 : T& operator*(T*);
3016 :
3017 :
3018 : 8 For every function type T that does not have cv-qualifiers or
3019 : a ref-qualifier, there exist candidate operator functions of the form
3020 : T& operator*(T*); */
3021 :
3022 113066 : case INDIRECT_REF:
3023 113066 : if (TYPE_PTR_P (type1)
3024 113066 : && (TYPE_PTROB_P (type1)
3025 12 : || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
3026 : break;
3027 : return;
3028 :
3029 : /* 9 For every type T, there exist candidate operator functions of the form
3030 : T* operator+(T*);
3031 :
3032 : 10 For every floating-point or promoted integral type T, there exist
3033 : candidate operator functions of the form
3034 : T operator+(T);
3035 : T operator-(T); */
3036 :
3037 481 : case UNARY_PLUS_EXPR: /* unary + */
3038 481 : if (TYPE_PTR_P (type1))
3039 : break;
3040 : /* FALLTHRU */
3041 211477 : case NEGATE_EXPR:
3042 211477 : if (ARITHMETIC_TYPE_P (type1))
3043 : break;
3044 : return;
3045 :
3046 : /* 11 For every promoted integral type T, there exist candidate operator
3047 : functions of the form
3048 : T operator~(T); */
3049 :
3050 160537 : case BIT_NOT_EXPR:
3051 160537 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
3052 : break;
3053 : return;
3054 :
3055 : /* 12 For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, C1
3056 : is the same type as C2 or is a derived class of C2, and T is an object
3057 : type or a function type there exist candidate operator functions of the
3058 : form
3059 : CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
3060 : where CV12 is the union of CV1 and CV2. */
3061 :
3062 34 : case MEMBER_REF:
3063 34 : if (TYPE_PTR_P (type1) && TYPE_PTRMEM_P (type2))
3064 : {
3065 34 : tree c1 = TREE_TYPE (type1);
3066 34 : tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
3067 :
3068 31 : if (CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
3069 62 : && (TYPE_PTRMEMFUNC_P (type2)
3070 12 : || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
3071 : break;
3072 : }
3073 : return;
3074 :
3075 : /* 13 For every pair of types L and R, where each of L and R is a floating-point
3076 : or promoted integral type, there exist candidate operator functions of the
3077 : form
3078 : LR operator*(L, R);
3079 : LR operator/(L, R);
3080 : LR operator+(L, R);
3081 : LR operator-(L, R);
3082 : bool operator<(L, R);
3083 : bool operator>(L, R);
3084 : bool operator<=(L, R);
3085 : bool operator>=(L, R);
3086 : bool operator==(L, R);
3087 : bool operator!=(L, R);
3088 : where LR is the result of the usual arithmetic conversions between
3089 : types L and R.
3090 :
3091 : 14 For every integral type T there exists a candidate operator function of
3092 : the form
3093 :
3094 : std::strong_ordering operator<=>(T, T);
3095 :
3096 : 15 For every pair of floating-point types L and R, there exists a candidate
3097 : operator function of the form
3098 :
3099 : std::partial_ordering operator<=>(L, R);
3100 :
3101 : 16 For every cv-qualified or cv-unqualified object type T there exist
3102 : candidate operator functions of the form
3103 : T* operator+(T*, std::ptrdiff_t);
3104 : T& operator[](T*, std::ptrdiff_t);
3105 : T* operator-(T*, std::ptrdiff_t);
3106 : T* operator+(std::ptrdiff_t, T*);
3107 : T& operator[](std::ptrdiff_t, T*);
3108 :
3109 : 17 For every T, where T is a pointer to object type, there exist candidate
3110 : operator functions of the form
3111 : std::ptrdiff_t operator-(T, T);
3112 :
3113 : 18 For every T, where T is an enumeration type or a pointer type, there
3114 : exist candidate operator functions of the form
3115 : bool operator<(T, T);
3116 : bool operator>(T, T);
3117 : bool operator<=(T, T);
3118 : bool operator>=(T, T);
3119 : bool operator==(T, T);
3120 : bool operator!=(T, T);
3121 : R operator<=>(T, T);
3122 :
3123 : where R is the result type specified in [expr.spaceship].
3124 :
3125 : 19 For every T, where T is a pointer-to-member type or std::nullptr_t,
3126 : there exist candidate operator functions of the form
3127 : bool operator==(T, T);
3128 : bool operator!=(T, T); */
3129 :
3130 206612 : case MINUS_EXPR:
3131 206612 : if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
3132 : break;
3133 13 : if (TYPE_PTROB_P (type1)
3134 206598 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3135 : {
3136 4 : type2 = ptrdiff_type_node;
3137 4 : break;
3138 : }
3139 : /* FALLTHRU */
3140 898620 : case MULT_EXPR:
3141 898620 : case TRUNC_DIV_EXPR:
3142 898620 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3143 : break;
3144 : return;
3145 :
3146 : /* This isn't exactly what's specified above for operator<=>, but it's
3147 : close enough. In particular, we don't care about the return type
3148 : specified above; it doesn't participate in overload resolution and it
3149 : doesn't affect the semantics of the built-in operator. */
3150 6797103 : case SPACESHIP_EXPR:
3151 6797103 : case EQ_EXPR:
3152 6797103 : case NE_EXPR:
3153 221875 : if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
3154 7018978 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)))
3155 : break;
3156 6797069 : if (NULLPTR_TYPE_P (type1) && NULLPTR_TYPE_P (type2))
3157 : break;
3158 6797063 : if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (args[1]))
3159 : {
3160 : type2 = type1;
3161 : break;
3162 : }
3163 6797060 : if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (args[0]))
3164 : {
3165 : type1 = type2;
3166 : break;
3167 : }
3168 : /* Fall through. */
3169 7529601 : case LT_EXPR:
3170 7529601 : case GT_EXPR:
3171 7529601 : case LE_EXPR:
3172 7529601 : case GE_EXPR:
3173 7529601 : case MAX_EXPR:
3174 7529601 : case MIN_EXPR:
3175 7529601 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3176 : break;
3177 5813717 : if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3178 : break;
3179 5810004 : if (TREE_CODE (type1) == ENUMERAL_TYPE
3180 4160410 : && TREE_CODE (type2) == ENUMERAL_TYPE)
3181 : break;
3182 2807239 : if (TYPE_PTR_P (type1)
3183 2807239 : && null_ptr_cst_p (args[1]))
3184 : {
3185 : type2 = type1;
3186 : break;
3187 : }
3188 2807220 : if (null_ptr_cst_p (args[0])
3189 2807220 : && TYPE_PTR_P (type2))
3190 : {
3191 : type1 = type2;
3192 : break;
3193 : }
3194 : return;
3195 :
3196 248018 : case PLUS_EXPR:
3197 248018 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3198 : break;
3199 : /* FALLTHRU */
3200 182229 : case ARRAY_REF:
3201 182229 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
3202 : {
3203 6 : type1 = ptrdiff_type_node;
3204 6 : break;
3205 : }
3206 182223 : if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3207 : {
3208 37588 : type2 = ptrdiff_type_node;
3209 37588 : break;
3210 : }
3211 : return;
3212 :
3213 : /* 18For every pair of promoted integral types L and R, there exist candi-
3214 : date operator functions of the form
3215 : LR operator%(L, R);
3216 : LR operator&(L, R);
3217 : LR operator^(L, R);
3218 : LR operator|(L, R);
3219 : L operator<<(L, R);
3220 : L operator>>(L, R);
3221 : where LR is the result of the usual arithmetic conversions between
3222 : types L and R. */
3223 :
3224 3088579 : case TRUNC_MOD_EXPR:
3225 3088579 : case BIT_AND_EXPR:
3226 3088579 : case BIT_IOR_EXPR:
3227 3088579 : case BIT_XOR_EXPR:
3228 3088579 : case LSHIFT_EXPR:
3229 3088579 : case RSHIFT_EXPR:
3230 3088579 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3231 : break;
3232 : return;
3233 :
3234 : /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
3235 : type, VQ is either volatile or empty, and R is a promoted arithmetic
3236 : type, there exist candidate operator functions of the form
3237 : VQ L& operator=(VQ L&, R);
3238 : VQ L& operator*=(VQ L&, R);
3239 : VQ L& operator/=(VQ L&, R);
3240 : VQ L& operator+=(VQ L&, R);
3241 : VQ L& operator-=(VQ L&, R);
3242 :
3243 : 20For every pair T, VQ), where T is any type and VQ is either volatile
3244 : or empty, there exist candidate operator functions of the form
3245 : T*VQ& operator=(T*VQ&, T*);
3246 :
3247 : 21For every pair T, VQ), where T is a pointer to member type and VQ is
3248 : either volatile or empty, there exist candidate operator functions of
3249 : the form
3250 : VQ T& operator=(VQ T&, T);
3251 :
3252 : 22For every triple T, VQ, I), where T is a cv-qualified or cv-
3253 : unqualified complete object type, VQ is either volatile or empty, and
3254 : I is a promoted integral type, there exist candidate operator func-
3255 : tions of the form
3256 : T*VQ& operator+=(T*VQ&, I);
3257 : T*VQ& operator-=(T*VQ&, I);
3258 :
3259 : 23For every triple L, VQ, R), where L is an integral or enumeration
3260 : type, VQ is either volatile or empty, and R is a promoted integral
3261 : type, there exist candidate operator functions of the form
3262 :
3263 : VQ L& operator%=(VQ L&, R);
3264 : VQ L& operator<<=(VQ L&, R);
3265 : VQ L& operator>>=(VQ L&, R);
3266 : VQ L& operator&=(VQ L&, R);
3267 : VQ L& operator^=(VQ L&, R);
3268 : VQ L& operator|=(VQ L&, R); */
3269 :
3270 1801711 : case MODIFY_EXPR:
3271 1801711 : switch (code2)
3272 : {
3273 103674 : case PLUS_EXPR:
3274 103674 : case MINUS_EXPR:
3275 103674 : if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3276 : {
3277 0 : type2 = ptrdiff_type_node;
3278 0 : break;
3279 : }
3280 : /* FALLTHRU */
3281 103679 : case MULT_EXPR:
3282 103679 : case TRUNC_DIV_EXPR:
3283 103679 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3284 : break;
3285 : return;
3286 :
3287 1698032 : case TRUNC_MOD_EXPR:
3288 1698032 : case BIT_AND_EXPR:
3289 1698032 : case BIT_IOR_EXPR:
3290 1698032 : case BIT_XOR_EXPR:
3291 1698032 : case LSHIFT_EXPR:
3292 1698032 : case RSHIFT_EXPR:
3293 1698032 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3294 : break;
3295 : return;
3296 :
3297 0 : case NOP_EXPR:
3298 0 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3299 : break;
3300 0 : if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
3301 0 : || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3302 0 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
3303 0 : || ((TYPE_PTRMEMFUNC_P (type1)
3304 0 : || TYPE_PTR_P (type1))
3305 0 : && null_ptr_cst_p (args[1])))
3306 : {
3307 : type2 = type1;
3308 : break;
3309 : }
3310 : return;
3311 :
3312 0 : default:
3313 0 : gcc_unreachable ();
3314 : }
3315 1453522 : type1 = build_reference_type (type1);
3316 1453522 : break;
3317 :
3318 2925 : case COND_EXPR:
3319 : /* [over.built]
3320 :
3321 : For every pair of promoted arithmetic types L and R, there
3322 : exist candidate operator functions of the form
3323 :
3324 : LR operator?(bool, L, R);
3325 :
3326 : where LR is the result of the usual arithmetic conversions
3327 : between types L and R.
3328 :
3329 : For every type T, where T is a pointer or pointer-to-member
3330 : type, there exist candidate operator functions of the form T
3331 : operator?(bool, T, T); */
3332 :
3333 2925 : if (promoted_arithmetic_type_p (type1)
3334 2925 : && promoted_arithmetic_type_p (type2))
3335 : /* That's OK. */
3336 : break;
3337 :
3338 : /* Otherwise, the types should be pointers. */
3339 2900 : if (!((TYPE_PTR_OR_PTRMEM_P (type1) || null_ptr_cst_p (args[0]))
3340 369 : && (TYPE_PTR_OR_PTRMEM_P (type2) || null_ptr_cst_p (args[1]))))
3341 : return;
3342 :
3343 : /* We don't check that the two types are the same; the logic
3344 : below will actually create two candidates; one in which both
3345 : parameter types are TYPE1, and one in which both parameter
3346 : types are TYPE2. */
3347 : break;
3348 :
3349 3 : case REALPART_EXPR:
3350 3 : case IMAGPART_EXPR:
3351 3 : if (ARITHMETIC_TYPE_P (type1))
3352 : break;
3353 : return;
3354 :
3355 0 : default:
3356 0 : gcc_unreachable ();
3357 : }
3358 :
3359 : /* Make sure we don't create builtin candidates with dependent types. */
3360 8999868 : bool u1 = uses_template_parms (type1);
3361 8999868 : bool u2 = type2 ? uses_template_parms (type2) : false;
3362 8999855 : if (u1 || u2)
3363 : {
3364 : /* Try to recover if one of the types is non-dependent. But if
3365 : there's only one type, there's nothing we can do. */
3366 19 : if (!type2)
3367 : return;
3368 : /* And we lose if both are dependent. */
3369 16 : if (u1 && u2)
3370 : return;
3371 : /* Or if they have different forms. */
3372 9 : if (TREE_CODE (type1) != TREE_CODE (type2))
3373 : return;
3374 :
3375 9 : if (u1 && !u2)
3376 : type1 = type2;
3377 6 : else if (u2 && !u1)
3378 8999858 : type2 = type1;
3379 : }
3380 :
3381 : /* If we're dealing with two pointer types or two enumeral types,
3382 : we need candidates for both of them. */
3383 8760598 : if (type2 && !same_type_p (type1, type2)
3384 1941731 : && TREE_CODE (type1) == TREE_CODE (type2)
3385 9450180 : && (TYPE_REF_P (type1)
3386 450322 : || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3387 450206 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
3388 450188 : || TYPE_PTRMEMFUNC_P (type1)
3389 450188 : || MAYBE_CLASS_TYPE_P (type1)
3390 450188 : || TREE_CODE (type1) == ENUMERAL_TYPE))
3391 : {
3392 229 : if (TYPE_PTR_OR_PTRMEM_P (type1))
3393 : {
3394 134 : tree cptype = composite_pointer_type (input_location,
3395 : type1, type2,
3396 : error_mark_node,
3397 : error_mark_node,
3398 : CPO_CONVERSION,
3399 : tf_none);
3400 134 : if (cptype != error_mark_node)
3401 : {
3402 94 : build_builtin_candidate
3403 94 : (candidates, fnname, cptype, cptype, args, argtypes,
3404 : flags, complain);
3405 94 : return;
3406 : }
3407 : }
3408 :
3409 135 : build_builtin_candidate
3410 135 : (candidates, fnname, type1, type1, args, argtypes, flags, complain);
3411 135 : build_builtin_candidate
3412 135 : (candidates, fnname, type2, type2, args, argtypes, flags, complain);
3413 135 : return;
3414 : }
3415 :
3416 8999629 : build_builtin_candidate
3417 8999629 : (candidates, fnname, type1, type2, args, argtypes, flags, complain);
3418 : }
3419 :
3420 : tree
3421 859511641 : type_decays_to (tree type)
3422 : {
3423 859511641 : if (TREE_CODE (type) == ARRAY_TYPE)
3424 6881922 : return build_pointer_type (TREE_TYPE (type));
3425 852629719 : if (TREE_CODE (type) == FUNCTION_TYPE)
3426 37611 : return build_pointer_type (type);
3427 : return type;
3428 : }
3429 :
3430 : /* There are three conditions of builtin candidates:
3431 :
3432 : 1) bool-taking candidates. These are the same regardless of the input.
3433 : 2) pointer-pair taking candidates. These are generated for each type
3434 : one of the input types converts to.
3435 : 3) arithmetic candidates. According to the standard, we should generate
3436 : all of these, but I'm trying not to...
3437 :
3438 : Here we generate a superset of the possible candidates for this particular
3439 : case. That is a subset of the full set the standard defines, plus some
3440 : other cases which the standard disallows. add_builtin_candidate will
3441 : filter out the invalid set. */
3442 :
3443 : static void
3444 24599419 : add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
3445 : enum tree_code code2, tree fnname,
3446 : vec<tree, va_gc> *argv,
3447 : int flags, tsubst_flags_t complain)
3448 : {
3449 24599419 : int ref1;
3450 24599419 : int enum_p = 0;
3451 24599419 : tree type, argtypes[3], t;
3452 : /* TYPES[i] is the set of possible builtin-operator parameter types
3453 : we will consider for the Ith argument. */
3454 24599419 : vec<tree, va_gc> *types[2];
3455 24599419 : unsigned ix;
3456 24599419 : vec<tree, va_gc> &args = *argv;
3457 24599419 : unsigned len = args.length ();
3458 :
3459 68759590 : for (unsigned i = 0; i < len; ++i)
3460 : {
3461 44160171 : if (args[i])
3462 44160171 : argtypes[i] = unlowered_expr_type (args[i]);
3463 : else
3464 : argtypes[i] = NULL_TREE;
3465 : }
3466 :
3467 24599419 : switch (code)
3468 : {
3469 : /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
3470 : and VQ is either volatile or empty, there exist candidate operator
3471 : functions of the form
3472 : VQ T& operator++(VQ T&); */
3473 :
3474 : case POSTINCREMENT_EXPR:
3475 : case PREINCREMENT_EXPR:
3476 : case POSTDECREMENT_EXPR:
3477 : case PREDECREMENT_EXPR:
3478 : case MODIFY_EXPR:
3479 : ref1 = 1;
3480 : break;
3481 :
3482 : /* 24There also exist candidate operator functions of the form
3483 : bool operator!(bool);
3484 : bool operator&&(bool, bool);
3485 : bool operator||(bool, bool); */
3486 :
3487 501435 : case TRUTH_NOT_EXPR:
3488 501435 : build_builtin_candidate
3489 501435 : (candidates, fnname, boolean_type_node,
3490 : NULL_TREE, args, argtypes, flags, complain);
3491 13876787 : return;
3492 :
3493 68522 : case TRUTH_ORIF_EXPR:
3494 68522 : case TRUTH_ANDIF_EXPR:
3495 68522 : build_builtin_candidate
3496 68522 : (candidates, fnname, boolean_type_node,
3497 : boolean_type_node, args, argtypes, flags, complain);
3498 68522 : return;
3499 :
3500 : case ADDR_EXPR:
3501 : case COMPOUND_EXPR:
3502 : case COMPONENT_REF:
3503 : case CO_AWAIT_EXPR:
3504 : return;
3505 :
3506 6371807 : case COND_EXPR:
3507 6371807 : case EQ_EXPR:
3508 6371807 : case NE_EXPR:
3509 6371807 : case LT_EXPR:
3510 6371807 : case LE_EXPR:
3511 6371807 : case GT_EXPR:
3512 6371807 : case GE_EXPR:
3513 6371807 : case SPACESHIP_EXPR:
3514 6371807 : enum_p = 1;
3515 : /* Fall through. */
3516 :
3517 : default:
3518 : ref1 = 0;
3519 : }
3520 :
3521 21853182 : types[0] = make_tree_vector ();
3522 21853182 : types[1] = make_tree_vector ();
3523 :
3524 21853182 : if (len == 3)
3525 738 : len = 2;
3526 44643186 : for (unsigned i = 0; i < len; ++i)
3527 : {
3528 33419119 : if (MAYBE_CLASS_TYPE_P (argtypes[i]))
3529 : {
3530 15210178 : tree convs;
3531 :
3532 15210178 : if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
3533 : return;
3534 :
3535 11618696 : convs = lookup_conversions (argtypes[i]);
3536 :
3537 11618696 : if (code == COND_EXPR)
3538 : {
3539 1295 : if (lvalue_p (args[i]))
3540 940 : vec_safe_push (types[i], build_reference_type (argtypes[i]));
3541 :
3542 1295 : vec_safe_push (types[i], TYPE_MAIN_VARIANT (argtypes[i]));
3543 : }
3544 :
3545 11617401 : else if (! convs)
3546 : return;
3547 :
3548 11296038 : for (; convs; convs = TREE_CHAIN (convs))
3549 : {
3550 6714975 : type = TREE_TYPE (convs);
3551 :
3552 8313473 : if (i == 0 && ref1
3553 6714975 : && (!TYPE_REF_P (type)
3554 2599 : || CP_TYPE_CONST_P (TREE_TYPE (type))))
3555 1598498 : continue;
3556 :
3557 5116477 : if (code == COND_EXPR && TYPE_REF_P (type))
3558 24 : vec_safe_push (types[i], type);
3559 :
3560 5116477 : type = non_reference (type);
3561 5116477 : if (i != 0 || ! ref1)
3562 : {
3563 5116438 : type = cv_unqualified (type_decays_to (type));
3564 5116438 : if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
3565 656 : vec_safe_push (types[i], type);
3566 5116438 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3567 2881394 : type = type_promotes_to (type);
3568 : }
3569 :
3570 5116477 : if (! vec_member (type, types[i]))
3571 5108223 : vec_safe_push (types[i], type);
3572 : }
3573 : }
3574 : else
3575 : {
3576 18208941 : if (code == COND_EXPR && lvalue_p (args[i]))
3577 95 : vec_safe_push (types[i], build_reference_type (argtypes[i]));
3578 18208941 : type = non_reference (argtypes[i]);
3579 18208941 : if (i != 0 || ! ref1)
3580 : {
3581 16407347 : type = cv_unqualified (type_decays_to (type));
3582 16407347 : if (enum_p && UNSCOPED_ENUM_P (type))
3583 1952487 : vec_safe_push (types[i], type);
3584 16407347 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3585 9257848 : type = type_promotes_to (type);
3586 : }
3587 18208941 : vec_safe_push (types[i], type);
3588 : }
3589 : }
3590 :
3591 : /* Run through the possible parameter types of both arguments,
3592 : creating candidates with those parameter types. */
3593 23009255 : FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t)
3594 : {
3595 11785188 : unsigned jx;
3596 11785188 : tree u;
3597 :
3598 11785188 : if (!types[1]->is_empty ())
3599 25407126 : FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u)
3600 13621938 : add_builtin_candidate
3601 13621938 : (candidates, code, code2, fnname, t,
3602 : u, args, argtypes, flags, complain);
3603 : else
3604 485164 : add_builtin_candidate
3605 485164 : (candidates, code, code2, fnname, t,
3606 : NULL_TREE, args, argtypes, flags, complain);
3607 : }
3608 :
3609 11224067 : release_tree_vector (types[0]);
3610 11224067 : release_tree_vector (types[1]);
3611 : }
3612 :
3613 :
3614 : /* If TMPL can be successfully instantiated as indicated by
3615 : EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
3616 :
3617 : TMPL is the template. EXPLICIT_TARGS are any explicit template
3618 : arguments. ARGLIST is the arguments provided at the call-site.
3619 : This does not change ARGLIST. The RETURN_TYPE is the desired type
3620 : for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
3621 : as for add_function_candidate. If an OBJ is supplied, FLAGS and
3622 : CTYPE are ignored, and OBJ is as for add_conv_candidate.
3623 :
3624 : SHORTCUT_BAD_CONVS is as in add_function_candidate. */
3625 :
3626 : static struct z_candidate*
3627 515182649 : add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
3628 : tree ctype, tree explicit_targs, tree first_arg,
3629 : const vec<tree, va_gc> *arglist, tree return_type,
3630 : tree access_path, tree conversion_path,
3631 : int flags, tree obj, unification_kind_t strict,
3632 : bool shortcut_bad_convs, tsubst_flags_t complain)
3633 : {
3634 515182649 : int ntparms = DECL_NTPARMS (tmpl);
3635 515182649 : tree targs = make_tree_vec (ntparms);
3636 515182649 : unsigned int len = vec_safe_length (arglist);
3637 515182649 : unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
3638 515182649 : unsigned int skip_without_in_chrg = 0;
3639 515182649 : tree first_arg_without_in_chrg = first_arg;
3640 515182649 : tree *args_without_in_chrg;
3641 515182649 : unsigned int nargs_without_in_chrg;
3642 515182649 : unsigned int ia, ix;
3643 515182649 : tree arg;
3644 515182649 : struct z_candidate *cand;
3645 515182649 : tree fn;
3646 515182649 : struct rejection_reason *reason = NULL;
3647 515182649 : int errs;
3648 515182649 : conversion **convs = NULL;
3649 :
3650 : /* We don't do deduction on the in-charge parameter, the VTT
3651 : parameter or 'this'. */
3652 515182649 : if (DECL_IOBJ_MEMBER_FUNCTION_P (tmpl))
3653 : {
3654 53095495 : if (first_arg_without_in_chrg != NULL_TREE)
3655 : first_arg_without_in_chrg = NULL_TREE;
3656 12 : else if (return_type && strict == DEDUCE_CALL)
3657 : /* We're deducing for a call to the result of a template conversion
3658 : function, so the args don't contain 'this'; leave them alone. */;
3659 : else
3660 515182649 : ++skip_without_in_chrg;
3661 : }
3662 :
3663 515182649 : if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
3664 515182649 : || DECL_BASE_CONSTRUCTOR_P (tmpl))
3665 516183085 : && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
3666 : {
3667 261 : if (first_arg_without_in_chrg != NULL_TREE)
3668 : first_arg_without_in_chrg = NULL_TREE;
3669 : else
3670 261 : ++skip_without_in_chrg;
3671 : }
3672 :
3673 515182649 : if (len < skip_without_in_chrg)
3674 0 : return add_ignored_candidate (candidates, tmpl);
3675 :
3676 563587397 : if (DECL_CONSTRUCTOR_P (tmpl) && nargs == 2
3677 558236664 : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (first_arg),
3678 43054015 : TREE_TYPE ((*arglist)[0])))
3679 : {
3680 : /* 12.8/6 says, "A declaration of a constructor for a class X is
3681 : ill-formed if its first parameter is of type (optionally cv-qualified)
3682 : X and either there are no other parameters or else all other
3683 : parameters have default arguments. A member function template is never
3684 : instantiated to produce such a constructor signature."
3685 :
3686 : So if we're trying to copy an object of the containing class, don't
3687 : consider a template constructor that has a first parameter type that
3688 : is just a template parameter, as we would deduce a signature that we
3689 : would then reject in the code below. */
3690 2359382 : if (tree firstparm = FUNCTION_FIRST_USER_PARMTYPE (tmpl))
3691 : {
3692 2359382 : firstparm = TREE_VALUE (firstparm);
3693 2359382 : if (PACK_EXPANSION_P (firstparm))
3694 2122 : firstparm = PACK_EXPANSION_PATTERN (firstparm);
3695 2359382 : if (TREE_CODE (firstparm) == TEMPLATE_TYPE_PARM)
3696 : {
3697 756512 : gcc_assert (!explicit_targs);
3698 756512 : reason = invalid_copy_with_fn_template_rejection ();
3699 756512 : goto fail;
3700 : }
3701 : }
3702 : }
3703 :
3704 514426137 : nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
3705 514426137 : + (len - skip_without_in_chrg));
3706 514426137 : args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
3707 514426137 : ia = 0;
3708 514426137 : if (first_arg_without_in_chrg != NULL_TREE)
3709 : {
3710 10139 : args_without_in_chrg[ia] = first_arg_without_in_chrg;
3711 10139 : ++ia;
3712 : }
3713 514426137 : for (ix = skip_without_in_chrg;
3714 1373602948 : vec_safe_iterate (arglist, ix, &arg);
3715 : ++ix)
3716 : {
3717 859176811 : args_without_in_chrg[ia] = arg;
3718 859176811 : ++ia;
3719 : }
3720 514426137 : gcc_assert (ia == nargs_without_in_chrg);
3721 :
3722 514426137 : if (!obj)
3723 : {
3724 : /* Check that there's no obvious arity mismatch before proceeding with
3725 : deduction. This avoids substituting explicit template arguments
3726 : into the template or e.g. derived-to-base parm/arg unification
3727 : (which could result in an error outside the immediate context) when
3728 : the resulting candidate would be unviable anyway. */
3729 514426125 : int min_arity = 0, max_arity = 0;
3730 514426125 : tree parms = TYPE_ARG_TYPES (TREE_TYPE (tmpl));
3731 514426125 : parms = skip_artificial_parms_for (tmpl, parms);
3732 1929561481 : for (; parms != void_list_node; parms = TREE_CHAIN (parms))
3733 : {
3734 1807598845 : if (!parms || PACK_EXPANSION_P (TREE_VALUE (parms)))
3735 : {
3736 : max_arity = -1;
3737 : break;
3738 : }
3739 900709231 : if (TREE_PURPOSE (parms))
3740 : /* A parameter with a default argument. */
3741 10067519 : ++max_arity;
3742 : else
3743 890641712 : ++min_arity, ++max_arity;
3744 : }
3745 514426125 : if (ia < (unsigned)min_arity)
3746 : {
3747 : /* Too few arguments. */
3748 34819228 : reason = arity_rejection (NULL_TREE, min_arity, ia,
3749 : /*least_p=*/(max_arity == -1));
3750 34819228 : goto fail;
3751 : }
3752 479606897 : else if (max_arity != -1 && ia > (unsigned)max_arity)
3753 : {
3754 : /* Too many arguments. */
3755 5165120 : reason = arity_rejection (NULL_TREE, max_arity, ia);
3756 5165120 : goto fail;
3757 : }
3758 :
3759 474441777 : convs = alloc_conversions (nargs);
3760 :
3761 474441777 : if (shortcut_bad_convs
3762 474432399 : && DECL_IOBJ_MEMBER_FUNCTION_P (tmpl)
3763 548497821 : && !DECL_CONSTRUCTOR_P (tmpl))
3764 : {
3765 : /* Check the 'this' conversion before proceeding with deduction.
3766 : This is effectively an extension of the DR 1391 resolution
3767 : that we perform in check_non_deducible_conversions, though it's
3768 : convenient to do this extra check here instead of there. */
3769 4119784 : tree parmtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (tmpl)));
3770 4119784 : tree argtype = lvalue_type (first_arg);
3771 4119784 : tree arg = first_arg;
3772 4119784 : conversion *t = build_this_conversion (tmpl, ctype,
3773 : parmtype, argtype, arg,
3774 : flags, complain);
3775 4119784 : convs[0] = t;
3776 4119784 : if (t->bad_p)
3777 : {
3778 39437 : reason = bad_arg_conversion_rejection (first_arg, 0,
3779 : arg, parmtype,
3780 39437 : EXPR_LOCATION (arg));
3781 39437 : goto fail;
3782 : }
3783 : }
3784 : }
3785 :
3786 474402352 : errs = errorcount+sorrycount;
3787 948791153 : fn = fn_type_unification (tmpl, explicit_targs, targs,
3788 : args_without_in_chrg,
3789 : nargs_without_in_chrg,
3790 : return_type, strict, flags, convs,
3791 474402352 : false, complain & tf_decltype);
3792 :
3793 474388801 : if (fn == error_mark_node)
3794 : {
3795 : /* Don't repeat unification later if it already resulted in errors. */
3796 384180556 : if (errorcount+sorrycount == errs)
3797 384180440 : reason = template_unification_rejection (tmpl, explicit_targs,
3798 : targs, args_without_in_chrg,
3799 : nargs_without_in_chrg,
3800 : return_type, strict, flags);
3801 : else
3802 116 : reason = template_unification_error_rejection ();
3803 384180556 : goto fail;
3804 : }
3805 :
3806 : /* Now the explicit specifier might have been deduced; check if this
3807 : declaration is explicit. If it is and we're ignoring non-converting
3808 : constructors, don't add this function to the set of candidates. */
3809 90208245 : if (((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
3810 : == LOOKUP_ONLYCONVERTING)
3811 90208245 : && DECL_NONCONVERTING_P (fn))
3812 5457 : return add_ignored_candidate (candidates, fn);
3813 :
3814 180405576 : if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
3815 : {
3816 4827325 : tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
3817 9654623 : if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
3818 : ctype))
3819 : {
3820 : /* We're trying to produce a constructor with a prohibited signature,
3821 : as discussed above; handle here any cases we didn't catch then,
3822 : such as X(X<T>). */
3823 842 : reason = invalid_copy_with_fn_template_rejection ();
3824 842 : goto fail;
3825 : }
3826 : }
3827 :
3828 90201946 : if (obj != NULL_TREE)
3829 : /* Aha, this is a conversion function. */
3830 9 : cand = add_conv_candidate (candidates, fn, obj, arglist,
3831 : access_path, conversion_path, complain);
3832 : else
3833 90201937 : cand = add_function_candidate (candidates, fn, ctype,
3834 : first_arg, arglist, access_path,
3835 : conversion_path, flags, convs,
3836 : shortcut_bad_convs, complain);
3837 90201946 : if (DECL_TI_TEMPLATE (fn) != tmpl)
3838 : /* This situation can occur if a member template of a template
3839 : class is specialized. Then, instantiate_template might return
3840 : an instantiation of the specialization, in which case the
3841 : DECL_TI_TEMPLATE field will point at the original
3842 : specialization. For example:
3843 :
3844 : template <class T> struct S { template <class U> void f(U);
3845 : template <> void f(int) {}; };
3846 : S<double> sd;
3847 : sd.f(3);
3848 :
3849 : Here, TMPL will be template <class U> S<double>::f(U).
3850 : And, instantiate template will give us the specialization
3851 : template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
3852 : for this will point at template <class T> template <> S<T>::f(int),
3853 : so that we can find the definition. For the purposes of
3854 : overload resolution, however, we want the original TMPL. */
3855 5556912 : cand->template_decl = build_template_info (tmpl, targs);
3856 : else
3857 84645034 : cand->template_decl = DECL_TEMPLATE_INFO (fn);
3858 90201946 : cand->explicit_targs = explicit_targs;
3859 :
3860 90201946 : return cand;
3861 424961695 : fail:
3862 424961695 : int viable = (reason->code == rr_bad_arg_conversion ? -1 : 0);
3863 424961695 : return add_candidate (candidates, tmpl, first_arg, arglist, nargs, convs,
3864 424961695 : access_path, conversion_path, viable, reason, flags);
3865 : }
3866 :
3867 :
3868 : static struct z_candidate *
3869 515182637 : add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
3870 : tree explicit_targs, tree first_arg,
3871 : const vec<tree, va_gc> *arglist, tree return_type,
3872 : tree access_path, tree conversion_path, int flags,
3873 : unification_kind_t strict, bool shortcut_bad_convs,
3874 : tsubst_flags_t complain)
3875 : {
3876 515182637 : return
3877 0 : add_template_candidate_real (candidates, tmpl, ctype,
3878 : explicit_targs, first_arg, arglist,
3879 : return_type, access_path, conversion_path,
3880 : flags, NULL_TREE, strict, shortcut_bad_convs,
3881 0 : complain);
3882 : }
3883 :
3884 : /* Create an overload candidate for the conversion function template TMPL,
3885 : returning RETURN_TYPE, which will be invoked for expression OBJ to produce a
3886 : pointer-to-function which will in turn be called with the argument list
3887 : ARGLIST, and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
3888 : passed on to implicit_conversion. */
3889 :
3890 : static struct z_candidate *
3891 12 : add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
3892 : tree obj,
3893 : const vec<tree, va_gc> *arglist,
3894 : tree return_type, tree access_path,
3895 : tree conversion_path, tsubst_flags_t complain)
3896 : {
3897 12 : return
3898 12 : add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
3899 : NULL_TREE, arglist, return_type, access_path,
3900 : conversion_path, 0, obj, DEDUCE_CALL,
3901 12 : /*shortcut_bad_convs=*/false, complain);
3902 : }
3903 :
3904 : /* The CANDS are the set of candidates that were considered for
3905 : overload resolution. Sort CANDS so that the strictly viable
3906 : candidates appear first, followed by non-strictly viable candidates,
3907 : followed by non-viable candidates. Returns the first candidate
3908 : in this sorted list. If any of the candidates were viable, set
3909 : *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
3910 : considered viable only if it is strictly viable when setting
3911 : *ANY_VIABLE_P. */
3912 :
3913 : static struct z_candidate*
3914 327996734 : splice_viable (struct z_candidate *cands,
3915 : bool strict_p,
3916 : bool *any_viable_p)
3917 : {
3918 327996734 : z_candidate *strictly_viable = nullptr;
3919 327996734 : z_candidate **strictly_viable_tail = &strictly_viable;
3920 :
3921 327996734 : z_candidate *non_strictly_viable = nullptr;
3922 327996734 : z_candidate **non_strictly_viable_tail = &non_strictly_viable;
3923 :
3924 327996734 : z_candidate *non_viable = nullptr;
3925 327996734 : z_candidate **non_viable_tail = &non_viable;
3926 :
3927 327996734 : z_candidate *non_viable_ignored = nullptr;
3928 327996734 : z_candidate **non_viable_ignored_tail = &non_viable_ignored;
3929 :
3930 : /* Be strict inside templates, since build_over_call won't actually
3931 : do the conversions to get pedwarns. */
3932 327996734 : if (processing_template_decl)
3933 56956240 : strict_p = true;
3934 :
3935 1247216086 : for (z_candidate *cand = cands; cand; cand = cand->next)
3936 : {
3937 919219352 : if (!strict_p
3938 323055126 : && (cand->viable == 1 || TREE_CODE (cand->fn) == TEMPLATE_DECL))
3939 : /* Be strict in the presence of a viable candidate. Also if
3940 : there are template candidates, so that we get deduction errors
3941 : for them instead of silently preferring a bad conversion. */
3942 819917778 : strict_p = true;
3943 :
3944 : /* Move this candidate to the appropriate list according to
3945 : its viability. */
3946 919219352 : auto& tail = (cand->viable == 1 ? strictly_viable_tail
3947 : : cand->viable == -1 ? non_strictly_viable_tail
3948 1464435430 : : ignored_candidate_p (cand) ? non_viable_ignored_tail
3949 1521606322 : : non_viable_tail);
3950 919219352 : *tail = cand;
3951 919219352 : tail = &cand->next;
3952 : }
3953 :
3954 655993468 : *any_viable_p = (strictly_viable != nullptr
3955 327996734 : || (!strict_p && non_strictly_viable != nullptr));
3956 :
3957 : /* Combine the lists. */
3958 327996734 : *non_viable_ignored_tail = nullptr;
3959 327996734 : *non_viable_tail = non_viable_ignored;
3960 327996734 : *non_strictly_viable_tail = non_viable;
3961 327996734 : *strictly_viable_tail = non_strictly_viable;
3962 :
3963 327996734 : return strictly_viable;
3964 : }
3965 :
3966 : static bool
3967 314023101 : any_strictly_viable (struct z_candidate *cands)
3968 : {
3969 429380841 : for (; cands; cands = cands->next)
3970 124330943 : if (cands->viable == 1)
3971 : return true;
3972 : return false;
3973 : }
3974 :
3975 : /* OBJ is being used in an expression like "OBJ.f (...)". In other
3976 : words, it is about to become the "this" pointer for a member
3977 : function call. Take the address of the object. */
3978 :
3979 : static tree
3980 68730395 : build_this (tree obj)
3981 : {
3982 : /* In a template, we are only concerned about the type of the
3983 : expression, so we can take a shortcut. */
3984 68730395 : if (processing_template_decl)
3985 76653 : return build_address (obj);
3986 :
3987 68653742 : return cp_build_addr_expr (obj, tf_warning_or_error);
3988 : }
3989 :
3990 : /* Returns true iff functions are equivalent. Equivalent functions are
3991 : not '==' only if one is a function-local extern function or if
3992 : both are extern "C". */
3993 :
3994 : static inline int
3995 4882671 : equal_functions (tree fn1, tree fn2)
3996 : {
3997 4882671 : if (TREE_CODE (fn1) != TREE_CODE (fn2))
3998 : return 0;
3999 4867310 : if (TREE_CODE (fn1) == TEMPLATE_DECL)
4000 36051 : return fn1 == fn2;
4001 9662500 : if (DECL_LOCAL_DECL_P (fn1) || DECL_LOCAL_DECL_P (fn2)
4002 9662497 : || DECL_EXTERN_C_FUNCTION_P (fn1))
4003 3019117 : return decls_match (fn1, fn2);
4004 1812142 : return fn1 == fn2;
4005 : }
4006 :
4007 : /* Print information about a candidate FN being rejected due to INFO. */
4008 :
4009 : static void
4010 5325 : print_conversion_rejection (location_t loc, struct conversion_info *info,
4011 : tree fn)
4012 : {
4013 5325 : tree from = info->from;
4014 5325 : if (!TYPE_P (from))
4015 4385 : from = lvalue_type (from);
4016 5325 : if (info->n_arg == -1)
4017 : {
4018 : /* Conversion of implicit `this' argument failed. */
4019 32 : if (!TYPE_P (info->from))
4020 : /* A bad conversion for 'this' must be discarding cv-quals. */
4021 32 : inform (loc, "passing %qT as %<this%> "
4022 : "argument discards qualifiers",
4023 : from);
4024 : else
4025 0 : inform (loc, "no known conversion for implicit "
4026 : "%<this%> parameter from %qH to %qI",
4027 : from, info->to_type);
4028 : }
4029 5293 : else if (!TYPE_P (info->from))
4030 : {
4031 4353 : if (info->n_arg >= 0)
4032 4353 : inform (loc, "conversion of argument %d would be ill-formed:",
4033 : info->n_arg + 1);
4034 4353 : iloc_sentinel ils = loc;
4035 4353 : perform_implicit_conversion (info->to_type, info->from,
4036 : tf_warning_or_error);
4037 4353 : }
4038 940 : else if (info->n_arg == -2)
4039 : /* Conversion of conversion function return value failed. */
4040 33 : inform (loc, "no known conversion from %qH to %qI",
4041 : from, info->to_type);
4042 : else
4043 : {
4044 907 : if (TREE_CODE (fn) == FUNCTION_DECL)
4045 751 : loc = get_fndecl_argument_location (fn, info->n_arg);
4046 907 : inform (loc, "no known conversion for argument %d from %qH to %qI",
4047 907 : info->n_arg + 1, from, info->to_type);
4048 : }
4049 5325 : }
4050 :
4051 : /* Print information about a candidate with WANT parameters and we found
4052 : HAVE. */
4053 :
4054 : static void
4055 1457 : print_arity_information (location_t loc, unsigned int have, unsigned int want,
4056 : bool least_p)
4057 : {
4058 1457 : if (least_p)
4059 41 : inform_n (loc, want,
4060 : "candidate expects at least %d argument, %d provided",
4061 : "candidate expects at least %d arguments, %d provided",
4062 : want, have);
4063 : else
4064 1416 : inform_n (loc, want,
4065 : "candidate expects %d argument, %d provided",
4066 : "candidate expects %d arguments, %d provided",
4067 : want, have);
4068 1457 : }
4069 :
4070 : /* Print information about one overload candidate CANDIDATE. MSGSTR
4071 : is the text to print before the candidate itself.
4072 :
4073 : NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
4074 : to have been run through gettext by the caller. This wart makes
4075 : life simpler in print_z_candidates and for the translators. */
4076 :
4077 : static void
4078 13323 : print_z_candidate (location_t loc, const char *msgstr,
4079 : struct z_candidate *candidate)
4080 : {
4081 13323 : const char *msg = (msgstr == NULL
4082 13323 : ? ""
4083 13323 : : ACONCAT ((_(msgstr), " ", NULL)));
4084 13323 : tree fn = candidate->fn;
4085 13323 : if (flag_new_inheriting_ctors)
4086 13296 : fn = strip_inheriting_ctors (fn);
4087 13323 : location_t cloc = location_of (fn);
4088 :
4089 13323 : if (identifier_p (fn))
4090 : {
4091 214 : cloc = loc;
4092 214 : if (candidate->num_convs == 3)
4093 0 : inform (cloc, "%s%<%D(%T, %T, %T)%> (built-in)", msg, fn,
4094 0 : candidate->convs[0]->type,
4095 0 : candidate->convs[1]->type,
4096 0 : candidate->convs[2]->type);
4097 214 : else if (candidate->num_convs == 2)
4098 190 : inform (cloc, "%s%<%D(%T, %T)%> (built-in)", msg, fn,
4099 190 : candidate->convs[0]->type,
4100 190 : candidate->convs[1]->type);
4101 : else
4102 24 : inform (cloc, "%s%<%D(%T)%> (built-in)", msg, fn,
4103 24 : candidate->convs[0]->type);
4104 : }
4105 13109 : else if (TYPE_P (fn))
4106 15 : inform (cloc, "%s%qT (conversion)", msg, fn);
4107 13094 : else if (candidate->viable == -1)
4108 4392 : inform (cloc, "%s%#qD (near match)", msg, fn);
4109 8702 : else if (ignored_candidate_p (candidate))
4110 6 : inform (cloc, "%s%#qD (ignored)", msg, fn);
4111 8696 : else if (DECL_DELETED_FN (fn))
4112 60 : inform (cloc, "%s%#qD (deleted)", msg, fn);
4113 8636 : else if (candidate->reversed ())
4114 300 : inform (cloc, "%s%#qD (reversed)", msg, fn);
4115 8336 : else if (candidate->rewritten ())
4116 150 : inform (cloc, "%s%#qD (rewritten)", msg, fn);
4117 : else
4118 8186 : inform (cloc, "%s%#qD", msg, fn);
4119 :
4120 13323 : auto_diagnostic_nesting_level sentinel;
4121 :
4122 13323 : if (fn != candidate->fn)
4123 : {
4124 57 : cloc = location_of (candidate->fn);
4125 57 : inform (cloc, "inherited here");
4126 : }
4127 :
4128 : /* Give the user some information about why this candidate failed. */
4129 13323 : if (candidate->reason != NULL)
4130 : {
4131 10208 : struct rejection_reason *r = candidate->reason;
4132 :
4133 10208 : switch (r->code)
4134 : {
4135 1457 : case rr_arity:
4136 1457 : print_arity_information (cloc, r->u.arity.actual,
4137 1457 : r->u.arity.expected,
4138 : r->u.arity.least_p);
4139 1457 : break;
4140 913 : case rr_arg_conversion:
4141 913 : print_conversion_rejection (cloc, &r->u.conversion, fn);
4142 913 : break;
4143 4412 : case rr_bad_arg_conversion:
4144 4412 : print_conversion_rejection (cloc, &r->u.bad_conversion, fn);
4145 4412 : break;
4146 11 : case rr_explicit_conversion:
4147 11 : inform (cloc, "return type %qT of explicit conversion function "
4148 : "cannot be converted to %qT with a qualification "
4149 : "conversion", r->u.conversion.from,
4150 : r->u.conversion.to_type);
4151 11 : break;
4152 6 : case rr_template_conversion:
4153 6 : inform (cloc, "conversion from return type %qT of template "
4154 : "conversion function specialization to %qT is not an "
4155 : "exact match", r->u.conversion.from,
4156 : r->u.conversion.to_type);
4157 6 : break;
4158 3276 : case rr_template_unification:
4159 : /* We use template_unification_error_rejection if unification caused
4160 : actual non-SFINAE errors, in which case we don't need to repeat
4161 : them here. */
4162 3276 : if (r->u.template_unification.tmpl == NULL_TREE)
4163 : {
4164 45 : inform (cloc, "substitution of deduced template arguments "
4165 : "resulted in errors seen above");
4166 45 : break;
4167 : }
4168 : /* Re-run template unification with diagnostics. */
4169 3231 : inform (cloc, "template argument deduction/substitution failed:");
4170 3231 : {
4171 3231 : auto_diagnostic_nesting_level sentinel;
4172 3231 : fn_type_unification (r->u.template_unification.tmpl,
4173 : r->u.template_unification.explicit_targs,
4174 : (make_tree_vec
4175 : (r->u.template_unification.num_targs)),
4176 : r->u.template_unification.args,
4177 : r->u.template_unification.nargs,
4178 : r->u.template_unification.return_type,
4179 : r->u.template_unification.strict,
4180 : r->u.template_unification.flags,
4181 : NULL, true, false);
4182 3231 : }
4183 3231 : break;
4184 2 : case rr_invalid_copy:
4185 2 : inform (cloc,
4186 : "a constructor taking a single argument of its own "
4187 : "class type is invalid");
4188 2 : break;
4189 93 : case rr_constraint_failure:
4190 93 : diagnose_constraints (cloc, fn, NULL_TREE);
4191 93 : break;
4192 32 : case rr_inherited_ctor:
4193 32 : inform (cloc, "an inherited constructor is not a candidate for "
4194 : "initialization from an expression of the same or derived "
4195 : "type");
4196 32 : break;
4197 : case rr_ignored:
4198 : break;
4199 0 : case rr_none:
4200 0 : default:
4201 : /* This candidate didn't have any issues or we failed to
4202 : handle a particular code. Either way... */
4203 0 : gcc_unreachable ();
4204 : }
4205 : }
4206 13323 : }
4207 :
4208 : /* Print information about each overload candidate in CANDIDATES,
4209 : which is assumed to have gone through splice_viable and tourney
4210 : (if splice_viable succeeded). */
4211 :
4212 : static void
4213 5564 : print_z_candidates (location_t loc, struct z_candidate *candidates,
4214 : tristate only_viable_p /* = tristate::unknown () */)
4215 : {
4216 5564 : struct z_candidate *cand1;
4217 5564 : struct z_candidate **cand2;
4218 :
4219 5564 : if (!candidates)
4220 1011 : return;
4221 :
4222 : /* Remove non-viable deleted candidates. */
4223 4553 : cand1 = candidates;
4224 20453 : for (cand2 = &cand1; *cand2; )
4225 : {
4226 15900 : if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
4227 11447 : && !(*cand2)->viable
4228 19661 : && DECL_DELETED_FN ((*cand2)->fn))
4229 102 : *cand2 = (*cand2)->next;
4230 : else
4231 15798 : cand2 = &(*cand2)->next;
4232 : }
4233 : /* ...if there are any non-deleted ones. */
4234 4553 : if (cand1)
4235 4547 : candidates = cand1;
4236 :
4237 : /* There may be duplicates in the set of candidates. We put off
4238 : checking this condition as long as possible, since we have no way
4239 : to eliminate duplicates from a set of functions in less than n^2
4240 : time. Now we are about to emit an error message, so it is more
4241 : permissible to go slowly. */
4242 20051 : for (cand1 = candidates; cand1; cand1 = cand1->next)
4243 : {
4244 15498 : tree fn = cand1->fn;
4245 : /* Skip builtin candidates and conversion functions. */
4246 15498 : if (!DECL_P (fn))
4247 289 : continue;
4248 15209 : cand2 = &cand1->next;
4249 143367 : while (*cand2)
4250 : {
4251 128158 : if (DECL_P ((*cand2)->fn)
4252 128158 : && equal_functions (fn, (*cand2)->fn))
4253 306 : *cand2 = (*cand2)->next;
4254 : else
4255 127852 : cand2 = &(*cand2)->next;
4256 : }
4257 : }
4258 :
4259 : /* Unless otherwise specified, if there's a (strictly) viable candidate
4260 : then we assume we're being called as part of diagnosing ambiguity, in
4261 : which case we want to print only viable candidates since non-viable
4262 : candidates couldn't have contributed to the ambiguity. */
4263 4553 : if (only_viable_p.is_unknown ())
4264 4544 : only_viable_p = candidates->viable == 1;
4265 :
4266 4553 : auto_diagnostic_nesting_level sentinel;
4267 :
4268 4553 : int num_candidates = 0;
4269 22319 : for (auto iter = candidates; iter; iter = iter->next)
4270 : {
4271 13554 : if (only_viable_p.is_true () && iter->viable != 1)
4272 : break;
4273 13213 : ++num_candidates;
4274 : }
4275 :
4276 4553 : inform_num_candidates (loc, num_candidates);
4277 :
4278 4553 : auto_diagnostic_nesting_level sentinel2;
4279 :
4280 4553 : int candidate_idx = 0;
4281 22284 : for (; candidates; candidates = candidates->next)
4282 : {
4283 13550 : if (only_viable_p.is_true () && candidates->viable != 1)
4284 : break;
4285 13246 : if (ignored_candidate_p (candidates) && !flag_diagnostics_all_candidates)
4286 : {
4287 31 : inform (loc, "some candidates omitted; "
4288 : "use %<-fdiagnostics-all-candidates%> to display them");
4289 31 : break;
4290 : }
4291 13178 : pretty_printer pp;
4292 13178 : pp_printf (&pp, N_("candidate %i:"), candidate_idx + 1);
4293 13178 : const char *const msgstr = pp_formatted_text (&pp);
4294 13178 : print_z_candidate (loc, msgstr, candidates);
4295 13178 : ++candidate_idx;
4296 13178 : }
4297 4553 : }
4298 :
4299 : /* USER_SEQ is a user-defined conversion sequence, beginning with a
4300 : USER_CONV. STD_SEQ is the standard conversion sequence applied to
4301 : the result of the conversion function to convert it to the final
4302 : desired type. Merge the two sequences into a single sequence,
4303 : and return the merged sequence. */
4304 :
4305 : static conversion *
4306 15456603 : merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
4307 : {
4308 15456603 : conversion **t;
4309 15456603 : bool bad = user_seq->bad_p;
4310 :
4311 15456603 : gcc_assert (user_seq->kind == ck_user);
4312 :
4313 : /* Find the end of the second conversion sequence. */
4314 16237599 : for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
4315 : {
4316 : /* The entire sequence is a user-conversion sequence. */
4317 780996 : (*t)->user_conv_p = true;
4318 780996 : if (bad)
4319 3506 : (*t)->bad_p = true;
4320 : }
4321 :
4322 15456603 : if ((*t)->rvaluedness_matches_p)
4323 : /* We're binding a reference directly to the result of the conversion.
4324 : build_user_type_conversion_1 stripped the REFERENCE_TYPE from the return
4325 : type, but we want it back. */
4326 466126 : user_seq->type = TREE_TYPE (TREE_TYPE (user_seq->cand->fn));
4327 :
4328 : /* Replace the identity conversion with the user conversion
4329 : sequence. */
4330 15456603 : *t = user_seq;
4331 :
4332 15456603 : return std_seq;
4333 : }
4334 :
4335 : /* Handle overload resolution for initializing an object of class type from
4336 : an initializer list. First we look for a suitable constructor that
4337 : takes a std::initializer_list; if we don't find one, we then look for a
4338 : non-list constructor.
4339 :
4340 : Parameters are as for add_candidates, except that the arguments are in
4341 : the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
4342 : the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
4343 :
4344 : static void
4345 2550322 : add_list_candidates (tree fns, tree first_arg,
4346 : const vec<tree, va_gc> *args, tree totype,
4347 : tree explicit_targs, bool template_only,
4348 : tree conversion_path, tree access_path,
4349 : int flags,
4350 : struct z_candidate **candidates,
4351 : tsubst_flags_t complain)
4352 : {
4353 2550322 : gcc_assert (*candidates == NULL);
4354 :
4355 : /* We're looking for a ctor for list-initialization. */
4356 2550322 : flags |= LOOKUP_LIST_INIT_CTOR;
4357 : /* And we don't allow narrowing conversions. We also use this flag to
4358 : avoid the copy constructor call for copy-list-initialization. */
4359 2550322 : flags |= LOOKUP_NO_NARROWING;
4360 :
4361 5100644 : unsigned nart = num_artificial_parms_for (OVL_FIRST (fns)) - 1;
4362 2550322 : tree init_list = (*args)[nart];
4363 :
4364 : /* Always use the default constructor if the list is empty (DR 990). */
4365 2550322 : if (CONSTRUCTOR_NELTS (init_list) == 0
4366 2550322 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
4367 : ;
4368 2306829 : else if (CONSTRUCTOR_IS_DESIGNATED_INIT (init_list)
4369 2306829 : && !CP_AGGREGATE_TYPE_P (totype))
4370 : {
4371 53 : if (complain & tf_error)
4372 15 : error ("designated initializers cannot be used with a "
4373 : "non-aggregate type %qT", totype);
4374 11745 : return;
4375 : }
4376 : /* If the class has a list ctor, try passing the list as a single
4377 : argument first, but only consider list ctors. */
4378 2306776 : else if (TYPE_HAS_LIST_CTOR (totype))
4379 : {
4380 88415 : flags |= LOOKUP_LIST_ONLY;
4381 88415 : add_candidates (fns, first_arg, args, NULL_TREE,
4382 : explicit_targs, template_only, conversion_path,
4383 : access_path, flags, candidates, complain);
4384 176830 : if (any_strictly_viable (*candidates))
4385 : return;
4386 : }
4387 :
4388 : /* Expand the CONSTRUCTOR into a new argument vec. */
4389 2538577 : vec<tree, va_gc> *new_args;
4390 4832907 : vec_alloc (new_args, nart + CONSTRUCTOR_NELTS (init_list));
4391 5077157 : for (unsigned i = 0; i < nart; ++i)
4392 3 : new_args->quick_push ((*args)[i]);
4393 2538577 : new_args = append_ctor_to_tree_vector (new_args, init_list);
4394 :
4395 : /* We aren't looking for list-ctors anymore. */
4396 2538577 : flags &= ~LOOKUP_LIST_ONLY;
4397 : /* We allow more user-defined conversions within an init-list. */
4398 2538577 : flags &= ~LOOKUP_NO_CONVERSION;
4399 :
4400 2538577 : add_candidates (fns, first_arg, new_args, NULL_TREE,
4401 : explicit_targs, template_only, conversion_path,
4402 : access_path, flags, candidates, complain);
4403 : }
4404 :
4405 : /* Given C(std::initializer_list<A>), return A. */
4406 :
4407 : static tree
4408 1507 : list_ctor_element_type (tree fn)
4409 : {
4410 1507 : gcc_checking_assert (is_list_ctor (fn));
4411 :
4412 1507 : tree parm = FUNCTION_FIRST_USER_PARMTYPE (fn);
4413 1507 : parm = non_reference (TREE_VALUE (parm));
4414 1507 : return TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
4415 : }
4416 :
4417 : /* If EXPR is a braced-init-list where the elements all decay to the same type,
4418 : return that type. */
4419 :
4420 : static tree
4421 1583 : braced_init_element_type (tree expr)
4422 : {
4423 1583 : if (TREE_CODE (expr) == CONSTRUCTOR
4424 1583 : && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
4425 0 : return TREE_TYPE (TREE_TYPE (expr));
4426 1583 : if (!BRACE_ENCLOSED_INITIALIZER_P (expr))
4427 : return NULL_TREE;
4428 :
4429 1583 : tree elttype = NULL_TREE;
4430 15372 : for (constructor_elt &e: CONSTRUCTOR_ELTS (expr))
4431 : {
4432 10825 : tree type = TREE_TYPE (e.value);
4433 10825 : type = type_decays_to (type);
4434 10825 : if (!elttype)
4435 : elttype = type;
4436 9270 : else if (!same_type_p (type, elttype))
4437 : return NULL_TREE;
4438 : }
4439 : return elttype;
4440 : }
4441 :
4442 : /* True iff EXPR contains any temporaries with non-trivial destruction.
4443 :
4444 : ??? Also ignore classes with non-trivial but no-op destruction other than
4445 : std::allocator? */
4446 :
4447 : static bool
4448 193 : has_non_trivial_temporaries (tree expr)
4449 : {
4450 193 : auto_vec<tree*> temps;
4451 193 : cp_walk_tree_without_duplicates (&expr, find_temps_r, &temps);
4452 388 : for (tree *p : temps)
4453 : {
4454 65 : tree t = TREE_TYPE (*p);
4455 65 : if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (t)
4456 65 : && !is_std_allocator (t))
4457 : return true;
4458 : }
4459 : return false;
4460 193 : }
4461 :
4462 : /* Return number of initialized elements in CTOR. */
4463 :
4464 : unsigned HOST_WIDE_INT
4465 263 : count_ctor_elements (tree ctor)
4466 : {
4467 263 : unsigned HOST_WIDE_INT len = 0;
4468 2194 : for (constructor_elt &e: CONSTRUCTOR_ELTS (ctor))
4469 1405 : if (TREE_CODE (e.value) == RAW_DATA_CST)
4470 9 : len += RAW_DATA_LENGTH (e.value);
4471 : else
4472 1396 : ++len;
4473 263 : return len;
4474 : }
4475 :
4476 : /* We're initializing an array of ELTTYPE from INIT. If it seems useful,
4477 : return INIT as an array (of its own type) so the caller can initialize the
4478 : target array in a loop. */
4479 :
4480 : static tree
4481 19097 : maybe_init_list_as_array (tree elttype, tree init)
4482 : {
4483 : /* Only do this if the array can go in rodata but not once converted. */
4484 19097 : if (!TYPE_NON_AGGREGATE_CLASS (elttype))
4485 : return NULL_TREE;
4486 1583 : tree init_elttype = braced_init_element_type (init);
4487 1583 : if (!init_elttype || !SCALAR_TYPE_P (init_elttype) || !TREE_CONSTANT (init))
4488 : return NULL_TREE;
4489 :
4490 : /* Check with a stub expression to weed out special cases, and check whether
4491 : we call the same function for direct-init as copy-list-init. */
4492 375 : conversion_obstack_sentinel cos;
4493 375 : init_elttype = cp_build_qualified_type (init_elttype, TYPE_QUAL_CONST);
4494 375 : tree arg = build_stub_object (init_elttype);
4495 375 : conversion *c = implicit_conversion (elttype, init_elttype, arg, false,
4496 : LOOKUP_NORMAL, tf_none);
4497 375 : if (c && c->kind == ck_rvalue)
4498 0 : c = next_conversion (c);
4499 369 : if (!c || c->kind != ck_user)
4500 : return NULL_TREE;
4501 : /* Check that we actually can perform the conversion. */
4502 366 : if (convert_like (c, arg, tf_none) == error_mark_node)
4503 : /* Let the normal code give the error. */
4504 : return NULL_TREE;
4505 :
4506 : /* A glvalue initializer might be significant to a reference constructor
4507 : or conversion operator. */
4508 360 : if (!DECL_CONSTRUCTOR_P (c->cand->fn)
4509 360 : || (TYPE_REF_P (TREE_VALUE
4510 : (FUNCTION_FIRST_USER_PARMTYPE (c->cand->fn)))))
4511 240 : for (auto &ce : CONSTRUCTOR_ELTS (init))
4512 108 : if (non_mergeable_glvalue_p (ce.value))
4513 : return NULL_TREE;
4514 :
4515 357 : tree first = CONSTRUCTOR_ELT (init, 0)->value;
4516 357 : conversion *fc = implicit_conversion (elttype, init_elttype, first, false,
4517 : LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING,
4518 : tf_none);
4519 357 : if (fc && fc->kind == ck_rvalue)
4520 48 : fc = next_conversion (fc);
4521 357 : if (!fc || fc->kind != ck_user || fc->cand->fn != c->cand->fn)
4522 : return NULL_TREE;
4523 315 : first = convert_like (fc, first, tf_none);
4524 315 : if (first == error_mark_node)
4525 : /* Let the normal code give the error. */
4526 : return NULL_TREE;
4527 :
4528 : /* Don't do this if the conversion would be constant. */
4529 312 : first = maybe_constant_init (first);
4530 312 : if (TREE_CONSTANT (first))
4531 : return NULL_TREE;
4532 :
4533 : /* We can't do this if the conversion creates temporaries that need
4534 : to live until the whole array is initialized. */
4535 193 : if (has_non_trivial_temporaries (first))
4536 : return NULL_TREE;
4537 :
4538 : /* We can't do this if copying from the initializer_list would be
4539 : ill-formed. */
4540 193 : tree copy_argtypes = make_tree_vec (1);
4541 193 : TREE_VEC_ELT (copy_argtypes, 0)
4542 193 : = cp_build_qualified_type (elttype, TYPE_QUAL_CONST);
4543 193 : if (!is_xible (INIT_EXPR, elttype, copy_argtypes))
4544 : return NULL_TREE;
4545 :
4546 187 : unsigned HOST_WIDE_INT len = count_ctor_elements (init);
4547 187 : tree arr = build_array_of_n_type (init_elttype, len);
4548 187 : arr = finish_compound_literal (arr, init, tf_none);
4549 187 : DECL_MERGEABLE (TARGET_EXPR_SLOT (arr)) = true;
4550 187 : return arr;
4551 375 : }
4552 :
4553 : /* If we were going to call e.g. vector(initializer_list<string>) starting
4554 : with a list of string-literals (which is inefficient, see PR105838),
4555 : instead build an array of const char* and pass it to the range constructor.
4556 : But only do this for standard library types, where we can assume the
4557 : transformation makes sense.
4558 :
4559 : Really the container classes should have initializer_list<U> constructors to
4560 : get the same effect more simply; this is working around that lack. */
4561 :
4562 : static tree
4563 14998989 : maybe_init_list_as_range (tree fn, tree expr)
4564 : {
4565 14998989 : if (!processing_template_decl
4566 14466572 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
4567 902048 : && is_list_ctor (fn)
4568 15000649 : && decl_in_std_namespace_p (fn))
4569 : {
4570 1507 : tree to = list_ctor_element_type (fn);
4571 1507 : if (tree init = maybe_init_list_as_array (to, expr))
4572 : {
4573 55 : tree begin = decay_conversion (TARGET_EXPR_SLOT (init), tf_none);
4574 55 : tree nelts = array_type_nelts_top (TREE_TYPE (init));
4575 55 : tree end = cp_build_binary_op (input_location, PLUS_EXPR, begin,
4576 : nelts, tf_none);
4577 55 : begin = cp_build_compound_expr (init, begin, tf_none);
4578 55 : return build_constructor_va (init_list_type_node, 2,
4579 55 : NULL_TREE, begin, NULL_TREE, end);
4580 : }
4581 : }
4582 :
4583 : return NULL_TREE;
4584 : }
4585 :
4586 : /* Returns the best overload candidate to perform the requested
4587 : conversion. This function is used for three the overloading situations
4588 : described in [over.match.copy], [over.match.conv], and [over.match.ref].
4589 : If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
4590 : per [dcl.init.ref], so we ignore temporary bindings. */
4591 :
4592 : static struct z_candidate *
4593 96322236 : build_user_type_conversion_1 (tree totype, tree expr, int flags,
4594 : tsubst_flags_t complain)
4595 : {
4596 96322236 : struct z_candidate *candidates, *cand;
4597 96322236 : tree fromtype;
4598 96322236 : tree ctors = NULL_TREE;
4599 96322236 : tree conv_fns = NULL_TREE;
4600 96322236 : conversion *conv = NULL;
4601 96322236 : tree first_arg = NULL_TREE;
4602 96322236 : vec<tree, va_gc> *args = NULL;
4603 96322236 : bool any_viable_p;
4604 96322236 : int convflags;
4605 :
4606 96322236 : if (!expr)
4607 : return NULL;
4608 :
4609 96322230 : fromtype = TREE_TYPE (expr);
4610 :
4611 : /* We represent conversion within a hierarchy using RVALUE_CONV and
4612 : BASE_CONV, as specified by [over.best.ics]; these become plain
4613 : constructor calls, as specified in [dcl.init]. */
4614 96322230 : gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
4615 : || !DERIVED_FROM_P (totype, fromtype));
4616 :
4617 96322230 : if (CLASS_TYPE_P (totype))
4618 : /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
4619 : creating a garbage BASELINK; constructors can't be inherited. */
4620 53102216 : ctors = get_class_binding (totype, complete_ctor_identifier);
4621 :
4622 96322230 : tree to_nonref = non_reference (totype);
4623 96322230 : if (MAYBE_CLASS_TYPE_P (fromtype))
4624 : {
4625 67152395 : if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
4626 67142709 : (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
4627 54588164 : && DERIVED_FROM_P (to_nonref, fromtype)))
4628 : {
4629 : /* [class.conv.fct] A conversion function is never used to
4630 : convert a (possibly cv-qualified) object to the (possibly
4631 : cv-qualified) same object type (or a reference to it), to a
4632 : (possibly cv-qualified) base class of that type (or a
4633 : reference to it)... */
4634 : }
4635 : else
4636 67142706 : conv_fns = lookup_conversions (fromtype);
4637 : }
4638 :
4639 96322230 : candidates = 0;
4640 96322230 : flags |= LOOKUP_NO_CONVERSION;
4641 96322230 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4642 945456 : flags |= LOOKUP_NO_NARROWING;
4643 : /* Prevent add_candidates from treating a non-strictly viable candidate
4644 : as unviable. */
4645 96322230 : complain |= tf_conv;
4646 :
4647 : /* It's OK to bind a temporary for converting constructor arguments, but
4648 : not in converting the return value of a conversion operator. */
4649 96322230 : convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION
4650 96322230 : | (flags & LOOKUP_NO_NARROWING));
4651 96322230 : flags &= ~LOOKUP_NO_TEMP_BIND;
4652 :
4653 96322230 : if (ctors)
4654 : {
4655 52883623 : int ctorflags = flags;
4656 :
4657 52883623 : first_arg = build_dummy_object (totype);
4658 :
4659 : /* We should never try to call the abstract or base constructor
4660 : from here. */
4661 371171443 : gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_FIRST (ctors))
4662 : && !DECL_HAS_VTT_PARM_P (OVL_FIRST (ctors)));
4663 :
4664 52883623 : args = make_tree_vector_single (expr);
4665 52883623 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4666 : {
4667 : /* List-initialization. */
4668 945456 : add_list_candidates (ctors, first_arg, args, totype, NULL_TREE,
4669 945456 : false, TYPE_BINFO (totype), TYPE_BINFO (totype),
4670 : ctorflags, &candidates, complain);
4671 : }
4672 : else
4673 : {
4674 51938167 : add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
4675 51938167 : TYPE_BINFO (totype), TYPE_BINFO (totype),
4676 : ctorflags, &candidates, complain);
4677 : }
4678 :
4679 292195271 : for (cand = candidates; cand; cand = cand->next)
4680 : {
4681 239311648 : cand->second_conv = build_identity_conv (totype, NULL_TREE);
4682 :
4683 : /* If totype isn't a reference, and LOOKUP_ONLYCONVERTING is
4684 : set, then this is copy-initialization. In that case, "The
4685 : result of the call is then used to direct-initialize the
4686 : object that is the destination of the copy-initialization."
4687 : [dcl.init]
4688 :
4689 : We represent this in the conversion sequence with an
4690 : rvalue conversion, which means a constructor call. */
4691 239311648 : if (!TYPE_REF_P (totype)
4692 239311648 : && cxx_dialect < cxx17
4693 435663 : && (flags & LOOKUP_ONLYCONVERTING)
4694 377440 : && !(convflags & LOOKUP_NO_TEMP_BIND))
4695 117486 : cand->second_conv
4696 117486 : = build_conv (ck_rvalue, totype, cand->second_conv);
4697 : }
4698 : }
4699 :
4700 96322230 : if (conv_fns)
4701 : {
4702 30245920 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4703 0 : first_arg = CONSTRUCTOR_ELT (expr, 0)->value;
4704 : else
4705 96322230 : first_arg = expr;
4706 : }
4707 :
4708 131556617 : for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
4709 : {
4710 35234387 : tree conversion_path = TREE_PURPOSE (conv_fns);
4711 35234387 : struct z_candidate *old_candidates;
4712 :
4713 : /* If LOOKUP_NO_CONVERSION, don't consider a conversion function that
4714 : would need an additional user-defined conversion, i.e. if the return
4715 : type differs in class-ness from the desired type. So we avoid
4716 : considering operator bool when calling a copy constructor.
4717 :
4718 : This optimization avoids the failure in PR97600, and is allowed by
4719 : [temp.inst]/9: "If the function selected by overload resolution can be
4720 : determined without instantiating a class template definition, it is
4721 : unspecified whether that instantiation actually takes place." */
4722 35234387 : tree convtype = non_reference (TREE_TYPE (conv_fns));
4723 51920010 : if ((flags & LOOKUP_NO_CONVERSION)
4724 35234387 : && !WILDCARD_TYPE_P (convtype)
4725 66839700 : && (CLASS_TYPE_P (to_nonref)
4726 33419850 : != CLASS_TYPE_P (convtype)))
4727 16685623 : continue;
4728 :
4729 : /* If we are called to convert to a reference type, we are trying to
4730 : find a direct binding, so don't even consider temporaries. If
4731 : we don't find a direct binding, the caller will try again to
4732 : look for a temporary binding. */
4733 18548764 : if (TYPE_REF_P (totype))
4734 5148878 : convflags |= LOOKUP_NO_TEMP_BIND;
4735 :
4736 18548764 : old_candidates = candidates;
4737 18548764 : add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
4738 : NULL_TREE, false,
4739 18548764 : conversion_path, TYPE_BINFO (fromtype),
4740 : flags, &candidates, complain);
4741 :
4742 37096815 : for (cand = candidates; cand != old_candidates; cand = cand->next)
4743 : {
4744 18548051 : if (cand->viable == 0)
4745 : /* Already rejected, don't change to -1. */
4746 7008546 : continue;
4747 :
4748 11539505 : tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
4749 11539505 : conversion *ics
4750 11539505 : = implicit_conversion (totype,
4751 : rettype,
4752 : 0,
4753 : /*c_cast_p=*/false, convflags,
4754 : complain);
4755 :
4756 : /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
4757 : copy-initialization. In that case, "The result of the
4758 : call is then used to direct-initialize the object that is
4759 : the destination of the copy-initialization." [dcl.init]
4760 :
4761 : We represent this in the conversion sequence with an
4762 : rvalue conversion, which means a constructor call. But
4763 : don't add a second rvalue conversion if there's already
4764 : one there. Which there really shouldn't be, but it's
4765 : harmless since we'd add it here anyway. */
4766 4398523 : if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
4767 12251249 : && !(convflags & LOOKUP_NO_TEMP_BIND))
4768 259983 : ics = build_conv (ck_rvalue, totype, ics);
4769 :
4770 11539505 : cand->second_conv = ics;
4771 :
4772 11539505 : if (!ics)
4773 : {
4774 7140982 : cand->viable = 0;
4775 14279399 : cand->reason = arg_conversion_rejection (NULL_TREE, -2,
4776 : rettype, totype,
4777 7140982 : EXPR_LOCATION (expr));
4778 : }
4779 14625 : else if (TYPE_REF_P (totype) && !ics->rvaluedness_matches_p
4780 : /* Limit this to non-templates for now (PR90546). */
4781 1896 : && !cand->template_decl
4782 4400414 : && TREE_CODE (TREE_TYPE (totype)) != FUNCTION_TYPE)
4783 : {
4784 : /* If we are called to convert to a reference type, we are trying
4785 : to find a direct binding per [over.match.ref], so rvaluedness
4786 : must match for non-functions. */
4787 1885 : cand->viable = 0;
4788 : }
4789 4396638 : else if (DECL_NONCONVERTING_P (cand->fn)
4790 4396638 : && ics->rank > cr_exact)
4791 : {
4792 : /* 13.3.1.5: For direct-initialization, those explicit
4793 : conversion functions that are not hidden within S and
4794 : yield type T or a type that can be converted to type T
4795 : with a qualification conversion (4.4) are also candidate
4796 : functions. */
4797 : /* 13.3.1.6 doesn't have a parallel restriction, but it should;
4798 : I've raised this issue with the committee. --jason 9/2011 */
4799 2735 : cand->viable = -1;
4800 2735 : cand->reason = explicit_conversion_rejection (rettype, totype);
4801 : }
4802 4393903 : else if (cand->viable == 1 && ics->bad_p)
4803 : {
4804 2576 : cand->viable = -1;
4805 2576 : cand->reason
4806 5152 : = bad_arg_conversion_rejection (NULL_TREE, -2,
4807 : rettype, totype,
4808 2576 : EXPR_LOCATION (expr));
4809 : }
4810 4391327 : else if (primary_template_specialization_p (cand->fn)
4811 4391327 : && ics->rank > cr_exact)
4812 : {
4813 : /* 13.3.3.1.2: If the user-defined conversion is specified by
4814 : a specialization of a conversion function template, the
4815 : second standard conversion sequence shall have exact match
4816 : rank. */
4817 21 : cand->viable = -1;
4818 21 : cand->reason = template_conversion_rejection (rettype, totype);
4819 : }
4820 : }
4821 : }
4822 :
4823 96322230 : candidates = splice_viable (candidates, false, &any_viable_p);
4824 96322230 : if (!any_viable_p)
4825 : {
4826 81321895 : if (args)
4827 41555706 : release_tree_vector (args);
4828 : return NULL;
4829 : }
4830 :
4831 15000335 : cand = tourney (candidates, complain);
4832 15000335 : if (cand == NULL)
4833 : {
4834 1346 : if (complain & tf_error)
4835 : {
4836 71 : auto_diagnostic_group d;
4837 74 : error_at (cp_expr_loc_or_input_loc (expr),
4838 : "conversion from %qH to %qI is ambiguous",
4839 : fromtype, totype);
4840 71 : print_z_candidates (location_of (expr), candidates);
4841 71 : }
4842 :
4843 1346 : cand = candidates; /* any one will do */
4844 1346 : cand->second_conv = build_ambiguous_conv (totype, expr);
4845 1346 : cand->second_conv->user_conv_p = true;
4846 2692 : if (!any_strictly_viable (candidates))
4847 12 : cand->second_conv->bad_p = true;
4848 1346 : if (flags & LOOKUP_ONLYCONVERTING)
4849 1273 : cand->second_conv->need_temporary_p = true;
4850 : /* If there are viable candidates, don't set ICS_BAD_FLAG; an
4851 : ambiguous conversion is no worse than another user-defined
4852 : conversion. */
4853 :
4854 : return cand;
4855 : }
4856 :
4857 : /* Maybe pass { } as iterators instead of an initializer_list. */
4858 14998989 : if (tree iters = maybe_init_list_as_range (cand->fn, expr))
4859 110 : if (z_candidate *cand2
4860 55 : = build_user_type_conversion_1 (totype, iters, flags, tf_none))
4861 52 : if (cand2->viable == 1 && !is_list_ctor (cand2->fn))
4862 : {
4863 : cand = cand2;
4864 : expr = iters;
4865 : }
4866 :
4867 14998989 : tree convtype;
4868 29997978 : if (!DECL_CONSTRUCTOR_P (cand->fn))
4869 4384311 : convtype = non_reference (TREE_TYPE (TREE_TYPE (cand->fn)));
4870 10614678 : else if (cand->second_conv->kind == ck_rvalue)
4871 : /* DR 5: [in the first step of copy-initialization]...if the function
4872 : is a constructor, the call initializes a temporary of the
4873 : cv-unqualified version of the destination type. */
4874 12089 : convtype = cv_unqualified (totype);
4875 : else
4876 : convtype = totype;
4877 : /* Build the user conversion sequence. */
4878 14998989 : conv = build_conv
4879 14998989 : (ck_user,
4880 : convtype,
4881 14998989 : build_identity_conv (TREE_TYPE (expr), expr));
4882 14998989 : conv->cand = cand;
4883 14998989 : if (cand->viable == -1)
4884 11669 : conv->bad_p = true;
4885 :
4886 : /* Remember that this was a list-initialization. */
4887 14998989 : if (flags & LOOKUP_NO_NARROWING)
4888 3366570 : conv->check_narrowing = true;
4889 :
4890 : /* Combine it with the second conversion sequence. */
4891 14998989 : cand->second_conv = merge_conversion_sequences (conv,
4892 : cand->second_conv);
4893 :
4894 14998989 : return cand;
4895 : }
4896 :
4897 : /* Wrapper for above. */
4898 :
4899 : tree
4900 24193 : build_user_type_conversion (tree totype, tree expr, int flags,
4901 : tsubst_flags_t complain)
4902 : {
4903 24193 : struct z_candidate *cand;
4904 24193 : tree ret;
4905 :
4906 24193 : auto_cond_timevar tv (TV_OVERLOAD);
4907 :
4908 24193 : conversion_obstack_sentinel cos;
4909 :
4910 24193 : cand = build_user_type_conversion_1 (totype, expr, flags, complain);
4911 :
4912 24193 : if (cand)
4913 : {
4914 23963 : if (cand->second_conv->kind == ck_ambig)
4915 65 : ret = error_mark_node;
4916 : else
4917 : {
4918 23898 : expr = convert_like (cand->second_conv, expr, complain);
4919 23898 : ret = convert_from_reference (expr);
4920 : }
4921 : }
4922 : else
4923 : ret = NULL_TREE;
4924 :
4925 48386 : return ret;
4926 24193 : }
4927 :
4928 : /* Give a helpful diagnostic when implicit_conversion fails. */
4929 :
4930 : static void
4931 700 : implicit_conversion_error (location_t loc, tree type, tree expr,
4932 : int flags)
4933 : {
4934 700 : tsubst_flags_t complain = tf_warning_or_error;
4935 :
4936 : /* If expr has unknown type, then it is an overloaded function.
4937 : Call instantiate_type to get good error messages. */
4938 700 : if (TREE_TYPE (expr) == unknown_type_node)
4939 60 : instantiate_type (type, expr, complain);
4940 640 : else if (invalid_nonstatic_memfn_p (loc, expr, complain))
4941 : /* We gave an error. */;
4942 72 : else if (BRACE_ENCLOSED_INITIALIZER_P (expr)
4943 69 : && CONSTRUCTOR_IS_DESIGNATED_INIT (expr)
4944 655 : && !CP_AGGREGATE_TYPE_P (type))
4945 20 : error_at (loc, "designated initializers cannot be used with a "
4946 : "non-aggregate type %qT", type);
4947 : else
4948 : {
4949 615 : auto_diagnostic_group d;
4950 615 : if (is_stub_object (expr))
4951 : /* The expression is generated by a trait check, we don't have
4952 : a useful location to highlight the label. */
4953 17 : error_at (loc, "could not convert %qH to %qI",
4954 17 : TREE_TYPE (expr), type);
4955 : else
4956 : {
4957 598 : range_label_for_type_mismatch label (TREE_TYPE (expr), type);
4958 598 : gcc_rich_location rich_loc (loc, &label,
4959 598 : highlight_colors::percent_h);
4960 598 : error_at (&rich_loc, "could not convert %qE from %qH to %qI",
4961 598 : expr, TREE_TYPE (expr), type);
4962 598 : }
4963 615 : maybe_show_nonconverting_candidate (type, TREE_TYPE (expr), expr, flags);
4964 615 : }
4965 700 : }
4966 :
4967 : /* Worker for build_converted_constant_expr. */
4968 :
4969 : static tree
4970 336720487 : build_converted_constant_expr_internal (tree type, tree expr,
4971 : int flags, tsubst_flags_t complain)
4972 : {
4973 336720487 : conversion *conv;
4974 336720487 : tree t;
4975 336720487 : location_t loc = cp_expr_loc_or_input_loc (expr);
4976 :
4977 336720487 : if (error_operand_p (expr))
4978 65 : return error_mark_node;
4979 :
4980 336720422 : conversion_obstack_sentinel cos;
4981 :
4982 336720422 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
4983 : /*c_cast_p=*/false, flags, complain);
4984 :
4985 : /* A converted constant expression of type T is an expression, implicitly
4986 : converted to type T, where the converted expression is a constant
4987 : expression and the implicit conversion sequence contains only
4988 :
4989 : * user-defined conversions,
4990 : * lvalue-to-rvalue conversions (7.1),
4991 : * array-to-pointer conversions (7.2),
4992 : * function-to-pointer conversions (7.3),
4993 : * qualification conversions (7.5),
4994 : * integral promotions (7.6),
4995 : * integral conversions (7.8) other than narrowing conversions (11.6.4),
4996 : * null pointer conversions (7.11) from std::nullptr_t,
4997 : * null member pointer conversions (7.12) from std::nullptr_t, and
4998 : * function pointer conversions (7.13),
4999 :
5000 : and where the reference binding (if any) binds directly. */
5001 :
5002 336720422 : for (conversion *c = conv;
5003 388158679 : c && c->kind != ck_identity;
5004 51438257 : c = next_conversion (c))
5005 : {
5006 51438257 : switch (c->kind)
5007 : {
5008 : /* A conversion function is OK. If it isn't constexpr, we'll
5009 : complain later that the argument isn't constant. */
5010 : case ck_user:
5011 : /* List-initialization is OK. */
5012 : case ck_aggr:
5013 : /* The lvalue-to-rvalue conversion is OK. */
5014 : case ck_rvalue:
5015 : /* Array-to-pointer and function-to-pointer. */
5016 : case ck_lvalue:
5017 : /* Function pointer conversions. */
5018 : case ck_fnptr:
5019 : /* Qualification conversions. */
5020 : case ck_qual:
5021 : break;
5022 :
5023 767 : case ck_ref_bind:
5024 767 : if (c->need_temporary_p)
5025 : {
5026 6 : if (complain & tf_error)
5027 1 : error_at (loc, "initializing %qH with %qI in converted "
5028 : "constant expression does not bind directly",
5029 1 : type, next_conversion (c)->type);
5030 : conv = NULL;
5031 : }
5032 : break;
5033 :
5034 9408934 : case ck_base:
5035 9408934 : case ck_pmem:
5036 9408934 : case ck_ptr:
5037 9408934 : case ck_std:
5038 9408934 : t = next_conversion (c)->type;
5039 9408934 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (t)
5040 9408854 : && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5041 : /* Integral promotion or conversion. */
5042 : break;
5043 114 : if (NULLPTR_TYPE_P (t))
5044 : /* Conversion from nullptr to pointer or pointer-to-member. */
5045 : break;
5046 :
5047 114 : if (complain & tf_error)
5048 82 : error_at (loc, "conversion from %qH to %qI in a "
5049 : "converted constant expression", t, type);
5050 : /* fall through. */
5051 :
5052 : default:
5053 : conv = NULL;
5054 : break;
5055 : }
5056 : }
5057 :
5058 : /* Avoid confusing convert_nontype_argument by introducing
5059 : a redundant conversion to the same reference type. */
5060 336718896 : if (conv && conv->kind == ck_ref_bind
5061 336721183 : && REFERENCE_REF_P (expr))
5062 : {
5063 491 : tree ref = TREE_OPERAND (expr, 0);
5064 491 : if (same_type_p (type, TREE_TYPE (ref)))
5065 : return ref;
5066 : }
5067 :
5068 336719953 : if (conv)
5069 : {
5070 : /* Don't copy a class in a template. */
5071 67477 : if (CLASS_TYPE_P (type) && conv->kind == ck_rvalue
5072 336753637 : && processing_template_decl)
5073 393 : conv = next_conversion (conv);
5074 :
5075 : /* Issuing conversion warnings for value-dependent expressions is
5076 : likely too noisy. */
5077 336718427 : warning_sentinel w (warn_conversion);
5078 336718427 : conv->check_narrowing = true;
5079 336718427 : conv->check_narrowing_const_only = true;
5080 336718427 : expr = convert_like (conv, expr, complain);
5081 336718427 : }
5082 : else
5083 : {
5084 1526 : if (complain & tf_error)
5085 212 : implicit_conversion_error (loc, type, expr, flags);
5086 1526 : expr = error_mark_node;
5087 : }
5088 :
5089 : return expr;
5090 336720422 : }
5091 :
5092 : /* Subroutine of convert_nontype_argument.
5093 :
5094 : EXPR is an expression used in a context that requires a converted
5095 : constant-expression, such as a template non-type parameter. Do any
5096 : necessary conversions (that are permitted for converted
5097 : constant-expressions) to convert it to the desired type.
5098 :
5099 : This function doesn't consider explicit conversion functions. If
5100 : you mean to use "a contextually converted constant expression of type
5101 : bool", use build_converted_constant_bool_expr.
5102 :
5103 : If conversion is successful, returns the converted expression;
5104 : otherwise, returns error_mark_node. */
5105 :
5106 : tree
5107 191834696 : build_converted_constant_expr (tree type, tree expr, tsubst_flags_t complain)
5108 : {
5109 191834696 : return build_converted_constant_expr_internal (type, expr, LOOKUP_IMPLICIT,
5110 191834696 : complain);
5111 : }
5112 :
5113 : /* Used to create "a contextually converted constant expression of type
5114 : bool". This differs from build_converted_constant_expr in that it
5115 : also considers explicit conversion functions. */
5116 :
5117 : tree
5118 144885791 : build_converted_constant_bool_expr (tree expr, tsubst_flags_t complain)
5119 : {
5120 144885791 : return build_converted_constant_expr_internal (boolean_type_node, expr,
5121 144885791 : LOOKUP_NORMAL, complain);
5122 : }
5123 :
5124 : /* Do any initial processing on the arguments to a function call. */
5125 :
5126 : vec<tree, va_gc> *
5127 210833593 : resolve_args (vec<tree, va_gc> *args, tsubst_flags_t complain)
5128 : {
5129 210833593 : unsigned int ix;
5130 210833593 : tree arg;
5131 :
5132 390467682 : FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
5133 : {
5134 179634821 : if (error_operand_p (arg))
5135 : return NULL;
5136 179634271 : else if (VOID_TYPE_P (TREE_TYPE (arg)))
5137 : {
5138 155 : if (complain & tf_error)
5139 12 : error_at (cp_expr_loc_or_input_loc (arg),
5140 : "invalid use of void expression");
5141 : return NULL;
5142 : }
5143 179634116 : else if (invalid_nonstatic_memfn_p (EXPR_LOCATION (arg), arg, complain))
5144 : return NULL;
5145 :
5146 : /* Force auto deduction now. Omit tf_warning to avoid redundant
5147 : deprecated warning on deprecated-14.C. */
5148 179634089 : if (!mark_single_function (arg, complain & ~tf_warning))
5149 : return NULL;
5150 : }
5151 : return args;
5152 : }
5153 :
5154 : /* Perform overload resolution on FN, which is called with the ARGS.
5155 :
5156 : Return the candidate function selected by overload resolution, or
5157 : NULL if the event that overload resolution failed. In the case
5158 : that overload resolution fails, *CANDIDATES will be the set of
5159 : candidates considered, and ANY_VIABLE_P will be set to true or
5160 : false to indicate whether or not any of the candidates were
5161 : viable.
5162 :
5163 : The ARGS should already have gone through RESOLVE_ARGS before this
5164 : function is called. */
5165 :
5166 : static struct z_candidate *
5167 103586400 : perform_overload_resolution (tree fn,
5168 : const vec<tree, va_gc> *args,
5169 : struct z_candidate **candidates,
5170 : bool *any_viable_p, tsubst_flags_t complain)
5171 : {
5172 103586400 : struct z_candidate *cand;
5173 103586400 : tree explicit_targs;
5174 103586400 : int template_only;
5175 :
5176 103586400 : auto_cond_timevar tv (TV_OVERLOAD);
5177 :
5178 103586400 : explicit_targs = NULL_TREE;
5179 103586400 : template_only = 0;
5180 :
5181 103586400 : *candidates = NULL;
5182 103586400 : *any_viable_p = true;
5183 :
5184 : /* Check FN. */
5185 103586400 : gcc_assert (OVL_P (fn) || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
5186 :
5187 103586400 : if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
5188 : {
5189 36439790 : explicit_targs = TREE_OPERAND (fn, 1);
5190 36439790 : fn = TREE_OPERAND (fn, 0);
5191 36439790 : template_only = 1;
5192 : }
5193 :
5194 : /* Add the various candidate functions. */
5195 103586400 : add_candidates (fn, NULL_TREE, args, NULL_TREE,
5196 : explicit_targs, template_only,
5197 : /*conversion_path=*/NULL_TREE,
5198 : /*access_path=*/NULL_TREE,
5199 : LOOKUP_NORMAL,
5200 : candidates, complain & ~tf_any_viable);
5201 :
5202 103572882 : *candidates = splice_viable (*candidates, false, any_viable_p);
5203 103572882 : if (*any_viable_p)
5204 : {
5205 103382039 : if (complain & tf_any_viable)
5206 : cand = *candidates;
5207 : else
5208 103381853 : cand = tourney (*candidates, complain);
5209 : }
5210 : else
5211 : cand = NULL;
5212 :
5213 207145764 : return cand;
5214 103572882 : }
5215 :
5216 : /* Print an error message about being unable to build a call to FN with
5217 : ARGS. ANY_VIABLE_P indicates whether any candidate functions could
5218 : be located; CANDIDATES is a possibly empty list of such
5219 : functions. */
5220 :
5221 : static void
5222 2997 : print_error_for_call_failure (tree fn, const vec<tree, va_gc> *args,
5223 : struct z_candidate *candidates)
5224 : {
5225 2997 : tree targs = NULL_TREE;
5226 2997 : if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
5227 : {
5228 548 : targs = TREE_OPERAND (fn, 1);
5229 548 : fn = TREE_OPERAND (fn, 0);
5230 : }
5231 5994 : tree name = OVL_NAME (fn);
5232 2997 : location_t loc = location_of (name);
5233 2997 : if (targs)
5234 548 : name = lookup_template_function (name, targs);
5235 :
5236 2997 : auto_diagnostic_group d;
5237 5994 : if (!any_strictly_viable (candidates))
5238 2512 : error_at (loc, "no matching function for call to %<%D(%A)%>",
5239 : name, build_tree_list_vec (args));
5240 : else
5241 485 : error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous",
5242 : name, build_tree_list_vec (args));
5243 2997 : if (candidates)
5244 2997 : print_z_candidates (loc, candidates);
5245 2997 : }
5246 :
5247 : /* Perform overload resolution on the set of deduction guides DGUIDES
5248 : using ARGS. Returns the selected deduction guide, or error_mark_node
5249 : if overload resolution fails. */
5250 :
5251 : tree
5252 31209 : perform_dguide_overload_resolution (tree dguides, const vec<tree, va_gc> *args,
5253 : tsubst_flags_t complain)
5254 : {
5255 31209 : z_candidate *candidates;
5256 31209 : bool any_viable_p;
5257 31209 : tree result;
5258 :
5259 62418 : gcc_assert (deduction_guide_p (OVL_FIRST (dguides)));
5260 :
5261 31209 : conversion_obstack_sentinel cos;
5262 :
5263 31209 : z_candidate *cand = perform_overload_resolution (dguides, args, &candidates,
5264 : &any_viable_p, complain);
5265 31209 : if (!cand)
5266 : {
5267 969 : if (complain & tf_error)
5268 198 : print_error_for_call_failure (dguides, args, candidates);
5269 969 : result = error_mark_node;
5270 : }
5271 : else
5272 30240 : result = cand->fn;
5273 :
5274 62418 : return result;
5275 31209 : }
5276 :
5277 : /* Return an expression for a call to FN (a namespace-scope function,
5278 : or a static member function) with the ARGS. This may change
5279 : ARGS. */
5280 :
5281 : tree
5282 102473549 : build_new_function_call (tree fn, vec<tree, va_gc> **args,
5283 : tsubst_flags_t complain)
5284 : {
5285 102473549 : struct z_candidate *candidates, *cand;
5286 102473549 : bool any_viable_p;
5287 102473549 : tree result;
5288 :
5289 102473549 : if (args != NULL && *args != NULL)
5290 : {
5291 102473549 : *args = resolve_args (*args, complain & ~tf_any_viable);
5292 102473549 : if (*args == NULL)
5293 420 : return error_mark_node;
5294 : }
5295 :
5296 102473129 : if (flag_tm)
5297 3544 : tm_malloc_replacement (fn);
5298 :
5299 102473129 : conversion_obstack_sentinel cos;
5300 :
5301 102473129 : cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
5302 : complain);
5303 :
5304 102459611 : if (!cand)
5305 : {
5306 202775 : if (complain & tf_error)
5307 : {
5308 : // If there is a single (non-viable) function candidate,
5309 : // let the error be diagnosed by cp_build_function_call_vec.
5310 3213 : if (!any_viable_p && candidates && ! candidates->next
5311 1428 : && TREE_CODE (candidates->fn) == FUNCTION_DECL
5312 : /* A template-id callee consisting of a single (ignored)
5313 : non-template candidate needs to be diagnosed the
5314 : ordinary way. */
5315 435 : && (TREE_CODE (fn) != TEMPLATE_ID_EXPR
5316 33 : || candidates->template_decl))
5317 423 : return cp_build_function_call_vec (candidates->fn, args, complain);
5318 :
5319 : // Otherwise, emit notes for non-viable candidates.
5320 2790 : print_error_for_call_failure (fn, *args, candidates);
5321 : }
5322 202352 : result = error_mark_node;
5323 : }
5324 102256836 : else if (complain & tf_any_viable)
5325 186 : return void_node;
5326 : else
5327 102256650 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
5328 :
5329 102459002 : if (flag_coroutines
5330 100352243 : && result
5331 100352243 : && TREE_CODE (result) == CALL_EXPR
5332 163585475 : && DECL_BUILT_IN_CLASS (TREE_OPERAND (CALL_EXPR_FN (result), 0))
5333 61126473 : == BUILT_IN_NORMAL)
5334 8165185 : result = coro_validate_builtin_call (result);
5335 :
5336 : return result;
5337 102459611 : }
5338 :
5339 : /* Build a call to a global operator new. FNNAME is the name of the
5340 : operator (either "operator new" or "operator new[]") and ARGS are
5341 : the arguments provided. This may change ARGS. *SIZE points to the
5342 : total number of bytes required by the allocation, and is updated if
5343 : that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
5344 : be used. If this function determines that no cookie should be
5345 : used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK
5346 : is not NULL_TREE, it is evaluated before calculating the final
5347 : array size, and if it fails, the array size is replaced with
5348 : (size_t)-1 (usually triggering a std::bad_alloc exception). If FN
5349 : is non-NULL, it will be set, upon return, to the allocation
5350 : function called. */
5351 :
5352 : tree
5353 1082007 : build_operator_new_call (tree fnname, vec<tree, va_gc> **args,
5354 : tree *size, tree *cookie_size,
5355 : tree align_arg, tree size_check,
5356 : tree *fn, tsubst_flags_t complain)
5357 : {
5358 1082007 : tree original_size = *size;
5359 1082007 : tree fns;
5360 1082007 : struct z_candidate *candidates;
5361 1082007 : struct z_candidate *cand = NULL;
5362 1082007 : bool any_viable_p;
5363 :
5364 1082007 : if (fn)
5365 1080556 : *fn = NULL_TREE;
5366 : /* Set to (size_t)-1 if the size check fails. */
5367 1082007 : if (size_check != NULL_TREE)
5368 : {
5369 21070 : tree errval = TYPE_MAX_VALUE (sizetype);
5370 21070 : if (cxx_dialect >= cxx11 && flag_exceptions)
5371 20661 : errval = throw_bad_array_new_length ();
5372 21070 : *size = fold_build3 (COND_EXPR, sizetype, size_check,
5373 : original_size, errval);
5374 : }
5375 1082007 : vec_safe_insert (*args, 0, *size);
5376 1082007 : *args = resolve_args (*args, complain);
5377 1082007 : if (*args == NULL)
5378 0 : return error_mark_node;
5379 :
5380 1082007 : conversion_obstack_sentinel cos;
5381 :
5382 : /* Based on:
5383 :
5384 : [expr.new]
5385 :
5386 : If this lookup fails to find the name, or if the allocated type
5387 : is not a class type, the allocation function's name is looked
5388 : up in the global scope.
5389 :
5390 : we disregard block-scope declarations of "operator new". */
5391 1082007 : fns = lookup_qualified_name (global_namespace, fnname);
5392 :
5393 1082007 : if (align_arg)
5394 : {
5395 9425 : vec<tree, va_gc>* align_args
5396 9425 : = vec_copy_and_insert (*args, align_arg, 1);
5397 9425 : cand = perform_overload_resolution (fns, align_args, &candidates,
5398 : &any_viable_p, tf_none);
5399 9425 : if (cand)
5400 9370 : *args = align_args;
5401 : /* If no aligned allocation function matches, try again without the
5402 : alignment. */
5403 : }
5404 :
5405 : /* Figure out what function is being called. */
5406 9370 : if (!cand)
5407 1072637 : cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p,
5408 : complain);
5409 :
5410 : /* If no suitable function could be found, issue an error message
5411 : and give up. */
5412 1082007 : if (!cand)
5413 : {
5414 9 : if (complain & tf_error)
5415 9 : print_error_for_call_failure (fns, *args, candidates);
5416 9 : return error_mark_node;
5417 : }
5418 :
5419 : /* If a cookie is required, add some extra space. Whether
5420 : or not a cookie is required cannot be determined until
5421 : after we know which function was called. */
5422 1081998 : if (*cookie_size)
5423 : {
5424 268 : bool use_cookie = true;
5425 268 : tree arg_types;
5426 :
5427 268 : arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
5428 : /* Skip the size_t parameter. */
5429 268 : arg_types = TREE_CHAIN (arg_types);
5430 : /* Check the remaining parameters (if any). */
5431 268 : if (arg_types
5432 268 : && TREE_CHAIN (arg_types) == void_list_node
5433 331 : && same_type_p (TREE_VALUE (arg_types),
5434 : ptr_type_node))
5435 48 : use_cookie = false;
5436 : /* If we need a cookie, adjust the number of bytes allocated. */
5437 268 : if (use_cookie)
5438 : {
5439 : /* Update the total size. */
5440 220 : *size = size_binop (PLUS_EXPR, original_size, *cookie_size);
5441 220 : if (size_check)
5442 : {
5443 : /* Set to (size_t)-1 if the size check fails. */
5444 47 : gcc_assert (size_check != NULL_TREE);
5445 47 : *size = fold_build3 (COND_EXPR, sizetype, size_check,
5446 : *size, TYPE_MAX_VALUE (sizetype));
5447 : }
5448 : /* Update the argument list to reflect the adjusted size. */
5449 220 : (**args)[0] = *size;
5450 : }
5451 : else
5452 48 : *cookie_size = NULL_TREE;
5453 : }
5454 :
5455 : /* Tell our caller which function we decided to call. */
5456 1081998 : if (fn)
5457 1080547 : *fn = cand->fn;
5458 :
5459 : /* Build the CALL_EXPR. */
5460 1081998 : tree ret = build_over_call (cand, LOOKUP_NORMAL, complain);
5461 :
5462 : /* Set this flag for all callers of this function. In addition to
5463 : new-expressions, this is called for allocating coroutine state; treat
5464 : that as an implicit new-expression. */
5465 1081998 : tree call = extract_call_expr (ret);
5466 1081998 : if (TREE_CODE (call) == CALL_EXPR)
5467 1081998 : CALL_FROM_NEW_OR_DELETE_P (call) = 1;
5468 :
5469 : return ret;
5470 1082007 : }
5471 :
5472 : /* Evaluate side-effects from OBJ before evaluating call
5473 : to FN in RESULT expression.
5474 : This is for expressions of the form `obj->fn(...)'
5475 : where `fn' turns out to be a static member function and
5476 : `obj' needs to be evaluated. `fn' could be also static operator[]
5477 : or static operator(), in which cases the source expression
5478 : would be `obj[...]' or `obj(...)'. */
5479 :
5480 : tree
5481 80387494 : keep_unused_object_arg (tree result, tree obj, tree fn)
5482 : {
5483 80387494 : if (result == NULL_TREE
5484 80387494 : || result == error_mark_node
5485 79385784 : || DECL_OBJECT_MEMBER_FUNCTION_P (fn)
5486 83127403 : || !TREE_SIDE_EFFECTS (obj))
5487 : return result;
5488 :
5489 : /* But avoid the implicit lvalue-rvalue conversion when `obj' is
5490 : volatile. */
5491 89165 : tree a = obj;
5492 89165 : if (TREE_THIS_VOLATILE (a))
5493 57894 : a = build_this (a);
5494 89165 : if (TREE_SIDE_EFFECTS (a))
5495 31280 : return cp_build_compound_expr (a, result, tf_error);
5496 : return result;
5497 : }
5498 :
5499 : /* Build a new call to operator(). This may change ARGS. */
5500 :
5501 : tree
5502 2641775 : build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
5503 : {
5504 2641775 : struct z_candidate *candidates = 0, *cand;
5505 2641775 : tree fns, convs, first_mem_arg = NULL_TREE;
5506 2641775 : bool any_viable_p;
5507 2641775 : tree result = NULL_TREE;
5508 :
5509 2641775 : auto_cond_timevar tv (TV_OVERLOAD);
5510 :
5511 2641775 : obj = mark_lvalue_use (obj);
5512 :
5513 2641775 : if (error_operand_p (obj))
5514 0 : return error_mark_node;
5515 :
5516 2641775 : tree type = TREE_TYPE (obj);
5517 :
5518 2641775 : obj = prep_operand (obj);
5519 :
5520 2641775 : if (TYPE_PTRMEMFUNC_P (type))
5521 : {
5522 0 : if (complain & tf_error)
5523 : /* It's no good looking for an overloaded operator() on a
5524 : pointer-to-member-function. */
5525 0 : error ("pointer-to-member function %qE cannot be called without "
5526 : "an object; consider using %<.*%> or %<->*%>", obj);
5527 0 : return error_mark_node;
5528 : }
5529 :
5530 2641775 : if (TYPE_BINFO (type))
5531 : {
5532 2641751 : fns = lookup_fnfields (TYPE_BINFO (type), call_op_identifier, 1, complain);
5533 2641751 : if (fns == error_mark_node)
5534 : return error_mark_node;
5535 : }
5536 : else
5537 : fns = NULL_TREE;
5538 :
5539 2641765 : if (args != NULL && *args != NULL)
5540 : {
5541 2641765 : *args = resolve_args (*args, complain);
5542 2641765 : if (*args == NULL)
5543 110 : return error_mark_node;
5544 : }
5545 :
5546 2641655 : conversion_obstack_sentinel cos;
5547 :
5548 2641655 : if (fns)
5549 : {
5550 2636306 : first_mem_arg = obj;
5551 :
5552 2636306 : add_candidates (BASELINK_FUNCTIONS (fns),
5553 : first_mem_arg, *args, NULL_TREE,
5554 : NULL_TREE, false,
5555 2636306 : BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
5556 : LOOKUP_NORMAL, &candidates, complain);
5557 : }
5558 :
5559 2641655 : bool any_call_ops = candidates != nullptr;
5560 :
5561 2641655 : convs = lookup_conversions (type);
5562 :
5563 5475589 : for (; convs; convs = TREE_CHAIN (convs))
5564 : {
5565 192279 : tree totype = TREE_TYPE (convs);
5566 :
5567 158731 : if (TYPE_PTRFN_P (totype)
5568 33551 : || TYPE_REFFN_P (totype)
5569 225786 : || (TYPE_REF_P (totype)
5570 1188 : && TYPE_PTRFN_P (TREE_TYPE (totype))))
5571 317986 : for (tree fn : ovl_range (TREE_VALUE (convs)))
5572 : {
5573 158993 : if (DECL_NONCONVERTING_P (fn))
5574 3 : continue;
5575 :
5576 158990 : if (TREE_CODE (fn) == TEMPLATE_DECL)
5577 : {
5578 : /* Making this work broke PR 71117 and 85118, so until the
5579 : committee resolves core issue 2189, let's disable this
5580 : candidate if there are any call operators. */
5581 39582 : if (any_call_ops)
5582 39570 : continue;
5583 :
5584 12 : add_template_conv_candidate
5585 12 : (&candidates, fn, obj, *args, totype,
5586 : /*access_path=*/NULL_TREE,
5587 : /*conversion_path=*/NULL_TREE, complain);
5588 : }
5589 : else
5590 119408 : add_conv_candidate (&candidates, fn, obj,
5591 : *args, /*conversion_path=*/NULL_TREE,
5592 : /*access_path=*/NULL_TREE, complain);
5593 : }
5594 : }
5595 :
5596 : /* Be strict here because if we choose a bad conversion candidate, the
5597 : errors we get won't mention the call context. */
5598 2641655 : candidates = splice_viable (candidates, true, &any_viable_p);
5599 2641655 : if (!any_viable_p)
5600 : {
5601 40210 : if (complain & tf_error)
5602 : {
5603 282 : auto_diagnostic_group d;
5604 282 : error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
5605 : build_tree_list_vec (*args));
5606 282 : print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
5607 282 : }
5608 40210 : result = error_mark_node;
5609 : }
5610 : else
5611 : {
5612 2601445 : cand = tourney (candidates, complain);
5613 2601445 : if (cand == 0)
5614 : {
5615 22 : if (complain & tf_error)
5616 : {
5617 6 : auto_diagnostic_group d;
5618 12 : error ("call of %<(%T) (%A)%> is ambiguous",
5619 6 : TREE_TYPE (obj), build_tree_list_vec (*args));
5620 6 : print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
5621 6 : }
5622 22 : result = error_mark_node;
5623 : }
5624 2601423 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL
5625 2601364 : && DECL_OVERLOADED_OPERATOR_P (cand->fn)
5626 5202787 : && DECL_OVERLOADED_OPERATOR_IS (cand->fn, CALL_EXPR))
5627 : {
5628 2601364 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
5629 : /* In an expression of the form `a()' where cand->fn
5630 : which is operator() turns out to be a static member function,
5631 : `a' is none-the-less evaluated. */
5632 2601364 : result = keep_unused_object_arg (result, obj, cand->fn);
5633 : }
5634 : else
5635 : {
5636 59 : if (TREE_CODE (cand->fn) == FUNCTION_DECL)
5637 0 : obj = convert_like_with_context (cand->convs[0], obj, cand->fn,
5638 : -1, complain);
5639 : else
5640 : {
5641 59 : gcc_checking_assert (TYPE_P (cand->fn));
5642 59 : obj = convert_like (cand->convs[0], obj, complain);
5643 : }
5644 59 : obj = convert_from_reference (obj);
5645 59 : result = cp_build_function_call_vec (obj, args, complain);
5646 : }
5647 : }
5648 :
5649 2641655 : return result;
5650 2641775 : }
5651 :
5652 : /* Subroutine for preparing format strings suitable for the error
5653 : function. It concatenates a prefix (controlled by MATCH), ERRMSG,
5654 : and SUFFIX. */
5655 :
5656 : static const char *
5657 1474 : concat_op_error_string (bool match, const char *errmsg, const char *suffix)
5658 : {
5659 1474 : return concat (match
5660 : ? G_("ambiguous overload for ")
5661 : : G_("no match for "),
5662 0 : errmsg, suffix, nullptr);
5663 : }
5664 :
5665 : /* Called by op_error to prepare format strings suitable for the error
5666 : function. It concatenates a prefix (controlled by MATCH), ERRMSG,
5667 : and a suffix (controlled by NTYPES). */
5668 :
5669 : static const char *
5670 1453 : op_error_string (const char *errmsg, int ntypes, bool match)
5671 : {
5672 1453 : const char *suffix;
5673 0 : if (ntypes == 3)
5674 : suffix = G_(" (operand types are %qT, %qT, and %qT)");
5675 0 : else if (ntypes == 2)
5676 : suffix = G_(" (operand types are %qT and %qT)");
5677 : else
5678 0 : suffix = G_(" (operand type is %qT)");
5679 0 : return concat_op_error_string (match, errmsg, suffix);
5680 : }
5681 :
5682 : /* Similar to op_error_string, but a special-case for binary ops that
5683 : use %e for the args, rather than %qT. */
5684 :
5685 : static const char *
5686 21 : binop_error_string (const char *errmsg, bool match)
5687 : {
5688 21 : return concat_op_error_string (match, errmsg,
5689 21 : G_(" (operand types are %e and %e)"));
5690 : }
5691 :
5692 : static void
5693 1474 : op_error (const op_location_t &loc,
5694 : enum tree_code code, enum tree_code code2,
5695 : tree arg1, tree arg2, tree arg3, bool match)
5696 : {
5697 1474 : bool assop = code == MODIFY_EXPR;
5698 1474 : const char *opname = OVL_OP_INFO (assop, assop ? code2 : code)->name;
5699 :
5700 1474 : switch (code)
5701 : {
5702 0 : case COND_EXPR:
5703 0 : if (flag_diagnostics_show_caret)
5704 0 : error_at (loc, op_error_string (G_("ternary %<operator?:%>"),
5705 : 3, match),
5706 0 : TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
5707 : else
5708 0 : error_at (loc, op_error_string (G_("ternary %<operator?:%> "
5709 : "in %<%E ? %E : %E%>"), 3, match),
5710 : arg1, arg2, arg3,
5711 0 : TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
5712 : break;
5713 :
5714 0 : case POSTINCREMENT_EXPR:
5715 0 : case POSTDECREMENT_EXPR:
5716 0 : if (flag_diagnostics_show_caret)
5717 0 : error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
5718 0 : opname, TREE_TYPE (arg1));
5719 : else
5720 0 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
5721 : 1, match),
5722 0 : opname, arg1, opname, TREE_TYPE (arg1));
5723 : break;
5724 :
5725 21 : case ARRAY_REF:
5726 21 : if (flag_diagnostics_show_caret)
5727 0 : error_at (loc, op_error_string (G_("%<operator[]%>"), 2, match),
5728 0 : TREE_TYPE (arg1), TREE_TYPE (arg2));
5729 : else
5730 42 : error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
5731 : 2, match),
5732 21 : arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2));
5733 : break;
5734 :
5735 6 : case REALPART_EXPR:
5736 6 : case IMAGPART_EXPR:
5737 6 : if (flag_diagnostics_show_caret)
5738 0 : error_at (loc, op_error_string (G_("%qs"), 1, match),
5739 0 : opname, TREE_TYPE (arg1));
5740 : else
5741 12 : error_at (loc, op_error_string (G_("%qs in %<%s %E%>"), 1, match),
5742 6 : opname, opname, arg1, TREE_TYPE (arg1));
5743 : break;
5744 :
5745 0 : case CO_AWAIT_EXPR:
5746 0 : if (flag_diagnostics_show_caret)
5747 0 : error_at (loc, op_error_string (G_("%<operator %s%>"), 1, match),
5748 0 : opname, TREE_TYPE (arg1));
5749 : else
5750 0 : error_at (loc, op_error_string (G_("%<operator %s%> in %<%s%E%>"),
5751 : 1, match),
5752 0 : opname, opname, arg1, TREE_TYPE (arg1));
5753 : break;
5754 :
5755 1447 : default:
5756 1447 : if (arg2)
5757 1348 : if (flag_diagnostics_show_caret)
5758 : {
5759 21 : binary_op_rich_location richloc (loc, arg1, arg2, true);
5760 21 : pp_markup::element_quoted_type element_0
5761 21 : (TREE_TYPE (arg1), highlight_colors::lhs);
5762 21 : pp_markup::element_quoted_type element_1
5763 21 : (TREE_TYPE (arg2), highlight_colors::rhs);
5764 21 : error_at (&richloc,
5765 : binop_error_string (G_("%<operator%s%>"), match),
5766 : opname, &element_0, &element_1);
5767 21 : }
5768 : else
5769 2654 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
5770 : 2, match),
5771 : opname, arg1, opname, arg2,
5772 1327 : TREE_TYPE (arg1), TREE_TYPE (arg2));
5773 : else
5774 99 : if (flag_diagnostics_show_caret)
5775 0 : error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
5776 0 : opname, TREE_TYPE (arg1));
5777 : else
5778 198 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
5779 : 1, match),
5780 99 : opname, opname, arg1, TREE_TYPE (arg1));
5781 : break;
5782 : }
5783 1474 : }
5784 :
5785 : /* Return the implicit conversion sequence that could be used to
5786 : convert E1 to E2 in [expr.cond]. */
5787 :
5788 : static conversion *
5789 517792 : conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
5790 : {
5791 517792 : tree t1 = non_reference (TREE_TYPE (e1));
5792 517792 : tree t2 = non_reference (TREE_TYPE (e2));
5793 517792 : conversion *conv;
5794 517792 : bool good_base;
5795 :
5796 : /* [expr.cond]
5797 :
5798 : If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
5799 : implicitly converted (clause _conv_) to the type "lvalue reference to
5800 : T2", subject to the constraint that in the conversion the
5801 : reference must bind directly (_dcl.init.ref_) to an lvalue.
5802 :
5803 : If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
5804 : implicitly converted to the type "rvalue reference to T2", subject to
5805 : the constraint that the reference must bind directly. */
5806 517792 : if (glvalue_p (e2))
5807 : {
5808 507822 : tree rtype = cp_build_reference_type (t2, !lvalue_p (e2));
5809 507822 : conv = implicit_conversion (rtype,
5810 : t1,
5811 : e1,
5812 : /*c_cast_p=*/false,
5813 : LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
5814 : |LOOKUP_ONLYCONVERTING,
5815 : complain);
5816 507822 : if (conv && !conv->bad_p)
5817 : return conv;
5818 : }
5819 :
5820 : /* If E2 is a prvalue or if neither of the conversions above can be done
5821 : and at least one of the operands has (possibly cv-qualified) class
5822 : type: */
5823 407691 : if (!CLASS_TYPE_P (t1) && !CLASS_TYPE_P (t2))
5824 : return NULL;
5825 :
5826 : /* [expr.cond]
5827 :
5828 : If E1 and E2 have class type, and the underlying class types are
5829 : the same or one is a base class of the other: E1 can be converted
5830 : to match E2 if the class of T2 is the same type as, or a base
5831 : class of, the class of T1, and the cv-qualification of T2 is the
5832 : same cv-qualification as, or a greater cv-qualification than, the
5833 : cv-qualification of T1. If the conversion is applied, E1 is
5834 : changed to an rvalue of type T2 that still refers to the original
5835 : source class object (or the appropriate subobject thereof). */
5836 161233 : if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
5837 322573 : && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
5838 : {
5839 19607 : if (good_base && at_least_as_qualified_p (t2, t1))
5840 : {
5841 9692 : conv = build_identity_conv (t1, e1);
5842 9692 : if (!same_type_p (TYPE_MAIN_VARIANT (t1),
5843 : TYPE_MAIN_VARIANT (t2)))
5844 6 : conv = build_conv (ck_base, t2, conv);
5845 : else
5846 9686 : conv = build_conv (ck_rvalue, t2, conv);
5847 : return conv;
5848 : }
5849 : else
5850 : return NULL;
5851 : }
5852 : else
5853 : /* [expr.cond]
5854 :
5855 : Otherwise: E1 can be converted to match E2 if E1 can be implicitly
5856 : converted to the type that expression E2 would have if E2 were
5857 : converted to an rvalue (or the type it has, if E2 is an rvalue). */
5858 278336 : return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
5859 278336 : LOOKUP_IMPLICIT, complain);
5860 : }
5861 :
5862 : /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
5863 : arguments to the conditional expression. */
5864 :
5865 : tree
5866 7991373 : build_conditional_expr (const op_location_t &loc,
5867 : tree arg1, tree arg2, tree arg3,
5868 : tsubst_flags_t complain)
5869 : {
5870 7991373 : tree arg2_type;
5871 7991373 : tree arg3_type;
5872 7991373 : tree result = NULL_TREE;
5873 7991373 : tree result_type = NULL_TREE;
5874 7991373 : tree semantic_result_type = NULL_TREE;
5875 7991373 : bool is_glvalue = true;
5876 7991373 : struct z_candidate *candidates = 0;
5877 7991373 : struct z_candidate *cand;
5878 7991373 : tree orig_arg2, orig_arg3;
5879 :
5880 7991373 : auto_cond_timevar tv (TV_OVERLOAD);
5881 :
5882 : /* As a G++ extension, the second argument to the conditional can be
5883 : omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
5884 : c'.) If the second operand is omitted, make sure it is
5885 : calculated only once. */
5886 7991373 : if (!arg2)
5887 : {
5888 386 : if (complain & tf_error)
5889 380 : pedwarn (loc, OPT_Wpedantic,
5890 : "ISO C++ forbids omitting the middle term of "
5891 : "a %<?:%> expression");
5892 :
5893 386 : if ((complain & tf_warning) && !truth_value_p (TREE_CODE (arg1)))
5894 338 : warn_for_omitted_condop (loc, arg1);
5895 :
5896 : /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
5897 386 : if (glvalue_p (arg1))
5898 : {
5899 95 : arg1 = cp_stabilize_reference (arg1);
5900 95 : arg2 = arg1 = prevent_lifetime_extension (arg1);
5901 : }
5902 291 : else if (TREE_CODE (arg1) == TARGET_EXPR)
5903 : /* arg1 can't be a prvalue result of the conditional
5904 : expression, since it needs to be materialized for the
5905 : conversion to bool, so treat it as an xvalue in arg2. */
5906 6 : arg2 = move (TARGET_EXPR_SLOT (arg1));
5907 285 : else if (TREE_CODE (arg1) == EXCESS_PRECISION_EXPR)
5908 3 : arg2 = arg1 = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (arg1),
5909 3 : cp_save_expr (TREE_OPERAND (arg1, 0)));
5910 : else
5911 282 : arg2 = arg1 = cp_save_expr (arg1);
5912 : }
5913 :
5914 : /* If something has already gone wrong, just pass that fact up the
5915 : tree. */
5916 7991373 : if (error_operand_p (arg1)
5917 7991294 : || error_operand_p (arg2)
5918 15982617 : || error_operand_p (arg3))
5919 180 : return error_mark_node;
5920 :
5921 7991193 : conversion_obstack_sentinel cos;
5922 :
5923 7991193 : orig_arg2 = arg2;
5924 7991193 : orig_arg3 = arg3;
5925 :
5926 7991193 : if (gnu_vector_type_p (TREE_TYPE (arg1))
5927 7991193 : && (VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1))
5928 3 : || VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (arg1))))
5929 : {
5930 1860 : tree arg1_type = TREE_TYPE (arg1);
5931 :
5932 : /* If arg1 is another cond_expr choosing between -1 and 0,
5933 : then we can use its comparison. It may help to avoid
5934 : additional comparison, produce more accurate diagnostics
5935 : and enables folding. */
5936 1860 : if (TREE_CODE (arg1) == VEC_COND_EXPR
5937 1587 : && integer_minus_onep (TREE_OPERAND (arg1, 1))
5938 3447 : && integer_zerop (TREE_OPERAND (arg1, 2)))
5939 1587 : arg1 = TREE_OPERAND (arg1, 0);
5940 :
5941 1860 : arg1 = force_rvalue (arg1, complain);
5942 1860 : arg2 = force_rvalue (arg2, complain);
5943 1860 : arg3 = force_rvalue (arg3, complain);
5944 :
5945 : /* force_rvalue can return error_mark on valid arguments. */
5946 1860 : if (error_operand_p (arg1)
5947 1860 : || error_operand_p (arg2)
5948 3720 : || error_operand_p (arg3))
5949 0 : return error_mark_node;
5950 :
5951 1860 : arg2_type = TREE_TYPE (arg2);
5952 1860 : arg3_type = TREE_TYPE (arg3);
5953 :
5954 1860 : if (!VECTOR_TYPE_P (arg2_type)
5955 559 : && !VECTOR_TYPE_P (arg3_type))
5956 : {
5957 : /* Rely on the error messages of the scalar version. */
5958 541 : tree scal = build_conditional_expr (loc, integer_one_node,
5959 : orig_arg2, orig_arg3, complain);
5960 541 : if (scal == error_mark_node)
5961 : return error_mark_node;
5962 541 : tree stype = TREE_TYPE (scal);
5963 541 : tree ctype = TREE_TYPE (arg1_type);
5964 541 : if (TYPE_SIZE (stype) != TYPE_SIZE (ctype)
5965 541 : || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype)))
5966 : {
5967 9 : if (complain & tf_error)
5968 9 : error_at (loc, "inferred scalar type %qT is not an integer or "
5969 : "floating-point type of the same size as %qT", stype,
5970 9 : COMPARISON_CLASS_P (arg1)
5971 9 : ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0)))
5972 : : ctype);
5973 9 : return error_mark_node;
5974 : }
5975 :
5976 532 : tree vtype = build_opaque_vector_type (stype,
5977 532 : TYPE_VECTOR_SUBPARTS (arg1_type));
5978 : /* We could pass complain & tf_warning to unsafe_conversion_p,
5979 : but the warnings (like Wsign-conversion) have already been
5980 : given by the scalar build_conditional_expr_1. We still check
5981 : unsafe_conversion_p to forbid truncating long long -> float. */
5982 532 : if (unsafe_conversion_p (stype, arg2, NULL_TREE, false))
5983 : {
5984 0 : if (complain & tf_error)
5985 0 : error_at (loc, "conversion of scalar %qH to vector %qI "
5986 : "involves truncation", arg2_type, vtype);
5987 0 : return error_mark_node;
5988 : }
5989 532 : if (unsafe_conversion_p (stype, arg3, NULL_TREE, false))
5990 : {
5991 3 : if (complain & tf_error)
5992 3 : error_at (loc, "conversion of scalar %qH to vector %qI "
5993 : "involves truncation", arg3_type, vtype);
5994 3 : return error_mark_node;
5995 : }
5996 :
5997 529 : arg2 = cp_convert (stype, arg2, complain);
5998 529 : arg2 = save_expr (arg2);
5999 529 : arg2 = build_vector_from_val (vtype, arg2);
6000 529 : arg2_type = vtype;
6001 529 : arg3 = cp_convert (stype, arg3, complain);
6002 529 : arg3 = save_expr (arg3);
6003 529 : arg3 = build_vector_from_val (vtype, arg3);
6004 529 : arg3_type = vtype;
6005 : }
6006 :
6007 3678 : if ((gnu_vector_type_p (arg2_type) && !VECTOR_TYPE_P (arg3_type))
6008 3600 : || (gnu_vector_type_p (arg3_type) && !VECTOR_TYPE_P (arg2_type)))
6009 : {
6010 96 : enum stv_conv convert_flag =
6011 96 : scalar_to_vector (loc, VEC_COND_EXPR, arg2, arg3,
6012 : complain & tf_error);
6013 :
6014 96 : switch (convert_flag)
6015 : {
6016 0 : case stv_error:
6017 0 : return error_mark_node;
6018 15 : case stv_firstarg:
6019 15 : {
6020 15 : arg2 = save_expr (arg2);
6021 15 : arg2 = convert (TREE_TYPE (arg3_type), arg2);
6022 15 : arg2 = build_vector_from_val (arg3_type, arg2);
6023 15 : arg2_type = TREE_TYPE (arg2);
6024 15 : break;
6025 : }
6026 72 : case stv_secondarg:
6027 72 : {
6028 72 : arg3 = save_expr (arg3);
6029 72 : arg3 = convert (TREE_TYPE (arg2_type), arg3);
6030 72 : arg3 = build_vector_from_val (arg2_type, arg3);
6031 72 : arg3_type = TREE_TYPE (arg3);
6032 72 : break;
6033 : }
6034 : default:
6035 : break;
6036 : }
6037 : }
6038 :
6039 1848 : if (!gnu_vector_type_p (arg2_type)
6040 1845 : || !gnu_vector_type_p (arg3_type)
6041 1839 : || !same_type_p (arg2_type, arg3_type)
6042 1839 : || maybe_ne (TYPE_VECTOR_SUBPARTS (arg1_type),
6043 1848 : TYPE_VECTOR_SUBPARTS (arg2_type))
6044 3687 : || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type))
6045 : {
6046 9 : if (complain & tf_error)
6047 6 : error_at (loc,
6048 : "incompatible vector types in conditional expression: "
6049 6 : "%qT, %qT and %qT", TREE_TYPE (arg1),
6050 6 : TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3));
6051 9 : return error_mark_node;
6052 : }
6053 :
6054 1839 : if (!COMPARISON_CLASS_P (arg1))
6055 : {
6056 270 : tree cmp_type = truth_type_for (arg1_type);
6057 270 : arg1 = build2 (NE_EXPR, cmp_type, arg1, build_zero_cst (arg1_type));
6058 : }
6059 1839 : return build3_loc (loc, VEC_COND_EXPR, arg2_type, arg1, arg2, arg3);
6060 : }
6061 :
6062 : /* [expr.cond]
6063 :
6064 : The first expression is implicitly converted to bool (clause
6065 : _conv_). */
6066 7989333 : arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
6067 : LOOKUP_NORMAL);
6068 7989333 : if (error_operand_p (arg1))
6069 25 : return error_mark_node;
6070 :
6071 7989308 : arg2_type = unlowered_expr_type (arg2);
6072 7989308 : arg3_type = unlowered_expr_type (arg3);
6073 :
6074 7989308 : if ((TREE_CODE (arg2) == EXCESS_PRECISION_EXPR
6075 7989290 : || TREE_CODE (arg3) == EXCESS_PRECISION_EXPR)
6076 45 : && (TREE_CODE (arg2_type) == INTEGER_TYPE
6077 : || SCALAR_FLOAT_TYPE_P (arg2_type)
6078 : || TREE_CODE (arg2_type) == COMPLEX_TYPE)
6079 45 : && (TREE_CODE (arg3_type) == INTEGER_TYPE
6080 : || SCALAR_FLOAT_TYPE_P (arg3_type)
6081 : || TREE_CODE (arg3_type) == COMPLEX_TYPE))
6082 : {
6083 45 : semantic_result_type
6084 45 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
6085 45 : if (semantic_result_type == error_mark_node)
6086 : {
6087 0 : tree t1 = arg2_type;
6088 0 : tree t2 = arg3_type;
6089 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6090 0 : t1 = TREE_TYPE (t1);
6091 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6092 0 : t2 = TREE_TYPE (t2);
6093 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
6094 : && SCALAR_FLOAT_TYPE_P (t2)
6095 : && (extended_float_type_p (t1)
6096 : || extended_float_type_p (t2))
6097 : && cp_compare_floating_point_conversion_ranks
6098 : (t1, t2) == 3);
6099 0 : if (complain & tf_error)
6100 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
6101 : "have unordered conversion rank",
6102 : arg2_type, arg3_type);
6103 0 : return error_mark_node;
6104 : }
6105 45 : if (TREE_CODE (arg2) == EXCESS_PRECISION_EXPR)
6106 : {
6107 18 : arg2 = TREE_OPERAND (arg2, 0);
6108 18 : arg2_type = TREE_TYPE (arg2);
6109 : }
6110 45 : if (TREE_CODE (arg3) == EXCESS_PRECISION_EXPR)
6111 : {
6112 27 : arg3 = TREE_OPERAND (arg3, 0);
6113 27 : arg3_type = TREE_TYPE (arg3);
6114 : }
6115 : }
6116 :
6117 : /* [expr.cond]
6118 :
6119 : If either the second or the third operand has type (possibly
6120 : cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
6121 : array-to-pointer (_conv.array_), and function-to-pointer
6122 : (_conv.func_) standard conversions are performed on the second
6123 : and third operands. */
6124 7989308 : if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
6125 : {
6126 : /* 'void' won't help in resolving an overloaded expression on the
6127 : other side, so require it to resolve by itself. */
6128 128481 : if (arg2_type == unknown_type_node)
6129 : {
6130 18 : arg2 = resolve_nondeduced_context_or_error (arg2, complain);
6131 18 : arg2_type = TREE_TYPE (arg2);
6132 : }
6133 128481 : if (arg3_type == unknown_type_node)
6134 : {
6135 0 : arg3 = resolve_nondeduced_context_or_error (arg3, complain);
6136 0 : arg3_type = TREE_TYPE (arg3);
6137 : }
6138 :
6139 : /* [expr.cond]
6140 :
6141 : One of the following shall hold:
6142 :
6143 : --The second or the third operand (but not both) is a
6144 : throw-expression (_except.throw_); the result is of the type
6145 : and value category of the other.
6146 :
6147 : --Both the second and the third operands have type void; the
6148 : result is of type void and is a prvalue. */
6149 128481 : if (TREE_CODE (arg2) == THROW_EXPR
6150 84 : && TREE_CODE (arg3) != THROW_EXPR)
6151 : {
6152 72 : result_type = arg3_type;
6153 72 : is_glvalue = glvalue_p (arg3);
6154 : }
6155 128409 : else if (TREE_CODE (arg2) != THROW_EXPR
6156 128397 : && TREE_CODE (arg3) == THROW_EXPR)
6157 : {
6158 180 : result_type = arg2_type;
6159 180 : is_glvalue = glvalue_p (arg2);
6160 : }
6161 128229 : else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
6162 : {
6163 128200 : result_type = void_type_node;
6164 128200 : is_glvalue = false;
6165 : }
6166 : else
6167 : {
6168 29 : if (complain & tf_error)
6169 : {
6170 18 : if (VOID_TYPE_P (arg2_type))
6171 9 : error_at (cp_expr_loc_or_loc (arg3, loc),
6172 : "second operand to the conditional operator "
6173 : "is of type %<void%>, but the third operand is "
6174 : "neither a throw-expression nor of type %<void%>");
6175 : else
6176 9 : error_at (cp_expr_loc_or_loc (arg2, loc),
6177 : "third operand to the conditional operator "
6178 : "is of type %<void%>, but the second operand is "
6179 : "neither a throw-expression nor of type %<void%>");
6180 : }
6181 29 : return error_mark_node;
6182 : }
6183 :
6184 128452 : goto valid_operands;
6185 : }
6186 : /* [expr.cond]
6187 :
6188 : Otherwise, if the second and third operand have different types,
6189 : and either has (possibly cv-qualified) class type, or if both are
6190 : glvalues of the same value category and the same type except for
6191 : cv-qualification, an attempt is made to convert each of those operands
6192 : to the type of the other. */
6193 7860827 : else if (!same_type_p (arg2_type, arg3_type)
6194 7860827 : && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)
6195 1575275 : || (same_type_ignoring_top_level_qualifiers_p (arg2_type,
6196 : arg3_type)
6197 448957 : && glvalue_p (arg2) && glvalue_p (arg3)
6198 109818 : && lvalue_p (arg2) == lvalue_p (arg3))))
6199 : {
6200 258896 : conversion *conv2;
6201 258896 : conversion *conv3;
6202 258896 : bool converted = false;
6203 :
6204 258896 : conv2 = conditional_conversion (arg2, arg3, complain);
6205 258896 : conv3 = conditional_conversion (arg3, arg2, complain);
6206 :
6207 : /* [expr.cond]
6208 :
6209 : If both can be converted, or one can be converted but the
6210 : conversion is ambiguous, the program is ill-formed. If
6211 : neither can be converted, the operands are left unchanged and
6212 : further checking is performed as described below. If exactly
6213 : one conversion is possible, that conversion is applied to the
6214 : chosen operand and the converted operand is used in place of
6215 : the original operand for the remainder of this section. */
6216 258896 : if ((conv2 && !conv2->bad_p
6217 47384 : && conv3 && !conv3->bad_p)
6218 47362 : || (conv2 && conv2->kind == ck_ambig)
6219 258774 : || (conv3 && conv3->kind == ck_ambig))
6220 : {
6221 122 : if (complain & tf_error)
6222 : {
6223 6 : error_at (loc, "operands to %<?:%> have different types "
6224 : "%qT and %qT",
6225 : arg2_type, arg3_type);
6226 6 : if (conv2 && !conv2->bad_p && conv3 && !conv3->bad_p)
6227 3 : inform (loc, " and each type can be converted to the other");
6228 3 : else if (conv2 && conv2->kind == ck_ambig)
6229 3 : convert_like (conv2, arg2, complain);
6230 : else
6231 0 : convert_like (conv3, arg3, complain);
6232 : }
6233 122 : result = error_mark_node;
6234 : }
6235 258774 : else if (conv2 && !conv2->bad_p)
6236 : {
6237 47262 : arg2 = convert_like (conv2, arg2, complain);
6238 47262 : arg2 = convert_from_reference (arg2);
6239 47262 : arg2_type = TREE_TYPE (arg2);
6240 : /* Even if CONV2 is a valid conversion, the result of the
6241 : conversion may be invalid. For example, if ARG3 has type
6242 : "volatile X", and X does not have a copy constructor
6243 : accepting a "volatile X&", then even if ARG2 can be
6244 : converted to X, the conversion will fail. */
6245 47262 : if (error_operand_p (arg2))
6246 8 : result = error_mark_node;
6247 : converted = true;
6248 : }
6249 211512 : else if (conv3 && !conv3->bad_p)
6250 : {
6251 210774 : arg3 = convert_like (conv3, arg3, complain);
6252 210774 : arg3 = convert_from_reference (arg3);
6253 210774 : arg3_type = TREE_TYPE (arg3);
6254 210774 : if (error_operand_p (arg3))
6255 0 : result = error_mark_node;
6256 : converted = true;
6257 : }
6258 :
6259 130 : if (result)
6260 : return result;
6261 :
6262 : /* If, after the conversion, both operands have class type,
6263 : treat the cv-qualification of both operands as if it were the
6264 : union of the cv-qualification of the operands.
6265 :
6266 : The standard is not clear about what to do in this
6267 : circumstance. For example, if the first operand has type
6268 : "const X" and the second operand has a user-defined
6269 : conversion to "volatile X", what is the type of the second
6270 : operand after this step? Making it be "const X" (matching
6271 : the first operand) seems wrong, as that discards the
6272 : qualification without actually performing a copy. Leaving it
6273 : as "volatile X" seems wrong as that will result in the
6274 : conditional expression failing altogether, even though,
6275 : according to this step, the one operand could be converted to
6276 : the type of the other. */
6277 258766 : if (converted
6278 258028 : && CLASS_TYPE_P (arg2_type)
6279 270767 : && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
6280 0 : arg2_type = arg3_type =
6281 0 : cp_build_qualified_type (arg2_type,
6282 0 : cp_type_quals (arg2_type)
6283 0 : | cp_type_quals (arg3_type));
6284 : }
6285 :
6286 : /* [expr.cond]
6287 :
6288 : If the second and third operands are glvalues of the same value
6289 : category and have the same type, the result is of that type and
6290 : value category. */
6291 11667390 : if (((lvalue_p (arg2) && lvalue_p (arg3))
6292 4686882 : || (xvalue_p (arg2) && xvalue_p (arg3)))
6293 11083625 : && same_type_p (arg2_type, arg3_type))
6294 : {
6295 3127496 : result_type = arg2_type;
6296 3127496 : goto valid_operands;
6297 : }
6298 :
6299 : /* [expr.cond]
6300 :
6301 : Otherwise, the result is an rvalue. If the second and third
6302 : operand do not have the same type, and either has (possibly
6303 : cv-qualified) class type, overload resolution is used to
6304 : determine the conversions (if any) to be applied to the operands
6305 : (_over.match.oper_, _over.built_). */
6306 4733201 : is_glvalue = false;
6307 4733201 : if (!same_type_p (arg2_type, arg3_type)
6308 4733201 : && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
6309 : {
6310 738 : releasing_vec args;
6311 738 : conversion *conv;
6312 738 : bool any_viable_p;
6313 :
6314 : /* Rearrange the arguments so that add_builtin_candidate only has
6315 : to know about two args. In build_builtin_candidate, the
6316 : arguments are unscrambled. */
6317 738 : args->quick_push (arg2);
6318 738 : args->quick_push (arg3);
6319 738 : args->quick_push (arg1);
6320 738 : add_builtin_candidates (&candidates,
6321 : COND_EXPR,
6322 : NOP_EXPR,
6323 : ovl_op_identifier (false, COND_EXPR),
6324 : args,
6325 : LOOKUP_NORMAL, complain);
6326 :
6327 : /* [expr.cond]
6328 :
6329 : If the overload resolution fails, the program is
6330 : ill-formed. */
6331 738 : candidates = splice_viable (candidates, false, &any_viable_p);
6332 738 : if (!any_viable_p)
6333 : {
6334 615 : if (complain & tf_error)
6335 22 : error_at (loc, "operands to %<?:%> have different types %qT and %qT",
6336 : arg2_type, arg3_type);
6337 615 : return error_mark_node;
6338 : }
6339 123 : cand = tourney (candidates, complain);
6340 123 : if (!cand)
6341 : {
6342 0 : if (complain & tf_error)
6343 : {
6344 0 : auto_diagnostic_group d;
6345 0 : op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, false);
6346 0 : print_z_candidates (loc, candidates);
6347 0 : }
6348 0 : return error_mark_node;
6349 : }
6350 :
6351 : /* [expr.cond]
6352 :
6353 : Otherwise, the conversions thus determined are applied, and
6354 : the converted operands are used in place of the original
6355 : operands for the remainder of this section. */
6356 123 : conv = cand->convs[0];
6357 123 : arg1 = convert_like (conv, arg1, complain);
6358 123 : conv = cand->convs[1];
6359 123 : arg2 = convert_like (conv, arg2, complain);
6360 123 : arg2_type = TREE_TYPE (arg2);
6361 123 : conv = cand->convs[2];
6362 123 : arg3 = convert_like (conv, arg3, complain);
6363 123 : arg3_type = TREE_TYPE (arg3);
6364 738 : }
6365 :
6366 : /* [expr.cond]
6367 :
6368 : Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
6369 : and function-to-pointer (_conv.func_) standard conversions are
6370 : performed on the second and third operands.
6371 :
6372 : We need to force the lvalue-to-rvalue conversion here for class types,
6373 : so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
6374 : that isn't wrapped with a TARGET_EXPR plays havoc with exception
6375 : regions. */
6376 :
6377 4732586 : arg2 = force_rvalue (arg2, complain);
6378 4732586 : if (!CLASS_TYPE_P (arg2_type))
6379 4640502 : arg2_type = TREE_TYPE (arg2);
6380 :
6381 4732586 : arg3 = force_rvalue (arg3, complain);
6382 4732586 : if (!CLASS_TYPE_P (arg3_type))
6383 4640502 : arg3_type = TREE_TYPE (arg3);
6384 :
6385 4732586 : if (arg2 == error_mark_node || arg3 == error_mark_node)
6386 : return error_mark_node;
6387 :
6388 : /* [expr.cond]
6389 :
6390 : After those conversions, one of the following shall hold:
6391 :
6392 : --The second and third operands have the same type; the result is of
6393 : that type. */
6394 4732538 : if (same_type_p (arg2_type, arg3_type))
6395 : result_type = arg2_type;
6396 : /* [expr.cond]
6397 :
6398 : --The second and third operands have arithmetic or enumeration
6399 : type; the usual arithmetic conversions are performed to bring
6400 : them to a common type, and the result is of that type. */
6401 1033582 : else if ((ARITHMETIC_TYPE_P (arg2_type)
6402 106 : || UNSCOPED_ENUM_P (arg2_type))
6403 1010631 : && (ARITHMETIC_TYPE_P (arg3_type)
6404 71 : || UNSCOPED_ENUM_P (arg3_type)))
6405 : {
6406 : /* A conditional expression between a floating-point
6407 : type and an integer type should convert the integer type to
6408 : the evaluation format of the floating-point type, with
6409 : possible excess precision. */
6410 1010517 : tree eptype2 = arg2_type;
6411 1010517 : tree eptype3 = arg3_type;
6412 1010517 : tree eptype;
6413 831 : if (ANY_INTEGRAL_TYPE_P (arg2_type)
6414 1010520 : && (eptype = excess_precision_type (arg3_type)) != NULL_TREE)
6415 : {
6416 15 : eptype3 = eptype;
6417 15 : if (!semantic_result_type)
6418 15 : semantic_result_type
6419 15 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
6420 : }
6421 505 : else if (ANY_INTEGRAL_TYPE_P (arg3_type)
6422 1010502 : && (eptype = excess_precision_type (arg2_type)) != NULL_TREE)
6423 : {
6424 27 : eptype2 = eptype;
6425 27 : if (!semantic_result_type)
6426 27 : semantic_result_type
6427 27 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
6428 : }
6429 1010517 : result_type = type_after_usual_arithmetic_conversions (eptype2,
6430 : eptype3);
6431 1010517 : if (result_type == error_mark_node)
6432 : {
6433 0 : tree t1 = eptype2;
6434 0 : tree t2 = eptype3;
6435 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6436 0 : t1 = TREE_TYPE (t1);
6437 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6438 0 : t2 = TREE_TYPE (t2);
6439 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
6440 : && SCALAR_FLOAT_TYPE_P (t2)
6441 : && (extended_float_type_p (t1)
6442 : || extended_float_type_p (t2))
6443 : && cp_compare_floating_point_conversion_ranks
6444 : (t1, t2) == 3);
6445 0 : if (complain & tf_error)
6446 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
6447 : "have unordered conversion rank",
6448 : eptype2, eptype3);
6449 0 : return error_mark_node;
6450 : }
6451 1010517 : if (semantic_result_type == error_mark_node)
6452 : {
6453 0 : tree t1 = arg2_type;
6454 0 : tree t2 = arg3_type;
6455 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6456 0 : t1 = TREE_TYPE (t1);
6457 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6458 0 : t2 = TREE_TYPE (t2);
6459 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
6460 : && SCALAR_FLOAT_TYPE_P (t2)
6461 : && (extended_float_type_p (t1)
6462 : || extended_float_type_p (t2))
6463 : && cp_compare_floating_point_conversion_ranks
6464 : (t1, t2) == 3);
6465 0 : if (complain & tf_error)
6466 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
6467 : "have unordered conversion rank",
6468 : arg2_type, arg3_type);
6469 0 : return error_mark_node;
6470 : }
6471 :
6472 1010517 : if (complain & tf_warning)
6473 957081 : do_warn_double_promotion (result_type, arg2_type, arg3_type,
6474 : "implicit conversion from %qH to %qI to "
6475 : "match other result of conditional",
6476 : loc);
6477 :
6478 1010517 : if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
6479 102 : && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
6480 : {
6481 35 : tree stripped_orig_arg2 = tree_strip_any_location_wrapper (orig_arg2);
6482 35 : tree stripped_orig_arg3 = tree_strip_any_location_wrapper (orig_arg3);
6483 35 : if (TREE_CODE (stripped_orig_arg2) == CONST_DECL
6484 23 : && TREE_CODE (stripped_orig_arg3) == CONST_DECL
6485 58 : && (DECL_CONTEXT (stripped_orig_arg2)
6486 23 : == DECL_CONTEXT (stripped_orig_arg3)))
6487 : /* Two enumerators from the same enumeration can have different
6488 : types when the enumeration is still being defined. */;
6489 29 : else if (complain & (cxx_dialect >= cxx26
6490 29 : ? tf_warning_or_error : tf_warning))
6491 44 : emit_diagnostic ((cxx_dialect >= cxx26
6492 : ? diagnostics::kind::pedwarn
6493 : : diagnostics::kind::warning),
6494 26 : loc, OPT_Wenum_compare, "enumerated mismatch "
6495 : "in conditional expression: %qT vs %qT",
6496 : arg2_type, arg3_type);
6497 3 : else if (cxx_dialect >= cxx26)
6498 2 : return error_mark_node;
6499 : }
6500 1010482 : else if ((((complain & (cxx_dialect >= cxx26
6501 1010482 : ? tf_warning_or_error : tf_warning))
6502 961450 : && warn_deprecated_enum_float_conv)
6503 64555 : || (cxx_dialect >= cxx26
6504 2147 : && (complain & tf_warning_or_error) == 0))
6505 947971 : && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
6506 40 : && SCALAR_FLOAT_TYPE_P (arg3_type))
6507 947958 : || (SCALAR_FLOAT_TYPE_P (arg2_type)
6508 354 : && TREE_CODE (arg3_type) == ENUMERAL_TYPE)))
6509 : {
6510 23 : if (cxx_dialect >= cxx26 && (complain & tf_warning_or_error) == 0)
6511 4 : return error_mark_node;
6512 19 : if (cxx_dialect >= cxx26 && TREE_CODE (arg2_type) == ENUMERAL_TYPE)
6513 5 : pedwarn (loc, OPT_Wdeprecated_enum_float_conversion,
6514 : "conditional expression between enumeration type "
6515 : "%qT and floating-point type %qT", arg2_type, arg3_type);
6516 14 : else if (cxx_dialect >= cxx26)
6517 4 : pedwarn (loc, OPT_Wdeprecated_enum_float_conversion,
6518 : "conditional expression between floating-point type "
6519 : "%qT and enumeration type %qT", arg2_type, arg3_type);
6520 10 : else if (TREE_CODE (arg2_type) == ENUMERAL_TYPE)
6521 6 : warning_at (loc, OPT_Wdeprecated_enum_float_conversion,
6522 : "conditional expression between enumeration type "
6523 : "%qT and floating-point type %qT is deprecated",
6524 : arg2_type, arg3_type);
6525 : else
6526 4 : warning_at (loc, OPT_Wdeprecated_enum_float_conversion,
6527 : "conditional expression between floating-point "
6528 : "type %qT and enumeration type %qT is deprecated",
6529 : arg2_type, arg3_type);
6530 : }
6531 1000691 : else if ((extra_warnings || warn_enum_conversion)
6532 1010464 : && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
6533 15 : && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
6534 9761 : || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
6535 8 : && !same_type_p (arg2_type,
6536 : type_promotes_to (arg3_type)))))
6537 : {
6538 17 : if (complain & tf_warning)
6539 : {
6540 12 : enum opt_code opt = (warn_enum_conversion
6541 17 : ? OPT_Wenum_conversion
6542 : : OPT_Wextra);
6543 17 : warning_at (loc, opt, "enumerated and "
6544 : "non-enumerated type in conditional expression");
6545 : }
6546 : }
6547 :
6548 1010511 : arg2 = perform_implicit_conversion (result_type, arg2, complain);
6549 1010511 : arg3 = perform_implicit_conversion (result_type, arg3, complain);
6550 : }
6551 : /* [expr.cond]
6552 :
6553 : --The second and third operands have pointer type, or one has
6554 : pointer type and the other is a null pointer constant; pointer
6555 : conversions (_conv.ptr_) and qualification conversions
6556 : (_conv.qual_) are performed to bring them to their composite
6557 : pointer type (_expr.rel_). The result is of the composite
6558 : pointer type.
6559 :
6560 : --The second and third operands have pointer to member type, or
6561 : one has pointer to member type and the other is a null pointer
6562 : constant; pointer to member conversions (_conv.mem_) and
6563 : qualification conversions (_conv.qual_) are performed to bring
6564 : them to a common type, whose cv-qualification shall match the
6565 : cv-qualification of either the second or the third operand.
6566 : The result is of the common type. */
6567 23065 : else if ((null_ptr_cst_p (arg2)
6568 184 : && TYPE_PTR_OR_PTRMEM_P (arg3_type))
6569 22881 : || (null_ptr_cst_p (arg3)
6570 21917 : && TYPE_PTR_OR_PTRMEM_P (arg2_type))
6571 964 : || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
6572 99 : || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type))
6573 23141 : || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
6574 : {
6575 23011 : result_type = composite_pointer_type (loc,
6576 : arg2_type, arg3_type, arg2,
6577 : arg3, CPO_CONDITIONAL_EXPR,
6578 : complain);
6579 23011 : if (result_type == error_mark_node)
6580 : return error_mark_node;
6581 22985 : arg2 = perform_implicit_conversion (result_type, arg2, complain);
6582 22985 : arg3 = perform_implicit_conversion (result_type, arg3, complain);
6583 : }
6584 :
6585 4732452 : if (!result_type)
6586 : {
6587 54 : if (complain & tf_error)
6588 3 : error_at (loc, "operands to %<?:%> have different types %qT and %qT",
6589 : arg2_type, arg3_type);
6590 54 : return error_mark_node;
6591 : }
6592 :
6593 4732452 : if (arg2 == error_mark_node || arg3 == error_mark_node)
6594 : return error_mark_node;
6595 :
6596 4732449 : valid_operands:
6597 7988397 : if (processing_template_decl && is_glvalue)
6598 : {
6599 : /* Let lvalue_kind know this was a glvalue. */
6600 114617 : tree arg = (result_type == arg2_type ? arg2 : arg3);
6601 114617 : result_type = cp_build_reference_type (result_type, xvalue_p (arg));
6602 : }
6603 :
6604 7988397 : result = build3_loc (loc, COND_EXPR, result_type, arg1, arg2, arg3);
6605 :
6606 : /* If the ARG2 and ARG3 are the same and don't have side-effects,
6607 : warn here, because the COND_EXPR will be turned into ARG2. */
6608 7988397 : if (warn_duplicated_branches
6609 153 : && (complain & tf_warning)
6610 7988550 : && (arg2 == arg3 || operand_equal_p (arg2, arg3,
6611 : OEP_ADDRESS_OF_SAME_FIELD)))
6612 57 : warning_at (EXPR_LOCATION (result), OPT_Wduplicated_branches,
6613 : "this condition has identical branches");
6614 :
6615 : /* We can't use result_type below, as fold might have returned a
6616 : throw_expr. */
6617 :
6618 7988397 : if (!is_glvalue)
6619 : {
6620 : /* Expand both sides into the same slot, hopefully the target of
6621 : the ?: expression. We used to check for TARGET_EXPRs here,
6622 : but now we sometimes wrap them in NOP_EXPRs so the test would
6623 : fail. */
6624 4860832 : if (CLASS_TYPE_P (TREE_TYPE (result)))
6625 : {
6626 92121 : result = get_target_expr (result, complain);
6627 : /* Tell gimplify_modify_expr_rhs not to strip this in
6628 : assignment context: we want both arms to initialize
6629 : the same temporary. */
6630 92121 : TARGET_EXPR_NO_ELIDE (result) = true;
6631 : }
6632 : /* If this expression is an rvalue, but might be mistaken for an
6633 : lvalue, we must add a NON_LVALUE_EXPR. */
6634 4860832 : result = rvalue (result);
6635 4860832 : if (semantic_result_type)
6636 87 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type,
6637 : result);
6638 : }
6639 : else
6640 : {
6641 3127565 : result = force_paren_expr (result);
6642 3127565 : gcc_assert (semantic_result_type == NULL_TREE);
6643 : }
6644 :
6645 : return result;
6646 7991373 : }
6647 :
6648 : /* OPERAND is an operand to an expression. Perform necessary steps
6649 : required before using it. If OPERAND is NULL_TREE, NULL_TREE is
6650 : returned. */
6651 :
6652 : static tree
6653 660831733 : prep_operand (tree operand)
6654 : {
6655 660831733 : if (operand)
6656 : {
6657 763773688 : if (CLASS_TYPE_P (TREE_TYPE (operand))
6658 408895482 : && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
6659 : /* Make sure the template type is instantiated now. */
6660 10813202 : instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
6661 : }
6662 :
6663 660831733 : return operand;
6664 : }
6665 :
6666 : /* True iff CONV represents a conversion sequence which no other can be better
6667 : than under [over.ics.rank]: in other words, a "conversion" to the exact same
6668 : type (including binding to a reference to the same type). This is stronger
6669 : than the standard's "identity" category, which also includes reference
6670 : bindings that add cv-qualifiers or change rvalueness. */
6671 :
6672 : static bool
6673 232953672 : perfect_conversion_p (conversion *conv)
6674 : {
6675 232953672 : if (CONVERSION_RANK (conv) != cr_identity)
6676 : return false;
6677 166610447 : if (conv->kind == ck_ref_bind)
6678 : {
6679 43627759 : if (!conv->rvaluedness_matches_p)
6680 : return false;
6681 30196421 : if (!same_type_p (TREE_TYPE (conv->type),
6682 : next_conversion (conv)->type))
6683 : return false;
6684 : }
6685 149214575 : if (conv->check_narrowing)
6686 : /* Brace elision is imperfect. */
6687 50 : return false;
6688 : return true;
6689 : }
6690 :
6691 : /* True if CAND represents a perfect match, i.e. all perfect conversions, so no
6692 : other candidate can be a better match. Since the template/non-template
6693 : tiebreaker comes immediately after the conversion comparison in
6694 : [over.match.best], a perfect non-template candidate is better than all
6695 : templates. */
6696 :
6697 : static bool
6698 549343036 : perfect_candidate_p (z_candidate *cand)
6699 : {
6700 549343036 : if (cand->viable < 1)
6701 : return false;
6702 : /* CWG1402 makes an implicitly deleted move op worse than other
6703 : candidates. */
6704 214532335 : if (DECL_DELETED_FN (cand->fn) && DECL_DEFAULTED_FN (cand->fn)
6705 213513928 : && move_fn_p (cand->fn))
6706 : return false;
6707 211840691 : int len = cand->num_convs;
6708 361055216 : for (int i = 0; i < len; ++i)
6709 232953672 : if (!perfect_conversion_p (cand->convs[i]))
6710 : return false;
6711 128101544 : if (conversion *conv = cand->second_conv)
6712 0 : if (!perfect_conversion_p (conv))
6713 : return false;
6714 : return true;
6715 : }
6716 :
6717 : /* True iff one of CAND's argument conversions is missing. */
6718 :
6719 : static bool
6720 22552233 : missing_conversion_p (const z_candidate *cand)
6721 : {
6722 41082253 : for (unsigned i = 0; i < cand->num_convs; ++i)
6723 : {
6724 27270294 : conversion *conv = cand->convs[i];
6725 27270294 : if (!conv)
6726 : return true;
6727 26266222 : if (conv->kind == ck_deferred_bad)
6728 : {
6729 : /* We don't know whether this conversion is outright invalid or
6730 : just bad, so conservatively assume it's missing. */
6731 7736202 : gcc_checking_assert (conv->bad_p);
6732 : return true;
6733 : }
6734 : }
6735 : return false;
6736 : }
6737 :
6738 : /* Add each of the viable functions in FNS (a FUNCTION_DECL or
6739 : OVERLOAD) to the CANDIDATES, returning an updated list of
6740 : CANDIDATES. The ARGS are the arguments provided to the call;
6741 : if FIRST_ARG is non-null it is the implicit object argument,
6742 : otherwise the first element of ARGS is used if needed. The
6743 : EXPLICIT_TARGS are explicit template arguments provided.
6744 : TEMPLATE_ONLY is true if only template functions should be
6745 : considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
6746 : add_function_candidate. */
6747 :
6748 : static void
6749 317360859 : add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args,
6750 : tree return_type,
6751 : tree explicit_targs, bool template_only,
6752 : tree conversion_path, tree access_path,
6753 : int flags,
6754 : struct z_candidate **candidates,
6755 : tsubst_flags_t complain)
6756 : {
6757 317360859 : tree ctype;
6758 317360859 : const vec<tree, va_gc> *non_static_args;
6759 317360859 : bool check_list_ctor = false;
6760 317360859 : bool check_converting = false;
6761 317360859 : unification_kind_t strict;
6762 317360859 : tree ne_context = NULL_TREE;
6763 317360859 : tree ne_fns = NULL_TREE;
6764 :
6765 317360859 : if (!fns)
6766 3430609 : return;
6767 :
6768 : /* Precalculate special handling of constructors and conversion ops. */
6769 313930250 : tree fn = OVL_FIRST (fns);
6770 313930250 : if (DECL_CONV_FN_P (fn))
6771 : {
6772 18550714 : check_list_ctor = false;
6773 18550714 : check_converting = (flags & LOOKUP_ONLYCONVERTING) != 0;
6774 18550714 : if (flags & LOOKUP_NO_CONVERSION)
6775 : /* We're doing return_type(x). */
6776 : strict = DEDUCE_CONV;
6777 : else
6778 : /* We're doing x.operator return_type(). */
6779 1950 : strict = DEDUCE_EXACT;
6780 : /* [over.match.funcs] For conversion functions, the function
6781 : is considered to be a member of the class of the implicit
6782 : object argument for the purpose of defining the type of
6783 : the implicit object parameter. */
6784 18550714 : ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg));
6785 : }
6786 : else
6787 : {
6788 590759072 : if (DECL_CONSTRUCTOR_P (fn))
6789 : {
6790 79618389 : check_list_ctor = (flags & LOOKUP_LIST_ONLY) != 0;
6791 : /* For list-initialization we consider explicit constructors
6792 : and complain if one is chosen. */
6793 79618389 : check_converting
6794 79618389 : = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
6795 : == LOOKUP_ONLYCONVERTING);
6796 : }
6797 295379536 : strict = DEDUCE_CALL;
6798 459929081 : ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
6799 : }
6800 :
6801 : /* P2468: Check if operator== is a rewrite target with first operand
6802 : (*args)[0]; for now just do the lookups. */
6803 313930250 : if ((flags & (LOOKUP_REWRITTEN | LOOKUP_REVERSED))
6804 313930250 : && DECL_OVERLOADED_OPERATOR_IS (fn, EQ_EXPR))
6805 : {
6806 7006867 : tree ne_name = ovl_op_identifier (false, NE_EXPR);
6807 7006867 : if (DECL_CLASS_SCOPE_P (fn))
6808 : {
6809 187408 : ne_context = DECL_CONTEXT (fn);
6810 187408 : ne_fns = lookup_fnfields (TREE_TYPE ((*args)[0]), ne_name,
6811 : 1, tf_none);
6812 187408 : if (ne_fns == error_mark_node || ne_fns == NULL_TREE)
6813 : ne_fns = NULL_TREE;
6814 : else
6815 19728 : ne_fns = BASELINK_FUNCTIONS (ne_fns);
6816 : }
6817 : else
6818 : {
6819 6819459 : ne_context = decl_namespace_context (fn);
6820 6819459 : ne_fns = lookup_qualified_name (ne_context, ne_name,
6821 : LOOK_want::NORMAL,
6822 : /*complain*/false);
6823 6819459 : if (ne_fns == error_mark_node
6824 2255498 : || !is_overloaded_fn (ne_fns))
6825 4563961 : ne_fns = NULL_TREE;
6826 : }
6827 : }
6828 :
6829 313930250 : if (first_arg)
6830 : non_static_args = args;
6831 : else
6832 : /* Delay creating the implicit this parameter until it is needed. */
6833 139444335 : non_static_args = NULL;
6834 :
6835 313930250 : bool seen_strictly_viable = any_strictly_viable (*candidates);
6836 : /* If there's a non-template perfect match, we don't need to consider
6837 : templates. So check non-templates first. This optimization is only
6838 : really needed for the defaulted copy constructor of tuple and the like
6839 : (96926), but it seems like we might as well enable it more generally. */
6840 313930250 : bool seen_perfect = false;
6841 313930250 : enum { templates, non_templates, either } which = either;
6842 313930250 : if (template_only)
6843 : which = templates;
6844 : else /*if (flags & LOOKUP_DEFAULTED)*/
6845 274759227 : which = non_templates;
6846 :
6847 : /* Template candidates that we'll potentially ignore if the
6848 : perfect candidate optimization succeeds. */
6849 313930250 : z_candidate *ignored_template_cands = nullptr;
6850 :
6851 : /* During overload resolution, we first consider each function under the
6852 : assumption that we'll eventually find a strictly viable candidate.
6853 : This allows us to circumvent our defacto behavior when checking
6854 : argument conversions and shortcut consideration of the candidate
6855 : upon encountering the first bad conversion. If this assumption
6856 : turns out to be false, and all candidates end up being non-strictly
6857 : viable, then we reconsider such candidates under the defacto behavior.
6858 : This trick is important for pruning member function overloads according
6859 : to their const/ref-qualifiers (since all 'this' conversions are at
6860 : worst bad) without breaking -fpermissive. */
6861 313930250 : z_candidate *bad_cands = nullptr;
6862 313930250 : bool shortcut_bad_convs = true;
6863 :
6864 146782354 : again:
6865 2662161145 : for (tree fn : lkp_range (fns))
6866 : {
6867 2201462092 : if (which == templates && TREE_CODE (fn) != TEMPLATE_DECL)
6868 : {
6869 321376352 : if (template_only)
6870 800088 : add_ignored_candidate (candidates, fn);
6871 321376352 : continue;
6872 : }
6873 1880085740 : if (which == non_templates && TREE_CODE (fn) == TEMPLATE_DECL)
6874 : {
6875 651541105 : add_ignored_candidate (&ignored_template_cands, fn);
6876 651541105 : continue;
6877 : }
6878 236227877 : if ((check_converting && DECL_NONCONVERTING_P (fn))
6879 1439044164 : || (check_list_ctor && !is_list_ctor (fn)))
6880 : {
6881 27152150 : add_ignored_candidate (candidates, fn);
6882 27152150 : continue;
6883 : }
6884 :
6885 1201392485 : tree fn_first_arg = NULL_TREE;
6886 1201392485 : const vec<tree, va_gc> *fn_args = args;
6887 :
6888 1201392485 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fn))
6889 : {
6890 : /* Figure out where the object arg comes from. If this
6891 : function is a non-static member and we didn't get an
6892 : implicit object argument, move it out of args. */
6893 435980712 : if (first_arg == NULL_TREE)
6894 : {
6895 8614344 : unsigned int ix;
6896 8614344 : tree arg;
6897 8614344 : vec<tree, va_gc> *tempvec;
6898 8614344 : vec_alloc (tempvec, args->length () - 1);
6899 23321574 : for (ix = 1; args->iterate (ix, &arg); ++ix)
6900 6092886 : tempvec->quick_push (arg);
6901 8614344 : non_static_args = tempvec;
6902 8614344 : first_arg = (*args)[0];
6903 : }
6904 :
6905 : fn_first_arg = first_arg;
6906 : fn_args = non_static_args;
6907 : }
6908 :
6909 : /* Don't bother reversing an operator with two identical parameters. */
6910 765411773 : else if (vec_safe_length (args) == 2 && (flags & LOOKUP_REVERSED))
6911 : {
6912 212774063 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
6913 212774063 : if (same_type_p (TREE_VALUE (parmlist),
6914 : TREE_VALUE (TREE_CHAIN (parmlist))))
6915 100960120 : continue;
6916 : }
6917 :
6918 : /* When considering reversed operator==, if there's a corresponding
6919 : operator!= in the same scope, it's not a rewrite target. */
6920 1100432365 : if (ne_context)
6921 : {
6922 150768981 : if (TREE_CODE (ne_context) == NAMESPACE_DECL)
6923 : {
6924 : /* With argument-dependent lookup, fns can span multiple
6925 : namespaces; make sure we look in the fn's namespace for a
6926 : corresponding operator!=. */
6927 150576314 : tree fn_ns = decl_namespace_context (fn);
6928 150576314 : if (fn_ns != ne_context)
6929 : {
6930 8375291 : ne_context = fn_ns;
6931 8375291 : tree ne_name = ovl_op_identifier (false, NE_EXPR);
6932 8375291 : ne_fns = lookup_qualified_name (ne_context, ne_name,
6933 : LOOK_want::NORMAL,
6934 : /*complain*/false);
6935 8375291 : if (ne_fns == error_mark_node
6936 5305794 : || !is_overloaded_fn (ne_fns))
6937 3069497 : ne_fns = NULL_TREE;
6938 : }
6939 : }
6940 150768981 : bool found = false;
6941 1625484531 : for (lkp_iterator ne (ne_fns); !found && ne; ++ne)
6942 1510622242 : if (0 && !ne.using_p ()
6943 : && DECL_NAMESPACE_SCOPE_P (fn)
6944 : && DECL_CONTEXT (*ne) != DECL_CONTEXT (fn))
6945 : /* ??? This kludge excludes inline namespace members for the H
6946 : test in spaceship-eq15.C, but I don't see why we would want
6947 : that behavior. Asked Core 2022-11-04. Disabling for now. */;
6948 1510622242 : else if (fns_correspond (fn, *ne))
6949 : {
6950 : found = true;
6951 : break;
6952 : }
6953 150768981 : if (found)
6954 35906692 : continue;
6955 : }
6956 :
6957 : /* Do not resolve any non-default function. Only the default version
6958 : is resolvable (for the target_version attribute semantics.) */
6959 1064525673 : if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
6960 : && TREE_CODE (fn) == FUNCTION_DECL
6961 : && !is_function_default_version (fn))
6962 : {
6963 : add_ignored_candidate (candidates, fn);
6964 : continue;
6965 : }
6966 :
6967 1064525673 : if (TREE_CODE (fn) == TEMPLATE_DECL)
6968 515182637 : add_template_candidate (candidates,
6969 : fn,
6970 : ctype,
6971 : explicit_targs,
6972 : fn_first_arg,
6973 : fn_args,
6974 : return_type,
6975 : access_path,
6976 : conversion_path,
6977 : flags,
6978 : strict,
6979 : shortcut_bad_convs,
6980 : complain);
6981 : else
6982 : {
6983 549343036 : add_function_candidate (candidates,
6984 : fn,
6985 : ctype,
6986 : fn_first_arg,
6987 : fn_args,
6988 : access_path,
6989 : conversion_path,
6990 : flags,
6991 : NULL,
6992 : shortcut_bad_convs,
6993 : complain);
6994 549343036 : if (perfect_candidate_p (*candidates))
6995 1064512122 : seen_perfect = true;
6996 : }
6997 :
6998 1064512122 : z_candidate *cand = *candidates;
6999 1064512122 : if (cand->viable == 1)
7000 299663217 : seen_strictly_viable = true;
7001 :
7002 1064512122 : if (cand->viable == -1
7003 22552747 : && shortcut_bad_convs
7004 1087064355 : && (missing_conversion_p (cand)
7005 13811959 : || TREE_CODE (cand->fn) == TEMPLATE_DECL))
7006 : {
7007 : /* This candidate has been tentatively marked non-strictly viable,
7008 : and we didn't compute all argument conversions for it (having
7009 : stopped at the first bad conversion). Move it to BAD_CANDS to
7010 : to fully reconsider later if we don't find any strictly viable
7011 : candidates. */
7012 8772040 : if (complain & (tf_error | tf_conv))
7013 : {
7014 8444535 : *candidates = cand->next;
7015 8444535 : cand->next = bad_cands;
7016 8444535 : bad_cands = cand;
7017 : }
7018 : else
7019 : /* But if we're in a SFINAE context, just mark this candidate as
7020 : unviable outright and avoid potentially reconsidering it.
7021 : This is safe to do because in a SFINAE context, performing a bad
7022 : conversion is always an error (even with -fpermissive), so a
7023 : non-strictly viable candidate is effectively unviable anyway. */
7024 327505 : cand->viable = 0;
7025 : }
7026 : }
7027 460699053 : if (which == non_templates && !seen_perfect)
7028 : {
7029 146663853 : which = templates;
7030 146663853 : ignored_template_cands = nullptr;
7031 146663853 : goto again;
7032 : }
7033 314035200 : else if (which == templates
7034 314035200 : && !seen_strictly_viable
7035 : && shortcut_bad_convs
7036 57853917 : && bad_cands)
7037 : {
7038 : /* None of the candidates are strictly viable, so consider again those
7039 : functions in BAD_CANDS, this time without shortcutting bad conversions
7040 : so that all their argument conversions are computed. */
7041 261884 : which = either;
7042 : fns = NULL_TREE;
7043 261884 : for (z_candidate *cand = bad_cands; cand; cand = cand->next)
7044 : {
7045 143383 : tree fn = cand->fn;
7046 143383 : if (tree ti = cand->template_decl)
7047 120 : fn = TI_TEMPLATE (ti);
7048 143383 : fns = ovl_make (fn, fns);
7049 : }
7050 118501 : shortcut_bad_convs = false;
7051 118501 : bad_cands = nullptr;
7052 118501 : goto again;
7053 : }
7054 :
7055 313916699 : if (complain & tf_error)
7056 : {
7057 : /* Remember any omitted candidates; we may want to print all candidates
7058 : as part of overload resolution failure diagnostics. */
7059 480485874 : for (z_candidate *omitted_cands : { ignored_template_cands, bad_cands })
7060 : {
7061 320323916 : z_candidate **omitted_cands_tail = &omitted_cands;
7062 368600903 : while (*omitted_cands_tail)
7063 48276987 : omitted_cands_tail = &(*omitted_cands_tail)->next;
7064 320323916 : *omitted_cands_tail = *candidates;
7065 320323916 : *candidates = omitted_cands;
7066 : }
7067 : }
7068 : }
7069 :
7070 : /* Returns 1 if P0145R2 says that the LHS of operator CODE is evaluated first,
7071 : -1 if the RHS is evaluated first, or 0 if the order is unspecified. */
7072 :
7073 : static int
7074 16569898 : op_is_ordered (tree_code code)
7075 : {
7076 16569239 : switch (code)
7077 : {
7078 : // 5. b @= a
7079 3725543 : case MODIFY_EXPR:
7080 3725543 : return (flag_strong_eval_order > 1 ? -1 : 0);
7081 :
7082 : // 6. a[b]
7083 425450 : case ARRAY_REF:
7084 424791 : return (flag_strong_eval_order > 1 ? 1 : 0);
7085 :
7086 : // 1. a.b
7087 : // Not overloadable (yet).
7088 : // 2. a->b
7089 : // Only one argument.
7090 : // 3. a->*b
7091 66624 : case MEMBER_REF:
7092 : // 7. a << b
7093 66624 : case LSHIFT_EXPR:
7094 : // 8. a >> b
7095 66624 : case RSHIFT_EXPR:
7096 : // a && b
7097 : // Predates P0145R3.
7098 66624 : case TRUTH_ANDIF_EXPR:
7099 : // a || b
7100 : // Predates P0145R3.
7101 66624 : case TRUTH_ORIF_EXPR:
7102 : // a , b
7103 : // Predates P0145R3.
7104 66624 : case COMPOUND_EXPR:
7105 66624 : return (flag_strong_eval_order ? 1 : 0);
7106 :
7107 : default:
7108 : return 0;
7109 : }
7110 : }
7111 :
7112 : /* Subroutine of build_new_op: Add to CANDIDATES all candidates for the
7113 : operator indicated by CODE/CODE2. This function calls itself recursively to
7114 : handle C++20 rewritten comparison operator candidates. Returns NULL_TREE
7115 : upon success, and error_mark_node if something went wrong that prevented
7116 : us from performing overload resolution (e.g. ambiguous member name lookup).
7117 :
7118 : LOOKUPS, if non-NULL, is the set of pertinent namespace-scope operator
7119 : overloads to consider. This parameter is used when instantiating a
7120 : dependent operator expression and has the same structure as
7121 : DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS. */
7122 :
7123 : static tree
7124 34955357 : add_operator_candidates (z_candidate **candidates,
7125 : tree_code code, tree_code code2,
7126 : vec<tree, va_gc> *arglist, tree lookups,
7127 : int flags, tsubst_flags_t complain)
7128 : {
7129 34955357 : z_candidate *start_candidates = *candidates;
7130 34955357 : bool ismodop = code2 != ERROR_MARK;
7131 34955357 : tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
7132 :
7133 : /* LOOKUP_REWRITTEN is set when we're looking for the == or <=> operator to
7134 : rewrite from, and also when we're looking for the e.g. < operator to use
7135 : on the result of <=>. In the latter case, we don't want the flag set in
7136 : the candidate, we just want to suppress looking for rewrites. */
7137 34955357 : bool rewritten = (flags & LOOKUP_REWRITTEN);
7138 34955357 : if (rewritten && code != EQ_EXPR && code != SPACESHIP_EXPR)
7139 949206 : flags &= ~LOOKUP_REWRITTEN;
7140 :
7141 32402322 : bool memonly = false;
7142 32402322 : switch (code)
7143 : {
7144 : /* =, ->, [], () must be non-static member functions. */
7145 6552524 : case MODIFY_EXPR:
7146 6552524 : if (code2 != NOP_EXPR)
7147 : break;
7148 : /* FALLTHRU */
7149 : case COMPONENT_REF:
7150 : case ARRAY_REF:
7151 : memonly = true;
7152 : break;
7153 :
7154 : default:
7155 : break;
7156 : }
7157 :
7158 : /* Add namespace-scope operators to the list of functions to
7159 : consider. */
7160 : if (!memonly)
7161 : {
7162 30674200 : tree fns;
7163 30674200 : if (!lookups)
7164 22644343 : fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE);
7165 : /* If LOOKUPS is non-NULL, then we're instantiating a dependent operator
7166 : expression, and LOOKUPS is the result of stage 1 name lookup. */
7167 8029857 : else if (tree found = purpose_member (fnname, lookups))
7168 1752034 : fns = TREE_VALUE (found);
7169 : else
7170 : fns = NULL_TREE;
7171 30674200 : fns = lookup_arg_dependent (fnname, fns, arglist);
7172 30674200 : add_candidates (fns, NULL_TREE, arglist, NULL_TREE,
7173 : NULL_TREE, false, NULL_TREE, NULL_TREE,
7174 : flags, candidates, complain);
7175 : }
7176 :
7177 : /* Add class-member operators to the candidate set. */
7178 34955324 : tree arg1_type = TREE_TYPE ((*arglist)[0]);
7179 34955324 : unsigned nargs = arglist->length () > 1 ? 2 : 1;
7180 29915916 : tree arg2_type = nargs > 1 ? TREE_TYPE ((*arglist)[1]) : NULL_TREE;
7181 34955324 : if (CLASS_TYPE_P (arg1_type))
7182 : {
7183 20271457 : tree fns = lookup_fnfields (arg1_type, fnname, 1, complain);
7184 20271457 : if (fns == error_mark_node)
7185 : return error_mark_node;
7186 20271445 : if (fns)
7187 : {
7188 9043864 : if (code == ARRAY_REF)
7189 : {
7190 429520 : vec<tree,va_gc> *restlist = make_tree_vector ();
7191 859040 : for (unsigned i = 1; i < nargs; ++i)
7192 429520 : vec_safe_push (restlist, (*arglist)[i]);
7193 429520 : z_candidate *save_cand = *candidates;
7194 859040 : add_candidates (BASELINK_FUNCTIONS (fns),
7195 429520 : (*arglist)[0], restlist, NULL_TREE,
7196 : NULL_TREE, false,
7197 429520 : BASELINK_BINFO (fns),
7198 429520 : BASELINK_ACCESS_BINFO (fns),
7199 : flags, candidates, complain);
7200 : /* Release the vec if we didn't add a candidate that uses it. */
7201 430118 : for (z_candidate *c = *candidates; c != save_cand; c = c->next)
7202 430118 : if (c->args == restlist)
7203 : {
7204 429520 : restlist = NULL;
7205 429520 : break;
7206 : }
7207 429520 : release_tree_vector (restlist);
7208 : }
7209 : else
7210 8614344 : add_candidates (BASELINK_FUNCTIONS (fns),
7211 : NULL_TREE, arglist, NULL_TREE,
7212 : NULL_TREE, false,
7213 8614344 : BASELINK_BINFO (fns),
7214 8614344 : BASELINK_ACCESS_BINFO (fns),
7215 : flags, candidates, complain);
7216 : }
7217 : }
7218 : /* Per [over.match.oper]3.2, if no operand has a class type, then
7219 : only non-member functions that have type T1 or reference to
7220 : cv-qualified-opt T1 for the first argument, if the first argument
7221 : has an enumeration type, or T2 or reference to cv-qualified-opt
7222 : T2 for the second argument, if the second argument has an
7223 : enumeration type. Filter out those that don't match. */
7224 14683867 : else if (! arg2_type || ! CLASS_TYPE_P (arg2_type))
7225 : {
7226 : struct z_candidate **candp, **next;
7227 :
7228 242025673 : for (candp = candidates; *candp != start_candidates; candp = next)
7229 : {
7230 227940166 : unsigned i;
7231 227940166 : z_candidate *cand = *candp;
7232 227940166 : next = &cand->next;
7233 :
7234 227940166 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
7235 :
7236 672659355 : for (i = 0; i < nargs; ++i)
7237 : {
7238 449895243 : tree parmtype = TREE_VALUE (parmlist);
7239 449895243 : tree argtype = unlowered_expr_type ((*arglist)[i]);
7240 :
7241 449895243 : if (TYPE_REF_P (parmtype))
7242 355730983 : parmtype = TREE_TYPE (parmtype);
7243 449895243 : if (TREE_CODE (argtype) == ENUMERAL_TYPE
7244 878730014 : && (same_type_ignoring_top_level_qualifiers_p
7245 428834771 : (argtype, parmtype)))
7246 : break;
7247 :
7248 444719189 : parmlist = TREE_CHAIN (parmlist);
7249 : }
7250 :
7251 : /* No argument has an appropriate type, so remove this
7252 : candidate function from the list. */
7253 227940166 : if (i == nargs)
7254 : {
7255 222764112 : *candp = cand->next;
7256 222764112 : next = candp;
7257 : }
7258 : }
7259 : }
7260 :
7261 34955312 : if (!rewritten)
7262 : {
7263 : /* The standard says to rewrite built-in candidates, too,
7264 : but there's no point. */
7265 24598681 : add_builtin_candidates (candidates, code, code2, fnname, arglist,
7266 : flags, complain);
7267 :
7268 : /* Maybe add C++20 rewritten comparison candidates. */
7269 24598681 : tree_code rewrite_code = ERROR_MARK;
7270 24598681 : if (cxx_dialect >= cxx20
7271 24296350 : && nargs == 2
7272 43978044 : && (OVERLOAD_TYPE_P (arg1_type) || OVERLOAD_TYPE_P (arg2_type)))
7273 19371743 : switch (code)
7274 : {
7275 1339633 : case LT_EXPR:
7276 1339633 : case LE_EXPR:
7277 1339633 : case GT_EXPR:
7278 1339633 : case GE_EXPR:
7279 1339633 : case SPACESHIP_EXPR:
7280 1339633 : rewrite_code = SPACESHIP_EXPR;
7281 1339633 : break;
7282 :
7283 : case NE_EXPR:
7284 : case EQ_EXPR:
7285 : rewrite_code = EQ_EXPR;
7286 : break;
7287 :
7288 : default:;
7289 : }
7290 :
7291 1339633 : if (rewrite_code)
7292 : {
7293 6308687 : tree r;
7294 6308687 : flags |= LOOKUP_REWRITTEN;
7295 6308687 : if (rewrite_code != code)
7296 : {
7297 : /* Add rewritten candidates in same order. */
7298 3098436 : r = add_operator_candidates (candidates, rewrite_code, ERROR_MARK,
7299 : arglist, lookups, flags, complain);
7300 3098436 : if (r == error_mark_node)
7301 : return error_mark_node;
7302 : }
7303 :
7304 6308681 : z_candidate *save_cand = *candidates;
7305 :
7306 : /* Add rewritten candidates in reverse order. */
7307 6308681 : flags |= LOOKUP_REVERSED;
7308 6308681 : vec<tree,va_gc> *revlist = make_tree_vector ();
7309 6308681 : revlist->quick_push ((*arglist)[1]);
7310 6308681 : revlist->quick_push ((*arglist)[0]);
7311 6308681 : r = add_operator_candidates (candidates, rewrite_code, ERROR_MARK,
7312 : revlist, lookups, flags, complain);
7313 6308681 : if (r == error_mark_node)
7314 : return error_mark_node;
7315 :
7316 : /* Release the vec if we didn't add a candidate that uses it. */
7317 6470778 : for (z_candidate *c = *candidates; c != save_cand; c = c->next)
7318 2985756 : if (c->args == revlist)
7319 : {
7320 : revlist = NULL;
7321 : break;
7322 : }
7323 6308681 : release_tree_vector (revlist);
7324 : }
7325 : }
7326 :
7327 : return NULL_TREE;
7328 : }
7329 :
7330 : tree
7331 219406609 : build_new_op (const op_location_t &loc, enum tree_code code, int flags,
7332 : tree arg1, tree arg2, tree arg3, tree lookups,
7333 : tree *overload, tsubst_flags_t complain)
7334 : {
7335 219406609 : struct z_candidate *candidates = 0, *cand;
7336 219406609 : releasing_vec arglist;
7337 219406609 : tree result = NULL_TREE;
7338 219406609 : bool result_valid_p = false;
7339 219406609 : enum tree_code code2 = ERROR_MARK;
7340 219406609 : enum tree_code code_orig_arg1 = ERROR_MARK;
7341 219406609 : enum tree_code code_orig_arg2 = ERROR_MARK;
7342 219406609 : bool strict_p;
7343 219406609 : bool any_viable_p;
7344 :
7345 219406609 : auto_cond_timevar tv (TV_OVERLOAD);
7346 :
7347 219406609 : if (error_operand_p (arg1)
7348 219402457 : || error_operand_p (arg2)
7349 438803027 : || error_operand_p (arg3))
7350 10191 : return error_mark_node;
7351 :
7352 219396418 : conversion_obstack_sentinel cos;
7353 :
7354 219396418 : bool ismodop = code == MODIFY_EXPR;
7355 219396418 : if (ismodop)
7356 : {
7357 11370729 : code2 = TREE_CODE (arg3);
7358 11370729 : arg3 = NULL_TREE;
7359 : }
7360 :
7361 219396418 : tree arg1_type = unlowered_expr_type (arg1);
7362 219396418 : tree arg2_type = arg2 ? unlowered_expr_type (arg2) : NULL_TREE;
7363 :
7364 219396418 : arg1 = prep_operand (arg1);
7365 :
7366 219396418 : switch (code)
7367 : {
7368 0 : case NEW_EXPR:
7369 0 : case VEC_NEW_EXPR:
7370 0 : case VEC_DELETE_EXPR:
7371 0 : case DELETE_EXPR:
7372 : /* Use build_operator_new_call and build_op_delete_call instead. */
7373 0 : gcc_unreachable ();
7374 :
7375 0 : case CALL_EXPR:
7376 : /* Use build_op_call instead. */
7377 0 : gcc_unreachable ();
7378 :
7379 18501198 : case TRUTH_ORIF_EXPR:
7380 18501198 : case TRUTH_ANDIF_EXPR:
7381 18501198 : case TRUTH_AND_EXPR:
7382 18501198 : case TRUTH_OR_EXPR:
7383 : /* These are saved for the sake of warn_logical_operator. */
7384 18501198 : code_orig_arg1 = TREE_CODE (arg1);
7385 18501198 : code_orig_arg2 = TREE_CODE (arg2);
7386 18501198 : break;
7387 51309982 : case GT_EXPR:
7388 51309982 : case LT_EXPR:
7389 51309982 : case GE_EXPR:
7390 51309982 : case LE_EXPR:
7391 51309982 : case EQ_EXPR:
7392 51309982 : case NE_EXPR:
7393 : /* These are saved for the sake of maybe_warn_bool_compare. */
7394 51309982 : code_orig_arg1 = TREE_CODE (arg1_type);
7395 51309982 : code_orig_arg2 = TREE_CODE (arg2_type);
7396 51309982 : break;
7397 :
7398 : default:
7399 : break;
7400 : }
7401 :
7402 219396418 : arg2 = prep_operand (arg2);
7403 219396418 : arg3 = prep_operand (arg3);
7404 :
7405 219396418 : if (code == COND_EXPR)
7406 : /* Use build_conditional_expr instead. */
7407 0 : gcc_unreachable ();
7408 219396418 : else if (! OVERLOAD_TYPE_P (arg1_type)
7409 413682312 : && (! arg2 || ! OVERLOAD_TYPE_P (arg2_type)))
7410 193848178 : goto builtin;
7411 :
7412 25548240 : if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
7413 : {
7414 318949 : arg2 = integer_zero_node;
7415 318949 : arg2_type = integer_type_node;
7416 : }
7417 :
7418 25548240 : arglist->quick_push (arg1);
7419 25548240 : if (arg2 != NULL_TREE)
7420 20508832 : arglist->quick_push (arg2);
7421 25548240 : if (arg3 != NULL_TREE)
7422 0 : arglist->quick_push (arg3);
7423 :
7424 25548240 : result = add_operator_candidates (&candidates, code, code2, arglist,
7425 : lookups, flags, complain);
7426 25548207 : if (result == error_mark_node)
7427 : return error_mark_node;
7428 :
7429 25548195 : switch (code)
7430 : {
7431 : case COMPOUND_EXPR:
7432 : case ADDR_EXPR:
7433 : /* For these, the built-in candidates set is empty
7434 : [over.match.oper]/3. We don't want non-strict matches
7435 : because exact matches are always possible with built-in
7436 : operators. The built-in candidate set for COMPONENT_REF
7437 : would be empty too, but since there are no such built-in
7438 : operators, we accept non-strict matches for them. */
7439 : strict_p = true;
7440 : break;
7441 :
7442 23633492 : default:
7443 23633492 : strict_p = false;
7444 23633492 : break;
7445 : }
7446 :
7447 25548195 : candidates = splice_viable (candidates, strict_p, &any_viable_p);
7448 25548195 : if (!any_viable_p)
7449 : {
7450 1965763 : switch (code)
7451 : {
7452 357 : case POSTINCREMENT_EXPR:
7453 357 : case POSTDECREMENT_EXPR:
7454 : /* Don't try anything fancy if we're not allowed to produce
7455 : errors. */
7456 357 : if (!(complain & tf_error))
7457 : return error_mark_node;
7458 :
7459 : /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
7460 : distinguish between prefix and postfix ++ and
7461 : operator++() was used for both, so we allow this with
7462 : -fpermissive. */
7463 : else
7464 : {
7465 42 : tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
7466 30 : const char *msg = (flag_permissive)
7467 42 : ? G_("no %<%D(int)%> declared for postfix %qs,"
7468 : " trying prefix operator instead")
7469 : : G_("no %<%D(int)%> declared for postfix %qs");
7470 42 : permerror (loc, msg, fnname, OVL_OP_INFO (false, code)->name);
7471 : }
7472 :
7473 42 : if (!flag_permissive)
7474 30 : return error_mark_node;
7475 :
7476 12 : if (code == POSTINCREMENT_EXPR)
7477 : code = PREINCREMENT_EXPR;
7478 : else
7479 0 : code = PREDECREMENT_EXPR;
7480 12 : result = build_new_op (loc, code, flags, arg1, NULL_TREE,
7481 : NULL_TREE, lookups, overload, complain);
7482 12 : break;
7483 :
7484 : /* The caller will deal with these. */
7485 : case ADDR_EXPR:
7486 : case COMPOUND_EXPR:
7487 : case COMPONENT_REF:
7488 : case CO_AWAIT_EXPR:
7489 : result = NULL_TREE;
7490 : result_valid_p = true;
7491 : break;
7492 :
7493 47971 : default:
7494 47971 : if (complain & tf_error)
7495 : {
7496 : /* If one of the arguments of the operator represents
7497 : an invalid use of member function pointer, try to report
7498 : a meaningful error ... */
7499 1354 : if (invalid_nonstatic_memfn_p (loc, arg1, tf_error)
7500 1351 : || invalid_nonstatic_memfn_p (loc, arg2, tf_error)
7501 2699 : || invalid_nonstatic_memfn_p (loc, arg3, tf_error))
7502 : /* We displayed the error message. */;
7503 : else
7504 : {
7505 : /* ... Otherwise, report the more generic
7506 : "no matching operator found" error */
7507 1345 : auto_diagnostic_group d;
7508 1345 : op_error (loc, code, code2, arg1, arg2, arg3, false);
7509 1345 : print_z_candidates (loc, candidates);
7510 1345 : }
7511 : }
7512 47971 : result = error_mark_node;
7513 47971 : break;
7514 : }
7515 : }
7516 : else
7517 : {
7518 23582432 : cand = tourney (candidates, complain);
7519 23582432 : if (cand == 0)
7520 : {
7521 184 : if (complain & tf_error)
7522 : {
7523 129 : auto_diagnostic_group d;
7524 129 : op_error (loc, code, code2, arg1, arg2, arg3, true);
7525 129 : print_z_candidates (loc, candidates);
7526 129 : }
7527 184 : result = error_mark_node;
7528 184 : if (overload)
7529 165 : *overload = error_mark_node;
7530 : }
7531 23582248 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
7532 : {
7533 19394182 : if (overload)
7534 : {
7535 14855236 : if (cand->rewritten ())
7536 : /* build_min_non_dep_op_overload needs to know whether the
7537 : candidate is rewritten/reversed. */
7538 1853882 : *overload = build_tree_list (build_int_cst (integer_type_node,
7539 1853882 : cand->flags),
7540 : cand->fn);
7541 : else
7542 13001354 : *overload = cand->fn;
7543 : }
7544 :
7545 19394182 : if (resolve_args (arglist, complain) == NULL)
7546 6 : result = error_mark_node;
7547 : else
7548 : {
7549 19394176 : tsubst_flags_t ocomplain = complain;
7550 19394176 : if (cand->rewritten ())
7551 : /* We'll wrap this call in another one. */
7552 1854488 : ocomplain &= ~tf_decltype;
7553 19394176 : if (cand->reversed ())
7554 : {
7555 : /* We swapped these in add_candidate, swap them back now. */
7556 21378 : std::swap (cand->convs[0], cand->convs[1]);
7557 21378 : if (cand->fn == current_function_decl)
7558 1 : warning_at (loc, 0, "in C++20 this comparison calls the "
7559 : "current function recursively with reversed "
7560 : "arguments");
7561 : }
7562 19394176 : result = build_over_call (cand, LOOKUP_NORMAL, ocomplain);
7563 : }
7564 :
7565 35973575 : if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn))
7566 : /* There won't be a CALL_EXPR. */;
7567 16571875 : else if (result && result != error_mark_node)
7568 : {
7569 16569239 : tree call = extract_call_expr (result);
7570 16569239 : CALL_EXPR_OPERATOR_SYNTAX (call) = true;
7571 :
7572 : /* Specify evaluation order as per P0145R2. */
7573 16569239 : CALL_EXPR_ORDERED_ARGS (call) = false;
7574 16569239 : switch (op_is_ordered (code))
7575 : {
7576 3697279 : case -1:
7577 3697279 : CALL_EXPR_REVERSE_ARGS (call) = true;
7578 3697279 : break;
7579 :
7580 490406 : case 1:
7581 490406 : CALL_EXPR_ORDERED_ARGS (call) = true;
7582 490406 : break;
7583 :
7584 : default:
7585 : break;
7586 : }
7587 : }
7588 :
7589 : /* If this was a C++20 rewritten comparison, adjust the result. */
7590 19394179 : if (cand->rewritten ())
7591 : {
7592 1854488 : switch (code)
7593 : {
7594 9923 : case EQ_EXPR:
7595 9923 : gcc_checking_assert (cand->reversed ());
7596 903919 : gcc_fallthrough ();
7597 903919 : case NE_EXPR:
7598 903919 : if (result == error_mark_node)
7599 : ;
7600 : /* If a rewritten operator== candidate is selected by
7601 : overload resolution for an operator @, its return type
7602 : shall be cv bool.... */
7603 903917 : else if (TREE_CODE (TREE_TYPE (result)) != BOOLEAN_TYPE)
7604 : {
7605 44 : if (complain & tf_error)
7606 : {
7607 6 : auto_diagnostic_group d;
7608 6 : error_at (loc, "return type of %qD is not %qs",
7609 : cand->fn, "bool");
7610 6 : inform (loc, "used as rewritten candidate for "
7611 : "comparison of %qT and %qT",
7612 : arg1_type, arg2_type);
7613 6 : }
7614 44 : result = error_mark_node;
7615 : }
7616 903873 : else if (code == NE_EXPR)
7617 : /* !(y == x) or !(x == y) */
7618 893971 : result = build1_loc (loc, TRUTH_NOT_EXPR,
7619 : boolean_type_node, result);
7620 : break;
7621 :
7622 : /* If a rewritten operator<=> candidate is selected by
7623 : overload resolution for an operator @, x @ y is
7624 : interpreted as 0 @ (y <=> x) if the selected candidate is
7625 : a synthesized candidate with reversed order of parameters,
7626 : or (x <=> y) @ 0 otherwise, using the selected rewritten
7627 : operator<=> candidate. */
7628 637 : case SPACESHIP_EXPR:
7629 637 : if (!cand->reversed ())
7630 : /* We're in the build_new_op call below for an outer
7631 : reversed call; we don't need to do anything more. */
7632 : break;
7633 950255 : gcc_fallthrough ();
7634 950255 : case LT_EXPR:
7635 950255 : case LE_EXPR:
7636 950255 : case GT_EXPR:
7637 950255 : case GE_EXPR:
7638 950255 : {
7639 950255 : tree lhs = result;
7640 950255 : tree rhs = integer_zero_node;
7641 950255 : if (cand->reversed ())
7642 2148 : std::swap (lhs, rhs);
7643 950255 : warning_sentinel ws (warn_zero_as_null_pointer_constant);
7644 950255 : result = build_new_op (loc, code,
7645 : LOOKUP_NORMAL|LOOKUP_REWRITTEN,
7646 : lhs, rhs, NULL_TREE, lookups,
7647 : NULL, complain);
7648 950255 : }
7649 950255 : break;
7650 :
7651 0 : default:
7652 0 : gcc_unreachable ();
7653 : }
7654 : }
7655 :
7656 : /* In an expression of the form `a[]' where cand->fn
7657 : which is operator[] turns out to be a static member function,
7658 : `a' is none-the-less evaluated. */
7659 19394179 : if (code == ARRAY_REF)
7660 429495 : result = keep_unused_object_arg (result, arg1, cand->fn);
7661 : }
7662 : else
7663 : {
7664 : /* Give any warnings we noticed during overload resolution. */
7665 4188066 : if (cand->warnings && (complain & tf_warning))
7666 : {
7667 : struct candidate_warning *w;
7668 0 : for (w = cand->warnings; w; w = w->next)
7669 0 : joust (cand, w->loser, 1, complain);
7670 : }
7671 :
7672 : /* Check for comparison of different enum types. */
7673 4188066 : switch (code)
7674 : {
7675 3289721 : case GT_EXPR:
7676 3289721 : case LT_EXPR:
7677 3289721 : case GE_EXPR:
7678 3289721 : case LE_EXPR:
7679 3289721 : case EQ_EXPR:
7680 3289721 : case NE_EXPR:
7681 3289721 : if (TREE_CODE (arg1_type) == ENUMERAL_TYPE
7682 3079866 : && TREE_CODE (arg2_type) == ENUMERAL_TYPE
7683 6291779 : && (TYPE_MAIN_VARIANT (arg1_type)
7684 3002058 : != TYPE_MAIN_VARIANT (arg2_type)))
7685 : {
7686 80 : if (cxx_dialect >= cxx26
7687 26 : && (complain & tf_warning_or_error) == 0)
7688 2 : result = error_mark_node;
7689 78 : else if (cxx_dialect >= cxx26 || (complain & tf_warning))
7690 130 : emit_diagnostic ((cxx_dialect >= cxx26
7691 : ? diagnostics::kind::pedwarn
7692 : : diagnostics::kind::warning),
7693 77 : loc, OPT_Wenum_compare,
7694 : "comparison between %q#T and %q#T",
7695 : arg1_type, arg2_type);
7696 : }
7697 : break;
7698 : default:
7699 : break;
7700 : }
7701 :
7702 : /* "If a built-in candidate is selected by overload resolution, the
7703 : operands of class type are converted to the types of the
7704 : corresponding parameters of the selected operation function,
7705 : except that the second standard conversion sequence of a
7706 : user-defined conversion sequence (12.3.3.1.2) is not applied." */
7707 4188066 : conversion *conv = cand->convs[0];
7708 4188066 : if (conv->user_conv_p)
7709 : {
7710 65047 : conv = strip_standard_conversion (conv);
7711 65047 : arg1 = convert_like (conv, arg1, complain);
7712 : }
7713 :
7714 4188066 : if (arg2)
7715 : {
7716 3691000 : conv = cand->convs[1];
7717 3691000 : if (conv->user_conv_p)
7718 : {
7719 11272 : conv = strip_standard_conversion (conv);
7720 11272 : arg2 = convert_like (conv, arg2, complain);
7721 : }
7722 : }
7723 :
7724 4188066 : if (arg3)
7725 : {
7726 0 : conv = cand->convs[2];
7727 0 : if (conv->user_conv_p)
7728 : {
7729 0 : conv = strip_standard_conversion (conv);
7730 0 : arg3 = convert_like (conv, arg3, complain);
7731 : }
7732 : }
7733 : }
7734 : }
7735 :
7736 25547847 : if (result || result_valid_p)
7737 : return result;
7738 :
7739 4188064 : builtin:
7740 198036242 : switch (code)
7741 : {
7742 4818640 : case MODIFY_EXPR:
7743 4818640 : return cp_build_modify_expr (loc, arg1, code2, arg2, complain);
7744 :
7745 20800575 : case INDIRECT_REF:
7746 20800575 : return cp_build_indirect_ref (loc, arg1, RO_UNARY_STAR, complain);
7747 :
7748 18499015 : case TRUTH_ANDIF_EXPR:
7749 18499015 : case TRUTH_ORIF_EXPR:
7750 18499015 : case TRUTH_AND_EXPR:
7751 18499015 : case TRUTH_OR_EXPR:
7752 18499015 : if ((complain & tf_warning) && !processing_template_decl)
7753 7751500 : warn_logical_operator (loc, code, boolean_type_node,
7754 : code_orig_arg1, arg1,
7755 : code_orig_arg2, arg2);
7756 : /* Fall through. */
7757 65109527 : case GT_EXPR:
7758 65109527 : case LT_EXPR:
7759 65109527 : case GE_EXPR:
7760 65109527 : case LE_EXPR:
7761 65109527 : case EQ_EXPR:
7762 65109527 : case NE_EXPR:
7763 65109527 : if ((complain & tf_warning)
7764 58990347 : && ((code_orig_arg1 == BOOLEAN_TYPE)
7765 58990347 : ^ (code_orig_arg2 == BOOLEAN_TYPE)))
7766 26539 : maybe_warn_bool_compare (loc, code, arg1, arg2);
7767 58990347 : if (complain & tf_warning && warn_tautological_compare)
7768 452314 : warn_tautological_cmp (loc, code, arg1, arg2);
7769 : /* Fall through. */
7770 135585745 : case SPACESHIP_EXPR:
7771 135585745 : case PLUS_EXPR:
7772 135585745 : case MINUS_EXPR:
7773 135585745 : case MULT_EXPR:
7774 135585745 : case TRUNC_DIV_EXPR:
7775 135585745 : case MAX_EXPR:
7776 135585745 : case MIN_EXPR:
7777 135585745 : case LSHIFT_EXPR:
7778 135585745 : case RSHIFT_EXPR:
7779 135585745 : case TRUNC_MOD_EXPR:
7780 135585745 : case BIT_AND_EXPR:
7781 135585745 : case BIT_IOR_EXPR:
7782 135585745 : case BIT_XOR_EXPR:
7783 135585745 : return cp_build_binary_op (loc, code, arg1, arg2, complain);
7784 :
7785 31333059 : case UNARY_PLUS_EXPR:
7786 31333059 : case NEGATE_EXPR:
7787 31333059 : case BIT_NOT_EXPR:
7788 31333059 : case TRUTH_NOT_EXPR:
7789 31333059 : case PREINCREMENT_EXPR:
7790 31333059 : case POSTINCREMENT_EXPR:
7791 31333059 : case PREDECREMENT_EXPR:
7792 31333059 : case POSTDECREMENT_EXPR:
7793 31333059 : case REALPART_EXPR:
7794 31333059 : case IMAGPART_EXPR:
7795 31333059 : case ABS_EXPR:
7796 31333059 : case CO_AWAIT_EXPR:
7797 31333059 : return cp_build_unary_op (code, arg1, false, complain);
7798 :
7799 2443211 : case ARRAY_REF:
7800 2443211 : return cp_build_array_ref (input_location, arg1, arg2, complain);
7801 :
7802 769 : case MEMBER_REF:
7803 769 : return build_m_component_ref (cp_build_indirect_ref (loc, arg1,
7804 : RO_ARROW_STAR,
7805 : complain),
7806 769 : arg2, complain);
7807 :
7808 : /* The caller will deal with these. */
7809 : case ADDR_EXPR:
7810 : case COMPONENT_REF:
7811 : case COMPOUND_EXPR:
7812 : return NULL_TREE;
7813 :
7814 0 : default:
7815 0 : gcc_unreachable ();
7816 : }
7817 : return NULL_TREE;
7818 219406573 : }
7819 :
7820 : /* Build a new call to operator[]. This may change ARGS. */
7821 :
7822 : tree
7823 704 : build_op_subscript (const op_location_t &loc, tree obj,
7824 : vec<tree, va_gc> **args, tree *overload,
7825 : tsubst_flags_t complain)
7826 : {
7827 704 : struct z_candidate *candidates = 0, *cand;
7828 704 : tree fns, first_mem_arg = NULL_TREE;
7829 704 : bool any_viable_p;
7830 704 : tree result = NULL_TREE;
7831 :
7832 704 : auto_cond_timevar tv (TV_OVERLOAD);
7833 :
7834 704 : obj = mark_lvalue_use (obj);
7835 :
7836 704 : if (error_operand_p (obj))
7837 0 : return error_mark_node;
7838 :
7839 704 : tree type = TREE_TYPE (obj);
7840 :
7841 704 : obj = prep_operand (obj);
7842 :
7843 704 : if (TYPE_BINFO (type))
7844 : {
7845 704 : fns = lookup_fnfields (TYPE_BINFO (type), ovl_op_identifier (ARRAY_REF),
7846 : 1, complain);
7847 704 : if (fns == error_mark_node)
7848 : return error_mark_node;
7849 : }
7850 : else
7851 : fns = NULL_TREE;
7852 :
7853 704 : if (args != NULL && *args != NULL)
7854 : {
7855 704 : *args = resolve_args (*args, complain);
7856 704 : if (*args == NULL)
7857 0 : return error_mark_node;
7858 : }
7859 :
7860 704 : conversion_obstack_sentinel cos;
7861 :
7862 704 : if (fns)
7863 : {
7864 702 : first_mem_arg = obj;
7865 :
7866 702 : add_candidates (BASELINK_FUNCTIONS (fns),
7867 : first_mem_arg, *args, NULL_TREE,
7868 : NULL_TREE, false,
7869 702 : BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
7870 : LOOKUP_NORMAL, &candidates, complain);
7871 : }
7872 :
7873 : /* Be strict here because if we choose a bad conversion candidate, the
7874 : errors we get won't mention the call context. */
7875 704 : candidates = splice_viable (candidates, true, &any_viable_p);
7876 704 : if (!any_viable_p)
7877 : {
7878 45 : if (complain & tf_error)
7879 : {
7880 1 : auto_diagnostic_group d;
7881 2 : error ("no match for call to %<%T::operator[] (%A)%>",
7882 1 : TREE_TYPE (obj), build_tree_list_vec (*args));
7883 1 : print_z_candidates (loc, candidates);
7884 1 : }
7885 45 : result = error_mark_node;
7886 : }
7887 : else
7888 : {
7889 659 : cand = tourney (candidates, complain);
7890 659 : if (cand == 0)
7891 : {
7892 0 : if (complain & tf_error)
7893 : {
7894 0 : auto_diagnostic_group d;
7895 0 : error ("call of %<%T::operator[] (%A)%> is ambiguous",
7896 0 : TREE_TYPE (obj), build_tree_list_vec (*args));
7897 0 : print_z_candidates (loc, candidates);
7898 0 : }
7899 0 : result = error_mark_node;
7900 : }
7901 659 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL
7902 659 : && DECL_OVERLOADED_OPERATOR_P (cand->fn)
7903 1318 : && DECL_OVERLOADED_OPERATOR_IS (cand->fn, ARRAY_REF))
7904 : {
7905 659 : if (overload)
7906 659 : *overload = cand->fn;
7907 659 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
7908 1318 : if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn))
7909 : /* There won't be a CALL_EXPR. */;
7910 659 : else if (result && result != error_mark_node)
7911 : {
7912 659 : tree call = extract_call_expr (result);
7913 659 : CALL_EXPR_OPERATOR_SYNTAX (call) = true;
7914 :
7915 : /* Specify evaluation order as per P0145R2. */
7916 659 : CALL_EXPR_ORDERED_ARGS (call) = op_is_ordered (ARRAY_REF) == 1;
7917 : }
7918 :
7919 : /* In an expression of the form `a[]' where cand->fn
7920 : which is operator[] turns out to be a static member function,
7921 : `a' is none-the-less evaluated. */
7922 659 : result = keep_unused_object_arg (result, obj, cand->fn);
7923 : }
7924 : else
7925 0 : gcc_unreachable ();
7926 : }
7927 :
7928 704 : return result;
7929 704 : }
7930 :
7931 : /* CALL was returned by some call-building function; extract the actual
7932 : CALL_EXPR from any bits that have been tacked on, e.g. by
7933 : convert_from_reference. */
7934 :
7935 : tree
7936 45699023 : extract_call_expr (tree call)
7937 : {
7938 45699725 : while (TREE_CODE (call) == COMPOUND_EXPR)
7939 702 : call = TREE_OPERAND (call, 1);
7940 45699023 : if (REFERENCE_REF_P (call))
7941 10453923 : call = TREE_OPERAND (call, 0);
7942 45699023 : if (TREE_CODE (call) == TARGET_EXPR)
7943 3110641 : call = TARGET_EXPR_INITIAL (call);
7944 :
7945 45699023 : if (TREE_CODE (call) != CALL_EXPR
7946 857088 : && TREE_CODE (call) != AGGR_INIT_EXPR
7947 750086 : && call != error_mark_node)
7948 750068 : return NULL_TREE;
7949 : return call;
7950 : }
7951 :
7952 : /* Returns true if FN has two parameters, of which the second has type
7953 : size_t. */
7954 :
7955 : static bool
7956 679360 : second_parm_is_size_t (tree fn)
7957 : {
7958 679360 : tree t = FUNCTION_ARG_CHAIN (fn);
7959 679360 : if (!t || !same_type_p (TREE_VALUE (t), size_type_node))
7960 : return false;
7961 15 : t = TREE_CHAIN (t);
7962 15 : if (t == void_list_node)
7963 9 : return true;
7964 : return false;
7965 : }
7966 :
7967 : /* True if T, an allocation function, has std::align_val_t as its second
7968 : argument. */
7969 :
7970 : bool
7971 352502 : aligned_allocation_fn_p (tree t)
7972 : {
7973 352502 : if (!aligned_new_threshold)
7974 : return false;
7975 :
7976 342680 : tree a = FUNCTION_ARG_CHAIN (t);
7977 342680 : return (a && same_type_p (TREE_VALUE (a), align_type_node));
7978 : }
7979 :
7980 : /* True if T is std::destroying_delete_t. */
7981 :
7982 : static bool
7983 5836949 : std_destroying_delete_t_p (tree t)
7984 : {
7985 5836949 : return (TYPE_CONTEXT (t) == std_node
7986 5836949 : && id_equal (TYPE_IDENTIFIER (t), "destroying_delete_t"));
7987 : }
7988 :
7989 : /* A deallocation function with at least two parameters whose second parameter
7990 : type is of type std::destroying_delete_t is a destroying operator delete. A
7991 : destroying operator delete shall be a class member function named operator
7992 : delete. [ Note: Array deletion cannot use a destroying operator
7993 : delete. --end note ] */
7994 :
7995 : tree
7996 5836964 : destroying_delete_p (tree t)
7997 : {
7998 5836964 : tree a = TYPE_ARG_TYPES (TREE_TYPE (t));
7999 5836964 : if (!a || !TREE_CHAIN (a))
8000 : return NULL_TREE;
8001 5836949 : tree type = TREE_VALUE (TREE_CHAIN (a));
8002 5836949 : return std_destroying_delete_t_p (type) ? type : NULL_TREE;
8003 : }
8004 :
8005 : struct dealloc_info
8006 : {
8007 : bool sized;
8008 : bool aligned;
8009 : tree destroying;
8010 : };
8011 :
8012 : /* Returns true iff T, an element of an OVERLOAD chain, is a usual deallocation
8013 : function (3.7.4.2 [basic.stc.dynamic.deallocation]). If so, and DI is
8014 : non-null, also set *DI. */
8015 :
8016 : static bool
8017 3987169 : usual_deallocation_fn_p (tree t, dealloc_info *di)
8018 : {
8019 3987169 : if (di) *di = dealloc_info();
8020 :
8021 : /* A template instance is never a usual deallocation function,
8022 : regardless of its signature. */
8023 3987169 : if (TREE_CODE (t) == TEMPLATE_DECL
8024 3987169 : || primary_template_specialization_p (t))
8025 : return false;
8026 :
8027 : /* A usual deallocation function is a deallocation function whose parameters
8028 : after the first are
8029 : - optionally, a parameter of type std::destroying_delete_t, then
8030 : - optionally, a parameter of type std::size_t, then
8031 : - optionally, a parameter of type std::align_val_t. */
8032 3987157 : bool global = DECL_NAMESPACE_SCOPE_P (t);
8033 3987157 : tree chain = FUNCTION_ARG_CHAIN (t);
8034 3987157 : if (chain && destroying_delete_p (t))
8035 : {
8036 86 : if (di) di->destroying = TREE_VALUE (chain);
8037 86 : chain = TREE_CHAIN (chain);
8038 : }
8039 3987157 : if (chain
8040 3987154 : && (!global || flag_sized_deallocation)
8041 7958952 : && same_type_p (TREE_VALUE (chain), size_type_node))
8042 : {
8043 1149480 : if (di) di->sized = true;
8044 1149480 : chain = TREE_CHAIN (chain);
8045 : }
8046 3987154 : if (chain && aligned_new_threshold
8047 7957919 : && same_type_p (TREE_VALUE (chain), align_type_node))
8048 : {
8049 1706301 : if (di) di->aligned = true;
8050 1706301 : chain = TREE_CHAIN (chain);
8051 : }
8052 3987157 : return (chain == void_list_node);
8053 : }
8054 :
8055 : /* Just return whether FN is a usual deallocation function. */
8056 :
8057 : bool
8058 8883 : usual_deallocation_fn_p (tree fn)
8059 : {
8060 8883 : return usual_deallocation_fn_p (fn, NULL);
8061 : }
8062 :
8063 : /* Build a call to operator delete. This has to be handled very specially,
8064 : because the restrictions on what signatures match are different from all
8065 : other call instances. For a normal delete, only a delete taking (void *)
8066 : or (void *, size_t) is accepted. For a placement delete, only an exact
8067 : match with the placement new is accepted.
8068 :
8069 : CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
8070 : ADDR is the pointer to be deleted.
8071 : SIZE is the size of the memory block to be deleted.
8072 : GLOBAL_P is true if the delete-expression should not consider
8073 : class-specific delete operators.
8074 : CORO_P is true if the allocation is for a coroutine, where the two argument
8075 : usual deallocation should be chosen in preference to the single argument
8076 : version in a class context.
8077 : PLACEMENT is the corresponding placement new call, or NULL_TREE.
8078 :
8079 : If this call to "operator delete" is being generated as part to
8080 : deallocate memory allocated via a new-expression (as per [expr.new]
8081 : which requires that if the initialization throws an exception then
8082 : we call a deallocation function), then ALLOC_FN is the allocation
8083 : function. */
8084 :
8085 : static tree
8086 1260122 : build_op_delete_call_1 (enum tree_code code, tree addr, tree size,
8087 : bool global_p, bool coro_p, tree placement,
8088 : tree alloc_fn, tsubst_flags_t complain)
8089 : {
8090 1260122 : tree fn = NULL_TREE;
8091 1260122 : tree fns, fnname, type, t;
8092 1260122 : dealloc_info di_fn = { };
8093 :
8094 1260122 : if (addr == error_mark_node)
8095 : return error_mark_node;
8096 :
8097 1260122 : type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
8098 :
8099 1260122 : fnname = ovl_op_identifier (false, code);
8100 :
8101 874533 : if (CLASS_TYPE_P (type)
8102 874497 : && COMPLETE_TYPE_P (complete_type (type))
8103 2134584 : && !global_p)
8104 : /* In [class.free]
8105 :
8106 : If the result of the lookup is ambiguous or inaccessible, or if
8107 : the lookup selects a placement deallocation function, the
8108 : program is ill-formed.
8109 :
8110 : Therefore, we ask lookup_fnfields to complain about ambiguity. */
8111 : {
8112 516827 : fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1, complain);
8113 516827 : if (fns == error_mark_node)
8114 : return error_mark_node;
8115 : }
8116 : else
8117 : fns = NULL_TREE;
8118 :
8119 516824 : if (fns == NULL_TREE)
8120 1259047 : fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE);
8121 :
8122 : /* Strip const and volatile from addr. */
8123 1260119 : tree oaddr = addr;
8124 1260119 : addr = cp_convert (ptr_type_node, addr, complain);
8125 :
8126 1260119 : tree excluded_destroying = NULL_TREE;
8127 :
8128 1260119 : if (placement)
8129 : {
8130 : /* "A declaration of a placement deallocation function matches the
8131 : declaration of a placement allocation function if it has the same
8132 : number of parameters and, after parameter transformations (8.3.5),
8133 : all parameter types except the first are identical."
8134 :
8135 : So we build up the function type we want and ask instantiate_type
8136 : to get it for us. */
8137 680334 : t = FUNCTION_ARG_CHAIN (alloc_fn);
8138 680334 : t = tree_cons (NULL_TREE, ptr_type_node, t);
8139 680334 : t = build_function_type (void_type_node, t);
8140 :
8141 680334 : fn = instantiate_type (t, fns, tf_none);
8142 680334 : if (fn == error_mark_node)
8143 : return NULL_TREE;
8144 :
8145 679360 : fn = MAYBE_BASELINK_FUNCTIONS (fn);
8146 :
8147 : /* "If the lookup finds the two-parameter form of a usual deallocation
8148 : function (3.7.4.2) and that function, considered as a placement
8149 : deallocation function, would have been selected as a match for the
8150 : allocation function, the program is ill-formed." */
8151 679360 : if (second_parm_is_size_t (fn))
8152 : {
8153 9 : const char *const msg1
8154 : = G_("exception cleanup for this placement new selects "
8155 : "non-placement %<operator delete%>");
8156 9 : const char *const msg2
8157 : = G_("%qD is a usual (non-placement) deallocation "
8158 : "function in C++14 (or with %<-fsized-deallocation%>)");
8159 :
8160 : /* But if the class has an operator delete (void *), then that is
8161 : the usual deallocation function, so we shouldn't complain
8162 : about using the operator delete (void *, size_t). */
8163 9 : if (DECL_CLASS_SCOPE_P (fn))
8164 18 : for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns)))
8165 : {
8166 9 : if (usual_deallocation_fn_p (elt)
8167 9 : && FUNCTION_ARG_CHAIN (elt) == void_list_node)
8168 3 : goto ok;
8169 : }
8170 : /* Before C++14 a two-parameter global deallocation function is
8171 : always a placement deallocation function, but warn if
8172 : -Wc++14-compat. */
8173 3 : else if (!flag_sized_deallocation)
8174 : {
8175 1 : if (complain & tf_warning)
8176 : {
8177 1 : auto_diagnostic_group d;
8178 1 : if (warning (OPT_Wc__14_compat, msg1))
8179 1 : inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
8180 1 : }
8181 1 : goto ok;
8182 : }
8183 :
8184 5 : if (complain & tf_warning_or_error)
8185 : {
8186 5 : auto_diagnostic_group d;
8187 5 : if (permerror (input_location, msg1))
8188 : {
8189 : /* Only mention C++14 for namespace-scope delete. */
8190 5 : if (DECL_NAMESPACE_SCOPE_P (fn))
8191 2 : inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
8192 : else
8193 3 : inform (DECL_SOURCE_LOCATION (fn),
8194 : "%qD is a usual (non-placement) deallocation "
8195 : "function", fn);
8196 : }
8197 5 : }
8198 : else
8199 0 : return error_mark_node;
8200 1259145 : ok:;
8201 : }
8202 : }
8203 : else
8204 : /* "Any non-placement deallocation function matches a non-placement
8205 : allocation function. If the lookup finds a single matching
8206 : deallocation function, that function will be called; otherwise, no
8207 : deallocation function will be called." */
8208 5137856 : for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns)))
8209 : {
8210 3978286 : dealloc_info di_elt;
8211 3978286 : if (usual_deallocation_fn_p (elt, &di_elt))
8212 : {
8213 : /* If we're called for an EH cleanup in a new-expression, we can't
8214 : use a destroying delete; the exception was thrown before the
8215 : object was constructed. */
8216 2297455 : if (alloc_fn && di_elt.destroying)
8217 : {
8218 13 : excluded_destroying = elt;
8219 1170954 : continue;
8220 : }
8221 :
8222 2297442 : if (!fn)
8223 : {
8224 579760 : fn = elt;
8225 579760 : di_fn = di_elt;
8226 579760 : continue;
8227 : }
8228 :
8229 : /* -- If any of the deallocation functions is a destroying
8230 : operator delete, all deallocation functions that are not
8231 : destroying operator deletes are eliminated from further
8232 : consideration. */
8233 1717682 : if (di_elt.destroying != di_fn.destroying)
8234 : {
8235 12 : if (di_elt.destroying)
8236 : {
8237 6 : fn = elt;
8238 6 : di_fn = di_elt;
8239 : }
8240 12 : continue;
8241 : }
8242 :
8243 : /* -- If the type has new-extended alignment, a function with a
8244 : parameter of type std::align_val_t is preferred; otherwise a
8245 : function without such a parameter is preferred. If exactly one
8246 : preferred function is found, that function is selected and the
8247 : selection process terminates. If more than one preferred
8248 : function is found, all non-preferred functions are eliminated
8249 : from further consideration. */
8250 1717670 : if (aligned_new_threshold)
8251 : {
8252 1717403 : bool want_align = type_has_new_extended_alignment (type);
8253 1717403 : if (di_elt.aligned != di_fn.aligned)
8254 : {
8255 591169 : if (want_align == di_elt.aligned)
8256 : {
8257 553754 : fn = elt;
8258 553754 : di_fn = di_elt;
8259 : }
8260 591169 : continue;
8261 : }
8262 : }
8263 :
8264 : /* -- If the deallocation functions have class scope, the one
8265 : without a parameter of type std::size_t is selected. */
8266 1126501 : bool want_size;
8267 1126501 : if (DECL_CLASS_SCOPE_P (fn) && !coro_p)
8268 : want_size = false;
8269 :
8270 : /* -- If the type is complete and if, for the second alternative
8271 : (delete array) only, the operand is a pointer to a class type
8272 : with a non-trivial destructor or a (possibly multi-dimensional)
8273 : array thereof, the function with a parameter of type std::size_t
8274 : is selected.
8275 :
8276 : -- Otherwise, it is unspecified whether a deallocation function
8277 : with a parameter of type std::size_t is selected. */
8278 : else
8279 : {
8280 1126393 : want_size = COMPLETE_TYPE_P (type);
8281 1126393 : if (code == VEC_DELETE_EXPR
8282 1126393 : && !TYPE_VEC_NEW_USES_COOKIE (type))
8283 : /* We need a cookie to determine the array size. */
8284 : want_size = false;
8285 : }
8286 1126501 : gcc_assert (di_fn.sized != di_elt.sized);
8287 1126501 : if (want_size == di_elt.sized)
8288 : {
8289 45893 : fn = elt;
8290 45893 : di_fn = di_elt;
8291 : }
8292 : }
8293 : }
8294 :
8295 : /* If we have a matching function, call it. */
8296 1259145 : if (fn)
8297 : {
8298 1259120 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8299 :
8300 : /* If the FN is a member function, make sure that it is
8301 : accessible. */
8302 1259120 : if (BASELINK_P (fns))
8303 1005 : perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn,
8304 : complain);
8305 :
8306 : /* Core issue 901: It's ok to new a type with deleted delete. */
8307 1259120 : if (DECL_DELETED_FN (fn) && alloc_fn)
8308 : return NULL_TREE;
8309 :
8310 1259114 : tree ret;
8311 1259114 : if (placement)
8312 : {
8313 : /* The placement args might not be suitable for overload
8314 : resolution at this point, so build the call directly. */
8315 679360 : int nargs = call_expr_nargs (placement);
8316 679360 : tree *argarray = XALLOCAVEC (tree, nargs);
8317 679360 : int i;
8318 679360 : argarray[0] = addr;
8319 1358772 : for (i = 1; i < nargs; i++)
8320 679412 : argarray[i] = CALL_EXPR_ARG (placement, i);
8321 679360 : if (!mark_used (fn, complain) && !(complain & tf_error))
8322 0 : return error_mark_node;
8323 679360 : ret = build_cxx_call (fn, nargs, argarray, complain);
8324 : }
8325 : else
8326 : {
8327 579754 : tree destroying = di_fn.destroying;
8328 579754 : if (destroying)
8329 : {
8330 : /* Strip const and volatile from addr but retain the type of the
8331 : object. */
8332 28 : tree rtype = TREE_TYPE (TREE_TYPE (oaddr));
8333 28 : rtype = cv_unqualified (rtype);
8334 28 : rtype = TYPE_POINTER_TO (rtype);
8335 28 : addr = cp_convert (rtype, oaddr, complain);
8336 28 : destroying = build_functional_cast (input_location,
8337 : destroying, NULL_TREE,
8338 : complain);
8339 : }
8340 :
8341 579754 : releasing_vec args;
8342 579754 : args->quick_push (addr);
8343 579754 : if (destroying)
8344 28 : args->quick_push (destroying);
8345 579754 : if (di_fn.sized)
8346 540727 : args->quick_push (size);
8347 579754 : if (di_fn.aligned)
8348 : {
8349 18711 : tree al = build_int_cst (align_type_node, TYPE_ALIGN_UNIT (type));
8350 18711 : args->quick_push (al);
8351 : }
8352 579754 : ret = cp_build_function_call_vec (fn, &args, complain);
8353 579754 : }
8354 :
8355 : /* Set this flag for all callers of this function. In addition to
8356 : delete-expressions, this is called for deallocating coroutine state;
8357 : treat that as an implicit delete-expression. This is also called for
8358 : the delete if the constructor throws in a new-expression, and for a
8359 : deleting destructor (which implements a delete-expression). */
8360 : /* But leave this flag off for destroying delete to avoid wrong
8361 : assumptions in the optimizers. */
8362 1259114 : tree call = extract_call_expr (ret);
8363 1259114 : if (TREE_CODE (call) == CALL_EXPR && !destroying_delete_p (fn))
8364 1259080 : CALL_FROM_NEW_OR_DELETE_P (call) = 1;
8365 :
8366 : return ret;
8367 : }
8368 :
8369 : /* If there's only a destroying delete that we can't use because the
8370 : object isn't constructed yet, and we used global new, use global
8371 : delete as well. */
8372 25 : if (excluded_destroying
8373 25 : && DECL_NAMESPACE_SCOPE_P (alloc_fn))
8374 7 : return build_op_delete_call (code, addr, size, true, placement,
8375 7 : alloc_fn, complain);
8376 :
8377 : /* [expr.new]
8378 :
8379 : If no unambiguous matching deallocation function can be found,
8380 : propagating the exception does not cause the object's memory to
8381 : be freed. */
8382 18 : if (alloc_fn)
8383 : {
8384 15 : if ((complain & tf_warning) && !placement)
8385 : {
8386 15 : auto_diagnostic_group d;
8387 15 : bool w = warning (0,
8388 : "no corresponding deallocation function for %qD",
8389 : alloc_fn);
8390 15 : if (w && excluded_destroying)
8391 3 : inform (DECL_SOURCE_LOCATION (excluded_destroying), "destroying "
8392 : "delete %qD cannot be used to release the allocated memory"
8393 : " if the initialization throws because the object is not "
8394 : "constructed yet", excluded_destroying);
8395 15 : }
8396 : return NULL_TREE;
8397 : }
8398 :
8399 3 : if (complain & tf_error)
8400 3 : error ("no suitable %<operator %s%> for %qT",
8401 3 : OVL_OP_INFO (false, code)->name, type);
8402 3 : return error_mark_node;
8403 : }
8404 :
8405 : /* Arguments as per build_op_delete_call_1 (). */
8406 :
8407 : tree
8408 1256861 : build_op_delete_call (enum tree_code code, tree addr, tree size, bool global_p,
8409 : tree placement, tree alloc_fn, tsubst_flags_t complain)
8410 : {
8411 1256861 : return build_op_delete_call_1 (code, addr, size, global_p, /*coro_p*/false,
8412 1256861 : placement, alloc_fn, complain);
8413 : }
8414 :
8415 : /* Arguments as per build_op_delete_call_1 (). */
8416 :
8417 : tree
8418 3261 : build_coroutine_op_delete_call (enum tree_code code, tree addr, tree size,
8419 : bool global_p, tree placement, tree alloc_fn,
8420 : tsubst_flags_t complain)
8421 : {
8422 3261 : return build_op_delete_call_1 (code, addr, size, global_p, /*coro_p*/true,
8423 3261 : placement, alloc_fn, complain);
8424 : }
8425 :
8426 : /* Issue diagnostics about a disallowed access of DECL, using DIAG_DECL
8427 : in the diagnostics.
8428 :
8429 : If ISSUE_ERROR is true, then issue an error about the access, followed
8430 : by a note showing the declaration. Otherwise, just show the note.
8431 :
8432 : DIAG_DECL and DIAG_LOCATION will almost always be the same.
8433 : DIAG_LOCATION is just another DECL. NO_ACCESS_REASON is an optional
8434 : parameter used to specify why DECL wasn't accessible (e.g. ak_private
8435 : would be because DECL was private). If not using NO_ACCESS_REASON,
8436 : then it must be ak_none, and the access failure reason will be
8437 : figured out by looking at the protection of DECL. */
8438 :
8439 : void
8440 1187 : complain_about_access (tree decl, tree diag_decl, tree diag_location,
8441 : bool issue_error, access_kind no_access_reason)
8442 : {
8443 : /* If we have not already figured out why DECL is inaccessible... */
8444 1187 : if (no_access_reason == ak_none)
8445 : {
8446 : /* Examine the access of DECL to find out why. */
8447 1005 : if (TREE_PRIVATE (decl))
8448 : no_access_reason = ak_private;
8449 225 : else if (TREE_PROTECTED (decl))
8450 180 : no_access_reason = ak_protected;
8451 : }
8452 :
8453 : /* Now generate an error message depending on calculated access. */
8454 1187 : auto_diagnostic_group d;
8455 1187 : if (no_access_reason == ak_private)
8456 : {
8457 962 : if (issue_error)
8458 948 : error ("%q#D is private within this context", diag_decl);
8459 962 : inform (DECL_SOURCE_LOCATION (diag_location), "declared private here");
8460 : }
8461 225 : else if (no_access_reason == ak_protected)
8462 : {
8463 180 : if (issue_error)
8464 166 : error ("%q#D is protected within this context", diag_decl);
8465 180 : inform (DECL_SOURCE_LOCATION (diag_location), "declared protected here");
8466 : }
8467 : /* Couldn't figure out why DECL is inaccessible, so just say it's
8468 : inaccessible. */
8469 : else
8470 : {
8471 45 : if (issue_error)
8472 45 : error ("%q#D is inaccessible within this context", diag_decl);
8473 45 : inform (DECL_SOURCE_LOCATION (diag_decl), "declared here");
8474 : }
8475 1187 : }
8476 :
8477 : /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
8478 : bitwise or of LOOKUP_* values. If any errors are warnings are
8479 : generated, set *DIAGNOSTIC_FN to "error" or "warning",
8480 : respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
8481 : to NULL. */
8482 :
8483 : static tree
8484 14328204 : build_temp (tree expr, tree type, int flags,
8485 : enum diagnostics::kind *diagnostic_kind, tsubst_flags_t complain)
8486 : {
8487 14328204 : int savew, savee;
8488 :
8489 14328204 : *diagnostic_kind = diagnostics::kind::unspecified;
8490 :
8491 : /* If the source is a packed field, calling the copy constructor will require
8492 : binding the field to the reference parameter to the copy constructor, and
8493 : we'll end up with an infinite loop. If we can use a bitwise copy, then
8494 : do that now. */
8495 14328204 : if ((lvalue_kind (expr) & clk_packed)
8496 6 : && CLASS_TYPE_P (TREE_TYPE (expr))
8497 14328210 : && !type_has_nontrivial_copy_init (TREE_TYPE (expr)))
8498 3 : return get_target_expr (expr, complain);
8499 :
8500 : /* In decltype, we might have decided not to wrap this call in a TARGET_EXPR.
8501 : But it turns out to be a subexpression, so perform temporary
8502 : materialization now. */
8503 14328201 : if (TREE_CODE (expr) == CALL_EXPR
8504 6 : && CLASS_TYPE_P (type)
8505 14328207 : && same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
8506 3 : expr = build_cplus_new (type, expr, complain);
8507 :
8508 14328201 : savew = warningcount + werrorcount, savee = errorcount;
8509 14328201 : releasing_vec args (make_tree_vector_single (expr));
8510 14328201 : expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
8511 : &args, type, flags, complain);
8512 14328201 : if (warningcount + werrorcount > savew)
8513 0 : *diagnostic_kind = diagnostics::kind::warning;
8514 14328201 : else if (errorcount > savee)
8515 78 : *diagnostic_kind = diagnostics::kind::error;
8516 14328201 : return expr;
8517 14328201 : }
8518 :
8519 : /* Get any location for EXPR, falling back to input_location.
8520 :
8521 : If the result is in a system header and is the virtual location for
8522 : a token coming from the expansion of a macro, unwind it to the
8523 : location of the expansion point of the macro (e.g. to avoid the
8524 : diagnostic being suppressed for expansions of NULL where "NULL" is
8525 : in a system header). */
8526 :
8527 : static location_t
8528 2433934 : get_location_for_expr_unwinding_for_system_header (tree expr)
8529 : {
8530 2433934 : location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
8531 2433934 : loc = expansion_point_location_if_in_system_header (loc);
8532 2433934 : return loc;
8533 : }
8534 :
8535 : /* Perform warnings about peculiar, but valid, conversions from/to NULL.
8536 : Also handle a subset of zero as null warnings.
8537 : EXPR is implicitly converted to type TOTYPE.
8538 : FN and ARGNUM are used for diagnostics. */
8539 :
8540 : static void
8541 547937632 : conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
8542 : {
8543 : /* Issue warnings about peculiar, but valid, uses of NULL. */
8544 547937632 : if (TREE_CODE (totype) != BOOLEAN_TYPE
8545 : && ARITHMETIC_TYPE_P (totype)
8546 187884083 : && null_node_p (expr))
8547 : {
8548 94 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8549 94 : if (fn)
8550 : {
8551 33 : auto_diagnostic_group d;
8552 33 : if (warning_at (loc, OPT_Wconversion_null,
8553 : "passing NULL to non-pointer argument %P of %qD",
8554 : argnum, fn))
8555 27 : inform (get_fndecl_argument_location (fn, argnum),
8556 : "declared here");
8557 33 : }
8558 : else
8559 61 : warning_at (loc, OPT_Wconversion_null,
8560 : "converting to non-pointer type %qT from NULL", totype);
8561 : }
8562 :
8563 : /* Issue warnings if "false" is converted to a NULL pointer */
8564 547937538 : else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
8565 547937538 : && TYPE_PTR_P (totype))
8566 : {
8567 7 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8568 7 : if (fn)
8569 : {
8570 3 : auto_diagnostic_group d;
8571 3 : if (warning_at (loc, OPT_Wconversion_null,
8572 : "converting %<false%> to pointer type for argument "
8573 : "%P of %qD", argnum, fn))
8574 3 : inform (get_fndecl_argument_location (fn, argnum),
8575 : "declared here");
8576 3 : }
8577 : else
8578 4 : warning_at (loc, OPT_Wconversion_null,
8579 : "converting %<false%> to pointer type %qT", totype);
8580 : }
8581 : /* Handle zero as null pointer warnings for cases other
8582 : than EQ_EXPR and NE_EXPR */
8583 504746949 : else if ((TYPE_PTR_OR_PTRMEM_P (totype) || NULLPTR_TYPE_P (totype))
8584 548253906 : && null_ptr_cst_p (expr))
8585 : {
8586 2433833 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8587 2433833 : maybe_warn_zero_as_null_pointer_constant (expr, loc);
8588 : }
8589 547937632 : }
8590 :
8591 : /* We gave a diagnostic during a conversion. If this was in the second
8592 : standard conversion sequence of a user-defined conversion sequence, say
8593 : which user-defined conversion. */
8594 :
8595 : static void
8596 5260 : maybe_print_user_conv_context (conversion *convs)
8597 : {
8598 5260 : if (convs->user_conv_p)
8599 167 : for (conversion *t = convs; t; t = next_conversion (t))
8600 142 : if (t->kind == ck_user)
8601 : {
8602 43 : print_z_candidate (0, N_(" after user-defined conversion:"),
8603 : t->cand);
8604 43 : break;
8605 : }
8606 5260 : }
8607 :
8608 : /* Locate the parameter with the given index within FNDECL.
8609 : ARGNUM is zero based, -1 indicates the `this' argument of a method.
8610 : Return the location of the FNDECL itself if there are problems. */
8611 :
8612 : location_t
8613 9112697 : get_fndecl_argument_location (tree fndecl, int argnum)
8614 : {
8615 : /* The locations of implicitly-declared functions are likely to be
8616 : more meaningful than those of their parameters. */
8617 9112697 : if (DECL_ARTIFICIAL (fndecl))
8618 297 : return DECL_SOURCE_LOCATION (fndecl);
8619 :
8620 9112400 : if (argnum == -1)
8621 51 : return DECL_SOURCE_LOCATION (fndecl);
8622 :
8623 9112349 : int i;
8624 9112349 : tree param;
8625 :
8626 : /* Locate param by index within DECL_ARGUMENTS (fndecl). */
8627 9112349 : for (i = 0, param = FUNCTION_FIRST_USER_PARM (fndecl);
8628 22886707 : i < argnum && param;
8629 13774358 : i++, param = TREE_CHAIN (param))
8630 : ;
8631 :
8632 : /* If something went wrong (e.g. if we have a builtin and thus no arguments),
8633 : return the location of FNDECL. */
8634 9112349 : if (param == NULL)
8635 40 : return DECL_SOURCE_LOCATION (fndecl);
8636 :
8637 9112309 : return DECL_SOURCE_LOCATION (param);
8638 : }
8639 :
8640 : /* If FNDECL is non-NULL, issue a note highlighting ARGNUM
8641 : within its declaration (or the fndecl itself if something went
8642 : wrong). */
8643 :
8644 : void
8645 7011 : maybe_inform_about_fndecl_for_bogus_argument_init (tree fn, int argnum,
8646 : const char *highlight_color)
8647 : {
8648 7011 : if (fn)
8649 : {
8650 1123 : gcc_rich_location richloc (get_fndecl_argument_location (fn, argnum));
8651 1123 : richloc.set_highlight_color (highlight_color);
8652 1123 : inform (&richloc,
8653 : "initializing argument %P of %qD", argnum, fn);
8654 1123 : }
8655 7011 : }
8656 :
8657 : /* Maybe warn about C++20 Conversions to arrays of unknown bound. C is
8658 : the conversion, EXPR is the expression we're converting. */
8659 :
8660 : static void
8661 39450906 : maybe_warn_array_conv (location_t loc, conversion *c, tree expr)
8662 : {
8663 39450906 : if (cxx_dialect >= cxx20)
8664 : return;
8665 :
8666 437246 : tree type = TREE_TYPE (expr);
8667 437246 : type = strip_pointer_operator (type);
8668 :
8669 437246 : if (TREE_CODE (type) != ARRAY_TYPE
8670 437246 : || TYPE_DOMAIN (type) == NULL_TREE)
8671 : return;
8672 :
8673 1858 : if (pedantic && conv_binds_to_array_of_unknown_bound (c))
8674 10 : pedwarn (loc, OPT_Wc__20_extensions,
8675 : "conversions to arrays of unknown bound "
8676 : "are only available with %<-std=c++20%> or %<-std=gnu++20%>");
8677 : }
8678 :
8679 : /* We call this recursively in convert_like_internal. */
8680 : static tree convert_like (conversion *, tree, tree, int, bool, bool, bool,
8681 : tsubst_flags_t);
8682 :
8683 : /* Adjust the result EXPR of a conversion to the expected type TOTYPE, which
8684 : must be equivalent but might be a typedef. */
8685 :
8686 : static tree
8687 926949579 : maybe_adjust_type_name (tree type, tree expr, conversion_kind kind)
8688 : {
8689 926949579 : if (expr == error_mark_node
8690 926949528 : || processing_template_decl)
8691 : return expr;
8692 :
8693 814348809 : tree etype = TREE_TYPE (expr);
8694 814348809 : if (etype == type)
8695 : return expr;
8696 :
8697 107093148 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p (etype, type)
8698 : || is_bitfield_expr_with_lowered_type (expr)
8699 : || seen_error ());
8700 :
8701 107093148 : if (SCALAR_TYPE_P (type)
8702 101211427 : && (kind == ck_rvalue
8703 : /* ??? We should be able to do this for ck_identity of more prvalue
8704 : expressions, but checking !obvalue_p here breaks, so for now let's
8705 : just handle NON_LVALUE_EXPR (such as the location wrapper for a
8706 : literal). Maybe we want to express already-rvalue in the
8707 : conversion somehow? */
8708 90782363 : || TREE_CODE (expr) == NON_LVALUE_EXPR))
8709 11991808 : expr = build_nop (type, expr);
8710 :
8711 : return expr;
8712 : }
8713 :
8714 : /* Perform the conversions in CONVS on the expression EXPR. FN and
8715 : ARGNUM are used for diagnostics. ARGNUM is zero based, -1
8716 : indicates the `this' argument of a method. INNER is nonzero when
8717 : being called to continue a conversion chain. It is negative when a
8718 : reference binding will be applied, positive otherwise. If
8719 : ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
8720 : conversions will be emitted if appropriate. If C_CAST_P is true,
8721 : this conversion is coming from a C-style cast; in that case,
8722 : conversions to inaccessible bases are permitted. */
8723 :
8724 : static tree
8725 1129563657 : convert_like_internal (conversion *convs, tree expr, tree fn, int argnum,
8726 : bool issue_conversion_warnings, bool c_cast_p,
8727 : bool nested_p, tsubst_flags_t complain)
8728 : {
8729 1129563657 : tree totype = convs->type;
8730 1129563657 : enum diagnostics::kind diag_kind;
8731 1129563657 : int flags;
8732 1129563657 : location_t loc = cp_expr_loc_or_input_loc (expr);
8733 1129563657 : const bool stub_object_p = is_stub_object (expr);
8734 :
8735 1129563657 : if (convs->bad_p && !(complain & tf_error))
8736 46696 : return error_mark_node;
8737 :
8738 1129516961 : gcc_checking_assert (!TYPE_REF_P (TREE_TYPE (expr)));
8739 :
8740 1129516961 : if (convs->bad_p
8741 6982 : && convs->kind != ck_user
8742 6887 : && convs->kind != ck_list
8743 6881 : && convs->kind != ck_ambig
8744 6881 : && (convs->kind != ck_ref_bind
8745 5267 : || (convs->user_conv_p && next_conversion (convs)->bad_p))
8746 1635 : && (convs->kind != ck_rvalue
8747 15 : || SCALAR_TYPE_P (totype))
8748 1129518587 : && convs->kind != ck_base)
8749 : {
8750 1626 : int complained = 0;
8751 1626 : conversion *t = convs;
8752 1626 : auto_diagnostic_group d;
8753 :
8754 : /* Give a helpful error if this is bad because of excess braces. */
8755 46 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8756 46 : && SCALAR_TYPE_P (totype)
8757 34 : && CONSTRUCTOR_NELTS (expr) > 0
8758 1660 : && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
8759 : {
8760 11 : complained = permerror (loc, "too many braces around initializer "
8761 : "for %qT", totype);
8762 33 : while (BRACE_ENCLOSED_INITIALIZER_P (expr)
8763 55 : && CONSTRUCTOR_NELTS (expr) == 1)
8764 22 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
8765 : }
8766 :
8767 : /* Give a helpful error if this is bad because a conversion to bool
8768 : from std::nullptr_t requires direct-initialization. */
8769 1626 : if (NULLPTR_TYPE_P (TREE_TYPE (expr))
8770 1626 : && TREE_CODE (totype) == BOOLEAN_TYPE)
8771 15 : complained = permerror (loc, "converting to %qH from %qI requires "
8772 : "direct-initialization",
8773 15 : totype, TREE_TYPE (expr));
8774 :
8775 1626 : if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (expr))
8776 105 : && SCALAR_FLOAT_TYPE_P (totype)
8777 1731 : && (extended_float_type_p (TREE_TYPE (expr))
8778 27 : || extended_float_type_p (totype)))
8779 105 : switch (cp_compare_floating_point_conversion_ranks (TREE_TYPE (expr),
8780 : totype))
8781 : {
8782 102 : case 2:
8783 102 : if (pedwarn (loc, OPT_Wnarrowing, "ISO C++ does not allow "
8784 : "converting to %qH from %qI with greater "
8785 102 : "conversion rank", totype, TREE_TYPE (expr)))
8786 1626 : complained = 1;
8787 21 : else if (!complained)
8788 1626 : complained = -1;
8789 : break;
8790 3 : case 3:
8791 3 : if (pedwarn (loc, OPT_Wnarrowing, "ISO C++ does not allow "
8792 : "converting to %qH from %qI with unordered "
8793 3 : "conversion rank", totype, TREE_TYPE (expr)))
8794 : complained = 1;
8795 0 : else if (!complained)
8796 1626 : complained = -1;
8797 : break;
8798 : default:
8799 : break;
8800 : }
8801 :
8802 3268 : for (; t ; t = next_conversion (t))
8803 : {
8804 3259 : if (t->kind == ck_user && t->cand->reason)
8805 : {
8806 52 : auto_diagnostic_group d;
8807 52 : complained = permerror (loc, "invalid user-defined conversion "
8808 52 : "from %qH to %qI", TREE_TYPE (expr),
8809 : totype);
8810 52 : if (complained)
8811 : {
8812 52 : auto_diagnostic_nesting_level sentinel;
8813 52 : print_z_candidate (loc, N_("candidate is:"), t->cand);
8814 52 : }
8815 52 : expr = convert_like (t, expr, fn, argnum,
8816 : /*issue_conversion_warnings=*/false,
8817 : /*c_cast_p=*/false, /*nested_p=*/true,
8818 : complain);
8819 52 : break;
8820 52 : }
8821 3207 : else if (t->kind == ck_user || !t->bad_p)
8822 : {
8823 1565 : expr = convert_like (t, expr, fn, argnum,
8824 : /*issue_conversion_warnings=*/false,
8825 : /*c_cast_p=*/false, /*nested_p=*/true,
8826 : complain);
8827 1565 : if (t->bad_p)
8828 0 : complained = 1;
8829 : break;
8830 : }
8831 1642 : else if (t->kind == ck_ambig)
8832 0 : return convert_like (t, expr, fn, argnum,
8833 : /*issue_conversion_warnings=*/false,
8834 : /*c_cast_p=*/false, /*nested_p=*/true,
8835 0 : complain);
8836 1642 : else if (t->kind == ck_identity)
8837 : break;
8838 : }
8839 1626 : if (!complained && stub_object_p)
8840 : {
8841 : /* An error diagnosed within a trait, don't give extra labels. */
8842 12 : error_at (loc, "invalid conversion from %qH to %qI",
8843 6 : TREE_TYPE (expr), totype);
8844 6 : complained = 1;
8845 : }
8846 1620 : else if (!complained && expr != error_mark_node)
8847 : {
8848 1435 : range_label_for_type_mismatch label (TREE_TYPE (expr), totype);
8849 1435 : gcc_rich_location richloc (loc, &label, highlight_colors::percent_h);
8850 1435 : complained = permerror (&richloc,
8851 : "invalid conversion from %qH to %qI",
8852 1435 : TREE_TYPE (expr), totype);
8853 1435 : if (complained)
8854 1380 : maybe_emit_indirection_note (loc, expr, totype);
8855 1435 : }
8856 1626 : if (convs->kind == ck_ref_bind)
8857 21 : expr = convert_to_reference (totype, expr, CONV_IMPLICIT,
8858 : LOOKUP_NORMAL, NULL_TREE,
8859 : complain);
8860 : else
8861 1605 : expr = cp_convert (totype, expr, complain);
8862 1626 : if (complained == 1)
8863 1548 : maybe_inform_about_fndecl_for_bogus_argument_init
8864 1548 : (fn, argnum, highlight_colors::percent_i);
8865 : return expr;
8866 1626 : }
8867 :
8868 1129515335 : if (issue_conversion_warnings && (complain & tf_warning))
8869 547937632 : conversion_null_warnings (totype, expr, fn, argnum);
8870 :
8871 1129515335 : switch (convs->kind)
8872 : {
8873 6698064 : case ck_user:
8874 6698064 : {
8875 6698064 : struct z_candidate *cand = convs->cand;
8876 :
8877 6698064 : if (cand == NULL)
8878 : /* We chose the surrogate function from add_conv_candidate, now we
8879 : actually need to build the conversion. */
8880 56 : cand = build_user_type_conversion_1 (totype, expr,
8881 : LOOKUP_NO_CONVERSION, complain);
8882 :
8883 6698064 : tree convfn = cand->fn;
8884 :
8885 : /* When converting from an init list we consider explicit
8886 : constructors, but actually trying to call one is an error. */
8887 7186177 : if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
8888 10093 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
8889 : /* Unless this is for direct-list-initialization. */
8890 10090 : && (!CONSTRUCTOR_IS_DIRECT_INIT (expr) || convs->need_temporary_p)
8891 : /* And in C++98 a default constructor can't be explicit. */
8892 6698304 : && cxx_dialect >= cxx11)
8893 : {
8894 238 : if (!(complain & tf_error))
8895 54 : return error_mark_node;
8896 184 : location_t loc = location_of (expr);
8897 184 : if (CONSTRUCTOR_NELTS (expr) == 0
8898 184 : && FUNCTION_FIRST_USER_PARMTYPE (convfn) != void_list_node)
8899 : {
8900 9 : auto_diagnostic_group d;
8901 9 : if (pedwarn (loc, 0, "converting to %qT from initializer list "
8902 : "would use explicit constructor %qD",
8903 : totype, convfn))
8904 : {
8905 9 : inform (DECL_SOURCE_LOCATION (convfn), "%qD declared here",
8906 : convfn);
8907 9 : inform (loc, "in C++11 and above a default constructor "
8908 : "can be explicit");
8909 : }
8910 9 : }
8911 : else
8912 : {
8913 175 : auto_diagnostic_group d;
8914 175 : error ("converting to %qT from initializer list would use "
8915 : "explicit constructor %qD", totype, convfn);
8916 175 : inform (DECL_SOURCE_LOCATION (convfn), "%qD declared here",
8917 : convfn);
8918 175 : }
8919 : }
8920 :
8921 : /* If we're initializing from {}, it's value-initialization. */
8922 878868 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8923 878846 : && CONSTRUCTOR_NELTS (expr) == 0
8924 149405 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype)
8925 6847388 : && !processing_template_decl)
8926 : {
8927 149378 : if (abstract_virtuals_error (NULL_TREE, totype, complain))
8928 12 : return error_mark_node;
8929 149366 : expr = build_value_init (totype, complain);
8930 149366 : expr = get_target_expr (expr, complain);
8931 149366 : if (expr != error_mark_node)
8932 149226 : TARGET_EXPR_LIST_INIT_P (expr) = true;
8933 : return expr;
8934 : }
8935 :
8936 : /* We don't know here whether EXPR is being used as an lvalue or
8937 : rvalue, but we know it's read. */
8938 6548632 : mark_exp_read (expr);
8939 :
8940 : /* Give the conversion call the location of EXPR rather than the
8941 : location of the context that caused the conversion. */
8942 6548632 : iloc_sentinel ils (loc);
8943 :
8944 : /* Pass LOOKUP_NO_CONVERSION so rvalue/base handling knows not to allow
8945 : any more UDCs. */
8946 6548632 : expr = build_over_call (cand, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
8947 : complain);
8948 :
8949 : /* If this is a constructor or a function returning an aggr type,
8950 : we need to build up a TARGET_EXPR. */
8951 13097264 : if (DECL_CONSTRUCTOR_P (convfn))
8952 : {
8953 2929972 : expr = build_cplus_new (totype, expr, complain);
8954 :
8955 : /* Remember that this was list-initialization. */
8956 2929972 : if (convs->check_narrowing && expr != error_mark_node)
8957 754652 : TARGET_EXPR_LIST_INIT_P (expr) = true;
8958 : }
8959 :
8960 6548632 : return expr;
8961 6548632 : }
8962 797295912 : case ck_identity:
8963 797295912 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
8964 : {
8965 1165625 : int nelts = CONSTRUCTOR_NELTS (expr);
8966 180499 : if (nelts == 0)
8967 985126 : expr = build_value_init (totype, complain);
8968 180499 : else if (nelts == 1)
8969 180499 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
8970 : else
8971 0 : gcc_unreachable ();
8972 : }
8973 797295912 : expr = mark_use (expr, /*rvalue_p=*/!convs->rvaluedness_matches_p,
8974 : /*read_p=*/true, UNKNOWN_LOCATION,
8975 : /*reject_builtin=*/true);
8976 :
8977 797295912 : if (type_unknown_p (expr))
8978 20679 : expr = instantiate_type (totype, expr, complain);
8979 797295912 : if (!nested_p && TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8980 104311 : expr = cp_convert (totype, TREE_OPERAND (expr, 0), complain);
8981 797295912 : if (expr == null_node
8982 797295912 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype))
8983 : /* If __null has been converted to an integer type, we do not want to
8984 : continue to warn about uses of EXPR as an integer, rather than as a
8985 : pointer. */
8986 152988 : expr = build_int_cst (totype, 0);
8987 797295912 : return maybe_adjust_type_name (totype, expr, convs->kind);
8988 53 : case ck_ambig:
8989 : /* We leave bad_p off ck_ambig because overload resolution considers
8990 : it valid, it just fails when we try to perform it. So we need to
8991 : check complain here, too. */
8992 53 : if (complain & tf_error)
8993 : {
8994 : /* Call build_user_type_conversion again for the error. */
8995 53 : auto_diagnostic_group d;
8996 3 : int flags = (convs->need_temporary_p
8997 53 : ? LOOKUP_IMPLICIT : LOOKUP_NORMAL);
8998 53 : build_user_type_conversion (totype, convs->u.expr, flags, complain);
8999 53 : gcc_assert (seen_error ());
9000 53 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9001 53 : }
9002 53 : return error_mark_node;
9003 :
9004 17590 : case ck_list:
9005 17590 : {
9006 : /* Conversion to std::initializer_list<T>. */
9007 17590 : tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
9008 17590 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (expr);
9009 17590 : tree array;
9010 :
9011 17590 : if (tree init = maybe_init_list_as_array (elttype, expr))
9012 : {
9013 132 : elttype
9014 132 : = cp_build_qualified_type (elttype, (cp_type_quals (elttype)
9015 : | TYPE_QUAL_CONST));
9016 132 : tree index_type = TYPE_DOMAIN (TREE_TYPE (init));
9017 132 : array = build_cplus_array_type (elttype, index_type);
9018 132 : len = TREE_INT_CST_LOW (TYPE_MAX_VALUE (index_type)) + 1;
9019 132 : array = build_vec_init_expr (array, init, complain);
9020 132 : array = get_target_expr (array);
9021 132 : array = cp_build_addr_expr (array, complain);
9022 : }
9023 17458 : else if (len)
9024 : {
9025 16764 : tree val;
9026 16764 : unsigned ix;
9027 16764 : tree new_ctor = build_constructor (init_list_type_node, NULL);
9028 :
9029 : /* Convert all the elements. */
9030 144614 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
9031 : {
9032 111101 : if (TREE_CODE (val) == RAW_DATA_CST)
9033 : {
9034 : /* For conversion to initializer_list<unsigned char> or
9035 : initializer_list<char> or initializer_list<signed char>
9036 : we can optimize and keep RAW_DATA_CST with adjusted
9037 : type if we report narrowing errors if needed, for
9038 : others this converts each element separately. */
9039 48 : if (convs->u.list[ix]->kind == ck_std)
9040 : {
9041 18 : tree et = convs->u.list[ix]->type;
9042 18 : conversion *next = next_conversion (convs->u.list[ix]);
9043 18 : gcc_assert (et
9044 : && (TREE_CODE (et) == INTEGER_TYPE
9045 : || is_byte_access_type (et))
9046 : && TYPE_PRECISION (et) == CHAR_BIT
9047 : && next
9048 : && next->kind == ck_identity);
9049 18 : if (!TYPE_UNSIGNED (et)
9050 : /* For RAW_DATA_CST, TREE_TYPE (val) can be
9051 : either integer_type_node (when it has been
9052 : created by the lexer from CPP_EMBED) or
9053 : after digestion/conversion some integral
9054 : type with CHAR_BIT precision. For int with
9055 : precision higher than CHAR_BIT or unsigned char
9056 : diagnose narrowing conversions from
9057 : that int/unsigned char to signed char if any
9058 : byte has most significant bit set. */
9059 18 : && (TYPE_UNSIGNED (TREE_TYPE (val))
9060 12 : || (TYPE_PRECISION (TREE_TYPE (val))
9061 : > CHAR_BIT)))
9062 2460 : for (int i = 0; i < RAW_DATA_LENGTH (val); ++i)
9063 : {
9064 2454 : if (RAW_DATA_SCHAR_ELT (val, i) >= 0)
9065 2448 : continue;
9066 6 : else if (complain & tf_error)
9067 : {
9068 6 : location_t loc
9069 6 : = cp_expr_loc_or_input_loc (val);
9070 6 : int savederrorcount = errorcount;
9071 18 : permerror_opt (loc, OPT_Wnarrowing,
9072 : "narrowing conversion of "
9073 : "%qd from %qH to %qI",
9074 6 : RAW_DATA_UCHAR_ELT (val, i),
9075 6 : TREE_TYPE (val), et);
9076 6 : if (errorcount != savederrorcount)
9077 6 : return error_mark_node;
9078 : }
9079 : else
9080 0 : return error_mark_node;
9081 : }
9082 12 : tree sub = copy_node (val);
9083 12 : TREE_TYPE (sub) = et;
9084 12 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor),
9085 : NULL_TREE, sub);
9086 : }
9087 : else
9088 : {
9089 30 : conversion *conv = convs->u.list[ix];
9090 30 : gcc_assert (conv->kind == ck_list);
9091 15495 : for (int i = 0; i < RAW_DATA_LENGTH (val); ++i)
9092 : {
9093 15465 : tree elt
9094 15465 : = build_int_cst (TREE_TYPE (val),
9095 15465 : RAW_DATA_UCHAR_ELT (val, i));
9096 15465 : tree sub
9097 15465 : = convert_like (conv->u.list[i], elt,
9098 : fn, argnum, false, false,
9099 : /*nested_p=*/true, complain);
9100 15465 : if (sub == error_mark_node)
9101 : return sub;
9102 15465 : if (!check_narrowing (TREE_TYPE (sub), elt,
9103 : complain))
9104 0 : return error_mark_node;
9105 15465 : tree nc = new_ctor;
9106 15465 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (nc),
9107 : NULL_TREE, sub);
9108 15465 : if (!TREE_CONSTANT (sub))
9109 11679 : TREE_CONSTANT (new_ctor) = false;
9110 : }
9111 : }
9112 42 : len += RAW_DATA_LENGTH (val) - 1;
9113 42 : continue;
9114 42 : }
9115 111053 : tree sub = convert_like (convs->u.list[ix], val, fn,
9116 : argnum, false, false,
9117 : /*nested_p=*/true, complain);
9118 111053 : if (sub == error_mark_node)
9119 : return sub;
9120 1648 : if (!BRACE_ENCLOSED_INITIALIZER_P (val)
9121 111064 : && !check_narrowing (TREE_TYPE (sub), val, complain))
9122 0 : return error_mark_node;
9123 111044 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor),
9124 : NULL_TREE, sub);
9125 111044 : if (!TREE_CONSTANT (sub))
9126 11447 : TREE_CONSTANT (new_ctor) = false;
9127 : }
9128 : /* Build up the array. */
9129 16749 : elttype
9130 16749 : = cp_build_qualified_type (elttype, (cp_type_quals (elttype)
9131 : | TYPE_QUAL_CONST));
9132 16749 : array = build_array_of_n_type (elttype, len);
9133 16749 : array = finish_compound_literal (array, new_ctor, complain);
9134 : /* This is dubious now, should be blessed by P2752. */
9135 16749 : DECL_MERGEABLE (TARGET_EXPR_SLOT (array)) = true;
9136 16749 : array = cp_build_addr_expr (array, complain);
9137 : }
9138 : else
9139 694 : array = nullptr_node;
9140 :
9141 17575 : array = cp_convert (build_pointer_type (elttype), array, complain);
9142 17575 : if (array == error_mark_node)
9143 : return error_mark_node;
9144 :
9145 : /* Build up the initializer_list object. Note: fail gracefully
9146 : if the object cannot be completed because, for example, no
9147 : definition is provided (c++/80956). */
9148 17575 : totype = complete_type_or_maybe_complain (totype, NULL_TREE, complain);
9149 17575 : if (!totype)
9150 0 : return error_mark_node;
9151 17575 : tree field = next_aggregate_field (TYPE_FIELDS (totype));
9152 17575 : vec<constructor_elt, va_gc> *vec = NULL;
9153 17575 : CONSTRUCTOR_APPEND_ELT (vec, field, array);
9154 17575 : field = next_aggregate_field (DECL_CHAIN (field));
9155 17575 : CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
9156 17575 : tree new_ctor = build_constructor (totype, vec);
9157 17575 : return get_target_expr (new_ctor, complain);
9158 : }
9159 :
9160 1325450 : case ck_aggr:
9161 1325450 : if (TREE_CODE (totype) == COMPLEX_TYPE)
9162 : {
9163 28584 : tree real = CONSTRUCTOR_ELT (expr, 0)->value;
9164 28584 : tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
9165 28584 : real = perform_implicit_conversion (TREE_TYPE (totype),
9166 : real, complain);
9167 28584 : imag = perform_implicit_conversion (TREE_TYPE (totype),
9168 : imag, complain);
9169 28584 : expr = build2 (COMPLEX_EXPR, totype, real, imag);
9170 28584 : return expr;
9171 : }
9172 1296866 : expr = reshape_init (totype, expr, complain);
9173 1296866 : expr = get_target_expr (digest_init (totype, expr, complain),
9174 : complain);
9175 1296866 : if (expr != error_mark_node)
9176 1296855 : TARGET_EXPR_LIST_INIT_P (expr) = true;
9177 : return expr;
9178 :
9179 324178266 : default:
9180 324178266 : break;
9181 324178266 : };
9182 :
9183 324178266 : conversion *nc = next_conversion (convs);
9184 324178266 : if (convs->kind == ck_ref_bind && nc->kind == ck_qual
9185 8871 : && !convs->need_temporary_p)
9186 : /* direct_reference_binding might have inserted a ck_qual under
9187 : this ck_ref_bind for the benefit of conversion sequence ranking.
9188 : Don't actually perform that conversion. */
9189 6496 : nc = next_conversion (nc);
9190 :
9191 584806028 : expr = convert_like (nc, expr, fn, argnum,
9192 : convs->kind == ck_ref_bind
9193 : ? issue_conversion_warnings : false,
9194 : c_cast_p, /*nested_p=*/true, complain & ~tf_no_cleanup);
9195 324178266 : if (expr == error_mark_node)
9196 : return error_mark_node;
9197 :
9198 324177436 : switch (convs->kind)
9199 : {
9200 143757396 : case ck_rvalue:
9201 143757396 : expr = decay_conversion (expr, complain);
9202 143757396 : if (expr == error_mark_node)
9203 : {
9204 15 : if (complain & tf_error)
9205 : {
9206 12 : auto_diagnostic_group d;
9207 12 : maybe_print_user_conv_context (convs);
9208 12 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9209 12 : }
9210 15 : return error_mark_node;
9211 : }
9212 :
9213 143757381 : if ((complain & tf_warning) && fn
9214 41587698 : && warn_suggest_attribute_format)
9215 : {
9216 706 : tree rhstype = TREE_TYPE (expr);
9217 706 : const enum tree_code coder = TREE_CODE (rhstype);
9218 706 : const enum tree_code codel = TREE_CODE (totype);
9219 706 : if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
9220 250 : && coder == codel
9221 956 : && check_missing_format_attribute (totype, rhstype))
9222 6 : warning (OPT_Wsuggest_attribute_format,
9223 : "argument of function call might be a candidate "
9224 : "for a format attribute");
9225 : }
9226 :
9227 143757381 : if (! MAYBE_CLASS_TYPE_P (totype))
9228 129653667 : return maybe_adjust_type_name (totype, expr, convs->kind);
9229 :
9230 : /* Don't introduce copies when passing arguments along to the inherited
9231 : constructor. */
9232 14103714 : if (current_function_decl
9233 11146142 : && flag_new_inheriting_ctors
9234 36395518 : && DECL_INHERITED_CTOR (current_function_decl))
9235 : return expr;
9236 :
9237 14099007 : if (TREE_CODE (expr) == TARGET_EXPR
9238 14099007 : && TARGET_EXPR_LIST_INIT_P (expr))
9239 : /* Copy-list-initialization doesn't actually involve a copy. */
9240 : return expr;
9241 :
9242 : /* Fall through. */
9243 16156482 : case ck_base:
9244 16156482 : if (convs->kind == ck_base && !convs->need_temporary_p)
9245 : {
9246 : /* We are going to bind a reference directly to a base-class
9247 : subobject of EXPR. */
9248 : /* Build an expression for `*((base*) &expr)'. */
9249 1828278 : expr = convert_to_base (expr, totype,
9250 : !c_cast_p, /*nonnull=*/true, complain);
9251 1828278 : return expr;
9252 : }
9253 :
9254 : /* Copy-initialization where the cv-unqualified version of the source
9255 : type is the same class as, or a derived class of, the class of the
9256 : destination [is treated as direct-initialization]. [dcl.init] */
9257 14328204 : flags = LOOKUP_NORMAL;
9258 : /* This conversion is being done in the context of a user-defined
9259 : conversion (i.e. the second step of copy-initialization), so
9260 : don't allow any more. */
9261 14328204 : if (convs->user_conv_p)
9262 264101 : flags |= LOOKUP_NO_CONVERSION;
9263 : /* We might be performing a conversion of the argument
9264 : to the user-defined conversion, i.e., not a conversion of the
9265 : result of the user-defined conversion. In which case we skip
9266 : explicit constructors. */
9267 14328204 : if (convs->copy_init_p)
9268 14054876 : flags |= LOOKUP_ONLYCONVERTING;
9269 14328204 : expr = build_temp (expr, totype, flags, &diag_kind, complain);
9270 14328204 : if (diag_kind != diagnostics::kind::unspecified && complain)
9271 : {
9272 78 : auto_diagnostic_group d;
9273 78 : maybe_print_user_conv_context (convs);
9274 78 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9275 78 : }
9276 :
9277 14328204 : return build_cplus_new (totype, expr, complain);
9278 :
9279 63549732 : case ck_ref_bind:
9280 63549732 : {
9281 63549732 : tree ref_type = totype;
9282 :
9283 63549732 : if (convs->bad_p && !next_conversion (convs)->bad_p)
9284 : {
9285 5170 : tree extype = TREE_TYPE (expr);
9286 5170 : auto_diagnostic_group d;
9287 5170 : if (TYPE_REF_IS_RVALUE (ref_type)
9288 5170 : && lvalue_p (expr))
9289 1646 : error_at (loc, "cannot bind rvalue reference of type %qH to "
9290 : "lvalue of type %qI", totype, extype);
9291 6202 : else if (!TYPE_REF_IS_RVALUE (ref_type) && !lvalue_p (expr)
9292 5004 : && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
9293 : {
9294 1231 : conversion *next = next_conversion (convs);
9295 1231 : if (next->kind == ck_std)
9296 : {
9297 59 : next = next_conversion (next);
9298 59 : error_at (loc, "cannot bind non-const lvalue reference of "
9299 : "type %qH to a value of type %qI",
9300 : totype, next->type);
9301 : }
9302 1172 : else if (!CP_TYPE_CONST_P (TREE_TYPE (ref_type)))
9303 831 : error_at (loc, "cannot bind non-const lvalue reference of "
9304 : "type %qH to an rvalue of type %qI", totype, extype);
9305 : else // extype is volatile
9306 341 : error_at (loc, "cannot bind lvalue reference of type "
9307 : "%qH to an rvalue of type %qI", totype,
9308 : extype);
9309 : }
9310 2293 : else if (!reference_compatible_p (TREE_TYPE (totype), extype))
9311 : {
9312 : /* If we're converting from T[] to T[N], don't talk
9313 : about discarding qualifiers. (Converting from T[N] to
9314 : T[] is allowed by P0388R4.) */
9315 2293 : if (TREE_CODE (extype) == ARRAY_TYPE
9316 27 : && TYPE_DOMAIN (extype) == NULL_TREE
9317 15 : && TREE_CODE (TREE_TYPE (totype)) == ARRAY_TYPE
9318 2308 : && TYPE_DOMAIN (TREE_TYPE (totype)) != NULL_TREE)
9319 15 : error_at (loc, "cannot bind reference of type %qH to %qI "
9320 : "due to different array bounds", totype, extype);
9321 : else
9322 2278 : error_at (loc, "binding reference of type %qH to %qI "
9323 : "discards qualifiers", totype, extype);
9324 : }
9325 : else
9326 0 : gcc_unreachable ();
9327 5170 : maybe_print_user_conv_context (convs);
9328 5170 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9329 :
9330 5170 : return error_mark_node;
9331 5170 : }
9332 63544562 : else if (complain & tf_warning)
9333 38009024 : maybe_warn_array_conv (loc, convs, expr);
9334 :
9335 : /* If necessary, create a temporary.
9336 :
9337 : VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
9338 : that need temporaries, even when their types are reference
9339 : compatible with the type of reference being bound, so the
9340 : upcoming call to cp_build_addr_expr doesn't fail. */
9341 63544562 : if (convs->need_temporary_p
9342 61009445 : || TREE_CODE (expr) == CONSTRUCTOR
9343 61009239 : || TREE_CODE (expr) == VA_ARG_EXPR)
9344 : {
9345 : /* Otherwise, a temporary of type "cv1 T1" is created and
9346 : initialized from the initializer expression using the rules
9347 : for a non-reference copy-initialization (8.5). */
9348 :
9349 2535323 : tree type = TREE_TYPE (ref_type);
9350 2535323 : cp_lvalue_kind lvalue = lvalue_kind (expr);
9351 :
9352 2535323 : gcc_assert (similar_type_p (type, next_conversion (convs)->type));
9353 2535323 : if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
9354 3423136 : && !TYPE_REF_IS_RVALUE (ref_type))
9355 : {
9356 : /* If the reference is volatile or non-const, we
9357 : cannot create a temporary. */
9358 105 : if (complain & tf_error)
9359 : {
9360 103 : if (lvalue & clk_bitfield)
9361 30 : error_at (loc, "cannot bind bit-field %qE to %qT",
9362 : expr, ref_type);
9363 73 : else if (lvalue & clk_packed)
9364 9 : error_at (loc, "cannot bind packed field %qE to %qT",
9365 : expr, ref_type);
9366 : else
9367 64 : error_at (loc, "cannot bind rvalue %qE to %qT",
9368 : expr, ref_type);
9369 : }
9370 105 : return error_mark_node;
9371 : }
9372 : /* If the source is a packed field, and we must use a copy
9373 : constructor, then building the target expr will require
9374 : binding the field to the reference parameter to the
9375 : copy constructor, and we'll end up with an infinite
9376 : loop. If we can use a bitwise copy, then we'll be
9377 : OK. */
9378 2535218 : if ((lvalue & clk_packed)
9379 20 : && CLASS_TYPE_P (type)
9380 2535235 : && type_has_nontrivial_copy_init (type))
9381 : {
9382 6 : error_at (loc, "cannot bind packed field %qE to %qT",
9383 : expr, ref_type);
9384 6 : return error_mark_node;
9385 : }
9386 2535212 : if (lvalue & clk_bitfield)
9387 : {
9388 24 : expr = convert_bitfield_to_declared_type (expr);
9389 24 : expr = fold_convert (type, expr);
9390 : }
9391 :
9392 : /* Creating &TARGET_EXPR<> in a template would break when
9393 : tsubsting the expression, so use an IMPLICIT_CONV_EXPR
9394 : instead. This can happen even when there's no class
9395 : involved, e.g., when converting an integer to a reference
9396 : type. */
9397 2535212 : if (processing_template_decl)
9398 9670 : return build1 (IMPLICIT_CONV_EXPR, totype, expr);
9399 2525542 : expr = build_target_expr_with_type (expr, type, complain);
9400 : }
9401 :
9402 : /* Take the address of the thing to which we will bind the
9403 : reference. */
9404 63534781 : expr = cp_build_addr_expr (expr, complain);
9405 63534781 : if (expr == error_mark_node)
9406 : return error_mark_node;
9407 :
9408 : /* Convert it to a pointer to the type referred to by the
9409 : reference. This will adjust the pointer if a derived to
9410 : base conversion is being performed. */
9411 63534781 : expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
9412 : expr, complain);
9413 : /* Convert the pointer to the desired reference type. */
9414 63534781 : return build_nop (ref_type, expr);
9415 : }
9416 :
9417 2910124 : case ck_lvalue:
9418 2910124 : return decay_conversion (expr, complain);
9419 :
9420 241602 : case ck_fnptr:
9421 : /* ??? Should the address of a transaction-safe pointer point to the TM
9422 : clone, and this conversion look up the primary function? */
9423 241602 : return build_nop (totype, expr);
9424 :
9425 1640646 : case ck_qual:
9426 : /* Warn about deprecated conversion if appropriate. */
9427 1640646 : if (complain & tf_warning)
9428 : {
9429 1441882 : string_conv_p (totype, expr, 1);
9430 1441882 : maybe_warn_array_conv (loc, convs, expr);
9431 : }
9432 : break;
9433 :
9434 3054838 : case ck_ptr:
9435 3054838 : if (convs->base_p)
9436 218474 : expr = convert_to_base (expr, totype, !c_cast_p,
9437 : /*nonnull=*/false, complain);
9438 3054838 : return build_nop (totype, expr);
9439 :
9440 28843 : case ck_pmem:
9441 28843 : return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
9442 28843 : c_cast_p, complain);
9443 :
9444 : default:
9445 : break;
9446 : }
9447 :
9448 108550727 : if (convs->check_narrowing
9449 108550727 : && !check_narrowing (totype, expr, complain,
9450 : convs->check_narrowing_const_only))
9451 456 : return error_mark_node;
9452 :
9453 217100542 : warning_sentinel w (warn_zero_as_null_pointer_constant);
9454 108550271 : if (issue_conversion_warnings)
9455 85330123 : expr = cp_convert_and_check (totype, expr, complain);
9456 : else
9457 : {
9458 23220148 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
9459 39 : expr = TREE_OPERAND (expr, 0);
9460 23220148 : expr = cp_convert (totype, expr, complain);
9461 : }
9462 :
9463 108550271 : return expr;
9464 : }
9465 :
9466 : /* Return true if converting FROM to TO is unsafe in a template. */
9467 :
9468 : static bool
9469 1976231 : conv_unsafe_in_template_p (tree to, tree from)
9470 : {
9471 : /* Converting classes involves TARGET_EXPR. */
9472 1976231 : if (CLASS_TYPE_P (to) || CLASS_TYPE_P (from))
9473 : return true;
9474 :
9475 : /* Converting real to integer produces FIX_TRUNC_EXPR which tsubst
9476 : doesn't handle. */
9477 1561950 : if (SCALAR_FLOAT_TYPE_P (from) && INTEGRAL_OR_ENUMERATION_TYPE_P (to))
9478 : return true;
9479 :
9480 : /* Converting integer to real isn't a trivial conversion, either. */
9481 1561947 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (from) && SCALAR_FLOAT_TYPE_P (to))
9482 3 : return true;
9483 :
9484 : return false;
9485 : }
9486 :
9487 : /* Wrapper for convert_like_internal that handles creating
9488 : IMPLICIT_CONV_EXPR. */
9489 :
9490 : static tree
9491 1129977935 : convert_like (conversion *convs, tree expr, tree fn, int argnum,
9492 : bool issue_conversion_warnings, bool c_cast_p, bool nested_p,
9493 : tsubst_flags_t complain)
9494 : {
9495 : /* Creating &TARGET_EXPR<> in a template breaks when substituting,
9496 : and creating a CALL_EXPR in a template breaks in finish_call_expr
9497 : so use an IMPLICIT_CONV_EXPR for this conversion. We would have
9498 : created such codes e.g. when calling a user-defined conversion
9499 : function. */
9500 1129977935 : tree conv_expr = NULL_TREE;
9501 1129977935 : if (processing_template_decl
9502 113816166 : && convs->kind != ck_identity
9503 1131954166 : && conv_unsafe_in_template_p (convs->type, TREE_TYPE (expr)))
9504 : {
9505 414287 : conv_expr = build1 (IMPLICIT_CONV_EXPR, convs->type, expr);
9506 414287 : if (convs->kind != ck_ref_bind)
9507 29039 : conv_expr = convert_from_reference (conv_expr);
9508 414287 : if (!convs->bad_p)
9509 : return conv_expr;
9510 : /* Do the normal processing to give the bad_p errors. But we still
9511 : need to return the IMPLICIT_CONV_EXPR, unless we're returning
9512 : error_mark_node. */
9513 : }
9514 1129563657 : expr = convert_like_internal (convs, expr, fn, argnum,
9515 : issue_conversion_warnings, c_cast_p,
9516 : nested_p, complain);
9517 1129563657 : if (expr == error_mark_node)
9518 : return error_mark_node;
9519 1129508801 : return conv_expr ? conv_expr : expr;
9520 : }
9521 :
9522 : /* Convenience wrapper for convert_like. */
9523 :
9524 : static inline tree
9525 628232872 : convert_like (conversion *convs, tree expr, tsubst_flags_t complain)
9526 : {
9527 628232872 : return convert_like (convs, expr, NULL_TREE, 0,
9528 : /*issue_conversion_warnings=*/true,
9529 628232872 : /*c_cast_p=*/false, /*nested_p=*/false, complain);
9530 : }
9531 :
9532 : /* Convenience wrapper for convert_like. */
9533 :
9534 : static inline tree
9535 137025972 : convert_like_with_context (conversion *convs, tree expr, tree fn, int argnum,
9536 : tsubst_flags_t complain)
9537 : {
9538 0 : return convert_like (convs, expr, fn, argnum,
9539 : /*issue_conversion_warnings=*/true,
9540 : /*c_cast_p=*/false, /*nested_p=*/false, complain);
9541 : }
9542 :
9543 : /* ARG is being passed to a varargs function. Perform any conversions
9544 : required. Return the converted value. */
9545 :
9546 : tree
9547 1444711 : convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
9548 : {
9549 1444711 : tree arg_type = TREE_TYPE (arg);
9550 1444711 : location_t loc = cp_expr_loc_or_input_loc (arg);
9551 :
9552 : /* [expr.call]
9553 :
9554 : If the argument has integral or enumeration type that is subject
9555 : to the integral promotions (_conv.prom_), or a floating-point
9556 : type that is subject to the floating-point promotion
9557 : (_conv.fpprom_), the value of the argument is converted to the
9558 : promoted type before the call. */
9559 1444711 : if (SCALAR_FLOAT_TYPE_P (arg_type)
9560 48681 : && (TYPE_PRECISION (arg_type)
9561 48681 : < TYPE_PRECISION (double_type_node))
9562 12049 : && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type))
9563 1456082 : && !extended_float_type_p (arg_type))
9564 : {
9565 11365 : if ((complain & tf_warning)
9566 11362 : && warn_double_promotion && !c_inhibit_evaluation_warnings)
9567 3 : warning_at (loc, OPT_Wdouble_promotion,
9568 : "implicit conversion from %qH to %qI when passing "
9569 : "argument to function",
9570 : arg_type, double_type_node);
9571 11365 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
9572 3 : arg = TREE_OPERAND (arg, 0);
9573 11365 : arg = mark_rvalue_use (arg);
9574 11365 : arg = convert_to_real_nofold (double_type_node, arg);
9575 : }
9576 1433346 : else if (NULLPTR_TYPE_P (arg_type))
9577 : {
9578 80 : arg = mark_rvalue_use (arg);
9579 80 : if (TREE_SIDE_EFFECTS (arg))
9580 : {
9581 6 : warning_sentinel w(warn_unused_result);
9582 6 : arg = cp_build_compound_expr (arg, null_pointer_node, complain);
9583 6 : }
9584 : else
9585 74 : arg = null_pointer_node;
9586 : }
9587 1433266 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
9588 : {
9589 937490 : if (SCOPED_ENUM_P (arg_type))
9590 : {
9591 64 : tree prom = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg,
9592 : complain);
9593 64 : prom = cp_perform_integral_promotions (prom, complain);
9594 125 : if (abi_version_crosses (6)
9595 30 : && TYPE_MODE (TREE_TYPE (prom)) != TYPE_MODE (arg_type)
9596 94 : && (complain & tf_warning))
9597 60 : warning_at (loc, OPT_Wabi, "scoped enum %qT passed through %<...%>"
9598 : " as %qT before %<-fabi-version=6%>, %qT after",
9599 : arg_type,
9600 30 : TREE_TYPE (prom), ENUM_UNDERLYING_TYPE (arg_type));
9601 64 : if (!abi_version_at_least (6))
9602 1444711 : arg = prom;
9603 : }
9604 : else
9605 937426 : arg = cp_perform_integral_promotions (arg, complain);
9606 : }
9607 : else
9608 : /* [expr.call]
9609 :
9610 : The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
9611 : standard conversions are performed. */
9612 495776 : arg = decay_conversion (arg, complain);
9613 :
9614 1444711 : arg = require_complete_type (arg, complain);
9615 1444711 : arg_type = TREE_TYPE (arg);
9616 :
9617 1444711 : if (arg != error_mark_node
9618 : /* In a template (or ill-formed code), we can have an incomplete type
9619 : even after require_complete_type, in which case we don't know
9620 : whether it has trivial copy or not. */
9621 1444699 : && COMPLETE_TYPE_P (arg_type)
9622 2889410 : && !cp_unevaluated_operand)
9623 : {
9624 : /* [expr.call] 5.2.2/7:
9625 : Passing a potentially-evaluated argument of class type (Clause 9)
9626 : with a non-trivial copy constructor or a non-trivial destructor
9627 : with no corresponding parameter is conditionally-supported, with
9628 : implementation-defined semantics.
9629 :
9630 : We support it as pass-by-invisible-reference, just like a normal
9631 : value parameter.
9632 :
9633 : If the call appears in the context of a sizeof expression,
9634 : it is not potentially-evaluated. */
9635 698864 : if (type_has_nontrivial_copy_init (arg_type)
9636 698864 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type))
9637 : {
9638 37 : arg = force_rvalue (arg, complain);
9639 37 : if (complain & tf_warning)
9640 37 : warning (OPT_Wconditionally_supported,
9641 : "passing objects of non-trivially-copyable "
9642 : "type %q#T through %<...%> is conditionally supported",
9643 : arg_type);
9644 37 : return build1 (ADDR_EXPR, build_reference_type (arg_type), arg);
9645 : }
9646 : /* Build up a real lvalue-to-rvalue conversion in case the
9647 : copy constructor is trivial but not callable. */
9648 698827 : else if (CLASS_TYPE_P (arg_type))
9649 44260 : force_rvalue (arg, complain);
9650 :
9651 : }
9652 :
9653 : return arg;
9654 : }
9655 :
9656 : /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
9657 :
9658 : tree
9659 31298 : build_x_va_arg (location_t loc, tree expr, tree type)
9660 : {
9661 31298 : if (processing_template_decl)
9662 : {
9663 39 : tree r = build_min (VA_ARG_EXPR, type, expr);
9664 39 : SET_EXPR_LOCATION (r, loc);
9665 39 : return r;
9666 : }
9667 :
9668 31259 : type = complete_type_or_else (type, NULL_TREE);
9669 :
9670 31259 : if (expr == error_mark_node || !type)
9671 : return error_mark_node;
9672 :
9673 31238 : expr = mark_lvalue_use (expr);
9674 :
9675 31238 : if (TYPE_REF_P (type))
9676 : {
9677 6 : error ("cannot receive reference type %qT through %<...%>", type);
9678 6 : return error_mark_node;
9679 : }
9680 :
9681 31232 : if (type_has_nontrivial_copy_init (type)
9682 31232 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
9683 : {
9684 : /* conditionally-supported behavior [expr.call] 5.2.2/7. Let's treat
9685 : it as pass by invisible reference. */
9686 12 : warning_at (loc, OPT_Wconditionally_supported,
9687 : "receiving objects of non-trivially-copyable type %q#T "
9688 : "through %<...%> is conditionally-supported", type);
9689 :
9690 12 : tree ref = cp_build_reference_type (type, false);
9691 12 : expr = build_va_arg (loc, expr, ref);
9692 12 : return convert_from_reference (expr);
9693 : }
9694 :
9695 31220 : tree ret = build_va_arg (loc, expr, type);
9696 31220 : if (CLASS_TYPE_P (type))
9697 : /* Wrap the VA_ARG_EXPR in a TARGET_EXPR now so other code doesn't need to
9698 : know how to handle it. */
9699 12094 : ret = get_target_expr (ret);
9700 : return ret;
9701 : }
9702 :
9703 : /* TYPE has been given to va_arg. Apply the default conversions which
9704 : would have happened when passed via ellipsis. Return the promoted
9705 : type, or the passed type if there is no change. */
9706 :
9707 : tree
9708 2718590 : cxx_type_promotes_to (tree type)
9709 : {
9710 2718590 : tree promote;
9711 :
9712 : /* Perform the array-to-pointer and function-to-pointer
9713 : conversions. */
9714 2718590 : type = type_decays_to (type);
9715 :
9716 2718590 : promote = type_promotes_to (type);
9717 2718590 : if (same_type_p (type, promote))
9718 2718566 : promote = type;
9719 :
9720 2718590 : return promote;
9721 : }
9722 :
9723 : /* ARG is a default argument expression being passed to a parameter of
9724 : the indicated TYPE, which is a parameter to FN. PARMNUM is the
9725 : zero-based argument number. Do any required conversions. Return
9726 : the converted value. */
9727 :
9728 : static GTY(()) vec<tree, va_gc> *default_arg_context;
9729 : void
9730 8017635 : push_defarg_context (tree fn)
9731 8017635 : { vec_safe_push (default_arg_context, fn); }
9732 :
9733 : void
9734 8017635 : pop_defarg_context (void)
9735 8017635 : { default_arg_context->pop (); }
9736 :
9737 : tree
9738 1141136 : convert_default_arg (tree type, tree arg, tree fn, int parmnum,
9739 : tsubst_flags_t complain)
9740 : {
9741 1141136 : int i;
9742 1141136 : tree t;
9743 :
9744 : /* See through clones. */
9745 1141136 : fn = DECL_ORIGIN (fn);
9746 : /* And inheriting ctors. */
9747 1141136 : if (flag_new_inheriting_ctors)
9748 1140496 : fn = strip_inheriting_ctors (fn);
9749 :
9750 : /* Detect recursion. */
9751 1144543 : FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t)
9752 3413 : if (t == fn)
9753 : {
9754 6 : if (complain & tf_error)
9755 6 : error ("recursive evaluation of default argument for %q#D", fn);
9756 6 : return error_mark_node;
9757 : }
9758 :
9759 : /* If the ARG is an unparsed default argument expression, the
9760 : conversion cannot be performed. */
9761 1141130 : if (TREE_CODE (arg) == DEFERRED_PARSE)
9762 : {
9763 15 : if (complain & tf_error)
9764 15 : error ("call to %qD uses the default argument for parameter %P, which "
9765 : "is not yet defined", fn, parmnum);
9766 15 : return error_mark_node;
9767 : }
9768 :
9769 1141115 : push_defarg_context (fn);
9770 :
9771 1141115 : if (fn && DECL_TEMPLATE_INFO (fn))
9772 837498 : arg = tsubst_default_argument (fn, parmnum, type, arg, complain);
9773 :
9774 : /* Due to:
9775 :
9776 : [dcl.fct.default]
9777 :
9778 : The names in the expression are bound, and the semantic
9779 : constraints are checked, at the point where the default
9780 : expressions appears.
9781 :
9782 : we must not perform access checks here. */
9783 1141115 : push_deferring_access_checks (dk_no_check);
9784 : /* We must make a copy of ARG, in case subsequent processing
9785 : alters any part of it. */
9786 1141115 : arg = break_out_target_exprs (arg, /*clear location*/true);
9787 :
9788 1141115 : arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
9789 : ICR_DEFAULT_ARGUMENT, fn, parmnum,
9790 : complain);
9791 1141115 : arg = convert_for_arg_passing (type, arg, complain);
9792 1141115 : pop_deferring_access_checks();
9793 :
9794 1141115 : pop_defarg_context ();
9795 :
9796 1141115 : return arg;
9797 : }
9798 :
9799 : /* Returns the type which will really be used for passing an argument of
9800 : type TYPE. */
9801 :
9802 : tree
9803 677733570 : type_passed_as (tree type)
9804 : {
9805 : /* Pass classes with copy ctors by invisible reference. */
9806 677733570 : if (TREE_ADDRESSABLE (type))
9807 626202 : type = build_reference_type (type);
9808 :
9809 677733570 : return type;
9810 : }
9811 :
9812 : /* Actually perform the appropriate conversion. */
9813 :
9814 : tree
9815 143042947 : convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
9816 : {
9817 143042947 : tree bitfield_type;
9818 :
9819 : /* If VAL is a bitfield, then -- since it has already been converted
9820 : to TYPE -- it cannot have a precision greater than TYPE.
9821 :
9822 : If it has a smaller precision, we must widen it here. For
9823 : example, passing "int f:3;" to a function expecting an "int" will
9824 : not result in any conversion before this point.
9825 :
9826 : If the precision is the same we must not risk widening. For
9827 : example, the COMPONENT_REF for a 32-bit "long long" bitfield will
9828 : often have type "int", even though the C++ type for the field is
9829 : "long long". If the value is being passed to a function
9830 : expecting an "int", then no conversions will be required. But,
9831 : if we call convert_bitfield_to_declared_type, the bitfield will
9832 : be converted to "long long". */
9833 143042947 : bitfield_type = is_bitfield_expr_with_lowered_type (val);
9834 143042947 : if (bitfield_type
9835 143042947 : && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
9836 0 : val = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type), val);
9837 :
9838 143042947 : if (val == error_mark_node)
9839 : ;
9840 : /* Pass classes with copy ctors by invisible reference. */
9841 143039301 : else if (TREE_ADDRESSABLE (type))
9842 211505 : val = build1 (ADDR_EXPR, build_reference_type (type), val);
9843 143042947 : if (complain & tf_warning)
9844 131297312 : maybe_warn_parm_abi (type, cp_expr_loc_or_input_loc (val));
9845 :
9846 105536846 : if (complain & tf_warning)
9847 105536846 : warn_for_address_of_packed_member (type, val);
9848 :
9849 : /* gimplify_arg elides TARGET_EXPRs that initialize a function argument,
9850 : unless the initializer is a CONSTRUCTOR. In that case, we fail to
9851 : elide the copy anyway. See that function for more information. */
9852 7909450 : if (SIMPLE_TARGET_EXPR_P (val)
9853 149524713 : && TREE_CODE (TARGET_EXPR_INITIAL (val)) != CONSTRUCTOR)
9854 3813408 : set_target_expr_eliding (val);
9855 :
9856 143042947 : return val;
9857 : }
9858 :
9859 : /* Returns non-zero iff FN is a function with magic varargs, i.e. ones for
9860 : which just decay_conversion or no conversions at all should be done.
9861 : This is true for some builtins which don't act like normal functions.
9862 : Return 2 if just decay_conversion and removal of excess precision should
9863 : be done, 1 if just decay_conversion. Return 3 for special treatment of
9864 : the 3rd argument for __builtin_*_overflow_p. Return 4 for special
9865 : treatment of the 1st argument for
9866 : __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g. */
9867 :
9868 : int
9869 174588648 : magic_varargs_p (tree fn)
9870 : {
9871 174588648 : if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
9872 6093540 : switch (DECL_FUNCTION_CODE (fn))
9873 : {
9874 : case BUILT_IN_CLASSIFY_TYPE:
9875 : case BUILT_IN_CONSTANT_P:
9876 : case BUILT_IN_NEXT_ARG:
9877 : case BUILT_IN_VA_START:
9878 : return 1;
9879 :
9880 20842 : case BUILT_IN_ADD_OVERFLOW_P:
9881 20842 : case BUILT_IN_SUB_OVERFLOW_P:
9882 20842 : case BUILT_IN_MUL_OVERFLOW_P:
9883 20842 : return 3;
9884 :
9885 319834 : case BUILT_IN_ISFINITE:
9886 319834 : case BUILT_IN_ISINF:
9887 319834 : case BUILT_IN_ISINF_SIGN:
9888 319834 : case BUILT_IN_ISNAN:
9889 319834 : case BUILT_IN_ISNORMAL:
9890 319834 : case BUILT_IN_FPCLASSIFY:
9891 319834 : return 2;
9892 :
9893 101238 : case BUILT_IN_CLZG:
9894 101238 : case BUILT_IN_CTZG:
9895 101238 : case BUILT_IN_CLRSBG:
9896 101238 : case BUILT_IN_FFSG:
9897 101238 : case BUILT_IN_PARITYG:
9898 101238 : case BUILT_IN_POPCOUNTG:
9899 101238 : return 4;
9900 :
9901 5445005 : default:
9902 5445005 : return lookup_attribute ("type generic",
9903 10890010 : TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
9904 : }
9905 :
9906 : return 0;
9907 : }
9908 :
9909 : /* Returns the decl of the dispatcher function if FN is a function version. */
9910 :
9911 : tree
9912 240 : get_function_version_dispatcher (tree fn)
9913 : {
9914 240 : tree dispatcher_decl = NULL;
9915 :
9916 240 : if (DECL_LOCAL_DECL_P (fn))
9917 9 : fn = DECL_LOCAL_DECL_ALIAS (fn);
9918 :
9919 240 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
9920 : && DECL_FUNCTION_VERSIONED (fn));
9921 :
9922 240 : gcc_assert (targetm.get_function_versions_dispatcher);
9923 240 : dispatcher_decl = targetm.get_function_versions_dispatcher (fn);
9924 :
9925 240 : if (dispatcher_decl == NULL)
9926 : {
9927 9 : error_at (input_location, "use of multiversioned function "
9928 : "without a default");
9929 9 : return NULL;
9930 : }
9931 :
9932 231 : retrofit_lang_decl (dispatcher_decl);
9933 231 : gcc_assert (dispatcher_decl != NULL);
9934 : return dispatcher_decl;
9935 : }
9936 :
9937 : /* fn is a function version dispatcher that is marked used. Mark all the
9938 : semantically identical function versions it will dispatch as used. */
9939 :
9940 : void
9941 159 : mark_versions_used (tree fn)
9942 : {
9943 159 : struct cgraph_node *node;
9944 159 : struct cgraph_function_version_info *node_v;
9945 159 : struct cgraph_function_version_info *it_v;
9946 :
9947 159 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9948 :
9949 159 : node = cgraph_node::get (fn);
9950 159 : if (node == NULL)
9951 : return;
9952 :
9953 159 : gcc_assert (node->dispatcher_function);
9954 :
9955 159 : node_v = node->function_version ();
9956 159 : if (node_v == NULL)
9957 : return;
9958 :
9959 : /* All semantically identical versions are chained. Traverse and mark each
9960 : one of them as used. */
9961 159 : it_v = node_v->next;
9962 1146 : while (it_v != NULL)
9963 : {
9964 987 : mark_used (it_v->this_node->decl);
9965 987 : it_v = it_v->next;
9966 : }
9967 : }
9968 :
9969 : /* Build a call to "the copy constructor" for the type of A, even if it
9970 : wouldn't be selected by normal overload resolution. Used for
9971 : diagnostics. */
9972 :
9973 : static tree
9974 3 : call_copy_ctor (tree a, tsubst_flags_t complain)
9975 : {
9976 3 : tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (a));
9977 3 : tree binfo = TYPE_BINFO (ctype);
9978 3 : tree copy = get_copy_ctor (ctype, complain);
9979 3 : copy = build_baselink (binfo, binfo, copy, NULL_TREE);
9980 3 : tree ob = build_dummy_object (ctype);
9981 3 : releasing_vec args (make_tree_vector_single (a));
9982 3 : tree r = build_new_method_call (ob, copy, &args, NULL_TREE,
9983 : LOOKUP_NORMAL, NULL, complain);
9984 3 : return r;
9985 3 : }
9986 :
9987 : /* Return the base constructor corresponding to COMPLETE_CTOR or NULL_TREE. */
9988 :
9989 : static tree
9990 48 : base_ctor_for (tree complete_ctor)
9991 : {
9992 48 : tree clone;
9993 48 : FOR_EACH_CLONE (clone, DECL_CLONED_FUNCTION (complete_ctor))
9994 48 : if (DECL_BASE_CONSTRUCTOR_P (clone))
9995 : return clone;
9996 : return NULL_TREE;
9997 : }
9998 :
9999 : /* Try to make EXP suitable to be used as the initializer for a base subobject,
10000 : and return whether we were successful. EXP must have already been cleared
10001 : by unsafe_copy_elision_p{,_opt}. */
10002 :
10003 : static bool
10004 224 : make_base_init_ok (tree exp)
10005 : {
10006 224 : if (TREE_CODE (exp) == TARGET_EXPR)
10007 224 : exp = TARGET_EXPR_INITIAL (exp);
10008 227 : while (TREE_CODE (exp) == COMPOUND_EXPR)
10009 3 : exp = TREE_OPERAND (exp, 1);
10010 224 : if (TREE_CODE (exp) == COND_EXPR)
10011 : {
10012 3 : bool ret = make_base_init_ok (TREE_OPERAND (exp, 2));
10013 3 : if (tree op1 = TREE_OPERAND (exp, 1))
10014 : {
10015 3 : bool r1 = make_base_init_ok (op1);
10016 : /* If unsafe_copy_elision_p was false, the arms should match. */
10017 3 : gcc_assert (r1 == ret);
10018 : }
10019 : return ret;
10020 : }
10021 221 : if (TREE_CODE (exp) != AGGR_INIT_EXPR)
10022 : /* A trivial copy is OK. */
10023 : return true;
10024 70 : if (!AGGR_INIT_VIA_CTOR_P (exp))
10025 : /* unsafe_copy_elision_p_opt must have said this is OK. */
10026 : return true;
10027 48 : tree fn = cp_get_callee_fndecl_nofold (exp);
10028 48 : if (DECL_BASE_CONSTRUCTOR_P (fn))
10029 : return true;
10030 48 : gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (fn));
10031 48 : fn = base_ctor_for (fn);
10032 48 : if (!fn || DECL_HAS_VTT_PARM_P (fn))
10033 : /* The base constructor has more parameters, so we can't just change the
10034 : call target. It would be possible to splice in the appropriate
10035 : arguments, but probably not worth the complexity. */
10036 : return false;
10037 39 : mark_used (fn);
10038 39 : AGGR_INIT_EXPR_FN (exp) = build_address (fn);
10039 39 : return true;
10040 : }
10041 :
10042 : /* Return 2 if T refers to a base, 1 if a potentially-overlapping field,
10043 : neither of which can be used for return by invisible reference. We avoid
10044 : doing C++17 mandatory copy elision for either of these cases.
10045 :
10046 : This returns non-zero even if the type of T has no tail padding that other
10047 : data could be allocated into, because that depends on the particular ABI.
10048 : unsafe_copy_elision_p_opt does consider whether there is padding. */
10049 :
10050 : int
10051 42964421 : unsafe_return_slot_p (tree t)
10052 : {
10053 : /* Check empty bases separately, they don't have fields. */
10054 42964421 : if (is_empty_base_ref (t))
10055 : return 2;
10056 :
10057 : /* A delegating constructor might be used to initialize a base. */
10058 42479069 : if (current_function_decl
10059 57610592 : && DECL_CONSTRUCTOR_P (current_function_decl)
10060 46715617 : && (t == current_class_ref
10061 4064966 : || tree_strip_nop_conversions (t) == current_class_ptr))
10062 : return 2;
10063 :
10064 42256894 : STRIP_NOPS (t);
10065 42256894 : if (TREE_CODE (t) == ADDR_EXPR)
10066 66907 : t = TREE_OPERAND (t, 0);
10067 42256894 : if (TREE_CODE (t) == COMPONENT_REF)
10068 3550462 : t = TREE_OPERAND (t, 1);
10069 42256894 : if (TREE_CODE (t) != FIELD_DECL)
10070 : return false;
10071 3596810 : if (!CLASS_TYPE_P (TREE_TYPE (t)))
10072 : /* The middle-end will do the right thing for scalar types. */
10073 : return false;
10074 3058061 : if (DECL_FIELD_IS_BASE (t))
10075 : return 2;
10076 2235745 : if (lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (t)))
10077 164397 : return 1;
10078 : return 0;
10079 : }
10080 :
10081 : /* True IFF EXP is a prvalue that represents return by invisible reference. */
10082 :
10083 : static bool
10084 286364 : init_by_return_slot_p (tree exp)
10085 : {
10086 : /* Copy elision only happens with a TARGET_EXPR. */
10087 286367 : if (TREE_CODE (exp) != TARGET_EXPR)
10088 : return false;
10089 15588 : tree init = TARGET_EXPR_INITIAL (exp);
10090 : /* build_compound_expr pushes COMPOUND_EXPR inside TARGET_EXPR. */
10091 15594 : while (TREE_CODE (init) == COMPOUND_EXPR)
10092 6 : init = TREE_OPERAND (init, 1);
10093 15588 : if (TREE_CODE (init) == COND_EXPR)
10094 : {
10095 : /* We'll end up copying from each of the arms of the COND_EXPR directly
10096 : into the target, so look at them. */
10097 6 : if (tree op = TREE_OPERAND (init, 1))
10098 6 : if (init_by_return_slot_p (op))
10099 : return true;
10100 3 : return init_by_return_slot_p (TREE_OPERAND (init, 2));
10101 : }
10102 15582 : return (TREE_CODE (init) == AGGR_INIT_EXPR
10103 15582 : && !AGGR_INIT_VIA_CTOR_P (init));
10104 : }
10105 :
10106 : /* We can't elide a copy from a function returning by value to a
10107 : potentially-overlapping subobject, as the callee might clobber tail padding.
10108 : Return true iff this could be that case.
10109 :
10110 : Places that use this function (or _opt) to decide to elide a copy should
10111 : probably use make_safe_copy_elision instead. */
10112 :
10113 : bool
10114 2430376 : unsafe_copy_elision_p (tree target, tree exp)
10115 : {
10116 2430376 : return unsafe_return_slot_p (target) && init_by_return_slot_p (exp);
10117 : }
10118 :
10119 : /* As above, but for optimization allow more cases that are actually safe. */
10120 :
10121 : static bool
10122 8251269 : unsafe_copy_elision_p_opt (tree target, tree exp)
10123 : {
10124 8251269 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
10125 : /* It's safe to elide the copy for a class with no tail padding. */
10126 8251269 : if (!is_empty_class (type)
10127 8251269 : && tree_int_cst_equal (TYPE_SIZE (type), CLASSTYPE_SIZE (type)))
10128 : return false;
10129 2384028 : return unsafe_copy_elision_p (target, exp);
10130 : }
10131 :
10132 : /* Try to make EXP suitable to be used as the initializer for TARGET,
10133 : and return whether we were successful. */
10134 :
10135 : bool
10136 22 : make_safe_copy_elision (tree target, tree exp)
10137 : {
10138 22 : int uns = unsafe_return_slot_p (target);
10139 22 : if (!uns)
10140 : return true;
10141 22 : if (init_by_return_slot_p (exp))
10142 : return false;
10143 22 : if (uns == 1)
10144 : return true;
10145 22 : return make_base_init_ok (exp);
10146 : }
10147 :
10148 : /* True IFF the result of the conversion C is a prvalue. */
10149 :
10150 : static bool
10151 14311327 : conv_is_prvalue (conversion *c)
10152 : {
10153 14311327 : if (c->kind == ck_rvalue)
10154 : return true;
10155 14311327 : if (c->kind == ck_base && c->need_temporary_p)
10156 : return true;
10157 14311327 : if (c->kind == ck_user && !TYPE_REF_P (c->type))
10158 : return true;
10159 14176454 : if (c->kind == ck_identity && c->u.expr
10160 13923430 : && TREE_CODE (c->u.expr) == TARGET_EXPR)
10161 69 : return true;
10162 :
10163 : return false;
10164 : }
10165 :
10166 : /* True iff C is a conversion that binds a reference to a prvalue. */
10167 :
10168 : static bool
10169 14312629 : conv_binds_ref_to_prvalue (conversion *c)
10170 : {
10171 14312629 : if (c->kind != ck_ref_bind)
10172 : return false;
10173 14312629 : if (c->need_temporary_p)
10174 : return true;
10175 :
10176 14311327 : return conv_is_prvalue (next_conversion (c));
10177 : }
10178 :
10179 : /* True iff EXPR represents a (subobject of a) temporary. */
10180 :
10181 : static bool
10182 14951 : expr_represents_temporary_p (tree expr)
10183 : {
10184 15055 : while (handled_component_p (expr))
10185 104 : expr = TREE_OPERAND (expr, 0);
10186 14951 : return TREE_CODE (expr) == TARGET_EXPR;
10187 : }
10188 :
10189 : /* True iff C is a conversion that binds a reference to a temporary.
10190 : This is a superset of conv_binds_ref_to_prvalue: here we're also
10191 : interested in xvalues. */
10192 :
10193 : static bool
10194 14294 : conv_binds_ref_to_temporary (conversion *c)
10195 : {
10196 14294 : if (conv_binds_ref_to_prvalue (c))
10197 : return true;
10198 13807 : if (c->kind != ck_ref_bind)
10199 : return false;
10200 13807 : c = next_conversion (c);
10201 : /* This is the case for
10202 : struct Base {};
10203 : struct Derived : Base {};
10204 : const Base& b(Derived{});
10205 : where we bind 'b' to the Base subobject of a temporary object of type
10206 : Derived. The subobject is an xvalue; the whole object is a prvalue.
10207 :
10208 : The ck_base doesn't have to be present for cases like X{}.m. */
10209 13807 : if (c->kind == ck_base)
10210 14 : c = next_conversion (c);
10211 13728 : if (c->kind == ck_identity && c->u.expr
10212 27535 : && expr_represents_temporary_p (c->u.expr))
10213 : return true;
10214 : return false;
10215 : }
10216 :
10217 : /* Return tristate::TS_TRUE if converting EXPR to a reference type TYPE binds
10218 : the reference to a temporary. Return tristate::TS_FALSE if converting
10219 : EXPR to a reference type TYPE doesn't bind the reference to a temporary. If
10220 : the conversion is invalid or bad, return tristate::TS_UNKNOWN. DIRECT_INIT_P
10221 : says whether the conversion should be done in direct- or copy-initialization
10222 : context. */
10223 :
10224 : tristate
10225 14823 : ref_conv_binds_to_temporary (tree type, tree expr, bool direct_init_p/*=false*/)
10226 : {
10227 14823 : gcc_assert (TYPE_REF_P (type));
10228 :
10229 14823 : conversion_obstack_sentinel cos;
10230 :
10231 14823 : const int flags = direct_init_p ? LOOKUP_NORMAL : LOOKUP_IMPLICIT;
10232 14823 : conversion *conv = implicit_conversion (type, TREE_TYPE (expr), expr,
10233 : /*c_cast_p=*/false, flags, tf_none);
10234 14823 : if (!conv || conv->bad_p)
10235 529 : return tristate::unknown ();
10236 :
10237 14294 : if (conv_binds_ref_to_temporary (conv))
10238 : {
10239 : /* Actually perform the conversion to check access control. */
10240 499 : if (convert_like (conv, expr, tf_none) != error_mark_node)
10241 487 : return tristate (true);
10242 : else
10243 12 : return tristate::unknown ();
10244 : }
10245 :
10246 13795 : return tristate (false);
10247 14823 : }
10248 :
10249 : /* Call the trivial destructor for INSTANCE, which can be either an lvalue of
10250 : class type or a pointer to class type. If NO_PTR_DEREF is true and
10251 : INSTANCE has pointer type, clobber the pointer rather than what it points
10252 : to. */
10253 :
10254 : tree
10255 3349991 : build_trivial_dtor_call (tree instance, bool no_ptr_deref)
10256 : {
10257 3349991 : gcc_assert (!is_dummy_object (instance));
10258 :
10259 3349991 : if (!flag_lifetime_dse)
10260 : {
10261 58 : no_clobber:
10262 77 : return fold_convert (void_type_node, instance);
10263 : }
10264 :
10265 3395293 : if (INDIRECT_TYPE_P (TREE_TYPE (instance))
10266 3349933 : && (!no_ptr_deref || TYPE_REF_P (TREE_TYPE (instance))))
10267 : {
10268 3186462 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
10269 19 : goto no_clobber;
10270 3186443 : instance = cp_build_fold_indirect_ref (instance);
10271 : }
10272 :
10273 : /* A trivial destructor should still clobber the object. */
10274 3349914 : tree clobber = build_clobber (TREE_TYPE (instance), CLOBBER_OBJECT_END);
10275 3349914 : return build2 (MODIFY_EXPR, void_type_node,
10276 3349914 : instance, clobber);
10277 : }
10278 :
10279 : /* Return true if in an immediate function context, or an unevaluated operand,
10280 : or a default argument/member initializer, or a subexpression of an immediate
10281 : invocation. */
10282 :
10283 : bool
10284 16395197 : in_immediate_context ()
10285 : {
10286 16395197 : return (cp_unevaluated_operand != 0
10287 14702316 : || (current_function_decl != NULL_TREE
10288 24764350 : && DECL_IMMEDIATE_FUNCTION_P (current_function_decl))
10289 : /* DR 2631: default args and DMI aren't immediately evaluated.
10290 : Return true here so immediate_invocation_p returns false. */
10291 14355252 : || current_binding_level->kind == sk_function_parms
10292 14352257 : || current_binding_level->kind == sk_template_parms
10293 14272722 : || parsing_nsdmi ()
10294 30665326 : || in_consteval_if_p);
10295 : }
10296 :
10297 : /* Return true if a call to FN with number of arguments NARGS
10298 : is an immediate invocation. */
10299 :
10300 : bool
10301 216313099 : immediate_invocation_p (tree fn)
10302 : {
10303 216313099 : return (TREE_CODE (fn) == FUNCTION_DECL
10304 216313099 : && DECL_IMMEDIATE_FUNCTION_P (fn)
10305 219402232 : && !in_immediate_context ());
10306 : }
10307 :
10308 : /* Subroutine of the various build_*_call functions. Overload resolution
10309 : has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
10310 : ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
10311 : bitmask of various LOOKUP_* flags which apply to the call itself. */
10312 :
10313 : static tree
10314 231766170 : build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
10315 : {
10316 231766170 : tree fn = cand->fn;
10317 231766170 : const vec<tree, va_gc> *args = cand->args;
10318 231766170 : tree first_arg = cand->first_arg;
10319 231766170 : conversion **convs = cand->convs;
10320 231766170 : tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
10321 231766170 : int parmlen;
10322 231766170 : tree val;
10323 231766170 : int nargs;
10324 231766170 : tree *argarray;
10325 231766170 : bool already_used = false;
10326 :
10327 : /* In a template, there is no need to perform all of the work that
10328 : is normally done. We are only interested in the type of the call
10329 : expression, i.e., the return type of the function. Any semantic
10330 : errors will be deferred until the template is instantiated. */
10331 231766170 : if (processing_template_decl)
10332 : {
10333 30119001 : if (undeduced_auto_decl (fn))
10334 12906 : mark_used (fn, complain);
10335 : else
10336 : /* Otherwise set TREE_USED for the benefit of -Wunused-function.
10337 : See PR80598. */
10338 30106095 : TREE_USED (fn) = 1;
10339 :
10340 30119001 : tree return_type = TREE_TYPE (TREE_TYPE (fn));
10341 30119001 : tree callee;
10342 30119001 : if (first_arg == NULL_TREE)
10343 : {
10344 22520340 : callee = build_addr_func (fn, complain);
10345 22520340 : if (callee == error_mark_node)
10346 : return error_mark_node;
10347 : }
10348 : else
10349 : {
10350 7598661 : callee = build_baselink (cand->conversion_path, cand->access_path,
10351 : fn, NULL_TREE);
10352 7598661 : callee = build_min (COMPONENT_REF, TREE_TYPE (fn),
10353 : first_arg, callee, NULL_TREE);
10354 : }
10355 :
10356 30119001 : tree expr = build_call_vec (return_type, callee, args);
10357 30119001 : SET_EXPR_LOCATION (expr, input_location);
10358 30119001 : if (TREE_THIS_VOLATILE (fn) && cfun)
10359 2601753 : current_function_returns_abnormally = 1;
10360 30119001 : if (TREE_DEPRECATED (fn)
10361 30119001 : && warning_suppressed_at (input_location,
10362 : OPT_Wdeprecated_declarations))
10363 : /* Make the expr consistent with the location. */
10364 19941 : TREE_NO_WARNING (expr) = true;
10365 30119001 : if (immediate_invocation_p (fn))
10366 : {
10367 206124 : tree obj_arg = NULL_TREE;
10368 412248 : if (DECL_CONSTRUCTOR_P (fn))
10369 0 : obj_arg = first_arg;
10370 : /* Look through *(const T *)&obj. */
10371 0 : if (obj_arg && INDIRECT_REF_P (obj_arg))
10372 : {
10373 0 : tree addr = TREE_OPERAND (obj_arg, 0);
10374 0 : STRIP_NOPS (addr);
10375 0 : if (TREE_CODE (addr) == ADDR_EXPR)
10376 : {
10377 0 : tree typeo = TREE_TYPE (obj_arg);
10378 0 : tree typei = TREE_TYPE (TREE_OPERAND (addr, 0));
10379 0 : if (same_type_ignoring_top_level_qualifiers_p (typeo, typei))
10380 0 : obj_arg = TREE_OPERAND (addr, 0);
10381 : }
10382 : }
10383 206124 : fold_non_dependent_expr (expr, complain,
10384 : /*manifestly_const_eval=*/true,
10385 : obj_arg);
10386 : }
10387 30119001 : return convert_from_reference (expr);
10388 : }
10389 :
10390 : /* Give any warnings we noticed during overload resolution. */
10391 201647169 : if (cand->warnings && (complain & tf_warning))
10392 : {
10393 : struct candidate_warning *w;
10394 98 : for (w = cand->warnings; w; w = w->next)
10395 49 : joust (cand, w->loser, 1, complain);
10396 : }
10397 :
10398 : /* Core issue 2327: P0135 doesn't say how to handle the case where the
10399 : argument to the copy constructor ends up being a prvalue after
10400 : conversion. Let's do the normal processing, but pretend we aren't
10401 : actually using the copy constructor. */
10402 201647169 : bool force_elide = false;
10403 201647169 : if (cxx_dialect >= cxx17
10404 199603904 : && cand->num_convs == 1
10405 112898480 : && DECL_COMPLETE_CONSTRUCTOR_P (fn)
10406 39520506 : && (DECL_COPY_CONSTRUCTOR_P (fn)
10407 21437034 : || DECL_MOVE_CONSTRUCTOR_P (fn))
10408 14369927 : && !unsafe_return_slot_p (first_arg)
10409 215945438 : && conv_binds_ref_to_prvalue (convs[0]))
10410 : {
10411 135754 : force_elide = true;
10412 135754 : goto not_really_used;
10413 : }
10414 :
10415 : /* OK, we're actually calling this inherited constructor; set its deletedness
10416 : appropriately. We can get away with doing this here because calling is
10417 : the only way to refer to a constructor. */
10418 403022830 : if (DECL_INHERITED_CTOR (fn)
10419 29290510 : && !deduce_inheriting_ctor (fn))
10420 : {
10421 2 : if (complain & tf_error)
10422 2 : mark_used (fn);
10423 2 : return error_mark_node;
10424 : }
10425 :
10426 : /* Make =delete work with SFINAE. */
10427 201511413 : if (DECL_DELETED_FN (fn))
10428 : {
10429 1031347 : if (complain & tf_error)
10430 : {
10431 2300 : mark_used (fn);
10432 2300 : if (cand->next)
10433 : {
10434 2086 : if (flag_diagnostics_all_candidates)
10435 9 : print_z_candidates (input_location, cand, /*only_viable_p=*/false);
10436 : else
10437 2077 : inform (input_location,
10438 : "use %<-fdiagnostics-all-candidates%> to display "
10439 : "considered candidates");
10440 : }
10441 : }
10442 1031347 : return error_mark_node;
10443 : }
10444 :
10445 200480066 : if (DECL_FUNCTION_MEMBER_P (fn))
10446 : {
10447 106867221 : tree access_fn;
10448 : /* If FN is a template function, two cases must be considered.
10449 : For example:
10450 :
10451 : struct A {
10452 : protected:
10453 : template <class T> void f();
10454 : };
10455 : template <class T> struct B {
10456 : protected:
10457 : void g();
10458 : };
10459 : struct C : A, B<int> {
10460 : using A::f; // #1
10461 : using B<int>::g; // #2
10462 : };
10463 :
10464 : In case #1 where `A::f' is a member template, DECL_ACCESS is
10465 : recorded in the primary template but not in its specialization.
10466 : We check access of FN using its primary template.
10467 :
10468 : In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
10469 : because it is a member of class template B, DECL_ACCESS is
10470 : recorded in the specialization `B<int>::g'. We cannot use its
10471 : primary template because `B<T>::g' and `B<int>::g' may have
10472 : different access. */
10473 106867221 : if (DECL_TEMPLATE_INFO (fn)
10474 106867221 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
10475 8041593 : access_fn = DECL_TI_TEMPLATE (fn);
10476 : else
10477 : access_fn = fn;
10478 106867221 : if (!perform_or_defer_access_check (cand->access_path, access_fn,
10479 : fn, complain))
10480 21067 : return error_mark_node;
10481 : }
10482 :
10483 : /* If we're checking for implicit delete, don't bother with argument
10484 : conversions. */
10485 200458999 : if (flags & LOOKUP_SPECULATIVE)
10486 : {
10487 26002905 : if (cand->viable == 1)
10488 : return fn;
10489 178 : else if (!(complain & tf_error))
10490 : /* Reject bad conversions now. */
10491 146 : return error_mark_node;
10492 : /* else continue to get conversion error. */
10493 : }
10494 :
10495 174456094 : not_really_used:
10496 :
10497 : /* N3276 magic doesn't apply to nested calls. */
10498 174591880 : tsubst_flags_t decltype_flag = (complain & tf_decltype);
10499 174591880 : complain &= ~tf_decltype;
10500 : /* No-Cleanup doesn't apply to nested calls either. */
10501 174591880 : tsubst_flags_t no_cleanup_complain = complain;
10502 174591880 : complain &= ~tf_no_cleanup;
10503 :
10504 : /* Find maximum size of vector to hold converted arguments. */
10505 174591880 : parmlen = list_length (parm);
10506 328573154 : nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0);
10507 174591880 : if (parmlen > nargs)
10508 : nargs = parmlen;
10509 174591880 : argarray = XALLOCAVEC (tree, nargs);
10510 :
10511 349183757 : in_consteval_if_p_temp_override icip;
10512 : /* If the call is immediate function invocation, make sure
10513 : taking address of immediate functions is allowed in its arguments. */
10514 174591880 : if (immediate_invocation_p (STRIP_TEMPLATE (fn)))
10515 1561918 : in_consteval_if_p = true;
10516 :
10517 174591880 : int argarray_size = 0;
10518 174591880 : unsigned int arg_index = 0;
10519 174591880 : int conv_index = 0;
10520 174591880 : int param_index = 0;
10521 174591880 : tree parmd = DECL_ARGUMENTS (fn);
10522 :
10523 242569023 : auto consume_object_arg = [&arg_index, &first_arg, args]()
10524 : {
10525 67977143 : if (!first_arg)
10526 0 : return (*args)[arg_index++];
10527 67977143 : tree object_arg = first_arg;
10528 67977143 : first_arg = NULL_TREE;
10529 67977143 : return object_arg;
10530 174591880 : };
10531 :
10532 : /* The implicit parameters to a constructor are not considered by overload
10533 : resolution, and must be of the proper type. */
10534 174591880 : if (DECL_CONSTRUCTOR_P (fn))
10535 : {
10536 20826360 : tree object_arg = consume_object_arg ();
10537 20826360 : argarray[argarray_size++] = build_this (object_arg);
10538 20826360 : parm = TREE_CHAIN (parm);
10539 20826360 : if (parmd)
10540 20826360 : parmd = DECL_CHAIN (parmd);
10541 : /* We should never try to call the abstract constructor. */
10542 20826360 : gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
10543 :
10544 20826360 : if (DECL_HAS_VTT_PARM_P (fn))
10545 : {
10546 17635 : argarray[argarray_size++] = (*args)[arg_index];
10547 17635 : ++arg_index;
10548 17635 : parm = TREE_CHAIN (parm);
10549 17635 : if (parmd)
10550 17635 : parmd = DECL_CHAIN (parmd);
10551 : }
10552 : }
10553 : /* Bypass access control for 'this' parameter. */
10554 153765520 : else if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
10555 : {
10556 47141539 : tree arg = build_this (consume_object_arg ());
10557 47141539 : tree argtype = TREE_TYPE (arg);
10558 :
10559 47141539 : if (arg == error_mark_node)
10560 : return error_mark_node;
10561 47141536 : if (convs[conv_index++]->bad_p)
10562 : {
10563 1231 : if (complain & tf_error)
10564 : {
10565 105 : auto_diagnostic_group d;
10566 105 : if (permerror (input_location, "passing %qT as %<this%> "
10567 : "argument discards qualifiers",
10568 105 : TREE_TYPE (argtype)))
10569 102 : inform (DECL_SOURCE_LOCATION (fn), " in call to %qD", fn);
10570 105 : }
10571 : else
10572 : return error_mark_node;
10573 : }
10574 :
10575 : /* The class where FN is defined. */
10576 47140410 : tree ctx = DECL_CONTEXT (fn);
10577 :
10578 : /* See if the function member or the whole class type is declared
10579 : final and the call can be devirtualized. */
10580 47140410 : if (DECL_FINAL_P (fn) || CLASSTYPE_FINAL (ctx))
10581 133824 : flags |= LOOKUP_NONVIRTUAL;
10582 :
10583 : /* [class.mfct.non-static]: If a non-static member function of a class
10584 : X is called for an object that is not of type X, or of a type
10585 : derived from X, the behavior is undefined.
10586 :
10587 : So we can assume that anything passed as 'this' is non-null, and
10588 : optimize accordingly. */
10589 : /* Check that the base class is accessible. */
10590 47140410 : if (!accessible_base_p (TREE_TYPE (argtype),
10591 47140410 : BINFO_TYPE (cand->conversion_path), true))
10592 : {
10593 33 : if (complain & tf_error)
10594 30 : error ("%qT is not an accessible base of %qT",
10595 30 : BINFO_TYPE (cand->conversion_path),
10596 30 : TREE_TYPE (argtype));
10597 : else
10598 3 : return error_mark_node;
10599 : }
10600 : /* If fn was found by a using declaration, the conversion path
10601 : will be to the derived class, not the base declaring fn. We
10602 : must convert to the base. */
10603 47140407 : tree base_binfo = cand->conversion_path;
10604 47140407 : if (BINFO_TYPE (base_binfo) != ctx)
10605 : {
10606 122629 : base_binfo = lookup_base (base_binfo, ctx, ba_unique, NULL, complain);
10607 122629 : if (base_binfo == error_mark_node)
10608 : return error_mark_node;
10609 : }
10610 :
10611 : /* If we know the dynamic type of the object, look up the final overrider
10612 : in the BINFO. */
10613 48057798 : if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
10614 47643223 : && resolves_to_fixed_type_p (arg))
10615 : {
10616 1212 : tree ov = lookup_vfn_in_binfo (DECL_VINDEX (fn), base_binfo);
10617 :
10618 : /* And unwind base_binfo to match. If we don't find the type we're
10619 : looking for in BINFO_INHERITANCE_CHAIN, we're looking at diamond
10620 : inheritance; for now do a normal virtual call in that case. */
10621 1212 : tree octx = DECL_CONTEXT (ov);
10622 1212 : tree obinfo = base_binfo;
10623 2511 : while (obinfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (obinfo), octx))
10624 45 : obinfo = BINFO_INHERITANCE_CHAIN (obinfo);
10625 1212 : if (obinfo)
10626 : {
10627 1209 : fn = ov;
10628 1209 : base_binfo = obinfo;
10629 1209 : flags |= LOOKUP_NONVIRTUAL;
10630 : }
10631 : }
10632 :
10633 47140401 : tree converted_arg = build_base_path (PLUS_EXPR, arg,
10634 : base_binfo, 1, complain);
10635 :
10636 47140401 : argarray[argarray_size++] = converted_arg;
10637 47140401 : parm = TREE_CHAIN (parm);
10638 47140401 : if (parmd)
10639 47140401 : parmd = DECL_CHAIN (parmd);
10640 : }
10641 :
10642 311616714 : auto handle_arg = [fn, flags](tree type,
10643 : tree arg,
10644 : int const param_index,
10645 : conversion *conv,
10646 : tsubst_flags_t const arg_complain)
10647 : {
10648 : /* Set user_conv_p on the argument conversions, so rvalue/base handling
10649 : knows not to allow any more UDCs. This needs to happen after we
10650 : process cand->warnings. */
10651 137025972 : if (flags & LOOKUP_NO_CONVERSION)
10652 3729907 : conv->user_conv_p = true;
10653 :
10654 137025972 : if (arg_complain & tf_warning)
10655 99951706 : maybe_warn_pessimizing_move (arg, type, /*return_p=*/false);
10656 :
10657 137025972 : tree val = convert_like_with_context (conv, arg, fn,
10658 : param_index, arg_complain);
10659 137025972 : val = convert_for_arg_passing (type, val, arg_complain);
10660 137025972 : return val;
10661 174590742 : };
10662 :
10663 312755739 : auto handle_indeterminate_arg = [](tree parmd, tree val)
10664 : {
10665 138164997 : if (parmd
10666 138164997 : && lookup_attribute (NULL, "indeterminate", DECL_ATTRIBUTES (parmd)))
10667 : {
10668 48 : STRIP_NOPS (val);
10669 48 : if (TREE_CODE (val) == ADDR_EXPR
10670 48 : && TREE_CODE (TREE_OPERAND (val, 0)) == TARGET_EXPR)
10671 : {
10672 48 : val = TARGET_EXPR_SLOT (TREE_OPERAND (val, 0));
10673 48 : if (auto_var_p (val) && DECL_ARTIFICIAL (val))
10674 : {
10675 48 : tree id = get_identifier ("indeterminate");
10676 48 : DECL_ATTRIBUTES (val)
10677 96 : = tree_cons (build_tree_list (NULL_TREE, id), NULL_TREE,
10678 48 : DECL_ATTRIBUTES (val));
10679 : }
10680 : }
10681 : }
10682 138164997 : };
10683 :
10684 174590742 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
10685 : {
10686 9244 : gcc_assert (cand->num_convs > 0);
10687 9244 : tree object_arg = consume_object_arg ();
10688 9244 : val = handle_arg (TREE_VALUE (parm),
10689 : object_arg,
10690 : param_index++,
10691 9244 : convs[conv_index++],
10692 : complain);
10693 :
10694 9244 : if (val == error_mark_node)
10695 : return error_mark_node;
10696 : else
10697 : {
10698 9076 : argarray[argarray_size++] = val;
10699 9076 : handle_indeterminate_arg (parmd, val);
10700 : }
10701 9076 : parm = TREE_CHAIN (parm);
10702 9076 : if (parmd)
10703 9076 : parmd = DECL_CHAIN (parmd);
10704 : }
10705 :
10706 174590574 : gcc_assert (first_arg == NULL_TREE);
10707 311605476 : for (; arg_index < vec_safe_length (args) && parm;
10708 137014902 : parm = TREE_CHAIN (parm), ++arg_index, ++param_index, ++conv_index)
10709 : {
10710 137016728 : tree current_arg = (*args)[arg_index];
10711 :
10712 : /* If the argument is NULL and used to (implicitly) instantiate a
10713 : template function (and bind one of the template arguments to
10714 : the type of 'long int'), we don't want to warn about passing NULL
10715 : to non-pointer argument.
10716 : For example, if we have this template function:
10717 :
10718 : template<typename T> void func(T x) {}
10719 :
10720 : we want to warn (when -Wconversion is enabled) in this case:
10721 :
10722 : void foo() {
10723 : func<int>(NULL);
10724 : }
10725 :
10726 : but not in this case:
10727 :
10728 : void foo() {
10729 : func(NULL);
10730 : }
10731 : */
10732 137016728 : bool conversion_warning = !(null_node_p (current_arg)
10733 48297 : && DECL_TEMPLATE_INFO (fn)
10734 61 : && cand->template_decl
10735 30 : && !cand->explicit_targs);
10736 :
10737 : /* Also don't warn about (x <=> y) < 0 (c++/100903). */
10738 : if (conversion_warning
10739 137016713 : && integer_zerop (current_arg)
10740 10621594 : && warn_zero_as_null_pointer_constant
10741 195 : && !warning_enabled_at (DECL_SOURCE_LOCATION (fn),
10742 195 : OPT_Wzero_as_null_pointer_constant))
10743 120 : conversion_warning = false;
10744 :
10745 135 : tsubst_flags_t const arg_complain
10746 137016608 : = conversion_warning ? complain : complain & ~tf_warning;
10747 :
10748 137016728 : val = handle_arg (TREE_VALUE (parm),
10749 : current_arg,
10750 : param_index,
10751 137016728 : convs[conv_index],
10752 : arg_complain);
10753 :
10754 137016728 : if (val == error_mark_node)
10755 : return error_mark_node;
10756 : else
10757 : {
10758 137014902 : argarray[argarray_size++] = val;
10759 137014902 : handle_indeterminate_arg (parmd, val);
10760 : }
10761 137014902 : if (parmd)
10762 125780165 : parmd = DECL_CHAIN (parmd);
10763 : }
10764 :
10765 : /* Default arguments */
10766 175729767 : for (; parm && parm != void_list_node;
10767 1141019 : parm = TREE_CHAIN (parm), param_index++)
10768 : {
10769 1141140 : if (TREE_VALUE (parm) == error_mark_node)
10770 : return error_mark_node;
10771 2282256 : val = convert_default_arg (TREE_VALUE (parm),
10772 1141128 : TREE_PURPOSE (parm),
10773 : fn, param_index,
10774 : complain);
10775 1141128 : if (val == error_mark_node)
10776 : return error_mark_node;
10777 1141019 : argarray[argarray_size++] = val;
10778 1141019 : handle_indeterminate_arg (parmd, val);
10779 1141019 : if (parmd)
10780 1141019 : parmd = DECL_CHAIN (parmd);
10781 : }
10782 :
10783 : /* Ellipsis */
10784 174588627 : int magic = magic_varargs_p (fn);
10785 352245022 : for (; arg_index < vec_safe_length (args); ++arg_index)
10786 : {
10787 3067789 : tree a = (*args)[arg_index];
10788 3067789 : if ((magic == 3 && arg_index == 2) || (magic == 4 && arg_index == 0))
10789 : {
10790 : /* Do no conversions for certain magic varargs. */
10791 122044 : a = mark_type_use (a);
10792 122044 : if (TREE_CODE (a) == FUNCTION_DECL && reject_gcc_builtin (a))
10793 0 : return error_mark_node;
10794 : }
10795 2945745 : else if (magic != 0)
10796 : {
10797 : /* Don't truncate excess precision to the semantic type. */
10798 1501477 : if (magic == 1 && TREE_CODE (a) == EXCESS_PRECISION_EXPR)
10799 84 : a = TREE_OPERAND (a, 0);
10800 : /* For other magic varargs only do decay_conversion. */
10801 1501477 : a = decay_conversion (a, complain);
10802 : }
10803 1444268 : else if (DECL_CONSTRUCTOR_P (fn)
10804 387 : && vec_safe_length (args) == 1
10805 1444469 : && same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (fn),
10806 201 : TREE_TYPE (a)))
10807 : {
10808 : /* Avoid infinite recursion trying to call A(...). */
10809 3 : if (complain & tf_error)
10810 : /* Try to call the actual copy constructor for a good error. */
10811 3 : call_copy_ctor (a, complain);
10812 3 : return error_mark_node;
10813 : }
10814 : else
10815 1444265 : a = convert_arg_to_ellipsis (a, complain);
10816 3067786 : if (a == error_mark_node)
10817 : return error_mark_node;
10818 3067768 : argarray[argarray_size++] = a;
10819 : }
10820 :
10821 174588606 : gcc_assert (argarray_size <= nargs);
10822 174588606 : nargs = argarray_size;
10823 174588606 : icip.reset ();
10824 :
10825 : /* Avoid performing argument transformation if warnings are disabled.
10826 : When tf_warning is set and at least one of the warnings is active
10827 : the check_function_arguments function might warn about something. */
10828 :
10829 174588606 : bool warned_p = false;
10830 174588606 : if ((complain & tf_warning)
10831 113673881 : && (warn_nonnull
10832 111685297 : || warn_format
10833 111685294 : || warn_suggest_attribute_format
10834 111685198 : || warn_restrict))
10835 : {
10836 1990930 : tree *fargs = (!nargs ? argarray
10837 1640908 : : (tree *) alloca (nargs * sizeof (tree)));
10838 6052995 : for (int j = 0; j < nargs; j++)
10839 : {
10840 : /* For -Wformat undo the implicit passing by hidden reference
10841 : done by convert_arg_to_ellipsis. */
10842 4062065 : if (TREE_CODE (argarray[j]) == ADDR_EXPR
10843 4062065 : && TYPE_REF_P (TREE_TYPE (argarray[j])))
10844 1501 : fargs[j] = TREE_OPERAND (argarray[j], 0);
10845 : else
10846 4060564 : fargs[j] = argarray[j];
10847 : }
10848 :
10849 1990930 : warned_p = check_function_arguments (input_location, fn, TREE_TYPE (fn),
10850 : nargs, fargs, NULL,
10851 : cp_comp_parm_types);
10852 : }
10853 :
10854 349177212 : if (DECL_INHERITED_CTOR (fn))
10855 : {
10856 : /* Check for passing ellipsis arguments to an inherited constructor. We
10857 : could handle this by open-coding the inherited constructor rather than
10858 : defining it, but let's not bother now. */
10859 64784 : if (!cp_unevaluated_operand
10860 64361 : && cand->num_convs
10861 64361 : && cand->convs[cand->num_convs-1]->ellipsis_p)
10862 : {
10863 15 : if (complain & tf_error)
10864 : {
10865 15 : auto_diagnostic_group d;
10866 15 : sorry ("passing arguments to ellipsis of inherited constructor "
10867 : "%qD", cand->fn);
10868 15 : inform (DECL_SOURCE_LOCATION (cand->fn), "declared here");
10869 15 : }
10870 15 : return error_mark_node;
10871 : }
10872 :
10873 : /* A base constructor inheriting from a virtual base doesn't get the
10874 : inherited arguments, just this and __vtt. */
10875 64769 : if (ctor_omit_inherited_parms (fn))
10876 174588591 : nargs = 2;
10877 : }
10878 :
10879 : /* Avoid actually calling copy constructors and copy assignment operators,
10880 : if possible. */
10881 :
10882 174588591 : if (!force_elide
10883 174453405 : && (!flag_elide_constructors
10884 : /* It's unsafe to elide the operation when handling
10885 : a noexcept-expression, it may evaluate to the wrong
10886 : value (c++/53025, c++/96090). */
10887 174453201 : || cp_noexcept_operand != 0))
10888 : /* Do things the hard way. */;
10889 171801430 : else if (cand->num_convs == 1
10890 262346670 : && (DECL_COPY_CONSTRUCTOR_P (fn)
10891 170271440 : || DECL_MOVE_CONSTRUCTOR_P (fn)))
10892 : {
10893 8251269 : tree targ;
10894 8251269 : tree arg = argarray[num_artificial_parms_for (fn)];
10895 8251269 : tree fa = argarray[0];
10896 8251269 : bool trivial = trivial_fn_p (fn);
10897 :
10898 : /* Pull out the real argument, disregarding const-correctness. */
10899 8251269 : targ = arg;
10900 : /* Strip the reference binding for the constructor parameter. */
10901 0 : if (CONVERT_EXPR_P (targ)
10902 8251269 : && TYPE_REF_P (TREE_TYPE (targ)))
10903 8251269 : targ = TREE_OPERAND (targ, 0);
10904 : /* But don't strip any other reference bindings; binding a temporary to a
10905 : reference prevents copy elision. */
10906 13665622 : while ((CONVERT_EXPR_P (targ)
10907 11514448 : && !TYPE_REF_P (TREE_TYPE (targ)))
10908 28666507 : || TREE_CODE (targ) == NON_LVALUE_EXPR)
10909 10311204 : targ = TREE_OPERAND (targ, 0);
10910 8251269 : if (TREE_CODE (targ) == ADDR_EXPR)
10911 : {
10912 2501539 : targ = TREE_OPERAND (targ, 0);
10913 2501539 : if (!same_type_ignoring_top_level_qualifiers_p
10914 2501539 : (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
10915 : targ = NULL_TREE;
10916 : }
10917 : else
10918 : targ = NULL_TREE;
10919 :
10920 2501087 : if (targ)
10921 : arg = targ;
10922 : else
10923 5750182 : arg = cp_build_fold_indirect_ref (arg);
10924 :
10925 : /* In C++17 we shouldn't be copying a TARGET_EXPR except into a
10926 : potentially-overlapping subobject. */
10927 8251269 : if (CHECKING_P && cxx_dialect >= cxx17)
10928 8171759 : gcc_assert (TREE_CODE (arg) != TARGET_EXPR
10929 : || force_elide
10930 : /* It's from binding the ref parm to a packed field. */
10931 : || convs[0]->need_temporary_p
10932 : || seen_error ()
10933 : /* See unsafe_copy_elision_p. */
10934 : || unsafe_return_slot_p (fa));
10935 :
10936 8251269 : bool unsafe = unsafe_copy_elision_p_opt (fa, arg);
10937 8251269 : bool eliding_temp = (TREE_CODE (arg) == TARGET_EXPR && !unsafe);
10938 :
10939 : /* [class.copy]: the copy constructor is implicitly defined even if the
10940 : implementation elided its use. But don't warn about deprecation when
10941 : eliding a temporary, as then no copy is actually performed. */
10942 8251269 : warning_sentinel s (warn_deprecated_copy, eliding_temp);
10943 8251269 : if (force_elide)
10944 : /* The language says this isn't called. */;
10945 8116083 : else if (!trivial)
10946 : {
10947 1367504 : if (!mark_used (fn, complain) && !(complain & tf_error))
10948 0 : return error_mark_node;
10949 : already_used = true;
10950 : }
10951 : else
10952 6748579 : cp_handle_deprecated_or_unavailable (fn, complain);
10953 :
10954 197415 : if (eliding_temp && DECL_BASE_CONSTRUCTOR_P (fn)
10955 8251465 : && !make_base_init_ok (arg))
10956 : unsafe = true;
10957 :
10958 : /* If we're creating a temp and we already have one, don't create a
10959 : new one. If we're not creating a temp but we get one, use
10960 : INIT_EXPR to collapse the temp into our target. Otherwise, if the
10961 : ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
10962 : temp or an INIT_EXPR otherwise. */
10963 8251269 : if (is_dummy_object (fa))
10964 : {
10965 6083567 : if (TREE_CODE (arg) == TARGET_EXPR)
10966 : return arg;
10967 5917473 : else if (trivial)
10968 5343938 : return force_target_expr (DECL_CONTEXT (fn), arg, complain);
10969 : }
10970 2167702 : else if ((trivial || TREE_CODE (arg) == TARGET_EXPR)
10971 1380257 : && !unsafe)
10972 : {
10973 1380163 : tree to = cp_build_fold_indirect_ref (fa);
10974 1380163 : val = cp_build_init_expr (to, arg);
10975 1380163 : return val;
10976 : }
10977 8251269 : }
10978 163550161 : else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
10979 3245834 : && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR)
10980 165753530 : && trivial_fn_p (fn))
10981 : {
10982 1552137 : tree to = cp_build_fold_indirect_ref (argarray[0]);
10983 1552137 : tree type = TREE_TYPE (to);
10984 1552137 : tree as_base = CLASSTYPE_AS_BASE (type);
10985 1552137 : tree arg = argarray[1];
10986 1552137 : location_t loc = cp_expr_loc_or_input_loc (arg);
10987 :
10988 1552137 : if (is_really_empty_class (type, /*ignore_vptr*/true))
10989 : {
10990 : /* Avoid copying empty classes, but ensure op= returns an lvalue even
10991 : if the object argument isn't one. */
10992 238510 : to = force_lvalue (to, complain);
10993 238510 : val = build2 (COMPOUND_EXPR, type, arg, to);
10994 238510 : suppress_warning (val, OPT_Wunused);
10995 : }
10996 1313627 : else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
10997 : {
10998 1020251 : if (is_std_init_list (type)
10999 1020251 : && conv_binds_ref_to_prvalue (convs[1]))
11000 3 : warning_at (loc, OPT_Winit_list_lifetime,
11001 : "assignment from temporary %<initializer_list%> does "
11002 : "not extend the lifetime of the underlying array");
11003 1020251 : arg = cp_build_fold_indirect_ref (arg);
11004 1020251 : val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
11005 : }
11006 : else
11007 : {
11008 : /* We must only copy the non-tail padding parts. */
11009 293376 : tree arg0, arg2, t;
11010 293376 : tree array_type, alias_set;
11011 :
11012 293376 : arg2 = TYPE_SIZE_UNIT (as_base);
11013 : /* Ensure op= returns an lvalue even if the object argument isn't
11014 : one. */
11015 293376 : to = force_lvalue (to, complain);
11016 293376 : to = cp_stabilize_reference (to);
11017 293376 : arg0 = cp_build_addr_expr (to, complain);
11018 :
11019 293376 : array_type = build_array_type (unsigned_char_type_node,
11020 : build_index_type
11021 : (size_binop (MINUS_EXPR,
11022 : arg2, size_int (1))));
11023 293376 : alias_set = build_int_cst (build_pointer_type (type), 0);
11024 293376 : t = build2 (MODIFY_EXPR, void_type_node,
11025 : build2 (MEM_REF, array_type, arg0, alias_set),
11026 : build2 (MEM_REF, array_type, arg, alias_set));
11027 293376 : val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
11028 293376 : suppress_warning (val, OPT_Wunused);
11029 : }
11030 :
11031 1552137 : cp_handle_deprecated_or_unavailable (fn, complain);
11032 :
11033 1552137 : return val;
11034 : }
11035 161998024 : else if (trivial_fn_p (fn))
11036 : {
11037 7913372 : if (DECL_DESTRUCTOR_P (fn))
11038 3169811 : return build_trivial_dtor_call (argarray[0]);
11039 786875 : else if (default_ctor_p (fn))
11040 : {
11041 786875 : if (is_dummy_object (argarray[0]))
11042 309414 : return force_target_expr (DECL_CONTEXT (fn), void_node,
11043 309414 : no_cleanup_complain);
11044 : else
11045 477461 : return cp_build_fold_indirect_ref (argarray[0]);
11046 : }
11047 : }
11048 :
11049 162189573 : gcc_assert (!force_elide);
11050 :
11051 162189573 : if (!already_used
11052 162189573 : && !mark_used (fn, complain))
11053 795 : return error_mark_node;
11054 :
11055 : /* Warn if the built-in writes to an object of a non-trivial type. */
11056 162188775 : if (warn_class_memaccess
11057 2049303 : && vec_safe_length (args) >= 2
11058 163167512 : && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
11059 69662 : maybe_warn_class_memaccess (input_location, fn, args);
11060 :
11061 162188775 : if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
11062 : {
11063 501613 : tree t;
11064 501613 : tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
11065 501613 : DECL_CONTEXT (fn),
11066 : ba_any, NULL, complain);
11067 501613 : gcc_assert (binfo && binfo != error_mark_node);
11068 :
11069 501613 : argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
11070 : complain);
11071 501613 : if (TREE_SIDE_EFFECTS (argarray[0]))
11072 47640 : argarray[0] = save_expr (argarray[0]);
11073 501613 : t = build_pointer_type (TREE_TYPE (fn));
11074 501613 : fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
11075 501613 : TREE_TYPE (fn) = t;
11076 : }
11077 : else
11078 : {
11079 : /* If FN is marked deprecated or unavailable, then we've already
11080 : issued a diagnostic from mark_used above, so avoid redundantly
11081 : issuing another one from build_addr_func. */
11082 161687162 : auto w = make_temp_override (deprecated_state,
11083 161687162 : UNAVAILABLE_DEPRECATED_SUPPRESS);
11084 :
11085 161687162 : fn = build_addr_func (fn, complain);
11086 161687162 : if (fn == error_mark_node)
11087 0 : return error_mark_node;
11088 :
11089 : /* We're actually invoking the function. (Immediate functions get an
11090 : & when invoking it even though the user didn't use &.) */
11091 161687162 : ADDR_EXPR_DENOTES_CALL_P (fn) = true;
11092 161687162 : }
11093 :
11094 162188775 : tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag);
11095 162188775 : if (call == error_mark_node)
11096 : return call;
11097 162187801 : if (cand->flags & LOOKUP_LIST_INIT_CTOR)
11098 : {
11099 2040270 : tree c = extract_call_expr (call);
11100 : /* build_new_op will clear this when appropriate. */
11101 2040270 : CALL_EXPR_ORDERED_ARGS (c) = true;
11102 : }
11103 162187801 : if (warned_p)
11104 : {
11105 253 : tree c = extract_call_expr (call);
11106 253 : if (TREE_CODE (c) == CALL_EXPR)
11107 253 : suppress_warning (c /* Suppress all warnings. */);
11108 : }
11109 162187548 : else if (TREE_DEPRECATED (fn)
11110 162187548 : && warning_suppressed_at (input_location,
11111 : OPT_Wdeprecated_declarations))
11112 : {
11113 0 : tree c = extract_call_expr (call);
11114 0 : if (TREE_CODE (c) == CALL_EXPR)
11115 0 : TREE_NO_WARNING (c) = true;
11116 : }
11117 :
11118 : return call;
11119 : }
11120 :
11121 : namespace
11122 : {
11123 :
11124 : /* Return the DECL of the first non-static subobject of class TYPE
11125 : that satisfies the predicate PRED or null if none can be found. */
11126 :
11127 : template <class Predicate>
11128 : tree
11129 3556 : first_non_static_field (tree type, Predicate pred)
11130 : {
11131 3556 : if (!type || !CLASS_TYPE_P (type))
11132 : return NULL_TREE;
11133 :
11134 71398 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
11135 : {
11136 68436 : if (TREE_CODE (field) != FIELD_DECL)
11137 43330 : continue;
11138 25106 : if (TREE_STATIC (field))
11139 0 : continue;
11140 25106 : if (pred (field))
11141 : return field;
11142 : }
11143 :
11144 2962 : int i = 0;
11145 :
11146 3082 : for (tree base_binfo, binfo = TYPE_BINFO (type);
11147 3082 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11148 : {
11149 126 : tree base = TREE_TYPE (base_binfo);
11150 126 : if (pred (base))
11151 : return base;
11152 120 : if (tree field = first_non_static_field (base, pred))
11153 : return field;
11154 : }
11155 :
11156 : return NULL_TREE;
11157 : }
11158 :
11159 : struct NonPublicField
11160 : {
11161 24356 : bool operator() (const_tree t) const
11162 : {
11163 24356 : return DECL_P (t) && (TREE_PRIVATE (t) || TREE_PROTECTED (t));
11164 : }
11165 : };
11166 :
11167 : /* Return the DECL of the first non-public subobject of class TYPE
11168 : or null if none can be found. */
11169 :
11170 : static inline tree
11171 3262 : first_non_public_field (tree type)
11172 : {
11173 3262 : return first_non_static_field (type, NonPublicField ());
11174 : }
11175 :
11176 : struct NonTrivialField
11177 : {
11178 876 : bool operator() (const_tree t) const
11179 : {
11180 876 : return !trivial_type_p (DECL_P (t) ? TREE_TYPE (t) : t);
11181 : }
11182 : };
11183 :
11184 : /* Return the DECL of the first non-trivial subobject of class TYPE
11185 : or null if none can be found. */
11186 :
11187 : static inline tree
11188 174 : first_non_trivial_field (tree type)
11189 : {
11190 174 : return first_non_static_field (type, NonTrivialField ());
11191 : }
11192 :
11193 : } /* unnamed namespace */
11194 :
11195 : /* Return true if all copy and move assignment operator overloads for
11196 : class TYPE are trivial and at least one of them is not deleted and,
11197 : when ACCESS is set, accessible. Return false otherwise. Set
11198 : HASASSIGN to true when the TYPE has a (not necessarily trivial)
11199 : copy or move assignment. */
11200 :
11201 : static bool
11202 5021 : has_trivial_copy_assign_p (tree type, bool access, bool *hasassign)
11203 : {
11204 5021 : tree fns = get_class_binding (type, assign_op_identifier);
11205 5021 : bool all_trivial = true;
11206 :
11207 : /* Iterate over overloads of the assignment operator, checking
11208 : accessible copy assignments for triviality. */
11209 :
11210 16515 : for (tree f : ovl_range (fns))
11211 : {
11212 : /* Skip operators that aren't copy assignments. */
11213 8571 : if (!copy_fn_p (f))
11214 3046 : continue;
11215 :
11216 5525 : bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
11217 5813 : || accessible_p (TYPE_BINFO (type), f, true));
11218 :
11219 : /* Skip template assignment operators and deleted functions. */
11220 5525 : if (TREE_CODE (f) != FUNCTION_DECL || DECL_DELETED_FN (f))
11221 694 : continue;
11222 :
11223 4831 : if (accessible)
11224 4579 : *hasassign = true;
11225 :
11226 4579 : if (!accessible || !trivial_fn_p (f))
11227 : all_trivial = false;
11228 :
11229 : /* Break early when both properties have been determined. */
11230 4831 : if (*hasassign && !all_trivial)
11231 : break;
11232 : }
11233 :
11234 : /* Return true if they're all trivial and one of the expressions
11235 : TYPE() = TYPE() or TYPE() = (TYPE&)() is valid. */
11236 5021 : tree ref = cp_build_reference_type (type, false);
11237 5021 : return (all_trivial
11238 5021 : && (is_trivially_xible (MODIFY_EXPR, type, type)
11239 333 : || is_trivially_xible (MODIFY_EXPR, type, ref)));
11240 : }
11241 :
11242 : /* Return true if all copy and move ctor overloads for class TYPE are
11243 : trivial and at least one of them is not deleted and, when ACCESS is
11244 : set, accessible. Return false otherwise. Set each element of HASCTOR[]
11245 : to true when the TYPE has a (not necessarily trivial) default and copy
11246 : (or move) ctor, respectively. */
11247 :
11248 : static bool
11249 5021 : has_trivial_copy_p (tree type, bool access, bool hasctor[2])
11250 : {
11251 5021 : tree fns = get_class_binding (type, complete_ctor_identifier);
11252 5021 : bool all_trivial = true;
11253 :
11254 25141 : for (tree f : ovl_range (fns))
11255 : {
11256 : /* Skip template constructors. */
11257 12602 : if (TREE_CODE (f) != FUNCTION_DECL)
11258 105 : continue;
11259 :
11260 12497 : bool cpy_or_move_ctor_p = copy_fn_p (f);
11261 :
11262 : /* Skip ctors other than default, copy, and move. */
11263 12497 : if (!cpy_or_move_ctor_p && !default_ctor_p (f))
11264 2757 : continue;
11265 :
11266 9740 : if (DECL_DELETED_FN (f))
11267 306 : continue;
11268 :
11269 9434 : bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
11270 9626 : || accessible_p (TYPE_BINFO (type), f, true));
11271 :
11272 : if (accessible)
11273 9314 : hasctor[cpy_or_move_ctor_p] = true;
11274 :
11275 9434 : if (cpy_or_move_ctor_p && (!accessible || !trivial_fn_p (f)))
11276 : all_trivial = false;
11277 :
11278 : /* Break early when both properties have been determined. */
11279 9434 : if (hasctor[0] && hasctor[1] && !all_trivial)
11280 : break;
11281 : }
11282 :
11283 5021 : return all_trivial;
11284 : }
11285 :
11286 : /* Issue a warning on a call to the built-in function FNDECL if it is
11287 : a raw memory write whose destination is not an object of (something
11288 : like) trivial or standard layout type with a non-deleted assignment
11289 : and copy ctor. Detects const correctness violations, corrupting
11290 : references, virtual table pointers, and bypassing non-trivial
11291 : assignments. */
11292 :
11293 : static void
11294 69662 : maybe_warn_class_memaccess (location_t loc, tree fndecl,
11295 : const vec<tree, va_gc> *args)
11296 : {
11297 : /* Except for bcopy where it's second, the destination pointer is
11298 : the first argument for all functions handled here. Compute
11299 : the index of the destination and source arguments. */
11300 69662 : unsigned dstidx = DECL_FUNCTION_CODE (fndecl) == BUILT_IN_BCOPY;
11301 69662 : unsigned srcidx = !dstidx;
11302 :
11303 69662 : tree dest = (*args)[dstidx];
11304 69662 : if (!TREE_TYPE (dest)
11305 69662 : || (TREE_CODE (TREE_TYPE (dest)) != ARRAY_TYPE
11306 68488 : && !INDIRECT_TYPE_P (TREE_TYPE (dest))))
11307 66287 : return;
11308 :
11309 22336 : tree srctype = NULL_TREE;
11310 :
11311 : /* Determine the type of the pointed-to object and whether it's
11312 : a complete class type. */
11313 22336 : tree desttype = TREE_TYPE (TREE_TYPE (dest));
11314 :
11315 22336 : if (!desttype || !COMPLETE_TYPE_P (desttype) || !CLASS_TYPE_P (desttype))
11316 : return;
11317 :
11318 : /* Check to see if the raw memory call is made by a non-static member
11319 : function with THIS as the destination argument for the destination
11320 : type. If so, and if the class has no non-trivial bases or members,
11321 : be more permissive. */
11322 5123 : if (current_function_decl
11323 5123 : && DECL_OBJECT_MEMBER_FUNCTION_P (current_function_decl)
11324 5390 : && is_object_parameter (tree_strip_nop_conversions (dest)))
11325 : {
11326 204 : tree ctx = DECL_CONTEXT (current_function_decl);
11327 204 : bool special = same_type_ignoring_top_level_qualifiers_p (ctx, desttype);
11328 204 : tree binfo = TYPE_BINFO (ctx);
11329 :
11330 204 : if (special
11331 204 : && !BINFO_VTABLE (binfo)
11332 378 : && !first_non_trivial_field (desttype))
11333 : return;
11334 : }
11335 :
11336 : /* True if the class is trivial. */
11337 5021 : bool trivial = trivial_type_p (desttype);
11338 :
11339 : /* Set to true if DESTYPE has an accessible copy assignment. */
11340 5021 : bool hasassign = false;
11341 : /* True if all of the class' overloaded copy assignment operators
11342 : are all trivial (and not deleted) and at least one of them is
11343 : accessible. */
11344 5021 : bool trivassign = has_trivial_copy_assign_p (desttype, true, &hasassign);
11345 :
11346 : /* Set to true if DESTTYPE has an accessible default and copy ctor,
11347 : respectively. */
11348 5021 : bool hasctors[2] = { false, false };
11349 :
11350 : /* True if all of the class' overloaded copy constructors are all
11351 : trivial (and not deleted) and at least one of them is accessible. */
11352 5021 : bool trivcopy = has_trivial_copy_p (desttype, true, hasctors);
11353 :
11354 : /* Set FLD to the first private/protected member of the class. */
11355 5021 : tree fld = trivial ? first_non_public_field (desttype) : NULL_TREE;
11356 :
11357 : /* The warning format string. */
11358 5021 : const char *warnfmt = NULL;
11359 : /* A suggested alternative to offer instead of the raw memory call.
11360 : Empty string when none can be come up with. */
11361 5021 : const char *suggest = "";
11362 5021 : bool warned = false;
11363 :
11364 5021 : auto_diagnostic_group d;
11365 5021 : switch (DECL_FUNCTION_CODE (fndecl))
11366 : {
11367 560 : case BUILT_IN_MEMSET:
11368 560 : if (!integer_zerop (maybe_constant_value ((*args)[1])))
11369 : {
11370 : /* Diagnose setting non-copy-assignable or non-trivial types,
11371 : or types with a private member, to (potentially) non-zero
11372 : bytes. Since the value of the bytes being written is unknown,
11373 : suggest using assignment instead (if one exists). Also warn
11374 : for writes into objects for which zero-initialization doesn't
11375 : mean all bits clear (pointer-to-member data, where null is all
11376 : bits set). Since the value being written is (most likely)
11377 : non-zero, simply suggest assignment (but not copy assignment). */
11378 196 : suggest = "; use assignment instead";
11379 196 : if (!trivassign)
11380 : warnfmt = G_("%qD writing to an object of type %#qT with "
11381 : "no trivial copy-assignment");
11382 125 : else if (!trivial)
11383 : warnfmt = G_("%qD writing to an object of non-trivial type %#qT%s");
11384 85 : else if (fld)
11385 : {
11386 24 : const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
11387 24 : warned = warning_at (loc, OPT_Wclass_memaccess,
11388 : "%qD writing to an object of type %#qT with "
11389 : "%qs member %qD",
11390 : fndecl, desttype, access, fld);
11391 : }
11392 61 : else if (!zero_init_p (desttype))
11393 : warnfmt = G_("%qD writing to an object of type %#qT containing "
11394 : "a pointer to data member%s");
11395 :
11396 : break;
11397 : }
11398 : /* Fall through. */
11399 :
11400 481 : case BUILT_IN_BZERO:
11401 : /* Similarly to the above, diagnose clearing non-trivial or non-
11402 : standard layout objects, or objects of types with no assignmenmt.
11403 : Since the value being written is known to be zero, suggest either
11404 : copy assignment, copy ctor, or default ctor as an alternative,
11405 : depending on what's available. */
11406 :
11407 481 : if (hasassign && hasctors[0])
11408 : suggest = G_("; use assignment or value-initialization instead");
11409 49 : else if (hasassign)
11410 : suggest = G_("; use assignment instead");
11411 31 : else if (hasctors[0])
11412 16 : suggest = G_("; use value-initialization instead");
11413 :
11414 481 : if (!trivassign)
11415 : warnfmt = G_("%qD clearing an object of type %#qT with "
11416 : "no trivial copy-assignment%s");
11417 374 : else if (!trivial)
11418 : warnfmt = G_("%qD clearing an object of non-trivial type %#qT%s");
11419 304 : else if (!zero_init_p (desttype))
11420 : warnfmt = G_("%qD clearing an object of type %#qT containing "
11421 : "a pointer-to-member%s");
11422 : break;
11423 :
11424 2437 : case BUILT_IN_BCOPY:
11425 2437 : case BUILT_IN_MEMCPY:
11426 2437 : case BUILT_IN_MEMMOVE:
11427 2437 : case BUILT_IN_MEMPCPY:
11428 : /* Determine the type of the source object. */
11429 2437 : srctype = TREE_TYPE ((*args)[srcidx]);
11430 2437 : if (!srctype || !INDIRECT_TYPE_P (srctype))
11431 0 : srctype = void_type_node;
11432 : else
11433 2437 : srctype = TREE_TYPE (srctype);
11434 :
11435 : /* Since it's impossible to determine whether the byte copy is
11436 : being used in place of assignment to an existing object or
11437 : as a substitute for initialization, assume it's the former.
11438 : Determine the best alternative to use instead depending on
11439 : what's not deleted. */
11440 2437 : if (hasassign && hasctors[1])
11441 : suggest = G_("; use copy-assignment or copy-initialization instead");
11442 488 : else if (hasassign)
11443 : suggest = G_("; use copy-assignment instead");
11444 341 : else if (hasctors[1])
11445 290 : suggest = G_("; use copy-initialization instead");
11446 :
11447 2437 : if (!trivassign)
11448 : warnfmt = G_("%qD writing to an object of type %#qT with no trivial "
11449 : "copy-assignment%s");
11450 1639 : else if (!trivially_copyable_p (desttype))
11451 : warnfmt = G_("%qD writing to an object of non-trivially copyable "
11452 : "type %#qT%s");
11453 1315 : else if (!trivcopy)
11454 : warnfmt = G_("%qD writing to an object with a deleted copy constructor");
11455 :
11456 1315 : else if (!trivial
11457 211 : && !VOID_TYPE_P (srctype)
11458 160 : && !is_byte_access_type (srctype)
11459 1412 : && !same_type_ignoring_top_level_qualifiers_p (desttype,
11460 : srctype))
11461 : {
11462 : /* Warn when copying into a non-trivial object from an object
11463 : of a different type other than void or char. */
11464 54 : warned = warning_at (loc, OPT_Wclass_memaccess,
11465 : "%qD copying an object of non-trivial type "
11466 : "%#qT from an array of %#qT",
11467 : fndecl, desttype, srctype);
11468 : }
11469 1261 : else if (fld
11470 432 : && !VOID_TYPE_P (srctype)
11471 384 : && !is_byte_access_type (srctype)
11472 1549 : && !same_type_ignoring_top_level_qualifiers_p (desttype,
11473 : srctype))
11474 : {
11475 216 : const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
11476 216 : warned = warning_at (loc, OPT_Wclass_memaccess,
11477 : "%qD copying an object of type %#qT with "
11478 : "%qs member %qD from an array of %#qT; use "
11479 : "assignment or copy-initialization instead",
11480 : fndecl, desttype, access, fld, srctype);
11481 : }
11482 1045 : else if (!trivial && vec_safe_length (args) > 2)
11483 : {
11484 157 : tree sz = maybe_constant_value ((*args)[2]);
11485 157 : if (!tree_fits_uhwi_p (sz))
11486 : break;
11487 :
11488 : /* Finally, warn on partial copies. */
11489 99 : unsigned HOST_WIDE_INT typesize
11490 99 : = tree_to_uhwi (TYPE_SIZE_UNIT (desttype));
11491 99 : if (typesize == 0)
11492 : break;
11493 96 : if (unsigned HOST_WIDE_INT partial = tree_to_uhwi (sz) % typesize)
11494 12 : warned = warning_at (loc, OPT_Wclass_memaccess,
11495 : (typesize - partial > 1
11496 : ? G_("%qD writing to an object of "
11497 : "a non-trivial type %#qT leaves %wu "
11498 : "bytes unchanged")
11499 : : G_("%qD writing to an object of "
11500 : "a non-trivial type %#qT leaves %wu "
11501 : "byte unchanged")),
11502 : fndecl, desttype, typesize - partial);
11503 : }
11504 : break;
11505 :
11506 261 : case BUILT_IN_REALLOC:
11507 :
11508 261 : if (!trivially_copyable_p (desttype))
11509 : warnfmt = G_("%qD moving an object of non-trivially copyable type "
11510 : "%#qT; use %<new%> and %<delete%> instead");
11511 138 : else if (!trivcopy)
11512 : warnfmt = G_("%qD moving an object of type %#qT with deleted copy "
11513 : "constructor; use %<new%> and %<delete%> instead");
11514 135 : else if (!get_dtor (desttype, tf_none))
11515 : warnfmt = G_("%qD moving an object of type %#qT with deleted "
11516 : "destructor");
11517 126 : else if (!trivial)
11518 : {
11519 33 : tree sz = maybe_constant_value ((*args)[1]);
11520 33 : if (TREE_CODE (sz) == INTEGER_CST
11521 33 : && tree_int_cst_lt (sz, TYPE_SIZE_UNIT (desttype)))
11522 : /* Finally, warn on reallocation into insufficient space. */
11523 7 : warned = warning_at (loc, OPT_Wclass_memaccess,
11524 : "%qD moving an object of non-trivial type "
11525 : "%#qT and size %E into a region of size %E",
11526 7 : fndecl, desttype, TYPE_SIZE_UNIT (desttype),
11527 : sz);
11528 : }
11529 : break;
11530 :
11531 1646 : default:
11532 1646 : return;
11533 : }
11534 :
11535 310 : if (warnfmt)
11536 : {
11537 1569 : if (suggest)
11538 1569 : warned = warning_at (loc, OPT_Wclass_memaccess,
11539 : warnfmt, fndecl, desttype, suggest);
11540 : else
11541 : warned = warning_at (loc, OPT_Wclass_memaccess,
11542 : warnfmt, fndecl, desttype);
11543 : }
11544 :
11545 3375 : if (warned)
11546 1879 : inform (location_of (desttype), "%#qT declared here", desttype);
11547 5021 : }
11548 :
11549 : /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
11550 : If FN is the result of resolving an overloaded target built-in,
11551 : ORIG_FNDECL is the original function decl, otherwise it is null.
11552 : This function performs no overload resolution, conversion, or other
11553 : high-level operations. */
11554 :
11555 : tree
11556 171125262 : build_cxx_call (tree fn, int nargs, tree *argarray,
11557 : tsubst_flags_t complain, tree orig_fndecl)
11558 : {
11559 171125262 : tree fndecl;
11560 :
11561 : /* Remember roughly where this call is. */
11562 171125262 : location_t loc = cp_expr_loc_or_input_loc (fn);
11563 171125262 : fn = build_call_a (fn, nargs, argarray);
11564 171125262 : SET_EXPR_LOCATION (fn, loc);
11565 :
11566 171125262 : fndecl = get_callee_fndecl (fn);
11567 171125262 : if (!orig_fndecl)
11568 171125262 : orig_fndecl = fndecl;
11569 :
11570 : /* Check that arguments to builtin functions match the expectations. */
11571 171125262 : if (fndecl
11572 164414376 : && !processing_template_decl
11573 335484930 : && fndecl_built_in_p (fndecl))
11574 : {
11575 : int i;
11576 :
11577 : /* We need to take care that values to BUILT_IN_NORMAL
11578 : are reduced. */
11579 22540493 : for (i = 0; i < nargs; i++)
11580 14764160 : argarray[i] = maybe_constant_value (argarray[i]);
11581 :
11582 7776333 : if (!check_builtin_function_arguments (EXPR_LOCATION (fn), vNULL, fndecl,
11583 : orig_fndecl, nargs, argarray,
11584 : complain & tf_error))
11585 927 : return error_mark_node;
11586 7775406 : else if (fndecl_built_in_p (fndecl, BUILT_IN_CLEAR_PADDING))
11587 : {
11588 14529 : tree arg0 = argarray[0];
11589 14529 : STRIP_NOPS (arg0);
11590 14529 : if (TREE_CODE (arg0) == ADDR_EXPR
11591 264 : && DECL_P (TREE_OPERAND (arg0, 0))
11592 14772 : && same_type_ignoring_top_level_qualifiers_p
11593 243 : (TREE_TYPE (TREE_TYPE (argarray[0])),
11594 243 : TREE_TYPE (TREE_TYPE (arg0))))
11595 : /* For __builtin_clear_padding (&var) we know the type
11596 : is for a complete object, so there is no risk in clearing
11597 : padding that is reused in some derived class member. */;
11598 14293 : else if (!trivially_copyable_p (TREE_TYPE (TREE_TYPE (argarray[0]))))
11599 : {
11600 18 : error_at (EXPR_LOC_OR_LOC (argarray[0], input_location),
11601 : "argument %u in call to function %qE "
11602 : "has pointer to a non-trivially-copyable type (%qT)",
11603 18 : 1, fndecl, TREE_TYPE (argarray[0]));
11604 18 : return error_mark_node;
11605 : }
11606 : }
11607 : }
11608 :
11609 171124317 : if (VOID_TYPE_P (TREE_TYPE (fn)))
11610 37856007 : return maybe_contract_wrap_call (fndecl, fn);
11611 :
11612 : /* 5.2.2/11: If a function call is a prvalue of object type: if the
11613 : function call is either the operand of a decltype-specifier or the
11614 : right operand of a comma operator that is the operand of a
11615 : decltype-specifier, a temporary object is not introduced for the
11616 : prvalue. The type of the prvalue may be incomplete. */
11617 133268310 : if (!(complain & tf_decltype))
11618 : {
11619 110228619 : fn = require_complete_type (fn, complain);
11620 110228619 : if (fn == error_mark_node)
11621 : return error_mark_node;
11622 110228593 : fn = maybe_contract_wrap_call (fndecl, fn);
11623 :
11624 110228593 : if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
11625 : {
11626 12519971 : fn = build_cplus_new (TREE_TYPE (fn), fn, complain);
11627 12519971 : maybe_warn_parm_abi (TREE_TYPE (fn), loc);
11628 : }
11629 : }
11630 : else
11631 23039691 : fn = maybe_contract_wrap_call (fndecl, fn);
11632 133268284 : return convert_from_reference (fn);
11633 : }
11634 :
11635 : /* Returns the value to use for the in-charge parameter when making a
11636 : call to a function with the indicated NAME.
11637 :
11638 : FIXME:Can't we find a neater way to do this mapping? */
11639 :
11640 : tree
11641 37006 : in_charge_arg_for_name (tree name)
11642 : {
11643 37006 : if (IDENTIFIER_CTOR_P (name))
11644 : {
11645 21186 : if (name == complete_ctor_identifier)
11646 10593 : return integer_one_node;
11647 10593 : gcc_checking_assert (name == base_ctor_identifier);
11648 : }
11649 : else
11650 : {
11651 15820 : if (name == complete_dtor_identifier)
11652 7910 : return integer_two_node;
11653 7910 : else if (name == deleting_dtor_identifier)
11654 : /* The deleting dtor should now be handled by
11655 : build_delete_destructor_body. */
11656 0 : gcc_unreachable ();
11657 7910 : gcc_checking_assert (name == base_dtor_identifier);
11658 : }
11659 :
11660 18503 : return integer_zero_node;
11661 : }
11662 :
11663 : /* We've built up a constructor call RET. Complain if it delegates to the
11664 : constructor we're currently compiling. */
11665 :
11666 : static void
11667 365410 : check_self_delegation (tree ret)
11668 : {
11669 365410 : if (TREE_CODE (ret) == TARGET_EXPR)
11670 0 : ret = TARGET_EXPR_INITIAL (ret);
11671 365410 : tree fn = cp_get_callee_fndecl_nofold (ret);
11672 365410 : if (fn && DECL_ABSTRACT_ORIGIN (fn) == current_function_decl)
11673 46 : error ("constructor delegates to itself");
11674 365410 : }
11675 :
11676 : /* Build a call to a constructor, destructor, or an assignment
11677 : operator for INSTANCE, an expression with class type. NAME
11678 : indicates the special member function to call; *ARGS are the
11679 : arguments. ARGS may be NULL. This may change ARGS. BINFO
11680 : indicates the base of INSTANCE that is to be passed as the `this'
11681 : parameter to the member function called.
11682 :
11683 : FLAGS are the LOOKUP_* flags to use when processing the call.
11684 :
11685 : If NAME indicates a complete object constructor, INSTANCE may be
11686 : NULL_TREE. In this case, the caller will call build_cplus_new to
11687 : store the newly constructed object into a VAR_DECL. */
11688 :
11689 : tree
11690 40206579 : build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
11691 : tree binfo, int flags, tsubst_flags_t complain)
11692 : {
11693 40206579 : tree fns;
11694 : /* The type of the subobject to be constructed or destroyed. */
11695 40206579 : tree class_type;
11696 40206579 : vec<tree, va_gc> *allocated = NULL;
11697 40206579 : tree ret;
11698 :
11699 40206579 : gcc_assert (IDENTIFIER_CDTOR_P (name) || name == assign_op_identifier);
11700 :
11701 40206579 : if (error_operand_p (instance))
11702 0 : return error_mark_node;
11703 :
11704 40206579 : if (IDENTIFIER_DTOR_P (name))
11705 : {
11706 10942792 : gcc_assert (args == NULL || vec_safe_is_empty (*args));
11707 10942792 : if (!type_build_dtor_call (TREE_TYPE (instance)))
11708 : /* Shortcut to avoid lazy destructor declaration. */
11709 1491 : return build_trivial_dtor_call (instance);
11710 : }
11711 :
11712 40205088 : if (TYPE_P (binfo))
11713 : {
11714 : /* Resolve the name. */
11715 32142648 : if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
11716 6 : return error_mark_node;
11717 :
11718 32142642 : binfo = TYPE_BINFO (binfo);
11719 : }
11720 :
11721 32142642 : gcc_assert (binfo != NULL_TREE);
11722 :
11723 40205082 : class_type = BINFO_TYPE (binfo);
11724 :
11725 : /* Handle the special case where INSTANCE is NULL_TREE. */
11726 40205082 : if (name == complete_ctor_identifier && !instance)
11727 21329219 : instance = build_dummy_object (class_type);
11728 : else
11729 : {
11730 : /* Convert to the base class, if necessary. */
11731 18875863 : if (!same_type_ignoring_top_level_qualifiers_p
11732 18875863 : (TREE_TYPE (instance), BINFO_TYPE (binfo)))
11733 : {
11734 1660690 : if (IDENTIFIER_CDTOR_P (name))
11735 : /* For constructors and destructors, either the base is
11736 : non-virtual, or it is virtual but we are doing the
11737 : conversion from a constructor or destructor for the
11738 : complete object. In either case, we can convert
11739 : statically. */
11740 1640756 : instance = convert_to_base_statically (instance, binfo);
11741 : else
11742 : {
11743 : /* However, for assignment operators, we must convert
11744 : dynamically if the base is virtual. */
11745 19934 : gcc_checking_assert (name == assign_op_identifier);
11746 19934 : instance = build_base_path (PLUS_EXPR, instance,
11747 : binfo, /*nonnull=*/1, complain);
11748 : }
11749 : }
11750 : }
11751 :
11752 40205082 : gcc_assert (instance != NULL_TREE);
11753 :
11754 : /* In C++17, "If the initializer expression is a prvalue and the
11755 : cv-unqualified version of the source type is the same class as the class
11756 : of the destination, the initializer expression is used to initialize the
11757 : destination object." Handle that here to avoid doing overload
11758 : resolution. */
11759 40205082 : if (cxx_dialect >= cxx17
11760 40005613 : && args && vec_safe_length (*args) == 1
11761 64215393 : && !unsafe_return_slot_p (instance))
11762 : {
11763 22703961 : tree arg = (**args)[0];
11764 :
11765 1726120 : if (BRACE_ENCLOSED_INITIALIZER_P (arg)
11766 1725728 : && !TYPE_HAS_LIST_CTOR (class_type)
11767 1648296 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (arg)
11768 24352240 : && CONSTRUCTOR_NELTS (arg) == 1)
11769 1166168 : arg = CONSTRUCTOR_ELT (arg, 0)->value;
11770 :
11771 22703961 : if ((TREE_CODE (arg) == TARGET_EXPR
11772 11005743 : || TREE_CODE (arg) == CONSTRUCTOR)
11773 34962205 : && (same_type_ignoring_top_level_qualifiers_p
11774 12258244 : (class_type, TREE_TYPE (arg))))
11775 : {
11776 11072969 : if (is_dummy_object (instance))
11777 : return arg;
11778 233598 : else if (TREE_CODE (arg) == TARGET_EXPR)
11779 233598 : TARGET_EXPR_DIRECT_INIT_P (arg) = true;
11780 :
11781 233598 : if ((complain & tf_error)
11782 233596 : && (flags & LOOKUP_DELEGATING_CONS))
11783 0 : check_self_delegation (arg);
11784 : /* Avoid change of behavior on Wunused-var-2.C. */
11785 233598 : instance = mark_lvalue_use (instance);
11786 233598 : return cp_build_init_expr (instance, arg);
11787 : }
11788 : }
11789 :
11790 29132113 : fns = lookup_fnfields (binfo, name, 1, complain);
11791 :
11792 : /* When making a call to a constructor or destructor for a subobject
11793 : that uses virtual base classes, pass down a pointer to a VTT for
11794 : the subobject. */
11795 29132113 : if ((name == base_ctor_identifier
11796 26723244 : || name == base_dtor_identifier)
11797 30772949 : && CLASSTYPE_VBASECLASSES (class_type))
11798 : {
11799 48306 : tree vtt;
11800 48306 : tree sub_vtt;
11801 :
11802 : /* If the current function is a complete object constructor
11803 : or destructor, then we fetch the VTT directly.
11804 : Otherwise, we look it up using the VTT we were given. */
11805 48306 : vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
11806 48306 : vtt = decay_conversion (vtt, complain);
11807 48306 : if (vtt == error_mark_node)
11808 0 : return error_mark_node;
11809 48306 : vtt = build_if_in_charge (vtt, current_vtt_parm);
11810 48306 : if (BINFO_SUBVTT_INDEX (binfo))
11811 48126 : sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
11812 : else
11813 : sub_vtt = vtt;
11814 :
11815 48306 : if (args == NULL)
11816 : {
11817 30667 : allocated = make_tree_vector ();
11818 30667 : args = &allocated;
11819 : }
11820 :
11821 48306 : vec_safe_insert (*args, 0, sub_vtt);
11822 : }
11823 :
11824 58264226 : ret = build_new_method_call (instance, fns, args,
11825 29132113 : TYPE_BINFO (BINFO_TYPE (binfo)),
11826 : flags, /*fn=*/NULL,
11827 : complain);
11828 :
11829 29132113 : if (allocated != NULL)
11830 30667 : release_tree_vector (allocated);
11831 :
11832 29132113 : if ((complain & tf_error)
11833 25915678 : && (flags & LOOKUP_DELEGATING_CONS)
11834 365510 : && name == complete_ctor_identifier)
11835 365410 : check_self_delegation (ret);
11836 :
11837 : return ret;
11838 : }
11839 :
11840 : /* Return the NAME, as a C string. The NAME indicates a function that
11841 : is a member of TYPE. *FREE_P is set to true if the caller must
11842 : free the memory returned.
11843 :
11844 : Rather than go through all of this, we should simply set the names
11845 : of constructors and destructors appropriately, and dispense with
11846 : ctor_identifier, dtor_identifier, etc. */
11847 :
11848 : static char *
11849 93 : name_as_c_string (tree name, tree type, bool *free_p)
11850 : {
11851 93 : const char *pretty_name;
11852 :
11853 : /* Assume that we will not allocate memory. */
11854 93 : *free_p = false;
11855 : /* Constructors and destructors are special. */
11856 93 : if (IDENTIFIER_CDTOR_P (name))
11857 : {
11858 42 : pretty_name
11859 42 : = identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type)));
11860 : /* For a destructor, add the '~'. */
11861 42 : if (IDENTIFIER_DTOR_P (name))
11862 : {
11863 0 : pretty_name = concat ("~", pretty_name, NULL);
11864 : /* Remember that we need to free the memory allocated. */
11865 0 : *free_p = true;
11866 : }
11867 : }
11868 51 : else if (IDENTIFIER_CONV_OP_P (name))
11869 : {
11870 0 : pretty_name = concat ("operator ",
11871 0 : type_as_string_translate (TREE_TYPE (name),
11872 : TFF_PLAIN_IDENTIFIER),
11873 : NULL);
11874 : /* Remember that we need to free the memory allocated. */
11875 0 : *free_p = true;
11876 : }
11877 : else
11878 51 : pretty_name = identifier_to_locale (IDENTIFIER_POINTER (name));
11879 :
11880 93 : return const_cast<char *> (pretty_name);
11881 : }
11882 :
11883 : /* If CANDIDATES contains exactly one candidate, return it, otherwise
11884 : return NULL. */
11885 :
11886 : static z_candidate *
11887 683 : single_z_candidate (z_candidate *candidates)
11888 : {
11889 0 : if (candidates == NULL)
11890 : return NULL;
11891 :
11892 668 : if (candidates->next)
11893 0 : return NULL;
11894 :
11895 : return candidates;
11896 : }
11897 :
11898 : /* If CANDIDATE is invalid due to a bad argument type, return the
11899 : pertinent conversion_info.
11900 :
11901 : Otherwise, return NULL. */
11902 :
11903 : static const conversion_info *
11904 339 : maybe_get_bad_conversion_for_unmatched_call (const z_candidate *candidate)
11905 : {
11906 : /* Must be an rr_arg_conversion or rr_bad_arg_conversion. */
11907 339 : rejection_reason *r = candidate->reason;
11908 :
11909 339 : if (r == NULL)
11910 : return NULL;
11911 :
11912 339 : switch (r->code)
11913 : {
11914 : default:
11915 : return NULL;
11916 :
11917 52 : case rr_arg_conversion:
11918 52 : return &r->u.conversion;
11919 :
11920 6 : case rr_bad_arg_conversion:
11921 6 : return &r->u.bad_conversion;
11922 : }
11923 : }
11924 :
11925 : /* Issue an error and note complaining about a bad argument type at a
11926 : callsite with a single candidate FNDECL.
11927 :
11928 : ARG_LOC is the location of the argument (or UNKNOWN_LOCATION, in which
11929 : case input_location is used).
11930 : FROM_TYPE is the type of the actual argument; TO_TYPE is the type of
11931 : the formal parameter. */
11932 :
11933 : void
11934 150 : complain_about_bad_argument (location_t arg_loc,
11935 : tree from_type, tree to_type,
11936 : tree fndecl, int parmnum)
11937 : {
11938 150 : auto_diagnostic_group d;
11939 150 : range_label_for_type_mismatch rhs_label (from_type, to_type);
11940 150 : range_label *label = &rhs_label;
11941 150 : if (arg_loc == UNKNOWN_LOCATION)
11942 : {
11943 6 : arg_loc = input_location;
11944 6 : label = NULL;
11945 : }
11946 150 : gcc_rich_location richloc (arg_loc, label, highlight_colors::percent_h);
11947 150 : error_at (&richloc,
11948 : "cannot convert %qH to %qI",
11949 : from_type, to_type);
11950 150 : maybe_inform_about_fndecl_for_bogus_argument_init
11951 150 : (fndecl,
11952 : parmnum,
11953 : highlight_colors::percent_i);
11954 150 : }
11955 :
11956 : /* Subroutine of build_new_method_call_1, for where there are no viable
11957 : candidates for the call. */
11958 :
11959 : static void
11960 689 : complain_about_no_candidates_for_method_call (tree instance,
11961 : z_candidate *candidates,
11962 : tree explicit_targs,
11963 : tree basetype,
11964 : tree optype, tree name,
11965 : bool skip_first_for_error,
11966 : vec<tree, va_gc> *user_args)
11967 : {
11968 689 : auto_diagnostic_group d;
11969 689 : if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
11970 0 : cxx_incomplete_type_error (instance, basetype);
11971 689 : else if (optype)
11972 6 : error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
11973 : basetype, optype, build_tree_list_vec (user_args),
11974 6 : TREE_TYPE (instance));
11975 : else
11976 : {
11977 : /* Special-case for when there's a single candidate that's failing
11978 : due to a bad argument type. */
11979 683 : if (z_candidate *candidate = single_z_candidate (candidates))
11980 339 : if (const conversion_info *conv
11981 339 : = maybe_get_bad_conversion_for_unmatched_call (candidate))
11982 : {
11983 58 : tree from_type = conv->from;
11984 58 : if (!TYPE_P (conv->from))
11985 6 : from_type = lvalue_type (conv->from);
11986 58 : complain_about_bad_argument (conv->loc,
11987 58 : from_type, conv->to_type,
11988 58 : candidate->fn, conv->n_arg);
11989 58 : return;
11990 : }
11991 :
11992 625 : tree arglist = build_tree_list_vec (user_args);
11993 625 : tree errname = name;
11994 625 : bool twiddle = false;
11995 625 : if (IDENTIFIER_CDTOR_P (errname))
11996 : {
11997 322 : twiddle = IDENTIFIER_DTOR_P (errname);
11998 322 : errname = constructor_name (basetype);
11999 : }
12000 625 : if (explicit_targs)
12001 48 : errname = lookup_template_function (errname, explicit_targs);
12002 625 : if (skip_first_for_error)
12003 3 : arglist = TREE_CHAIN (arglist);
12004 1250 : error ("no matching function for call to %<%T::%s%E(%A)%#V%>",
12005 625 : basetype, &"~"[!twiddle], errname, arglist,
12006 625 : TREE_TYPE (instance));
12007 : }
12008 631 : print_z_candidates (location_of (name), candidates);
12009 689 : }
12010 :
12011 : /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
12012 : be set, upon return, to the function called. ARGS may be NULL.
12013 : This may change ARGS. */
12014 :
12015 : tree
12016 101500402 : build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args,
12017 : tree conversion_path, int flags,
12018 : tree *fn_p, tsubst_flags_t complain)
12019 : {
12020 101500402 : struct z_candidate *candidates = 0, *cand;
12021 101500402 : tree explicit_targs = NULL_TREE;
12022 101500402 : tree basetype = NULL_TREE;
12023 101500402 : tree access_binfo;
12024 101500402 : tree optype;
12025 101500402 : tree first_mem_arg = NULL_TREE;
12026 101500402 : tree name;
12027 101500402 : bool skip_first_for_error;
12028 101500402 : vec<tree, va_gc> *user_args;
12029 101500402 : tree call;
12030 101500402 : tree fn;
12031 101500402 : int template_only = 0;
12032 101500402 : bool any_viable_p;
12033 101500402 : tree orig_instance;
12034 101500402 : tree orig_fns;
12035 101500402 : vec<tree, va_gc> *orig_args = NULL;
12036 :
12037 101500402 : auto_cond_timevar tv (TV_OVERLOAD);
12038 :
12039 101500402 : gcc_assert (instance != NULL_TREE);
12040 :
12041 : /* We don't know what function we're going to call, yet. */
12042 101500402 : if (fn_p)
12043 28498077 : *fn_p = NULL_TREE;
12044 :
12045 101500402 : if (error_operand_p (instance)
12046 101500402 : || !fns || error_operand_p (fns))
12047 1467011 : return error_mark_node;
12048 :
12049 100033391 : if (!BASELINK_P (fns))
12050 : {
12051 0 : if (complain & tf_error)
12052 0 : error ("call to non-function %qD", fns);
12053 0 : return error_mark_node;
12054 : }
12055 :
12056 100033391 : orig_instance = instance;
12057 100033391 : orig_fns = fns;
12058 :
12059 : /* Dismantle the baselink to collect all the information we need. */
12060 100033391 : if (!conversion_path)
12061 43884365 : conversion_path = BASELINK_BINFO (fns);
12062 100033391 : access_binfo = BASELINK_ACCESS_BINFO (fns);
12063 100033391 : optype = BASELINK_OPTYPE (fns);
12064 100033391 : fns = BASELINK_FUNCTIONS (fns);
12065 100033391 : if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12066 : {
12067 2731259 : explicit_targs = TREE_OPERAND (fns, 1);
12068 2731259 : fns = TREE_OPERAND (fns, 0);
12069 2731259 : template_only = 1;
12070 : }
12071 100033391 : gcc_assert (OVL_P (fns));
12072 100033391 : fn = OVL_FIRST (fns);
12073 100033391 : name = DECL_NAME (fn);
12074 :
12075 100033391 : basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
12076 100033391 : gcc_assert (CLASS_TYPE_P (basetype));
12077 :
12078 100033391 : user_args = args == NULL ? NULL : *args;
12079 : /* Under DR 147 A::A() is an invalid constructor call,
12080 : not a functional cast. */
12081 100033391 : if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
12082 : {
12083 54 : if (! (complain & tf_error))
12084 0 : return error_mark_node;
12085 :
12086 54 : basetype = DECL_CONTEXT (fn);
12087 54 : name = constructor_name (basetype);
12088 54 : auto_diagnostic_group d;
12089 54 : if (permerror (input_location,
12090 : "cannot call constructor %<%T::%D%> directly",
12091 : basetype, name))
12092 54 : inform (input_location, "for a function-style cast, remove the "
12093 : "redundant %<::%D%>", name);
12094 54 : call = build_functional_cast (input_location, basetype,
12095 : build_tree_list_vec (user_args),
12096 : complain);
12097 54 : return call;
12098 54 : }
12099 :
12100 100033337 : if (processing_template_decl)
12101 7186722 : orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
12102 :
12103 : /* Process the argument list. */
12104 99948068 : if (args != NULL && *args != NULL)
12105 : {
12106 85201261 : *args = resolve_args (*args, complain);
12107 85201261 : if (*args == NULL)
12108 189 : return error_mark_node;
12109 : user_args = *args;
12110 : }
12111 :
12112 : /* Consider the object argument to be used even if we end up selecting a
12113 : static member function. */
12114 100033148 : instance = mark_type_use (instance);
12115 :
12116 : /* Figure out whether to skip the first argument for the error
12117 : message we will display to users if an error occurs. We don't
12118 : want to display any compiler-generated arguments. The "this"
12119 : pointer hasn't been added yet. However, we must remove the VTT
12120 : pointer if this is a call to a base-class constructor or
12121 : destructor. */
12122 100033148 : skip_first_for_error = false;
12123 100033148 : if (IDENTIFIER_CDTOR_P (name))
12124 : {
12125 : /* Callers should explicitly indicate whether they want to ctor
12126 : the complete object or just the part without virtual bases. */
12127 53378411 : gcc_assert (name != ctor_identifier);
12128 :
12129 : /* Remove the VTT pointer, if present. */
12130 50969548 : if ((name == base_ctor_identifier || name == base_dtor_identifier)
12131 55019247 : && CLASSTYPE_VBASECLASSES (basetype))
12132 : skip_first_for_error = true;
12133 :
12134 : /* It's OK to call destructors and constructors on cv-qualified
12135 : objects. Therefore, convert the INSTANCE to the unqualified
12136 : type, if necessary. */
12137 53378411 : if (!same_type_p (basetype, TREE_TYPE (instance)))
12138 : {
12139 704602 : instance = build_this (instance);
12140 704602 : instance = build_nop (build_pointer_type (basetype), instance);
12141 704602 : instance = build_fold_indirect_ref (instance);
12142 : }
12143 : }
12144 : else
12145 93309474 : gcc_assert (!DECL_DESTRUCTOR_P (fn) && !DECL_CONSTRUCTOR_P (fn));
12146 :
12147 : /* For the overload resolution we need to find the actual `this`
12148 : that would be captured if the call turns out to be to a
12149 : non-static member function. Do not actually capture it at this
12150 : point. */
12151 200066296 : if (DECL_CONSTRUCTOR_P (fn))
12152 : /* Constructors don't use the enclosing 'this'. */
12153 : first_mem_arg = instance;
12154 : else
12155 73252234 : first_mem_arg = maybe_resolve_dummy (instance, false);
12156 :
12157 100033148 : conversion_obstack_sentinel cos;
12158 :
12159 : /* The number of arguments artificial parms in ARGS; we subtract one because
12160 : there's no 'this' in ARGS. */
12161 100033148 : unsigned skip = num_artificial_parms_for (fn) - 1;
12162 :
12163 : /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
12164 : initializer, not T({ }). */
12165 100033148 : if (DECL_CONSTRUCTOR_P (fn)
12166 22880254 : && vec_safe_length (user_args) > skip
12167 120637628 : && DIRECT_LIST_INIT_P ((*user_args)[skip]))
12168 : {
12169 1727684 : tree init_list = (*user_args)[skip];
12170 1727684 : tree init = NULL_TREE;
12171 :
12172 1727684 : gcc_assert (user_args->length () == skip + 1
12173 : && !(flags & LOOKUP_ONLYCONVERTING));
12174 :
12175 : /* If the initializer list has no elements and T is a class type with
12176 : a default constructor, the object is value-initialized. Handle
12177 : this here so we don't need to handle it wherever we use
12178 : build_special_member_call. */
12179 1727684 : if (CONSTRUCTOR_NELTS (init_list) == 0
12180 196721 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
12181 : /* For a user-provided default constructor, use the normal
12182 : mechanisms so that protected access works. */
12183 196721 : && type_has_non_user_provided_default_constructor (basetype)
12184 1653743 : && !processing_template_decl)
12185 122777 : init = build_value_init (basetype, complain);
12186 :
12187 : /* If BASETYPE is an aggregate, we need to do aggregate
12188 : initialization. */
12189 1604907 : else if (CP_AGGREGATE_TYPE_P (basetype))
12190 : {
12191 41 : init = reshape_init (basetype, init_list, complain);
12192 41 : init = digest_init (basetype, init, complain);
12193 : }
12194 :
12195 122818 : if (init)
12196 : {
12197 122818 : if (is_dummy_object (instance))
12198 36089 : return get_target_expr (init, complain);
12199 86729 : return cp_build_init_expr (instance, init);
12200 : }
12201 :
12202 : /* Otherwise go ahead with overload resolution. */
12203 1604866 : add_list_candidates (fns, first_mem_arg, user_args,
12204 : basetype, explicit_targs, template_only,
12205 : conversion_path, access_binfo, flags,
12206 : &candidates, complain);
12207 : }
12208 : else
12209 98305464 : add_candidates (fns, first_mem_arg, user_args, optype,
12210 : explicit_targs, template_only, conversion_path,
12211 : access_binfo, flags, &candidates, complain);
12212 :
12213 99910330 : any_viable_p = false;
12214 99910330 : candidates = splice_viable (candidates, false, &any_viable_p);
12215 :
12216 99910330 : if (!any_viable_p)
12217 : {
12218 : /* [dcl.init], 17.6.2.2:
12219 :
12220 : Otherwise, if no constructor is viable, the destination type is
12221 : a (possibly cv-qualified) aggregate class A, and the initializer
12222 : is a parenthesized expression-list, the object is initialized as
12223 : follows...
12224 :
12225 : We achieve this by building up a CONSTRUCTOR, as for list-init,
12226 : and setting CONSTRUCTOR_IS_PAREN_INIT to distinguish between
12227 : the two. */
12228 26983 : if (DECL_CONSTRUCTOR_P (fn)
12229 20470 : && !(flags & LOOKUP_ONLYCONVERTING)
12230 20446 : && cxx_dialect >= cxx20
12231 19829 : && CP_AGGREGATE_TYPE_P (basetype)
12232 26983 : && !vec_safe_is_empty (user_args))
12233 : {
12234 : /* Create a CONSTRUCTOR from ARGS, e.g. {1, 2} from <1, 2>. */
12235 1171 : tree ctor = build_constructor_from_vec (init_list_type_node,
12236 : user_args);
12237 1171 : CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true;
12238 1171 : CONSTRUCTOR_IS_PAREN_INIT (ctor) = true;
12239 1171 : if (is_dummy_object (instance))
12240 : return ctor;
12241 : else
12242 : {
12243 677 : ctor = digest_init (basetype, ctor, complain);
12244 677 : if (ctor == error_mark_node)
12245 : return error_mark_node;
12246 457 : return cp_build_init_expr (instance, ctor);
12247 : }
12248 : }
12249 25812 : if (complain & tf_error)
12250 689 : complain_about_no_candidates_for_method_call (instance, candidates,
12251 : explicit_targs, basetype,
12252 : optype, name,
12253 : skip_first_for_error,
12254 : user_args);
12255 25812 : call = error_mark_node;
12256 : }
12257 : else
12258 : {
12259 99883347 : cand = tourney (candidates, complain);
12260 99883347 : if (cand == 0)
12261 : {
12262 193 : char *pretty_name;
12263 193 : bool free_p;
12264 193 : tree arglist;
12265 :
12266 193 : if (complain & tf_error)
12267 : {
12268 93 : pretty_name = name_as_c_string (name, basetype, &free_p);
12269 93 : arglist = build_tree_list_vec (user_args);
12270 93 : if (skip_first_for_error)
12271 0 : arglist = TREE_CHAIN (arglist);
12272 93 : auto_diagnostic_group d;
12273 186 : if (!any_strictly_viable (candidates))
12274 13 : error ("no matching function for call to %<%s(%A)%>",
12275 : pretty_name, arglist);
12276 : else
12277 80 : error ("call of overloaded %<%s(%A)%> is ambiguous",
12278 : pretty_name, arglist);
12279 93 : print_z_candidates (location_of (name), candidates);
12280 93 : if (free_p)
12281 0 : free (pretty_name);
12282 93 : }
12283 193 : call = error_mark_node;
12284 193 : if (fn_p)
12285 84 : *fn_p = error_mark_node;
12286 : }
12287 : else
12288 : {
12289 99883154 : fn = cand->fn;
12290 99883154 : call = NULL_TREE;
12291 :
12292 99883154 : if (!(flags & LOOKUP_NONVIRTUAL)
12293 76637261 : && DECL_PURE_VIRTUAL_P (fn)
12294 210115 : && instance == current_class_ref
12295 100048126 : && (complain & tf_warning))
12296 : {
12297 : /* This is not an error, it is runtime undefined
12298 : behavior. */
12299 164972 : if (!current_function_decl)
12300 3 : warning (0, "pure virtual %q#D called from "
12301 : "non-static data member initializer", fn);
12302 164969 : else if (DECL_CONSTRUCTOR_P (current_function_decl)
12303 164969 : || DECL_DESTRUCTOR_P (current_function_decl))
12304 9 : warning (0, (DECL_CONSTRUCTOR_P (current_function_decl)
12305 : ? G_("pure virtual %q#D called from constructor")
12306 : : G_("pure virtual %q#D called from destructor")),
12307 : fn);
12308 : }
12309 :
12310 114664758 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fn)
12311 170206328 : && !DECL_CONSTRUCTOR_P (fn)
12312 158348832 : && is_dummy_object (instance))
12313 : {
12314 52579 : instance = maybe_resolve_dummy (instance, true);
12315 52579 : if (instance == error_mark_node)
12316 : call = error_mark_node;
12317 52579 : else if (!is_dummy_object (instance))
12318 : {
12319 : /* We captured 'this' in the current lambda now that
12320 : we know we really need it. */
12321 52077 : cand->first_arg = instance;
12322 : }
12323 502 : else if (current_class_ptr && any_dependent_bases_p ())
12324 : /* We can't tell until instantiation time whether we can use
12325 : *this as the implicit object argument. */;
12326 : else
12327 : {
12328 463 : if (complain & tf_error)
12329 69 : error ("cannot call member function %qD without object",
12330 : fn);
12331 463 : call = error_mark_node;
12332 : }
12333 : }
12334 :
12335 99883154 : if (call != error_mark_node)
12336 : {
12337 : /* Now we know what function is being called. */
12338 99882691 : if (fn_p)
12339 27016996 : *fn_p = fn;
12340 : /* Build the actual CALL_EXPR. */
12341 99882691 : call = build_over_call (cand, flags, complain);
12342 :
12343 : /* Suppress warnings for if (my_struct.operator= (x)) where
12344 : my_struct is implicitly converted to bool. */
12345 99882691 : if (TREE_CODE (call) == MODIFY_EXPR)
12346 3189030 : suppress_warning (call, OPT_Wparentheses);
12347 :
12348 : /* In an expression of the form `a->f()' where `f' turns
12349 : out to be a static member function, `a' is
12350 : none-the-less evaluated. */
12351 99882691 : if (!is_dummy_object (instance))
12352 77355955 : call = keep_unused_object_arg (call, instance, fn);
12353 99882691 : if (call != error_mark_node
12354 197756918 : && DECL_DESTRUCTOR_P (cand->fn)
12355 126479239 : && !VOID_TYPE_P (TREE_TYPE (call)))
12356 : /* An explicit call of the form "x->~X()" has type
12357 : "void". However, on platforms where destructors
12358 : return "this" (i.e., those where
12359 : targetm.cxx.cdtor_returns_this is true), such calls
12360 : will appear to have a return value of pointer type
12361 : to the low-level call machinery. We do not want to
12362 : change the low-level machinery, since we want to be
12363 : able to optimize "delete f()" on such platforms as
12364 : "operator delete(~X(f()))" (rather than generating
12365 : "t = f(), ~X(t), operator delete (t)"). */
12366 15431168 : call = build_nop (void_type_node, call);
12367 : }
12368 : }
12369 : }
12370 :
12371 99909159 : if (processing_template_decl && call != error_mark_node)
12372 : {
12373 7186668 : bool cast_to_void = false;
12374 :
12375 7186668 : if (TREE_CODE (call) == COMPOUND_EXPR)
12376 10 : call = TREE_OPERAND (call, 1);
12377 7186658 : else if (TREE_CODE (call) == NOP_EXPR)
12378 : {
12379 0 : cast_to_void = true;
12380 0 : call = TREE_OPERAND (call, 0);
12381 : }
12382 7186668 : if (INDIRECT_REF_P (call))
12383 609932 : call = TREE_OPERAND (call, 0);
12384 :
12385 : /* Prune all but the selected function from the original overload
12386 : set so that we can avoid some duplicate work at instantiation time. */
12387 7186668 : if (really_overloaded_fn (fns))
12388 : {
12389 1643895 : if (DECL_TEMPLATE_INFO (fn)
12390 1643895 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
12391 : {
12392 : /* Use the selected template, not the specialization, so that
12393 : this looks like an actual lookup result for sake of
12394 : filter_memfn_lookup. */
12395 :
12396 280232 : if (OVL_SINGLE_P (fns))
12397 : /* If the original overload set consists of a single function
12398 : template, this isn't beneficial. */
12399 240257 : goto skip_prune;
12400 :
12401 39975 : fn = ovl_make (DECL_TI_TEMPLATE (fn));
12402 39975 : if (template_only)
12403 30279 : fn = lookup_template_function (fn, explicit_targs);
12404 : }
12405 1403638 : orig_fns = copy_node (orig_fns);
12406 1403638 : BASELINK_FUNCTIONS (orig_fns) = fn;
12407 1403638 : BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (orig_fns) = true;
12408 : }
12409 :
12410 5542773 : skip_prune:
12411 7186668 : call = (build_min_non_dep_call_vec
12412 7186668 : (call,
12413 7186668 : build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
12414 : orig_instance, orig_fns, NULL_TREE),
12415 : orig_args));
12416 7186668 : SET_EXPR_LOCATION (call, input_location);
12417 7186668 : call = convert_from_reference (call);
12418 7186668 : if (cast_to_void)
12419 0 : call = build_nop (void_type_node, call);
12420 : }
12421 :
12422 99909159 : if (orig_args != NULL)
12423 7101447 : release_tree_vector (orig_args);
12424 :
12425 : return call;
12426 101500402 : }
12427 :
12428 : /* Returns true iff standard conversion sequence ICS1 is a proper
12429 : subsequence of ICS2. */
12430 :
12431 : static bool
12432 79946255 : is_subseq (conversion *ics1, conversion *ics2)
12433 : {
12434 : /* We can assume that a conversion of the same code
12435 : between the same types indicates a subsequence since we only get
12436 : here if the types we are converting from are the same. */
12437 :
12438 79946255 : while (ics1->kind == ck_rvalue
12439 95100046 : || ics1->kind == ck_lvalue)
12440 15153791 : ics1 = next_conversion (ics1);
12441 :
12442 : while (1)
12443 : {
12444 107249156 : while (ics2->kind == ck_rvalue
12445 107249156 : || ics2->kind == ck_lvalue)
12446 15153791 : ics2 = next_conversion (ics2);
12447 :
12448 92095365 : if (ics2->kind == ck_user
12449 92095365 : || !has_next (ics2->kind))
12450 : /* At this point, ICS1 cannot be a proper subsequence of
12451 : ICS2. We can get a USER_CONV when we are comparing the
12452 : second standard conversion sequence of two user conversion
12453 : sequences. */
12454 : return false;
12455 :
12456 12155551 : ics2 = next_conversion (ics2);
12457 :
12458 12155551 : while (ics2->kind == ck_rvalue
12459 19240667 : || ics2->kind == ck_lvalue)
12460 7085116 : ics2 = next_conversion (ics2);
12461 :
12462 12155551 : if (ics2->kind == ics1->kind
12463 6486 : && same_type_p (ics2->type, ics1->type)
12464 12161992 : && (ics1->kind == ck_identity
12465 6441 : || same_type_p (next_conversion (ics2)->type,
12466 : next_conversion (ics1)->type)))
12467 : return true;
12468 : }
12469 : }
12470 :
12471 : /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
12472 : be any _TYPE nodes. */
12473 :
12474 : bool
12475 644477315 : is_properly_derived_from (tree derived, tree base)
12476 : {
12477 644477315 : if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
12478 : return false;
12479 :
12480 : /* We only allow proper derivation here. The DERIVED_FROM_P macro
12481 : considers every class derived from itself. */
12482 484636886 : return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
12483 484636886 : && DERIVED_FROM_P (base, derived));
12484 : }
12485 :
12486 : /* We build the ICS for an implicit object parameter as a pointer
12487 : conversion sequence. However, such a sequence should be compared
12488 : as if it were a reference conversion sequence. If ICS is the
12489 : implicit conversion sequence for an implicit object parameter,
12490 : modify it accordingly. */
12491 :
12492 : static void
12493 167092892 : maybe_handle_implicit_object (conversion **ics)
12494 : {
12495 167092892 : if ((*ics)->this_p)
12496 : {
12497 : /* [over.match.funcs]
12498 :
12499 : For non-static member functions, the type of the
12500 : implicit object parameter is "reference to cv X"
12501 : where X is the class of which the function is a
12502 : member and cv is the cv-qualification on the member
12503 : function declaration. */
12504 13315853 : conversion *t = *ics;
12505 13315853 : tree reference_type;
12506 :
12507 : /* The `this' parameter is a pointer to a class type. Make the
12508 : implicit conversion talk about a reference to that same class
12509 : type. */
12510 13315853 : reference_type = TREE_TYPE (t->type);
12511 13315853 : reference_type = build_reference_type (reference_type);
12512 :
12513 13315853 : if (t->kind == ck_qual)
12514 3188610 : t = next_conversion (t);
12515 13315853 : if (t->kind == ck_ptr)
12516 622595 : t = next_conversion (t);
12517 13315853 : t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
12518 13315853 : t = direct_reference_binding (reference_type, t);
12519 13315853 : t->this_p = 1;
12520 13315853 : t->rvaluedness_matches_p = 0;
12521 13315853 : *ics = t;
12522 : }
12523 167092892 : }
12524 :
12525 : /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
12526 : and return the initial reference binding conversion. Otherwise,
12527 : leave *ICS unchanged and return NULL. */
12528 :
12529 : static conversion *
12530 167092892 : maybe_handle_ref_bind (conversion **ics)
12531 : {
12532 167092892 : if ((*ics)->kind == ck_ref_bind)
12533 : {
12534 61584286 : conversion *old_ics = *ics;
12535 61584286 : *ics = next_conversion (old_ics);
12536 61584286 : (*ics)->user_conv_p = old_ics->user_conv_p;
12537 61584286 : return old_ics;
12538 : }
12539 :
12540 : return NULL;
12541 : }
12542 :
12543 : /* Get the expression at the beginning of the conversion chain C. */
12544 :
12545 : static tree
12546 51 : conv_get_original_expr (conversion *c)
12547 : {
12548 60 : for (; c; c = next_conversion (c))
12549 60 : if (c->kind == ck_identity || c->kind == ck_ambig || c->kind == ck_aggr)
12550 51 : return c->u.expr;
12551 : return NULL_TREE;
12552 : }
12553 :
12554 : /* Return a tree representing the number of elements initialized by the
12555 : list-initialization C. The caller must check that C converts to an
12556 : array type. */
12557 :
12558 : static tree
12559 126 : nelts_initialized_by_list_init (conversion *c)
12560 : {
12561 : /* If the array we're converting to has a dimension, we'll use that. */
12562 126 : if (TYPE_DOMAIN (c->type))
12563 84 : return array_type_nelts_top (c->type);
12564 : else
12565 : {
12566 : /* Otherwise, we look at how many elements the constructor we're
12567 : initializing from has. */
12568 42 : tree ctor = conv_get_original_expr (c);
12569 72 : return size_int (CONSTRUCTOR_NELTS (ctor));
12570 : }
12571 : }
12572 :
12573 : /* True iff C is a conversion that binds a reference or a pointer to
12574 : an array of unknown bound. */
12575 :
12576 : static inline bool
12577 30700864 : conv_binds_to_array_of_unknown_bound (conversion *c)
12578 : {
12579 : /* ck_ref_bind won't have the reference stripped. */
12580 30700864 : tree type = non_reference (c->type);
12581 : /* ck_qual won't have the pointer stripped. */
12582 30700864 : type = strip_pointer_operator (type);
12583 30700864 : return (TREE_CODE (type) == ARRAY_TYPE
12584 30700864 : && TYPE_DOMAIN (type) == NULL_TREE);
12585 : }
12586 :
12587 : /* Compare two implicit conversion sequences according to the rules set out in
12588 : [over.ics.rank]. Return values:
12589 :
12590 : 1: ics1 is better than ics2
12591 : -1: ics2 is better than ics1
12592 : 0: ics1 and ics2 are indistinguishable */
12593 :
12594 : static int
12595 83546487 : compare_ics (conversion *ics1, conversion *ics2)
12596 : {
12597 83546487 : tree from_type1;
12598 83546487 : tree from_type2;
12599 83546487 : tree to_type1;
12600 83546487 : tree to_type2;
12601 83546487 : tree deref_from_type1 = NULL_TREE;
12602 83546487 : tree deref_from_type2 = NULL_TREE;
12603 83546487 : tree deref_to_type1 = NULL_TREE;
12604 83546487 : tree deref_to_type2 = NULL_TREE;
12605 83546487 : conversion_rank rank1, rank2;
12606 :
12607 : /* REF_BINDING is nonzero if the result of the conversion sequence
12608 : is a reference type. In that case REF_CONV is the reference
12609 : binding conversion. */
12610 83546487 : conversion *ref_conv1;
12611 83546487 : conversion *ref_conv2;
12612 :
12613 : /* Compare badness before stripping the reference conversion. */
12614 83546487 : if (ics1->bad_p > ics2->bad_p)
12615 : return -1;
12616 83546473 : else if (ics1->bad_p < ics2->bad_p)
12617 : return 1;
12618 :
12619 : /* Handle implicit object parameters. */
12620 83546446 : maybe_handle_implicit_object (&ics1);
12621 83546446 : maybe_handle_implicit_object (&ics2);
12622 :
12623 : /* Handle reference parameters. */
12624 83546446 : ref_conv1 = maybe_handle_ref_bind (&ics1);
12625 83546446 : ref_conv2 = maybe_handle_ref_bind (&ics2);
12626 :
12627 : /* List-initialization sequence L1 is a better conversion sequence than
12628 : list-initialization sequence L2 if L1 converts to
12629 : std::initializer_list<X> for some X and L2 does not. */
12630 83546446 : if (ics1->kind == ck_list && ics2->kind != ck_list)
12631 : return 1;
12632 83545635 : if (ics2->kind == ck_list && ics1->kind != ck_list)
12633 : return -1;
12634 :
12635 : /* [over.ics.rank]
12636 :
12637 : When comparing the basic forms of implicit conversion sequences (as
12638 : defined in _over.best.ics_)
12639 :
12640 : --a standard conversion sequence (_over.ics.scs_) is a better
12641 : conversion sequence than a user-defined conversion sequence
12642 : or an ellipsis conversion sequence, and
12643 :
12644 : --a user-defined conversion sequence (_over.ics.user_) is a
12645 : better conversion sequence than an ellipsis conversion sequence
12646 : (_over.ics.ellipsis_). */
12647 : /* Use BAD_CONVERSION_RANK because we already checked for a badness
12648 : mismatch. If both ICS are bad, we try to make a decision based on
12649 : what would have happened if they'd been good. This is not an
12650 : extension, we'll still give an error when we build up the call; this
12651 : just helps us give a more helpful error message. */
12652 83545370 : rank1 = BAD_CONVERSION_RANK (ics1);
12653 83545370 : rank2 = BAD_CONVERSION_RANK (ics2);
12654 :
12655 83545370 : if (rank1 > rank2)
12656 : return -1;
12657 72549716 : else if (rank1 < rank2)
12658 : return 1;
12659 :
12660 40414562 : if (ics1->ellipsis_p)
12661 : /* Both conversions are ellipsis conversions. */
12662 : return 0;
12663 :
12664 : /* User-defined conversion sequence U1 is a better conversion sequence
12665 : than another user-defined conversion sequence U2 if they contain the
12666 : same user-defined conversion operator or constructor and if the sec-
12667 : ond standard conversion sequence of U1 is better than the second
12668 : standard conversion sequence of U2. */
12669 :
12670 : /* Handle list-conversion with the same code even though it isn't always
12671 : ranked as a user-defined conversion and it doesn't have a second
12672 : standard conversion sequence; it will still have the desired effect.
12673 : Specifically, we need to do the reference binding comparison at the
12674 : end of this function. */
12675 :
12676 40414516 : if (ics1->user_conv_p || ics1->kind == ck_list
12677 39685450 : || ics1->kind == ck_aggr || ics2->kind == ck_aggr)
12678 : {
12679 729135 : conversion *t1 = strip_standard_conversion (ics1);
12680 729135 : conversion *t2 = strip_standard_conversion (ics2);
12681 :
12682 729135 : if (!t1 || !t2 || t1->kind != t2->kind)
12683 : return 0;
12684 729116 : else if (t1->kind == ck_user)
12685 : {
12686 713352 : tree f1 = t1->cand ? t1->cand->fn : t1->type;
12687 713352 : tree f2 = t2->cand ? t2->cand->fn : t2->type;
12688 713352 : if (f1 != f2)
12689 : return 0;
12690 : }
12691 : /* List-initialization sequence L1 is a better conversion sequence than
12692 : list-initialization sequence L2 if
12693 :
12694 : -- L1 and L2 convert to arrays of the same element type, and either
12695 : the number of elements n1 initialized by L1 is less than the number
12696 : of elements n2 initialized by L2, or n1=n2 and L2 converts to an array
12697 : of unknown bound and L1 does not. (Added in CWG 1307 and extended by
12698 : P0388R4.) */
12699 15764 : else if (t1->kind == ck_aggr
12700 14752 : && TREE_CODE (t1->type) == ARRAY_TYPE
12701 66 : && TREE_CODE (t2->type) == ARRAY_TYPE
12702 15830 : && same_type_p (TREE_TYPE (t1->type), TREE_TYPE (t2->type)))
12703 : {
12704 63 : tree n1 = nelts_initialized_by_list_init (t1);
12705 63 : tree n2 = nelts_initialized_by_list_init (t2);
12706 63 : if (tree_int_cst_lt (n1, n2))
12707 : return 1;
12708 24 : else if (tree_int_cst_lt (n2, n1))
12709 : return -1;
12710 : /* The n1 == n2 case. */
12711 24 : bool c1 = conv_binds_to_array_of_unknown_bound (t1);
12712 24 : bool c2 = conv_binds_to_array_of_unknown_bound (t2);
12713 24 : if (c1 && !c2)
12714 : return -1;
12715 6 : else if (!c1 && c2)
12716 : return 1;
12717 : else
12718 6 : return 0;
12719 : }
12720 : else
12721 : {
12722 : /* For ambiguous or aggregate conversions, use the target type as
12723 : a proxy for the conversion function. */
12724 15701 : if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
12725 : return 0;
12726 : }
12727 :
12728 : /* We can just fall through here, after setting up
12729 : FROM_TYPE1 and FROM_TYPE2. */
12730 726072 : from_type1 = t1->type;
12731 726072 : from_type2 = t2->type;
12732 726072 : }
12733 : else
12734 : {
12735 : conversion *t1;
12736 : conversion *t2;
12737 :
12738 : /* We're dealing with two standard conversion sequences.
12739 :
12740 : [over.ics.rank]
12741 :
12742 : Standard conversion sequence S1 is a better conversion
12743 : sequence than standard conversion sequence S2 if
12744 :
12745 : --S1 is a proper subsequence of S2 (comparing the conversion
12746 : sequences in the canonical form defined by _over.ics.scs_,
12747 : excluding any Lvalue Transformation; the identity
12748 : conversion sequence is considered to be a subsequence of
12749 : any non-identity conversion sequence */
12750 :
12751 : t1 = ics1;
12752 56977324 : while (t1->kind != ck_identity)
12753 17291943 : t1 = next_conversion (t1);
12754 39685381 : from_type1 = t1->type;
12755 :
12756 39685381 : t2 = ics2;
12757 56869522 : while (t2->kind != ck_identity)
12758 17184141 : t2 = next_conversion (t2);
12759 39685381 : from_type2 = t2->type;
12760 : }
12761 :
12762 : /* One sequence can only be a subsequence of the other if they start with
12763 : the same type. They can start with different types when comparing the
12764 : second standard conversion sequence in two user-defined conversion
12765 : sequences. */
12766 40411453 : if (same_type_p (from_type1, from_type2))
12767 : {
12768 39976319 : if (is_subseq (ics1, ics2))
12769 : return 1;
12770 39969936 : if (is_subseq (ics2, ics1))
12771 : return -1;
12772 : }
12773 :
12774 : /* [over.ics.rank]
12775 :
12776 : Or, if not that,
12777 :
12778 : --the rank of S1 is better than the rank of S2 (by the rules
12779 : defined below):
12780 :
12781 : Standard conversion sequences are ordered by their ranks: an Exact
12782 : Match is a better conversion than a Promotion, which is a better
12783 : conversion than a Conversion.
12784 :
12785 : Two conversion sequences with the same rank are indistinguishable
12786 : unless one of the following rules applies:
12787 :
12788 : --A conversion that does not a convert a pointer, pointer to member,
12789 : or std::nullptr_t to bool is better than one that does.
12790 :
12791 : The ICS_STD_RANK automatically handles the pointer-to-bool rule,
12792 : so that we do not have to check it explicitly. */
12793 40405012 : if (ics1->rank < ics2->rank)
12794 : return 1;
12795 40404932 : else if (ics2->rank < ics1->rank)
12796 : return -1;
12797 :
12798 40404932 : to_type1 = ics1->type;
12799 40404932 : to_type2 = ics2->type;
12800 :
12801 : /* A conversion from scalar arithmetic type to complex is worse than a
12802 : conversion between scalar arithmetic types. */
12803 40404932 : if (same_type_p (from_type1, from_type2)
12804 39969798 : && ARITHMETIC_TYPE_P (from_type1)
12805 8113384 : && ARITHMETIC_TYPE_P (to_type1)
12806 8113211 : && ARITHMETIC_TYPE_P (to_type2)
12807 40404932 : && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
12808 8113157 : != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
12809 : {
12810 982 : if (TREE_CODE (to_type1) == COMPLEX_TYPE)
12811 : return -1;
12812 : else
12813 979 : return 1;
12814 : }
12815 :
12816 40403950 : {
12817 : /* A conversion in either direction between floating-point type FP1 and
12818 : floating-point type FP2 is better than a conversion in the same
12819 : direction between FP1 and arithmetic type T3 if
12820 : - the floating-point conversion rank of FP1 is equal to the rank of
12821 : FP2, and
12822 : - T3 is not a floating-point type, or T3 is a floating-point type
12823 : whose rank is not equal to the rank of FP1, or the floating-point
12824 : conversion subrank of FP2 is greater than the subrank of T3. */
12825 40403950 : tree fp1 = from_type1;
12826 40403950 : tree fp2 = to_type1;
12827 40403950 : tree fp3 = from_type2;
12828 40403950 : tree t3 = to_type2;
12829 40403950 : int ret = 1;
12830 40403950 : if (TYPE_MAIN_VARIANT (fp2) == TYPE_MAIN_VARIANT (t3))
12831 : {
12832 35450560 : std::swap (fp1, fp2);
12833 35450560 : std::swap (fp3, t3);
12834 : }
12835 40403950 : if (TYPE_MAIN_VARIANT (fp1) == TYPE_MAIN_VARIANT (fp3)
12836 40403863 : && SCALAR_FLOAT_TYPE_P (fp1)
12837 : /* Only apply this rule if at least one of the 3 types is
12838 : extended floating-point type, otherwise keep them as
12839 : before for compatibility reasons with types like __float128.
12840 : float, double and long double alone have different conversion
12841 : ranks and so when just those 3 types are involved, this
12842 : rule doesn't trigger. */
12843 44779643 : && (extended_float_type_p (fp1)
12844 4296391 : || (SCALAR_FLOAT_TYPE_P (fp2) && extended_float_type_p (fp2))
12845 4205667 : || (SCALAR_FLOAT_TYPE_P (t3) && extended_float_type_p (t3))))
12846 : {
12847 170026 : if (TREE_CODE (fp2) != REAL_TYPE)
12848 : {
12849 33576 : ret = -ret;
12850 33576 : std::swap (fp2, t3);
12851 : }
12852 170026 : if (SCALAR_FLOAT_TYPE_P (fp2))
12853 : {
12854 : /* cp_compare_floating_point_conversion_ranks returns -1, 0 or 1
12855 : if the conversion rank is equal (-1 or 1 if the subrank is
12856 : different). */
12857 151597 : if (IN_RANGE (cp_compare_floating_point_conversion_ranks (fp1,
12858 : fp2),
12859 : -1, 1))
12860 : {
12861 : /* Conversion ranks of FP1 and FP2 are equal. */
12862 94388 : if (TREE_CODE (t3) != REAL_TYPE
12863 94388 : || !IN_RANGE (cp_compare_floating_point_conversion_ranks
12864 : (fp1, t3),
12865 : -1, 1))
12866 : /* FP1 <-> FP2 conversion is better. */
12867 83546487 : return ret;
12868 746 : int c = cp_compare_floating_point_conversion_ranks (fp2, t3);
12869 746 : gcc_assert (IN_RANGE (c, -1, 1));
12870 746 : if (c == 1)
12871 : /* Conversion subrank of FP2 is greater than subrank of T3.
12872 : FP1 <-> FP2 conversion is better. */
12873 : return ret;
12874 746 : else if (c == -1)
12875 : /* Conversion subrank of FP2 is less than subrank of T3.
12876 : FP1 <-> T3 conversion is better. */
12877 0 : return -ret;
12878 : }
12879 57209 : else if (SCALAR_FLOAT_TYPE_P (t3)
12880 57209 : && IN_RANGE (cp_compare_floating_point_conversion_ranks
12881 : (fp1, t3),
12882 : -1, 1))
12883 : /* Conversion ranks of FP1 and FP2 are not equal, conversion
12884 : ranks of FP1 and T3 are equal.
12885 : FP1 <-> T3 conversion is better. */
12886 10047 : return -ret;
12887 : }
12888 : }
12889 : }
12890 :
12891 40300261 : if (TYPE_PTR_P (from_type1)
12892 5252991 : && TYPE_PTR_P (from_type2)
12893 5252975 : && TYPE_PTR_P (to_type1)
12894 5252951 : && TYPE_PTR_P (to_type2))
12895 : {
12896 5252951 : deref_from_type1 = TREE_TYPE (from_type1);
12897 5252951 : deref_from_type2 = TREE_TYPE (from_type2);
12898 5252951 : deref_to_type1 = TREE_TYPE (to_type1);
12899 5252951 : deref_to_type2 = TREE_TYPE (to_type2);
12900 : }
12901 : /* The rules for pointers to members A::* are just like the rules
12902 : for pointers A*, except opposite: if B is derived from A then
12903 : A::* converts to B::*, not vice versa. For that reason, we
12904 : switch the from_ and to_ variables here. */
12905 59 : else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2)
12906 59 : && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2))
12907 35047310 : || (TYPE_PTRMEMFUNC_P (from_type1)
12908 437 : && TYPE_PTRMEMFUNC_P (from_type2)
12909 437 : && TYPE_PTRMEMFUNC_P (to_type1)
12910 437 : && TYPE_PTRMEMFUNC_P (to_type2)))
12911 : {
12912 496 : deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
12913 496 : deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
12914 496 : deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
12915 496 : deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
12916 : }
12917 :
12918 5253447 : if (deref_from_type1 != NULL_TREE
12919 5253447 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
12920 305775 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
12921 : {
12922 : /* This was one of the pointer or pointer-like conversions.
12923 :
12924 : [over.ics.rank]
12925 :
12926 : --If class B is derived directly or indirectly from class A,
12927 : conversion of B* to A* is better than conversion of B* to
12928 : void*, and conversion of A* to void* is better than
12929 : conversion of B* to void*. */
12930 305775 : if (VOID_TYPE_P (deref_to_type1)
12931 57 : && VOID_TYPE_P (deref_to_type2))
12932 : {
12933 14 : if (is_properly_derived_from (deref_from_type1,
12934 : deref_from_type2))
12935 : return -1;
12936 14 : else if (is_properly_derived_from (deref_from_type2,
12937 : deref_from_type1))
12938 : return 1;
12939 : }
12940 305761 : else if (VOID_TYPE_P (deref_to_type1)
12941 305718 : || VOID_TYPE_P (deref_to_type2))
12942 : {
12943 47 : if (same_type_p (deref_from_type1, deref_from_type2))
12944 : {
12945 47 : if (VOID_TYPE_P (deref_to_type2))
12946 : {
12947 4 : if (is_properly_derived_from (deref_from_type1,
12948 : deref_to_type1))
12949 : return 1;
12950 : }
12951 : /* We know that DEREF_TO_TYPE1 is `void' here. */
12952 43 : else if (is_properly_derived_from (deref_from_type1,
12953 : deref_to_type2))
12954 : return -1;
12955 : }
12956 : }
12957 305714 : else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
12958 305714 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
12959 : {
12960 : /* [over.ics.rank]
12961 :
12962 : --If class B is derived directly or indirectly from class A
12963 : and class C is derived directly or indirectly from B,
12964 :
12965 : --conversion of C* to B* is better than conversion of C* to
12966 : A*,
12967 :
12968 : --conversion of B* to A* is better than conversion of C* to
12969 : A* */
12970 305714 : if (same_type_p (deref_from_type1, deref_from_type2))
12971 : {
12972 305708 : if (is_properly_derived_from (deref_to_type1,
12973 : deref_to_type2))
12974 : return 1;
12975 305708 : else if (is_properly_derived_from (deref_to_type2,
12976 : deref_to_type1))
12977 : return -1;
12978 : }
12979 6 : else if (same_type_p (deref_to_type1, deref_to_type2))
12980 : {
12981 6 : if (is_properly_derived_from (deref_from_type2,
12982 : deref_from_type1))
12983 : return 1;
12984 0 : else if (is_properly_derived_from (deref_from_type1,
12985 : deref_from_type2))
12986 : return -1;
12987 : }
12988 : }
12989 : }
12990 79988972 : else if (CLASS_TYPE_P (non_reference (from_type1))
12991 64599505 : && same_type_p (from_type1, from_type2))
12992 : {
12993 24232677 : tree from = non_reference (from_type1);
12994 :
12995 : /* [over.ics.rank]
12996 :
12997 : --binding of an expression of type C to a reference of type
12998 : B& is better than binding an expression of type C to a
12999 : reference of type A&
13000 :
13001 : --conversion of C to B is better than conversion of C to A, */
13002 24232677 : if (is_properly_derived_from (from, to_type1)
13003 24232677 : && is_properly_derived_from (from, to_type2))
13004 : {
13005 968349 : if (is_properly_derived_from (to_type1, to_type2))
13006 : return 1;
13007 964288 : else if (is_properly_derived_from (to_type2, to_type1))
13008 : return -1;
13009 : }
13010 : }
13011 31523618 : else if (CLASS_TYPE_P (non_reference (to_type1))
13012 16134151 : && same_type_p (to_type1, to_type2))
13013 : {
13014 7 : tree to = non_reference (to_type1);
13015 :
13016 : /* [over.ics.rank]
13017 :
13018 : --binding of an expression of type B to a reference of type
13019 : A& is better than binding an expression of type C to a
13020 : reference of type A&,
13021 :
13022 : --conversion of B to A is better than conversion of C to A */
13023 7 : if (is_properly_derived_from (from_type1, to)
13024 7 : && is_properly_derived_from (from_type2, to))
13025 : {
13026 3 : if (is_properly_derived_from (from_type2, from_type1))
13027 : return 1;
13028 3 : else if (is_properly_derived_from (from_type1, from_type2))
13029 : return -1;
13030 : }
13031 : }
13032 :
13033 : /* [over.ics.rank]
13034 :
13035 : --S1 and S2 differ only in their qualification conversion and yield
13036 : similar types T1 and T2 (_conv.qual_), respectively, and the cv-
13037 : qualification signature of type T1 is a proper subset of the cv-
13038 : qualification signature of type T2 */
13039 40210448 : if (ics1->kind == ck_qual
13040 355 : && ics2->kind == ck_qual
13041 40210803 : && same_type_p (from_type1, from_type2))
13042 : {
13043 355 : int result = comp_cv_qual_signature (to_type1, to_type2);
13044 355 : if (result != 0)
13045 : return result;
13046 : }
13047 :
13048 : /* [over.ics.rank]
13049 :
13050 : --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
13051 : to an implicit object parameter of a non-static member function
13052 : declared without a ref-qualifier, and either S1 binds an lvalue
13053 : reference to an lvalue and S2 binds an rvalue reference or S1 binds an
13054 : rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x
13055 : draft standard, 13.3.3.2)
13056 :
13057 : --S1 and S2 are reference bindings (_dcl.init.ref_), and the
13058 : types to which the references refer are the same type except for
13059 : top-level cv-qualifiers, and the type to which the reference
13060 : initialized by S2 refers is more cv-qualified than the type to
13061 : which the reference initialized by S1 refers.
13062 :
13063 : DR 1328 [over.match.best]: the context is an initialization by
13064 : conversion function for direct reference binding (13.3.1.6) of a
13065 : reference to function type, the return type of F1 is the same kind of
13066 : reference (i.e. lvalue or rvalue) as the reference being initialized,
13067 : and the return type of F2 is not. */
13068 :
13069 40210362 : if (ref_conv1 && ref_conv2)
13070 : {
13071 18509350 : if (!ref_conv1->this_p && !ref_conv2->this_p
13072 18247672 : && (ref_conv1->rvaluedness_matches_p
13073 18247672 : != ref_conv2->rvaluedness_matches_p)
13074 34820446 : && (same_type_p (ref_conv1->type, ref_conv2->type)
13075 9734981 : || (TYPE_REF_IS_RVALUE (ref_conv1->type)
13076 9734981 : != TYPE_REF_IS_RVALUE (ref_conv2->type))))
13077 : {
13078 9734709 : if (ref_conv1->bad_p
13079 9734709 : && !same_type_p (TREE_TYPE (ref_conv1->type),
13080 : TREE_TYPE (ref_conv2->type)))
13081 : /* Don't prefer a bad conversion that drops cv-quals to a bad
13082 : conversion with the wrong rvalueness. */
13083 : return 0;
13084 9733246 : return (ref_conv1->rvaluedness_matches_p
13085 9733246 : - ref_conv2->rvaluedness_matches_p);
13086 : }
13087 :
13088 15350753 : if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
13089 : {
13090 : /* Per P0388R4:
13091 :
13092 : void f (int(&)[]), // (1)
13093 : f (int(&)[1]), // (2)
13094 : f (int*); // (3)
13095 :
13096 : (2) is better than (1), but (3) should be equal to (1) and to
13097 : (2). For that reason we don't use ck_qual for (1) which would
13098 : give it the cr_exact rank while (3) remains ck_identity.
13099 : Therefore we compare (1) and (2) here. For (1) we'll have
13100 :
13101 : ck_ref_bind <- ck_identity
13102 : int[] & int[1]
13103 :
13104 : so to handle this we must look at ref_conv. */
13105 15350170 : bool c1 = conv_binds_to_array_of_unknown_bound (ref_conv1);
13106 15350170 : bool c2 = conv_binds_to_array_of_unknown_bound (ref_conv2);
13107 15350170 : if (c1 && !c2)
13108 : return -1;
13109 15350164 : else if (!c1 && c2)
13110 : return 1;
13111 :
13112 15350164 : int q1 = cp_type_quals (TREE_TYPE (ref_conv1->type));
13113 15350164 : int q2 = cp_type_quals (TREE_TYPE (ref_conv2->type));
13114 15350164 : if (ref_conv1->bad_p)
13115 : {
13116 : /* Prefer the one that drops fewer cv-quals. */
13117 1606 : tree ftype = next_conversion (ref_conv1)->type;
13118 1606 : int fquals = cp_type_quals (ftype);
13119 1606 : q1 ^= fquals;
13120 1606 : q2 ^= fquals;
13121 : }
13122 15350164 : return comp_cv_qualification (q2, q1);
13123 : }
13124 : }
13125 :
13126 : /* [over.ics.rank]
13127 :
13128 : Per CWG 1601:
13129 : -- A conversion that promotes an enumeration whose underlying type
13130 : is fixed to its underlying type is better than one that promotes to
13131 : the promoted underlying type, if the two are different. */
13132 15125483 : if (ics1->rank == cr_promotion
13133 146 : && ics2->rank == cr_promotion
13134 146 : && UNSCOPED_ENUM_P (from_type1)
13135 27 : && ENUM_FIXED_UNDERLYING_TYPE_P (from_type1)
13136 15125507 : && same_type_p (from_type1, from_type2))
13137 : {
13138 24 : tree utype = ENUM_UNDERLYING_TYPE (from_type1);
13139 24 : tree prom = type_promotes_to (from_type1);
13140 24 : if (!same_type_p (utype, prom))
13141 : {
13142 12 : if (same_type_p (to_type1, utype)
13143 12 : && same_type_p (to_type2, prom))
13144 : return 1;
13145 6 : else if (same_type_p (to_type2, utype)
13146 6 : && same_type_p (to_type1, prom))
13147 6 : return -1;
13148 : }
13149 : }
13150 :
13151 : /* Neither conversion sequence is better than the other. */
13152 : return 0;
13153 : }
13154 :
13155 : /* The source type for this standard conversion sequence. */
13156 :
13157 : static tree
13158 9 : source_type (conversion *t)
13159 : {
13160 9 : return strip_standard_conversion (t)->type;
13161 : }
13162 :
13163 : /* Note a warning about preferring WINNER to LOSER. We do this by storing
13164 : a pointer to LOSER and re-running joust to produce the warning if WINNER
13165 : is actually used. */
13166 :
13167 : static void
13168 510 : add_warning (struct z_candidate *winner, struct z_candidate *loser)
13169 : {
13170 510 : candidate_warning *cw = (candidate_warning *)
13171 0 : conversion_obstack_alloc (sizeof (candidate_warning));
13172 510 : cw->loser = loser;
13173 510 : cw->next = winner->warnings;
13174 510 : winner->warnings = cw;
13175 0 : }
13176 :
13177 : /* CAND is a constructor candidate in joust in C++17 and up. If it copies a
13178 : prvalue returned from a conversion function, return true. Otherwise, return
13179 : false. */
13180 :
13181 : static bool
13182 883846 : joust_maybe_elide_copy (z_candidate *cand)
13183 : {
13184 883846 : tree fn = cand->fn;
13185 2219025 : if (!DECL_COPY_CONSTRUCTOR_P (fn) && !DECL_MOVE_CONSTRUCTOR_P (fn))
13186 : return false;
13187 432708 : conversion *conv = cand->convs[0];
13188 432708 : if (conv->kind == ck_ambig)
13189 : return false;
13190 432706 : gcc_checking_assert (conv->kind == ck_ref_bind);
13191 432706 : conv = next_conversion (conv);
13192 432706 : if (conv->kind == ck_user && !TYPE_REF_P (conv->type))
13193 : {
13194 342 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
13195 : (conv->type, DECL_CONTEXT (fn)));
13196 342 : z_candidate *uc = conv->cand;
13197 342 : if (DECL_CONV_FN_P (uc->fn))
13198 233 : return true;
13199 : }
13200 : return false;
13201 : }
13202 :
13203 : /* Return the class that CAND's implicit object parameter refers to. */
13204 :
13205 : static tree
13206 295338 : class_of_implicit_object (z_candidate *cand)
13207 : {
13208 295338 : if (!DECL_IOBJ_MEMBER_FUNCTION_P (cand->fn))
13209 : return NULL_TREE;
13210 :
13211 : /* "For conversion functions that are implicit object member functions,
13212 : the function is considered to be a member of the class of the implied
13213 : object argument for the purpose of defining the type of the implicit
13214 : object parameter." */
13215 295338 : if (DECL_CONV_FN_P (cand->fn))
13216 0 : return TYPE_MAIN_VARIANT (TREE_TYPE (cand->first_arg));
13217 :
13218 : /* "For non-conversion functions that are implicit object member
13219 : functions nominated by a using-declaration in a derived class, the
13220 : function is considered to be a member of the derived class for the
13221 : purpose of defining the type of the implicit object parameter."
13222 :
13223 : That derived class is reflected in the conversion_path binfo. */
13224 295338 : return BINFO_TYPE (cand->conversion_path);
13225 : }
13226 :
13227 : /* Return whether the first parameter of C1 matches the second parameter
13228 : of C2. */
13229 :
13230 : static bool
13231 565238 : reversed_match (z_candidate *c1, z_candidate *c2)
13232 : {
13233 565238 : tree fn1 = c1->fn;
13234 565238 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (c2->fn));
13235 565238 : tree parm2 = TREE_VALUE (TREE_CHAIN (parms2));
13236 565238 : if (DECL_IOBJ_MEMBER_FUNCTION_P (fn1))
13237 : {
13238 295338 : tree ctx = class_of_implicit_object (c1);
13239 295338 : return iobj_parm_corresponds_to (fn1, parm2, ctx);
13240 : }
13241 : else
13242 : {
13243 269900 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn1));
13244 269900 : tree parm1 = TREE_VALUE (parms1);
13245 269900 : return same_type_p (parm1, parm2);
13246 : }
13247 : }
13248 :
13249 : /* True if the defining declarations of the two candidates have equivalent
13250 : parameters. MATCH_KIND controls whether we're trying to compare the
13251 : original declarations (for a warning) or the actual candidates. */
13252 :
13253 : enum class pmatch { original, current };
13254 :
13255 : static bool
13256 4973402 : cand_parms_match (z_candidate *c1, z_candidate *c2, pmatch match_kind)
13257 : {
13258 4973402 : tree fn1 = c1->fn;
13259 4973402 : tree fn2 = c2->fn;
13260 4973402 : bool reversed = (match_kind == pmatch::current
13261 4973402 : && c1->reversed () != c2->reversed ());
13262 4973402 : if (fn1 == fn2 && !reversed)
13263 : return true;
13264 4973077 : if (identifier_p (fn1) || identifier_p (fn2))
13265 : return false;
13266 4973072 : if (match_kind == pmatch::original)
13267 : {
13268 : /* We don't look at c1->template_decl because that's only set for
13269 : primary templates, not e.g. non-template member functions of
13270 : class templates. */
13271 22 : tree t1 = most_general_template (fn1);
13272 22 : tree t2 = most_general_template (fn2);
13273 22 : if (t1 || t2)
13274 : {
13275 15 : if (!t1 || !t2)
13276 : return false;
13277 15 : if (t1 == t2)
13278 : return true;
13279 3 : fn1 = DECL_TEMPLATE_RESULT (t1);
13280 3 : fn2 = DECL_TEMPLATE_RESULT (t2);
13281 : }
13282 : }
13283 :
13284 4973060 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn1));
13285 4973060 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (fn2));
13286 :
13287 9726249 : if (DECL_FUNCTION_MEMBER_P (fn1)
13288 4973684 : && DECL_FUNCTION_MEMBER_P (fn2))
13289 : {
13290 220481 : tree base1 = DECL_CONTEXT (strip_inheriting_ctors (fn1));
13291 220481 : tree base2 = DECL_CONTEXT (strip_inheriting_ctors (fn2));
13292 220481 : if (base1 != base2)
13293 148151 : return false;
13294 :
13295 220304 : if (reversed)
13296 147974 : return (reversed_match (c1, c2)
13297 147974 : && reversed_match (c2, c1));
13298 :
13299 : /* Use object_parms_correspond to simplify comparing iobj/xobj/static
13300 : member functions. */
13301 72330 : if (!object_parms_correspond (fn1, fn2, base1))
13302 : return false;
13303 :
13304 : /* We just compared the object parameters, if they don't correspond
13305 : we already returned false. */
13306 216990 : auto skip_parms = [] (tree fn, tree parms)
13307 : {
13308 144660 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
13309 1020 : return TREE_CHAIN (parms);
13310 : else
13311 143640 : return skip_artificial_parms_for (fn, parms);
13312 : };
13313 72330 : parms1 = skip_parms (fn1, parms1);
13314 72330 : parms2 = skip_parms (fn2, parms2);
13315 : }
13316 4752579 : else if (reversed)
13317 134915 : return (reversed_match (c1, c2)
13318 134915 : && reversed_match (c2, c1));
13319 4689994 : return compparms (parms1, parms2);
13320 : }
13321 :
13322 : /* True iff FN is a copy or move constructor or assignment operator. */
13323 :
13324 : static bool
13325 20680424 : sfk_copy_or_move (tree fn)
13326 : {
13327 20680424 : if (TREE_CODE (fn) != FUNCTION_DECL)
13328 : return false;
13329 20680334 : special_function_kind sfk = special_function_p (fn);
13330 20680334 : return sfk >= sfk_copy_constructor && sfk <= sfk_move_assignment;
13331 : }
13332 :
13333 : /* Compare two candidates for overloading as described in
13334 : [over.match.best]. Return values:
13335 :
13336 : 1: cand1 is better than cand2
13337 : -1: cand2 is better than cand1
13338 : 0: cand1 and cand2 are indistinguishable */
13339 :
13340 : static int
13341 78271423 : joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
13342 : tsubst_flags_t complain)
13343 : {
13344 78271423 : int winner = 0;
13345 78271423 : int off1 = 0, off2 = 0;
13346 78271423 : size_t i;
13347 78271423 : size_t len;
13348 :
13349 : /* Candidates that involve bad conversions are always worse than those
13350 : that don't. */
13351 78271423 : if (cand1->viable > cand2->viable)
13352 : return 1;
13353 62810207 : if (cand1->viable < cand2->viable)
13354 : return -1;
13355 :
13356 : /* If we have two pseudo-candidates for conversions to the same type,
13357 : or two candidates for the same function, arbitrarily pick one. */
13358 62810207 : if (cand1->fn == cand2->fn
13359 1905472 : && cand1->reversed () == cand2->reversed ()
13360 63645864 : && (IS_TYPE_OR_DECL_P (cand1->fn)))
13361 : return 1;
13362 :
13363 : /* Prefer a non-deleted function over an implicitly deleted move
13364 : constructor or assignment operator. This differs slightly from the
13365 : wording for issue 1402 (which says the move op is ignored by overload
13366 : resolution), but this way produces better error messages. */
13367 62810207 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13368 58439679 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13369 121240090 : && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn))
13370 : {
13371 2526125 : if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn)
13372 2107107 : && move_fn_p (cand1->fn))
13373 : return -1;
13374 3782396 : if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn)
13375 3052448 : && move_fn_p (cand2->fn))
13376 : return 1;
13377 : }
13378 :
13379 : /* a viable function F1
13380 : is defined to be a better function than another viable function F2 if
13381 : for all arguments i, ICSi(F1) is not a worse conversion sequence than
13382 : ICSi(F2), and then */
13383 :
13384 : /* for some argument j, ICSj(F1) is a better conversion sequence than
13385 : ICSj(F2) */
13386 :
13387 : /* For comparing static and non-static member functions, we ignore
13388 : the implicit object parameter of the non-static function. The
13389 : standard says to pretend that the static function has an object
13390 : parm, but that won't work with operator overloading. */
13391 62795501 : len = cand1->num_convs;
13392 62795501 : if (len != cand2->num_convs)
13393 : {
13394 778 : int static_1 = (TREE_CODE (cand1->fn) == FUNCTION_DECL
13395 778 : && DECL_STATIC_FUNCTION_P (cand1->fn));
13396 778 : int static_2 = (TREE_CODE (cand2->fn) == FUNCTION_DECL
13397 778 : && DECL_STATIC_FUNCTION_P (cand2->fn));
13398 :
13399 778 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13400 193 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13401 193 : && DECL_CONSTRUCTOR_P (cand1->fn)
13402 784 : && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
13403 : /* We're comparing a near-match list constructor and a near-match
13404 : non-list constructor. Just treat them as unordered. */
13405 : return 0;
13406 :
13407 772 : gcc_assert (static_1 != static_2);
13408 :
13409 772 : if (static_1)
13410 : {
13411 : /* C++23 [over.best.ics.general] says:
13412 : When the parameter is the implicit object parameter of a static
13413 : member function, the implicit conversion sequence is a standard
13414 : conversion sequence that is neither better nor worse than any
13415 : other standard conversion sequence. */
13416 72 : if (CONVERSION_RANK (cand2->convs[0]) >= cr_user)
13417 0 : winner = 1;
13418 : off2 = 1;
13419 : }
13420 : else
13421 : {
13422 700 : if (CONVERSION_RANK (cand1->convs[0]) >= cr_user)
13423 585 : winner = -1;
13424 700 : off1 = 1;
13425 700 : --len;
13426 : }
13427 : }
13428 :
13429 146288758 : for (i = 0; i < len; ++i)
13430 : {
13431 83494612 : conversion *t1 = cand1->convs[i + off1];
13432 83494612 : conversion *t2 = cand2->convs[i + off2];
13433 83494612 : int comp = compare_ics (t1, t2);
13434 :
13435 83494612 : if (comp != 0)
13436 : {
13437 56524282 : if ((complain & tf_warning)
13438 46650392 : && warn_sign_promo
13439 82 : && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
13440 : == cr_std + cr_promotion)
13441 50 : && t1->kind == ck_std
13442 31 : && t2->kind == ck_std
13443 31 : && TREE_CODE (t1->type) == INTEGER_TYPE
13444 31 : && TREE_CODE (t2->type) == INTEGER_TYPE
13445 31 : && (TYPE_PRECISION (t1->type)
13446 31 : == TYPE_PRECISION (t2->type))
13447 56524313 : && (TYPE_UNSIGNED (next_conversion (t1)->type)
13448 0 : || (TREE_CODE (next_conversion (t1)->type)
13449 : == ENUMERAL_TYPE)))
13450 : {
13451 31 : tree type = next_conversion (t1)->type;
13452 31 : tree type1, type2;
13453 31 : struct z_candidate *w, *l;
13454 31 : if (comp > 0)
13455 : type1 = t1->type, type2 = t2->type,
13456 : w = cand1, l = cand2;
13457 : else
13458 8 : type1 = t2->type, type2 = t1->type,
13459 8 : w = cand2, l = cand1;
13460 :
13461 31 : if (warn)
13462 : {
13463 9 : warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
13464 : type, type1, type2);
13465 9 : warning (OPT_Wsign_promo, " in call to %qD", w->fn);
13466 : }
13467 : else
13468 22 : add_warning (w, l);
13469 : }
13470 :
13471 56524282 : if (winner && comp != winner)
13472 : {
13473 : /* Ambiguity between normal and reversed comparison operators
13474 : with the same parameter types. P2468 decided not to go with
13475 : this approach to resolving the ambiguity, so pedwarn. */
13476 1349 : if ((complain & tf_warning_or_error)
13477 767 : && (cand1->reversed () != cand2->reversed ())
13478 1701 : && cand_parms_match (cand1, cand2, pmatch::original))
13479 : {
13480 337 : struct z_candidate *w, *l;
13481 337 : if (cand2->reversed ())
13482 : winner = 1, w = cand1, l = cand2;
13483 : else
13484 315 : winner = -1, w = cand2, l = cand1;
13485 337 : if (warn)
13486 : {
13487 22 : auto_diagnostic_group d;
13488 22 : if (pedwarn (input_location, 0,
13489 : "C++20 says that these are ambiguous, "
13490 : "even though the second is reversed:"))
13491 : {
13492 22 : print_z_candidate (input_location,
13493 : N_("candidate 1:"), w);
13494 22 : print_z_candidate (input_location,
13495 : N_("candidate 2:"), l);
13496 22 : if (w->fn == l->fn
13497 17 : && DECL_IOBJ_MEMBER_FUNCTION_P (w->fn)
13498 37 : && (type_memfn_quals (TREE_TYPE (w->fn))
13499 15 : & TYPE_QUAL_CONST) == 0)
13500 : {
13501 : /* Suggest adding const to
13502 : struct A { bool operator==(const A&); }; */
13503 12 : tree parmtype
13504 12 : = FUNCTION_FIRST_USER_PARMTYPE (w->fn);
13505 12 : parmtype = TREE_VALUE (parmtype);
13506 12 : if (TYPE_REF_P (parmtype)
13507 12 : && TYPE_READONLY (TREE_TYPE (parmtype))
13508 24 : && (same_type_ignoring_top_level_qualifiers_p
13509 12 : (TREE_TYPE (parmtype),
13510 12 : DECL_CONTEXT (w->fn))))
13511 12 : inform (DECL_SOURCE_LOCATION (w->fn),
13512 : "try making the operator a %<const%> "
13513 : "member function");
13514 : }
13515 : }
13516 22 : }
13517 : else
13518 315 : add_warning (w, l);
13519 : return winner;
13520 : }
13521 :
13522 1012 : winner = 0;
13523 1012 : goto tweak;
13524 : }
13525 : winner = comp;
13526 : }
13527 : }
13528 :
13529 : /* warn about confusing overload resolution for user-defined conversions,
13530 : either between a constructor and a conversion op, or between two
13531 : conversion ops. */
13532 62794146 : if ((complain & tf_warning)
13533 : /* In C++17, the constructor might have been elided, which means that
13534 : an originally null ->second_conv could become non-null. */
13535 51206632 : && winner && warn_conversion && cand1->second_conv && cand2->second_conv
13536 54 : && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
13537 62794173 : && winner != compare_ics (cand1->second_conv, cand2->second_conv))
13538 : {
13539 27 : struct z_candidate *w, *l;
13540 27 : bool give_warning = false;
13541 :
13542 27 : if (winner == 1)
13543 : w = cand1, l = cand2;
13544 : else
13545 6 : w = cand2, l = cand1;
13546 :
13547 : /* We don't want to complain about `X::operator T1 ()'
13548 : beating `X::operator T2 () const', when T2 is a no less
13549 : cv-qualified version of T1. */
13550 27 : if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
13551 39 : && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
13552 : {
13553 6 : tree t = TREE_TYPE (TREE_TYPE (l->fn));
13554 6 : tree f = TREE_TYPE (TREE_TYPE (w->fn));
13555 :
13556 6 : if (TREE_CODE (t) == TREE_CODE (f) && INDIRECT_TYPE_P (t))
13557 : {
13558 6 : t = TREE_TYPE (t);
13559 6 : f = TREE_TYPE (f);
13560 : }
13561 6 : if (!comp_ptr_ttypes (t, f))
13562 : give_warning = true;
13563 : }
13564 : else
13565 : give_warning = true;
13566 :
13567 : if (!give_warning)
13568 : /*NOP*/;
13569 21 : else if (warn)
13570 : {
13571 9 : tree source = source_type (w->convs[0]);
13572 9 : if (INDIRECT_TYPE_P (source))
13573 6 : source = TREE_TYPE (source);
13574 9 : auto_diagnostic_group d;
13575 9 : if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
13576 18 : && warning (OPT_Wconversion, " for conversion from %qH to %qI",
13577 9 : source, w->second_conv->type))
13578 : {
13579 9 : inform (input_location, " because conversion sequence "
13580 : "for the argument is better");
13581 : }
13582 9 : }
13583 : else
13584 12 : add_warning (w, l);
13585 : }
13586 :
13587 62794146 : if (winner)
13588 : return winner;
13589 :
13590 : /* DR 495 moved this tiebreaker above the template ones. */
13591 : /* or, if not that,
13592 : the context is an initialization by user-defined conversion (see
13593 : _dcl.init_ and _over.match.user_) and the standard conversion
13594 : sequence from the return type of F1 to the destination type (i.e.,
13595 : the type of the entity being initialized) is a better conversion
13596 : sequence than the standard conversion sequence from the return type
13597 : of F2 to the destination type. */
13598 :
13599 10340364 : if (cand1->second_conv)
13600 : {
13601 51848 : winner = compare_ics (cand1->second_conv, cand2->second_conv);
13602 51848 : if (winner)
13603 : return winner;
13604 : }
13605 :
13606 : /* CWG2735 (PR109247): A copy/move ctor/op= for which its operand uses an
13607 : explicit conversion (due to list-initialization) is worse. */
13608 10340212 : {
13609 10340212 : z_candidate *sp = nullptr;
13610 10340212 : if (sfk_copy_or_move (cand1->fn))
13611 682 : sp = cand1;
13612 10340212 : if (sfk_copy_or_move (cand2->fn))
13613 892270 : sp = sp ? nullptr : cand2;
13614 10340159 : if (sp)
13615 : {
13616 1785692 : conversion *conv = sp->convs[!DECL_CONSTRUCTOR_P (sp->fn)];
13617 892846 : if (conv->user_conv_p)
13618 2262 : for (; conv; conv = next_conversion (conv))
13619 1912 : if (conv->kind == ck_user
13620 781 : && DECL_P (conv->cand->fn)
13621 2693 : && DECL_NONCONVERTING_P (conv->cand->fn))
13622 431 : return (sp == cand1) ? -1 : 1;
13623 : }
13624 : }
13625 :
13626 : /* DR2327: C++17 copy elision in [over.match.ctor] (direct-init) context.
13627 : The standard currently says that only constructors are candidates, but if
13628 : one copies a prvalue returned by a conversion function we prefer that.
13629 :
13630 : Clang does something similar, as discussed at
13631 : http://lists.isocpp.org/core/2017/10/3166.php
13632 : http://lists.isocpp.org/core/2019/03/5721.php */
13633 6746255 : if (len == 1 && cxx_dialect >= cxx17
13634 6731640 : && DECL_P (cand1->fn)
13635 6731640 : && DECL_COMPLETE_CONSTRUCTOR_P (cand1->fn)
13636 11155530 : && !(cand1->flags & LOOKUP_ONLYCONVERTING))
13637 : {
13638 441923 : bool elided1 = joust_maybe_elide_copy (cand1);
13639 441923 : bool elided2 = joust_maybe_elide_copy (cand2);
13640 441923 : winner = elided1 - elided2;
13641 441923 : if (winner)
13642 : return winner;
13643 : }
13644 :
13645 : /* or, if not that,
13646 : F1 is a non-template function and F2 is a template function
13647 : specialization. */
13648 :
13649 10339548 : if (!cand1->template_decl && cand2->template_decl)
13650 : return 1;
13651 10277762 : else if (cand1->template_decl && !cand2->template_decl)
13652 : return -1;
13653 :
13654 : /* or, if not that,
13655 : F1 and F2 are template functions and the function template for F1 is
13656 : more specialized than the template for F2 according to the partial
13657 : ordering rules. */
13658 :
13659 9119327 : if (cand1->template_decl && cand2->template_decl)
13660 : {
13661 4134101 : winner = more_specialized_fn
13662 4134101 : (TI_TEMPLATE (cand1->template_decl),
13663 4134101 : TI_TEMPLATE (cand2->template_decl),
13664 : /* [temp.func.order]: The presence of unused ellipsis and default
13665 : arguments has no effect on the partial ordering of function
13666 : templates. add_function_candidate() will not have
13667 : counted the "this" argument for constructors. */
13668 8268202 : cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
13669 4134101 : if (winner)
13670 : return winner;
13671 : }
13672 :
13673 : /* F1 and F2 are non-template functions and
13674 : - they have the same non-object-parameter-type-lists ([dcl.fct]), and
13675 : - if they are member functions, both are direct members of the same
13676 : class, and
13677 : - if both are non-static member functions, they have the same types for
13678 : their object parameters, and
13679 : - F1 is more constrained than F2 according to the partial ordering of
13680 : constraints described in [temp.constr.order]. */
13681 6094736 : if (flag_concepts && DECL_P (cand1->fn) && DECL_P (cand2->fn)
13682 6094688 : && !cand1->template_decl && !cand2->template_decl
13683 11080325 : && cand_parms_match (cand1, cand2, pmatch::current))
13684 : {
13685 407113 : winner = more_constrained (cand1->fn, cand2->fn);
13686 407113 : if (winner)
13687 : return winner;
13688 : }
13689 :
13690 : /* F2 is a rewritten candidate (12.4.1.2) and F1 is not, or F1 and F2 are
13691 : rewritten candidates, and F2 is a synthesized candidate with reversed
13692 : order of parameters and F1 is not. */
13693 6099195 : if (cand1->rewritten ())
13694 : {
13695 1269842 : if (!cand2->rewritten ())
13696 : return -1;
13697 814120 : if (!cand1->reversed () && cand2->reversed ())
13698 : return 1;
13699 814120 : if (cand1->reversed () && !cand2->reversed ())
13700 : return -1;
13701 : }
13702 4829353 : else if (cand2->rewritten ())
13703 : return 1;
13704 :
13705 4760732 : if (deduction_guide_p (cand1->fn))
13706 : {
13707 4032 : gcc_assert (deduction_guide_p (cand2->fn));
13708 :
13709 : /* F1 and F2 are generated from class template argument deduction for a
13710 : class D, and F2 is generated from inheriting constructors from a base
13711 : class of D while F1 is not, and for each explicit function argument,
13712 : the corresponding parameters of F1 and F2 are either both ellipses or
13713 : have the same type. */
13714 4032 : bool inherited1 = inherited_guide_p (cand1->fn);
13715 4032 : bool inherited2 = inherited_guide_p (cand2->fn);
13716 4032 : if (int diff = inherited2 - inherited1)
13717 : {
13718 68 : for (i = 0; i < len; ++i)
13719 : {
13720 19 : conversion *t1 = cand1->convs[i + off1];
13721 19 : conversion *t2 = cand2->convs[i + off2];
13722 : /* ??? It seems the ellipses part of this tiebreaker isn't
13723 : needed since a mismatch should have broken the tie earlier
13724 : during ICS comparison. */
13725 19 : gcc_checking_assert (t1->ellipsis_p == t2->ellipsis_p);
13726 19 : if (!same_type_p (t1->type, t2->type))
13727 : break;
13728 : }
13729 51 : if (i == len)
13730 : return diff;
13731 : }
13732 :
13733 : /* F1 is generated from a deduction-guide (13.3.1.8) and F2 is not */
13734 : /* We distinguish between candidates from an explicit deduction guide and
13735 : candidates built from a constructor based on DECL_ARTIFICIAL. */
13736 3983 : int art1 = DECL_ARTIFICIAL (cand1->fn);
13737 3983 : int art2 = DECL_ARTIFICIAL (cand2->fn);
13738 3983 : if (art1 != art2)
13739 1402 : return art2 - art1;
13740 :
13741 2581 : if (art1)
13742 : {
13743 : /* Prefer the special copy guide over a declared copy/move
13744 : constructor. */
13745 2578 : if (copy_guide_p (cand1->fn))
13746 : return 1;
13747 86 : if (copy_guide_p (cand2->fn))
13748 : return -1;
13749 :
13750 : /* Prefer a candidate generated from a non-template constructor. */
13751 28 : int tg1 = template_guide_p (cand1->fn);
13752 28 : int tg2 = template_guide_p (cand2->fn);
13753 28 : if (tg1 != tg2)
13754 6 : return tg2 - tg1;
13755 : }
13756 : }
13757 :
13758 : /* F1 is a constructor for a class D, F2 is a constructor for a base class B
13759 : of D, and for all arguments the corresponding parameters of F1 and F2 have
13760 : the same type (CWG 2273/2277). */
13761 14270020 : if (DECL_INHERITED_CTOR (cand1->fn) || DECL_INHERITED_CTOR (cand2->fn))
13762 : {
13763 87 : tree base1 = DECL_CONTEXT (strip_inheriting_ctors (cand1->fn));
13764 87 : tree base2 = DECL_CONTEXT (strip_inheriting_ctors (cand2->fn));
13765 :
13766 87 : bool used1 = false;
13767 87 : bool used2 = false;
13768 87 : if (base1 == base2)
13769 : /* No difference. */;
13770 87 : else if (DERIVED_FROM_P (base1, base2))
13771 : used1 = true;
13772 18 : else if (DERIVED_FROM_P (base2, base1))
13773 : used2 = true;
13774 :
13775 78 : if (int diff = used2 - used1)
13776 : {
13777 105 : for (i = 0; i < len; ++i)
13778 : {
13779 36 : conversion *t1 = cand1->convs[i + off1];
13780 36 : conversion *t2 = cand2->convs[i + off2];
13781 36 : if (!same_type_p (t1->type, t2->type))
13782 : break;
13783 : }
13784 78 : if (i == len)
13785 : return diff;
13786 : }
13787 : }
13788 :
13789 : /* Check whether we can discard a builtin candidate, either because we
13790 : have two identical ones or matching builtin and non-builtin candidates.
13791 :
13792 : (Pedantically in the latter case the builtin which matched the user
13793 : function should not be added to the overload set, but we spot it here.
13794 :
13795 : [over.match.oper]
13796 : ... the builtin candidates include ...
13797 : - do not have the same parameter type list as any non-template
13798 : non-member candidate. */
13799 :
13800 4756656 : if (identifier_p (cand1->fn) || identifier_p (cand2->fn))
13801 : {
13802 93 : for (i = 0; i < len; ++i)
13803 69 : if (!same_type_p (cand1->convs[i]->type,
13804 : cand2->convs[i]->type))
13805 : break;
13806 43 : if (i == cand1->num_convs)
13807 : {
13808 24 : if (cand1->fn == cand2->fn)
13809 : /* Two built-in candidates; arbitrarily pick one. */
13810 : return 1;
13811 21 : else if (identifier_p (cand1->fn))
13812 : /* cand1 is built-in; prefer cand2. */
13813 21 : return -1;
13814 : else
13815 : /* cand2 is built-in; prefer cand1. */
13816 : return 1;
13817 : }
13818 : }
13819 :
13820 : /* For candidates of a multi-versioned function, make the version with
13821 : the highest priority win. This version will be checked for dispatching
13822 : first. If this version can be inlined into the caller, the front-end
13823 : will simply make a direct call to this function. */
13824 :
13825 4756632 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13826 4756610 : && DECL_FUNCTION_VERSIONED (cand1->fn)
13827 941 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13828 4757573 : && DECL_FUNCTION_VERSIONED (cand2->fn))
13829 : {
13830 941 : tree f1 = TREE_TYPE (cand1->fn);
13831 941 : tree f2 = TREE_TYPE (cand2->fn);
13832 941 : tree p1 = TYPE_ARG_TYPES (f1);
13833 941 : tree p2 = TYPE_ARG_TYPES (f2);
13834 :
13835 : /* Check if cand1->fn and cand2->fn are versions of the same function. It
13836 : is possible that cand1->fn and cand2->fn are function versions but of
13837 : different functions. Check types to see if they are versions of the same
13838 : function. */
13839 941 : if (compparms (p1, p2)
13840 941 : && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
13841 : {
13842 : /* Always make the version with the higher priority, more
13843 : specialized, win. */
13844 941 : gcc_assert (targetm.compare_version_priority);
13845 941 : if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
13846 : return 1;
13847 : else
13848 254 : return -1;
13849 : }
13850 : }
13851 :
13852 : /* If the two function declarations represent the same function (this can
13853 : happen with declarations in multiple scopes and arg-dependent lookup),
13854 : arbitrarily choose one. But first make sure the default args we're
13855 : using match. */
13856 4755669 : if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
13857 9511360 : && equal_functions (cand1->fn, cand2->fn))
13858 : {
13859 35 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
13860 35 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
13861 :
13862 70 : gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
13863 :
13864 50 : for (i = 0; i < len; ++i)
13865 : {
13866 : /* Don't crash if the fn is variadic. */
13867 15 : if (!parms1)
13868 : break;
13869 15 : parms1 = TREE_CHAIN (parms1);
13870 15 : parms2 = TREE_CHAIN (parms2);
13871 : }
13872 :
13873 35 : if (off1)
13874 0 : parms1 = TREE_CHAIN (parms1);
13875 35 : else if (off2)
13876 0 : parms2 = TREE_CHAIN (parms2);
13877 :
13878 69 : for (; parms1; ++i)
13879 : {
13880 80 : if (!cp_tree_equal (TREE_PURPOSE (parms1),
13881 40 : TREE_PURPOSE (parms2)))
13882 : {
13883 6 : if (warn)
13884 : {
13885 3 : if (complain & tf_error)
13886 : {
13887 3 : auto_diagnostic_group d;
13888 3 : if (permerror (input_location,
13889 : "default argument mismatch in "
13890 : "overload resolution"))
13891 : {
13892 3 : inform (DECL_SOURCE_LOCATION (cand1->fn),
13893 : " candidate 1: %q#F", cand1->fn);
13894 3 : inform (DECL_SOURCE_LOCATION (cand2->fn),
13895 : " candidate 2: %q#F", cand2->fn);
13896 : }
13897 3 : }
13898 : else
13899 : return 0;
13900 : }
13901 : else
13902 3 : add_warning (cand1, cand2);
13903 : break;
13904 : }
13905 34 : parms1 = TREE_CHAIN (parms1);
13906 34 : parms2 = TREE_CHAIN (parms2);
13907 : }
13908 :
13909 : return 1;
13910 : }
13911 :
13912 4756668 : tweak:
13913 :
13914 : /* Extension: If the worst conversion for one candidate is better than the
13915 : worst conversion for the other, take the first. */
13916 4756668 : if (!pedantic && (complain & tf_warning_or_error))
13917 : {
13918 : conversion_rank rank1 = cr_identity, rank2 = cr_identity;
13919 10246641 : struct z_candidate *w = 0, *l = 0;
13920 :
13921 10246641 : for (i = 0; i < len; ++i)
13922 : {
13923 5739692 : if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
13924 4418615 : rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
13925 5739692 : if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
13926 4418632 : rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
13927 : }
13928 4506949 : if (rank1 < rank2)
13929 56 : winner = 1, w = cand1, l = cand2;
13930 4506949 : if (rank1 > rank2)
13931 : winner = -1, w = cand2, l = cand1;
13932 4506841 : if (winner)
13933 : {
13934 : /* Don't choose a deleted function over ambiguity. */
13935 164 : if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
13936 : return 0;
13937 164 : if (warn)
13938 : {
13939 6 : auto_diagnostic_group d;
13940 6 : if (pedwarn (input_location, 0,
13941 : "ISO C++ says that these are ambiguous, even "
13942 : "though the worst conversion for the first is "
13943 : "better than the worst conversion for the second:"))
13944 : {
13945 3 : print_z_candidate (input_location, N_("candidate 1:"), w);
13946 3 : print_z_candidate (input_location, N_("candidate 2:"), l);
13947 : }
13948 6 : }
13949 : else
13950 158 : add_warning (w, l);
13951 : return winner;
13952 : }
13953 : }
13954 :
13955 : gcc_assert (!winner);
13956 : return 0;
13957 : }
13958 :
13959 : /* Given a list of candidates for overloading, find the best one, if any.
13960 : This algorithm has a worst case of O(2n) (winner is last), and a best
13961 : case of O(n/2) (totally ambiguous); much better than a sorting
13962 : algorithm. The candidates list is assumed to be sorted according
13963 : to viability (via splice_viable). */
13964 :
13965 : static struct z_candidate *
13966 244450194 : tourney (struct z_candidate *candidates, tsubst_flags_t complain)
13967 : {
13968 244450194 : struct z_candidate **champ = &candidates, **challenger;
13969 244450194 : int fate;
13970 244450194 : struct z_candidate *previous_worse_champ = nullptr;
13971 :
13972 : /* Walk through the list once, comparing each current champ to the next
13973 : candidate, knocking out a candidate or two with each comparison. */
13974 :
13975 311733375 : for (challenger = &candidates->next; *challenger && (*challenger)->viable; )
13976 : {
13977 67295985 : fate = joust (*champ, *challenger, 0, complain);
13978 67295985 : if (fate == 1)
13979 43596262 : challenger = &(*challenger)->next;
13980 23699723 : else if (fate == -1)
13981 : {
13982 18944925 : previous_worse_champ = *champ;
13983 18944925 : champ = challenger;
13984 18944925 : challenger = &(*challenger)->next;
13985 : }
13986 : else
13987 : {
13988 4754798 : previous_worse_champ = nullptr;
13989 4754798 : champ = &(*challenger)->next;
13990 4754798 : if (!*champ || !(*champ)->viable
13991 4742088 : || (*champ)->viable < (*challenger)->viable)
13992 : {
13993 : champ = nullptr;
13994 : break;
13995 : }
13996 4741994 : challenger = &(*champ)->next;
13997 : }
13998 : }
13999 :
14000 : /* Make sure the champ is better than all the candidates it hasn't yet
14001 : been compared to. */
14002 :
14003 244450194 : if (champ)
14004 28710711 : for (challenger = &candidates;
14005 273148101 : challenger != champ;
14006 28710711 : challenger = &(*challenger)->next)
14007 : {
14008 28712617 : if (*challenger == previous_worse_champ)
14009 : /* We already know this candidate is worse than the champ. */
14010 17737228 : continue;
14011 10975389 : fate = joust (*champ, *challenger, 0, complain);
14012 10975389 : if (fate != 1)
14013 : {
14014 : champ = nullptr;
14015 : break;
14016 : }
14017 : }
14018 :
14019 244437390 : if (!champ)
14020 : return nullptr;
14021 :
14022 : /* Move the champ to the front of the candidate list. */
14023 :
14024 244435484 : if (champ != &candidates)
14025 : {
14026 19422935 : z_candidate *saved_champ = *champ;
14027 19422935 : *champ = saved_champ->next;
14028 19422935 : saved_champ->next = candidates;
14029 19422935 : candidates = saved_champ;
14030 : }
14031 :
14032 244435484 : return candidates;
14033 : }
14034 :
14035 : /* Returns nonzero if things of type FROM can be converted to TO. */
14036 :
14037 : bool
14038 4512148 : can_convert (tree to, tree from, tsubst_flags_t complain)
14039 : {
14040 4512148 : tree arg = NULL_TREE;
14041 : /* implicit_conversion only considers user-defined conversions
14042 : if it has an expression for the call argument list. */
14043 4512148 : if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to))
14044 111 : arg = build_stub_object (from);
14045 4512148 : return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain);
14046 : }
14047 :
14048 : /* Returns nonzero if things of type FROM can be converted to TO with a
14049 : standard conversion. */
14050 :
14051 : bool
14052 263 : can_convert_standard (tree to, tree from, tsubst_flags_t complain)
14053 : {
14054 263 : return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
14055 : }
14056 :
14057 : /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
14058 :
14059 : bool
14060 5511506 : can_convert_arg (tree to, tree from, tree arg, int flags,
14061 : tsubst_flags_t complain)
14062 : {
14063 5511506 : conversion *t;
14064 5511506 : bool ok_p;
14065 :
14066 5511506 : conversion_obstack_sentinel cos;
14067 : /* We want to discard any access checks done for this test,
14068 : as we might not be in the appropriate access context and
14069 : we'll do the check again when we actually perform the
14070 : conversion. */
14071 5511506 : push_deferring_access_checks (dk_deferred);
14072 :
14073 : /* Handle callers like check_local_shadow forgetting to
14074 : convert_from_reference. */
14075 5511506 : if (TYPE_REF_P (from) && arg)
14076 : {
14077 61 : arg = convert_from_reference (arg);
14078 61 : from = TREE_TYPE (arg);
14079 : }
14080 :
14081 5511506 : t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
14082 : flags, complain);
14083 5511506 : ok_p = (t && !t->bad_p);
14084 :
14085 : /* Discard the access checks now. */
14086 5511506 : pop_deferring_access_checks ();
14087 :
14088 11023012 : return ok_p;
14089 5511506 : }
14090 :
14091 : /* Like can_convert_arg, but allows dubious conversions as well. */
14092 :
14093 : bool
14094 184564947 : can_convert_arg_bad (tree to, tree from, tree arg, int flags,
14095 : tsubst_flags_t complain)
14096 : {
14097 184564947 : conversion *t;
14098 :
14099 184564947 : conversion_obstack_sentinel cos;
14100 : /* Try to perform the conversion. */
14101 184564947 : t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
14102 : flags, complain);
14103 :
14104 369129894 : return t != NULL;
14105 184564947 : }
14106 :
14107 : /* Return an IMPLICIT_CONV_EXPR from EXPR to TYPE with bits set from overload
14108 : resolution FLAGS. */
14109 :
14110 : tree
14111 14579320 : build_implicit_conv_flags (tree type, tree expr, int flags)
14112 : {
14113 : /* In a template, we are only concerned about determining the
14114 : type of non-dependent expressions, so we do not have to
14115 : perform the actual conversion. But for initializers, we
14116 : need to be able to perform it at instantiation
14117 : (or instantiate_non_dependent_expr) time. */
14118 14579320 : expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
14119 14579320 : if (!(flags & LOOKUP_ONLYCONVERTING))
14120 6694636 : IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
14121 14579320 : if (flags & LOOKUP_NO_NARROWING)
14122 23482 : IMPLICIT_CONV_EXPR_BRACED_INIT (expr) = true;
14123 14579320 : return expr;
14124 : }
14125 :
14126 : /* Convert EXPR to TYPE. Return the converted expression.
14127 :
14128 : Note that we allow bad conversions here because by the time we get to
14129 : this point we are committed to doing the conversion. If we end up
14130 : doing a bad conversion, convert_like will complain. */
14131 :
14132 : tree
14133 299485901 : perform_implicit_conversion_flags (tree type, tree expr,
14134 : tsubst_flags_t complain, int flags)
14135 : {
14136 299485901 : conversion *conv;
14137 299485901 : location_t loc = cp_expr_loc_or_input_loc (expr);
14138 :
14139 299485901 : if (error_operand_p (expr))
14140 450 : return error_mark_node;
14141 :
14142 299485451 : conversion_obstack_sentinel cos;
14143 :
14144 299485451 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
14145 : /*c_cast_p=*/false,
14146 : flags, complain);
14147 :
14148 299485451 : if (!conv)
14149 : {
14150 262618 : if (complain & tf_error)
14151 488 : implicit_conversion_error (loc, type, expr, flags);
14152 262618 : expr = error_mark_node;
14153 : }
14154 299222833 : else if (processing_template_decl && conv->kind != ck_identity)
14155 14579320 : expr = build_implicit_conv_flags (type, expr, flags);
14156 : else
14157 : {
14158 : /* Give a conversion call the same location as expr. */
14159 284643513 : iloc_sentinel il (loc);
14160 284643513 : expr = convert_like (conv, expr, complain);
14161 284643513 : }
14162 :
14163 299485451 : return expr;
14164 299485451 : }
14165 :
14166 : tree
14167 13074550 : perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
14168 : {
14169 13074550 : return perform_implicit_conversion_flags (type, expr, complain,
14170 13074550 : LOOKUP_IMPLICIT);
14171 : }
14172 :
14173 : /* Convert EXPR to TYPE (as a direct-initialization) if that is
14174 : permitted. If the conversion is valid, the converted expression is
14175 : returned. Otherwise, NULL_TREE is returned, except in the case
14176 : that TYPE is a class type; in that case, an error is issued. If
14177 : C_CAST_P is true, then this direct-initialization is taking
14178 : place as part of a static_cast being attempted as part of a C-style
14179 : cast. */
14180 :
14181 : tree
14182 49168258 : perform_direct_initialization_if_possible (tree type,
14183 : tree expr,
14184 : bool c_cast_p,
14185 : tsubst_flags_t complain)
14186 : {
14187 49168258 : conversion *conv;
14188 :
14189 49168258 : if (type == error_mark_node || error_operand_p (expr))
14190 : return error_mark_node;
14191 : /* [dcl.init]
14192 :
14193 : If the destination type is a (possibly cv-qualified) class type:
14194 :
14195 : -- If the initialization is direct-initialization ...,
14196 : constructors are considered.
14197 :
14198 : -- If overload resolution is successful, the selected constructor
14199 : is called to initialize the object, with the initializer expression
14200 : or expression-list as its argument(s).
14201 :
14202 : -- Otherwise, if no constructor is viable, the destination type is
14203 : a (possibly cv-qualified) aggregate class A, and the initializer is
14204 : a parenthesized expression-list, the object is initialized as
14205 : follows... */
14206 49168258 : if (CLASS_TYPE_P (type))
14207 : {
14208 3247915 : releasing_vec args (make_tree_vector_single (expr));
14209 3247915 : expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
14210 : &args, type, LOOKUP_NORMAL, complain);
14211 3247915 : return build_cplus_new (type, expr, complain);
14212 3247915 : }
14213 :
14214 45920343 : conversion_obstack_sentinel cos;
14215 :
14216 45920343 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
14217 : c_cast_p,
14218 : LOOKUP_NORMAL, complain);
14219 45920343 : if (!conv || conv->bad_p)
14220 : expr = NULL_TREE;
14221 41119736 : else if (processing_template_decl && conv->kind != ck_identity)
14222 : {
14223 : /* In a template, we are only concerned about determining the
14224 : type of non-dependent expressions, so we do not have to
14225 : perform the actual conversion. But for initializers, we
14226 : need to be able to perform it at instantiation
14227 : (or instantiate_non_dependent_expr) time. */
14228 707046 : expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
14229 707046 : IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
14230 : }
14231 : else
14232 40412690 : expr = convert_like (conv, expr, NULL_TREE, 0,
14233 : /*issue_conversion_warnings=*/false,
14234 : c_cast_p, /*nested_p=*/false, complain);
14235 :
14236 45920343 : return expr;
14237 45920343 : }
14238 :
14239 : /* When initializing a reference that lasts longer than a full-expression,
14240 : this special rule applies:
14241 :
14242 : [class.temporary]
14243 :
14244 : The temporary to which the reference is bound or the temporary
14245 : that is the complete object to which the reference is bound
14246 : persists for the lifetime of the reference.
14247 :
14248 : The temporaries created during the evaluation of the expression
14249 : initializing the reference, except the temporary to which the
14250 : reference is bound, are destroyed at the end of the
14251 : full-expression in which they are created.
14252 :
14253 : In that case, we store the converted expression into a new
14254 : VAR_DECL in a new scope.
14255 :
14256 : However, we want to be careful not to create temporaries when
14257 : they are not required. For example, given:
14258 :
14259 : struct B {};
14260 : struct D : public B {};
14261 : D f();
14262 : const B& b = f();
14263 :
14264 : there is no need to copy the return value from "f"; we can just
14265 : extend its lifetime. Similarly, given:
14266 :
14267 : struct S {};
14268 : struct T { operator S(); };
14269 : T t;
14270 : const S& s = t;
14271 :
14272 : we can extend the lifetime of the return value of the conversion
14273 : operator.
14274 :
14275 : The next several functions are involved in this lifetime extension. */
14276 :
14277 : /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
14278 : reference is being bound to a temporary. Create and return a new
14279 : VAR_DECL with the indicated TYPE; this variable will store the value to
14280 : which the reference is bound. */
14281 :
14282 : tree
14283 9329 : make_temporary_var_for_ref_to_temp (tree decl, tree type)
14284 : {
14285 9329 : tree var = create_temporary_var (type);
14286 :
14287 : /* Register the variable. */
14288 9329 : if (VAR_P (decl)
14289 9329 : && (TREE_STATIC (decl) || CP_DECL_THREAD_LOCAL_P (decl)))
14290 : {
14291 : /* Namespace-scope or local static; give it a mangled name. */
14292 :
14293 : /* If an initializer is visible to multiple translation units, those
14294 : translation units must agree on the addresses of the
14295 : temporaries. Therefore the temporaries must be given a consistent name
14296 : and vague linkage. The mangled name of a temporary is the name of the
14297 : non-temporary object in whose initializer they appear, prefixed with
14298 : GR and suffixed with a sequence number mangled using the usual rules
14299 : for a seq-id. Temporaries are numbered with a pre-order, depth-first,
14300 : left-to-right walk of the complete initializer. */
14301 823 : copy_linkage (var, decl);
14302 :
14303 823 : tree name = mangle_ref_init_variable (decl);
14304 823 : DECL_NAME (var) = name;
14305 823 : SET_DECL_ASSEMBLER_NAME (var, name);
14306 :
14307 : /* Set the context to make the variable mergeable in modules. */
14308 823 : DECL_CONTEXT (var) = current_scope ();
14309 : }
14310 : else
14311 : /* Create a new cleanup level if necessary. */
14312 8506 : maybe_push_cleanup_level (type);
14313 :
14314 9329 : return pushdecl (var);
14315 : }
14316 :
14317 : static tree extend_temps_r (tree *, int *, void *);
14318 :
14319 : /* EXPR is the initializer for a variable DECL of reference or
14320 : std::initializer_list type. Create, push and return a new VAR_DECL
14321 : for the initializer so that it will live as long as DECL. Any
14322 : cleanup for the new variable is returned through CLEANUP, and the
14323 : code to initialize the new variable is returned through INITP. */
14324 :
14325 : static tree
14326 9329 : set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
14327 : tree *initp, tree *cond_guard,
14328 : void *walk_data)
14329 : {
14330 9329 : tree init;
14331 9329 : tree type;
14332 9329 : tree var;
14333 :
14334 : /* Create the temporary variable. */
14335 9329 : type = TREE_TYPE (expr);
14336 9329 : var = make_temporary_var_for_ref_to_temp (decl, type);
14337 9329 : layout_decl (var, 0);
14338 : /* If the rvalue is the result of a function call it will be
14339 : a TARGET_EXPR. If it is some other construct (such as a
14340 : member access expression where the underlying object is
14341 : itself the result of a function call), turn it into a
14342 : TARGET_EXPR here. It is important that EXPR be a
14343 : TARGET_EXPR below since otherwise the INIT_EXPR will
14344 : attempt to make a bitwise copy of EXPR to initialize
14345 : VAR. */
14346 9329 : if (TREE_CODE (expr) != TARGET_EXPR)
14347 0 : expr = get_target_expr (expr);
14348 : else
14349 : {
14350 9329 : if (TREE_ADDRESSABLE (expr))
14351 9246 : TREE_ADDRESSABLE (var) = 1;
14352 9329 : if (DECL_MERGEABLE (TARGET_EXPR_SLOT (expr)))
14353 633 : DECL_MERGEABLE (var) = true;
14354 : }
14355 :
14356 9329 : if (TREE_CODE (decl) == FIELD_DECL
14357 9329 : && extra_warnings && !warning_suppressed_p (decl))
14358 : {
14359 3 : warning (OPT_Wextra, "a temporary bound to %qD only persists "
14360 : "until the constructor exits", decl);
14361 3 : suppress_warning (decl);
14362 : }
14363 :
14364 : /* Recursively extend temps in this initializer. The recursion needs to come
14365 : after creating the variable to conform to the mangling ABI, and before
14366 : maybe_constant_init because the extension might change its result. */
14367 9329 : if (walk_data)
14368 2184 : cp_walk_tree (&TARGET_EXPR_INITIAL (expr), extend_temps_r,
14369 : walk_data, nullptr);
14370 : else
14371 7145 : TARGET_EXPR_INITIAL (expr)
14372 14290 : = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups,
14373 : cond_guard);
14374 :
14375 : /* Any reference temp has a non-trivial initializer. */
14376 9329 : DECL_NONTRIVIALLY_INITIALIZED_P (var) = true;
14377 :
14378 : /* If the initializer is constant, put it in DECL_INITIAL so we get
14379 : static initialization and use in constant expressions. */
14380 9329 : init = maybe_constant_init (expr, var, /*manifestly_const_eval=*/true);
14381 : /* As in store_init_value. */
14382 9329 : init = cp_fully_fold (init);
14383 9329 : if (TREE_CONSTANT (init))
14384 : {
14385 1290 : if (literal_type_p (type)
14386 1264 : && CP_TYPE_CONST_NON_VOLATILE_P (type)
14387 2046 : && !TYPE_HAS_MUTABLE_P (type))
14388 : {
14389 : /* 5.19 says that a constant expression can include an
14390 : lvalue-rvalue conversion applied to "a glvalue of literal type
14391 : that refers to a non-volatile temporary object initialized
14392 : with a constant expression". Rather than try to communicate
14393 : that this VAR_DECL is a temporary, just mark it constexpr. */
14394 753 : DECL_DECLARED_CONSTEXPR_P (var) = true;
14395 753 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
14396 753 : TREE_CONSTANT (var) = true;
14397 753 : TREE_READONLY (var) = true;
14398 : }
14399 1290 : DECL_INITIAL (var) = init;
14400 1290 : init = NULL_TREE;
14401 : }
14402 : else
14403 : /* Create the INIT_EXPR that will initialize the temporary
14404 : variable. */
14405 8039 : init = split_nonconstant_init (var, expr);
14406 9329 : if (at_function_scope_p ())
14407 : {
14408 8598 : add_decl_expr (var);
14409 :
14410 8598 : if (TREE_STATIC (var))
14411 92 : init = add_stmt_to_compound (init, register_dtor_fn (var));
14412 : else
14413 : {
14414 : /* ??? Instead of rebuilding the cleanup, we could replace the slot
14415 : with var in TARGET_EXPR_CLEANUP (expr). */
14416 8506 : tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
14417 8506 : if (cleanup)
14418 : {
14419 3517 : if (cond_guard && cleanup != error_mark_node)
14420 : {
14421 23 : if (*cond_guard == NULL_TREE)
14422 : {
14423 23 : *cond_guard = build_local_temp (boolean_type_node);
14424 23 : add_decl_expr (*cond_guard);
14425 23 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION,
14426 : *cond_guard, NOP_EXPR,
14427 : boolean_false_node,
14428 : tf_warning_or_error);
14429 23 : finish_expr_stmt (set);
14430 : }
14431 23 : cleanup = build3 (COND_EXPR, void_type_node,
14432 : *cond_guard, cleanup, NULL_TREE);
14433 : }
14434 3517 : if (flag_exceptions && TREE_CODE (TREE_TYPE (var)) != ARRAY_TYPE)
14435 : {
14436 : /* The normal cleanup for this extended variable isn't pushed
14437 : until cp_finish_decl, so we need to retain a TARGET_EXPR
14438 : to clean it up in case a later initializer throws
14439 : (g++.dg/eh/ref-temp3.C).
14440 :
14441 : We don't do this for array temporaries because they have
14442 : the array cleanup region from build_vec_init.
14443 :
14444 : Unlike maybe_push_temp_cleanup, we don't actually need a
14445 : flag, but a TARGET_EXPR needs a TARGET_EXPR_SLOT.
14446 : Perhaps this could use WITH_CLEANUP_EXPR instead, but
14447 : gimplify.cc doesn't handle that, and front-end handling
14448 : was removed in r8-1725 and r8-1818.
14449 :
14450 : Alternately it might be preferable to flatten an
14451 : initialization with extended temps into a sequence of
14452 : (non-full-expression) statements, so we could immediately
14453 : push_cleanup here for only a single cleanup region, but we
14454 : don't have a mechanism for that in the front-end, only the
14455 : gimplifier. */
14456 3409 : tree targ = get_internal_target_expr (boolean_true_node);
14457 3409 : TARGET_EXPR_CLEANUP (targ) = cleanup;
14458 3409 : CLEANUP_EH_ONLY (targ) = true;
14459 : /* Don't actually initialize the bool. */
14460 3409 : init = (!init ? void_node
14461 3383 : : convert_to_void (init, ICV_STATEMENT, tf_none));
14462 3409 : TARGET_EXPR_INITIAL (targ) = init;
14463 3409 : init = targ;
14464 : }
14465 3517 : vec_safe_push (*cleanups, cleanup);
14466 : }
14467 : }
14468 :
14469 : /* We must be careful to destroy the temporary only
14470 : after its initialization has taken place. If the
14471 : initialization throws an exception, then the
14472 : destructor should not be run. We cannot simply
14473 : transform INIT into something like:
14474 :
14475 : (INIT, ({ CLEANUP_STMT; }))
14476 :
14477 : because emit_local_var always treats the
14478 : initializer as a full-expression. Thus, the
14479 : destructor would run too early; it would run at the
14480 : end of initializing the reference variable, rather
14481 : than at the end of the block enclosing the
14482 : reference variable.
14483 :
14484 : The solution is to pass back a cleanup expression
14485 : which the caller is responsible for attaching to
14486 : the statement tree. */
14487 : }
14488 : else
14489 : {
14490 : /* Don't output reflection variables. */
14491 731 : if (consteval_only_p (var))
14492 2 : DECL_EXTERNAL (var) = true;
14493 :
14494 731 : rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
14495 731 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
14496 : {
14497 43 : if (CP_DECL_THREAD_LOCAL_P (var))
14498 12 : tls_aggregates = tree_cons (NULL_TREE, var,
14499 : tls_aggregates);
14500 : else
14501 31 : static_aggregates = tree_cons (NULL_TREE, var,
14502 : static_aggregates);
14503 : }
14504 : else
14505 : /* Check whether the dtor is callable. */
14506 688 : cxx_maybe_build_cleanup (var, tf_warning_or_error);
14507 : }
14508 : /* Avoid -Wunused-variable warning (c++/38958). */
14509 9329 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
14510 9329 : && VAR_P (decl))
14511 3491 : TREE_USED (decl) = DECL_READ_P (decl) = true;
14512 :
14513 9329 : *initp = init;
14514 9329 : return var;
14515 : }
14516 :
14517 : /* Convert EXPR to the indicated reference TYPE, in a way suitable for
14518 : initializing a variable of that TYPE. */
14519 :
14520 : tree
14521 6511557 : initialize_reference (tree type, tree expr,
14522 : int flags, tsubst_flags_t complain)
14523 : {
14524 6511557 : conversion *conv;
14525 6511557 : location_t loc = cp_expr_loc_or_input_loc (expr);
14526 :
14527 6511557 : if (type == error_mark_node || error_operand_p (expr))
14528 : return error_mark_node;
14529 :
14530 6511480 : conversion_obstack_sentinel cos;
14531 :
14532 6511480 : conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
14533 : flags, complain);
14534 : /* If this conversion failed, we're in C++20, and we have something like
14535 : A& a(b) where A is an aggregate, try again, this time as A& a{b}. */
14536 6511480 : if ((!conv || conv->bad_p)
14537 725 : && (flags & LOOKUP_AGGREGATE_PAREN_INIT))
14538 : {
14539 4 : tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
14540 4 : CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
14541 4 : CONSTRUCTOR_IS_PAREN_INIT (e) = true;
14542 4 : conversion *c = reference_binding (type, TREE_TYPE (e), e,
14543 : /*c_cast_p=*/false, flags, complain);
14544 : /* If this worked, use it. */
14545 4 : if (c && !c->bad_p)
14546 : expr = e, conv = c;
14547 : }
14548 6511480 : if (!conv || conv->bad_p)
14549 : {
14550 722 : if (complain & tf_error)
14551 : {
14552 366 : if (conv)
14553 313 : convert_like (conv, expr, complain);
14554 53 : else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
14555 21 : && !TYPE_REF_IS_RVALUE (type)
14556 74 : && !lvalue_p (expr))
14557 6 : error_at (loc, "invalid initialization of non-const reference of "
14558 : "type %qH from an rvalue of type %qI",
14559 6 : type, TREE_TYPE (expr));
14560 : else
14561 47 : error_at (loc, "invalid initialization of reference of type "
14562 : "%qH from expression of type %qI", type,
14563 47 : TREE_TYPE (expr));
14564 : }
14565 722 : return error_mark_node;
14566 : }
14567 :
14568 6510758 : if (conv->kind == ck_ref_bind)
14569 : /* Perform the conversion. */
14570 6510755 : expr = convert_like (conv, expr, complain);
14571 3 : else if (conv->kind == ck_ambig)
14572 : /* We gave an error in build_user_type_conversion_1. */
14573 3 : expr = error_mark_node;
14574 : else
14575 0 : gcc_unreachable ();
14576 :
14577 : return expr;
14578 6511480 : }
14579 :
14580 : /* Return true if T is std::pair<const T&, const T&>. */
14581 :
14582 : static bool
14583 618843 : std_pair_ref_ref_p (tree t)
14584 : {
14585 : /* First, check if we have std::pair. */
14586 58738 : if (!NON_UNION_CLASS_TYPE_P (t)
14587 677121 : || !CLASSTYPE_TEMPLATE_INSTANTIATION (t))
14588 : return false;
14589 21235 : tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (t));
14590 21235 : if (!decl_in_std_namespace_p (tdecl))
14591 : return false;
14592 15894 : tree name = DECL_NAME (tdecl);
14593 15894 : if (!name || !id_equal (name, "pair"))
14594 : return false;
14595 :
14596 : /* Now see if the template arguments are both const T&. */
14597 361 : tree args = CLASSTYPE_TI_ARGS (t);
14598 361 : if (TREE_VEC_LENGTH (args) != 2)
14599 : return false;
14600 421 : for (int i = 0; i < 2; i++)
14601 451 : if (!TYPE_REF_OBJ_P (TREE_VEC_ELT (args, i))
14602 451 : || !CP_TYPE_CONST_P (TREE_TYPE (TREE_VEC_ELT (args, i))))
14603 : return false;
14604 :
14605 : return true;
14606 : }
14607 :
14608 : /* Return true if a class T has a reference member. */
14609 :
14610 : static bool
14611 216 : class_has_reference_member_p (tree t)
14612 : {
14613 216 : for (tree fields = TYPE_FIELDS (t);
14614 3380 : fields;
14615 3164 : fields = DECL_CHAIN (fields))
14616 3227 : if (TREE_CODE (fields) == FIELD_DECL
14617 196 : && !DECL_ARTIFICIAL (fields)
14618 3394 : && TYPE_REF_P (TREE_TYPE (fields)))
14619 : return true;
14620 : return false;
14621 : }
14622 :
14623 : /* A wrapper for the above suitable as a callback for dfs_walk_once. */
14624 :
14625 : static tree
14626 216 : class_has_reference_member_p_r (tree binfo, void *)
14627 : {
14628 216 : return (class_has_reference_member_p (BINFO_TYPE (binfo))
14629 216 : ? integer_one_node : NULL_TREE);
14630 : }
14631 :
14632 :
14633 : /* Return true if T (either a class or a function) has been marked as
14634 : not-dangling. */
14635 :
14636 : static bool
14637 1557 : no_dangling_p (tree t)
14638 : {
14639 1557 : t = lookup_attribute ("no_dangling", TYPE_ATTRIBUTES (t));
14640 1557 : if (!t)
14641 : return false;
14642 :
14643 75 : t = TREE_VALUE (t);
14644 75 : if (!t)
14645 : return true;
14646 :
14647 51 : t = build_converted_constant_bool_expr (TREE_VALUE (t), tf_warning_or_error);
14648 51 : t = cxx_constant_value (t);
14649 51 : return t == boolean_true_node;
14650 : }
14651 :
14652 : /* Return true if a class CTYPE is either std::reference_wrapper or
14653 : std::ref_view, or a reference wrapper class. We consider a class
14654 : a reference wrapper class if it has a reference member. We no
14655 : longer check that it has a constructor taking the same reference type
14656 : since that approach still generated too many false positives. */
14657 :
14658 : static bool
14659 447 : reference_like_class_p (tree ctype)
14660 : {
14661 447 : if (!CLASS_TYPE_P (ctype))
14662 : return false;
14663 :
14664 314 : if (no_dangling_p (ctype))
14665 : return true;
14666 :
14667 : /* Also accept a std::pair<const T&, const T&>. */
14668 290 : if (std_pair_ref_ref_p (ctype))
14669 : return true;
14670 :
14671 275 : tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (ctype));
14672 275 : if (decl_in_std_namespace_p (tdecl))
14673 : {
14674 38 : tree name = DECL_NAME (tdecl);
14675 38 : if (name
14676 38 : && (id_equal (name, "reference_wrapper")
14677 26 : || id_equal (name, "span")
14678 11 : || id_equal (name, "ref_view")))
14679 : return true;
14680 : }
14681 :
14682 : /* Avoid warning if CTYPE looks like std::span: it has a T* member and
14683 : a trivial destructor. For example,
14684 :
14685 : template<typename T>
14686 : struct Span {
14687 : T* data_;
14688 : std::size len_;
14689 : };
14690 :
14691 : is considered std::span-like. */
14692 248 : if (NON_UNION_CLASS_TYPE_P (ctype) && TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
14693 228 : for (tree field = next_aggregate_field (TYPE_FIELDS (ctype));
14694 367 : field; field = next_aggregate_field (DECL_CHAIN (field)))
14695 199 : if (TYPE_PTR_P (TREE_TYPE (field)))
14696 : return true;
14697 :
14698 : /* Some classes, such as std::tuple, have the reference member in its
14699 : (non-direct) base class. */
14700 188 : if (dfs_walk_once (TYPE_BINFO (ctype), class_has_reference_member_p_r,
14701 : nullptr, nullptr))
14702 63 : return true;
14703 :
14704 : return false;
14705 : }
14706 :
14707 : /* Helper for maybe_warn_dangling_reference to find a problematic temporary
14708 : in EXPR (as outlined in maybe_warn_dangling_reference), or NULL_TREE
14709 : if none found. For instance:
14710 :
14711 : const S& s = S().self(); // S()
14712 : const int& r = (42, f(1)); // temporary for passing 1 to f
14713 : const int& t = b ? f(1) : f(2); // temporary for 1
14714 : const int& u = b ? f(1) : f(g); // temporary for 1
14715 : const int& v = b ? f(g) : f(2); // temporary for 2
14716 : const int& w = b ? f(g) : f(g); // NULL_TREE
14717 : const int& y = (f(1), 42); // NULL_TREE
14718 : const int& z = f(f(1)); // temporary for 1
14719 :
14720 : EXPR is the initializer. If ARG_P is true, we're processing an argument
14721 : to a function; the point is to distinguish between, for example,
14722 :
14723 : Ref::inner (&TARGET_EXPR <D.2839, F::foo (fm)>)
14724 :
14725 : where we shouldn't warn, and
14726 :
14727 : Ref::inner (&TARGET_EXPR <D.2908, F::foo (&TARGET_EXPR <...>)>)
14728 :
14729 : where we should warn (Ref is a reference_like_class_p so we see through
14730 : it. */
14731 :
14732 : static tree
14733 3715 : do_warn_dangling_reference (tree expr, bool arg_p)
14734 : {
14735 3881 : STRIP_NOPS (expr);
14736 :
14737 3881 : if (arg_p && expr_represents_temporary_p (expr))
14738 : {
14739 : /* An attempt to reduce the number of -Wdangling-reference
14740 : false positives concerning reference wrappers (c++/107532).
14741 : When we encounter a reference_like_class_p, we don't warn
14742 : just yet; instead, we keep recursing to see if there were
14743 : any temporaries behind the reference-wrapper class. */
14744 : tree e = expr;
14745 355 : while (handled_component_p (e))
14746 15 : e = TREE_OPERAND (e, 0);
14747 340 : tree type = TREE_TYPE (e);
14748 : /* If the temporary represents a lambda, we don't really know
14749 : what's going on here. */
14750 462 : if (!reference_like_class_p (type) && !LAMBDA_TYPE_P (type))
14751 : return expr;
14752 : }
14753 :
14754 3648 : switch (TREE_CODE (expr))
14755 : {
14756 1822 : case CALL_EXPR:
14757 1822 : {
14758 1822 : tree fndecl = cp_get_callee_fndecl_nofold (expr);
14759 1822 : if (!fndecl
14760 1822 : || warning_suppressed_p (fndecl, OPT_Wdangling_reference)
14761 2055 : || !warning_enabled_at (DECL_SOURCE_LOCATION (fndecl),
14762 1811 : OPT_Wdangling_reference)
14763 : /* Don't emit a false positive for:
14764 : std::vector<int> v = ...;
14765 : std::vector<int>::const_iterator it = v.begin();
14766 : const int &r = *it++;
14767 : because R refers to one of the int elements of V, not to
14768 : a temporary object. Member operator* may return a reference
14769 : but probably not to one of its arguments. */
14770 1452 : || (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl)
14771 852 : && DECL_OVERLOADED_OPERATOR_P (fndecl)
14772 319 : && DECL_OVERLOADED_OPERATOR_IS (fndecl, INDIRECT_REF))
14773 3065 : || no_dangling_p (TREE_TYPE (fndecl)))
14774 : return NULL_TREE;
14775 :
14776 1219 : tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
14777 : /* If the function doesn't return a reference, don't warn. This
14778 : can be e.g.
14779 : const int& z = std::min({1, 2, 3, 4, 5, 6, 7});
14780 : which doesn't dangle: std::min here returns an int.
14781 :
14782 : If the function returns a std::pair<const T&, const T&>, we
14783 : warn, to detect e.g.
14784 : std::pair<const int&, const int&> v = std::minmax(1, 2);
14785 : which also creates a dangling reference, because std::minmax
14786 : returns std::pair<const T&, const T&>(b, a). */
14787 1219 : if (!(TYPE_REF_OBJ_P (rettype) || reference_like_class_p (rettype)))
14788 : return NULL_TREE;
14789 :
14790 : /* Here we're looking to see if any of the arguments is a temporary
14791 : initializing a reference parameter. */
14792 1647 : for (int i = 0; i < call_expr_nargs (expr); ++i)
14793 : {
14794 1240 : tree arg = CALL_EXPR_ARG (expr, i);
14795 : /* Check that this argument initializes a reference, except for
14796 : the argument initializing the object of a member function. */
14797 1240 : if (!DECL_IOBJ_MEMBER_FUNCTION_P (fndecl)
14798 1240 : && !TYPE_REF_P (TREE_TYPE (arg)))
14799 130 : continue;
14800 1110 : STRIP_NOPS (arg);
14801 1110 : if (TREE_CODE (arg) == ADDR_EXPR)
14802 725 : arg = TREE_OPERAND (arg, 0);
14803 : /* Recurse to see if the argument is a temporary. It could also
14804 : be another call taking a temporary and returning it and
14805 : initializing this reference parameter. */
14806 1110 : if ((arg = do_warn_dangling_reference (arg, /*arg_p=*/true)))
14807 : {
14808 : /* If we know the temporary could not bind to the return type,
14809 : don't warn. This is for scalars and empty classes only
14810 : because for other classes we can't be sure we are not
14811 : returning its sub-object. */
14812 278 : if ((SCALAR_TYPE_P (TREE_TYPE (arg))
14813 152 : || is_empty_class (TREE_TYPE (arg)))
14814 153 : && TYPE_REF_P (rettype)
14815 138 : && !reference_related_p (TREE_TYPE (rettype),
14816 138 : TREE_TYPE (arg)))
14817 21 : continue;
14818 : return arg;
14819 : }
14820 : /* Don't warn about member functions like:
14821 : std::any a(...);
14822 : S& s = a.emplace<S>({0}, 0);
14823 : which construct a new object and return a reference to it, but
14824 : we still want to detect:
14825 : struct S { const S& self () { return *this; } };
14826 : const S& s = S().self();
14827 : where 's' dangles. If we've gotten here, the object this function
14828 : is invoked on is not a temporary. */
14829 832 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl))
14830 : break;
14831 : }
14832 : return NULL_TREE;
14833 : }
14834 28 : case COMPOUND_EXPR:
14835 28 : return do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p);
14836 31 : case COND_EXPR:
14837 31 : if (tree t = do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p))
14838 : return t;
14839 16 : return do_warn_dangling_reference (TREE_OPERAND (expr, 2), arg_p);
14840 0 : case PAREN_EXPR:
14841 0 : return do_warn_dangling_reference (TREE_OPERAND (expr, 0), arg_p);
14842 122 : case TARGET_EXPR:
14843 122 : return do_warn_dangling_reference (TARGET_EXPR_INITIAL (expr), arg_p);
14844 : default:
14845 : return NULL_TREE;
14846 : }
14847 : }
14848 :
14849 : /* Implement -Wdangling-reference, to detect cases like
14850 :
14851 : int n = 1;
14852 : const int& r = std::max(n - 1, n + 1); // r is dangling
14853 :
14854 : This creates temporaries from the arguments, returns a reference to
14855 : one of the temporaries, but both temporaries are destroyed at the end
14856 : of the full expression.
14857 :
14858 : This works by checking if a reference is initialized with a function
14859 : that returns a reference, and at least one parameter of the function
14860 : is a reference that is bound to a temporary. It assumes that such a
14861 : function actually returns one of its arguments.
14862 :
14863 : DECL is the reference being initialized, INIT is the initializer. */
14864 :
14865 : static void
14866 109580209 : maybe_warn_dangling_reference (const_tree decl, tree init)
14867 : {
14868 109580209 : if (!warn_dangling_reference)
14869 : return;
14870 621112 : tree type = TREE_TYPE (decl);
14871 : /* Only warn if what we're initializing has type T&& or const T&, or
14872 : std::pair<const T&, const T&>. (A non-const lvalue reference can't
14873 : bind to a temporary.) */
14874 1239665 : if (!((TYPE_REF_OBJ_P (type)
14875 5444 : && (TYPE_REF_IS_RVALUE (type)
14876 5004 : || CP_TYPE_CONST_P (TREE_TYPE (type))))
14877 618553 : || std_pair_ref_ref_p (type)))
14878 : return;
14879 : /* Don't suppress the diagnostic just because the call comes from
14880 : a system header. If the DECL is not in a system header, or if
14881 : -Wsystem-headers was provided, warn. */
14882 2574 : auto wsh
14883 2574 : = make_temp_override (global_dc->m_warn_system_headers,
14884 2574 : (!in_system_header_at (DECL_SOURCE_LOCATION (decl))
14885 2574 : || global_dc->m_warn_system_headers));
14886 2574 : if (tree call = do_warn_dangling_reference (init, /*arg_p=*/false))
14887 : {
14888 212 : auto_diagnostic_group d;
14889 212 : if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wdangling_reference,
14890 : "possibly dangling reference to a temporary"))
14891 212 : inform (EXPR_LOCATION (call), "%qT temporary created here",
14892 212 : TREE_TYPE (call));
14893 212 : }
14894 2574 : }
14895 :
14896 : /* If *P is an xvalue expression, prevent temporary lifetime extension if it
14897 : gets used to initialize a reference. */
14898 :
14899 : static tree
14900 95 : prevent_lifetime_extension (tree t)
14901 : {
14902 95 : tree *p = &t;
14903 95 : while (TREE_CODE (*p) == COMPOUND_EXPR)
14904 0 : p = &TREE_OPERAND (*p, 1);
14905 104 : while (handled_component_p (*p))
14906 9 : p = &TREE_OPERAND (*p, 0);
14907 : /* Change a TARGET_EXPR from prvalue to xvalue. */
14908 95 : if (TREE_CODE (*p) == TARGET_EXPR)
14909 3 : *p = build2 (COMPOUND_EXPR, TREE_TYPE (*p), *p,
14910 3 : move (TARGET_EXPR_SLOT (*p)));
14911 95 : return t;
14912 : }
14913 :
14914 : /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
14915 : which is bound either to a reference or a std::initializer_list. */
14916 :
14917 : static tree
14918 764874 : extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups,
14919 : tree *cond_guard)
14920 : {
14921 : /* CWG1299 (C++20): The temporary object to which the reference is bound or
14922 : the temporary object that is the complete object of a subobject to which
14923 : the reference is bound persists for the lifetime of the reference if the
14924 : glvalue to which the reference is bound was obtained through one of the
14925 : following:
14926 : - a temporary materialization conversion ([conv.rval]),
14927 : - ( expression ), where expression is one of these expressions,
14928 : - subscripting ([expr.sub]) of an array operand, where that operand is one
14929 : of these expressions,
14930 : - a class member access ([expr.ref]) using the . operator where the left
14931 : operand is one of these expressions and the right operand designates a
14932 : non-static data member of non-reference type,
14933 : - a pointer-to-member operation ([expr.mptr.oper]) using the .* operator
14934 : where the left operand is one of these expressions and the right operand
14935 : is a pointer to data member of non-reference type,
14936 : - a const_cast ([expr.const.cast]), static_cast ([expr.static.cast]),
14937 : dynamic_cast ([expr.dynamic.cast]), or reinterpret_cast
14938 : ([expr.reinterpret.cast]) converting, without a user-defined conversion,
14939 : a glvalue operand that is one of these expressions to a glvalue that
14940 : refers to the object designated by the operand, or to its complete
14941 : object or a subobject thereof,
14942 : - a conditional expression ([expr.cond]) that is a glvalue where the
14943 : second or third operand is one of these expressions, or
14944 : - a comma expression ([expr.comma]) that is a glvalue where the right
14945 : operand is one of these expressions. */
14946 :
14947 : /* FIXME several cases are still handled wrong (101572, 81420). */
14948 :
14949 764874 : tree sub = init;
14950 764874 : tree *p;
14951 764874 : STRIP_NOPS (sub);
14952 764874 : if (TREE_CODE (sub) == COMPOUND_EXPR)
14953 : {
14954 242 : TREE_OPERAND (sub, 1)
14955 242 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups,
14956 : cond_guard);
14957 242 : return init;
14958 : }
14959 764632 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14960 764632 : && TYPE_PTRDATAMEM_P (TREE_TYPE (tree_strip_nop_conversions
14961 : (TREE_OPERAND (sub, 1)))))
14962 : {
14963 : /* A pointer-to-member operation. */
14964 6 : TREE_OPERAND (sub, 0)
14965 6 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 0), cleanups,
14966 : cond_guard);
14967 6 : return init;
14968 : }
14969 764626 : if (TREE_CODE (sub) == COND_EXPR)
14970 : {
14971 23449 : tree cur_cond_guard = NULL_TREE;
14972 23449 : if (TREE_OPERAND (sub, 1))
14973 23449 : TREE_OPERAND (sub, 1)
14974 46898 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups,
14975 : &cur_cond_guard);
14976 23449 : if (cur_cond_guard)
14977 : {
14978 9 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard,
14979 : NOP_EXPR, boolean_true_node,
14980 : tf_warning_or_error);
14981 9 : TREE_OPERAND (sub, 1)
14982 18 : = cp_build_compound_expr (set, TREE_OPERAND (sub, 1),
14983 : tf_warning_or_error);
14984 : }
14985 23449 : cur_cond_guard = NULL_TREE;
14986 23449 : if (TREE_OPERAND (sub, 2))
14987 23449 : TREE_OPERAND (sub, 2)
14988 46898 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 2), cleanups,
14989 : &cur_cond_guard);
14990 23449 : if (cur_cond_guard)
14991 : {
14992 12 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard,
14993 : NOP_EXPR, boolean_true_node,
14994 : tf_warning_or_error);
14995 12 : TREE_OPERAND (sub, 2)
14996 24 : = cp_build_compound_expr (set, TREE_OPERAND (sub, 2),
14997 : tf_warning_or_error);
14998 : }
14999 23449 : return init;
15000 : }
15001 741177 : if (TREE_CODE (sub) != ADDR_EXPR)
15002 : return init;
15003 : /* Deal with binding to a subobject. */
15004 264718 : for (p = &TREE_OPERAND (sub, 0);
15005 278234 : TREE_CODE (*p) == COMPONENT_REF || TREE_CODE (*p) == ARRAY_REF; )
15006 13516 : p = &TREE_OPERAND (*p, 0);
15007 264718 : if (TREE_CODE (*p) == TARGET_EXPR)
15008 : {
15009 7145 : tree subinit = NULL_TREE;
15010 7145 : *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit,
15011 : cond_guard, nullptr);
15012 7145 : recompute_tree_invariant_for_addr_expr (sub);
15013 7145 : if (init != sub)
15014 7127 : init = fold_convert (TREE_TYPE (init), sub);
15015 7145 : if (subinit)
15016 6029 : init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
15017 : }
15018 : return init;
15019 : }
15020 :
15021 : /* Data for extend_temps_r, mostly matching the parameters of
15022 : extend_ref_init_temps. */
15023 :
15024 : struct extend_temps_data
15025 : {
15026 : tree decl;
15027 : tree init;
15028 : vec<tree, va_gc> **cleanups;
15029 : tree* cond_guard;
15030 : hash_set<tree> *pset; // For avoiding redundant walk_tree.
15031 : hash_map<tree, tree> *var_map; // For remapping extended temps.
15032 : };
15033 :
15034 : /* Tree walk function for extend_all_temps. Generally parallel to
15035 : extend_ref_init_temps_1, but adapted for walk_tree. */
15036 :
15037 : tree
15038 98906 : extend_temps_r (tree *tp, int *walk_subtrees, void *data)
15039 : {
15040 98906 : extend_temps_data *d = (extend_temps_data *)data;
15041 :
15042 98906 : if (TREE_CODE (*tp) == VAR_DECL)
15043 : {
15044 11546 : if (tree *r = d->var_map->get (*tp))
15045 0 : *tp = *r;
15046 : return NULL_TREE;
15047 : }
15048 :
15049 87341 : if (TYPE_P (*tp) || TREE_CODE (*tp) == CLEANUP_POINT_EXPR
15050 174700 : || d->pset->add (*tp))
15051 : {
15052 4956 : *walk_subtrees = 0;
15053 4956 : return NULL_TREE;
15054 : }
15055 :
15056 82404 : if (TREE_CODE (*tp) == COND_EXPR)
15057 : {
15058 8 : cp_walk_tree (&TREE_OPERAND (*tp, 0), extend_temps_r, d, nullptr);
15059 :
15060 24 : auto walk_arm = [d](tree &op)
15061 : {
15062 16 : tree cur_cond_guard = NULL_TREE;
15063 16 : auto ov = make_temp_override (d->cond_guard, &cur_cond_guard);
15064 16 : cp_walk_tree (&op, extend_temps_r, d, nullptr);
15065 16 : if (cur_cond_guard)
15066 : {
15067 2 : tree set = build2 (MODIFY_EXPR, boolean_type_node,
15068 : cur_cond_guard, boolean_true_node);
15069 2 : op = cp_build_compound_expr (set, op, tf_none);
15070 : }
15071 24 : };
15072 8 : walk_arm (TREE_OPERAND (*tp, 1));
15073 8 : walk_arm (TREE_OPERAND (*tp, 2));
15074 :
15075 8 : *walk_subtrees = 0;
15076 8 : return NULL_TREE;
15077 : }
15078 :
15079 82396 : tree *p = tp;
15080 :
15081 82396 : if (TREE_CODE (*tp) == ADDR_EXPR)
15082 18594 : for (p = &TREE_OPERAND (*tp, 0);
15083 20058 : TREE_CODE (*p) == COMPONENT_REF || TREE_CODE (*p) == ARRAY_REF; )
15084 1464 : p = &TREE_OPERAND (*p, 0);
15085 :
15086 82396 : if (TREE_CODE (*p) == TARGET_EXPR
15087 : /* An eliding TARGET_EXPR isn't a temporary at all. */
15088 3590 : && !TARGET_EXPR_ELIDING_P (*p)
15089 : /* A TARGET_EXPR with TARGET_EXPR_INTERNAL_P is an artificial variable
15090 : used during initialization that need not be extended. */
15091 84914 : && !TARGET_EXPR_INTERNAL_P (*p))
15092 : {
15093 : /* A CLEANUP_EH_ONLY expr should also have TARGET_EXPR_INTERNAL_P. */
15094 2184 : gcc_checking_assert (!CLEANUP_EH_ONLY (*p));
15095 :
15096 2184 : tree subinit = NULL_TREE;
15097 2184 : tree slot = TARGET_EXPR_SLOT (*p);
15098 2184 : *p = set_up_extended_ref_temp (d->decl, *p, d->cleanups, &subinit,
15099 : d->cond_guard, d);
15100 2184 : if (TREE_CODE (*tp) == ADDR_EXPR)
15101 2170 : recompute_tree_invariant_for_addr_expr (*tp);
15102 2184 : if (subinit)
15103 2036 : *tp = cp_build_compound_expr (subinit, *tp, tf_none);
15104 2184 : d->var_map->put (slot, *p);
15105 : }
15106 :
15107 : return NULL_TREE;
15108 : }
15109 :
15110 : /* Extend all the temporaries in a for-range-initializer. */
15111 :
15112 : static tree
15113 19984 : extend_all_temps (tree decl, tree init, vec<tree, va_gc> **cleanups)
15114 : {
15115 19984 : hash_set<tree> pset;
15116 19984 : hash_map<tree, tree> map;
15117 19984 : gcc_assert (!TREE_STATIC (decl));
15118 19984 : extend_temps_data d = { decl, init, cleanups, nullptr, &pset, &map };
15119 19984 : cp_walk_tree (&init, extend_temps_r, &d, nullptr);
15120 19984 : return init;
15121 19984 : }
15122 :
15123 : /* INIT is part of the initializer for DECL. If there are any
15124 : reference or initializer lists being initialized, extend their
15125 : lifetime to match that of DECL. */
15126 :
15127 : tree
15128 112084073 : extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups,
15129 : tree *cond_guard)
15130 : {
15131 112084073 : tree type = TREE_TYPE (init);
15132 112084073 : if (processing_template_decl)
15133 : return init;
15134 :
15135 : /* P2718R0 - in C++23 for-range-initializer, extend all temps. */
15136 109600193 : if (DECL_NAME (decl) == for_range__identifier
15137 94871 : && flag_range_for_ext_temps
15138 : /* Iterating expansion statement decl is static right now, but that
15139 : could change depending on CWG3044 and CWG3043. */
15140 109620235 : && !TREE_STATIC (decl))
15141 : {
15142 19984 : gcc_checking_assert (!cond_guard);
15143 19984 : return extend_all_temps (decl, init, cleanups);
15144 : }
15145 :
15146 109580209 : maybe_warn_dangling_reference (decl, init);
15147 :
15148 109580209 : if (TYPE_REF_P (type))
15149 717069 : init = extend_ref_init_temps_1 (decl, init, cleanups, cond_guard);
15150 : else
15151 : {
15152 108863140 : tree ctor = init;
15153 108863140 : if (TREE_CODE (ctor) == TARGET_EXPR)
15154 1566488 : ctor = TARGET_EXPR_INITIAL (ctor);
15155 108863140 : if (TREE_CODE (ctor) == CONSTRUCTOR)
15156 : {
15157 : /* [dcl.init] When initializing an aggregate from a parenthesized list
15158 : of values... a temporary object bound to a reference does not have
15159 : its lifetime extended. */
15160 5686738 : if (CONSTRUCTOR_IS_PAREN_INIT (ctor))
15161 : return init;
15162 :
15163 5686250 : if (is_std_init_list (type))
15164 : {
15165 : /* The temporary array underlying a std::initializer_list
15166 : is handled like a reference temporary. */
15167 659 : tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
15168 659 : array = extend_ref_init_temps_1 (decl, array, cleanups,
15169 : cond_guard);
15170 659 : CONSTRUCTOR_ELT (ctor, 0)->value = array;
15171 : }
15172 : else
15173 : {
15174 5685591 : unsigned i;
15175 5685591 : constructor_elt *p;
15176 5685591 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
15177 63216724 : FOR_EACH_VEC_SAFE_ELT (elts, i, p)
15178 57531133 : p->value = extend_ref_init_temps (decl, p->value, cleanups,
15179 : cond_guard);
15180 : }
15181 5686250 : recompute_constructor_flags (ctor);
15182 5686250 : if (decl_maybe_constant_var_p (decl) && TREE_CONSTANT (ctor))
15183 3169579 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
15184 : }
15185 : }
15186 :
15187 : return init;
15188 : }
15189 :
15190 : /* Returns true iff an initializer for TYPE could contain temporaries that
15191 : need to be extended because they are bound to references or
15192 : std::initializer_list. */
15193 :
15194 : bool
15195 0 : type_has_extended_temps (tree type)
15196 : {
15197 0 : type = strip_array_types (type);
15198 0 : if (TYPE_REF_P (type))
15199 : return true;
15200 0 : if (CLASS_TYPE_P (type))
15201 : {
15202 0 : if (is_std_init_list (type))
15203 : return true;
15204 0 : for (tree f = next_aggregate_field (TYPE_FIELDS (type));
15205 0 : f; f = next_aggregate_field (DECL_CHAIN (f)))
15206 0 : if (type_has_extended_temps (TREE_TYPE (f)))
15207 : return true;
15208 : }
15209 : return false;
15210 : }
15211 :
15212 : /* Returns true iff TYPE is some variant of std::initializer_list. */
15213 :
15214 : bool
15215 183596631 : is_std_init_list (tree type)
15216 : {
15217 183596631 : if (!TYPE_P (type))
15218 : return false;
15219 183596608 : if (cxx_dialect == cxx98)
15220 : return false;
15221 : /* Look through typedefs. */
15222 183177804 : type = TYPE_MAIN_VARIANT (type);
15223 146818158 : return (CLASS_TYPE_P (type)
15224 146671321 : && CP_TYPE_CONTEXT (type) == std_node
15225 264957100 : && init_list_identifier == DECL_NAME (TYPE_NAME (type)));
15226 : }
15227 :
15228 : /* Returns true iff DECL is a list constructor: i.e. a constructor which
15229 : will accept an argument list of a single std::initializer_list<T>. */
15230 :
15231 : bool
15232 159453916 : is_list_ctor (tree decl)
15233 : {
15234 159453916 : tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
15235 159453916 : tree arg;
15236 :
15237 159453916 : if (!args || args == void_list_node)
15238 : return false;
15239 :
15240 128343345 : arg = non_reference (TREE_VALUE (args));
15241 128343345 : if (!is_std_init_list (arg))
15242 : return false;
15243 :
15244 2310397 : args = TREE_CHAIN (args);
15245 :
15246 3932680 : if (args && args != void_list_node && !TREE_PURPOSE (args))
15247 : /* There are more non-defaulted parms. */
15248 635302 : return false;
15249 :
15250 : return true;
15251 : }
15252 :
15253 : /* We know that can_convert_arg_bad already said "no" when trying to convert
15254 : FROM to TO with ARG and FLAGS. Try to figure out if it was because
15255 : an explicit conversion function was skipped when looking for a way to
15256 : perform the conversion. At this point we've already printed an error. */
15257 :
15258 : void
15259 2146 : maybe_show_nonconverting_candidate (tree to, tree from, tree arg, int flags)
15260 : {
15261 2146 : if (!(flags & LOOKUP_ONLYCONVERTING))
15262 311 : return;
15263 :
15264 1835 : conversion_obstack_sentinel cos;
15265 1835 : conversion *c = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
15266 : flags & ~LOOKUP_ONLYCONVERTING, tf_none);
15267 1835 : if (c && !c->bad_p && c->user_conv_p)
15268 : /* Ay, the conversion would have worked in direct-init context. */
15269 859 : for (; c; c = next_conversion (c))
15270 575 : if (c->kind == ck_user
15271 281 : && DECL_P (c->cand->fn)
15272 856 : && DECL_NONCONVERTING_P (c->cand->fn))
15273 277 : inform (DECL_SOURCE_LOCATION (c->cand->fn), "explicit conversion "
15274 : "function was not considered");
15275 1835 : }
15276 :
15277 : /* We're converting EXPR to TYPE. If that conversion involves a conversion
15278 : function and we're binding EXPR to a reference parameter of that function,
15279 : return true. */
15280 :
15281 : bool
15282 171 : conv_binds_to_reference_parm_p (tree type, tree expr)
15283 : {
15284 171 : conversion_obstack_sentinel cos;
15285 171 : conversion *c = implicit_conversion (type, TREE_TYPE (expr), expr,
15286 : /*c_cast_p=*/false, LOOKUP_NORMAL,
15287 : tf_none);
15288 171 : if (c && !c->bad_p && c->user_conv_p)
15289 117 : for (; c; c = next_conversion (c))
15290 81 : if (c->kind == ck_user)
15291 244 : for (z_candidate *cand = c->cand; cand; cand = cand->next)
15292 208 : if (cand->viable == 1)
15293 81 : for (size_t i = 0; i < cand->num_convs; ++i)
15294 45 : if (cand->convs[i]->kind == ck_ref_bind
15295 45 : && conv_get_original_expr (cand->convs[i]) == expr)
15296 : return true;
15297 :
15298 : return false;
15299 171 : }
15300 :
15301 : #include "gt-cp-call.h"
|