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 3215187 : check_dtor_name (tree basetype, tree name)
236 : {
237 : /* Just accept something we've already complained about. */
238 3215187 : if (name == error_mark_node)
239 : return true;
240 :
241 3215187 : if (TREE_CODE (name) == TYPE_DECL)
242 84 : name = TREE_TYPE (name);
243 3215103 : 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 3215161 : 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 364346000 : build_addr_func (tree function, tsubst_flags_t complain)
282 : {
283 364346000 : tree type = TREE_TYPE (function);
284 :
285 : /* We have to do these by hand to avoid real pointer to member
286 : functions. */
287 364346000 : if (TREE_CODE (type) == METHOD_TYPE)
288 : {
289 55687613 : 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 55687540 : function = build_address (function);
297 : }
298 308658387 : else if (TREE_CODE (function) == FUNCTION_DECL
299 440646794 : && DECL_IMMEDIATE_FUNCTION_P (function))
300 1706635 : function = build_address (function);
301 : else
302 306951752 : 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 172381 : build_call_n (tree function, int n, ...)
315 : {
316 172381 : if (n == 0)
317 0 : return build_call_a (function, 0, NULL);
318 : else
319 : {
320 172381 : tree *argarray = XALLOCAVEC (tree, n);
321 172381 : va_list ap;
322 172381 : int i;
323 :
324 172381 : va_start (ap, n);
325 346066 : for (i = 0; i < n; i++)
326 173685 : argarray[i] = va_arg (ap, tree);
327 172381 : va_end (ap);
328 172381 : 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 177811742 : set_flags_from_callee (tree call)
337 : {
338 : /* Handle both CALL_EXPRs and AGGR_INIT_EXPRs. */
339 177811742 : 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 177811742 : bool nothrow = decl && TREE_NOTHROW (decl);
344 177811742 : tree callee = cp_get_callee (call);
345 177811742 : if (callee)
346 177811734 : 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 177811742 : if (cfun && cp_function_chain && !cp_unevaluated_operand)
352 : {
353 101919135 : if (!nothrow && at_function_scope_p ())
354 25226447 : cp_function_chain->can_throw = 1;
355 :
356 101919135 : if (decl && TREE_THIS_VOLATILE (decl))
357 3061108 : current_function_returns_abnormally = 1;
358 : }
359 :
360 177811742 : TREE_NOTHROW (call) = nothrow;
361 177811742 : }
362 :
363 : tree
364 171737280 : build_call_a (tree function, int n, tree *argarray)
365 : {
366 171737280 : tree decl;
367 171737280 : tree result_type;
368 171737280 : tree fntype;
369 171737280 : int i;
370 :
371 171737280 : function = build_addr_func (function, tf_warning_or_error);
372 :
373 171737280 : gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
374 171737280 : fntype = TREE_TYPE (TREE_TYPE (function));
375 171737280 : gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype));
376 171737280 : result_type = TREE_TYPE (fntype);
377 : /* An rvalue has no cv-qualifiers. */
378 171737280 : if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
379 99953331 : result_type = cv_unqualified (result_type);
380 :
381 171737280 : function = build_call_array_loc (input_location,
382 : result_type, function, n, argarray);
383 171737280 : set_flags_from_callee (function);
384 :
385 171737280 : decl = get_callee_fndecl (function);
386 :
387 171737280 : 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 217667 : gcc_assert (DECL_ARTIFICIAL (decl)
394 : || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
395 : "__", 2));
396 217667 : mark_used (decl);
397 : }
398 :
399 171737280 : require_complete_eh_spec_types (fntype, decl);
400 :
401 495310862 : 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 171737280 : if (!decl || !fndecl_built_in_p (decl))
407 344568392 : for (i = 0; i < n; i++)
408 : {
409 180780903 : tree arg = CALL_EXPR_ARG (function, i);
410 180780903 : if (is_empty_class (TREE_TYPE (arg))
411 180780903 : && simple_empty_class_p (TREE_TYPE (arg), arg, INIT_EXPR))
412 : {
413 5709158 : while (TREE_CODE (arg) == TARGET_EXPR)
414 : /* We're disconnecting the initializer from its target,
415 : don't create a temporary. */
416 2853399 : arg = TARGET_EXPR_INITIAL (arg);
417 2855759 : suppress_warning (arg, OPT_Wunused_result);
418 2855759 : tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg));
419 2855759 : arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t);
420 2855759 : CALL_EXPR_ARG (function, i) = arg;
421 : }
422 : }
423 :
424 171737280 : 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 66018330 : bool rewritten () const { return (flags & LOOKUP_REWRITTEN); }
543 1082643888 : 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 131662838 : null_ptr_cst_p (tree t)
551 : {
552 131662838 : 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 131662838 : if (NULLPTR_TYPE_P (type))
559 : return true;
560 :
561 122460821 : if (cxx_dialect >= cxx11)
562 : {
563 122065855 : STRIP_ANY_LOCATION_WRAPPER (t);
564 :
565 : /* Core issue 903 says only literal 0 is a null pointer constant. */
566 122065855 : if (TREE_CODE (t) == INTEGER_CST
567 13877281 : && !TREE_OVERFLOW (t)
568 13877281 : && TREE_CODE (type) == INTEGER_TYPE
569 13265196 : && integer_zerop (t)
570 130875423 : && !char_type_p (type))
571 8809272 : return true;
572 : }
573 394966 : else if (CP_INTEGRAL_TYPE_P (type))
574 : {
575 76207 : t = fold_non_dependent_expr (t, tf_none);
576 76207 : STRIP_NOPS (t);
577 76207 : if (integer_zerop (t) && !TREE_OVERFLOW (t))
578 54635 : 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 102569109 : null_member_pointer_value_p (tree t)
588 : {
589 102569109 : tree type = TREE_TYPE (t);
590 102569109 : if (!type)
591 : return false;
592 102380710 : 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 102379601 : else if (TYPE_PTRDATAMEM_P (type))
597 5971 : 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 745285393 : sufficient_parms_p (const_tree parmlist)
607 : {
608 755153098 : for (; parmlist && parmlist != void_list_node;
609 9867705 : parmlist = TREE_CHAIN (parmlist))
610 226696184 : if (!TREE_PURPOSE (parmlist)
611 226696184 : && !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 8159691507 : conversion_obstack_alloc (size_t n)
621 : {
622 8159691507 : void *p;
623 8159691507 : if (!conversion_obstack_initialized)
624 : {
625 87161 : gcc_obstack_init (&conversion_obstack);
626 87161 : conversion_obstack_initialized = true;
627 : }
628 8159691507 : p = obstack_alloc (&conversion_obstack, n);
629 8159691507 : memset (p, 0, n);
630 8159691507 : 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 2639784758 : conversion_obstack_sentinel (): p (conversion_obstack_alloc (0)) {}
639 1319878825 : ~conversion_obstack_sentinel () { obstack_free (&conversion_obstack, p); }
640 : };
641 :
642 : /* Allocate rejection reasons. */
643 :
644 : static struct rejection_reason *
645 789028029 : alloc_rejection (enum rejection_reason_code code)
646 : {
647 789028029 : struct rejection_reason *p;
648 0 : p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
649 789028029 : p->code = code;
650 789028029 : return p;
651 : }
652 :
653 : static struct rejection_reason *
654 199076948 : arity_rejection (tree first_arg, int expected, int actual, bool least_p = false)
655 : {
656 158943191 : struct rejection_reason *r = alloc_rejection (rr_arity);
657 199076948 : int adjust = first_arg != NULL_TREE;
658 199076948 : r->u.arity.expected = expected - adjust;
659 199076948 : r->u.arity.actual = actual - adjust;
660 199076948 : r->u.arity.least_p = least_p;
661 199076948 : return r;
662 : }
663 :
664 : static struct rejection_reason *
665 167722062 : arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to,
666 : location_t loc)
667 : {
668 7166918 : struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
669 167722062 : int adjust = first_arg != NULL_TREE;
670 167722062 : r->u.conversion.n_arg = n_arg - adjust;
671 167722062 : r->u.conversion.from = from;
672 167722062 : r->u.conversion.to_type = to;
673 167722062 : r->u.conversion.loc = loc;
674 167722062 : return r;
675 : }
676 :
677 : static struct rejection_reason *
678 22673512 : bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to,
679 : location_t loc)
680 : {
681 2606 : struct rejection_reason *r = alloc_rejection (rr_bad_arg_conversion);
682 22673512 : int adjust = first_arg != NULL_TREE;
683 22673512 : r->u.bad_conversion.n_arg = n_arg - adjust;
684 22673512 : r->u.bad_conversion.from = from;
685 22673512 : r->u.bad_conversion.to_type = to;
686 22673512 : r->u.bad_conversion.loc = loc;
687 22673512 : return r;
688 : }
689 :
690 : static struct rejection_reason *
691 2747 : explicit_conversion_rejection (tree from, tree to)
692 : {
693 2747 : struct rejection_reason *r = alloc_rejection (rr_explicit_conversion);
694 2747 : r->u.conversion.n_arg = 0;
695 2747 : r->u.conversion.from = from;
696 2747 : r->u.conversion.to_type = to;
697 2747 : r->u.conversion.loc = UNKNOWN_LOCATION;
698 2747 : 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 397786790 : 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 397786790 : size_t args_n_bytes = sizeof (*args) * nargs;
719 397786790 : tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes);
720 397786790 : struct rejection_reason *r = alloc_rejection (rr_template_unification);
721 397786790 : r->u.template_unification.tmpl = tmpl;
722 397786790 : r->u.template_unification.explicit_targs = explicit_targs;
723 397786790 : r->u.template_unification.num_targs = TREE_VEC_LENGTH (targs);
724 : /* Copy args to our own storage. */
725 397786790 : memcpy (args1, args, args_n_bytes);
726 397786790 : r->u.template_unification.args = args1;
727 397786790 : r->u.template_unification.nargs = nargs;
728 397786790 : r->u.template_unification.return_type = return_type;
729 397786790 : r->u.template_unification.strict = strict;
730 397786790 : r->u.template_unification.flags = flags;
731 397786790 : 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 758885 : invalid_copy_with_fn_template_rejection (void)
742 : {
743 0 : struct rejection_reason *r = alloc_rejection (rr_invalid_copy);
744 758885 : return r;
745 : }
746 :
747 : static struct rejection_reason *
748 807777 : inherited_ctor_rejection (void)
749 : {
750 0 : struct rejection_reason *r = alloc_rejection (rr_inherited_ctor);
751 807777 : return r;
752 : }
753 :
754 : /* Build a constraint failure record. */
755 :
756 : static struct rejection_reason *
757 199171 : constraint_failure (void)
758 : {
759 0 : struct rejection_reason *r = alloc_rejection (rr_constraint_failure);
760 199171 : return r;
761 : }
762 :
763 : /* Dynamically allocate a conversion. */
764 :
765 : static conversion *
766 2805120192 : alloc_conversion (conversion_kind kind)
767 : {
768 2805120192 : conversion *c;
769 0 : c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
770 2805120192 : c->kind = kind;
771 2805120192 : return c;
772 : }
773 :
774 : /* Make sure that all memory on the conversion obstack has been
775 : freed. */
776 :
777 : void
778 99519 : validate_conversion_obstack (void)
779 : {
780 99519 : if (conversion_obstack_initialized)
781 86827 : gcc_assert ((obstack_next_free (&conversion_obstack)
782 : == obstack_base (&conversion_obstack)));
783 99519 : }
784 :
785 : /* Dynamically allocate an array of N conversions. */
786 :
787 : static conversion **
788 1053823369 : 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 1531321912 : has_next (conversion_kind code)
797 : {
798 1439299429 : return !(code == ck_identity
799 1531321912 : || code == ck_ambig
800 : || code == ck_list
801 1439392586 : || code == ck_aggr
802 1531321912 : || code == ck_deferred_bad);
803 : }
804 :
805 : static conversion *
806 822005777 : build_conv (conversion_kind code, tree type, conversion *from)
807 : {
808 822005777 : conversion *t;
809 822005777 : conversion_rank rank = CONVERSION_RANK (from);
810 :
811 : /* Only call this function for conversions that use u.next. */
812 822005777 : 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 822005777 : t = alloc_conversion (code);
817 822005777 : t->type = type;
818 822005777 : t->u.next = from;
819 :
820 822005777 : switch (code)
821 : {
822 240129836 : case ck_ptr:
823 240129836 : case ck_pmem:
824 240129836 : case ck_base:
825 240129836 : case ck_std:
826 240129836 : if (rank < cr_std)
827 822005777 : rank = cr_std;
828 : break;
829 :
830 55428656 : case ck_qual:
831 55428656 : case ck_fnptr:
832 55428656 : if (rank < cr_exact)
833 822005777 : rank = cr_exact;
834 : break;
835 :
836 : default:
837 : break;
838 : }
839 822005777 : t->rank = rank;
840 822005777 : t->user_conv_p = (code == ck_user || from->user_conv_p);
841 822005777 : t->bad_p = from->bad_p;
842 822005777 : t->base_p = false;
843 822005777 : 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 96246 : build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
852 : {
853 96246 : tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
854 96246 : unsigned len = CONSTRUCTOR_NELTS (ctor);
855 96246 : conversion **subconvs = alloc_conversions (len);
856 96246 : conversion *t;
857 96246 : unsigned i;
858 96246 : tree val;
859 :
860 : /* Within a list-initialization we can have more user-defined
861 : conversions. */
862 96246 : flags &= ~LOOKUP_NO_CONVERSION;
863 : /* But no narrowing conversions. */
864 96246 : flags |= LOOKUP_NO_NARROWING;
865 :
866 : /* Can't make an array of these types. */
867 96246 : if (TYPE_REF_P (elttype)
868 96237 : || TREE_CODE (elttype) == FUNCTION_TYPE
869 96237 : || VOID_TYPE_P (elttype))
870 : return NULL;
871 :
872 343371 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
873 : {
874 260806 : 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 260755 : conversion *sub
936 260755 : = implicit_conversion (elttype, TREE_TYPE (val), val,
937 : false, flags, complain);
938 260755 : if (sub == NULL)
939 : return NULL;
940 :
941 247083 : subconvs[i] = sub;
942 : }
943 :
944 82565 : t = alloc_conversion (ck_list);
945 82565 : t->type = type;
946 82565 : t->u.list = subconvs;
947 82565 : t->rank = cr_exact;
948 :
949 310829 : for (i = 0; i < len; ++i)
950 : {
951 228264 : conversion *sub = subconvs[i];
952 228264 : if (sub->rank > t->rank)
953 63571 : t->rank = sub->rank;
954 228264 : if (sub->user_conv_p)
955 10370 : t->user_conv_p = true;
956 228264 : if (sub->bad_p)
957 63558 : 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 617554339 : next_conversion (conversion *conv)
969 : {
970 617554339 : if (conv == NULL
971 617554339 : || !has_next (conv->kind))
972 : return NULL;
973 605102173 : 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 1541316 : strip_standard_conversion (conversion *conv)
981 : {
982 1541316 : while (conv
983 1551769 : && conv->kind != ck_user
984 1593844 : && has_next (conv->kind))
985 10453 : conv = next_conversion (conv);
986 1541316 : 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 27944 : can_convert_array (tree atype, tree from, int flags, tsubst_flags_t complain)
994 : {
995 27956 : tree elttype = TREE_TYPE (atype);
996 27956 : unsigned i;
997 :
998 27956 : if (TREE_CODE (from) == CONSTRUCTOR)
999 : {
1000 31175 : for (i = 0; i < CONSTRUCTOR_NELTS (from); ++i)
1001 : {
1002 3276 : tree val = CONSTRUCTOR_ELT (from, i)->value;
1003 3276 : bool ok;
1004 3276 : if (TREE_CODE (elttype) == ARRAY_TYPE)
1005 18 : ok = can_convert_array (elttype, val, flags, complain);
1006 : else
1007 3258 : ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags,
1008 : complain);
1009 3276 : 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 872711 : field_in_pset (hash_set<tree, true> &pset, tree field)
1032 : {
1033 872711 : 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 1373100 : build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
1053 : {
1054 1373100 : unsigned HOST_WIDE_INT i = 0;
1055 1373100 : conversion *c;
1056 1373100 : tree field = next_aggregate_field (TYPE_FIELDS (type));
1057 1373100 : tree empty_ctor = NULL_TREE;
1058 1373100 : 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 1373100 : 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 1373100 : tree idx, val;
1072 3616313 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, val)
1073 : {
1074 870205 : if (!idx)
1075 : break;
1076 :
1077 870145 : gcc_checking_assert (TREE_CODE (idx) == FIELD_DECL);
1078 :
1079 870145 : tree ftype = TREE_TYPE (idx);
1080 870145 : bool ok;
1081 :
1082 870145 : if (TREE_CODE (ftype) == ARRAY_TYPE)
1083 1197 : ok = can_convert_array (ftype, val, flags, complain);
1084 : else
1085 868948 : ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
1086 : complain);
1087 :
1088 870145 : if (!ok)
1089 : return NULL;
1090 :
1091 : /* For unions, there should be just one initializer. */
1092 870136 : if (TREE_CODE (type) == UNION_TYPE)
1093 : {
1094 1373091 : field = NULL_TREE;
1095 1373091 : i = 1;
1096 : break;
1097 : }
1098 870113 : pset.add (idx);
1099 : }
1100 :
1101 2353704 : for (; field; field = next_aggregate_field (DECL_CHAIN (field)))
1102 : {
1103 980873 : tree ftype = TREE_TYPE (field);
1104 980873 : bool ok;
1105 :
1106 980873 : if (!pset.is_empty () && field_in_pset (pset, field))
1107 870113 : continue;
1108 110760 : 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 110721 : else if (DECL_INITIAL (field))
1116 44935 : val = get_nsdmi (field, /*ctor*/false, complain);
1117 65786 : else if (TYPE_REF_P (ftype))
1118 : /* Value-initialization of reference is ill-formed. */
1119 : return NULL;
1120 : else
1121 : {
1122 65780 : if (empty_ctor == NULL_TREE)
1123 46941 : empty_ctor = build_constructor (init_list_type_node, NULL);
1124 : val = empty_ctor;
1125 : }
1126 :
1127 110754 : if (TREE_CODE (ftype) == ARRAY_TYPE)
1128 26729 : ok = can_convert_array (ftype, val, flags, complain);
1129 : else
1130 84025 : ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
1131 : complain);
1132 :
1133 110754 : if (!ok)
1134 : return NULL;
1135 :
1136 110741 : if (TREE_CODE (type) == UNION_TYPE)
1137 : break;
1138 : }
1139 :
1140 1373072 : if (i < CONSTRUCTOR_NELTS (ctor))
1141 : return NULL;
1142 :
1143 1373045 : c = alloc_conversion (ck_aggr);
1144 1373045 : c->type = type;
1145 1373045 : c->rank = cr_exact;
1146 1373045 : c->user_conv_p = true;
1147 1373045 : c->check_narrowing = true;
1148 1373045 : c->u.expr = ctor;
1149 1373045 : return c;
1150 1373100 : }
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 57292 : build_complex_conv (tree type, tree ctor, int flags,
1208 : tsubst_flags_t complain)
1209 : {
1210 57292 : conversion *c;
1211 57292 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1212 57292 : tree elttype = TREE_TYPE (type);
1213 57292 : bool bad = false;
1214 57292 : bool user = false;
1215 57292 : enum conversion_rank rank = cr_exact;
1216 :
1217 57292 : if (len != 2)
1218 : return NULL;
1219 :
1220 57252 : flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1221 :
1222 286260 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
1223 : {
1224 114504 : conversion *sub
1225 114504 : = implicit_conversion (elttype, TREE_TYPE (e.value), e.value,
1226 : false, flags, complain);
1227 114504 : if (sub == NULL)
1228 : return NULL;
1229 :
1230 114504 : if (sub->rank > rank)
1231 44 : rank = sub->rank;
1232 114504 : if (sub->user_conv_p)
1233 0 : user = true;
1234 114504 : if (sub->bad_p)
1235 0 : bad = true;
1236 : }
1237 :
1238 57252 : c = alloc_conversion (ck_aggr);
1239 57252 : c->type = type;
1240 57252 : c->rank = rank;
1241 57252 : c->user_conv_p = user;
1242 57252 : c->bad_p = bad;
1243 57252 : c->u.expr = ctor;
1244 57252 : 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 1973850785 : build_identity_conv (tree type, tree expr)
1252 : {
1253 1973850785 : conversion *c;
1254 :
1255 0 : c = alloc_conversion (ck_identity);
1256 1973850785 : c->type = type;
1257 1973850785 : c->u.expr = expr;
1258 :
1259 1973850785 : 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 3536509744 : strip_top_quals (tree t)
1280 : {
1281 3536509744 : if (TREE_CODE (t) == ARRAY_TYPE)
1282 : return t;
1283 3529322160 : 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 1580480955 : standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
1293 : int flags, tsubst_flags_t complain)
1294 : {
1295 1580480955 : enum tree_code fcode, tcode;
1296 1580480955 : conversion *conv;
1297 1580480955 : bool fromref = false;
1298 1580480955 : tree qualified_to;
1299 :
1300 1580480955 : to = non_reference (to);
1301 1580480955 : if (TYPE_REF_P (from))
1302 : {
1303 80740 : fromref = true;
1304 80740 : from = TREE_TYPE (from);
1305 : }
1306 1580480955 : qualified_to = to;
1307 1580480955 : to = strip_top_quals (to);
1308 1580480955 : from = strip_top_quals (from);
1309 :
1310 1580480955 : if (expr && type_unknown_p (expr))
1311 : {
1312 723007 : if (TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
1313 : {
1314 68031 : tsubst_flags_t tflags = tf_conv;
1315 68031 : expr = instantiate_type (to, expr, tflags);
1316 68031 : if (expr == error_mark_node)
1317 : return NULL;
1318 42654 : from = TREE_TYPE (expr);
1319 : }
1320 654976 : else if (TREE_CODE (to) == BOOLEAN_TYPE)
1321 : {
1322 : /* Necessary for eg, TEMPLATE_ID_EXPRs (c++/50961). */
1323 12469 : expr = resolve_nondeduced_context (expr, complain);
1324 12469 : from = TREE_TYPE (expr);
1325 : }
1326 : }
1327 :
1328 1580455578 : fcode = TREE_CODE (from);
1329 1580455578 : tcode = TREE_CODE (to);
1330 :
1331 1580455578 : conv = build_identity_conv (from, expr);
1332 1580455578 : if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
1333 : {
1334 7210894 : from = type_decays_to (from);
1335 7210894 : fcode = TREE_CODE (from);
1336 : /* Tell convert_like that we're using the address. */
1337 7210894 : conv->rvaluedness_matches_p = true;
1338 7210894 : 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 1573244684 : else if (fromref || (expr && obvalue_p (expr)))
1344 : {
1345 365916247 : if (expr)
1346 : {
1347 365835519 : tree bitfield_type;
1348 365835519 : bitfield_type = is_bitfield_expr_with_lowered_type (expr);
1349 365835519 : if (bitfield_type)
1350 : {
1351 2921093 : from = strip_top_quals (bitfield_type);
1352 2921093 : fcode = TREE_CODE (from);
1353 : }
1354 : }
1355 365916247 : conv = build_conv (ck_rvalue, from, conv);
1356 : /* If we're performing copy-initialization, remember to skip
1357 : explicit constructors. */
1358 365916247 : if (flags & LOOKUP_ONLYCONVERTING)
1359 313717804 : conv->copy_init_p = true;
1360 : }
1361 :
1362 : /* Allow conversion between `__complex__' data types. */
1363 1580455578 : 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 2387993 : conversion *part_conv = standard_conversion
1369 2387993 : (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags,
1370 : complain);
1371 :
1372 2387993 : if (!part_conv)
1373 : conv = NULL;
1374 2387993 : else if (part_conv->kind == ck_identity)
1375 : /* Leave conv alone. */;
1376 : else
1377 : {
1378 279190 : conv = build_conv (part_conv->kind, to, conv);
1379 279190 : conv->rank = part_conv->rank;
1380 : }
1381 :
1382 : return conv;
1383 : }
1384 :
1385 1578067585 : if (same_type_p (from, to))
1386 : {
1387 965894045 : if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue)
1388 17817312 : conv->type = qualified_to;
1389 948076733 : else if (from != to)
1390 : /* Use TO in order to not lose TO in diagnostics. */
1391 82403923 : 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 393864583 : if ((tcode == POINTER_TYPE || TYPE_PTRMEM_P (to)
1400 393772824 : || NULLPTR_TYPE_P (to))
1401 618283892 : && ((expr && null_ptr_cst_p (expr))
1402 218682830 : || NULLPTR_TYPE_P (from)))
1403 5736483 : conv = build_conv (ck_std, to, conv);
1404 606437057 : else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
1405 605730153 : || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
1406 : {
1407 : /* For backwards brain damage compatibility, allow interconversion of
1408 : pointers and integers with a pedwarn. */
1409 2242016 : conv = build_conv (ck_std, to, conv);
1410 2242016 : conv->bad_p = true;
1411 : }
1412 604195041 : 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 364913 : conv = build_conv (ck_std, to, conv);
1417 364913 : conv->bad_p = true;
1418 : }
1419 603830128 : else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
1420 397834840 : || (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 205995288 : to_pointee = TREE_TYPE (to);
1428 205995288 : 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 205995288 : if (TREE_CODE (to_pointee) == FUNCTION_TYPE
1434 205995288 : && TYPE_QUALS (to_pointee))
1435 9 : to_pointee = build_qualified_type (to_pointee, TYPE_UNQUALIFIED);
1436 205995288 : if (TREE_CODE (from_pointee) == FUNCTION_TYPE
1437 205995288 : && 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 205998053 : if (tcode == POINTER_TYPE
1447 205998053 : && same_type_ignoring_top_level_qualifiers_p (from_pointee,
1448 : to_pointee))
1449 : ;
1450 145874461 : else if (VOID_TYPE_P (to_pointee)
1451 4626517 : && !TYPE_PTRDATAMEM_P (from)
1452 4626517 : && TREE_CODE (from_pointee) != FUNCTION_TYPE)
1453 : {
1454 4626105 : tree nfrom = TREE_TYPE (from);
1455 : /* Don't try to apply restrict to void. */
1456 4626105 : int quals = cp_type_quals (nfrom) & ~TYPE_QUAL_RESTRICT;
1457 4626105 : from_pointee = cp_build_qualified_type (void_type_node, quals);
1458 4626105 : from = build_pointer_type (from_pointee);
1459 4626105 : conv = build_conv (ck_ptr, from, conv);
1460 4626105 : }
1461 141248356 : 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 67638192 : else if (CLASS_TYPE_P (from_pointee)
1479 67635819 : && 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 207986274 : && DERIVED_FROM_P (to_pointee, from_pointee))
1492 : {
1493 6255070 : from_pointee
1494 6255070 : = cp_build_qualified_type (to_pointee,
1495 : cp_type_quals (from_pointee));
1496 6255070 : from = build_pointer_type (from_pointee);
1497 6255070 : conv = build_conv (ck_ptr, from, conv);
1498 6255070 : conv->base_p = true;
1499 : }
1500 :
1501 205997817 : if (same_type_p (from, to))
1502 : /* OK */;
1503 197800269 : 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 636 : conv = build_conv (ck_qual, to, conv);
1508 197799633 : else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
1509 55165138 : conv = build_conv (ck_qual, to, conv);
1510 142634495 : 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 142634291 : else if (fnptr_conv_p (to, from))
1514 244999 : conv = build_conv (ck_fnptr, to, conv);
1515 : /* Allow conversions among compatible ObjC pointer types (base
1516 : conversions have been already handled above). */
1517 142389292 : else if (c_dialect_objc ()
1518 142389292 : && objc_compare_types (to, from, -4, NULL_TREE))
1519 0 : conv = build_conv (ck_ptr, to, conv);
1520 142389292 : else if (ptr_reasonably_similar (to_pointee, from_pointee))
1521 : {
1522 8659315 : conv = build_conv (ck_ptr, to, conv);
1523 8659315 : conv->bad_p = true;
1524 : }
1525 : else
1526 : return NULL;
1527 :
1528 279308623 : from = to;
1529 : }
1530 397832075 : else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
1531 : {
1532 86595 : tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
1533 86595 : tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
1534 86595 : tree fbase = class_of_this_parm (fromfn);
1535 86595 : 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 86595 : if (!same_type_p (fbase, tbase) && !DERIVED_FROM_P (fbase, tbase))
1540 : return NULL;
1541 :
1542 86434 : tree fstat = static_fn_type (fromfn);
1543 86434 : tree tstat = static_fn_type (tofn);
1544 86434 : if (same_type_p (tstat, fstat)
1545 86434 : || fnptr_conv_p (tstat, fstat))
1546 : /* OK */;
1547 : else
1548 : return NULL;
1549 :
1550 86166 : if (!same_type_p (fbase, tbase))
1551 : {
1552 86053 : tree first = to;
1553 86053 : 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 86053 : conv = build_conv (ck_pmem, first, conv);
1562 86053 : conv->base_p = true;
1563 : }
1564 86166 : if (fnptr_conv_p (tstat, fstat))
1565 113 : conv = build_conv (ck_fnptr, to, conv);
1566 : }
1567 397745480 : 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 12478159 : if (ARITHMETIC_TYPE_P (from)
1576 2196563 : || UNSCOPED_ENUM_P (from)
1577 4262957 : || fcode == POINTER_TYPE
1578 2994032 : || TYPE_PTRMEM_P (from)
1579 2993791 : || NULLPTR_TYPE_P (from))
1580 : {
1581 9484450 : conv = build_conv (ck_std, to, conv);
1582 9484450 : if (fcode == POINTER_TYPE
1583 8215525 : || TYPE_PTRDATAMEM_P (from)
1584 8215370 : || (TYPE_PTRMEMFUNC_P (from)
1585 86 : && conv->rank < cr_pbool)
1586 17699734 : || NULLPTR_TYPE_P (from))
1587 1269248 : conv->rank = cr_pbool;
1588 9484450 : if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING))
1589 40 : conv->bad_p = true;
1590 9484450 : if (flags & LOOKUP_NO_NARROWING)
1591 31607 : 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 385267321 : else if (ARITHMETIC_TYPE_P (to))
1601 : {
1602 32046276 : if (! (INTEGRAL_CODE_P (fcode)
1603 25127600 : || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL)))
1604 231677157 : || 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 198143035 : if (!COMPLETE_TYPE_P (from))
1611 : return NULL;
1612 :
1613 198143029 : conv = build_conv (ck_std, to, conv);
1614 :
1615 198143029 : tree underlying_type = NULL_TREE;
1616 198143029 : if (TREE_CODE (from) == ENUMERAL_TYPE
1617 198143029 : && ENUM_FIXED_UNDERLYING_TYPE_P (from))
1618 1719196 : 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 198143029 : if ((same_type_p (to, type_promotes_to (from))
1626 186104359 : || (underlying_type && same_type_p (to, underlying_type)))
1627 198143048 : && next_conversion (conv)->rank <= cr_promotion)
1628 12038689 : 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 198143029 : if (fcode == REAL_TYPE
1640 198143029 : && tcode == REAL_TYPE
1641 17110865 : && (extended_float_type_p (from)
1642 16291792 : || extended_float_type_p (to))
1643 202075739 : && cp_compare_floating_point_conversion_ranks (from, to) >= 2)
1644 1804143 : conv->bad_p = true;
1645 : }
1646 178717764 : else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
1647 178717764 : && vector_types_convertible_p (from, to, false))
1648 4905 : return build_conv (ck_std, to, conv);
1649 178712859 : else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
1650 70222284 : && is_properly_derived_from (from, to))
1651 : {
1652 468176 : if (conv->kind == ck_rvalue)
1653 468125 : conv = next_conversion (conv);
1654 468176 : 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 468176 : conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
1660 : /* If we're performing copy-initialization, remember to skip
1661 : explicit constructors. */
1662 468176 : if (flags & LOOKUP_ONLYCONVERTING)
1663 468109 : conv->copy_init_p = true;
1664 : }
1665 : else
1666 : return NULL;
1667 :
1668 279308623 : if (flags & LOOKUP_NO_NARROWING)
1669 96227774 : 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 283878890 : reference_related_p (tree t1, tree t2)
1680 : {
1681 283878890 : if (t1 == error_mark_node || t2 == error_mark_node)
1682 : return false;
1683 :
1684 283878886 : t1 = TYPE_MAIN_VARIANT (t1);
1685 283878886 : 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 283878886 : return (similar_type_p (t1, t2)
1692 283878886 : || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1693 68608798 : && DERIVED_FROM_P (t1, t2)));
1694 : }
1695 :
1696 : /* Returns nonzero if T1 is reference-compatible with T2. */
1697 :
1698 : bool
1699 251815938 : 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 251815938 : tree ptype1 = build_pointer_type (t1);
1707 251815938 : tree ptype2 = build_pointer_type (t2);
1708 251815938 : conversion *conv = standard_conversion (ptype1, ptype2, NULL_TREE,
1709 : /*c_cast_p=*/false, 0, tf_none);
1710 251815938 : if (!conv || conv->bad_p)
1711 140277938 : 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 122448190 : 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 122448190 : if (!((TYPE_PTR_P (to) && TYPE_PTR_P (from))
1724 3098 : || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from))))
1725 : return false;
1726 :
1727 4810263 : conversion *conv = standard_conversion (to, from, NULL_TREE,
1728 : /*c_cast_p=*/false, 0, tf_none);
1729 14413252 : for (conversion *t = conv; t; t = next_conversion (t))
1730 4810292 : 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 10644 : handler_match_for_exception_type (tree handler_type, tree except_type)
1741 : {
1742 10644 : 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 10562 : if (same_type_ignoring_top_level_qualifiers_p (handler_type, except_type))
1747 : return true;
1748 6827 : if (CLASS_TYPE_P (except_type) && CLASS_TYPE_P (handler_type))
1749 3806 : return publicly_uniquely_derived_p (handler_type, except_type);
1750 3021 : 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 126227932 : direct_reference_binding (tree type, conversion *conv)
1796 : {
1797 126227932 : tree t;
1798 :
1799 126227932 : gcc_assert (TYPE_REF_P (type));
1800 126227932 : gcc_assert (!TYPE_REF_P (conv->type));
1801 :
1802 126227932 : t = TREE_TYPE (type);
1803 :
1804 126227932 : if (conv->kind == ck_identity)
1805 : /* Mark the identity conv as to not decay to rvalue. */
1806 126227932 : 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 126227932 : if (is_properly_derived_from (conv->type, t))
1825 : {
1826 : /* Represent the derived-to-base conversion. */
1827 3779742 : 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 3779742 : conv->need_temporary_p = false;
1832 : }
1833 122448190 : 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 17566 : conv = build_conv (ck_qual, strip_top_quals (t), conv);
1857 :
1858 126227932 : 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 250955802 : reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags,
1870 : tsubst_flags_t complain)
1871 : {
1872 250955802 : conversion *conv = NULL;
1873 250955802 : conversion *bad_direct_conv = nullptr;
1874 250955802 : tree to = TREE_TYPE (rto);
1875 250955802 : tree from = rfrom;
1876 250955802 : tree tfrom;
1877 250955802 : bool related_p;
1878 250955802 : bool compatible_p;
1879 250955802 : cp_lvalue_kind gl_kind;
1880 250955802 : bool is_lvalue;
1881 :
1882 250955802 : 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 250955802 : bool copy_list_init = false;
1891 250955802 : bool single_list_conv = false;
1892 250955802 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1893 : {
1894 104738 : 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 104738 : if (CONSTRUCTOR_NELTS (expr) == 1
1906 59812 : && !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 250955797 : skip:;
1929 : }
1930 :
1931 250955797 : if (TYPE_REF_P (from))
1932 : {
1933 64066 : from = TREE_TYPE (from);
1934 64066 : if (!TYPE_REF_IS_RVALUE (rfrom)
1935 64066 : || TREE_CODE (from) == FUNCTION_TYPE)
1936 : gl_kind = clk_ordinary;
1937 : else
1938 250955797 : gl_kind = clk_rvalueref;
1939 : }
1940 250891731 : else if (expr)
1941 247400035 : gl_kind = lvalue_kind (expr);
1942 3205026 : else if (CLASS_TYPE_P (from)
1943 3491696 : || 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 250955797 : if ((flags & LOOKUP_NO_TEMP_BIND)
1950 3605791 : && (gl_kind & clk_class))
1951 : gl_kind = clk_none;
1952 :
1953 : /* Same mask as real_lvalue_p. */
1954 248193584 : is_lvalue = gl_kind && !(gl_kind & (clk_rvalueref|clk_class));
1955 :
1956 213223601 : tfrom = from;
1957 213223601 : if ((gl_kind & clk_bitfield) != 0)
1958 2038469 : 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 250955797 : 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 250955797 : if (related_p && c_cast_p
1967 250955797 : && !at_least_as_qualified_p (to, tfrom))
1968 200 : to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1969 250955797 : 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 250955797 : 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 112813742 : conv = build_identity_conv (tfrom, expr);
1995 112813742 : conv = direct_reference_binding (rto, conv);
1996 :
1997 112813742 : if (TYPE_REF_P (rfrom))
1998 : /* Handle rvalue reference to function properly. */
1999 16433 : conv->rvaluedness_matches_p
2000 16433 : = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom));
2001 : else
2002 112797309 : conv->rvaluedness_matches_p
2003 112797309 : = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
2004 :
2005 112813742 : if ((gl_kind & clk_bitfield) != 0
2006 112813742 : || ((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 82832407 : if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
2023 123008420 : && TREE_CODE (to) != FUNCTION_TYPE)
2024 10192686 : conv->bad_p = true;
2025 :
2026 : /* Nor the reverse. */
2027 29981335 : 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 18571746 : && !(cxx_dialect <= cxx20
2032 14786596 : && (gl_kind & clk_implicit_rval))
2033 17470319 : && (!CP_TYPE_CONST_NON_VOLATILE_P (to)
2034 17352813 : || (flags & LOOKUP_NO_RVAL_BIND))
2035 112931248 : && TREE_CODE (to) != FUNCTION_TYPE)
2036 117506 : conv->bad_p = true;
2037 :
2038 112813742 : if (!compatible_p)
2039 6658122 : 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 4524600 : else if (!related_p
2048 133617455 : && !(flags & LOOKUP_NO_CONVERSION)
2049 49372878 : && (CLASS_TYPE_P (from) || single_list_conv))
2050 : {
2051 17940734 : tree rexpr = expr;
2052 17940734 : 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 17940734 : z_candidate *cand = build_user_type_conversion_1 (rto, rexpr, flags,
2069 : complain);
2070 17940734 : if (cand)
2071 : {
2072 12692 : 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 138129900 : 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 134759078 : 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 178214602 : 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 134759078 : 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 134759078 : if (!maybe_valid_p && (flags & LOOKUP_SHORTCUT_BAD_CONVS))
2127 : {
2128 7748777 : if (bad_direct_conv)
2129 : return bad_direct_conv;
2130 :
2131 7748310 : conv = alloc_conversion (ck_deferred_bad);
2132 7748310 : conv->bad_p = true;
2133 7748310 : 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 127010301 : 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 127010301 : if (!(flags & LOOKUP_COPY_PARM))
2148 107630726 : flags |= LOOKUP_ONLYCONVERTING;
2149 :
2150 127010301 : if (!conv)
2151 127010301 : conv = implicit_conversion (to, from, expr, c_cast_p,
2152 : flags, complain);
2153 127010301 : if (!conv)
2154 : return bad_direct_conv ? bad_direct_conv : nullptr;
2155 :
2156 12005968 : if (conv->user_conv_p)
2157 : {
2158 8085337 : if (copy_list_init)
2159 : /* Remember this was copy-list-initialization. */
2160 74017 : conv->need_temporary_p = true;
2161 :
2162 : /* If initializing the temporary used a conversion function,
2163 : recalculate the second conversion sequence. */
2164 23311675 : for (conversion *t = conv; t; t = next_conversion (t))
2165 15684858 : if (t->kind == ck_user
2166 8055345 : && 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 15684846 : else if (t->kind == ck_user
2176 15684846 : && DECL_CONV_FN_P (t->cand->fn))
2177 : {
2178 458508 : tree ftype = TREE_TYPE (TREE_TYPE (t->cand->fn));
2179 : /* A prvalue of non-class type is cv-unqualified. */
2180 458508 : if (!TYPE_REF_P (ftype) && !CLASS_TYPE_P (ftype))
2181 4174 : ftype = cv_unqualified (ftype);
2182 458508 : int sflags = (flags|LOOKUP_NO_CONVERSION)&~LOOKUP_NO_TEMP_BIND;
2183 458508 : conversion *new_second
2184 458508 : = reference_binding (rto, ftype, NULL_TREE, c_cast_p,
2185 : sflags, complain);
2186 458508 : if (!new_second)
2187 : return bad_direct_conv ? bad_direct_conv : nullptr;
2188 458508 : conv = merge_conversion_sequences (t, new_second);
2189 458508 : gcc_assert (maybe_valid_p || conv->bad_p);
2190 : return conv;
2191 : }
2192 : }
2193 :
2194 11547460 : conv = build_conv (ck_ref_bind, rto, conv);
2195 : /* This reference binding, unlike those above, requires the
2196 : creation of a temporary. */
2197 11547460 : conv->need_temporary_p = true;
2198 11547460 : conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
2199 11547460 : conv->bad_p |= !maybe_valid_p;
2200 :
2201 11547460 : 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 1565436641 : implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
2211 : int flags, tsubst_flags_t complain)
2212 : {
2213 1565436726 : conversion *conv;
2214 :
2215 1565436726 : if (from == error_mark_node || to == error_mark_node
2216 1565435161 : || 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 1565435161 : 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 1565435161 : complain &= ~tf_error;
2231 :
2232 : /* Call reshape_init early to remove redundant braces. */
2233 1565435161 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr) && CLASS_TYPE_P (to))
2234 : {
2235 2416912 : to = complete_type (to);
2236 2416912 : if (!COMPLETE_TYPE_P (to))
2237 : return nullptr;
2238 2416888 : if (!CLASSTYPE_NON_AGGREGATE (to))
2239 : {
2240 1373402 : expr = reshape_init (to, expr, complain);
2241 1373402 : if (expr == error_mark_node)
2242 : return nullptr;
2243 1373159 : from = TREE_TYPE (expr);
2244 : }
2245 : }
2246 :
2247 : /* An argument should have gone through convert_from_reference. */
2248 1565434894 : gcc_checking_assert (!expr || !TYPE_REF_P (from));
2249 :
2250 1565434894 : if (TYPE_REF_P (to))
2251 243968853 : conv = reference_binding (to, from, expr, c_cast_p, flags, complain);
2252 : else
2253 1321466041 : conv = standard_conversion (to, from, expr, c_cast_p, flags, complain);
2254 :
2255 1565434894 : if (conv)
2256 : return conv;
2257 :
2258 308222886 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
2259 : {
2260 4944145 : if (is_std_init_list (to) && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2261 96246 : return build_list_conv (to, expr, flags, complain);
2262 :
2263 : /* As an extension, allow list-initialization of _Complex. */
2264 4751644 : if (TREE_CODE (to) == COMPLEX_TYPE
2265 4808945 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2266 : {
2267 57292 : conv = build_complex_conv (to, expr, flags, complain);
2268 57292 : if (conv)
2269 : return conv;
2270 : }
2271 :
2272 : /* Allow conversion from an initializer-list with one element to a
2273 : scalar type. */
2274 4694392 : if (SCALAR_TYPE_P (to))
2275 : {
2276 2348066 : int nelts = CONSTRUCTOR_NELTS (expr);
2277 363734 : tree elt;
2278 :
2279 363734 : if (nelts == 0)
2280 1984332 : elt = build_value_init (to, tf_none);
2281 363734 : else if (nelts == 1 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2282 362860 : elt = CONSTRUCTOR_ELT (expr, 0)->value;
2283 : else
2284 874 : elt = error_mark_node;
2285 :
2286 2348066 : conv = implicit_conversion (to, TREE_TYPE (elt), elt,
2287 : c_cast_p, flags, complain);
2288 2348066 : if (conv)
2289 : {
2290 2346054 : conv->check_narrowing = true;
2291 2346054 : 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 2346326 : else if (TREE_CODE (to) == ARRAY_TYPE)
2298 1889 : return build_array_conv (to, expr, flags, complain);
2299 : }
2300 :
2301 305721445 : if (expr != NULL_TREE
2302 298554016 : && (MAYBE_CLASS_TYPE_P (from)
2303 161276649 : || MAYBE_CLASS_TYPE_P (to))
2304 521374609 : && (flags & LOOKUP_NO_CONVERSION) == 0)
2305 : {
2306 81173606 : struct z_candidate *cand;
2307 :
2308 55747800 : if (CLASS_TYPE_P (to)
2309 55747750 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
2310 83493631 : && !CLASSTYPE_NON_AGGREGATE (complete_type (to)))
2311 1373100 : return build_aggr_conv (to, expr, flags, complain);
2312 :
2313 79800506 : cand = build_user_type_conversion_1 (to, expr, flags, complain);
2314 79800506 : if (cand)
2315 : {
2316 932639 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
2317 922874 : && CONSTRUCTOR_NELTS (expr) == 1
2318 59112 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr)
2319 15060874 : && !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 58712 : tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
2327 58712 : tree elttype = TREE_TYPE (elt);
2328 58712 : if (reference_related_p (to, elttype))
2329 : return implicit_conversion (to, elttype, elt,
2330 : c_cast_p, flags, complain);
2331 : }
2332 15001677 : 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 7354236 : good_conversion (tree to, tree from, tree expr,
2352 : int flags, tsubst_flags_t complain)
2353 : {
2354 7354236 : conversion *c = implicit_conversion (to, from, expr, /*cast*/false,
2355 : flags, complain);
2356 7354236 : if (c && c->bad_p)
2357 3173108 : c = NULL;
2358 7354236 : 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 1094599544 : 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 1094599544 : struct z_candidate *cand = (struct z_candidate *)
2374 1094599544 : conversion_obstack_alloc (sizeof (struct z_candidate));
2375 :
2376 1094599544 : cand->fn = fn;
2377 1094599544 : cand->first_arg = first_arg;
2378 1094599544 : cand->args = args;
2379 1094599544 : cand->convs = convs;
2380 1094599544 : cand->num_convs = num_convs;
2381 1094599544 : cand->access_path = access_path;
2382 1094599544 : cand->conversion_path = conversion_path;
2383 1094599544 : cand->viable = viable;
2384 1094599544 : cand->reason = reason;
2385 1094599544 : cand->next = *candidates;
2386 1094599544 : cand->flags = flags;
2387 1094599544 : *candidates = cand;
2388 :
2389 1094599544 : if (convs && cand->reversed ())
2390 : /* Swap the conversions for comparison in joust; we'll swap them back
2391 : before build_over_call. */
2392 89653367 : std::swap (convs[0], convs[1]);
2393 :
2394 1094599544 : 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 699440676 : add_ignored_candidate (z_candidate **candidates, tree fn)
2403 : {
2404 : /* No need to dynamically allocate these. */
2405 699440676 : static const rejection_reason reason_ignored = { rr_ignored, {} };
2406 :
2407 699440676 : struct z_candidate *cand = (struct z_candidate *)
2408 699435219 : conversion_obstack_alloc (sizeof (struct z_candidate));
2409 :
2410 699440676 : cand->fn = fn;
2411 699440676 : cand->reason = const_cast<rejection_reason *> (&reason_ignored);
2412 699440676 : cand->next = *candidates;
2413 699440676 : *candidates = cand;
2414 :
2415 699440676 : return cand;
2416 : }
2417 :
2418 : /* True iff CAND is a candidate added by add_ignored_candidate. */
2419 :
2420 : static bool
2421 609550099 : ignored_candidate_p (const z_candidate *cand)
2422 : {
2423 609543992 : 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 158948062 : remaining_arguments (tree arg)
2431 : {
2432 158948062 : int n;
2433 :
2434 276336059 : for (n = 0; arg != NULL_TREE && arg != void_list_node;
2435 117387997 : arg = TREE_CHAIN (arg))
2436 117387997 : n++;
2437 :
2438 158948062 : 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 455861269 : conv_flags (int i, int nargs, tree fn, tree arg, int flags)
2452 : {
2453 455861269 : int lflags = flags;
2454 455861269 : tree t;
2455 473459990 : if (i == 0 && nargs == 1 && DECL_CONSTRUCTOR_P (fn)
2456 151570643 : && (t = FUNCTION_FIRST_USER_PARMTYPE (fn))
2457 607431912 : && (same_type_ignoring_top_level_qualifiers_p
2458 151570643 : (non_reference (TREE_VALUE (t)), DECL_CONTEXT (fn))))
2459 : {
2460 117974058 : if (!(flags & LOOKUP_ONLYCONVERTING))
2461 35378759 : lflags |= LOOKUP_COPY_PARM;
2462 117974058 : if ((flags & LOOKUP_LIST_INIT_CTOR)
2463 117974058 : && BRACE_ENCLOSED_INITIALIZER_P (arg))
2464 1835 : lflags |= LOOKUP_NO_CONVERSION;
2465 : }
2466 : else
2467 337887211 : lflags |= LOOKUP_ONLYCONVERTING;
2468 :
2469 455861269 : 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 96142816 : build_this_conversion (tree fn, tree ctype,
2478 : tree& parmtype, tree& argtype, tree& arg,
2479 : int flags, tsubst_flags_t complain)
2480 : {
2481 192285632 : 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 96142816 : parmtype = cp_build_qualified_type (ctype,
2494 96142816 : cp_type_quals (TREE_TYPE (parmtype)));
2495 96142816 : bool this_p = true;
2496 96142816 : 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 168321 : bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn));
2501 168321 : 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 168321 : this_p = false;
2505 : }
2506 : else
2507 : {
2508 95974495 : 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 95974495 : arg = build_address (arg);
2513 95974495 : argtype = lvalue_type (arg);
2514 : }
2515 96142816 : flags |= LOOKUP_ONLYCONVERTING;
2516 96142816 : conversion *t = implicit_conversion (parmtype, argtype, arg,
2517 : /*c_cast_p=*/false, flags, complain);
2518 96142816 : t->this_p = this_p;
2519 96142816 : 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 646025027 : 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 646025027 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
2548 646025027 : int i, len;
2549 646025027 : tree parmnode;
2550 646025027 : tree orig_first_arg = first_arg;
2551 646025027 : int skip;
2552 646025027 : int viable = 1;
2553 646025027 : struct rejection_reason *reason = NULL;
2554 :
2555 : /* The `this', `in_chrg' and VTT arguments to constructors are not
2556 : considered in overload resolution. */
2557 1292050054 : if (DECL_CONSTRUCTOR_P (fn))
2558 : {
2559 294828542 : 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 294828467 : parmlist = skip_artificial_parms_for (fn, parmlist);
2564 294828542 : skip = num_artificial_parms_for (fn);
2565 294828542 : if (skip > 0 && first_arg != NULL_TREE)
2566 : {
2567 294828542 : --skip;
2568 294828542 : first_arg = NULL_TREE;
2569 : }
2570 : }
2571 : else
2572 : skip = 0;
2573 :
2574 1250525077 : len = vec_safe_length (args) - skip + (first_arg != NULL_TREE ? 1 : 0);
2575 646025027 : if (!convs)
2576 555621142 : 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 646025027 : parmnode = parmlist;
2586 1320754516 : for (i = 0; i < len; ++i)
2587 : {
2588 753448009 : if (parmnode == NULL_TREE || parmnode == void_list_node)
2589 : break;
2590 674729489 : parmnode = TREE_CHAIN (parmnode);
2591 : }
2592 :
2593 646025027 : if ((i < len && parmnode)
2594 646025027 : || !sufficient_parms_p (parmnode))
2595 : {
2596 158943191 : int remaining = remaining_arguments (parmnode);
2597 158943191 : viable = 0;
2598 158943191 : 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 589080361 : if (viable && len == 1 && parmlist && DECL_CONSTRUCTOR_P (fn)
2607 154034818 : && flag_new_inheriting_ctors
2608 800051160 : && DECL_INHERITED_CTOR (fn))
2609 : {
2610 1066730 : tree ptype = non_reference (TREE_VALUE (parmlist));
2611 1066730 : tree dtype = DECL_CONTEXT (fn);
2612 2133460 : tree btype = DECL_INHERITED_CTOR_BASE (fn);
2613 1066730 : if (reference_related_p (ptype, dtype)
2614 1066730 : && reference_related_p (btype, ptype))
2615 : {
2616 807777 : viable = false;
2617 807777 : reason = inherited_ctor_rejection ();
2618 : }
2619 : }
2620 :
2621 : /* Second, for a function to be viable, its constraints must be
2622 : satisfied. */
2623 646025027 : if (flag_concepts && viable && !constraints_satisfied_p (fn))
2624 : {
2625 199162 : reason = constraint_failure ();
2626 199162 : 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 646025027 : if (viable && parmlist && (flags & LOOKUP_DEFAULTED))
2633 : {
2634 80159942 : if (DECL_CONSTRUCTOR_P (fn))
2635 : i = 1;
2636 20873293 : else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2637 20873293 : && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
2638 : i = 2;
2639 : else
2640 : i = 0;
2641 40079971 : if (i && len == i)
2642 : {
2643 22119062 : parmnode = chain_index (i-1, parmlist);
2644 22119062 : if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
2645 : ctype))
2646 4477507 : viable = 0;
2647 : }
2648 :
2649 : /* This only applies at the top level. */
2650 35602464 : flags &= ~LOOKUP_DEFAULTED;
2651 : }
2652 :
2653 646025027 : if (! viable)
2654 164427637 : goto out;
2655 :
2656 481597390 : if (shortcut_bad_convs)
2657 481463158 : flags |= LOOKUP_SHORTCUT_BAD_CONVS;
2658 : else
2659 134232 : 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 481597390 : parmnode = parmlist;
2666 :
2667 827718249 : for (i = 0; i < len; ++i)
2668 : {
2669 527346159 : tree argtype, to_type;
2670 527346159 : tree arg;
2671 :
2672 527346159 : if (parmnode == void_list_node)
2673 : break;
2674 :
2675 527346159 : if (convs[i])
2676 : {
2677 : /* Already set during deduction. */
2678 6497637 : parmnode = TREE_CHAIN (parmnode);
2679 6497637 : continue;
2680 : }
2681 :
2682 520848522 : if (i == 0 && first_arg != NULL_TREE)
2683 92028476 : arg = first_arg;
2684 : else
2685 428820046 : arg = const_cast<tree> (
2686 820927372 : (*args)[i + skip - (first_arg != NULL_TREE ? 1 : 0)]);
2687 520848522 : argtype = lvalue_type (arg);
2688 :
2689 520848522 : conversion *t;
2690 520848522 : if (parmnode)
2691 : {
2692 515191366 : tree parmtype = TREE_VALUE (parmnode);
2693 515191366 : if (i == 0
2694 415830548 : && DECL_IOBJ_MEMBER_FUNCTION_P (fn)
2695 1004360142 : && !DECL_CONSTRUCTOR_P (fn))
2696 92014939 : t = build_this_conversion (fn, ctype, parmtype, argtype, arg,
2697 : flags, complain);
2698 : else
2699 : {
2700 423176427 : int lflags = conv_flags (i, len-skip, fn, arg, flags);
2701 423176427 : t = implicit_conversion (parmtype, argtype, arg,
2702 : /*c_cast_p=*/false, lflags, complain);
2703 : }
2704 515191366 : to_type = parmtype;
2705 515191366 : parmnode = TREE_CHAIN (parmnode);
2706 : }
2707 : else
2708 : {
2709 5657156 : t = build_identity_conv (argtype, arg);
2710 5657156 : t->ellipsis_p = true;
2711 5657156 : to_type = argtype;
2712 : }
2713 :
2714 520848522 : convs[i] = t;
2715 520848522 : if (! t)
2716 : {
2717 158627130 : viable = 0;
2718 158627130 : reason = arg_conversion_rejection (first_arg, i, argtype, to_type,
2719 158627130 : EXPR_LOCATION (arg));
2720 158627130 : break;
2721 : }
2722 :
2723 362221392 : if (t->bad_p)
2724 : {
2725 22630975 : viable = -1;
2726 22630975 : reason = bad_arg_conversion_rejection (first_arg, i, arg, to_type,
2727 22630975 : EXPR_LOCATION (arg));
2728 22630975 : if (shortcut_bad_convs)
2729 : break;
2730 : }
2731 : }
2732 :
2733 300372090 : out:
2734 646025027 : return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
2735 646025027 : 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 119525 : 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 119525 : tree totype = TREE_TYPE (TREE_TYPE (fn));
2757 119525 : int i, len, viable, flags;
2758 119525 : tree parmlist, parmnode;
2759 119525 : conversion **convs;
2760 119525 : struct rejection_reason *reason;
2761 :
2762 239271 : for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
2763 119746 : parmlist = TREE_TYPE (parmlist);
2764 119525 : parmlist = TYPE_ARG_TYPES (parmlist);
2765 :
2766 119525 : len = vec_safe_length (arglist) + 1;
2767 119525 : convs = alloc_conversions (len);
2768 119525 : parmnode = parmlist;
2769 119525 : viable = 1;
2770 119525 : flags = LOOKUP_IMPLICIT;
2771 119525 : reason = NULL;
2772 :
2773 : /* Don't bother looking up the same type twice. */
2774 119525 : if (*candidates && (*candidates)->fn == totype)
2775 : return NULL;
2776 :
2777 119510 : 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 380211 : for (i = 0; i < len; ++i)
2786 : {
2787 261010 : tree arg, argtype, convert_type = NULL_TREE;
2788 261010 : conversion *t;
2789 :
2790 261010 : if (i == 0)
2791 : arg = obj;
2792 : else
2793 141509 : arg = (*arglist)[i - 1];
2794 261010 : argtype = lvalue_type (arg);
2795 :
2796 261010 : if (i == 0)
2797 : {
2798 119501 : t = build_identity_conv (argtype, NULL_TREE);
2799 119501 : 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 119501 : convert_type = totype;
2803 : }
2804 141509 : else if (parmnode == void_list_node)
2805 : break;
2806 141282 : else if (parmnode)
2807 : {
2808 141273 : t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
2809 : /*c_cast_p=*/false, flags, complain);
2810 141273 : 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 260783 : convs[i] = t;
2820 260783 : if (! t)
2821 : break;
2822 :
2823 260710 : 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 260710 : if (i == 0)
2831 119501 : continue;
2832 :
2833 141209 : if (parmnode)
2834 141200 : parmnode = TREE_CHAIN (parmnode);
2835 : }
2836 :
2837 119501 : if (i < len
2838 119501 : || ! 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 119501 : return add_candidate (candidates, totype, obj, arglist, len, convs,
2846 119501 : access_path, conversion_path, viable, reason, flags);
2847 : }
2848 :
2849 : static void
2850 9736104 : 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 9736104 : conversion *t;
2855 9736104 : conversion **convs;
2856 9736104 : size_t num_convs;
2857 9736104 : int viable = 1;
2858 9736104 : tree types[2];
2859 9736104 : struct rejection_reason *reason = NULL;
2860 :
2861 9736104 : types[0] = type1;
2862 9736104 : types[1] = type2;
2863 :
2864 9736104 : num_convs = args.length ();
2865 9736104 : 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 9736104 : if (type1 != boolean_type_node)
2873 9165167 : flags |= LOOKUP_ONLYCONVERTING;
2874 :
2875 28466621 : for (unsigned i = 0; i < 2 && i < num_convs; ++i)
2876 : {
2877 18730517 : t = implicit_conversion (types[i], argtypes[i], args[i],
2878 : /*c_cast_p=*/false, flags, complain);
2879 18730517 : if (! t)
2880 : {
2881 1928014 : viable = 0;
2882 : /* We need something for printing the candidate. */
2883 1928014 : t = build_identity_conv (types[i], NULL_TREE);
2884 1928014 : reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2885 1928014 : types[i], EXPR_LOCATION (args[i]));
2886 : }
2887 16802503 : 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 18730517 : convs[i] = t;
2895 : }
2896 :
2897 : /* For COND_EXPR we rearranged the arguments; undo that now. */
2898 9736104 : 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 9736104 : 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 9736104 : }
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 14292058 : 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 14292058 : 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 14292058 : 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 113250 : case INDIRECT_REF:
3023 113250 : if (TYPE_PTR_P (type1)
3024 113250 : && (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 211781 : case NEGATE_EXPR:
3042 211781 : 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 160697 : case BIT_NOT_EXPR:
3051 160697 : 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 206884 : case MINUS_EXPR:
3131 206884 : if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
3132 : break;
3133 13 : if (TYPE_PTROB_P (type1)
3134 206870 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3135 : {
3136 4 : type2 = ptrdiff_type_node;
3137 4 : break;
3138 : }
3139 : /* FALLTHRU */
3140 899904 : case MULT_EXPR:
3141 899904 : case TRUNC_DIV_EXPR:
3142 899904 : 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 6959568 : case SPACESHIP_EXPR:
3151 6959568 : case EQ_EXPR:
3152 6959568 : case NE_EXPR:
3153 222199 : if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
3154 7181767 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)))
3155 : break;
3156 6959534 : if (NULLPTR_TYPE_P (type1) && NULLPTR_TYPE_P (type2))
3157 : break;
3158 6959528 : if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (args[1]))
3159 : {
3160 : type2 = type1;
3161 : break;
3162 : }
3163 6959525 : if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (args[0]))
3164 : {
3165 : type1 = type2;
3166 : break;
3167 : }
3168 : /* Fall through. */
3169 7693093 : case LT_EXPR:
3170 7693093 : case GT_EXPR:
3171 7693093 : case LE_EXPR:
3172 7693093 : case GE_EXPR:
3173 7693093 : case MAX_EXPR:
3174 7693093 : case MIN_EXPR:
3175 7693093 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3176 : break;
3177 5974598 : if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3178 : break;
3179 5970885 : if (TREE_CODE (type1) == ENUMERAL_TYPE
3180 4318651 : && TREE_CODE (type2) == ENUMERAL_TYPE)
3181 : break;
3182 2811730 : if (TYPE_PTR_P (type1)
3183 2811730 : && null_ptr_cst_p (args[1]))
3184 : {
3185 : type2 = type1;
3186 : break;
3187 : }
3188 2811711 : if (null_ptr_cst_p (args[0])
3189 2811711 : && TYPE_PTR_P (type2))
3190 : {
3191 : type1 = type2;
3192 : break;
3193 : }
3194 : return;
3195 :
3196 249210 : case PLUS_EXPR:
3197 249210 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3198 : break;
3199 : /* FALLTHRU */
3200 183344 : case ARRAY_REF:
3201 183344 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
3202 : {
3203 6 : type1 = ptrdiff_type_node;
3204 6 : break;
3205 : }
3206 183338 : if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3207 : {
3208 37705 : type2 = ptrdiff_type_node;
3209 37705 : 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 3104714 : case TRUNC_MOD_EXPR:
3225 3104714 : case BIT_AND_EXPR:
3226 3104714 : case BIT_IOR_EXPR:
3227 3104714 : case BIT_XOR_EXPR:
3228 3104714 : case LSHIFT_EXPR:
3229 3104714 : case RSHIFT_EXPR:
3230 3104714 : 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 1803877 : case MODIFY_EXPR:
3271 1803877 : switch (code2)
3272 : {
3273 103844 : case PLUS_EXPR:
3274 103844 : case MINUS_EXPR:
3275 103844 : 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 103872 : case MULT_EXPR:
3282 103872 : case TRUNC_DIV_EXPR:
3283 103872 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3284 : break;
3285 : return;
3286 :
3287 1700005 : case TRUNC_MOD_EXPR:
3288 1700005 : case BIT_AND_EXPR:
3289 1700005 : case BIT_IOR_EXPR:
3290 1700005 : case BIT_XOR_EXPR:
3291 1700005 : case LSHIFT_EXPR:
3292 1700005 : case RSHIFT_EXPR:
3293 1700005 : 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 1455161 : type1 = build_reference_type (type1);
3316 1455161 : 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 9165042 : bool u1 = uses_template_parms (type1);
3361 9165042 : bool u2 = type2 ? uses_template_parms (type2) : false;
3362 9165029 : 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 9165032 : 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 8925498 : if (type2 && !same_type_p (type1, type2)
3384 1944375 : && TREE_CODE (type1) == TREE_CODE (type2)
3385 9616242 : && (TYPE_REF_P (type1)
3386 451210 : || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3387 451094 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
3388 451076 : || TYPE_PTRMEMFUNC_P (type1)
3389 451076 : || MAYBE_CLASS_TYPE_P (type1)
3390 451076 : || 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 9164803 : build_builtin_candidate
3417 9164803 : (candidates, fnname, type1, type2, args, argtypes, flags, complain);
3418 : }
3419 :
3420 : tree
3421 863342884 : type_decays_to (tree type)
3422 : {
3423 863342884 : if (TREE_CODE (type) == ARRAY_TYPE)
3424 7251576 : return build_pointer_type (TREE_TYPE (type));
3425 856091308 : if (TREE_CODE (type) == FUNCTION_TYPE)
3426 37646 : 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 24810098 : 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 24810098 : int ref1;
3450 24810098 : int enum_p = 0;
3451 24810098 : 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 24810098 : vec<tree, va_gc> *types[2];
3455 24810098 : unsigned ix;
3456 24810098 : vec<tree, va_gc> &args = *argv;
3457 24810098 : unsigned len = args.length ();
3458 :
3459 69378165 : for (unsigned i = 0; i < len; ++i)
3460 : {
3461 44568067 : if (args[i])
3462 44568067 : argtypes[i] = unlowered_expr_type (args[i]);
3463 : else
3464 : argtypes[i] = NULL_TREE;
3465 : }
3466 :
3467 24810098 : 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 502157 : case TRUTH_NOT_EXPR:
3488 502157 : build_builtin_candidate
3489 502157 : (candidates, fnname, boolean_type_node,
3490 : NULL_TREE, args, argtypes, flags, complain);
3491 13907891 : return;
3492 :
3493 68780 : case TRUTH_ORIF_EXPR:
3494 68780 : case TRUTH_ANDIF_EXPR:
3495 68780 : build_builtin_candidate
3496 68780 : (candidates, fnname, boolean_type_node,
3497 : boolean_type_node, args, argtypes, flags, complain);
3498 68780 : return;
3499 :
3500 : case ADDR_EXPR:
3501 : case COMPOUND_EXPR:
3502 : case COMPONENT_REF:
3503 : case CO_AWAIT_EXPR:
3504 : return;
3505 :
3506 6535665 : case COND_EXPR:
3507 6535665 : case EQ_EXPR:
3508 6535665 : case NE_EXPR:
3509 6535665 : case LT_EXPR:
3510 6535665 : case LE_EXPR:
3511 6535665 : case GT_EXPR:
3512 6535665 : case GE_EXPR:
3513 6535665 : case SPACESHIP_EXPR:
3514 6535665 : enum_p = 1;
3515 : /* Fall through. */
3516 :
3517 : default:
3518 : ref1 = 0;
3519 : }
3520 :
3521 22056442 : types[0] = make_tree_vector ();
3522 22056442 : types[1] = make_tree_vector ();
3523 :
3524 22056442 : if (len == 3)
3525 738 : len = 2;
3526 45207634 : for (unsigned i = 0; i < len; ++i)
3527 : {
3528 33803270 : if (MAYBE_CLASS_TYPE_P (argtypes[i]))
3529 : {
3530 15254320 : tree convs;
3531 :
3532 15254320 : if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
3533 : return;
3534 :
3535 11656647 : convs = lookup_conversions (argtypes[i]);
3536 :
3537 11656647 : 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 11655352 : else if (! convs)
3546 : return;
3547 :
3548 11341569 : for (; convs; convs = TREE_CHAIN (convs))
3549 : {
3550 6739327 : type = TREE_TYPE (convs);
3551 :
3552 8340245 : if (i == 0 && ref1
3553 6739327 : && (!TYPE_REF_P (type)
3554 2599 : || CP_TYPE_CONST_P (TREE_TYPE (type))))
3555 1600918 : continue;
3556 :
3557 5138409 : if (code == COND_EXPR && TYPE_REF_P (type))
3558 24 : vec_safe_push (types[i], type);
3559 :
3560 5138409 : type = non_reference (type);
3561 5138409 : if (i != 0 || ! ref1)
3562 : {
3563 5138370 : type = cv_unqualified (type_decays_to (type));
3564 5138370 : if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
3565 656 : vec_safe_push (types[i], type);
3566 5138370 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3567 2897853 : type = type_promotes_to (type);
3568 : }
3569 :
3570 5138409 : if (! vec_member (type, types[i]))
3571 5130134 : vec_safe_push (types[i], type);
3572 : }
3573 : }
3574 : else
3575 : {
3576 18548950 : if (code == COND_EXPR && lvalue_p (args[i]))
3577 95 : vec_safe_push (types[i], build_reference_type (argtypes[i]));
3578 18548950 : type = non_reference (argtypes[i]);
3579 18548950 : if (i != 0 || ! ref1)
3580 : {
3581 16745198 : type = cv_unqualified (type_decays_to (type));
3582 16745198 : if (enum_p && UNSCOPED_ENUM_P (type))
3583 1955642 : vec_safe_push (types[i], type);
3584 16745198 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3585 9270664 : type = type_promotes_to (type);
3586 : }
3587 18548950 : 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 23370749 : FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t)
3594 : {
3595 11966385 : unsigned jx;
3596 11966385 : tree u;
3597 :
3598 11966385 : if (!types[1]->is_empty ())
3599 25772631 : FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u)
3600 13806246 : add_builtin_candidate
3601 13806246 : (candidates, code, code2, fnname, t,
3602 : u, args, argtypes, flags, complain);
3603 : else
3604 485812 : add_builtin_candidate
3605 485812 : (candidates, code, code2, fnname, t,
3606 : NULL_TREE, args, argtypes, flags, complain);
3607 : }
3608 :
3609 11404364 : release_tree_vector (types[0]);
3610 11404364 : 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 529141805 : 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 529141805 : int ntparms = DECL_NTPARMS (tmpl);
3635 529141805 : tree targs = make_tree_vec (ntparms);
3636 529141805 : unsigned int len = vec_safe_length (arglist);
3637 529141805 : unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
3638 529141805 : unsigned int skip_without_in_chrg = 0;
3639 529141805 : tree first_arg_without_in_chrg = first_arg;
3640 529141805 : tree *args_without_in_chrg;
3641 529141805 : unsigned int nargs_without_in_chrg;
3642 529141805 : unsigned int ia, ix;
3643 529141805 : tree arg;
3644 529141805 : struct z_candidate *cand;
3645 529141805 : tree fn;
3646 529141805 : struct rejection_reason *reason = NULL;
3647 529141805 : int errs;
3648 529141805 : conversion **convs = NULL;
3649 :
3650 : /* We don't do deduction on the in-charge parameter, the VTT
3651 : parameter or 'this'. */
3652 529141805 : if (DECL_IOBJ_MEMBER_FUNCTION_P (tmpl))
3653 : {
3654 53858164 : 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 529141805 : ++skip_without_in_chrg;
3661 : }
3662 :
3663 529141805 : if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
3664 529141805 : || DECL_BASE_CONSTRUCTOR_P (tmpl))
3665 530144766 : && 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 529141805 : if (len < skip_without_in_chrg)
3674 0 : return add_ignored_candidate (candidates, tmpl);
3675 :
3676 578299830 : if (DECL_CONSTRUCTOR_P (tmpl) && nargs == 2
3677 572935978 : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (first_arg),
3678 43794173 : 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 2365142 : if (tree firstparm = FUNCTION_FIRST_USER_PARMTYPE (tmpl))
3691 : {
3692 2365142 : firstparm = TREE_VALUE (firstparm);
3693 2365142 : if (PACK_EXPANSION_P (firstparm))
3694 2122 : firstparm = PACK_EXPANSION_PATTERN (firstparm);
3695 2365142 : if (TREE_CODE (firstparm) == TEMPLATE_TYPE_PARM)
3696 : {
3697 758041 : gcc_assert (!explicit_targs);
3698 758041 : reason = invalid_copy_with_fn_template_rejection ();
3699 758041 : goto fail;
3700 : }
3701 : }
3702 : }
3703 :
3704 528383764 : nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
3705 528383764 : + (len - skip_without_in_chrg));
3706 528383764 : args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
3707 528383764 : ia = 0;
3708 528383764 : if (first_arg_without_in_chrg != NULL_TREE)
3709 : {
3710 10151 : args_without_in_chrg[ia] = first_arg_without_in_chrg;
3711 10151 : ++ia;
3712 : }
3713 528383764 : for (ix = skip_without_in_chrg;
3714 1414500893 : vec_safe_iterate (arglist, ix, &arg);
3715 : ++ix)
3716 : {
3717 886117129 : args_without_in_chrg[ia] = arg;
3718 886117129 : ++ia;
3719 : }
3720 528383764 : gcc_assert (ia == nargs_without_in_chrg);
3721 :
3722 528383764 : 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 528383752 : int min_arity = 0, max_arity = 0;
3730 528383752 : tree parms = TYPE_ARG_TYPES (TREE_TYPE (tmpl));
3731 528383752 : parms = skip_artificial_parms_for (tmpl, parms);
3732 1984823392 : for (; parms != void_list_node; parms = TREE_CHAIN (parms))
3733 : {
3734 1862300267 : if (!parms || PACK_EXPANSION_P (TREE_VALUE (parms)))
3735 : {
3736 : max_arity = -1;
3737 : break;
3738 : }
3739 928055888 : if (TREE_PURPOSE (parms))
3740 : /* A parameter with a default argument. */
3741 10317931 : ++max_arity;
3742 : else
3743 917737957 : ++min_arity, ++max_arity;
3744 : }
3745 528383752 : if (ia < (unsigned)min_arity)
3746 : {
3747 : /* Too few arguments. */
3748 34957794 : reason = arity_rejection (NULL_TREE, min_arity, ia,
3749 : /*least_p=*/(max_arity == -1));
3750 34957794 : goto fail;
3751 : }
3752 493425958 : else if (max_arity != -1 && ia > (unsigned)max_arity)
3753 : {
3754 : /* Too many arguments. */
3755 5175639 : reason = arity_rejection (NULL_TREE, max_arity, ia);
3756 5175639 : goto fail;
3757 : }
3758 :
3759 488250319 : convs = alloc_conversions (nargs);
3760 :
3761 488250319 : if (shortcut_bad_convs
3762 488240859 : && DECL_IOBJ_MEMBER_FUNCTION_P (tmpl)
3763 563698559 : && !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 4127877 : tree parmtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (tmpl)));
3770 4127877 : tree argtype = lvalue_type (first_arg);
3771 4127877 : tree arg = first_arg;
3772 4127877 : conversion *t = build_this_conversion (tmpl, ctype,
3773 : parmtype, argtype, arg,
3774 : flags, complain);
3775 4127877 : convs[0] = t;
3776 4127877 : if (t->bad_p)
3777 : {
3778 39679 : reason = bad_arg_conversion_rejection (first_arg, 0,
3779 : arg, parmtype,
3780 39679 : EXPR_LOCATION (arg));
3781 39679 : goto fail;
3782 : }
3783 : }
3784 : }
3785 :
3786 488210652 : errs = errorcount+sorrycount;
3787 976407753 : 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 488210652 : false, complain & tf_decltype);
3792 :
3793 488197101 : if (fn == error_mark_node)
3794 : {
3795 : /* Don't repeat unification later if it already resulted in errors. */
3796 397786906 : if (errorcount+sorrycount == errs)
3797 397786790 : 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 397786906 : 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 90410195 : if (((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
3810 : == LOOKUP_ONLYCONVERTING)
3811 90410195 : && DECL_NONCONVERTING_P (fn))
3812 5457 : return add_ignored_candidate (candidates, fn);
3813 :
3814 180809476 : if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
3815 : {
3816 4840929 : tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
3817 9681831 : 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 844 : reason = invalid_copy_with_fn_template_rejection ();
3824 844 : goto fail;
3825 : }
3826 : }
3827 :
3828 90403894 : 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 90403885 : cand = add_function_candidate (candidates, fn, ctype,
3834 : first_arg, arglist, access_path,
3835 : conversion_path, flags, convs,
3836 : shortcut_bad_convs, complain);
3837 90403894 : 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 5572341 : cand->template_decl = build_template_info (tmpl, targs);
3856 : else
3857 84831553 : cand->template_decl = DECL_TEMPLATE_INFO (fn);
3858 90403894 : cand->explicit_targs = explicit_targs;
3859 :
3860 90403894 : return cand;
3861 438718903 : fail:
3862 438718903 : int viable = (reason->code == rr_bad_arg_conversion ? -1 : 0);
3863 438718903 : return add_candidate (candidates, tmpl, first_arg, arglist, nargs, convs,
3864 438718903 : access_path, conversion_path, viable, reason, flags);
3865 : }
3866 :
3867 :
3868 : static struct z_candidate *
3869 529141793 : 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 529141793 : 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 330104260 : splice_viable (struct z_candidate *cands,
3915 : bool strict_p,
3916 : bool *any_viable_p)
3917 : {
3918 330104260 : z_candidate *strictly_viable = nullptr;
3919 330104260 : z_candidate **strictly_viable_tail = &strictly_viable;
3920 :
3921 330104260 : z_candidate *non_strictly_viable = nullptr;
3922 330104260 : z_candidate **non_strictly_viable_tail = &non_strictly_viable;
3923 :
3924 330104260 : z_candidate *non_viable = nullptr;
3925 330104260 : z_candidate **non_viable_tail = &non_viable;
3926 :
3927 330104260 : z_candidate *non_viable_ignored = nullptr;
3928 330104260 : 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 330104260 : if (processing_template_decl)
3933 58413407 : strict_p = true;
3934 :
3935 1257387441 : for (z_candidate *cand = cands; cand; cand = cand->next)
3936 : {
3937 927283181 : if (!strict_p
3938 323848078 : && (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 827662813 : strict_p = true;
3943 :
3944 : /* Move this candidate to the appropriate list according to
3945 : its viability. */
3946 927283181 : auto& tail = (cand->viable == 1 ? strictly_viable_tail
3947 : : cand->viable == -1 ? non_strictly_viable_tail
3948 1479344648 : : ignored_candidate_p (cand) ? non_viable_ignored_tail
3949 1536811345 : : non_viable_tail);
3950 927283181 : *tail = cand;
3951 927283181 : tail = &cand->next;
3952 : }
3953 :
3954 660208520 : *any_viable_p = (strictly_viable != nullptr
3955 330104260 : || (!strict_p && non_strictly_viable != nullptr));
3956 :
3957 : /* Combine the lists. */
3958 330104260 : *non_viable_ignored_tail = nullptr;
3959 330104260 : *non_viable_tail = non_viable_ignored;
3960 330104260 : *non_strictly_viable_tail = non_viable;
3961 330104260 : *strictly_viable_tail = non_strictly_viable;
3962 :
3963 330104260 : return strictly_viable;
3964 : }
3965 :
3966 : static bool
3967 316240511 : any_strictly_viable (struct z_candidate *cands)
3968 : {
3969 432551891 : for (; cands; cands = cands->next)
3970 125479602 : 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 68904091 : 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 68904091 : if (processing_template_decl)
3985 76771 : return build_address (obj);
3986 :
3987 68827320 : 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 4896344 : equal_functions (tree fn1, tree fn2)
3996 : {
3997 4896344 : if (TREE_CODE (fn1) != TREE_CODE (fn2))
3998 : return 0;
3999 4880983 : if (TREE_CODE (fn1) == TEMPLATE_DECL)
4000 36051 : return fn1 == fn2;
4001 9689846 : if (DECL_LOCAL_DECL_P (fn1) || DECL_LOCAL_DECL_P (fn2)
4002 9689843 : || DECL_EXTERN_C_FUNCTION_P (fn1))
4003 3023185 : return decls_match (fn1, fn2);
4004 1821747 : 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 1469 : print_arity_information (location_t loc, unsigned int have, unsigned int want,
4056 : bool least_p)
4057 : {
4058 1469 : 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 1428 : inform_n (loc, want,
4065 : "candidate expects %d argument, %d provided",
4066 : "candidate expects %d arguments, %d provided",
4067 : want, have);
4068 1469 : }
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 13335 : print_z_candidate (location_t loc, const char *msgstr,
4079 : struct z_candidate *candidate)
4080 : {
4081 13335 : const char *msg = (msgstr == NULL
4082 13335 : ? ""
4083 13335 : : ACONCAT ((_(msgstr), " ", NULL)));
4084 13335 : tree fn = candidate->fn;
4085 13335 : if (flag_new_inheriting_ctors)
4086 13308 : fn = strip_inheriting_ctors (fn);
4087 13335 : location_t cloc = location_of (fn);
4088 :
4089 13335 : 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 13121 : else if (TYPE_P (fn))
4106 15 : inform (cloc, "%s%qT (conversion)", msg, fn);
4107 13106 : else if (candidate->viable == -1)
4108 4392 : inform (cloc, "%s%#qD (near match)", msg, fn);
4109 8714 : else if (ignored_candidate_p (candidate))
4110 6 : inform (cloc, "%s%#qD (ignored)", msg, fn);
4111 8708 : else if (DECL_DELETED_FN (fn))
4112 60 : inform (cloc, "%s%#qD (deleted)", msg, fn);
4113 8648 : else if (candidate->reversed ())
4114 300 : inform (cloc, "%s%#qD (reversed)", msg, fn);
4115 8348 : else if (candidate->rewritten ())
4116 150 : inform (cloc, "%s%#qD (rewritten)", msg, fn);
4117 : else
4118 8198 : inform (cloc, "%s%#qD", msg, fn);
4119 :
4120 13335 : auto_diagnostic_nesting_level sentinel;
4121 :
4122 13335 : 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 13335 : if (candidate->reason != NULL)
4130 : {
4131 10220 : struct rejection_reason *r = candidate->reason;
4132 :
4133 10220 : switch (r->code)
4134 : {
4135 1469 : case rr_arity:
4136 1469 : print_arity_information (cloc, r->u.arity.actual,
4137 1469 : r->u.arity.expected,
4138 : r->u.arity.least_p);
4139 1469 : 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 13335 : }
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 5576 : print_z_candidates (location_t loc, struct z_candidate *candidates,
4214 : tristate only_viable_p /* = tristate::unknown () */)
4215 : {
4216 5576 : struct z_candidate *cand1;
4217 5576 : struct z_candidate **cand2;
4218 :
4219 5576 : if (!candidates)
4220 1011 : return;
4221 :
4222 : /* Remove non-viable deleted candidates. */
4223 4565 : cand1 = candidates;
4224 20477 : for (cand2 = &cand1; *cand2; )
4225 : {
4226 15912 : if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
4227 11447 : && !(*cand2)->viable
4228 19673 : && DECL_DELETED_FN ((*cand2)->fn))
4229 102 : *cand2 = (*cand2)->next;
4230 : else
4231 15810 : cand2 = &(*cand2)->next;
4232 : }
4233 : /* ...if there are any non-deleted ones. */
4234 4565 : if (cand1)
4235 4559 : 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 20075 : for (cand1 = candidates; cand1; cand1 = cand1->next)
4243 : {
4244 15510 : tree fn = cand1->fn;
4245 : /* Skip builtin candidates and conversion functions. */
4246 15510 : if (!DECL_P (fn))
4247 289 : continue;
4248 15221 : cand2 = &cand1->next;
4249 143379 : 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 4565 : if (only_viable_p.is_unknown ())
4264 4556 : only_viable_p = candidates->viable == 1;
4265 :
4266 4565 : auto_diagnostic_nesting_level sentinel;
4267 :
4268 4565 : int num_candidates = 0;
4269 22355 : for (auto iter = candidates; iter; iter = iter->next)
4270 : {
4271 13566 : if (only_viable_p.is_true () && iter->viable != 1)
4272 : break;
4273 13225 : ++num_candidates;
4274 : }
4275 :
4276 4565 : inform_num_candidates (loc, num_candidates);
4277 :
4278 4565 : auto_diagnostic_nesting_level sentinel2;
4279 :
4280 4565 : int candidate_idx = 0;
4281 22320 : for (; candidates; candidates = candidates->next)
4282 : {
4283 13562 : if (only_viable_p.is_true () && candidates->viable != 1)
4284 : break;
4285 13258 : 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 13190 : pretty_printer pp;
4292 13190 : pp_printf (&pp, N_("candidate %i:"), candidate_idx + 1);
4293 13190 : const char *const msgstr = pp_formatted_text (&pp);
4294 13190 : print_z_candidate (loc, msgstr, candidates);
4295 13190 : ++candidate_idx;
4296 13190 : }
4297 4565 : }
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 15495869 : merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
4307 : {
4308 15495869 : conversion **t;
4309 15495869 : bool bad = user_seq->bad_p;
4310 :
4311 15495869 : gcc_assert (user_seq->kind == ck_user);
4312 :
4313 : /* Find the end of the second conversion sequence. */
4314 16278324 : for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
4315 : {
4316 : /* The entire sequence is a user-conversion sequence. */
4317 782455 : (*t)->user_conv_p = true;
4318 782455 : if (bad)
4319 3506 : (*t)->bad_p = true;
4320 : }
4321 :
4322 15495869 : 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 467034 : user_seq->type = TREE_TYPE (TREE_TYPE (user_seq->cand->fn));
4327 :
4328 : /* Replace the identity conversion with the user conversion
4329 : sequence. */
4330 15495869 : *t = user_seq;
4331 :
4332 15495869 : 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 2552838 : 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 2552838 : gcc_assert (*candidates == NULL);
4354 :
4355 : /* We're looking for a ctor for list-initialization. */
4356 2552838 : 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 2552838 : flags |= LOOKUP_NO_NARROWING;
4360 :
4361 5105676 : unsigned nart = num_artificial_parms_for (OVL_FIRST (fns)) - 1;
4362 2552838 : tree init_list = (*args)[nart];
4363 :
4364 : /* Always use the default constructor if the list is empty (DR 990). */
4365 2552838 : if (CONSTRUCTOR_NELTS (init_list) == 0
4366 2552838 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
4367 : ;
4368 2308881 : else if (CONSTRUCTOR_IS_DESIGNATED_INIT (init_list)
4369 2308881 : && !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 10347 : 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 2308828 : else if (TYPE_HAS_LIST_CTOR (totype))
4379 : {
4380 87107 : flags |= LOOKUP_LIST_ONLY;
4381 87107 : add_candidates (fns, first_arg, args, NULL_TREE,
4382 : explicit_targs, template_only, conversion_path,
4383 : access_path, flags, candidates, complain);
4384 174214 : if (any_strictly_viable (*candidates))
4385 : return;
4386 : }
4387 :
4388 : /* Expand the CONSTRUCTOR into a new argument vec. */
4389 2542491 : vec<tree, va_gc> *new_args;
4390 4840271 : vec_alloc (new_args, nart + CONSTRUCTOR_NELTS (init_list));
4391 5084985 : for (unsigned i = 0; i < nart; ++i)
4392 3 : new_args->quick_push ((*args)[i]);
4393 2542491 : new_args = append_ctor_to_tree_vector (new_args, init_list);
4394 :
4395 : /* We aren't looking for list-ctors anymore. */
4396 2542491 : flags &= ~LOOKUP_LIST_ONLY;
4397 : /* We allow more user-defined conversions within an init-list. */
4398 2542491 : flags &= ~LOOKUP_NO_CONVERSION;
4399 :
4400 2542491 : 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 1511 : list_ctor_element_type (tree fn)
4409 : {
4410 1511 : gcc_checking_assert (is_list_ctor (fn));
4411 :
4412 1511 : tree parm = FUNCTION_FIRST_USER_PARMTYPE (fn);
4413 1511 : parm = non_reference (TREE_VALUE (parm));
4414 1511 : 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 1585 : braced_init_element_type (tree expr)
4422 : {
4423 1585 : if (TREE_CODE (expr) == CONSTRUCTOR
4424 1585 : && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
4425 0 : return TREE_TYPE (TREE_TYPE (expr));
4426 1585 : if (!BRACE_ENCLOSED_INITIALIZER_P (expr))
4427 : return NULL_TREE;
4428 :
4429 1585 : tree elttype = NULL_TREE;
4430 15382 : for (constructor_elt &e: CONSTRUCTOR_ELTS (expr))
4431 : {
4432 10829 : tree type = TREE_TYPE (e.value);
4433 10829 : type = type_decays_to (type);
4434 10829 : if (!elttype)
4435 : elttype = type;
4436 9272 : 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 17703 : 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 17703 : if (!TYPE_NON_AGGREGATE_CLASS (elttype))
4485 : return NULL_TREE;
4486 1585 : tree init_elttype = braced_init_element_type (init);
4487 1585 : 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 15037361 : maybe_init_list_as_range (tree fn, tree expr)
4564 : {
4565 15037361 : if (!processing_template_decl
4566 14503576 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
4567 903504 : && is_list_ctor (fn)
4568 15039025 : && decl_in_std_namespace_p (fn))
4569 : {
4570 1511 : tree to = list_ctor_element_type (fn);
4571 1511 : 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 97765726 : build_user_type_conversion_1 (tree totype, tree expr, int flags,
4594 : tsubst_flags_t complain)
4595 : {
4596 97765726 : struct z_candidate *candidates, *cand;
4597 97765726 : tree fromtype;
4598 97765726 : tree ctors = NULL_TREE;
4599 97765726 : tree conv_fns = NULL_TREE;
4600 97765726 : conversion *conv = NULL;
4601 97765726 : tree first_arg = NULL_TREE;
4602 97765726 : vec<tree, va_gc> *args = NULL;
4603 97765726 : bool any_viable_p;
4604 97765726 : int convflags;
4605 :
4606 97765726 : if (!expr)
4607 : return NULL;
4608 :
4609 97765720 : 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 97765720 : gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
4615 : || !DERIVED_FROM_P (totype, fromtype));
4616 :
4617 97765720 : 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 54398863 : ctors = get_class_binding (totype, complete_ctor_identifier);
4621 :
4622 97765720 : tree to_nonref = non_reference (totype);
4623 97765720 : if (MAYBE_CLASS_TYPE_P (fromtype))
4624 : {
4625 67371831 : if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
4626 67362130 : (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
4627 54747040 : && 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 67362127 : conv_fns = lookup_conversions (fromtype);
4637 : }
4638 :
4639 97765720 : candidates = 0;
4640 97765720 : flags |= LOOKUP_NO_CONVERSION;
4641 97765720 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4642 946980 : flags |= LOOKUP_NO_NARROWING;
4643 : /* Prevent add_candidates from treating a non-strictly viable candidate
4644 : as unviable. */
4645 97765720 : 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 97765720 : convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION
4650 97765720 : | (flags & LOOKUP_NO_NARROWING));
4651 97765720 : flags &= ~LOOKUP_NO_TEMP_BIND;
4652 :
4653 97765720 : if (ctors)
4654 : {
4655 54179952 : int ctorflags = flags;
4656 :
4657 54179952 : first_arg = build_dummy_object (totype);
4658 :
4659 : /* We should never try to call the abstract or base constructor
4660 : from here. */
4661 380247198 : gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_FIRST (ctors))
4662 : && !DECL_HAS_VTT_PARM_P (OVL_FIRST (ctors)));
4663 :
4664 54179952 : args = make_tree_vector_single (expr);
4665 54179952 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4666 : {
4667 : /* List-initialization. */
4668 946980 : add_list_candidates (ctors, first_arg, args, totype, NULL_TREE,
4669 946980 : false, TYPE_BINFO (totype), TYPE_BINFO (totype),
4670 : ctorflags, &candidates, complain);
4671 : }
4672 : else
4673 : {
4674 53232972 : add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
4675 53232972 : TYPE_BINFO (totype), TYPE_BINFO (totype),
4676 : ctorflags, &candidates, complain);
4677 : }
4678 :
4679 298595479 : for (cand = candidates; cand; cand = cand->next)
4680 : {
4681 244415527 : 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 244415527 : if (!TYPE_REF_P (totype)
4692 244415527 : && cxx_dialect < cxx17
4693 437407 : && (flags & LOOKUP_ONLYCONVERTING)
4694 379188 : && !(convflags & LOOKUP_NO_TEMP_BIND))
4695 117636 : cand->second_conv
4696 117636 : = build_conv (ck_rvalue, totype, cand->second_conv);
4697 : }
4698 : }
4699 :
4700 97765720 : if (conv_fns)
4701 : {
4702 30344340 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4703 0 : first_arg = CONSTRUCTOR_ELT (expr, 0)->value;
4704 : else
4705 97765720 : first_arg = expr;
4706 : }
4707 :
4708 133107711 : for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
4709 : {
4710 35341991 : tree conversion_path = TREE_PURPOSE (conv_fns);
4711 35341991 : 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 35341991 : tree convtype = non_reference (TREE_TYPE (conv_fns));
4723 52083143 : if ((flags & LOOKUP_NO_CONVERSION)
4724 35341991 : && !WILDCARD_TYPE_P (convtype)
4725 67049232 : && (CLASS_TYPE_P (to_nonref)
4726 33524616 : != CLASS_TYPE_P (convtype)))
4727 16741152 : 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 18600839 : if (TYPE_REF_P (totype))
4734 5162460 : convflags |= LOOKUP_NO_TEMP_BIND;
4735 :
4736 18600839 : old_candidates = candidates;
4737 18600839 : add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
4738 : NULL_TREE, false,
4739 18600839 : conversion_path, TYPE_BINFO (fromtype),
4740 : flags, &candidates, complain);
4741 :
4742 37200965 : for (cand = candidates; cand != old_candidates; cand = cand->next)
4743 : {
4744 18600126 : if (cand->viable == 0)
4745 : /* Already rejected, don't change to -1. */
4746 7027039 : continue;
4747 :
4748 11573087 : tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
4749 11573087 : conversion *ics
4750 11573087 : = 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 4406169 : if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
4767 12286431 : && !(convflags & LOOKUP_NO_TEMP_BIND))
4768 260553 : ics = build_conv (ck_rvalue, totype, ics);
4769 :
4770 11573087 : cand->second_conv = ics;
4771 :
4772 11573087 : if (!ics)
4773 : {
4774 7166918 : cand->viable = 0;
4775 14331271 : cand->reason = arg_conversion_rejection (NULL_TREE, -2,
4776 : rettype, totype,
4777 7166918 : EXPR_LOCATION (expr));
4778 : }
4779 14639 : 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 4408060 : && 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 4404284 : else if (DECL_NONCONVERTING_P (cand->fn)
4790 4404284 : && 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 2747 : cand->viable = -1;
4800 2747 : cand->reason = explicit_conversion_rejection (rettype, totype);
4801 : }
4802 4401537 : else if (cand->viable == 1 && ics->bad_p)
4803 : {
4804 2579 : cand->viable = -1;
4805 2579 : cand->reason
4806 5158 : = bad_arg_conversion_rejection (NULL_TREE, -2,
4807 : rettype, totype,
4808 2579 : EXPR_LOCATION (expr));
4809 : }
4810 4398958 : else if (primary_template_specialization_p (cand->fn)
4811 4398958 : && 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 97765720 : candidates = splice_viable (candidates, false, &any_viable_p);
4824 97765720 : if (!any_viable_p)
4825 : {
4826 82727013 : if (args)
4827 42819691 : release_tree_vector (args);
4828 : return NULL;
4829 : }
4830 :
4831 15038707 : cand = tourney (candidates, complain);
4832 15038707 : 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 15037361 : 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 15037361 : tree convtype;
4868 30074722 : if (!DECL_CONSTRUCTOR_P (cand->fn))
4869 4391789 : convtype = non_reference (TREE_TYPE (TREE_TYPE (cand->fn)));
4870 10645572 : 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 12081 : convtype = cv_unqualified (totype);
4875 : else
4876 : convtype = totype;
4877 : /* Build the user conversion sequence. */
4878 15037361 : conv = build_conv
4879 15037361 : (ck_user,
4880 : convtype,
4881 15037361 : build_identity_conv (TREE_TYPE (expr), expr));
4882 15037361 : conv->cand = cand;
4883 15037361 : if (cand->viable == -1)
4884 11601 : conv->bad_p = true;
4885 :
4886 : /* Remember that this was a list-initialization. */
4887 15037361 : if (flags & LOOKUP_NO_NARROWING)
4888 3371580 : conv->check_narrowing = true;
4889 :
4890 : /* Combine it with the second conversion sequence. */
4891 15037361 : cand->second_conv = merge_conversion_sequences (conv,
4892 : cand->second_conv);
4893 :
4894 15037361 : return cand;
4895 : }
4896 :
4897 : /* Wrapper for above. */
4898 :
4899 : tree
4900 24375 : build_user_type_conversion (tree totype, tree expr, int flags,
4901 : tsubst_flags_t complain)
4902 : {
4903 24375 : struct z_candidate *cand;
4904 24375 : tree ret;
4905 :
4906 24375 : auto_cond_timevar tv (TV_OVERLOAD);
4907 :
4908 24375 : conversion_obstack_sentinel cos;
4909 :
4910 24375 : cand = build_user_type_conversion_1 (totype, expr, flags, complain);
4911 :
4912 24375 : if (cand)
4913 : {
4914 24145 : if (cand->second_conv->kind == ck_ambig)
4915 65 : ret = error_mark_node;
4916 : else
4917 : {
4918 24080 : expr = convert_like (cand->second_conv, expr, complain);
4919 24080 : ret = convert_from_reference (expr);
4920 : }
4921 : }
4922 : else
4923 : ret = NULL_TREE;
4924 :
4925 48750 : return ret;
4926 24375 : }
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 339762509 : build_converted_constant_expr_internal (tree type, tree expr,
4971 : int flags, tsubst_flags_t complain)
4972 : {
4973 339762509 : conversion *conv;
4974 339762509 : tree t;
4975 339762509 : location_t loc = cp_expr_loc_or_input_loc (expr);
4976 :
4977 339762509 : if (error_operand_p (expr))
4978 65 : return error_mark_node;
4979 :
4980 339762444 : conversion_obstack_sentinel cos;
4981 :
4982 339762444 : 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 339762444 : for (conversion *c = conv;
5003 391971010 : c && c->kind != ck_identity;
5004 52208566 : c = next_conversion (c))
5005 : {
5006 52208566 : 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 9474166 : case ck_base:
5035 9474166 : case ck_pmem:
5036 9474166 : case ck_ptr:
5037 9474166 : case ck_std:
5038 9474166 : t = next_conversion (c)->type;
5039 9474166 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (t)
5040 9474086 : && 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 339760918 : if (conv && conv->kind == ck_ref_bind
5061 339763205 : && 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 339761975 : if (conv)
5069 : {
5070 : /* Don't copy a class in a template. */
5071 67461 : if (CLASS_TYPE_P (type) && conv->kind == ck_rvalue
5072 339795643 : && processing_template_decl)
5073 393 : conv = next_conversion (conv);
5074 :
5075 : /* Issuing conversion warnings for value-dependent expressions is
5076 : likely too noisy. */
5077 339760449 : warning_sentinel w (warn_conversion);
5078 339760449 : conv->check_narrowing = true;
5079 339760449 : conv->check_narrowing_const_only = true;
5080 339760449 : expr = convert_like (conv, expr, complain);
5081 339760449 : }
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 339762444 : }
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 194401988 : build_converted_constant_expr (tree type, tree expr, tsubst_flags_t complain)
5108 : {
5109 194401988 : return build_converted_constant_expr_internal (type, expr, LOOKUP_IMPLICIT,
5110 194401988 : 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 145360521 : build_converted_constant_bool_expr (tree expr, tsubst_flags_t complain)
5119 : {
5120 145360521 : return build_converted_constant_expr_internal (boolean_type_node, expr,
5121 145360521 : LOOKUP_NORMAL, complain);
5122 : }
5123 :
5124 : /* Do any initial processing on the arguments to a function call. */
5125 :
5126 : vec<tree, va_gc> *
5127 211299231 : resolve_args (vec<tree, va_gc> *args, tsubst_flags_t complain)
5128 : {
5129 211299231 : unsigned int ix;
5130 211299231 : tree arg;
5131 :
5132 391321719 : FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
5133 : {
5134 180023241 : if (error_operand_p (arg))
5135 : return NULL;
5136 180022670 : 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 180022515 : 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 180022488 : 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 103804492 : 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 103804492 : struct z_candidate *cand;
5173 103804492 : tree explicit_targs;
5174 103804492 : int template_only;
5175 :
5176 103804492 : auto_cond_timevar tv (TV_OVERLOAD);
5177 :
5178 103804492 : explicit_targs = NULL_TREE;
5179 103804492 : template_only = 0;
5180 :
5181 103804492 : *candidates = NULL;
5182 103804492 : *any_viable_p = true;
5183 :
5184 : /* Check FN. */
5185 103804492 : gcc_assert (OVL_P (fn) || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
5186 :
5187 103804492 : if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
5188 : {
5189 36493821 : explicit_targs = TREE_OPERAND (fn, 1);
5190 36493821 : fn = TREE_OPERAND (fn, 0);
5191 36493821 : template_only = 1;
5192 : }
5193 :
5194 : /* Add the various candidate functions. */
5195 103804492 : 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 103790974 : *candidates = splice_viable (*candidates, false, any_viable_p);
5203 103790974 : if (*any_viable_p)
5204 : {
5205 103599680 : if (complain & tf_any_viable)
5206 : cand = *candidates;
5207 : else
5208 103599494 : cand = tourney (*candidates, complain);
5209 : }
5210 : else
5211 : cand = NULL;
5212 :
5213 207581948 : return cand;
5214 103790974 : }
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 3009 : print_error_for_call_failure (tree fn, const vec<tree, va_gc> *args,
5223 : struct z_candidate *candidates)
5224 : {
5225 3009 : tree targs = NULL_TREE;
5226 3009 : if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
5227 : {
5228 560 : targs = TREE_OPERAND (fn, 1);
5229 560 : fn = TREE_OPERAND (fn, 0);
5230 : }
5231 6018 : tree name = OVL_NAME (fn);
5232 3009 : location_t loc = location_of (name);
5233 3009 : if (targs)
5234 560 : name = lookup_template_function (name, targs);
5235 :
5236 3009 : auto_diagnostic_group d;
5237 6018 : if (!any_strictly_viable (candidates))
5238 2524 : 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 3009 : if (candidates)
5244 3009 : print_z_candidates (loc, candidates);
5245 3009 : }
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 31252 : perform_dguide_overload_resolution (tree dguides, const vec<tree, va_gc> *args,
5253 : tsubst_flags_t complain)
5254 : {
5255 31252 : z_candidate *candidates;
5256 31252 : bool any_viable_p;
5257 31252 : tree result;
5258 :
5259 62504 : gcc_assert (deduction_guide_p (OVL_FIRST (dguides)));
5260 :
5261 31252 : conversion_obstack_sentinel cos;
5262 :
5263 31252 : z_candidate *cand = perform_overload_resolution (dguides, args, &candidates,
5264 : &any_viable_p, complain);
5265 31252 : if (!cand)
5266 : {
5267 971 : if (complain & tf_error)
5268 198 : print_error_for_call_failure (dguides, args, candidates);
5269 971 : result = error_mark_node;
5270 : }
5271 : else
5272 30281 : result = cand->fn;
5273 :
5274 62504 : return result;
5275 31252 : }
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 102689436 : build_new_function_call (tree fn, vec<tree, va_gc> **args,
5283 : tsubst_flags_t complain)
5284 : {
5285 102689436 : struct z_candidate *candidates, *cand;
5286 102689436 : bool any_viable_p;
5287 102689436 : tree result;
5288 :
5289 102689436 : if (args != NULL && *args != NULL)
5290 : {
5291 102689436 : *args = resolve_args (*args, complain & ~tf_any_viable);
5292 102689436 : if (*args == NULL)
5293 441 : return error_mark_node;
5294 : }
5295 :
5296 102688995 : if (flag_tm)
5297 3559 : tm_malloc_replacement (fn);
5298 :
5299 102688995 : conversion_obstack_sentinel cos;
5300 :
5301 102688995 : cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
5302 : complain);
5303 :
5304 102675477 : if (!cand)
5305 : {
5306 203234 : 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 3225 : if (!any_viable_p && candidates && ! candidates->next
5311 1440 : && 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 2802 : print_error_for_call_failure (fn, *args, candidates);
5321 : }
5322 202811 : result = error_mark_node;
5323 : }
5324 102472243 : else if (complain & tf_any_viable)
5325 186 : return void_node;
5326 : else
5327 102472057 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
5328 :
5329 102674868 : if (flag_coroutines
5330 100568065 : && result
5331 100568065 : && TREE_CODE (result) == CALL_EXPR
5332 163944530 : && DECL_BUILT_IN_CLASS (TREE_OPERAND (CALL_EXPR_FN (result), 0))
5333 61269662 : == BUILT_IN_NORMAL)
5334 8174754 : result = coro_validate_builtin_call (result);
5335 :
5336 : return result;
5337 102675477 : }
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 1084190 : 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 1084190 : tree original_size = *size;
5359 1084190 : tree fns;
5360 1084190 : struct z_candidate *candidates;
5361 1084190 : struct z_candidate *cand = NULL;
5362 1084190 : bool any_viable_p;
5363 :
5364 1084190 : if (fn)
5365 1082739 : *fn = NULL_TREE;
5366 : /* Set to (size_t)-1 if the size check fails. */
5367 1084190 : if (size_check != NULL_TREE)
5368 : {
5369 21091 : tree errval = TYPE_MAX_VALUE (sizetype);
5370 21091 : if (cxx_dialect >= cxx11 && flag_exceptions)
5371 20684 : errval = throw_bad_array_new_length ();
5372 21091 : *size = fold_build3 (COND_EXPR, sizetype, size_check,
5373 : original_size, errval);
5374 : }
5375 1084190 : vec_safe_insert (*args, 0, *size);
5376 1084190 : *args = resolve_args (*args, complain);
5377 1084190 : if (*args == NULL)
5378 0 : return error_mark_node;
5379 :
5380 1084190 : 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 1084190 : fns = lookup_qualified_name (global_namespace, fnname);
5392 :
5393 1084190 : if (align_arg)
5394 : {
5395 9439 : vec<tree, va_gc>* align_args
5396 9439 : = vec_copy_and_insert (*args, align_arg, 1);
5397 9439 : cand = perform_overload_resolution (fns, align_args, &candidates,
5398 : &any_viable_p, tf_none);
5399 9439 : if (cand)
5400 9384 : *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 9384 : if (!cand)
5407 1074806 : 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 1084190 : 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 1084181 : 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 1084181 : if (fn)
5457 1082730 : *fn = cand->fn;
5458 :
5459 : /* Build the CALL_EXPR. */
5460 1084181 : 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 1084181 : tree call = extract_call_expr (ret);
5466 1084181 : if (TREE_CODE (call) == CALL_EXPR)
5467 1084181 : CALL_FROM_NEW_OR_DELETE_P (call) = 1;
5468 :
5469 : return ret;
5470 1084190 : }
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 80580050 : keep_unused_object_arg (tree result, tree obj, tree fn)
5482 : {
5483 80580050 : if (result == NULL_TREE
5484 80580050 : || result == error_mark_node
5485 79576775 : || DECL_OBJECT_MEMBER_FUNCTION_P (fn)
5486 83317670 : || !TREE_SIDE_EFFECTS (obj))
5487 : return result;
5488 :
5489 : /* But avoid the implicit lvalue-rvalue conversion when `obj' is
5490 : volatile. */
5491 89279 : tree a = obj;
5492 89279 : if (TREE_THIS_VOLATILE (a))
5493 57984 : a = build_this (a);
5494 89279 : if (TREE_SIDE_EFFECTS (a))
5495 31304 : 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 2646014 : build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
5503 : {
5504 2646014 : struct z_candidate *candidates = 0, *cand;
5505 2646014 : tree fns, convs, first_mem_arg = NULL_TREE;
5506 2646014 : bool any_viable_p;
5507 2646014 : tree result = NULL_TREE;
5508 :
5509 2646014 : auto_cond_timevar tv (TV_OVERLOAD);
5510 :
5511 2646014 : obj = mark_lvalue_use (obj);
5512 :
5513 2646014 : if (error_operand_p (obj))
5514 0 : return error_mark_node;
5515 :
5516 2646014 : tree type = TREE_TYPE (obj);
5517 :
5518 2646014 : obj = prep_operand (obj);
5519 :
5520 2646014 : 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 2646014 : if (TYPE_BINFO (type))
5531 : {
5532 2645990 : fns = lookup_fnfields (TYPE_BINFO (type), call_op_identifier, 1, complain);
5533 2645990 : if (fns == error_mark_node)
5534 : return error_mark_node;
5535 : }
5536 : else
5537 : fns = NULL_TREE;
5538 :
5539 2646004 : if (args != NULL && *args != NULL)
5540 : {
5541 2646004 : *args = resolve_args (*args, complain);
5542 2646004 : if (*args == NULL)
5543 110 : return error_mark_node;
5544 : }
5545 :
5546 2645894 : conversion_obstack_sentinel cos;
5547 :
5548 2645894 : if (fns)
5549 : {
5550 2640541 : first_mem_arg = obj;
5551 :
5552 2640541 : add_candidates (BASELINK_FUNCTIONS (fns),
5553 : first_mem_arg, *args, NULL_TREE,
5554 : NULL_TREE, false,
5555 2640541 : BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
5556 : LOOKUP_NORMAL, &candidates, complain);
5557 : }
5558 :
5559 2645894 : bool any_call_ops = candidates != nullptr;
5560 :
5561 2645894 : convs = lookup_conversions (type);
5562 :
5563 5484233 : for (; convs; convs = TREE_CHAIN (convs))
5564 : {
5565 192445 : tree totype = TREE_TYPE (convs);
5566 :
5567 158872 : if (TYPE_PTRFN_P (totype)
5568 33576 : || TYPE_REFFN_P (totype)
5569 225977 : || (TYPE_REF_P (totype)
5570 1189 : && TYPE_PTRFN_P (TREE_TYPE (totype))))
5571 318268 : for (tree fn : ovl_range (TREE_VALUE (convs)))
5572 : {
5573 159134 : if (DECL_NONCONVERTING_P (fn))
5574 3 : continue;
5575 :
5576 159131 : 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 39615 : if (any_call_ops)
5582 39603 : 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 119516 : 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 2645894 : candidates = splice_viable (candidates, true, &any_viable_p);
5599 2645894 : if (!any_viable_p)
5600 : {
5601 40238 : 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 40238 : result = error_mark_node;
5609 : }
5610 : else
5611 : {
5612 2605656 : cand = tourney (candidates, complain);
5613 2605656 : 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 2605634 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL
5625 2605575 : && DECL_OVERLOADED_OPERATOR_P (cand->fn)
5626 5211209 : && DECL_OVERLOADED_OPERATOR_IS (cand->fn, CALL_EXPR))
5627 : {
5628 2605575 : 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 2605575 : 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 2645894 : return result;
5650 2646014 : }
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 518546 : conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
5790 : {
5791 518546 : tree t1 = non_reference (TREE_TYPE (e1));
5792 518546 : tree t2 = non_reference (TREE_TYPE (e2));
5793 518546 : conversion *conv;
5794 518546 : 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 518546 : if (glvalue_p (e2))
5807 : {
5808 508561 : tree rtype = cp_build_reference_type (t2, !lvalue_p (e2));
5809 508561 : 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 508561 : 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 408287 : 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 161465 : if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
5837 323037 : && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
5838 : {
5839 19635 : if (good_base && at_least_as_qualified_p (t2, t1))
5840 : {
5841 9707 : conv = build_identity_conv (t1, e1);
5842 9707 : 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 9701 : 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 278744 : return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
5859 278744 : 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 8029821 : build_conditional_expr (const op_location_t &loc,
5867 : tree arg1, tree arg2, tree arg3,
5868 : tsubst_flags_t complain)
5869 : {
5870 8029821 : tree arg2_type;
5871 8029821 : tree arg3_type;
5872 8029821 : tree result = NULL_TREE;
5873 8029821 : tree result_type = NULL_TREE;
5874 8029821 : tree semantic_result_type = NULL_TREE;
5875 8029821 : bool is_glvalue = true;
5876 8029821 : struct z_candidate *candidates = 0;
5877 8029821 : struct z_candidate *cand;
5878 8029821 : tree orig_arg2, orig_arg3;
5879 :
5880 8029821 : 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 8029821 : 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 8029821 : if (error_operand_p (arg1)
5917 8029742 : || error_operand_p (arg2)
5918 16059513 : || error_operand_p (arg3))
5919 180 : return error_mark_node;
5920 :
5921 8029641 : conversion_obstack_sentinel cos;
5922 :
5923 8029641 : orig_arg2 = arg2;
5924 8029641 : orig_arg3 = arg3;
5925 :
5926 8029641 : if (gnu_vector_type_p (TREE_TYPE (arg1))
5927 8029641 : && (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 8027781 : arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
6067 : LOOKUP_NORMAL);
6068 8027781 : if (error_operand_p (arg1))
6069 25 : return error_mark_node;
6070 :
6071 8027756 : arg2_type = unlowered_expr_type (arg2);
6072 8027756 : arg3_type = unlowered_expr_type (arg3);
6073 :
6074 8027756 : if ((TREE_CODE (arg2) == EXCESS_PRECISION_EXPR
6075 8027738 : || 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 8027756 : 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 128822 : 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 128822 : 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 128822 : 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 128750 : else if (TREE_CODE (arg2) != THROW_EXPR
6156 128738 : && TREE_CODE (arg3) == THROW_EXPR)
6157 : {
6158 180 : result_type = arg2_type;
6159 180 : is_glvalue = glvalue_p (arg2);
6160 : }
6161 128570 : else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
6162 : {
6163 128541 : result_type = void_type_node;
6164 128541 : 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 128793 : 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 7898934 : else if (!same_type_p (arg2_type, arg3_type)
6194 7898934 : && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)
6195 1595394 : || (same_type_ignoring_top_level_qualifiers_p (arg2_type,
6196 : arg3_type)
6197 449616 : && glvalue_p (arg2) && glvalue_p (arg3)
6198 109978 : && lvalue_p (arg2) == lvalue_p (arg3))))
6199 : {
6200 259273 : conversion *conv2;
6201 259273 : conversion *conv3;
6202 259273 : bool converted = false;
6203 :
6204 259273 : conv2 = conditional_conversion (arg2, arg3, complain);
6205 259273 : 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 259273 : if ((conv2 && !conv2->bad_p
6217 47448 : && conv3 && !conv3->bad_p)
6218 47426 : || (conv2 && conv2->kind == ck_ambig)
6219 259151 : || (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 259151 : else if (conv2 && !conv2->bad_p)
6236 : {
6237 47326 : arg2 = convert_like (conv2, arg2, complain);
6238 47326 : arg2 = convert_from_reference (arg2);
6239 47326 : 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 47326 : if (error_operand_p (arg2))
6246 8 : result = error_mark_node;
6247 : converted = true;
6248 : }
6249 211825 : else if (conv3 && !conv3->bad_p)
6250 : {
6251 211087 : arg3 = convert_like (conv3, arg3, complain);
6252 211087 : arg3 = convert_from_reference (arg3);
6253 211087 : arg3_type = TREE_TYPE (arg3);
6254 211087 : 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 259143 : if (converted
6278 258405 : && CLASS_TYPE_P (arg2_type)
6279 271157 : && 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 11711313 : if (((lvalue_p (arg2) && lvalue_p (arg3))
6292 4720116 : || (xvalue_p (arg2) && xvalue_p (arg3)))
6293 11126674 : && same_type_p (arg2_type, arg3_type))
6294 : {
6295 3132237 : result_type = arg2_type;
6296 3132237 : 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 4766567 : is_glvalue = false;
6307 4766567 : if (!same_type_p (arg2_type, arg3_type)
6308 4766567 : && (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 4765952 : arg2 = force_rvalue (arg2, complain);
6378 4765952 : if (!CLASS_TYPE_P (arg2_type))
6379 4673672 : arg2_type = TREE_TYPE (arg2);
6380 :
6381 4765952 : arg3 = force_rvalue (arg3, complain);
6382 4765952 : if (!CLASS_TYPE_P (arg3_type))
6383 4673672 : arg3_type = TREE_TYPE (arg3);
6384 :
6385 4765952 : 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 4765904 : 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 1052862 : else if ((ARITHMETIC_TYPE_P (arg2_type)
6402 106 : || UNSCOPED_ENUM_P (arg2_type))
6403 1029887 : && (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 1029733 : tree eptype2 = arg2_type;
6411 1029733 : tree eptype3 = arg3_type;
6412 1029733 : tree eptype;
6413 831 : if (ANY_INTEGRAL_TYPE_P (arg2_type)
6414 1029736 : && (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 1029718 : && (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 1029733 : result_type = type_after_usual_arithmetic_conversions (eptype2,
6430 : eptype3);
6431 1029733 : 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 1029733 : 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 1029733 : if (complain & tf_warning)
6473 976228 : 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 1029733 : 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 1029698 : else if ((((complain & (cxx_dialect >= cxx26
6501 1029698 : ? tf_warning_or_error : tf_warning))
6502 980602 : && warn_deprecated_enum_float_conv)
6503 65695 : || (cxx_dialect >= cxx26
6504 2149 : && (complain & tf_warning_or_error) == 0))
6505 966049 : && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
6506 40 : && SCALAR_FLOAT_TYPE_P (arg3_type))
6507 966036 : || (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 1019905 : else if ((extra_warnings || warn_enum_conversion)
6532 1029680 : && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
6533 15 : && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
6534 9763 : || (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 1029727 : arg2 = perform_implicit_conversion (result_type, arg2, complain);
6549 1029727 : 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 23129 : else if ((null_ptr_cst_p (arg2)
6568 224 : && TYPE_PTR_OR_PTRMEM_P (arg3_type))
6569 22905 : || (null_ptr_cst_p (arg3)
6570 21941 : && 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 23205 : || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
6574 : {
6575 23075 : result_type = composite_pointer_type (loc,
6576 : arg2_type, arg3_type, arg2,
6577 : arg3, CPO_CONDITIONAL_EXPR,
6578 : complain);
6579 23075 : if (result_type == error_mark_node)
6580 : return error_mark_node;
6581 23049 : arg2 = perform_implicit_conversion (result_type, arg2, complain);
6582 23049 : arg3 = perform_implicit_conversion (result_type, arg3, complain);
6583 : }
6584 :
6585 4765818 : 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 4765818 : if (arg2 == error_mark_node || arg3 == error_mark_node)
6594 : return error_mark_node;
6595 :
6596 4765815 : valid_operands:
6597 8026845 : if (processing_template_decl && is_glvalue)
6598 : {
6599 : /* Let lvalue_kind know this was a glvalue. */
6600 114750 : tree arg = (result_type == arg2_type ? arg2 : arg3);
6601 114750 : result_type = cp_build_reference_type (result_type, xvalue_p (arg));
6602 : }
6603 :
6604 8026845 : 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 8026845 : if (warn_duplicated_branches
6609 153 : && (complain & tf_warning)
6610 8026998 : && (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 8026845 : 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 4894539 : if (CLASS_TYPE_P (TREE_TYPE (result)))
6625 : {
6626 92317 : 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 92317 : 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 4894539 : result = rvalue (result);
6635 4894539 : if (semantic_result_type)
6636 87 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type,
6637 : result);
6638 : }
6639 : else
6640 : {
6641 3132306 : result = force_paren_expr (result);
6642 3132306 : gcc_assert (semantic_result_type == NULL_TREE);
6643 : }
6644 :
6645 : return result;
6646 8029821 : }
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 665100742 : prep_operand (tree operand)
6654 : {
6655 665100742 : if (operand)
6656 : {
6657 768958782 : if (CLASS_TYPE_P (TREE_TYPE (operand))
6658 411556885 : && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
6659 : /* Make sure the template type is instantiated now. */
6660 10858025 : instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
6661 : }
6662 :
6663 665100742 : 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 233542972 : perfect_conversion_p (conversion *conv)
6674 : {
6675 233542972 : if (CONVERSION_RANK (conv) != cr_identity)
6676 : return false;
6677 167025900 : if (conv->kind == ck_ref_bind)
6678 : {
6679 43736581 : if (!conv->rvaluedness_matches_p)
6680 : return false;
6681 30270418 : if (!same_type_p (TREE_TYPE (conv->type),
6682 : next_conversion (conv)->type))
6683 : return false;
6684 : }
6685 149583982 : 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 555621142 : perfect_candidate_p (z_candidate *cand)
6699 : {
6700 555621142 : if (cand->viable < 1)
6701 : return false;
6702 : /* CWG1402 makes an implicitly deleted move op worse than other
6703 : candidates. */
6704 215073448 : if (DECL_DELETED_FN (cand->fn) && DECL_DEFAULTED_FN (cand->fn)
6705 214053567 : && move_fn_p (cand->fn))
6706 : return false;
6707 212377531 : int len = cand->num_convs;
6708 361961463 : for (int i = 0; i < len; ++i)
6709 233542972 : if (!perfect_conversion_p (cand->convs[i]))
6710 : return false;
6711 128418491 : 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 22637849 : missing_conversion_p (const z_candidate *cand)
6721 : {
6722 41293171 : for (unsigned i = 0; i < cand->num_convs; ++i)
6723 : {
6724 27409825 : conversion *conv = cand->convs[i];
6725 27409825 : if (!conv)
6726 : return true;
6727 26403078 : 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 7747756 : 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 319587996 : 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 319587996 : tree ctype;
6758 319587996 : const vec<tree, va_gc> *non_static_args;
6759 319587996 : bool check_list_ctor = false;
6760 319587996 : bool check_converting = false;
6761 319587996 : unification_kind_t strict;
6762 319587996 : tree ne_context = NULL_TREE;
6763 319587996 : tree ne_fns = NULL_TREE;
6764 :
6765 319587996 : if (!fns)
6766 3439040 : return;
6767 :
6768 : /* Precalculate special handling of constructors and conversion ops. */
6769 316148956 : tree fn = OVL_FIRST (fns);
6770 316148956 : if (DECL_CONV_FN_P (fn))
6771 : {
6772 18602792 : check_list_ctor = false;
6773 18602792 : check_converting = (flags & LOOKUP_ONLYCONVERTING) != 0;
6774 18602792 : 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 1953 : 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 18602792 : ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg));
6785 : }
6786 : else
6787 : {
6788 595092328 : if (DECL_CONSTRUCTOR_P (fn))
6789 : {
6790 80968638 : check_list_ctor = (flags & LOOKUP_LIST_ONLY) != 0;
6791 : /* For list-initialization we consider explicit constructors
6792 : and complain if one is chosen. */
6793 80968638 : check_converting
6794 80968638 : = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
6795 : == LOOKUP_ONLYCONVERTING);
6796 : }
6797 297546164 : strict = DEDUCE_CALL;
6798 463659966 : 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 316148956 : if ((flags & (LOOKUP_REWRITTEN | LOOKUP_REVERSED))
6804 316148956 : && DECL_OVERLOADED_OPERATOR_IS (fn, EQ_EXPR))
6805 : {
6806 7193374 : tree ne_name = ovl_op_identifier (false, NE_EXPR);
6807 7193374 : if (DECL_CLASS_SCOPE_P (fn))
6808 : {
6809 189943 : ne_context = DECL_CONTEXT (fn);
6810 189943 : ne_fns = lookup_fnfields (TREE_TYPE ((*args)[0]), ne_name,
6811 : 1, tf_none);
6812 189943 : if (ne_fns == error_mark_node || ne_fns == NULL_TREE)
6813 : ne_fns = NULL_TREE;
6814 : else
6815 22020 : ne_fns = BASELINK_FUNCTIONS (ne_fns);
6816 : }
6817 : else
6818 : {
6819 7003431 : ne_context = decl_namespace_context (fn);
6820 7003431 : ne_fns = lookup_qualified_name (ne_context, ne_name,
6821 : LOOK_want::NORMAL,
6822 : /*complain*/false);
6823 7003431 : if (ne_fns == error_mark_node
6824 2270081 : || !is_overloaded_fn (ne_fns))
6825 4733350 : ne_fns = NULL_TREE;
6826 : }
6827 : }
6828 :
6829 316148956 : if (first_arg)
6830 : non_static_args = args;
6831 : else
6832 : /* Delay creating the implicit this parameter until it is needed. */
6833 140079010 : non_static_args = NULL;
6834 :
6835 316148956 : 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 316148956 : bool seen_perfect = false;
6841 316148956 : enum { templates, non_templates, either } which = either;
6842 316148956 : if (template_only)
6843 : which = templates;
6844 : else /*if (flags & LOOKUP_DEFAULTED)*/
6845 276915370 : which = non_templates;
6846 :
6847 : /* Template candidates that we'll potentially ignore if the
6848 : perfect candidate optimization succeeds. */
6849 316148956 : 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 316148956 : z_candidate *bad_cands = nullptr;
6862 316148956 : bool shortcut_bad_convs = true;
6863 :
6864 148621720 : again:
6865 2719483856 : for (tree fn : lkp_range (fns))
6866 : {
6867 2254726731 : if (which == templates && TREE_CODE (fn) != TEMPLATE_DECL)
6868 : {
6869 327961051 : if (template_only)
6870 801362 : add_ignored_candidate (candidates, fn);
6871 327961051 : continue;
6872 : }
6873 1926765680 : if (which == non_templates && TREE_CODE (fn) == TEMPLATE_DECL)
6874 : {
6875 671238917 : add_ignored_candidate (&ignored_template_cands, fn);
6876 671238917 : continue;
6877 : }
6878 241324922 : if ((check_converting && DECL_NONCONVERTING_P (fn))
6879 1470865443 : || (check_list_ctor && !is_list_ctor (fn)))
6880 : {
6881 27394940 : add_ignored_candidate (candidates, fn);
6882 27394940 : continue;
6883 : }
6884 :
6885 1228131823 : tree fn_first_arg = NULL_TREE;
6886 1228131823 : const vec<tree, va_gc> *fn_args = args;
6887 :
6888 1228131823 : 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 441581894 : if (first_arg == NULL_TREE)
6894 : {
6895 8646648 : unsigned int ix;
6896 8646648 : tree arg;
6897 8646648 : vec<tree, va_gc> *tempvec;
6898 8646648 : vec_alloc (tempvec, args->length () - 1);
6899 23411866 : for (ix = 1; args->iterate (ix, &arg); ++ix)
6900 6118570 : tempvec->quick_push (arg);
6901 8646648 : non_static_args = tempvec;
6902 8646648 : 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 786549929 : else if (vec_safe_length (args) == 2 && (flags & LOOKUP_REVERSED))
6911 : {
6912 222336754 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
6913 222336754 : if (same_type_p (TREE_VALUE (parmlist),
6914 : TREE_VALUE (TREE_CHAIN (parmlist))))
6915 105583689 : 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 1122548134 : if (ne_context)
6921 : {
6922 156990957 : 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 156793760 : tree fn_ns = decl_namespace_context (fn);
6928 156793760 : if (fn_ns != ne_context)
6929 : {
6930 8891656 : ne_context = fn_ns;
6931 8891656 : tree ne_name = ovl_op_identifier (false, NE_EXPR);
6932 8891656 : ne_fns = lookup_qualified_name (ne_context, ne_name,
6933 : LOOK_want::NORMAL,
6934 : /*complain*/false);
6935 8891656 : if (ne_fns == error_mark_node
6936 5641437 : || !is_overloaded_fn (ne_fns))
6937 3250219 : ne_fns = NULL_TREE;
6938 : }
6939 : }
6940 156990957 : bool found = false;
6941 1702755107 : for (lkp_iterator ne (ne_fns); !found && ne; ++ne)
6942 1583549349 : 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 1583549349 : else if (fns_correspond (fn, *ne))
6949 : {
6950 : found = true;
6951 : break;
6952 : }
6953 156990957 : if (found)
6954 37785199 : 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 1084762935 : 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 1084762935 : if (TREE_CODE (fn) == TEMPLATE_DECL)
6968 529141793 : 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 555621142 : 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 555621142 : if (perfect_candidate_p (*candidates))
6995 1084749384 : seen_perfect = true;
6996 : }
6997 :
6998 1084749384 : z_candidate *cand = *candidates;
6999 1084749384 : if (cand->viable == 1)
7000 300371576 : seen_strictly_viable = true;
7001 :
7002 1084749384 : if (cand->viable == -1
7003 22638363 : && shortcut_bad_convs
7004 1107387233 : && (missing_conversion_p (cand)
7005 13883346 : || 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 8786379 : if (complain & (tf_error | tf_conv))
7013 : {
7014 8458146 : *candidates = cand->next;
7015 8458146 : cand->next = bad_cands;
7016 8458146 : 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 328233 : cand->viable = 0;
7025 : }
7026 : }
7027 464757125 : if (which == non_templates && !seen_perfect)
7028 : {
7029 148503080 : which = templates;
7030 148503080 : ignored_template_cands = nullptr;
7031 148503080 : goto again;
7032 : }
7033 316254045 : else if (which == templates
7034 316254045 : && !seen_strictly_viable
7035 : && shortcut_bad_convs
7036 59313642 : && 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 262180 : which = either;
7042 : fns = NULL_TREE;
7043 262180 : for (z_candidate *cand = bad_cands; cand; cand = cand->next)
7044 : {
7045 143540 : tree fn = cand->fn;
7046 143540 : if (tree ti = cand->template_decl)
7047 120 : fn = TI_TEMPLATE (ti);
7048 143540 : fns = ovl_make (fn, fns);
7049 : }
7050 118640 : shortcut_bad_convs = false;
7051 118640 : bad_cands = nullptr;
7052 118640 : goto again;
7053 : }
7054 :
7055 316135405 : 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 482698689 : for (z_candidate *omitted_cands : { ignored_template_cands, bad_cands })
7060 : {
7061 321799126 : z_candidate **omitted_cands_tail = &omitted_cands;
7062 370157511 : while (*omitted_cands_tail)
7063 48358385 : omitted_cands_tail = &(*omitted_cands_tail)->next;
7064 321799126 : *omitted_cands_tail = *candidates;
7065 321799126 : *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 16619868 : op_is_ordered (tree_code code)
7075 : {
7076 16619209 : switch (code)
7077 : {
7078 : // 5. b @= a
7079 3730473 : case MODIFY_EXPR:
7080 3730473 : return (flag_strong_eval_order > 1 ? -1 : 0);
7081 :
7082 : // 6. a[b]
7083 431863 : case ARRAY_REF:
7084 431204 : 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 78955 : case MEMBER_REF:
7092 : // 7. a << b
7093 78955 : case LSHIFT_EXPR:
7094 : // 8. a >> b
7095 78955 : case RSHIFT_EXPR:
7096 : // a && b
7097 : // Predates P0145R3.
7098 78955 : case TRUTH_ANDIF_EXPR:
7099 : // a || b
7100 : // Predates P0145R3.
7101 78955 : case TRUTH_ORIF_EXPR:
7102 : // a , b
7103 : // Predates P0145R3.
7104 78955 : case COMPOUND_EXPR:
7105 78955 : 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 35356378 : 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 35356378 : z_candidate *start_candidates = *candidates;
7130 35356378 : bool ismodop = code2 != ERROR_MARK;
7131 35356378 : 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 35356378 : bool rewritten = (flags & LOOKUP_REWRITTEN);
7138 35356378 : if (rewritten && code != EQ_EXPR && code != SPACESHIP_EXPR)
7139 950639 : flags &= ~LOOKUP_REWRITTEN;
7140 :
7141 32799574 : bool memonly = false;
7142 32799574 : switch (code)
7143 : {
7144 : /* =, ->, [], () must be non-static member functions. */
7145 6562550 : case MODIFY_EXPR:
7146 6562550 : 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 31066910 : tree fns;
7163 31066910 : if (!lookups)
7164 23011117 : 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 8055793 : else if (tree found = purpose_member (fnname, lookups))
7168 1754052 : fns = TREE_VALUE (found);
7169 : else
7170 : fns = NULL_TREE;
7171 31066910 : fns = lookup_arg_dependent (fnname, fns, arglist);
7172 31066910 : 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 35356345 : tree arg1_type = TREE_TYPE ((*arglist)[0]);
7179 35356345 : unsigned nargs = arglist->length () > 1 ? 2 : 1;
7180 30303475 : tree arg2_type = nargs > 1 ? TREE_TYPE ((*arglist)[1]) : NULL_TREE;
7181 35356345 : if (CLASS_TYPE_P (arg1_type))
7182 : {
7183 20328947 : tree fns = lookup_fnfields (arg1_type, fnname, 1, complain);
7184 20328947 : if (fns == error_mark_node)
7185 : return error_mark_node;
7186 20328935 : if (fns)
7187 : {
7188 9077877 : if (code == ARRAY_REF)
7189 : {
7190 431229 : vec<tree,va_gc> *restlist = make_tree_vector ();
7191 862458 : for (unsigned i = 1; i < nargs; ++i)
7192 431229 : vec_safe_push (restlist, (*arglist)[i]);
7193 431229 : z_candidate *save_cand = *candidates;
7194 862458 : add_candidates (BASELINK_FUNCTIONS (fns),
7195 431229 : (*arglist)[0], restlist, NULL_TREE,
7196 : NULL_TREE, false,
7197 431229 : BASELINK_BINFO (fns),
7198 431229 : BASELINK_ACCESS_BINFO (fns),
7199 : flags, candidates, complain);
7200 : /* Release the vec if we didn't add a candidate that uses it. */
7201 431827 : for (z_candidate *c = *candidates; c != save_cand; c = c->next)
7202 431827 : if (c->args == restlist)
7203 : {
7204 431229 : restlist = NULL;
7205 431229 : break;
7206 : }
7207 431229 : release_tree_vector (restlist);
7208 : }
7209 : else
7210 8646648 : add_candidates (BASELINK_FUNCTIONS (fns),
7211 : NULL_TREE, arglist, NULL_TREE,
7212 : NULL_TREE, false,
7213 8646648 : BASELINK_BINFO (fns),
7214 8646648 : 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 15027398 : else if (! arg2_type || ! CLASS_TYPE_P (arg2_type))
7225 : {
7226 : struct z_candidate **candp, **next;
7227 :
7228 255026208 : for (candp = candidates; *candp != start_candidates; candp = next)
7229 : {
7230 240598167 : unsigned i;
7231 240598167 : z_candidate *cand = *candp;
7232 240598167 : next = &cand->next;
7233 :
7234 240598167 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
7235 :
7236 710619458 : for (i = 0; i < nargs; ++i)
7237 : {
7238 475203800 : tree parmtype = TREE_VALUE (parmlist);
7239 475203800 : tree argtype = unlowered_expr_type ((*arglist)[i]);
7240 :
7241 475203800 : if (TYPE_REF_P (parmtype))
7242 377265184 : parmtype = TREE_TYPE (parmtype);
7243 475203800 : if (TREE_CODE (argtype) == ENUMERAL_TYPE
7244 929314737 : && (same_type_ignoring_top_level_qualifiers_p
7245 454110937 : (argtype, parmtype)))
7246 : break;
7247 :
7248 470021291 : parmlist = TREE_CHAIN (parmlist);
7249 : }
7250 :
7251 : /* No argument has an appropriate type, so remove this
7252 : candidate function from the list. */
7253 240598167 : if (i == nargs)
7254 : {
7255 235415658 : *candp = cand->next;
7256 235415658 : next = candp;
7257 : }
7258 : }
7259 : }
7260 :
7261 35356333 : if (!rewritten)
7262 : {
7263 : /* The standard says to rewrite built-in candidates, too,
7264 : but there's no point. */
7265 24809360 : add_builtin_candidates (candidates, code, code2, fnname, arglist,
7266 : flags, complain);
7267 :
7268 : /* Maybe add C++20 rewritten comparison candidates. */
7269 24809360 : tree_code rewrite_code = ERROR_MARK;
7270 24809360 : if (cxx_dialect >= cxx20
7271 24507238 : && nargs == 2
7272 44386128 : && (OVERLOAD_TYPE_P (arg1_type) || OVERLOAD_TYPE_P (arg2_type)))
7273 19569148 : switch (code)
7274 : {
7275 1341602 : case LT_EXPR:
7276 1341602 : case LE_EXPR:
7277 1341602 : case GT_EXPR:
7278 1341602 : case GE_EXPR:
7279 1341602 : case SPACESHIP_EXPR:
7280 1341602 : rewrite_code = SPACESHIP_EXPR;
7281 1341602 : break;
7282 :
7283 : case NE_EXPR:
7284 : case EQ_EXPR:
7285 : rewrite_code = EQ_EXPR;
7286 : break;
7287 :
7288 : default:;
7289 : }
7290 :
7291 1341602 : if (rewrite_code)
7292 : {
7293 6472438 : tree r;
7294 6472438 : flags |= LOOKUP_REWRITTEN;
7295 6472438 : if (rewrite_code != code)
7296 : {
7297 : /* Add rewritten candidates in same order. */
7298 3123594 : r = add_operator_candidates (candidates, rewrite_code, ERROR_MARK,
7299 : arglist, lookups, flags, complain);
7300 3123594 : if (r == error_mark_node)
7301 : return error_mark_node;
7302 : }
7303 :
7304 6472432 : z_candidate *save_cand = *candidates;
7305 :
7306 : /* Add rewritten candidates in reverse order. */
7307 6472432 : flags |= LOOKUP_REVERSED;
7308 6472432 : vec<tree,va_gc> *revlist = make_tree_vector ();
7309 6472432 : revlist->quick_push ((*arglist)[1]);
7310 6472432 : revlist->quick_push ((*arglist)[0]);
7311 6472432 : r = add_operator_candidates (candidates, rewrite_code, ERROR_MARK,
7312 : revlist, lookups, flags, complain);
7313 6472432 : 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 6634725 : for (z_candidate *c = *candidates; c != save_cand; c = c->next)
7318 2992574 : if (c->args == revlist)
7319 : {
7320 : revlist = NULL;
7321 : break;
7322 : }
7323 6472432 : release_tree_vector (revlist);
7324 : }
7325 : }
7326 :
7327 : return NULL_TREE;
7328 : }
7329 :
7330 : tree
7331 220828243 : 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 220828243 : struct z_candidate *candidates = 0, *cand;
7336 220828243 : releasing_vec arglist;
7337 220828243 : tree result = NULL_TREE;
7338 220828243 : bool result_valid_p = false;
7339 220828243 : enum tree_code code2 = ERROR_MARK;
7340 220828243 : enum tree_code code_orig_arg1 = ERROR_MARK;
7341 220828243 : enum tree_code code_orig_arg2 = ERROR_MARK;
7342 220828243 : bool strict_p;
7343 220828243 : bool any_viable_p;
7344 :
7345 220828243 : auto_cond_timevar tv (TV_OVERLOAD);
7346 :
7347 220828243 : if (error_operand_p (arg1)
7348 220824068 : || error_operand_p (arg2)
7349 441646251 : || error_operand_p (arg3))
7350 10235 : return error_mark_node;
7351 :
7352 220818008 : conversion_obstack_sentinel cos;
7353 :
7354 220818008 : bool ismodop = code == MODIFY_EXPR;
7355 220818008 : if (ismodop)
7356 : {
7357 11397720 : code2 = TREE_CODE (arg3);
7358 11397720 : arg3 = NULL_TREE;
7359 : }
7360 :
7361 220818008 : tree arg1_type = unlowered_expr_type (arg1);
7362 220818008 : tree arg2_type = arg2 ? unlowered_expr_type (arg2) : NULL_TREE;
7363 :
7364 220818008 : arg1 = prep_operand (arg1);
7365 :
7366 220818008 : 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 18599210 : case TRUTH_ORIF_EXPR:
7380 18599210 : case TRUTH_ANDIF_EXPR:
7381 18599210 : case TRUTH_AND_EXPR:
7382 18599210 : case TRUTH_OR_EXPR:
7383 : /* These are saved for the sake of warn_logical_operator. */
7384 18599210 : code_orig_arg1 = TREE_CODE (arg1);
7385 18599210 : code_orig_arg2 = TREE_CODE (arg2);
7386 18599210 : break;
7387 51840484 : case GT_EXPR:
7388 51840484 : case LT_EXPR:
7389 51840484 : case GE_EXPR:
7390 51840484 : case LE_EXPR:
7391 51840484 : case EQ_EXPR:
7392 51840484 : case NE_EXPR:
7393 : /* These are saved for the sake of maybe_warn_bool_compare. */
7394 51840484 : code_orig_arg1 = TREE_CODE (arg1_type);
7395 51840484 : code_orig_arg2 = TREE_CODE (arg2_type);
7396 51840484 : break;
7397 :
7398 : default:
7399 : break;
7400 : }
7401 :
7402 220818008 : arg2 = prep_operand (arg2);
7403 220818008 : arg3 = prep_operand (arg3);
7404 :
7405 220818008 : if (code == COND_EXPR)
7406 : /* Use build_conditional_expr instead. */
7407 0 : gcc_unreachable ();
7408 220818008 : else if (! OVERLOAD_TYPE_P (arg1_type)
7409 416314188 : && (! arg2 || ! OVERLOAD_TYPE_P (arg2_type)))
7410 195057656 : goto builtin;
7411 :
7412 25760352 : if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
7413 : {
7414 319458 : arg2 = integer_zero_node;
7415 319458 : arg2_type = integer_type_node;
7416 : }
7417 :
7418 25760352 : arglist->quick_push (arg1);
7419 25760352 : if (arg2 != NULL_TREE)
7420 20707482 : arglist->quick_push (arg2);
7421 25760352 : if (arg3 != NULL_TREE)
7422 0 : arglist->quick_push (arg3);
7423 :
7424 25760352 : result = add_operator_candidates (&candidates, code, code2, arglist,
7425 : lookups, flags, complain);
7426 25760319 : if (result == error_mark_node)
7427 : return error_mark_node;
7428 :
7429 25760307 : 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 23839573 : default:
7443 23839573 : strict_p = false;
7444 23839573 : break;
7445 : }
7446 :
7447 25760307 : candidates = splice_viable (candidates, strict_p, &any_viable_p);
7448 25760307 : if (!any_viable_p)
7449 : {
7450 1971863 : 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 48040 : default:
7494 48040 : 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 48040 : result = error_mark_node;
7513 48040 : break;
7514 : }
7515 : }
7516 : else
7517 : {
7518 23788444 : cand = tourney (candidates, complain);
7519 23788444 : 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 23788260 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
7532 : {
7533 19442105 : if (overload)
7534 : {
7535 14895557 : if (cand->rewritten ())
7536 : /* build_min_non_dep_op_overload needs to know whether the
7537 : candidate is rewritten/reversed. */
7538 1857217 : *overload = build_tree_list (build_int_cst (integer_type_node,
7539 1857217 : cand->flags),
7540 : cand->fn);
7541 : else
7542 13038340 : *overload = cand->fn;
7543 : }
7544 :
7545 19442105 : if (resolve_args (arglist, complain) == NULL)
7546 6 : result = error_mark_node;
7547 : else
7548 : {
7549 19442099 : tsubst_flags_t ocomplain = complain;
7550 19442099 : if (cand->rewritten ())
7551 : /* We'll wrap this call in another one. */
7552 1857823 : ocomplain &= ~tf_decltype;
7553 19442099 : if (cand->reversed ())
7554 : {
7555 : /* We swapped these in add_candidate, swap them back now. */
7556 21419 : std::swap (cand->convs[0], cand->convs[1]);
7557 21419 : 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 19442099 : result = build_over_call (cand, LOOKUP_NORMAL, ocomplain);
7563 : }
7564 :
7565 36064368 : if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn))
7566 : /* There won't be a CALL_EXPR. */;
7567 16621849 : else if (result && result != error_mark_node)
7568 : {
7569 16619209 : tree call = extract_call_expr (result);
7570 16619209 : CALL_EXPR_OPERATOR_SYNTAX (call) = true;
7571 :
7572 : /* Specify evaluation order as per P0145R2. */
7573 16619209 : CALL_EXPR_ORDERED_ARGS (call) = false;
7574 16619209 : switch (op_is_ordered (code))
7575 : {
7576 3702349 : case -1:
7577 3702349 : CALL_EXPR_REVERSE_ARGS (call) = true;
7578 3702349 : break;
7579 :
7580 509133 : case 1:
7581 509133 : CALL_EXPR_ORDERED_ARGS (call) = true;
7582 509133 : break;
7583 :
7584 : default:
7585 : break;
7586 : }
7587 : }
7588 :
7589 : /* If this was a C++20 rewritten comparison, adjust the result. */
7590 19442102 : if (cand->rewritten ())
7591 : {
7592 1857823 : switch (code)
7593 : {
7594 9939 : case EQ_EXPR:
7595 9939 : gcc_checking_assert (cand->reversed ());
7596 905821 : gcc_fallthrough ();
7597 905821 : case NE_EXPR:
7598 905821 : 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 905819 : 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 905775 : else if (code == NE_EXPR)
7617 : /* !(y == x) or !(x == y) */
7618 895857 : 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 951688 : gcc_fallthrough ();
7634 951688 : case LT_EXPR:
7635 951688 : case LE_EXPR:
7636 951688 : case GT_EXPR:
7637 951688 : case GE_EXPR:
7638 951688 : {
7639 951688 : tree lhs = result;
7640 951688 : tree rhs = integer_zero_node;
7641 951688 : if (cand->reversed ())
7642 2164 : std::swap (lhs, rhs);
7643 951688 : warning_sentinel ws (warn_zero_as_null_pointer_constant);
7644 951688 : result = build_new_op (loc, code,
7645 : LOOKUP_NORMAL|LOOKUP_REWRITTEN,
7646 : lhs, rhs, NULL_TREE, lookups,
7647 : NULL, complain);
7648 951688 : }
7649 951688 : 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 19442102 : if (code == ARRAY_REF)
7660 431204 : result = keep_unused_object_arg (result, arg1, cand->fn);
7661 : }
7662 : else
7663 : {
7664 : /* Give any warnings we noticed during overload resolution. */
7665 4346155 : 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 4346155 : switch (code)
7674 : {
7675 3446440 : case GT_EXPR:
7676 3446440 : case LT_EXPR:
7677 3446440 : case GE_EXPR:
7678 3446440 : case LE_EXPR:
7679 3446440 : case EQ_EXPR:
7680 3446440 : case NE_EXPR:
7681 3446440 : if (TREE_CODE (arg1_type) == ENUMERAL_TYPE
7682 3236330 : && TREE_CODE (arg2_type) == ENUMERAL_TYPE
7683 6604888 : && (TYPE_MAIN_VARIANT (arg1_type)
7684 3158448 : != 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 4346155 : conversion *conv = cand->convs[0];
7708 4346155 : if (conv->user_conv_p)
7709 : {
7710 65099 : conv = strip_standard_conversion (conv);
7711 65099 : arg1 = convert_like (conv, arg1, complain);
7712 : }
7713 :
7714 4346155 : if (arg2)
7715 : {
7716 3848367 : conv = cand->convs[1];
7717 3848367 : 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 4346155 : 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 25759959 : if (result || result_valid_p)
7737 : return result;
7738 :
7739 4346153 : builtin:
7740 199403809 : switch (code)
7741 : {
7742 4835628 : case MODIFY_EXPR:
7743 4835628 : return cp_build_modify_expr (loc, arg1, code2, arg2, complain);
7744 :
7745 20868482 : case INDIRECT_REF:
7746 20868482 : return cp_build_indirect_ref (loc, arg1, RO_UNARY_STAR, complain);
7747 :
7748 18597027 : case TRUTH_ANDIF_EXPR:
7749 18597027 : case TRUTH_ORIF_EXPR:
7750 18597027 : case TRUTH_AND_EXPR:
7751 18597027 : case TRUTH_OR_EXPR:
7752 18597027 : if ((complain & tf_warning) && !processing_template_decl)
7753 7767838 : warn_logical_operator (loc, code, boolean_type_node,
7754 : code_orig_arg1, arg1,
7755 : code_orig_arg2, arg2);
7756 : /* Fall through. */
7757 65728389 : case GT_EXPR:
7758 65728389 : case LT_EXPR:
7759 65728389 : case GE_EXPR:
7760 65728389 : case LE_EXPR:
7761 65728389 : case EQ_EXPR:
7762 65728389 : case NE_EXPR:
7763 65728389 : if ((complain & tf_warning)
7764 59595983 : && ((code_orig_arg1 == BOOLEAN_TYPE)
7765 59595983 : ^ (code_orig_arg2 == BOOLEAN_TYPE)))
7766 26571 : maybe_warn_bool_compare (loc, code, arg1, arg2);
7767 59595983 : if (complain & tf_warning && warn_tautological_compare)
7768 456456 : warn_tautological_cmp (loc, code, arg1, arg2);
7769 : /* Fall through. */
7770 136689459 : case SPACESHIP_EXPR:
7771 136689459 : case PLUS_EXPR:
7772 136689459 : case MINUS_EXPR:
7773 136689459 : case MULT_EXPR:
7774 136689459 : case TRUNC_DIV_EXPR:
7775 136689459 : case MAX_EXPR:
7776 136689459 : case MIN_EXPR:
7777 136689459 : case LSHIFT_EXPR:
7778 136689459 : case RSHIFT_EXPR:
7779 136689459 : case TRUNC_MOD_EXPR:
7780 136689459 : case BIT_AND_EXPR:
7781 136689459 : case BIT_IOR_EXPR:
7782 136689459 : case BIT_XOR_EXPR:
7783 136689459 : return cp_build_binary_op (loc, code, arg1, arg2, complain);
7784 :
7785 31504099 : case UNARY_PLUS_EXPR:
7786 31504099 : case NEGATE_EXPR:
7787 31504099 : case BIT_NOT_EXPR:
7788 31504099 : case TRUTH_NOT_EXPR:
7789 31504099 : case PREINCREMENT_EXPR:
7790 31504099 : case POSTINCREMENT_EXPR:
7791 31504099 : case PREDECREMENT_EXPR:
7792 31504099 : case POSTDECREMENT_EXPR:
7793 31504099 : case REALPART_EXPR:
7794 31504099 : case IMAGPART_EXPR:
7795 31504099 : case ABS_EXPR:
7796 31504099 : case CO_AWAIT_EXPR:
7797 31504099 : return cp_build_unary_op (code, arg1, false, complain);
7798 :
7799 2447645 : case ARRAY_REF:
7800 2447645 : return cp_build_array_ref (input_location, arg1, arg2, complain);
7801 :
7802 775 : case MEMBER_REF:
7803 775 : return build_m_component_ref (cp_build_indirect_ref (loc, arg1,
7804 : RO_ARROW_STAR,
7805 : complain),
7806 775 : 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 220828207 : }
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 45859197 : extract_call_expr (tree call)
7937 : {
7938 45859899 : while (TREE_CODE (call) == COMPOUND_EXPR)
7939 702 : call = TREE_OPERAND (call, 1);
7940 45859197 : if (REFERENCE_REF_P (call))
7941 10541524 : call = TREE_OPERAND (call, 0);
7942 45859197 : if (TREE_CODE (call) == TARGET_EXPR)
7943 3116656 : call = TARGET_EXPR_INITIAL (call);
7944 :
7945 45859197 : if (TREE_CODE (call) != CALL_EXPR
7946 860019 : && TREE_CODE (call) != AGGR_INIT_EXPR
7947 751534 : && call != error_mark_node)
7948 751516 : 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 680678 : second_parm_is_size_t (tree fn)
7957 : {
7958 680678 : tree t = FUNCTION_ARG_CHAIN (fn);
7959 680678 : 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 352992 : aligned_allocation_fn_p (tree t)
7972 : {
7973 352992 : if (!aligned_new_threshold)
7974 : return false;
7975 :
7976 343146 : tree a = FUNCTION_ARG_CHAIN (t);
7977 343146 : 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 5853920 : std_destroying_delete_t_p (tree t)
7984 : {
7985 5853920 : return (TYPE_CONTEXT (t) == std_node
7986 5853920 : && 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 5853935 : destroying_delete_p (tree t)
7997 : {
7998 5853935 : tree a = TYPE_ARG_TYPES (TREE_TYPE (t));
7999 5853935 : if (!a || !TREE_CHAIN (a))
8000 : return NULL_TREE;
8001 5853920 : tree type = TREE_VALUE (TREE_CHAIN (a));
8002 5853920 : 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 3999556 : usual_deallocation_fn_p (tree t, dealloc_info *di)
8018 : {
8019 3999556 : if (di) *di = dealloc_info();
8020 :
8021 : /* A template instance is never a usual deallocation function,
8022 : regardless of its signature. */
8023 3999556 : if (TREE_CODE (t) == TEMPLATE_DECL
8024 3999556 : || 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 3999544 : bool global = DECL_NAMESPACE_SCOPE_P (t);
8033 3999544 : tree chain = FUNCTION_ARG_CHAIN (t);
8034 3999544 : if (chain && destroying_delete_p (t))
8035 : {
8036 86 : if (di) di->destroying = TREE_VALUE (chain);
8037 86 : chain = TREE_CHAIN (chain);
8038 : }
8039 3999544 : if (chain
8040 3999541 : && (!global || flag_sized_deallocation)
8041 7983779 : && same_type_p (TREE_VALUE (chain), size_type_node))
8042 : {
8043 1153066 : if (di) di->sized = true;
8044 1153066 : chain = TREE_CHAIN (chain);
8045 : }
8046 3999541 : if (chain && aligned_new_threshold
8047 7982746 : && same_type_p (TREE_VALUE (chain), align_type_node))
8048 : {
8049 1711643 : if (di) di->aligned = true;
8050 1711643 : chain = TREE_CHAIN (chain);
8051 : }
8052 3999544 : 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 : PLACEMENT_ARGS is a pointer to vector containing the original placement
8079 : arguments, or NULL.
8080 :
8081 : If this call to "operator delete" is being generated as part to
8082 : deallocate memory allocated via a new-expression (as per [expr.new]
8083 : which requires that if the initialization throws an exception then
8084 : we call a deallocation function), then ALLOC_FN is the allocation
8085 : function. */
8086 :
8087 : static tree
8088 1263196 : build_op_delete_call_1 (enum tree_code code, tree addr, tree size,
8089 : bool global_p, bool coro_p, tree placement,
8090 : vec<tree, va_gc> *placement_args, tree alloc_fn,
8091 : tsubst_flags_t complain)
8092 : {
8093 1263196 : tree fn = NULL_TREE;
8094 1263196 : tree fns, fnname, type, t;
8095 1263196 : dealloc_info di_fn = { };
8096 :
8097 1263196 : if (addr == error_mark_node)
8098 : return error_mark_node;
8099 :
8100 1263196 : type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
8101 :
8102 1263196 : fnname = ovl_op_identifier (false, code);
8103 :
8104 877031 : if (CLASS_TYPE_P (type)
8105 876995 : && COMPLETE_TYPE_P (complete_type (type))
8106 2140156 : && !global_p)
8107 : /* In [class.free]
8108 :
8109 : If the result of the lookup is ambiguous or inaccessible, or if
8110 : the lookup selects a placement deallocation function, the
8111 : program is ill-formed.
8112 :
8113 : Therefore, we ask lookup_fnfields to complain about ambiguity. */
8114 : {
8115 518654 : fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1, complain);
8116 518654 : if (fns == error_mark_node)
8117 : return error_mark_node;
8118 : }
8119 : else
8120 : fns = NULL_TREE;
8121 :
8122 518651 : if (fns == NULL_TREE)
8123 1262112 : fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE);
8124 :
8125 : /* Strip const and volatile from addr. */
8126 1263193 : tree oaddr = addr;
8127 1263193 : addr = cp_convert (ptr_type_node, addr, complain);
8128 :
8129 1263193 : tree excluded_destroying = NULL_TREE;
8130 :
8131 1263193 : if (placement)
8132 : {
8133 : /* For a placement allocation function, the matching deallocation
8134 : function is selected as follows:
8135 :
8136 : - Each candidate that is a function template is replaced by the
8137 : function template specializations (if any) generated using template
8138 : argument deduction with arguments as specified below.
8139 : - Each candidate whose parameter-type-list is not identical to that
8140 : of the allocation function, ignoring their respective first
8141 : parameters, is removed from the set of candidates.
8142 : - Each candidate whose associated constraints (if any) are not
8143 : satisfied is removed from the set of candidates.
8144 : - If exactly one function remains, that function is selected.
8145 : - Otherwise, no deallocation function is selected. */
8146 681632 : t = FUNCTION_ARG_CHAIN (alloc_fn);
8147 681632 : fn = NULL_TREE;
8148 681632 : tree *args = NULL;
8149 681632 : unsigned nargs = 0;
8150 6128304 : for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns)))
8151 : {
8152 4765055 : if (TREE_CODE (elt) == TEMPLATE_DECL)
8153 : {
8154 359 : tree targs = make_tree_vec (DECL_NTPARMS (elt));
8155 359 : if (args == NULL)
8156 : {
8157 57 : nargs = vec_safe_length (placement_args);
8158 57 : args = XALLOCAVEC (tree, nargs);
8159 57 : args[0] = addr;
8160 162 : for (unsigned i = 1; i < nargs; ++i)
8161 105 : args[i] = (*placement_args)[i];
8162 : }
8163 359 : elt = fn_type_unification (elt, NULL_TREE, targs, args,
8164 : nargs, NULL_TREE,
8165 : DEDUCE_CALL, LOOKUP_NORMAL,
8166 : NULL, false, false);
8167 359 : if (elt == error_mark_node)
8168 : /* Instantiation failed. */
8169 227 : continue;
8170 : }
8171 :
8172 4764828 : if (!constraints_satisfied_p (elt))
8173 0 : continue;
8174 :
8175 : /* See if there's a match. */
8176 4764828 : if (!compparms (t, FUNCTION_ARG_CHAIN (elt)))
8177 4084120 : continue;
8178 :
8179 680708 : if (fn != NULL_TREE)
8180 : {
8181 : fn = NULL_TREE;
8182 : break;
8183 : }
8184 : else
8185 : fn = elt;
8186 : }
8187 :
8188 : /* "If the lookup finds the two-parameter form of a usual deallocation
8189 : function (3.7.4.2) and that function, considered as a placement
8190 : deallocation function, would have been selected as a match for the
8191 : allocation function, the program is ill-formed." */
8192 681632 : if (fn && second_parm_is_size_t (fn))
8193 : {
8194 9 : const char *const msg1
8195 : = G_("exception cleanup for this placement new selects "
8196 : "non-placement %<operator delete%>");
8197 9 : const char *const msg2
8198 : = G_("%qD is a usual (non-placement) deallocation "
8199 : "function in C++14 (or with %<-fsized-deallocation%>)");
8200 :
8201 : /* But if the class has an operator delete (void *), then that is
8202 : the usual deallocation function, so we shouldn't complain
8203 : about using the operator delete (void *, size_t). */
8204 9 : if (DECL_CLASS_SCOPE_P (fn))
8205 18 : for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns)))
8206 : {
8207 9 : if (usual_deallocation_fn_p (elt)
8208 9 : && FUNCTION_ARG_CHAIN (elt) == void_list_node)
8209 3 : goto ok;
8210 : }
8211 : /* Before C++14 a two-parameter global deallocation function is
8212 : always a placement deallocation function, but warn if
8213 : -Wc++14-compat. */
8214 3 : else if (!flag_sized_deallocation)
8215 : {
8216 1 : if (complain & tf_warning)
8217 : {
8218 1 : auto_diagnostic_group d;
8219 1 : if (warning (OPT_Wc__14_compat, msg1))
8220 1 : inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
8221 1 : }
8222 1 : goto ok;
8223 : }
8224 :
8225 5 : if (complain & tf_warning_or_error)
8226 : {
8227 5 : auto_diagnostic_group d;
8228 5 : if (permerror (input_location, msg1))
8229 : {
8230 : /* Only mention C++14 for namespace-scope delete. */
8231 5 : if (DECL_NAMESPACE_SCOPE_P (fn))
8232 2 : inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
8233 : else
8234 3 : inform (DECL_SOURCE_LOCATION (fn),
8235 : "%qD is a usual (non-placement) deallocation "
8236 : "function", fn);
8237 : }
8238 5 : }
8239 : else
8240 0 : return error_mark_node;
8241 1263193 : ok:;
8242 : }
8243 : }
8244 : else
8245 : /* "Any non-placement deallocation function matches a non-placement
8246 : allocation function. If the lookup finds a single matching
8247 : deallocation function, that function will be called; otherwise, no
8248 : deallocation function will be called." */
8249 5153795 : for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns)))
8250 : {
8251 3990673 : dealloc_info di_elt;
8252 3990673 : if (usual_deallocation_fn_p (elt, &di_elt))
8253 : {
8254 : /* If we're called for an EH cleanup in a new-expression, we can't
8255 : use a destroying delete; the exception was thrown before the
8256 : object was constructed. */
8257 2304610 : if (alloc_fn && di_elt.destroying)
8258 : {
8259 13 : excluded_destroying = elt;
8260 1174551 : continue;
8261 : }
8262 :
8263 2304597 : if (!fn)
8264 : {
8265 581536 : fn = elt;
8266 581536 : di_fn = di_elt;
8267 581536 : continue;
8268 : }
8269 :
8270 : /* -- If any of the deallocation functions is a destroying
8271 : operator delete, all deallocation functions that are not
8272 : destroying operator deletes are eliminated from further
8273 : consideration. */
8274 1723061 : if (di_elt.destroying != di_fn.destroying)
8275 : {
8276 12 : if (di_elt.destroying)
8277 : {
8278 6 : fn = elt;
8279 6 : di_fn = di_elt;
8280 : }
8281 12 : continue;
8282 : }
8283 :
8284 : /* -- If the type has new-extended alignment, a function with a
8285 : parameter of type std::align_val_t is preferred; otherwise a
8286 : function without such a parameter is preferred. If exactly one
8287 : preferred function is found, that function is selected and the
8288 : selection process terminates. If more than one preferred
8289 : function is found, all non-preferred functions are eliminated
8290 : from further consideration. */
8291 1723049 : if (aligned_new_threshold)
8292 : {
8293 1722782 : bool want_align = type_has_new_extended_alignment (type);
8294 1722782 : if (di_elt.aligned != di_fn.aligned)
8295 : {
8296 592990 : if (want_align == di_elt.aligned)
8297 : {
8298 555519 : fn = elt;
8299 555519 : di_fn = di_elt;
8300 : }
8301 592990 : continue;
8302 : }
8303 : }
8304 :
8305 : /* -- If the deallocation functions have class scope, the one
8306 : without a parameter of type std::size_t is selected. */
8307 1130059 : bool want_size;
8308 1130059 : if (DECL_CLASS_SCOPE_P (fn) && !coro_p)
8309 : want_size = false;
8310 :
8311 : /* -- If the type is complete and if, for the second alternative
8312 : (delete array) only, the operand is a pointer to a class type
8313 : with a non-trivial destructor or a (possibly multi-dimensional)
8314 : array thereof, the function with a parameter of type std::size_t
8315 : is selected.
8316 :
8317 : -- Otherwise, it is unspecified whether a deallocation function
8318 : with a parameter of type std::size_t is selected. */
8319 : else
8320 : {
8321 1129951 : want_size = COMPLETE_TYPE_P (type);
8322 1129951 : if (code == VEC_DELETE_EXPR
8323 1129951 : && !TYPE_VEC_NEW_USES_COOKIE (type))
8324 : /* We need a cookie to determine the array size. */
8325 : want_size = false;
8326 : }
8327 1130059 : gcc_assert (di_fn.sized != di_elt.sized);
8328 1130059 : if (want_size == di_elt.sized)
8329 : {
8330 45941 : fn = elt;
8331 45941 : di_fn = di_elt;
8332 : }
8333 : }
8334 : }
8335 :
8336 : /* If we have a matching function, call it. */
8337 1263193 : if (fn)
8338 : {
8339 1262214 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8340 :
8341 : /* If the FN is a member function, make sure that it is
8342 : accessible. */
8343 1262214 : if (BASELINK_P (fns))
8344 1011 : perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn,
8345 : complain);
8346 :
8347 : /* Core issue 901: It's ok to new a type with deleted delete. */
8348 1262214 : if (DECL_DELETED_FN (fn) && alloc_fn)
8349 : return NULL_TREE;
8350 :
8351 1262208 : tree ret;
8352 1262208 : if (placement)
8353 : {
8354 : /* The placement args might not be suitable for overload
8355 : resolution at this point, so build the call directly. */
8356 680678 : int nargs = call_expr_nargs (placement);
8357 680678 : tree *argarray = XALLOCAVEC (tree, nargs);
8358 680678 : int i;
8359 680678 : argarray[0] = addr;
8360 1361420 : for (i = 1; i < nargs; i++)
8361 680742 : argarray[i] = CALL_EXPR_ARG (placement, i);
8362 680678 : if (!mark_used (fn, complain) && !(complain & tf_error))
8363 0 : return error_mark_node;
8364 680678 : ret = build_cxx_call (fn, nargs, argarray, complain);
8365 : }
8366 : else
8367 : {
8368 581530 : tree destroying = di_fn.destroying;
8369 581530 : if (destroying)
8370 : {
8371 : /* Strip const and volatile from addr but retain the type of the
8372 : object. */
8373 28 : tree rtype = TREE_TYPE (TREE_TYPE (oaddr));
8374 28 : rtype = cv_unqualified (rtype);
8375 28 : rtype = TYPE_POINTER_TO (rtype);
8376 28 : addr = cp_convert (rtype, oaddr, complain);
8377 28 : destroying = build_functional_cast (input_location,
8378 : destroying, NULL_TREE,
8379 : complain);
8380 : }
8381 :
8382 581530 : releasing_vec args;
8383 581530 : args->quick_push (addr);
8384 581530 : if (destroying)
8385 28 : args->quick_push (destroying);
8386 581530 : if (di_fn.sized)
8387 542482 : args->quick_push (size);
8388 581530 : if (di_fn.aligned)
8389 : {
8390 18739 : tree al = build_int_cst (align_type_node, TYPE_ALIGN_UNIT (type));
8391 18739 : args->quick_push (al);
8392 : }
8393 581530 : ret = cp_build_function_call_vec (fn, &args, complain);
8394 581530 : }
8395 :
8396 : /* Set this flag for all callers of this function. In addition to
8397 : delete-expressions, this is called for deallocating coroutine state;
8398 : treat that as an implicit delete-expression. This is also called for
8399 : the delete if the constructor throws in a new-expression, and for a
8400 : deleting destructor (which implements a delete-expression). */
8401 : /* But leave this flag off for destroying delete to avoid wrong
8402 : assumptions in the optimizers. */
8403 1262208 : tree call = extract_call_expr (ret);
8404 1262208 : if (TREE_CODE (call) == CALL_EXPR && !destroying_delete_p (fn))
8405 1262174 : CALL_FROM_NEW_OR_DELETE_P (call) = 1;
8406 :
8407 : return ret;
8408 : }
8409 :
8410 : /* If there's only a destroying delete that we can't use because the
8411 : object isn't constructed yet, and we used global new, use global
8412 : delete as well. */
8413 979 : if (excluded_destroying
8414 979 : && DECL_NAMESPACE_SCOPE_P (alloc_fn))
8415 7 : return build_op_delete_call (code, addr, size, true, placement,
8416 7 : placement_args, alloc_fn, complain);
8417 :
8418 : /* [expr.new]
8419 :
8420 : If no unambiguous matching deallocation function can be found,
8421 : propagating the exception does not cause the object's memory to
8422 : be freed. */
8423 972 : if (alloc_fn)
8424 : {
8425 969 : if ((complain & tf_warning) && !placement)
8426 : {
8427 15 : auto_diagnostic_group d;
8428 15 : bool w = warning (0,
8429 : "no corresponding deallocation function for %qD",
8430 : alloc_fn);
8431 15 : if (w && excluded_destroying)
8432 3 : inform (DECL_SOURCE_LOCATION (excluded_destroying), "destroying "
8433 : "delete %qD cannot be used to release the allocated memory"
8434 : " if the initialization throws because the object is not "
8435 : "constructed yet", excluded_destroying);
8436 15 : }
8437 : return NULL_TREE;
8438 : }
8439 :
8440 3 : if (complain & tf_error)
8441 3 : error ("no suitable %<operator %s%> for %qT",
8442 3 : OVL_OP_INFO (false, code)->name, type);
8443 3 : return error_mark_node;
8444 : }
8445 :
8446 : /* Arguments as per build_op_delete_call_1 (). */
8447 :
8448 : tree
8449 1259935 : build_op_delete_call (enum tree_code code, tree addr, tree size, bool global_p,
8450 : tree placement, vec<tree, va_gc> *placement_args,
8451 : tree alloc_fn, tsubst_flags_t complain)
8452 : {
8453 1259935 : return build_op_delete_call_1 (code, addr, size, global_p, /*coro_p*/false,
8454 : placement, placement_args, alloc_fn,
8455 1259935 : complain);
8456 : }
8457 :
8458 : /* Arguments as per build_op_delete_call_1 (). */
8459 :
8460 : tree
8461 3261 : build_coroutine_op_delete_call (enum tree_code code, tree addr, tree size,
8462 : bool global_p, tree alloc_fn,
8463 : tsubst_flags_t complain)
8464 : {
8465 3261 : return build_op_delete_call_1 (code, addr, size, global_p, /*coro_p*/true,
8466 : /*placement*/NULL_TREE,
8467 3261 : /*placement_args*/NULL, alloc_fn, complain);
8468 : }
8469 :
8470 : /* Issue diagnostics about a disallowed access of DECL, using DIAG_DECL
8471 : in the diagnostics.
8472 :
8473 : If ISSUE_ERROR is true, then issue an error about the access, followed
8474 : by a note showing the declaration. Otherwise, just show the note.
8475 :
8476 : DIAG_DECL and DIAG_LOCATION will almost always be the same.
8477 : DIAG_LOCATION is just another DECL. NO_ACCESS_REASON is an optional
8478 : parameter used to specify why DECL wasn't accessible (e.g. ak_private
8479 : would be because DECL was private). If not using NO_ACCESS_REASON,
8480 : then it must be ak_none, and the access failure reason will be
8481 : figured out by looking at the protection of DECL. */
8482 :
8483 : void
8484 1187 : complain_about_access (tree decl, tree diag_decl, tree diag_location,
8485 : bool issue_error, access_kind no_access_reason)
8486 : {
8487 : /* If we have not already figured out why DECL is inaccessible... */
8488 1187 : if (no_access_reason == ak_none)
8489 : {
8490 : /* Examine the access of DECL to find out why. */
8491 1005 : if (TREE_PRIVATE (decl))
8492 : no_access_reason = ak_private;
8493 225 : else if (TREE_PROTECTED (decl))
8494 180 : no_access_reason = ak_protected;
8495 : }
8496 :
8497 : /* Now generate an error message depending on calculated access. */
8498 1187 : auto_diagnostic_group d;
8499 1187 : if (no_access_reason == ak_private)
8500 : {
8501 962 : if (issue_error)
8502 948 : error ("%q#D is private within this context", diag_decl);
8503 962 : inform (DECL_SOURCE_LOCATION (diag_location), "declared private here");
8504 : }
8505 225 : else if (no_access_reason == ak_protected)
8506 : {
8507 180 : if (issue_error)
8508 166 : error ("%q#D is protected within this context", diag_decl);
8509 180 : inform (DECL_SOURCE_LOCATION (diag_location), "declared protected here");
8510 : }
8511 : /* Couldn't figure out why DECL is inaccessible, so just say it's
8512 : inaccessible. */
8513 : else
8514 : {
8515 45 : if (issue_error)
8516 45 : error ("%q#D is inaccessible within this context", diag_decl);
8517 45 : inform (DECL_SOURCE_LOCATION (diag_decl), "declared here");
8518 : }
8519 1187 : }
8520 :
8521 : /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
8522 : bitwise or of LOOKUP_* values. If any errors are warnings are
8523 : generated, set *DIAGNOSTIC_FN to "error" or "warning",
8524 : respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
8525 : to NULL. */
8526 :
8527 : static tree
8528 14356345 : build_temp (tree expr, tree type, int flags,
8529 : enum diagnostics::kind *diagnostic_kind, tsubst_flags_t complain)
8530 : {
8531 14356345 : int savew, savee;
8532 :
8533 14356345 : *diagnostic_kind = diagnostics::kind::unspecified;
8534 :
8535 : /* If the source is a packed field, calling the copy constructor will require
8536 : binding the field to the reference parameter to the copy constructor, and
8537 : we'll end up with an infinite loop. If we can use a bitwise copy, then
8538 : do that now. */
8539 14356345 : if ((lvalue_kind (expr) & clk_packed)
8540 6 : && CLASS_TYPE_P (TREE_TYPE (expr))
8541 14356351 : && !type_has_nontrivial_copy_init (TREE_TYPE (expr)))
8542 3 : return get_target_expr (expr, complain);
8543 :
8544 : /* In decltype, we might have decided not to wrap this call in a TARGET_EXPR.
8545 : But it turns out to be a subexpression, so perform temporary
8546 : materialization now. */
8547 14356342 : if (TREE_CODE (expr) == CALL_EXPR
8548 6 : && CLASS_TYPE_P (type)
8549 14356348 : && same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
8550 3 : expr = build_cplus_new (type, expr, complain);
8551 :
8552 14356342 : savew = warningcount + werrorcount, savee = errorcount;
8553 14356342 : releasing_vec args (make_tree_vector_single (expr));
8554 14356342 : expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
8555 : &args, type, flags, complain);
8556 14356342 : if (warningcount + werrorcount > savew)
8557 0 : *diagnostic_kind = diagnostics::kind::warning;
8558 14356342 : else if (errorcount > savee)
8559 78 : *diagnostic_kind = diagnostics::kind::error;
8560 14356342 : return expr;
8561 14356342 : }
8562 :
8563 : /* Get any location for EXPR, falling back to input_location.
8564 :
8565 : If the result is in a system header and is the virtual location for
8566 : a token coming from the expansion of a macro, unwind it to the
8567 : location of the expansion point of the macro (e.g. to avoid the
8568 : diagnostic being suppressed for expansions of NULL where "NULL" is
8569 : in a system header). */
8570 :
8571 : static location_t
8572 2438547 : get_location_for_expr_unwinding_for_system_header (tree expr)
8573 : {
8574 2438547 : location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
8575 2438547 : loc = expansion_point_location_if_in_system_header (loc);
8576 2438547 : return loc;
8577 : }
8578 :
8579 : /* Perform warnings about peculiar, but valid, conversions from/to NULL.
8580 : Also handle a subset of zero as null warnings.
8581 : EXPR is implicitly converted to type TOTYPE.
8582 : FN and ARGNUM are used for diagnostics. */
8583 :
8584 : static void
8585 550297364 : conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
8586 : {
8587 : /* Issue warnings about peculiar, but valid, uses of NULL. */
8588 550297364 : if (TREE_CODE (totype) != BOOLEAN_TYPE
8589 : && ARITHMETIC_TYPE_P (totype)
8590 188639035 : && null_node_p (expr))
8591 : {
8592 94 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8593 94 : if (fn)
8594 : {
8595 33 : auto_diagnostic_group d;
8596 33 : if (warning_at (loc, OPT_Wconversion_null,
8597 : "passing NULL to non-pointer argument %P of %qD",
8598 : argnum, fn))
8599 27 : inform (get_fndecl_argument_location (fn, argnum),
8600 : "declared here");
8601 33 : }
8602 : else
8603 61 : warning_at (loc, OPT_Wconversion_null,
8604 : "converting to non-pointer type %qT from NULL", totype);
8605 : }
8606 :
8607 : /* Issue warnings if "false" is converted to a NULL pointer */
8608 550297270 : else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
8609 550297270 : && TYPE_PTR_P (totype))
8610 : {
8611 7 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8612 7 : if (fn)
8613 : {
8614 3 : auto_diagnostic_group d;
8615 3 : if (warning_at (loc, OPT_Wconversion_null,
8616 : "converting %<false%> to pointer type for argument "
8617 : "%P of %qD", argnum, fn))
8618 3 : inform (get_fndecl_argument_location (fn, argnum),
8619 : "declared here");
8620 3 : }
8621 : else
8622 4 : warning_at (loc, OPT_Wconversion_null,
8623 : "converting %<false%> to pointer type %qT", totype);
8624 : }
8625 : /* Handle zero as null pointer warnings for cases other
8626 : than EQ_EXPR and NE_EXPR */
8627 506978573 : else if ((TYPE_PTR_OR_PTRMEM_P (totype) || NULLPTR_TYPE_P (totype))
8628 550614125 : && null_ptr_cst_p (expr))
8629 : {
8630 2438446 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8631 2438446 : maybe_warn_zero_as_null_pointer_constant (expr, loc);
8632 : }
8633 550297364 : }
8634 :
8635 : /* We gave a diagnostic during a conversion. If this was in the second
8636 : standard conversion sequence of a user-defined conversion sequence, say
8637 : which user-defined conversion. */
8638 :
8639 : static void
8640 5260 : maybe_print_user_conv_context (conversion *convs)
8641 : {
8642 5260 : if (convs->user_conv_p)
8643 167 : for (conversion *t = convs; t; t = next_conversion (t))
8644 142 : if (t->kind == ck_user)
8645 : {
8646 43 : print_z_candidate (0, N_(" after user-defined conversion:"),
8647 : t->cand);
8648 43 : break;
8649 : }
8650 5260 : }
8651 :
8652 : /* Locate the parameter with the given index within FNDECL.
8653 : ARGNUM is zero based, -1 indicates the `this' argument of a method.
8654 : Return the location of the FNDECL itself if there are problems. */
8655 :
8656 : location_t
8657 9165980 : get_fndecl_argument_location (tree fndecl, int argnum)
8658 : {
8659 : /* The locations of implicitly-declared functions are likely to be
8660 : more meaningful than those of their parameters. */
8661 9165980 : if (DECL_ARTIFICIAL (fndecl))
8662 297 : return DECL_SOURCE_LOCATION (fndecl);
8663 :
8664 9165683 : if (argnum == -1)
8665 51 : return DECL_SOURCE_LOCATION (fndecl);
8666 :
8667 9165632 : int i;
8668 9165632 : tree param;
8669 :
8670 : /* Locate param by index within DECL_ARGUMENTS (fndecl). */
8671 9165632 : for (i = 0, param = FUNCTION_FIRST_USER_PARM (fndecl);
8672 23019101 : i < argnum && param;
8673 13853469 : i++, param = TREE_CHAIN (param))
8674 : ;
8675 :
8676 : /* If something went wrong (e.g. if we have a builtin and thus no arguments),
8677 : return the location of FNDECL. */
8678 9165632 : if (param == NULL)
8679 40 : return DECL_SOURCE_LOCATION (fndecl);
8680 :
8681 9165592 : return DECL_SOURCE_LOCATION (param);
8682 : }
8683 :
8684 : /* If FNDECL is non-NULL, issue a note highlighting ARGNUM
8685 : within its declaration (or the fndecl itself if something went
8686 : wrong). */
8687 :
8688 : void
8689 7011 : maybe_inform_about_fndecl_for_bogus_argument_init (tree fn, int argnum,
8690 : const char *highlight_color)
8691 : {
8692 7011 : if (fn)
8693 : {
8694 1123 : gcc_rich_location richloc (get_fndecl_argument_location (fn, argnum));
8695 1123 : richloc.set_highlight_color (highlight_color);
8696 1123 : inform (&richloc,
8697 : "initializing argument %P of %qD", argnum, fn);
8698 1123 : }
8699 7011 : }
8700 :
8701 : /* Maybe warn about C++20 Conversions to arrays of unknown bound. C is
8702 : the conversion, EXPR is the expression we're converting. */
8703 :
8704 : static void
8705 39558545 : maybe_warn_array_conv (location_t loc, conversion *c, tree expr)
8706 : {
8707 39558545 : if (cxx_dialect >= cxx20)
8708 : return;
8709 :
8710 436824 : tree type = TREE_TYPE (expr);
8711 436824 : type = strip_pointer_operator (type);
8712 :
8713 436824 : if (TREE_CODE (type) != ARRAY_TYPE
8714 436824 : || TYPE_DOMAIN (type) == NULL_TREE)
8715 : return;
8716 :
8717 1858 : if (pedantic && conv_binds_to_array_of_unknown_bound (c))
8718 10 : pedwarn (loc, OPT_Wc__20_extensions,
8719 : "conversions to arrays of unknown bound "
8720 : "are only available with %<-std=c++20%> or %<-std=gnu++20%>");
8721 : }
8722 :
8723 : /* We call this recursively in convert_like_internal. */
8724 : static tree convert_like (conversion *, tree, tree, int, bool, bool, bool,
8725 : tsubst_flags_t);
8726 :
8727 : /* Adjust the result EXPR of a conversion to the expected type TOTYPE, which
8728 : must be equivalent but might be a typedef. */
8729 :
8730 : static tree
8731 932500408 : maybe_adjust_type_name (tree type, tree expr, conversion_kind kind)
8732 : {
8733 932500408 : if (expr == error_mark_node
8734 932500357 : || processing_template_decl)
8735 : return expr;
8736 :
8737 818818848 : tree etype = TREE_TYPE (expr);
8738 818818848 : if (etype == type)
8739 : return expr;
8740 :
8741 107948298 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p (etype, type)
8742 : || is_bitfield_expr_with_lowered_type (expr)
8743 : || seen_error ());
8744 :
8745 107948298 : if (SCALAR_TYPE_P (type)
8746 102064573 : && (kind == ck_rvalue
8747 : /* ??? We should be able to do this for ck_identity of more prvalue
8748 : expressions, but checking !obvalue_p here breaks, so for now let's
8749 : just handle NON_LVALUE_EXPR (such as the location wrapper for a
8750 : literal). Maybe we want to express already-rvalue in the
8751 : conversion somehow? */
8752 91595492 : || TREE_CODE (expr) == NON_LVALUE_EXPR))
8753 12033633 : expr = build_nop (type, expr);
8754 :
8755 : return expr;
8756 : }
8757 :
8758 : /* Perform the conversions in CONVS on the expression EXPR. FN and
8759 : ARGNUM are used for diagnostics. ARGNUM is zero based, -1
8760 : indicates the `this' argument of a method. INNER is nonzero when
8761 : being called to continue a conversion chain. It is negative when a
8762 : reference binding will be applied, positive otherwise. If
8763 : ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
8764 : conversions will be emitted if appropriate. If C_CAST_P is true,
8765 : this conversion is coming from a C-style cast; in that case,
8766 : conversions to inaccessible bases are permitted. */
8767 :
8768 : static tree
8769 1135845542 : convert_like_internal (conversion *convs, tree expr, tree fn, int argnum,
8770 : bool issue_conversion_warnings, bool c_cast_p,
8771 : bool nested_p, tsubst_flags_t complain)
8772 : {
8773 1135845542 : tree totype = convs->type;
8774 1135845542 : enum diagnostics::kind diag_kind;
8775 1135845542 : int flags;
8776 1135845542 : location_t loc = cp_expr_loc_or_input_loc (expr);
8777 1135845542 : const bool stub_object_p = is_stub_object (expr);
8778 :
8779 1135845542 : if (convs->bad_p && !(complain & tf_error))
8780 46773 : return error_mark_node;
8781 :
8782 1135798769 : gcc_checking_assert (!TYPE_REF_P (TREE_TYPE (expr)));
8783 :
8784 1135798769 : if (convs->bad_p
8785 6982 : && convs->kind != ck_user
8786 6887 : && convs->kind != ck_list
8787 6881 : && convs->kind != ck_ambig
8788 6881 : && (convs->kind != ck_ref_bind
8789 5267 : || (convs->user_conv_p && next_conversion (convs)->bad_p))
8790 1635 : && (convs->kind != ck_rvalue
8791 15 : || SCALAR_TYPE_P (totype))
8792 1135800395 : && convs->kind != ck_base)
8793 : {
8794 1626 : int complained = 0;
8795 1626 : conversion *t = convs;
8796 1626 : auto_diagnostic_group d;
8797 :
8798 : /* Give a helpful error if this is bad because of excess braces. */
8799 46 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8800 46 : && SCALAR_TYPE_P (totype)
8801 34 : && CONSTRUCTOR_NELTS (expr) > 0
8802 1660 : && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
8803 : {
8804 11 : complained = permerror (loc, "too many braces around initializer "
8805 : "for %qT", totype);
8806 33 : while (BRACE_ENCLOSED_INITIALIZER_P (expr)
8807 55 : && CONSTRUCTOR_NELTS (expr) == 1)
8808 22 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
8809 : }
8810 :
8811 : /* Give a helpful error if this is bad because a conversion to bool
8812 : from std::nullptr_t requires direct-initialization. */
8813 1626 : if (NULLPTR_TYPE_P (TREE_TYPE (expr))
8814 1626 : && TREE_CODE (totype) == BOOLEAN_TYPE)
8815 15 : complained = permerror (loc, "converting to %qH from %qI requires "
8816 : "direct-initialization",
8817 15 : totype, TREE_TYPE (expr));
8818 :
8819 1626 : if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (expr))
8820 105 : && SCALAR_FLOAT_TYPE_P (totype)
8821 1731 : && (extended_float_type_p (TREE_TYPE (expr))
8822 27 : || extended_float_type_p (totype)))
8823 105 : switch (cp_compare_floating_point_conversion_ranks (TREE_TYPE (expr),
8824 : totype))
8825 : {
8826 102 : case 2:
8827 102 : if (pedwarn (loc, OPT_Wnarrowing, "ISO C++ does not allow "
8828 : "converting to %qH from %qI with greater "
8829 102 : "conversion rank", totype, TREE_TYPE (expr)))
8830 1626 : complained = 1;
8831 21 : else if (!complained)
8832 1626 : complained = -1;
8833 : break;
8834 3 : case 3:
8835 3 : if (pedwarn (loc, OPT_Wnarrowing, "ISO C++ does not allow "
8836 : "converting to %qH from %qI with unordered "
8837 3 : "conversion rank", totype, TREE_TYPE (expr)))
8838 : complained = 1;
8839 0 : else if (!complained)
8840 1626 : complained = -1;
8841 : break;
8842 : default:
8843 : break;
8844 : }
8845 :
8846 3268 : for (; t ; t = next_conversion (t))
8847 : {
8848 3259 : if (t->kind == ck_user && t->cand->reason)
8849 : {
8850 52 : auto_diagnostic_group d;
8851 52 : complained = permerror (loc, "invalid user-defined conversion "
8852 52 : "from %qH to %qI", TREE_TYPE (expr),
8853 : totype);
8854 52 : if (complained)
8855 : {
8856 52 : auto_diagnostic_nesting_level sentinel;
8857 52 : print_z_candidate (loc, N_("candidate is:"), t->cand);
8858 52 : }
8859 52 : expr = convert_like (t, expr, fn, argnum,
8860 : /*issue_conversion_warnings=*/false,
8861 : /*c_cast_p=*/false, /*nested_p=*/true,
8862 : complain);
8863 52 : break;
8864 52 : }
8865 3207 : else if (t->kind == ck_user || !t->bad_p)
8866 : {
8867 1565 : expr = convert_like (t, expr, fn, argnum,
8868 : /*issue_conversion_warnings=*/false,
8869 : /*c_cast_p=*/false, /*nested_p=*/true,
8870 : complain);
8871 1565 : if (t->bad_p)
8872 0 : complained = 1;
8873 : break;
8874 : }
8875 1642 : else if (t->kind == ck_ambig)
8876 0 : return convert_like (t, expr, fn, argnum,
8877 : /*issue_conversion_warnings=*/false,
8878 : /*c_cast_p=*/false, /*nested_p=*/true,
8879 0 : complain);
8880 1642 : else if (t->kind == ck_identity)
8881 : break;
8882 : }
8883 1626 : if (!complained && stub_object_p)
8884 : {
8885 : /* An error diagnosed within a trait, don't give extra labels. */
8886 12 : error_at (loc, "invalid conversion from %qH to %qI",
8887 6 : TREE_TYPE (expr), totype);
8888 6 : complained = 1;
8889 : }
8890 1620 : else if (!complained && expr != error_mark_node)
8891 : {
8892 1435 : range_label_for_type_mismatch label (TREE_TYPE (expr), totype);
8893 1435 : gcc_rich_location richloc (loc, &label, highlight_colors::percent_h);
8894 1435 : complained = permerror (&richloc,
8895 : "invalid conversion from %qH to %qI",
8896 1435 : TREE_TYPE (expr), totype);
8897 1435 : if (complained)
8898 1380 : maybe_emit_indirection_note (loc, expr, totype);
8899 1435 : }
8900 1626 : if (convs->kind == ck_ref_bind)
8901 21 : expr = convert_to_reference (totype, expr, CONV_IMPLICIT,
8902 : LOOKUP_NORMAL, NULL_TREE,
8903 : complain);
8904 : else
8905 1605 : expr = cp_convert (totype, expr, complain);
8906 1626 : if (complained == 1)
8907 1548 : maybe_inform_about_fndecl_for_bogus_argument_init
8908 1548 : (fn, argnum, highlight_colors::percent_i);
8909 : return expr;
8910 1626 : }
8911 :
8912 1135797143 : if (issue_conversion_warnings && (complain & tf_warning))
8913 550297364 : conversion_null_warnings (totype, expr, fn, argnum);
8914 :
8915 1135797143 : switch (convs->kind)
8916 : {
8917 6711205 : case ck_user:
8918 6711205 : {
8919 6711205 : struct z_candidate *cand = convs->cand;
8920 :
8921 6711205 : if (cand == NULL)
8922 : /* We chose the surrogate function from add_conv_candidate, now we
8923 : actually need to build the conversion. */
8924 56 : cand = build_user_type_conversion_1 (totype, expr,
8925 : LOOKUP_NO_CONVERSION, complain);
8926 :
8927 6711205 : tree convfn = cand->fn;
8928 :
8929 : /* When converting from an init list we consider explicit
8930 : constructors, but actually trying to call one is an error. */
8931 7200063 : if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
8932 10107 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
8933 : /* Unless this is for direct-list-initialization. */
8934 10104 : && (!CONSTRUCTOR_IS_DIRECT_INIT (expr) || convs->need_temporary_p)
8935 : /* And in C++98 a default constructor can't be explicit. */
8936 6711445 : && cxx_dialect >= cxx11)
8937 : {
8938 238 : if (!(complain & tf_error))
8939 54 : return error_mark_node;
8940 184 : location_t loc = location_of (expr);
8941 184 : if (CONSTRUCTOR_NELTS (expr) == 0
8942 184 : && FUNCTION_FIRST_USER_PARMTYPE (convfn) != void_list_node)
8943 : {
8944 9 : auto_diagnostic_group d;
8945 9 : if (pedwarn (loc, 0, "converting to %qT from initializer list "
8946 : "would use explicit constructor %qD",
8947 : totype, convfn))
8948 : {
8949 9 : inform (DECL_SOURCE_LOCATION (convfn), "%qD declared here",
8950 : convfn);
8951 9 : inform (loc, "in C++11 and above a default constructor "
8952 : "can be explicit");
8953 : }
8954 9 : }
8955 : else
8956 : {
8957 175 : auto_diagnostic_group d;
8958 175 : error ("converting to %qT from initializer list would use "
8959 : "explicit constructor %qD", totype, convfn);
8960 175 : inform (DECL_SOURCE_LOCATION (convfn), "%qD declared here",
8961 : convfn);
8962 175 : }
8963 : }
8964 :
8965 : /* If we're initializing from {}, it's value-initialization. */
8966 880310 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8967 880288 : && CONSTRUCTOR_NELTS (expr) == 0
8968 149736 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype)
8969 6860860 : && !processing_template_decl)
8970 : {
8971 149709 : if (abstract_virtuals_error (NULL_TREE, totype, complain))
8972 12 : return error_mark_node;
8973 149697 : expr = build_value_init (totype, complain);
8974 149697 : expr = get_target_expr (expr, complain);
8975 149697 : if (expr != error_mark_node)
8976 149557 : TARGET_EXPR_LIST_INIT_P (expr) = true;
8977 : return expr;
8978 : }
8979 :
8980 : /* We don't know here whether EXPR is being used as an lvalue or
8981 : rvalue, but we know it's read. */
8982 6561442 : mark_exp_read (expr);
8983 :
8984 : /* Give the conversion call the location of EXPR rather than the
8985 : location of the context that caused the conversion. */
8986 6561442 : iloc_sentinel ils (loc);
8987 :
8988 : /* Pass LOOKUP_NO_CONVERSION so rvalue/base handling knows not to allow
8989 : any more UDCs. */
8990 6561442 : expr = build_over_call (cand, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
8991 : complain);
8992 :
8993 : /* If this is a constructor or a function returning an aggr type,
8994 : we need to build up a TARGET_EXPR. */
8995 13122884 : if (DECL_CONSTRUCTOR_P (convfn))
8996 : {
8997 2936699 : expr = build_cplus_new (totype, expr, complain);
8998 :
8999 : /* Remember that this was list-initialization. */
9000 2936699 : if (convs->check_narrowing && expr != error_mark_node)
9001 755771 : TARGET_EXPR_LIST_INIT_P (expr) = true;
9002 : }
9003 :
9004 6561442 : return expr;
9005 6561442 : }
9006 801985354 : case ck_identity:
9007 801985354 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
9008 : {
9009 1167872 : int nelts = CONSTRUCTOR_NELTS (expr);
9010 180799 : if (nelts == 0)
9011 987073 : expr = build_value_init (totype, complain);
9012 180799 : else if (nelts == 1)
9013 180799 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
9014 : else
9015 0 : gcc_unreachable ();
9016 : }
9017 801985354 : expr = mark_use (expr, /*rvalue_p=*/!convs->rvaluedness_matches_p,
9018 : /*read_p=*/true, UNKNOWN_LOCATION,
9019 : /*reject_builtin=*/true);
9020 :
9021 801985354 : if (type_unknown_p (expr))
9022 35362 : expr = instantiate_type (totype, expr, complain);
9023 801985354 : if (!nested_p && TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
9024 104639 : expr = cp_convert (totype, TREE_OPERAND (expr, 0), complain);
9025 801985354 : if (expr == null_node
9026 801985354 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype))
9027 : /* If __null has been converted to an integer type, we do not want to
9028 : continue to warn about uses of EXPR as an integer, rather than as a
9029 : pointer. */
9030 153142 : expr = build_int_cst (totype, 0);
9031 801985354 : return maybe_adjust_type_name (totype, expr, convs->kind);
9032 53 : case ck_ambig:
9033 : /* We leave bad_p off ck_ambig because overload resolution considers
9034 : it valid, it just fails when we try to perform it. So we need to
9035 : check complain here, too. */
9036 53 : if (complain & tf_error)
9037 : {
9038 : /* Call build_user_type_conversion again for the error. */
9039 53 : auto_diagnostic_group d;
9040 3 : int flags = (convs->need_temporary_p
9041 53 : ? LOOKUP_IMPLICIT : LOOKUP_NORMAL);
9042 53 : build_user_type_conversion (totype, convs->u.expr, flags, complain);
9043 53 : gcc_assert (seen_error ());
9044 53 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9045 53 : }
9046 53 : return error_mark_node;
9047 :
9048 16192 : case ck_list:
9049 16192 : {
9050 : /* Conversion to std::initializer_list<T>. */
9051 16192 : tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
9052 16192 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (expr);
9053 16192 : tree array;
9054 :
9055 16192 : if (tree init = maybe_init_list_as_array (elttype, expr))
9056 : {
9057 132 : elttype
9058 132 : = cp_build_qualified_type (elttype, (cp_type_quals (elttype)
9059 : | TYPE_QUAL_CONST));
9060 132 : tree index_type = TYPE_DOMAIN (TREE_TYPE (init));
9061 132 : array = build_cplus_array_type (elttype, index_type);
9062 132 : len = TREE_INT_CST_LOW (TYPE_MAX_VALUE (index_type)) + 1;
9063 132 : array = build_vec_init_expr (array, init, complain);
9064 132 : array = get_target_expr (array);
9065 132 : array = cp_build_addr_expr (array, complain);
9066 : }
9067 16060 : else if (len)
9068 : {
9069 15366 : tree val;
9070 15366 : unsigned ix;
9071 15366 : tree new_ctor = build_constructor (init_list_type_node, NULL);
9072 :
9073 : /* Convert all the elements. */
9074 124142 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
9075 : {
9076 93425 : if (TREE_CODE (val) == RAW_DATA_CST)
9077 : {
9078 : /* For conversion to initializer_list<unsigned char> or
9079 : initializer_list<char> or initializer_list<signed char>
9080 : we can optimize and keep RAW_DATA_CST with adjusted
9081 : type if we report narrowing errors if needed, for
9082 : others this converts each element separately. */
9083 48 : if (convs->u.list[ix]->kind == ck_std)
9084 : {
9085 18 : tree et = convs->u.list[ix]->type;
9086 18 : conversion *next = next_conversion (convs->u.list[ix]);
9087 18 : gcc_assert (et
9088 : && (TREE_CODE (et) == INTEGER_TYPE
9089 : || is_byte_access_type (et))
9090 : && TYPE_PRECISION (et) == CHAR_BIT
9091 : && next
9092 : && next->kind == ck_identity);
9093 18 : if (!TYPE_UNSIGNED (et)
9094 : /* For RAW_DATA_CST, TREE_TYPE (val) can be
9095 : either integer_type_node (when it has been
9096 : created by the lexer from CPP_EMBED) or
9097 : after digestion/conversion some integral
9098 : type with CHAR_BIT precision. For int with
9099 : precision higher than CHAR_BIT or unsigned char
9100 : diagnose narrowing conversions from
9101 : that int/unsigned char to signed char if any
9102 : byte has most significant bit set. */
9103 18 : && (TYPE_UNSIGNED (TREE_TYPE (val))
9104 12 : || (TYPE_PRECISION (TREE_TYPE (val))
9105 : > CHAR_BIT)))
9106 2460 : for (int i = 0; i < RAW_DATA_LENGTH (val); ++i)
9107 : {
9108 2454 : if (RAW_DATA_SCHAR_ELT (val, i) >= 0)
9109 2448 : continue;
9110 6 : else if (complain & tf_error)
9111 : {
9112 6 : location_t loc
9113 6 : = cp_expr_loc_or_input_loc (val);
9114 6 : int savederrorcount = errorcount;
9115 18 : permerror_opt (loc, OPT_Wnarrowing,
9116 : "narrowing conversion of "
9117 : "%qd from %qH to %qI",
9118 6 : RAW_DATA_UCHAR_ELT (val, i),
9119 6 : TREE_TYPE (val), et);
9120 6 : if (errorcount != savederrorcount)
9121 6 : return error_mark_node;
9122 : }
9123 : else
9124 0 : return error_mark_node;
9125 : }
9126 12 : tree sub = copy_node (val);
9127 12 : TREE_TYPE (sub) = et;
9128 12 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor),
9129 : NULL_TREE, sub);
9130 : }
9131 : else
9132 : {
9133 30 : conversion *conv = convs->u.list[ix];
9134 30 : gcc_assert (conv->kind == ck_list);
9135 15495 : for (int i = 0; i < RAW_DATA_LENGTH (val); ++i)
9136 : {
9137 15465 : tree elt
9138 15465 : = build_int_cst (TREE_TYPE (val),
9139 15465 : RAW_DATA_UCHAR_ELT (val, i));
9140 15465 : tree sub
9141 15465 : = convert_like (conv->u.list[i], elt,
9142 : fn, argnum, false, false,
9143 : /*nested_p=*/true, complain);
9144 15465 : if (sub == error_mark_node)
9145 : return sub;
9146 15465 : if (!check_narrowing (TREE_TYPE (sub), elt,
9147 : complain))
9148 0 : return error_mark_node;
9149 15465 : tree nc = new_ctor;
9150 15465 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (nc),
9151 : NULL_TREE, sub);
9152 15465 : if (!TREE_CONSTANT (sub))
9153 11679 : TREE_CONSTANT (new_ctor) = false;
9154 : }
9155 : }
9156 42 : len += RAW_DATA_LENGTH (val) - 1;
9157 42 : continue;
9158 42 : }
9159 93377 : tree sub = convert_like (convs->u.list[ix], val, fn,
9160 : argnum, false, false,
9161 : /*nested_p=*/true, complain);
9162 93377 : if (sub == error_mark_node)
9163 : return sub;
9164 1652 : if (!BRACE_ENCLOSED_INITIALIZER_P (val)
9165 93388 : && !check_narrowing (TREE_TYPE (sub), val, complain))
9166 0 : return error_mark_node;
9167 93368 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor),
9168 : NULL_TREE, sub);
9169 93368 : if (!TREE_CONSTANT (sub))
9170 11451 : TREE_CONSTANT (new_ctor) = false;
9171 : }
9172 : /* Build up the array. */
9173 15351 : elttype
9174 15351 : = cp_build_qualified_type (elttype, (cp_type_quals (elttype)
9175 : | TYPE_QUAL_CONST));
9176 15351 : array = build_array_of_n_type (elttype, len);
9177 15351 : array = finish_compound_literal (array, new_ctor, complain);
9178 : /* This is dubious now, should be blessed by P2752. */
9179 15351 : DECL_MERGEABLE (TARGET_EXPR_SLOT (array)) = true;
9180 15351 : array = cp_build_addr_expr (array, complain);
9181 : }
9182 : else
9183 694 : array = nullptr_node;
9184 :
9185 16177 : array = cp_convert (build_pointer_type (elttype), array, complain);
9186 16177 : if (array == error_mark_node)
9187 : return error_mark_node;
9188 :
9189 : /* Build up the initializer_list object. Note: fail gracefully
9190 : if the object cannot be completed because, for example, no
9191 : definition is provided (c++/80956). */
9192 16177 : totype = complete_type_or_maybe_complain (totype, NULL_TREE, complain);
9193 16177 : if (!totype)
9194 0 : return error_mark_node;
9195 16177 : tree field = next_aggregate_field (TYPE_FIELDS (totype));
9196 16177 : vec<constructor_elt, va_gc> *vec = NULL;
9197 16177 : CONSTRUCTOR_APPEND_ELT (vec, field, array);
9198 16177 : field = next_aggregate_field (DECL_CHAIN (field));
9199 16177 : CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
9200 16177 : tree new_ctor = build_constructor (totype, vec);
9201 16177 : return get_target_expr (new_ctor, complain);
9202 : }
9203 :
9204 1327750 : case ck_aggr:
9205 1327750 : if (TREE_CODE (totype) == COMPLEX_TYPE)
9206 : {
9207 28626 : tree real = CONSTRUCTOR_ELT (expr, 0)->value;
9208 28626 : tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
9209 28626 : real = perform_implicit_conversion (TREE_TYPE (totype),
9210 : real, complain);
9211 28626 : imag = perform_implicit_conversion (TREE_TYPE (totype),
9212 : imag, complain);
9213 28626 : expr = build2 (COMPLEX_EXPR, totype, real, imag);
9214 28626 : return expr;
9215 : }
9216 1299124 : expr = reshape_init (totype, expr, complain);
9217 1299124 : expr = get_target_expr (digest_init (totype, expr, complain),
9218 : complain);
9219 1299124 : if (expr != error_mark_node)
9220 1299113 : TARGET_EXPR_LIST_INIT_P (expr) = true;
9221 : return expr;
9222 :
9223 325756589 : default:
9224 325756589 : break;
9225 325756589 : };
9226 :
9227 325756589 : conversion *nc = next_conversion (convs);
9228 325756589 : if (convs->kind == ck_ref_bind && nc->kind == ck_qual
9229 8914 : && !convs->need_temporary_p)
9230 : /* direct_reference_binding might have inserted a ck_qual under
9231 : this ck_ref_bind for the benefit of conversion sequence ranking.
9232 : Don't actually perform that conversion. */
9233 6499 : nc = next_conversion (nc);
9234 :
9235 587822642 : expr = convert_like (nc, expr, fn, argnum,
9236 : convs->kind == ck_ref_bind
9237 : ? issue_conversion_warnings : false,
9238 : c_cast_p, /*nested_p=*/true, complain & ~tf_no_cleanup);
9239 325756589 : if (expr == error_mark_node)
9240 : return error_mark_node;
9241 :
9242 325755759 : switch (convs->kind)
9243 : {
9244 144646588 : case ck_rvalue:
9245 144646588 : expr = decay_conversion (expr, complain);
9246 144646588 : if (expr == error_mark_node)
9247 : {
9248 15 : if (complain & tf_error)
9249 : {
9250 12 : auto_diagnostic_group d;
9251 12 : maybe_print_user_conv_context (convs);
9252 12 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9253 12 : }
9254 15 : return error_mark_node;
9255 : }
9256 :
9257 144646573 : if ((complain & tf_warning) && fn
9258 41660116 : && warn_suggest_attribute_format)
9259 : {
9260 706 : tree rhstype = TREE_TYPE (expr);
9261 706 : const enum tree_code coder = TREE_CODE (rhstype);
9262 706 : const enum tree_code codel = TREE_CODE (totype);
9263 706 : if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
9264 250 : && coder == codel
9265 956 : && check_missing_format_attribute (totype, rhstype))
9266 6 : warning (OPT_Wsuggest_attribute_format,
9267 : "argument of function call might be a candidate "
9268 : "for a format attribute");
9269 : }
9270 :
9271 144646573 : if (! MAYBE_CLASS_TYPE_P (totype))
9272 130515054 : return maybe_adjust_type_name (totype, expr, convs->kind);
9273 :
9274 : /* Don't introduce copies when passing arguments along to the inherited
9275 : constructor. */
9276 14131519 : if (current_function_decl
9277 11168598 : && flag_new_inheriting_ctors
9278 36468235 : && DECL_INHERITED_CTOR (current_function_decl))
9279 : return expr;
9280 :
9281 14126807 : if (TREE_CODE (expr) == TARGET_EXPR
9282 14126807 : && TARGET_EXPR_LIST_INIT_P (expr))
9283 : /* Copy-list-initialization doesn't actually involve a copy. */
9284 : return expr;
9285 :
9286 : /* Fall through. */
9287 16189202 : case ck_base:
9288 16189202 : if (convs->kind == ck_base && !convs->need_temporary_p)
9289 : {
9290 : /* We are going to bind a reference directly to a base-class
9291 : subobject of EXPR. */
9292 : /* Build an expression for `*((base*) &expr)'. */
9293 1832857 : expr = convert_to_base (expr, totype,
9294 : !c_cast_p, /*nonnull=*/true, complain);
9295 1832857 : return expr;
9296 : }
9297 :
9298 : /* Copy-initialization where the cv-unqualified version of the source
9299 : type is the same class as, or a derived class of, the class of the
9300 : destination [is treated as direct-initialization]. [dcl.init] */
9301 14356345 : flags = LOOKUP_NORMAL;
9302 : /* This conversion is being done in the context of a user-defined
9303 : conversion (i.e. the second step of copy-initialization), so
9304 : don't allow any more. */
9305 14356345 : if (convs->user_conv_p)
9306 264659 : flags |= LOOKUP_NO_CONVERSION;
9307 : /* We might be performing a conversion of the argument
9308 : to the user-defined conversion, i.e., not a conversion of the
9309 : result of the user-defined conversion. In which case we skip
9310 : explicit constructors. */
9311 14356345 : if (convs->copy_init_p)
9312 14082593 : flags |= LOOKUP_ONLYCONVERTING;
9313 14356345 : expr = build_temp (expr, totype, flags, &diag_kind, complain);
9314 14356345 : if (diag_kind != diagnostics::kind::unspecified && complain)
9315 : {
9316 78 : auto_diagnostic_group d;
9317 78 : maybe_print_user_conv_context (convs);
9318 78 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9319 78 : }
9320 :
9321 14356345 : return build_cplus_new (totype, expr, complain);
9322 :
9323 63689764 : case ck_ref_bind:
9324 63689764 : {
9325 63689764 : tree ref_type = totype;
9326 :
9327 63689764 : if (convs->bad_p && !next_conversion (convs)->bad_p)
9328 : {
9329 5170 : tree extype = TREE_TYPE (expr);
9330 5170 : auto_diagnostic_group d;
9331 5170 : if (TYPE_REF_IS_RVALUE (ref_type)
9332 5170 : && lvalue_p (expr))
9333 1646 : error_at (loc, "cannot bind rvalue reference of type %qH to "
9334 : "lvalue of type %qI", totype, extype);
9335 6202 : else if (!TYPE_REF_IS_RVALUE (ref_type) && !lvalue_p (expr)
9336 5004 : && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
9337 : {
9338 1231 : conversion *next = next_conversion (convs);
9339 1231 : if (next->kind == ck_std)
9340 : {
9341 59 : next = next_conversion (next);
9342 59 : error_at (loc, "cannot bind non-const lvalue reference of "
9343 : "type %qH to a value of type %qI",
9344 : totype, next->type);
9345 : }
9346 1172 : else if (!CP_TYPE_CONST_P (TREE_TYPE (ref_type)))
9347 831 : error_at (loc, "cannot bind non-const lvalue reference of "
9348 : "type %qH to an rvalue of type %qI", totype, extype);
9349 : else // extype is volatile
9350 341 : error_at (loc, "cannot bind lvalue reference of type "
9351 : "%qH to an rvalue of type %qI", totype,
9352 : extype);
9353 : }
9354 2293 : else if (!reference_compatible_p (TREE_TYPE (totype), extype))
9355 : {
9356 : /* If we're converting from T[] to T[N], don't talk
9357 : about discarding qualifiers. (Converting from T[N] to
9358 : T[] is allowed by P0388R4.) */
9359 2293 : if (TREE_CODE (extype) == ARRAY_TYPE
9360 27 : && TYPE_DOMAIN (extype) == NULL_TREE
9361 15 : && TREE_CODE (TREE_TYPE (totype)) == ARRAY_TYPE
9362 2308 : && TYPE_DOMAIN (TREE_TYPE (totype)) != NULL_TREE)
9363 15 : error_at (loc, "cannot bind reference of type %qH to %qI "
9364 : "due to different array bounds", totype, extype);
9365 : else
9366 2278 : error_at (loc, "binding reference of type %qH to %qI "
9367 : "discards qualifiers", totype, extype);
9368 : }
9369 : else
9370 0 : gcc_unreachable ();
9371 5170 : maybe_print_user_conv_context (convs);
9372 5170 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9373 :
9374 5170 : return error_mark_node;
9375 5170 : }
9376 63684594 : else if (complain & tf_warning)
9377 38113441 : maybe_warn_array_conv (loc, convs, expr);
9378 :
9379 : /* If necessary, create a temporary.
9380 :
9381 : VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
9382 : that need temporaries, even when their types are reference
9383 : compatible with the type of reference being bound, so the
9384 : upcoming call to cp_build_addr_expr doesn't fail. */
9385 63684594 : if (convs->need_temporary_p
9386 61145001 : || TREE_CODE (expr) == CONSTRUCTOR
9387 61144795 : || TREE_CODE (expr) == VA_ARG_EXPR)
9388 : {
9389 : /* Otherwise, a temporary of type "cv1 T1" is created and
9390 : initialized from the initializer expression using the rules
9391 : for a non-reference copy-initialization (8.5). */
9392 :
9393 2539799 : tree type = TREE_TYPE (ref_type);
9394 2539799 : cp_lvalue_kind lvalue = lvalue_kind (expr);
9395 :
9396 2539799 : gcc_assert (similar_type_p (type, next_conversion (convs)->type));
9397 2539799 : if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
9398 3428391 : && !TYPE_REF_IS_RVALUE (ref_type))
9399 : {
9400 : /* If the reference is volatile or non-const, we
9401 : cannot create a temporary. */
9402 105 : if (complain & tf_error)
9403 : {
9404 103 : if (lvalue & clk_bitfield)
9405 30 : error_at (loc, "cannot bind bit-field %qE to %qT",
9406 : expr, ref_type);
9407 73 : else if (lvalue & clk_packed)
9408 9 : error_at (loc, "cannot bind packed field %qE to %qT",
9409 : expr, ref_type);
9410 : else
9411 64 : error_at (loc, "cannot bind rvalue %qE to %qT",
9412 : expr, ref_type);
9413 : }
9414 105 : return error_mark_node;
9415 : }
9416 : /* If the source is a packed field, and we must use a copy
9417 : constructor, then building the target expr will require
9418 : binding the field to the reference parameter to the
9419 : copy constructor, and we'll end up with an infinite
9420 : loop. If we can use a bitwise copy, then we'll be
9421 : OK. */
9422 2539694 : if ((lvalue & clk_packed)
9423 20 : && CLASS_TYPE_P (type)
9424 2539711 : && type_has_nontrivial_copy_init (type))
9425 : {
9426 6 : error_at (loc, "cannot bind packed field %qE to %qT",
9427 : expr, ref_type);
9428 6 : return error_mark_node;
9429 : }
9430 2539688 : if (lvalue & clk_bitfield)
9431 : {
9432 24 : expr = convert_bitfield_to_declared_type (expr);
9433 24 : expr = fold_convert (type, expr);
9434 : }
9435 :
9436 : /* Creating &TARGET_EXPR<> in a template would break when
9437 : tsubsting the expression, so use an IMPLICIT_CONV_EXPR
9438 : instead. This can happen even when there's no class
9439 : involved, e.g., when converting an integer to a reference
9440 : type. */
9441 2539688 : if (processing_template_decl)
9442 9685 : return build1 (IMPLICIT_CONV_EXPR, totype, expr);
9443 2530003 : expr = build_target_expr_with_type (expr, type, complain);
9444 : }
9445 :
9446 : /* Take the address of the thing to which we will bind the
9447 : reference. */
9448 63674798 : expr = cp_build_addr_expr (expr, complain);
9449 63674798 : if (expr == error_mark_node)
9450 : return error_mark_node;
9451 :
9452 : /* Convert it to a pointer to the type referred to by the
9453 : reference. This will adjust the pointer if a derived to
9454 : base conversion is being performed. */
9455 63674798 : expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
9456 : expr, complain);
9457 : /* Convert the pointer to the desired reference type. */
9458 63674798 : return build_nop (ref_type, expr);
9459 : }
9460 :
9461 2934148 : case ck_lvalue:
9462 2934148 : return decay_conversion (expr, complain);
9463 :
9464 241815 : case ck_fnptr:
9465 : /* ??? Should the address of a transaction-safe pointer point to the TM
9466 : clone, and this conversion look up the primary function? */
9467 241815 : return build_nop (totype, expr);
9468 :
9469 1644233 : case ck_qual:
9470 : /* Warn about deprecated conversion if appropriate. */
9471 1644233 : if (complain & tf_warning)
9472 : {
9473 1445104 : string_conv_p (totype, expr, 1);
9474 1445104 : maybe_warn_array_conv (loc, convs, expr);
9475 : }
9476 : break;
9477 :
9478 3061275 : case ck_ptr:
9479 3061275 : if (convs->base_p)
9480 219809 : expr = convert_to_base (expr, totype, !c_cast_p,
9481 : /*nonnull=*/false, complain);
9482 3061275 : return build_nop (totype, expr);
9483 :
9484 28885 : case ck_pmem:
9485 28885 : return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
9486 28885 : c_cast_p, complain);
9487 :
9488 : default:
9489 : break;
9490 : }
9491 :
9492 109064181 : if (convs->check_narrowing
9493 109064181 : && !check_narrowing (totype, expr, complain,
9494 : convs->check_narrowing_const_only))
9495 451 : return error_mark_node;
9496 :
9497 218127460 : warning_sentinel w (warn_zero_as_null_pointer_constant);
9498 109063730 : if (issue_conversion_warnings)
9499 85802146 : expr = cp_convert_and_check (totype, expr, complain);
9500 : else
9501 : {
9502 23261584 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
9503 39 : expr = TREE_OPERAND (expr, 0);
9504 23261584 : expr = cp_convert (totype, expr, complain);
9505 : }
9506 :
9507 109063730 : return expr;
9508 : }
9509 :
9510 : /* Return true if converting FROM to TO is unsafe in a template. */
9511 :
9512 : static bool
9513 2020850 : conv_unsafe_in_template_p (tree to, tree from)
9514 : {
9515 : /* Converting classes involves TARGET_EXPR. */
9516 2020850 : if (CLASS_TYPE_P (to) || CLASS_TYPE_P (from))
9517 : return true;
9518 :
9519 : /* Converting real to integer produces FIX_TRUNC_EXPR which tsubst
9520 : doesn't handle. */
9521 1605792 : if (SCALAR_FLOAT_TYPE_P (from) && INTEGRAL_OR_ENUMERATION_TYPE_P (to))
9522 : return true;
9523 :
9524 : /* Converting integer to real isn't a trivial conversion, either. */
9525 1605789 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (from) && SCALAR_FLOAT_TYPE_P (to))
9526 3 : return true;
9527 :
9528 : return false;
9529 : }
9530 :
9531 : /* Wrapper for convert_like_internal that handles creating
9532 : IMPLICIT_CONV_EXPR. */
9533 :
9534 : static tree
9535 1136260597 : convert_like (conversion *convs, tree expr, tree fn, int argnum,
9536 : bool issue_conversion_warnings, bool c_cast_p, bool nested_p,
9537 : tsubst_flags_t complain)
9538 : {
9539 : /* Creating &TARGET_EXPR<> in a template breaks when substituting,
9540 : and creating a CALL_EXPR in a template breaks in finish_call_expr
9541 : so use an IMPLICIT_CONV_EXPR for this conversion. We would have
9542 : created such codes e.g. when calling a user-defined conversion
9543 : function. */
9544 1136260597 : tree conv_expr = NULL_TREE;
9545 1136260597 : if (processing_template_decl
9546 114940473 : && convs->kind != ck_identity
9547 1138281447 : && conv_unsafe_in_template_p (convs->type, TREE_TYPE (expr)))
9548 : {
9549 415064 : conv_expr = build1 (IMPLICIT_CONV_EXPR, convs->type, expr);
9550 415064 : if (convs->kind != ck_ref_bind)
9551 29083 : conv_expr = convert_from_reference (conv_expr);
9552 415064 : if (!convs->bad_p)
9553 : return conv_expr;
9554 : /* Do the normal processing to give the bad_p errors. But we still
9555 : need to return the IMPLICIT_CONV_EXPR, unless we're returning
9556 : error_mark_node. */
9557 : }
9558 1135845542 : expr = convert_like_internal (convs, expr, fn, argnum,
9559 : issue_conversion_warnings, c_cast_p,
9560 : nested_p, complain);
9561 1135845542 : if (expr == error_mark_node)
9562 : return error_mark_node;
9563 1135790610 : return conv_expr ? conv_expr : expr;
9564 : }
9565 :
9566 : /* Convenience wrapper for convert_like. */
9567 :
9568 : static inline tree
9569 632602652 : convert_like (conversion *convs, tree expr, tsubst_flags_t complain)
9570 : {
9571 632602652 : return convert_like (convs, expr, NULL_TREE, 0,
9572 : /*issue_conversion_warnings=*/true,
9573 632602652 : /*c_cast_p=*/false, /*nested_p=*/false, complain);
9574 : }
9575 :
9576 : /* Convenience wrapper for convert_like. */
9577 :
9578 : static inline tree
9579 137310893 : convert_like_with_context (conversion *convs, tree expr, tree fn, int argnum,
9580 : tsubst_flags_t complain)
9581 : {
9582 0 : return convert_like (convs, expr, fn, argnum,
9583 : /*issue_conversion_warnings=*/true,
9584 : /*c_cast_p=*/false, /*nested_p=*/false, complain);
9585 : }
9586 :
9587 : /* ARG is being passed to a varargs function. Perform any conversions
9588 : required. Return the converted value. */
9589 :
9590 : tree
9591 1446960 : convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
9592 : {
9593 1446960 : tree arg_type = TREE_TYPE (arg);
9594 1446960 : location_t loc = cp_expr_loc_or_input_loc (arg);
9595 :
9596 : /* [expr.call]
9597 :
9598 : If the argument has integral or enumeration type that is subject
9599 : to the integral promotions (_conv.prom_), or a floating-point
9600 : type that is subject to the floating-point promotion
9601 : (_conv.fpprom_), the value of the argument is converted to the
9602 : promoted type before the call. */
9603 1446960 : if (SCALAR_FLOAT_TYPE_P (arg_type)
9604 48706 : && (TYPE_PRECISION (arg_type)
9605 48706 : < TYPE_PRECISION (double_type_node))
9606 12054 : && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type))
9607 1458336 : && !extended_float_type_p (arg_type))
9608 : {
9609 11370 : if ((complain & tf_warning)
9610 11367 : && warn_double_promotion && !c_inhibit_evaluation_warnings)
9611 3 : warning_at (loc, OPT_Wdouble_promotion,
9612 : "implicit conversion from %qH to %qI when passing "
9613 : "argument to function",
9614 : arg_type, double_type_node);
9615 11370 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
9616 3 : arg = TREE_OPERAND (arg, 0);
9617 11370 : arg = mark_rvalue_use (arg);
9618 11370 : arg = convert_to_real_nofold (double_type_node, arg);
9619 : }
9620 1435590 : else if (NULLPTR_TYPE_P (arg_type))
9621 : {
9622 80 : arg = mark_rvalue_use (arg);
9623 80 : if (TREE_SIDE_EFFECTS (arg))
9624 : {
9625 6 : warning_sentinel w(warn_unused_result);
9626 6 : arg = cp_build_compound_expr (arg, null_pointer_node, complain);
9627 6 : }
9628 : else
9629 74 : arg = null_pointer_node;
9630 : }
9631 1435510 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
9632 : {
9633 938920 : if (SCOPED_ENUM_P (arg_type))
9634 : {
9635 64 : tree prom = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg,
9636 : complain);
9637 64 : prom = cp_perform_integral_promotions (prom, complain);
9638 125 : if (abi_version_crosses (6)
9639 30 : && TYPE_MODE (TREE_TYPE (prom)) != TYPE_MODE (arg_type)
9640 94 : && (complain & tf_warning))
9641 60 : warning_at (loc, OPT_Wabi, "scoped enum %qT passed through %<...%>"
9642 : " as %qT before %<-fabi-version=6%>, %qT after",
9643 : arg_type,
9644 30 : TREE_TYPE (prom), ENUM_UNDERLYING_TYPE (arg_type));
9645 64 : if (!abi_version_at_least (6))
9646 1446960 : arg = prom;
9647 : }
9648 : else
9649 938856 : arg = cp_perform_integral_promotions (arg, complain);
9650 : }
9651 : else
9652 : /* [expr.call]
9653 :
9654 : The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
9655 : standard conversions are performed. */
9656 496590 : arg = decay_conversion (arg, complain);
9657 :
9658 1446960 : arg = require_complete_type (arg, complain);
9659 1446960 : arg_type = TREE_TYPE (arg);
9660 :
9661 1446960 : if (arg != error_mark_node
9662 : /* In a template (or ill-formed code), we can have an incomplete type
9663 : even after require_complete_type, in which case we don't know
9664 : whether it has trivial copy or not. */
9665 1446948 : && COMPLETE_TYPE_P (arg_type)
9666 2893908 : && !cp_unevaluated_operand)
9667 : {
9668 : /* [expr.call] 5.2.2/7:
9669 : Passing a potentially-evaluated argument of class type (Clause 9)
9670 : with a non-trivial copy constructor or a non-trivial destructor
9671 : with no corresponding parameter is conditionally-supported, with
9672 : implementation-defined semantics.
9673 :
9674 : We support it as pass-by-invisible-reference, just like a normal
9675 : value parameter.
9676 :
9677 : If the call appears in the context of a sizeof expression,
9678 : it is not potentially-evaluated. */
9679 699919 : if (type_has_nontrivial_copy_init (arg_type)
9680 699919 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type))
9681 : {
9682 37 : arg = force_rvalue (arg, complain);
9683 37 : if (complain & tf_warning)
9684 37 : warning (OPT_Wconditionally_supported,
9685 : "passing objects of non-trivially-copyable "
9686 : "type %q#T through %<...%> is conditionally supported",
9687 : arg_type);
9688 37 : return build1 (ADDR_EXPR, build_reference_type (arg_type), arg);
9689 : }
9690 : /* Build up a real lvalue-to-rvalue conversion in case the
9691 : copy constructor is trivial but not callable. */
9692 699882 : else if (CLASS_TYPE_P (arg_type))
9693 44254 : force_rvalue (arg, complain);
9694 :
9695 : }
9696 :
9697 : return arg;
9698 : }
9699 :
9700 : /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
9701 :
9702 : tree
9703 31298 : build_x_va_arg (location_t loc, tree expr, tree type)
9704 : {
9705 31298 : if (processing_template_decl)
9706 : {
9707 39 : tree r = build_min (VA_ARG_EXPR, type, expr);
9708 39 : SET_EXPR_LOCATION (r, loc);
9709 39 : return r;
9710 : }
9711 :
9712 31259 : type = complete_type_or_else (type, NULL_TREE);
9713 :
9714 31259 : if (expr == error_mark_node || !type)
9715 : return error_mark_node;
9716 :
9717 31238 : expr = mark_lvalue_use (expr);
9718 :
9719 31238 : if (TYPE_REF_P (type))
9720 : {
9721 6 : error ("cannot receive reference type %qT through %<...%>", type);
9722 6 : return error_mark_node;
9723 : }
9724 :
9725 31232 : if (type_has_nontrivial_copy_init (type)
9726 31232 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
9727 : {
9728 : /* conditionally-supported behavior [expr.call] 5.2.2/7. Let's treat
9729 : it as pass by invisible reference. */
9730 12 : warning_at (loc, OPT_Wconditionally_supported,
9731 : "receiving objects of non-trivially-copyable type %q#T "
9732 : "through %<...%> is conditionally-supported", type);
9733 :
9734 12 : tree ref = cp_build_reference_type (type, false);
9735 12 : expr = build_va_arg (loc, expr, ref);
9736 12 : return convert_from_reference (expr);
9737 : }
9738 :
9739 31220 : tree ret = build_va_arg (loc, expr, type);
9740 31220 : if (CLASS_TYPE_P (type))
9741 : /* Wrap the VA_ARG_EXPR in a TARGET_EXPR now so other code doesn't need to
9742 : know how to handle it. */
9743 12094 : ret = get_target_expr (ret);
9744 : return ret;
9745 : }
9746 :
9747 : /* TYPE has been given to va_arg. Apply the default conversions which
9748 : would have happened when passed via ellipsis. Return the promoted
9749 : type, or the passed type if there is no change. */
9750 :
9751 : tree
9752 2719879 : cxx_type_promotes_to (tree type)
9753 : {
9754 2719879 : tree promote;
9755 :
9756 : /* Perform the array-to-pointer and function-to-pointer
9757 : conversions. */
9758 2719879 : type = type_decays_to (type);
9759 :
9760 2719879 : promote = type_promotes_to (type);
9761 2719879 : if (same_type_p (type, promote))
9762 2719855 : promote = type;
9763 :
9764 2719879 : return promote;
9765 : }
9766 :
9767 : /* ARG is a default argument expression being passed to a parameter of
9768 : the indicated TYPE, which is a parameter to FN. PARMNUM is the
9769 : zero-based argument number. Do any required conversions. Return
9770 : the converted value. */
9771 :
9772 : static GTY(()) vec<tree, va_gc> *default_arg_context;
9773 : void
9774 8075041 : push_defarg_context (tree fn)
9775 8075041 : { vec_safe_push (default_arg_context, fn); }
9776 :
9777 : void
9778 8075041 : pop_defarg_context (void)
9779 8075041 : { default_arg_context->pop (); }
9780 :
9781 : tree
9782 1148568 : convert_default_arg (tree type, tree arg, tree fn, int parmnum,
9783 : tsubst_flags_t complain)
9784 : {
9785 1148568 : int i;
9786 1148568 : tree t;
9787 :
9788 : /* See through clones. */
9789 1148568 : fn = DECL_ORIGIN (fn);
9790 : /* And inheriting ctors. */
9791 1148568 : if (flag_new_inheriting_ctors)
9792 1147928 : fn = strip_inheriting_ctors (fn);
9793 :
9794 : /* Detect recursion. */
9795 1152025 : FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t)
9796 3463 : if (t == fn)
9797 : {
9798 6 : if (complain & tf_error)
9799 6 : error ("recursive evaluation of default argument for %q#D", fn);
9800 6 : return error_mark_node;
9801 : }
9802 :
9803 : /* If the ARG is an unparsed default argument expression, the
9804 : conversion cannot be performed. */
9805 1148562 : if (TREE_CODE (arg) == DEFERRED_PARSE)
9806 : {
9807 15 : if (complain & tf_error)
9808 15 : error ("call to %qD uses the default argument for parameter %P, which "
9809 : "is not yet defined", fn, parmnum);
9810 15 : return error_mark_node;
9811 : }
9812 :
9813 1148547 : push_defarg_context (fn);
9814 :
9815 1148547 : if (fn && DECL_TEMPLATE_INFO (fn))
9816 844429 : arg = tsubst_default_argument (fn, parmnum, type, arg, complain);
9817 :
9818 : /* Due to:
9819 :
9820 : [dcl.fct.default]
9821 :
9822 : The names in the expression are bound, and the semantic
9823 : constraints are checked, at the point where the default
9824 : expressions appears.
9825 :
9826 : we must not perform access checks here. */
9827 1148547 : push_deferring_access_checks (dk_no_check);
9828 : /* We must make a copy of ARG, in case subsequent processing
9829 : alters any part of it. */
9830 1148547 : arg = break_out_target_exprs (arg, /*clear location*/true);
9831 :
9832 1148547 : arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
9833 : ICR_DEFAULT_ARGUMENT, fn, parmnum,
9834 : complain);
9835 1148547 : arg = convert_for_arg_passing (type, arg, complain);
9836 1148547 : pop_deferring_access_checks();
9837 :
9838 1148547 : pop_defarg_context ();
9839 :
9840 1148547 : return arg;
9841 : }
9842 :
9843 : /* Returns the type which will really be used for passing an argument of
9844 : type TYPE. */
9845 :
9846 : tree
9847 679279087 : type_passed_as (tree type)
9848 : {
9849 : /* Pass classes with copy ctors by invisible reference. */
9850 679279087 : if (TREE_ADDRESSABLE (type))
9851 629128 : type = build_reference_type (type);
9852 :
9853 679279087 : return type;
9854 : }
9855 :
9856 : /* Actually perform the appropriate conversion. */
9857 :
9858 : tree
9859 143357933 : convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
9860 : {
9861 143357933 : tree bitfield_type;
9862 :
9863 : /* If VAL is a bitfield, then -- since it has already been converted
9864 : to TYPE -- it cannot have a precision greater than TYPE.
9865 :
9866 : If it has a smaller precision, we must widen it here. For
9867 : example, passing "int f:3;" to a function expecting an "int" will
9868 : not result in any conversion before this point.
9869 :
9870 : If the precision is the same we must not risk widening. For
9871 : example, the COMPONENT_REF for a 32-bit "long long" bitfield will
9872 : often have type "int", even though the C++ type for the field is
9873 : "long long". If the value is being passed to a function
9874 : expecting an "int", then no conversions will be required. But,
9875 : if we call convert_bitfield_to_declared_type, the bitfield will
9876 : be converted to "long long". */
9877 143357933 : bitfield_type = is_bitfield_expr_with_lowered_type (val);
9878 143357933 : if (bitfield_type
9879 143357933 : && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
9880 0 : val = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type), val);
9881 :
9882 143357933 : if (val == error_mark_node)
9883 : ;
9884 : /* Pass classes with copy ctors by invisible reference. */
9885 143354283 : else if (TREE_ADDRESSABLE (type))
9886 213764 : val = build1 (ADDR_EXPR, build_reference_type (type), val);
9887 143357933 : if (complain & tf_warning)
9888 131633033 : maybe_warn_parm_abi (type, cp_expr_loc_or_input_loc (val));
9889 :
9890 105798382 : if (complain & tf_warning)
9891 105798382 : warn_for_address_of_packed_member (type, val);
9892 :
9893 : /* gimplify_arg elides TARGET_EXPRs that initialize a function argument,
9894 : unless the initializer is a CONSTRUCTOR. In that case, we fail to
9895 : elide the copy anyway. See that function for more information. */
9896 7922406 : if (SIMPLE_TARGET_EXPR_P (val)
9897 149850345 : && TREE_CODE (TARGET_EXPR_INITIAL (val)) != CONSTRUCTOR)
9898 3820176 : set_target_expr_eliding (val);
9899 :
9900 143357933 : return val;
9901 : }
9902 :
9903 : /* Returns non-zero iff FN is a function with magic varargs, i.e. ones for
9904 : which just decay_conversion or no conversions at all should be done.
9905 : This is true for some builtins which don't act like normal functions.
9906 : Return 2 if just decay_conversion and removal of excess precision should
9907 : be done, 1 if just decay_conversion. Return 3 for special treatment of
9908 : the 3rd argument for __builtin_*_overflow_p. Return 4 for special
9909 : treatment of the 1st argument for
9910 : __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g. */
9911 :
9912 : int
9913 174938662 : magic_varargs_p (tree fn)
9914 : {
9915 174938662 : if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
9916 6103104 : switch (DECL_FUNCTION_CODE (fn))
9917 : {
9918 : case BUILT_IN_CLASSIFY_TYPE:
9919 : case BUILT_IN_CONSTANT_P:
9920 : case BUILT_IN_NEXT_ARG:
9921 : case BUILT_IN_VA_START:
9922 : return 1;
9923 :
9924 20842 : case BUILT_IN_ADD_OVERFLOW_P:
9925 20842 : case BUILT_IN_SUB_OVERFLOW_P:
9926 20842 : case BUILT_IN_MUL_OVERFLOW_P:
9927 20842 : return 3;
9928 :
9929 320301 : case BUILT_IN_ISFINITE:
9930 320301 : case BUILT_IN_ISINF:
9931 320301 : case BUILT_IN_ISINF_SIGN:
9932 320301 : case BUILT_IN_ISNAN:
9933 320301 : case BUILT_IN_ISNORMAL:
9934 320301 : case BUILT_IN_FPCLASSIFY:
9935 320301 : return 2;
9936 :
9937 101513 : case BUILT_IN_CLZG:
9938 101513 : case BUILT_IN_CTZG:
9939 101513 : case BUILT_IN_CLRSBG:
9940 101513 : case BUILT_IN_FFSG:
9941 101513 : case BUILT_IN_PARITYG:
9942 101513 : case BUILT_IN_POPCOUNTG:
9943 101513 : return 4;
9944 :
9945 5453428 : default:
9946 5453428 : return lookup_attribute ("type generic",
9947 10906856 : TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
9948 : }
9949 :
9950 : return 0;
9951 : }
9952 :
9953 : /* Returns the decl of the dispatcher function if FN is a function version. */
9954 :
9955 : tree
9956 240 : get_function_version_dispatcher (tree fn)
9957 : {
9958 240 : tree dispatcher_decl = NULL;
9959 :
9960 240 : if (DECL_LOCAL_DECL_P (fn))
9961 9 : fn = DECL_LOCAL_DECL_ALIAS (fn);
9962 :
9963 240 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
9964 : && DECL_FUNCTION_VERSIONED (fn));
9965 :
9966 240 : gcc_assert (targetm.get_function_versions_dispatcher);
9967 240 : dispatcher_decl = targetm.get_function_versions_dispatcher (fn);
9968 :
9969 240 : if (dispatcher_decl == NULL)
9970 : {
9971 9 : error_at (input_location, "use of multiversioned function "
9972 : "without a default");
9973 9 : return NULL;
9974 : }
9975 :
9976 231 : retrofit_lang_decl (dispatcher_decl);
9977 231 : gcc_assert (dispatcher_decl != NULL);
9978 : return dispatcher_decl;
9979 : }
9980 :
9981 : /* fn is a function version dispatcher that is marked used. Mark all the
9982 : semantically identical function versions it will dispatch as used. */
9983 :
9984 : void
9985 159 : mark_versions_used (tree fn)
9986 : {
9987 159 : struct cgraph_node *node;
9988 159 : struct cgraph_function_version_info *node_v;
9989 159 : struct cgraph_function_version_info *it_v;
9990 :
9991 159 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9992 :
9993 159 : node = cgraph_node::get (fn);
9994 159 : if (node == NULL)
9995 : return;
9996 :
9997 159 : gcc_assert (node->dispatcher_function);
9998 :
9999 159 : node_v = node->function_version ();
10000 159 : if (node_v == NULL)
10001 : return;
10002 :
10003 : /* All semantically identical versions are chained. Traverse and mark each
10004 : one of them as used. */
10005 159 : it_v = node_v->next;
10006 1146 : while (it_v != NULL)
10007 : {
10008 987 : mark_used (it_v->this_node->decl);
10009 987 : it_v = it_v->next;
10010 : }
10011 : }
10012 :
10013 : /* Build a call to "the copy constructor" for the type of A, even if it
10014 : wouldn't be selected by normal overload resolution. Used for
10015 : diagnostics. */
10016 :
10017 : static tree
10018 3 : call_copy_ctor (tree a, tsubst_flags_t complain)
10019 : {
10020 3 : tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (a));
10021 3 : tree binfo = TYPE_BINFO (ctype);
10022 3 : tree copy = get_copy_ctor (ctype, complain);
10023 3 : copy = build_baselink (binfo, binfo, copy, NULL_TREE);
10024 3 : tree ob = build_dummy_object (ctype);
10025 3 : releasing_vec args (make_tree_vector_single (a));
10026 3 : tree r = build_new_method_call (ob, copy, &args, NULL_TREE,
10027 : LOOKUP_NORMAL, NULL, complain);
10028 3 : return r;
10029 3 : }
10030 :
10031 : /* Return the base constructor corresponding to COMPLETE_CTOR or NULL_TREE. */
10032 :
10033 : static tree
10034 48 : base_ctor_for (tree complete_ctor)
10035 : {
10036 48 : tree clone;
10037 48 : FOR_EACH_CLONE (clone, DECL_CLONED_FUNCTION (complete_ctor))
10038 48 : if (DECL_BASE_CONSTRUCTOR_P (clone))
10039 : return clone;
10040 : return NULL_TREE;
10041 : }
10042 :
10043 : /* Try to make EXP suitable to be used as the initializer for a base subobject,
10044 : and return whether we were successful. EXP must have already been cleared
10045 : by unsafe_copy_elision_p{,_opt}. */
10046 :
10047 : static bool
10048 224 : make_base_init_ok (tree exp)
10049 : {
10050 224 : if (TREE_CODE (exp) == TARGET_EXPR)
10051 224 : exp = TARGET_EXPR_INITIAL (exp);
10052 227 : while (TREE_CODE (exp) == COMPOUND_EXPR)
10053 3 : exp = TREE_OPERAND (exp, 1);
10054 224 : if (TREE_CODE (exp) == COND_EXPR)
10055 : {
10056 3 : bool ret = make_base_init_ok (TREE_OPERAND (exp, 2));
10057 3 : if (tree op1 = TREE_OPERAND (exp, 1))
10058 : {
10059 3 : bool r1 = make_base_init_ok (op1);
10060 : /* If unsafe_copy_elision_p was false, the arms should match. */
10061 3 : gcc_assert (r1 == ret);
10062 : }
10063 : return ret;
10064 : }
10065 221 : if (TREE_CODE (exp) != AGGR_INIT_EXPR)
10066 : /* A trivial copy is OK. */
10067 : return true;
10068 70 : if (!AGGR_INIT_VIA_CTOR_P (exp))
10069 : /* unsafe_copy_elision_p_opt must have said this is OK. */
10070 : return true;
10071 48 : tree fn = cp_get_callee_fndecl_nofold (exp);
10072 48 : if (DECL_BASE_CONSTRUCTOR_P (fn))
10073 : return true;
10074 48 : gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (fn));
10075 48 : fn = base_ctor_for (fn);
10076 48 : if (!fn || DECL_HAS_VTT_PARM_P (fn))
10077 : /* The base constructor has more parameters, so we can't just change the
10078 : call target. It would be possible to splice in the appropriate
10079 : arguments, but probably not worth the complexity. */
10080 : return false;
10081 39 : mark_used (fn);
10082 39 : AGGR_INIT_EXPR_FN (exp) = build_address (fn);
10083 39 : return true;
10084 : }
10085 :
10086 : /* Return 2 if T refers to a base, 1 if a potentially-overlapping field,
10087 : neither of which can be used for return by invisible reference. We avoid
10088 : doing C++17 mandatory copy elision for either of these cases.
10089 :
10090 : This returns non-zero even if the type of T has no tail padding that other
10091 : data could be allocated into, because that depends on the particular ABI.
10092 : unsafe_copy_elision_p_opt does consider whether there is padding. */
10093 :
10094 : int
10095 43053681 : unsafe_return_slot_p (tree t)
10096 : {
10097 : /* Check empty bases separately, they don't have fields. */
10098 43053681 : if (is_empty_base_ref (t))
10099 : return 2;
10100 :
10101 : /* A delegating constructor might be used to initialize a base. */
10102 42567559 : if (current_function_decl
10103 57735112 : && DECL_CONSTRUCTOR_P (current_function_decl)
10104 46812741 : && (t == current_class_ref
10105 4073329 : || tree_strip_nop_conversions (t) == current_class_ptr))
10106 : return 2;
10107 :
10108 42345038 : STRIP_NOPS (t);
10109 42345038 : if (TREE_CODE (t) == ADDR_EXPR)
10110 67015 : t = TREE_OPERAND (t, 0);
10111 42345038 : if (TREE_CODE (t) == COMPONENT_REF)
10112 3556691 : t = TREE_OPERAND (t, 1);
10113 42345038 : if (TREE_CODE (t) != FIELD_DECL)
10114 : return false;
10115 3603079 : if (!CLASS_TYPE_P (TREE_TYPE (t)))
10116 : /* The middle-end will do the right thing for scalar types. */
10117 : return false;
10118 3063529 : if (DECL_FIELD_IS_BASE (t))
10119 : return 2;
10120 2239570 : if (lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (t)))
10121 164610 : return 1;
10122 : return 0;
10123 : }
10124 :
10125 : /* True IFF EXP is a prvalue that represents return by invisible reference. */
10126 :
10127 : static bool
10128 286777 : init_by_return_slot_p (tree exp)
10129 : {
10130 : /* Copy elision only happens with a TARGET_EXPR. */
10131 286780 : if (TREE_CODE (exp) != TARGET_EXPR)
10132 : return false;
10133 15588 : tree init = TARGET_EXPR_INITIAL (exp);
10134 : /* build_compound_expr pushes COMPOUND_EXPR inside TARGET_EXPR. */
10135 15594 : while (TREE_CODE (init) == COMPOUND_EXPR)
10136 6 : init = TREE_OPERAND (init, 1);
10137 15588 : if (TREE_CODE (init) == COND_EXPR)
10138 : {
10139 : /* We'll end up copying from each of the arms of the COND_EXPR directly
10140 : into the target, so look at them. */
10141 6 : if (tree op = TREE_OPERAND (init, 1))
10142 6 : if (init_by_return_slot_p (op))
10143 : return true;
10144 3 : return init_by_return_slot_p (TREE_OPERAND (init, 2));
10145 : }
10146 15582 : return (TREE_CODE (init) == AGGR_INIT_EXPR
10147 15582 : && !AGGR_INIT_VIA_CTOR_P (init));
10148 : }
10149 :
10150 : /* We can't elide a copy from a function returning by value to a
10151 : potentially-overlapping subobject, as the callee might clobber tail padding.
10152 : Return true iff this could be that case.
10153 :
10154 : Places that use this function (or _opt) to decide to elide a copy should
10155 : probably use make_safe_copy_elision instead. */
10156 :
10157 : bool
10158 2434163 : unsafe_copy_elision_p (tree target, tree exp)
10159 : {
10160 2434163 : return unsafe_return_slot_p (target) && init_by_return_slot_p (exp);
10161 : }
10162 :
10163 : /* As above, but for optimization allow more cases that are actually safe. */
10164 :
10165 : static bool
10166 8266717 : unsafe_copy_elision_p_opt (tree target, tree exp)
10167 : {
10168 8266717 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
10169 : /* It's safe to elide the copy for a class with no tail padding. */
10170 8266717 : if (!is_empty_class (type)
10171 8266717 : && tree_int_cst_equal (TYPE_SIZE (type), CLASSTYPE_SIZE (type)))
10172 : return false;
10173 2387775 : return unsafe_copy_elision_p (target, exp);
10174 : }
10175 :
10176 : /* Try to make EXP suitable to be used as the initializer for TARGET,
10177 : and return whether we were successful. */
10178 :
10179 : bool
10180 22 : make_safe_copy_elision (tree target, tree exp)
10181 : {
10182 22 : int uns = unsafe_return_slot_p (target);
10183 22 : if (!uns)
10184 : return true;
10185 22 : if (init_by_return_slot_p (exp))
10186 : return false;
10187 22 : if (uns == 1)
10188 : return true;
10189 22 : return make_base_init_ok (exp);
10190 : }
10191 :
10192 : /* True IFF the result of the conversion C is a prvalue. */
10193 :
10194 : static bool
10195 14339292 : conv_is_prvalue (conversion *c)
10196 : {
10197 14339292 : if (c->kind == ck_rvalue)
10198 : return true;
10199 14339292 : if (c->kind == ck_base && c->need_temporary_p)
10200 : return true;
10201 14339292 : if (c->kind == ck_user && !TYPE_REF_P (c->type))
10202 : return true;
10203 14204216 : if (c->kind == ck_identity && c->u.expr
10204 13950688 : && TREE_CODE (c->u.expr) == TARGET_EXPR)
10205 69 : return true;
10206 :
10207 : return false;
10208 : }
10209 :
10210 : /* True iff C is a conversion that binds a reference to a prvalue. */
10211 :
10212 : static bool
10213 14340594 : conv_binds_ref_to_prvalue (conversion *c)
10214 : {
10215 14340594 : if (c->kind != ck_ref_bind)
10216 : return false;
10217 14340594 : if (c->need_temporary_p)
10218 : return true;
10219 :
10220 14339292 : return conv_is_prvalue (next_conversion (c));
10221 : }
10222 :
10223 : /* True iff EXPR represents a (subobject of a) temporary. */
10224 :
10225 : static bool
10226 14967 : expr_represents_temporary_p (tree expr)
10227 : {
10228 15071 : while (handled_component_p (expr))
10229 104 : expr = TREE_OPERAND (expr, 0);
10230 14967 : return TREE_CODE (expr) == TARGET_EXPR;
10231 : }
10232 :
10233 : /* True iff C is a conversion that binds a reference to a temporary.
10234 : This is a superset of conv_binds_ref_to_prvalue: here we're also
10235 : interested in xvalues. */
10236 :
10237 : static bool
10238 14310 : conv_binds_ref_to_temporary (conversion *c)
10239 : {
10240 14310 : if (conv_binds_ref_to_prvalue (c))
10241 : return true;
10242 13823 : if (c->kind != ck_ref_bind)
10243 : return false;
10244 13823 : c = next_conversion (c);
10245 : /* This is the case for
10246 : struct Base {};
10247 : struct Derived : Base {};
10248 : const Base& b(Derived{});
10249 : where we bind 'b' to the Base subobject of a temporary object of type
10250 : Derived. The subobject is an xvalue; the whole object is a prvalue.
10251 :
10252 : The ck_base doesn't have to be present for cases like X{}.m. */
10253 13823 : if (c->kind == ck_base)
10254 14 : c = next_conversion (c);
10255 13744 : if (c->kind == ck_identity && c->u.expr
10256 27567 : && expr_represents_temporary_p (c->u.expr))
10257 : return true;
10258 : return false;
10259 : }
10260 :
10261 : /* Return tristate::TS_TRUE if converting EXPR to a reference type TYPE binds
10262 : the reference to a temporary. Return tristate::TS_FALSE if converting
10263 : EXPR to a reference type TYPE doesn't bind the reference to a temporary. If
10264 : the conversion is invalid or bad, return tristate::TS_UNKNOWN. DIRECT_INIT_P
10265 : says whether the conversion should be done in direct- or copy-initialization
10266 : context. */
10267 :
10268 : tristate
10269 14839 : ref_conv_binds_to_temporary (tree type, tree expr, bool direct_init_p/*=false*/)
10270 : {
10271 14839 : gcc_assert (TYPE_REF_P (type));
10272 :
10273 14839 : conversion_obstack_sentinel cos;
10274 :
10275 14839 : const int flags = direct_init_p ? LOOKUP_NORMAL : LOOKUP_IMPLICIT;
10276 14839 : conversion *conv = implicit_conversion (type, TREE_TYPE (expr), expr,
10277 : /*c_cast_p=*/false, flags, tf_none);
10278 14839 : if (!conv || conv->bad_p)
10279 529 : return tristate::unknown ();
10280 :
10281 14310 : if (conv_binds_ref_to_temporary (conv))
10282 : {
10283 : /* Actually perform the conversion to check access control. */
10284 499 : if (convert_like (conv, expr, tf_none) != error_mark_node)
10285 487 : return tristate (true);
10286 : else
10287 12 : return tristate::unknown ();
10288 : }
10289 :
10290 13811 : return tristate (false);
10291 14839 : }
10292 :
10293 : /* Call the trivial destructor for INSTANCE, which can be either an lvalue of
10294 : class type or a pointer to class type. If NO_PTR_DEREF is true and
10295 : INSTANCE has pointer type, clobber the pointer rather than what it points
10296 : to. */
10297 :
10298 : tree
10299 3354994 : build_trivial_dtor_call (tree instance, bool no_ptr_deref)
10300 : {
10301 3354994 : gcc_assert (!is_dummy_object (instance));
10302 :
10303 3354994 : if (!flag_lifetime_dse)
10304 : {
10305 58 : no_clobber:
10306 77 : return fold_convert (void_type_node, instance);
10307 : }
10308 :
10309 3400382 : if (INDIRECT_TYPE_P (TREE_TYPE (instance))
10310 3354936 : && (!no_ptr_deref || TYPE_REF_P (TREE_TYPE (instance))))
10311 : {
10312 3191228 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
10313 19 : goto no_clobber;
10314 3191209 : instance = cp_build_fold_indirect_ref (instance);
10315 : }
10316 :
10317 : /* A trivial destructor should still clobber the object. */
10318 3354917 : tree clobber = build_clobber (TREE_TYPE (instance), CLOBBER_OBJECT_END);
10319 3354917 : return build2 (MODIFY_EXPR, void_type_node,
10320 3354917 : instance, clobber);
10321 : }
10322 :
10323 : /* Return true if in an immediate function context, or an unevaluated operand,
10324 : or a default argument/member initializer, or a subexpression of an immediate
10325 : invocation. */
10326 :
10327 : bool
10328 10914151 : in_immediate_context ()
10329 : {
10330 10914151 : return (cp_unevaluated_operand != 0
10331 9958021 : || (current_function_decl != NULL_TREE
10332 17963662 : && DECL_IMMEDIATE_FUNCTION_P (current_function_decl))
10333 : /* DR 2631: default args and DMI aren't immediately evaluated.
10334 : Return true here so immediate_invocation_p returns false. */
10335 9760140 : || current_binding_level->kind == sk_function_parms
10336 9757271 : || current_binding_level->kind == sk_template_parms
10337 9756270 : || parsing_nsdmi ()
10338 20667828 : || in_consteval_if_p);
10339 : }
10340 :
10341 : /* Return true if a call to FN with number of arguments NARGS
10342 : is an immediate invocation. */
10343 :
10344 : bool
10345 214012972 : immediate_invocation_p (tree fn)
10346 : {
10347 214012972 : return (TREE_CODE (fn) == FUNCTION_DECL
10348 214012972 : && DECL_IMMEDIATE_FUNCTION_P (fn)
10349 216797149 : && !in_immediate_context ());
10350 : }
10351 :
10352 : /* Subroutine of the various build_*_call functions. Overload resolution
10353 : has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
10354 : ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
10355 : bitmask of various LOOKUP_* flags which apply to the call itself. */
10356 :
10357 : static tree
10358 232277922 : build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
10359 : {
10360 232277922 : tree fn = cand->fn;
10361 232277922 : const vec<tree, va_gc> *args = cand->args;
10362 232277922 : tree first_arg = cand->first_arg;
10363 232277922 : conversion **convs = cand->convs;
10364 232277922 : tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
10365 232277922 : int parmlen;
10366 232277922 : tree val;
10367 232277922 : int nargs;
10368 232277922 : tree *argarray;
10369 232277922 : bool already_used = false;
10370 :
10371 : /* In a template, there is no need to perform all of the work that
10372 : is normally done. We are only interested in the type of the call
10373 : expression, i.e., the return type of the function. Any semantic
10374 : errors will be deferred until the template is instantiated. */
10375 232277922 : if (processing_template_decl)
10376 : {
10377 30228250 : if (undeduced_auto_decl (fn))
10378 12931 : mark_used (fn, complain);
10379 : else
10380 : /* Otherwise set TREE_USED for the benefit of -Wunused-function.
10381 : See PR80598. */
10382 30215319 : TREE_USED (fn) = 1;
10383 :
10384 30228250 : tree return_type = TREE_TYPE (TREE_TYPE (fn));
10385 30228250 : tree callee;
10386 30228250 : if (first_arg == NULL_TREE)
10387 : {
10388 22603661 : callee = build_addr_func (fn, complain);
10389 22603661 : if (callee == error_mark_node)
10390 : return error_mark_node;
10391 : }
10392 : else
10393 : {
10394 7624589 : callee = build_baselink (cand->conversion_path, cand->access_path,
10395 : fn, NULL_TREE);
10396 7624589 : callee = build_min (COMPONENT_REF, TREE_TYPE (fn),
10397 : first_arg, callee, NULL_TREE);
10398 : }
10399 :
10400 30228250 : tree expr = build_call_vec (return_type, callee, args);
10401 30228250 : SET_EXPR_LOCATION (expr, input_location);
10402 30228250 : if (TREE_THIS_VOLATILE (fn) && cfun)
10403 2605371 : current_function_returns_abnormally = 1;
10404 30228250 : if (TREE_DEPRECATED (fn)
10405 30228250 : && warning_suppressed_at (input_location,
10406 : OPT_Wdeprecated_declarations))
10407 : /* Make the expr consistent with the location. */
10408 19964 : TREE_NO_WARNING (expr) = true;
10409 30228250 : if (immediate_invocation_p (fn))
10410 : {
10411 204968 : tree obj_arg = NULL_TREE;
10412 409936 : if (DECL_CONSTRUCTOR_P (fn))
10413 0 : obj_arg = first_arg;
10414 : /* Look through *(const T *)&obj. */
10415 0 : if (obj_arg && INDIRECT_REF_P (obj_arg))
10416 : {
10417 0 : tree addr = TREE_OPERAND (obj_arg, 0);
10418 0 : STRIP_NOPS (addr);
10419 0 : if (TREE_CODE (addr) == ADDR_EXPR)
10420 : {
10421 0 : tree typeo = TREE_TYPE (obj_arg);
10422 0 : tree typei = TREE_TYPE (TREE_OPERAND (addr, 0));
10423 0 : if (same_type_ignoring_top_level_qualifiers_p (typeo, typei))
10424 0 : obj_arg = TREE_OPERAND (addr, 0);
10425 : }
10426 : }
10427 204968 : fold_non_dependent_expr (expr, complain,
10428 : /*manifestly_const_eval=*/true,
10429 : obj_arg);
10430 : }
10431 30228250 : return convert_from_reference (expr);
10432 : }
10433 :
10434 : /* Give any warnings we noticed during overload resolution. */
10435 202049672 : if (cand->warnings && (complain & tf_warning))
10436 : {
10437 : struct candidate_warning *w;
10438 98 : for (w = cand->warnings; w; w = w->next)
10439 49 : joust (cand, w->loser, 1, complain);
10440 : }
10441 :
10442 : /* Core issue 2327: P0135 doesn't say how to handle the case where the
10443 : argument to the copy constructor ends up being a prvalue after
10444 : conversion. Let's do the normal processing, but pretend we aren't
10445 : actually using the copy constructor. */
10446 202049672 : bool force_elide = false;
10447 202049672 : if (cxx_dialect >= cxx17
10448 200006479 : && cand->num_convs == 1
10449 113112280 : && DECL_COMPLETE_CONSTRUCTOR_P (fn)
10450 39602832 : && (DECL_COPY_CONSTRUCTOR_P (fn)
10451 21483562 : || DECL_MOVE_CONSTRUCTOR_P (fn))
10452 14397980 : && !unsafe_return_slot_p (first_arg)
10453 216375890 : && conv_binds_ref_to_prvalue (convs[0]))
10454 : {
10455 135957 : force_elide = true;
10456 135957 : goto not_really_used;
10457 : }
10458 :
10459 : /* OK, we're actually calling this inherited constructor; set its deletedness
10460 : appropriately. We can get away with doing this here because calling is
10461 : the only way to refer to a constructor. */
10462 403827430 : if (DECL_INHERITED_CTOR (fn)
10463 29350501 : && !deduce_inheriting_ctor (fn))
10464 : {
10465 2 : if (complain & tf_error)
10466 2 : mark_used (fn);
10467 2 : return error_mark_node;
10468 : }
10469 :
10470 : /* Make =delete work with SFINAE. */
10471 201913713 : if (DECL_DELETED_FN (fn))
10472 : {
10473 1032957 : if (complain & tf_error)
10474 : {
10475 2300 : mark_used (fn);
10476 2300 : if (cand->next)
10477 : {
10478 2086 : if (flag_diagnostics_all_candidates)
10479 9 : print_z_candidates (input_location, cand, /*only_viable_p=*/false);
10480 : else
10481 2077 : inform (input_location,
10482 : "use %<-fdiagnostics-all-candidates%> to display "
10483 : "considered candidates");
10484 : }
10485 : }
10486 1032957 : return error_mark_node;
10487 : }
10488 :
10489 200880756 : if (DECL_FUNCTION_MEMBER_P (fn))
10490 : {
10491 107117497 : tree access_fn;
10492 : /* If FN is a template function, two cases must be considered.
10493 : For example:
10494 :
10495 : struct A {
10496 : protected:
10497 : template <class T> void f();
10498 : };
10499 : template <class T> struct B {
10500 : protected:
10501 : void g();
10502 : };
10503 : struct C : A, B<int> {
10504 : using A::f; // #1
10505 : using B<int>::g; // #2
10506 : };
10507 :
10508 : In case #1 where `A::f' is a member template, DECL_ACCESS is
10509 : recorded in the primary template but not in its specialization.
10510 : We check access of FN using its primary template.
10511 :
10512 : In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
10513 : because it is a member of class template B, DECL_ACCESS is
10514 : recorded in the specialization `B<int>::g'. We cannot use its
10515 : primary template because `B<T>::g' and `B<int>::g' may have
10516 : different access. */
10517 107117497 : if (DECL_TEMPLATE_INFO (fn)
10518 107117497 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
10519 8061109 : access_fn = DECL_TI_TEMPLATE (fn);
10520 : else
10521 : access_fn = fn;
10522 107117497 : if (!perform_or_defer_access_check (cand->access_path, access_fn,
10523 : fn, complain))
10524 21091 : return error_mark_node;
10525 : }
10526 :
10527 : /* If we're checking for implicit delete, don't bother with argument
10528 : conversions. */
10529 200859665 : if (flags & LOOKUP_SPECULATIVE)
10530 : {
10531 26053745 : if (cand->viable == 1)
10532 : return fn;
10533 178 : else if (!(complain & tf_error))
10534 : /* Reject bad conversions now. */
10535 146 : return error_mark_node;
10536 : /* else continue to get conversion error. */
10537 : }
10538 :
10539 174805920 : not_really_used:
10540 :
10541 : /* N3276 magic doesn't apply to nested calls. */
10542 174941909 : tsubst_flags_t decltype_flag = (complain & tf_decltype);
10543 174941909 : complain &= ~tf_decltype;
10544 : /* No-Cleanup doesn't apply to nested calls either. */
10545 174941909 : tsubst_flags_t no_cleanup_complain = complain;
10546 174941909 : complain &= ~tf_no_cleanup;
10547 :
10548 : /* Find maximum size of vector to hold converted arguments. */
10549 174941909 : parmlen = list_length (parm);
10550 329226685 : nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0);
10551 174941909 : if (parmlen > nargs)
10552 : nargs = parmlen;
10553 174941909 : argarray = XALLOCAVEC (tree, nargs);
10554 :
10555 349883815 : in_consteval_if_p_temp_override icip;
10556 : /* If the call is immediate function invocation, make sure
10557 : taking address of immediate functions is allowed in its arguments. */
10558 174941909 : if (immediate_invocation_p (STRIP_TEMPLATE (fn)))
10559 1510557 : in_consteval_if_p = true;
10560 :
10561 174941909 : int argarray_size = 0;
10562 174941909 : unsigned int arg_index = 0;
10563 174941909 : int conv_index = 0;
10564 174941909 : int param_index = 0;
10565 174941909 : tree parmd = DECL_ARGUMENTS (fn);
10566 :
10567 243091475 : auto consume_object_arg = [&arg_index, &first_arg, args]()
10568 : {
10569 68149566 : if (!first_arg)
10570 0 : return (*args)[arg_index++];
10571 68149566 : tree object_arg = first_arg;
10572 68149566 : first_arg = NULL_TREE;
10573 68149566 : return object_arg;
10574 174941909 : };
10575 :
10576 : /* The implicit parameters to a constructor are not considered by overload
10577 : resolution, and must be of the proper type. */
10578 174941909 : if (DECL_CONSTRUCTOR_P (fn))
10579 : {
10580 20869350 : tree object_arg = consume_object_arg ();
10581 20869350 : argarray[argarray_size++] = build_this (object_arg);
10582 20869350 : parm = TREE_CHAIN (parm);
10583 20869350 : if (parmd)
10584 20869350 : parmd = DECL_CHAIN (parmd);
10585 : /* We should never try to call the abstract constructor. */
10586 20869350 : gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
10587 :
10588 20869350 : if (DECL_HAS_VTT_PARM_P (fn))
10589 : {
10590 17646 : argarray[argarray_size++] = (*args)[arg_index];
10591 17646 : ++arg_index;
10592 17646 : parm = TREE_CHAIN (parm);
10593 17646 : if (parmd)
10594 17646 : parmd = DECL_CHAIN (parmd);
10595 : }
10596 : }
10597 : /* Bypass access control for 'this' parameter. */
10598 154072559 : else if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
10599 : {
10600 47270960 : tree arg = build_this (consume_object_arg ());
10601 47270960 : tree argtype = TREE_TYPE (arg);
10602 :
10603 47270960 : if (arg == error_mark_node)
10604 : return error_mark_node;
10605 47270957 : if (convs[conv_index++]->bad_p)
10606 : {
10607 1242 : if (complain & tf_error)
10608 : {
10609 105 : auto_diagnostic_group d;
10610 105 : if (permerror (input_location, "passing %qT as %<this%> "
10611 : "argument discards qualifiers",
10612 105 : TREE_TYPE (argtype)))
10613 102 : inform (DECL_SOURCE_LOCATION (fn), " in call to %qD", fn);
10614 105 : }
10615 : else
10616 : return error_mark_node;
10617 : }
10618 :
10619 : /* The class where FN is defined. */
10620 47269820 : tree ctx = DECL_CONTEXT (fn);
10621 :
10622 : /* See if the function member or the whole class type is declared
10623 : final and the call can be devirtualized. */
10624 47269820 : if (DECL_FINAL_P (fn) || CLASSTYPE_FINAL (ctx))
10625 134003 : flags |= LOOKUP_NONVIRTUAL;
10626 :
10627 : /* [class.mfct.non-static]: If a non-static member function of a class
10628 : X is called for an object that is not of type X, or of a type
10629 : derived from X, the behavior is undefined.
10630 :
10631 : So we can assume that anything passed as 'this' is non-null, and
10632 : optimize accordingly. */
10633 : /* Check that the base class is accessible. */
10634 47269820 : if (!accessible_base_p (TREE_TYPE (argtype),
10635 47269820 : BINFO_TYPE (cand->conversion_path), true))
10636 : {
10637 33 : if (complain & tf_error)
10638 30 : error ("%qT is not an accessible base of %qT",
10639 30 : BINFO_TYPE (cand->conversion_path),
10640 30 : TREE_TYPE (argtype));
10641 : else
10642 3 : return error_mark_node;
10643 : }
10644 : /* If fn was found by a using declaration, the conversion path
10645 : will be to the derived class, not the base declaring fn. We
10646 : must convert to the base. */
10647 47269817 : tree base_binfo = cand->conversion_path;
10648 47269817 : if (BINFO_TYPE (base_binfo) != ctx)
10649 : {
10650 122790 : base_binfo = lookup_base (base_binfo, ctx, ba_unique, NULL, complain);
10651 122790 : if (base_binfo == error_mark_node)
10652 : return error_mark_node;
10653 : }
10654 :
10655 : /* If we know the dynamic type of the object, look up the final overrider
10656 : in the BINFO. */
10657 48192173 : if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
10658 47773881 : && resolves_to_fixed_type_p (arg))
10659 : {
10660 1217 : tree ov = lookup_vfn_in_binfo (DECL_VINDEX (fn), base_binfo);
10661 :
10662 : /* And unwind base_binfo to match. If we don't find the type we're
10663 : looking for in BINFO_INHERITANCE_CHAIN, we're looking at diamond
10664 : inheritance; for now do a normal virtual call in that case. */
10665 1217 : tree octx = DECL_CONTEXT (ov);
10666 1217 : tree obinfo = base_binfo;
10667 2527 : while (obinfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (obinfo), octx))
10668 48 : obinfo = BINFO_INHERITANCE_CHAIN (obinfo);
10669 1217 : if (obinfo)
10670 : {
10671 1214 : fn = ov;
10672 1214 : base_binfo = obinfo;
10673 1214 : flags |= LOOKUP_NONVIRTUAL;
10674 : }
10675 : }
10676 :
10677 47269811 : tree converted_arg = build_base_path (PLUS_EXPR, arg,
10678 : base_binfo, 1, complain);
10679 :
10680 47269811 : argarray[argarray_size++] = converted_arg;
10681 47269811 : parm = TREE_CHAIN (parm);
10682 47269811 : if (parmd)
10683 47269811 : parmd = DECL_CHAIN (parmd);
10684 : }
10685 :
10686 312251653 : auto handle_arg = [fn, flags](tree type,
10687 : tree arg,
10688 : int const param_index,
10689 : conversion *conv,
10690 : tsubst_flags_t const arg_complain)
10691 : {
10692 : /* Set user_conv_p on the argument conversions, so rvalue/base handling
10693 : knows not to allow any more UDCs. This needs to happen after we
10694 : process cand->warnings. */
10695 137310893 : if (flags & LOOKUP_NO_CONVERSION)
10696 3737931 : conv->user_conv_p = true;
10697 :
10698 137310893 : if (arg_complain & tf_warning)
10699 100183998 : maybe_warn_pessimizing_move (arg, type, /*return_p=*/false);
10700 :
10701 137310893 : tree val = convert_like_with_context (conv, arg, fn,
10702 : param_index, arg_complain);
10703 137310893 : val = convert_for_arg_passing (type, val, arg_complain);
10704 137310893 : return val;
10705 174940760 : };
10706 :
10707 313398106 : auto handle_indeterminate_arg = [](tree parmd, tree val)
10708 : {
10709 138457346 : if (parmd
10710 138457346 : && lookup_attribute (NULL, "indeterminate", DECL_ATTRIBUTES (parmd)))
10711 : {
10712 48 : STRIP_NOPS (val);
10713 48 : if (TREE_CODE (val) == ADDR_EXPR
10714 48 : && TREE_CODE (TREE_OPERAND (val, 0)) == TARGET_EXPR)
10715 : {
10716 48 : val = TARGET_EXPR_SLOT (TREE_OPERAND (val, 0));
10717 48 : if (auto_var_p (val) && DECL_ARTIFICIAL (val))
10718 : {
10719 48 : tree id = get_identifier ("indeterminate");
10720 48 : DECL_ATTRIBUTES (val)
10721 96 : = tree_cons (build_tree_list (NULL_TREE, id), NULL_TREE,
10722 48 : DECL_ATTRIBUTES (val));
10723 : }
10724 : }
10725 : }
10726 138457346 : };
10727 :
10728 174940760 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
10729 : {
10730 9256 : gcc_assert (cand->num_convs > 0);
10731 9256 : tree object_arg = consume_object_arg ();
10732 9256 : val = handle_arg (TREE_VALUE (parm),
10733 : object_arg,
10734 : param_index++,
10735 9256 : convs[conv_index++],
10736 : complain);
10737 :
10738 9256 : if (val == error_mark_node)
10739 : return error_mark_node;
10740 : else
10741 : {
10742 9088 : argarray[argarray_size++] = val;
10743 9088 : handle_indeterminate_arg (parmd, val);
10744 : }
10745 9088 : parm = TREE_CHAIN (parm);
10746 9088 : if (parmd)
10747 9088 : parmd = DECL_CHAIN (parmd);
10748 : }
10749 :
10750 174940592 : gcc_assert (first_arg == NULL_TREE);
10751 312240399 : for (; arg_index < vec_safe_length (args) && parm;
10752 137299807 : parm = TREE_CHAIN (parm), ++arg_index, ++param_index, ++conv_index)
10753 : {
10754 137301637 : tree current_arg = (*args)[arg_index];
10755 :
10756 : /* If the argument is NULL and used to (implicitly) instantiate a
10757 : template function (and bind one of the template arguments to
10758 : the type of 'long int'), we don't want to warn about passing NULL
10759 : to non-pointer argument.
10760 : For example, if we have this template function:
10761 :
10762 : template<typename T> void func(T x) {}
10763 :
10764 : we want to warn (when -Wconversion is enabled) in this case:
10765 :
10766 : void foo() {
10767 : func<int>(NULL);
10768 : }
10769 :
10770 : but not in this case:
10771 :
10772 : void foo() {
10773 : func(NULL);
10774 : }
10775 : */
10776 137301637 : bool conversion_warning = !(null_node_p (current_arg)
10777 48329 : && DECL_TEMPLATE_INFO (fn)
10778 61 : && cand->template_decl
10779 30 : && !cand->explicit_targs);
10780 :
10781 : /* Also don't warn about (x <=> y) < 0 (c++/100903). */
10782 : if (conversion_warning
10783 137301622 : && integer_zerop (current_arg)
10784 10638819 : && warn_zero_as_null_pointer_constant
10785 195 : && !warning_enabled_at (DECL_SOURCE_LOCATION (fn),
10786 195 : OPT_Wzero_as_null_pointer_constant))
10787 120 : conversion_warning = false;
10788 :
10789 135 : tsubst_flags_t const arg_complain
10790 137301517 : = conversion_warning ? complain : complain & ~tf_warning;
10791 :
10792 137301637 : val = handle_arg (TREE_VALUE (parm),
10793 : current_arg,
10794 : param_index,
10795 137301637 : convs[conv_index],
10796 : arg_complain);
10797 :
10798 137301637 : if (val == error_mark_node)
10799 : return error_mark_node;
10800 : else
10801 : {
10802 137299807 : argarray[argarray_size++] = val;
10803 137299807 : handle_indeterminate_arg (parmd, val);
10804 : }
10805 137299807 : if (parmd)
10806 126054571 : parmd = DECL_CHAIN (parmd);
10807 : }
10808 :
10809 : /* Default arguments */
10810 176087213 : for (; parm && parm != void_list_node;
10811 1148451 : parm = TREE_CHAIN (parm), param_index++)
10812 : {
10813 1148572 : if (TREE_VALUE (parm) == error_mark_node)
10814 : return error_mark_node;
10815 2297120 : val = convert_default_arg (TREE_VALUE (parm),
10816 1148560 : TREE_PURPOSE (parm),
10817 : fn, param_index,
10818 : complain);
10819 1148560 : if (val == error_mark_node)
10820 : return error_mark_node;
10821 1148451 : argarray[argarray_size++] = val;
10822 1148451 : handle_indeterminate_arg (parmd, val);
10823 1148451 : if (parmd)
10824 1148451 : parmd = DECL_CHAIN (parmd);
10825 : }
10826 :
10827 : /* Ellipsis */
10828 174938641 : int magic = magic_varargs_p (fn);
10829 352949851 : for (; arg_index < vec_safe_length (args); ++arg_index)
10830 : {
10831 3072590 : tree a = (*args)[arg_index];
10832 3072590 : if ((magic == 3 && arg_index == 2) || (magic == 4 && arg_index == 0))
10833 : {
10834 : /* Do no conversions for certain magic varargs. */
10835 122319 : a = mark_type_use (a);
10836 122319 : if (TREE_CODE (a) == FUNCTION_DECL && reject_gcc_builtin (a))
10837 0 : return error_mark_node;
10838 : }
10839 2950271 : else if (magic != 0)
10840 : {
10841 : /* Don't truncate excess precision to the semantic type. */
10842 1503754 : if (magic == 1 && TREE_CODE (a) == EXCESS_PRECISION_EXPR)
10843 84 : a = TREE_OPERAND (a, 0);
10844 : /* For other magic varargs only do decay_conversion. */
10845 1503754 : a = decay_conversion (a, complain);
10846 : }
10847 1446517 : else if (DECL_CONSTRUCTOR_P (fn)
10848 387 : && vec_safe_length (args) == 1
10849 1446718 : && same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (fn),
10850 201 : TREE_TYPE (a)))
10851 : {
10852 : /* Avoid infinite recursion trying to call A(...). */
10853 3 : if (complain & tf_error)
10854 : /* Try to call the actual copy constructor for a good error. */
10855 3 : call_copy_ctor (a, complain);
10856 3 : return error_mark_node;
10857 : }
10858 : else
10859 1446514 : a = convert_arg_to_ellipsis (a, complain);
10860 3072587 : if (a == error_mark_node)
10861 : return error_mark_node;
10862 3072569 : argarray[argarray_size++] = a;
10863 : }
10864 :
10865 174938620 : gcc_assert (argarray_size <= nargs);
10866 174938620 : nargs = argarray_size;
10867 174938620 : icip.reset ();
10868 :
10869 : /* Avoid performing argument transformation if warnings are disabled.
10870 : When tf_warning is set and at least one of the warnings is active
10871 : the check_function_arguments function might warn about something. */
10872 :
10873 174938620 : bool warned_p = false;
10874 174938620 : if ((complain & tf_warning)
10875 113933836 : && (warn_nonnull
10876 111945259 : || warn_format
10877 111945256 : || warn_suggest_attribute_format
10878 111945160 : || warn_restrict))
10879 : {
10880 1990923 : tree *fargs = (!nargs ? argarray
10881 1640901 : : (tree *) alloca (nargs * sizeof (tree)));
10882 6053024 : for (int j = 0; j < nargs; j++)
10883 : {
10884 : /* For -Wformat undo the implicit passing by hidden reference
10885 : done by convert_arg_to_ellipsis. */
10886 4062101 : if (TREE_CODE (argarray[j]) == ADDR_EXPR
10887 4062101 : && TYPE_REF_P (TREE_TYPE (argarray[j])))
10888 1501 : fargs[j] = TREE_OPERAND (argarray[j], 0);
10889 : else
10890 4060600 : fargs[j] = argarray[j];
10891 : }
10892 :
10893 1990923 : warned_p = check_function_arguments (input_location, fn, TREE_TYPE (fn),
10894 : nargs, fargs, NULL,
10895 : cp_comp_parm_types);
10896 : }
10897 :
10898 349877240 : if (DECL_INHERITED_CTOR (fn))
10899 : {
10900 : /* Check for passing ellipsis arguments to an inherited constructor. We
10901 : could handle this by open-coding the inherited constructor rather than
10902 : defining it, but let's not bother now. */
10903 64859 : if (!cp_unevaluated_operand
10904 64436 : && cand->num_convs
10905 64436 : && cand->convs[cand->num_convs-1]->ellipsis_p)
10906 : {
10907 15 : if (complain & tf_error)
10908 : {
10909 15 : auto_diagnostic_group d;
10910 15 : sorry ("passing arguments to ellipsis of inherited constructor "
10911 : "%qD", cand->fn);
10912 15 : inform (DECL_SOURCE_LOCATION (cand->fn), "declared here");
10913 15 : }
10914 15 : return error_mark_node;
10915 : }
10916 :
10917 : /* A base constructor inheriting from a virtual base doesn't get the
10918 : inherited arguments, just this and __vtt. */
10919 64844 : if (ctor_omit_inherited_parms (fn))
10920 174938605 : nargs = 2;
10921 : }
10922 :
10923 : /* Avoid actually calling copy constructors and copy assignment operators,
10924 : if possible. */
10925 :
10926 174938605 : if (!force_elide
10927 174803216 : && (!flag_elide_constructors
10928 : /* It's unsafe to elide the operation when handling
10929 : a noexcept-expression, it may evaluate to the wrong
10930 : value (c++/53025, c++/96090). */
10931 174803012 : || cp_noexcept_operand != 0))
10932 : /* Do things the hard way. */;
10933 172146494 : else if (cand->num_convs == 1
10934 262861657 : && (DECL_COPY_CONSTRUCTOR_P (fn)
10935 170590104 : || DECL_MOVE_CONSTRUCTOR_P (fn)))
10936 : {
10937 8266717 : tree targ;
10938 8266717 : tree arg = argarray[num_artificial_parms_for (fn)];
10939 8266717 : tree fa = argarray[0];
10940 8266717 : bool trivial = trivial_fn_p (fn);
10941 :
10942 : /* Pull out the real argument, disregarding const-correctness. */
10943 8266717 : targ = arg;
10944 : /* Strip the reference binding for the constructor parameter. */
10945 0 : if (CONVERT_EXPR_P (targ)
10946 8266717 : && TYPE_REF_P (TREE_TYPE (targ)))
10947 8266717 : targ = TREE_OPERAND (targ, 0);
10948 : /* But don't strip any other reference bindings; binding a temporary to a
10949 : reference prevents copy elision. */
10950 13691279 : while ((CONVERT_EXPR_P (targ)
10951 11536899 : && !TYPE_REF_P (TREE_TYPE (targ)))
10952 28720073 : || TREE_CODE (targ) == NON_LVALUE_EXPR)
10953 10330894 : targ = TREE_OPERAND (targ, 0);
10954 8266717 : if (TREE_CODE (targ) == ADDR_EXPR)
10955 : {
10956 2506151 : targ = TREE_OPERAND (targ, 0);
10957 2506151 : if (!same_type_ignoring_top_level_qualifiers_p
10958 2506151 : (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
10959 : targ = NULL_TREE;
10960 : }
10961 : else
10962 : targ = NULL_TREE;
10963 :
10964 2505699 : if (targ)
10965 : arg = targ;
10966 : else
10967 5761018 : arg = cp_build_fold_indirect_ref (arg);
10968 :
10969 : /* In C++17 we shouldn't be copying a TARGET_EXPR except into a
10970 : potentially-overlapping subobject. */
10971 8266717 : if (CHECKING_P && cxx_dialect >= cxx17)
10972 8187349 : gcc_assert (TREE_CODE (arg) != TARGET_EXPR
10973 : || force_elide
10974 : /* It's from binding the ref parm to a packed field. */
10975 : || convs[0]->need_temporary_p
10976 : || seen_error ()
10977 : /* See unsafe_copy_elision_p. */
10978 : || unsafe_return_slot_p (fa));
10979 :
10980 8266717 : bool unsafe = unsafe_copy_elision_p_opt (fa, arg);
10981 8266717 : bool eliding_temp = (TREE_CODE (arg) == TARGET_EXPR && !unsafe);
10982 :
10983 : /* [class.copy]: the copy constructor is implicitly defined even if the
10984 : implementation elided its use. But don't warn about deprecation when
10985 : eliding a temporary, as then no copy is actually performed. */
10986 8266717 : warning_sentinel s (warn_deprecated_copy, eliding_temp);
10987 8266717 : if (force_elide)
10988 : /* The language says this isn't called. */;
10989 8131328 : else if (!trivial)
10990 : {
10991 1371001 : if (!mark_used (fn, complain) && !(complain & tf_error))
10992 0 : return error_mark_node;
10993 : already_used = true;
10994 : }
10995 : else
10996 6760327 : cp_handle_deprecated_or_unavailable (fn, complain);
10997 :
10998 197592 : if (eliding_temp && DECL_BASE_CONSTRUCTOR_P (fn)
10999 8266913 : && !make_base_init_ok (arg))
11000 : unsafe = true;
11001 :
11002 : /* If we're creating a temp and we already have one, don't create a
11003 : new one. If we're not creating a temp but we get one, use
11004 : INIT_EXPR to collapse the temp into our target. Otherwise, if the
11005 : ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
11006 : temp or an INIT_EXPR otherwise. */
11007 8266717 : if (is_dummy_object (fa))
11008 : {
11009 6094291 : if (TREE_CODE (arg) == TARGET_EXPR)
11010 : return arg;
11011 5928065 : else if (trivial)
11012 5352828 : return force_target_expr (DECL_CONTEXT (fn), arg, complain);
11013 : }
11014 2172426 : else if ((trivial || TREE_CODE (arg) == TARGET_EXPR)
11015 1383181 : && !unsafe)
11016 : {
11017 1383083 : tree to = cp_build_fold_indirect_ref (fa);
11018 1383083 : val = cp_build_init_expr (to, arg);
11019 1383083 : return val;
11020 : }
11021 8266717 : }
11022 163879777 : else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
11023 3251559 : && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR)
11024 166087195 : && trivial_fn_p (fn))
11025 : {
11026 1555234 : tree to = cp_build_fold_indirect_ref (argarray[0]);
11027 1555234 : tree type = TREE_TYPE (to);
11028 1555234 : tree as_base = CLASSTYPE_AS_BASE (type);
11029 1555234 : tree arg = argarray[1];
11030 1555234 : location_t loc = cp_expr_loc_or_input_loc (arg);
11031 :
11032 1555234 : if (is_really_empty_class (type, /*ignore_vptr*/true))
11033 : {
11034 : /* Avoid copying empty classes, but ensure op= returns an lvalue even
11035 : if the object argument isn't one. */
11036 238955 : to = force_lvalue (to, complain);
11037 238955 : val = build2 (COMPOUND_EXPR, type, arg, to);
11038 238955 : suppress_warning (val, OPT_Wunused);
11039 : }
11040 1316279 : else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
11041 : {
11042 1022454 : if (is_std_init_list (type)
11043 1022454 : && conv_binds_ref_to_prvalue (convs[1]))
11044 3 : warning_at (loc, OPT_Winit_list_lifetime,
11045 : "assignment from temporary %<initializer_list%> does "
11046 : "not extend the lifetime of the underlying array");
11047 1022454 : arg = cp_build_fold_indirect_ref (arg);
11048 1022454 : val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
11049 : }
11050 : else
11051 : {
11052 : /* We must only copy the non-tail padding parts. */
11053 293825 : tree arg0, arg2, t;
11054 293825 : tree array_type, alias_set;
11055 :
11056 293825 : arg2 = TYPE_SIZE_UNIT (as_base);
11057 : /* Ensure op= returns an lvalue even if the object argument isn't
11058 : one. */
11059 293825 : to = force_lvalue (to, complain);
11060 293825 : to = cp_stabilize_reference (to);
11061 293825 : arg0 = cp_build_addr_expr (to, complain);
11062 :
11063 293825 : array_type = build_array_type (unsigned_char_type_node,
11064 : build_index_type
11065 : (size_binop (MINUS_EXPR,
11066 : arg2, size_int (1))));
11067 293825 : alias_set = build_int_cst (build_pointer_type (type), 0);
11068 293825 : t = build2 (MODIFY_EXPR, void_type_node,
11069 : build2 (MEM_REF, array_type, arg0, alias_set),
11070 : build2 (MEM_REF, array_type, arg, alias_set));
11071 293825 : val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
11072 293825 : suppress_warning (val, OPT_Wunused);
11073 : }
11074 :
11075 1555234 : cp_handle_deprecated_or_unavailable (fn, complain);
11076 :
11077 1555234 : return val;
11078 : }
11079 162324543 : else if (trivial_fn_p (fn))
11080 : {
11081 7926048 : if (DECL_DESTRUCTOR_P (fn))
11082 3174544 : return build_trivial_dtor_call (argarray[0]);
11083 788480 : else if (default_ctor_p (fn))
11084 : {
11085 788480 : if (is_dummy_object (argarray[0]))
11086 309802 : return force_target_expr (DECL_CONTEXT (fn), void_node,
11087 309802 : no_cleanup_complain);
11088 : else
11089 478678 : return cp_build_fold_indirect_ref (argarray[0]);
11090 : }
11091 : }
11092 :
11093 162518210 : gcc_assert (!force_elide);
11094 :
11095 162518210 : if (!already_used
11096 162518210 : && !mark_used (fn, complain))
11097 795 : return error_mark_node;
11098 :
11099 : /* Warn if the built-in writes to an object of a non-trivial type. */
11100 162517412 : if (warn_class_memaccess
11101 2048116 : && vec_safe_length (args) >= 2
11102 163496370 : && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
11103 69680 : maybe_warn_class_memaccess (input_location, fn, args);
11104 :
11105 162517412 : if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
11106 : {
11107 502856 : tree t;
11108 502856 : tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
11109 502856 : DECL_CONTEXT (fn),
11110 : ba_any, NULL, complain);
11111 502856 : gcc_assert (binfo && binfo != error_mark_node);
11112 :
11113 502856 : argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
11114 : complain);
11115 502856 : if (TREE_SIDE_EFFECTS (argarray[0]))
11116 47680 : argarray[0] = save_expr (argarray[0]);
11117 502856 : t = build_pointer_type (TREE_TYPE (fn));
11118 502856 : fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
11119 502856 : TREE_TYPE (fn) = t;
11120 : }
11121 : else
11122 : {
11123 : /* If FN is marked deprecated or unavailable, then we've already
11124 : issued a diagnostic from mark_used above, so avoid redundantly
11125 : issuing another one from build_addr_func. */
11126 162014556 : auto w = make_temp_override (deprecated_state,
11127 162014556 : UNAVAILABLE_DEPRECATED_SUPPRESS);
11128 :
11129 162014556 : fn = build_addr_func (fn, complain);
11130 162014556 : if (fn == error_mark_node)
11131 0 : return error_mark_node;
11132 :
11133 : /* We're actually invoking the function. (Immediate functions get an
11134 : & when invoking it even though the user didn't use &.) */
11135 162014556 : ADDR_EXPR_DENOTES_CALL_P (fn) = true;
11136 162014556 : }
11137 :
11138 162517412 : tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag);
11139 162517412 : if (call == error_mark_node)
11140 : return call;
11141 162516438 : if (cand->flags & LOOKUP_LIST_INIT_CTOR)
11142 : {
11143 2041867 : tree c = extract_call_expr (call);
11144 : /* build_new_op will clear this when appropriate. */
11145 2041867 : CALL_EXPR_ORDERED_ARGS (c) = true;
11146 : }
11147 162516438 : if (warned_p)
11148 : {
11149 253 : tree c = extract_call_expr (call);
11150 253 : if (TREE_CODE (c) == CALL_EXPR)
11151 253 : suppress_warning (c /* Suppress all warnings. */);
11152 : }
11153 162516185 : else if (TREE_DEPRECATED (fn)
11154 162516185 : && warning_suppressed_at (input_location,
11155 : OPT_Wdeprecated_declarations))
11156 : {
11157 0 : tree c = extract_call_expr (call);
11158 0 : if (TREE_CODE (c) == CALL_EXPR)
11159 0 : TREE_NO_WARNING (c) = true;
11160 : }
11161 :
11162 : return call;
11163 : }
11164 :
11165 : namespace
11166 : {
11167 :
11168 : /* Return the DECL of the first non-static subobject of class TYPE
11169 : that satisfies the predicate PRED or null if none can be found. */
11170 :
11171 : template <class Predicate>
11172 : tree
11173 3556 : first_non_static_field (tree type, Predicate pred)
11174 : {
11175 3556 : if (!type || !CLASS_TYPE_P (type))
11176 : return NULL_TREE;
11177 :
11178 71398 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
11179 : {
11180 68436 : if (TREE_CODE (field) != FIELD_DECL)
11181 43330 : continue;
11182 25106 : if (TREE_STATIC (field))
11183 0 : continue;
11184 25106 : if (pred (field))
11185 : return field;
11186 : }
11187 :
11188 2962 : int i = 0;
11189 :
11190 3082 : for (tree base_binfo, binfo = TYPE_BINFO (type);
11191 3082 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11192 : {
11193 126 : tree base = TREE_TYPE (base_binfo);
11194 126 : if (pred (base))
11195 : return base;
11196 120 : if (tree field = first_non_static_field (base, pred))
11197 : return field;
11198 : }
11199 :
11200 : return NULL_TREE;
11201 : }
11202 :
11203 : struct NonPublicField
11204 : {
11205 24356 : bool operator() (const_tree t) const
11206 : {
11207 24356 : return DECL_P (t) && (TREE_PRIVATE (t) || TREE_PROTECTED (t));
11208 : }
11209 : };
11210 :
11211 : /* Return the DECL of the first non-public subobject of class TYPE
11212 : or null if none can be found. */
11213 :
11214 : static inline tree
11215 3262 : first_non_public_field (tree type)
11216 : {
11217 3262 : return first_non_static_field (type, NonPublicField ());
11218 : }
11219 :
11220 : struct NonTrivialField
11221 : {
11222 876 : bool operator() (const_tree t) const
11223 : {
11224 876 : return !trivial_type_p (DECL_P (t) ? TREE_TYPE (t) : t);
11225 : }
11226 : };
11227 :
11228 : /* Return the DECL of the first non-trivial subobject of class TYPE
11229 : or null if none can be found. */
11230 :
11231 : static inline tree
11232 174 : first_non_trivial_field (tree type)
11233 : {
11234 174 : return first_non_static_field (type, NonTrivialField ());
11235 : }
11236 :
11237 : } /* unnamed namespace */
11238 :
11239 : /* Return true if all copy and move assignment operator overloads for
11240 : class TYPE are trivial and at least one of them is not deleted and,
11241 : when ACCESS is set, accessible. Return false otherwise. Set
11242 : HASASSIGN to true when the TYPE has a (not necessarily trivial)
11243 : copy or move assignment. */
11244 :
11245 : static bool
11246 5021 : has_trivial_copy_assign_p (tree type, bool access, bool *hasassign)
11247 : {
11248 5021 : tree fns = get_class_binding (type, assign_op_identifier);
11249 5021 : bool all_trivial = true;
11250 :
11251 : /* Iterate over overloads of the assignment operator, checking
11252 : accessible copy assignments for triviality. */
11253 :
11254 16515 : for (tree f : ovl_range (fns))
11255 : {
11256 : /* Skip operators that aren't copy assignments. */
11257 8571 : if (!copy_fn_p (f))
11258 3046 : continue;
11259 :
11260 5525 : bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
11261 5813 : || accessible_p (TYPE_BINFO (type), f, true));
11262 :
11263 : /* Skip template assignment operators and deleted functions. */
11264 5525 : if (TREE_CODE (f) != FUNCTION_DECL || DECL_DELETED_FN (f))
11265 694 : continue;
11266 :
11267 4831 : if (accessible)
11268 4579 : *hasassign = true;
11269 :
11270 4579 : if (!accessible || !trivial_fn_p (f))
11271 : all_trivial = false;
11272 :
11273 : /* Break early when both properties have been determined. */
11274 4831 : if (*hasassign && !all_trivial)
11275 : break;
11276 : }
11277 :
11278 : /* Return true if they're all trivial and one of the expressions
11279 : TYPE() = TYPE() or TYPE() = (TYPE&)() is valid. */
11280 5021 : tree ref = cp_build_reference_type (type, false);
11281 5021 : return (all_trivial
11282 5021 : && (is_trivially_xible (MODIFY_EXPR, type, type)
11283 333 : || is_trivially_xible (MODIFY_EXPR, type, ref)));
11284 : }
11285 :
11286 : /* Return true if all copy and move ctor overloads for class TYPE are
11287 : trivial and at least one of them is not deleted and, when ACCESS is
11288 : set, accessible. Return false otherwise. Set each element of HASCTOR[]
11289 : to true when the TYPE has a (not necessarily trivial) default and copy
11290 : (or move) ctor, respectively. */
11291 :
11292 : static bool
11293 5021 : has_trivial_copy_p (tree type, bool access, bool hasctor[2])
11294 : {
11295 5021 : tree fns = get_class_binding (type, complete_ctor_identifier);
11296 5021 : bool all_trivial = true;
11297 :
11298 25141 : for (tree f : ovl_range (fns))
11299 : {
11300 : /* Skip template constructors. */
11301 12602 : if (TREE_CODE (f) != FUNCTION_DECL)
11302 105 : continue;
11303 :
11304 12497 : bool cpy_or_move_ctor_p = copy_fn_p (f);
11305 :
11306 : /* Skip ctors other than default, copy, and move. */
11307 12497 : if (!cpy_or_move_ctor_p && !default_ctor_p (f))
11308 2757 : continue;
11309 :
11310 9740 : if (DECL_DELETED_FN (f))
11311 306 : continue;
11312 :
11313 9434 : bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
11314 9626 : || accessible_p (TYPE_BINFO (type), f, true));
11315 :
11316 : if (accessible)
11317 9314 : hasctor[cpy_or_move_ctor_p] = true;
11318 :
11319 9434 : if (cpy_or_move_ctor_p && (!accessible || !trivial_fn_p (f)))
11320 : all_trivial = false;
11321 :
11322 : /* Break early when both properties have been determined. */
11323 9434 : if (hasctor[0] && hasctor[1] && !all_trivial)
11324 : break;
11325 : }
11326 :
11327 5021 : return all_trivial;
11328 : }
11329 :
11330 : /* Issue a warning on a call to the built-in function FNDECL if it is
11331 : a raw memory write whose destination is not an object of (something
11332 : like) trivial or standard layout type with a non-deleted assignment
11333 : and copy ctor. Detects const correctness violations, corrupting
11334 : references, virtual table pointers, and bypassing non-trivial
11335 : assignments. */
11336 :
11337 : static void
11338 69680 : maybe_warn_class_memaccess (location_t loc, tree fndecl,
11339 : const vec<tree, va_gc> *args)
11340 : {
11341 : /* Except for bcopy where it's second, the destination pointer is
11342 : the first argument for all functions handled here. Compute
11343 : the index of the destination and source arguments. */
11344 69680 : unsigned dstidx = DECL_FUNCTION_CODE (fndecl) == BUILT_IN_BCOPY;
11345 69680 : unsigned srcidx = !dstidx;
11346 :
11347 69680 : tree dest = (*args)[dstidx];
11348 69680 : if (!TREE_TYPE (dest)
11349 69680 : || (TREE_CODE (TREE_TYPE (dest)) != ARRAY_TYPE
11350 68488 : && !INDIRECT_TYPE_P (TREE_TYPE (dest))))
11351 66305 : return;
11352 :
11353 22354 : tree srctype = NULL_TREE;
11354 :
11355 : /* Determine the type of the pointed-to object and whether it's
11356 : a complete class type. */
11357 22354 : tree desttype = TREE_TYPE (TREE_TYPE (dest));
11358 :
11359 22354 : if (!desttype || !COMPLETE_TYPE_P (desttype) || !CLASS_TYPE_P (desttype))
11360 : return;
11361 :
11362 : /* Check to see if the raw memory call is made by a non-static member
11363 : function with THIS as the destination argument for the destination
11364 : type. If so, and if the class has no non-trivial bases or members,
11365 : be more permissive. */
11366 5123 : if (current_function_decl
11367 5123 : && DECL_OBJECT_MEMBER_FUNCTION_P (current_function_decl)
11368 5390 : && is_object_parameter (tree_strip_nop_conversions (dest)))
11369 : {
11370 204 : tree ctx = DECL_CONTEXT (current_function_decl);
11371 204 : bool special = same_type_ignoring_top_level_qualifiers_p (ctx, desttype);
11372 204 : tree binfo = TYPE_BINFO (ctx);
11373 :
11374 204 : if (special
11375 204 : && !BINFO_VTABLE (binfo)
11376 378 : && !first_non_trivial_field (desttype))
11377 : return;
11378 : }
11379 :
11380 : /* True if the class is trivial. */
11381 5021 : bool trivial = trivial_type_p (desttype);
11382 :
11383 : /* Set to true if DESTYPE has an accessible copy assignment. */
11384 5021 : bool hasassign = false;
11385 : /* True if all of the class' overloaded copy assignment operators
11386 : are all trivial (and not deleted) and at least one of them is
11387 : accessible. */
11388 5021 : bool trivassign = has_trivial_copy_assign_p (desttype, true, &hasassign);
11389 :
11390 : /* Set to true if DESTTYPE has an accessible default and copy ctor,
11391 : respectively. */
11392 5021 : bool hasctors[2] = { false, false };
11393 :
11394 : /* True if all of the class' overloaded copy constructors are all
11395 : trivial (and not deleted) and at least one of them is accessible. */
11396 5021 : bool trivcopy = has_trivial_copy_p (desttype, true, hasctors);
11397 :
11398 : /* Set FLD to the first private/protected member of the class. */
11399 5021 : tree fld = trivial ? first_non_public_field (desttype) : NULL_TREE;
11400 :
11401 : /* The warning format string. */
11402 5021 : const char *warnfmt = NULL;
11403 : /* A suggested alternative to offer instead of the raw memory call.
11404 : Empty string when none can be come up with. */
11405 5021 : const char *suggest = "";
11406 5021 : bool warned = false;
11407 :
11408 5021 : auto_diagnostic_group d;
11409 5021 : switch (DECL_FUNCTION_CODE (fndecl))
11410 : {
11411 560 : case BUILT_IN_MEMSET:
11412 560 : if (!integer_zerop (maybe_constant_value ((*args)[1])))
11413 : {
11414 : /* Diagnose setting non-copy-assignable or non-trivial types,
11415 : or types with a private member, to (potentially) non-zero
11416 : bytes. Since the value of the bytes being written is unknown,
11417 : suggest using assignment instead (if one exists). Also warn
11418 : for writes into objects for which zero-initialization doesn't
11419 : mean all bits clear (pointer-to-member data, where null is all
11420 : bits set). Since the value being written is (most likely)
11421 : non-zero, simply suggest assignment (but not copy assignment). */
11422 196 : suggest = "; use assignment instead";
11423 196 : if (!trivassign)
11424 : warnfmt = G_("%qD writing to an object of type %#qT with "
11425 : "no trivial copy-assignment");
11426 125 : else if (!trivial)
11427 : warnfmt = G_("%qD writing to an object of non-trivial type %#qT%s");
11428 85 : else if (fld)
11429 : {
11430 24 : const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
11431 24 : warned = warning_at (loc, OPT_Wclass_memaccess,
11432 : "%qD writing to an object of type %#qT with "
11433 : "%qs member %qD",
11434 : fndecl, desttype, access, fld);
11435 : }
11436 61 : else if (!zero_init_p (desttype))
11437 : warnfmt = G_("%qD writing to an object of type %#qT containing "
11438 : "a pointer to data member%s");
11439 :
11440 : break;
11441 : }
11442 : /* Fall through. */
11443 :
11444 481 : case BUILT_IN_BZERO:
11445 : /* Similarly to the above, diagnose clearing non-trivial or non-
11446 : standard layout objects, or objects of types with no assignmenmt.
11447 : Since the value being written is known to be zero, suggest either
11448 : copy assignment, copy ctor, or default ctor as an alternative,
11449 : depending on what's available. */
11450 :
11451 481 : if (hasassign && hasctors[0])
11452 : suggest = G_("; use assignment or value-initialization instead");
11453 49 : else if (hasassign)
11454 : suggest = G_("; use assignment instead");
11455 31 : else if (hasctors[0])
11456 16 : suggest = G_("; use value-initialization instead");
11457 :
11458 481 : if (!trivassign)
11459 : warnfmt = G_("%qD clearing an object of type %#qT with "
11460 : "no trivial copy-assignment%s");
11461 374 : else if (!trivial)
11462 : warnfmt = G_("%qD clearing an object of non-trivial type %#qT%s");
11463 304 : else if (!zero_init_p (desttype))
11464 : warnfmt = G_("%qD clearing an object of type %#qT containing "
11465 : "a pointer-to-member%s");
11466 : break;
11467 :
11468 2437 : case BUILT_IN_BCOPY:
11469 2437 : case BUILT_IN_MEMCPY:
11470 2437 : case BUILT_IN_MEMMOVE:
11471 2437 : case BUILT_IN_MEMPCPY:
11472 : /* Determine the type of the source object. */
11473 2437 : srctype = TREE_TYPE ((*args)[srcidx]);
11474 2437 : if (!srctype || !INDIRECT_TYPE_P (srctype))
11475 0 : srctype = void_type_node;
11476 : else
11477 2437 : srctype = TREE_TYPE (srctype);
11478 :
11479 : /* Since it's impossible to determine whether the byte copy is
11480 : being used in place of assignment to an existing object or
11481 : as a substitute for initialization, assume it's the former.
11482 : Determine the best alternative to use instead depending on
11483 : what's not deleted. */
11484 2437 : if (hasassign && hasctors[1])
11485 : suggest = G_("; use copy-assignment or copy-initialization instead");
11486 488 : else if (hasassign)
11487 : suggest = G_("; use copy-assignment instead");
11488 341 : else if (hasctors[1])
11489 290 : suggest = G_("; use copy-initialization instead");
11490 :
11491 2437 : if (!trivassign)
11492 : warnfmt = G_("%qD writing to an object of type %#qT with no trivial "
11493 : "copy-assignment%s");
11494 1639 : else if (!trivially_copyable_p (desttype))
11495 : warnfmt = G_("%qD writing to an object of non-trivially copyable "
11496 : "type %#qT%s");
11497 1315 : else if (!trivcopy)
11498 : warnfmt = G_("%qD writing to an object with a deleted copy constructor");
11499 :
11500 1315 : else if (!trivial
11501 211 : && !VOID_TYPE_P (srctype)
11502 160 : && !is_byte_access_type (srctype)
11503 1412 : && !same_type_ignoring_top_level_qualifiers_p (desttype,
11504 : srctype))
11505 : {
11506 : /* Warn when copying into a non-trivial object from an object
11507 : of a different type other than void or char. */
11508 54 : warned = warning_at (loc, OPT_Wclass_memaccess,
11509 : "%qD copying an object of non-trivial type "
11510 : "%#qT from an array of %#qT",
11511 : fndecl, desttype, srctype);
11512 : }
11513 1261 : else if (fld
11514 432 : && !VOID_TYPE_P (srctype)
11515 384 : && !is_byte_access_type (srctype)
11516 1549 : && !same_type_ignoring_top_level_qualifiers_p (desttype,
11517 : srctype))
11518 : {
11519 216 : const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
11520 216 : warned = warning_at (loc, OPT_Wclass_memaccess,
11521 : "%qD copying an object of type %#qT with "
11522 : "%qs member %qD from an array of %#qT; use "
11523 : "assignment or copy-initialization instead",
11524 : fndecl, desttype, access, fld, srctype);
11525 : }
11526 1045 : else if (!trivial && vec_safe_length (args) > 2)
11527 : {
11528 157 : tree sz = maybe_constant_value ((*args)[2]);
11529 157 : if (!tree_fits_uhwi_p (sz))
11530 : break;
11531 :
11532 : /* Finally, warn on partial copies. */
11533 99 : unsigned HOST_WIDE_INT typesize
11534 99 : = tree_to_uhwi (TYPE_SIZE_UNIT (desttype));
11535 99 : if (typesize == 0)
11536 : break;
11537 96 : if (unsigned HOST_WIDE_INT partial = tree_to_uhwi (sz) % typesize)
11538 12 : warned = warning_at (loc, OPT_Wclass_memaccess,
11539 : (typesize - partial > 1
11540 : ? G_("%qD writing to an object of "
11541 : "a non-trivial type %#qT leaves %wu "
11542 : "bytes unchanged")
11543 : : G_("%qD writing to an object of "
11544 : "a non-trivial type %#qT leaves %wu "
11545 : "byte unchanged")),
11546 : fndecl, desttype, typesize - partial);
11547 : }
11548 : break;
11549 :
11550 261 : case BUILT_IN_REALLOC:
11551 :
11552 261 : if (!trivially_copyable_p (desttype))
11553 : warnfmt = G_("%qD moving an object of non-trivially copyable type "
11554 : "%#qT; use %<new%> and %<delete%> instead");
11555 138 : else if (!trivcopy)
11556 : warnfmt = G_("%qD moving an object of type %#qT with deleted copy "
11557 : "constructor; use %<new%> and %<delete%> instead");
11558 135 : else if (!get_dtor (desttype, tf_none))
11559 : warnfmt = G_("%qD moving an object of type %#qT with deleted "
11560 : "destructor");
11561 126 : else if (!trivial)
11562 : {
11563 33 : tree sz = maybe_constant_value ((*args)[1]);
11564 33 : if (TREE_CODE (sz) == INTEGER_CST
11565 33 : && tree_int_cst_lt (sz, TYPE_SIZE_UNIT (desttype)))
11566 : /* Finally, warn on reallocation into insufficient space. */
11567 7 : warned = warning_at (loc, OPT_Wclass_memaccess,
11568 : "%qD moving an object of non-trivial type "
11569 : "%#qT and size %E into a region of size %E",
11570 7 : fndecl, desttype, TYPE_SIZE_UNIT (desttype),
11571 : sz);
11572 : }
11573 : break;
11574 :
11575 1646 : default:
11576 1646 : return;
11577 : }
11578 :
11579 310 : if (warnfmt)
11580 : {
11581 1569 : if (suggest)
11582 1569 : warned = warning_at (loc, OPT_Wclass_memaccess,
11583 : warnfmt, fndecl, desttype, suggest);
11584 : else
11585 : warned = warning_at (loc, OPT_Wclass_memaccess,
11586 : warnfmt, fndecl, desttype);
11587 : }
11588 :
11589 3375 : if (warned)
11590 1879 : inform (location_of (desttype), "%#qT declared here", desttype);
11591 5021 : }
11592 :
11593 : /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
11594 : If FN is the result of resolving an overloaded target built-in,
11595 : ORIG_FNDECL is the original function decl, otherwise it is null.
11596 : This function performs no overload resolution, conversion, or other
11597 : high-level operations. */
11598 :
11599 : tree
11600 171478895 : build_cxx_call (tree fn, int nargs, tree *argarray,
11601 : tsubst_flags_t complain, tree orig_fndecl)
11602 : {
11603 171478895 : tree fndecl;
11604 :
11605 : /* Remember roughly where this call is. */
11606 171478895 : location_t loc = cp_expr_loc_or_input_loc (fn);
11607 171478895 : fn = build_call_a (fn, nargs, argarray);
11608 171478895 : SET_EXPR_LOCATION (fn, loc);
11609 :
11610 171478895 : fndecl = get_callee_fndecl (fn);
11611 171478895 : if (!orig_fndecl)
11612 171478895 : orig_fndecl = fndecl;
11613 :
11614 : /* Check that arguments to builtin functions match the expectations. */
11615 171478895 : if (fndecl
11616 164757122 : && !processing_template_decl
11617 336181262 : && fndecl_built_in_p (fndecl))
11618 : {
11619 : int i;
11620 :
11621 : /* We need to take care that values to BUILT_IN_NORMAL
11622 : are reduced. */
11623 22566735 : for (i = 0; i < nargs; i++)
11624 14779925 : argarray[i] = maybe_constant_value (argarray[i]);
11625 :
11626 7786810 : if (!check_builtin_function_arguments (EXPR_LOCATION (fn), vNULL, fndecl,
11627 : orig_fndecl, nargs, argarray,
11628 : complain & tf_error))
11629 927 : return error_mark_node;
11630 7785883 : else if (fndecl_built_in_p (fndecl, BUILT_IN_CLEAR_PADDING))
11631 : {
11632 14553 : tree arg0 = argarray[0];
11633 14553 : STRIP_NOPS (arg0);
11634 14553 : if (TREE_CODE (arg0) == ADDR_EXPR
11635 264 : && DECL_P (TREE_OPERAND (arg0, 0))
11636 14796 : && same_type_ignoring_top_level_qualifiers_p
11637 243 : (TREE_TYPE (TREE_TYPE (argarray[0])),
11638 243 : TREE_TYPE (TREE_TYPE (arg0))))
11639 : /* For __builtin_clear_padding (&var) we know the type
11640 : is for a complete object, so there is no risk in clearing
11641 : padding that is reused in some derived class member. */;
11642 14317 : else if (!trivially_copyable_p (TREE_TYPE (TREE_TYPE (argarray[0]))))
11643 : {
11644 18 : error_at (EXPR_LOC_OR_LOC (argarray[0], input_location),
11645 : "argument %u in call to function %qE "
11646 : "has pointer to a non-trivially-copyable type (%qT)",
11647 18 : 1, fndecl, TREE_TYPE (argarray[0]));
11648 18 : return error_mark_node;
11649 : }
11650 : }
11651 : }
11652 :
11653 171477950 : if (VOID_TYPE_P (TREE_TYPE (fn)))
11654 37951469 : return maybe_contract_wrap_call (fndecl, fn);
11655 :
11656 : /* 5.2.2/11: If a function call is a prvalue of object type: if the
11657 : function call is either the operand of a decltype-specifier or the
11658 : right operand of a comma operator that is the operand of a
11659 : decltype-specifier, a temporary object is not introduced for the
11660 : prvalue. The type of the prvalue may be incomplete. */
11661 133526481 : if (!(complain & tf_decltype))
11662 : {
11663 110454945 : fn = require_complete_type (fn, complain);
11664 110454945 : if (fn == error_mark_node)
11665 : return error_mark_node;
11666 110454919 : fn = maybe_contract_wrap_call (fndecl, fn);
11667 :
11668 110454919 : if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
11669 : {
11670 12547349 : fn = build_cplus_new (TREE_TYPE (fn), fn, complain);
11671 12547349 : maybe_warn_parm_abi (TREE_TYPE (fn), loc);
11672 : }
11673 : }
11674 : else
11675 23071536 : fn = maybe_contract_wrap_call (fndecl, fn);
11676 133526455 : return convert_from_reference (fn);
11677 : }
11678 :
11679 : /* Returns the value to use for the in-charge parameter when making a
11680 : call to a function with the indicated NAME.
11681 :
11682 : FIXME:Can't we find a neater way to do this mapping? */
11683 :
11684 : tree
11685 37072 : in_charge_arg_for_name (tree name)
11686 : {
11687 37072 : if (IDENTIFIER_CTOR_P (name))
11688 : {
11689 21222 : if (name == complete_ctor_identifier)
11690 10611 : return integer_one_node;
11691 10611 : gcc_checking_assert (name == base_ctor_identifier);
11692 : }
11693 : else
11694 : {
11695 15850 : if (name == complete_dtor_identifier)
11696 7925 : return integer_two_node;
11697 7925 : else if (name == deleting_dtor_identifier)
11698 : /* The deleting dtor should now be handled by
11699 : build_delete_destructor_body. */
11700 0 : gcc_unreachable ();
11701 7925 : gcc_checking_assert (name == base_dtor_identifier);
11702 : }
11703 :
11704 18536 : return integer_zero_node;
11705 : }
11706 :
11707 : /* We've built up a constructor call RET. Complain if it delegates to the
11708 : constructor we're currently compiling. */
11709 :
11710 : static void
11711 365946 : check_self_delegation (tree ret)
11712 : {
11713 365946 : if (TREE_CODE (ret) == TARGET_EXPR)
11714 0 : ret = TARGET_EXPR_INITIAL (ret);
11715 365946 : tree fn = cp_get_callee_fndecl_nofold (ret);
11716 365946 : if (fn && DECL_ABSTRACT_ORIGIN (fn) == current_function_decl)
11717 46 : error ("constructor delegates to itself");
11718 365946 : }
11719 :
11720 : /* Build a call to a constructor, destructor, or an assignment
11721 : operator for INSTANCE, an expression with class type. NAME
11722 : indicates the special member function to call; *ARGS are the
11723 : arguments. ARGS may be NULL. This may change ARGS. BINFO
11724 : indicates the base of INSTANCE that is to be passed as the `this'
11725 : parameter to the member function called.
11726 :
11727 : FLAGS are the LOOKUP_* flags to use when processing the call.
11728 :
11729 : If NAME indicates a complete object constructor, INSTANCE may be
11730 : NULL_TREE. In this case, the caller will call build_cplus_new to
11731 : store the newly constructed object into a VAR_DECL. */
11732 :
11733 : tree
11734 40295768 : build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
11735 : tree binfo, int flags, tsubst_flags_t complain)
11736 : {
11737 40295768 : tree fns;
11738 : /* The type of the subobject to be constructed or destroyed. */
11739 40295768 : tree class_type;
11740 40295768 : vec<tree, va_gc> *allocated = NULL;
11741 40295768 : tree ret;
11742 :
11743 40295768 : gcc_assert (IDENTIFIER_CDTOR_P (name) || name == assign_op_identifier);
11744 :
11745 40295768 : if (error_operand_p (instance))
11746 0 : return error_mark_node;
11747 :
11748 40295768 : if (IDENTIFIER_DTOR_P (name))
11749 : {
11750 10969163 : gcc_assert (args == NULL || vec_safe_is_empty (*args));
11751 10969163 : if (!type_build_dtor_call (TREE_TYPE (instance)))
11752 : /* Shortcut to avoid lazy destructor declaration. */
11753 1495 : return build_trivial_dtor_call (instance);
11754 : }
11755 :
11756 40294273 : if (TYPE_P (binfo))
11757 : {
11758 : /* Resolve the name. */
11759 32214457 : if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
11760 6 : return error_mark_node;
11761 :
11762 32214451 : binfo = TYPE_BINFO (binfo);
11763 : }
11764 :
11765 32214451 : gcc_assert (binfo != NULL_TREE);
11766 :
11767 40294267 : class_type = BINFO_TYPE (binfo);
11768 :
11769 : /* Handle the special case where INSTANCE is NULL_TREE. */
11770 40294267 : if (name == complete_ctor_identifier && !instance)
11771 21372252 : instance = build_dummy_object (class_type);
11772 : else
11773 : {
11774 : /* Convert to the base class, if necessary. */
11775 18922015 : if (!same_type_ignoring_top_level_qualifiers_p
11776 18922015 : (TREE_TYPE (instance), BINFO_TYPE (binfo)))
11777 : {
11778 1664486 : if (IDENTIFIER_CDTOR_P (name))
11779 : /* For constructors and destructors, either the base is
11780 : non-virtual, or it is virtual but we are doing the
11781 : conversion from a constructor or destructor for the
11782 : complete object. In either case, we can convert
11783 : statically. */
11784 1644513 : instance = convert_to_base_statically (instance, binfo);
11785 : else
11786 : {
11787 : /* However, for assignment operators, we must convert
11788 : dynamically if the base is virtual. */
11789 19973 : gcc_checking_assert (name == assign_op_identifier);
11790 19973 : instance = build_base_path (PLUS_EXPR, instance,
11791 : binfo, /*nonnull=*/1, complain);
11792 : }
11793 : }
11794 : }
11795 :
11796 40294267 : gcc_assert (instance != NULL_TREE);
11797 :
11798 : /* In C++17, "If the initializer expression is a prvalue and the
11799 : cv-unqualified version of the source type is the same class as the class
11800 : of the destination, the initializer expression is used to initialize the
11801 : destination object." Handle that here to avoid doing overload
11802 : resolution. */
11803 40294267 : if (cxx_dialect >= cxx17
11804 40095124 : && args && vec_safe_length (*args) == 1
11805 64356525 : && !unsafe_return_slot_p (instance))
11806 : {
11807 22753497 : tree arg = (**args)[0];
11808 :
11809 1727296 : if (BRACE_ENCLOSED_INITIALIZER_P (arg)
11810 1726924 : && !TYPE_HAS_LIST_CTOR (class_type)
11811 1650818 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (arg)
11812 24404298 : && CONSTRUCTOR_NELTS (arg) == 1)
11813 1167954 : arg = CONSTRUCTOR_ELT (arg, 0)->value;
11814 :
11815 22753497 : if ((TREE_CODE (arg) == TARGET_EXPR
11816 11028324 : || TREE_CODE (arg) == CONSTRUCTOR)
11817 35038086 : && (same_type_ignoring_top_level_qualifiers_p
11818 12284589 : (class_type, TREE_TYPE (arg))))
11819 : {
11820 11098942 : if (is_dummy_object (instance))
11821 : return arg;
11822 236414 : else if (TREE_CODE (arg) == TARGET_EXPR)
11823 236414 : TARGET_EXPR_DIRECT_INIT_P (arg) = true;
11824 :
11825 236414 : if ((complain & tf_error)
11826 236412 : && (flags & LOOKUP_DELEGATING_CONS))
11827 0 : check_self_delegation (arg);
11828 : /* Avoid change of behavior on Wunused-var-2.C. */
11829 236414 : instance = mark_lvalue_use (instance);
11830 236414 : return cp_build_init_expr (instance, arg);
11831 : }
11832 : }
11833 :
11834 29195325 : fns = lookup_fnfields (binfo, name, 1, complain);
11835 :
11836 : /* When making a call to a constructor or destructor for a subobject
11837 : that uses virtual base classes, pass down a pointer to a VTT for
11838 : the subobject. */
11839 29195325 : if ((name == base_ctor_identifier
11840 26781054 : || name == base_dtor_identifier)
11841 30839918 : && CLASSTYPE_VBASECLASSES (class_type))
11842 : {
11843 48336 : tree vtt;
11844 48336 : tree sub_vtt;
11845 :
11846 : /* If the current function is a complete object constructor
11847 : or destructor, then we fetch the VTT directly.
11848 : Otherwise, we look it up using the VTT we were given. */
11849 48336 : vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
11850 48336 : vtt = decay_conversion (vtt, complain);
11851 48336 : if (vtt == error_mark_node)
11852 0 : return error_mark_node;
11853 48336 : vtt = build_if_in_charge (vtt, current_vtt_parm);
11854 48336 : if (BINFO_SUBVTT_INDEX (binfo))
11855 48156 : sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
11856 : else
11857 : sub_vtt = vtt;
11858 :
11859 48336 : if (args == NULL)
11860 : {
11861 30686 : allocated = make_tree_vector ();
11862 30686 : args = &allocated;
11863 : }
11864 :
11865 48336 : vec_safe_insert (*args, 0, sub_vtt);
11866 : }
11867 :
11868 58390650 : ret = build_new_method_call (instance, fns, args,
11869 29195325 : TYPE_BINFO (BINFO_TYPE (binfo)),
11870 : flags, /*fn=*/NULL,
11871 : complain);
11872 :
11873 29195325 : if (allocated != NULL)
11874 30686 : release_tree_vector (allocated);
11875 :
11876 29195325 : if ((complain & tf_error)
11877 25972392 : && (flags & LOOKUP_DELEGATING_CONS)
11878 366046 : && name == complete_ctor_identifier)
11879 365946 : check_self_delegation (ret);
11880 :
11881 : return ret;
11882 : }
11883 :
11884 : /* Return the NAME, as a C string. The NAME indicates a function that
11885 : is a member of TYPE. *FREE_P is set to true if the caller must
11886 : free the memory returned.
11887 :
11888 : Rather than go through all of this, we should simply set the names
11889 : of constructors and destructors appropriately, and dispense with
11890 : ctor_identifier, dtor_identifier, etc. */
11891 :
11892 : static char *
11893 93 : name_as_c_string (tree name, tree type, bool *free_p)
11894 : {
11895 93 : const char *pretty_name;
11896 :
11897 : /* Assume that we will not allocate memory. */
11898 93 : *free_p = false;
11899 : /* Constructors and destructors are special. */
11900 93 : if (IDENTIFIER_CDTOR_P (name))
11901 : {
11902 42 : pretty_name
11903 42 : = identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type)));
11904 : /* For a destructor, add the '~'. */
11905 42 : if (IDENTIFIER_DTOR_P (name))
11906 : {
11907 0 : pretty_name = concat ("~", pretty_name, NULL);
11908 : /* Remember that we need to free the memory allocated. */
11909 0 : *free_p = true;
11910 : }
11911 : }
11912 51 : else if (IDENTIFIER_CONV_OP_P (name))
11913 : {
11914 0 : pretty_name = concat ("operator ",
11915 0 : type_as_string_translate (TREE_TYPE (name),
11916 : TFF_PLAIN_IDENTIFIER),
11917 : NULL);
11918 : /* Remember that we need to free the memory allocated. */
11919 0 : *free_p = true;
11920 : }
11921 : else
11922 51 : pretty_name = identifier_to_locale (IDENTIFIER_POINTER (name));
11923 :
11924 93 : return const_cast<char *> (pretty_name);
11925 : }
11926 :
11927 : /* If CANDIDATES contains exactly one candidate, return it, otherwise
11928 : return NULL. */
11929 :
11930 : static z_candidate *
11931 683 : single_z_candidate (z_candidate *candidates)
11932 : {
11933 0 : if (candidates == NULL)
11934 : return NULL;
11935 :
11936 668 : if (candidates->next)
11937 0 : return NULL;
11938 :
11939 : return candidates;
11940 : }
11941 :
11942 : /* If CANDIDATE is invalid due to a bad argument type, return the
11943 : pertinent conversion_info.
11944 :
11945 : Otherwise, return NULL. */
11946 :
11947 : static const conversion_info *
11948 339 : maybe_get_bad_conversion_for_unmatched_call (const z_candidate *candidate)
11949 : {
11950 : /* Must be an rr_arg_conversion or rr_bad_arg_conversion. */
11951 339 : rejection_reason *r = candidate->reason;
11952 :
11953 339 : if (r == NULL)
11954 : return NULL;
11955 :
11956 339 : switch (r->code)
11957 : {
11958 : default:
11959 : return NULL;
11960 :
11961 52 : case rr_arg_conversion:
11962 52 : return &r->u.conversion;
11963 :
11964 6 : case rr_bad_arg_conversion:
11965 6 : return &r->u.bad_conversion;
11966 : }
11967 : }
11968 :
11969 : /* Issue an error and note complaining about a bad argument type at a
11970 : callsite with a single candidate FNDECL.
11971 :
11972 : ARG_LOC is the location of the argument (or UNKNOWN_LOCATION, in which
11973 : case input_location is used).
11974 : FROM_TYPE is the type of the actual argument; TO_TYPE is the type of
11975 : the formal parameter. */
11976 :
11977 : void
11978 150 : complain_about_bad_argument (location_t arg_loc,
11979 : tree from_type, tree to_type,
11980 : tree fndecl, int parmnum)
11981 : {
11982 150 : auto_diagnostic_group d;
11983 150 : range_label_for_type_mismatch rhs_label (from_type, to_type);
11984 150 : range_label *label = &rhs_label;
11985 150 : if (arg_loc == UNKNOWN_LOCATION)
11986 : {
11987 6 : arg_loc = input_location;
11988 6 : label = NULL;
11989 : }
11990 150 : gcc_rich_location richloc (arg_loc, label, highlight_colors::percent_h);
11991 150 : error_at (&richloc,
11992 : "cannot convert %qH to %qI",
11993 : from_type, to_type);
11994 150 : maybe_inform_about_fndecl_for_bogus_argument_init
11995 150 : (fndecl,
11996 : parmnum,
11997 : highlight_colors::percent_i);
11998 150 : }
11999 :
12000 : /* Subroutine of build_new_method_call_1, for where there are no viable
12001 : candidates for the call. */
12002 :
12003 : static void
12004 689 : complain_about_no_candidates_for_method_call (tree instance,
12005 : z_candidate *candidates,
12006 : tree explicit_targs,
12007 : tree basetype,
12008 : tree optype, tree name,
12009 : bool skip_first_for_error,
12010 : vec<tree, va_gc> *user_args)
12011 : {
12012 689 : auto_diagnostic_group d;
12013 689 : if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
12014 0 : cxx_incomplete_type_error (instance, basetype);
12015 689 : else if (optype)
12016 6 : error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
12017 : basetype, optype, build_tree_list_vec (user_args),
12018 6 : TREE_TYPE (instance));
12019 : else
12020 : {
12021 : /* Special-case for when there's a single candidate that's failing
12022 : due to a bad argument type. */
12023 683 : if (z_candidate *candidate = single_z_candidate (candidates))
12024 339 : if (const conversion_info *conv
12025 339 : = maybe_get_bad_conversion_for_unmatched_call (candidate))
12026 : {
12027 58 : tree from_type = conv->from;
12028 58 : if (!TYPE_P (conv->from))
12029 6 : from_type = lvalue_type (conv->from);
12030 58 : complain_about_bad_argument (conv->loc,
12031 58 : from_type, conv->to_type,
12032 58 : candidate->fn, conv->n_arg);
12033 58 : return;
12034 : }
12035 :
12036 625 : tree arglist = build_tree_list_vec (user_args);
12037 625 : tree errname = name;
12038 625 : bool twiddle = false;
12039 625 : if (IDENTIFIER_CDTOR_P (errname))
12040 : {
12041 322 : twiddle = IDENTIFIER_DTOR_P (errname);
12042 322 : errname = constructor_name (basetype);
12043 : }
12044 625 : if (explicit_targs)
12045 48 : errname = lookup_template_function (errname, explicit_targs);
12046 625 : if (skip_first_for_error)
12047 3 : arglist = TREE_CHAIN (arglist);
12048 1250 : error ("no matching function for call to %<%T::%s%E(%A)%#V%>",
12049 625 : basetype, &"~"[!twiddle], errname, arglist,
12050 625 : TREE_TYPE (instance));
12051 : }
12052 631 : print_z_candidates (location_of (name), candidates);
12053 689 : }
12054 :
12055 : /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
12056 : be set, upon return, to the function called. ARGS may be NULL.
12057 : This may change ARGS. */
12058 :
12059 : tree
12060 101732637 : build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args,
12061 : tree conversion_path, int flags,
12062 : tree *fn_p, tsubst_flags_t complain)
12063 : {
12064 101732637 : struct z_candidate *candidates = 0, *cand;
12065 101732637 : tree explicit_targs = NULL_TREE;
12066 101732637 : tree basetype = NULL_TREE;
12067 101732637 : tree access_binfo;
12068 101732637 : tree optype;
12069 101732637 : tree first_mem_arg = NULL_TREE;
12070 101732637 : tree name;
12071 101732637 : bool skip_first_for_error;
12072 101732637 : vec<tree, va_gc> *user_args;
12073 101732637 : tree call;
12074 101732637 : tree fn;
12075 101732637 : int template_only = 0;
12076 101732637 : bool any_viable_p;
12077 101732637 : tree orig_instance;
12078 101732637 : tree orig_fns;
12079 101732637 : vec<tree, va_gc> *orig_args = NULL;
12080 :
12081 101732637 : auto_cond_timevar tv (TV_OVERLOAD);
12082 :
12083 101732637 : gcc_assert (instance != NULL_TREE);
12084 :
12085 : /* We don't know what function we're going to call, yet. */
12086 101732637 : if (fn_p)
12087 28552952 : *fn_p = NULL_TREE;
12088 :
12089 101732637 : if (error_operand_p (instance)
12090 101732637 : || !fns || error_operand_p (fns))
12091 1469452 : return error_mark_node;
12092 :
12093 100263185 : if (!BASELINK_P (fns))
12094 : {
12095 0 : if (complain & tf_error)
12096 0 : error ("call to non-function %qD", fns);
12097 0 : return error_mark_node;
12098 : }
12099 :
12100 100263185 : orig_instance = instance;
12101 100263185 : orig_fns = fns;
12102 :
12103 : /* Dismantle the baselink to collect all the information we need. */
12104 100263185 : if (!conversion_path)
12105 43998522 : conversion_path = BASELINK_BINFO (fns);
12106 100263185 : access_binfo = BASELINK_ACCESS_BINFO (fns);
12107 100263185 : optype = BASELINK_OPTYPE (fns);
12108 100263185 : fns = BASELINK_FUNCTIONS (fns);
12109 100263185 : if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12110 : {
12111 2739791 : explicit_targs = TREE_OPERAND (fns, 1);
12112 2739791 : fns = TREE_OPERAND (fns, 0);
12113 2739791 : template_only = 1;
12114 : }
12115 100263185 : gcc_assert (OVL_P (fns));
12116 100263185 : fn = OVL_FIRST (fns);
12117 100263185 : name = DECL_NAME (fn);
12118 :
12119 100263185 : basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
12120 100263185 : gcc_assert (CLASS_TYPE_P (basetype));
12121 :
12122 100263185 : user_args = args == NULL ? NULL : *args;
12123 : /* Under DR 147 A::A() is an invalid constructor call,
12124 : not a functional cast. */
12125 100263185 : if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
12126 : {
12127 54 : if (! (complain & tf_error))
12128 0 : return error_mark_node;
12129 :
12130 54 : basetype = DECL_CONTEXT (fn);
12131 54 : name = constructor_name (basetype);
12132 54 : auto_diagnostic_group d;
12133 54 : if (permerror (input_location,
12134 : "cannot call constructor %<%T::%D%> directly",
12135 : basetype, name))
12136 54 : inform (input_location, "for a function-style cast, remove the "
12137 : "redundant %<::%D%>", name);
12138 54 : call = build_functional_cast (input_location, basetype,
12139 : build_tree_list_vec (user_args),
12140 : complain);
12141 54 : return call;
12142 54 : }
12143 :
12144 100263131 : if (processing_template_decl)
12145 7197259 : orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
12146 :
12147 : /* Process the argument list. */
12148 100177637 : if (args != NULL && *args != NULL)
12149 : {
12150 85396602 : *args = resolve_args (*args, complain);
12151 85396602 : if (*args == NULL)
12152 189 : return error_mark_node;
12153 : user_args = *args;
12154 : }
12155 :
12156 : /* Consider the object argument to be used even if we end up selecting a
12157 : static member function. */
12158 100262942 : instance = mark_type_use (instance);
12159 :
12160 : /* Figure out whether to skip the first argument for the error
12161 : message we will display to users if an error occurs. We don't
12162 : want to display any compiler-generated arguments. The "this"
12163 : pointer hasn't been added yet. However, we must remove the VTT
12164 : pointer if this is a call to a base-class constructor or
12165 : destructor. */
12166 100262942 : skip_first_for_error = false;
12167 100262942 : if (IDENTIFIER_CDTOR_P (name))
12168 : {
12169 : /* Callers should explicitly indicate whether they want to ctor
12170 : the complete object or just the part without virtual bases. */
12171 53488551 : gcc_assert (name != ctor_identifier);
12172 :
12173 : /* Remove the VTT pointer, if present. */
12174 51074286 : if ((name == base_ctor_identifier || name == base_dtor_identifier)
12175 55133144 : && CLASSTYPE_VBASECLASSES (basetype))
12176 : skip_first_for_error = true;
12177 :
12178 : /* It's OK to call destructors and constructors on cv-qualified
12179 : objects. Therefore, convert the INSTANCE to the unqualified
12180 : type, if necessary. */
12181 53488551 : if (!same_type_p (basetype, TREE_TYPE (instance)))
12182 : {
12183 705797 : instance = build_this (instance);
12184 705797 : instance = build_nop (build_pointer_type (basetype), instance);
12185 705797 : instance = build_fold_indirect_ref (instance);
12186 : }
12187 : }
12188 : else
12189 93548782 : gcc_assert (!DECL_DESTRUCTOR_P (fn) && !DECL_CONSTRUCTOR_P (fn));
12190 :
12191 : /* For the overload resolution we need to find the actual `this`
12192 : that would be captured if the call turns out to be to a
12193 : non-static member function. Do not actually capture it at this
12194 : point. */
12195 200525884 : if (DECL_CONSTRUCTOR_P (fn))
12196 : /* Constructors don't use the enclosing 'this'. */
12197 : first_mem_arg = instance;
12198 : else
12199 73427997 : first_mem_arg = maybe_resolve_dummy (instance, false);
12200 :
12201 100262942 : conversion_obstack_sentinel cos;
12202 :
12203 : /* The number of arguments artificial parms in ARGS; we subtract one because
12204 : there's no 'this' in ARGS. */
12205 100262942 : unsigned skip = num_artificial_parms_for (fn) - 1;
12206 :
12207 : /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
12208 : initializer, not T({ }). */
12209 100262942 : if (DECL_CONSTRUCTOR_P (fn)
12210 22926180 : && vec_safe_length (user_args) > skip
12211 120908712 : && DIRECT_LIST_INIT_P ((*user_args)[skip]))
12212 : {
12213 1728877 : tree init_list = (*user_args)[skip];
12214 1728877 : tree init = NULL_TREE;
12215 :
12216 1728877 : gcc_assert (user_args->length () == skip + 1
12217 : && !(flags & LOOKUP_ONLYCONVERTING));
12218 :
12219 : /* If the initializer list has no elements and T is a class type with
12220 : a default constructor, the object is value-initialized. Handle
12221 : this here so we don't need to handle it wherever we use
12222 : build_special_member_call. */
12223 1728877 : if (CONSTRUCTOR_NELTS (init_list) == 0
12224 197021 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
12225 : /* For a user-provided default constructor, use the normal
12226 : mechanisms so that protected access works. */
12227 197021 : && type_has_non_user_provided_default_constructor (basetype)
12228 1654837 : && !processing_template_decl)
12229 122978 : init = build_value_init (basetype, complain);
12230 :
12231 : /* If BASETYPE is an aggregate, we need to do aggregate
12232 : initialization. */
12233 1605899 : else if (CP_AGGREGATE_TYPE_P (basetype))
12234 : {
12235 41 : init = reshape_init (basetype, init_list, complain);
12236 41 : init = digest_init (basetype, init, complain);
12237 : }
12238 :
12239 123019 : if (init)
12240 : {
12241 123019 : if (is_dummy_object (instance))
12242 36162 : return get_target_expr (init, complain);
12243 86857 : return cp_build_init_expr (instance, init);
12244 : }
12245 :
12246 : /* Otherwise go ahead with overload resolution. */
12247 1605858 : add_list_candidates (fns, first_mem_arg, user_args,
12248 : basetype, explicit_targs, template_only,
12249 : conversion_path, access_binfo, flags,
12250 : &candidates, complain);
12251 : }
12252 : else
12253 98534065 : add_candidates (fns, first_mem_arg, user_args, optype,
12254 : explicit_targs, template_only, conversion_path,
12255 : access_binfo, flags, &candidates, complain);
12256 :
12257 100139923 : any_viable_p = false;
12258 100139923 : candidates = splice_viable (candidates, false, &any_viable_p);
12259 :
12260 100139923 : if (!any_viable_p)
12261 : {
12262 : /* [dcl.init], 17.6.2.2:
12263 :
12264 : Otherwise, if no constructor is viable, the destination type is
12265 : a (possibly cv-qualified) aggregate class A, and the initializer
12266 : is a parenthesized expression-list, the object is initialized as
12267 : follows...
12268 :
12269 : We achieve this by building up a CONSTRUCTOR, as for list-init,
12270 : and setting CONSTRUCTOR_IS_PAREN_INIT to distinguish between
12271 : the two. */
12272 27358 : if (DECL_CONSTRUCTOR_P (fn)
12273 20587 : && !(flags & LOOKUP_ONLYCONVERTING)
12274 20563 : && cxx_dialect >= cxx20
12275 19942 : && CP_AGGREGATE_TYPE_P (basetype)
12276 27358 : && !vec_safe_is_empty (user_args))
12277 : {
12278 : /* Create a CONSTRUCTOR from ARGS, e.g. {1, 2} from <1, 2>. */
12279 1171 : tree ctor = build_constructor_from_vec (init_list_type_node,
12280 : user_args);
12281 1171 : CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true;
12282 1171 : CONSTRUCTOR_IS_PAREN_INIT (ctor) = true;
12283 1171 : if (is_dummy_object (instance))
12284 : return ctor;
12285 : else
12286 : {
12287 677 : ctor = digest_init (basetype, ctor, complain);
12288 677 : if (ctor == error_mark_node)
12289 : return error_mark_node;
12290 457 : return cp_build_init_expr (instance, ctor);
12291 : }
12292 : }
12293 26187 : if (complain & tf_error)
12294 689 : complain_about_no_candidates_for_method_call (instance, candidates,
12295 : explicit_targs, basetype,
12296 : optype, name,
12297 : skip_first_for_error,
12298 : user_args);
12299 26187 : call = error_mark_node;
12300 : }
12301 : else
12302 : {
12303 100112565 : cand = tourney (candidates, complain);
12304 100112565 : if (cand == 0)
12305 : {
12306 193 : char *pretty_name;
12307 193 : bool free_p;
12308 193 : tree arglist;
12309 :
12310 193 : if (complain & tf_error)
12311 : {
12312 93 : pretty_name = name_as_c_string (name, basetype, &free_p);
12313 93 : arglist = build_tree_list_vec (user_args);
12314 93 : if (skip_first_for_error)
12315 0 : arglist = TREE_CHAIN (arglist);
12316 93 : auto_diagnostic_group d;
12317 186 : if (!any_strictly_viable (candidates))
12318 13 : error ("no matching function for call to %<%s(%A)%>",
12319 : pretty_name, arglist);
12320 : else
12321 80 : error ("call of overloaded %<%s(%A)%> is ambiguous",
12322 : pretty_name, arglist);
12323 93 : print_z_candidates (location_of (name), candidates);
12324 93 : if (free_p)
12325 0 : free (pretty_name);
12326 93 : }
12327 193 : call = error_mark_node;
12328 193 : if (fn_p)
12329 84 : *fn_p = error_mark_node;
12330 : }
12331 : else
12332 : {
12333 100112372 : fn = cand->fn;
12334 100112372 : call = NULL_TREE;
12335 :
12336 100112372 : if (!(flags & LOOKUP_NONVIRTUAL)
12337 76814170 : && DECL_PURE_VIRTUAL_P (fn)
12338 210450 : && instance == current_class_ref
12339 100277622 : && (complain & tf_warning))
12340 : {
12341 : /* This is not an error, it is runtime undefined
12342 : behavior. */
12343 165250 : if (!current_function_decl)
12344 3 : warning (0, "pure virtual %q#D called from "
12345 : "non-static data member initializer", fn);
12346 165247 : else if (DECL_CONSTRUCTOR_P (current_function_decl)
12347 165247 : || DECL_DESTRUCTOR_P (current_function_decl))
12348 9 : warning (0, (DECL_CONSTRUCTOR_P (current_function_decl)
12349 : ? G_("pure virtual %q#D called from constructor")
12350 : : G_("pure virtual %q#D called from destructor")),
12351 : fn);
12352 : }
12353 :
12354 114914506 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fn)
12355 170623704 : && !DECL_CONSTRUCTOR_P (fn)
12356 158733025 : && is_dummy_object (instance))
12357 : {
12358 52660 : instance = maybe_resolve_dummy (instance, true);
12359 52660 : if (instance == error_mark_node)
12360 : call = error_mark_node;
12361 52660 : else if (!is_dummy_object (instance))
12362 : {
12363 : /* We captured 'this' in the current lambda now that
12364 : we know we really need it. */
12365 52158 : cand->first_arg = instance;
12366 : }
12367 502 : else if (current_class_ptr && any_dependent_bases_p ())
12368 : /* We can't tell until instantiation time whether we can use
12369 : *this as the implicit object argument. */;
12370 : else
12371 : {
12372 463 : if (complain & tf_error)
12373 69 : error ("cannot call member function %qD without object",
12374 : fn);
12375 463 : call = error_mark_node;
12376 : }
12377 : }
12378 :
12379 100112372 : if (call != error_mark_node)
12380 : {
12381 : /* Now we know what function is being called. */
12382 100111909 : if (fn_p)
12383 27069405 : *fn_p = fn;
12384 : /* Build the actual CALL_EXPR. */
12385 100111909 : call = build_over_call (cand, flags, complain);
12386 :
12387 : /* Suppress warnings for if (my_struct.operator= (x)) where
12388 : my_struct is implicitly converted to bool. */
12389 100111909 : if (TREE_CODE (call) == MODIFY_EXPR)
12390 3193795 : suppress_warning (call, OPT_Wparentheses);
12391 :
12392 : /* In an expression of the form `a->f()' where `f' turns
12393 : out to be a static member function, `a' is
12394 : none-the-less evaluated. */
12395 100111909 : if (!is_dummy_object (instance))
12396 77542591 : call = keep_unused_object_arg (call, instance, fn);
12397 100111909 : if (call != error_mark_node
12398 198212216 : && DECL_DESTRUCTOR_P (cand->fn)
12399 126764566 : && !VOID_TYPE_P (TREE_TYPE (call)))
12400 : /* An explicit call of the form "x->~X()" has type
12401 : "void". However, on platforms where destructors
12402 : return "this" (i.e., those where
12403 : targetm.cxx.cdtor_returns_this is true), such calls
12404 : will appear to have a return value of pointer type
12405 : to the low-level call machinery. We do not want to
12406 : change the low-level machinery, since we want to be
12407 : able to optimize "delete f()" on such platforms as
12408 : "operator delete(~X(f()))" (rather than generating
12409 : "t = f(), ~X(t), operator delete (t)"). */
12410 15460326 : call = build_nop (void_type_node, call);
12411 : }
12412 : }
12413 : }
12414 :
12415 100138752 : if (processing_template_decl && call != error_mark_node)
12416 : {
12417 7197205 : bool cast_to_void = false;
12418 :
12419 7197205 : if (TREE_CODE (call) == COMPOUND_EXPR)
12420 10 : call = TREE_OPERAND (call, 1);
12421 7197195 : else if (TREE_CODE (call) == NOP_EXPR)
12422 : {
12423 0 : cast_to_void = true;
12424 0 : call = TREE_OPERAND (call, 0);
12425 : }
12426 7197205 : if (INDIRECT_REF_P (call))
12427 610718 : call = TREE_OPERAND (call, 0);
12428 :
12429 : /* Prune all but the selected function from the original overload
12430 : set so that we can avoid some duplicate work at instantiation time. */
12431 7197205 : if (really_overloaded_fn (fns))
12432 : {
12433 1655609 : if (DECL_TEMPLATE_INFO (fn)
12434 1655609 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
12435 : {
12436 : /* Use the selected template, not the specialization, so that
12437 : this looks like an actual lookup result for sake of
12438 : filter_memfn_lookup. */
12439 :
12440 280678 : if (OVL_SINGLE_P (fns))
12441 : /* If the original overload set consists of a single function
12442 : template, this isn't beneficial. */
12443 240599 : goto skip_prune;
12444 :
12445 40079 : fn = ovl_make (DECL_TI_TEMPLATE (fn));
12446 40079 : if (template_only)
12447 30369 : fn = lookup_template_function (fn, explicit_targs);
12448 : }
12449 1415010 : orig_fns = copy_node (orig_fns);
12450 1415010 : BASELINK_FUNCTIONS (orig_fns) = fn;
12451 1415010 : BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (orig_fns) = true;
12452 : }
12453 :
12454 5541596 : skip_prune:
12455 7197205 : call = (build_min_non_dep_call_vec
12456 7197205 : (call,
12457 7197205 : build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
12458 : orig_instance, orig_fns, NULL_TREE),
12459 : orig_args));
12460 7197205 : SET_EXPR_LOCATION (call, input_location);
12461 7197205 : call = convert_from_reference (call);
12462 7197205 : if (cast_to_void)
12463 0 : call = build_nop (void_type_node, call);
12464 : }
12465 :
12466 100138752 : if (orig_args != NULL)
12467 7111759 : release_tree_vector (orig_args);
12468 :
12469 : return call;
12470 101732637 : }
12471 :
12472 : /* Returns true iff standard conversion sequence ICS1 is a proper
12473 : subsequence of ICS2. */
12474 :
12475 : static bool
12476 80260266 : is_subseq (conversion *ics1, conversion *ics2)
12477 : {
12478 : /* We can assume that a conversion of the same code
12479 : between the same types indicates a subsequence since we only get
12480 : here if the types we are converting from are the same. */
12481 :
12482 80260266 : while (ics1->kind == ck_rvalue
12483 95471669 : || ics1->kind == ck_lvalue)
12484 15211403 : ics1 = next_conversion (ics1);
12485 :
12486 : while (1)
12487 : {
12488 107646227 : while (ics2->kind == ck_rvalue
12489 107646227 : || ics2->kind == ck_lvalue)
12490 15211403 : ics2 = next_conversion (ics2);
12491 :
12492 92434824 : if (ics2->kind == ck_user
12493 92434824 : || !has_next (ics2->kind))
12494 : /* At this point, ICS1 cannot be a proper subsequence of
12495 : ICS2. We can get a USER_CONV when we are comparing the
12496 : second standard conversion sequence of two user conversion
12497 : sequences. */
12498 : return false;
12499 :
12500 12181026 : ics2 = next_conversion (ics2);
12501 :
12502 12181026 : while (ics2->kind == ck_rvalue
12503 19277364 : || ics2->kind == ck_lvalue)
12504 7096338 : ics2 = next_conversion (ics2);
12505 :
12506 12181026 : if (ics2->kind == ics1->kind
12507 6513 : && same_type_p (ics2->type, ics1->type)
12508 12187494 : && (ics1->kind == ck_identity
12509 6468 : || same_type_p (next_conversion (ics2)->type,
12510 : next_conversion (ics1)->type)))
12511 : return true;
12512 : }
12513 : }
12514 :
12515 : /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
12516 : be any _TYPE nodes. */
12517 :
12518 : bool
12519 649129837 : is_properly_derived_from (tree derived, tree base)
12520 : {
12521 649129837 : if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
12522 : return false;
12523 :
12524 : /* We only allow proper derivation here. The DERIVED_FROM_P macro
12525 : considers every class derived from itself. */
12526 486333190 : return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
12527 486333190 : && DERIVED_FROM_P (base, derived));
12528 : }
12529 :
12530 : /* We build the ICS for an implicit object parameter as a pointer
12531 : conversion sequence. However, such a sequence should be compared
12532 : as if it were a reference conversion sequence. If ICS is the
12533 : implicit conversion sequence for an implicit object parameter,
12534 : modify it accordingly. */
12535 :
12536 : static void
12537 167649498 : maybe_handle_implicit_object (conversion **ics)
12538 : {
12539 167649498 : if ((*ics)->this_p)
12540 : {
12541 : /* [over.match.funcs]
12542 :
12543 : For non-static member functions, the type of the
12544 : implicit object parameter is "reference to cv X"
12545 : where X is the class of which the function is a
12546 : member and cv is the cv-qualification on the member
12547 : function declaration. */
12548 13414190 : conversion *t = *ics;
12549 13414190 : tree reference_type;
12550 :
12551 : /* The `this' parameter is a pointer to a class type. Make the
12552 : implicit conversion talk about a reference to that same class
12553 : type. */
12554 13414190 : reference_type = TREE_TYPE (t->type);
12555 13414190 : reference_type = build_reference_type (reference_type);
12556 :
12557 13414190 : if (t->kind == ck_qual)
12558 3199623 : t = next_conversion (t);
12559 13414190 : if (t->kind == ck_ptr)
12560 626446 : t = next_conversion (t);
12561 13414190 : t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
12562 13414190 : t = direct_reference_binding (reference_type, t);
12563 13414190 : t->this_p = 1;
12564 13414190 : t->rvaluedness_matches_p = 0;
12565 13414190 : *ics = t;
12566 : }
12567 167649498 : }
12568 :
12569 : /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
12570 : and return the initial reference binding conversion. Otherwise,
12571 : leave *ICS unchanged and return NULL. */
12572 :
12573 : static conversion *
12574 167649498 : maybe_handle_ref_bind (conversion **ics)
12575 : {
12576 167649498 : if ((*ics)->kind == ck_ref_bind)
12577 : {
12578 61852807 : conversion *old_ics = *ics;
12579 61852807 : *ics = next_conversion (old_ics);
12580 61852807 : (*ics)->user_conv_p = old_ics->user_conv_p;
12581 61852807 : return old_ics;
12582 : }
12583 :
12584 : return NULL;
12585 : }
12586 :
12587 : /* Get the expression at the beginning of the conversion chain C. */
12588 :
12589 : static tree
12590 51 : conv_get_original_expr (conversion *c)
12591 : {
12592 60 : for (; c; c = next_conversion (c))
12593 60 : if (c->kind == ck_identity || c->kind == ck_ambig || c->kind == ck_aggr)
12594 51 : return c->u.expr;
12595 : return NULL_TREE;
12596 : }
12597 :
12598 : /* Return a tree representing the number of elements initialized by the
12599 : list-initialization C. The caller must check that C converts to an
12600 : array type. */
12601 :
12602 : static tree
12603 126 : nelts_initialized_by_list_init (conversion *c)
12604 : {
12605 : /* If the array we're converting to has a dimension, we'll use that. */
12606 126 : if (TYPE_DOMAIN (c->type))
12607 84 : return array_type_nelts_top (c->type);
12608 : else
12609 : {
12610 : /* Otherwise, we look at how many elements the constructor we're
12611 : initializing from has. */
12612 42 : tree ctor = conv_get_original_expr (c);
12613 72 : return size_int (CONSTRUCTOR_NELTS (ctor));
12614 : }
12615 : }
12616 :
12617 : /* True iff C is a conversion that binds a reference or a pointer to
12618 : an array of unknown bound. */
12619 :
12620 : static inline bool
12621 30889484 : conv_binds_to_array_of_unknown_bound (conversion *c)
12622 : {
12623 : /* ck_ref_bind won't have the reference stripped. */
12624 30889484 : tree type = non_reference (c->type);
12625 : /* ck_qual won't have the pointer stripped. */
12626 30889484 : type = strip_pointer_operator (type);
12627 30889484 : return (TREE_CODE (type) == ARRAY_TYPE
12628 30889484 : && TYPE_DOMAIN (type) == NULL_TREE);
12629 : }
12630 :
12631 : /* Compare two implicit conversion sequences according to the rules set out in
12632 : [over.ics.rank]. Return values:
12633 :
12634 : 1: ics1 is better than ics2
12635 : -1: ics2 is better than ics1
12636 : 0: ics1 and ics2 are indistinguishable */
12637 :
12638 : static int
12639 83824790 : compare_ics (conversion *ics1, conversion *ics2)
12640 : {
12641 83824790 : tree from_type1;
12642 83824790 : tree from_type2;
12643 83824790 : tree to_type1;
12644 83824790 : tree to_type2;
12645 83824790 : tree deref_from_type1 = NULL_TREE;
12646 83824790 : tree deref_from_type2 = NULL_TREE;
12647 83824790 : tree deref_to_type1 = NULL_TREE;
12648 83824790 : tree deref_to_type2 = NULL_TREE;
12649 83824790 : conversion_rank rank1, rank2;
12650 :
12651 : /* REF_BINDING is nonzero if the result of the conversion sequence
12652 : is a reference type. In that case REF_CONV is the reference
12653 : binding conversion. */
12654 83824790 : conversion *ref_conv1;
12655 83824790 : conversion *ref_conv2;
12656 :
12657 : /* Compare badness before stripping the reference conversion. */
12658 83824790 : if (ics1->bad_p > ics2->bad_p)
12659 : return -1;
12660 83824776 : else if (ics1->bad_p < ics2->bad_p)
12661 : return 1;
12662 :
12663 : /* Handle implicit object parameters. */
12664 83824749 : maybe_handle_implicit_object (&ics1);
12665 83824749 : maybe_handle_implicit_object (&ics2);
12666 :
12667 : /* Handle reference parameters. */
12668 83824749 : ref_conv1 = maybe_handle_ref_bind (&ics1);
12669 83824749 : ref_conv2 = maybe_handle_ref_bind (&ics2);
12670 :
12671 : /* List-initialization sequence L1 is a better conversion sequence than
12672 : list-initialization sequence L2 if L1 converts to
12673 : std::initializer_list<X> for some X and L2 does not. */
12674 83824749 : if (ics1->kind == ck_list && ics2->kind != ck_list)
12675 : return 1;
12676 83823938 : if (ics2->kind == ck_list && ics1->kind != ck_list)
12677 : return -1;
12678 :
12679 : /* [over.ics.rank]
12680 :
12681 : When comparing the basic forms of implicit conversion sequences (as
12682 : defined in _over.best.ics_)
12683 :
12684 : --a standard conversion sequence (_over.ics.scs_) is a better
12685 : conversion sequence than a user-defined conversion sequence
12686 : or an ellipsis conversion sequence, and
12687 :
12688 : --a user-defined conversion sequence (_over.ics.user_) is a
12689 : better conversion sequence than an ellipsis conversion sequence
12690 : (_over.ics.ellipsis_). */
12691 : /* Use BAD_CONVERSION_RANK because we already checked for a badness
12692 : mismatch. If both ICS are bad, we try to make a decision based on
12693 : what would have happened if they'd been good. This is not an
12694 : extension, we'll still give an error when we build up the call; this
12695 : just helps us give a more helpful error message. */
12696 83823673 : rank1 = BAD_CONVERSION_RANK (ics1);
12697 83823673 : rank2 = BAD_CONVERSION_RANK (ics2);
12698 :
12699 83823673 : if (rank1 > rank2)
12700 : return -1;
12701 72798677 : else if (rank1 < rank2)
12702 : return 1;
12703 :
12704 40574637 : if (ics1->ellipsis_p)
12705 : /* Both conversions are ellipsis conversions. */
12706 : return 0;
12707 :
12708 : /* User-defined conversion sequence U1 is a better conversion sequence
12709 : than another user-defined conversion sequence U2 if they contain the
12710 : same user-defined conversion operator or constructor and if the sec-
12711 : ond standard conversion sequence of U1 is better than the second
12712 : standard conversion sequence of U2. */
12713 :
12714 : /* Handle list-conversion with the same code even though it isn't always
12715 : ranked as a user-defined conversion and it doesn't have a second
12716 : standard conversion sequence; it will still have the desired effect.
12717 : Specifically, we need to do the reference binding comparison at the
12718 : end of this function. */
12719 :
12720 40574591 : if (ics1->user_conv_p || ics1->kind == ck_list
12721 39842192 : || ics1->kind == ck_aggr || ics2->kind == ck_aggr)
12722 : {
12723 732468 : conversion *t1 = strip_standard_conversion (ics1);
12724 732468 : conversion *t2 = strip_standard_conversion (ics2);
12725 :
12726 732468 : if (!t1 || !t2 || t1->kind != t2->kind)
12727 : return 0;
12728 732449 : else if (t1->kind == ck_user)
12729 : {
12730 716658 : tree f1 = t1->cand ? t1->cand->fn : t1->type;
12731 716658 : tree f2 = t2->cand ? t2->cand->fn : t2->type;
12732 716658 : if (f1 != f2)
12733 : return 0;
12734 : }
12735 : /* List-initialization sequence L1 is a better conversion sequence than
12736 : list-initialization sequence L2 if
12737 :
12738 : -- L1 and L2 convert to arrays of the same element type, and either
12739 : the number of elements n1 initialized by L1 is less than the number
12740 : of elements n2 initialized by L2, or n1=n2 and L2 converts to an array
12741 : of unknown bound and L1 does not. (Added in CWG 1307 and extended by
12742 : P0388R4.) */
12743 15791 : else if (t1->kind == ck_aggr
12744 14779 : && TREE_CODE (t1->type) == ARRAY_TYPE
12745 66 : && TREE_CODE (t2->type) == ARRAY_TYPE
12746 15857 : && same_type_p (TREE_TYPE (t1->type), TREE_TYPE (t2->type)))
12747 : {
12748 63 : tree n1 = nelts_initialized_by_list_init (t1);
12749 63 : tree n2 = nelts_initialized_by_list_init (t2);
12750 63 : if (tree_int_cst_lt (n1, n2))
12751 : return 1;
12752 24 : else if (tree_int_cst_lt (n2, n1))
12753 : return -1;
12754 : /* The n1 == n2 case. */
12755 24 : bool c1 = conv_binds_to_array_of_unknown_bound (t1);
12756 24 : bool c2 = conv_binds_to_array_of_unknown_bound (t2);
12757 24 : if (c1 && !c2)
12758 : return -1;
12759 6 : else if (!c1 && c2)
12760 : return 1;
12761 : else
12762 6 : return 0;
12763 : }
12764 : else
12765 : {
12766 : /* For ambiguous or aggregate conversions, use the target type as
12767 : a proxy for the conversion function. */
12768 15728 : if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
12769 : return 0;
12770 : }
12771 :
12772 : /* We can just fall through here, after setting up
12773 : FROM_TYPE1 and FROM_TYPE2. */
12774 729372 : from_type1 = t1->type;
12775 729372 : from_type2 = t2->type;
12776 729372 : }
12777 : else
12778 : {
12779 : conversion *t1;
12780 : conversion *t2;
12781 :
12782 : /* We're dealing with two standard conversion sequences.
12783 :
12784 : [over.ics.rank]
12785 :
12786 : Standard conversion sequence S1 is a better conversion
12787 : sequence than standard conversion sequence S2 if
12788 :
12789 : --S1 is a proper subsequence of S2 (comparing the conversion
12790 : sequences in the canonical form defined by _over.ics.scs_,
12791 : excluding any Lvalue Transformation; the identity
12792 : conversion sequence is considered to be a subsequence of
12793 : any non-identity conversion sequence */
12794 :
12795 : t1 = ics1;
12796 57181384 : while (t1->kind != ck_identity)
12797 17339261 : t1 = next_conversion (t1);
12798 39842123 : from_type1 = t1->type;
12799 :
12800 39842123 : t2 = ics2;
12801 57073426 : while (t2->kind != ck_identity)
12802 17231303 : t2 = next_conversion (t2);
12803 39842123 : from_type2 = t2->type;
12804 : }
12805 :
12806 : /* One sequence can only be a subsequence of the other if they start with
12807 : the same type. They can start with different types when comparing the
12808 : second standard conversion sequence in two user-defined conversion
12809 : sequences. */
12810 40571495 : if (same_type_p (from_type1, from_type2))
12811 : {
12812 40133332 : if (is_subseq (ics1, ics2))
12813 : return 1;
12814 40126934 : if (is_subseq (ics2, ics1))
12815 : return -1;
12816 : }
12817 :
12818 : /* [over.ics.rank]
12819 :
12820 : Or, if not that,
12821 :
12822 : --the rank of S1 is better than the rank of S2 (by the rules
12823 : defined below):
12824 :
12825 : Standard conversion sequences are ordered by their ranks: an Exact
12826 : Match is a better conversion than a Promotion, which is a better
12827 : conversion than a Conversion.
12828 :
12829 : Two conversion sequences with the same rank are indistinguishable
12830 : unless one of the following rules applies:
12831 :
12832 : --A conversion that does not a convert a pointer, pointer to member,
12833 : or std::nullptr_t to bool is better than one that does.
12834 :
12835 : The ICS_STD_RANK automatically handles the pointer-to-bool rule,
12836 : so that we do not have to check it explicitly. */
12837 40565027 : if (ics1->rank < ics2->rank)
12838 : return 1;
12839 40564947 : else if (ics2->rank < ics1->rank)
12840 : return -1;
12841 :
12842 40564947 : to_type1 = ics1->type;
12843 40564947 : to_type2 = ics2->type;
12844 :
12845 : /* A conversion from scalar arithmetic type to complex is worse than a
12846 : conversion between scalar arithmetic types. */
12847 40564947 : if (same_type_p (from_type1, from_type2)
12848 40126784 : && ARITHMETIC_TYPE_P (from_type1)
12849 8128121 : && ARITHMETIC_TYPE_P (to_type1)
12850 8127948 : && ARITHMETIC_TYPE_P (to_type2)
12851 40564947 : && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
12852 8127894 : != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
12853 : {
12854 982 : if (TREE_CODE (to_type1) == COMPLEX_TYPE)
12855 : return -1;
12856 : else
12857 979 : return 1;
12858 : }
12859 :
12860 40563965 : {
12861 : /* A conversion in either direction between floating-point type FP1 and
12862 : floating-point type FP2 is better than a conversion in the same
12863 : direction between FP1 and arithmetic type T3 if
12864 : - the floating-point conversion rank of FP1 is equal to the rank of
12865 : FP2, and
12866 : - T3 is not a floating-point type, or T3 is a floating-point type
12867 : whose rank is not equal to the rank of FP1, or the floating-point
12868 : conversion subrank of FP2 is greater than the subrank of T3. */
12869 40563965 : tree fp1 = from_type1;
12870 40563965 : tree fp2 = to_type1;
12871 40563965 : tree fp3 = from_type2;
12872 40563965 : tree t3 = to_type2;
12873 40563965 : int ret = 1;
12874 40563965 : if (TYPE_MAIN_VARIANT (fp2) == TYPE_MAIN_VARIANT (t3))
12875 : {
12876 35601669 : std::swap (fp1, fp2);
12877 35601669 : std::swap (fp3, t3);
12878 : }
12879 40563965 : if (TYPE_MAIN_VARIANT (fp1) == TYPE_MAIN_VARIANT (fp3)
12880 40563878 : && SCALAR_FLOAT_TYPE_P (fp1)
12881 : /* Only apply this rule if at least one of the 3 types is
12882 : extended floating-point type, otherwise keep them as
12883 : before for compatibility reasons with types like __float128.
12884 : float, double and long double alone have different conversion
12885 : ranks and so when just those 3 types are involved, this
12886 : rule doesn't trigger. */
12887 44945990 : && (extended_float_type_p (fp1)
12888 4302589 : || (SCALAR_FLOAT_TYPE_P (fp2) && extended_float_type_p (fp2))
12889 4211717 : || (SCALAR_FLOAT_TYPE_P (t3) && extended_float_type_p (t3))))
12890 : {
12891 170308 : if (TREE_CODE (fp2) != REAL_TYPE)
12892 : {
12893 33642 : ret = -ret;
12894 33642 : std::swap (fp2, t3);
12895 : }
12896 170308 : if (SCALAR_FLOAT_TYPE_P (fp2))
12897 : {
12898 : /* cp_compare_floating_point_conversion_ranks returns -1, 0 or 1
12899 : if the conversion rank is equal (-1 or 1 if the subrank is
12900 : different). */
12901 151843 : if (IN_RANGE (cp_compare_floating_point_conversion_ranks (fp1,
12902 : fp2),
12903 : -1, 1))
12904 : {
12905 : /* Conversion ranks of FP1 and FP2 are equal. */
12906 94542 : if (TREE_CODE (t3) != REAL_TYPE
12907 94542 : || !IN_RANGE (cp_compare_floating_point_conversion_ranks
12908 : (fp1, t3),
12909 : -1, 1))
12910 : /* FP1 <-> FP2 conversion is better. */
12911 83824790 : return ret;
12912 746 : int c = cp_compare_floating_point_conversion_ranks (fp2, t3);
12913 746 : gcc_assert (IN_RANGE (c, -1, 1));
12914 746 : if (c == 1)
12915 : /* Conversion subrank of FP2 is greater than subrank of T3.
12916 : FP1 <-> FP2 conversion is better. */
12917 : return ret;
12918 746 : else if (c == -1)
12919 : /* Conversion subrank of FP2 is less than subrank of T3.
12920 : FP1 <-> T3 conversion is better. */
12921 0 : return -ret;
12922 : }
12923 57301 : else if (SCALAR_FLOAT_TYPE_P (t3)
12924 57301 : && IN_RANGE (cp_compare_floating_point_conversion_ranks
12925 : (fp1, t3),
12926 : -1, 1))
12927 : /* Conversion ranks of FP1 and FP2 are not equal, conversion
12928 : ranks of FP1 and T3 are equal.
12929 : FP1 <-> T3 conversion is better. */
12930 10061 : return -ret;
12931 : }
12932 : }
12933 : }
12934 :
12935 40460108 : if (TYPE_PTR_P (from_type1)
12936 5263848 : && TYPE_PTR_P (from_type2)
12937 5263832 : && TYPE_PTR_P (to_type1)
12938 5263808 : && TYPE_PTR_P (to_type2))
12939 : {
12940 5263808 : deref_from_type1 = TREE_TYPE (from_type1);
12941 5263808 : deref_from_type2 = TREE_TYPE (from_type2);
12942 5263808 : deref_to_type1 = TREE_TYPE (to_type1);
12943 5263808 : deref_to_type2 = TREE_TYPE (to_type2);
12944 : }
12945 : /* The rules for pointers to members A::* are just like the rules
12946 : for pointers A*, except opposite: if B is derived from A then
12947 : A::* converts to B::*, not vice versa. For that reason, we
12948 : switch the from_ and to_ variables here. */
12949 59 : else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2)
12950 59 : && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2))
12951 35196300 : || (TYPE_PTRMEMFUNC_P (from_type1)
12952 437 : && TYPE_PTRMEMFUNC_P (from_type2)
12953 437 : && TYPE_PTRMEMFUNC_P (to_type1)
12954 437 : && TYPE_PTRMEMFUNC_P (to_type2)))
12955 : {
12956 496 : deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
12957 496 : deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
12958 496 : deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
12959 496 : deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
12960 : }
12961 :
12962 5264304 : if (deref_from_type1 != NULL_TREE
12963 5264304 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
12964 307283 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
12965 : {
12966 : /* This was one of the pointer or pointer-like conversions.
12967 :
12968 : [over.ics.rank]
12969 :
12970 : --If class B is derived directly or indirectly from class A,
12971 : conversion of B* to A* is better than conversion of B* to
12972 : void*, and conversion of A* to void* is better than
12973 : conversion of B* to void*. */
12974 307283 : if (VOID_TYPE_P (deref_to_type1)
12975 57 : && VOID_TYPE_P (deref_to_type2))
12976 : {
12977 14 : if (is_properly_derived_from (deref_from_type1,
12978 : deref_from_type2))
12979 : return -1;
12980 14 : else if (is_properly_derived_from (deref_from_type2,
12981 : deref_from_type1))
12982 : return 1;
12983 : }
12984 307269 : else if (VOID_TYPE_P (deref_to_type1)
12985 307226 : || VOID_TYPE_P (deref_to_type2))
12986 : {
12987 47 : if (same_type_p (deref_from_type1, deref_from_type2))
12988 : {
12989 47 : if (VOID_TYPE_P (deref_to_type2))
12990 : {
12991 4 : if (is_properly_derived_from (deref_from_type1,
12992 : deref_to_type1))
12993 : return 1;
12994 : }
12995 : /* We know that DEREF_TO_TYPE1 is `void' here. */
12996 43 : else if (is_properly_derived_from (deref_from_type1,
12997 : deref_to_type2))
12998 : return -1;
12999 : }
13000 : }
13001 307222 : else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
13002 307222 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
13003 : {
13004 : /* [over.ics.rank]
13005 :
13006 : --If class B is derived directly or indirectly from class A
13007 : and class C is derived directly or indirectly from B,
13008 :
13009 : --conversion of C* to B* is better than conversion of C* to
13010 : A*,
13011 :
13012 : --conversion of B* to A* is better than conversion of C* to
13013 : A* */
13014 307222 : if (same_type_p (deref_from_type1, deref_from_type2))
13015 : {
13016 307216 : if (is_properly_derived_from (deref_to_type1,
13017 : deref_to_type2))
13018 : return 1;
13019 307216 : else if (is_properly_derived_from (deref_to_type2,
13020 : deref_to_type1))
13021 : return -1;
13022 : }
13023 6 : else if (same_type_p (deref_to_type1, deref_to_type2))
13024 : {
13025 6 : if (is_properly_derived_from (deref_from_type2,
13026 : deref_from_type1))
13027 : return 1;
13028 0 : else if (is_properly_derived_from (deref_from_type1,
13029 : deref_from_type2))
13030 : return -1;
13031 : }
13032 : }
13033 : }
13034 80305650 : else if (CLASS_TYPE_P (non_reference (from_type1))
13035 64873788 : && same_type_p (from_type1, from_type2))
13036 : {
13037 24345694 : tree from = non_reference (from_type1);
13038 :
13039 : /* [over.ics.rank]
13040 :
13041 : --binding of an expression of type C to a reference of type
13042 : B& is better than binding an expression of type C to a
13043 : reference of type A&
13044 :
13045 : --conversion of C to B is better than conversion of C to A, */
13046 24345694 : if (is_properly_derived_from (from, to_type1)
13047 24345694 : && is_properly_derived_from (from, to_type2))
13048 : {
13049 971358 : if (is_properly_derived_from (to_type1, to_type2))
13050 : return 1;
13051 967284 : else if (is_properly_derived_from (to_type2, to_type1))
13052 : return -1;
13053 : }
13054 : }
13055 31614262 : else if (CLASS_TYPE_P (non_reference (to_type1))
13056 16182400 : && same_type_p (to_type1, to_type2))
13057 : {
13058 7 : tree to = non_reference (to_type1);
13059 :
13060 : /* [over.ics.rank]
13061 :
13062 : --binding of an expression of type B to a reference of type
13063 : A& is better than binding an expression of type C to a
13064 : reference of type A&,
13065 :
13066 : --conversion of B to A is better than conversion of C to A */
13067 7 : if (is_properly_derived_from (from_type1, to)
13068 7 : && is_properly_derived_from (from_type2, to))
13069 : {
13070 3 : if (is_properly_derived_from (from_type2, from_type1))
13071 : return 1;
13072 3 : else if (is_properly_derived_from (from_type1, from_type2))
13073 : return -1;
13074 : }
13075 : }
13076 :
13077 : /* [over.ics.rank]
13078 :
13079 : --S1 and S2 differ only in their qualification conversion and yield
13080 : similar types T1 and T2 (_conv.qual_), respectively, and the cv-
13081 : qualification signature of type T1 is a proper subset of the cv-
13082 : qualification signature of type T2 */
13083 40370144 : if (ics1->kind == ck_qual
13084 385 : && ics2->kind == ck_qual
13085 40370529 : && same_type_p (from_type1, from_type2))
13086 : {
13087 385 : int result = comp_cv_qual_signature (to_type1, to_type2);
13088 385 : if (result != 0)
13089 : return result;
13090 : }
13091 :
13092 : /* [over.ics.rank]
13093 :
13094 : --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
13095 : to an implicit object parameter of a non-static member function
13096 : declared without a ref-qualifier, and either S1 binds an lvalue
13097 : reference to an lvalue and S2 binds an rvalue reference or S1 binds an
13098 : rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x
13099 : draft standard, 13.3.3.2)
13100 :
13101 : --S1 and S2 are reference bindings (_dcl.init.ref_), and the
13102 : types to which the references refer are the same type except for
13103 : top-level cv-qualifiers, and the type to which the reference
13104 : initialized by S2 refers is more cv-qualified than the type to
13105 : which the reference initialized by S1 refers.
13106 :
13107 : DR 1328 [over.match.best]: the context is an initialization by
13108 : conversion function for direct reference binding (13.3.1.6) of a
13109 : reference to function type, the return type of F1 is the same kind of
13110 : reference (i.e. lvalue or rvalue) as the reference being initialized,
13111 : and the return type of F2 is not. */
13112 :
13113 40370058 : if (ref_conv1 && ref_conv2)
13114 : {
13115 18574872 : if (!ref_conv1->this_p && !ref_conv2->this_p
13116 18303224 : && (ref_conv1->rvaluedness_matches_p
13117 18303224 : != ref_conv2->rvaluedness_matches_p)
13118 34960582 : && (same_type_p (ref_conv1->type, ref_conv2->type)
13119 9757894 : || (TYPE_REF_IS_RVALUE (ref_conv1->type)
13120 9757894 : != TYPE_REF_IS_RVALUE (ref_conv2->type))))
13121 : {
13122 9757622 : if (ref_conv1->bad_p
13123 9757622 : && !same_type_p (TREE_TYPE (ref_conv1->type),
13124 : TREE_TYPE (ref_conv2->type)))
13125 : /* Don't prefer a bad conversion that drops cv-quals to a bad
13126 : conversion with the wrong rvalueness. */
13127 : return 0;
13128 9756159 : return (ref_conv1->rvaluedness_matches_p
13129 9756159 : - ref_conv2->rvaluedness_matches_p);
13130 : }
13131 :
13132 15445063 : if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
13133 : {
13134 : /* Per P0388R4:
13135 :
13136 : void f (int(&)[]), // (1)
13137 : f (int(&)[1]), // (2)
13138 : f (int*); // (3)
13139 :
13140 : (2) is better than (1), but (3) should be equal to (1) and to
13141 : (2). For that reason we don't use ck_qual for (1) which would
13142 : give it the cr_exact rank while (3) remains ck_identity.
13143 : Therefore we compare (1) and (2) here. For (1) we'll have
13144 :
13145 : ck_ref_bind <- ck_identity
13146 : int[] & int[1]
13147 :
13148 : so to handle this we must look at ref_conv. */
13149 15444480 : bool c1 = conv_binds_to_array_of_unknown_bound (ref_conv1);
13150 15444480 : bool c2 = conv_binds_to_array_of_unknown_bound (ref_conv2);
13151 15444480 : if (c1 && !c2)
13152 : return -1;
13153 15444474 : else if (!c1 && c2)
13154 : return 1;
13155 :
13156 15444474 : int q1 = cp_type_quals (TREE_TYPE (ref_conv1->type));
13157 15444474 : int q2 = cp_type_quals (TREE_TYPE (ref_conv2->type));
13158 15444474 : if (ref_conv1->bad_p)
13159 : {
13160 : /* Prefer the one that drops fewer cv-quals. */
13161 1606 : tree ftype = next_conversion (ref_conv1)->type;
13162 1606 : int fquals = cp_type_quals (ftype);
13163 1606 : q1 ^= fquals;
13164 1606 : q2 ^= fquals;
13165 : }
13166 15444474 : return comp_cv_qualification (q2, q1);
13167 : }
13168 : }
13169 :
13170 : /* [over.ics.rank]
13171 :
13172 : Per CWG 1601:
13173 : -- A conversion that promotes an enumeration whose underlying type
13174 : is fixed to its underlying type is better than one that promotes to
13175 : the promoted underlying type, if the two are different. */
13176 15167956 : if (ics1->rank == cr_promotion
13177 146 : && ics2->rank == cr_promotion
13178 146 : && UNSCOPED_ENUM_P (from_type1)
13179 27 : && ENUM_FIXED_UNDERLYING_TYPE_P (from_type1)
13180 15167980 : && same_type_p (from_type1, from_type2))
13181 : {
13182 24 : tree utype = ENUM_UNDERLYING_TYPE (from_type1);
13183 24 : tree prom = type_promotes_to (from_type1);
13184 24 : if (!same_type_p (utype, prom))
13185 : {
13186 12 : if (same_type_p (to_type1, utype)
13187 12 : && same_type_p (to_type2, prom))
13188 : return 1;
13189 6 : else if (same_type_p (to_type2, utype)
13190 6 : && same_type_p (to_type1, prom))
13191 6 : return -1;
13192 : }
13193 : }
13194 :
13195 : /* Neither conversion sequence is better than the other. */
13196 : return 0;
13197 : }
13198 :
13199 : /* The source type for this standard conversion sequence. */
13200 :
13201 : static tree
13202 9 : source_type (conversion *t)
13203 : {
13204 9 : return strip_standard_conversion (t)->type;
13205 : }
13206 :
13207 : /* Note a warning about preferring WINNER to LOSER. We do this by storing
13208 : a pointer to LOSER and re-running joust to produce the warning if WINNER
13209 : is actually used. */
13210 :
13211 : static void
13212 528 : add_warning (struct z_candidate *winner, struct z_candidate *loser)
13213 : {
13214 528 : candidate_warning *cw = (candidate_warning *)
13215 0 : conversion_obstack_alloc (sizeof (candidate_warning));
13216 528 : cw->loser = loser;
13217 528 : cw->next = winner->warnings;
13218 528 : winner->warnings = cw;
13219 0 : }
13220 :
13221 : /* CAND is a constructor candidate in joust in C++17 and up. If it copies a
13222 : prvalue returned from a conversion function, return true. Otherwise, return
13223 : false. */
13224 :
13225 : static bool
13226 885484 : joust_maybe_elide_copy (z_candidate *cand)
13227 : {
13228 885484 : tree fn = cand->fn;
13229 2223132 : if (!DECL_COPY_CONSTRUCTOR_P (fn) && !DECL_MOVE_CONSTRUCTOR_P (fn))
13230 : return false;
13231 433515 : conversion *conv = cand->convs[0];
13232 433515 : if (conv->kind == ck_ambig)
13233 : return false;
13234 433513 : gcc_checking_assert (conv->kind == ck_ref_bind);
13235 433513 : conv = next_conversion (conv);
13236 433513 : if (conv->kind == ck_user && !TYPE_REF_P (conv->type))
13237 : {
13238 372 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
13239 : (conv->type, DECL_CONTEXT (fn)));
13240 372 : z_candidate *uc = conv->cand;
13241 372 : if (DECL_CONV_FN_P (uc->fn))
13242 233 : return true;
13243 : }
13244 : return false;
13245 : }
13246 :
13247 : /* Return the class that CAND's implicit object parameter refers to. */
13248 :
13249 : static tree
13250 295718 : class_of_implicit_object (z_candidate *cand)
13251 : {
13252 295718 : if (!DECL_IOBJ_MEMBER_FUNCTION_P (cand->fn))
13253 : return NULL_TREE;
13254 :
13255 : /* "For conversion functions that are implicit object member functions,
13256 : the function is considered to be a member of the class of the implied
13257 : object argument for the purpose of defining the type of the implicit
13258 : object parameter." */
13259 295718 : if (DECL_CONV_FN_P (cand->fn))
13260 0 : return TYPE_MAIN_VARIANT (TREE_TYPE (cand->first_arg));
13261 :
13262 : /* "For non-conversion functions that are implicit object member
13263 : functions nominated by a using-declaration in a derived class, the
13264 : function is considered to be a member of the derived class for the
13265 : purpose of defining the type of the implicit object parameter."
13266 :
13267 : That derived class is reflected in the conversion_path binfo. */
13268 295718 : return BINFO_TYPE (cand->conversion_path);
13269 : }
13270 :
13271 : /* Return whether the first parameter of C1 matches the second parameter
13272 : of C2. */
13273 :
13274 : static bool
13275 565946 : reversed_match (z_candidate *c1, z_candidate *c2)
13276 : {
13277 565946 : tree fn1 = c1->fn;
13278 565946 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (c2->fn));
13279 565946 : tree parm2 = TREE_VALUE (TREE_CHAIN (parms2));
13280 565946 : if (DECL_IOBJ_MEMBER_FUNCTION_P (fn1))
13281 : {
13282 295718 : tree ctx = class_of_implicit_object (c1);
13283 295718 : return iobj_parm_corresponds_to (fn1, parm2, ctx);
13284 : }
13285 : else
13286 : {
13287 270228 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn1));
13288 270228 : tree parm1 = TREE_VALUE (parms1);
13289 270228 : return same_type_p (parm1, parm2);
13290 : }
13291 : }
13292 :
13293 : /* True if the defining declarations of the two candidates have equivalent
13294 : parameters. MATCH_KIND controls whether we're trying to compare the
13295 : original declarations (for a warning) or the actual candidates. */
13296 :
13297 : enum class pmatch { original, current };
13298 :
13299 : static bool
13300 4982314 : cand_parms_match (z_candidate *c1, z_candidate *c2, pmatch match_kind)
13301 : {
13302 4982314 : tree fn1 = c1->fn;
13303 4982314 : tree fn2 = c2->fn;
13304 4982314 : bool reversed = (match_kind == pmatch::current
13305 4982314 : && c1->reversed () != c2->reversed ());
13306 4982314 : if (fn1 == fn2 && !reversed)
13307 : return true;
13308 4981989 : if (identifier_p (fn1) || identifier_p (fn2))
13309 : return false;
13310 4981984 : if (match_kind == pmatch::original)
13311 : {
13312 : /* We don't look at c1->template_decl because that's only set for
13313 : primary templates, not e.g. non-template member functions of
13314 : class templates. */
13315 22 : tree t1 = most_general_template (fn1);
13316 22 : tree t2 = most_general_template (fn2);
13317 22 : if (t1 || t2)
13318 : {
13319 15 : if (!t1 || !t2)
13320 : return false;
13321 15 : if (t1 == t2)
13322 : return true;
13323 3 : fn1 = DECL_TEMPLATE_RESULT (t1);
13324 3 : fn2 = DECL_TEMPLATE_RESULT (t2);
13325 : }
13326 : }
13327 :
13328 4981972 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn1));
13329 4981972 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (fn2));
13330 :
13331 9741738 : if (DECL_FUNCTION_MEMBER_P (fn1)
13332 4982596 : && DECL_FUNCTION_MEMBER_P (fn2))
13333 : {
13334 222816 : tree base1 = DECL_CONTEXT (strip_inheriting_ctors (fn1));
13335 222816 : tree base2 = DECL_CONTEXT (strip_inheriting_ctors (fn2));
13336 222816 : if (base1 != base2)
13337 148341 : return false;
13338 :
13339 222639 : if (reversed)
13340 148164 : return (reversed_match (c1, c2)
13341 148164 : && reversed_match (c2, c1));
13342 :
13343 : /* Use object_parms_correspond to simplify comparing iobj/xobj/static
13344 : member functions. */
13345 74475 : if (!object_parms_correspond (fn1, fn2, base1))
13346 : return false;
13347 :
13348 : /* We just compared the object parameters, if they don't correspond
13349 : we already returned false. */
13350 223425 : auto skip_parms = [] (tree fn, tree parms)
13351 : {
13352 148950 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
13353 1020 : return TREE_CHAIN (parms);
13354 : else
13355 147930 : return skip_artificial_parms_for (fn, parms);
13356 : };
13357 74475 : parms1 = skip_parms (fn1, parms1);
13358 74475 : parms2 = skip_parms (fn2, parms2);
13359 : }
13360 4759156 : else if (reversed)
13361 135079 : return (reversed_match (c1, c2)
13362 135079 : && reversed_match (c2, c1));
13363 4698552 : return compparms (parms1, parms2);
13364 : }
13365 :
13366 : /* True iff FN is a copy or move constructor or assignment operator. */
13367 :
13368 : static bool
13369 20749392 : sfk_copy_or_move (tree fn)
13370 : {
13371 20749392 : if (TREE_CODE (fn) != FUNCTION_DECL)
13372 : return false;
13373 20749302 : special_function_kind sfk = special_function_p (fn);
13374 20749302 : return sfk >= sfk_copy_constructor && sfk <= sfk_move_assignment;
13375 : }
13376 :
13377 : /* Compare two candidates for overloading as described in
13378 : [over.match.best]. Return values:
13379 :
13380 : 1: cand1 is better than cand2
13381 : -1: cand2 is better than cand1
13382 : 0: cand1 and cand2 are indistinguishable */
13383 :
13384 : static int
13385 78527436 : joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
13386 : tsubst_flags_t complain)
13387 : {
13388 78527436 : int winner = 0;
13389 78527436 : int off1 = 0, off2 = 0;
13390 78527436 : size_t i;
13391 78527436 : size_t len;
13392 :
13393 : /* Candidates that involve bad conversions are always worse than those
13394 : that don't. */
13395 78527436 : if (cand1->viable > cand2->viable)
13396 : return 1;
13397 62989401 : if (cand1->viable < cand2->viable)
13398 : return -1;
13399 :
13400 : /* If we have two pseudo-candidates for conversions to the same type,
13401 : or two candidates for the same function, arbitrarily pick one. */
13402 62989401 : if (cand1->fn == cand2->fn
13403 1908652 : && cand1->reversed () == cand2->reversed ()
13404 63826474 : && (IS_TYPE_OR_DECL_P (cand1->fn)))
13405 : return 1;
13406 :
13407 : /* Prefer a non-deleted function over an implicitly deleted move
13408 : constructor or assignment operator. This differs slightly from the
13409 : wording for issue 1402 (which says the move op is ignored by overload
13410 : resolution), but this way produces better error messages. */
13411 62989401 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13412 58613526 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13413 121593119 : && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn))
13414 : {
13415 2535307 : if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn)
13416 2115593 : && move_fn_p (cand1->fn))
13417 : return -1;
13418 3798672 : if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn)
13419 3062463 : && move_fn_p (cand2->fn))
13420 : return 1;
13421 : }
13422 :
13423 : /* a viable function F1
13424 : is defined to be a better function than another viable function F2 if
13425 : for all arguments i, ICSi(F1) is not a worse conversion sequence than
13426 : ICSi(F2), and then */
13427 :
13428 : /* for some argument j, ICSj(F1) is a better conversion sequence than
13429 : ICSj(F2) */
13430 :
13431 : /* For comparing static and non-static member functions, we ignore
13432 : the implicit object parameter of the non-static function. The
13433 : standard says to pretend that the static function has an object
13434 : parm, but that won't work with operator overloading. */
13435 62974643 : len = cand1->num_convs;
13436 62974643 : if (len != cand2->num_convs)
13437 : {
13438 778 : int static_1 = (TREE_CODE (cand1->fn) == FUNCTION_DECL
13439 778 : && DECL_STATIC_FUNCTION_P (cand1->fn));
13440 778 : int static_2 = (TREE_CODE (cand2->fn) == FUNCTION_DECL
13441 778 : && DECL_STATIC_FUNCTION_P (cand2->fn));
13442 :
13443 778 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13444 193 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13445 193 : && DECL_CONSTRUCTOR_P (cand1->fn)
13446 784 : && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
13447 : /* We're comparing a near-match list constructor and a near-match
13448 : non-list constructor. Just treat them as unordered. */
13449 : return 0;
13450 :
13451 772 : gcc_assert (static_1 != static_2);
13452 :
13453 772 : if (static_1)
13454 : {
13455 : /* C++23 [over.best.ics.general] says:
13456 : When the parameter is the implicit object parameter of a static
13457 : member function, the implicit conversion sequence is a standard
13458 : conversion sequence that is neither better nor worse than any
13459 : other standard conversion sequence. */
13460 72 : if (CONVERSION_RANK (cand2->convs[0]) >= cr_user)
13461 0 : winner = 1;
13462 : off2 = 1;
13463 : }
13464 : else
13465 : {
13466 700 : if (CONVERSION_RANK (cand1->convs[0]) >= cr_user)
13467 585 : winner = -1;
13468 700 : off1 = 1;
13469 700 : --len;
13470 : }
13471 : }
13472 :
13473 146745927 : for (i = 0; i < len; ++i)
13474 : {
13475 83772873 : conversion *t1 = cand1->convs[i + off1];
13476 83772873 : conversion *t2 = cand2->convs[i + off2];
13477 83772873 : int comp = compare_ics (t1, t2);
13478 :
13479 83772873 : if (comp != 0)
13480 : {
13481 56675695 : if ((complain & tf_warning)
13482 46782856 : && warn_sign_promo
13483 82 : && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
13484 : == cr_std + cr_promotion)
13485 50 : && t1->kind == ck_std
13486 31 : && t2->kind == ck_std
13487 31 : && TREE_CODE (t1->type) == INTEGER_TYPE
13488 31 : && TREE_CODE (t2->type) == INTEGER_TYPE
13489 31 : && (TYPE_PRECISION (t1->type)
13490 31 : == TYPE_PRECISION (t2->type))
13491 56675726 : && (TYPE_UNSIGNED (next_conversion (t1)->type)
13492 0 : || (TREE_CODE (next_conversion (t1)->type)
13493 : == ENUMERAL_TYPE)))
13494 : {
13495 31 : tree type = next_conversion (t1)->type;
13496 31 : tree type1, type2;
13497 31 : struct z_candidate *w, *l;
13498 31 : if (comp > 0)
13499 : type1 = t1->type, type2 = t2->type,
13500 : w = cand1, l = cand2;
13501 : else
13502 8 : type1 = t2->type, type2 = t1->type,
13503 8 : w = cand2, l = cand1;
13504 :
13505 31 : if (warn)
13506 : {
13507 9 : warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
13508 : type, type1, type2);
13509 9 : warning (OPT_Wsign_promo, " in call to %qD", w->fn);
13510 : }
13511 : else
13512 22 : add_warning (w, l);
13513 : }
13514 :
13515 56675695 : if (winner && comp != winner)
13516 : {
13517 : /* Ambiguity between normal and reversed comparison operators
13518 : with the same parameter types. P2468 decided not to go with
13519 : this approach to resolving the ambiguity, so pedwarn. */
13520 1583 : if ((complain & tf_warning_or_error)
13521 1009 : && (cand1->reversed () != cand2->reversed ())
13522 1935 : && cand_parms_match (cand1, cand2, pmatch::original))
13523 : {
13524 337 : struct z_candidate *w, *l;
13525 337 : if (cand2->reversed ())
13526 : winner = 1, w = cand1, l = cand2;
13527 : else
13528 315 : winner = -1, w = cand2, l = cand1;
13529 337 : if (warn)
13530 : {
13531 22 : auto_diagnostic_group d;
13532 22 : if (pedwarn (input_location, 0,
13533 : "C++20 says that these are ambiguous, "
13534 : "even though the second is reversed:"))
13535 : {
13536 22 : print_z_candidate (input_location,
13537 : N_("candidate 1:"), w);
13538 22 : print_z_candidate (input_location,
13539 : N_("candidate 2:"), l);
13540 22 : if (w->fn == l->fn
13541 17 : && DECL_IOBJ_MEMBER_FUNCTION_P (w->fn)
13542 37 : && (type_memfn_quals (TREE_TYPE (w->fn))
13543 15 : & TYPE_QUAL_CONST) == 0)
13544 : {
13545 : /* Suggest adding const to
13546 : struct A { bool operator==(const A&); }; */
13547 12 : tree parmtype
13548 12 : = FUNCTION_FIRST_USER_PARMTYPE (w->fn);
13549 12 : parmtype = TREE_VALUE (parmtype);
13550 12 : if (TYPE_REF_P (parmtype)
13551 12 : && TYPE_READONLY (TREE_TYPE (parmtype))
13552 24 : && (same_type_ignoring_top_level_qualifiers_p
13553 12 : (TREE_TYPE (parmtype),
13554 12 : DECL_CONTEXT (w->fn))))
13555 12 : inform (DECL_SOURCE_LOCATION (w->fn),
13556 : "try making the operator a %<const%> "
13557 : "member function");
13558 : }
13559 : }
13560 22 : }
13561 : else
13562 315 : add_warning (w, l);
13563 : return winner;
13564 : }
13565 :
13566 1246 : winner = 0;
13567 1246 : goto tweak;
13568 : }
13569 : winner = comp;
13570 : }
13571 : }
13572 :
13573 : /* warn about confusing overload resolution for user-defined conversions,
13574 : either between a constructor and a conversion op, or between two
13575 : conversion ops. */
13576 62973054 : if ((complain & tf_warning)
13577 : /* In C++17, the constructor might have been elided, which means that
13578 : an originally null ->second_conv could become non-null. */
13579 51363803 : && winner && warn_conversion && cand1->second_conv && cand2->second_conv
13580 54 : && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
13581 62973081 : && winner != compare_ics (cand1->second_conv, cand2->second_conv))
13582 : {
13583 27 : struct z_candidate *w, *l;
13584 27 : bool give_warning = false;
13585 :
13586 27 : if (winner == 1)
13587 : w = cand1, l = cand2;
13588 : else
13589 6 : w = cand2, l = cand1;
13590 :
13591 : /* We don't want to complain about `X::operator T1 ()'
13592 : beating `X::operator T2 () const', when T2 is a no less
13593 : cv-qualified version of T1. */
13594 27 : if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
13595 39 : && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
13596 : {
13597 6 : tree t = TREE_TYPE (TREE_TYPE (l->fn));
13598 6 : tree f = TREE_TYPE (TREE_TYPE (w->fn));
13599 :
13600 6 : if (TREE_CODE (t) == TREE_CODE (f) && INDIRECT_TYPE_P (t))
13601 : {
13602 6 : t = TREE_TYPE (t);
13603 6 : f = TREE_TYPE (f);
13604 : }
13605 6 : if (!comp_ptr_ttypes (t, f))
13606 : give_warning = true;
13607 : }
13608 : else
13609 : give_warning = true;
13610 :
13611 : if (!give_warning)
13612 : /*NOP*/;
13613 21 : else if (warn)
13614 : {
13615 9 : tree source = source_type (w->convs[0]);
13616 9 : if (INDIRECT_TYPE_P (source))
13617 6 : source = TREE_TYPE (source);
13618 9 : auto_diagnostic_group d;
13619 9 : if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
13620 18 : && warning (OPT_Wconversion, " for conversion from %qH to %qI",
13621 9 : source, w->second_conv->type))
13622 : {
13623 9 : inform (input_location, " because conversion sequence "
13624 : "for the argument is better");
13625 : }
13626 9 : }
13627 : else
13628 12 : add_warning (w, l);
13629 : }
13630 :
13631 62973054 : if (winner)
13632 : return winner;
13633 :
13634 : /* DR 495 moved this tiebreaker above the template ones. */
13635 : /* or, if not that,
13636 : the context is an initialization by user-defined conversion (see
13637 : _dcl.init_ and _over.match.user_) and the standard conversion
13638 : sequence from the return type of F1 to the destination type (i.e.,
13639 : the type of the entity being initialized) is a better conversion
13640 : sequence than the standard conversion sequence from the return type
13641 : of F2 to the destination type. */
13642 :
13643 10374848 : if (cand1->second_conv)
13644 : {
13645 51890 : winner = compare_ics (cand1->second_conv, cand2->second_conv);
13646 51890 : if (winner)
13647 : return winner;
13648 : }
13649 :
13650 : /* CWG2735 (PR109247): A copy/move ctor/op= for which its operand uses an
13651 : explicit conversion (due to list-initialization) is worse. */
13652 10374696 : {
13653 10374696 : z_candidate *sp = nullptr;
13654 10374696 : if (sfk_copy_or_move (cand1->fn))
13655 680 : sp = cand1;
13656 10374696 : if (sfk_copy_or_move (cand2->fn))
13657 894027 : sp = sp ? nullptr : cand2;
13658 10374643 : if (sp)
13659 : {
13660 1789202 : conversion *conv = sp->convs[!DECL_CONSTRUCTOR_P (sp->fn)];
13661 894601 : if (conv->user_conv_p)
13662 2378 : for (; conv; conv = next_conversion (conv))
13663 1998 : if (conv->kind == ck_user
13664 809 : && DECL_P (conv->cand->fn)
13665 2807 : && DECL_NONCONVERTING_P (conv->cand->fn))
13666 429 : return (sp == cand1) ? -1 : 1;
13667 : }
13668 : }
13669 :
13670 : /* DR2327: C++17 copy elision in [over.match.ctor] (direct-init) context.
13671 : The standard currently says that only constructors are candidates, but if
13672 : one copies a prvalue returned by a conversion function we prefer that.
13673 :
13674 : Clang does something similar, as discussed at
13675 : http://lists.isocpp.org/core/2017/10/3166.php
13676 : http://lists.isocpp.org/core/2019/03/5721.php */
13677 6756735 : if (len == 1 && cxx_dialect >= cxx17
13678 6742132 : && DECL_P (cand1->fn)
13679 6742132 : && DECL_COMPLETE_CONSTRUCTOR_P (cand1->fn)
13680 11191642 : && !(cand1->flags & LOOKUP_ONLYCONVERTING))
13681 : {
13682 442742 : bool elided1 = joust_maybe_elide_copy (cand1);
13683 442742 : bool elided2 = joust_maybe_elide_copy (cand2);
13684 442742 : winner = elided1 - elided2;
13685 442742 : if (winner)
13686 : return winner;
13687 : }
13688 :
13689 : /* or, if not that,
13690 : F1 is a non-template function and F2 is a template function
13691 : specialization. */
13692 :
13693 10374034 : if (!cand1->template_decl && cand2->template_decl)
13694 : return 1;
13695 10311879 : else if (cand1->template_decl && !cand2->template_decl)
13696 : return -1;
13697 :
13698 : /* or, if not that,
13699 : F1 and F2 are template functions and the function template for F1 is
13700 : more specialized than the template for F2 according to the partial
13701 : ordering rules. */
13702 :
13703 9151114 : if (cand1->template_decl && cand2->template_decl)
13704 : {
13705 4156959 : winner = more_specialized_fn
13706 4156959 : (TI_TEMPLATE (cand1->template_decl),
13707 4156959 : TI_TEMPLATE (cand2->template_decl),
13708 : /* [temp.func.order]: The presence of unused ellipsis and default
13709 : arguments has no effect on the partial ordering of function
13710 : templates. add_function_candidate() will not have
13711 : counted the "this" argument for constructors. */
13712 8313918 : cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
13713 4156959 : if (winner)
13714 : return winner;
13715 : }
13716 :
13717 : /* F1 and F2 are non-template functions and
13718 : - they have the same non-object-parameter-type-lists ([dcl.fct]), and
13719 : - if they are member functions, both are direct members of the same
13720 : class, and
13721 : - if both are non-static member functions, they have the same types for
13722 : their object parameters, and
13723 : - F1 is more constrained than F2 according to the partial ordering of
13724 : constraints described in [temp.constr.order]. */
13725 6110701 : if (flag_concepts && DECL_P (cand1->fn) && DECL_P (cand2->fn)
13726 6110653 : && !cand1->template_decl && !cand2->template_decl
13727 11105217 : && cand_parms_match (cand1, cand2, pmatch::current))
13728 : {
13729 407674 : winner = more_constrained (cand1->fn, cand2->fn);
13730 407674 : if (winner)
13731 : return winner;
13732 : }
13733 :
13734 : /* F2 is a rewritten candidate (12.4.1.2) and F1 is not, or F1 and F2 are
13735 : rewritten candidates, and F2 is a synthesized candidate with reversed
13736 : order of parameters and F1 is not. */
13737 6115112 : if (cand1->rewritten ())
13738 : {
13739 1271971 : if (!cand2->rewritten ())
13740 : return -1;
13741 815425 : if (!cand1->reversed () && cand2->reversed ())
13742 : return 1;
13743 815425 : if (cand1->reversed () && !cand2->reversed ())
13744 : return -1;
13745 : }
13746 4843141 : else if (cand2->rewritten ())
13747 : return 1;
13748 :
13749 4774422 : if (deduction_guide_p (cand1->fn))
13750 : {
13751 4049 : gcc_assert (deduction_guide_p (cand2->fn));
13752 :
13753 : /* F1 and F2 are generated from class template argument deduction for a
13754 : class D, and F2 is generated from inheriting constructors from a base
13755 : class of D while F1 is not, and for each explicit function argument,
13756 : the corresponding parameters of F1 and F2 are either both ellipses or
13757 : have the same type. */
13758 4049 : bool inherited1 = inherited_guide_p (cand1->fn);
13759 4049 : bool inherited2 = inherited_guide_p (cand2->fn);
13760 4049 : if (int diff = inherited2 - inherited1)
13761 : {
13762 68 : for (i = 0; i < len; ++i)
13763 : {
13764 19 : conversion *t1 = cand1->convs[i + off1];
13765 19 : conversion *t2 = cand2->convs[i + off2];
13766 : /* ??? It seems the ellipses part of this tiebreaker isn't
13767 : needed since a mismatch should have broken the tie earlier
13768 : during ICS comparison. */
13769 19 : gcc_checking_assert (t1->ellipsis_p == t2->ellipsis_p);
13770 19 : if (!same_type_p (t1->type, t2->type))
13771 : break;
13772 : }
13773 51 : if (i == len)
13774 : return diff;
13775 : }
13776 :
13777 : /* F1 is generated from a deduction-guide (13.3.1.8) and F2 is not */
13778 : /* We distinguish between candidates from an explicit deduction guide and
13779 : candidates built from a constructor based on DECL_ARTIFICIAL. */
13780 4000 : int art1 = DECL_ARTIFICIAL (cand1->fn);
13781 4000 : int art2 = DECL_ARTIFICIAL (cand2->fn);
13782 4000 : if (art1 != art2)
13783 1415 : return art2 - art1;
13784 :
13785 2585 : if (art1)
13786 : {
13787 : /* Prefer the special copy guide over a declared copy/move
13788 : constructor. */
13789 2582 : if (copy_guide_p (cand1->fn))
13790 : return 1;
13791 86 : if (copy_guide_p (cand2->fn))
13792 : return -1;
13793 :
13794 : /* Prefer a candidate generated from a non-template constructor. */
13795 28 : int tg1 = template_guide_p (cand1->fn);
13796 28 : int tg2 = template_guide_p (cand2->fn);
13797 28 : if (tg1 != tg2)
13798 6 : return tg2 - tg1;
13799 : }
13800 : }
13801 :
13802 : /* F1 is a constructor for a class D, F2 is a constructor for a base class B
13803 : of D, and for all arguments the corresponding parameters of F1 and F2 have
13804 : the same type (CWG 2273/2277). */
13805 14311039 : if (DECL_INHERITED_CTOR (cand1->fn) || DECL_INHERITED_CTOR (cand2->fn))
13806 : {
13807 87 : tree base1 = DECL_CONTEXT (strip_inheriting_ctors (cand1->fn));
13808 87 : tree base2 = DECL_CONTEXT (strip_inheriting_ctors (cand2->fn));
13809 :
13810 87 : bool used1 = false;
13811 87 : bool used2 = false;
13812 87 : if (base1 == base2)
13813 : /* No difference. */;
13814 87 : else if (DERIVED_FROM_P (base1, base2))
13815 : used1 = true;
13816 18 : else if (DERIVED_FROM_P (base2, base1))
13817 : used2 = true;
13818 :
13819 78 : if (int diff = used2 - used1)
13820 : {
13821 105 : for (i = 0; i < len; ++i)
13822 : {
13823 36 : conversion *t1 = cand1->convs[i + off1];
13824 36 : conversion *t2 = cand2->convs[i + off2];
13825 36 : if (!same_type_p (t1->type, t2->type))
13826 : break;
13827 : }
13828 78 : if (i == len)
13829 : return diff;
13830 : }
13831 : }
13832 :
13833 : /* Check whether we can discard a builtin candidate, either because we
13834 : have two identical ones or matching builtin and non-builtin candidates.
13835 :
13836 : (Pedantically in the latter case the builtin which matched the user
13837 : function should not be added to the overload set, but we spot it here.
13838 :
13839 : [over.match.oper]
13840 : ... the builtin candidates include ...
13841 : - do not have the same parameter type list as any non-template
13842 : non-member candidate. */
13843 :
13844 4770329 : if (identifier_p (cand1->fn) || identifier_p (cand2->fn))
13845 : {
13846 93 : for (i = 0; i < len; ++i)
13847 69 : if (!same_type_p (cand1->convs[i]->type,
13848 : cand2->convs[i]->type))
13849 : break;
13850 43 : if (i == cand1->num_convs)
13851 : {
13852 24 : if (cand1->fn == cand2->fn)
13853 : /* Two built-in candidates; arbitrarily pick one. */
13854 : return 1;
13855 21 : else if (identifier_p (cand1->fn))
13856 : /* cand1 is built-in; prefer cand2. */
13857 21 : return -1;
13858 : else
13859 : /* cand2 is built-in; prefer cand1. */
13860 : return 1;
13861 : }
13862 : }
13863 :
13864 : /* For candidates of a multi-versioned function, make the version with
13865 : the highest priority win. This version will be checked for dispatching
13866 : first. If this version can be inlined into the caller, the front-end
13867 : will simply make a direct call to this function. */
13868 :
13869 4770305 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13870 4770283 : && DECL_FUNCTION_VERSIONED (cand1->fn)
13871 941 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13872 4771246 : && DECL_FUNCTION_VERSIONED (cand2->fn))
13873 : {
13874 941 : tree f1 = TREE_TYPE (cand1->fn);
13875 941 : tree f2 = TREE_TYPE (cand2->fn);
13876 941 : tree p1 = TYPE_ARG_TYPES (f1);
13877 941 : tree p2 = TYPE_ARG_TYPES (f2);
13878 :
13879 : /* Check if cand1->fn and cand2->fn are versions of the same function. It
13880 : is possible that cand1->fn and cand2->fn are function versions but of
13881 : different functions. Check types to see if they are versions of the same
13882 : function. */
13883 941 : if (compparms (p1, p2)
13884 941 : && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
13885 : {
13886 : /* Always make the version with the higher priority, more
13887 : specialized, win. */
13888 941 : gcc_assert (targetm.compare_version_priority);
13889 941 : if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
13890 : return 1;
13891 : else
13892 254 : return -1;
13893 : }
13894 : }
13895 :
13896 : /* If the two function declarations represent the same function (this can
13897 : happen with declarations in multiple scopes and arg-dependent lookup),
13898 : arbitrarily choose one. But first make sure the default args we're
13899 : using match. */
13900 4769342 : if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
13901 9538706 : && equal_functions (cand1->fn, cand2->fn))
13902 : {
13903 35 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
13904 35 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
13905 :
13906 70 : gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
13907 :
13908 50 : for (i = 0; i < len; ++i)
13909 : {
13910 : /* Don't crash if the fn is variadic. */
13911 15 : if (!parms1)
13912 : break;
13913 15 : parms1 = TREE_CHAIN (parms1);
13914 15 : parms2 = TREE_CHAIN (parms2);
13915 : }
13916 :
13917 35 : if (off1)
13918 0 : parms1 = TREE_CHAIN (parms1);
13919 35 : else if (off2)
13920 0 : parms2 = TREE_CHAIN (parms2);
13921 :
13922 69 : for (; parms1; ++i)
13923 : {
13924 80 : if (!cp_tree_equal (TREE_PURPOSE (parms1),
13925 40 : TREE_PURPOSE (parms2)))
13926 : {
13927 6 : if (warn)
13928 : {
13929 3 : if (complain & tf_error)
13930 : {
13931 3 : auto_diagnostic_group d;
13932 3 : if (permerror (input_location,
13933 : "default argument mismatch in "
13934 : "overload resolution"))
13935 : {
13936 3 : inform (DECL_SOURCE_LOCATION (cand1->fn),
13937 : " candidate 1: %q#F", cand1->fn);
13938 3 : inform (DECL_SOURCE_LOCATION (cand2->fn),
13939 : " candidate 2: %q#F", cand2->fn);
13940 : }
13941 3 : }
13942 : else
13943 : return 0;
13944 : }
13945 : else
13946 3 : add_warning (cand1, cand2);
13947 : break;
13948 : }
13949 34 : parms1 = TREE_CHAIN (parms1);
13950 34 : parms2 = TREE_CHAIN (parms2);
13951 : }
13952 :
13953 : return 1;
13954 : }
13955 :
13956 4770575 : tweak:
13957 :
13958 : /* Extension: If the worst conversion for one candidate is better than the
13959 : worst conversion for the other, take the first. */
13960 4770575 : if (!pedantic && (complain & tf_warning_or_error))
13961 : {
13962 : conversion_rank rank1 = cr_identity, rank2 = cr_identity;
13963 10282676 : struct z_candidate *w = 0, *l = 0;
13964 :
13965 10282676 : for (i = 0; i < len; ++i)
13966 : {
13967 5762413 : if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
13968 4426640 : rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
13969 5762413 : if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
13970 4426657 : rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
13971 : }
13972 4520263 : if (rank1 < rank2)
13973 74 : winner = 1, w = cand1, l = cand2;
13974 4520263 : if (rank1 > rank2)
13975 : winner = -1, w = cand2, l = cand1;
13976 4520155 : if (winner)
13977 : {
13978 : /* Don't choose a deleted function over ambiguity. */
13979 182 : if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
13980 : return 0;
13981 182 : if (warn)
13982 : {
13983 6 : auto_diagnostic_group d;
13984 6 : if (pedwarn (input_location, 0,
13985 : "ISO C++ says that these are ambiguous, even "
13986 : "though the worst conversion for the first is "
13987 : "better than the worst conversion for the second:"))
13988 : {
13989 3 : print_z_candidate (input_location, N_("candidate 1:"), w);
13990 3 : print_z_candidate (input_location, N_("candidate 2:"), l);
13991 : }
13992 6 : }
13993 : else
13994 176 : add_warning (w, l);
13995 : return winner;
13996 : }
13997 : }
13998 :
13999 : gcc_assert (!winner);
14000 : return 0;
14001 : }
14002 :
14003 : /* Given a list of candidates for overloading, find the best one, if any.
14004 : This algorithm has a worst case of O(2n) (winner is last), and a best
14005 : case of O(n/2) (totally ambiguous); much better than a sorting
14006 : algorithm. The candidates list is assumed to be sorted according
14007 : to viability (via splice_viable). */
14008 :
14009 : static struct z_candidate *
14010 245145648 : tourney (struct z_candidate *candidates, tsubst_flags_t complain)
14011 : {
14012 245145648 : struct z_candidate **champ = &candidates, **challenger;
14013 245145648 : int fate;
14014 245145648 : struct z_candidate *previous_worse_champ = nullptr;
14015 :
14016 : /* Walk through the list once, comparing each current champ to the next
14017 : candidate, knocking out a candidate or two with each comparison. */
14018 :
14019 312641944 : for (challenger = &candidates->next; *challenger && (*challenger)->viable; )
14020 : {
14021 67509110 : fate = joust (*champ, *challenger, 0, complain);
14022 67509110 : if (fate == 1)
14023 43746447 : challenger = &(*challenger)->next;
14024 23762663 : else if (fate == -1)
14025 : {
14026 18993976 : previous_worse_champ = *champ;
14027 18993976 : champ = challenger;
14028 18993976 : challenger = &(*challenger)->next;
14029 : }
14030 : else
14031 : {
14032 4768687 : previous_worse_champ = nullptr;
14033 4768687 : champ = &(*challenger)->next;
14034 4768687 : if (!*champ || !(*champ)->viable
14035 4755967 : || (*champ)->viable < (*challenger)->viable)
14036 : {
14037 : champ = nullptr;
14038 : break;
14039 : }
14040 4755873 : challenger = &(*champ)->next;
14041 : }
14042 : }
14043 :
14044 : /* Make sure the champ is better than all the candidates it hasn't yet
14045 : been compared to. */
14046 :
14047 245145648 : if (champ)
14048 28788143 : for (challenger = &candidates;
14049 273920977 : challenger != champ;
14050 28788143 : challenger = &(*challenger)->next)
14051 : {
14052 28790049 : if (*challenger == previous_worse_champ)
14053 : /* We already know this candidate is worse than the champ. */
14054 17771772 : continue;
14055 11018277 : fate = joust (*champ, *challenger, 0, complain);
14056 11018277 : if (fate != 1)
14057 : {
14058 : champ = nullptr;
14059 : break;
14060 : }
14061 : }
14062 :
14063 245132834 : if (!champ)
14064 : return nullptr;
14065 :
14066 : /* Move the champ to the front of the candidate list. */
14067 :
14068 245130928 : if (champ != &candidates)
14069 : {
14070 19465120 : z_candidate *saved_champ = *champ;
14071 19465120 : *champ = saved_champ->next;
14072 19465120 : saved_champ->next = candidates;
14073 19465120 : candidates = saved_champ;
14074 : }
14075 :
14076 245130928 : return candidates;
14077 : }
14078 :
14079 : /* Returns nonzero if things of type FROM can be converted to TO. */
14080 :
14081 : bool
14082 4521054 : can_convert (tree to, tree from, tsubst_flags_t complain)
14083 : {
14084 4521054 : tree arg = NULL_TREE;
14085 : /* implicit_conversion only considers user-defined conversions
14086 : if it has an expression for the call argument list. */
14087 4521054 : if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to))
14088 111 : arg = build_stub_object (from);
14089 4521054 : return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain);
14090 : }
14091 :
14092 : /* Returns nonzero if things of type FROM can be converted to TO with a
14093 : standard conversion. */
14094 :
14095 : bool
14096 263 : can_convert_standard (tree to, tree from, tsubst_flags_t complain)
14097 : {
14098 263 : return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
14099 : }
14100 :
14101 : /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
14102 :
14103 : bool
14104 5522370 : can_convert_arg (tree to, tree from, tree arg, int flags,
14105 : tsubst_flags_t complain)
14106 : {
14107 5522370 : conversion *t;
14108 5522370 : bool ok_p;
14109 :
14110 5522370 : conversion_obstack_sentinel cos;
14111 : /* We want to discard any access checks done for this test,
14112 : as we might not be in the appropriate access context and
14113 : we'll do the check again when we actually perform the
14114 : conversion. */
14115 5522370 : push_deferring_access_checks (dk_deferred);
14116 :
14117 : /* Handle callers like check_local_shadow forgetting to
14118 : convert_from_reference. */
14119 5522370 : if (TYPE_REF_P (from) && arg)
14120 : {
14121 61 : arg = convert_from_reference (arg);
14122 61 : from = TREE_TYPE (arg);
14123 : }
14124 :
14125 5522370 : t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
14126 : flags, complain);
14127 5522370 : ok_p = (t && !t->bad_p);
14128 :
14129 : /* Discard the access checks now. */
14130 5522370 : pop_deferring_access_checks ();
14131 :
14132 11044740 : return ok_p;
14133 5522370 : }
14134 :
14135 : /* Like can_convert_arg, but allows dubious conversions as well. */
14136 :
14137 : bool
14138 185409017 : can_convert_arg_bad (tree to, tree from, tree arg, int flags,
14139 : tsubst_flags_t complain)
14140 : {
14141 185409017 : conversion *t;
14142 :
14143 185409017 : conversion_obstack_sentinel cos;
14144 : /* Try to perform the conversion. */
14145 185409017 : t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
14146 : flags, complain);
14147 :
14148 370818034 : return t != NULL;
14149 185409017 : }
14150 :
14151 : /* Return an IMPLICIT_CONV_EXPR from EXPR to TYPE with bits set from overload
14152 : resolution FLAGS. */
14153 :
14154 : tree
14155 14856504 : build_implicit_conv_flags (tree type, tree expr, int flags)
14156 : {
14157 : /* In a template, we are only concerned about determining the
14158 : type of non-dependent expressions, so we do not have to
14159 : perform the actual conversion. But for initializers, we
14160 : need to be able to perform it at instantiation
14161 : (or instantiate_non_dependent_expr) time. */
14162 14856504 : expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
14163 14856504 : if (!(flags & LOOKUP_ONLYCONVERTING))
14164 6750779 : IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
14165 14856504 : if (flags & LOOKUP_NO_NARROWING)
14166 23514 : IMPLICIT_CONV_EXPR_BRACED_INIT (expr) = true;
14167 14856504 : return expr;
14168 : }
14169 :
14170 : /* Convert EXPR to TYPE. Return the converted expression.
14171 :
14172 : Note that we allow bad conversions here because by the time we get to
14173 : this point we are committed to doing the conversion. If we end up
14174 : doing a bad conversion, convert_like will complain. */
14175 :
14176 : tree
14177 301073727 : perform_implicit_conversion_flags (tree type, tree expr,
14178 : tsubst_flags_t complain, int flags)
14179 : {
14180 301073727 : conversion *conv;
14181 301073727 : location_t loc = cp_expr_loc_or_input_loc (expr);
14182 :
14183 301073727 : if (error_operand_p (expr))
14184 453 : return error_mark_node;
14185 :
14186 301073274 : conversion_obstack_sentinel cos;
14187 :
14188 301073274 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
14189 : /*c_cast_p=*/false,
14190 : flags, complain);
14191 :
14192 301073274 : if (!conv)
14193 : {
14194 263067 : if (complain & tf_error)
14195 488 : implicit_conversion_error (loc, type, expr, flags);
14196 263067 : expr = error_mark_node;
14197 : }
14198 300810207 : else if (processing_template_decl && conv->kind != ck_identity)
14199 14856504 : expr = build_implicit_conv_flags (type, expr, flags);
14200 : else
14201 : {
14202 : /* Give a conversion call the same location as expr. */
14203 285953703 : iloc_sentinel il (loc);
14204 285953703 : expr = convert_like (conv, expr, complain);
14205 285953703 : }
14206 :
14207 301073274 : return expr;
14208 301073274 : }
14209 :
14210 : tree
14211 13165043 : perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
14212 : {
14213 13165043 : return perform_implicit_conversion_flags (type, expr, complain,
14214 13165043 : LOOKUP_IMPLICIT);
14215 : }
14216 :
14217 : /* Convert EXPR to TYPE (as a direct-initialization) if that is
14218 : permitted. If the conversion is valid, the converted expression is
14219 : returned. Otherwise, NULL_TREE is returned, except in the case
14220 : that TYPE is a class type; in that case, an error is issued. If
14221 : C_CAST_P is true, then this direct-initialization is taking
14222 : place as part of a static_cast being attempted as part of a C-style
14223 : cast. */
14224 :
14225 : tree
14226 49248050 : perform_direct_initialization_if_possible (tree type,
14227 : tree expr,
14228 : bool c_cast_p,
14229 : tsubst_flags_t complain)
14230 : {
14231 49248050 : conversion *conv;
14232 :
14233 49248050 : if (type == error_mark_node || error_operand_p (expr))
14234 : return error_mark_node;
14235 : /* [dcl.init]
14236 :
14237 : If the destination type is a (possibly cv-qualified) class type:
14238 :
14239 : -- If the initialization is direct-initialization ...,
14240 : constructors are considered.
14241 :
14242 : -- If overload resolution is successful, the selected constructor
14243 : is called to initialize the object, with the initializer expression
14244 : or expression-list as its argument(s).
14245 :
14246 : -- Otherwise, if no constructor is viable, the destination type is
14247 : a (possibly cv-qualified) aggregate class A, and the initializer is
14248 : a parenthesized expression-list, the object is initialized as
14249 : follows... */
14250 49248050 : if (CLASS_TYPE_P (type))
14251 : {
14252 3254434 : releasing_vec args (make_tree_vector_single (expr));
14253 3254434 : expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
14254 : &args, type, LOOKUP_NORMAL, complain);
14255 3254434 : return build_cplus_new (type, expr, complain);
14256 3254434 : }
14257 :
14258 45993616 : conversion_obstack_sentinel cos;
14259 :
14260 45993616 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
14261 : c_cast_p,
14262 : LOOKUP_NORMAL, complain);
14263 45993616 : if (!conv || conv->bad_p)
14264 : expr = NULL_TREE;
14265 41188008 : else if (processing_template_decl && conv->kind != ck_identity)
14266 : {
14267 : /* In a template, we are only concerned about determining the
14268 : type of non-dependent expressions, so we do not have to
14269 : perform the actual conversion. But for initializers, we
14270 : need to be able to perform it at instantiation
14271 : (or instantiate_non_dependent_expr) time. */
14272 708004 : expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
14273 708004 : IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
14274 : }
14275 : else
14276 40480004 : expr = convert_like (conv, expr, NULL_TREE, 0,
14277 : /*issue_conversion_warnings=*/false,
14278 : c_cast_p, /*nested_p=*/false, complain);
14279 :
14280 45993616 : return expr;
14281 45993616 : }
14282 :
14283 : /* When initializing a reference that lasts longer than a full-expression,
14284 : this special rule applies:
14285 :
14286 : [class.temporary]
14287 :
14288 : The temporary to which the reference is bound or the temporary
14289 : that is the complete object to which the reference is bound
14290 : persists for the lifetime of the reference.
14291 :
14292 : The temporaries created during the evaluation of the expression
14293 : initializing the reference, except the temporary to which the
14294 : reference is bound, are destroyed at the end of the
14295 : full-expression in which they are created.
14296 :
14297 : In that case, we store the converted expression into a new
14298 : VAR_DECL in a new scope.
14299 :
14300 : However, we want to be careful not to create temporaries when
14301 : they are not required. For example, given:
14302 :
14303 : struct B {};
14304 : struct D : public B {};
14305 : D f();
14306 : const B& b = f();
14307 :
14308 : there is no need to copy the return value from "f"; we can just
14309 : extend its lifetime. Similarly, given:
14310 :
14311 : struct S {};
14312 : struct T { operator S(); };
14313 : T t;
14314 : const S& s = t;
14315 :
14316 : we can extend the lifetime of the return value of the conversion
14317 : operator.
14318 :
14319 : The next several functions are involved in this lifetime extension. */
14320 :
14321 : /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
14322 : reference is being bound to a temporary. Create and return a new
14323 : VAR_DECL with the indicated TYPE; this variable will store the value to
14324 : which the reference is bound. */
14325 :
14326 : tree
14327 9373 : make_temporary_var_for_ref_to_temp (tree decl, tree type)
14328 : {
14329 9373 : tree var = create_temporary_var (type);
14330 :
14331 : /* Register the variable. */
14332 9373 : if (VAR_P (decl)
14333 9373 : && (TREE_STATIC (decl) || CP_DECL_THREAD_LOCAL_P (decl)))
14334 : {
14335 : /* Namespace-scope or local static; give it a mangled name. */
14336 :
14337 : /* If an initializer is visible to multiple translation units, those
14338 : translation units must agree on the addresses of the
14339 : temporaries. Therefore the temporaries must be given a consistent name
14340 : and vague linkage. The mangled name of a temporary is the name of the
14341 : non-temporary object in whose initializer they appear, prefixed with
14342 : GR and suffixed with a sequence number mangled using the usual rules
14343 : for a seq-id. Temporaries are numbered with a pre-order, depth-first,
14344 : left-to-right walk of the complete initializer. */
14345 829 : copy_linkage (var, decl);
14346 :
14347 829 : tree name = mangle_ref_init_variable (decl);
14348 829 : DECL_NAME (var) = name;
14349 829 : SET_DECL_ASSEMBLER_NAME (var, name);
14350 :
14351 : /* Set the context to make the variable mergeable in modules. */
14352 829 : DECL_CONTEXT (var) = current_scope ();
14353 : }
14354 : else
14355 : /* Create a new cleanup level if necessary. */
14356 8544 : maybe_push_cleanup_level (type);
14357 :
14358 9373 : return pushdecl (var);
14359 : }
14360 :
14361 : static tree extend_temps_r (tree *, int *, void *);
14362 :
14363 : /* EXPR is the initializer for a variable DECL of reference or
14364 : std::initializer_list type. Create, push and return a new VAR_DECL
14365 : for the initializer so that it will live as long as DECL. Any
14366 : cleanup for the new variable is returned through CLEANUP, and the
14367 : code to initialize the new variable is returned through INITP. */
14368 :
14369 : static tree
14370 9373 : set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
14371 : tree *initp, tree *cond_guard,
14372 : void *walk_data)
14373 : {
14374 9373 : tree init;
14375 9373 : tree type;
14376 9373 : tree var;
14377 :
14378 : /* Create the temporary variable. */
14379 9373 : type = TREE_TYPE (expr);
14380 9373 : var = make_temporary_var_for_ref_to_temp (decl, type);
14381 9373 : layout_decl (var, 0);
14382 : /* If the rvalue is the result of a function call it will be
14383 : a TARGET_EXPR. If it is some other construct (such as a
14384 : member access expression where the underlying object is
14385 : itself the result of a function call), turn it into a
14386 : TARGET_EXPR here. It is important that EXPR be a
14387 : TARGET_EXPR below since otherwise the INIT_EXPR will
14388 : attempt to make a bitwise copy of EXPR to initialize
14389 : VAR. */
14390 9373 : if (TREE_CODE (expr) != TARGET_EXPR)
14391 0 : expr = get_target_expr (expr);
14392 : else
14393 : {
14394 9373 : if (TREE_ADDRESSABLE (expr))
14395 9290 : TREE_ADDRESSABLE (var) = 1;
14396 9373 : if (DECL_MERGEABLE (TARGET_EXPR_SLOT (expr)))
14397 633 : DECL_MERGEABLE (var) = true;
14398 : }
14399 :
14400 9373 : if (TREE_CODE (decl) == FIELD_DECL
14401 9373 : && extra_warnings && !warning_suppressed_p (decl))
14402 : {
14403 3 : warning (OPT_Wextra, "a temporary bound to %qD only persists "
14404 : "until the constructor exits", decl);
14405 3 : suppress_warning (decl);
14406 : }
14407 :
14408 : /* Recursively extend temps in this initializer. The recursion needs to come
14409 : after creating the variable to conform to the mangling ABI, and before
14410 : maybe_constant_init because the extension might change its result. */
14411 9373 : if (walk_data)
14412 2188 : cp_walk_tree (&TARGET_EXPR_INITIAL (expr), extend_temps_r,
14413 : walk_data, nullptr);
14414 : else
14415 7185 : TARGET_EXPR_INITIAL (expr)
14416 14370 : = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups,
14417 : cond_guard);
14418 :
14419 : /* Any reference temp has a non-trivial initializer. */
14420 9373 : DECL_NONTRIVIALLY_INITIALIZED_P (var) = true;
14421 :
14422 : /* If the initializer is constant, put it in DECL_INITIAL so we get
14423 : static initialization and use in constant expressions. */
14424 9373 : init = maybe_constant_init (expr, var, /*manifestly_const_eval=*/true);
14425 : /* As in store_init_value. */
14426 9373 : init = cp_fully_fold (init);
14427 9373 : if (TREE_CONSTANT (init))
14428 : {
14429 1306 : if (literal_type_p (type)
14430 1280 : && CP_TYPE_CONST_NON_VOLATILE_P (type)
14431 2078 : && !TYPE_HAS_MUTABLE_P (type))
14432 : {
14433 : /* 5.19 says that a constant expression can include an
14434 : lvalue-rvalue conversion applied to "a glvalue of literal type
14435 : that refers to a non-volatile temporary object initialized
14436 : with a constant expression". Rather than try to communicate
14437 : that this VAR_DECL is a temporary, just mark it constexpr. */
14438 769 : DECL_DECLARED_CONSTEXPR_P (var) = true;
14439 769 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
14440 769 : TREE_CONSTANT (var) = true;
14441 769 : TREE_READONLY (var) = true;
14442 : }
14443 1306 : DECL_INITIAL (var) = init;
14444 1306 : init = NULL_TREE;
14445 : }
14446 : else
14447 : /* Create the INIT_EXPR that will initialize the temporary
14448 : variable. */
14449 8067 : init = split_nonconstant_init (var, expr);
14450 9373 : if (at_function_scope_p ())
14451 : {
14452 8636 : add_decl_expr (var);
14453 :
14454 8636 : if (TREE_STATIC (var))
14455 92 : init = add_stmt_to_compound (init, register_dtor_fn (var));
14456 : else
14457 : {
14458 : /* ??? Instead of rebuilding the cleanup, we could replace the slot
14459 : with var in TARGET_EXPR_CLEANUP (expr). */
14460 8544 : tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
14461 8544 : if (cleanup)
14462 : {
14463 3526 : if (cond_guard && cleanup != error_mark_node)
14464 : {
14465 23 : if (*cond_guard == NULL_TREE)
14466 : {
14467 23 : *cond_guard = build_local_temp (boolean_type_node);
14468 23 : add_decl_expr (*cond_guard);
14469 23 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION,
14470 : *cond_guard, NOP_EXPR,
14471 : boolean_false_node,
14472 : tf_warning_or_error);
14473 23 : finish_expr_stmt (set);
14474 : }
14475 23 : cleanup = build3 (COND_EXPR, void_type_node,
14476 : *cond_guard, cleanup, NULL_TREE);
14477 : }
14478 3526 : if (flag_exceptions && TREE_CODE (TREE_TYPE (var)) != ARRAY_TYPE)
14479 : {
14480 : /* The normal cleanup for this extended variable isn't pushed
14481 : until cp_finish_decl, so we need to retain a TARGET_EXPR
14482 : to clean it up in case a later initializer throws
14483 : (g++.dg/eh/ref-temp3.C).
14484 :
14485 : We don't do this for array temporaries because they have
14486 : the array cleanup region from build_vec_init.
14487 :
14488 : Unlike maybe_push_temp_cleanup, we don't actually need a
14489 : flag, but a TARGET_EXPR needs a TARGET_EXPR_SLOT.
14490 : Perhaps this could use WITH_CLEANUP_EXPR instead, but
14491 : gimplify.cc doesn't handle that, and front-end handling
14492 : was removed in r8-1725 and r8-1818.
14493 :
14494 : Alternately it might be preferable to flatten an
14495 : initialization with extended temps into a sequence of
14496 : (non-full-expression) statements, so we could immediately
14497 : push_cleanup here for only a single cleanup region, but we
14498 : don't have a mechanism for that in the front-end, only the
14499 : gimplifier. */
14500 3418 : tree targ = get_internal_target_expr (boolean_true_node);
14501 3418 : TARGET_EXPR_CLEANUP (targ) = cleanup;
14502 3418 : CLEANUP_EH_ONLY (targ) = true;
14503 : /* Don't actually initialize the bool. */
14504 3418 : init = (!init ? void_node
14505 3392 : : convert_to_void (init, ICV_STATEMENT, tf_none));
14506 3418 : TARGET_EXPR_INITIAL (targ) = init;
14507 3418 : init = targ;
14508 : }
14509 3526 : vec_safe_push (*cleanups, cleanup);
14510 : }
14511 : }
14512 :
14513 : /* We must be careful to destroy the temporary only
14514 : after its initialization has taken place. If the
14515 : initialization throws an exception, then the
14516 : destructor should not be run. We cannot simply
14517 : transform INIT into something like:
14518 :
14519 : (INIT, ({ CLEANUP_STMT; }))
14520 :
14521 : because emit_local_var always treats the
14522 : initializer as a full-expression. Thus, the
14523 : destructor would run too early; it would run at the
14524 : end of initializing the reference variable, rather
14525 : than at the end of the block enclosing the
14526 : reference variable.
14527 :
14528 : The solution is to pass back a cleanup expression
14529 : which the caller is responsible for attaching to
14530 : the statement tree. */
14531 : }
14532 : else
14533 : {
14534 : /* Don't output reflection variables. */
14535 737 : if (consteval_only_p (var))
14536 6 : DECL_EXTERNAL (var) = true;
14537 :
14538 737 : rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
14539 737 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
14540 : {
14541 43 : if (CP_DECL_THREAD_LOCAL_P (var))
14542 12 : tls_aggregates = tree_cons (NULL_TREE, var,
14543 : tls_aggregates);
14544 : else
14545 31 : static_aggregates = tree_cons (NULL_TREE, var,
14546 : static_aggregates);
14547 : }
14548 : else
14549 : /* Check whether the dtor is callable. */
14550 694 : cxx_maybe_build_cleanup (var, tf_warning_or_error);
14551 : }
14552 : /* Avoid -Wunused-variable warning (c++/38958). */
14553 9373 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
14554 9373 : && VAR_P (decl))
14555 3500 : TREE_USED (decl) = DECL_READ_P (decl) = true;
14556 :
14557 9373 : *initp = init;
14558 9373 : return var;
14559 : }
14560 :
14561 : /* Convert EXPR to the indicated reference TYPE, in a way suitable for
14562 : initializing a variable of that TYPE. */
14563 :
14564 : tree
14565 6528514 : initialize_reference (tree type, tree expr,
14566 : int flags, tsubst_flags_t complain)
14567 : {
14568 6528514 : conversion *conv;
14569 6528514 : location_t loc = cp_expr_loc_or_input_loc (expr);
14570 :
14571 6528514 : if (type == error_mark_node || error_operand_p (expr))
14572 : return error_mark_node;
14573 :
14574 6528437 : conversion_obstack_sentinel cos;
14575 :
14576 6528437 : conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
14577 : flags, complain);
14578 : /* If this conversion failed, we're in C++20, and we have something like
14579 : A& a(b) where A is an aggregate, try again, this time as A& a{b}. */
14580 6528437 : if ((!conv || conv->bad_p)
14581 725 : && (flags & LOOKUP_AGGREGATE_PAREN_INIT))
14582 : {
14583 4 : tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
14584 4 : CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
14585 4 : CONSTRUCTOR_IS_PAREN_INIT (e) = true;
14586 4 : conversion *c = reference_binding (type, TREE_TYPE (e), e,
14587 : /*c_cast_p=*/false, flags, complain);
14588 : /* If this worked, use it. */
14589 4 : if (c && !c->bad_p)
14590 : expr = e, conv = c;
14591 : }
14592 6528437 : if (!conv || conv->bad_p)
14593 : {
14594 722 : if (complain & tf_error)
14595 : {
14596 366 : if (conv)
14597 313 : convert_like (conv, expr, complain);
14598 53 : else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
14599 21 : && !TYPE_REF_IS_RVALUE (type)
14600 74 : && !lvalue_p (expr))
14601 6 : error_at (loc, "invalid initialization of non-const reference of "
14602 : "type %qH from an rvalue of type %qI",
14603 6 : type, TREE_TYPE (expr));
14604 : else
14605 47 : error_at (loc, "invalid initialization of reference of type "
14606 : "%qH from expression of type %qI", type,
14607 47 : TREE_TYPE (expr));
14608 : }
14609 722 : return error_mark_node;
14610 : }
14611 :
14612 6527715 : if (conv->kind == ck_ref_bind)
14613 : /* Perform the conversion. */
14614 6527712 : expr = convert_like (conv, expr, complain);
14615 3 : else if (conv->kind == ck_ambig)
14616 : /* We gave an error in build_user_type_conversion_1. */
14617 3 : expr = error_mark_node;
14618 : else
14619 0 : gcc_unreachable ();
14620 :
14621 : return expr;
14622 6528437 : }
14623 :
14624 : /* Return true if T is std::pair<const T&, const T&>. */
14625 :
14626 : static bool
14627 619991 : std_pair_ref_ref_p (tree t)
14628 : {
14629 : /* First, check if we have std::pair. */
14630 58742 : if (!NON_UNION_CLASS_TYPE_P (t)
14631 678273 : || !CLASSTYPE_TEMPLATE_INSTANTIATION (t))
14632 : return false;
14633 21227 : tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (t));
14634 21227 : if (!decl_in_std_namespace_p (tdecl))
14635 : return false;
14636 15886 : tree name = DECL_NAME (tdecl);
14637 15886 : if (!name || !id_equal (name, "pair"))
14638 : return false;
14639 :
14640 : /* Now see if the template arguments are both const T&. */
14641 361 : tree args = CLASSTYPE_TI_ARGS (t);
14642 361 : if (TREE_VEC_LENGTH (args) != 2)
14643 : return false;
14644 421 : for (int i = 0; i < 2; i++)
14645 451 : if (!TYPE_REF_OBJ_P (TREE_VEC_ELT (args, i))
14646 451 : || !CP_TYPE_CONST_P (TREE_TYPE (TREE_VEC_ELT (args, i))))
14647 : return false;
14648 :
14649 : return true;
14650 : }
14651 :
14652 : /* Return true if a class T has a reference member. */
14653 :
14654 : static bool
14655 216 : class_has_reference_member_p (tree t)
14656 : {
14657 216 : for (tree fields = TYPE_FIELDS (t);
14658 3380 : fields;
14659 3164 : fields = DECL_CHAIN (fields))
14660 3227 : if (TREE_CODE (fields) == FIELD_DECL
14661 196 : && !DECL_ARTIFICIAL (fields)
14662 3394 : && TYPE_REF_P (TREE_TYPE (fields)))
14663 : return true;
14664 : return false;
14665 : }
14666 :
14667 : /* A wrapper for the above suitable as a callback for dfs_walk_once. */
14668 :
14669 : static tree
14670 216 : class_has_reference_member_p_r (tree binfo, void *)
14671 : {
14672 216 : return (class_has_reference_member_p (BINFO_TYPE (binfo))
14673 216 : ? integer_one_node : NULL_TREE);
14674 : }
14675 :
14676 :
14677 : /* Return true if T (either a class or a function) has been marked as
14678 : not-dangling. */
14679 :
14680 : static bool
14681 1557 : no_dangling_p (tree t)
14682 : {
14683 1557 : t = lookup_attribute ("no_dangling", TYPE_ATTRIBUTES (t));
14684 1557 : if (!t)
14685 : return false;
14686 :
14687 75 : t = TREE_VALUE (t);
14688 75 : if (!t)
14689 : return true;
14690 :
14691 51 : t = build_converted_constant_bool_expr (TREE_VALUE (t), tf_warning_or_error);
14692 51 : t = cxx_constant_value (t);
14693 51 : return t == boolean_true_node;
14694 : }
14695 :
14696 : /* Return true if a class CTYPE is either std::reference_wrapper or
14697 : std::ref_view, or a reference wrapper class. We consider a class
14698 : a reference wrapper class if it has a reference member. We no
14699 : longer check that it has a constructor taking the same reference type
14700 : since that approach still generated too many false positives. */
14701 :
14702 : static bool
14703 447 : reference_like_class_p (tree ctype)
14704 : {
14705 447 : if (!CLASS_TYPE_P (ctype))
14706 : return false;
14707 :
14708 314 : if (no_dangling_p (ctype))
14709 : return true;
14710 :
14711 : /* Also accept a std::pair<const T&, const T&>. */
14712 290 : if (std_pair_ref_ref_p (ctype))
14713 : return true;
14714 :
14715 275 : tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (ctype));
14716 275 : if (decl_in_std_namespace_p (tdecl))
14717 : {
14718 38 : tree name = DECL_NAME (tdecl);
14719 38 : if (name
14720 38 : && (id_equal (name, "reference_wrapper")
14721 26 : || id_equal (name, "span")
14722 11 : || id_equal (name, "ref_view")))
14723 : return true;
14724 : }
14725 :
14726 : /* Avoid warning if CTYPE looks like std::span: it has a T* member and
14727 : a trivial destructor. For example,
14728 :
14729 : template<typename T>
14730 : struct Span {
14731 : T* data_;
14732 : std::size len_;
14733 : };
14734 :
14735 : is considered std::span-like. */
14736 248 : if (NON_UNION_CLASS_TYPE_P (ctype) && TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
14737 228 : for (tree field = next_aggregate_field (TYPE_FIELDS (ctype));
14738 367 : field; field = next_aggregate_field (DECL_CHAIN (field)))
14739 199 : if (TYPE_PTR_P (TREE_TYPE (field)))
14740 : return true;
14741 :
14742 : /* Some classes, such as std::tuple, have the reference member in its
14743 : (non-direct) base class. */
14744 188 : if (dfs_walk_once (TYPE_BINFO (ctype), class_has_reference_member_p_r,
14745 : nullptr, nullptr))
14746 63 : return true;
14747 :
14748 : return false;
14749 : }
14750 :
14751 : /* Helper for maybe_warn_dangling_reference to find a problematic temporary
14752 : in EXPR (as outlined in maybe_warn_dangling_reference), or NULL_TREE
14753 : if none found. For instance:
14754 :
14755 : const S& s = S().self(); // S()
14756 : const int& r = (42, f(1)); // temporary for passing 1 to f
14757 : const int& t = b ? f(1) : f(2); // temporary for 1
14758 : const int& u = b ? f(1) : f(g); // temporary for 1
14759 : const int& v = b ? f(g) : f(2); // temporary for 2
14760 : const int& w = b ? f(g) : f(g); // NULL_TREE
14761 : const int& y = (f(1), 42); // NULL_TREE
14762 : const int& z = f(f(1)); // temporary for 1
14763 :
14764 : EXPR is the initializer. If ARG_P is true, we're processing an argument
14765 : to a function; the point is to distinguish between, for example,
14766 :
14767 : Ref::inner (&TARGET_EXPR <D.2839, F::foo (fm)>)
14768 :
14769 : where we shouldn't warn, and
14770 :
14771 : Ref::inner (&TARGET_EXPR <D.2908, F::foo (&TARGET_EXPR <...>)>)
14772 :
14773 : where we should warn (Ref is a reference_like_class_p so we see through
14774 : it. */
14775 :
14776 : static tree
14777 3715 : do_warn_dangling_reference (tree expr, bool arg_p)
14778 : {
14779 3881 : STRIP_NOPS (expr);
14780 :
14781 3881 : if (arg_p && expr_represents_temporary_p (expr))
14782 : {
14783 : /* An attempt to reduce the number of -Wdangling-reference
14784 : false positives concerning reference wrappers (c++/107532).
14785 : When we encounter a reference_like_class_p, we don't warn
14786 : just yet; instead, we keep recursing to see if there were
14787 : any temporaries behind the reference-wrapper class. */
14788 : tree e = expr;
14789 355 : while (handled_component_p (e))
14790 15 : e = TREE_OPERAND (e, 0);
14791 340 : tree type = TREE_TYPE (e);
14792 : /* If the temporary represents a lambda, we don't really know
14793 : what's going on here. */
14794 462 : if (!reference_like_class_p (type) && !LAMBDA_TYPE_P (type))
14795 : return expr;
14796 : }
14797 :
14798 3648 : switch (TREE_CODE (expr))
14799 : {
14800 1822 : case CALL_EXPR:
14801 1822 : {
14802 1822 : tree fndecl = cp_get_callee_fndecl_nofold (expr);
14803 1822 : if (!fndecl
14804 1822 : || warning_suppressed_p (fndecl, OPT_Wdangling_reference)
14805 2055 : || !warning_enabled_at (DECL_SOURCE_LOCATION (fndecl),
14806 1811 : OPT_Wdangling_reference)
14807 : /* Don't emit a false positive for:
14808 : std::vector<int> v = ...;
14809 : std::vector<int>::const_iterator it = v.begin();
14810 : const int &r = *it++;
14811 : because R refers to one of the int elements of V, not to
14812 : a temporary object. Member operator* may return a reference
14813 : but probably not to one of its arguments. */
14814 1452 : || (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl)
14815 852 : && DECL_OVERLOADED_OPERATOR_P (fndecl)
14816 319 : && DECL_OVERLOADED_OPERATOR_IS (fndecl, INDIRECT_REF))
14817 3065 : || no_dangling_p (TREE_TYPE (fndecl)))
14818 : return NULL_TREE;
14819 :
14820 1219 : tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
14821 : /* If the function doesn't return a reference, don't warn. This
14822 : can be e.g.
14823 : const int& z = std::min({1, 2, 3, 4, 5, 6, 7});
14824 : which doesn't dangle: std::min here returns an int.
14825 :
14826 : If the function returns a std::pair<const T&, const T&>, we
14827 : warn, to detect e.g.
14828 : std::pair<const int&, const int&> v = std::minmax(1, 2);
14829 : which also creates a dangling reference, because std::minmax
14830 : returns std::pair<const T&, const T&>(b, a). */
14831 1219 : if (!(TYPE_REF_OBJ_P (rettype) || reference_like_class_p (rettype)))
14832 : return NULL_TREE;
14833 :
14834 : /* Here we're looking to see if any of the arguments is a temporary
14835 : initializing a reference parameter. */
14836 1647 : for (int i = 0; i < call_expr_nargs (expr); ++i)
14837 : {
14838 1240 : tree arg = CALL_EXPR_ARG (expr, i);
14839 : /* Check that this argument initializes a reference, except for
14840 : the argument initializing the object of a member function. */
14841 1240 : if (!DECL_IOBJ_MEMBER_FUNCTION_P (fndecl)
14842 1240 : && !TYPE_REF_P (TREE_TYPE (arg)))
14843 130 : continue;
14844 1110 : STRIP_NOPS (arg);
14845 1110 : if (TREE_CODE (arg) == ADDR_EXPR)
14846 725 : arg = TREE_OPERAND (arg, 0);
14847 : /* Recurse to see if the argument is a temporary. It could also
14848 : be another call taking a temporary and returning it and
14849 : initializing this reference parameter. */
14850 1110 : if ((arg = do_warn_dangling_reference (arg, /*arg_p=*/true)))
14851 : {
14852 : /* If we know the temporary could not bind to the return type,
14853 : don't warn. This is for scalars and empty classes only
14854 : because for other classes we can't be sure we are not
14855 : returning its sub-object. */
14856 278 : if ((SCALAR_TYPE_P (TREE_TYPE (arg))
14857 152 : || is_empty_class (TREE_TYPE (arg)))
14858 153 : && TYPE_REF_P (rettype)
14859 138 : && !reference_related_p (TREE_TYPE (rettype),
14860 138 : TREE_TYPE (arg)))
14861 21 : continue;
14862 : return arg;
14863 : }
14864 : /* Don't warn about member functions like:
14865 : std::any a(...);
14866 : S& s = a.emplace<S>({0}, 0);
14867 : which construct a new object and return a reference to it, but
14868 : we still want to detect:
14869 : struct S { const S& self () { return *this; } };
14870 : const S& s = S().self();
14871 : where 's' dangles. If we've gotten here, the object this function
14872 : is invoked on is not a temporary. */
14873 832 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl))
14874 : break;
14875 : }
14876 : return NULL_TREE;
14877 : }
14878 28 : case COMPOUND_EXPR:
14879 28 : return do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p);
14880 31 : case COND_EXPR:
14881 31 : if (tree t = do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p))
14882 : return t;
14883 16 : return do_warn_dangling_reference (TREE_OPERAND (expr, 2), arg_p);
14884 0 : case PAREN_EXPR:
14885 0 : return do_warn_dangling_reference (TREE_OPERAND (expr, 0), arg_p);
14886 122 : case TARGET_EXPR:
14887 122 : return do_warn_dangling_reference (TARGET_EXPR_INITIAL (expr), arg_p);
14888 : default:
14889 : return NULL_TREE;
14890 : }
14891 : }
14892 :
14893 : /* Implement -Wdangling-reference, to detect cases like
14894 :
14895 : int n = 1;
14896 : const int& r = std::max(n - 1, n + 1); // r is dangling
14897 :
14898 : This creates temporaries from the arguments, returns a reference to
14899 : one of the temporaries, but both temporaries are destroyed at the end
14900 : of the full expression.
14901 :
14902 : This works by checking if a reference is initialized with a function
14903 : that returns a reference, and at least one parameter of the function
14904 : is a reference that is bound to a temporary. It assumes that such a
14905 : function actually returns one of its arguments.
14906 :
14907 : DECL is the reference being initialized, INIT is the initializer. */
14908 :
14909 : static void
14910 110025935 : maybe_warn_dangling_reference (const_tree decl, tree init)
14911 : {
14912 110025935 : if (!warn_dangling_reference)
14913 : return;
14914 622260 : tree type = TREE_TYPE (decl);
14915 : /* Only warn if what we're initializing has type T&& or const T&, or
14916 : std::pair<const T&, const T&>. (A non-const lvalue reference can't
14917 : bind to a temporary.) */
14918 1241961 : if (!((TYPE_REF_OBJ_P (type)
14919 5444 : && (TYPE_REF_IS_RVALUE (type)
14920 5004 : || CP_TYPE_CONST_P (TREE_TYPE (type))))
14921 619701 : || std_pair_ref_ref_p (type)))
14922 : return;
14923 : /* Don't suppress the diagnostic just because the call comes from
14924 : a system header. If the DECL is not in a system header, or if
14925 : -Wsystem-headers was provided, warn. */
14926 2574 : auto wsh
14927 2574 : = make_temp_override (global_dc->m_warn_system_headers,
14928 2574 : (!in_system_header_at (DECL_SOURCE_LOCATION (decl))
14929 2574 : || global_dc->m_warn_system_headers));
14930 2574 : if (tree call = do_warn_dangling_reference (init, /*arg_p=*/false))
14931 : {
14932 212 : auto_diagnostic_group d;
14933 212 : if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wdangling_reference,
14934 : "possibly dangling reference to a temporary"))
14935 212 : inform (EXPR_LOCATION (call), "%qT temporary created here",
14936 212 : TREE_TYPE (call));
14937 212 : }
14938 2574 : }
14939 :
14940 : /* If *P is an xvalue expression, prevent temporary lifetime extension if it
14941 : gets used to initialize a reference. */
14942 :
14943 : static tree
14944 95 : prevent_lifetime_extension (tree t)
14945 : {
14946 95 : tree *p = &t;
14947 95 : while (TREE_CODE (*p) == COMPOUND_EXPR)
14948 0 : p = &TREE_OPERAND (*p, 1);
14949 104 : while (handled_component_p (*p))
14950 9 : p = &TREE_OPERAND (*p, 0);
14951 : /* Change a TARGET_EXPR from prvalue to xvalue. */
14952 95 : if (TREE_CODE (*p) == TARGET_EXPR)
14953 3 : *p = build2 (COMPOUND_EXPR, TREE_TYPE (*p), *p,
14954 3 : move (TARGET_EXPR_SLOT (*p)));
14955 95 : return t;
14956 : }
14957 :
14958 : /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
14959 : which is bound either to a reference or a std::initializer_list. */
14960 :
14961 : static tree
14962 767233 : extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups,
14963 : tree *cond_guard)
14964 : {
14965 : /* CWG1299 (C++20): The temporary object to which the reference is bound or
14966 : the temporary object that is the complete object of a subobject to which
14967 : the reference is bound persists for the lifetime of the reference if the
14968 : glvalue to which the reference is bound was obtained through one of the
14969 : following:
14970 : - a temporary materialization conversion ([conv.rval]),
14971 : - ( expression ), where expression is one of these expressions,
14972 : - subscripting ([expr.sub]) of an array operand, where that operand is one
14973 : of these expressions,
14974 : - a class member access ([expr.ref]) using the . operator where the left
14975 : operand is one of these expressions and the right operand designates a
14976 : non-static data member of non-reference type,
14977 : - a pointer-to-member operation ([expr.mptr.oper]) using the .* operator
14978 : where the left operand is one of these expressions and the right operand
14979 : is a pointer to data member of non-reference type,
14980 : - a const_cast ([expr.const.cast]), static_cast ([expr.static.cast]),
14981 : dynamic_cast ([expr.dynamic.cast]), or reinterpret_cast
14982 : ([expr.reinterpret.cast]) converting, without a user-defined conversion,
14983 : a glvalue operand that is one of these expressions to a glvalue that
14984 : refers to the object designated by the operand, or to its complete
14985 : object or a subobject thereof,
14986 : - a conditional expression ([expr.cond]) that is a glvalue where the
14987 : second or third operand is one of these expressions, or
14988 : - a comma expression ([expr.comma]) that is a glvalue where the right
14989 : operand is one of these expressions. */
14990 :
14991 : /* FIXME several cases are still handled wrong (101572, 81420). */
14992 :
14993 767233 : tree sub = init;
14994 767233 : tree *p;
14995 767233 : STRIP_NOPS (sub);
14996 767233 : if (TREE_CODE (sub) == COMPOUND_EXPR)
14997 : {
14998 242 : TREE_OPERAND (sub, 1)
14999 242 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups,
15000 : cond_guard);
15001 242 : return init;
15002 : }
15003 766991 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15004 766991 : && TYPE_PTRDATAMEM_P (TREE_TYPE (tree_strip_nop_conversions
15005 : (TREE_OPERAND (sub, 1)))))
15006 : {
15007 : /* A pointer-to-member operation. */
15008 6 : TREE_OPERAND (sub, 0)
15009 6 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 0), cleanups,
15010 : cond_guard);
15011 6 : return init;
15012 : }
15013 766985 : if (TREE_CODE (sub) == COND_EXPR)
15014 : {
15015 23465 : tree cur_cond_guard = NULL_TREE;
15016 23465 : if (TREE_OPERAND (sub, 1))
15017 23465 : TREE_OPERAND (sub, 1)
15018 46930 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups,
15019 : &cur_cond_guard);
15020 23465 : if (cur_cond_guard)
15021 : {
15022 9 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard,
15023 : NOP_EXPR, boolean_true_node,
15024 : tf_warning_or_error);
15025 9 : TREE_OPERAND (sub, 1)
15026 18 : = cp_build_compound_expr (set, TREE_OPERAND (sub, 1),
15027 : tf_warning_or_error);
15028 : }
15029 23465 : cur_cond_guard = NULL_TREE;
15030 23465 : if (TREE_OPERAND (sub, 2))
15031 23465 : TREE_OPERAND (sub, 2)
15032 46930 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 2), cleanups,
15033 : &cur_cond_guard);
15034 23465 : if (cur_cond_guard)
15035 : {
15036 12 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard,
15037 : NOP_EXPR, boolean_true_node,
15038 : tf_warning_or_error);
15039 12 : TREE_OPERAND (sub, 2)
15040 24 : = cp_build_compound_expr (set, TREE_OPERAND (sub, 2),
15041 : tf_warning_or_error);
15042 : }
15043 23465 : return init;
15044 : }
15045 743520 : if (TREE_CODE (sub) != ADDR_EXPR)
15046 : return init;
15047 : /* Deal with binding to a subobject. */
15048 265196 : for (p = &TREE_OPERAND (sub, 0);
15049 278767 : TREE_CODE (*p) == COMPONENT_REF || TREE_CODE (*p) == ARRAY_REF; )
15050 13571 : p = &TREE_OPERAND (*p, 0);
15051 265196 : if (TREE_CODE (*p) == TARGET_EXPR)
15052 : {
15053 7185 : tree subinit = NULL_TREE;
15054 7185 : *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit,
15055 : cond_guard, nullptr);
15056 7185 : recompute_tree_invariant_for_addr_expr (sub);
15057 7185 : if (init != sub)
15058 7167 : init = fold_convert (TREE_TYPE (init), sub);
15059 7185 : if (subinit)
15060 6053 : init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
15061 : }
15062 : return init;
15063 : }
15064 :
15065 : /* Data for extend_temps_r, mostly matching the parameters of
15066 : extend_ref_init_temps. */
15067 :
15068 : struct extend_temps_data
15069 : {
15070 : tree decl;
15071 : tree init;
15072 : vec<tree, va_gc> **cleanups;
15073 : tree* cond_guard;
15074 : hash_set<tree> *pset; // For avoiding redundant walk_tree.
15075 : hash_map<tree, tree> *var_map; // For remapping extended temps.
15076 : };
15077 :
15078 : /* Tree walk function for extend_all_temps. Generally parallel to
15079 : extend_ref_init_temps_1, but adapted for walk_tree. */
15080 :
15081 : tree
15082 99096 : extend_temps_r (tree *tp, int *walk_subtrees, void *data)
15083 : {
15084 99096 : extend_temps_data *d = (extend_temps_data *)data;
15085 :
15086 99096 : if (TREE_CODE (*tp) == VAR_DECL)
15087 : {
15088 11570 : if (tree *r = d->var_map->get (*tp))
15089 0 : *tp = *r;
15090 : return NULL_TREE;
15091 : }
15092 :
15093 87507 : if (TYPE_P (*tp) || TREE_CODE (*tp) == CLEANUP_POINT_EXPR
15094 175032 : || d->pset->add (*tp))
15095 : {
15096 4964 : *walk_subtrees = 0;
15097 4964 : return NULL_TREE;
15098 : }
15099 :
15100 82562 : if (TREE_CODE (*tp) == COND_EXPR)
15101 : {
15102 8 : cp_walk_tree (&TREE_OPERAND (*tp, 0), extend_temps_r, d, nullptr);
15103 :
15104 24 : auto walk_arm = [d](tree &op)
15105 : {
15106 16 : tree cur_cond_guard = NULL_TREE;
15107 16 : auto ov = make_temp_override (d->cond_guard, &cur_cond_guard);
15108 16 : cp_walk_tree (&op, extend_temps_r, d, nullptr);
15109 16 : if (cur_cond_guard)
15110 : {
15111 2 : tree set = build2 (MODIFY_EXPR, boolean_type_node,
15112 : cur_cond_guard, boolean_true_node);
15113 2 : op = cp_build_compound_expr (set, op, tf_none);
15114 : }
15115 24 : };
15116 8 : walk_arm (TREE_OPERAND (*tp, 1));
15117 8 : walk_arm (TREE_OPERAND (*tp, 2));
15118 :
15119 8 : *walk_subtrees = 0;
15120 8 : return NULL_TREE;
15121 : }
15122 :
15123 82554 : tree *p = tp;
15124 :
15125 82554 : if (TREE_CODE (*tp) == ADDR_EXPR)
15126 18630 : for (p = &TREE_OPERAND (*tp, 0);
15127 20098 : TREE_CODE (*p) == COMPONENT_REF || TREE_CODE (*p) == ARRAY_REF; )
15128 1468 : p = &TREE_OPERAND (*p, 0);
15129 :
15130 82554 : if (TREE_CODE (*p) == TARGET_EXPR
15131 : /* An eliding TARGET_EXPR isn't a temporary at all. */
15132 3598 : && !TARGET_EXPR_ELIDING_P (*p)
15133 : /* A TARGET_EXPR with TARGET_EXPR_INTERNAL_P is an artificial variable
15134 : used during initialization that need not be extended. */
15135 85078 : && !TARGET_EXPR_INTERNAL_P (*p))
15136 : {
15137 : /* A CLEANUP_EH_ONLY expr should also have TARGET_EXPR_INTERNAL_P. */
15138 2188 : gcc_checking_assert (!CLEANUP_EH_ONLY (*p));
15139 :
15140 2188 : tree subinit = NULL_TREE;
15141 2188 : tree slot = TARGET_EXPR_SLOT (*p);
15142 2188 : *p = set_up_extended_ref_temp (d->decl, *p, d->cleanups, &subinit,
15143 : d->cond_guard, d);
15144 2188 : if (TREE_CODE (*tp) == ADDR_EXPR)
15145 2174 : recompute_tree_invariant_for_addr_expr (*tp);
15146 2188 : if (subinit)
15147 2040 : *tp = cp_build_compound_expr (subinit, *tp, tf_none);
15148 2188 : d->var_map->put (slot, *p);
15149 : }
15150 :
15151 : return NULL_TREE;
15152 : }
15153 :
15154 : /* Extend all the temporaries in a for-range-initializer. */
15155 :
15156 : static tree
15157 20018 : extend_all_temps (tree decl, tree init, vec<tree, va_gc> **cleanups)
15158 : {
15159 20018 : hash_set<tree> pset;
15160 20018 : hash_map<tree, tree> map;
15161 20018 : gcc_assert (!TREE_STATIC (decl));
15162 20018 : extend_temps_data d = { decl, init, cleanups, nullptr, &pset, &map };
15163 20018 : cp_walk_tree (&init, extend_temps_r, &d, nullptr);
15164 20018 : return init;
15165 20018 : }
15166 :
15167 : /* INIT is part of the initializer for DECL. If there are any
15168 : reference or initializer lists being initialized, extend their
15169 : lifetime to match that of DECL. */
15170 :
15171 : tree
15172 112543105 : extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups,
15173 : tree *cond_guard)
15174 : {
15175 112543105 : tree type = TREE_TYPE (init);
15176 112543105 : if (processing_template_decl)
15177 : return init;
15178 :
15179 : /* P2718R0 - in C++23 for-range-initializer, extend all temps. */
15180 110045953 : if (DECL_NAME (decl) == for_range__identifier
15181 95007 : && flag_range_for_ext_temps
15182 : /* Iterating expansion statement decl is static right now, but that
15183 : could change depending on CWG3044 and CWG3043. */
15184 110066029 : && !TREE_STATIC (decl))
15185 : {
15186 20018 : gcc_checking_assert (!cond_guard);
15187 20018 : return extend_all_temps (decl, init, cleanups);
15188 : }
15189 :
15190 110025935 : maybe_warn_dangling_reference (decl, init);
15191 :
15192 110025935 : if (TYPE_REF_P (type))
15193 719396 : init = extend_ref_init_temps_1 (decl, init, cleanups, cond_guard);
15194 : else
15195 : {
15196 109306539 : tree ctor = init;
15197 109306539 : if (TREE_CODE (ctor) == TARGET_EXPR)
15198 1571103 : ctor = TARGET_EXPR_INITIAL (ctor);
15199 109306539 : if (TREE_CODE (ctor) == CONSTRUCTOR)
15200 : {
15201 : /* [dcl.init] When initializing an aggregate from a parenthesized list
15202 : of values... a temporary object bound to a reference does not have
15203 : its lifetime extended. */
15204 5728913 : if (CONSTRUCTOR_IS_PAREN_INIT (ctor))
15205 : return init;
15206 :
15207 5728419 : if (is_std_init_list (type))
15208 : {
15209 : /* The temporary array underlying a std::initializer_list
15210 : is handled like a reference temporary. */
15211 659 : tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
15212 659 : array = extend_ref_init_temps_1 (decl, array, cleanups,
15213 : cond_guard);
15214 659 : CONSTRUCTOR_ELT (ctor, 0)->value = array;
15215 : }
15216 : else
15217 : {
15218 5727760 : unsigned i;
15219 5727760 : constructor_elt *p;
15220 5727760 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
15221 63617512 : FOR_EACH_VEC_SAFE_ELT (elts, i, p)
15222 57889752 : p->value = extend_ref_init_temps (decl, p->value, cleanups,
15223 : cond_guard);
15224 : }
15225 5728419 : recompute_constructor_flags (ctor);
15226 5728419 : if (decl_maybe_constant_var_p (decl) && TREE_CONSTANT (ctor))
15227 3185060 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
15228 : }
15229 : }
15230 :
15231 : return init;
15232 : }
15233 :
15234 : /* Returns true iff an initializer for TYPE could contain temporaries that
15235 : need to be extended because they are bound to references or
15236 : std::initializer_list. */
15237 :
15238 : bool
15239 0 : type_has_extended_temps (tree type)
15240 : {
15241 0 : type = strip_array_types (type);
15242 0 : if (TYPE_REF_P (type))
15243 : return true;
15244 0 : if (CLASS_TYPE_P (type))
15245 : {
15246 0 : if (is_std_init_list (type))
15247 : return true;
15248 0 : for (tree f = next_aggregate_field (TYPE_FIELDS (type));
15249 0 : f; f = next_aggregate_field (DECL_CHAIN (f)))
15250 0 : if (type_has_extended_temps (TREE_TYPE (f)))
15251 : return true;
15252 : }
15253 : return false;
15254 : }
15255 :
15256 : /* Returns true iff TYPE is some variant of std::initializer_list. */
15257 :
15258 : bool
15259 184073343 : is_std_init_list (tree type)
15260 : {
15261 184073343 : if (!TYPE_P (type))
15262 : return false;
15263 184073320 : if (cxx_dialect == cxx98)
15264 : return false;
15265 : /* Look through typedefs. */
15266 183656326 : type = TYPE_MAIN_VARIANT (type);
15267 147170971 : return (CLASS_TYPE_P (type)
15268 147023912 : && CP_TYPE_CONTEXT (type) == std_node
15269 265623833 : && init_list_identifier == DECL_NAME (TYPE_NAME (type)));
15270 : }
15271 :
15272 : /* Returns true iff DECL is a list constructor: i.e. a constructor which
15273 : will accept an argument list of a single std::initializer_list<T>. */
15274 :
15275 : bool
15276 159783000 : is_list_ctor (tree decl)
15277 : {
15278 159783000 : tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
15279 159783000 : tree arg;
15280 :
15281 159783000 : if (!args || args == void_list_node)
15282 : return false;
15283 :
15284 128621414 : arg = non_reference (TREE_VALUE (args));
15285 128621414 : if (!is_std_init_list (arg))
15286 : return false;
15287 :
15288 2312348 : args = TREE_CHAIN (args);
15289 :
15290 3935585 : if (args && args != void_list_node && !TREE_PURPOSE (args))
15291 : /* There are more non-defaulted parms. */
15292 636296 : return false;
15293 :
15294 : return true;
15295 : }
15296 :
15297 : /* We know that can_convert_arg_bad already said "no" when trying to convert
15298 : FROM to TO with ARG and FLAGS. Try to figure out if it was because
15299 : an explicit conversion function was skipped when looking for a way to
15300 : perform the conversion. At this point we've already printed an error. */
15301 :
15302 : void
15303 2146 : maybe_show_nonconverting_candidate (tree to, tree from, tree arg, int flags)
15304 : {
15305 2146 : if (!(flags & LOOKUP_ONLYCONVERTING))
15306 311 : return;
15307 :
15308 1835 : conversion_obstack_sentinel cos;
15309 1835 : conversion *c = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
15310 : flags & ~LOOKUP_ONLYCONVERTING, tf_none);
15311 1835 : if (c && !c->bad_p && c->user_conv_p)
15312 : /* Ay, the conversion would have worked in direct-init context. */
15313 859 : for (; c; c = next_conversion (c))
15314 575 : if (c->kind == ck_user
15315 281 : && DECL_P (c->cand->fn)
15316 856 : && DECL_NONCONVERTING_P (c->cand->fn))
15317 277 : inform (DECL_SOURCE_LOCATION (c->cand->fn), "explicit conversion "
15318 : "function was not considered");
15319 1835 : }
15320 :
15321 : /* We're converting EXPR to TYPE. If that conversion involves a conversion
15322 : function and we're binding EXPR to a reference parameter of that function,
15323 : return true. */
15324 :
15325 : bool
15326 171 : conv_binds_to_reference_parm_p (tree type, tree expr)
15327 : {
15328 171 : conversion_obstack_sentinel cos;
15329 171 : conversion *c = implicit_conversion (type, TREE_TYPE (expr), expr,
15330 : /*c_cast_p=*/false, LOOKUP_NORMAL,
15331 : tf_none);
15332 171 : if (c && !c->bad_p && c->user_conv_p)
15333 117 : for (; c; c = next_conversion (c))
15334 81 : if (c->kind == ck_user)
15335 244 : for (z_candidate *cand = c->cand; cand; cand = cand->next)
15336 208 : if (cand->viable == 1)
15337 81 : for (size_t i = 0; i < cand->num_convs; ++i)
15338 45 : if (cand->convs[i]->kind == ck_ref_bind
15339 45 : && conv_get_original_expr (cand->convs[i]) == expr)
15340 : return true;
15341 :
15342 : return false;
15343 171 : }
15344 :
15345 : #include "gt-cp-call.h"
|