Line data Source code
1 : /* Functions related to invoking -*- C++ -*- methods and overloaded functions.
2 : Copyright (C) 1987-2026 Free Software Foundation, Inc.
3 : Contributed by Michael Tiemann (tiemann@cygnus.com) and
4 : modified by Brendan Kehoe (brendan@cygnus.com).
5 :
6 : This file is part of GCC.
7 :
8 : GCC is free software; you can redistribute it and/or modify
9 : it under the terms of the GNU General Public License as published by
10 : the Free Software Foundation; either version 3, or (at your option)
11 : any later version.
12 :
13 : GCC is distributed in the hope that it will be useful,
14 : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : GNU General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with GCC; see the file COPYING3. If not see
20 : <http://www.gnu.org/licenses/>. */
21 :
22 :
23 : /* High-level class interface. */
24 :
25 : #include "config.h"
26 : #include "system.h"
27 : #include "coretypes.h"
28 : #include "target.h"
29 : #include "cp-tree.h"
30 : #include "timevar.h"
31 : #include "stringpool.h"
32 : #include "cgraph.h"
33 : #include "stor-layout.h"
34 : #include "trans-mem.h"
35 : #include "flags.h"
36 : #include "toplev.h"
37 : #include "intl.h"
38 : #include "convert.h"
39 : #include "langhooks.h"
40 : #include "c-family/c-objc.h"
41 : #include "internal-fn.h"
42 : #include "stringpool.h"
43 : #include "attribs.h"
44 : #include "decl.h"
45 : #include "c-family/c-type-mismatch.h"
46 : #include "tristate.h"
47 : #include "tree-pretty-print-markup.h"
48 : #include "contracts.h" // maybe_contract_wrap_call
49 :
50 : /* The various kinds of conversion. */
51 :
52 : enum conversion_kind {
53 : ck_identity,
54 : ck_lvalue,
55 : ck_fnptr,
56 : ck_qual,
57 : ck_std,
58 : ck_ptr,
59 : ck_pmem,
60 : ck_base,
61 : ck_ref_bind,
62 : ck_user,
63 : ck_ambig,
64 : ck_list,
65 : ck_aggr,
66 : ck_rvalue,
67 : /* When LOOKUP_SHORTCUT_BAD_CONVS is set, we may return a conversion of
68 : this kind whenever we know the true conversion is either bad or outright
69 : invalid, but we don't want to attempt to compute the bad conversion (for
70 : sake of avoiding unnecessary instantiation). bad_p should always be set
71 : for these. */
72 : ck_deferred_bad,
73 : };
74 :
75 : /* The rank of the conversion. Order of the enumerals matters; better
76 : conversions should come earlier in the list. */
77 :
78 : enum conversion_rank {
79 : cr_identity,
80 : cr_exact,
81 : cr_promotion,
82 : cr_std,
83 : cr_pbool,
84 : cr_user,
85 : cr_ellipsis,
86 : cr_bad
87 : };
88 :
89 : /* An implicit conversion sequence, in the sense of [over.best.ics].
90 : The first conversion to be performed is at the end of the chain.
91 : That conversion is always a cr_identity conversion. */
92 :
93 : struct conversion {
94 : /* The kind of conversion represented by this step. */
95 : conversion_kind kind;
96 : /* The rank of this conversion. */
97 : conversion_rank rank;
98 : BOOL_BITFIELD user_conv_p : 1;
99 : BOOL_BITFIELD ellipsis_p : 1;
100 : BOOL_BITFIELD this_p : 1;
101 : /* True if this conversion would be permitted with a bending of
102 : language standards, e.g. disregarding pointer qualifiers or
103 : converting integers to pointers. */
104 : BOOL_BITFIELD bad_p : 1;
105 : /* If KIND is ck_ref_bind or ck_base, true to indicate that a
106 : temporary should be created to hold the result of the
107 : conversion. If KIND is ck_ambig or ck_user, true means force
108 : copy-initialization. */
109 : BOOL_BITFIELD need_temporary_p : 1;
110 : /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion
111 : from a pointer-to-derived to pointer-to-base is being performed. */
112 : BOOL_BITFIELD base_p : 1;
113 : /* If KIND is ck_ref_bind, true when either an lvalue reference is
114 : being bound to an lvalue expression or an rvalue reference is
115 : being bound to an rvalue expression. If KIND is ck_rvalue or ck_base,
116 : true when we are treating an lvalue as an rvalue (12.8p33). If
117 : ck_identity, we will be binding a reference directly or decaying to
118 : a pointer. */
119 : BOOL_BITFIELD rvaluedness_matches_p: 1;
120 : BOOL_BITFIELD check_narrowing: 1;
121 : /* Whether check_narrowing should only check TREE_CONSTANTs; used
122 : in build_converted_constant_expr. */
123 : BOOL_BITFIELD check_narrowing_const_only: 1;
124 : /* True if this conversion is taking place in a copy-initialization context
125 : and we should only consider converting constructors. Only set in
126 : ck_base and ck_rvalue. */
127 : BOOL_BITFIELD copy_init_p : 1;
128 : /* The type of the expression resulting from the conversion. */
129 : tree type;
130 : union {
131 : /* The next conversion in the chain. Since the conversions are
132 : arranged from outermost to innermost, the NEXT conversion will
133 : actually be performed before this conversion. This variant is
134 : used only when KIND is neither ck_identity, ck_aggr, ck_ambig nor
135 : ck_list. Please use the next_conversion function instead
136 : of using this field directly. */
137 : conversion *next;
138 : /* The expression at the beginning of the conversion chain. This
139 : variant is used only if KIND is ck_identity, ck_aggr, or ck_ambig.
140 : You can use conv_get_original_expr to get this expression. */
141 : tree expr;
142 : /* The array of conversions for an initializer_list, so this
143 : variant is used only when KIN D is ck_list. */
144 : conversion **list;
145 : } u;
146 : /* The function candidate corresponding to this conversion
147 : sequence. This field is only used if KIND is ck_user. */
148 : struct z_candidate *cand;
149 : };
150 :
151 : #define CONVERSION_RANK(NODE) \
152 : ((NODE)->bad_p ? cr_bad \
153 : : (NODE)->ellipsis_p ? cr_ellipsis \
154 : : (NODE)->user_conv_p ? cr_user \
155 : : (NODE)->rank)
156 :
157 : #define BAD_CONVERSION_RANK(NODE) \
158 : ((NODE)->ellipsis_p ? cr_ellipsis \
159 : : (NODE)->user_conv_p ? cr_user \
160 : : (NODE)->rank)
161 :
162 : static struct obstack conversion_obstack;
163 : static bool conversion_obstack_initialized;
164 : struct rejection_reason;
165 :
166 : static struct z_candidate * tourney (struct z_candidate *, tsubst_flags_t);
167 : static int equal_functions (tree, tree);
168 : static int joust (struct z_candidate *, struct z_candidate *, bool,
169 : tsubst_flags_t);
170 : static int compare_ics (conversion *, conversion *);
171 : static void maybe_warn_class_memaccess (location_t, tree,
172 : const vec<tree, va_gc> *);
173 : static tree build_over_call (struct z_candidate *, int, tsubst_flags_t);
174 : static tree convert_like (conversion *, tree, tsubst_flags_t);
175 : static tree convert_like_with_context (conversion *, tree, tree, int,
176 : tsubst_flags_t);
177 : static void op_error (const op_location_t &, enum tree_code, enum tree_code,
178 : tree, tree, tree, bool);
179 : static struct z_candidate *build_user_type_conversion_1 (tree, tree, int,
180 : tsubst_flags_t);
181 : static void print_z_candidate (location_t, const char *, struct z_candidate *);
182 : static void print_z_candidates (location_t, struct z_candidate *,
183 : tristate = tristate::unknown ());
184 : static tree build_this (tree);
185 : static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
186 : static bool any_strictly_viable (struct z_candidate *);
187 : static struct z_candidate *add_template_candidate
188 : (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
189 : tree, tree, tree, int, unification_kind_t, bool, tsubst_flags_t);
190 : static struct z_candidate *add_template_candidate_real
191 : (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
192 : tree, tree, tree, int, tree, unification_kind_t, bool, tsubst_flags_t);
193 : static bool is_complete (tree);
194 : static struct z_candidate *add_conv_candidate
195 : (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, tree,
196 : tree, tsubst_flags_t);
197 : static struct z_candidate *add_function_candidate
198 : (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *, tree,
199 : tree, int, conversion**, bool, tsubst_flags_t);
200 : static conversion *implicit_conversion (tree, tree, tree, bool, int,
201 : tsubst_flags_t);
202 : static conversion *reference_binding (tree, tree, tree, bool, int,
203 : tsubst_flags_t);
204 : static conversion *build_conv (conversion_kind, tree, conversion *);
205 : static conversion *build_list_conv (tree, tree, int, tsubst_flags_t);
206 : static conversion *next_conversion (conversion *);
207 : static bool is_subseq (conversion *, conversion *);
208 : static conversion *maybe_handle_ref_bind (conversion **);
209 : static void maybe_handle_implicit_object (conversion **);
210 : static struct z_candidate *add_candidate
211 : (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, size_t,
212 : conversion **, tree, tree, int, struct rejection_reason *, int);
213 : static tree source_type (conversion *);
214 : static void add_warning (struct z_candidate *, struct z_candidate *);
215 : static conversion *direct_reference_binding (tree, conversion *);
216 : static bool promoted_arithmetic_type_p (tree);
217 : static conversion *conditional_conversion (tree, tree, tsubst_flags_t);
218 : static char *name_as_c_string (tree, tree, bool *);
219 : static tree prep_operand (tree);
220 : static void add_candidates (tree, tree, const vec<tree, va_gc> *, tree, tree,
221 : bool, tree, tree, int, struct z_candidate **,
222 : tsubst_flags_t);
223 : static conversion *merge_conversion_sequences (conversion *, conversion *);
224 : static tree build_temp (tree, tree, int, enum diagnostics::kind *,
225 : tsubst_flags_t);
226 : static conversion *build_identity_conv (tree, tree);
227 : static inline bool conv_binds_to_array_of_unknown_bound (conversion *);
228 : static bool conv_is_prvalue (conversion *);
229 : static tree prevent_lifetime_extension (tree);
230 :
231 : /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
232 : NAME can take many forms... */
233 :
234 : bool
235 3021915 : check_dtor_name (tree basetype, tree name)
236 : {
237 : /* Just accept something we've already complained about. */
238 3021915 : if (name == error_mark_node)
239 : return true;
240 :
241 3021915 : if (TREE_CODE (name) == TYPE_DECL)
242 84 : name = TREE_TYPE (name);
243 3021831 : 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 3021889 : 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 471408456 : build_addr_func (tree function, tsubst_flags_t complain)
282 : {
283 471408456 : tree type = TREE_TYPE (function);
284 :
285 : /* We have to do these by hand to avoid real pointer to member
286 : functions. */
287 471408456 : if (TREE_CODE (type) == METHOD_TYPE)
288 : {
289 75564129 : if (TREE_CODE (function) == OFFSET_REF)
290 : {
291 71 : tree object = build_address (TREE_OPERAND (function, 0));
292 71 : return get_member_function_from_ptrfunc (&object,
293 71 : TREE_OPERAND (function, 1),
294 : complain);
295 : }
296 75564058 : function = build_address (function);
297 : }
298 395844327 : else if (TREE_CODE (function) == FUNCTION_DECL
299 560177018 : && DECL_IMMEDIATE_FUNCTION_P (function))
300 2455718 : function = build_address (function);
301 : else
302 393388609 : 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 160264 : build_call_n (tree function, int n, ...)
315 : {
316 160264 : if (n == 0)
317 0 : return build_call_a (function, 0, NULL);
318 : else
319 : {
320 160264 : tree *argarray = XALLOCAVEC (tree, n);
321 160264 : va_list ap;
322 160264 : int i;
323 :
324 160264 : va_start (ap, n);
325 321254 : for (i = 0; i < n; i++)
326 160990 : argarray[i] = va_arg (ap, tree);
327 160264 : va_end (ap);
328 160264 : 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 229488097 : set_flags_from_callee (tree call)
337 : {
338 : /* Handle both CALL_EXPRs and AGGR_INIT_EXPRs. */
339 229488097 : 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 229488097 : bool nothrow = decl && TREE_NOTHROW (decl);
344 229488097 : tree callee = cp_get_callee (call);
345 229488097 : if (callee)
346 229488089 : 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 229488097 : if (cfun && cp_function_chain && !cp_unevaluated_operand)
352 : {
353 123231654 : if (!nothrow && at_function_scope_p ())
354 34422678 : cp_function_chain->can_throw = 1;
355 :
356 123231654 : if (decl && TREE_THIS_VOLATILE (decl))
357 3753873 : current_function_returns_abnormally = 1;
358 : }
359 :
360 229488097 : TREE_NOTHROW (call) = nothrow;
361 229488097 : }
362 :
363 : tree
364 226019904 : build_call_a (tree function, int n, tree *argarray)
365 : {
366 226019904 : tree decl;
367 226019904 : tree result_type;
368 226019904 : tree fntype;
369 226019904 : int i;
370 :
371 226019904 : function = build_addr_func (function, tf_warning_or_error);
372 :
373 226019904 : gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
374 226019904 : fntype = TREE_TYPE (TREE_TYPE (function));
375 226019904 : gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype));
376 226019904 : result_type = TREE_TYPE (fntype);
377 : /* An rvalue has no cv-qualifiers. */
378 226019904 : if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
379 123079924 : result_type = cv_unqualified (result_type);
380 :
381 226019904 : function = build_call_array_loc (input_location,
382 : result_type, function, n, argarray);
383 226019904 : set_flags_from_callee (function);
384 :
385 226019904 : decl = get_callee_fndecl (function);
386 :
387 226019904 : 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 202784 : gcc_assert (DECL_ARTIFICIAL (decl)
394 : || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
395 : "__", 2));
396 202784 : mark_used (decl);
397 : }
398 :
399 226019904 : require_complete_eh_spec_types (fntype, decl);
400 :
401 654724898 : 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 226019904 : if (!decl || !fndecl_built_in_p (decl))
407 455690139 : for (i = 0; i < n; i++)
408 : {
409 238248791 : tree arg = CALL_EXPR_ARG (function, i);
410 238248791 : if (is_empty_class (TREE_TYPE (arg))
411 238248791 : && simple_empty_class_p (TREE_TYPE (arg), arg, INIT_EXPR))
412 : {
413 5802762 : while (TREE_CODE (arg) == TARGET_EXPR)
414 : /* We're disconnecting the initializer from its target,
415 : don't create a temporary. */
416 2900452 : arg = TARGET_EXPR_INITIAL (arg);
417 2902310 : suppress_warning (arg, OPT_Wunused_result);
418 2902310 : tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg));
419 2902310 : arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t);
420 2902310 : CALL_EXPR_ARG (function, i) = arg;
421 : }
422 : }
423 :
424 226019904 : 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 86112894 : bool rewritten () const { return (flags & LOOKUP_REWRITTEN); }
543 1477393258 : 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 163894196 : null_ptr_cst_p (tree t)
551 : {
552 163894196 : 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 163894196 : if (NULLPTR_TYPE_P (type))
559 : return true;
560 :
561 154344671 : if (cxx_dialect >= cxx11)
562 : {
563 153970658 : STRIP_ANY_LOCATION_WRAPPER (t);
564 :
565 : /* Core issue 903 says only literal 0 is a null pointer constant. */
566 153970658 : if (TREE_CODE (t) == INTEGER_CST
567 16222138 : && !TREE_OVERFLOW (t)
568 16222138 : && TREE_CODE (type) == INTEGER_TYPE
569 15645917 : && integer_zerop (t)
570 163200438 : && !char_type_p (type))
571 : return true;
572 : }
573 374013 : else if (CP_INTEGRAL_TYPE_P (type))
574 : {
575 71367 : t = fold_non_dependent_expr (t, tf_none);
576 71367 : STRIP_NOPS (t);
577 71367 : if (integer_zerop (t) && !TREE_OVERFLOW (t))
578 : return true;
579 : }
580 :
581 : return false;
582 : }
583 :
584 : /* Returns true iff T is a null member pointer value (4.11). */
585 :
586 : bool
587 102345643 : null_member_pointer_value_p (tree t)
588 : {
589 102345643 : tree type = TREE_TYPE (t);
590 102345643 : if (!type)
591 : return false;
592 102150376 : else if (TYPE_PTRMEMFUNC_P (type))
593 751 : return (TREE_CODE (t) == CONSTRUCTOR
594 401 : && CONSTRUCTOR_NELTS (t)
595 1149 : && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value));
596 102149625 : else if (TYPE_PTRDATAMEM_P (type))
597 5700 : 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 904312868 : sufficient_parms_p (const_tree parmlist)
607 : {
608 915817591 : for (; parmlist && parmlist != void_list_node;
609 11504723 : parmlist = TREE_CHAIN (parmlist))
610 250376592 : if (!TREE_PURPOSE (parmlist)
611 250376592 : && !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 10439991837 : conversion_obstack_alloc (size_t n)
621 : {
622 10439991837 : void *p;
623 10439991837 : if (!conversion_obstack_initialized)
624 : {
625 83528 : gcc_obstack_init (&conversion_obstack);
626 83528 : conversion_obstack_initialized = true;
627 : }
628 10439991837 : p = obstack_alloc (&conversion_obstack, n);
629 10439991837 : memset (p, 0, n);
630 10439991837 : 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 2858873526 : conversion_obstack_sentinel (): p (conversion_obstack_alloc (0)) {}
639 1429423209 : ~conversion_obstack_sentinel () { obstack_free (&conversion_obstack, p); }
640 : };
641 :
642 : /* Allocate rejection reasons. */
643 :
644 : static struct rejection_reason *
645 1105348157 : alloc_rejection (enum rejection_reason_code code)
646 : {
647 1105348157 : struct rejection_reason *p;
648 0 : p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
649 1105348157 : p->code = code;
650 1105348157 : return p;
651 : }
652 :
653 : static struct rejection_reason *
654 262109820 : arity_rejection (tree first_arg, int expected, int actual, bool least_p = false)
655 : {
656 212916373 : struct rejection_reason *r = alloc_rejection (rr_arity);
657 262109820 : int adjust = first_arg != NULL_TREE;
658 262109820 : r->u.arity.expected = expected - adjust;
659 262109820 : r->u.arity.actual = actual - adjust;
660 262109820 : r->u.arity.least_p = least_p;
661 262109820 : return r;
662 : }
663 :
664 : static struct rejection_reason *
665 223483594 : arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to,
666 : location_t loc)
667 : {
668 7969400 : struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
669 223483594 : int adjust = first_arg != NULL_TREE;
670 223483594 : r->u.conversion.n_arg = n_arg - adjust;
671 223483594 : r->u.conversion.from = from;
672 223483594 : r->u.conversion.to_type = to;
673 223483594 : r->u.conversion.loc = loc;
674 223483594 : return r;
675 : }
676 :
677 : static struct rejection_reason *
678 25828419 : bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to,
679 : location_t loc)
680 : {
681 1674 : struct rejection_reason *r = alloc_rejection (rr_bad_arg_conversion);
682 25828419 : int adjust = first_arg != NULL_TREE;
683 25828419 : r->u.bad_conversion.n_arg = n_arg - adjust;
684 25828419 : r->u.bad_conversion.from = from;
685 25828419 : r->u.bad_conversion.to_type = to;
686 25828419 : r->u.bad_conversion.loc = loc;
687 25828419 : return r;
688 : }
689 :
690 : static struct rejection_reason *
691 2567 : explicit_conversion_rejection (tree from, tree to)
692 : {
693 2567 : struct rejection_reason *r = alloc_rejection (rr_explicit_conversion);
694 2567 : r->u.conversion.n_arg = 0;
695 2567 : r->u.conversion.from = from;
696 2567 : r->u.conversion.to_type = to;
697 2567 : r->u.conversion.loc = UNKNOWN_LOCATION;
698 2567 : 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 591145980 : 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 591145980 : size_t args_n_bytes = sizeof (*args) * nargs;
719 591145980 : tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes);
720 591145980 : struct rejection_reason *r = alloc_rejection (rr_template_unification);
721 591145980 : r->u.template_unification.tmpl = tmpl;
722 591145980 : r->u.template_unification.explicit_targs = explicit_targs;
723 591145980 : r->u.template_unification.num_targs = TREE_VEC_LENGTH (targs);
724 : /* Copy args to our own storage. */
725 591145980 : memcpy (args1, args, args_n_bytes);
726 591145980 : r->u.template_unification.args = args1;
727 591145980 : r->u.template_unification.nargs = nargs;
728 591145980 : r->u.template_unification.return_type = return_type;
729 591145980 : r->u.template_unification.strict = strict;
730 591145980 : r->u.template_unification.flags = flags;
731 591145980 : return r;
732 : }
733 :
734 : static struct rejection_reason *
735 110 : template_unification_error_rejection (void)
736 : {
737 0 : return alloc_rejection (rr_template_unification);
738 : }
739 :
740 : static struct rejection_reason *
741 2008577 : invalid_copy_with_fn_template_rejection (void)
742 : {
743 0 : struct rejection_reason *r = alloc_rejection (rr_invalid_copy);
744 2008577 : return r;
745 : }
746 :
747 : static struct rejection_reason *
748 542969 : inherited_ctor_rejection (void)
749 : {
750 0 : struct rejection_reason *r = alloc_rejection (rr_inherited_ctor);
751 542969 : return r;
752 : }
753 :
754 : /* Build a constraint failure record. */
755 :
756 : static struct rejection_reason *
757 226100 : constraint_failure (void)
758 : {
759 0 : struct rejection_reason *r = alloc_rejection (rr_constraint_failure);
760 226100 : return r;
761 : }
762 :
763 : /* Dynamically allocate a conversion. */
764 :
765 : static conversion *
766 3353105806 : alloc_conversion (conversion_kind kind)
767 : {
768 3353105806 : conversion *c;
769 0 : c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
770 3353105806 : c->kind = kind;
771 3353105806 : return c;
772 : }
773 :
774 : /* Make sure that all memory on the conversion obstack has been
775 : freed. */
776 :
777 : void
778 95534 : validate_conversion_obstack (void)
779 : {
780 95534 : if (conversion_obstack_initialized)
781 83198 : gcc_assert ((obstack_next_free (&conversion_obstack)
782 : == obstack_base (&conversion_obstack)));
783 95534 : }
784 :
785 : /* Dynamically allocate an array of N conversions. */
786 :
787 : static conversion **
788 1438724517 : 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 2031119401 : has_next (conversion_kind code)
797 : {
798 1862458216 : return !(code == ck_identity
799 2031119401 : || code == ck_ambig
800 : || code == ck_list
801 1862608761 : || code == ck_aggr
802 2031119401 : || code == ck_deferred_bad);
803 : }
804 :
805 : static conversion *
806 1006395235 : build_conv (conversion_kind code, tree type, conversion *from)
807 : {
808 1006395235 : conversion *t;
809 1006395235 : conversion_rank rank = CONVERSION_RANK (from);
810 :
811 : /* Only call this function for conversions that use u.next. */
812 1006395235 : 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 1006395235 : t = alloc_conversion (code);
817 1006395235 : t->type = type;
818 1006395235 : t->u.next = from;
819 :
820 1006395235 : switch (code)
821 : {
822 252117929 : case ck_ptr:
823 252117929 : case ck_pmem:
824 252117929 : case ck_base:
825 252117929 : case ck_std:
826 252117929 : if (rank < cr_std)
827 1006395235 : rank = cr_std;
828 : break;
829 :
830 74198727 : case ck_qual:
831 74198727 : case ck_fnptr:
832 74198727 : if (rank < cr_exact)
833 1006395235 : rank = cr_exact;
834 : break;
835 :
836 : default:
837 : break;
838 : }
839 1006395235 : t->rank = rank;
840 1006395235 : t->user_conv_p = (code == ck_user || from->user_conv_p);
841 1006395235 : t->bad_p = from->bad_p;
842 1006395235 : t->base_p = false;
843 1006395235 : 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 81136 : build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
852 : {
853 81136 : tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
854 81136 : unsigned len = CONSTRUCTOR_NELTS (ctor);
855 81136 : conversion **subconvs = alloc_conversions (len);
856 81136 : conversion *t;
857 81136 : unsigned i;
858 81136 : tree val;
859 :
860 : /* Within a list-initialization we can have more user-defined
861 : conversions. */
862 81136 : flags &= ~LOOKUP_NO_CONVERSION;
863 : /* But no narrowing conversions. */
864 81136 : flags |= LOOKUP_NO_NARROWING;
865 :
866 : /* Can't make an array of these types. */
867 81136 : if (TYPE_REF_P (elttype)
868 81127 : || TREE_CODE (elttype) == FUNCTION_TYPE
869 81127 : || VOID_TYPE_P (elttype))
870 : return NULL;
871 :
872 279653 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
873 : {
874 209642 : 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 209591 : conversion *sub
936 209591 : = implicit_conversion (elttype, TREE_TYPE (val), val,
937 : false, flags, complain);
938 209591 : if (sub == NULL)
939 : return NULL;
940 :
941 198475 : subconvs[i] = sub;
942 : }
943 :
944 70011 : t = alloc_conversion (ck_list);
945 70011 : t->type = type;
946 70011 : t->u.list = subconvs;
947 70011 : t->rank = cr_exact;
948 :
949 250529 : for (i = 0; i < len; ++i)
950 : {
951 180518 : conversion *sub = subconvs[i];
952 180518 : if (sub->rank > t->rank)
953 58453 : t->rank = sub->rank;
954 180518 : if (sub->user_conv_p)
955 10603 : t->user_conv_p = true;
956 180518 : if (sub->bad_p)
957 58937 : 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 852488910 : next_conversion (conversion *conv)
969 : {
970 852488910 : if (conv == NULL
971 852488910 : || !has_next (conv->kind))
972 : return NULL;
973 838717482 : 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 1395939 : strip_standard_conversion (conversion *conv)
981 : {
982 1395939 : while (conv
983 1400025 : && conv->kind != ck_user
984 1480117 : && has_next (conv->kind))
985 4086 : conv = next_conversion (conv);
986 1395939 : 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 74418 : can_convert_array (tree atype, tree from, int flags, tsubst_flags_t complain)
994 : {
995 74418 : tree elttype = TREE_TYPE (atype);
996 74418 : unsigned i;
997 :
998 74418 : if (TREE_CODE (from) == CONSTRUCTOR)
999 : {
1000 77354 : for (i = 0; i < CONSTRUCTOR_NELTS (from); ++i)
1001 : {
1002 2981 : tree val = CONSTRUCTOR_ELT (from, i)->value;
1003 2981 : bool ok;
1004 2981 : if (TREE_CODE (elttype) == ARRAY_TYPE)
1005 18 : ok = can_convert_array (elttype, val, flags, complain);
1006 : else
1007 2963 : ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags,
1008 : complain);
1009 2981 : if (!ok)
1010 : return false;
1011 : }
1012 : return true;
1013 : }
1014 :
1015 45 : if (char_type_p (TYPE_MAIN_VARIANT (elttype))
1016 45 : && TREE_CODE (tree_strip_any_location_wrapper (from)) == STRING_CST)
1017 45 : return array_string_literal_compatible_p (atype, from);
1018 :
1019 : /* No other valid way to aggregate initialize an array. */
1020 : return false;
1021 : }
1022 :
1023 : /* Helper for build_aggr_conv. Return true if FIELD is in PSET, or if
1024 : FIELD has ANON_AGGR_TYPE_P and any initializable field in there recursively
1025 : is in PSET. */
1026 :
1027 : static bool
1028 836404 : field_in_pset (hash_set<tree, true> &pset, tree field)
1029 : {
1030 836404 : if (pset.contains (field))
1031 : return true;
1032 944 : if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1033 0 : for (field = TYPE_FIELDS (TREE_TYPE (field));
1034 0 : field; field = DECL_CHAIN (field))
1035 : {
1036 0 : field = next_aggregate_field (field);
1037 0 : if (field == NULL_TREE)
1038 : break;
1039 0 : if (field_in_pset (pset, field))
1040 : return true;
1041 : }
1042 : return false;
1043 : }
1044 :
1045 : /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
1046 : aggregate class, if such a conversion is possible. */
1047 :
1048 : static conversion *
1049 1355878 : build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
1050 : {
1051 1355878 : unsigned HOST_WIDE_INT i = 0;
1052 1355878 : conversion *c;
1053 1355878 : tree field = next_aggregate_field (TYPE_FIELDS (type));
1054 1355878 : tree empty_ctor = NULL_TREE;
1055 1355878 : hash_set<tree, true> pset;
1056 :
1057 : /* We already called reshape_init in implicit_conversion, but it might not
1058 : have done anything in the case of parenthesized aggr init. */
1059 :
1060 : /* The conversions within the init-list aren't affected by the enclosing
1061 : context; they're always simple copy-initialization. */
1062 1355878 : flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1063 :
1064 : /* For designated initializers, verify that each initializer is convertible
1065 : to corresponding TREE_TYPE (ce->index) and mark those FIELD_DECLs as
1066 : visited. In the following loop then ignore already visited
1067 : FIELD_DECLs. */
1068 1355878 : tree idx, val;
1069 2191338 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, val)
1070 : {
1071 835535 : if (!idx)
1072 : break;
1073 :
1074 835492 : gcc_checking_assert (TREE_CODE (idx) == FIELD_DECL);
1075 :
1076 835492 : tree ftype = TREE_TYPE (idx);
1077 835492 : bool ok;
1078 :
1079 835492 : if (TREE_CODE (ftype) == ARRAY_TYPE)
1080 1055 : ok = can_convert_array (ftype, val, flags, complain);
1081 : else
1082 834437 : ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
1083 : complain);
1084 :
1085 835492 : if (!ok)
1086 : return NULL;
1087 :
1088 : /* For unions, there should be just one initializer. */
1089 835483 : if (TREE_CODE (type) == UNION_TYPE)
1090 : {
1091 : field = NULL_TREE;
1092 : i = 1;
1093 : break;
1094 : }
1095 835460 : pset.add (idx);
1096 : }
1097 :
1098 2286138 : for (; field; field = next_aggregate_field (DECL_CHAIN (field)))
1099 : {
1100 930526 : tree ftype = TREE_TYPE (field);
1101 930526 : bool ok;
1102 :
1103 930526 : if (!pset.is_empty () && field_in_pset (pset, field))
1104 835460 : continue;
1105 95066 : if (i < CONSTRUCTOR_NELTS (ctor))
1106 : {
1107 29 : constructor_elt *ce = CONSTRUCTOR_ELT (ctor, i);
1108 29 : gcc_checking_assert (!ce->index);
1109 29 : val = ce->value;
1110 29 : ++i;
1111 : }
1112 95037 : else if (DECL_INITIAL (field))
1113 801 : val = get_nsdmi (field, /*ctor*/false, complain);
1114 94236 : else if (TYPE_REF_P (ftype))
1115 : /* Value-initialization of reference is ill-formed. */
1116 : return NULL;
1117 : else
1118 : {
1119 94230 : if (empty_ctor == NULL_TREE)
1120 84312 : empty_ctor = build_constructor (init_list_type_node, NULL);
1121 : val = empty_ctor;
1122 : }
1123 :
1124 95060 : if (TREE_CODE (ftype) == ARRAY_TYPE)
1125 73345 : ok = can_convert_array (ftype, val, flags, complain);
1126 : else
1127 21715 : ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
1128 : complain);
1129 :
1130 95060 : if (!ok)
1131 : return NULL;
1132 :
1133 95049 : if (TREE_CODE (type) == UNION_TYPE)
1134 : break;
1135 : }
1136 :
1137 1355852 : if (i < CONSTRUCTOR_NELTS (ctor))
1138 : return NULL;
1139 :
1140 1355833 : c = alloc_conversion (ck_aggr);
1141 1355833 : c->type = type;
1142 1355833 : c->rank = cr_exact;
1143 1355833 : c->user_conv_p = true;
1144 1355833 : c->check_narrowing = true;
1145 1355833 : c->u.expr = ctor;
1146 1355833 : return c;
1147 1355878 : }
1148 :
1149 : /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
1150 : array type, if such a conversion is possible. */
1151 :
1152 : static conversion *
1153 1652 : build_array_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
1154 : {
1155 1652 : conversion *c;
1156 1652 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1157 1652 : tree elttype = TREE_TYPE (type);
1158 1652 : bool bad = false;
1159 1652 : bool user = false;
1160 1652 : enum conversion_rank rank = cr_exact;
1161 :
1162 : /* We might need to propagate the size from the element to the array. */
1163 1652 : complete_type (type);
1164 :
1165 1652 : if (TYPE_DOMAIN (type)
1166 1652 : && !variably_modified_type_p (TYPE_DOMAIN (type), NULL_TREE))
1167 : {
1168 1578 : unsigned HOST_WIDE_INT alen = tree_to_uhwi (array_type_nelts_top (type));
1169 1578 : if (alen < len)
1170 : return NULL;
1171 : }
1172 :
1173 1615 : flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1174 :
1175 6940 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
1176 : {
1177 2758 : conversion *sub
1178 2758 : = implicit_conversion (elttype, TREE_TYPE (e.value), e.value,
1179 : false, flags, complain);
1180 2758 : if (sub == NULL)
1181 : return NULL;
1182 :
1183 2181 : if (sub->rank > rank)
1184 264 : rank = sub->rank;
1185 2181 : if (sub->user_conv_p)
1186 23 : user = true;
1187 2181 : if (sub->bad_p)
1188 195 : bad = true;
1189 : }
1190 :
1191 1038 : c = alloc_conversion (ck_aggr);
1192 1038 : c->type = type;
1193 1038 : c->rank = rank;
1194 1038 : c->user_conv_p = user;
1195 1038 : c->bad_p = bad;
1196 1038 : c->u.expr = ctor;
1197 1038 : return c;
1198 : }
1199 :
1200 : /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
1201 : complex type, if such a conversion is possible. */
1202 :
1203 : static conversion *
1204 54502 : build_complex_conv (tree type, tree ctor, int flags,
1205 : tsubst_flags_t complain)
1206 : {
1207 54502 : conversion *c;
1208 54502 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1209 54502 : tree elttype = TREE_TYPE (type);
1210 54502 : bool bad = false;
1211 54502 : bool user = false;
1212 54502 : enum conversion_rank rank = cr_exact;
1213 :
1214 54502 : if (len != 2)
1215 : return NULL;
1216 :
1217 54462 : flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1218 :
1219 272310 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
1220 : {
1221 108924 : conversion *sub
1222 108924 : = implicit_conversion (elttype, TREE_TYPE (e.value), e.value,
1223 : false, flags, complain);
1224 108924 : if (sub == NULL)
1225 : return NULL;
1226 :
1227 108924 : if (sub->rank > rank)
1228 44 : rank = sub->rank;
1229 108924 : if (sub->user_conv_p)
1230 0 : user = true;
1231 108924 : if (sub->bad_p)
1232 0 : bad = true;
1233 : }
1234 :
1235 54462 : c = alloc_conversion (ck_aggr);
1236 54462 : c->type = type;
1237 54462 : c->rank = rank;
1238 54462 : c->user_conv_p = user;
1239 54462 : c->bad_p = bad;
1240 54462 : c->u.expr = ctor;
1241 54462 : return c;
1242 : }
1243 :
1244 : /* Build a representation of the identity conversion from EXPR to
1245 : itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
1246 :
1247 : static conversion *
1248 2337515282 : build_identity_conv (tree type, tree expr)
1249 : {
1250 2337515282 : conversion *c;
1251 :
1252 0 : c = alloc_conversion (ck_identity);
1253 2337515282 : c->type = type;
1254 2337515282 : c->u.expr = expr;
1255 :
1256 2337515282 : return c;
1257 : }
1258 :
1259 : /* Converting from EXPR to TYPE was ambiguous in the sense that there
1260 : were multiple user-defined conversions to accomplish the job.
1261 : Build a conversion that indicates that ambiguity. */
1262 :
1263 : static conversion *
1264 1338 : build_ambiguous_conv (tree type, tree expr)
1265 : {
1266 1338 : conversion *c;
1267 :
1268 0 : c = alloc_conversion (ck_ambig);
1269 1338 : c->type = type;
1270 1338 : c->u.expr = expr;
1271 :
1272 1338 : return c;
1273 : }
1274 :
1275 : tree
1276 4046241574 : strip_top_quals (tree t)
1277 : {
1278 4046241574 : if (TREE_CODE (t) == ARRAY_TYPE)
1279 : return t;
1280 4038351453 : return cp_build_qualified_type (t, 0);
1281 : }
1282 :
1283 : /* Returns the standard conversion path (see [conv]) from type FROM to type
1284 : TO, if any. For proper handling of null pointer constants, you must
1285 : also pass the expression EXPR to convert from. If C_CAST_P is true,
1286 : this conversion is coming from a C-style cast. */
1287 :
1288 : static conversion *
1289 1826444880 : standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
1290 : int flags, tsubst_flags_t complain)
1291 : {
1292 1826444880 : enum tree_code fcode, tcode;
1293 1826444880 : conversion *conv;
1294 1826444880 : bool fromref = false;
1295 1826444880 : tree qualified_to;
1296 :
1297 1826444880 : to = non_reference (to);
1298 1826444880 : if (TYPE_REF_P (from))
1299 : {
1300 75569 : fromref = true;
1301 75569 : from = TREE_TYPE (from);
1302 : }
1303 1826444880 : qualified_to = to;
1304 1826444880 : to = strip_top_quals (to);
1305 1826444880 : from = strip_top_quals (from);
1306 :
1307 1826444880 : if (expr && type_unknown_p (expr))
1308 : {
1309 306123 : if (TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
1310 : {
1311 33478 : tsubst_flags_t tflags = tf_conv;
1312 33478 : expr = instantiate_type (to, expr, tflags);
1313 33478 : if (expr == error_mark_node)
1314 : return NULL;
1315 20604 : from = TREE_TYPE (expr);
1316 : }
1317 272645 : else if (TREE_CODE (to) == BOOLEAN_TYPE)
1318 : {
1319 : /* Necessary for eg, TEMPLATE_ID_EXPRs (c++/50961). */
1320 6236 : expr = resolve_nondeduced_context (expr, complain);
1321 6236 : from = TREE_TYPE (expr);
1322 : }
1323 : }
1324 :
1325 1826432006 : fcode = TREE_CODE (from);
1326 1826432006 : tcode = TREE_CODE (to);
1327 :
1328 1826432006 : conv = build_identity_conv (from, expr);
1329 1826432006 : if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
1330 : {
1331 7907853 : from = type_decays_to (from);
1332 7907853 : fcode = TREE_CODE (from);
1333 : /* Tell convert_like that we're using the address. */
1334 7907853 : conv->rvaluedness_matches_p = true;
1335 7907853 : conv = build_conv (ck_lvalue, from, conv);
1336 : }
1337 : /* Wrapping a ck_rvalue around a class prvalue (as a result of using
1338 : obvalue_p) seems odd, since it's already a prvalue, but that's how we
1339 : express the copy constructor call required by copy-initialization. */
1340 1818524153 : else if (fromref || (expr && obvalue_p (expr)))
1341 : {
1342 475638746 : if (expr)
1343 : {
1344 475563200 : tree bitfield_type;
1345 475563200 : bitfield_type = is_bitfield_expr_with_lowered_type (expr);
1346 475563200 : if (bitfield_type)
1347 : {
1348 17358070 : from = strip_top_quals (bitfield_type);
1349 17358070 : fcode = TREE_CODE (from);
1350 : }
1351 : }
1352 475638746 : conv = build_conv (ck_rvalue, from, conv);
1353 : /* If we're performing copy-initialization, remember to skip
1354 : explicit constructors. */
1355 475638746 : if (flags & LOOKUP_ONLYCONVERTING)
1356 424274744 : conv->copy_init_p = true;
1357 : }
1358 :
1359 : /* Allow conversion between `__complex__' data types. */
1360 1826432006 : if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
1361 : {
1362 : /* The standard conversion sequence to convert FROM to TO is
1363 : the standard conversion sequence to perform componentwise
1364 : conversion. */
1365 2199515 : conversion *part_conv = standard_conversion
1366 2199515 : (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags,
1367 : complain);
1368 :
1369 2199515 : if (!part_conv)
1370 : conv = NULL;
1371 2199515 : else if (part_conv->kind == ck_identity)
1372 : /* Leave conv alone. */;
1373 : else
1374 : {
1375 220044 : conv = build_conv (part_conv->kind, to, conv);
1376 220044 : conv->rank = part_conv->rank;
1377 : }
1378 :
1379 2199515 : return conv;
1380 : }
1381 :
1382 1824232491 : if (same_type_p (from, to))
1383 : {
1384 1086663002 : if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue)
1385 24761814 : conv->type = qualified_to;
1386 1061901188 : else if (from != to)
1387 : /* Use TO in order to not lose TO in diagnostics. */
1388 94999275 : conv->type = to;
1389 1086663002 : return conv;
1390 : }
1391 :
1392 : /* [conv.ptr]
1393 : A null pointer constant can be converted to a pointer type; ... A
1394 : null pointer constant of integral type can be converted to an
1395 : rvalue of type std::nullptr_t. */
1396 456297785 : if ((tcode == POINTER_TYPE || TYPE_PTRMEM_P (to)
1397 456211537 : || NULLPTR_TYPE_P (to))
1398 745744137 : && ((expr && null_ptr_cst_p (expr))
1399 283377937 : || NULLPTR_TYPE_P (from)))
1400 6068419 : conv = build_conv (ck_std, to, conv);
1401 731501070 : else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
1402 730813328 : || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
1403 : {
1404 : /* For backwards brain damage compatibility, allow interconversion of
1405 : pointers and integers with a pedwarn. */
1406 2641287 : conv = build_conv (ck_std, to, conv);
1407 2641287 : conv->bad_p = true;
1408 : }
1409 728859783 : else if (UNSCOPED_ENUM_P (to) && fcode == INTEGER_TYPE)
1410 : {
1411 : /* For backwards brain damage compatibility, allow interconversion of
1412 : enums and integers with a pedwarn. */
1413 345733 : conv = build_conv (ck_std, to, conv);
1414 345733 : conv->bad_p = true;
1415 : }
1416 728514050 : else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
1417 463063390 : || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from)))
1418 : {
1419 1700 : tree to_pointee;
1420 1700 : tree from_pointee;
1421 :
1422 1700 : if (tcode == POINTER_TYPE)
1423 : {
1424 265450660 : to_pointee = TREE_TYPE (to);
1425 265450660 : from_pointee = TREE_TYPE (from);
1426 :
1427 : /* Since this is the target of a pointer, it can't have function
1428 : qualifiers, so any TYPE_QUALS must be for attributes const or
1429 : noreturn. Strip them. */
1430 265450660 : if (TREE_CODE (to_pointee) == FUNCTION_TYPE
1431 265450660 : && TYPE_QUALS (to_pointee))
1432 9 : to_pointee = build_qualified_type (to_pointee, TYPE_UNQUALIFIED);
1433 265450660 : if (TREE_CODE (from_pointee) == FUNCTION_TYPE
1434 265450660 : && TYPE_QUALS (from_pointee))
1435 36 : from_pointee = build_qualified_type (from_pointee, TYPE_UNQUALIFIED);
1436 : }
1437 : else
1438 : {
1439 1700 : to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
1440 1700 : from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
1441 : }
1442 :
1443 265452360 : if (tcode == POINTER_TYPE
1444 265452360 : && same_type_ignoring_top_level_qualifiers_p (from_pointee,
1445 : to_pointee))
1446 : ;
1447 185364731 : else if (VOID_TYPE_P (to_pointee)
1448 5181533 : && !TYPE_PTRDATAMEM_P (from)
1449 5181533 : && TREE_CODE (from_pointee) != FUNCTION_TYPE)
1450 : {
1451 5181278 : tree nfrom = TREE_TYPE (from);
1452 : /* Don't try to apply restrict to void. */
1453 5181278 : int quals = cp_type_quals (nfrom) & ~TYPE_QUAL_RESTRICT;
1454 5181278 : from_pointee = cp_build_qualified_type (void_type_node, quals);
1455 5181278 : from = build_pointer_type (from_pointee);
1456 5181278 : conv = build_conv (ck_ptr, from, conv);
1457 5181278 : }
1458 180183453 : else if (TYPE_PTRDATAMEM_P (from))
1459 : {
1460 1700 : tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
1461 1700 : tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
1462 :
1463 1700 : if (same_type_p (fbase, tbase))
1464 : /* No base conversion needed. */;
1465 589 : else if (DERIVED_FROM_P (fbase, tbase)
1466 990 : && (same_type_ignoring_top_level_qualifiers_p
1467 401 : (from_pointee, to_pointee)))
1468 : {
1469 377 : from = build_ptrmem_type (tbase, from_pointee);
1470 377 : conv = build_conv (ck_pmem, from, conv);
1471 : }
1472 : else
1473 212 : return NULL;
1474 : }
1475 84021868 : else if (CLASS_TYPE_P (from_pointee)
1476 84019505 : && CLASS_TYPE_P (to_pointee)
1477 : /* [conv.ptr]
1478 :
1479 : An rvalue of type "pointer to cv D," where D is a
1480 : class type, can be converted to an rvalue of type
1481 : "pointer to cv B," where B is a base class (clause
1482 : _class.derived_) of D. If B is an inaccessible
1483 : (clause _class.access_) or ambiguous
1484 : (_class.member.lookup_) base class of D, a program
1485 : that necessitates this conversion is ill-formed.
1486 : Therefore, we use DERIVED_FROM_P, and do not check
1487 : access or uniqueness. */
1488 263015130 : && DERIVED_FROM_P (to_pointee, from_pointee))
1489 : {
1490 7232465 : from_pointee
1491 7232465 : = cp_build_qualified_type (to_pointee,
1492 : cp_type_quals (from_pointee));
1493 7232465 : from = build_pointer_type (from_pointee);
1494 7232465 : conv = build_conv (ck_ptr, from, conv);
1495 7232465 : conv->base_p = true;
1496 : }
1497 :
1498 265452148 : if (same_type_p (from, to))
1499 : /* OK */;
1500 255797743 : else if (c_cast_p && comp_ptr_ttypes_const (to, from, bounds_either))
1501 : /* In a C-style cast, we ignore CV-qualification because we
1502 : are allowed to perform a static_cast followed by a
1503 : const_cast. */
1504 623 : conv = build_conv (ck_qual, to, conv);
1505 255797120 : else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
1506 73905524 : conv = build_conv (ck_qual, to, conv);
1507 181891596 : else if (expr && string_conv_p (to, expr, 0))
1508 : /* converting from string constant to char *. */
1509 204 : conv = build_conv (ck_qual, to, conv);
1510 181891392 : else if (fnptr_conv_p (to, from))
1511 222948 : conv = build_conv (ck_fnptr, to, conv);
1512 : /* Allow conversions among compatible ObjC pointer types (base
1513 : conversions have been already handled above). */
1514 181668444 : else if (c_dialect_objc ()
1515 181668444 : && objc_compare_types (to, from, -4, NULL_TREE))
1516 0 : conv = build_conv (ck_ptr, to, conv);
1517 181668444 : else if (ptr_reasonably_similar (to_pointee, from_pointee))
1518 : {
1519 10300901 : conv = build_conv (ck_ptr, to, conv);
1520 10300901 : conv->bad_p = true;
1521 : }
1522 : else
1523 : return NULL;
1524 :
1525 308951487 : from = to;
1526 : }
1527 463061690 : else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
1528 : {
1529 82631 : tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
1530 82631 : tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
1531 82631 : tree fbase = class_of_this_parm (fromfn);
1532 82631 : tree tbase = class_of_this_parm (tofn);
1533 :
1534 : /* If FBASE and TBASE are equivalent but incomplete, DERIVED_FROM_P
1535 : yields false. But a pointer to member of incomplete class is OK. */
1536 82631 : if (!same_type_p (fbase, tbase) && !DERIVED_FROM_P (fbase, tbase))
1537 : return NULL;
1538 :
1539 82470 : tree fstat = static_fn_type (fromfn);
1540 82470 : tree tstat = static_fn_type (tofn);
1541 82470 : if (same_type_p (tstat, fstat)
1542 82470 : || fnptr_conv_p (tstat, fstat))
1543 : /* OK */;
1544 : else
1545 : return NULL;
1546 :
1547 82202 : if (!same_type_p (fbase, tbase))
1548 : {
1549 82133 : from = build_memfn_type (fstat,
1550 : tbase,
1551 : cp_type_quals (tbase),
1552 : type_memfn_rqual (tofn));
1553 82133 : from = build_ptrmemfunc_type (build_pointer_type (from));
1554 82133 : conv = build_conv (ck_pmem, from, conv);
1555 82133 : conv->base_p = true;
1556 : }
1557 82202 : if (fnptr_conv_p (tstat, fstat))
1558 69 : conv = build_conv (ck_fnptr, to, conv);
1559 : }
1560 462979059 : else if (tcode == BOOLEAN_TYPE)
1561 : {
1562 : /* [conv.bool]
1563 :
1564 : A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
1565 : to member type can be converted to a prvalue of type bool. ...
1566 : For direct-initialization (8.5 [dcl.init]), a prvalue of type
1567 : std::nullptr_t can be converted to a prvalue of type bool; */
1568 6980202 : if (ARITHMETIC_TYPE_P (from)
1569 6792653 : || UNSCOPED_ENUM_P (from)
1570 4433149 : || fcode == POINTER_TYPE
1571 3142284 : || TYPE_PTRMEM_P (from)
1572 16671241 : || NULLPTR_TYPE_P (from))
1573 : {
1574 10387192 : conv = build_conv (ck_std, to, conv);
1575 10387192 : if (fcode == POINTER_TYPE
1576 9096327 : || TYPE_PTRDATAMEM_P (from)
1577 9096183 : || (TYPE_PTRMEMFUNC_P (from)
1578 77 : && conv->rank < cr_pbool)
1579 19483298 : || NULLPTR_TYPE_P (from))
1580 1291163 : conv->rank = cr_pbool;
1581 10387192 : if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING))
1582 38 : conv->bad_p = true;
1583 10387192 : if (flags & LOOKUP_NO_NARROWING)
1584 30701 : conv->check_narrowing = true;
1585 10387192 : return conv;
1586 : }
1587 :
1588 : return NULL;
1589 : }
1590 : /* We don't check for ENUMERAL_TYPE here because there are no standard
1591 : conversions to enum type. */
1592 : /* As an extension, allow conversion to complex type. */
1593 449449881 : else if (ARITHMETIC_TYPE_P (to))
1594 : {
1595 32481996 : if (! (INTEGRAL_CODE_P (fcode)
1596 25144902 : || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL)))
1597 239927002 : || SCOPED_ENUM_P (from))
1598 : return NULL;
1599 :
1600 : /* If we're parsing an enum with no fixed underlying type, we're
1601 : dealing with an incomplete type, which renders the conversion
1602 : ill-formed. */
1603 205223056 : if (!COMPLETE_TYPE_P (from))
1604 : return NULL;
1605 :
1606 205223050 : conv = build_conv (ck_std, to, conv);
1607 :
1608 205223050 : tree underlying_type = NULL_TREE;
1609 205223050 : if (TREE_CODE (from) == ENUMERAL_TYPE
1610 205223050 : && ENUM_FIXED_UNDERLYING_TYPE_P (from))
1611 2013179 : underlying_type = ENUM_UNDERLYING_TYPE (from);
1612 :
1613 : /* Give this a better rank if it's a promotion.
1614 :
1615 : To handle CWG 1601, also bump the rank if we are converting
1616 : an enumeration with a fixed underlying type to the underlying
1617 : type. */
1618 205223050 : if ((same_type_p (to, type_promotes_to (from))
1619 192445854 : || (underlying_type && same_type_p (to, underlying_type)))
1620 205223069 : && next_conversion (conv)->rank <= cr_promotion)
1621 12777215 : conv->rank = cr_promotion;
1622 :
1623 : /* A prvalue of floating-point type can be converted to a prvalue of
1624 : another floating-point type with a greater or equal conversion
1625 : rank ([conv.rank]). A prvalue of standard floating-point type can
1626 : be converted to a prvalue of another standard floating-point type.
1627 : For backwards compatibility with handling __float128 and other
1628 : non-standard floating point types, allow all implicit floating
1629 : point conversions if neither type is extended floating-point
1630 : type and if at least one of them is, fail if they have unordered
1631 : conversion rank or from has higher conversion rank. */
1632 205223050 : if (fcode == REAL_TYPE
1633 205223050 : && tcode == REAL_TYPE
1634 16043163 : && (extended_float_type_p (from)
1635 15409631 : || extended_float_type_p (to))
1636 208197533 : && cp_compare_floating_point_conversion_ranks (from, to) >= 2)
1637 1223372 : conv->bad_p = true;
1638 : }
1639 234667781 : else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
1640 234667781 : && vector_types_convertible_p (from, to, false))
1641 4814 : return build_conv (ck_std, to, conv);
1642 234662967 : else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
1643 89921523 : && is_properly_derived_from (from, to))
1644 : {
1645 506191 : if (conv->kind == ck_rvalue)
1646 506140 : conv = next_conversion (conv);
1647 506191 : conv = build_conv (ck_base, to, conv);
1648 : /* The derived-to-base conversion indicates the initialization
1649 : of a parameter with base type from an object of a derived
1650 : type. A temporary object is created to hold the result of
1651 : the conversion unless we're binding directly to a reference. */
1652 506191 : conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
1653 : /* If we're performing copy-initialization, remember to skip
1654 : explicit constructors. */
1655 506191 : if (flags & LOOKUP_ONLYCONVERTING)
1656 506124 : conv->copy_init_p = true;
1657 : }
1658 : else
1659 234156776 : return NULL;
1660 :
1661 308951487 : if (flags & LOOKUP_NO_NARROWING)
1662 91166875 : conv->check_narrowing = true;
1663 :
1664 : return conv;
1665 : }
1666 :
1667 : /* Returns nonzero if T1 is reference-related to T2.
1668 :
1669 : This is considered when a reference to T1 is initialized by a T2. */
1670 :
1671 : bool
1672 363354726 : reference_related_p (tree t1, tree t2)
1673 : {
1674 363354726 : if (t1 == error_mark_node || t2 == error_mark_node)
1675 : return false;
1676 :
1677 363354722 : t1 = TYPE_MAIN_VARIANT (t1);
1678 363354722 : t2 = TYPE_MAIN_VARIANT (t2);
1679 :
1680 : /* [dcl.init.ref]
1681 :
1682 : Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
1683 : to "cv2 T2" if T1 is similar to T2, or T1 is a base class of T2. */
1684 363354722 : return (similar_type_p (t1, t2)
1685 363354722 : || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1686 84208343 : && DERIVED_FROM_P (t1, t2)));
1687 : }
1688 :
1689 : /* Returns nonzero if T1 is reference-compatible with T2. */
1690 :
1691 : bool
1692 327034660 : reference_compatible_p (tree t1, tree t2)
1693 : {
1694 : /* [dcl.init.ref]
1695 :
1696 : "cv1 T1" is reference compatible with "cv2 T2" if
1697 : a prvalue of type "pointer to cv2 T2" can be converted to the type
1698 : "pointer to cv1 T1" via a standard conversion sequence. */
1699 327034660 : tree ptype1 = build_pointer_type (t1);
1700 327034660 : tree ptype2 = build_pointer_type (t2);
1701 327034660 : conversion *conv = standard_conversion (ptype1, ptype2, NULL_TREE,
1702 : /*c_cast_p=*/false, 0, tf_none);
1703 327034660 : if (!conv || conv->bad_p)
1704 179495553 : return false;
1705 : return true;
1706 : }
1707 :
1708 : /* Return true if converting FROM to TO would involve a qualification
1709 : conversion. */
1710 :
1711 : static bool
1712 162948896 : involves_qualification_conversion_p (tree to, tree from)
1713 : {
1714 : /* If we're not convering a pointer to another one, we won't get
1715 : a qualification conversion. */
1716 162948896 : if (!((TYPE_PTR_P (to) && TYPE_PTR_P (from))
1717 2327 : || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from))))
1718 : return false;
1719 :
1720 5307606 : conversion *conv = standard_conversion (to, from, NULL_TREE,
1721 : /*c_cast_p=*/false, 0, tf_none);
1722 10545869 : for (conversion *t = conv; t; t = next_conversion (t))
1723 5307622 : if (t->kind == ck_qual)
1724 : return true;
1725 :
1726 : return false;
1727 : }
1728 :
1729 : /* Return true if HANDLER is a match for exception object with EXCEPT_TYPE as
1730 : per [except.handle]/3. */
1731 :
1732 : bool
1733 1912 : handler_match_for_exception_type (tree handler, tree except_type)
1734 : {
1735 1912 : tree handler_type = HANDLER_TYPE (handler);
1736 1912 : if (handler_type == NULL_TREE)
1737 : return true; /* ... */
1738 1880 : if (same_type_ignoring_top_level_qualifiers_p (handler_type, except_type))
1739 : return true;
1740 297 : if (CLASS_TYPE_P (except_type) && CLASS_TYPE_P (handler_type))
1741 : {
1742 85 : base_kind b_kind;
1743 85 : tree binfo = lookup_base (except_type, handler_type, ba_check, &b_kind,
1744 : tf_none);
1745 85 : if (binfo && binfo != error_mark_node)
1746 41 : return true;
1747 : }
1748 256 : if (TYPE_PTR_P (handler_type) || TYPE_PTRDATAMEM_P (handler_type))
1749 : {
1750 60 : if (TREE_CODE (except_type) == NULLPTR_TYPE)
1751 : return true;
1752 54 : if ((TYPE_PTR_P (handler_type) && TYPE_PTR_P (except_type))
1753 9 : || (TYPE_PTRDATAMEM_P (handler_type)
1754 0 : && TYPE_PTRDATAMEM_P (except_type)))
1755 : {
1756 45 : conversion *conv
1757 45 : = standard_conversion (handler_type, except_type, NULL_TREE,
1758 : /*c_cast_p=*/false, 0, tf_none);
1759 45 : if (conv && !conv->bad_p)
1760 : {
1761 33 : for (conversion *t = conv; t; t = next_conversion (t))
1762 22 : switch (t->kind)
1763 : {
1764 22 : case ck_ptr:
1765 22 : case ck_fnptr:
1766 22 : case ck_qual:
1767 22 : case ck_identity:
1768 22 : break;
1769 : default:
1770 : return false;
1771 : }
1772 : return true;
1773 : }
1774 : }
1775 : }
1776 : return false;
1777 : }
1778 :
1779 : /* A reference of the indicated TYPE is being bound directly to the
1780 : expression represented by the implicit conversion sequence CONV.
1781 : Return a conversion sequence for this binding. */
1782 :
1783 : static conversion *
1784 166872935 : direct_reference_binding (tree type, conversion *conv)
1785 : {
1786 166872935 : tree t;
1787 :
1788 166872935 : gcc_assert (TYPE_REF_P (type));
1789 166872935 : gcc_assert (!TYPE_REF_P (conv->type));
1790 :
1791 166872935 : t = TREE_TYPE (type);
1792 :
1793 166872935 : if (conv->kind == ck_identity)
1794 : /* Mark the identity conv as to not decay to rvalue. */
1795 166872935 : conv->rvaluedness_matches_p = true;
1796 :
1797 : /* [over.ics.rank]
1798 :
1799 : When a parameter of reference type binds directly
1800 : (_dcl.init.ref_) to an argument expression, the implicit
1801 : conversion sequence is the identity conversion, unless the
1802 : argument expression has a type that is a derived class of the
1803 : parameter type, in which case the implicit conversion sequence is
1804 : a derived-to-base Conversion.
1805 :
1806 : If the parameter binds directly to the result of applying a
1807 : conversion function to the argument expression, the implicit
1808 : conversion sequence is a user-defined conversion sequence
1809 : (_over.ics.user_), with the second standard conversion sequence
1810 : either an identity conversion or, if the conversion function
1811 : returns an entity of a type that is a derived class of the
1812 : parameter type, a derived-to-base conversion. */
1813 166872935 : if (is_properly_derived_from (conv->type, t))
1814 : {
1815 : /* Represent the derived-to-base conversion. */
1816 3924039 : conv = build_conv (ck_base, t, conv);
1817 : /* We will actually be binding to the base-class subobject in
1818 : the derived class, so we mark this conversion appropriately.
1819 : That way, convert_like knows not to generate a temporary. */
1820 3924039 : conv->need_temporary_p = false;
1821 : }
1822 162948896 : else if (involves_qualification_conversion_p (t, conv->type))
1823 : /* Represent the qualification conversion. After DR 2352
1824 : #1 and #2 were indistinguishable conversion sequences:
1825 :
1826 : void f(int*); // #1
1827 : void f(const int* const &); // #2
1828 : void g(int* p) { f(p); }
1829 :
1830 : because the types "int *" and "const int *const" are
1831 : reference-related and we were binding both directly and they
1832 : had the same rank. To break it up, we add a ck_qual under the
1833 : ck_ref_bind so that conversion sequence ranking chooses #1.
1834 :
1835 : We strip_top_quals here which is also what standard_conversion
1836 : does. Failure to do so would confuse comp_cv_qual_signature
1837 : into thinking that in
1838 :
1839 : void f(const int * const &); // #1
1840 : void f(const int *); // #2
1841 : int *x;
1842 : f(x);
1843 :
1844 : #2 is a better match than #1 even though they're ambiguous (97296). */
1845 69359 : conv = build_conv (ck_qual, strip_top_quals (t), conv);
1846 :
1847 166872935 : return build_conv (ck_ref_bind, type, conv);
1848 : }
1849 :
1850 : /* Returns the conversion path from type FROM to reference type TO for
1851 : purposes of reference binding. For lvalue binding, either pass a
1852 : reference type to FROM or an lvalue expression to EXPR. If the
1853 : reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1854 : the conversion returned. If C_CAST_P is true, this
1855 : conversion is coming from a C-style cast. */
1856 :
1857 : static conversion *
1858 326140178 : reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags,
1859 : tsubst_flags_t complain)
1860 : {
1861 326140178 : conversion *conv = NULL;
1862 326140178 : conversion *bad_direct_conv = nullptr;
1863 326140178 : tree to = TREE_TYPE (rto);
1864 326140178 : tree from = rfrom;
1865 326140178 : tree tfrom;
1866 326140178 : bool related_p;
1867 326140178 : bool compatible_p;
1868 326140178 : cp_lvalue_kind gl_kind;
1869 326140178 : bool is_lvalue;
1870 :
1871 326140178 : if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1872 : {
1873 49 : expr = instantiate_type (to, expr, tf_none);
1874 49 : if (expr == error_mark_node)
1875 : return NULL;
1876 49 : from = TREE_TYPE (expr);
1877 : }
1878 :
1879 326140178 : bool copy_list_init = false;
1880 326140178 : bool single_list_conv = false;
1881 326140178 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1882 : {
1883 276299 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
1884 : /* DR 1288: Otherwise, if the initializer list has a single element
1885 : of type E and ... [T's] referenced type is reference-related to E,
1886 : the object or reference is initialized from that element...
1887 :
1888 : ??? With P0388R4, we should bind 't' directly to U{}:
1889 : using U = A[2];
1890 : A (&&t)[] = {U{}};
1891 : because A[] and A[2] are reference-related. But we don't do it
1892 : because grok_reference_init has deduced the array size (to 1), and
1893 : A[1] and A[2] aren't reference-related. */
1894 276299 : if (CONSTRUCTOR_NELTS (expr) == 1
1895 154016 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
1896 : {
1897 3005 : tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1898 3005 : if (error_operand_p (elt))
1899 : return NULL;
1900 3000 : tree etype = TREE_TYPE (elt);
1901 3000 : if (reference_related_p (to, etype))
1902 : {
1903 590 : expr = elt;
1904 590 : from = etype;
1905 590 : goto skip;
1906 : }
1907 2410 : else if (CLASS_TYPE_P (etype) && TYPE_HAS_CONVERSION (etype))
1908 : /* CWG1996: jason's proposed drafting adds "or initializing T from E
1909 : would bind directly". We check that in the direct binding with
1910 : conversion code below. */
1911 : single_list_conv = true;
1912 : }
1913 : /* Otherwise, if T is a reference type, a prvalue temporary of the type
1914 : referenced by T is copy-list-initialized, and the reference is bound
1915 : to that temporary. */
1916 : copy_list_init = true;
1917 326140173 : skip:;
1918 : }
1919 :
1920 326140173 : if (TYPE_REF_P (from))
1921 : {
1922 56582 : from = TREE_TYPE (from);
1923 56582 : if (!TYPE_REF_IS_RVALUE (rfrom)
1924 56582 : || TREE_CODE (from) == FUNCTION_TYPE)
1925 : gl_kind = clk_ordinary;
1926 : else
1927 : gl_kind = clk_rvalueref;
1928 : }
1929 326083591 : else if (expr)
1930 322375951 : gl_kind = lvalue_kind (expr);
1931 3460463 : else if (CLASS_TYPE_P (from)
1932 3707640 : || TREE_CODE (from) == ARRAY_TYPE)
1933 : gl_kind = clk_class;
1934 : else
1935 : gl_kind = clk_none;
1936 :
1937 : /* Don't allow a class prvalue when LOOKUP_NO_TEMP_BIND. */
1938 326140173 : if ((flags & LOOKUP_NO_TEMP_BIND)
1939 3649333 : && (gl_kind & clk_class))
1940 : gl_kind = clk_none;
1941 :
1942 : /* Same mask as real_lvalue_p. */
1943 323245753 : is_lvalue = gl_kind && !(gl_kind & (clk_rvalueref|clk_class));
1944 :
1945 285475681 : tfrom = from;
1946 285475681 : if ((gl_kind & clk_bitfield) != 0)
1947 11138385 : tfrom = unlowered_expr_type (expr);
1948 :
1949 : /* Figure out whether or not the types are reference-related and
1950 : reference compatible. We have to do this after stripping
1951 : references from FROM. */
1952 326140173 : related_p = reference_related_p (to, tfrom);
1953 : /* If this is a C cast, first convert to an appropriately qualified
1954 : type, so that we can later do a const_cast to the desired type. */
1955 326140173 : if (related_p && c_cast_p
1956 326140173 : && !at_least_as_qualified_p (to, tfrom))
1957 194 : to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1958 326140173 : compatible_p = reference_compatible_p (to, tfrom);
1959 :
1960 : /* Directly bind reference when target expression's type is compatible with
1961 : the reference and expression is an lvalue. In DR391, the wording in
1962 : [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1963 : const and rvalue references to rvalues of compatible class type.
1964 : We should also do direct bindings for non-class xvalues. */
1965 326140173 : if ((related_p || compatible_p) && gl_kind)
1966 : {
1967 : /* [dcl.init.ref]
1968 :
1969 : If the initializer expression
1970 :
1971 : -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1972 : is reference-compatible with "cv2 T2,"
1973 :
1974 : the reference is bound directly to the initializer expression
1975 : lvalue.
1976 :
1977 : [...]
1978 : If the initializer expression is an rvalue, with T2 a class type,
1979 : and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1980 : is bound to the object represented by the rvalue or to a sub-object
1981 : within that object. */
1982 :
1983 149938145 : conv = build_identity_conv (tfrom, expr);
1984 149938145 : conv = direct_reference_binding (rto, conv);
1985 :
1986 149938145 : if (TYPE_REF_P (rfrom))
1987 : /* Handle rvalue reference to function properly. */
1988 15335 : conv->rvaluedness_matches_p
1989 15335 : = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom));
1990 : else
1991 149922810 : conv->rvaluedness_matches_p
1992 149922810 : = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
1993 :
1994 149938145 : if ((gl_kind & clk_bitfield) != 0
1995 149938145 : || ((gl_kind & clk_packed) != 0 && !TYPE_PACKED (to)))
1996 : /* For the purposes of overload resolution, we ignore the fact
1997 : this expression is a bitfield or packed field. (In particular,
1998 : [over.ics.ref] says specifically that a function with a
1999 : non-const reference parameter is viable even if the
2000 : argument is a bitfield.)
2001 :
2002 : However, when we actually call the function we must create
2003 : a temporary to which to bind the reference. If the
2004 : reference is volatile, or isn't const, then we cannot make
2005 : a temporary, so we just issue an error when the conversion
2006 : actually occurs. */
2007 110 : conv->need_temporary_p = true;
2008 :
2009 : /* Don't allow binding of lvalues (other than function lvalues) to
2010 : rvalue references. */
2011 113256321 : if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
2012 163142119 : && TREE_CODE (to) != FUNCTION_TYPE)
2013 13202408 : conv->bad_p = true;
2014 :
2015 : /* Nor the reverse. */
2016 36681824 : if (!is_lvalue && !TYPE_REF_IS_RVALUE (rto)
2017 : /* Unless it's really a C++20 lvalue being treated as an xvalue.
2018 : But in C++23, such an expression is just an xvalue, not a special
2019 : lvalue, so the binding is once again ill-formed. */
2020 22922592 : && !(cxx_dialect <= cxx20
2021 20421062 : && (gl_kind & clk_implicit_rval))
2022 21649108 : && (!CP_TYPE_CONST_NON_VOLATILE_P (to)
2023 21478718 : || (flags & LOOKUP_NO_RVAL_BIND))
2024 150108535 : && TREE_CODE (to) != FUNCTION_TYPE)
2025 170390 : conv->bad_p = true;
2026 :
2027 149938145 : if (!compatible_p)
2028 8037132 : conv->bad_p = true;
2029 :
2030 149938145 : return conv;
2031 : }
2032 : /* [class.conv.fct] A conversion function is never used to convert a
2033 : (possibly cv-qualified) object to the (possibly cv-qualified) same
2034 : object type (or a reference to it), to a (possibly cv-qualified) base
2035 : class of that type (or a reference to it).... */
2036 4745943 : else if (!related_p
2037 171456085 : && !(flags & LOOKUP_NO_CONVERSION)
2038 60547967 : && (CLASS_TYPE_P (from) || single_list_conv))
2039 : {
2040 22192579 : tree rexpr = expr;
2041 22192579 : if (single_list_conv)
2042 88 : rexpr = CONSTRUCTOR_ELT (expr, 0)->value;
2043 :
2044 : /* [dcl.init.ref]
2045 :
2046 : If the initializer expression
2047 :
2048 : -- has a class type (i.e., T2 is a class type) can be
2049 : implicitly converted to an lvalue of type "cv3 T3," where
2050 : "cv1 T1" is reference-compatible with "cv3 T3". (this
2051 : conversion is selected by enumerating the applicable
2052 : conversion functions (_over.match.ref_) and choosing the
2053 : best one through overload resolution. (_over.match_).
2054 :
2055 : the reference is bound to the lvalue result of the conversion
2056 : in the second case. */
2057 22192579 : z_candidate *cand = build_user_type_conversion_1 (rto, rexpr, flags,
2058 : complain);
2059 22192579 : if (cand)
2060 : {
2061 12657 : if (!cand->second_conv->bad_p)
2062 : return cand->second_conv;
2063 :
2064 : /* Direct reference binding wasn't successful and yielded a bad
2065 : conversion. Proceed with trying to go through a temporary
2066 : instead, and if that also fails then we'll return this bad
2067 : conversion rather than no conversion for sake of better
2068 : diagnostics. */
2069 : bad_direct_conv = cand->second_conv;
2070 : }
2071 : }
2072 :
2073 : /* From this point on, we conceptually need temporaries, even if we
2074 : elide them. Only the cases above are "direct bindings". */
2075 176189763 : if (flags & LOOKUP_NO_TEMP_BIND)
2076 : return bad_direct_conv ? bad_direct_conv : nullptr;
2077 :
2078 : /* [over.ics.rank]
2079 :
2080 : When a parameter of reference type is not bound directly to an
2081 : argument expression, the conversion sequence is the one required
2082 : to convert the argument expression to the underlying type of the
2083 : reference according to _over.best.ics_. Conceptually, this
2084 : conversion sequence corresponds to copy-initializing a temporary
2085 : of the underlying type with the argument expression. Any
2086 : difference in top-level cv-qualification is subsumed by the
2087 : initialization itself and does not constitute a conversion. */
2088 :
2089 172758425 : bool maybe_valid_p = true;
2090 :
2091 : /* [dcl.init.ref]
2092 :
2093 : Otherwise, the reference shall be an lvalue reference to a
2094 : non-volatile const type, or the reference shall be an rvalue
2095 : reference. */
2096 224182878 : if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto))
2097 : maybe_valid_p = false;
2098 :
2099 : /* [dcl.init.ref]
2100 :
2101 : Otherwise, a temporary of type "cv1 T1" is created and
2102 : initialized from the initializer expression using the rules for a
2103 : non-reference copy initialization. If T1 is reference-related to
2104 : T2, cv1 must be the same cv-qualification as, or greater
2105 : cv-qualification than, cv2; otherwise, the program is ill-formed. */
2106 172758425 : if (related_p && !at_least_as_qualified_p (to, from))
2107 : maybe_valid_p = false;
2108 :
2109 : /* We try below to treat an invalid reference binding as a bad conversion
2110 : to improve diagnostics, but doing so may cause otherwise unnecessary
2111 : instantiations that can lead to a hard error. So during the first pass
2112 : of overload resolution wherein we shortcut bad conversions, instead just
2113 : produce a special conversion indicating a second pass is necessary if
2114 : there's no strictly viable candidate. */
2115 172758425 : if (!maybe_valid_p && (flags & LOOKUP_SHORTCUT_BAD_CONVS))
2116 : {
2117 7712903 : if (bad_direct_conv)
2118 : return bad_direct_conv;
2119 :
2120 7712574 : conv = alloc_conversion (ck_deferred_bad);
2121 7712574 : conv->bad_p = true;
2122 7712574 : return conv;
2123 : }
2124 :
2125 : /* We're generating a temporary now, but don't bind any more in the
2126 : conversion (specifically, don't slice the temporary returned by a
2127 : conversion operator). */
2128 165045522 : flags |= LOOKUP_NO_TEMP_BIND;
2129 :
2130 : /* Core issue 899: When [copy-]initializing a temporary to be bound
2131 : to the first parameter of a copy constructor (12.8) called with
2132 : a single argument in the context of direct-initialization,
2133 : explicit conversion functions are also considered.
2134 :
2135 : So don't set LOOKUP_ONLYCONVERTING in that case. */
2136 165045522 : if (!(flags & LOOKUP_COPY_PARM))
2137 144892134 : flags |= LOOKUP_ONLYCONVERTING;
2138 :
2139 165045522 : if (!conv)
2140 165045522 : conv = implicit_conversion (to, from, expr, c_cast_p,
2141 : flags, complain);
2142 165045522 : if (!conv)
2143 : return bad_direct_conv ? bad_direct_conv : nullptr;
2144 :
2145 13160054 : if (conv->user_conv_p)
2146 : {
2147 9128079 : if (copy_list_init)
2148 : /* Remember this was copy-list-initialization. */
2149 233450 : conv->need_temporary_p = true;
2150 :
2151 : /* If initializing the temporary used a conversion function,
2152 : recalculate the second conversion sequence. */
2153 26120375 : for (conversion *t = conv; t; t = next_conversion (t))
2154 17587738 : if (t->kind == ck_user
2155 9053300 : && c_cast_p && !maybe_valid_p)
2156 : {
2157 12 : if (complain & tf_warning)
2158 12 : warning (OPT_Wcast_user_defined,
2159 : "casting %qT to %qT does not use %qD",
2160 12 : from, rto, t->cand->fn);
2161 : /* Don't let recalculation try to make this valid. */
2162 : break;
2163 : }
2164 17587726 : else if (t->kind == ck_user
2165 17587726 : && DECL_CONV_FN_P (t->cand->fn))
2166 : {
2167 595430 : tree ftype = TREE_TYPE (TREE_TYPE (t->cand->fn));
2168 : /* A prvalue of non-class type is cv-unqualified. */
2169 595430 : if (!TYPE_REF_P (ftype) && !CLASS_TYPE_P (ftype))
2170 826 : ftype = cv_unqualified (ftype);
2171 595430 : int sflags = (flags|LOOKUP_NO_CONVERSION)&~LOOKUP_NO_TEMP_BIND;
2172 595430 : conversion *new_second
2173 595430 : = reference_binding (rto, ftype, NULL_TREE, c_cast_p,
2174 : sflags, complain);
2175 595430 : if (!new_second)
2176 : return bad_direct_conv ? bad_direct_conv : nullptr;
2177 595430 : conv = merge_conversion_sequences (t, new_second);
2178 595430 : gcc_assert (maybe_valid_p || conv->bad_p);
2179 : return conv;
2180 : }
2181 : }
2182 :
2183 12564624 : conv = build_conv (ck_ref_bind, rto, conv);
2184 : /* This reference binding, unlike those above, requires the
2185 : creation of a temporary. */
2186 12564624 : conv->need_temporary_p = true;
2187 12564624 : conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
2188 12564624 : conv->bad_p |= !maybe_valid_p;
2189 :
2190 12564624 : return conv;
2191 : }
2192 :
2193 : /* Returns the implicit conversion sequence (see [over.ics]) from type
2194 : FROM to type TO. The optional expression EXPR may affect the
2195 : conversion. FLAGS are the usual overloading flags. If C_CAST_P is
2196 : true, this conversion is coming from a C-style cast. */
2197 :
2198 : static conversion *
2199 1809584333 : implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
2200 : int flags, tsubst_flags_t complain)
2201 : {
2202 1809584333 : conversion *conv;
2203 :
2204 1809584333 : if (from == error_mark_node || to == error_mark_node
2205 1809583164 : || expr == error_mark_node)
2206 : return NULL;
2207 :
2208 : /* Other flags only apply to the primary function in overload
2209 : resolution, or after we've chosen one. */
2210 1809583164 : flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM
2211 : |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_NO_NARROWING
2212 : |LOOKUP_PROTECT|LOOKUP_NO_NON_INTEGRAL|LOOKUP_SHORTCUT_BAD_CONVS);
2213 :
2214 : /* FIXME: actually we don't want warnings either, but we can't just
2215 : have 'complain &= ~(tf_warning|tf_error)' because it would cause
2216 : the regression of, eg, g++.old-deja/g++.benjamin/16077.C.
2217 : We really ought not to issue that warning until we've committed
2218 : to that conversion. */
2219 1809583164 : complain &= ~tf_error;
2220 :
2221 : /* Call reshape_init early to remove redundant braces. */
2222 1809583164 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr) && CLASS_TYPE_P (to))
2223 : {
2224 2524737 : to = complete_type (to);
2225 2524737 : if (!COMPLETE_TYPE_P (to))
2226 : return nullptr;
2227 2524713 : if (!CLASSTYPE_NON_AGGREGATE (to))
2228 : {
2229 1356174 : expr = reshape_init (to, expr, complain);
2230 1356174 : if (expr == error_mark_node)
2231 : return nullptr;
2232 1355933 : from = TREE_TYPE (expr);
2233 : }
2234 : }
2235 :
2236 : /* An argument should have gone through convert_from_reference. */
2237 1809582899 : gcc_checking_assert (!expr || !TYPE_REF_P (from));
2238 :
2239 1809582899 : if (TYPE_REF_P (to))
2240 317679845 : conv = reference_binding (to, from, expr, c_cast_p, flags, complain);
2241 : else
2242 1491903054 : conv = standard_conversion (to, from, expr, c_cast_p, flags, complain);
2243 :
2244 1809582899 : if (conv)
2245 : return conv;
2246 :
2247 402408645 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
2248 : {
2249 5253053 : if (is_std_init_list (to) && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2250 81136 : return build_list_conv (to, expr, flags, complain);
2251 :
2252 : /* As an extension, allow list-initialization of _Complex. */
2253 5090772 : if (TREE_CODE (to) == COMPLEX_TYPE
2254 5145283 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2255 : {
2256 54502 : conv = build_complex_conv (to, expr, flags, complain);
2257 54502 : if (conv)
2258 : return conv;
2259 : }
2260 :
2261 : /* Allow conversion from an initializer-list with one element to a
2262 : scalar type. */
2263 5036310 : if (SCALAR_TYPE_P (to))
2264 : {
2265 2552770 : int nelts = CONSTRUCTOR_NELTS (expr);
2266 423733 : tree elt;
2267 :
2268 423733 : if (nelts == 0)
2269 2129037 : elt = build_value_init (to, tf_none);
2270 423733 : else if (nelts == 1 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2271 423017 : elt = CONSTRUCTOR_ELT (expr, 0)->value;
2272 : else
2273 716 : elt = error_mark_node;
2274 :
2275 2552770 : conv = implicit_conversion (to, TREE_TYPE (elt), elt,
2276 : c_cast_p, flags, complain);
2277 2552770 : if (conv)
2278 : {
2279 2551091 : conv->check_narrowing = true;
2280 2551091 : if (BRACE_ENCLOSED_INITIALIZER_P (elt))
2281 : /* Too many levels of braces, i.e. '{{1}}'. */
2282 16 : conv->bad_p = true;
2283 2551091 : return conv;
2284 : }
2285 : }
2286 2483540 : else if (TREE_CODE (to) == ARRAY_TYPE)
2287 1652 : return build_array_conv (to, expr, flags, complain);
2288 : }
2289 :
2290 399720304 : if (expr != NULL_TREE
2291 391750394 : && (MAYBE_CLASS_TYPE_P (from)
2292 216700820 : || MAYBE_CLASS_TYPE_P (to))
2293 680789034 : && (flags & LOOKUP_NO_CONVERSION) == 0)
2294 : {
2295 106306912 : struct z_candidate *cand;
2296 :
2297 75115487 : if (CLASS_TYPE_P (to)
2298 75115437 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
2299 108749873 : && !CLASSTYPE_NON_AGGREGATE (complete_type (to)))
2300 1355878 : return build_aggr_conv (to, expr, flags, complain);
2301 :
2302 104951034 : cand = build_user_type_conversion_1 (to, expr, flags, complain);
2303 104951034 : if (cand)
2304 : {
2305 1057734 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
2306 1048456 : && CONSTRUCTOR_NELTS (expr) == 1
2307 56303 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr)
2308 16599022 : && !is_list_ctor (cand->fn))
2309 : {
2310 : /* "If C is not an initializer-list constructor and the
2311 : initializer list has a single element of type cv U, where U is
2312 : X or a class derived from X, the implicit conversion sequence
2313 : has Exact Match rank if U is X, or Conversion rank if U is
2314 : derived from X." */
2315 55957 : tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
2316 55957 : tree elttype = TREE_TYPE (elt);
2317 55957 : if (reference_related_p (to, elttype))
2318 67 : return implicit_conversion (to, elttype, elt,
2319 67 : c_cast_p, flags, complain);
2320 : }
2321 16542652 : conv = cand->second_conv;
2322 : }
2323 :
2324 : /* We used to try to bind a reference to a temporary here, but that
2325 : is now handled after the recursive call to this function at the end
2326 : of reference_binding. */
2327 104950967 : return conv;
2328 : }
2329 :
2330 : return NULL;
2331 : }
2332 :
2333 : /* Like implicit_conversion, but return NULL if the conversion is bad.
2334 :
2335 : This is not static so that check_non_deducible_conversion can call it within
2336 : add_template_candidate_real as part of overload resolution; it should not be
2337 : called outside of overload resolution. */
2338 :
2339 : conversion *
2340 8252387 : good_conversion (tree to, tree from, tree expr,
2341 : int flags, tsubst_flags_t complain)
2342 : {
2343 8252387 : conversion *c = implicit_conversion (to, from, expr, /*cast*/false,
2344 : flags, complain);
2345 8252387 : if (c && c->bad_p)
2346 2801255 : c = NULL;
2347 8252387 : return c;
2348 : }
2349 :
2350 : /* Add a new entry to the list of candidates. Used by the add_*_candidate
2351 : functions. ARGS will not be changed until a single candidate is
2352 : selected. */
2353 :
2354 : static struct z_candidate *
2355 1489826645 : add_candidate (struct z_candidate **candidates,
2356 : tree fn, tree first_arg, const vec<tree, va_gc> *args,
2357 : size_t num_convs, conversion **convs,
2358 : tree access_path, tree conversion_path,
2359 : int viable, struct rejection_reason *reason,
2360 : int flags)
2361 : {
2362 1489826645 : struct z_candidate *cand = (struct z_candidate *)
2363 1489826645 : conversion_obstack_alloc (sizeof (struct z_candidate));
2364 :
2365 1489826645 : cand->fn = fn;
2366 1489826645 : cand->first_arg = first_arg;
2367 1489826645 : cand->args = args;
2368 1489826645 : cand->convs = convs;
2369 1489826645 : cand->num_convs = num_convs;
2370 1489826645 : cand->access_path = access_path;
2371 1489826645 : cand->conversion_path = conversion_path;
2372 1489826645 : cand->viable = viable;
2373 1489826645 : cand->reason = reason;
2374 1489826645 : cand->next = *candidates;
2375 1489826645 : cand->flags = flags;
2376 1489826645 : *candidates = cand;
2377 :
2378 1489826645 : if (convs && cand->reversed ())
2379 : /* Swap the conversions for comparison in joust; we'll swap them back
2380 : before build_over_call. */
2381 142637972 : std::swap (convs[0], convs[1]);
2382 :
2383 1489826645 : return cand;
2384 : }
2385 :
2386 : /* FN is a function from the overload set that we outright didn't even
2387 : consider (for some reason); add it to the list as an non-viable "ignored"
2388 : candidate. */
2389 :
2390 : static z_candidate *
2391 1032403571 : add_ignored_candidate (z_candidate **candidates, tree fn)
2392 : {
2393 : /* No need to dynamically allocate these. */
2394 1032403571 : static const rejection_reason reason_ignored = { rr_ignored, {} };
2395 :
2396 1032403571 : struct z_candidate *cand = (struct z_candidate *)
2397 1032399091 : conversion_obstack_alloc (sizeof (struct z_candidate));
2398 :
2399 1032403571 : cand->fn = fn;
2400 1032403571 : cand->reason = const_cast<rejection_reason *> (&reason_ignored);
2401 1032403571 : cand->next = *candidates;
2402 1032403571 : *candidates = cand;
2403 :
2404 1032403571 : return cand;
2405 : }
2406 :
2407 : /* True iff CAND is a candidate added by add_ignored_candidate. */
2408 :
2409 : static bool
2410 812665687 : ignored_candidate_p (const z_candidate *cand)
2411 : {
2412 812659640 : return cand->reason && cand->reason->code == rr_ignored;
2413 : }
2414 :
2415 : /* Return the number of remaining arguments in the parameter list
2416 : beginning with ARG. */
2417 :
2418 : int
2419 212919376 : remaining_arguments (tree arg)
2420 : {
2421 212919376 : int n;
2422 :
2423 355540769 : for (n = 0; arg != NULL_TREE && arg != void_list_node;
2424 142621393 : arg = TREE_CHAIN (arg))
2425 142621393 : n++;
2426 :
2427 212919376 : return n;
2428 : }
2429 :
2430 : /* [over.match.copy]: When initializing a temporary object (12.2) to be bound
2431 : to the first parameter of a constructor where the parameter is of type
2432 : "reference to possibly cv-qualified T" and the constructor is called with a
2433 : single argument in the context of direct-initialization of an object of type
2434 : "cv2 T", explicit conversion functions are also considered.
2435 :
2436 : So set LOOKUP_COPY_PARM to let reference_binding know that
2437 : it's being called in that context. */
2438 :
2439 : int
2440 604431925 : conv_flags (int i, int nargs, tree fn, tree arg, int flags)
2441 : {
2442 604431925 : int lflags = flags;
2443 604431925 : tree t;
2444 590405692 : if (i == 0 && nargs == 1 && DECL_CONSTRUCTOR_P (fn)
2445 194298275 : && (t = FUNCTION_FIRST_USER_PARMTYPE (fn))
2446 798730200 : && (same_type_ignoring_top_level_qualifiers_p
2447 194298275 : (non_reference (TREE_VALUE (t)), DECL_CONTEXT (fn))))
2448 : {
2449 154119803 : if (!(flags & LOOKUP_ONLYCONVERTING))
2450 40465851 : lflags |= LOOKUP_COPY_PARM;
2451 154119803 : if ((flags & LOOKUP_LIST_INIT_CTOR)
2452 154119803 : && BRACE_ENCLOSED_INITIALIZER_P (arg))
2453 1398 : lflags |= LOOKUP_NO_CONVERSION;
2454 : }
2455 : else
2456 450312122 : lflags |= LOOKUP_ONLYCONVERTING;
2457 :
2458 604431925 : return lflags;
2459 : }
2460 :
2461 : /* Build an appropriate 'this' conversion for the method FN and class
2462 : type CTYPE from the value ARG (having type ARGTYPE) to the type PARMTYPE.
2463 : This function modifies PARMTYPE, ARGTYPE and ARG. */
2464 :
2465 : static conversion *
2466 124002626 : build_this_conversion (tree fn, tree ctype,
2467 : tree& parmtype, tree& argtype, tree& arg,
2468 : int flags, tsubst_flags_t complain)
2469 : {
2470 248005252 : gcc_assert (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
2471 : && !DECL_CONSTRUCTOR_P (fn));
2472 :
2473 : /* The type of the implicit object parameter ('this') for
2474 : overload resolution is not always the same as for the
2475 : function itself; conversion functions are considered to
2476 : be members of the class being converted, and functions
2477 : introduced by a using-declaration are considered to be
2478 : members of the class that uses them.
2479 :
2480 : Since build_over_call ignores the ICS for the `this'
2481 : parameter, we can just change the parm type. */
2482 124002626 : parmtype = cp_build_qualified_type (ctype,
2483 124002626 : cp_type_quals (TREE_TYPE (parmtype)));
2484 124002626 : bool this_p = true;
2485 124002626 : if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn)))
2486 : {
2487 : /* If the function has a ref-qualifier, the implicit
2488 : object parameter has reference type. */
2489 155285 : bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn));
2490 155285 : parmtype = cp_build_reference_type (parmtype, rv);
2491 : /* The special handling of 'this' conversions in compare_ics
2492 : does not apply if there is a ref-qualifier. */
2493 155285 : this_p = false;
2494 : }
2495 : else
2496 : {
2497 123847341 : parmtype = build_pointer_type (parmtype);
2498 : /* We don't use build_this here because we don't want to
2499 : capture the object argument until we've chosen a
2500 : non-static member function. */
2501 123847341 : arg = build_address (arg);
2502 123847341 : argtype = lvalue_type (arg);
2503 : }
2504 124002626 : flags |= LOOKUP_ONLYCONVERTING;
2505 124002626 : conversion *t = implicit_conversion (parmtype, argtype, arg,
2506 : /*c_cast_p=*/false, flags, complain);
2507 124002626 : t->this_p = this_p;
2508 124002626 : return t;
2509 : }
2510 :
2511 : /* Create an overload candidate for the function or method FN called
2512 : with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
2513 : FLAGS is passed on to implicit_conversion.
2514 :
2515 : This does not change ARGS.
2516 :
2517 : CTYPE, if non-NULL, is the type we want to pretend this function
2518 : comes from for purposes of overload resolution.
2519 :
2520 : SHORTCUT_BAD_CONVS controls how we handle "bad" argument conversions.
2521 : If true, we stop computing conversions upon seeing the first bad
2522 : conversion. This is used by add_candidates to avoid computing
2523 : more conversions than necessary in the presence of a strictly viable
2524 : candidate, while preserving the defacto behavior of overload resolution
2525 : when it turns out there are only non-strictly viable candidates. */
2526 :
2527 : static struct z_candidate *
2528 835922812 : add_function_candidate (struct z_candidate **candidates,
2529 : tree fn, tree ctype, tree first_arg,
2530 : const vec<tree, va_gc> *args, tree access_path,
2531 : tree conversion_path, int flags,
2532 : conversion **convs,
2533 : bool shortcut_bad_convs,
2534 : tsubst_flags_t complain)
2535 : {
2536 835922812 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
2537 835922812 : int i, len;
2538 835922812 : tree parmnode;
2539 835922812 : tree orig_first_arg = first_arg;
2540 835922812 : int skip;
2541 835922812 : int viable = 1;
2542 835922812 : struct rejection_reason *reason = NULL;
2543 :
2544 : /* The `this', `in_chrg' and VTT arguments to constructors are not
2545 : considered in overload resolution. */
2546 1671845624 : if (DECL_CONSTRUCTOR_P (fn))
2547 : {
2548 386116736 : if (ctor_omit_inherited_parms (fn))
2549 : /* Bring back parameters omitted from an inherited ctor. */
2550 150 : parmlist = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
2551 : else
2552 386116661 : parmlist = skip_artificial_parms_for (fn, parmlist);
2553 386116736 : skip = num_artificial_parms_for (fn);
2554 386116736 : if (skip > 0 && first_arg != NULL_TREE)
2555 : {
2556 386116736 : --skip;
2557 386116736 : first_arg = NULL_TREE;
2558 : }
2559 : }
2560 : else
2561 : skip = 0;
2562 :
2563 1622056628 : len = vec_safe_length (args) - skip + (first_arg != NULL_TREE ? 1 : 0);
2564 835922812 : if (!convs)
2565 707406976 : convs = alloc_conversions (len);
2566 :
2567 : /* 13.3.2 - Viable functions [over.match.viable]
2568 : First, to be a viable function, a candidate function shall have enough
2569 : parameters to agree in number with the arguments in the list.
2570 :
2571 : We need to check this first; otherwise, checking the ICSes might cause
2572 : us to produce an ill-formed template instantiation. */
2573 :
2574 835922812 : parmnode = parmlist;
2575 1748722147 : for (i = 0; i < len; ++i)
2576 : {
2577 1023371746 : if (parmnode == NULL_TREE || parmnode == void_list_node)
2578 : break;
2579 912799335 : parmnode = TREE_CHAIN (parmnode);
2580 : }
2581 :
2582 835922812 : if ((i < len && parmnode)
2583 835922812 : || !sufficient_parms_p (parmnode))
2584 : {
2585 212916373 : int remaining = remaining_arguments (parmnode);
2586 212916373 : viable = 0;
2587 212916373 : reason = arity_rejection (first_arg, i + remaining, len);
2588 : }
2589 :
2590 : /* An inherited constructor (12.6.3 [class.inhctor.init]) that has a first
2591 : parameter of type "reference to cv C" (including such a constructor
2592 : instantiated from a template) is excluded from the set of candidate
2593 : functions when used to construct an object of type D with an argument list
2594 : containing a single argument if C is reference-related to D. */
2595 732369173 : if (viable && len == 1 && parmlist && DECL_CONSTRUCTOR_P (fn)
2596 196969216 : && flag_new_inheriting_ctors
2597 1032883343 : && DECL_INHERITED_CTOR (fn))
2598 : {
2599 817708 : tree ptype = non_reference (TREE_VALUE (parmlist));
2600 817708 : tree dtype = DECL_CONTEXT (fn);
2601 1635416 : tree btype = DECL_INHERITED_CTOR_BASE (fn);
2602 817708 : if (reference_related_p (ptype, dtype)
2603 817708 : && reference_related_p (btype, ptype))
2604 : {
2605 542969 : viable = false;
2606 542969 : reason = inherited_ctor_rejection ();
2607 : }
2608 : }
2609 :
2610 : /* Second, for a function to be viable, its constraints must be
2611 : satisfied. */
2612 835922812 : if (flag_concepts && viable && !constraints_satisfied_p (fn))
2613 : {
2614 226091 : reason = constraint_failure ();
2615 226091 : viable = false;
2616 : }
2617 :
2618 : /* When looking for a function from a subobject from an implicit
2619 : copy/move constructor/operator=, don't consider anything that takes (a
2620 : reference to) an unrelated type. See c++/44909 and core 1092. */
2621 835922812 : if (viable && parmlist && (flags & LOOKUP_DEFAULTED))
2622 : {
2623 95376750 : if (DECL_CONSTRUCTOR_P (fn))
2624 : i = 1;
2625 24277912 : else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2626 24277912 : && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
2627 : i = 2;
2628 : else
2629 : i = 0;
2630 47688375 : if (i && len == i)
2631 : {
2632 26127589 : parmnode = chain_index (i-1, parmlist);
2633 26127589 : if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
2634 : ctype))
2635 5092220 : viable = 0;
2636 : }
2637 :
2638 : /* This only applies at the top level. */
2639 42596155 : flags &= ~LOOKUP_DEFAULTED;
2640 : }
2641 :
2642 835922812 : if (! viable)
2643 218777653 : goto out;
2644 :
2645 617145159 : if (shortcut_bad_convs)
2646 617017401 : flags |= LOOKUP_SHORTCUT_BAD_CONVS;
2647 : else
2648 127758 : flags &= ~LOOKUP_SHORTCUT_BAD_CONVS;
2649 :
2650 : /* Third, for F to be a viable function, there shall exist for each
2651 : argument an implicit conversion sequence that converts that argument
2652 : to the corresponding parameter of F. */
2653 :
2654 617145159 : parmnode = parmlist;
2655 :
2656 1077602024 : for (i = 0; i < len; ++i)
2657 : {
2658 699942671 : tree argtype, to_type;
2659 699942671 : tree arg;
2660 :
2661 699942671 : if (parmnode == void_list_node)
2662 : break;
2663 :
2664 699942671 : if (convs[i])
2665 : {
2666 : /* Already set during deduction. */
2667 10602453 : parmnode = TREE_CHAIN (parmnode);
2668 10602453 : continue;
2669 : }
2670 :
2671 689340218 : if (i == 0 && first_arg != NULL_TREE)
2672 117171761 : arg = first_arg;
2673 : else
2674 572168457 : arg = const_cast<tree> (
2675 1090637052 : (*args)[i + skip - (first_arg != NULL_TREE ? 1 : 0)]);
2676 689340218 : argtype = lvalue_type (arg);
2677 :
2678 689340218 : conversion *t;
2679 689340218 : if (parmnode)
2680 : {
2681 683693731 : tree parmtype = TREE_VALUE (parmnode);
2682 683693731 : if (i == 0
2683 531472969 : && DECL_IOBJ_MEMBER_FUNCTION_P (fn)
2684 1309641069 : && !DECL_CONSTRUCTOR_P (fn))
2685 117162757 : t = build_this_conversion (fn, ctype, parmtype, argtype, arg,
2686 : flags, complain);
2687 : else
2688 : {
2689 566530974 : int lflags = conv_flags (i, len-skip, fn, arg, flags);
2690 566530974 : t = implicit_conversion (parmtype, argtype, arg,
2691 : /*c_cast_p=*/false, lflags, complain);
2692 : }
2693 683693731 : to_type = parmtype;
2694 683693731 : parmnode = TREE_CHAIN (parmnode);
2695 : }
2696 : else
2697 : {
2698 5646487 : t = build_identity_conv (argtype, arg);
2699 5646487 : t->ellipsis_p = true;
2700 5646487 : to_type = argtype;
2701 : }
2702 :
2703 689340218 : convs[i] = t;
2704 689340218 : if (! t)
2705 : {
2706 213721802 : viable = 0;
2707 213721802 : reason = arg_conversion_rejection (first_arg, i, argtype, to_type,
2708 213721802 : EXPR_LOCATION (arg));
2709 213721802 : break;
2710 : }
2711 :
2712 475618416 : if (t->bad_p)
2713 : {
2714 25795133 : viable = -1;
2715 25795133 : reason = bad_arg_conversion_rejection (first_arg, i, arg, to_type,
2716 25795133 : EXPR_LOCATION (arg));
2717 25795133 : if (shortcut_bad_convs)
2718 : break;
2719 : }
2720 : }
2721 :
2722 377659353 : out:
2723 835922812 : return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
2724 835922812 : access_path, conversion_path, viable, reason, flags);
2725 : }
2726 :
2727 : /* Create an overload candidate for the conversion function FN which will
2728 : be invoked for expression OBJ, producing a pointer-to-function which
2729 : will in turn be called with the argument list FIRST_ARG/ARGLIST,
2730 : and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
2731 : passed on to implicit_conversion.
2732 :
2733 : Actually, we don't really care about FN; we care about the type it
2734 : converts to. There may be multiple conversion functions that will
2735 : convert to that type, and we rely on build_user_type_conversion_1 to
2736 : choose the best one; so when we create our candidate, we record the type
2737 : instead of the function. */
2738 :
2739 : static struct z_candidate *
2740 96608 : add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
2741 : const vec<tree, va_gc> *arglist,
2742 : tree access_path, tree conversion_path,
2743 : tsubst_flags_t complain)
2744 : {
2745 96608 : tree totype = TREE_TYPE (TREE_TYPE (fn));
2746 96608 : int i, len, viable, flags;
2747 96608 : tree parmlist, parmnode;
2748 96608 : conversion **convs;
2749 96608 : struct rejection_reason *reason;
2750 :
2751 193365 : for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
2752 96757 : parmlist = TREE_TYPE (parmlist);
2753 96608 : parmlist = TYPE_ARG_TYPES (parmlist);
2754 :
2755 96608 : len = vec_safe_length (arglist) + 1;
2756 96608 : convs = alloc_conversions (len);
2757 96608 : parmnode = parmlist;
2758 96608 : viable = 1;
2759 96608 : flags = LOOKUP_IMPLICIT;
2760 96608 : reason = NULL;
2761 :
2762 : /* Don't bother looking up the same type twice. */
2763 96608 : if (*candidates && (*candidates)->fn == totype)
2764 : return NULL;
2765 :
2766 96593 : if (!constraints_satisfied_p (fn))
2767 : {
2768 9 : reason = constraint_failure ();
2769 9 : viable = 0;
2770 9 : return add_candidate (candidates, fn, obj, arglist, len, convs,
2771 9 : access_path, conversion_path, viable, reason, flags);
2772 : }
2773 :
2774 300525 : for (i = 0; i < len; ++i)
2775 : {
2776 204029 : tree arg, argtype, convert_type = NULL_TREE;
2777 204029 : conversion *t;
2778 :
2779 204029 : if (i == 0)
2780 : arg = obj;
2781 : else
2782 107445 : arg = (*arglist)[i - 1];
2783 204029 : argtype = lvalue_type (arg);
2784 :
2785 204029 : if (i == 0)
2786 : {
2787 96584 : t = build_identity_conv (argtype, NULL_TREE);
2788 96584 : t = build_conv (ck_user, totype, t);
2789 : /* Leave the 'cand' field null; we'll figure out the conversion in
2790 : convert_like if this candidate is chosen. */
2791 96584 : convert_type = totype;
2792 : }
2793 107445 : else if (parmnode == void_list_node)
2794 : break;
2795 107418 : else if (parmnode)
2796 : {
2797 107409 : t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
2798 : /*c_cast_p=*/false, flags, complain);
2799 107409 : convert_type = TREE_VALUE (parmnode);
2800 : }
2801 : else
2802 : {
2803 9 : t = build_identity_conv (argtype, arg);
2804 9 : t->ellipsis_p = true;
2805 9 : convert_type = argtype;
2806 : }
2807 :
2808 204002 : convs[i] = t;
2809 204002 : if (! t)
2810 : break;
2811 :
2812 203941 : if (t->bad_p)
2813 : {
2814 17 : viable = -1;
2815 51 : reason = bad_arg_conversion_rejection (NULL_TREE, i, arg, convert_type,
2816 17 : EXPR_LOCATION (arg));
2817 : }
2818 :
2819 203941 : if (i == 0)
2820 96584 : continue;
2821 :
2822 107357 : if (parmnode)
2823 107348 : parmnode = TREE_CHAIN (parmnode);
2824 : }
2825 :
2826 96584 : if (i < len
2827 96584 : || ! sufficient_parms_p (parmnode))
2828 : {
2829 112 : int remaining = remaining_arguments (parmnode);
2830 112 : viable = 0;
2831 112 : reason = arity_rejection (NULL_TREE, i + remaining, len);
2832 : }
2833 :
2834 96584 : return add_candidate (candidates, totype, obj, arglist, len, convs,
2835 96584 : access_path, conversion_path, viable, reason, flags);
2836 : }
2837 :
2838 : static void
2839 11427800 : build_builtin_candidate (struct z_candidate **candidates, tree fnname,
2840 : tree type1, tree type2, const vec<tree,va_gc> &args,
2841 : tree *argtypes, int flags, tsubst_flags_t complain)
2842 : {
2843 11427800 : conversion *t;
2844 11427800 : conversion **convs;
2845 11427800 : size_t num_convs;
2846 11427800 : int viable = 1;
2847 11427800 : tree types[2];
2848 11427800 : struct rejection_reason *reason = NULL;
2849 :
2850 11427800 : types[0] = type1;
2851 11427800 : types[1] = type2;
2852 :
2853 11427800 : num_convs = args.length ();
2854 11427800 : convs = alloc_conversions (num_convs);
2855 :
2856 : /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2857 : conversion ops are allowed. We handle that here by just checking for
2858 : boolean_type_node because other operators don't ask for it. COND_EXPR
2859 : also does contextual conversion to bool for the first operand, but we
2860 : handle that in build_conditional_expr, and type1 here is operand 2. */
2861 11427800 : if (type1 != boolean_type_node)
2862 10892142 : flags |= LOOKUP_ONLYCONVERTING;
2863 :
2864 33587862 : for (unsigned i = 0; i < 2 && i < num_convs; ++i)
2865 : {
2866 22160062 : t = implicit_conversion (types[i], argtypes[i], args[i],
2867 : /*c_cast_p=*/false, flags, complain);
2868 22160062 : if (! t)
2869 : {
2870 1792392 : viable = 0;
2871 : /* We need something for printing the candidate. */
2872 1792392 : t = build_identity_conv (types[i], NULL_TREE);
2873 1792392 : reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2874 1792392 : types[i], EXPR_LOCATION (args[i]));
2875 : }
2876 20367670 : else if (t->bad_p)
2877 : {
2878 174 : viable = 0;
2879 174 : reason = bad_arg_conversion_rejection (NULL_TREE, i, args[i],
2880 : types[i],
2881 174 : EXPR_LOCATION (args[i]));
2882 : }
2883 22160062 : convs[i] = t;
2884 : }
2885 :
2886 : /* For COND_EXPR we rearranged the arguments; undo that now. */
2887 11427800 : if (num_convs == 3)
2888 : {
2889 144 : convs[2] = convs[1];
2890 144 : convs[1] = convs[0];
2891 144 : t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
2892 : /*c_cast_p=*/false, flags,
2893 : complain);
2894 144 : if (t)
2895 144 : convs[0] = t;
2896 : else
2897 : {
2898 0 : viable = 0;
2899 0 : reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2],
2900 : boolean_type_node,
2901 0 : EXPR_LOCATION (args[2]));
2902 : }
2903 : }
2904 :
2905 11427800 : add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
2906 : num_convs, convs,
2907 : /*access_path=*/NULL_TREE,
2908 : /*conversion_path=*/NULL_TREE,
2909 : viable, reason, flags);
2910 11427800 : }
2911 :
2912 : static bool
2913 9 : is_complete (tree t)
2914 : {
2915 9 : return COMPLETE_TYPE_P (complete_type (t));
2916 : }
2917 :
2918 : /* Returns nonzero if TYPE is a promoted arithmetic type. */
2919 :
2920 : static bool
2921 2867 : promoted_arithmetic_type_p (tree type)
2922 : {
2923 : /* [over.built]
2924 :
2925 : In this section, the term promoted integral type is used to refer
2926 : to those integral types which are preserved by integral promotion
2927 : (including e.g. int and long but excluding e.g. char).
2928 : Similarly, the term promoted arithmetic type refers to promoted
2929 : integral types plus floating types. */
2930 2867 : return ((CP_INTEGRAL_TYPE_P (type)
2931 204 : && same_type_p (type_promotes_to (type), type))
2932 2867 : || SCALAR_FLOAT_TYPE_P (type));
2933 : }
2934 :
2935 : /* Create any builtin operator overload candidates for the operator in
2936 : question given the converted operand types TYPE1 and TYPE2. The other
2937 : args are passed through from add_builtin_candidates to
2938 : build_builtin_candidate.
2939 :
2940 : TYPE1 and TYPE2 may not be permissible, and we must filter them.
2941 : If CODE is requires candidates operands of the same type of the kind
2942 : of which TYPE1 and TYPE2 are, we add both candidates
2943 : CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2944 :
2945 : static void
2946 16085117 : add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
2947 : enum tree_code code2, tree fnname, tree type1,
2948 : tree type2, vec<tree,va_gc> &args, tree *argtypes,
2949 : int flags, tsubst_flags_t complain)
2950 : {
2951 16085117 : switch (code)
2952 : {
2953 21 : case POSTINCREMENT_EXPR:
2954 21 : case POSTDECREMENT_EXPR:
2955 21 : args[1] = integer_zero_node;
2956 21 : type2 = integer_type_node;
2957 21 : break;
2958 : default:
2959 : break;
2960 : }
2961 :
2962 16085117 : switch (code)
2963 : {
2964 :
2965 : /* 4 For every pair (T, VQ), where T is an arithmetic type other than bool,
2966 : and VQ is either volatile or empty, there exist candidate operator
2967 : functions of the form
2968 : VQ T& operator++(VQ T&);
2969 : T operator++(VQ T&, int);
2970 : 5 For every pair (T, VQ), where T is an arithmetic type other than bool,
2971 : and VQ is either volatile or empty, there exist candidate operator
2972 : functions of the form
2973 : VQ T& operator--(VQ T&);
2974 : T operator--(VQ T&, int);
2975 : 6 For every pair (T, VQ), where T is a cv-qualified or cv-unqualified object
2976 : type, and VQ is either volatile or empty, there exist candidate operator
2977 : functions of the form
2978 : T*VQ& operator++(T*VQ&);
2979 : T*VQ& operator--(T*VQ&);
2980 : T* operator++(T*VQ&, int);
2981 : T* operator--(T*VQ&, int); */
2982 :
2983 18 : case POSTDECREMENT_EXPR:
2984 18 : case PREDECREMENT_EXPR:
2985 18 : if (TREE_CODE (type1) == BOOLEAN_TYPE)
2986 : return;
2987 : /* FALLTHRU */
2988 39 : case POSTINCREMENT_EXPR:
2989 39 : case PREINCREMENT_EXPR:
2990 : /* P0002R1, Remove deprecated operator++(bool) added "other than bool"
2991 : to p4. */
2992 39 : if (TREE_CODE (type1) == BOOLEAN_TYPE && cxx_dialect >= cxx17)
2993 : return;
2994 33 : if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
2995 : {
2996 24 : type1 = build_reference_type (type1);
2997 24 : break;
2998 : }
2999 : return;
3000 :
3001 : /* 7 For every cv-qualified or cv-unqualified object type T, there
3002 : exist candidate operator functions of the form
3003 :
3004 : T& operator*(T*);
3005 :
3006 :
3007 : 8 For every function type T that does not have cv-qualifiers or
3008 : a ref-qualifier, there exist candidate operator functions of the form
3009 : T& operator*(T*); */
3010 :
3011 107577 : case INDIRECT_REF:
3012 107577 : if (TYPE_PTR_P (type1)
3013 107577 : && (TYPE_PTROB_P (type1)
3014 12 : || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
3015 : break;
3016 : return;
3017 :
3018 : /* 9 For every type T, there exist candidate operator functions of the form
3019 : T* operator+(T*);
3020 :
3021 : 10 For every floating-point or promoted integral type T, there exist
3022 : candidate operator functions of the form
3023 : T operator+(T);
3024 : T operator-(T); */
3025 :
3026 250 : case UNARY_PLUS_EXPR: /* unary + */
3027 250 : if (TYPE_PTR_P (type1))
3028 : break;
3029 : /* FALLTHRU */
3030 190518 : case NEGATE_EXPR:
3031 190518 : if (ARITHMETIC_TYPE_P (type1))
3032 : break;
3033 : return;
3034 :
3035 : /* 11 For every promoted integral type T, there exist candidate operator
3036 : functions of the form
3037 : T operator~(T); */
3038 :
3039 149857 : case BIT_NOT_EXPR:
3040 149857 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
3041 : break;
3042 : return;
3043 :
3044 : /* 12 For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, C1
3045 : is the same type as C2 or is a derived class of C2, and T is an object
3046 : type or a function type there exist candidate operator functions of the
3047 : form
3048 : CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
3049 : where CV12 is the union of CV1 and CV2. */
3050 :
3051 28 : case MEMBER_REF:
3052 28 : if (TYPE_PTR_P (type1) && TYPE_PTRMEM_P (type2))
3053 : {
3054 28 : tree c1 = TREE_TYPE (type1);
3055 28 : tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
3056 :
3057 25 : if (CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
3058 50 : && (TYPE_PTRMEMFUNC_P (type2)
3059 9 : || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
3060 : break;
3061 : }
3062 : return;
3063 :
3064 : /* 13 For every pair of types L and R, where each of L and R is a floating-point
3065 : or promoted integral type, there exist candidate operator functions of the
3066 : form
3067 : LR operator*(L, R);
3068 : LR operator/(L, R);
3069 : LR operator+(L, R);
3070 : LR operator-(L, R);
3071 : bool operator<(L, R);
3072 : bool operator>(L, R);
3073 : bool operator<=(L, R);
3074 : bool operator>=(L, R);
3075 : bool operator==(L, R);
3076 : bool operator!=(L, R);
3077 : where LR is the result of the usual arithmetic conversions between
3078 : types L and R.
3079 :
3080 : 14 For every integral type T there exists a candidate operator function of
3081 : the form
3082 :
3083 : std::strong_ordering operator<=>(T, T);
3084 :
3085 : 15 For every pair of floating-point types L and R, there exists a candidate
3086 : operator function of the form
3087 :
3088 : std::partial_ordering operator<=>(L, R);
3089 :
3090 : 16 For every cv-qualified or cv-unqualified object type T there exist
3091 : candidate operator functions of the form
3092 : T* operator+(T*, std::ptrdiff_t);
3093 : T& operator[](T*, std::ptrdiff_t);
3094 : T* operator-(T*, std::ptrdiff_t);
3095 : T* operator+(std::ptrdiff_t, T*);
3096 : T& operator[](std::ptrdiff_t, T*);
3097 :
3098 : 17 For every T, where T is a pointer to object type, there exist candidate
3099 : operator functions of the form
3100 : std::ptrdiff_t operator-(T, T);
3101 :
3102 : 18 For every T, where T is an enumeration type or a pointer type, there
3103 : exist candidate operator functions of the form
3104 : bool operator<(T, T);
3105 : bool operator>(T, T);
3106 : bool operator<=(T, T);
3107 : bool operator>=(T, T);
3108 : bool operator==(T, T);
3109 : bool operator!=(T, T);
3110 : R operator<=>(T, T);
3111 :
3112 : where R is the result type specified in [expr.spaceship].
3113 :
3114 : 19 For every T, where T is a pointer-to-member type or std::nullptr_t,
3115 : there exist candidate operator functions of the form
3116 : bool operator==(T, T);
3117 : bool operator!=(T, T); */
3118 :
3119 197313 : case MINUS_EXPR:
3120 197313 : if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
3121 : break;
3122 13 : if (TYPE_PTROB_P (type1)
3123 197299 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3124 : {
3125 4 : type2 = ptrdiff_type_node;
3126 4 : break;
3127 : }
3128 : /* FALLTHRU */
3129 834255 : case MULT_EXPR:
3130 834255 : case TRUNC_DIV_EXPR:
3131 834255 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3132 : break;
3133 : return;
3134 :
3135 : /* This isn't exactly what's specified above for operator<=>, but it's
3136 : close enough. In particular, we don't care about the return type
3137 : specified above; it doesn't participate in overload resolution and it
3138 : doesn't affect the semantics of the built-in operator. */
3139 9262239 : case SPACESHIP_EXPR:
3140 9262239 : case EQ_EXPR:
3141 9262239 : case NE_EXPR:
3142 220214 : if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
3143 9482453 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)))
3144 : break;
3145 9262205 : if (NULLPTR_TYPE_P (type1) && NULLPTR_TYPE_P (type2))
3146 : break;
3147 9262199 : if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (args[1]))
3148 : {
3149 : type2 = type1;
3150 : break;
3151 : }
3152 9262196 : if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (args[0]))
3153 : {
3154 : type1 = type2;
3155 : break;
3156 : }
3157 : /* Fall through. */
3158 9931976 : case LT_EXPR:
3159 9931976 : case GT_EXPR:
3160 9931976 : case LE_EXPR:
3161 9931976 : case GE_EXPR:
3162 9931976 : case MAX_EXPR:
3163 9931976 : case MIN_EXPR:
3164 9931976 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3165 : break;
3166 8117624 : if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3167 : break;
3168 8113946 : if (TREE_CODE (type1) == ENUMERAL_TYPE
3169 6360623 : && TREE_CODE (type2) == ENUMERAL_TYPE)
3170 : break;
3171 3055430 : if (TYPE_PTR_P (type1)
3172 3055430 : && null_ptr_cst_p (args[1]))
3173 : {
3174 : type2 = type1;
3175 : break;
3176 : }
3177 3055411 : if (null_ptr_cst_p (args[0])
3178 3055411 : && TYPE_PTR_P (type2))
3179 : {
3180 : type1 = type2;
3181 : break;
3182 : }
3183 : return;
3184 :
3185 211633 : case PLUS_EXPR:
3186 211633 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3187 : break;
3188 : /* FALLTHRU */
3189 160930 : case ARRAY_REF:
3190 160930 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
3191 : {
3192 6 : type1 = ptrdiff_type_node;
3193 6 : break;
3194 : }
3195 160924 : if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3196 : {
3197 35635 : type2 = ptrdiff_type_node;
3198 35635 : break;
3199 : }
3200 : return;
3201 :
3202 : /* 18For every pair of promoted integral types L and R, there exist candi-
3203 : date operator functions of the form
3204 : LR operator%(L, R);
3205 : LR operator&(L, R);
3206 : LR operator^(L, R);
3207 : LR operator|(L, R);
3208 : L operator<<(L, R);
3209 : L operator>>(L, R);
3210 : where LR is the result of the usual arithmetic conversions between
3211 : types L and R. */
3212 :
3213 2915840 : case TRUNC_MOD_EXPR:
3214 2915840 : case BIT_AND_EXPR:
3215 2915840 : case BIT_IOR_EXPR:
3216 2915840 : case BIT_XOR_EXPR:
3217 2915840 : case LSHIFT_EXPR:
3218 2915840 : case RSHIFT_EXPR:
3219 2915840 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3220 : break;
3221 : return;
3222 :
3223 : /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
3224 : type, VQ is either volatile or empty, and R is a promoted arithmetic
3225 : type, there exist candidate operator functions of the form
3226 : VQ L& operator=(VQ L&, R);
3227 : VQ L& operator*=(VQ L&, R);
3228 : VQ L& operator/=(VQ L&, R);
3229 : VQ L& operator+=(VQ L&, R);
3230 : VQ L& operator-=(VQ L&, R);
3231 :
3232 : 20For every pair T, VQ), where T is any type and VQ is either volatile
3233 : or empty, there exist candidate operator functions of the form
3234 : T*VQ& operator=(T*VQ&, T*);
3235 :
3236 : 21For every pair T, VQ), where T is a pointer to member type and VQ is
3237 : either volatile or empty, there exist candidate operator functions of
3238 : the form
3239 : VQ T& operator=(VQ T&, T);
3240 :
3241 : 22For every triple T, VQ, I), where T is a cv-qualified or cv-
3242 : unqualified complete object type, VQ is either volatile or empty, and
3243 : I is a promoted integral type, there exist candidate operator func-
3244 : tions of the form
3245 : T*VQ& operator+=(T*VQ&, I);
3246 : T*VQ& operator-=(T*VQ&, I);
3247 :
3248 : 23For every triple L, VQ, R), where L is an integral or enumeration
3249 : type, VQ is either volatile or empty, and R is a promoted integral
3250 : type, there exist candidate operator functions of the form
3251 :
3252 : VQ L& operator%=(VQ L&, R);
3253 : VQ L& operator<<=(VQ L&, R);
3254 : VQ L& operator>>=(VQ L&, R);
3255 : VQ L& operator&=(VQ L&, R);
3256 : VQ L& operator^=(VQ L&, R);
3257 : VQ L& operator|=(VQ L&, R); */
3258 :
3259 1698354 : case MODIFY_EXPR:
3260 1698354 : switch (code2)
3261 : {
3262 99016 : case PLUS_EXPR:
3263 99016 : case MINUS_EXPR:
3264 99016 : if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3265 : {
3266 0 : type2 = ptrdiff_type_node;
3267 0 : break;
3268 : }
3269 : /* FALLTHRU */
3270 99021 : case MULT_EXPR:
3271 99021 : case TRUNC_DIV_EXPR:
3272 99021 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3273 : break;
3274 : return;
3275 :
3276 1599333 : case TRUNC_MOD_EXPR:
3277 1599333 : case BIT_AND_EXPR:
3278 1599333 : case BIT_IOR_EXPR:
3279 1599333 : case BIT_XOR_EXPR:
3280 1599333 : case LSHIFT_EXPR:
3281 1599333 : case RSHIFT_EXPR:
3282 1599333 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3283 : break;
3284 : return;
3285 :
3286 0 : case NOP_EXPR:
3287 0 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3288 : break;
3289 0 : if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
3290 0 : || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3291 0 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
3292 0 : || ((TYPE_PTRMEMFUNC_P (type1)
3293 0 : || TYPE_PTR_P (type1))
3294 0 : && null_ptr_cst_p (args[1])))
3295 : {
3296 : type2 = type1;
3297 : break;
3298 : }
3299 : return;
3300 :
3301 0 : default:
3302 0 : gcc_unreachable ();
3303 : }
3304 1365694 : type1 = build_reference_type (type1);
3305 1365694 : break;
3306 :
3307 2686 : case COND_EXPR:
3308 : /* [over.built]
3309 :
3310 : For every pair of promoted arithmetic types L and R, there
3311 : exist candidate operator functions of the form
3312 :
3313 : LR operator?(bool, L, R);
3314 :
3315 : where LR is the result of the usual arithmetic conversions
3316 : between types L and R.
3317 :
3318 : For every type T, where T is a pointer or pointer-to-member
3319 : type, there exist candidate operator functions of the form T
3320 : operator?(bool, T, T); */
3321 :
3322 2686 : if (promoted_arithmetic_type_p (type1)
3323 2686 : && promoted_arithmetic_type_p (type2))
3324 : /* That's OK. */
3325 : break;
3326 :
3327 : /* Otherwise, the types should be pointers. */
3328 2663 : if (!((TYPE_PTR_OR_PTRMEM_P (type1) || null_ptr_cst_p (args[0]))
3329 337 : && (TYPE_PTR_OR_PTRMEM_P (type2) || null_ptr_cst_p (args[1]))))
3330 2542 : return;
3331 :
3332 : /* We don't check that the two types are the same; the logic
3333 : below will actually create two candidates; one in which both
3334 : parameter types are TYPE1, and one in which both parameter
3335 : types are TYPE2. */
3336 : break;
3337 :
3338 3 : case REALPART_EXPR:
3339 3 : case IMAGPART_EXPR:
3340 3 : if (ARITHMETIC_TYPE_P (type1))
3341 : break;
3342 : return;
3343 :
3344 0 : default:
3345 0 : gcc_unreachable ();
3346 : }
3347 :
3348 : /* Make sure we don't create builtin candidates with dependent types. */
3349 10892018 : bool u1 = uses_template_parms (type1);
3350 10892018 : bool u2 = type2 ? uses_template_parms (type2) : false;
3351 10892005 : if (u1 || u2)
3352 : {
3353 : /* Try to recover if one of the types is non-dependent. But if
3354 : there's only one type, there's nothing we can do. */
3355 19 : if (!type2)
3356 : return;
3357 : /* And we lose if both are dependent. */
3358 16 : if (u1 && u2)
3359 : return;
3360 : /* Or if they have different forms. */
3361 9 : if (TREE_CODE (type1) != TREE_CODE (type2))
3362 : return;
3363 :
3364 9 : if (u1 && !u2)
3365 : type1 = type2;
3366 6 : else if (u2 && !u1)
3367 10892008 : type2 = type1;
3368 : }
3369 :
3370 : /* If we're dealing with two pointer types or two enumeral types,
3371 : we need candidates for both of them. */
3372 10670332 : if (type2 && !same_type_p (type1, type2)
3373 1821388 : && TREE_CODE (type1) == TREE_CODE (type2)
3374 11311778 : && (TYPE_REF_P (type1)
3375 419770 : || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3376 419659 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
3377 419641 : || TYPE_PTRMEMFUNC_P (type1)
3378 419641 : || MAYBE_CLASS_TYPE_P (type1)
3379 419641 : || TREE_CODE (type1) == ENUMERAL_TYPE))
3380 : {
3381 223 : if (TYPE_PTR_OR_PTRMEM_P (type1))
3382 : {
3383 129 : tree cptype = composite_pointer_type (input_location,
3384 : type1, type2,
3385 : error_mark_node,
3386 : error_mark_node,
3387 : CPO_CONVERSION,
3388 : tf_none);
3389 129 : if (cptype != error_mark_node)
3390 : {
3391 89 : build_builtin_candidate
3392 89 : (candidates, fnname, cptype, cptype, args, argtypes,
3393 : flags, complain);
3394 89 : return;
3395 : }
3396 : }
3397 :
3398 134 : build_builtin_candidate
3399 134 : (candidates, fnname, type1, type1, args, argtypes, flags, complain);
3400 134 : build_builtin_candidate
3401 134 : (candidates, fnname, type2, type2, args, argtypes, flags, complain);
3402 134 : return;
3403 : }
3404 :
3405 10891785 : build_builtin_candidate
3406 10891785 : (candidates, fnname, type1, type2, args, argtypes, flags, complain);
3407 : }
3408 :
3409 : tree
3410 888255340 : type_decays_to (tree type)
3411 : {
3412 888255340 : if (TREE_CODE (type) == ARRAY_TYPE)
3413 7960778 : return build_pointer_type (TREE_TYPE (type));
3414 880294562 : if (TREE_CODE (type) == FUNCTION_TYPE)
3415 28935 : return build_pointer_type (type);
3416 : return type;
3417 : }
3418 :
3419 : /* There are three conditions of builtin candidates:
3420 :
3421 : 1) bool-taking candidates. These are the same regardless of the input.
3422 : 2) pointer-pair taking candidates. These are generated for each type
3423 : one of the input types converts to.
3424 : 3) arithmetic candidates. According to the standard, we should generate
3425 : all of these, but I'm trying not to...
3426 :
3427 : Here we generate a superset of the possible candidates for this particular
3428 : case. That is a subset of the full set the standard defines, plus some
3429 : other cases which the standard disallows. add_builtin_candidate will
3430 : filter out the invalid set. */
3431 :
3432 : static void
3433 32157122 : add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
3434 : enum tree_code code2, tree fnname,
3435 : vec<tree, va_gc> *argv,
3436 : int flags, tsubst_flags_t complain)
3437 : {
3438 32157122 : int ref1;
3439 32157122 : int enum_p = 0;
3440 32157122 : tree type, argtypes[3], t;
3441 : /* TYPES[i] is the set of possible builtin-operator parameter types
3442 : we will consider for the Ith argument. */
3443 32157122 : vec<tree, va_gc> *types[2];
3444 32157122 : unsigned ix;
3445 32157122 : vec<tree, va_gc> &args = *argv;
3446 32157122 : unsigned len = args.length ();
3447 :
3448 90377621 : for (unsigned i = 0; i < len; ++i)
3449 : {
3450 58220499 : if (args[i])
3451 58220499 : argtypes[i] = unlowered_expr_type (args[i]);
3452 : else
3453 0 : argtypes[i] = NULL_TREE;
3454 : }
3455 :
3456 32157122 : switch (code)
3457 : {
3458 : /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
3459 : and VQ is either volatile or empty, there exist candidate operator
3460 : functions of the form
3461 : VQ T& operator++(VQ T&); */
3462 :
3463 : case POSTINCREMENT_EXPR:
3464 : case PREINCREMENT_EXPR:
3465 : case POSTDECREMENT_EXPR:
3466 : case PREDECREMENT_EXPR:
3467 : case MODIFY_EXPR:
3468 : ref1 = 1;
3469 : break;
3470 :
3471 : /* 24There also exist candidate operator functions of the form
3472 : bool operator!(bool);
3473 : bool operator&&(bool, bool);
3474 : bool operator||(bool, bool); */
3475 :
3476 473862 : case TRUTH_NOT_EXPR:
3477 473862 : build_builtin_candidate
3478 473862 : (candidates, fnname, boolean_type_node,
3479 : NULL_TREE, args, argtypes, flags, complain);
3480 19851878 : return;
3481 :
3482 61796 : case TRUTH_ORIF_EXPR:
3483 61796 : case TRUTH_ANDIF_EXPR:
3484 61796 : build_builtin_candidate
3485 61796 : (candidates, fnname, boolean_type_node,
3486 : boolean_type_node, args, argtypes, flags, complain);
3487 61796 : return;
3488 :
3489 : case ADDR_EXPR:
3490 : case COMPOUND_EXPR:
3491 : case COMPONENT_REF:
3492 : case CO_AWAIT_EXPR:
3493 : return;
3494 :
3495 9368855 : case COND_EXPR:
3496 9368855 : case EQ_EXPR:
3497 9368855 : case NE_EXPR:
3498 9368855 : case LT_EXPR:
3499 9368855 : case LE_EXPR:
3500 9368855 : case GT_EXPR:
3501 9368855 : case GE_EXPR:
3502 9368855 : case SPACESHIP_EXPR:
3503 9368855 : enum_p = 1;
3504 : /* Fall through. */
3505 :
3506 : default:
3507 : ref1 = 0;
3508 : }
3509 :
3510 29541434 : types[0] = make_tree_vector ();
3511 29541434 : types[1] = make_tree_vector ();
3512 :
3513 29541434 : if (len == 3)
3514 685 : len = 2;
3515 55461241 : for (unsigned i = 0; i < len; ++i)
3516 : {
3517 42682135 : if (MAYBE_CLASS_TYPE_P (argtypes[i]))
3518 : {
3519 21009525 : tree convs;
3520 :
3521 21009525 : if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
3522 : return;
3523 :
3524 14684552 : convs = lookup_conversions (argtypes[i]);
3525 :
3526 14684552 : if (code == COND_EXPR)
3527 : {
3528 1200 : if (lvalue_p (args[i]))
3529 877 : vec_safe_push (types[i], build_reference_type (argtypes[i]));
3530 :
3531 1200 : vec_safe_push (types[i], TYPE_MAIN_VARIANT (argtypes[i]));
3532 : }
3533 :
3534 14683352 : else if (! convs)
3535 : return;
3536 :
3537 10428244 : for (; convs; convs = TREE_CHAIN (convs))
3538 : {
3539 6181047 : type = TREE_TYPE (convs);
3540 :
3541 7646128 : if (i == 0 && ref1
3542 6181047 : && (!TYPE_REF_P (type)
3543 1319 : || CP_TYPE_CONST_P (TREE_TYPE (type))))
3544 1465081 : continue;
3545 :
3546 4715966 : if (code == COND_EXPR && TYPE_REF_P (type))
3547 23 : vec_safe_push (types[i], type);
3548 :
3549 4715966 : type = non_reference (type);
3550 4715966 : if (i != 0 || ! ref1)
3551 : {
3552 4715927 : type = cv_unqualified (type_decays_to (type));
3553 4715927 : if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
3554 328 : vec_safe_push (types[i], type);
3555 4715927 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3556 2665633 : type = type_promotes_to (type);
3557 : }
3558 :
3559 4715966 : if (! vec_member (type, types[i]))
3560 4713574 : vec_safe_push (types[i], type);
3561 : }
3562 : }
3563 : else
3564 : {
3565 21672610 : if (code == COND_EXPR && lvalue_p (args[i]))
3566 88 : vec_safe_push (types[i], build_reference_type (argtypes[i]));
3567 21672610 : type = non_reference (argtypes[i]);
3568 21672610 : if (i != 0 || ! ref1)
3569 : {
3570 19974345 : type = cv_unqualified (type_decays_to (type));
3571 19974345 : if (enum_p && UNSCOPED_ENUM_P (type))
3572 2251321 : vec_safe_push (types[i], type);
3573 19974345 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3574 9154135 : type = type_promotes_to (type);
3575 : }
3576 21672610 : vec_safe_push (types[i], type);
3577 : }
3578 : }
3579 :
3580 : /* Run through the possible parameter types of both arguments,
3581 : creating candidates with those parameter types. */
3582 26288677 : FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t)
3583 : {
3584 13509571 : unsigned jx;
3585 13509571 : tree u;
3586 :
3587 13509571 : if (!types[1]->is_empty ())
3588 29146653 : FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u)
3589 15637082 : add_builtin_candidate
3590 15637082 : (candidates, code, code2, fnname, t,
3591 : u, args, argtypes, flags, complain);
3592 : else
3593 448035 : add_builtin_candidate
3594 448035 : (candidates, code, code2, fnname, t,
3595 : NULL_TREE, args, argtypes, flags, complain);
3596 : }
3597 :
3598 12779106 : release_tree_vector (types[0]);
3599 12779106 : release_tree_vector (types[1]);
3600 : }
3601 :
3602 :
3603 : /* If TMPL can be successfully instantiated as indicated by
3604 : EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
3605 :
3606 : TMPL is the template. EXPLICIT_TARGS are any explicit template
3607 : arguments. ARGLIST is the arguments provided at the call-site.
3608 : This does not change ARGLIST. The RETURN_TYPE is the desired type
3609 : for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
3610 : as for add_function_candidate. If an OBJ is supplied, FLAGS and
3611 : CTYPE are ignored, and OBJ is as for add_conv_candidate.
3612 :
3613 : SHORTCUT_BAD_CONVS is as in add_function_candidate. */
3614 :
3615 : static struct z_candidate*
3616 770913316 : add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
3617 : tree ctype, tree explicit_targs, tree first_arg,
3618 : const vec<tree, va_gc> *arglist, tree return_type,
3619 : tree access_path, tree conversion_path,
3620 : int flags, tree obj, unification_kind_t strict,
3621 : bool shortcut_bad_convs, tsubst_flags_t complain)
3622 : {
3623 770913316 : int ntparms = DECL_NTPARMS (tmpl);
3624 770913316 : tree targs = make_tree_vec (ntparms);
3625 770913316 : unsigned int len = vec_safe_length (arglist);
3626 770913316 : unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
3627 770913316 : unsigned int skip_without_in_chrg = 0;
3628 770913316 : tree first_arg_without_in_chrg = first_arg;
3629 770913316 : tree *args_without_in_chrg;
3630 770913316 : unsigned int nargs_without_in_chrg;
3631 770913316 : unsigned int ia, ix;
3632 770913316 : tree arg;
3633 770913316 : struct z_candidate *cand;
3634 770913316 : tree fn;
3635 770913316 : struct rejection_reason *reason = NULL;
3636 770913316 : int errs;
3637 770913316 : conversion **convs = NULL;
3638 :
3639 : /* We don't do deduction on the in-charge parameter, the VTT
3640 : parameter or 'this'. */
3641 770913316 : if (DECL_IOBJ_MEMBER_FUNCTION_P (tmpl))
3642 : {
3643 70166010 : if (first_arg_without_in_chrg != NULL_TREE)
3644 : first_arg_without_in_chrg = NULL_TREE;
3645 12 : else if (return_type && strict == DEDUCE_CALL)
3646 : /* We're deducing for a call to the result of a template conversion
3647 : function, so the args don't contain 'this'; leave them alone. */;
3648 : else
3649 770913316 : ++skip_without_in_chrg;
3650 : }
3651 :
3652 770913316 : if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
3653 770913316 : || DECL_BASE_CONSTRUCTOR_P (tmpl))
3654 771827159 : && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
3655 : {
3656 177 : if (first_arg_without_in_chrg != NULL_TREE)
3657 : first_arg_without_in_chrg = NULL_TREE;
3658 : else
3659 177 : ++skip_without_in_chrg;
3660 : }
3661 :
3662 770913316 : if (len < skip_without_in_chrg)
3663 0 : return add_ignored_candidate (candidates, tmpl);
3664 :
3665 833681876 : if (DECL_CONSTRUCTOR_P (tmpl) && nargs == 2
3666 828149539 : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (first_arg),
3667 57236223 : TREE_TYPE ((*arglist)[0])))
3668 : {
3669 : /* 12.8/6 says, "A declaration of a constructor for a class X is
3670 : ill-formed if its first parameter is of type (optionally cv-qualified)
3671 : X and either there are no other parameters or else all other
3672 : parameters have default arguments. A member function template is never
3673 : instantiated to produce such a constructor signature."
3674 :
3675 : So if we're trying to copy an object of the containing class, don't
3676 : consider a template constructor that has a first parameter type that
3677 : is just a template parameter, as we would deduce a signature that we
3678 : would then reject in the code below. */
3679 5008476 : if (tree firstparm = FUNCTION_FIRST_USER_PARMTYPE (tmpl))
3680 : {
3681 5008476 : firstparm = TREE_VALUE (firstparm);
3682 5008476 : if (PACK_EXPANSION_P (firstparm))
3683 1884 : firstparm = PACK_EXPANSION_PATTERN (firstparm);
3684 5008476 : if (TREE_CODE (firstparm) == TEMPLATE_TYPE_PARM)
3685 : {
3686 2008005 : gcc_assert (!explicit_targs);
3687 2008005 : reason = invalid_copy_with_fn_template_rejection ();
3688 2008005 : goto fail;
3689 : }
3690 : }
3691 : }
3692 :
3693 768905311 : nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
3694 768905311 : + (len - skip_without_in_chrg));
3695 768905311 : args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
3696 768905311 : ia = 0;
3697 768905311 : if (first_arg_without_in_chrg != NULL_TREE)
3698 : {
3699 6334 : args_without_in_chrg[ia] = first_arg_without_in_chrg;
3700 6334 : ++ia;
3701 : }
3702 1299481051 : for (ix = skip_without_in_chrg;
3703 2068386362 : vec_safe_iterate (arglist, ix, &arg);
3704 : ++ix)
3705 : {
3706 1299481051 : args_without_in_chrg[ia] = arg;
3707 1299481051 : ++ia;
3708 : }
3709 768905311 : gcc_assert (ia == nargs_without_in_chrg);
3710 :
3711 768905311 : if (!obj)
3712 : {
3713 : /* Check that there's no obvious arity mismatch before proceeding with
3714 : deduction. This avoids substituting explicit template arguments
3715 : into the template or e.g. derived-to-base parm/arg unification
3716 : (which could result in an error outside the immediate context) when
3717 : the resulting candidate would be unviable anyway. */
3718 768905299 : int min_arity = 0, max_arity = 0;
3719 768905299 : tree parms = TYPE_ARG_TYPES (TREE_TYPE (tmpl));
3720 768905299 : parms = skip_artificial_parms_for (tmpl, parms);
3721 2891169854 : for (; parms != void_list_node; parms = TREE_CHAIN (parms))
3722 : {
3723 2712529317 : if (!parms || PACK_EXPANSION_P (TREE_VALUE (parms)))
3724 : {
3725 : max_arity = -1;
3726 : break;
3727 : }
3728 1353359256 : if (TREE_PURPOSE (parms))
3729 : /* A parameter with a default argument. */
3730 10552593 : ++max_arity;
3731 : else
3732 1342806663 : ++min_arity, ++max_arity;
3733 : }
3734 768905299 : if (ia < (unsigned)min_arity)
3735 : {
3736 : /* Too few arguments. */
3737 44246263 : reason = arity_rejection (NULL_TREE, min_arity, ia,
3738 : /*least_p=*/(max_arity == -1));
3739 44246263 : goto fail;
3740 : }
3741 724659036 : else if (max_arity != -1 && ia > (unsigned)max_arity)
3742 : {
3743 : /* Too many arguments. */
3744 4947072 : reason = arity_rejection (NULL_TREE, max_arity, ia);
3745 4947072 : goto fail;
3746 : }
3747 :
3748 719711964 : convs = alloc_conversions (nargs);
3749 :
3750 719711964 : if (shortcut_bad_convs
3751 719707427 : && DECL_IOBJ_MEMBER_FUNCTION_P (tmpl)
3752 823734818 : && !DECL_CONSTRUCTOR_P (tmpl))
3753 : {
3754 : /* Check the 'this' conversion before proceeding with deduction.
3755 : This is effectively an extension of the DR 1391 resolution
3756 : that we perform in check_non_deducible_conversions, though it's
3757 : convenient to do this extra check here instead of there. */
3758 6839869 : tree parmtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (tmpl)));
3759 6839869 : tree argtype = lvalue_type (first_arg);
3760 6839869 : tree arg = first_arg;
3761 6839869 : conversion *t = build_this_conversion (tmpl, ctype,
3762 : parmtype, argtype, arg,
3763 : flags, complain);
3764 6839869 : convs[0] = t;
3765 6839869 : if (t->bad_p)
3766 : {
3767 31438 : reason = bad_arg_conversion_rejection (first_arg, 0,
3768 : arg, parmtype,
3769 31438 : EXPR_LOCATION (arg));
3770 31438 : goto fail;
3771 : }
3772 : }
3773 : }
3774 :
3775 719680538 : errs = errorcount+sorrycount;
3776 1439347525 : fn = fn_type_unification (tmpl, explicit_targs, targs,
3777 : args_without_in_chrg,
3778 : nargs_without_in_chrg,
3779 : return_type, strict, flags, convs,
3780 719680538 : false, complain & tf_decltype);
3781 :
3782 719666987 : if (fn == error_mark_node)
3783 : {
3784 : /* Don't repeat unification later if it already resulted in errors. */
3785 591146090 : if (errorcount+sorrycount == errs)
3786 591145980 : reason = template_unification_rejection (tmpl, explicit_targs,
3787 : targs, args_without_in_chrg,
3788 : nargs_without_in_chrg,
3789 : return_type, strict, flags);
3790 : else
3791 110 : reason = template_unification_error_rejection ();
3792 591146090 : goto fail;
3793 : }
3794 :
3795 : /* Now the explicit specifier might have been deduced; check if this
3796 : declaration is explicit. If it is and we're ignoring non-converting
3797 : constructors, don't add this function to the set of candidates. */
3798 128520897 : if (((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
3799 : == LOOKUP_ONLYCONVERTING)
3800 128520897 : && DECL_NONCONVERTING_P (fn))
3801 4480 : return add_ignored_candidate (candidates, fn);
3802 :
3803 257032834 : if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
3804 : {
3805 6197913 : tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
3806 12395799 : if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
3807 : ctype))
3808 : {
3809 : /* We're trying to produce a constructor with a prohibited signature,
3810 : as discussed above; handle here any cases we didn't catch then,
3811 : such as X(X<T>). */
3812 572 : reason = invalid_copy_with_fn_template_rejection ();
3813 572 : goto fail;
3814 : }
3815 : }
3816 :
3817 128515845 : if (obj != NULL_TREE)
3818 : /* Aha, this is a conversion function. */
3819 9 : cand = add_conv_candidate (candidates, fn, obj, arglist,
3820 : access_path, conversion_path, complain);
3821 : else
3822 128515836 : cand = add_function_candidate (candidates, fn, ctype,
3823 : first_arg, arglist, access_path,
3824 : conversion_path, flags, convs,
3825 : shortcut_bad_convs, complain);
3826 128515845 : if (DECL_TI_TEMPLATE (fn) != tmpl)
3827 : /* This situation can occur if a member template of a template
3828 : class is specialized. Then, instantiate_template might return
3829 : an instantiation of the specialization, in which case the
3830 : DECL_TI_TEMPLATE field will point at the original
3831 : specialization. For example:
3832 :
3833 : template <class T> struct S { template <class U> void f(U);
3834 : template <> void f(int) {}; };
3835 : S<double> sd;
3836 : sd.f(3);
3837 :
3838 : Here, TMPL will be template <class U> S<double>::f(U).
3839 : And, instantiate template will give us the specialization
3840 : template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
3841 : for this will point at template <class T> template <> S<T>::f(int),
3842 : so that we can find the definition. For the purposes of
3843 : overload resolution, however, we want the original TMPL. */
3844 7001981 : cand->template_decl = build_template_info (tmpl, targs);
3845 : else
3846 121513864 : cand->template_decl = DECL_TEMPLATE_INFO (fn);
3847 128515845 : cand->explicit_targs = explicit_targs;
3848 :
3849 128515845 : return cand;
3850 642379440 : fail:
3851 642379440 : int viable = (reason->code == rr_bad_arg_conversion ? -1 : 0);
3852 642379440 : return add_candidate (candidates, tmpl, first_arg, arglist, nargs, convs,
3853 642379440 : access_path, conversion_path, viable, reason, flags);
3854 : }
3855 :
3856 :
3857 : static struct z_candidate *
3858 770913304 : add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
3859 : tree explicit_targs, tree first_arg,
3860 : const vec<tree, va_gc> *arglist, tree return_type,
3861 : tree access_path, tree conversion_path, int flags,
3862 : unification_kind_t strict, bool shortcut_bad_convs,
3863 : tsubst_flags_t complain)
3864 : {
3865 770913304 : return
3866 0 : add_template_candidate_real (candidates, tmpl, ctype,
3867 : explicit_targs, first_arg, arglist,
3868 : return_type, access_path, conversion_path,
3869 : flags, NULL_TREE, strict, shortcut_bad_convs,
3870 770899753 : complain);
3871 : }
3872 :
3873 : /* Create an overload candidate for the conversion function template TMPL,
3874 : returning RETURN_TYPE, which will be invoked for expression OBJ to produce a
3875 : pointer-to-function which will in turn be called with the argument list
3876 : ARGLIST, and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
3877 : passed on to implicit_conversion. */
3878 :
3879 : static struct z_candidate *
3880 12 : add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
3881 : tree obj,
3882 : const vec<tree, va_gc> *arglist,
3883 : tree return_type, tree access_path,
3884 : tree conversion_path, tsubst_flags_t complain)
3885 : {
3886 12 : return
3887 12 : add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
3888 : NULL_TREE, arglist, return_type, access_path,
3889 : conversion_path, 0, obj, DEDUCE_CALL,
3890 12 : /*shortcut_bad_convs=*/false, complain);
3891 : }
3892 :
3893 : /* The CANDS are the set of candidates that were considered for
3894 : overload resolution. Sort CANDS so that the strictly viable
3895 : candidates appear first, followed by non-strictly viable candidates,
3896 : followed by non-viable candidates. Returns the first candidate
3897 : in this sorted list. If any of the candidates were viable, set
3898 : *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
3899 : considered viable only if it is strictly viable when setting
3900 : *ANY_VIABLE_P. */
3901 :
3902 : static struct z_candidate*
3903 424091229 : splice_viable (struct z_candidate *cands,
3904 : bool strict_p,
3905 : bool *any_viable_p)
3906 : {
3907 424091229 : z_candidate *strictly_viable = nullptr;
3908 424091229 : z_candidate **strictly_viable_tail = &strictly_viable;
3909 :
3910 424091229 : z_candidate *non_strictly_viable = nullptr;
3911 424091229 : z_candidate **non_strictly_viable_tail = &non_strictly_viable;
3912 :
3913 424091229 : z_candidate *non_viable = nullptr;
3914 424091229 : z_candidate **non_viable_tail = &non_viable;
3915 :
3916 424091229 : z_candidate *non_viable_ignored = nullptr;
3917 424091229 : z_candidate **non_viable_ignored_tail = &non_viable_ignored;
3918 :
3919 : /* Be strict inside templates, since build_over_call won't actually
3920 : do the conversions to get pedwarns. */
3921 424091229 : if (processing_template_decl)
3922 55911709 : strict_p = true;
3923 :
3924 1635931567 : for (z_candidate *cand = cands; cand; cand = cand->next)
3925 : {
3926 1211840338 : if (!strict_p
3927 436473347 : && (cand->viable == 1 || TREE_CODE (cand->fn) == TEMPLATE_DECL))
3928 : /* Be strict in the presence of a viable candidate. Also if
3929 : there are template candidates, so that we get deduction errors
3930 : for them instead of silently preferring a bad conversion. */
3931 1076294134 : strict_p = true;
3932 :
3933 : /* Move this candidate to the appropriate list according to
3934 : its viability. */
3935 1211840338 : auto& tail = (cand->viable == 1 ? strictly_viable_tail
3936 : : cand->viable == -1 ? non_strictly_viable_tail
3937 1954575180 : : ignored_candidate_p (cand) ? non_viable_ignored_tail
3938 1954575180 : : non_viable_tail);
3939 1211840338 : *tail = cand;
3940 1211840338 : tail = &cand->next;
3941 : }
3942 :
3943 848182458 : *any_viable_p = (strictly_viable != nullptr
3944 424091229 : || (!strict_p && non_strictly_viable != nullptr));
3945 :
3946 : /* Combine the lists. */
3947 424091229 : *non_viable_ignored_tail = nullptr;
3948 424091229 : *non_viable_tail = non_viable_ignored;
3949 424091229 : *non_strictly_viable_tail = non_viable;
3950 424091229 : *strictly_viable_tail = non_strictly_viable;
3951 :
3952 424091229 : return strictly_viable;
3953 : }
3954 :
3955 : static bool
3956 405232516 : any_strictly_viable (struct z_candidate *cands)
3957 : {
3958 582869004 : for (; cands; cands = cands->next)
3959 190103615 : if (cands->viable == 1)
3960 : return true;
3961 : return false;
3962 : }
3963 :
3964 : /* OBJ is being used in an expression like "OBJ.f (...)". In other
3965 : words, it is about to become the "this" pointer for a member
3966 : function call. Take the address of the object. */
3967 :
3968 : static tree
3969 94738284 : build_this (tree obj)
3970 : {
3971 : /* In a template, we are only concerned about the type of the
3972 : expression, so we can take a shortcut. */
3973 94738284 : if (processing_template_decl)
3974 73123 : return build_address (obj);
3975 :
3976 94665161 : return cp_build_addr_expr (obj, tf_warning_or_error);
3977 : }
3978 :
3979 : /* Returns true iff functions are equivalent. Equivalent functions are
3980 : not '==' only if one is a function-local extern function or if
3981 : both are extern "C". */
3982 :
3983 : static inline int
3984 6873682 : equal_functions (tree fn1, tree fn2)
3985 : {
3986 6873682 : if (TREE_CODE (fn1) != TREE_CODE (fn2))
3987 : return 0;
3988 6858337 : if (TREE_CODE (fn1) == TEMPLATE_DECL)
3989 36043 : return fn1 == fn2;
3990 13644570 : if (DECL_LOCAL_DECL_P (fn1) || DECL_LOCAL_DECL_P (fn2)
3991 13644567 : || DECL_EXTERN_C_FUNCTION_P (fn1))
3992 2881390 : return decls_match (fn1, fn2);
3993 3940904 : return fn1 == fn2;
3994 : }
3995 :
3996 : /* Print information about a candidate FN being rejected due to INFO. */
3997 :
3998 : static void
3999 5295 : print_conversion_rejection (location_t loc, struct conversion_info *info,
4000 : tree fn)
4001 : {
4002 5295 : tree from = info->from;
4003 5295 : if (!TYPE_P (from))
4004 4355 : from = lvalue_type (from);
4005 5295 : if (info->n_arg == -1)
4006 : {
4007 : /* Conversion of implicit `this' argument failed. */
4008 24 : if (!TYPE_P (info->from))
4009 : /* A bad conversion for 'this' must be discarding cv-quals. */
4010 24 : inform (loc, "passing %qT as %<this%> "
4011 : "argument discards qualifiers",
4012 : from);
4013 : else
4014 0 : inform (loc, "no known conversion for implicit "
4015 : "%<this%> parameter from %qH to %qI",
4016 : from, info->to_type);
4017 : }
4018 5271 : else if (!TYPE_P (info->from))
4019 : {
4020 4331 : if (info->n_arg >= 0)
4021 4331 : inform (loc, "conversion of argument %d would be ill-formed:",
4022 : info->n_arg + 1);
4023 4331 : iloc_sentinel ils = loc;
4024 4331 : perform_implicit_conversion (info->to_type, info->from,
4025 : tf_warning_or_error);
4026 4331 : }
4027 940 : else if (info->n_arg == -2)
4028 : /* Conversion of conversion function return value failed. */
4029 33 : inform (loc, "no known conversion from %qH to %qI",
4030 : from, info->to_type);
4031 : else
4032 : {
4033 907 : if (TREE_CODE (fn) == FUNCTION_DECL)
4034 751 : loc = get_fndecl_argument_location (fn, info->n_arg);
4035 907 : inform (loc, "no known conversion for argument %d from %qH to %qI",
4036 907 : info->n_arg + 1, from, info->to_type);
4037 : }
4038 5295 : }
4039 :
4040 : /* Print information about a candidate with WANT parameters and we found
4041 : HAVE. */
4042 :
4043 : static void
4044 1449 : print_arity_information (location_t loc, unsigned int have, unsigned int want,
4045 : bool least_p)
4046 : {
4047 1449 : if (least_p)
4048 41 : inform_n (loc, want,
4049 : "candidate expects at least %d argument, %d provided",
4050 : "candidate expects at least %d arguments, %d provided",
4051 : want, have);
4052 : else
4053 1408 : inform_n (loc, want,
4054 : "candidate expects %d argument, %d provided",
4055 : "candidate expects %d arguments, %d provided",
4056 : want, have);
4057 1449 : }
4058 :
4059 : /* Print information about one overload candidate CANDIDATE. MSGSTR
4060 : is the text to print before the candidate itself.
4061 :
4062 : NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
4063 : to have been run through gettext by the caller. This wart makes
4064 : life simpler in print_z_candidates and for the translators. */
4065 :
4066 : static void
4067 13153 : print_z_candidate (location_t loc, const char *msgstr,
4068 : struct z_candidate *candidate)
4069 : {
4070 13153 : const char *msg = (msgstr == NULL
4071 13153 : ? ""
4072 13153 : : ACONCAT ((_(msgstr), " ", NULL)));
4073 13153 : tree fn = candidate->fn;
4074 13153 : if (flag_new_inheriting_ctors)
4075 13126 : fn = strip_inheriting_ctors (fn);
4076 13153 : location_t cloc = location_of (fn);
4077 :
4078 13153 : if (identifier_p (fn))
4079 : {
4080 214 : cloc = loc;
4081 214 : if (candidate->num_convs == 3)
4082 0 : inform (cloc, "%s%<%D(%T, %T, %T)%> (built-in)", msg, fn,
4083 0 : candidate->convs[0]->type,
4084 0 : candidate->convs[1]->type,
4085 0 : candidate->convs[2]->type);
4086 214 : else if (candidate->num_convs == 2)
4087 190 : inform (cloc, "%s%<%D(%T, %T)%> (built-in)", msg, fn,
4088 190 : candidate->convs[0]->type,
4089 190 : candidate->convs[1]->type);
4090 : else
4091 24 : inform (cloc, "%s%<%D(%T)%> (built-in)", msg, fn,
4092 24 : candidate->convs[0]->type);
4093 : }
4094 12939 : else if (TYPE_P (fn))
4095 11 : inform (cloc, "%s%qT (conversion)", msg, fn);
4096 12928 : else if (candidate->viable == -1)
4097 4366 : inform (cloc, "%s%#qD (near match)", msg, fn);
4098 8562 : else if (ignored_candidate_p (candidate))
4099 6 : inform (cloc, "%s%#qD (ignored)", msg, fn);
4100 8556 : else if (DECL_DELETED_FN (fn))
4101 60 : inform (cloc, "%s%#qD (deleted)", msg, fn);
4102 8496 : else if (candidate->reversed ())
4103 300 : inform (cloc, "%s%#qD (reversed)", msg, fn);
4104 8196 : else if (candidate->rewritten ())
4105 150 : inform (cloc, "%s%#qD (rewritten)", msg, fn);
4106 : else
4107 8046 : inform (cloc, "%s%#qD", msg, fn);
4108 :
4109 13153 : auto_diagnostic_nesting_level sentinel;
4110 :
4111 13153 : if (fn != candidate->fn)
4112 : {
4113 57 : cloc = location_of (candidate->fn);
4114 57 : inform (cloc, "inherited here");
4115 : }
4116 :
4117 : /* Give the user some information about why this candidate failed. */
4118 13153 : if (candidate->reason != NULL)
4119 : {
4120 10068 : struct rejection_reason *r = candidate->reason;
4121 :
4122 10068 : switch (r->code)
4123 : {
4124 1449 : case rr_arity:
4125 1449 : print_arity_information (cloc, r->u.arity.actual,
4126 1449 : r->u.arity.expected,
4127 1449 : r->u.arity.least_p);
4128 1449 : break;
4129 913 : case rr_arg_conversion:
4130 913 : print_conversion_rejection (cloc, &r->u.conversion, fn);
4131 913 : break;
4132 4382 : case rr_bad_arg_conversion:
4133 4382 : print_conversion_rejection (cloc, &r->u.bad_conversion, fn);
4134 4382 : break;
4135 11 : case rr_explicit_conversion:
4136 11 : inform (cloc, "return type %qT of explicit conversion function "
4137 : "cannot be converted to %qT with a qualification "
4138 : "conversion", r->u.conversion.from,
4139 : r->u.conversion.to_type);
4140 11 : break;
4141 6 : case rr_template_conversion:
4142 6 : inform (cloc, "conversion from return type %qT of template "
4143 : "conversion function specialization to %qT is not an "
4144 : "exact match", r->u.conversion.from,
4145 : r->u.conversion.to_type);
4146 6 : break;
4147 3174 : case rr_template_unification:
4148 : /* We use template_unification_error_rejection if unification caused
4149 : actual non-SFINAE errors, in which case we don't need to repeat
4150 : them here. */
4151 3174 : if (r->u.template_unification.tmpl == NULL_TREE)
4152 : {
4153 45 : inform (cloc, "substitution of deduced template arguments "
4154 : "resulted in errors seen above");
4155 45 : break;
4156 : }
4157 : /* Re-run template unification with diagnostics. */
4158 3129 : inform (cloc, "template argument deduction/substitution failed:");
4159 3129 : {
4160 3129 : auto_diagnostic_nesting_level sentinel;
4161 3129 : fn_type_unification (r->u.template_unification.tmpl,
4162 : r->u.template_unification.explicit_targs,
4163 : (make_tree_vec
4164 : (r->u.template_unification.num_targs)),
4165 : r->u.template_unification.args,
4166 : r->u.template_unification.nargs,
4167 : r->u.template_unification.return_type,
4168 : r->u.template_unification.strict,
4169 : r->u.template_unification.flags,
4170 : NULL, true, false);
4171 3129 : }
4172 3129 : break;
4173 2 : case rr_invalid_copy:
4174 2 : inform (cloc,
4175 : "a constructor taking a single argument of its own "
4176 : "class type is invalid");
4177 2 : break;
4178 93 : case rr_constraint_failure:
4179 93 : diagnose_constraints (cloc, fn, NULL_TREE);
4180 93 : break;
4181 32 : case rr_inherited_ctor:
4182 32 : inform (cloc, "an inherited constructor is not a candidate for "
4183 : "initialization from an expression of the same or derived "
4184 : "type");
4185 32 : break;
4186 : case rr_ignored:
4187 : break;
4188 0 : case rr_none:
4189 0 : default:
4190 : /* This candidate didn't have any issues or we failed to
4191 : handle a particular code. Either way... */
4192 0 : gcc_unreachable ();
4193 : }
4194 : }
4195 13153 : }
4196 :
4197 : /* Print information about each overload candidate in CANDIDATES,
4198 : which is assumed to have gone through splice_viable and tourney
4199 : (if splice_viable succeeded). */
4200 :
4201 : static void
4202 5433 : print_z_candidates (location_t loc, struct z_candidate *candidates,
4203 : tristate only_viable_p /* = tristate::unknown () */)
4204 : {
4205 5433 : struct z_candidate *cand1;
4206 5433 : struct z_candidate **cand2;
4207 :
4208 5433 : if (!candidates)
4209 1004 : return;
4210 :
4211 : /* Remove non-viable deleted candidates. */
4212 4429 : cand1 = candidates;
4213 20157 : for (cand2 = &cand1; *cand2; )
4214 : {
4215 15728 : if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
4216 11389 : && !(*cand2)->viable
4217 19487 : && DECL_DELETED_FN ((*cand2)->fn))
4218 102 : *cand2 = (*cand2)->next;
4219 : else
4220 15626 : cand2 = &(*cand2)->next;
4221 : }
4222 : /* ...if there are any non-deleted ones. */
4223 4429 : if (cand1)
4224 4423 : candidates = cand1;
4225 :
4226 : /* There may be duplicates in the set of candidates. We put off
4227 : checking this condition as long as possible, since we have no way
4228 : to eliminate duplicates from a set of functions in less than n^2
4229 : time. Now we are about to emit an error message, so it is more
4230 : permissible to go slowly. */
4231 19755 : for (cand1 = candidates; cand1; cand1 = cand1->next)
4232 : {
4233 15326 : tree fn = cand1->fn;
4234 : /* Skip builtin candidates and conversion functions. */
4235 15326 : if (!DECL_P (fn))
4236 285 : continue;
4237 15041 : cand2 = &cand1->next;
4238 143140 : while (*cand2)
4239 : {
4240 128099 : if (DECL_P ((*cand2)->fn)
4241 128099 : && equal_functions (fn, (*cand2)->fn))
4242 306 : *cand2 = (*cand2)->next;
4243 : else
4244 127793 : cand2 = &(*cand2)->next;
4245 : }
4246 : }
4247 :
4248 : /* Unless otherwise specified, if there's a (strictly) viable candidate
4249 : then we assume we're being called as part of diagnosing ambiguity, in
4250 : which case we want to print only viable candidates since non-viable
4251 : candidates couldn't have contributed to the ambiguity. */
4252 4429 : if (only_viable_p.is_unknown ())
4253 4420 : only_viable_p = candidates->viable == 1;
4254 :
4255 4429 : auto_diagnostic_nesting_level sentinel;
4256 :
4257 4429 : int num_candidates = 0;
4258 17470 : for (auto iter = candidates; iter; iter = iter->next)
4259 : {
4260 13382 : if (only_viable_p.is_true () && iter->viable != 1)
4261 : break;
4262 13041 : ++num_candidates;
4263 : }
4264 :
4265 4429 : inform_num_candidates (loc, num_candidates);
4266 :
4267 4429 : auto_diagnostic_nesting_level sentinel2;
4268 :
4269 4429 : int candidate_idx = 0;
4270 21866 : for (; candidates; candidates = candidates->next)
4271 : {
4272 13378 : if (only_viable_p.is_true () && candidates->viable != 1)
4273 : break;
4274 13072 : if (ignored_candidate_p (candidates) && !flag_diagnostics_all_candidates)
4275 : {
4276 29 : inform (loc, "some candidates omitted; "
4277 : "use %<-fdiagnostics-all-candidates%> to display them");
4278 29 : break;
4279 : }
4280 13008 : pretty_printer pp;
4281 13008 : pp_printf (&pp, N_("candidate %i:"), candidate_idx + 1);
4282 13008 : const char *const msgstr = pp_formatted_text (&pp);
4283 13008 : print_z_candidate (loc, msgstr, candidates);
4284 13008 : ++candidate_idx;
4285 13008 : }
4286 4429 : }
4287 :
4288 : /* USER_SEQ is a user-defined conversion sequence, beginning with a
4289 : USER_CONV. STD_SEQ is the standard conversion sequence applied to
4290 : the result of the conversion function to convert it to the final
4291 : desired type. Merge the two sequences into a single sequence,
4292 : and return the merged sequence. */
4293 :
4294 : static conversion *
4295 17200517 : merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
4296 : {
4297 17200517 : conversion **t;
4298 17200517 : bool bad = user_seq->bad_p;
4299 :
4300 17200517 : gcc_assert (user_seq->kind == ck_user);
4301 :
4302 : /* Find the end of the second conversion sequence. */
4303 18092569 : for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
4304 : {
4305 : /* The entire sequence is a user-conversion sequence. */
4306 892052 : (*t)->user_conv_p = true;
4307 892052 : if (bad)
4308 2535 : (*t)->bad_p = true;
4309 : }
4310 :
4311 17200517 : if ((*t)->rvaluedness_matches_p)
4312 : /* We're binding a reference directly to the result of the conversion.
4313 : build_user_type_conversion_1 stripped the REFERENCE_TYPE from the return
4314 : type, but we want it back. */
4315 607288 : user_seq->type = TREE_TYPE (TREE_TYPE (user_seq->cand->fn));
4316 :
4317 : /* Replace the identity conversion with the user conversion
4318 : sequence. */
4319 17200517 : *t = user_seq;
4320 :
4321 17200517 : return std_seq;
4322 : }
4323 :
4324 : /* Handle overload resolution for initializing an object of class type from
4325 : an initializer list. First we look for a suitable constructor that
4326 : takes a std::initializer_list; if we don't find one, we then look for a
4327 : non-list constructor.
4328 :
4329 : Parameters are as for add_candidates, except that the arguments are in
4330 : the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
4331 : the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
4332 :
4333 : static void
4334 2699729 : add_list_candidates (tree fns, tree first_arg,
4335 : const vec<tree, va_gc> *args, tree totype,
4336 : tree explicit_targs, bool template_only,
4337 : tree conversion_path, tree access_path,
4338 : int flags,
4339 : struct z_candidate **candidates,
4340 : tsubst_flags_t complain)
4341 : {
4342 2699729 : gcc_assert (*candidates == NULL);
4343 :
4344 : /* We're looking for a ctor for list-initialization. */
4345 2699729 : flags |= LOOKUP_LIST_INIT_CTOR;
4346 : /* And we don't allow narrowing conversions. We also use this flag to
4347 : avoid the copy constructor call for copy-list-initialization. */
4348 2699729 : flags |= LOOKUP_NO_NARROWING;
4349 :
4350 5399458 : unsigned nart = num_artificial_parms_for (OVL_FIRST (fns)) - 1;
4351 2699729 : tree init_list = (*args)[nart];
4352 :
4353 : /* Always use the default constructor if the list is empty (DR 990). */
4354 2699729 : if (CONSTRUCTOR_NELTS (init_list) == 0
4355 2699729 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
4356 : ;
4357 2400110 : else if (CONSTRUCTOR_IS_DESIGNATED_INIT (init_list)
4358 2400110 : && !CP_AGGREGATE_TYPE_P (totype))
4359 : {
4360 48 : if (complain & tf_error)
4361 12 : error ("designated initializers cannot be used with a "
4362 : "non-aggregate type %qT", totype);
4363 6491 : return;
4364 : }
4365 : /* If the class has a list ctor, try passing the list as a single
4366 : argument first, but only consider list ctors. */
4367 2400062 : else if (TYPE_HAS_LIST_CTOR (totype))
4368 : {
4369 88602 : flags |= LOOKUP_LIST_ONLY;
4370 88602 : add_candidates (fns, first_arg, args, NULL_TREE,
4371 : explicit_targs, template_only, conversion_path,
4372 : access_path, flags, candidates, complain);
4373 177204 : if (any_strictly_viable (*candidates))
4374 : return;
4375 : }
4376 :
4377 : /* Expand the CONSTRUCTOR into a new argument vec. */
4378 2693238 : vec<tree, va_gc> *new_args;
4379 5086158 : vec_alloc (new_args, nart + CONSTRUCTOR_NELTS (init_list));
4380 2693241 : for (unsigned i = 0; i < nart; ++i)
4381 3 : new_args->quick_push ((*args)[i]);
4382 2693238 : new_args = append_ctor_to_tree_vector (new_args, init_list);
4383 :
4384 : /* We aren't looking for list-ctors anymore. */
4385 2693238 : flags &= ~LOOKUP_LIST_ONLY;
4386 : /* We allow more user-defined conversions within an init-list. */
4387 2693238 : flags &= ~LOOKUP_NO_CONVERSION;
4388 :
4389 2693238 : add_candidates (fns, first_arg, new_args, NULL_TREE,
4390 : explicit_targs, template_only, conversion_path,
4391 : access_path, flags, candidates, complain);
4392 : }
4393 :
4394 : /* Given C(std::initializer_list<A>), return A. */
4395 :
4396 : static tree
4397 1215 : list_ctor_element_type (tree fn)
4398 : {
4399 1215 : gcc_checking_assert (is_list_ctor (fn));
4400 :
4401 1215 : tree parm = FUNCTION_FIRST_USER_PARMTYPE (fn);
4402 1215 : parm = non_reference (TREE_VALUE (parm));
4403 1215 : return TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
4404 : }
4405 :
4406 : /* If EXPR is a braced-init-list where the elements all decay to the same type,
4407 : return that type. */
4408 :
4409 : static tree
4410 1277 : braced_init_element_type (tree expr)
4411 : {
4412 1277 : if (TREE_CODE (expr) == CONSTRUCTOR
4413 1277 : && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
4414 0 : return TREE_TYPE (TREE_TYPE (expr));
4415 1277 : if (!BRACE_ENCLOSED_INITIALIZER_P (expr))
4416 : return NULL_TREE;
4417 :
4418 1277 : tree elttype = NULL_TREE;
4419 13742 : for (constructor_elt &e: CONSTRUCTOR_ELTS (expr))
4420 : {
4421 10087 : tree type = TREE_TYPE (e.value);
4422 10087 : type = type_decays_to (type);
4423 10087 : if (!elttype)
4424 : elttype = type;
4425 8829 : else if (!same_type_p (type, elttype))
4426 : return NULL_TREE;
4427 : }
4428 : return elttype;
4429 : }
4430 :
4431 : /* True iff EXPR contains any temporaries with non-trivial destruction.
4432 :
4433 : ??? Also ignore classes with non-trivial but no-op destruction other than
4434 : std::allocator? */
4435 :
4436 : static bool
4437 191 : has_non_trivial_temporaries (tree expr)
4438 : {
4439 191 : auto_vec<tree*> temps;
4440 191 : cp_walk_tree_without_duplicates (&expr, find_temps_r, &temps);
4441 380 : for (tree *p : temps)
4442 : {
4443 63 : tree t = TREE_TYPE (*p);
4444 63 : if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (t)
4445 63 : && !is_std_allocator (t))
4446 : return true;
4447 : }
4448 : return false;
4449 191 : }
4450 :
4451 : /* Return number of initialized elements in CTOR. */
4452 :
4453 : unsigned HOST_WIDE_INT
4454 249 : count_ctor_elements (tree ctor)
4455 : {
4456 249 : unsigned HOST_WIDE_INT len = 0;
4457 2051 : for (constructor_elt &e: CONSTRUCTOR_ELTS (ctor))
4458 1304 : if (TREE_CODE (e.value) == RAW_DATA_CST)
4459 9 : len += RAW_DATA_LENGTH (e.value);
4460 : else
4461 1295 : ++len;
4462 249 : return len;
4463 : }
4464 :
4465 : /* We're initializing an array of ELTTYPE from INIT. If it seems useful,
4466 : return INIT as an array (of its own type) so the caller can initialize the
4467 : target array in a loop. */
4468 :
4469 : static tree
4470 10228 : maybe_init_list_as_array (tree elttype, tree init)
4471 : {
4472 : /* Only do this if the array can go in rodata but not once converted. */
4473 10228 : if (!TYPE_NON_AGGREGATE_CLASS (elttype))
4474 : return NULL_TREE;
4475 1277 : tree init_elttype = braced_init_element_type (init);
4476 1277 : if (!init_elttype || !SCALAR_TYPE_P (init_elttype) || !TREE_CONSTANT (init))
4477 : return NULL_TREE;
4478 :
4479 : /* Check with a stub expression to weed out special cases, and check whether
4480 : we call the same function for direct-init as copy-list-init. */
4481 353 : conversion_obstack_sentinel cos;
4482 353 : init_elttype = cp_build_qualified_type (init_elttype, TYPE_QUAL_CONST);
4483 353 : tree arg = build_stub_object (init_elttype);
4484 353 : conversion *c = implicit_conversion (elttype, init_elttype, arg, false,
4485 : LOOKUP_NORMAL, tf_none);
4486 353 : if (c && c->kind == ck_rvalue)
4487 0 : c = next_conversion (c);
4488 347 : if (!c || c->kind != ck_user)
4489 : return NULL_TREE;
4490 : /* Check that we actually can perform the conversion. */
4491 344 : if (convert_like (c, arg, tf_none) == error_mark_node)
4492 : /* Let the normal code give the error. */
4493 : return NULL_TREE;
4494 :
4495 : /* A glvalue initializer might be significant to a reference constructor
4496 : or conversion operator. */
4497 338 : if (!DECL_CONSTRUCTOR_P (c->cand->fn)
4498 338 : || (TYPE_REF_P (TREE_VALUE
4499 : (FUNCTION_FIRST_USER_PARMTYPE (c->cand->fn)))))
4500 240 : for (auto &ce : CONSTRUCTOR_ELTS (init))
4501 108 : if (non_mergeable_glvalue_p (ce.value))
4502 : return NULL_TREE;
4503 :
4504 335 : tree first = CONSTRUCTOR_ELT (init, 0)->value;
4505 335 : conversion *fc = implicit_conversion (elttype, init_elttype, first, false,
4506 : LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING,
4507 : tf_none);
4508 335 : if (fc && fc->kind == ck_rvalue)
4509 48 : fc = next_conversion (fc);
4510 335 : if (!fc || fc->kind != ck_user || fc->cand->fn != c->cand->fn)
4511 : return NULL_TREE;
4512 293 : first = convert_like (fc, first, tf_none);
4513 293 : if (first == error_mark_node)
4514 : /* Let the normal code give the error. */
4515 : return NULL_TREE;
4516 :
4517 : /* Don't do this if the conversion would be constant. */
4518 290 : first = maybe_constant_init (first);
4519 290 : if (TREE_CONSTANT (first))
4520 : return NULL_TREE;
4521 :
4522 : /* We can't do this if the conversion creates temporaries that need
4523 : to live until the whole array is initialized. */
4524 191 : if (has_non_trivial_temporaries (first))
4525 : return NULL_TREE;
4526 :
4527 : /* We can't do this if copying from the initializer_list would be
4528 : ill-formed. */
4529 191 : tree copy_argtypes = make_tree_vec (1);
4530 191 : TREE_VEC_ELT (copy_argtypes, 0)
4531 191 : = cp_build_qualified_type (elttype, TYPE_QUAL_CONST);
4532 191 : if (!is_xible (INIT_EXPR, elttype, copy_argtypes))
4533 : return NULL_TREE;
4534 :
4535 185 : unsigned HOST_WIDE_INT len = count_ctor_elements (init);
4536 185 : tree arr = build_array_of_n_type (init_elttype, len);
4537 185 : arr = finish_compound_literal (arr, init, tf_none);
4538 185 : DECL_MERGEABLE (TARGET_EXPR_SLOT (arr)) = true;
4539 185 : return arr;
4540 353 : }
4541 :
4542 : /* If we were going to call e.g. vector(initializer_list<string>) starting
4543 : with a list of string-literals (which is inefficient, see PR105838),
4544 : instead build an array of const char* and pass it to the range constructor.
4545 : But only do this for standard library types, where we can assume the
4546 : transformation makes sense.
4547 :
4548 : Really the container classes should have initializer_list<U> constructors to
4549 : get the same effect more simply; this is working around that lack. */
4550 :
4551 : static tree
4552 16605087 : maybe_init_list_as_range (tree fn, tree expr)
4553 : {
4554 16605087 : if (!processing_template_decl
4555 16105779 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
4556 1030005 : && is_list_ctor (fn)
4557 16606455 : && decl_in_std_namespace_p (fn))
4558 : {
4559 1215 : tree to = list_ctor_element_type (fn);
4560 1215 : if (tree init = maybe_init_list_as_array (to, expr))
4561 : {
4562 53 : tree begin = decay_conversion (TARGET_EXPR_SLOT (init), tf_none);
4563 53 : tree nelts = array_type_nelts_top (TREE_TYPE (init));
4564 53 : tree end = cp_build_binary_op (input_location, PLUS_EXPR, begin,
4565 : nelts, tf_none);
4566 53 : begin = cp_build_compound_expr (init, begin, tf_none);
4567 53 : return build_constructor_va (init_list_type_node, 2,
4568 53 : NULL_TREE, begin, NULL_TREE, end);
4569 : }
4570 : }
4571 :
4572 : return NULL_TREE;
4573 : }
4574 :
4575 : /* Returns the best overload candidate to perform the requested
4576 : conversion. This function is used for three the overloading situations
4577 : described in [over.match.copy], [over.match.conv], and [over.match.ref].
4578 : If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
4579 : per [dcl.init.ref], so we ignore temporary bindings. */
4580 :
4581 : static struct z_candidate *
4582 127194868 : build_user_type_conversion_1 (tree totype, tree expr, int flags,
4583 : tsubst_flags_t complain)
4584 : {
4585 127194868 : struct z_candidate *candidates, *cand;
4586 127194868 : tree fromtype;
4587 127194868 : tree ctors = NULL_TREE;
4588 127194868 : tree conv_fns = NULL_TREE;
4589 127194868 : conversion *conv = NULL;
4590 127194868 : tree first_arg = NULL_TREE;
4591 127194868 : vec<tree, va_gc> *args = NULL;
4592 127194868 : bool any_viable_p;
4593 127194868 : int convflags;
4594 :
4595 127194868 : if (!expr)
4596 : return NULL;
4597 :
4598 127194862 : fromtype = TREE_TYPE (expr);
4599 :
4600 : /* We represent conversion within a hierarchy using RVALUE_CONV and
4601 : BASE_CONV, as specified by [over.best.ics]; these become plain
4602 : constructor calls, as specified in [dcl.init]. */
4603 127194862 : gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
4604 : || !DERIVED_FROM_P (totype, fromtype));
4605 :
4606 127194862 : if (CLASS_TYPE_P (totype))
4607 : /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
4608 : creating a garbage BASELINK; constructors can't be inherited. */
4609 73810533 : ctors = get_class_binding (totype, complete_ctor_identifier);
4610 :
4611 127194862 : tree to_nonref = non_reference (totype);
4612 127194862 : if (MAYBE_CLASS_TYPE_P (fromtype))
4613 : {
4614 84953056 : if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
4615 84925776 : (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
4616 69485606 : && DERIVED_FROM_P (to_nonref, fromtype)))
4617 : {
4618 : /* [class.conv.fct] A conversion function is never used to
4619 : convert a (possibly cv-qualified) object to the (possibly
4620 : cv-qualified) same object type (or a reference to it), to a
4621 : (possibly cv-qualified) base class of that type (or a
4622 : reference to it)... */
4623 : }
4624 : else
4625 84925773 : conv_fns = lookup_conversions (fromtype);
4626 : }
4627 :
4628 127194862 : candidates = 0;
4629 127194862 : flags |= LOOKUP_NO_CONVERSION;
4630 127194862 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4631 1087136 : flags |= LOOKUP_NO_NARROWING;
4632 : /* Prevent add_candidates from treating a non-strictly viable candidate
4633 : as unviable. */
4634 127194862 : complain |= tf_conv;
4635 :
4636 : /* It's OK to bind a temporary for converting constructor arguments, but
4637 : not in converting the return value of a conversion operator. */
4638 127194862 : convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION
4639 127194862 : | (flags & LOOKUP_NO_NARROWING));
4640 127194862 : flags &= ~LOOKUP_NO_TEMP_BIND;
4641 :
4642 127194862 : if (ctors)
4643 : {
4644 73601861 : int ctorflags = flags;
4645 :
4646 73601861 : first_arg = build_dummy_object (totype);
4647 :
4648 : /* We should never try to call the abstract or base constructor
4649 : from here. */
4650 516153053 : gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_FIRST (ctors))
4651 : && !DECL_HAS_VTT_PARM_P (OVL_FIRST (ctors)));
4652 :
4653 73601861 : args = make_tree_vector_single (expr);
4654 73601861 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4655 : {
4656 : /* List-initialization. */
4657 1087136 : add_list_candidates (ctors, first_arg, args, totype, NULL_TREE,
4658 1087136 : false, TYPE_BINFO (totype), TYPE_BINFO (totype),
4659 : ctorflags, &candidates, complain);
4660 : }
4661 : else
4662 : {
4663 72514725 : add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
4664 72514725 : TYPE_BINFO (totype), TYPE_BINFO (totype),
4665 : ctorflags, &candidates, complain);
4666 : }
4667 :
4668 393644357 : for (cand = candidates; cand; cand = cand->next)
4669 : {
4670 320042496 : cand->second_conv = build_identity_conv (totype, NULL_TREE);
4671 :
4672 : /* If totype isn't a reference, and LOOKUP_ONLYCONVERTING is
4673 : set, then this is copy-initialization. In that case, "The
4674 : result of the call is then used to direct-initialize the
4675 : object that is the destination of the copy-initialization."
4676 : [dcl.init]
4677 :
4678 : We represent this in the conversion sequence with an
4679 : rvalue conversion, which means a constructor call. */
4680 320042496 : if (!TYPE_REF_P (totype)
4681 320042496 : && cxx_dialect < cxx17
4682 415273 : && (flags & LOOKUP_ONLYCONVERTING)
4683 359409 : && !(convflags & LOOKUP_NO_TEMP_BIND))
4684 112829 : cand->second_conv
4685 112829 : = build_conv (ck_rvalue, totype, cand->second_conv);
4686 : }
4687 : }
4688 :
4689 127194862 : if (conv_fns)
4690 : {
4691 30210554 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4692 0 : first_arg = CONSTRUCTOR_ELT (expr, 0)->value;
4693 : else
4694 127194862 : first_arg = expr;
4695 : }
4696 :
4697 162332137 : for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
4698 : {
4699 35137275 : tree conversion_path = TREE_PURPOSE (conv_fns);
4700 35137275 : struct z_candidate *old_candidates;
4701 :
4702 : /* If LOOKUP_NO_CONVERSION, don't consider a conversion function that
4703 : would need an addional user-defined conversion, i.e. if the return
4704 : type differs in class-ness from the desired type. So we avoid
4705 : considering operator bool when calling a copy constructor.
4706 :
4707 : This optimization avoids the failure in PR97600, and is allowed by
4708 : [temp.inst]/9: "If the function selected by overload resolution can be
4709 : determined without instantiating a class template definition, it is
4710 : unspecified whether that instantiation actually takes place." */
4711 35137275 : tree convtype = non_reference (TREE_TYPE (conv_fns));
4712 51244130 : if ((flags & LOOKUP_NO_CONVERSION)
4713 35137275 : && !WILDCARD_TYPE_P (convtype)
4714 67030332 : && (CLASS_TYPE_P (to_nonref)
4715 33515166 : != CLASS_TYPE_P (convtype)))
4716 16106855 : continue;
4717 :
4718 : /* If we are called to convert to a reference type, we are trying to
4719 : find a direct binding, so don't even consider temporaries. If
4720 : we don't find a direct binding, the caller will try again to
4721 : look for a temporary binding. */
4722 19030420 : if (TYPE_REF_P (totype))
4723 5091602 : convflags |= LOOKUP_NO_TEMP_BIND;
4724 :
4725 19030420 : old_candidates = candidates;
4726 19030420 : add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
4727 : NULL_TREE, false,
4728 19030420 : conversion_path, TYPE_BINFO (fromtype),
4729 : flags, &candidates, complain);
4730 :
4731 38060835 : for (cand = candidates; cand != old_candidates; cand = cand->next)
4732 : {
4733 19030415 : if (cand->viable == 0)
4734 : /* Already rejected, don't change to -1. */
4735 6463044 : continue;
4736 :
4737 12567371 : tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
4738 12567371 : conversion *ics
4739 12567371 : = implicit_conversion (totype,
4740 : rettype,
4741 : 0,
4742 : /*c_cast_p=*/false, convflags,
4743 : complain);
4744 :
4745 : /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
4746 : copy-initialization. In that case, "The result of the
4747 : call is then used to direct-initialize the object that is
4748 : the destination of the copy-initialization." [dcl.init]
4749 :
4750 : We represent this in the conversion sequence with an
4751 : rvalue conversion, which means a constructor call. But
4752 : don't add a second rvalue conversion if there's already
4753 : one there. Which there really shouldn't be, but it's
4754 : harmless since we'd add it here anyway. */
4755 4597971 : if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
4756 13413463 : && !(convflags & LOOKUP_NO_TEMP_BIND))
4757 252641 : ics = build_conv (ck_rvalue, totype, ics);
4758 :
4759 12567371 : cand->second_conv = ics;
4760 :
4761 12567371 : if (!ics)
4762 : {
4763 7969400 : cand->viable = 0;
4764 15937269 : cand->reason = arg_conversion_rejection (NULL_TREE, -2,
4765 : rettype, totype,
4766 7969400 : EXPR_LOCATION (expr));
4767 : }
4768 14075 : else if (TYPE_REF_P (totype) && !ics->rvaluedness_matches_p
4769 : /* Limit this to non-templates for now (PR90546). */
4770 1359 : && !cand->template_decl
4771 4599325 : && TREE_CODE (TREE_TYPE (totype)) != FUNCTION_TYPE)
4772 : {
4773 : /* If we are called to convert to a reference type, we are trying
4774 : to find a direct binding per [over.match.ref], so rvaluedness
4775 : must match for non-functions. */
4776 1348 : cand->viable = 0;
4777 : }
4778 4596623 : else if (DECL_NONCONVERTING_P (cand->fn)
4779 4596623 : && ics->rank > cr_exact)
4780 : {
4781 : /* 13.3.1.5: For direct-initialization, those explicit
4782 : conversion functions that are not hidden within S and
4783 : yield type T or a type that can be converted to type T
4784 : with a qualification conversion (4.4) are also candidate
4785 : functions. */
4786 : /* 13.3.1.6 doesn't have a parallel restriction, but it should;
4787 : I've raised this issue with the committee. --jason 9/2011 */
4788 2567 : cand->viable = -1;
4789 2567 : cand->reason = explicit_conversion_rejection (rettype, totype);
4790 : }
4791 4594056 : else if (cand->viable == 1 && ics->bad_p)
4792 : {
4793 1657 : cand->viable = -1;
4794 1657 : cand->reason
4795 3314 : = bad_arg_conversion_rejection (NULL_TREE, -2,
4796 : rettype, totype,
4797 1657 : EXPR_LOCATION (expr));
4798 : }
4799 4592399 : else if (primary_template_specialization_p (cand->fn)
4800 4592399 : && ics->rank > cr_exact)
4801 : {
4802 : /* 13.3.3.1.2: If the user-defined conversion is specified by
4803 : a specialization of a conversion function template, the
4804 : second standard conversion sequence shall have exact match
4805 : rank. */
4806 21 : cand->viable = -1;
4807 21 : cand->reason = template_conversion_rejection (rettype, totype);
4808 : }
4809 : }
4810 : }
4811 :
4812 127194862 : candidates = splice_viable (candidates, false, &any_viable_p);
4813 127194862 : if (!any_viable_p)
4814 : {
4815 110588437 : if (args)
4816 60733946 : release_tree_vector (args);
4817 110588437 : return NULL;
4818 : }
4819 :
4820 16606425 : cand = tourney (candidates, complain);
4821 16606425 : if (cand == NULL)
4822 : {
4823 1338 : if (complain & tf_error)
4824 : {
4825 71 : auto_diagnostic_group d;
4826 74 : error_at (cp_expr_loc_or_input_loc (expr),
4827 : "conversion from %qH to %qI is ambiguous",
4828 : fromtype, totype);
4829 71 : print_z_candidates (location_of (expr), candidates);
4830 71 : }
4831 :
4832 1338 : cand = candidates; /* any one will do */
4833 1338 : cand->second_conv = build_ambiguous_conv (totype, expr);
4834 1338 : cand->second_conv->user_conv_p = true;
4835 2676 : if (!any_strictly_viable (candidates))
4836 12 : cand->second_conv->bad_p = true;
4837 1338 : if (flags & LOOKUP_ONLYCONVERTING)
4838 1265 : cand->second_conv->need_temporary_p = true;
4839 : /* If there are viable candidates, don't set ICS_BAD_FLAG; an
4840 : ambiguous conversion is no worse than another user-defined
4841 : conversion. */
4842 :
4843 1338 : return cand;
4844 : }
4845 :
4846 : /* Maybe pass { } as iterators instead of an initializer_list. */
4847 16605087 : if (tree iters = maybe_init_list_as_range (cand->fn, expr))
4848 106 : if (z_candidate *cand2
4849 53 : = build_user_type_conversion_1 (totype, iters, flags, tf_none))
4850 50 : if (cand2->viable == 1 && !is_list_ctor (cand2->fn))
4851 : {
4852 : cand = cand2;
4853 : expr = iters;
4854 : }
4855 :
4856 16605087 : tree convtype;
4857 33210174 : if (!DECL_CONSTRUCTOR_P (cand->fn))
4858 4584576 : convtype = non_reference (TREE_TYPE (TREE_TYPE (cand->fn)));
4859 12020511 : else if (cand->second_conv->kind == ck_rvalue)
4860 : /* DR 5: [in the first step of copy-initialization]...if the function
4861 : is a constructor, the call initializes a temporary of the
4862 : cv-unqualified version of the destination type. */
4863 11646 : convtype = cv_unqualified (totype);
4864 : else
4865 : convtype = totype;
4866 : /* Build the user conversion sequence. */
4867 16605087 : conv = build_conv
4868 16605087 : (ck_user,
4869 : convtype,
4870 16605087 : build_identity_conv (TREE_TYPE (expr), expr));
4871 16605087 : conv->cand = cand;
4872 16605087 : if (cand->viable == -1)
4873 3927 : conv->bad_p = true;
4874 :
4875 : /* Remember that this was a list-initialization. */
4876 16605087 : if (flags & LOOKUP_NO_NARROWING)
4877 3390454 : conv->check_narrowing = true;
4878 :
4879 : /* Combine it with the second conversion sequence. */
4880 16605087 : cand->second_conv = merge_conversion_sequences (conv,
4881 : cand->second_conv);
4882 :
4883 16605087 : return cand;
4884 : }
4885 :
4886 : /* Wrapper for above. */
4887 :
4888 : tree
4889 51138 : build_user_type_conversion (tree totype, tree expr, int flags,
4890 : tsubst_flags_t complain)
4891 : {
4892 51138 : struct z_candidate *cand;
4893 51138 : tree ret;
4894 :
4895 51138 : auto_cond_timevar tv (TV_OVERLOAD);
4896 :
4897 51138 : conversion_obstack_sentinel cos;
4898 :
4899 51138 : cand = build_user_type_conversion_1 (totype, expr, flags, complain);
4900 :
4901 51138 : if (cand)
4902 : {
4903 50935 : if (cand->second_conv->kind == ck_ambig)
4904 65 : ret = error_mark_node;
4905 : else
4906 : {
4907 50870 : expr = convert_like (cand->second_conv, expr, complain);
4908 50870 : ret = convert_from_reference (expr);
4909 : }
4910 : }
4911 : else
4912 : ret = NULL_TREE;
4913 :
4914 102276 : return ret;
4915 51138 : }
4916 :
4917 : /* Give a helpful diagnostic when implicit_conversion fails. */
4918 :
4919 : static void
4920 668 : implicit_conversion_error (location_t loc, tree type, tree expr,
4921 : int flags)
4922 : {
4923 668 : tsubst_flags_t complain = tf_warning_or_error;
4924 :
4925 : /* If expr has unknown type, then it is an overloaded function.
4926 : Call instantiate_type to get good error messages. */
4927 668 : if (TREE_TYPE (expr) == unknown_type_node)
4928 58 : instantiate_type (type, expr, complain);
4929 610 : else if (invalid_nonstatic_memfn_p (loc, expr, complain))
4930 : /* We gave an error. */;
4931 70 : else if (BRACE_ENCLOSED_INITIALIZER_P (expr)
4932 67 : && CONSTRUCTOR_IS_DESIGNATED_INIT (expr)
4933 623 : && !CP_AGGREGATE_TYPE_P (type))
4934 18 : error_at (loc, "designated initializers cannot be used with a "
4935 : "non-aggregate type %qT", type);
4936 : else
4937 : {
4938 587 : auto_diagnostic_group d;
4939 587 : if (is_stub_object (expr))
4940 : /* The expression is generated by a trait check, we don't have
4941 : a useful location to highlight the label. */
4942 17 : error_at (loc, "could not convert %qH to %qI",
4943 17 : TREE_TYPE (expr), type);
4944 : else
4945 : {
4946 570 : range_label_for_type_mismatch label (TREE_TYPE (expr), type);
4947 570 : gcc_rich_location rich_loc (loc, &label,
4948 570 : highlight_colors::percent_h);
4949 570 : error_at (&rich_loc, "could not convert %qE from %qH to %qI",
4950 570 : expr, TREE_TYPE (expr), type);
4951 570 : }
4952 587 : maybe_show_nonconverting_candidate (type, TREE_TYPE (expr), expr, flags);
4953 587 : }
4954 668 : }
4955 :
4956 : /* Worker for build_converted_constant_expr. */
4957 :
4958 : static tree
4959 345050609 : build_converted_constant_expr_internal (tree type, tree expr,
4960 : int flags, tsubst_flags_t complain)
4961 : {
4962 345050609 : conversion *conv;
4963 345050609 : tree t;
4964 345050609 : location_t loc = cp_expr_loc_or_input_loc (expr);
4965 :
4966 345050609 : if (error_operand_p (expr))
4967 51 : return error_mark_node;
4968 :
4969 345050558 : conversion_obstack_sentinel cos;
4970 :
4971 345050558 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
4972 : /*c_cast_p=*/false, flags, complain);
4973 :
4974 : /* A converted constant expression of type T is an expression, implicitly
4975 : converted to type T, where the converted expression is a constant
4976 : expression and the implicit conversion sequence contains only
4977 :
4978 : * user-defined conversions,
4979 : * lvalue-to-rvalue conversions (7.1),
4980 : * array-to-pointer conversions (7.2),
4981 : * function-to-pointer conversions (7.3),
4982 : * qualification conversions (7.5),
4983 : * integral promotions (7.6),
4984 : * integral conversions (7.8) other than narrowing conversions (11.6.4),
4985 : * null pointer conversions (7.11) from std::nullptr_t,
4986 : * null member pointer conversions (7.12) from std::nullptr_t, and
4987 : * function pointer conversions (7.13),
4988 :
4989 : and where the reference binding (if any) binds directly. */
4990 :
4991 345050558 : for (conversion *c = conv;
4992 408264184 : c && c->kind != ck_identity;
4993 63213626 : c = next_conversion (c))
4994 : {
4995 63213626 : switch (c->kind)
4996 : {
4997 : /* A conversion function is OK. If it isn't constexpr, we'll
4998 : complain later that the argument isn't constant. */
4999 : case ck_user:
5000 : /* List-initialization is OK. */
5001 : case ck_aggr:
5002 : /* The lvalue-to-rvalue conversion is OK. */
5003 : case ck_rvalue:
5004 : /* Array-to-pointer and function-to-pointer. */
5005 : case ck_lvalue:
5006 : /* Function pointer conversions. */
5007 : case ck_fnptr:
5008 : /* Qualification conversions. */
5009 : case ck_qual:
5010 : break;
5011 :
5012 536 : case ck_ref_bind:
5013 536 : if (c->need_temporary_p)
5014 : {
5015 0 : if (complain & tf_error)
5016 0 : error_at (loc, "initializing %qH with %qI in converted "
5017 : "constant expression does not bind directly",
5018 0 : type, next_conversion (c)->type);
5019 : conv = NULL;
5020 : }
5021 : break;
5022 :
5023 10344764 : case ck_base:
5024 10344764 : case ck_pmem:
5025 10344764 : case ck_ptr:
5026 10344764 : case ck_std:
5027 10344764 : t = next_conversion (c)->type;
5028 10344764 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (t)
5029 10344689 : && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5030 : /* Integral promotion or conversion. */
5031 : break;
5032 106 : if (NULLPTR_TYPE_P (t))
5033 : /* Conversion from nullptr to pointer or pointer-to-member. */
5034 : break;
5035 :
5036 106 : if (complain & tf_error)
5037 80 : error_at (loc, "conversion from %qH to %qI in a "
5038 : "converted constant expression", t, type);
5039 : /* fall through. */
5040 :
5041 : default:
5042 : conv = NULL;
5043 : break;
5044 : }
5045 : }
5046 :
5047 : /* Avoid confusing convert_nontype_argument by introducing
5048 : a redundant conversion to the same reference type. */
5049 345050310 : if (conv && conv->kind == ck_ref_bind
5050 345051094 : && REFERENCE_REF_P (expr))
5051 : {
5052 290 : tree ref = TREE_OPERAND (expr, 0);
5053 290 : if (same_type_p (type, TREE_TYPE (ref)))
5054 : return ref;
5055 : }
5056 :
5057 345050290 : if (conv)
5058 : {
5059 : /* Don't copy a class in a template. */
5060 10655 : if (CLASS_TYPE_P (type) && conv->kind == ck_rvalue
5061 345058721 : && processing_template_decl)
5062 51 : conv = next_conversion (conv);
5063 :
5064 : /* Issuing conversion warnings for value-dependent expressions is
5065 : likely too noisy. */
5066 345050042 : warning_sentinel w (warn_conversion);
5067 345050042 : conv->check_narrowing = true;
5068 345050042 : conv->check_narrowing_const_only = true;
5069 345050042 : expr = convert_like (conv, expr, complain);
5070 345050042 : }
5071 : else
5072 : {
5073 248 : if (complain & tf_error)
5074 189 : implicit_conversion_error (loc, type, expr, flags);
5075 248 : expr = error_mark_node;
5076 : }
5077 :
5078 : return expr;
5079 345050558 : }
5080 :
5081 : /* Subroutine of convert_nontype_argument.
5082 :
5083 : EXPR is an expression used in a context that requires a converted
5084 : constant-expression, such as a template non-type parameter. Do any
5085 : necessary conversions (that are permitted for converted
5086 : constant-expressions) to convert it to the desired type.
5087 :
5088 : This function doesn't consider explicit conversion functions. If
5089 : you mean to use "a contextually converted constant expression of type
5090 : bool", use build_converted_constant_bool_expr.
5091 :
5092 : If conversion is successful, returns the converted expression;
5093 : otherwise, returns error_mark_node. */
5094 :
5095 : tree
5096 214293201 : build_converted_constant_expr (tree type, tree expr, tsubst_flags_t complain)
5097 : {
5098 214293201 : return build_converted_constant_expr_internal (type, expr, LOOKUP_IMPLICIT,
5099 214293201 : complain);
5100 : }
5101 :
5102 : /* Used to create "a contextually converted constant expression of type
5103 : bool". This differs from build_converted_constant_expr in that it
5104 : also considers explicit conversion functions. */
5105 :
5106 : tree
5107 130757408 : build_converted_constant_bool_expr (tree expr, tsubst_flags_t complain)
5108 : {
5109 130757408 : return build_converted_constant_expr_internal (boolean_type_node, expr,
5110 130757408 : LOOKUP_NORMAL, complain);
5111 : }
5112 :
5113 : /* Do any initial processing on the arguments to a function call. */
5114 :
5115 : vec<tree, va_gc> *
5116 272518272 : resolve_args (vec<tree, va_gc> *args, tsubst_flags_t complain)
5117 : {
5118 272518272 : unsigned int ix;
5119 272518272 : tree arg;
5120 :
5121 501399103 : FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
5122 : {
5123 228881431 : if (error_operand_p (arg))
5124 : return NULL;
5125 228880963 : else if (VOID_TYPE_P (TREE_TYPE (arg)))
5126 : {
5127 105 : if (complain & tf_error)
5128 12 : error_at (cp_expr_loc_or_input_loc (arg),
5129 : "invalid use of void expression");
5130 105 : return NULL;
5131 : }
5132 228880858 : else if (invalid_nonstatic_memfn_p (EXPR_LOCATION (arg), arg, complain))
5133 : return NULL;
5134 :
5135 : /* Force auto deduction now. Omit tf_warning to avoid redundant
5136 : deprecated warning on deprecated-14.C. */
5137 228880831 : if (!mark_single_function (arg, complain & ~tf_warning))
5138 : return NULL;
5139 : }
5140 : return args;
5141 : }
5142 :
5143 : /* Perform overload resolution on FN, which is called with the ARGS.
5144 :
5145 : Return the candidate function selected by overload resolution, or
5146 : NULL if the event that overload resolution failed. In the case
5147 : that overload resolution fails, *CANDIDATES will be the set of
5148 : candidates considered, and ANY_VIABLE_P will be set to true or
5149 : false to indicate whether or not any of the candidates were
5150 : viable.
5151 :
5152 : The ARGS should already have gone through RESOLVE_ARGS before this
5153 : function is called. */
5154 :
5155 : static struct z_candidate *
5156 135160066 : perform_overload_resolution (tree fn,
5157 : const vec<tree, va_gc> *args,
5158 : struct z_candidate **candidates,
5159 : bool *any_viable_p, tsubst_flags_t complain)
5160 : {
5161 135160066 : struct z_candidate *cand;
5162 135160066 : tree explicit_targs;
5163 135160066 : int template_only;
5164 :
5165 135160066 : auto_cond_timevar tv (TV_OVERLOAD);
5166 :
5167 135160066 : explicit_targs = NULL_TREE;
5168 135160066 : template_only = 0;
5169 :
5170 135160066 : *candidates = NULL;
5171 135160066 : *any_viable_p = true;
5172 :
5173 : /* Check FN. */
5174 135160066 : gcc_assert (OVL_P (fn) || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
5175 :
5176 135160066 : if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
5177 : {
5178 52026754 : explicit_targs = TREE_OPERAND (fn, 1);
5179 52026754 : fn = TREE_OPERAND (fn, 0);
5180 52026754 : template_only = 1;
5181 : }
5182 :
5183 : /* Add the various candidate functions. */
5184 135160066 : add_candidates (fn, NULL_TREE, args, NULL_TREE,
5185 : explicit_targs, template_only,
5186 : /*conversion_path=*/NULL_TREE,
5187 : /*access_path=*/NULL_TREE,
5188 : LOOKUP_NORMAL,
5189 : candidates, complain & ~tf_any_viable);
5190 :
5191 135146548 : *candidates = splice_viable (*candidates, false, any_viable_p);
5192 135146548 : if (*any_viable_p)
5193 : {
5194 134882902 : if (complain & tf_any_viable)
5195 : cand = *candidates;
5196 : else
5197 134882794 : cand = tourney (*candidates, complain);
5198 : }
5199 : else
5200 : cand = NULL;
5201 :
5202 270293096 : return cand;
5203 135146548 : }
5204 :
5205 : /* Print an error message about being unable to build a call to FN with
5206 : ARGS. ANY_VIABLE_P indicates whether any candidate functions could
5207 : be located; CANDIDATES is a possibly empty list of such
5208 : functions. */
5209 :
5210 : static void
5211 2890 : print_error_for_call_failure (tree fn, const vec<tree, va_gc> *args,
5212 : struct z_candidate *candidates)
5213 : {
5214 2890 : tree targs = NULL_TREE;
5215 2890 : if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
5216 : {
5217 498 : targs = TREE_OPERAND (fn, 1);
5218 498 : fn = TREE_OPERAND (fn, 0);
5219 : }
5220 5780 : tree name = OVL_NAME (fn);
5221 2890 : location_t loc = location_of (name);
5222 2890 : if (targs)
5223 498 : name = lookup_template_function (name, targs);
5224 :
5225 2890 : auto_diagnostic_group d;
5226 5780 : if (!any_strictly_viable (candidates))
5227 2416 : error_at (loc, "no matching function for call to %<%D(%A)%>",
5228 : name, build_tree_list_vec (args));
5229 : else
5230 474 : error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous",
5231 : name, build_tree_list_vec (args));
5232 2890 : if (candidates)
5233 2890 : print_z_candidates (loc, candidates);
5234 2890 : }
5235 :
5236 : /* Perform overload resolution on the set of deduction guides DGUIDES
5237 : using ARGS. Returns the selected deduction guide, or error_mark_node
5238 : if overload resolution fails. */
5239 :
5240 : tree
5241 40942 : perform_dguide_overload_resolution (tree dguides, const vec<tree, va_gc> *args,
5242 : tsubst_flags_t complain)
5243 : {
5244 40942 : z_candidate *candidates;
5245 40942 : bool any_viable_p;
5246 40942 : tree result;
5247 :
5248 81884 : gcc_assert (deduction_guide_p (OVL_FIRST (dguides)));
5249 :
5250 40942 : conversion_obstack_sentinel cos;
5251 :
5252 40942 : z_candidate *cand = perform_overload_resolution (dguides, args, &candidates,
5253 : &any_viable_p, complain);
5254 40942 : if (!cand)
5255 : {
5256 895 : if (complain & tf_error)
5257 192 : print_error_for_call_failure (dguides, args, candidates);
5258 895 : result = error_mark_node;
5259 : }
5260 : else
5261 40047 : result = cand->fn;
5262 :
5263 81884 : return result;
5264 40942 : }
5265 :
5266 : /* Return an expression for a call to FN (a namespace-scope function,
5267 : or a static member function) with the ARGS. This may change
5268 : ARGS. */
5269 :
5270 : tree
5271 134061870 : build_new_function_call (tree fn, vec<tree, va_gc> **args,
5272 : tsubst_flags_t complain)
5273 : {
5274 134061870 : struct z_candidate *candidates, *cand;
5275 134061870 : bool any_viable_p;
5276 134061870 : tree result;
5277 :
5278 134061870 : if (args != NULL && *args != NULL)
5279 : {
5280 134061870 : *args = resolve_args (*args, complain & ~tf_any_viable);
5281 134061870 : if (*args == NULL)
5282 327 : return error_mark_node;
5283 : }
5284 :
5285 134061543 : if (flag_tm)
5286 3561 : tm_malloc_replacement (fn);
5287 :
5288 134061543 : conversion_obstack_sentinel cos;
5289 :
5290 134061543 : cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
5291 : complain);
5292 :
5293 134048025 : if (!cand)
5294 : {
5295 347653 : if (complain & tf_error)
5296 : {
5297 : // If there is a single (non-viable) function candidate,
5298 : // let the error be diagnosed by cp_build_function_call_vec.
5299 3112 : if (!any_viable_p && candidates && ! candidates->next
5300 1340 : && TREE_CODE (candidates->fn) == FUNCTION_DECL
5301 : /* A template-id callee consisting of a single (ignored)
5302 : non-template candidate needs to be diagnosed the
5303 : ordinary way. */
5304 433 : && (TREE_CODE (fn) != TEMPLATE_ID_EXPR
5305 31 : || candidates->template_decl))
5306 423 : return cp_build_function_call_vec (candidates->fn, args, complain);
5307 :
5308 : // Otherwise, emit notes for non-viable candidates.
5309 2689 : print_error_for_call_failure (fn, *args, candidates);
5310 : }
5311 347230 : result = error_mark_node;
5312 : }
5313 133700372 : else if (complain & tf_any_viable)
5314 108 : return void_node;
5315 : else
5316 133700264 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
5317 :
5318 134047494 : if (flag_coroutines
5319 131986650 : && result
5320 131986650 : && TREE_CODE (result) == CALL_EXPR
5321 208725163 : && DECL_BUILT_IN_CLASS (TREE_OPERAND (CALL_EXPR_FN (result), 0))
5322 74677669 : == BUILT_IN_NORMAL)
5323 8737945 : result = coro_validate_builtin_call (result);
5324 :
5325 : return result;
5326 134048025 : }
5327 :
5328 : /* Build a call to a global operator new. FNNAME is the name of the
5329 : operator (either "operator new" or "operator new[]") and ARGS are
5330 : the arguments provided. This may change ARGS. *SIZE points to the
5331 : total number of bytes required by the allocation, and is updated if
5332 : that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
5333 : be used. If this function determines that no cookie should be
5334 : used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK
5335 : is not NULL_TREE, it is evaluated before calculating the final
5336 : array size, and if it fails, the array size is replaced with
5337 : (size_t)-1 (usually triggering a std::bad_alloc exception). If FN
5338 : is non-NULL, it will be set, upon return, to the allocation
5339 : function called. */
5340 :
5341 : tree
5342 1057558 : build_operator_new_call (tree fnname, vec<tree, va_gc> **args,
5343 : tree *size, tree *cookie_size,
5344 : tree align_arg, tree size_check,
5345 : tree *fn, tsubst_flags_t complain)
5346 : {
5347 1057558 : tree original_size = *size;
5348 1057558 : tree fns;
5349 1057558 : struct z_candidate *candidates;
5350 1057558 : struct z_candidate *cand = NULL;
5351 1057558 : bool any_viable_p;
5352 :
5353 1057558 : if (fn)
5354 1056138 : *fn = NULL_TREE;
5355 : /* Set to (size_t)-1 if the size check fails. */
5356 1057558 : if (size_check != NULL_TREE)
5357 : {
5358 20064 : tree errval = TYPE_MAX_VALUE (sizetype);
5359 20064 : if (cxx_dialect >= cxx11 && flag_exceptions)
5360 19678 : errval = throw_bad_array_new_length ();
5361 20064 : *size = fold_build3 (COND_EXPR, sizetype, size_check,
5362 : original_size, errval);
5363 : }
5364 1057558 : vec_safe_insert (*args, 0, *size);
5365 1057558 : *args = resolve_args (*args, complain);
5366 1057558 : if (*args == NULL)
5367 0 : return error_mark_node;
5368 :
5369 1057558 : conversion_obstack_sentinel cos;
5370 :
5371 : /* Based on:
5372 :
5373 : [expr.new]
5374 :
5375 : If this lookup fails to find the name, or if the allocated type
5376 : is not a class type, the allocation function's name is looked
5377 : up in the global scope.
5378 :
5379 : we disregard block-scope declarations of "operator new". */
5380 1057558 : fns = lookup_qualified_name (global_namespace, fnname);
5381 :
5382 1057558 : if (align_arg)
5383 : {
5384 8976 : vec<tree, va_gc>* align_args
5385 8976 : = vec_copy_and_insert (*args, align_arg, 1);
5386 8976 : cand = perform_overload_resolution (fns, align_args, &candidates,
5387 : &any_viable_p, tf_none);
5388 8976 : if (cand)
5389 8953 : *args = align_args;
5390 : /* If no aligned allocation function matches, try again without the
5391 : alignment. */
5392 : }
5393 :
5394 : /* Figure out what function is being called. */
5395 8953 : if (!cand)
5396 1048605 : cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p,
5397 : complain);
5398 :
5399 : /* If no suitable function could be found, issue an error message
5400 : and give up. */
5401 1057558 : if (!cand)
5402 : {
5403 9 : if (complain & tf_error)
5404 9 : print_error_for_call_failure (fns, *args, candidates);
5405 9 : return error_mark_node;
5406 : }
5407 :
5408 : /* If a cookie is required, add some extra space. Whether
5409 : or not a cookie is required cannot be determined until
5410 : after we know which function was called. */
5411 1057549 : if (*cookie_size)
5412 : {
5413 268 : bool use_cookie = true;
5414 268 : tree arg_types;
5415 :
5416 268 : arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
5417 : /* Skip the size_t parameter. */
5418 268 : arg_types = TREE_CHAIN (arg_types);
5419 : /* Check the remaining parameters (if any). */
5420 268 : if (arg_types
5421 268 : && TREE_CHAIN (arg_types) == void_list_node
5422 331 : && same_type_p (TREE_VALUE (arg_types),
5423 : ptr_type_node))
5424 48 : use_cookie = false;
5425 : /* If we need a cookie, adjust the number of bytes allocated. */
5426 268 : if (use_cookie)
5427 : {
5428 : /* Update the total size. */
5429 220 : *size = size_binop (PLUS_EXPR, original_size, *cookie_size);
5430 220 : if (size_check)
5431 : {
5432 : /* Set to (size_t)-1 if the size check fails. */
5433 47 : gcc_assert (size_check != NULL_TREE);
5434 47 : *size = fold_build3 (COND_EXPR, sizetype, size_check,
5435 : *size, TYPE_MAX_VALUE (sizetype));
5436 : }
5437 : /* Update the argument list to reflect the adjusted size. */
5438 220 : (**args)[0] = *size;
5439 : }
5440 : else
5441 48 : *cookie_size = NULL_TREE;
5442 : }
5443 :
5444 : /* Tell our caller which function we decided to call. */
5445 1057549 : if (fn)
5446 1056129 : *fn = cand->fn;
5447 :
5448 : /* Build the CALL_EXPR. */
5449 1057549 : tree ret = build_over_call (cand, LOOKUP_NORMAL, complain);
5450 :
5451 : /* Set this flag for all callers of this function. In addition to
5452 : new-expressions, this is called for allocating coroutine state; treat
5453 : that as an implicit new-expression. */
5454 1057549 : tree call = extract_call_expr (ret);
5455 1057549 : if (TREE_CODE (call) == CALL_EXPR)
5456 1057549 : CALL_FROM_NEW_OR_DELETE_P (call) = 1;
5457 :
5458 : return ret;
5459 1057558 : }
5460 :
5461 : /* Evaluate side-effects from OBJ before evaluating call
5462 : to FN in RESULT expression.
5463 : This is for expressions of the form `obj->fn(...)'
5464 : where `fn' turns out to be a static member function and
5465 : `obj' needs to be evaluated. `fn' could be also static operator[]
5466 : or static operator(), in which cases the source expression
5467 : would be `obj[...]' or `obj(...)'. */
5468 :
5469 : tree
5470 103076224 : keep_unused_object_arg (tree result, tree obj, tree fn)
5471 : {
5472 103076224 : if (result == NULL_TREE
5473 103076224 : || result == error_mark_node
5474 101935086 : || DECL_OBJECT_MEMBER_FUNCTION_P (fn)
5475 106054074 : || !TREE_SIDE_EFFECTS (obj))
5476 : return result;
5477 :
5478 : /* But avoid the implicit lvalue-rvalue conversion when `obj' is
5479 : volatile. */
5480 82870 : tree a = obj;
5481 82870 : if (TREE_THIS_VOLATILE (a))
5482 55200 : a = build_this (a);
5483 82870 : if (TREE_SIDE_EFFECTS (a))
5484 27679 : return cp_build_compound_expr (a, result, tf_error);
5485 : return result;
5486 : }
5487 :
5488 : /* Build a new call to operator(). This may change ARGS. */
5489 :
5490 : tree
5491 4478784 : build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
5492 : {
5493 4478784 : struct z_candidate *candidates = 0, *cand;
5494 4478784 : tree fns, convs, first_mem_arg = NULL_TREE;
5495 4478784 : bool any_viable_p;
5496 4478784 : tree result = NULL_TREE;
5497 :
5498 4478784 : auto_cond_timevar tv (TV_OVERLOAD);
5499 :
5500 4478784 : obj = mark_lvalue_use (obj);
5501 :
5502 4478784 : if (error_operand_p (obj))
5503 0 : return error_mark_node;
5504 :
5505 4478784 : tree type = TREE_TYPE (obj);
5506 :
5507 4478784 : obj = prep_operand (obj);
5508 :
5509 4478784 : if (TYPE_PTRMEMFUNC_P (type))
5510 : {
5511 0 : if (complain & tf_error)
5512 : /* It's no good looking for an overloaded operator() on a
5513 : pointer-to-member-function. */
5514 0 : error ("pointer-to-member function %qE cannot be called without "
5515 : "an object; consider using %<.*%> or %<->*%>", obj);
5516 0 : return error_mark_node;
5517 : }
5518 :
5519 4478784 : if (TYPE_BINFO (type))
5520 : {
5521 4478766 : fns = lookup_fnfields (TYPE_BINFO (type), call_op_identifier, 1, complain);
5522 4478766 : if (fns == error_mark_node)
5523 : return error_mark_node;
5524 : }
5525 : else
5526 : fns = NULL_TREE;
5527 :
5528 4478774 : if (args != NULL && *args != NULL)
5529 : {
5530 4478774 : *args = resolve_args (*args, complain);
5531 4478774 : if (*args == NULL)
5532 109 : return error_mark_node;
5533 : }
5534 :
5535 4478665 : conversion_obstack_sentinel cos;
5536 :
5537 4478665 : if (fns)
5538 : {
5539 4477216 : first_mem_arg = obj;
5540 :
5541 4477216 : add_candidates (BASELINK_FUNCTIONS (fns),
5542 : first_mem_arg, *args, NULL_TREE,
5543 : NULL_TREE, false,
5544 4477216 : BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
5545 : LOOKUP_NORMAL, &candidates, complain);
5546 : }
5547 :
5548 4478665 : bool any_call_ops = candidates != nullptr;
5549 :
5550 4478665 : convs = lookup_conversions (type);
5551 :
5552 4620255 : for (; convs; convs = TREE_CHAIN (convs))
5553 : {
5554 141590 : tree totype = TREE_TYPE (convs);
5555 :
5556 129347 : if (TYPE_PTRFN_P (totype)
5557 12246 : || TYPE_REFFN_P (totype)
5558 153792 : || (TYPE_REF_P (totype)
5559 888 : && TYPE_PTRFN_P (TREE_TYPE (totype))))
5560 259074 : for (tree fn : ovl_range (TREE_VALUE (convs)))
5561 : {
5562 129537 : if (DECL_NONCONVERTING_P (fn))
5563 3 : continue;
5564 :
5565 129534 : if (TREE_CODE (fn) == TEMPLATE_DECL)
5566 : {
5567 : /* Making this work broke PR 71117 and 85118, so until the
5568 : committee resolves core issue 2189, let's disable this
5569 : candidate if there are any call operators. */
5570 32935 : if (any_call_ops)
5571 32923 : continue;
5572 :
5573 12 : add_template_conv_candidate
5574 12 : (&candidates, fn, obj, *args, totype,
5575 : /*access_path=*/NULL_TREE,
5576 : /*conversion_path=*/NULL_TREE, complain);
5577 : }
5578 : else
5579 96599 : add_conv_candidate (&candidates, fn, obj,
5580 : *args, /*conversion_path=*/NULL_TREE,
5581 : /*access_path=*/NULL_TREE, complain);
5582 : }
5583 : }
5584 :
5585 : /* Be strict here because if we choose a bad conversion candidate, the
5586 : errors we get won't mention the call context. */
5587 4478665 : candidates = splice_viable (candidates, true, &any_viable_p);
5588 4478665 : if (!any_viable_p)
5589 : {
5590 22960 : if (complain & tf_error)
5591 : {
5592 276 : auto_diagnostic_group d;
5593 276 : error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
5594 : build_tree_list_vec (*args));
5595 276 : print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
5596 276 : }
5597 22960 : result = error_mark_node;
5598 : }
5599 : else
5600 : {
5601 4455705 : cand = tourney (candidates, complain);
5602 4455705 : if (cand == 0)
5603 : {
5604 17 : if (complain & tf_error)
5605 : {
5606 6 : auto_diagnostic_group d;
5607 12 : error ("call of %<(%T) (%A)%> is ambiguous",
5608 6 : TREE_TYPE (obj), build_tree_list_vec (*args));
5609 6 : print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
5610 6 : }
5611 17 : result = error_mark_node;
5612 : }
5613 4455688 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL
5614 4455621 : && DECL_OVERLOADED_OPERATOR_P (cand->fn)
5615 8911309 : && DECL_OVERLOADED_OPERATOR_IS (cand->fn, CALL_EXPR))
5616 : {
5617 4455621 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
5618 : /* In an expression of the form `a()' where cand->fn
5619 : which is operator() turns out to be a static member function,
5620 : `a' is none-the-less evaluated. */
5621 4455621 : result = keep_unused_object_arg (result, obj, cand->fn);
5622 : }
5623 : else
5624 : {
5625 67 : if (TREE_CODE (cand->fn) == FUNCTION_DECL)
5626 0 : obj = convert_like_with_context (cand->convs[0], obj, cand->fn,
5627 : -1, complain);
5628 : else
5629 : {
5630 67 : gcc_checking_assert (TYPE_P (cand->fn));
5631 67 : obj = convert_like (cand->convs[0], obj, complain);
5632 : }
5633 67 : obj = convert_from_reference (obj);
5634 67 : result = cp_build_function_call_vec (obj, args, complain);
5635 : }
5636 : }
5637 :
5638 4478665 : return result;
5639 4478784 : }
5640 :
5641 : /* Subroutine for preparing format strings suitable for the error
5642 : function. It concatenates a prefix (controlled by MATCH), ERRMSG,
5643 : and SUFFIX. */
5644 :
5645 : static const char *
5646 1462 : concat_op_error_string (bool match, const char *errmsg, const char *suffix)
5647 : {
5648 1462 : return concat (match
5649 : ? G_("ambiguous overload for ")
5650 : : G_("no match for "),
5651 0 : errmsg, suffix, nullptr);
5652 : }
5653 :
5654 : /* Called by op_error to prepare format strings suitable for the error
5655 : function. It concatenates a prefix (controlled by MATCH), ERRMSG,
5656 : and a suffix (controlled by NTYPES). */
5657 :
5658 : static const char *
5659 1441 : op_error_string (const char *errmsg, int ntypes, bool match)
5660 : {
5661 1441 : const char *suffix;
5662 0 : if (ntypes == 3)
5663 : suffix = G_(" (operand types are %qT, %qT, and %qT)");
5664 0 : else if (ntypes == 2)
5665 : suffix = G_(" (operand types are %qT and %qT)");
5666 : else
5667 0 : suffix = G_(" (operand type is %qT)");
5668 0 : return concat_op_error_string (match, errmsg, suffix);
5669 : }
5670 :
5671 : /* Similar to op_error_string, but a special-case for binary ops that
5672 : use %e for the args, rather than %qT. */
5673 :
5674 : static const char *
5675 21 : binop_error_string (const char *errmsg, bool match)
5676 : {
5677 21 : return concat_op_error_string (match, errmsg,
5678 21 : G_(" (operand types are %e and %e)"));
5679 : }
5680 :
5681 : static void
5682 1462 : op_error (const op_location_t &loc,
5683 : enum tree_code code, enum tree_code code2,
5684 : tree arg1, tree arg2, tree arg3, bool match)
5685 : {
5686 1462 : bool assop = code == MODIFY_EXPR;
5687 1462 : const char *opname = OVL_OP_INFO (assop, assop ? code2 : code)->name;
5688 :
5689 1462 : switch (code)
5690 : {
5691 0 : case COND_EXPR:
5692 0 : if (flag_diagnostics_show_caret)
5693 0 : error_at (loc, op_error_string (G_("ternary %<operator?:%>"),
5694 : 3, match),
5695 0 : TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
5696 : else
5697 0 : error_at (loc, op_error_string (G_("ternary %<operator?:%> "
5698 : "in %<%E ? %E : %E%>"), 3, match),
5699 : arg1, arg2, arg3,
5700 0 : TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
5701 : break;
5702 :
5703 0 : case POSTINCREMENT_EXPR:
5704 0 : case POSTDECREMENT_EXPR:
5705 0 : if (flag_diagnostics_show_caret)
5706 0 : error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
5707 0 : opname, TREE_TYPE (arg1));
5708 : else
5709 0 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
5710 : 1, match),
5711 0 : opname, arg1, opname, TREE_TYPE (arg1));
5712 : break;
5713 :
5714 13 : case ARRAY_REF:
5715 13 : if (flag_diagnostics_show_caret)
5716 0 : error_at (loc, op_error_string (G_("%<operator[]%>"), 2, match),
5717 0 : TREE_TYPE (arg1), TREE_TYPE (arg2));
5718 : else
5719 26 : error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
5720 : 2, match),
5721 13 : arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2));
5722 : break;
5723 :
5724 6 : case REALPART_EXPR:
5725 6 : case IMAGPART_EXPR:
5726 6 : if (flag_diagnostics_show_caret)
5727 0 : error_at (loc, op_error_string (G_("%qs"), 1, match),
5728 0 : opname, TREE_TYPE (arg1));
5729 : else
5730 12 : error_at (loc, op_error_string (G_("%qs in %<%s %E%>"), 1, match),
5731 6 : opname, opname, arg1, TREE_TYPE (arg1));
5732 : break;
5733 :
5734 0 : case CO_AWAIT_EXPR:
5735 0 : if (flag_diagnostics_show_caret)
5736 0 : error_at (loc, op_error_string (G_("%<operator %s%>"), 1, match),
5737 0 : opname, TREE_TYPE (arg1));
5738 : else
5739 0 : error_at (loc, op_error_string (G_("%<operator %s%> in %<%s%E%>"),
5740 : 1, match),
5741 0 : opname, opname, arg1, TREE_TYPE (arg1));
5742 : break;
5743 :
5744 1443 : default:
5745 1443 : if (arg2)
5746 1345 : if (flag_diagnostics_show_caret)
5747 : {
5748 21 : binary_op_rich_location richloc (loc, arg1, arg2, true);
5749 21 : pp_markup::element_quoted_type element_0
5750 21 : (TREE_TYPE (arg1), highlight_colors::lhs);
5751 21 : pp_markup::element_quoted_type element_1
5752 21 : (TREE_TYPE (arg2), highlight_colors::rhs);
5753 21 : error_at (&richloc,
5754 : binop_error_string (G_("%<operator%s%>"), match),
5755 : opname, &element_0, &element_1);
5756 21 : }
5757 : else
5758 2648 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
5759 : 2, match),
5760 : opname, arg1, opname, arg2,
5761 1324 : TREE_TYPE (arg1), TREE_TYPE (arg2));
5762 : else
5763 98 : if (flag_diagnostics_show_caret)
5764 0 : error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
5765 0 : opname, TREE_TYPE (arg1));
5766 : else
5767 196 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
5768 : 1, match),
5769 98 : opname, opname, arg1, TREE_TYPE (arg1));
5770 : break;
5771 : }
5772 1462 : }
5773 :
5774 : /* Return the implicit conversion sequence that could be used to
5775 : convert E1 to E2 in [expr.cond]. */
5776 :
5777 : static conversion *
5778 508122 : conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
5779 : {
5780 508122 : tree t1 = non_reference (TREE_TYPE (e1));
5781 508122 : tree t2 = non_reference (TREE_TYPE (e2));
5782 508122 : conversion *conv;
5783 508122 : bool good_base;
5784 :
5785 : /* [expr.cond]
5786 :
5787 : If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
5788 : implicitly converted (clause _conv_) to the type "lvalue reference to
5789 : T2", subject to the constraint that in the conversion the
5790 : reference must bind directly (_dcl.init.ref_) to an lvalue.
5791 :
5792 : If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
5793 : implicitly converted to the type "rvalue reference to T2", subject to
5794 : the constraint that the reference must bind directly. */
5795 508122 : if (glvalue_p (e2))
5796 : {
5797 480565 : tree rtype = cp_build_reference_type (t2, !lvalue_p (e2));
5798 480565 : conv = implicit_conversion (rtype,
5799 : t1,
5800 : e1,
5801 : /*c_cast_p=*/false,
5802 : LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
5803 : |LOOKUP_ONLYCONVERTING,
5804 : complain);
5805 480565 : if (conv && !conv->bad_p)
5806 : return conv;
5807 : }
5808 :
5809 : /* If E2 is a prvalue or if neither of the conversions above can be done
5810 : and at least one of the operands has (possibly cv-qualified) class
5811 : type: */
5812 405710 : if (!CLASS_TYPE_P (t1) && !CLASS_TYPE_P (t2))
5813 : return NULL;
5814 :
5815 : /* [expr.cond]
5816 :
5817 : If E1 and E2 have class type, and the underlying class types are
5818 : the same or one is a base class of the other: E1 can be converted
5819 : to match E2 if the class of T2 is the same type as, or a base
5820 : class of, the class of T1, and the cv-qualification of T2 is the
5821 : same cv-qualification as, or a greater cv-qualification than, the
5822 : cv-qualification of T1. If the conversion is applied, E1 is
5823 : changed to an rvalue of type T2 that still refers to the original
5824 : source class object (or the appropriate subobject thereof). */
5825 181367 : if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
5826 363561 : && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
5827 : {
5828 54761 : if (good_base && at_least_as_qualified_p (t2, t1))
5829 : {
5830 27286 : conv = build_identity_conv (t1, e1);
5831 27286 : if (!same_type_p (TYPE_MAIN_VARIANT (t1),
5832 : TYPE_MAIN_VARIANT (t2)))
5833 6 : conv = build_conv (ck_base, t2, conv);
5834 : else
5835 27280 : conv = build_conv (ck_rvalue, t2, conv);
5836 27286 : return conv;
5837 : }
5838 : else
5839 27475 : return NULL;
5840 : }
5841 : else
5842 : /* [expr.cond]
5843 :
5844 : Otherwise: E1 can be converted to match E2 if E1 can be implicitly
5845 : converted to the type that expression E2 would have if E2 were
5846 : converted to an rvalue (or the type it has, if E2 is an rvalue). */
5847 249576 : return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
5848 249576 : LOOKUP_IMPLICIT, complain);
5849 : }
5850 :
5851 : /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
5852 : arguments to the conditional expression. */
5853 :
5854 : tree
5855 8590661 : build_conditional_expr (const op_location_t &loc,
5856 : tree arg1, tree arg2, tree arg3,
5857 : tsubst_flags_t complain)
5858 : {
5859 8590661 : tree arg2_type;
5860 8590661 : tree arg3_type;
5861 8590661 : tree result = NULL_TREE;
5862 8590661 : tree result_type = NULL_TREE;
5863 8590661 : tree semantic_result_type = NULL_TREE;
5864 8590661 : bool is_glvalue = true;
5865 8590661 : struct z_candidate *candidates = 0;
5866 8590661 : struct z_candidate *cand;
5867 8590661 : tree orig_arg2, orig_arg3;
5868 :
5869 8590661 : auto_cond_timevar tv (TV_OVERLOAD);
5870 :
5871 : /* As a G++ extension, the second argument to the conditional can be
5872 : omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
5873 : c'.) If the second operand is omitted, make sure it is
5874 : calculated only once. */
5875 8590661 : if (!arg2)
5876 : {
5877 380 : if (complain & tf_error)
5878 374 : pedwarn (loc, OPT_Wpedantic,
5879 : "ISO C++ forbids omitting the middle term of "
5880 : "a %<?:%> expression");
5881 :
5882 380 : if ((complain & tf_warning) && !truth_value_p (TREE_CODE (arg1)))
5883 332 : warn_for_omitted_condop (loc, arg1);
5884 :
5885 : /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
5886 380 : if (glvalue_p (arg1))
5887 : {
5888 89 : arg1 = cp_stabilize_reference (arg1);
5889 89 : arg2 = arg1 = prevent_lifetime_extension (arg1);
5890 : }
5891 291 : else if (TREE_CODE (arg1) == TARGET_EXPR)
5892 : /* arg1 can't be a prvalue result of the conditional
5893 : expression, since it needs to be materialized for the
5894 : conversion to bool, so treat it as an xvalue in arg2. */
5895 6 : arg2 = move (TARGET_EXPR_SLOT (arg1));
5896 285 : else if (TREE_CODE (arg1) == EXCESS_PRECISION_EXPR)
5897 3 : arg2 = arg1 = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (arg1),
5898 3 : cp_save_expr (TREE_OPERAND (arg1, 0)));
5899 : else
5900 282 : arg2 = arg1 = cp_save_expr (arg1);
5901 : }
5902 :
5903 : /* If something has already gone wrong, just pass that fact up the
5904 : tree. */
5905 8590661 : if (error_operand_p (arg1)
5906 8590583 : || error_operand_p (arg2)
5907 17181195 : || error_operand_p (arg3))
5908 169 : return error_mark_node;
5909 :
5910 8590492 : conversion_obstack_sentinel cos;
5911 :
5912 8590492 : orig_arg2 = arg2;
5913 8590492 : orig_arg3 = arg3;
5914 :
5915 8590492 : if (gnu_vector_type_p (TREE_TYPE (arg1))
5916 8590492 : && (VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1))
5917 3 : || VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (arg1))))
5918 : {
5919 1005 : tree arg1_type = TREE_TYPE (arg1);
5920 :
5921 : /* If arg1 is another cond_expr choosing between -1 and 0,
5922 : then we can use its comparison. It may help to avoid
5923 : additional comparison, produce more accurate diagnostics
5924 : and enables folding. */
5925 1005 : if (TREE_CODE (arg1) == VEC_COND_EXPR
5926 915 : && integer_minus_onep (TREE_OPERAND (arg1, 1))
5927 1920 : && integer_zerop (TREE_OPERAND (arg1, 2)))
5928 915 : arg1 = TREE_OPERAND (arg1, 0);
5929 :
5930 1005 : arg1 = force_rvalue (arg1, complain);
5931 1005 : arg2 = force_rvalue (arg2, complain);
5932 1005 : arg3 = force_rvalue (arg3, complain);
5933 :
5934 : /* force_rvalue can return error_mark on valid arguments. */
5935 1005 : if (error_operand_p (arg1)
5936 1005 : || error_operand_p (arg2)
5937 2010 : || error_operand_p (arg3))
5938 0 : return error_mark_node;
5939 :
5940 1005 : arg2_type = TREE_TYPE (arg2);
5941 1005 : arg3_type = TREE_TYPE (arg3);
5942 :
5943 1005 : if (!VECTOR_TYPE_P (arg2_type)
5944 75 : && !VECTOR_TYPE_P (arg3_type))
5945 : {
5946 : /* Rely on the error messages of the scalar version. */
5947 57 : tree scal = build_conditional_expr (loc, integer_one_node,
5948 : orig_arg2, orig_arg3, complain);
5949 57 : if (scal == error_mark_node)
5950 : return error_mark_node;
5951 57 : tree stype = TREE_TYPE (scal);
5952 57 : tree ctype = TREE_TYPE (arg1_type);
5953 57 : if (TYPE_SIZE (stype) != TYPE_SIZE (ctype)
5954 57 : || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype)))
5955 : {
5956 9 : if (complain & tf_error)
5957 9 : error_at (loc, "inferred scalar type %qT is not an integer or "
5958 : "floating-point type of the same size as %qT", stype,
5959 9 : COMPARISON_CLASS_P (arg1)
5960 9 : ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0)))
5961 : : ctype);
5962 9 : return error_mark_node;
5963 : }
5964 :
5965 48 : tree vtype = build_opaque_vector_type (stype,
5966 48 : TYPE_VECTOR_SUBPARTS (arg1_type));
5967 : /* We could pass complain & tf_warning to unsafe_conversion_p,
5968 : but the warnings (like Wsign-conversion) have already been
5969 : given by the scalar build_conditional_expr_1. We still check
5970 : unsafe_conversion_p to forbid truncating long long -> float. */
5971 48 : if (unsafe_conversion_p (stype, arg2, NULL_TREE, false))
5972 : {
5973 0 : if (complain & tf_error)
5974 0 : error_at (loc, "conversion of scalar %qH to vector %qI "
5975 : "involves truncation", arg2_type, vtype);
5976 0 : return error_mark_node;
5977 : }
5978 48 : if (unsafe_conversion_p (stype, arg3, NULL_TREE, false))
5979 : {
5980 3 : if (complain & tf_error)
5981 3 : error_at (loc, "conversion of scalar %qH to vector %qI "
5982 : "involves truncation", arg3_type, vtype);
5983 3 : return error_mark_node;
5984 : }
5985 :
5986 45 : arg2 = cp_convert (stype, arg2, complain);
5987 45 : arg2 = save_expr (arg2);
5988 45 : arg2 = build_vector_from_val (vtype, arg2);
5989 45 : arg2_type = vtype;
5990 45 : arg3 = cp_convert (stype, arg3, complain);
5991 45 : arg3 = save_expr (arg3);
5992 45 : arg3 = build_vector_from_val (vtype, arg3);
5993 45 : arg3_type = vtype;
5994 : }
5995 :
5996 1968 : if ((gnu_vector_type_p (arg2_type) && !VECTOR_TYPE_P (arg3_type))
5997 1890 : || (gnu_vector_type_p (arg3_type) && !VECTOR_TYPE_P (arg2_type)))
5998 : {
5999 96 : enum stv_conv convert_flag =
6000 96 : scalar_to_vector (loc, VEC_COND_EXPR, arg2, arg3,
6001 : complain & tf_error);
6002 :
6003 96 : switch (convert_flag)
6004 : {
6005 0 : case stv_error:
6006 0 : return error_mark_node;
6007 15 : case stv_firstarg:
6008 15 : {
6009 15 : arg2 = save_expr (arg2);
6010 15 : arg2 = convert (TREE_TYPE (arg3_type), arg2);
6011 15 : arg2 = build_vector_from_val (arg3_type, arg2);
6012 15 : arg2_type = TREE_TYPE (arg2);
6013 15 : break;
6014 : }
6015 72 : case stv_secondarg:
6016 72 : {
6017 72 : arg3 = save_expr (arg3);
6018 72 : arg3 = convert (TREE_TYPE (arg2_type), arg3);
6019 72 : arg3 = build_vector_from_val (arg2_type, arg3);
6020 72 : arg3_type = TREE_TYPE (arg3);
6021 72 : break;
6022 : }
6023 : default:
6024 : break;
6025 : }
6026 : }
6027 :
6028 993 : if (!gnu_vector_type_p (arg2_type)
6029 990 : || !gnu_vector_type_p (arg3_type)
6030 984 : || !same_type_p (arg2_type, arg3_type)
6031 984 : || maybe_ne (TYPE_VECTOR_SUBPARTS (arg1_type),
6032 984 : TYPE_VECTOR_SUBPARTS (arg2_type))
6033 1977 : || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type))
6034 : {
6035 9 : if (complain & tf_error)
6036 6 : error_at (loc,
6037 : "incompatible vector types in conditional expression: "
6038 6 : "%qT, %qT and %qT", TREE_TYPE (arg1),
6039 6 : TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3));
6040 9 : return error_mark_node;
6041 : }
6042 :
6043 984 : if (!COMPARISON_CLASS_P (arg1))
6044 : {
6045 87 : tree cmp_type = truth_type_for (arg1_type);
6046 87 : arg1 = build2 (NE_EXPR, cmp_type, arg1, build_zero_cst (arg1_type));
6047 : }
6048 984 : return build3_loc (loc, VEC_COND_EXPR, arg2_type, arg1, arg2, arg3);
6049 : }
6050 :
6051 : /* [expr.cond]
6052 :
6053 : The first expression is implicitly converted to bool (clause
6054 : _conv_). */
6055 8589487 : arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
6056 : LOOKUP_NORMAL);
6057 8589487 : if (error_operand_p (arg1))
6058 25 : return error_mark_node;
6059 :
6060 8589462 : arg2_type = unlowered_expr_type (arg2);
6061 8589462 : arg3_type = unlowered_expr_type (arg3);
6062 :
6063 8589462 : if ((TREE_CODE (arg2) == EXCESS_PRECISION_EXPR
6064 8589444 : || TREE_CODE (arg3) == EXCESS_PRECISION_EXPR)
6065 45 : && (TREE_CODE (arg2_type) == INTEGER_TYPE
6066 : || SCALAR_FLOAT_TYPE_P (arg2_type)
6067 : || TREE_CODE (arg2_type) == COMPLEX_TYPE)
6068 45 : && (TREE_CODE (arg3_type) == INTEGER_TYPE
6069 : || SCALAR_FLOAT_TYPE_P (arg3_type)
6070 : || TREE_CODE (arg3_type) == COMPLEX_TYPE))
6071 : {
6072 45 : semantic_result_type
6073 45 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
6074 45 : if (semantic_result_type == error_mark_node)
6075 : {
6076 0 : tree t1 = arg2_type;
6077 0 : tree t2 = arg3_type;
6078 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6079 0 : t1 = TREE_TYPE (t1);
6080 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6081 0 : t2 = TREE_TYPE (t2);
6082 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
6083 : && SCALAR_FLOAT_TYPE_P (t2)
6084 : && (extended_float_type_p (t1)
6085 : || extended_float_type_p (t2))
6086 : && cp_compare_floating_point_conversion_ranks
6087 : (t1, t2) == 3);
6088 0 : if (complain & tf_error)
6089 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
6090 : "have unordered conversion rank",
6091 : arg2_type, arg3_type);
6092 0 : return error_mark_node;
6093 : }
6094 45 : if (TREE_CODE (arg2) == EXCESS_PRECISION_EXPR)
6095 : {
6096 18 : arg2 = TREE_OPERAND (arg2, 0);
6097 18 : arg2_type = TREE_TYPE (arg2);
6098 : }
6099 45 : if (TREE_CODE (arg3) == EXCESS_PRECISION_EXPR)
6100 : {
6101 27 : arg3 = TREE_OPERAND (arg3, 0);
6102 27 : arg3_type = TREE_TYPE (arg3);
6103 : }
6104 : }
6105 :
6106 : /* [expr.cond]
6107 :
6108 : If either the second or the third operand has type (possibly
6109 : cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
6110 : array-to-pointer (_conv.array_), and function-to-pointer
6111 : (_conv.func_) standard conversions are performed on the second
6112 : and third operands. */
6113 8589462 : if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
6114 : {
6115 : /* 'void' won't help in resolving an overloaded expression on the
6116 : other side, so require it to resolve by itself. */
6117 113903 : if (arg2_type == unknown_type_node)
6118 : {
6119 9 : arg2 = resolve_nondeduced_context_or_error (arg2, complain);
6120 9 : arg2_type = TREE_TYPE (arg2);
6121 : }
6122 113903 : if (arg3_type == unknown_type_node)
6123 : {
6124 0 : arg3 = resolve_nondeduced_context_or_error (arg3, complain);
6125 0 : arg3_type = TREE_TYPE (arg3);
6126 : }
6127 :
6128 : /* [expr.cond]
6129 :
6130 : One of the following shall hold:
6131 :
6132 : --The second or the third operand (but not both) is a
6133 : throw-expression (_except.throw_); the result is of the type
6134 : and value category of the other.
6135 :
6136 : --Both the second and the third operands have type void; the
6137 : result is of type void and is a prvalue. */
6138 113903 : if (TREE_CODE (arg2) == THROW_EXPR
6139 80 : && TREE_CODE (arg3) != THROW_EXPR)
6140 : {
6141 68 : result_type = arg3_type;
6142 68 : is_glvalue = glvalue_p (arg3);
6143 : }
6144 113835 : else if (TREE_CODE (arg2) != THROW_EXPR
6145 113823 : && TREE_CODE (arg3) == THROW_EXPR)
6146 : {
6147 171 : result_type = arg2_type;
6148 171 : is_glvalue = glvalue_p (arg2);
6149 : }
6150 113664 : else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
6151 : {
6152 113635 : result_type = void_type_node;
6153 113635 : is_glvalue = false;
6154 : }
6155 : else
6156 : {
6157 29 : if (complain & tf_error)
6158 : {
6159 18 : if (VOID_TYPE_P (arg2_type))
6160 9 : error_at (cp_expr_loc_or_loc (arg3, loc),
6161 : "second operand to the conditional operator "
6162 : "is of type %<void%>, but the third operand is "
6163 : "neither a throw-expression nor of type %<void%>");
6164 : else
6165 9 : error_at (cp_expr_loc_or_loc (arg2, loc),
6166 : "third operand to the conditional operator "
6167 : "is of type %<void%>, but the second operand is "
6168 : "neither a throw-expression nor of type %<void%>");
6169 : }
6170 29 : return error_mark_node;
6171 : }
6172 :
6173 113874 : goto valid_operands;
6174 : }
6175 : /* [expr.cond]
6176 :
6177 : Otherwise, if the second and third operand have different types,
6178 : and either has (possibly cv-qualified) class type, or if both are
6179 : glvalues of the same value category and the same type except for
6180 : cv-qualification, an attempt is made to convert each of those operands
6181 : to the type of the other. */
6182 8475559 : else if (!same_type_p (arg2_type, arg3_type)
6183 8475559 : && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)
6184 1599195 : || (same_type_ignoring_top_level_qualifiers_p (arg2_type,
6185 : arg3_type)
6186 419660 : && glvalue_p (arg2) && glvalue_p (arg3)
6187 101441 : && lvalue_p (arg2) == lvalue_p (arg3))))
6188 : {
6189 254061 : conversion *conv2;
6190 254061 : conversion *conv3;
6191 254061 : bool converted = false;
6192 :
6193 254061 : conv2 = conditional_conversion (arg2, arg3, complain);
6194 254061 : conv3 = conditional_conversion (arg3, arg2, complain);
6195 :
6196 : /* [expr.cond]
6197 :
6198 : If both can be converted, or one can be converted but the
6199 : conversion is ambiguous, the program is ill-formed. If
6200 : neither can be converted, the operands are left unchanged and
6201 : further checking is performed as described below. If exactly
6202 : one conversion is possible, that conversion is applied to the
6203 : chosen operand and the converted operand is used in place of
6204 : the original operand for the remainder of this section. */
6205 254061 : if ((conv2 && !conv2->bad_p
6206 60828 : && conv3 && !conv3->bad_p)
6207 60813 : || (conv2 && conv2->kind == ck_ambig)
6208 253951 : || (conv3 && conv3->kind == ck_ambig))
6209 : {
6210 110 : if (complain & tf_error)
6211 : {
6212 6 : error_at (loc, "operands to %<?:%> have different types "
6213 : "%qT and %qT",
6214 : arg2_type, arg3_type);
6215 6 : if (conv2 && !conv2->bad_p && conv3 && !conv3->bad_p)
6216 3 : inform (loc, " and each type can be converted to the other");
6217 3 : else if (conv2 && conv2->kind == ck_ambig)
6218 3 : convert_like (conv2, arg2, complain);
6219 : else
6220 0 : convert_like (conv3, arg3, complain);
6221 : }
6222 110 : result = error_mark_node;
6223 : }
6224 253951 : else if (conv2 && !conv2->bad_p)
6225 : {
6226 60718 : arg2 = convert_like (conv2, arg2, complain);
6227 60718 : arg2 = convert_from_reference (arg2);
6228 60718 : arg2_type = TREE_TYPE (arg2);
6229 : /* Even if CONV2 is a valid conversion, the result of the
6230 : conversion may be invalid. For example, if ARG3 has type
6231 : "volatile X", and X does not have a copy constructor
6232 : accepting a "volatile X&", then even if ARG2 can be
6233 : converted to X, the conversion will fail. */
6234 60718 : if (error_operand_p (arg2))
6235 0 : result = error_mark_node;
6236 0 : converted = true;
6237 : }
6238 193233 : else if (conv3 && !conv3->bad_p)
6239 : {
6240 192548 : arg3 = convert_like (conv3, arg3, complain);
6241 192548 : arg3 = convert_from_reference (arg3);
6242 192548 : arg3_type = TREE_TYPE (arg3);
6243 192548 : if (error_operand_p (arg3))
6244 0 : result = error_mark_node;
6245 0 : converted = true;
6246 : }
6247 :
6248 110 : if (result)
6249 : return result;
6250 :
6251 : /* If, after the conversion, both operands have class type,
6252 : treat the cv-qualification of both operands as if it were the
6253 : union of the cv-qualification of the operands.
6254 :
6255 : The standard is not clear about what to do in this
6256 : circumstance. For example, if the first operand has type
6257 : "const X" and the second operand has a user-defined
6258 : conversion to "volatile X", what is the type of the second
6259 : operand after this step? Making it be "const X" (matching
6260 : the first operand) seems wrong, as that discards the
6261 : qualification without actually performing a copy. Leaving it
6262 : as "volatile X" seems wrong as that will result in the
6263 : conditional expression failing altogether, even though,
6264 : according to this step, the one operand could be converted to
6265 : the type of the other. */
6266 253951 : if (converted
6267 253266 : && CLASS_TYPE_P (arg2_type)
6268 283276 : && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
6269 0 : arg2_type = arg3_type =
6270 0 : cp_build_qualified_type (arg2_type,
6271 0 : cp_type_quals (arg2_type)
6272 0 : | cp_type_quals (arg3_type));
6273 : }
6274 :
6275 : /* [expr.cond]
6276 :
6277 : If the second and third operands are glvalues of the same value
6278 : category and have the same type, the result is of that type and
6279 : value category. */
6280 12730367 : if (((lvalue_p (arg2) && lvalue_p (arg3))
6281 4812573 : || (xvalue_p (arg2) && xvalue_p (arg3)))
6282 12184905 : && same_type_p (arg2_type, arg3_type))
6283 : {
6284 3603648 : result_type = arg2_type;
6285 3603648 : goto valid_operands;
6286 : }
6287 :
6288 : /* [expr.cond]
6289 :
6290 : Otherwise, the result is an rvalue. If the second and third
6291 : operand do not have the same type, and either has (possibly
6292 : cv-qualified) class type, overload resolution is used to
6293 : determine the conversions (if any) to be applied to the operands
6294 : (_over.match.oper_, _over.built_). */
6295 4871801 : is_glvalue = false;
6296 4871801 : if (!same_type_p (arg2_type, arg3_type)
6297 4871801 : && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
6298 : {
6299 685 : releasing_vec args;
6300 685 : conversion *conv;
6301 685 : bool any_viable_p;
6302 :
6303 : /* Rearrange the arguments so that add_builtin_candidate only has
6304 : to know about two args. In build_builtin_candidate, the
6305 : arguments are unscrambled. */
6306 685 : args->quick_push (arg2);
6307 685 : args->quick_push (arg3);
6308 685 : args->quick_push (arg1);
6309 685 : add_builtin_candidates (&candidates,
6310 : COND_EXPR,
6311 : NOP_EXPR,
6312 : ovl_op_identifier (false, COND_EXPR),
6313 : args,
6314 : LOOKUP_NORMAL, complain);
6315 :
6316 : /* [expr.cond]
6317 :
6318 : If the overload resolution fails, the program is
6319 : ill-formed. */
6320 685 : candidates = splice_viable (candidates, false, &any_viable_p);
6321 685 : if (!any_viable_p)
6322 : {
6323 569 : if (complain & tf_error)
6324 22 : error_at (loc, "operands to %<?:%> have different types %qT and %qT",
6325 : arg2_type, arg3_type);
6326 569 : return error_mark_node;
6327 : }
6328 116 : cand = tourney (candidates, complain);
6329 116 : if (!cand)
6330 : {
6331 0 : if (complain & tf_error)
6332 : {
6333 0 : auto_diagnostic_group d;
6334 0 : op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, false);
6335 0 : print_z_candidates (loc, candidates);
6336 0 : }
6337 0 : return error_mark_node;
6338 : }
6339 :
6340 : /* [expr.cond]
6341 :
6342 : Otherwise, the conversions thus determined are applied, and
6343 : the converted operands are used in place of the original
6344 : operands for the remainder of this section. */
6345 116 : conv = cand->convs[0];
6346 116 : arg1 = convert_like (conv, arg1, complain);
6347 116 : conv = cand->convs[1];
6348 116 : arg2 = convert_like (conv, arg2, complain);
6349 116 : arg2_type = TREE_TYPE (arg2);
6350 116 : conv = cand->convs[2];
6351 116 : arg3 = convert_like (conv, arg3, complain);
6352 116 : arg3_type = TREE_TYPE (arg3);
6353 685 : }
6354 :
6355 : /* [expr.cond]
6356 :
6357 : Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
6358 : and function-to-pointer (_conv.func_) standard conversions are
6359 : performed on the second and third operands.
6360 :
6361 : We need to force the lvalue-to-rvalue conversion here for class types,
6362 : so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
6363 : that isn't wrapped with a TARGET_EXPR plays havoc with exception
6364 : regions. */
6365 :
6366 4871232 : arg2 = force_rvalue (arg2, complain);
6367 4871232 : if (!CLASS_TYPE_P (arg2_type))
6368 4749735 : arg2_type = TREE_TYPE (arg2);
6369 :
6370 4871232 : arg3 = force_rvalue (arg3, complain);
6371 4871232 : if (!CLASS_TYPE_P (arg3_type))
6372 4749735 : arg3_type = TREE_TYPE (arg3);
6373 :
6374 4871232 : if (arg2 == error_mark_node || arg3 == error_mark_node)
6375 : return error_mark_node;
6376 :
6377 : /* [expr.cond]
6378 :
6379 : After those conversions, one of the following shall hold:
6380 :
6381 : --The second and third operands have the same type; the result is of
6382 : that type. */
6383 4871184 : if (same_type_p (arg2_type, arg3_type))
6384 : result_type = arg2_type;
6385 : /* [expr.cond]
6386 :
6387 : --The second and third operands have arithmetic or enumeration
6388 : type; the usual arithmetic conversions are performed to bring
6389 : them to a common type, and the result is of that type. */
6390 22621 : else if ((ARITHMETIC_TYPE_P (arg2_type)
6391 21977 : || UNSCOPED_ENUM_P (arg2_type))
6392 1076666 : && (ARITHMETIC_TYPE_P (arg3_type)
6393 183 : || UNSCOPED_ENUM_P (arg3_type)))
6394 : {
6395 : /* A conditional expression between a floating-point
6396 : type and an integer type should convert the integer type to
6397 : the evaluation format of the floating-point type, with
6398 : possible excess precision. */
6399 1053931 : tree eptype2 = arg2_type;
6400 1053931 : tree eptype3 = arg3_type;
6401 1053931 : tree eptype;
6402 644 : if (ANY_INTEGRAL_TYPE_P (arg2_type)
6403 1053934 : && (eptype = excess_precision_type (arg3_type)) != NULL_TREE)
6404 : {
6405 3 : eptype3 = eptype;
6406 3 : if (!semantic_result_type)
6407 3 : semantic_result_type
6408 3 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
6409 : }
6410 352 : else if (ANY_INTEGRAL_TYPE_P (arg3_type)
6411 1053928 : && (eptype = excess_precision_type (arg2_type)) != NULL_TREE)
6412 : {
6413 23 : eptype2 = eptype;
6414 23 : if (!semantic_result_type)
6415 23 : semantic_result_type
6416 23 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
6417 : }
6418 1053931 : result_type = type_after_usual_arithmetic_conversions (eptype2,
6419 : eptype3);
6420 1053931 : if (result_type == error_mark_node)
6421 : {
6422 0 : tree t1 = eptype2;
6423 0 : tree t2 = eptype3;
6424 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6425 0 : t1 = TREE_TYPE (t1);
6426 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6427 0 : t2 = TREE_TYPE (t2);
6428 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
6429 : && SCALAR_FLOAT_TYPE_P (t2)
6430 : && (extended_float_type_p (t1)
6431 : || extended_float_type_p (t2))
6432 : && cp_compare_floating_point_conversion_ranks
6433 : (t1, t2) == 3);
6434 0 : if (complain & tf_error)
6435 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
6436 : "have unordered conversion rank",
6437 : eptype2, eptype3);
6438 0 : return error_mark_node;
6439 : }
6440 1053931 : if (semantic_result_type == error_mark_node)
6441 : {
6442 0 : tree t1 = arg2_type;
6443 0 : tree t2 = arg3_type;
6444 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6445 0 : t1 = TREE_TYPE (t1);
6446 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6447 0 : t2 = TREE_TYPE (t2);
6448 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
6449 : && SCALAR_FLOAT_TYPE_P (t2)
6450 : && (extended_float_type_p (t1)
6451 : || extended_float_type_p (t2))
6452 : && cp_compare_floating_point_conversion_ranks
6453 : (t1, t2) == 3);
6454 0 : if (complain & tf_error)
6455 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
6456 : "have unordered conversion rank",
6457 : arg2_type, arg3_type);
6458 0 : return error_mark_node;
6459 : }
6460 :
6461 1053931 : if (complain & tf_warning)
6462 987378 : do_warn_double_promotion (result_type, arg2_type, arg3_type,
6463 : "implicit conversion from %qH to %qI to "
6464 : "match other result of conditional",
6465 : loc);
6466 :
6467 1053931 : if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
6468 98 : && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
6469 : {
6470 34 : tree stripped_orig_arg2 = tree_strip_any_location_wrapper (orig_arg2);
6471 34 : tree stripped_orig_arg3 = tree_strip_any_location_wrapper (orig_arg3);
6472 34 : if (TREE_CODE (stripped_orig_arg2) == CONST_DECL
6473 23 : && TREE_CODE (stripped_orig_arg3) == CONST_DECL
6474 57 : && (DECL_CONTEXT (stripped_orig_arg2)
6475 23 : == DECL_CONTEXT (stripped_orig_arg3)))
6476 : /* Two enumerators from the same enumeration can have different
6477 : types when the enumeration is still being defined. */;
6478 28 : else if (complain & (cxx_dialect >= cxx26
6479 28 : ? tf_warning_or_error : tf_warning))
6480 43 : emit_diagnostic ((cxx_dialect >= cxx26
6481 : ? diagnostics::kind::pedwarn
6482 : : diagnostics::kind::warning),
6483 25 : loc, OPT_Wenum_compare, "enumerated mismatch "
6484 : "in conditional expression: %qT vs %qT",
6485 : arg2_type, arg3_type);
6486 3 : else if (cxx_dialect >= cxx26)
6487 1 : return error_mark_node;
6488 : }
6489 1053897 : else if ((((complain & (cxx_dialect >= cxx26
6490 1053897 : ? tf_warning_or_error : tf_warning))
6491 990034 : && warn_deprecated_enum_float_conv)
6492 79008 : || (cxx_dialect >= cxx26
6493 1064 : && (complain & tf_warning_or_error) == 0))
6494 975854 : && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
6495 36 : && SCALAR_FLOAT_TYPE_P (arg3_type))
6496 975843 : || (SCALAR_FLOAT_TYPE_P (arg2_type)
6497 166 : && TREE_CODE (arg3_type) == ENUMERAL_TYPE)))
6498 : {
6499 19 : if (cxx_dialect >= cxx26 && (complain & tf_warning_or_error) == 0)
6500 2 : return error_mark_node;
6501 17 : if (cxx_dialect >= cxx26 && TREE_CODE (arg2_type) == ENUMERAL_TYPE)
6502 4 : pedwarn (loc, OPT_Wdeprecated_enum_float_conversion,
6503 : "conditional expression between enumeration type "
6504 : "%qT and floating-point type %qT", arg2_type, arg3_type);
6505 13 : else if (cxx_dialect >= cxx26)
6506 3 : pedwarn (loc, OPT_Wdeprecated_enum_float_conversion,
6507 : "conditional expression between floating-point type "
6508 : "%qT and enumeration type %qT", arg2_type, arg3_type);
6509 10 : else if (TREE_CODE (arg2_type) == ENUMERAL_TYPE)
6510 6 : warning_at (loc, OPT_Wdeprecated_enum_float_conversion,
6511 : "conditional expression between enumeration type "
6512 : "%qT and floating-point type %qT is deprecated",
6513 : arg2_type, arg3_type);
6514 : else
6515 4 : warning_at (loc, OPT_Wdeprecated_enum_float_conversion,
6516 : "conditional expression between floating-point "
6517 : "type %qT and enumeration type %qT is deprecated",
6518 : arg2_type, arg3_type);
6519 : }
6520 1044516 : else if ((extra_warnings || warn_enum_conversion)
6521 1053883 : && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
6522 15 : && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
6523 9355 : || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
6524 8 : && !same_type_p (arg2_type,
6525 : type_promotes_to (arg3_type)))))
6526 : {
6527 17 : if (complain & tf_warning)
6528 : {
6529 12 : enum opt_code opt = (warn_enum_conversion
6530 17 : ? OPT_Wenum_conversion
6531 : : OPT_Wextra);
6532 17 : warning_at (loc, opt, "enumerated and "
6533 : "non-enumerated type in conditional expression");
6534 : }
6535 : }
6536 :
6537 1053928 : arg2 = perform_implicit_conversion (result_type, arg2, complain);
6538 1053928 : arg3 = perform_implicit_conversion (result_type, arg3, complain);
6539 : }
6540 : /* [expr.cond]
6541 :
6542 : --The second and third operands have pointer type, or one has
6543 : pointer type and the other is a null pointer constant; pointer
6544 : conversions (_conv.ptr_) and qualification conversions
6545 : (_conv.qual_) are performed to bring them to their composite
6546 : pointer type (_expr.rel_). The result is of the composite
6547 : pointer type.
6548 :
6549 : --The second and third operands have pointer to member type, or
6550 : one has pointer to member type and the other is a null pointer
6551 : constant; pointer to member conversions (_conv.mem_) and
6552 : qualification conversions (_conv.qual_) are performed to bring
6553 : them to a common type, whose cv-qualification shall match the
6554 : cv-qualification of either the second or the third operand.
6555 : The result is of the common type. */
6556 21993 : else if ((null_ptr_cst_p (arg2)
6557 163 : && TYPE_PTR_OR_PTRMEM_P (arg3_type))
6558 21830 : || (null_ptr_cst_p (arg3)
6559 20891 : && TYPE_PTR_OR_PTRMEM_P (arg2_type))
6560 939 : || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
6561 89 : || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type))
6562 22065 : || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
6563 : {
6564 21939 : result_type = composite_pointer_type (loc,
6565 : arg2_type, arg3_type, arg2,
6566 : arg3, CPO_CONDITIONAL_EXPR,
6567 : complain);
6568 21939 : if (result_type == error_mark_node)
6569 : return error_mark_node;
6570 21913 : arg2 = perform_implicit_conversion (result_type, arg2, complain);
6571 21913 : arg3 = perform_implicit_conversion (result_type, arg3, complain);
6572 : }
6573 :
6574 4871101 : if (!result_type)
6575 : {
6576 54 : if (complain & tf_error)
6577 3 : error_at (loc, "operands to %<?:%> have different types %qT and %qT",
6578 : arg2_type, arg3_type);
6579 54 : return error_mark_node;
6580 : }
6581 :
6582 4871101 : if (arg2 == error_mark_node || arg3 == error_mark_node)
6583 : return error_mark_node;
6584 :
6585 4871098 : valid_operands:
6586 8588620 : if (processing_template_decl && is_glvalue)
6587 : {
6588 : /* Let lvalue_kind know this was a glvalue. */
6589 108478 : tree arg = (result_type == arg2_type ? arg2 : arg3);
6590 108478 : result_type = cp_build_reference_type (result_type, xvalue_p (arg));
6591 : }
6592 :
6593 8588620 : result = build3_loc (loc, COND_EXPR, result_type, arg1, arg2, arg3);
6594 :
6595 : /* If the ARG2 and ARG3 are the same and don't have side-effects,
6596 : warn here, because the COND_EXPR will be turned into ARG2. */
6597 8588620 : if (warn_duplicated_branches
6598 153 : && (complain & tf_warning)
6599 8588773 : && (arg2 == arg3 || operand_equal_p (arg2, arg3,
6600 : OEP_ADDRESS_OF_SAME_FIELD)))
6601 57 : warning_at (EXPR_LOCATION (result), OPT_Wduplicated_branches,
6602 : "this condition has identical branches");
6603 :
6604 : /* We can't use result_type below, as fold might have returned a
6605 : throw_expr. */
6606 :
6607 8588620 : if (!is_glvalue)
6608 : {
6609 : /* Expand both sides into the same slot, hopefully the target of
6610 : the ?: expression. We used to check for TARGET_EXPRs here,
6611 : but now we sometimes wrap them in NOP_EXPRs so the test would
6612 : fail. */
6613 4984912 : if (CLASS_TYPE_P (TREE_TYPE (result)))
6614 : {
6615 121534 : result = get_target_expr (result, complain);
6616 : /* Tell gimplify_modify_expr_rhs not to strip this in
6617 : assignment context: we want both arms to initialize
6618 : the same temporary. */
6619 121534 : TARGET_EXPR_NO_ELIDE (result) = true;
6620 : }
6621 : /* If this expression is an rvalue, but might be mistaken for an
6622 : lvalue, we must add a NON_LVALUE_EXPR. */
6623 4984912 : result = rvalue (result);
6624 4984912 : if (semantic_result_type)
6625 71 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type,
6626 : result);
6627 : }
6628 : else
6629 : {
6630 3603708 : result = force_paren_expr (result);
6631 3603708 : gcc_assert (semantic_result_type == NULL_TREE);
6632 : }
6633 :
6634 : return result;
6635 8590661 : }
6636 :
6637 : /* OPERAND is an operand to an expression. Perform necessary steps
6638 : required before using it. If OPERAND is NULL_TREE, NULL_TREE is
6639 : returned. */
6640 :
6641 : static tree
6642 730628641 : prep_operand (tree operand)
6643 : {
6644 730628641 : if (operand)
6645 : {
6646 831911004 : if (CLASS_TYPE_P (TREE_TYPE (operand))
6647 453825364 : && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
6648 : /* Make sure the template type is instantiated now. */
6649 19106916 : instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
6650 : }
6651 :
6652 730628641 : return operand;
6653 : }
6654 :
6655 : /* True iff CONV represents a conversion sequence which no other can be better
6656 : than under [over.ics.rank]: in other words, a "conversion" to the exact same
6657 : type (including binding to a reference to the same type). This is stronger
6658 : than the standard's "identity" category, which also includes reference
6659 : bindings that add cv-qualifiers or change rvalueness. */
6660 :
6661 : static bool
6662 296836952 : perfect_conversion_p (conversion *conv)
6663 : {
6664 296836952 : if (CONVERSION_RANK (conv) != cr_identity)
6665 : return false;
6666 211395727 : if (conv->kind == ck_ref_bind)
6667 : {
6668 53428180 : if (!conv->rvaluedness_matches_p)
6669 : return false;
6670 36222566 : if (!same_type_p (TREE_TYPE (conv->type),
6671 : next_conversion (conv)->type))
6672 : return false;
6673 : }
6674 187750863 : if (conv->check_narrowing)
6675 : /* Brace elision is imperfect. */
6676 : return false;
6677 : return true;
6678 : }
6679 :
6680 : /* True if CAND represents a perfect match, i.e. all perfect conversions, so no
6681 : other candidate can be a better match. Since the template/non-template
6682 : tiebreaker comes immediately after the conversion comparison in
6683 : [over.match.best], a perfect non-template candidate is better than all
6684 : templates. */
6685 :
6686 : static bool
6687 707406976 : perfect_candidate_p (z_candidate *cand)
6688 : {
6689 707406976 : if (cand->viable < 1)
6690 : return false;
6691 : /* CWG1402 makes an implicitly deleted move op worse than other
6692 : candidates. */
6693 255820608 : if (DECL_DELETED_FN (cand->fn) && DECL_DEFAULTED_FN (cand->fn)
6694 254267229 : && move_fn_p (cand->fn))
6695 : return false;
6696 252320248 : int len = cand->num_convs;
6697 440071064 : for (int i = 0; i < len; ++i)
6698 296836952 : if (!perfect_conversion_p (cand->convs[i]))
6699 : return false;
6700 143234112 : if (conversion *conv = cand->second_conv)
6701 0 : if (!perfect_conversion_p (conv))
6702 : return false;
6703 : return true;
6704 : }
6705 :
6706 : /* True iff one of CAND's argument conversions is missing. */
6707 :
6708 : static bool
6709 25795442 : missing_conversion_p (const z_candidate *cand)
6710 : {
6711 47997884 : for (unsigned i = 0; i < cand->num_convs; ++i)
6712 : {
6713 30829048 : conversion *conv = cand->convs[i];
6714 30829048 : if (!conv)
6715 : return true;
6716 29914462 : if (conv->kind == ck_deferred_bad)
6717 : {
6718 : /* We don't know whether this conversion is outright invalid or
6719 : just bad, so conservatively assume it's missing. */
6720 7712020 : gcc_checking_assert (conv->bad_p);
6721 : return true;
6722 : }
6723 : }
6724 : return false;
6725 : }
6726 :
6727 : /* Add each of the viable functions in FNS (a FUNCTION_DECL or
6728 : OVERLOAD) to the CANDIDATES, returning an updated list of
6729 : CANDIDATES. The ARGS are the arguments provided to the call;
6730 : if FIRST_ARG is non-null it is the implicit object argument,
6731 : otherwise the first element of ARGS is used if needed. The
6732 : EXPLICIT_TARGS are explicit template arguments provided.
6733 : TEMPLATE_ONLY is true if only template functions should be
6734 : considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
6735 : add_function_candidate. */
6736 :
6737 : static void
6738 409461424 : add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args,
6739 : tree return_type,
6740 : tree explicit_targs, bool template_only,
6741 : tree conversion_path, tree access_path,
6742 : int flags,
6743 : struct z_candidate **candidates,
6744 : tsubst_flags_t complain)
6745 : {
6746 409461424 : tree ctype;
6747 409461424 : const vec<tree, va_gc> *non_static_args;
6748 409461424 : bool check_list_ctor = false;
6749 409461424 : bool check_converting = false;
6750 409461424 : unification_kind_t strict;
6751 409461424 : tree ne_context = NULL_TREE;
6752 409461424 : tree ne_fns = NULL_TREE;
6753 :
6754 409461424 : if (!fns)
6755 4321829 : return;
6756 :
6757 : /* Precalculate special handling of constructors and conversion ops. */
6758 405139595 : tree fn = OVL_FIRST (fns);
6759 405139595 : if (DECL_CONV_FN_P (fn))
6760 : {
6761 19031743 : check_list_ctor = false;
6762 19031743 : check_converting = (flags & LOOKUP_ONLYCONVERTING) != 0;
6763 19031743 : if (flags & LOOKUP_NO_CONVERSION)
6764 : /* We're doing return_type(x). */
6765 : strict = DEDUCE_CONV;
6766 : else
6767 : /* We're doing x.operator return_type(). */
6768 1323 : strict = DEDUCE_EXACT;
6769 : /* [over.match.funcs] For conversion functions, the function
6770 : is considered to be a member of the class of the implicit
6771 : object argument for the purpose of defining the type of
6772 : the implicit object parameter. */
6773 19031743 : ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg));
6774 : }
6775 : else
6776 : {
6777 772215704 : if (DECL_CONSTRUCTOR_P (fn))
6778 : {
6779 107769421 : check_list_ctor = (flags & LOOKUP_LIST_ONLY) != 0;
6780 : /* For list-initialization we consider explicit constructors
6781 : and complain if one is chosen. */
6782 107769421 : check_converting
6783 107769421 : = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
6784 : == LOOKUP_ONLYCONVERTING);
6785 : }
6786 386107852 : strict = DEDUCE_CALL;
6787 602228393 : ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
6788 : }
6789 :
6790 : /* P2468: Check if operator== is a rewrite target with first operand
6791 : (*args)[0]; for now just do the lookups. */
6792 405139595 : if ((flags & (LOOKUP_REWRITTEN | LOOKUP_REVERSED))
6793 405139595 : && DECL_OVERLOADED_OPERATOR_IS (fn, EQ_EXPR))
6794 : {
6795 10338542 : tree ne_name = ovl_op_identifier (false, NE_EXPR);
6796 10338542 : if (DECL_CLASS_SCOPE_P (fn))
6797 : {
6798 209345 : ne_context = DECL_CONTEXT (fn);
6799 209345 : ne_fns = lookup_fnfields (TREE_TYPE ((*args)[0]), ne_name,
6800 : 1, tf_none);
6801 209345 : if (ne_fns == error_mark_node || ne_fns == NULL_TREE)
6802 : ne_fns = NULL_TREE;
6803 : else
6804 17354 : ne_fns = BASELINK_FUNCTIONS (ne_fns);
6805 : }
6806 : else
6807 : {
6808 10129197 : ne_context = decl_namespace_context (fn);
6809 10129197 : ne_fns = lookup_qualified_name (ne_context, ne_name,
6810 : LOOK_want::NORMAL,
6811 : /*complain*/false);
6812 10129197 : if (ne_fns == error_mark_node
6813 2736475 : || !is_overloaded_fn (ne_fns))
6814 7392722 : ne_fns = NULL_TREE;
6815 : }
6816 : }
6817 :
6818 405139595 : if (first_arg)
6819 : non_static_args = args;
6820 : else
6821 : /* Delay creating the implicit this parameter until it is needed. */
6822 182983764 : non_static_args = NULL;
6823 :
6824 405139595 : bool seen_strictly_viable = any_strictly_viable (*candidates);
6825 : /* If there's a non-template perfect match, we don't need to consider
6826 : templates. So check non-templates first. This optimization is only
6827 : really needed for the defaulted copy constructor of tuple and the like
6828 : (96926), but it seems like we might as well enable it more generally. */
6829 405139595 : bool seen_perfect = false;
6830 405139595 : enum { templates, non_templates, either } which = either;
6831 405139595 : if (template_only)
6832 : which = templates;
6833 : else /*if (flags & LOOKUP_DEFAULTED)*/
6834 350145085 : which = non_templates;
6835 :
6836 : /* Template candidates that we'll potentially ignore if the
6837 : perfect candidate optimization succeeds. */
6838 405139595 : z_candidate *ignored_template_cands = nullptr;
6839 :
6840 : /* During overload resolution, we first consider each function under the
6841 : assumption that we'll eventually find a strictly viable candidate.
6842 : This allows us to circumvent our defacto behavior when checking
6843 : argument conversions and shortcut consideration of the candidate
6844 : upon encountering the first bad conversion. If this assumption
6845 : turns out to be false, and all candidates end up being non-strictly
6846 : viable, then we reconsider such candidates under the defacto behavior.
6847 : This trick is important for pruning member function overloads according
6848 : to their const/ref-qualifiers (since all 'this' conversions are at
6849 : worst bad) without breaking -fpermissive. */
6850 405139595 : z_candidate *bad_cands = nullptr;
6851 405139595 : bool shortcut_bad_convs = true;
6852 :
6853 612163615 : again:
6854 3818161897 : for (tree fn : lkp_range (fns))
6855 : {
6856 3206011833 : if (which == templates && TREE_CODE (fn) != TEMPLATE_DECL)
6857 : {
6858 454021165 : if (template_only)
6859 815264 : add_ignored_candidate (candidates, fn);
6860 454021165 : continue;
6861 : }
6862 2751990668 : if (which == non_templates && TREE_CODE (fn) == TEMPLATE_DECL)
6863 : {
6864 1001289275 : add_ignored_candidate (&ignored_template_cands, fn);
6865 1001289275 : continue;
6866 : }
6867 330210370 : if ((check_converting && DECL_NONCONVERTING_P (fn))
6868 2051953173 : || (check_list_ctor && !is_list_ctor (fn)))
6869 : {
6870 30294552 : add_ignored_candidate (candidates, fn);
6871 30294552 : continue;
6872 : }
6873 :
6874 1720406841 : tree fn_first_arg = NULL_TREE;
6875 1720406841 : const vec<tree, va_gc> *fn_args = args;
6876 :
6877 1720406841 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fn))
6878 : {
6879 : /* Figure out where the object arg comes from. If this
6880 : function is a non-static member and we didn't get an
6881 : implicit object argument, move it out of args. */
6882 573769402 : if (first_arg == NULL_TREE)
6883 : {
6884 12996453 : unsigned int ix;
6885 12996453 : tree arg;
6886 12996453 : vec<tree, va_gc> *tempvec;
6887 12996453 : vec_alloc (tempvec, args->length () - 1);
6888 35273531 : for (ix = 1; args->iterate (ix, &arg); ++ix)
6889 9280625 : tempvec->quick_push (arg);
6890 12996453 : non_static_args = tempvec;
6891 12996453 : first_arg = (*args)[0];
6892 : }
6893 :
6894 : fn_first_arg = first_arg;
6895 : fn_args = non_static_args;
6896 : }
6897 :
6898 : /* Don't bother reversing an operator with two identical parameters. */
6899 1146637439 : else if (vec_safe_length (args) == 2 && (flags & LOOKUP_REVERSED))
6900 : {
6901 368163976 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
6902 368163976 : if (same_type_p (TREE_VALUE (parmlist),
6903 : TREE_VALUE (TREE_CHAIN (parmlist))))
6904 175997644 : continue;
6905 : }
6906 :
6907 : /* When considering reversed operator==, if there's a corresponding
6908 : operator!= in the same scope, it's not a rewrite target. */
6909 1544409197 : if (ne_context)
6910 : {
6911 249113866 : if (TREE_CODE (ne_context) == NAMESPACE_DECL)
6912 : {
6913 : /* With argument-dependent lookup, fns can span multiple
6914 : namespaces; make sure we look in the fn's namespace for a
6915 : corresponding operator!=. */
6916 248867509 : tree fn_ns = decl_namespace_context (fn);
6917 248867509 : if (fn_ns != ne_context)
6918 : {
6919 15172292 : ne_context = fn_ns;
6920 15172292 : tree ne_name = ovl_op_identifier (false, NE_EXPR);
6921 15172292 : ne_fns = lookup_qualified_name (ne_context, ne_name,
6922 : LOOK_want::NORMAL,
6923 : /*complain*/false);
6924 15172292 : if (ne_fns == error_mark_node
6925 9163229 : || !is_overloaded_fn (ne_fns))
6926 6009063 : ne_fns = NULL_TREE;
6927 : }
6928 : }
6929 249113866 : bool found = false;
6930 2890542746 : for (lkp_iterator ne (ne_fns); !found && ne; ++ne)
6931 2707517797 : if (0 && !ne.using_p ()
6932 : && DECL_NAMESPACE_SCOPE_P (fn)
6933 : && DECL_CONTEXT (*ne) != DECL_CONTEXT (fn))
6934 : /* ??? This kludge excludes inline namespace members for the H
6935 : test in spaceship-eq15.C, but I don't see why we would want
6936 : that behavior. Asked Core 2022-11-04. Disabling for now. */;
6937 2707517797 : else if (fns_correspond (fn, *ne))
6938 : {
6939 : found = true;
6940 : break;
6941 : }
6942 249113866 : if (found)
6943 66088917 : continue;
6944 : }
6945 :
6946 : /* Do not resolve any non-default function. Only the default version
6947 : is resolvable (for the target_version attribute semantics.) */
6948 1478320280 : if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
6949 : && TREE_CODE (fn) == FUNCTION_DECL
6950 : && !is_function_default_version (fn))
6951 : {
6952 : add_ignored_candidate (candidates, fn);
6953 : continue;
6954 : }
6955 :
6956 1478320280 : if (TREE_CODE (fn) == TEMPLATE_DECL)
6957 770913304 : add_template_candidate (candidates,
6958 : fn,
6959 : ctype,
6960 : explicit_targs,
6961 : fn_first_arg,
6962 : fn_args,
6963 : return_type,
6964 : access_path,
6965 : conversion_path,
6966 : flags,
6967 : strict,
6968 : shortcut_bad_convs,
6969 : complain);
6970 : else
6971 : {
6972 707406976 : add_function_candidate (candidates,
6973 : fn,
6974 : ctype,
6975 : fn_first_arg,
6976 : fn_args,
6977 : access_path,
6978 : conversion_path,
6979 : flags,
6980 : NULL,
6981 : shortcut_bad_convs,
6982 : complain);
6983 707406976 : if (perfect_candidate_p (*candidates))
6984 1478306729 : seen_perfect = true;
6985 : }
6986 :
6987 1478306729 : z_candidate *cand = *candidates;
6988 1478306729 : if (cand->viable == 1)
6989 377658860 : seen_strictly_viable = true;
6990 :
6991 1478306729 : if (cand->viable == -1
6992 25795935 : && shortcut_bad_convs
6993 1504102171 : && (missing_conversion_p (cand)
6994 17168836 : || TREE_CODE (cand->fn) == TEMPLATE_DECL))
6995 : {
6996 : /* This candidate has been tentatively marked non-strictly viable,
6997 : and we didn't compute all argument conversions for it (having
6998 : stopped at the first bad conversion). Move it to BAD_CANDS to
6999 : to fully reconsider later if we don't find any strictly viable
7000 : candidates. */
7001 8651990 : if (complain & (tf_error | tf_conv))
7002 : {
7003 8133600 : *candidates = cand->next;
7004 8133600 : cand->next = bad_cands;
7005 8133600 : bad_cands = cand;
7006 : }
7007 : else
7008 : /* But if we're in a SFINAE context, just mark this candidate as
7009 : unviable outright and avoid potentially reconsidering it.
7010 : This is safe to do because in a SFINAE context, performing a bad
7011 : conversion is always an error (even with -fpermissive), so a
7012 : non-strictly viable candidate is effectively unviable anyway. */
7013 518390 : cand->viable = 0;
7014 : }
7015 : }
7016 612150064 : if (which == non_templates && !seen_perfect)
7017 : {
7018 206915634 : which = templates;
7019 206915634 : ignored_template_cands = nullptr;
7020 206915634 : goto again;
7021 : }
7022 405234430 : else if (which == templates
7023 405234430 : && !seen_strictly_viable
7024 : && shortcut_bad_convs
7025 79621698 : && bad_cands)
7026 : {
7027 : /* None of the candidates are strictly viable, so consider again those
7028 : functions in BAD_CANDS, this time without shortcutting bad conversions
7029 : so that all their argument conversions are computed. */
7030 240534 : which = either;
7031 : fns = NULL_TREE;
7032 240534 : for (z_candidate *cand = bad_cands; cand; cand = cand->next)
7033 : {
7034 132148 : tree fn = cand->fn;
7035 132148 : if (tree ti = cand->template_decl)
7036 115 : fn = TI_TEMPLATE (ti);
7037 132148 : fns = ovl_make (fn, fns);
7038 : }
7039 108386 : shortcut_bad_convs = false;
7040 108386 : bad_cands = nullptr;
7041 108386 : goto again;
7042 : }
7043 :
7044 405126044 : if (complain & tf_error)
7045 : {
7046 : /* Remember any omitted candidates; we may want to print all candidates
7047 : as part of overload resolution failure diagnostics. */
7048 577197078 : for (z_candidate *omitted_cands : { ignored_template_cands, bad_cands })
7049 : {
7050 384798052 : z_candidate **omitted_cands_tail = &omitted_cands;
7051 441829407 : while (*omitted_cands_tail)
7052 57031355 : omitted_cands_tail = &(*omitted_cands_tail)->next;
7053 384798052 : *omitted_cands_tail = *candidates;
7054 384798052 : *candidates = omitted_cands;
7055 : }
7056 : }
7057 : }
7058 :
7059 : /* Returns 1 if P0145R2 says that the LHS of operator CODE is evaluated first,
7060 : -1 if the RHS is evaluated first, or 0 if the order is unspecified. */
7061 :
7062 : static int
7063 20305084 : op_is_ordered (tree_code code)
7064 : {
7065 20304856 : switch (code)
7066 : {
7067 : // 5. b @= a
7068 4104292 : case MODIFY_EXPR:
7069 4104292 : return (flag_strong_eval_order > 1 ? -1 : 0);
7070 :
7071 : // 6. a[b]
7072 1126459 : case ARRAY_REF:
7073 1126231 : return (flag_strong_eval_order > 1 ? 1 : 0);
7074 :
7075 : // 1. a.b
7076 : // Not overloadable (yet).
7077 : // 2. a->b
7078 : // Only one argument.
7079 : // 3. a->*b
7080 52816 : case MEMBER_REF:
7081 : // 7. a << b
7082 52816 : case LSHIFT_EXPR:
7083 : // 8. a >> b
7084 52816 : case RSHIFT_EXPR:
7085 : // a && b
7086 : // Predates P0145R3.
7087 52816 : case TRUTH_ANDIF_EXPR:
7088 : // a || b
7089 : // Predates P0145R3.
7090 52816 : case TRUTH_ORIF_EXPR:
7091 : // a , b
7092 : // Predates P0145R3.
7093 52816 : case COMPOUND_EXPR:
7094 52816 : return (flag_strong_eval_order ? 1 : 0);
7095 :
7096 : default:
7097 : return 0;
7098 : }
7099 : }
7100 :
7101 : /* Subroutine of build_new_op: Add to CANDIDATES all candidates for the
7102 : operator indicated by CODE/CODE2. This function calls itself recursively to
7103 : handle C++20 rewritten comparison operator candidates. Returns NULL_TREE
7104 : upon success, and error_mark_node if something went wrong that prevented
7105 : us from performing overload resolution (e.g. ambiguous member name lookup).
7106 :
7107 : LOOKUPS, if non-NULL, is the set of pertinent namespace-scope operator
7108 : overloads to consider. This parameter is used when instantiating a
7109 : dependent operator expression and has the same structure as
7110 : DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS. */
7111 :
7112 : static tree
7113 46876948 : add_operator_candidates (z_candidate **candidates,
7114 : tree_code code, tree_code code2,
7115 : vec<tree, va_gc> *arglist, tree lookups,
7116 : int flags, tsubst_flags_t complain)
7117 : {
7118 46876948 : z_candidate *start_candidates = *candidates;
7119 46876948 : bool ismodop = code2 != ERROR_MARK;
7120 46876948 : tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
7121 :
7122 : /* LOOKUP_REWRITTEN is set when we're looking for the == or <=> operator to
7123 : rewrite from, and also when we're looking for the e.g. < operator to use
7124 : on the result of <=>. In the latter case, we don't want the flag set in
7125 : the candidate, we just want to suppress looking for rewrites. */
7126 46876948 : bool rewritten = (flags & LOOKUP_REWRITTEN);
7127 46876948 : if (rewritten && code != EQ_EXPR && code != SPACESHIP_EXPR)
7128 1277937 : flags &= ~LOOKUP_REWRITTEN;
7129 :
7130 43596567 : bool memonly = false;
7131 43596567 : switch (code)
7132 : {
7133 : /* =, ->, [], () must be non-static member functions. */
7134 9232425 : case MODIFY_EXPR:
7135 9232425 : if (code2 != NOP_EXPR)
7136 : break;
7137 : /* FALLTHRU */
7138 : case COMPONENT_REF:
7139 : case ARRAY_REF:
7140 : memonly = true;
7141 : break;
7142 :
7143 : default:
7144 : break;
7145 : }
7146 :
7147 : /* Add namespace-scope operators to the list of functions to
7148 : consider. */
7149 : if (!memonly)
7150 : {
7151 39149074 : tree fns;
7152 39149074 : if (!lookups)
7153 23877985 : fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE);
7154 : /* If LOOKUPS is non-NULL, then we're instantiating a dependent operator
7155 : expression, and LOOKUPS is the result of stage 1 name lookup. */
7156 15271089 : else if (tree found = purpose_member (fnname, lookups))
7157 5149270 : fns = TREE_VALUE (found);
7158 : else
7159 : fns = NULL_TREE;
7160 39149074 : fns = lookup_arg_dependent (fnname, fns, arglist);
7161 39149074 : add_candidates (fns, NULL_TREE, arglist, NULL_TREE,
7162 : NULL_TREE, false, NULL_TREE, NULL_TREE,
7163 : flags, candidates, complain);
7164 : }
7165 :
7166 : /* Add class-member operators to the candidate set. */
7167 46876915 : tree arg1_type = TREE_TYPE ((*arglist)[0]);
7168 46876915 : unsigned nargs = arglist->length () > 1 ? 2 : 1;
7169 40782482 : tree arg2_type = nargs > 1 ? TREE_TYPE ((*arglist)[1]) : NULL_TREE;
7170 46876915 : if (CLASS_TYPE_P (arg1_type))
7171 : {
7172 28057197 : tree fns = lookup_fnfields (arg1_type, fnname, 1, complain);
7173 28057197 : if (fns == error_mark_node)
7174 : return error_mark_node;
7175 28057185 : if (fns)
7176 : {
7177 14124867 : if (code == ARRAY_REF)
7178 : {
7179 1128414 : vec<tree,va_gc> *restlist = make_tree_vector ();
7180 2256828 : for (unsigned i = 1; i < nargs; ++i)
7181 1128414 : vec_safe_push (restlist, (*arglist)[i]);
7182 1128414 : z_candidate *save_cand = *candidates;
7183 2256828 : add_candidates (BASELINK_FUNCTIONS (fns),
7184 1128414 : (*arglist)[0], restlist, NULL_TREE,
7185 : NULL_TREE, false,
7186 1128414 : BASELINK_BINFO (fns),
7187 1128414 : BASELINK_ACCESS_BINFO (fns),
7188 : flags, candidates, complain);
7189 : /* Release the vec if we didn't add a candidate that uses it. */
7190 1129009 : for (z_candidate *c = *candidates; c != save_cand; c = c->next)
7191 1129009 : if (c->args == restlist)
7192 : {
7193 1128414 : restlist = NULL;
7194 1128414 : break;
7195 : }
7196 1128414 : release_tree_vector (restlist);
7197 : }
7198 : else
7199 12996453 : add_candidates (BASELINK_FUNCTIONS (fns),
7200 : NULL_TREE, arglist, NULL_TREE,
7201 : NULL_TREE, false,
7202 12996453 : BASELINK_BINFO (fns),
7203 12996453 : BASELINK_ACCESS_BINFO (fns),
7204 : flags, candidates, complain);
7205 : }
7206 : }
7207 : /* Per [over.match.oper]3.2, if no operand has a class type, then
7208 : only non-member functions that have type T1 or reference to
7209 : cv-qualified-opt T1 for the first argument, if the first argument
7210 : has an enumeration type, or T2 or reference to cv-qualified-opt
7211 : T2 for the second argument, if the second argument has an
7212 : enumeration type. Filter out those that don't match. */
7213 18819718 : else if (! arg2_type || ! CLASS_TYPE_P (arg2_type))
7214 : {
7215 : struct z_candidate **candp, **next;
7216 :
7217 381028359 : for (candp = candidates; *candp != start_candidates; candp = next)
7218 : {
7219 362897172 : unsigned i;
7220 362897172 : z_candidate *cand = *candp;
7221 362897172 : next = &cand->next;
7222 :
7223 362897172 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
7224 :
7225 1078117687 : for (i = 0; i < nargs; ++i)
7226 : {
7227 720122032 : tree parmtype = TREE_VALUE (parmlist);
7228 720122032 : tree argtype = unlowered_expr_type ((*arglist)[i]);
7229 :
7230 720122032 : if (TYPE_REF_P (parmtype))
7231 583925959 : parmtype = TREE_TYPE (parmtype);
7232 720122032 : if (TREE_CODE (argtype) == ENUMERAL_TYPE
7233 1420413074 : && (same_type_ignoring_top_level_qualifiers_p
7234 700291042 : (argtype, parmtype)))
7235 : break;
7236 :
7237 715220515 : parmlist = TREE_CHAIN (parmlist);
7238 : }
7239 :
7240 : /* No argument has an appropriate type, so remove this
7241 : candidate function from the list. */
7242 362897172 : if (i == nargs)
7243 : {
7244 357995655 : *candp = cand->next;
7245 357995655 : next = candp;
7246 : }
7247 : }
7248 : }
7249 :
7250 46876903 : if (!rewritten)
7251 : {
7252 : /* The standard says to rewrite built-in candidates, too,
7253 : but there's no point. */
7254 32156437 : add_builtin_candidates (candidates, code, code2, fnname, arglist,
7255 : flags, complain);
7256 :
7257 : /* Maybe add C++20 rewritten comparison candidates. */
7258 32156437 : tree_code rewrite_code = ERROR_MARK;
7259 32156437 : if (cxx_dialect >= cxx20
7260 31862079 : && nargs == 2
7261 58045328 : && (OVERLOAD_TYPE_P (arg1_type) || OVERLOAD_TYPE_P (arg2_type)))
7262 25881271 : switch (code)
7263 : {
7264 1718163 : case LT_EXPR:
7265 1718163 : case LE_EXPR:
7266 1718163 : case GT_EXPR:
7267 1718163 : case GE_EXPR:
7268 1718163 : case SPACESHIP_EXPR:
7269 1718163 : rewrite_code = SPACESHIP_EXPR;
7270 1718163 : break;
7271 :
7272 : case NE_EXPR:
7273 : case EQ_EXPR:
7274 : rewrite_code = EQ_EXPR;
7275 : break;
7276 :
7277 : default:;
7278 : }
7279 :
7280 1718163 : if (rewrite_code)
7281 : {
7282 9307267 : tree r;
7283 9307267 : flags |= LOOKUP_REWRITTEN;
7284 9307267 : if (rewrite_code != code)
7285 : {
7286 : /* Add rewritten candidates in same order. */
7287 4134984 : r = add_operator_candidates (candidates, rewrite_code, ERROR_MARK,
7288 : arglist, lookups, flags, complain);
7289 4134984 : if (r == error_mark_node)
7290 : return error_mark_node;
7291 : }
7292 :
7293 9307261 : z_candidate *save_cand = *candidates;
7294 :
7295 : /* Add rewritten candidates in reverse order. */
7296 9307261 : flags |= LOOKUP_REVERSED;
7297 9307261 : vec<tree,va_gc> *revlist = make_tree_vector ();
7298 9307261 : revlist->quick_push ((*arglist)[1]);
7299 9307261 : revlist->quick_push ((*arglist)[0]);
7300 9307261 : r = add_operator_candidates (candidates, rewrite_code, ERROR_MARK,
7301 : revlist, lookups, flags, complain);
7302 9307261 : if (r == error_mark_node)
7303 : return error_mark_node;
7304 :
7305 : /* Release the vec if we didn't add a candidate that uses it. */
7306 9458991 : for (z_candidate *c = *candidates; c != save_cand; c = c->next)
7307 3962189 : if (c->args == revlist)
7308 : {
7309 : revlist = NULL;
7310 : break;
7311 : }
7312 9307261 : release_tree_vector (revlist);
7313 : }
7314 : }
7315 :
7316 : return NULL_TREE;
7317 : }
7318 :
7319 : tree
7320 242059487 : build_new_op (const op_location_t &loc, enum tree_code code, int flags,
7321 : tree arg1, tree arg2, tree arg3, tree lookups,
7322 : tree *overload, tsubst_flags_t complain)
7323 : {
7324 242059487 : struct z_candidate *candidates = 0, *cand;
7325 242059487 : releasing_vec arglist;
7326 242059487 : tree result = NULL_TREE;
7327 242059487 : bool result_valid_p = false;
7328 242059487 : enum tree_code code2 = ERROR_MARK;
7329 242059487 : enum tree_code code_orig_arg1 = ERROR_MARK;
7330 242059487 : enum tree_code code_orig_arg2 = ERROR_MARK;
7331 242059487 : bool strict_p;
7332 242059487 : bool any_viable_p;
7333 :
7334 242059487 : auto_cond_timevar tv (TV_OVERLOAD);
7335 :
7336 242059487 : if (error_operand_p (arg1)
7337 242055849 : || error_operand_p (arg2)
7338 484109351 : || error_operand_p (arg3))
7339 9623 : return error_mark_node;
7340 :
7341 242049864 : conversion_obstack_sentinel cos;
7342 :
7343 242049864 : bool ismodop = code == MODIFY_EXPR;
7344 242049864 : if (ismodop)
7345 : {
7346 13971211 : code2 = TREE_CODE (arg3);
7347 13971211 : arg3 = NULL_TREE;
7348 : }
7349 :
7350 242049864 : tree arg1_type = unlowered_expr_type (arg1);
7351 242049864 : tree arg2_type = arg2 ? unlowered_expr_type (arg2) : NULL_TREE;
7352 :
7353 242049864 : arg1 = prep_operand (arg1);
7354 :
7355 242049864 : switch (code)
7356 : {
7357 0 : case NEW_EXPR:
7358 0 : case VEC_NEW_EXPR:
7359 0 : case VEC_DELETE_EXPR:
7360 0 : case DELETE_EXPR:
7361 : /* Use build_operator_new_call and build_op_delete_call instead. */
7362 0 : gcc_unreachable ();
7363 :
7364 0 : case CALL_EXPR:
7365 : /* Use build_op_call instead. */
7366 0 : gcc_unreachable ();
7367 :
7368 17959418 : case TRUTH_ORIF_EXPR:
7369 17959418 : case TRUTH_ANDIF_EXPR:
7370 17959418 : case TRUTH_AND_EXPR:
7371 17959418 : case TRUTH_OR_EXPR:
7372 : /* These are saved for the sake of warn_logical_operator. */
7373 17959418 : code_orig_arg1 = TREE_CODE (arg1);
7374 17959418 : code_orig_arg2 = TREE_CODE (arg2);
7375 17959418 : break;
7376 57005143 : case GT_EXPR:
7377 57005143 : case LT_EXPR:
7378 57005143 : case GE_EXPR:
7379 57005143 : case LE_EXPR:
7380 57005143 : case EQ_EXPR:
7381 57005143 : case NE_EXPR:
7382 : /* These are saved for the sake of maybe_warn_bool_compare. */
7383 57005143 : code_orig_arg1 = TREE_CODE (arg1_type);
7384 57005143 : code_orig_arg2 = TREE_CODE (arg2_type);
7385 57005143 : break;
7386 :
7387 : default:
7388 : break;
7389 : }
7390 :
7391 242049864 : arg2 = prep_operand (arg2);
7392 242049864 : arg3 = prep_operand (arg3);
7393 :
7394 242049864 : if (code == COND_EXPR)
7395 : /* Use build_conditional_expr instead. */
7396 0 : gcc_unreachable ();
7397 242049864 : else if (! OVERLOAD_TYPE_P (arg1_type)
7398 451098951 : && (! arg2 || ! OVERLOAD_TYPE_P (arg2_type)))
7399 208615161 : goto builtin;
7400 :
7401 33434703 : if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
7402 : {
7403 599990 : arg2 = integer_zero_node;
7404 599990 : arg2_type = integer_type_node;
7405 : }
7406 :
7407 33434703 : arglist->quick_push (arg1);
7408 33434703 : if (arg2 != NULL_TREE)
7409 27340270 : arglist->quick_push (arg2);
7410 33434703 : if (arg3 != NULL_TREE)
7411 0 : arglist->quick_push (arg3);
7412 :
7413 33434703 : result = add_operator_candidates (&candidates, code, code2, arglist,
7414 : lookups, flags, complain);
7415 33434670 : if (result == error_mark_node)
7416 : return error_mark_node;
7417 :
7418 33434658 : switch (code)
7419 : {
7420 : case COMPOUND_EXPR:
7421 : case ADDR_EXPR:
7422 : /* For these, the built-in candidates set is empty
7423 : [over.match.oper]/3. We don't want non-strict matches
7424 : because exact matches are always possible with built-in
7425 : operators. The built-in candidate set for COMPONENT_REF
7426 : would be empty too, but since there are no such built-in
7427 : operators, we accept non-strict matches for them. */
7428 : strict_p = true;
7429 : break;
7430 :
7431 31630774 : default:
7432 31630774 : strict_p = false;
7433 31630774 : break;
7434 : }
7435 :
7436 33434658 : candidates = splice_viable (candidates, strict_p, &any_viable_p);
7437 33434658 : if (!any_viable_p)
7438 : {
7439 1842768 : switch (code)
7440 : {
7441 225 : case POSTINCREMENT_EXPR:
7442 225 : case POSTDECREMENT_EXPR:
7443 : /* Don't try anything fancy if we're not allowed to produce
7444 : errors. */
7445 225 : if (!(complain & tf_error))
7446 : return error_mark_node;
7447 :
7448 : /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
7449 : distinguish between prefix and postfix ++ and
7450 : operator++() was used for both, so we allow this with
7451 : -fpermissive. */
7452 : else
7453 : {
7454 42 : tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
7455 30 : const char *msg = (flag_permissive)
7456 42 : ? G_("no %<%D(int)%> declared for postfix %qs,"
7457 : " trying prefix operator instead")
7458 : : G_("no %<%D(int)%> declared for postfix %qs");
7459 42 : permerror (loc, msg, fnname, OVL_OP_INFO (false, code)->name);
7460 : }
7461 :
7462 42 : if (!flag_permissive)
7463 30 : return error_mark_node;
7464 :
7465 12 : if (code == POSTINCREMENT_EXPR)
7466 : code = PREINCREMENT_EXPR;
7467 : else
7468 0 : code = PREDECREMENT_EXPR;
7469 12 : result = build_new_op (loc, code, flags, arg1, NULL_TREE,
7470 : NULL_TREE, lookups, overload, complain);
7471 12 : break;
7472 :
7473 : /* The caller will deal with these. */
7474 : case ADDR_EXPR:
7475 : case COMPOUND_EXPR:
7476 : case COMPONENT_REF:
7477 : case CO_AWAIT_EXPR:
7478 : result = NULL_TREE;
7479 : result_valid_p = true;
7480 : break;
7481 :
7482 35915 : default:
7483 35915 : if (complain & tf_error)
7484 : {
7485 : /* If one of the arguments of the operator represents
7486 : an invalid use of member function pointer, try to report
7487 : a meaningful error ... */
7488 1342 : if (invalid_nonstatic_memfn_p (loc, arg1, tf_error)
7489 1339 : || invalid_nonstatic_memfn_p (loc, arg2, tf_error)
7490 2675 : || invalid_nonstatic_memfn_p (loc, arg3, tf_error))
7491 : /* We displayed the error message. */;
7492 : else
7493 : {
7494 : /* ... Otherwise, report the more generic
7495 : "no matching operator found" error */
7496 1333 : auto_diagnostic_group d;
7497 1333 : op_error (loc, code, code2, arg1, arg2, arg3, false);
7498 1333 : print_z_candidates (loc, candidates);
7499 1333 : }
7500 : }
7501 35915 : result = error_mark_node;
7502 35915 : break;
7503 : }
7504 : }
7505 : else
7506 : {
7507 31591890 : cand = tourney (candidates, complain);
7508 31591890 : if (cand == 0)
7509 : {
7510 186 : if (complain & tf_error)
7511 : {
7512 129 : auto_diagnostic_group d;
7513 129 : op_error (loc, code, code2, arg1, arg2, arg3, true);
7514 129 : print_z_candidates (loc, candidates);
7515 129 : }
7516 186 : result = error_mark_node;
7517 186 : if (overload)
7518 167 : *overload = error_mark_node;
7519 : }
7520 31591704 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
7521 : {
7522 25430481 : if (overload)
7523 : {
7524 17828763 : if (cand->rewritten ())
7525 : /* build_min_non_dep_op_overload needs to know whether the
7526 : candidate is rewritten/reversed. */
7527 2552307 : *overload = build_tree_list (build_int_cst (integer_type_node,
7528 2552307 : cand->flags),
7529 : cand->fn);
7530 : else
7531 15276456 : *overload = cand->fn;
7532 : }
7533 :
7534 25430481 : if (resolve_args (arglist, complain) == NULL)
7535 4 : result = error_mark_node;
7536 : else
7537 : {
7538 25430477 : tsubst_flags_t ocomplain = complain;
7539 25430477 : if (cand->rewritten ())
7540 : /* We'll wrap this call in another one. */
7541 2552701 : ocomplain &= ~tf_decltype;
7542 25430477 : if (cand->reversed ())
7543 : {
7544 : /* We swapped these in add_candidate, swap them back now. */
7545 14583 : std::swap (cand->convs[0], cand->convs[1]);
7546 14583 : if (cand->fn == current_function_decl)
7547 1 : warning_at (loc, 0, "in C++20 this comparison calls the "
7548 : "current function recursively with reversed "
7549 : "arguments");
7550 : }
7551 25430477 : result = build_over_call (cand, LOOKUP_NORMAL, ocomplain);
7552 : }
7553 :
7554 45740909 : if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn))
7555 : /* There won't be a CALL_EXPR. */;
7556 20307270 : else if (result && result != error_mark_node)
7557 : {
7558 20304856 : tree call = extract_call_expr (result);
7559 20304856 : CALL_EXPR_OPERATOR_SYNTAX (call) = true;
7560 :
7561 : /* Specify evaluation order as per P0145R2. */
7562 20304856 : CALL_EXPR_ORDERED_ARGS (call) = false;
7563 20304856 : switch (op_is_ordered (code))
7564 : {
7565 4077922 : case -1:
7566 4077922 : CALL_EXPR_REVERSE_ARGS (call) = true;
7567 4077922 : break;
7568 :
7569 1178010 : case 1:
7570 1178010 : CALL_EXPR_ORDERED_ARGS (call) = true;
7571 1178010 : break;
7572 :
7573 : default:
7574 : break;
7575 : }
7576 : }
7577 :
7578 : /* If this was a C++20 rewritten comparison, adjust the result. */
7579 25430478 : if (cand->rewritten ())
7580 : {
7581 2552701 : switch (code)
7582 : {
7583 6358 : case EQ_EXPR:
7584 6358 : gcc_checking_assert (cand->reversed ());
7585 1273527 : gcc_fallthrough ();
7586 1273527 : case NE_EXPR:
7587 1273527 : if (result == error_mark_node)
7588 : ;
7589 : /* If a rewritten operator== candidate is selected by
7590 : overload resolution for an operator @, its return type
7591 : shall be cv bool.... */
7592 1273525 : else if (TREE_CODE (TREE_TYPE (result)) != BOOLEAN_TYPE)
7593 : {
7594 38 : if (complain & tf_error)
7595 : {
7596 6 : auto_diagnostic_group d;
7597 6 : error_at (loc, "return type of %qD is not %qs",
7598 : cand->fn, "bool");
7599 6 : inform (loc, "used as rewritten candidate for "
7600 : "comparison of %qT and %qT",
7601 : arg1_type, arg2_type);
7602 6 : }
7603 38 : result = error_mark_node;
7604 : }
7605 1273487 : else if (code == NE_EXPR)
7606 : /* !(y == x) or !(x == y) */
7607 1267150 : result = build1_loc (loc, TRUTH_NOT_EXPR,
7608 : boolean_type_node, result);
7609 : break;
7610 :
7611 : /* If a rewritten operator<=> candidate is selected by
7612 : overload resolution for an operator @, x @ y is
7613 : interpreted as 0 @ (y <=> x) if the selected candidate is
7614 : a synthesized candidate with reversed order of parameters,
7615 : or (x <=> y) @ 0 otherwise, using the selected rewritten
7616 : operator<=> candidate. */
7617 583 : case SPACESHIP_EXPR:
7618 583 : if (!cand->reversed ())
7619 : /* We're in the build_new_op call below for an outer
7620 : reversed call; we don't need to do anything more. */
7621 : break;
7622 1278884 : gcc_fallthrough ();
7623 1278884 : case LT_EXPR:
7624 1278884 : case LE_EXPR:
7625 1278884 : case GT_EXPR:
7626 1278884 : case GE_EXPR:
7627 1278884 : {
7628 1278884 : tree lhs = result;
7629 1278884 : tree rhs = integer_zero_node;
7630 1278884 : if (cand->reversed ())
7631 1968 : std::swap (lhs, rhs);
7632 1278884 : warning_sentinel ws (warn_zero_as_null_pointer_constant);
7633 1278884 : result = build_new_op (loc, code,
7634 : LOOKUP_NORMAL|LOOKUP_REWRITTEN,
7635 : lhs, rhs, NULL_TREE, lookups,
7636 : NULL, complain);
7637 1278884 : }
7638 1278884 : break;
7639 :
7640 0 : default:
7641 0 : gcc_unreachable ();
7642 : }
7643 : }
7644 :
7645 : /* In an expression of the form `a[]' where cand->fn
7646 : which is operator[] turns out to be a static member function,
7647 : `a' is none-the-less evaluated. */
7648 25430478 : if (code == ARRAY_REF)
7649 1128392 : result = keep_unused_object_arg (result, arg1, cand->fn);
7650 : }
7651 : else
7652 : {
7653 : /* Give any warnings we noticed during overload resolution. */
7654 6161223 : if (cand->warnings && (complain & tf_warning))
7655 : {
7656 : struct candidate_warning *w;
7657 0 : for (w = cand->warnings; w; w = w->next)
7658 0 : joust (cand, w->loser, 1, complain);
7659 : }
7660 :
7661 : /* Check for comparison of different enum types. */
7662 6161223 : switch (code)
7663 : {
7664 5323234 : case GT_EXPR:
7665 5323234 : case LT_EXPR:
7666 5323234 : case GE_EXPR:
7667 5323234 : case LE_EXPR:
7668 5323234 : case EQ_EXPR:
7669 5323234 : case NE_EXPR:
7670 5323234 : if (TREE_CODE (arg1_type) == ENUMERAL_TYPE
7671 5131748 : && TREE_CODE (arg2_type) == ENUMERAL_TYPE
7672 10381371 : && (TYPE_MAIN_VARIANT (arg1_type)
7673 5058137 : != TYPE_MAIN_VARIANT (arg2_type)))
7674 : {
7675 79 : if (cxx_dialect >= cxx26
7676 24 : && (complain & tf_warning_or_error) == 0)
7677 1 : result = error_mark_node;
7678 78 : else if (cxx_dialect >= cxx26 || (complain & tf_warning))
7679 129 : emit_diagnostic ((cxx_dialect >= cxx26
7680 : ? diagnostics::kind::pedwarn
7681 : : diagnostics::kind::warning),
7682 76 : loc, OPT_Wenum_compare,
7683 : "comparison between %q#T and %q#T",
7684 : arg1_type, arg2_type);
7685 : }
7686 : break;
7687 : default:
7688 : break;
7689 : }
7690 :
7691 : /* "If a built-in candidate is selected by overload resolution, the
7692 : operands of class type are converted to the types of the
7693 : corresponding parameters of the selected operation function,
7694 : except that the second standard conversion sequence of a
7695 : user-defined conversion sequence (12.3.3.1.2) is not applied." */
7696 6161223 : conversion *conv = cand->convs[0];
7697 6161223 : if (conv->user_conv_p)
7698 : {
7699 42246 : conv = strip_standard_conversion (conv);
7700 42246 : arg1 = convert_like (conv, arg1, complain);
7701 : }
7702 :
7703 6161223 : if (arg2)
7704 : {
7705 5688067 : conv = cand->convs[1];
7706 5688067 : if (conv->user_conv_p)
7707 : {
7708 8842 : conv = strip_standard_conversion (conv);
7709 8842 : arg2 = convert_like (conv, arg2, complain);
7710 : }
7711 : }
7712 :
7713 6161223 : if (arg3)
7714 : {
7715 0 : conv = cand->convs[2];
7716 0 : if (conv->user_conv_p)
7717 : {
7718 0 : conv = strip_standard_conversion (conv);
7719 0 : arg3 = convert_like (conv, arg3, complain);
7720 : }
7721 : }
7722 : }
7723 : }
7724 :
7725 33434442 : if (result || result_valid_p)
7726 : return result;
7727 :
7728 6161222 : builtin:
7729 214776383 : switch (code)
7730 : {
7731 4739217 : case MODIFY_EXPR:
7732 4739217 : return cp_build_modify_expr (loc, arg1, code2, arg2, complain);
7733 :
7734 31672730 : case INDIRECT_REF:
7735 31672730 : return cp_build_indirect_ref (loc, arg1, RO_UNARY_STAR, complain);
7736 :
7737 17958377 : case TRUTH_ANDIF_EXPR:
7738 17958377 : case TRUTH_ORIF_EXPR:
7739 17958377 : case TRUTH_AND_EXPR:
7740 17958377 : case TRUTH_OR_EXPR:
7741 17958377 : if ((complain & tf_warning) && !processing_template_decl)
7742 9190156 : warn_logical_operator (loc, code, boolean_type_node,
7743 : code_orig_arg1, arg1,
7744 : code_orig_arg2, arg2);
7745 : /* Fall through. */
7746 69098525 : case GT_EXPR:
7747 69098525 : case LT_EXPR:
7748 69098525 : case GE_EXPR:
7749 69098525 : case LE_EXPR:
7750 69098525 : case EQ_EXPR:
7751 69098525 : case NE_EXPR:
7752 69098525 : if ((complain & tf_warning)
7753 62300473 : && ((code_orig_arg1 == BOOLEAN_TYPE)
7754 62300473 : ^ (code_orig_arg2 == BOOLEAN_TYPE)))
7755 25467 : maybe_warn_bool_compare (loc, code, arg1, arg2);
7756 62300473 : if (complain & tf_warning && warn_tautological_compare)
7757 427165 : warn_tautological_cmp (loc, code, arg1, arg2);
7758 : /* Fall through. */
7759 140497257 : case SPACESHIP_EXPR:
7760 140497257 : case PLUS_EXPR:
7761 140497257 : case MINUS_EXPR:
7762 140497257 : case MULT_EXPR:
7763 140497257 : case TRUNC_DIV_EXPR:
7764 140497257 : case MAX_EXPR:
7765 140497257 : case MIN_EXPR:
7766 140497257 : case LSHIFT_EXPR:
7767 140497257 : case RSHIFT_EXPR:
7768 140497257 : case TRUNC_MOD_EXPR:
7769 140497257 : case BIT_AND_EXPR:
7770 140497257 : case BIT_IOR_EXPR:
7771 140497257 : case BIT_XOR_EXPR:
7772 140497257 : return cp_build_binary_op (loc, code, arg1, arg2, complain);
7773 :
7774 32235978 : case UNARY_PLUS_EXPR:
7775 32235978 : case NEGATE_EXPR:
7776 32235978 : case BIT_NOT_EXPR:
7777 32235978 : case TRUTH_NOT_EXPR:
7778 32235978 : case PREINCREMENT_EXPR:
7779 32235978 : case POSTINCREMENT_EXPR:
7780 32235978 : case PREDECREMENT_EXPR:
7781 32235978 : case POSTDECREMENT_EXPR:
7782 32235978 : case REALPART_EXPR:
7783 32235978 : case IMAGPART_EXPR:
7784 32235978 : case ABS_EXPR:
7785 32235978 : case CO_AWAIT_EXPR:
7786 32235978 : return cp_build_unary_op (code, arg1, false, complain);
7787 :
7788 2672237 : case ARRAY_REF:
7789 2672237 : return cp_build_array_ref (input_location, arg1, arg2, complain);
7790 :
7791 747 : case MEMBER_REF:
7792 747 : return build_m_component_ref (cp_build_indirect_ref (loc, arg1,
7793 : RO_ARROW_STAR,
7794 : complain),
7795 747 : arg2, complain);
7796 :
7797 : /* The caller will deal with these. */
7798 : case ADDR_EXPR:
7799 : case COMPONENT_REF:
7800 : case COMPOUND_EXPR:
7801 : return NULL_TREE;
7802 :
7803 0 : default:
7804 0 : gcc_unreachable ();
7805 : }
7806 : return NULL_TREE;
7807 242059451 : }
7808 :
7809 : /* Build a new call to operator[]. This may change ARGS. */
7810 :
7811 : tree
7812 265 : build_op_subscript (const op_location_t &loc, tree obj,
7813 : vec<tree, va_gc> **args, tree *overload,
7814 : tsubst_flags_t complain)
7815 : {
7816 265 : struct z_candidate *candidates = 0, *cand;
7817 265 : tree fns, first_mem_arg = NULL_TREE;
7818 265 : bool any_viable_p;
7819 265 : tree result = NULL_TREE;
7820 :
7821 265 : auto_cond_timevar tv (TV_OVERLOAD);
7822 :
7823 265 : obj = mark_lvalue_use (obj);
7824 :
7825 265 : if (error_operand_p (obj))
7826 0 : return error_mark_node;
7827 :
7828 265 : tree type = TREE_TYPE (obj);
7829 :
7830 265 : obj = prep_operand (obj);
7831 :
7832 265 : if (TYPE_BINFO (type))
7833 : {
7834 265 : fns = lookup_fnfields (TYPE_BINFO (type), ovl_op_identifier (ARRAY_REF),
7835 : 1, complain);
7836 265 : if (fns == error_mark_node)
7837 : return error_mark_node;
7838 : }
7839 : else
7840 : fns = NULL_TREE;
7841 :
7842 265 : if (args != NULL && *args != NULL)
7843 : {
7844 265 : *args = resolve_args (*args, complain);
7845 265 : if (*args == NULL)
7846 0 : return error_mark_node;
7847 : }
7848 :
7849 265 : conversion_obstack_sentinel cos;
7850 :
7851 265 : if (fns)
7852 : {
7853 263 : first_mem_arg = obj;
7854 :
7855 263 : add_candidates (BASELINK_FUNCTIONS (fns),
7856 : first_mem_arg, *args, NULL_TREE,
7857 : NULL_TREE, false,
7858 263 : BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
7859 : LOOKUP_NORMAL, &candidates, complain);
7860 : }
7861 :
7862 : /* Be strict here because if we choose a bad conversion candidate, the
7863 : errors we get won't mention the call context. */
7864 265 : candidates = splice_viable (candidates, true, &any_viable_p);
7865 265 : if (!any_viable_p)
7866 : {
7867 37 : if (complain & tf_error)
7868 : {
7869 1 : auto_diagnostic_group d;
7870 2 : error ("no match for call to %<%T::operator[] (%A)%>",
7871 1 : TREE_TYPE (obj), build_tree_list_vec (*args));
7872 1 : print_z_candidates (loc, candidates);
7873 1 : }
7874 37 : result = error_mark_node;
7875 : }
7876 : else
7877 : {
7878 228 : cand = tourney (candidates, complain);
7879 228 : if (cand == 0)
7880 : {
7881 0 : if (complain & tf_error)
7882 : {
7883 0 : auto_diagnostic_group d;
7884 0 : error ("call of %<%T::operator[] (%A)%> is ambiguous",
7885 0 : TREE_TYPE (obj), build_tree_list_vec (*args));
7886 0 : print_z_candidates (loc, candidates);
7887 0 : }
7888 0 : result = error_mark_node;
7889 : }
7890 228 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL
7891 228 : && DECL_OVERLOADED_OPERATOR_P (cand->fn)
7892 456 : && DECL_OVERLOADED_OPERATOR_IS (cand->fn, ARRAY_REF))
7893 : {
7894 228 : if (overload)
7895 228 : *overload = cand->fn;
7896 228 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
7897 456 : if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn))
7898 : /* There won't be a CALL_EXPR. */;
7899 228 : else if (result && result != error_mark_node)
7900 : {
7901 228 : tree call = extract_call_expr (result);
7902 228 : CALL_EXPR_OPERATOR_SYNTAX (call) = true;
7903 :
7904 : /* Specify evaluation order as per P0145R2. */
7905 228 : CALL_EXPR_ORDERED_ARGS (call) = op_is_ordered (ARRAY_REF) == 1;
7906 : }
7907 :
7908 : /* In an expression of the form `a[]' where cand->fn
7909 : which is operator[] turns out to be a static member function,
7910 : `a' is none-the-less evaluated. */
7911 228 : result = keep_unused_object_arg (result, obj, cand->fn);
7912 : }
7913 : else
7914 0 : gcc_unreachable ();
7915 : }
7916 :
7917 265 : return result;
7918 265 : }
7919 :
7920 : /* CALL was returned by some call-building function; extract the actual
7921 : CALL_EXPR from any bits that have been tacked on, e.g. by
7922 : convert_from_reference. */
7923 :
7924 : tree
7925 51034285 : extract_call_expr (tree call)
7926 : {
7927 51034678 : while (TREE_CODE (call) == COMPOUND_EXPR)
7928 393 : call = TREE_OPERAND (call, 1);
7929 51034285 : if (REFERENCE_REF_P (call))
7930 12630852 : call = TREE_OPERAND (call, 0);
7931 51034285 : if (TREE_CODE (call) == TARGET_EXPR)
7932 3741655 : call = TARGET_EXPR_INITIAL (call);
7933 :
7934 51034285 : if (TREE_CODE (call) != CALL_EXPR
7935 810131 : && TREE_CODE (call) != AGGR_INIT_EXPR
7936 714869 : && call != error_mark_node)
7937 714851 : return NULL_TREE;
7938 : return call;
7939 : }
7940 :
7941 : /* Returns true if FN has two parameters, of which the second has type
7942 : size_t. */
7943 :
7944 : static bool
7945 624159 : second_parm_is_size_t (tree fn)
7946 : {
7947 624159 : tree t = FUNCTION_ARG_CHAIN (fn);
7948 624159 : if (!t || !same_type_p (TREE_VALUE (t), size_type_node))
7949 624144 : return false;
7950 15 : t = TREE_CHAIN (t);
7951 15 : if (t == void_list_node)
7952 : return true;
7953 : return false;
7954 : }
7955 :
7956 : /* True if T, an allocation function, has std::align_val_t as its second
7957 : argument. */
7958 :
7959 : bool
7960 319146 : aligned_allocation_fn_p (tree t)
7961 : {
7962 319146 : if (!aligned_new_threshold)
7963 : return false;
7964 :
7965 309687 : tree a = FUNCTION_ARG_CHAIN (t);
7966 309687 : return (a && same_type_p (TREE_VALUE (a), align_type_node));
7967 : }
7968 :
7969 : /* True if T is std::destroying_delete_t. */
7970 :
7971 : static bool
7972 5571488 : std_destroying_delete_t_p (tree t)
7973 : {
7974 5571488 : return (TYPE_CONTEXT (t) == std_node
7975 5571488 : && id_equal (TYPE_IDENTIFIER (t), "destroying_delete_t"));
7976 : }
7977 :
7978 : /* A deallocation function with at least two parameters whose second parameter
7979 : type is of type std::destroying_delete_t is a destroying operator delete. A
7980 : destroying operator delete shall be a class member function named operator
7981 : delete. [ Note: Array deletion cannot use a destroying operator
7982 : delete. --end note ] */
7983 :
7984 : tree
7985 5571503 : destroying_delete_p (tree t)
7986 : {
7987 5571503 : tree a = TYPE_ARG_TYPES (TREE_TYPE (t));
7988 5571503 : if (!a || !TREE_CHAIN (a))
7989 : return NULL_TREE;
7990 5571488 : tree type = TREE_VALUE (TREE_CHAIN (a));
7991 5571488 : return std_destroying_delete_t_p (type) ? type : NULL_TREE;
7992 : }
7993 :
7994 : struct dealloc_info
7995 : {
7996 : bool sized;
7997 : bool aligned;
7998 : tree destroying;
7999 : };
8000 :
8001 : /* Returns true iff T, an element of an OVERLOAD chain, is a usual deallocation
8002 : function (3.7.4.2 [basic.stc.dynamic.deallocation]). If so, and DI is
8003 : non-null, also set *DI. */
8004 :
8005 : static bool
8006 3850799 : usual_deallocation_fn_p (tree t, dealloc_info *di)
8007 : {
8008 3850799 : if (di) *di = dealloc_info();
8009 :
8010 : /* A template instance is never a usual deallocation function,
8011 : regardless of its signature. */
8012 3850799 : if (TREE_CODE (t) == TEMPLATE_DECL
8013 3850799 : || primary_template_specialization_p (t))
8014 12 : return false;
8015 :
8016 : /* A usual deallocation function is a deallocation function whose parameters
8017 : after the first are
8018 : - optionally, a parameter of type std::destroying_delete_t, then
8019 : - optionally, a parameter of type std::size_t, then
8020 : - optionally, a parameter of type std::align_val_t. */
8021 3850787 : bool global = DECL_NAMESPACE_SCOPE_P (t);
8022 3850787 : tree chain = FUNCTION_ARG_CHAIN (t);
8023 3850787 : if (chain && destroying_delete_p (t))
8024 : {
8025 78 : if (di) di->destroying = TREE_VALUE (chain);
8026 78 : chain = TREE_CHAIN (chain);
8027 : }
8028 3850787 : if (chain
8029 3850784 : && (!global || flag_sized_deallocation)
8030 7686966 : && same_type_p (TREE_VALUE (chain), size_type_node))
8031 : {
8032 1108372 : if (di) di->sized = true;
8033 1108372 : chain = TREE_CHAIN (chain);
8034 : }
8035 3850784 : if (chain && aligned_new_threshold
8036 7686037 : && same_type_p (TREE_VALUE (chain), align_type_node))
8037 : {
8038 1647445 : if (di) di->aligned = true;
8039 1647445 : chain = TREE_CHAIN (chain);
8040 : }
8041 3850787 : return (chain == void_list_node);
8042 : }
8043 :
8044 : /* Just return whether FN is a usual deallocation function. */
8045 :
8046 : bool
8047 8672 : usual_deallocation_fn_p (tree fn)
8048 : {
8049 8672 : return usual_deallocation_fn_p (fn, NULL);
8050 : }
8051 :
8052 : /* Build a call to operator delete. This has to be handled very specially,
8053 : because the restrictions on what signatures match are different from all
8054 : other call instances. For a normal delete, only a delete taking (void *)
8055 : or (void *, size_t) is accepted. For a placement delete, only an exact
8056 : match with the placement new is accepted.
8057 :
8058 : CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
8059 : ADDR is the pointer to be deleted.
8060 : SIZE is the size of the memory block to be deleted.
8061 : GLOBAL_P is true if the delete-expression should not consider
8062 : class-specific delete operators.
8063 : CORO_P is true if the allocation is for a coroutine, where the two argument
8064 : usual deallocation should be chosen in preference to the single argument
8065 : version in a class context.
8066 : PLACEMENT is the corresponding placement new call, or NULL_TREE.
8067 :
8068 : If this call to "operator delete" is being generated as part to
8069 : deallocate memory allocated via a new-expression (as per [expr.new]
8070 : which requires that if the initialization throws an exception then
8071 : we call a deallocation function), then ALLOC_FN is the allocation
8072 : function. */
8073 :
8074 : static tree
8075 1183972 : build_op_delete_call_1 (enum tree_code code, tree addr, tree size,
8076 : bool global_p, bool coro_p, tree placement,
8077 : tree alloc_fn, tsubst_flags_t complain)
8078 : {
8079 1183972 : tree fn = NULL_TREE;
8080 1183972 : tree fns, fnname, type, t;
8081 1183972 : dealloc_info di_fn = { };
8082 :
8083 1183972 : if (addr == error_mark_node)
8084 : return error_mark_node;
8085 :
8086 1183972 : type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
8087 :
8088 1183972 : fnname = ovl_op_identifier (false, code);
8089 :
8090 838507 : if (CLASS_TYPE_P (type)
8091 838471 : && COMPLETE_TYPE_P (complete_type (type))
8092 2022420 : && !global_p)
8093 : /* In [class.free]
8094 :
8095 : If the result of the lookup is ambiguous or inaccessible, or if
8096 : the lookup selects a placement deallocation function, the
8097 : program is ill-formed.
8098 :
8099 : Therefore, we ask lookup_fnfields to complain about ambiguity. */
8100 : {
8101 473206 : fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1, complain);
8102 473206 : if (fns == error_mark_node)
8103 : return error_mark_node;
8104 : }
8105 : else
8106 : fns = NULL_TREE;
8107 :
8108 473203 : if (fns == NULL_TREE)
8109 1182912 : fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE);
8110 :
8111 : /* Strip const and volatile from addr. */
8112 1183969 : tree oaddr = addr;
8113 1183969 : addr = cp_convert (ptr_type_node, addr, complain);
8114 :
8115 1183969 : tree excluded_destroying = NULL_TREE;
8116 :
8117 1183969 : if (placement)
8118 : {
8119 : /* "A declaration of a placement deallocation function matches the
8120 : declaration of a placement allocation function if it has the same
8121 : number of parameters and, after parameter transformations (8.3.5),
8122 : all parameter types except the first are identical."
8123 :
8124 : So we build up the function type we want and ask instantiate_type
8125 : to get it for us. */
8126 625022 : t = FUNCTION_ARG_CHAIN (alloc_fn);
8127 625022 : t = tree_cons (NULL_TREE, ptr_type_node, t);
8128 625022 : t = build_function_type (void_type_node, t);
8129 :
8130 625022 : fn = instantiate_type (t, fns, tf_none);
8131 625022 : if (fn == error_mark_node)
8132 : return NULL_TREE;
8133 :
8134 624159 : fn = MAYBE_BASELINK_FUNCTIONS (fn);
8135 :
8136 : /* "If the lookup finds the two-parameter form of a usual deallocation
8137 : function (3.7.4.2) and that function, considered as a placement
8138 : deallocation function, would have been selected as a match for the
8139 : allocation function, the program is ill-formed." */
8140 624159 : if (second_parm_is_size_t (fn))
8141 : {
8142 9 : const char *const msg1
8143 : = G_("exception cleanup for this placement new selects "
8144 : "non-placement %<operator delete%>");
8145 9 : const char *const msg2
8146 : = G_("%qD is a usual (non-placement) deallocation "
8147 : "function in C++14 (or with %<-fsized-deallocation%>)");
8148 :
8149 : /* But if the class has an operator delete (void *), then that is
8150 : the usual deallocation function, so we shouldn't complain
8151 : about using the operator delete (void *, size_t). */
8152 9 : if (DECL_CLASS_SCOPE_P (fn))
8153 18 : for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns)))
8154 : {
8155 9 : if (usual_deallocation_fn_p (elt)
8156 9 : && FUNCTION_ARG_CHAIN (elt) == void_list_node)
8157 3 : goto ok;
8158 : }
8159 : /* Before C++14 a two-parameter global deallocation function is
8160 : always a placement deallocation function, but warn if
8161 : -Wc++14-compat. */
8162 3 : else if (!flag_sized_deallocation)
8163 : {
8164 1 : if (complain & tf_warning)
8165 : {
8166 1 : auto_diagnostic_group d;
8167 1 : if (warning (OPT_Wc__14_compat, msg1))
8168 1 : inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
8169 1 : }
8170 1 : goto ok;
8171 : }
8172 :
8173 5 : if (complain & tf_warning_or_error)
8174 : {
8175 5 : auto_diagnostic_group d;
8176 5 : if (permerror (input_location, msg1))
8177 : {
8178 : /* Only mention C++14 for namespace-scope delete. */
8179 5 : if (DECL_NAMESPACE_SCOPE_P (fn))
8180 2 : inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
8181 : else
8182 3 : inform (DECL_SOURCE_LOCATION (fn),
8183 : "%qD is a usual (non-placement) deallocation "
8184 : "function", fn);
8185 : }
8186 5 : }
8187 : else
8188 0 : return error_mark_node;
8189 1183106 : ok:;
8190 : }
8191 : }
8192 : else
8193 : /* "Any non-placement deallocation function matches a non-placement
8194 : allocation function. If the lookup finds a single matching
8195 : deallocation function, that function will be called; otherwise, no
8196 : deallocation function will be called." */
8197 4960021 : for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns)))
8198 : {
8199 3842127 : dealloc_info di_elt;
8200 3842127 : if (usual_deallocation_fn_p (elt, &di_elt))
8201 : {
8202 : /* If we're called for an EH cleanup in a new-expression, we can't
8203 : use a destroying delete; the exception was thrown before the
8204 : object was constructed. */
8205 2215076 : if (alloc_fn && di_elt.destroying)
8206 : {
8207 13 : excluded_destroying = elt;
8208 1128776 : continue;
8209 : }
8210 :
8211 2215063 : if (!fn)
8212 : {
8213 558922 : fn = elt;
8214 558922 : di_fn = di_elt;
8215 558922 : continue;
8216 : }
8217 :
8218 : /* -- If any of the deallocation functions is a destroying
8219 : operator delete, all deallocation functions that are not
8220 : destroying operator deletes are eliminated from further
8221 : consideration. */
8222 1656141 : if (di_elt.destroying != di_fn.destroying)
8223 : {
8224 12 : if (di_elt.destroying)
8225 : {
8226 6 : fn = elt;
8227 6 : di_fn = di_elt;
8228 : }
8229 12 : continue;
8230 : }
8231 :
8232 : /* -- If the type has new-extended alignment, a function with a
8233 : parameter of type std::align_val_t is preferred; otherwise a
8234 : function without such a parameter is preferred. If exactly one
8235 : preferred function is found, that function is selected and the
8236 : selection process terminates. If more than one preferred
8237 : function is found, all non-preferred functions are eliminated
8238 : from further consideration. */
8239 1656129 : if (aligned_new_threshold)
8240 : {
8241 1655888 : bool want_align = type_has_new_extended_alignment (type);
8242 1655888 : if (di_elt.aligned != di_fn.aligned)
8243 : {
8244 569829 : if (want_align == di_elt.aligned)
8245 : {
8246 534084 : fn = elt;
8247 534084 : di_fn = di_elt;
8248 : }
8249 569829 : continue;
8250 : }
8251 : }
8252 :
8253 : /* -- If the deallocation functions have class scope, the one
8254 : without a parameter of type std::size_t is selected. */
8255 1086300 : bool want_size;
8256 1086300 : if (DECL_CLASS_SCOPE_P (fn) && !coro_p)
8257 : want_size = false;
8258 :
8259 : /* -- If the type is complete and if, for the second alternative
8260 : (delete array) only, the operand is a pointer to a class type
8261 : with a non-trivial destructor or a (possibly multi-dimensional)
8262 : array thereof, the function with a parameter of type std::size_t
8263 : is selected.
8264 :
8265 : -- Otherwise, it is unspecified whether a deallocation function
8266 : with a parameter of type std::size_t is selected. */
8267 : else
8268 : {
8269 1086192 : want_size = COMPLETE_TYPE_P (type);
8270 1086192 : if (code == VEC_DELETE_EXPR
8271 1086192 : && !TYPE_VEC_NEW_USES_COOKIE (type))
8272 : /* We need a cookie to determine the array size. */
8273 : want_size = false;
8274 : }
8275 1086300 : gcc_assert (di_fn.sized != di_elt.sized);
8276 1086300 : if (want_size == di_elt.sized)
8277 : {
8278 43743 : fn = elt;
8279 43743 : di_fn = di_elt;
8280 : }
8281 : }
8282 : }
8283 :
8284 : /* If we have a matching function, call it. */
8285 1183106 : if (fn)
8286 : {
8287 1183081 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8288 :
8289 : /* If the FN is a member function, make sure that it is
8290 : accessible. */
8291 1183081 : if (BASELINK_P (fns))
8292 990 : perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn,
8293 : complain);
8294 :
8295 : /* Core issue 901: It's ok to new a type with deleted delete. */
8296 1183081 : if (DECL_DELETED_FN (fn) && alloc_fn)
8297 : return NULL_TREE;
8298 :
8299 1183075 : tree ret;
8300 1183075 : if (placement)
8301 : {
8302 : /* The placement args might not be suitable for overload
8303 : resolution at this point, so build the call directly. */
8304 624159 : int nargs = call_expr_nargs (placement);
8305 624159 : tree *argarray = XALLOCAVEC (tree, nargs);
8306 624159 : int i;
8307 624159 : argarray[0] = addr;
8308 1248369 : for (i = 1; i < nargs; i++)
8309 624210 : argarray[i] = CALL_EXPR_ARG (placement, i);
8310 624159 : if (!mark_used (fn, complain) && !(complain & tf_error))
8311 0 : return error_mark_node;
8312 624159 : ret = build_cxx_call (fn, nargs, argarray, complain);
8313 : }
8314 : else
8315 : {
8316 558916 : tree destroying = di_fn.destroying;
8317 558916 : if (destroying)
8318 : {
8319 : /* Strip const and volatile from addr but retain the type of the
8320 : object. */
8321 25 : tree rtype = TREE_TYPE (TREE_TYPE (oaddr));
8322 25 : rtype = cv_unqualified (rtype);
8323 25 : rtype = TYPE_POINTER_TO (rtype);
8324 25 : addr = cp_convert (rtype, oaddr, complain);
8325 25 : destroying = build_functional_cast (input_location,
8326 : destroying, NULL_TREE,
8327 : complain);
8328 : }
8329 :
8330 558916 : releasing_vec args;
8331 558916 : args->quick_push (addr);
8332 558916 : if (destroying)
8333 25 : args->quick_push (destroying);
8334 558916 : if (di_fn.sized)
8335 521677 : args->quick_push (size);
8336 558916 : if (di_fn.aligned)
8337 : {
8338 17876 : tree al = build_int_cst (align_type_node, TYPE_ALIGN_UNIT (type));
8339 17876 : args->quick_push (al);
8340 : }
8341 558916 : ret = cp_build_function_call_vec (fn, &args, complain);
8342 558916 : }
8343 :
8344 : /* Set this flag for all callers of this function. In addition to
8345 : delete-expressions, this is called for deallocating coroutine state;
8346 : treat that as an implicit delete-expression. This is also called for
8347 : the delete if the constructor throws in a new-expression, and for a
8348 : deleting destructor (which implements a delete-expression). */
8349 : /* But leave this flag off for destroying delete to avoid wrong
8350 : assumptions in the optimizers. */
8351 1183075 : tree call = extract_call_expr (ret);
8352 1183075 : if (TREE_CODE (call) == CALL_EXPR && !destroying_delete_p (fn))
8353 1183044 : CALL_FROM_NEW_OR_DELETE_P (call) = 1;
8354 :
8355 1183075 : return ret;
8356 : }
8357 :
8358 : /* If there's only a destroying delete that we can't use because the
8359 : object isn't constructed yet, and we used global new, use global
8360 : delete as well. */
8361 25 : if (excluded_destroying
8362 25 : && DECL_NAMESPACE_SCOPE_P (alloc_fn))
8363 7 : return build_op_delete_call (code, addr, size, true, placement,
8364 7 : alloc_fn, complain);
8365 :
8366 : /* [expr.new]
8367 :
8368 : If no unambiguous matching deallocation function can be found,
8369 : propagating the exception does not cause the object's memory to
8370 : be freed. */
8371 18 : if (alloc_fn)
8372 : {
8373 15 : if ((complain & tf_warning)
8374 15 : && !placement)
8375 : {
8376 15 : bool w = warning (0,
8377 : "no corresponding deallocation function for %qD",
8378 : alloc_fn);
8379 15 : if (w && excluded_destroying)
8380 3 : inform (DECL_SOURCE_LOCATION (excluded_destroying), "destroying "
8381 : "delete %qD cannot be used to release the allocated memory"
8382 : " if the initialization throws because the object is not "
8383 : "constructed yet", excluded_destroying);
8384 : }
8385 15 : return NULL_TREE;
8386 : }
8387 :
8388 3 : if (complain & tf_error)
8389 3 : error ("no suitable %<operator %s%> for %qT",
8390 3 : OVL_OP_INFO (false, code)->name, type);
8391 3 : return error_mark_node;
8392 : }
8393 :
8394 : /* Arguments as per build_op_delete_call_1 (). */
8395 :
8396 : tree
8397 1180785 : build_op_delete_call (enum tree_code code, tree addr, tree size, bool global_p,
8398 : tree placement, tree alloc_fn, tsubst_flags_t complain)
8399 : {
8400 1180785 : return build_op_delete_call_1 (code, addr, size, global_p, /*coro_p*/false,
8401 1180785 : placement, alloc_fn, complain);
8402 : }
8403 :
8404 : /* Arguments as per build_op_delete_call_1 (). */
8405 :
8406 : tree
8407 3187 : build_coroutine_op_delete_call (enum tree_code code, tree addr, tree size,
8408 : bool global_p, tree placement, tree alloc_fn,
8409 : tsubst_flags_t complain)
8410 : {
8411 3187 : return build_op_delete_call_1 (code, addr, size, global_p, /*coro_p*/true,
8412 3187 : placement, alloc_fn, complain);
8413 : }
8414 :
8415 : /* Issue diagnostics about a disallowed access of DECL, using DIAG_DECL
8416 : in the diagnostics.
8417 :
8418 : If ISSUE_ERROR is true, then issue an error about the access, followed
8419 : by a note showing the declaration. Otherwise, just show the note.
8420 :
8421 : DIAG_DECL and DIAG_LOCATION will almost always be the same.
8422 : DIAG_LOCATION is just another DECL. NO_ACCESS_REASON is an optional
8423 : parameter used to specify why DECL wasn't accessible (e.g. ak_private
8424 : would be because DECL was private). If not using NO_ACCESS_REASON,
8425 : then it must be ak_none, and the access failure reason will be
8426 : figured out by looking at the protection of DECL. */
8427 :
8428 : void
8429 1179 : complain_about_access (tree decl, tree diag_decl, tree diag_location,
8430 : bool issue_error, access_kind no_access_reason)
8431 : {
8432 : /* If we have not already figured out why DECL is inaccessible... */
8433 1179 : if (no_access_reason == ak_none)
8434 : {
8435 : /* Examine the access of DECL to find out why. */
8436 997 : if (TREE_PRIVATE (decl))
8437 : no_access_reason = ak_private;
8438 225 : else if (TREE_PROTECTED (decl))
8439 : no_access_reason = ak_protected;
8440 : }
8441 :
8442 : /* Now generate an error message depending on calculated access. */
8443 227 : if (no_access_reason == ak_private)
8444 : {
8445 954 : if (issue_error)
8446 940 : error ("%q#D is private within this context", diag_decl);
8447 954 : inform (DECL_SOURCE_LOCATION (diag_location), "declared private here");
8448 : }
8449 225 : else if (no_access_reason == ak_protected)
8450 : {
8451 180 : if (issue_error)
8452 166 : error ("%q#D is protected within this context", diag_decl);
8453 180 : inform (DECL_SOURCE_LOCATION (diag_location), "declared protected here");
8454 : }
8455 : /* Couldn't figure out why DECL is inaccesible, so just say it's
8456 : inaccessible. */
8457 : else
8458 : {
8459 45 : if (issue_error)
8460 45 : error ("%q#D is inaccessible within this context", diag_decl);
8461 45 : inform (DECL_SOURCE_LOCATION (diag_decl), "declared here");
8462 : }
8463 1179 : }
8464 :
8465 : /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
8466 : bitwise or of LOOKUP_* values. If any errors are warnings are
8467 : generated, set *DIAGNOSTIC_FN to "error" or "warning",
8468 : respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
8469 : to NULL. */
8470 :
8471 : static tree
8472 20101235 : build_temp (tree expr, tree type, int flags,
8473 : enum diagnostics::kind *diagnostic_kind, tsubst_flags_t complain)
8474 : {
8475 20101235 : int savew, savee;
8476 :
8477 20101235 : *diagnostic_kind = diagnostics::kind::unspecified;
8478 :
8479 : /* If the source is a packed field, calling the copy constructor will require
8480 : binding the field to the reference parameter to the copy constructor, and
8481 : we'll end up with an infinite loop. If we can use a bitwise copy, then
8482 : do that now. */
8483 20101235 : if ((lvalue_kind (expr) & clk_packed)
8484 6 : && CLASS_TYPE_P (TREE_TYPE (expr))
8485 20101241 : && !type_has_nontrivial_copy_init (TREE_TYPE (expr)))
8486 3 : return get_target_expr (expr, complain);
8487 :
8488 : /* In decltype, we might have decided not to wrap this call in a TARGET_EXPR.
8489 : But it turns out to be a subexpression, so perform temporary
8490 : materialization now. */
8491 20101232 : if (TREE_CODE (expr) == CALL_EXPR
8492 6 : && CLASS_TYPE_P (type)
8493 20101238 : && same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
8494 3 : expr = build_cplus_new (type, expr, complain);
8495 :
8496 20101232 : savew = warningcount + werrorcount, savee = errorcount;
8497 20101232 : releasing_vec args (make_tree_vector_single (expr));
8498 20101232 : expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
8499 : &args, type, flags, complain);
8500 20101232 : if (warningcount + werrorcount > savew)
8501 0 : *diagnostic_kind = diagnostics::kind::warning;
8502 20101232 : else if (errorcount > savee)
8503 77 : *diagnostic_kind = diagnostics::kind::error;
8504 20101232 : return expr;
8505 20101232 : }
8506 :
8507 : /* Get any location for EXPR, falling back to input_location.
8508 :
8509 : If the result is in a system header and is the virtual location for
8510 : a token coming from the expansion of a macro, unwind it to the
8511 : location of the expansion point of the macro (e.g. to avoid the
8512 : diagnostic being suppressed for expansions of NULL where "NULL" is
8513 : in a system header). */
8514 :
8515 : static location_t
8516 2468615 : get_location_for_expr_unwinding_for_system_header (tree expr)
8517 : {
8518 2468615 : location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
8519 2468615 : loc = expansion_point_location_if_in_system_header (loc);
8520 2468615 : return loc;
8521 : }
8522 :
8523 : /* Perform warnings about peculiar, but valid, conversions from/to NULL.
8524 : Also handle a subset of zero as null warnings.
8525 : EXPR is implicitly converted to type TOTYPE.
8526 : FN and ARGNUM are used for diagnostics. */
8527 :
8528 : static void
8529 599718916 : conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
8530 : {
8531 : /* Issue warnings about peculiar, but valid, uses of NULL. */
8532 599718916 : if (TREE_CODE (totype) != BOOLEAN_TYPE
8533 376187210 : && ARITHMETIC_TYPE_P (totype)
8534 794446420 : && null_node_p (expr))
8535 : {
8536 94 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8537 94 : if (fn)
8538 : {
8539 33 : auto_diagnostic_group d;
8540 33 : if (warning_at (loc, OPT_Wconversion_null,
8541 : "passing NULL to non-pointer argument %P of %qD",
8542 : argnum, fn))
8543 27 : inform (get_fndecl_argument_location (fn, argnum),
8544 : "declared here");
8545 33 : }
8546 : else
8547 61 : warning_at (loc, OPT_Wconversion_null,
8548 : "converting to non-pointer type %qT from NULL", totype);
8549 : }
8550 :
8551 : /* Issue warnings if "false" is converted to a NULL pointer */
8552 599718822 : else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
8553 599718822 : && TYPE_PTR_P (totype))
8554 : {
8555 7 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8556 7 : if (fn)
8557 : {
8558 3 : auto_diagnostic_group d;
8559 3 : if (warning_at (loc, OPT_Wconversion_null,
8560 : "converting %<false%> to pointer type for argument "
8561 : "%P of %qD", argnum, fn))
8562 3 : inform (get_fndecl_argument_location (fn, argnum),
8563 : "declared here");
8564 3 : }
8565 : else
8566 4 : warning_at (loc, OPT_Wconversion_null,
8567 : "converting %<false%> to pointer type %qT", totype);
8568 : }
8569 : /* Handle zero as null pointer warnings for cases other
8570 : than EQ_EXPR and NE_EXPR */
8571 548480081 : else if ((TYPE_PTR_OR_PTRMEM_P (totype) || NULLPTR_TYPE_P (totype))
8572 600028344 : && null_ptr_cst_p (expr))
8573 : {
8574 2468514 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8575 2468514 : maybe_warn_zero_as_null_pointer_constant (expr, loc);
8576 : }
8577 599718916 : }
8578 :
8579 : /* We gave a diagnostic during a conversion. If this was in the second
8580 : standard conversion sequence of a user-defined conversion sequence, say
8581 : which user-defined conversion. */
8582 :
8583 : static void
8584 5216 : maybe_print_user_conv_context (conversion *convs)
8585 : {
8586 5216 : if (convs->user_conv_p)
8587 164 : for (conversion *t = convs; t; t = next_conversion (t))
8588 140 : if (t->kind == ck_user)
8589 : {
8590 43 : print_z_candidate (0, N_(" after user-defined conversion:"),
8591 : t->cand);
8592 43 : break;
8593 : }
8594 5216 : }
8595 :
8596 : /* Locate the parameter with the given index within FNDECL.
8597 : ARGNUM is zero based, -1 indicates the `this' argument of a method.
8598 : Return the location of the FNDECL itself if there are problems. */
8599 :
8600 : location_t
8601 8506765 : get_fndecl_argument_location (tree fndecl, int argnum)
8602 : {
8603 : /* The locations of implicitly-declared functions are likely to be
8604 : more meaningful than those of their parameters. */
8605 8506765 : if (DECL_ARTIFICIAL (fndecl))
8606 297 : return DECL_SOURCE_LOCATION (fndecl);
8607 :
8608 8506468 : if (argnum == -1)
8609 49 : return DECL_SOURCE_LOCATION (fndecl);
8610 :
8611 8506419 : int i;
8612 8506419 : tree param;
8613 :
8614 : /* Locate param by index within DECL_ARGUMENTS (fndecl). */
8615 8506419 : for (i = 0, param = FUNCTION_FIRST_USER_PARM (fndecl);
8616 21316685 : i < argnum && param;
8617 12810266 : i++, param = TREE_CHAIN (param))
8618 : ;
8619 :
8620 : /* If something went wrong (e.g. if we have a builtin and thus no arguments),
8621 : return the location of FNDECL. */
8622 8506419 : if (param == NULL)
8623 28 : return DECL_SOURCE_LOCATION (fndecl);
8624 :
8625 8506391 : return DECL_SOURCE_LOCATION (param);
8626 : }
8627 :
8628 : /* If FNDECL is non-NULL, issue a note highlighting ARGNUM
8629 : within its declaration (or the fndecl itself if something went
8630 : wrong). */
8631 :
8632 : void
8633 6923 : maybe_inform_about_fndecl_for_bogus_argument_init (tree fn, int argnum,
8634 : const char *highlight_color)
8635 : {
8636 6923 : if (fn)
8637 : {
8638 1095 : gcc_rich_location richloc (get_fndecl_argument_location (fn, argnum));
8639 1095 : richloc.set_highlight_color (highlight_color);
8640 1095 : inform (&richloc,
8641 : "initializing argument %P of %qD", argnum, fn);
8642 1095 : }
8643 6923 : }
8644 :
8645 : /* Maybe warn about C++20 Conversions to arrays of unknown bound. C is
8646 : the conversion, EXPR is the expression we're converting. */
8647 :
8648 : static void
8649 51104891 : maybe_warn_array_conv (location_t loc, conversion *c, tree expr)
8650 : {
8651 51104891 : if (cxx_dialect >= cxx20)
8652 : return;
8653 :
8654 425646 : tree type = TREE_TYPE (expr);
8655 425646 : type = strip_pointer_operator (type);
8656 :
8657 425646 : if (TREE_CODE (type) != ARRAY_TYPE
8658 425646 : || TYPE_DOMAIN (type) == NULL_TREE)
8659 : return;
8660 :
8661 1847 : if (pedantic && conv_binds_to_array_of_unknown_bound (c))
8662 10 : pedwarn (loc, OPT_Wc__20_extensions,
8663 : "conversions to arrays of unknown bound "
8664 : "are only available with %<-std=c++20%> or %<-std=gnu++20%>");
8665 : }
8666 :
8667 : /* We call this recursively in convert_like_internal. */
8668 : static tree convert_like (conversion *, tree, tree, int, bool, bool, bool,
8669 : tsubst_flags_t);
8670 :
8671 : /* Adjust the result EXPR of a conversion to the expected type TOTYPE, which
8672 : must be equivalent but might be a typedef. */
8673 :
8674 : static tree
8675 1020970966 : maybe_adjust_type_name (tree type, tree expr, conversion_kind kind)
8676 : {
8677 1020970966 : if (expr == error_mark_node
8678 1020970915 : || processing_template_decl)
8679 : return expr;
8680 :
8681 916272077 : tree etype = TREE_TYPE (expr);
8682 916272077 : if (etype == type)
8683 : return expr;
8684 :
8685 129017831 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p (etype, type)
8686 : || is_bitfield_expr_with_lowered_type (expr)
8687 : || seen_error ());
8688 :
8689 126272408 : if (SCALAR_TYPE_P (type)
8690 249182282 : && (kind == ck_rvalue
8691 : /* ??? We should be able to do this for ck_identity of more prvalue
8692 : expressions, but checking !obvalue_p here breaks, so for now let's
8693 : just handle NON_LVALUE_EXPR (such as the location wrapper for a
8694 : literal). Maybe we want to express already-rvalue in the
8695 : conversion somehow? */
8696 110106735 : || TREE_CODE (expr) == NON_LVALUE_EXPR))
8697 14302465 : expr = build_nop (type, expr);
8698 :
8699 : return expr;
8700 : }
8701 :
8702 : /* Perform the conversions in CONVS on the expression EXPR. FN and
8703 : ARGNUM are used for diagnostics. ARGNUM is zero based, -1
8704 : indicates the `this' argument of a method. INNER is nonzero when
8705 : being called to continue a conversion chain. It is negative when a
8706 : reference binding will be applied, positive otherwise. If
8707 : ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
8708 : conversions will be emitted if appropriate. If C_CAST_P is true,
8709 : this conversion is coming from a C-style cast; in that case,
8710 : conversions to inaccessible bases are permitted. */
8711 :
8712 : static tree
8713 1262514781 : convert_like_internal (conversion *convs, tree expr, tree fn, int argnum,
8714 : bool issue_conversion_warnings, bool c_cast_p,
8715 : bool nested_p, tsubst_flags_t complain)
8716 : {
8717 1262514781 : tree totype = convs->type;
8718 1262514781 : enum diagnostics::kind diag_kind;
8719 1262514781 : int flags;
8720 1262514781 : location_t loc = cp_expr_loc_or_input_loc (expr);
8721 1262514781 : const bool stub_object_p = is_stub_object (expr);
8722 :
8723 1262514781 : if (convs->bad_p && !(complain & tf_error))
8724 66875 : return error_mark_node;
8725 :
8726 1262447906 : gcc_checking_assert (!TYPE_REF_P (TREE_TYPE (expr)));
8727 :
8728 1262447906 : if (convs->bad_p
8729 6896 : && convs->kind != ck_user
8730 6802 : && convs->kind != ck_list
8731 6796 : && convs->kind != ck_ambig
8732 6796 : && (convs->kind != ck_ref_bind
8733 5224 : || (convs->user_conv_p && next_conversion (convs)->bad_p))
8734 1593 : && (convs->kind != ck_rvalue
8735 15 : || SCALAR_TYPE_P (totype))
8736 1262449490 : && convs->kind != ck_base)
8737 : {
8738 1584 : int complained = 0;
8739 1584 : conversion *t = convs;
8740 :
8741 : /* Give a helpful error if this is bad because of excess braces. */
8742 46 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8743 46 : && SCALAR_TYPE_P (totype)
8744 34 : && CONSTRUCTOR_NELTS (expr) > 0
8745 1618 : && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
8746 : {
8747 11 : complained = permerror (loc, "too many braces around initializer "
8748 : "for %qT", totype);
8749 33 : while (BRACE_ENCLOSED_INITIALIZER_P (expr)
8750 55 : && CONSTRUCTOR_NELTS (expr) == 1)
8751 22 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
8752 : }
8753 :
8754 : /* Give a helpful error if this is bad because a conversion to bool
8755 : from std::nullptr_t requires direct-initialization. */
8756 1584 : if (NULLPTR_TYPE_P (TREE_TYPE (expr))
8757 1584 : && TREE_CODE (totype) == BOOLEAN_TYPE)
8758 15 : complained = permerror (loc, "converting to %qH from %qI requires "
8759 : "direct-initialization",
8760 15 : totype, TREE_TYPE (expr));
8761 :
8762 1584 : if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (expr))
8763 72 : && SCALAR_FLOAT_TYPE_P (totype)
8764 1656 : && (extended_float_type_p (TREE_TYPE (expr))
8765 18 : || extended_float_type_p (totype)))
8766 72 : switch (cp_compare_floating_point_conversion_ranks (TREE_TYPE (expr),
8767 : totype))
8768 : {
8769 69 : case 2:
8770 69 : if (pedwarn (loc, OPT_Wnarrowing, "ISO C++ does not allow "
8771 : "converting to %qH from %qI with greater "
8772 69 : "conversion rank", totype, TREE_TYPE (expr)))
8773 1584 : complained = 1;
8774 15 : else if (!complained)
8775 15 : complained = -1;
8776 : break;
8777 3 : case 3:
8778 3 : if (pedwarn (loc, OPT_Wnarrowing, "ISO C++ does not allow "
8779 : "converting to %qH from %qI with unordered "
8780 3 : "conversion rank", totype, TREE_TYPE (expr)))
8781 : complained = 1;
8782 0 : else if (!complained)
8783 15 : complained = -1;
8784 : break;
8785 : default:
8786 : break;
8787 : }
8788 :
8789 3184 : for (; t ; t = next_conversion (t))
8790 : {
8791 3175 : if (t->kind == ck_user && t->cand->reason)
8792 : {
8793 52 : auto_diagnostic_group d;
8794 52 : complained = permerror (loc, "invalid user-defined conversion "
8795 52 : "from %qH to %qI", TREE_TYPE (expr),
8796 : totype);
8797 52 : if (complained)
8798 : {
8799 52 : auto_diagnostic_nesting_level sentinel;
8800 52 : print_z_candidate (loc, N_("candidate is:"), t->cand);
8801 52 : }
8802 52 : expr = convert_like (t, expr, fn, argnum,
8803 : /*issue_conversion_warnings=*/false,
8804 : /*c_cast_p=*/false, /*nested_p=*/true,
8805 : complain);
8806 52 : break;
8807 52 : }
8808 3123 : else if (t->kind == ck_user || !t->bad_p)
8809 : {
8810 1523 : expr = convert_like (t, expr, fn, argnum,
8811 : /*issue_conversion_warnings=*/false,
8812 : /*c_cast_p=*/false, /*nested_p=*/true,
8813 : complain);
8814 1523 : if (t->bad_p)
8815 0 : complained = 1;
8816 : break;
8817 : }
8818 1600 : else if (t->kind == ck_ambig)
8819 0 : return convert_like (t, expr, fn, argnum,
8820 : /*issue_conversion_warnings=*/false,
8821 : /*c_cast_p=*/false, /*nested_p=*/true,
8822 0 : complain);
8823 1600 : else if (t->kind == ck_identity)
8824 : break;
8825 : }
8826 1584 : if (!complained && stub_object_p)
8827 : {
8828 : /* An error diagnosed within a trait, don't give extra labels. */
8829 12 : error_at (loc, "invalid conversion from %qH to %qI",
8830 6 : TREE_TYPE (expr), totype);
8831 6 : complained = 1;
8832 : }
8833 1578 : else if (!complained && expr != error_mark_node)
8834 : {
8835 1426 : range_label_for_type_mismatch label (TREE_TYPE (expr), totype);
8836 1426 : gcc_rich_location richloc (loc, &label, highlight_colors::percent_h);
8837 1426 : complained = permerror (&richloc,
8838 : "invalid conversion from %qH to %qI",
8839 1426 : TREE_TYPE (expr), totype);
8840 1426 : if (complained)
8841 1371 : maybe_emit_indirection_note (loc, expr, totype);
8842 1426 : }
8843 1584 : if (convs->kind == ck_ref_bind)
8844 21 : expr = convert_to_reference (totype, expr, CONV_IMPLICIT,
8845 : LOOKUP_NORMAL, NULL_TREE,
8846 : complain);
8847 : else
8848 1563 : expr = cp_convert (totype, expr, complain);
8849 1584 : if (complained == 1)
8850 1512 : maybe_inform_about_fndecl_for_bogus_argument_init
8851 1512 : (fn, argnum, highlight_colors::percent_i);
8852 1584 : return expr;
8853 : }
8854 :
8855 1262446322 : if (issue_conversion_warnings && (complain & tf_warning))
8856 599718916 : conversion_null_warnings (totype, expr, fn, argnum);
8857 :
8858 1262446322 : switch (convs->kind)
8859 : {
8860 7916069 : case ck_user:
8861 7916069 : {
8862 7916069 : struct z_candidate *cand = convs->cand;
8863 :
8864 7916069 : if (cand == NULL)
8865 : /* We chose the surrogate function from add_conv_candidate, now we
8866 : actually need to build the conversion. */
8867 64 : cand = build_user_type_conversion_1 (totype, expr,
8868 : LOOKUP_NO_CONVERSION, complain);
8869 :
8870 7916069 : tree convfn = cand->fn;
8871 :
8872 : /* When converting from an init list we consider explicit
8873 : constructors, but actually trying to call one is an error. */
8874 8475492 : if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
8875 9663 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
8876 : /* Unless this is for direct-list-initialization. */
8877 9660 : && (!CONSTRUCTOR_IS_DIRECT_INIT (expr) || convs->need_temporary_p)
8878 : /* And in C++98 a default constructor can't be explicit. */
8879 7916294 : && cxx_dialect >= cxx11)
8880 : {
8881 224 : if (!(complain & tf_error))
8882 41 : return error_mark_node;
8883 183 : location_t loc = location_of (expr);
8884 183 : if (CONSTRUCTOR_NELTS (expr) == 0
8885 183 : && FUNCTION_FIRST_USER_PARMTYPE (convfn) != void_list_node)
8886 : {
8887 7 : auto_diagnostic_group d;
8888 7 : if (pedwarn (loc, 0, "converting to %qT from initializer list "
8889 : "would use explicit constructor %qD",
8890 : totype, convfn))
8891 : {
8892 7 : inform (DECL_SOURCE_LOCATION (convfn), "%qD declared here",
8893 : convfn);
8894 7 : inform (loc, "in C++11 and above a default constructor "
8895 : "can be explicit");
8896 : }
8897 7 : }
8898 : else
8899 : {
8900 176 : auto_diagnostic_group d;
8901 176 : error ("converting to %qT from initializer list would use "
8902 : "explicit constructor %qD", totype, convfn);
8903 176 : inform (DECL_SOURCE_LOCATION (convfn), "%qD declared here",
8904 : convfn);
8905 176 : }
8906 : }
8907 :
8908 : /* If we're initializing from {}, it's value-initialization. */
8909 1007484 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8910 1007484 : && CONSTRUCTOR_NELTS (expr) == 0
8911 180986 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype)
8912 8096987 : && !processing_template_decl)
8913 : {
8914 180959 : if (abstract_virtuals_error (NULL_TREE, totype, complain))
8915 15 : return error_mark_node;
8916 180944 : expr = build_value_init (totype, complain);
8917 180944 : expr = get_target_expr (expr, complain);
8918 180944 : if (expr != error_mark_node)
8919 180804 : TARGET_EXPR_LIST_INIT_P (expr) = true;
8920 180944 : return expr;
8921 : }
8922 :
8923 : /* We don't know here whether EXPR is being used as an lvalue or
8924 : rvalue, but we know it's read. */
8925 7735069 : mark_exp_read (expr);
8926 :
8927 : /* Give the conversion call the location of EXPR rather than the
8928 : location of the context that caused the conversion. */
8929 7735069 : iloc_sentinel ils (loc);
8930 :
8931 : /* Pass LOOKUP_NO_CONVERSION so rvalue/base handling knows not to allow
8932 : any more UDCs. */
8933 7735069 : expr = build_over_call (cand, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
8934 : complain);
8935 :
8936 : /* If this is a constructor or a function returning an aggr type,
8937 : we need to build up a TARGET_EXPR. */
8938 15470138 : if (DECL_CONSTRUCTOR_P (convfn))
8939 : {
8940 3840064 : expr = build_cplus_new (totype, expr, complain);
8941 :
8942 : /* Remember that this was list-initialization. */
8943 3840064 : if (convs->check_narrowing && expr != error_mark_node)
8944 848839 : TARGET_EXPR_LIST_INIT_P (expr) = true;
8945 : }
8946 :
8947 7735069 : return expr;
8948 7735069 : }
8949 868566103 : case ck_identity:
8950 868566103 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
8951 : {
8952 1270605 : int nelts = CONSTRUCTOR_NELTS (expr);
8953 210959 : if (nelts == 0)
8954 1059646 : expr = build_value_init (totype, complain);
8955 210959 : else if (nelts == 1)
8956 210959 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
8957 : else
8958 0 : gcc_unreachable ();
8959 : }
8960 868566103 : expr = mark_use (expr, /*rvalue_p=*/!convs->rvaluedness_matches_p,
8961 : /*read_p=*/true, UNKNOWN_LOCATION,
8962 : /*reject_builtin=*/true);
8963 :
8964 868566103 : if (type_unknown_p (expr))
8965 18312 : expr = instantiate_type (totype, expr, complain);
8966 868566103 : if (!nested_p && TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8967 47459 : expr = cp_convert (totype, TREE_OPERAND (expr, 0), complain);
8968 868566103 : if (expr == null_node
8969 868566103 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype))
8970 : /* If __null has been converted to an integer type, we do not want to
8971 : continue to warn about uses of EXPR as an integer, rather than as a
8972 : pointer. */
8973 146205 : expr = build_int_cst (totype, 0);
8974 868566103 : return maybe_adjust_type_name (totype, expr, convs->kind);
8975 47 : case ck_ambig:
8976 : /* We leave bad_p off ck_ambig because overload resolution considers
8977 : it valid, it just fails when we try to perform it. So we need to
8978 : check complain here, too. */
8979 47 : if (complain & tf_error)
8980 : {
8981 : /* Call build_user_type_conversion again for the error. */
8982 3 : int flags = (convs->need_temporary_p
8983 47 : ? LOOKUP_IMPLICIT : LOOKUP_NORMAL);
8984 47 : build_user_type_conversion (totype, convs->u.expr, flags, complain);
8985 47 : gcc_assert (seen_error ());
8986 47 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
8987 : }
8988 47 : return error_mark_node;
8989 :
8990 9013 : case ck_list:
8991 9013 : {
8992 : /* Conversion to std::initializer_list<T>. */
8993 9013 : tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
8994 9013 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (expr);
8995 9013 : tree array;
8996 :
8997 9013 : if (tree init = maybe_init_list_as_array (elttype, expr))
8998 : {
8999 132 : elttype
9000 132 : = cp_build_qualified_type (elttype, (cp_type_quals (elttype)
9001 : | TYPE_QUAL_CONST));
9002 132 : tree index_type = TYPE_DOMAIN (TREE_TYPE (init));
9003 132 : array = build_cplus_array_type (elttype, index_type);
9004 132 : len = TREE_INT_CST_LOW (TYPE_MAX_VALUE (index_type)) + 1;
9005 132 : array = build_vec_init_expr (array, init, complain);
9006 132 : array = get_target_expr (array);
9007 132 : array = cp_build_addr_expr (array, complain);
9008 : }
9009 8881 : else if (len)
9010 : {
9011 8500 : tree val;
9012 8500 : unsigned ix;
9013 8500 : tree new_ctor = build_constructor (init_list_type_node, NULL);
9014 :
9015 : /* Convert all the elements. */
9016 64902 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
9017 : {
9018 56417 : if (TREE_CODE (val) == RAW_DATA_CST)
9019 : {
9020 : /* For conversion to initializer_list<unsigned char> or
9021 : initializer_list<char> or initializer_list<signed char>
9022 : we can optimize and keep RAW_DATA_CST with adjusted
9023 : type if we report narrowing errors if needed, for
9024 : others this converts each element separately. */
9025 48 : if (convs->u.list[ix]->kind == ck_std)
9026 : {
9027 18 : tree et = convs->u.list[ix]->type;
9028 18 : conversion *next = next_conversion (convs->u.list[ix]);
9029 18 : gcc_assert (et
9030 : && (TREE_CODE (et) == INTEGER_TYPE
9031 : || is_byte_access_type (et))
9032 : && TYPE_PRECISION (et) == CHAR_BIT
9033 : && next
9034 : && next->kind == ck_identity);
9035 18 : if (!TYPE_UNSIGNED (et)
9036 : /* For RAW_DATA_CST, TREE_TYPE (val) can be
9037 : either integer_type_node (when it has been
9038 : created by the lexer from CPP_EMBED) or
9039 : after digestion/conversion some integral
9040 : type with CHAR_BIT precision. For int with
9041 : precision higher than CHAR_BIT or unsigned char
9042 : diagnose narrowing conversions from
9043 : that int/unsigned char to signed char if any
9044 : byte has most significant bit set. */
9045 18 : && (TYPE_UNSIGNED (TREE_TYPE (val))
9046 12 : || (TYPE_PRECISION (TREE_TYPE (val))
9047 : > CHAR_BIT)))
9048 2460 : for (int i = 0; i < RAW_DATA_LENGTH (val); ++i)
9049 : {
9050 2454 : if (RAW_DATA_SCHAR_ELT (val, i) >= 0)
9051 2448 : continue;
9052 6 : else if (complain & tf_error)
9053 : {
9054 6 : location_t loc
9055 6 : = cp_expr_loc_or_input_loc (val);
9056 6 : int savederrorcount = errorcount;
9057 18 : permerror_opt (loc, OPT_Wnarrowing,
9058 : "narrowing conversion of "
9059 : "%qd from %qH to %qI",
9060 6 : RAW_DATA_UCHAR_ELT (val, i),
9061 6 : TREE_TYPE (val), et);
9062 6 : if (errorcount != savederrorcount)
9063 6 : return error_mark_node;
9064 : }
9065 : else
9066 0 : return error_mark_node;
9067 : }
9068 12 : tree sub = copy_node (val);
9069 12 : TREE_TYPE (sub) = et;
9070 12 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor),
9071 : NULL_TREE, sub);
9072 : }
9073 : else
9074 : {
9075 30 : conversion *conv = convs->u.list[ix];
9076 30 : gcc_assert (conv->kind == ck_list);
9077 15495 : for (int i = 0; i < RAW_DATA_LENGTH (val); ++i)
9078 : {
9079 15465 : tree elt
9080 15465 : = build_int_cst (TREE_TYPE (val),
9081 15465 : RAW_DATA_UCHAR_ELT (val, i));
9082 15465 : tree sub
9083 15465 : = convert_like (conv->u.list[i], elt,
9084 : fn, argnum, false, false,
9085 : /*nested_p=*/true, complain);
9086 15465 : if (sub == error_mark_node)
9087 : return sub;
9088 15465 : if (!check_narrowing (TREE_TYPE (sub), elt,
9089 : complain))
9090 0 : return error_mark_node;
9091 15465 : tree nc = new_ctor;
9092 15465 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (nc),
9093 : NULL_TREE, sub);
9094 15465 : if (!TREE_CONSTANT (sub))
9095 11679 : TREE_CONSTANT (new_ctor) = false;
9096 : }
9097 : }
9098 42 : len += RAW_DATA_LENGTH (val) - 1;
9099 42 : continue;
9100 42 : }
9101 56369 : tree sub = convert_like (convs->u.list[ix], val, fn,
9102 : argnum, false, false,
9103 : /*nested_p=*/true, complain);
9104 56369 : if (sub == error_mark_node)
9105 : return sub;
9106 1462 : if (!BRACE_ENCLOSED_INITIALIZER_P (val)
9107 56380 : && !check_narrowing (TREE_TYPE (sub), val, complain))
9108 0 : return error_mark_node;
9109 56360 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor),
9110 : NULL_TREE, sub);
9111 56360 : if (!TREE_CONSTANT (sub))
9112 10342 : TREE_CONSTANT (new_ctor) = false;
9113 : }
9114 : /* Build up the array. */
9115 8485 : elttype
9116 8485 : = cp_build_qualified_type (elttype, (cp_type_quals (elttype)
9117 : | TYPE_QUAL_CONST));
9118 8485 : array = build_array_of_n_type (elttype, len);
9119 8485 : array = finish_compound_literal (array, new_ctor, complain);
9120 : /* This is dubious now, should be blessed by P2752. */
9121 8485 : DECL_MERGEABLE (TARGET_EXPR_SLOT (array)) = true;
9122 8485 : array = cp_build_addr_expr (array, complain);
9123 : }
9124 : else
9125 381 : array = nullptr_node;
9126 :
9127 8998 : array = cp_convert (build_pointer_type (elttype), array, complain);
9128 8998 : if (array == error_mark_node)
9129 : return error_mark_node;
9130 :
9131 : /* Build up the initializer_list object. Note: fail gracefully
9132 : if the object cannot be completed because, for example, no
9133 : definition is provided (c++/80956). */
9134 8998 : totype = complete_type_or_maybe_complain (totype, NULL_TREE, complain);
9135 8998 : if (!totype)
9136 0 : return error_mark_node;
9137 8998 : tree field = next_aggregate_field (TYPE_FIELDS (totype));
9138 8998 : vec<constructor_elt, va_gc> *vec = NULL;
9139 8998 : CONSTRUCTOR_APPEND_ELT (vec, field, array);
9140 8998 : field = next_aggregate_field (DECL_CHAIN (field));
9141 8998 : CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
9142 8998 : tree new_ctor = build_constructor (totype, vec);
9143 8998 : return get_target_expr (new_ctor, complain);
9144 : }
9145 :
9146 1309057 : case ck_aggr:
9147 1309057 : if (TREE_CODE (totype) == COMPLEX_TYPE)
9148 : {
9149 27231 : tree real = CONSTRUCTOR_ELT (expr, 0)->value;
9150 27231 : tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
9151 27231 : real = perform_implicit_conversion (TREE_TYPE (totype),
9152 : real, complain);
9153 27231 : imag = perform_implicit_conversion (TREE_TYPE (totype),
9154 : imag, complain);
9155 27231 : expr = build2 (COMPLEX_EXPR, totype, real, imag);
9156 27231 : return expr;
9157 : }
9158 1281826 : expr = reshape_init (totype, expr, complain);
9159 1281826 : expr = get_target_expr (digest_init (totype, expr, complain),
9160 : complain);
9161 1281826 : if (expr != error_mark_node)
9162 1281815 : TARGET_EXPR_LIST_INIT_P (expr) = true;
9163 : return expr;
9164 :
9165 384646033 : default:
9166 384646033 : break;
9167 384646033 : };
9168 :
9169 384646033 : conversion *nc = next_conversion (convs);
9170 384646033 : if (convs->kind == ck_ref_bind && nc->kind == ck_qual
9171 25188 : && !convs->need_temporary_p)
9172 : /* direct_reference_binding might have inserted a ck_qual under
9173 : this ck_ref_bind for the benefit of conversion sequence ranking.
9174 : Don't actually perform that conversion. */
9175 23445 : nc = next_conversion (nc);
9176 :
9177 384646033 : expr = convert_like (nc, expr, fn, argnum,
9178 : convs->kind == ck_ref_bind
9179 384646033 : ? issue_conversion_warnings : false,
9180 : c_cast_p, /*nested_p=*/true, complain & ~tf_no_cleanup);
9181 384646033 : if (expr == error_mark_node)
9182 : return error_mark_node;
9183 :
9184 384645764 : switch (convs->kind)
9185 : {
9186 172318776 : case ck_rvalue:
9187 172318776 : expr = decay_conversion (expr, complain);
9188 172318776 : if (expr == error_mark_node)
9189 : {
9190 15 : if (complain & tf_error)
9191 : {
9192 12 : auto_diagnostic_group d;
9193 12 : maybe_print_user_conv_context (convs);
9194 12 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9195 12 : }
9196 15 : return error_mark_node;
9197 : }
9198 :
9199 172318761 : if ((complain & tf_warning) && fn
9200 50658057 : && warn_suggest_attribute_format)
9201 : {
9202 706 : tree rhstype = TREE_TYPE (expr);
9203 706 : const enum tree_code coder = TREE_CODE (rhstype);
9204 706 : const enum tree_code codel = TREE_CODE (totype);
9205 706 : if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
9206 250 : && coder == codel
9207 956 : && check_missing_format_attribute (totype, rhstype))
9208 6 : warning (OPT_Wsuggest_attribute_format,
9209 : "argument of function call might be a candidate "
9210 : "for a format attribute");
9211 : }
9212 :
9213 172318761 : if (! MAYBE_CLASS_TYPE_P (totype))
9214 152404863 : return maybe_adjust_type_name (totype, expr, convs->kind);
9215 :
9216 : /* Don't introduce copies when passing arguments along to the inherited
9217 : constructor. */
9218 19913898 : if (current_function_decl
9219 16422557 : && flag_new_inheriting_ctors
9220 52758532 : && DECL_INHERITED_CTOR (current_function_decl))
9221 : return expr;
9222 :
9223 19893772 : if (TREE_CODE (expr) == TARGET_EXPR
9224 19893772 : && TARGET_EXPR_LIST_INIT_P (expr))
9225 : /* Copy-list-initialization doesn't actually involve a copy. */
9226 : return expr;
9227 :
9228 : /* Fall through. */
9229 22113583 : case ck_base:
9230 22113583 : if (convs->kind == ck_base && !convs->need_temporary_p)
9231 : {
9232 : /* We are going to bind a reference directly to a base-class
9233 : subobject of EXPR. */
9234 : /* Build an expression for `*((base*) &expr)'. */
9235 2012348 : expr = convert_to_base (expr, totype,
9236 2012348 : !c_cast_p, /*nonnull=*/true, complain);
9237 2012348 : return expr;
9238 : }
9239 :
9240 : /* Copy-initialization where the cv-unqualified version of the source
9241 : type is the same class as, or a derived class of, the class of the
9242 : destination [is treated as direct-initialization]. [dcl.init] */
9243 20101235 : flags = LOOKUP_NORMAL;
9244 : /* This conversion is being done in the context of a user-defined
9245 : conversion (i.e. the second step of copy-initialization), so
9246 : don't allow any more. */
9247 20101235 : if (convs->user_conv_p)
9248 257948 : flags |= LOOKUP_NO_CONVERSION;
9249 : /* We might be performing a conversion of the argument
9250 : to the user-defined conversion, i.e., not a conversion of the
9251 : result of the user-defined conversion. In which case we skip
9252 : explicit constructors. */
9253 20101235 : if (convs->copy_init_p)
9254 19833437 : flags |= LOOKUP_ONLYCONVERTING;
9255 20101235 : expr = build_temp (expr, totype, flags, &diag_kind, complain);
9256 20101235 : if (diag_kind != diagnostics::kind::unspecified && complain)
9257 : {
9258 77 : auto_diagnostic_group d;
9259 77 : maybe_print_user_conv_context (convs);
9260 77 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9261 77 : }
9262 :
9263 20101235 : return build_cplus_new (totype, expr, complain);
9264 :
9265 91113461 : case ck_ref_bind:
9266 91113461 : {
9267 91113461 : tree ref_type = totype;
9268 :
9269 91113461 : if (convs->bad_p && !next_conversion (convs)->bad_p)
9270 : {
9271 5127 : tree extype = TREE_TYPE (expr);
9272 5127 : auto_diagnostic_group d;
9273 5127 : if (TYPE_REF_IS_RVALUE (ref_type)
9274 5127 : && lvalue_p (expr))
9275 1638 : error_at (loc, "cannot bind rvalue reference of type %qH to "
9276 : "lvalue of type %qI", totype, extype);
9277 6132 : else if (!TYPE_REF_IS_RVALUE (ref_type) && !lvalue_p (expr)
9278 4953 : && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
9279 : {
9280 1215 : conversion *next = next_conversion (convs);
9281 1215 : if (next->kind == ck_std)
9282 : {
9283 59 : next = next_conversion (next);
9284 59 : error_at (loc, "cannot bind non-const lvalue reference of "
9285 : "type %qH to a value of type %qI",
9286 : totype, next->type);
9287 : }
9288 1156 : else if (!CP_TYPE_CONST_P (TREE_TYPE (ref_type)))
9289 815 : error_at (loc, "cannot bind non-const lvalue reference of "
9290 : "type %qH to an rvalue of type %qI", totype, extype);
9291 : else // extype is volatile
9292 341 : error_at (loc, "cannot bind lvalue reference of type "
9293 : "%qH to an rvalue of type %qI", totype,
9294 : extype);
9295 : }
9296 2274 : else if (!reference_compatible_p (TREE_TYPE (totype), extype))
9297 : {
9298 : /* If we're converting from T[] to T[N], don't talk
9299 : about discarding qualifiers. (Converting from T[N] to
9300 : T[] is allowed by P0388R4.) */
9301 2274 : if (TREE_CODE (extype) == ARRAY_TYPE
9302 27 : && TYPE_DOMAIN (extype) == NULL_TREE
9303 15 : && TREE_CODE (TREE_TYPE (totype)) == ARRAY_TYPE
9304 2289 : && TYPE_DOMAIN (TREE_TYPE (totype)) != NULL_TREE)
9305 15 : error_at (loc, "cannot bind reference of type %qH to %qI "
9306 : "due to different array bounds", totype, extype);
9307 : else
9308 2259 : error_at (loc, "binding reference of type %qH to %qI "
9309 : "discards qualifiers", totype, extype);
9310 : }
9311 : else
9312 0 : gcc_unreachable ();
9313 5127 : maybe_print_user_conv_context (convs);
9314 5127 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9315 :
9316 5127 : return error_mark_node;
9317 5127 : }
9318 91108334 : else if (complain & tf_warning)
9319 49268297 : maybe_warn_array_conv (loc, convs, expr);
9320 :
9321 : /* If necessary, create a temporary.
9322 :
9323 : VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
9324 : that need temporaries, even when their types are reference
9325 : compatible with the type of reference being bound, so the
9326 : upcoming call to cp_build_addr_expr doesn't fail. */
9327 91108334 : if (convs->need_temporary_p
9328 88098162 : || TREE_CODE (expr) == CONSTRUCTOR
9329 88098084 : || TREE_CODE (expr) == VA_ARG_EXPR)
9330 : {
9331 : /* Otherwise, a temporary of type "cv1 T1" is created and
9332 : initialized from the initializer expression using the rules
9333 : for a non-reference copy-initialization (8.5). */
9334 :
9335 3010250 : tree type = TREE_TYPE (ref_type);
9336 3010250 : cp_lvalue_kind lvalue = lvalue_kind (expr);
9337 :
9338 3010250 : gcc_assert (similar_type_p (type, next_conversion (convs)->type));
9339 3010250 : if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
9340 3901442 : && !TYPE_REF_IS_RVALUE (ref_type))
9341 : {
9342 : /* If the reference is volatile or non-const, we
9343 : cannot create a temporary. */
9344 105 : if (complain & tf_error)
9345 : {
9346 103 : if (lvalue & clk_bitfield)
9347 30 : error_at (loc, "cannot bind bit-field %qE to %qT",
9348 : expr, ref_type);
9349 73 : else if (lvalue & clk_packed)
9350 9 : error_at (loc, "cannot bind packed field %qE to %qT",
9351 : expr, ref_type);
9352 : else
9353 64 : error_at (loc, "cannot bind rvalue %qE to %qT",
9354 : expr, ref_type);
9355 : }
9356 105 : return error_mark_node;
9357 : }
9358 : /* If the source is a packed field, and we must use a copy
9359 : constructor, then building the target expr will require
9360 : binding the field to the reference parameter to the
9361 : copy constructor, and we'll end up with an infinite
9362 : loop. If we can use a bitwise copy, then we'll be
9363 : OK. */
9364 3010145 : if ((lvalue & clk_packed)
9365 20 : && CLASS_TYPE_P (type)
9366 3010162 : && type_has_nontrivial_copy_init (type))
9367 : {
9368 6 : error_at (loc, "cannot bind packed field %qE to %qT",
9369 : expr, ref_type);
9370 6 : return error_mark_node;
9371 : }
9372 3010139 : if (lvalue & clk_bitfield)
9373 : {
9374 24 : expr = convert_bitfield_to_declared_type (expr);
9375 24 : expr = fold_convert (type, expr);
9376 : }
9377 :
9378 : /* Creating &TARGET_EXPR<> in a template would break when
9379 : tsubsting the expression, so use an IMPLICIT_CONV_EXPR
9380 : instead. This can happen even when there's no class
9381 : involved, e.g., when converting an integer to a reference
9382 : type. */
9383 3010139 : if (processing_template_decl)
9384 9224 : return build1 (IMPLICIT_CONV_EXPR, totype, expr);
9385 3000915 : expr = build_target_expr_with_type (expr, type, complain);
9386 : }
9387 :
9388 : /* Take the address of the thing to which we will bind the
9389 : reference. */
9390 91098999 : expr = cp_build_addr_expr (expr, complain);
9391 91098999 : if (expr == error_mark_node)
9392 : return error_mark_node;
9393 :
9394 : /* Convert it to a pointer to the type referred to by the
9395 : reference. This will adjust the pointer if a derived to
9396 : base conversion is being performed. */
9397 91098999 : expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
9398 : expr, complain);
9399 : /* Convert the pointer to the desired reference type. */
9400 91098999 : return build_nop (ref_type, expr);
9401 : }
9402 :
9403 3566817 : case ck_lvalue:
9404 3566817 : return decay_conversion (expr, complain);
9405 :
9406 220791 : case ck_fnptr:
9407 : /* ??? Should the address of a transaction-safe pointer point to the TM
9408 : clone, and this conversion look up the primary function? */
9409 220791 : return build_nop (totype, expr);
9410 :
9411 2028502 : case ck_qual:
9412 : /* Warn about deprecated conversion if appropriate. */
9413 2028502 : if (complain & tf_warning)
9414 : {
9415 1836594 : string_conv_p (totype, expr, 1);
9416 1836594 : maybe_warn_array_conv (loc, convs, expr);
9417 : }
9418 : break;
9419 :
9420 3516185 : case ck_ptr:
9421 3516185 : if (convs->base_p)
9422 204207 : expr = convert_to_base (expr, totype, !c_cast_p,
9423 : /*nonnull=*/false, complain);
9424 3516185 : return build_nop (totype, expr);
9425 :
9426 27571 : case ck_pmem:
9427 27571 : return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
9428 27571 : c_cast_p, complain);
9429 :
9430 : default:
9431 : break;
9432 : }
9433 :
9434 111636186 : if (convs->check_narrowing
9435 166586112 : && !check_narrowing (totype, expr, complain,
9436 54949926 : convs->check_narrowing_const_only))
9437 446 : return error_mark_node;
9438 :
9439 223271480 : warning_sentinel w (warn_zero_as_null_pointer_constant);
9440 111635740 : if (issue_conversion_warnings)
9441 88898304 : expr = cp_convert_and_check (totype, expr, complain);
9442 : else
9443 : {
9444 22737436 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
9445 36 : expr = TREE_OPERAND (expr, 0);
9446 22737436 : expr = cp_convert (totype, expr, complain);
9447 : }
9448 :
9449 111635740 : return expr;
9450 : }
9451 :
9452 : /* Return true if converting FROM to TO is unsafe in a template. */
9453 :
9454 : static bool
9455 1781170 : conv_unsafe_in_template_p (tree to, tree from)
9456 : {
9457 : /* Converting classes involves TARGET_EXPR. */
9458 1781170 : if (CLASS_TYPE_P (to) || CLASS_TYPE_P (from))
9459 : return true;
9460 :
9461 : /* Converting real to integer produces FIX_TRUNC_EXPR which tsubst
9462 : doesn't handle. */
9463 1370886 : if (SCALAR_FLOAT_TYPE_P (from) && INTEGRAL_OR_ENUMERATION_TYPE_P (to))
9464 : return true;
9465 :
9466 : /* Converting integer to real isn't a trivial conversion, either. */
9467 1370883 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (from) && SCALAR_FLOAT_TYPE_P (to))
9468 3 : return true;
9469 :
9470 : return false;
9471 : }
9472 :
9473 : /* Wrapper for convert_like_internal that handles creating
9474 : IMPLICIT_CONV_EXPR. */
9475 :
9476 : static tree
9477 1262925062 : convert_like (conversion *convs, tree expr, tree fn, int argnum,
9478 : bool issue_conversion_warnings, bool c_cast_p, bool nested_p,
9479 : tsubst_flags_t complain)
9480 : {
9481 : /* Creating &TARGET_EXPR<> in a template breaks when substituting,
9482 : and creating a CALL_EXPR in a template breaks in finish_call_expr
9483 : so use an IMPLICIT_CONV_EXPR for this conversion. We would have
9484 : created such codes e.g. when calling a user-defined conversion
9485 : function. */
9486 1262925062 : tree conv_expr = NULL_TREE;
9487 1262925062 : if (processing_template_decl
9488 105800816 : && convs->kind != ck_identity
9489 1264706232 : && conv_unsafe_in_template_p (convs->type, TREE_TYPE (expr)))
9490 : {
9491 410290 : conv_expr = build1 (IMPLICIT_CONV_EXPR, convs->type, expr);
9492 410290 : if (convs->kind != ck_ref_bind)
9493 45510 : conv_expr = convert_from_reference (conv_expr);
9494 410290 : if (!convs->bad_p)
9495 : return conv_expr;
9496 : /* Do the normal processing to give the bad_p errors. But we still
9497 : need to return the IMPLICIT_CONV_EXPR, unless we're returning
9498 : error_mark_node. */
9499 : }
9500 1262514781 : expr = convert_like_internal (convs, expr, fn, argnum,
9501 : issue_conversion_warnings, c_cast_p,
9502 : nested_p, complain);
9503 1262514781 : if (expr == error_mark_node)
9504 : return error_mark_node;
9505 1262440972 : return conv_expr ? conv_expr : expr;
9506 : }
9507 :
9508 : /* Convenience wrapper for convert_like. */
9509 :
9510 : static inline tree
9511 656798107 : convert_like (conversion *convs, tree expr, tsubst_flags_t complain)
9512 : {
9513 656798107 : return convert_like (convs, expr, NULL_TREE, 0,
9514 : /*issue_conversion_warnings=*/true,
9515 656798107 : /*c_cast_p=*/false, /*nested_p=*/false, complain);
9516 : }
9517 :
9518 : /* Convenience wrapper for convert_like. */
9519 :
9520 : static inline tree
9521 180906045 : convert_like_with_context (conversion *convs, tree expr, tree fn, int argnum,
9522 : tsubst_flags_t complain)
9523 : {
9524 0 : return convert_like (convs, expr, fn, argnum,
9525 : /*issue_conversion_warnings=*/true,
9526 : /*c_cast_p=*/false, /*nested_p=*/false, complain);
9527 : }
9528 :
9529 : /* ARG is being passed to a varargs function. Perform any conversions
9530 : required. Return the converted value. */
9531 :
9532 : tree
9533 1343587 : convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
9534 : {
9535 1343587 : tree arg_type = TREE_TYPE (arg);
9536 1343587 : location_t loc = cp_expr_loc_or_input_loc (arg);
9537 :
9538 : /* [expr.call]
9539 :
9540 : If the argument has integral or enumeration type that is subject
9541 : to the integral promotions (_conv.prom_), or a floating-point
9542 : type that is subject to the floating-point promotion
9543 : (_conv.fpprom_), the value of the argument is converted to the
9544 : promoted type before the call. */
9545 1343587 : if (SCALAR_FLOAT_TYPE_P (arg_type)
9546 47480 : && (TYPE_PRECISION (arg_type)
9547 47480 : < TYPE_PRECISION (double_type_node))
9548 11682 : && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type))
9549 1354591 : && !extended_float_type_p (arg_type))
9550 : {
9551 11000 : if ((complain & tf_warning)
9552 10997 : && warn_double_promotion && !c_inhibit_evaluation_warnings)
9553 3 : warning_at (loc, OPT_Wdouble_promotion,
9554 : "implicit conversion from %qH to %qI when passing "
9555 : "argument to function",
9556 : arg_type, double_type_node);
9557 11000 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
9558 3 : arg = TREE_OPERAND (arg, 0);
9559 11000 : arg = mark_rvalue_use (arg);
9560 11000 : arg = convert_to_real_nofold (double_type_node, arg);
9561 : }
9562 1332587 : else if (NULLPTR_TYPE_P (arg_type))
9563 : {
9564 75 : arg = mark_rvalue_use (arg);
9565 75 : if (TREE_SIDE_EFFECTS (arg))
9566 : {
9567 6 : warning_sentinel w(warn_unused_result);
9568 6 : arg = cp_build_compound_expr (arg, null_pointer_node, complain);
9569 6 : }
9570 : else
9571 69 : arg = null_pointer_node;
9572 : }
9573 1332512 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
9574 : {
9575 888243 : if (SCOPED_ENUM_P (arg_type))
9576 : {
9577 56 : tree prom = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg,
9578 : complain);
9579 56 : prom = cp_perform_integral_promotions (prom, complain);
9580 109 : if (abi_version_crosses (6)
9581 30 : && TYPE_MODE (TREE_TYPE (prom)) != TYPE_MODE (arg_type)
9582 86 : && (complain & tf_warning))
9583 60 : warning_at (loc, OPT_Wabi, "scoped enum %qT passed through %<...%>"
9584 : " as %qT before %<-fabi-version=6%>, %qT after",
9585 : arg_type,
9586 30 : TREE_TYPE (prom), ENUM_UNDERLYING_TYPE (arg_type));
9587 56 : if (!abi_version_at_least (6))
9588 1343587 : arg = prom;
9589 : }
9590 : else
9591 888187 : arg = cp_perform_integral_promotions (arg, complain);
9592 : }
9593 : else
9594 : /* [expr.call]
9595 :
9596 : The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
9597 : standard conversions are performed. */
9598 444269 : arg = decay_conversion (arg, complain);
9599 :
9600 1343587 : arg = require_complete_type (arg, complain);
9601 1343587 : arg_type = TREE_TYPE (arg);
9602 :
9603 1343587 : if (arg != error_mark_node
9604 : /* In a template (or ill-formed code), we can have an incomplete type
9605 : even after require_complete_type, in which case we don't know
9606 : whether it has trivial copy or not. */
9607 1343575 : && COMPLETE_TYPE_P (arg_type)
9608 2687162 : && !cp_unevaluated_operand)
9609 : {
9610 : /* [expr.call] 5.2.2/7:
9611 : Passing a potentially-evaluated argument of class type (Clause 9)
9612 : with a non-trivial copy constructor or a non-trivial destructor
9613 : with no corresponding parameter is conditionally-supported, with
9614 : implementation-defined semantics.
9615 :
9616 : We support it as pass-by-invisible-reference, just like a normal
9617 : value parameter.
9618 :
9619 : If the call appears in the context of a sizeof expression,
9620 : it is not potentially-evaluated. */
9621 628997 : if (type_has_nontrivial_copy_init (arg_type)
9622 628997 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type))
9623 : {
9624 33 : arg = force_rvalue (arg, complain);
9625 33 : if (complain & tf_warning)
9626 33 : warning (OPT_Wconditionally_supported,
9627 : "passing objects of non-trivially-copyable "
9628 : "type %q#T through %<...%> is conditionally supported",
9629 : arg_type);
9630 33 : return build1 (ADDR_EXPR, build_reference_type (arg_type), arg);
9631 : }
9632 : /* Build up a real lvalue-to-rvalue conversion in case the
9633 : copy constructor is trivial but not callable. */
9634 628964 : else if (CLASS_TYPE_P (arg_type))
9635 44140 : force_rvalue (arg, complain);
9636 :
9637 : }
9638 :
9639 : return arg;
9640 : }
9641 :
9642 : /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
9643 :
9644 : tree
9645 31142 : build_x_va_arg (location_t loc, tree expr, tree type)
9646 : {
9647 31142 : if (processing_template_decl)
9648 : {
9649 39 : tree r = build_min (VA_ARG_EXPR, type, expr);
9650 39 : SET_EXPR_LOCATION (r, loc);
9651 39 : return r;
9652 : }
9653 :
9654 31103 : type = complete_type_or_else (type, NULL_TREE);
9655 :
9656 31103 : if (expr == error_mark_node || !type)
9657 : return error_mark_node;
9658 :
9659 31082 : expr = mark_lvalue_use (expr);
9660 :
9661 31082 : if (TYPE_REF_P (type))
9662 : {
9663 6 : error ("cannot receive reference type %qT through %<...%>", type);
9664 6 : return error_mark_node;
9665 : }
9666 :
9667 31076 : if (type_has_nontrivial_copy_init (type)
9668 31076 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
9669 : {
9670 : /* conditionally-supported behavior [expr.call] 5.2.2/7. Let's treat
9671 : it as pass by invisible reference. */
9672 12 : warning_at (loc, OPT_Wconditionally_supported,
9673 : "receiving objects of non-trivially-copyable type %q#T "
9674 : "through %<...%> is conditionally-supported", type);
9675 :
9676 12 : tree ref = cp_build_reference_type (type, false);
9677 12 : expr = build_va_arg (loc, expr, ref);
9678 12 : return convert_from_reference (expr);
9679 : }
9680 :
9681 31064 : tree ret = build_va_arg (loc, expr, type);
9682 31064 : if (CLASS_TYPE_P (type))
9683 : /* Wrap the VA_ARG_EXPR in a TARGET_EXPR now so other code doesn't need to
9684 : know how to handle it. */
9685 12094 : ret = get_target_expr (ret);
9686 : return ret;
9687 : }
9688 :
9689 : /* TYPE has been given to va_arg. Apply the default conversions which
9690 : would have happened when passed via ellipsis. Return the promoted
9691 : type, or the passed type if there is no change. */
9692 :
9693 : tree
9694 2540774 : cxx_type_promotes_to (tree type)
9695 : {
9696 2540774 : tree promote;
9697 :
9698 : /* Perform the array-to-pointer and function-to-pointer
9699 : conversions. */
9700 2540774 : type = type_decays_to (type);
9701 :
9702 2540774 : promote = type_promotes_to (type);
9703 2540774 : if (same_type_p (type, promote))
9704 2540750 : promote = type;
9705 :
9706 2540774 : return promote;
9707 : }
9708 :
9709 : /* ARG is a default argument expression being passed to a parameter of
9710 : the indicated TYPE, which is a parameter to FN. PARMNUM is the
9711 : zero-based argument number. Do any required conversions. Return
9712 : the converted value. */
9713 :
9714 : static GTY(()) vec<tree, va_gc> *default_arg_context;
9715 : void
9716 8098676 : push_defarg_context (tree fn)
9717 8098676 : { vec_safe_push (default_arg_context, fn); }
9718 :
9719 : void
9720 8098676 : pop_defarg_context (void)
9721 8098676 : { default_arg_context->pop (); }
9722 :
9723 : tree
9724 1653603 : convert_default_arg (tree type, tree arg, tree fn, int parmnum,
9725 : tsubst_flags_t complain)
9726 : {
9727 1653603 : int i;
9728 1653603 : tree t;
9729 :
9730 : /* See through clones. */
9731 1653603 : fn = DECL_ORIGIN (fn);
9732 : /* And inheriting ctors. */
9733 1653603 : if (flag_new_inheriting_ctors)
9734 1652963 : fn = strip_inheriting_ctors (fn);
9735 :
9736 : /* Detect recursion. */
9737 1655115 : FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t)
9738 1518 : if (t == fn)
9739 : {
9740 6 : if (complain & tf_error)
9741 6 : error ("recursive evaluation of default argument for %q#D", fn);
9742 6 : return error_mark_node;
9743 : }
9744 :
9745 : /* If the ARG is an unparsed default argument expression, the
9746 : conversion cannot be performed. */
9747 1653597 : if (TREE_CODE (arg) == DEFERRED_PARSE)
9748 : {
9749 15 : if (complain & tf_error)
9750 15 : error ("call to %qD uses the default argument for parameter %P, which "
9751 : "is not yet defined", fn, parmnum);
9752 15 : return error_mark_node;
9753 : }
9754 :
9755 1653582 : push_defarg_context (fn);
9756 :
9757 1653582 : if (fn && DECL_TEMPLATE_INFO (fn))
9758 1373806 : arg = tsubst_default_argument (fn, parmnum, type, arg, complain);
9759 :
9760 : /* Due to:
9761 :
9762 : [dcl.fct.default]
9763 :
9764 : The names in the expression are bound, and the semantic
9765 : constraints are checked, at the point where the default
9766 : expressions appears.
9767 :
9768 : we must not perform access checks here. */
9769 1653582 : push_deferring_access_checks (dk_no_check);
9770 : /* We must make a copy of ARG, in case subsequent processing
9771 : alters any part of it. */
9772 1653582 : arg = break_out_target_exprs (arg, /*clear location*/true);
9773 :
9774 1653582 : arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
9775 : ICR_DEFAULT_ARGUMENT, fn, parmnum,
9776 : complain);
9777 1653582 : arg = convert_for_arg_passing (type, arg, complain);
9778 1653582 : pop_deferring_access_checks();
9779 :
9780 1653582 : pop_defarg_context ();
9781 :
9782 1653582 : return arg;
9783 : }
9784 :
9785 : /* Returns the type which will really be used for passing an argument of
9786 : type TYPE. */
9787 :
9788 : tree
9789 684752522 : type_passed_as (tree type)
9790 : {
9791 : /* Pass classes with copy ctors by invisible reference. */
9792 684752522 : if (TREE_ADDRESSABLE (type))
9793 581401 : type = build_reference_type (type);
9794 :
9795 684752522 : return type;
9796 : }
9797 :
9798 : /* Actually perform the appropriate conversion. */
9799 :
9800 : tree
9801 187184619 : convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
9802 : {
9803 187184619 : tree bitfield_type;
9804 :
9805 : /* If VAL is a bitfield, then -- since it has already been converted
9806 : to TYPE -- it cannot have a precision greater than TYPE.
9807 :
9808 : If it has a smaller precision, we must widen it here. For
9809 : example, passing "int f:3;" to a function expecting an "int" will
9810 : not result in any conversion before this point.
9811 :
9812 : If the precision is the same we must not risk widening. For
9813 : example, the COMPONENT_REF for a 32-bit "long long" bitfield will
9814 : often have type "int", even though the C++ type for the field is
9815 : "long long". If the value is being passed to a function
9816 : expecting an "int", then no conversions will be required. But,
9817 : if we call convert_bitfield_to_declared_type, the bitfield will
9818 : be converted to "long long". */
9819 187184619 : bitfield_type = is_bitfield_expr_with_lowered_type (val);
9820 187184619 : if (bitfield_type
9821 187184619 : && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
9822 0 : val = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type), val);
9823 :
9824 187184619 : if (val == error_mark_node)
9825 : ;
9826 : /* Pass classes with copy ctors by invisible reference. */
9827 187181766 : else if (TREE_ADDRESSABLE (type))
9828 193574 : val = build1 (ADDR_EXPR, build_reference_type (type), val);
9829 187184619 : if (complain & tf_warning)
9830 164563073 : maybe_warn_parm_abi (type, cp_expr_loc_or_input_loc (val));
9831 :
9832 131340143 : if (complain & tf_warning)
9833 131340143 : warn_for_address_of_packed_member (type, val);
9834 :
9835 : /* gimplify_arg elides TARGET_EXPRs that initialize a function argument,
9836 : unless the initializer is a CONSTRUCTOR. In that case, we fail to
9837 : elide the copy anyway. See that function for more information. */
9838 12145754 : if (SIMPLE_TARGET_EXPR_P (val)
9839 197332310 : && TREE_CODE (TARGET_EXPR_INITIAL (val)) != CONSTRUCTOR)
9840 7363654 : set_target_expr_eliding (val);
9841 :
9842 187184619 : return val;
9843 : }
9844 :
9845 : /* Returns non-zero iff FN is a function with magic varargs, i.e. ones for
9846 : which just decay_conversion or no conversions at all should be done.
9847 : This is true for some builtins which don't act like normal functions.
9848 : Return 2 if just decay_conversion and removal of excess precision should
9849 : be done, 1 if just decay_conversion. Return 3 for special treatment of
9850 : the 3rd argument for __builtin_*_overflow_p. Return 4 for special
9851 : treatment of the 1st argument for
9852 : __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g. */
9853 :
9854 : int
9855 234713217 : magic_varargs_p (tree fn)
9856 : {
9857 234713217 : if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
9858 6804179 : switch (DECL_FUNCTION_CODE (fn))
9859 : {
9860 : case BUILT_IN_CLASSIFY_TYPE:
9861 : case BUILT_IN_CONSTANT_P:
9862 : case BUILT_IN_NEXT_ARG:
9863 : case BUILT_IN_VA_START:
9864 : return 1;
9865 :
9866 20842 : case BUILT_IN_ADD_OVERFLOW_P:
9867 20842 : case BUILT_IN_SUB_OVERFLOW_P:
9868 20842 : case BUILT_IN_MUL_OVERFLOW_P:
9869 20842 : return 3;
9870 :
9871 376919 : case BUILT_IN_ISFINITE:
9872 376919 : case BUILT_IN_ISINF:
9873 376919 : case BUILT_IN_ISINF_SIGN:
9874 376919 : case BUILT_IN_ISNAN:
9875 376919 : case BUILT_IN_ISNORMAL:
9876 376919 : case BUILT_IN_FPCLASSIFY:
9877 376919 : return 2;
9878 :
9879 91614 : case BUILT_IN_CLZG:
9880 91614 : case BUILT_IN_CTZG:
9881 91614 : case BUILT_IN_CLRSBG:
9882 91614 : case BUILT_IN_FFSG:
9883 91614 : case BUILT_IN_PARITYG:
9884 91614 : case BUILT_IN_POPCOUNTG:
9885 91614 : return 4;
9886 :
9887 6073121 : default:
9888 6073121 : return lookup_attribute ("type generic",
9889 12146242 : TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
9890 : }
9891 :
9892 : return 0;
9893 : }
9894 :
9895 : /* Returns the decl of the dispatcher function if FN is a function version. */
9896 :
9897 : tree
9898 237 : get_function_version_dispatcher (tree fn)
9899 : {
9900 237 : tree dispatcher_decl = NULL;
9901 :
9902 237 : if (DECL_LOCAL_DECL_P (fn))
9903 9 : fn = DECL_LOCAL_DECL_ALIAS (fn);
9904 :
9905 237 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
9906 : && DECL_FUNCTION_VERSIONED (fn));
9907 :
9908 237 : gcc_assert (targetm.get_function_versions_dispatcher);
9909 237 : dispatcher_decl = targetm.get_function_versions_dispatcher (fn);
9910 :
9911 237 : if (dispatcher_decl == NULL)
9912 : {
9913 9 : error_at (input_location, "use of multiversioned function "
9914 : "without a default");
9915 9 : return NULL;
9916 : }
9917 :
9918 228 : retrofit_lang_decl (dispatcher_decl);
9919 228 : gcc_assert (dispatcher_decl != NULL);
9920 : return dispatcher_decl;
9921 : }
9922 :
9923 : /* fn is a function version dispatcher that is marked used. Mark all the
9924 : semantically identical function versions it will dispatch as used. */
9925 :
9926 : void
9927 156 : mark_versions_used (tree fn)
9928 : {
9929 156 : struct cgraph_node *node;
9930 156 : struct cgraph_function_version_info *node_v;
9931 156 : struct cgraph_function_version_info *it_v;
9932 :
9933 156 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9934 :
9935 156 : node = cgraph_node::get (fn);
9936 156 : if (node == NULL)
9937 : return;
9938 :
9939 156 : gcc_assert (node->dispatcher_function);
9940 :
9941 156 : node_v = node->function_version ();
9942 156 : if (node_v == NULL)
9943 : return;
9944 :
9945 : /* All semantically identical versions are chained. Traverse and mark each
9946 : one of them as used. */
9947 156 : it_v = node_v->next;
9948 1128 : while (it_v != NULL)
9949 : {
9950 972 : mark_used (it_v->this_node->decl);
9951 972 : it_v = it_v->next;
9952 : }
9953 : }
9954 :
9955 : /* Build a call to "the copy constructor" for the type of A, even if it
9956 : wouldn't be selected by normal overload resolution. Used for
9957 : diagnostics. */
9958 :
9959 : static tree
9960 3 : call_copy_ctor (tree a, tsubst_flags_t complain)
9961 : {
9962 3 : tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (a));
9963 3 : tree binfo = TYPE_BINFO (ctype);
9964 3 : tree copy = get_copy_ctor (ctype, complain);
9965 3 : copy = build_baselink (binfo, binfo, copy, NULL_TREE);
9966 3 : tree ob = build_dummy_object (ctype);
9967 3 : releasing_vec args (make_tree_vector_single (a));
9968 3 : tree r = build_new_method_call (ob, copy, &args, NULL_TREE,
9969 : LOOKUP_NORMAL, NULL, complain);
9970 3 : return r;
9971 3 : }
9972 :
9973 : /* Return the base constructor corresponding to COMPLETE_CTOR or NULL_TREE. */
9974 :
9975 : static tree
9976 42 : base_ctor_for (tree complete_ctor)
9977 : {
9978 42 : tree clone;
9979 42 : FOR_EACH_CLONE (clone, DECL_CLONED_FUNCTION (complete_ctor))
9980 42 : if (DECL_BASE_CONSTRUCTOR_P (clone))
9981 : return clone;
9982 : return NULL_TREE;
9983 : }
9984 :
9985 : /* Try to make EXP suitable to be used as the initializer for a base subobject,
9986 : and return whether we were successful. EXP must have already been cleared
9987 : by unsafe_copy_elision_p{,_opt}. */
9988 :
9989 : static bool
9990 206 : make_base_init_ok (tree exp)
9991 : {
9992 206 : if (TREE_CODE (exp) == TARGET_EXPR)
9993 206 : exp = TARGET_EXPR_INITIAL (exp);
9994 209 : while (TREE_CODE (exp) == COMPOUND_EXPR)
9995 3 : exp = TREE_OPERAND (exp, 1);
9996 206 : if (TREE_CODE (exp) == COND_EXPR)
9997 : {
9998 3 : bool ret = make_base_init_ok (TREE_OPERAND (exp, 2));
9999 3 : if (tree op1 = TREE_OPERAND (exp, 1))
10000 : {
10001 3 : bool r1 = make_base_init_ok (op1);
10002 : /* If unsafe_copy_elision_p was false, the arms should match. */
10003 3 : gcc_assert (r1 == ret);
10004 : }
10005 3 : return ret;
10006 : }
10007 203 : if (TREE_CODE (exp) != AGGR_INIT_EXPR)
10008 : /* A trivial copy is OK. */
10009 : return true;
10010 60 : if (!AGGR_INIT_VIA_CTOR_P (exp))
10011 : /* unsafe_copy_elision_p_opt must have said this is OK. */
10012 : return true;
10013 42 : tree fn = cp_get_callee_fndecl_nofold (exp);
10014 42 : if (DECL_BASE_CONSTRUCTOR_P (fn))
10015 : return true;
10016 42 : gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (fn));
10017 42 : fn = base_ctor_for (fn);
10018 42 : if (!fn || DECL_HAS_VTT_PARM_P (fn))
10019 : /* The base constructor has more parameters, so we can't just change the
10020 : call target. It would be possible to splice in the appropriate
10021 : arguments, but probably not worth the complexity. */
10022 : return false;
10023 33 : mark_used (fn);
10024 33 : AGGR_INIT_EXPR_FN (exp) = build_address (fn);
10025 33 : return true;
10026 : }
10027 :
10028 : /* Return 2 if T refers to a base, 1 if a potentially-overlapping field,
10029 : neither of which can be used for return by invisible reference. We avoid
10030 : doing C++17 mandatory copy elision for either of these cases.
10031 :
10032 : This returns non-zero even if the type of T has no tail padding that other
10033 : data could be allocated into, because that depends on the particular ABI.
10034 : unsafe_copy_elision_p_opt does consider whether there is padding. */
10035 :
10036 : int
10037 58064661 : unsafe_return_slot_p (tree t)
10038 : {
10039 : /* Check empty bases separately, they don't have fields. */
10040 58064661 : if (is_empty_base_ref (t))
10041 : return 2;
10042 :
10043 : /* A delegating constructor might be used to initialize a base. */
10044 57581279 : if (current_function_decl
10045 80129670 : && DECL_CONSTRUCTOR_P (current_function_decl)
10046 62273173 : && (t == current_class_ref
10047 4529624 : || tree_strip_nop_conversions (t) == current_class_ptr))
10048 209997 : return 2;
10049 :
10050 57371282 : STRIP_NOPS (t);
10051 57371282 : if (TREE_CODE (t) == ADDR_EXPR)
10052 77655 : t = TREE_OPERAND (t, 0);
10053 57371282 : if (TREE_CODE (t) == COMPONENT_REF)
10054 4729107 : t = TREE_OPERAND (t, 1);
10055 57371282 : if (TREE_CODE (t) != FIELD_DECL)
10056 : return false;
10057 4854759 : if (!CLASS_TYPE_P (TREE_TYPE (t)))
10058 : /* The middle-end will do the right thing for scalar types. */
10059 : return false;
10060 3390984 : if (DECL_FIELD_IS_BASE (t))
10061 : return 2;
10062 2590272 : if (lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (t)))
10063 : return 1;
10064 : return 0;
10065 : }
10066 :
10067 : /* True IFF EXP is a prvalue that represents return by invisible reference. */
10068 :
10069 : static bool
10070 326112 : init_by_return_slot_p (tree exp)
10071 : {
10072 : /* Copy elision only happens with a TARGET_EXPR. */
10073 326115 : if (TREE_CODE (exp) != TARGET_EXPR)
10074 : return false;
10075 68440 : tree init = TARGET_EXPR_INITIAL (exp);
10076 : /* build_compound_expr pushes COMPOUND_EXPR inside TARGET_EXPR. */
10077 68446 : while (TREE_CODE (init) == COMPOUND_EXPR)
10078 6 : init = TREE_OPERAND (init, 1);
10079 68440 : if (TREE_CODE (init) == COND_EXPR)
10080 : {
10081 : /* We'll end up copying from each of the arms of the COND_EXPR directly
10082 : into the target, so look at them. */
10083 6 : if (tree op = TREE_OPERAND (init, 1))
10084 6 : if (init_by_return_slot_p (op))
10085 : return true;
10086 3 : return init_by_return_slot_p (TREE_OPERAND (init, 2));
10087 : }
10088 68434 : return (TREE_CODE (init) == AGGR_INIT_EXPR
10089 68434 : && !AGGR_INIT_VIA_CTOR_P (init));
10090 : }
10091 :
10092 : /* We can't elide a copy from a function returning by value to a
10093 : potentially-overlapping subobject, as the callee might clobber tail padding.
10094 : Return true iff this could be that case.
10095 :
10096 : Places that use this function (or _opt) to decide to elide a copy should
10097 : probably use make_safe_copy_elision instead. */
10098 :
10099 : bool
10100 3323170 : unsafe_copy_elision_p (tree target, tree exp)
10101 : {
10102 3323170 : return unsafe_return_slot_p (target) && init_by_return_slot_p (exp);
10103 : }
10104 :
10105 : /* As above, but for optimization allow more cases that are actually safe. */
10106 :
10107 : static bool
10108 11687828 : unsafe_copy_elision_p_opt (tree target, tree exp)
10109 : {
10110 11687828 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
10111 : /* It's safe to elide the copy for a class with no tail padding. */
10112 11687828 : if (!is_empty_class (type)
10113 11687828 : && tree_int_cst_equal (TYPE_SIZE (type), CLASSTYPE_SIZE (type)))
10114 : return false;
10115 3197518 : return unsafe_copy_elision_p (target, exp);
10116 : }
10117 :
10118 : /* Try to make EXP suitable to be used as the initializer for TARGET,
10119 : and return whether we were successful. */
10120 :
10121 : bool
10122 23 : make_safe_copy_elision (tree target, tree exp)
10123 : {
10124 23 : int uns = unsafe_return_slot_p (target);
10125 23 : if (!uns)
10126 : return true;
10127 23 : if (init_by_return_slot_p (exp))
10128 : return false;
10129 23 : if (uns == 1)
10130 : return true;
10131 19 : return make_base_init_ok (exp);
10132 : }
10133 :
10134 : /* True IFF the result of the conversion C is a prvalue. */
10135 :
10136 : static bool
10137 19219684 : conv_is_prvalue (conversion *c)
10138 : {
10139 19219684 : if (c->kind == ck_rvalue)
10140 : return true;
10141 19219684 : if (c->kind == ck_base && c->need_temporary_p)
10142 : return true;
10143 19219684 : if (c->kind == ck_user && !TYPE_REF_P (c->type))
10144 : return true;
10145 19091675 : if (c->kind == ck_identity && c->u.expr
10146 18860583 : && TREE_CODE (c->u.expr) == TARGET_EXPR)
10147 56 : return true;
10148 :
10149 : return false;
10150 : }
10151 :
10152 : /* True iff C is a conversion that binds a reference to a prvalue. */
10153 :
10154 : static bool
10155 19220700 : conv_binds_ref_to_prvalue (conversion *c)
10156 : {
10157 19220700 : if (c->kind != ck_ref_bind)
10158 : return false;
10159 19220700 : if (c->need_temporary_p)
10160 : return true;
10161 :
10162 19219684 : return conv_is_prvalue (next_conversion (c));
10163 : }
10164 :
10165 : /* True iff EXPR represents a (subobject of a) temporary. */
10166 :
10167 : static bool
10168 13825 : expr_represents_temporary_p (tree expr)
10169 : {
10170 13929 : while (handled_component_p (expr))
10171 104 : expr = TREE_OPERAND (expr, 0);
10172 13825 : return TREE_CODE (expr) == TARGET_EXPR;
10173 : }
10174 :
10175 : /* True iff C is a conversion that binds a reference to a temporary.
10176 : This is a superset of conv_binds_ref_to_prvalue: here we're also
10177 : interested in xvalues. */
10178 :
10179 : static bool
10180 13127 : conv_binds_ref_to_temporary (conversion *c)
10181 : {
10182 13127 : if (conv_binds_ref_to_prvalue (c))
10183 : return true;
10184 12700 : if (c->kind != ck_ref_bind)
10185 : return false;
10186 12700 : c = next_conversion (c);
10187 : /* This is the case for
10188 : struct Base {};
10189 : struct Derived : Base {};
10190 : const Base& b(Derived{});
10191 : where we bind 'b' to the Base subobject of a temporary object of type
10192 : Derived. The subobject is an xvalue; the whole object is a prvalue.
10193 :
10194 : The ck_base doesn't have to be present for cases like X{}.m. */
10195 12700 : if (c->kind == ck_base)
10196 8 : c = next_conversion (c);
10197 12632 : if (c->kind == ck_identity && c->u.expr
10198 25332 : && expr_represents_temporary_p (c->u.expr))
10199 : return true;
10200 : return false;
10201 : }
10202 :
10203 : /* Return tristate::TS_TRUE if converting EXPR to a reference type TYPE binds
10204 : the reference to a temporary. Return tristate::TS_FALSE if converting
10205 : EXPR to a reference type TYPE doesn't bind the reference to a temporary. If
10206 : the conversion is invalid or bad, return tristate::TS_UNKNOWN. DIRECT_INIT_P
10207 : says whether the conversion should be done in direct- or copy-initialization
10208 : context. */
10209 :
10210 : tristate
10211 13628 : ref_conv_binds_to_temporary (tree type, tree expr, bool direct_init_p/*=false*/)
10212 : {
10213 13628 : gcc_assert (TYPE_REF_P (type));
10214 :
10215 13628 : conversion_obstack_sentinel cos;
10216 :
10217 13628 : const int flags = direct_init_p ? LOOKUP_NORMAL : LOOKUP_IMPLICIT;
10218 13628 : conversion *conv = implicit_conversion (type, TREE_TYPE (expr), expr,
10219 : /*c_cast_p=*/false, flags, tf_none);
10220 13628 : if (!conv || conv->bad_p)
10221 501 : return tristate::unknown ();
10222 :
10223 13127 : if (conv_binds_ref_to_temporary (conv))
10224 : {
10225 : /* Actually perform the conversion to check access control. */
10226 433 : if (convert_like (conv, expr, tf_none) != error_mark_node)
10227 421 : return tristate (true);
10228 : else
10229 12 : return tristate::unknown ();
10230 : }
10231 :
10232 12694 : return tristate (false);
10233 13628 : }
10234 :
10235 : /* Call the trivial destructor for INSTANCE, which can be either an lvalue of
10236 : class type or a pointer to class type. If NO_PTR_DEREF is true and
10237 : INSTANCE has pointer type, clobber the pointer rather than what it points
10238 : to. */
10239 :
10240 : tree
10241 3407182 : build_trivial_dtor_call (tree instance, bool no_ptr_deref)
10242 : {
10243 3407182 : gcc_assert (!is_dummy_object (instance));
10244 :
10245 3407182 : if (!flag_lifetime_dse)
10246 : {
10247 58 : no_clobber:
10248 77 : return fold_convert (void_type_node, instance);
10249 : }
10250 :
10251 3473939 : if (INDIRECT_TYPE_P (TREE_TYPE (instance))
10252 3407124 : && (!no_ptr_deref || TYPE_REF_P (TREE_TYPE (instance))))
10253 : {
10254 3231934 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
10255 19 : goto no_clobber;
10256 3231915 : instance = cp_build_fold_indirect_ref (instance);
10257 : }
10258 :
10259 : /* A trivial destructor should still clobber the object. */
10260 3407105 : tree clobber = build_clobber (TREE_TYPE (instance), CLOBBER_OBJECT_END);
10261 3407105 : return build2 (MODIFY_EXPR, void_type_node,
10262 3407105 : instance, clobber);
10263 : }
10264 :
10265 : /* Return true if in an immediate function context, or an unevaluated operand,
10266 : or a default argument/member initializer, or a subexpression of an immediate
10267 : invocation. */
10268 :
10269 : bool
10270 12148152 : in_immediate_context ()
10271 : {
10272 12148152 : return (cp_unevaluated_operand != 0
10273 10660016 : || (current_function_decl != NULL_TREE
10274 18502790 : && DECL_IMMEDIATE_FUNCTION_P (current_function_decl))
10275 : /* DR 2631: default args and DMI aren't immediately evaluated.
10276 : Return true here so immediate_invocation_p returns false. */
10277 9940971 : || current_binding_level->kind == sk_function_parms
10278 9940139 : || current_binding_level->kind == sk_template_parms
10279 9913669 : || parsing_nsdmi ()
10280 22060671 : || in_consteval_if_p);
10281 : }
10282 :
10283 : /* Return true if a call to FN with number of arguments NARGS
10284 : is an immediate invocation. */
10285 :
10286 : bool
10287 273117836 : immediate_invocation_p (tree fn)
10288 : {
10289 273117836 : return (TREE_CODE (fn) == FUNCTION_DECL
10290 273117836 : && DECL_IMMEDIATE_FUNCTION_P (fn)
10291 277018143 : && !in_immediate_context ());
10292 : }
10293 :
10294 : /* Subroutine of the various build_*_call functions. Overload resolution
10295 : has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
10296 : ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
10297 : bitmask of various LOOKUP_* flags which apply to the call itself. */
10298 :
10299 : static tree
10300 296174208 : build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
10301 : {
10302 296174208 : tree fn = cand->fn;
10303 296174208 : const vec<tree, va_gc> *args = cand->args;
10304 296174208 : tree first_arg = cand->first_arg;
10305 296174208 : conversion **convs = cand->convs;
10306 296174208 : tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
10307 296174208 : int parmlen;
10308 296174208 : tree val;
10309 296174208 : int nargs;
10310 296174208 : tree *argarray;
10311 296174208 : bool already_used = false;
10312 :
10313 : /* In a template, there is no need to perform all of the work that
10314 : is normally done. We are only interested in the type of the call
10315 : expression, i.e., the return type of the function. Any semantic
10316 : errors will be deferred until the template is instantiated. */
10317 296174208 : if (processing_template_decl)
10318 : {
10319 29212673 : if (undeduced_auto_decl (fn))
10320 9889 : mark_used (fn, complain);
10321 : else
10322 : /* Otherwise set TREE_USED for the benefit of -Wunused-function.
10323 : See PR80598. */
10324 29202784 : TREE_USED (fn) = 1;
10325 :
10326 29212673 : tree return_type = TREE_TYPE (TREE_TYPE (fn));
10327 29212673 : tree callee;
10328 29212673 : if (first_arg == NULL_TREE)
10329 : {
10330 21228351 : callee = build_addr_func (fn, complain);
10331 21228351 : if (callee == error_mark_node)
10332 : return error_mark_node;
10333 : }
10334 : else
10335 : {
10336 7984322 : callee = build_baselink (cand->conversion_path, cand->access_path,
10337 : fn, NULL_TREE);
10338 7984322 : callee = build_min (COMPONENT_REF, TREE_TYPE (fn),
10339 : first_arg, callee, NULL_TREE);
10340 : }
10341 :
10342 29212673 : tree expr = build_call_vec (return_type, callee, args);
10343 29212673 : SET_EXPR_LOCATION (expr, input_location);
10344 29212673 : if (TREE_THIS_VOLATILE (fn) && cfun)
10345 2423690 : current_function_returns_abnormally = 1;
10346 29212673 : if (TREE_DEPRECATED (fn)
10347 29212673 : && warning_suppressed_at (input_location,
10348 : OPT_Wdeprecated_declarations))
10349 : /* Make the expr consistent with the location. */
10350 18983 : TREE_NO_WARNING (expr) = true;
10351 29212673 : if (immediate_invocation_p (fn))
10352 : {
10353 189718 : tree obj_arg = NULL_TREE;
10354 379436 : if (DECL_CONSTRUCTOR_P (fn))
10355 3 : obj_arg = first_arg;
10356 : /* Look through *(const T *)&obj. */
10357 3 : if (obj_arg && INDIRECT_REF_P (obj_arg))
10358 : {
10359 3 : tree addr = TREE_OPERAND (obj_arg, 0);
10360 3 : STRIP_NOPS (addr);
10361 3 : if (TREE_CODE (addr) == ADDR_EXPR)
10362 : {
10363 0 : tree typeo = TREE_TYPE (obj_arg);
10364 0 : tree typei = TREE_TYPE (TREE_OPERAND (addr, 0));
10365 0 : if (same_type_ignoring_top_level_qualifiers_p (typeo, typei))
10366 0 : obj_arg = TREE_OPERAND (addr, 0);
10367 : }
10368 : }
10369 189718 : fold_non_dependent_expr (expr, complain,
10370 : /*manifestly_const_eval=*/true,
10371 : obj_arg);
10372 : }
10373 29212673 : return convert_from_reference (expr);
10374 : }
10375 :
10376 : /* Give any warnings we noticed during overload resolution. */
10377 266961535 : if (cand->warnings && (complain & tf_warning))
10378 : {
10379 : struct candidate_warning *w;
10380 98 : for (w = cand->warnings; w; w = w->next)
10381 49 : joust (cand, w->loser, 1, complain);
10382 : }
10383 :
10384 : /* Core issue 2327: P0135 doesn't say how to handle the case where the
10385 : argument to the copy constructor ends up being a prvalue after
10386 : conversion. Let's do the normal processing, but pretend we aren't
10387 : actually using the copy constructor. */
10388 266961535 : bool force_elide = false;
10389 266961535 : if (cxx_dialect >= cxx17
10390 264978166 : && cand->num_convs == 1
10391 146220342 : && DECL_COMPLETE_CONSTRUCTOR_P (fn)
10392 51199282 : && (DECL_COPY_CONSTRUCTOR_P (fn)
10393 25046130 : || DECL_MOVE_CONSTRUCTOR_P (fn))
10394 19289680 : && !unsafe_return_slot_p (first_arg)
10395 286169054 : && conv_binds_ref_to_prvalue (convs[0]))
10396 : {
10397 128651 : force_elide = true;
10398 128651 : goto not_really_used;
10399 : }
10400 :
10401 : /* OK, we're actually calling this inherited constructor; set its deletedness
10402 : appropriately. We can get away with doing this here because calling is
10403 : the only way to refer to a constructor. */
10404 533665768 : if (DECL_INHERITED_CTOR (fn)
10405 37574870 : && !deduce_inheriting_ctor (fn))
10406 : {
10407 2 : if (complain & tf_error)
10408 2 : mark_used (fn);
10409 2 : return error_mark_node;
10410 : }
10411 :
10412 : /* Make =delete work with SFINAE. */
10413 266832882 : if (DECL_DELETED_FN (fn))
10414 : {
10415 1207116 : if (complain & tf_error)
10416 : {
10417 2258 : mark_used (fn);
10418 2258 : if (cand->next)
10419 : {
10420 2065 : if (flag_diagnostics_all_candidates)
10421 9 : print_z_candidates (input_location, cand, /*only_viable_p=*/false);
10422 : else
10423 2056 : inform (input_location,
10424 : "use %<-fdiagnostics-all-candidates%> to display "
10425 : "considered candidates");
10426 : }
10427 : }
10428 1207116 : return error_mark_node;
10429 : }
10430 :
10431 265625766 : if (DECL_FUNCTION_MEMBER_P (fn))
10432 : {
10433 138487806 : tree access_fn;
10434 : /* If FN is a template function, two cases must be considered.
10435 : For example:
10436 :
10437 : struct A {
10438 : protected:
10439 : template <class T> void f();
10440 : };
10441 : template <class T> struct B {
10442 : protected:
10443 : void g();
10444 : };
10445 : struct C : A, B<int> {
10446 : using A::f; // #1
10447 : using B<int>::g; // #2
10448 : };
10449 :
10450 : In case #1 where `A::f' is a member template, DECL_ACCESS is
10451 : recorded in the primary template but not in its specialization.
10452 : We check access of FN using its primary template.
10453 :
10454 : In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
10455 : because it is a member of class template B, DECL_ACCESS is
10456 : recorded in the specialization `B<int>::g'. We cannot use its
10457 : primary template because `B<T>::g' and `B<int>::g' may have
10458 : different access. */
10459 138487806 : if (DECL_TEMPLATE_INFO (fn)
10460 138487806 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
10461 11047092 : access_fn = DECL_TI_TEMPLATE (fn);
10462 : else
10463 : access_fn = fn;
10464 138487806 : if (!perform_or_defer_access_check (cand->access_path, access_fn,
10465 : fn, complain))
10466 20062 : return error_mark_node;
10467 : }
10468 :
10469 : /* If we're checking for implicit delete, don't bother with argument
10470 : conversions. */
10471 265605704 : if (flags & LOOKUP_SPECULATIVE)
10472 : {
10473 31018807 : if (cand->viable == 1)
10474 : return fn;
10475 117 : else if (!(complain & tf_error))
10476 : /* Reject bad conversions now. */
10477 102 : return error_mark_node;
10478 : /* else continue to get conversion error. */
10479 : }
10480 :
10481 234586897 : not_really_used:
10482 :
10483 : /* N3276 magic doesn't apply to nested calls. */
10484 234715563 : tsubst_flags_t decltype_flag = (complain & tf_decltype);
10485 234715563 : complain &= ~tf_decltype;
10486 : /* No-Cleanup doesn't apply to nested calls either. */
10487 234715563 : tsubst_flags_t no_cleanup_complain = complain;
10488 234715563 : complain &= ~tf_no_cleanup;
10489 :
10490 : /* Find maximum size of vector to hold converted arguments. */
10491 234715563 : parmlen = list_length (parm);
10492 445720754 : nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0);
10493 234715563 : if (parmlen > nargs)
10494 : nargs = parmlen;
10495 234715563 : argarray = XALLOCAVEC (tree, nargs);
10496 :
10497 469431123 : in_consteval_if_p_temp_override icip;
10498 : /* If the call is immediate function invocation, make sure
10499 : taking address of immediate functions is allowed in its arguments. */
10500 234715563 : if (immediate_invocation_p (STRIP_TEMPLATE (fn)))
10501 2150256 : in_consteval_if_p = true;
10502 :
10503 234715563 : int argarray_size = 0;
10504 234715563 : unsigned int arg_index = 0;
10505 234715563 : int conv_index = 0;
10506 234715563 : int param_index = 0;
10507 234715563 : tree parmd = DECL_ARGUMENTS (fn);
10508 :
10509 328740688 : auto consume_object_arg = [&arg_index, &first_arg, args]()
10510 : {
10511 94025125 : if (!first_arg)
10512 0 : return (*args)[arg_index++];
10513 94025125 : tree object_arg = first_arg;
10514 94025125 : first_arg = NULL_TREE;
10515 94025125 : return object_arg;
10516 234715563 : };
10517 :
10518 : /* The implicit parameters to a constructor are not considered by overload
10519 : resolution, and must be of the proper type. */
10520 234715563 : if (DECL_CONSTRUCTOR_P (fn))
10521 : {
10522 27071866 : tree object_arg = consume_object_arg ();
10523 27071866 : argarray[argarray_size++] = build_this (object_arg);
10524 27071866 : parm = TREE_CHAIN (parm);
10525 27071866 : if (parmd)
10526 27071866 : parmd = DECL_CHAIN (parmd);
10527 : /* We should never try to call the abstract constructor. */
10528 27071866 : gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
10529 :
10530 27071866 : if (DECL_HAS_VTT_PARM_P (fn))
10531 : {
10532 16900 : argarray[argarray_size++] = (*args)[arg_index];
10533 16900 : ++arg_index;
10534 16900 : parm = TREE_CHAIN (parm);
10535 16900 : if (parmd)
10536 16900 : parmd = DECL_CHAIN (parmd);
10537 : }
10538 : }
10539 : /* Bypass access control for 'this' parameter. */
10540 207643697 : else if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
10541 : {
10542 66947109 : tree arg = build_this (consume_object_arg ());
10543 66947109 : tree argtype = TREE_TYPE (arg);
10544 :
10545 66947109 : if (arg == error_mark_node)
10546 : return error_mark_node;
10547 66947106 : if (convs[conv_index++]->bad_p)
10548 : {
10549 1053 : if (complain & tf_error)
10550 : {
10551 103 : auto_diagnostic_group d;
10552 103 : if (permerror (input_location, "passing %qT as %<this%> "
10553 : "argument discards qualifiers",
10554 103 : TREE_TYPE (argtype)))
10555 100 : inform (DECL_SOURCE_LOCATION (fn), " in call to %qD", fn);
10556 103 : }
10557 : else
10558 : return error_mark_node;
10559 : }
10560 :
10561 : /* The class where FN is defined. */
10562 66946156 : tree ctx = DECL_CONTEXT (fn);
10563 :
10564 : /* See if the function member or the whole class type is declared
10565 : final and the call can be devirtualized. */
10566 66946156 : if (DECL_FINAL_P (fn) || CLASSTYPE_FINAL (ctx))
10567 139162 : flags |= LOOKUP_NONVIRTUAL;
10568 :
10569 : /* [class.mfct.non-static]: If a non-static member function of a class
10570 : X is called for an object that is not of type X, or of a type
10571 : derived from X, the behavior is undefined.
10572 :
10573 : So we can assume that anything passed as 'this' is non-null, and
10574 : optimize accordingly. */
10575 : /* Check that the base class is accessible. */
10576 66946156 : if (!accessible_base_p (TREE_TYPE (argtype),
10577 66946156 : BINFO_TYPE (cand->conversion_path), true))
10578 : {
10579 33 : if (complain & tf_error)
10580 30 : error ("%qT is not an accessible base of %qT",
10581 30 : BINFO_TYPE (cand->conversion_path),
10582 30 : TREE_TYPE (argtype));
10583 : else
10584 3 : return error_mark_node;
10585 : }
10586 : /* If fn was found by a using declaration, the conversion path
10587 : will be to the derived class, not the base declaring fn. We
10588 : must convert to the base. */
10589 66946153 : tree base_binfo = cand->conversion_path;
10590 66946153 : if (BINFO_TYPE (base_binfo) != ctx)
10591 : {
10592 128824 : base_binfo = lookup_base (base_binfo, ctx, ba_unique, NULL, complain);
10593 128824 : if (base_binfo == error_mark_node)
10594 : return error_mark_node;
10595 : }
10596 :
10597 : /* If we know the dynamic type of the object, look up the final overrider
10598 : in the BINFO. */
10599 68115857 : if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
10600 67654273 : && resolves_to_fixed_type_p (arg))
10601 : {
10602 1058 : tree ov = lookup_vfn_in_binfo (DECL_VINDEX (fn), base_binfo);
10603 :
10604 : /* And unwind base_binfo to match. If we don't find the type we're
10605 : looking for in BINFO_INHERITANCE_CHAIN, we're looking at diamond
10606 : inheritance; for now do a normal virtual call in that case. */
10607 1058 : tree octx = DECL_CONTEXT (ov);
10608 1058 : tree obinfo = base_binfo;
10609 2181 : while (obinfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (obinfo), octx))
10610 34 : obinfo = BINFO_INHERITANCE_CHAIN (obinfo);
10611 1058 : if (obinfo)
10612 : {
10613 1055 : fn = ov;
10614 1055 : base_binfo = obinfo;
10615 1055 : flags |= LOOKUP_NONVIRTUAL;
10616 : }
10617 : }
10618 :
10619 66946147 : tree converted_arg = build_base_path (PLUS_EXPR, arg,
10620 : base_binfo, 1, complain);
10621 :
10622 66946147 : argarray[argarray_size++] = converted_arg;
10623 66946147 : parm = TREE_CHAIN (parm);
10624 66946147 : if (parmd)
10625 66946147 : parmd = DECL_CHAIN (parmd);
10626 : }
10627 :
10628 415620646 : auto handle_arg = [fn, flags](tree type,
10629 : tree arg,
10630 : int const param_index,
10631 : conversion *conv,
10632 : tsubst_flags_t const arg_complain)
10633 : {
10634 : /* Set user_conv_p on the argument conversions, so rvalue/base handling
10635 : knows not to allow any more UDCs. This needs to happen after we
10636 : process cand->warnings. */
10637 180906045 : if (flags & LOOKUP_NO_CONVERSION)
10638 4734745 : conv->user_conv_p = true;
10639 :
10640 180906045 : if (arg_complain & tf_warning)
10641 125521992 : maybe_warn_pessimizing_move (arg, type, /*return_p=*/false);
10642 :
10643 180906045 : tree val = convert_like_with_context (conv, arg, fn,
10644 : param_index, arg_complain);
10645 180906045 : val = convert_for_arg_passing (type, val, arg_complain);
10646 180906045 : return val;
10647 234714601 : };
10648 :
10649 417272848 : auto handle_indeterminate_arg = [](tree parmd, tree val)
10650 : {
10651 182558247 : if (parmd
10652 182558247 : && lookup_attribute (NULL, "indeterminate", DECL_ATTRIBUTES (parmd)))
10653 : {
10654 48 : STRIP_NOPS (val);
10655 48 : if (TREE_CODE (val) == ADDR_EXPR
10656 48 : && TREE_CODE (TREE_OPERAND (val, 0)) == TARGET_EXPR)
10657 : {
10658 48 : val = TARGET_EXPR_SLOT (TREE_OPERAND (val, 0));
10659 48 : if (auto_var_p (val) && DECL_ARTIFICIAL (val))
10660 : {
10661 48 : tree id = get_identifier ("indeterminate");
10662 48 : DECL_ATTRIBUTES (val)
10663 96 : = tree_cons (build_tree_list (NULL_TREE, id), NULL_TREE,
10664 48 : DECL_ATTRIBUTES (val));
10665 : }
10666 : }
10667 : }
10668 182558247 : };
10669 :
10670 234714601 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
10671 : {
10672 6150 : gcc_assert (cand->num_convs > 0);
10673 6150 : tree object_arg = consume_object_arg ();
10674 6150 : val = handle_arg (TREE_VALUE (parm),
10675 : object_arg,
10676 : param_index++,
10677 6150 : convs[conv_index++],
10678 : complain);
10679 :
10680 6150 : if (val == error_mark_node)
10681 : return error_mark_node;
10682 : else
10683 : {
10684 6038 : argarray[argarray_size++] = val;
10685 6038 : handle_indeterminate_arg (parmd, val);
10686 : }
10687 6038 : parm = TREE_CHAIN (parm);
10688 6038 : if (parmd)
10689 6038 : parmd = DECL_CHAIN (parmd);
10690 : }
10691 :
10692 234714489 : gcc_assert (first_arg == NULL_TREE);
10693 415613204 : for (; arg_index < vec_safe_length (args) && parm;
10694 180898715 : parm = TREE_CHAIN (parm), ++arg_index, ++param_index, ++conv_index)
10695 : {
10696 180899895 : tree current_arg = (*args)[arg_index];
10697 :
10698 : /* If the argument is NULL and used to (implicitly) instantiate a
10699 : template function (and bind one of the template arguments to
10700 : the type of 'long int'), we don't want to warn about passing NULL
10701 : to non-pointer argument.
10702 : For example, if we have this template function:
10703 :
10704 : template<typename T> void func(T x) {}
10705 :
10706 : we want to warn (when -Wconversion is enabled) in this case:
10707 :
10708 : void foo() {
10709 : func<int>(NULL);
10710 : }
10711 :
10712 : but not in this case:
10713 :
10714 : void foo() {
10715 : func(NULL);
10716 : }
10717 : */
10718 180899895 : bool const conversion_warning = !(null_node_p (current_arg)
10719 45686 : && DECL_TEMPLATE_INFO (fn)
10720 60 : && cand->template_decl
10721 30 : && !cand->explicit_targs);
10722 :
10723 15 : tsubst_flags_t const arg_complain
10724 : = conversion_warning ? complain : complain & ~tf_warning;
10725 :
10726 180899895 : val = handle_arg (TREE_VALUE (parm),
10727 : current_arg,
10728 : param_index,
10729 180899895 : convs[conv_index],
10730 : arg_complain);
10731 :
10732 180899895 : if (val == error_mark_node)
10733 : return error_mark_node;
10734 : else
10735 : {
10736 180898715 : argarray[argarray_size++] = val;
10737 180898715 : handle_indeterminate_arg (parmd, val);
10738 : }
10739 180898715 : if (parmd)
10740 169060777 : parmd = DECL_CHAIN (parmd);
10741 : }
10742 :
10743 : /* Default arguments */
10744 236366803 : for (; parm && parm != void_list_node;
10745 1653494 : parm = TREE_CHAIN (parm), param_index++)
10746 : {
10747 1653607 : if (TREE_VALUE (parm) == error_mark_node)
10748 : return error_mark_node;
10749 3307190 : val = convert_default_arg (TREE_VALUE (parm),
10750 1653595 : TREE_PURPOSE (parm),
10751 : fn, param_index,
10752 : complain);
10753 1653595 : if (val == error_mark_node)
10754 : return error_mark_node;
10755 1653494 : argarray[argarray_size++] = val;
10756 1653494 : handle_indeterminate_arg (parmd, val);
10757 1653494 : if (parmd)
10758 1653494 : parmd = DECL_CHAIN (parmd);
10759 : }
10760 :
10761 : /* Ellipsis */
10762 234713196 : int magic = magic_varargs_p (fn);
10763 237736304 : for (; arg_index < vec_safe_length (args); ++arg_index)
10764 : {
10765 3023129 : tree a = (*args)[arg_index];
10766 3023129 : if ((magic == 3 && arg_index == 2) || (magic == 4 && arg_index == 0))
10767 : {
10768 : /* Do no conversions for certain magic varargs. */
10769 112420 : a = mark_type_use (a);
10770 112420 : if (TREE_CODE (a) == FUNCTION_DECL && reject_gcc_builtin (a))
10771 0 : return error_mark_node;
10772 : }
10773 2910709 : else if (magic != 0)
10774 : {
10775 : /* Don't truncate excess precision to the semantic type. */
10776 1567565 : if (magic == 1 && TREE_CODE (a) == EXCESS_PRECISION_EXPR)
10777 84 : a = TREE_OPERAND (a, 0);
10778 : /* For other magic varargs only do decay_conversion. */
10779 1567565 : a = decay_conversion (a, complain);
10780 : }
10781 1343144 : else if (DECL_CONSTRUCTOR_P (fn)
10782 336 : && vec_safe_length (args) == 1
10783 1343304 : && same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (fn),
10784 160 : TREE_TYPE (a)))
10785 : {
10786 : /* Avoid infinite recursion trying to call A(...). */
10787 3 : if (complain & tf_error)
10788 : /* Try to call the actual copy constructor for a good error. */
10789 3 : call_copy_ctor (a, complain);
10790 3 : return error_mark_node;
10791 : }
10792 : else
10793 1343141 : a = convert_arg_to_ellipsis (a, complain);
10794 3023126 : if (a == error_mark_node)
10795 : return error_mark_node;
10796 3023108 : argarray[argarray_size++] = a;
10797 : }
10798 :
10799 234713175 : gcc_assert (argarray_size <= nargs);
10800 234713175 : nargs = argarray_size;
10801 234713175 : icip.reset ();
10802 :
10803 : /* Avoid performing argument transformation if warnings are disabled.
10804 : When tf_warning is set and at least one of the warnings is active
10805 : the check_function_arguments function might warn about something. */
10806 :
10807 234713175 : bool warned_p = false;
10808 234713175 : if ((complain & tf_warning)
10809 142069594 : && (warn_nonnull
10810 140141736 : || warn_format
10811 140141733 : || warn_suggest_attribute_format
10812 140141637 : || warn_restrict))
10813 : {
10814 1930204 : tree *fargs = (!nargs ? argarray
10815 1594444 : : (tree *) alloca (nargs * sizeof (tree)));
10816 5891386 : for (int j = 0; j < nargs; j++)
10817 : {
10818 : /* For -Wformat undo the implicit passing by hidden reference
10819 : done by convert_arg_to_ellipsis. */
10820 3961182 : if (TREE_CODE (argarray[j]) == ADDR_EXPR
10821 3961182 : && TYPE_REF_P (TREE_TYPE (argarray[j])))
10822 1602 : fargs[j] = TREE_OPERAND (argarray[j], 0);
10823 : else
10824 3959580 : fargs[j] = argarray[j];
10825 : }
10826 :
10827 1930204 : warned_p = check_function_arguments (input_location, fn, TREE_TYPE (fn),
10828 : nargs, fargs, NULL,
10829 : cp_comp_parm_types);
10830 : }
10831 :
10832 469426350 : if (DECL_INHERITED_CTOR (fn))
10833 : {
10834 : /* Check for passing ellipsis arguments to an inherited constructor. We
10835 : could handle this by open-coding the inherited constructor rather than
10836 : defining it, but let's not bother now. */
10837 76807 : if (!cp_unevaluated_operand
10838 76546 : && cand->num_convs
10839 76546 : && cand->convs[cand->num_convs-1]->ellipsis_p)
10840 : {
10841 15 : if (complain & tf_error)
10842 : {
10843 15 : sorry ("passing arguments to ellipsis of inherited constructor "
10844 : "%qD", cand->fn);
10845 15 : inform (DECL_SOURCE_LOCATION (cand->fn), "declared here");
10846 : }
10847 15 : return error_mark_node;
10848 : }
10849 :
10850 : /* A base constructor inheriting from a virtual base doesn't get the
10851 : inherited arguments, just this and __vtt. */
10852 76792 : if (ctor_omit_inherited_parms (fn))
10853 234713160 : nargs = 2;
10854 : }
10855 :
10856 : /* Avoid actually calling copy constructors and copy assignment operators,
10857 : if possible. */
10858 :
10859 234713160 : if (!force_elide
10860 234584559 : && (!flag_elide_constructors
10861 : /* It's unsafe to elide the operation when handling
10862 : a noexcept-expression, it may evaluate to the wrong
10863 : value (c++/53025, c++/96090). */
10864 234584355 : || cp_noexcept_operand != 0))
10865 : /* Do things the hard way. */;
10866 230997779 : else if (cand->num_convs == 1
10867 350231446 : && (DECL_COPY_CONSTRUCTOR_P (fn)
10868 221039132 : || DECL_MOVE_CONSTRUCTOR_P (fn)))
10869 : {
10870 11687828 : tree targ;
10871 11687828 : tree arg = argarray[num_artificial_parms_for (fn)];
10872 11687828 : tree fa = argarray[0];
10873 11687828 : bool trivial = trivial_fn_p (fn);
10874 :
10875 : /* Pull out the real argument, disregarding const-correctness. */
10876 11687828 : targ = arg;
10877 : /* Strip the reference binding for the constructor parameter. */
10878 0 : if (CONVERT_EXPR_P (targ)
10879 11687828 : && TYPE_REF_P (TREE_TYPE (targ)))
10880 11687828 : targ = TREE_OPERAND (targ, 0);
10881 : /* But don't strip any other reference bindings; binding a temporary to a
10882 : reference prevents copy elision. */
10883 18802638 : while ((CONVERT_EXPR_P (targ)
10884 16574616 : && !TYPE_REF_P (TREE_TYPE (targ)))
10885 40035620 : || TREE_CODE (targ) == NON_LVALUE_EXPR)
10886 14805004 : targ = TREE_OPERAND (targ, 0);
10887 11687828 : if (TREE_CODE (targ) == ADDR_EXPR)
10888 : {
10889 4238863 : targ = TREE_OPERAND (targ, 0);
10890 4238863 : if (!same_type_ignoring_top_level_qualifiers_p
10891 4238863 : (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
10892 : targ = NULL_TREE;
10893 : }
10894 : else
10895 : targ = NULL_TREE;
10896 :
10897 4238403 : if (targ)
10898 : arg = targ;
10899 : else
10900 7449425 : arg = cp_build_fold_indirect_ref (arg);
10901 :
10902 : /* In C++17 we shouldn't be copying a TARGET_EXPR except into a
10903 : potentially-overlapping subobject. */
10904 11687828 : if (CHECKING_P && cxx_dialect >= cxx17)
10905 11612343 : gcc_assert (TREE_CODE (arg) != TARGET_EXPR
10906 : || force_elide
10907 : /* It's from binding the ref parm to a packed field. */
10908 : || convs[0]->need_temporary_p
10909 : || seen_error ()
10910 : /* See unsafe_copy_elision_p. */
10911 : || unsafe_return_slot_p (fa));
10912 :
10913 11687828 : bool unsafe = unsafe_copy_elision_p_opt (fa, arg);
10914 11687828 : bool eliding_temp = (TREE_CODE (arg) == TARGET_EXPR && !unsafe);
10915 :
10916 : /* [class.copy]: the copy constructor is implicitly defined even if the
10917 : implementation elided its use. But don't warn about deprecation when
10918 : eliding a temporary, as then no copy is actually performed. */
10919 11687828 : warning_sentinel s (warn_deprecated_copy, eliding_temp);
10920 11687828 : if (force_elide)
10921 : /* The language says this isn't called. */;
10922 11559227 : else if (!trivial)
10923 : {
10924 1320792 : if (!mark_used (fn, complain) && !(complain & tf_error))
10925 0 : return error_mark_node;
10926 : already_used = true;
10927 : }
10928 : else
10929 10238435 : cp_handle_deprecated_or_unavailable (fn, complain);
10930 :
10931 186844 : if (eliding_temp && DECL_BASE_CONSTRUCTOR_P (fn)
10932 11688009 : && !make_base_init_ok (arg))
10933 : unsafe = true;
10934 :
10935 : /* If we're creating a temp and we already have one, don't create a
10936 : new one. If we're not creating a temp but we get one, use
10937 : INIT_EXPR to collapse the temp into our target. Otherwise, if the
10938 : ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
10939 : temp or an INIT_EXPR otherwise. */
10940 11687828 : if (is_dummy_object (fa))
10941 : {
10942 9139064 : if (TREE_CODE (arg) == TARGET_EXPR)
10943 : return arg;
10944 8981978 : else if (trivial)
10945 8392429 : return force_target_expr (DECL_CONTEXT (fn), arg, complain);
10946 : }
10947 2548764 : else if ((trivial || TREE_CODE (arg) == TARGET_EXPR)
10948 1823741 : && !unsafe)
10949 : {
10950 1823655 : tree to = cp_build_fold_indirect_ref (fa);
10951 1823655 : val = cp_build_init_expr (to, arg);
10952 1823655 : return val;
10953 : }
10954 11687828 : }
10955 219309951 : else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
10956 6078545 : && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR)
10957 224229275 : && trivial_fn_p (fn))
10958 : {
10959 3829442 : tree to = cp_build_fold_indirect_ref (argarray[0]);
10960 3829442 : tree type = TREE_TYPE (to);
10961 3829442 : tree as_base = CLASSTYPE_AS_BASE (type);
10962 3829442 : tree arg = argarray[1];
10963 3829442 : location_t loc = cp_expr_loc_or_input_loc (arg);
10964 :
10965 3829442 : if (is_really_empty_class (type, /*ignore_vptr*/true))
10966 : {
10967 : /* Avoid copying empty classes, but ensure op= returns an lvalue even
10968 : if the object argument isn't one. */
10969 233102 : to = force_lvalue (to, complain);
10970 233102 : val = build2 (COMPOUND_EXPR, type, arg, to);
10971 233102 : suppress_warning (val, OPT_Wunused);
10972 : }
10973 3596340 : else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
10974 : {
10975 3126286 : if (is_std_init_list (type)
10976 3126286 : && conv_binds_ref_to_prvalue (convs[1]))
10977 3 : warning_at (loc, OPT_Winit_list_lifetime,
10978 : "assignment from temporary %<initializer_list%> does "
10979 : "not extend the lifetime of the underlying array");
10980 3126286 : arg = cp_build_fold_indirect_ref (arg);
10981 3126286 : val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
10982 : }
10983 : else
10984 : {
10985 : /* We must only copy the non-tail padding parts. */
10986 470054 : tree arg0, arg2, t;
10987 470054 : tree array_type, alias_set;
10988 :
10989 470054 : arg2 = TYPE_SIZE_UNIT (as_base);
10990 : /* Ensure op= returns an lvalue even if the object argument isn't
10991 : one. */
10992 470054 : to = force_lvalue (to, complain);
10993 470054 : to = cp_stabilize_reference (to);
10994 470054 : arg0 = cp_build_addr_expr (to, complain);
10995 :
10996 470054 : array_type = build_array_type (unsigned_char_type_node,
10997 : build_index_type
10998 : (size_binop (MINUS_EXPR,
10999 : arg2, size_int (1))));
11000 470054 : alias_set = build_int_cst (build_pointer_type (type), 0);
11001 470054 : t = build2 (MODIFY_EXPR, void_type_node,
11002 : build2 (MEM_REF, array_type, arg0, alias_set),
11003 : build2 (MEM_REF, array_type, arg, alias_set));
11004 470054 : val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
11005 470054 : suppress_warning (val, OPT_Wunused);
11006 : }
11007 :
11008 3829442 : cp_handle_deprecated_or_unavailable (fn, complain);
11009 :
11010 3829442 : return val;
11011 : }
11012 215480509 : else if (trivial_fn_p (fn))
11013 : {
11014 7974924 : if (DECL_DESTRUCTOR_P (fn))
11015 3216498 : return build_trivial_dtor_call (argarray[0]);
11016 770964 : else if (default_ctor_p (fn))
11017 : {
11018 770964 : if (is_dummy_object (argarray[0]))
11019 253442 : return force_target_expr (DECL_CONTEXT (fn), void_node,
11020 253442 : no_cleanup_complain);
11021 : else
11022 517522 : return cp_build_fold_indirect_ref (argarray[0]);
11023 : }
11024 : }
11025 :
11026 216523086 : gcc_assert (!force_elide);
11027 :
11028 216523086 : if (!already_used
11029 216523086 : && !mark_used (fn, complain))
11030 773 : return error_mark_node;
11031 :
11032 : /* Warn if the built-in writes to an object of a non-trivial type. */
11033 216522310 : if (warn_class_memaccess
11034 1964283 : && vec_safe_length (args) >= 2
11035 217475891 : && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
11036 68382 : maybe_warn_class_memaccess (input_location, fn, args);
11037 :
11038 216522310 : if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
11039 : {
11040 707071 : tree t;
11041 707071 : tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
11042 707071 : DECL_CONTEXT (fn),
11043 : ba_any, NULL, complain);
11044 707071 : gcc_assert (binfo && binfo != error_mark_node);
11045 :
11046 707071 : argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
11047 : complain);
11048 707071 : if (TREE_SIDE_EFFECTS (argarray[0]))
11049 44912 : argarray[0] = save_expr (argarray[0]);
11050 707071 : t = build_pointer_type (TREE_TYPE (fn));
11051 707071 : fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
11052 707071 : TREE_TYPE (fn) = t;
11053 : }
11054 : else
11055 : {
11056 : /* If FN is marked deprecated or unavailable, then we've already
11057 : issued a diagnostic from mark_used above, so avoid redundantly
11058 : issuing another one from build_addr_func. */
11059 215815239 : auto w = make_temp_override (deprecated_state,
11060 215815239 : UNAVAILABLE_DEPRECATED_SUPPRESS);
11061 :
11062 215815239 : fn = build_addr_func (fn, complain);
11063 215815239 : if (fn == error_mark_node)
11064 0 : return error_mark_node;
11065 :
11066 : /* We're actually invoking the function. (Immediate functions get an
11067 : & when invoking it even though the user didn't use &.) */
11068 215815239 : ADDR_EXPR_DENOTES_CALL_P (fn) = true;
11069 215815239 : }
11070 :
11071 216522310 : tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag);
11072 216522310 : if (call == error_mark_node)
11073 : return call;
11074 216521459 : if (cand->flags & LOOKUP_LIST_INIT_CTOR)
11075 : {
11076 2159359 : tree c = extract_call_expr (call);
11077 : /* build_new_op will clear this when appropriate. */
11078 2159359 : CALL_EXPR_ORDERED_ARGS (c) = true;
11079 : }
11080 216521459 : if (warned_p)
11081 : {
11082 253 : tree c = extract_call_expr (call);
11083 253 : if (TREE_CODE (c) == CALL_EXPR)
11084 253 : suppress_warning (c /* Suppress all warnings. */);
11085 : }
11086 216521206 : else if (TREE_DEPRECATED (fn)
11087 216521206 : && warning_suppressed_at (input_location,
11088 : OPT_Wdeprecated_declarations))
11089 : {
11090 0 : tree c = extract_call_expr (call);
11091 0 : if (TREE_CODE (c) == CALL_EXPR)
11092 0 : TREE_NO_WARNING (c) = true;
11093 : }
11094 :
11095 : return call;
11096 : }
11097 :
11098 : namespace
11099 : {
11100 :
11101 : /* Return the DECL of the first non-static subobject of class TYPE
11102 : that satisfies the predicate PRED or null if none can be found. */
11103 :
11104 : template <class Predicate>
11105 : tree
11106 3548 : first_non_static_field (tree type, Predicate pred)
11107 : {
11108 3548 : if (!type || !CLASS_TYPE_P (type))
11109 : return NULL_TREE;
11110 :
11111 71038 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
11112 : {
11113 68084 : if (TREE_CODE (field) != FIELD_DECL)
11114 43234 : continue;
11115 24850 : if (TREE_STATIC (field))
11116 0 : continue;
11117 24850 : if (pred (field))
11118 : return field;
11119 : }
11120 :
11121 2954 : int i = 0;
11122 :
11123 3074 : for (tree base_binfo, binfo = TYPE_BINFO (type);
11124 3074 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11125 : {
11126 126 : tree base = TREE_TYPE (base_binfo);
11127 126 : if (pred (base))
11128 : return base;
11129 120 : if (tree field = first_non_static_field (base, pred))
11130 : return field;
11131 : }
11132 :
11133 : return NULL_TREE;
11134 : }
11135 :
11136 : struct NonPublicField
11137 : {
11138 24100 : bool operator() (const_tree t) const
11139 : {
11140 24100 : return DECL_P (t) && (TREE_PRIVATE (t) || TREE_PROTECTED (t));
11141 : }
11142 : };
11143 :
11144 : /* Return the DECL of the first non-public subobject of class TYPE
11145 : or null if none can be found. */
11146 :
11147 : static inline tree
11148 3254 : first_non_public_field (tree type)
11149 : {
11150 3254 : return first_non_static_field (type, NonPublicField ());
11151 : }
11152 :
11153 : struct NonTrivialField
11154 : {
11155 876 : bool operator() (const_tree t) const
11156 : {
11157 876 : return !trivial_type_p (DECL_P (t) ? TREE_TYPE (t) : t);
11158 : }
11159 : };
11160 :
11161 : /* Return the DECL of the first non-trivial subobject of class TYPE
11162 : or null if none can be found. */
11163 :
11164 : static inline tree
11165 174 : first_non_trivial_field (tree type)
11166 : {
11167 174 : return first_non_static_field (type, NonTrivialField ());
11168 : }
11169 :
11170 : } /* unnamed namespace */
11171 :
11172 : /* Return true if all copy and move assignment operator overloads for
11173 : class TYPE are trivial and at least one of them is not deleted and,
11174 : when ACCESS is set, accessible. Return false otherwise. Set
11175 : HASASSIGN to true when the TYPE has a (not necessarily trivial)
11176 : copy or move assignment. */
11177 :
11178 : static bool
11179 5013 : has_trivial_copy_assign_p (tree type, bool access, bool *hasassign)
11180 : {
11181 5013 : tree fns = get_class_binding (type, assign_op_identifier);
11182 5013 : bool all_trivial = true;
11183 :
11184 : /* Iterate over overloads of the assignment operator, checking
11185 : accessible copy assignments for triviality. */
11186 :
11187 16483 : for (tree f : ovl_range (fns))
11188 : {
11189 : /* Skip operators that aren't copy assignments. */
11190 8555 : if (!copy_fn_p (f))
11191 3038 : continue;
11192 :
11193 5517 : bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
11194 5805 : || accessible_p (TYPE_BINFO (type), f, true));
11195 :
11196 : /* Skip template assignment operators and deleted functions. */
11197 5517 : if (TREE_CODE (f) != FUNCTION_DECL || DECL_DELETED_FN (f))
11198 694 : continue;
11199 :
11200 4823 : if (accessible)
11201 4571 : *hasassign = true;
11202 :
11203 4571 : if (!accessible || !trivial_fn_p (f))
11204 : all_trivial = false;
11205 :
11206 : /* Break early when both properties have been determined. */
11207 4823 : if (*hasassign && !all_trivial)
11208 : break;
11209 : }
11210 :
11211 : /* Return true if they're all trivial and one of the expressions
11212 : TYPE() = TYPE() or TYPE() = (TYPE&)() is valid. */
11213 5013 : tree ref = cp_build_reference_type (type, false);
11214 5013 : return (all_trivial
11215 5013 : && (is_trivially_xible (MODIFY_EXPR, type, type)
11216 333 : || is_trivially_xible (MODIFY_EXPR, type, ref)));
11217 : }
11218 :
11219 : /* Return true if all copy and move ctor overloads for class TYPE are
11220 : trivial and at least one of them is not deleted and, when ACCESS is
11221 : set, accessible. Return false otherwise. Set each element of HASCTOR[]
11222 : to true when the TYPE has a (not necessarily trivial) default and copy
11223 : (or move) ctor, respectively. */
11224 :
11225 : static bool
11226 5013 : has_trivial_copy_p (tree type, bool access, bool hasctor[2])
11227 : {
11228 5013 : tree fns = get_class_binding (type, complete_ctor_identifier);
11229 5013 : bool all_trivial = true;
11230 :
11231 25093 : for (tree f : ovl_range (fns))
11232 : {
11233 : /* Skip template constructors. */
11234 12578 : if (TREE_CODE (f) != FUNCTION_DECL)
11235 105 : continue;
11236 :
11237 12473 : bool cpy_or_move_ctor_p = copy_fn_p (f);
11238 :
11239 : /* Skip ctors other than default, copy, and move. */
11240 12473 : if (!cpy_or_move_ctor_p && !default_ctor_p (f))
11241 2749 : continue;
11242 :
11243 9724 : if (DECL_DELETED_FN (f))
11244 306 : continue;
11245 :
11246 9418 : bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
11247 9610 : || accessible_p (TYPE_BINFO (type), f, true));
11248 :
11249 9298 : if (accessible)
11250 9298 : hasctor[cpy_or_move_ctor_p] = true;
11251 :
11252 9418 : if (cpy_or_move_ctor_p && (!accessible || !trivial_fn_p (f)))
11253 : all_trivial = false;
11254 :
11255 : /* Break early when both properties have been determined. */
11256 9418 : if (hasctor[0] && hasctor[1] && !all_trivial)
11257 : break;
11258 : }
11259 :
11260 5013 : return all_trivial;
11261 : }
11262 :
11263 : /* Issue a warning on a call to the built-in function FNDECL if it is
11264 : a raw memory write whose destination is not an object of (something
11265 : like) trivial or standard layout type with a non-deleted assignment
11266 : and copy ctor. Detects const correctness violations, corrupting
11267 : references, virtual table pointers, and bypassing non-trivial
11268 : assignments. */
11269 :
11270 : static void
11271 68382 : maybe_warn_class_memaccess (location_t loc, tree fndecl,
11272 : const vec<tree, va_gc> *args)
11273 : {
11274 : /* Except for bcopy where it's second, the destination pointer is
11275 : the first argument for all functions handled here. Compute
11276 : the index of the destination and source arguments. */
11277 68382 : unsigned dstidx = DECL_FUNCTION_CODE (fndecl) == BUILT_IN_BCOPY;
11278 68382 : unsigned srcidx = !dstidx;
11279 :
11280 68382 : tree dest = (*args)[dstidx];
11281 68382 : if (!TREE_TYPE (dest)
11282 68382 : || (TREE_CODE (TREE_TYPE (dest)) != ARRAY_TYPE
11283 67225 : && !INDIRECT_TYPE_P (TREE_TYPE (dest))))
11284 65007 : return;
11285 :
11286 21982 : tree srctype = NULL_TREE;
11287 :
11288 : /* Determine the type of the pointed-to object and whether it's
11289 : a complete class type. */
11290 21982 : tree desttype = TREE_TYPE (TREE_TYPE (dest));
11291 :
11292 21982 : if (!desttype || !COMPLETE_TYPE_P (desttype) || !CLASS_TYPE_P (desttype))
11293 : return;
11294 :
11295 : /* Check to see if the raw memory call is made by a non-static member
11296 : function with THIS as the destination argument for the destination
11297 : type. If so, and if the class has no non-trivial bases or members,
11298 : be more permissive. */
11299 5115 : if (current_function_decl
11300 5115 : && DECL_OBJECT_MEMBER_FUNCTION_P (current_function_decl)
11301 5382 : && is_object_parameter (tree_strip_nop_conversions (dest)))
11302 : {
11303 204 : tree ctx = DECL_CONTEXT (current_function_decl);
11304 204 : bool special = same_type_ignoring_top_level_qualifiers_p (ctx, desttype);
11305 204 : tree binfo = TYPE_BINFO (ctx);
11306 :
11307 204 : if (special
11308 204 : && !BINFO_VTABLE (binfo)
11309 378 : && !first_non_trivial_field (desttype))
11310 : return;
11311 : }
11312 :
11313 : /* True if the class is trivial. */
11314 5013 : bool trivial = trivial_type_p (desttype);
11315 :
11316 : /* Set to true if DESTYPE has an accessible copy assignment. */
11317 5013 : bool hasassign = false;
11318 : /* True if all of the class' overloaded copy assignment operators
11319 : are all trivial (and not deleted) and at least one of them is
11320 : accessible. */
11321 5013 : bool trivassign = has_trivial_copy_assign_p (desttype, true, &hasassign);
11322 :
11323 : /* Set to true if DESTTYPE has an accessible default and copy ctor,
11324 : respectively. */
11325 5013 : bool hasctors[2] = { false, false };
11326 :
11327 : /* True if all of the class' overloaded copy constructors are all
11328 : trivial (and not deleted) and at least one of them is accessible. */
11329 5013 : bool trivcopy = has_trivial_copy_p (desttype, true, hasctors);
11330 :
11331 : /* Set FLD to the first private/protected member of the class. */
11332 5013 : tree fld = trivial ? first_non_public_field (desttype) : NULL_TREE;
11333 :
11334 : /* The warning format string. */
11335 5013 : const char *warnfmt = NULL;
11336 : /* A suggested alternative to offer instead of the raw memory call.
11337 : Empty string when none can be come up with. */
11338 5013 : const char *suggest = "";
11339 5013 : bool warned = false;
11340 :
11341 5013 : switch (DECL_FUNCTION_CODE (fndecl))
11342 : {
11343 560 : case BUILT_IN_MEMSET:
11344 560 : if (!integer_zerop (maybe_constant_value ((*args)[1])))
11345 : {
11346 : /* Diagnose setting non-copy-assignable or non-trivial types,
11347 : or types with a private member, to (potentially) non-zero
11348 : bytes. Since the value of the bytes being written is unknown,
11349 : suggest using assignment instead (if one exists). Also warn
11350 : for writes into objects for which zero-initialization doesn't
11351 : mean all bits clear (pointer-to-member data, where null is all
11352 : bits set). Since the value being written is (most likely)
11353 : non-zero, simply suggest assignment (but not copy assignment). */
11354 196 : suggest = "; use assignment instead";
11355 196 : if (!trivassign)
11356 : warnfmt = G_("%qD writing to an object of type %#qT with "
11357 : "no trivial copy-assignment");
11358 125 : else if (!trivial)
11359 : warnfmt = G_("%qD writing to an object of non-trivial type %#qT%s");
11360 85 : else if (fld)
11361 : {
11362 24 : const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
11363 24 : warned = warning_at (loc, OPT_Wclass_memaccess,
11364 : "%qD writing to an object of type %#qT with "
11365 : "%qs member %qD",
11366 : fndecl, desttype, access, fld);
11367 : }
11368 61 : else if (!zero_init_p (desttype))
11369 : warnfmt = G_("%qD writing to an object of type %#qT containing "
11370 : "a pointer to data member%s");
11371 :
11372 : break;
11373 : }
11374 : /* Fall through. */
11375 :
11376 481 : case BUILT_IN_BZERO:
11377 : /* Similarly to the above, diagnose clearing non-trivial or non-
11378 : standard layout objects, or objects of types with no assignmenmt.
11379 : Since the value being written is known to be zero, suggest either
11380 : copy assignment, copy ctor, or default ctor as an alternative,
11381 : depending on what's available. */
11382 :
11383 481 : if (hasassign && hasctors[0])
11384 : suggest = G_("; use assignment or value-initialization instead");
11385 49 : else if (hasassign)
11386 : suggest = G_("; use assignment instead");
11387 31 : else if (hasctors[0])
11388 16 : suggest = G_("; use value-initialization instead");
11389 :
11390 481 : if (!trivassign)
11391 : warnfmt = G_("%qD clearing an object of type %#qT with "
11392 : "no trivial copy-assignment%s");
11393 374 : else if (!trivial)
11394 : warnfmt = G_("%qD clearing an object of non-trivial type %#qT%s");
11395 304 : else if (!zero_init_p (desttype))
11396 : warnfmt = G_("%qD clearing an object of type %#qT containing "
11397 : "a pointer-to-member%s");
11398 : break;
11399 :
11400 2437 : case BUILT_IN_BCOPY:
11401 2437 : case BUILT_IN_MEMCPY:
11402 2437 : case BUILT_IN_MEMMOVE:
11403 2437 : case BUILT_IN_MEMPCPY:
11404 : /* Determine the type of the source object. */
11405 2437 : srctype = TREE_TYPE ((*args)[srcidx]);
11406 2437 : if (!srctype || !INDIRECT_TYPE_P (srctype))
11407 0 : srctype = void_type_node;
11408 : else
11409 2437 : srctype = TREE_TYPE (srctype);
11410 :
11411 : /* Since it's impossible to determine wheter the byte copy is
11412 : being used in place of assignment to an existing object or
11413 : as a substitute for initialization, assume it's the former.
11414 : Determine the best alternative to use instead depending on
11415 : what's not deleted. */
11416 2437 : if (hasassign && hasctors[1])
11417 : suggest = G_("; use copy-assignment or copy-initialization instead");
11418 488 : else if (hasassign)
11419 : suggest = G_("; use copy-assignment instead");
11420 341 : else if (hasctors[1])
11421 290 : suggest = G_("; use copy-initialization instead");
11422 :
11423 2437 : if (!trivassign)
11424 : warnfmt = G_("%qD writing to an object of type %#qT with no trivial "
11425 : "copy-assignment%s");
11426 1639 : else if (!trivially_copyable_p (desttype))
11427 : warnfmt = G_("%qD writing to an object of non-trivially copyable "
11428 : "type %#qT%s");
11429 1315 : else if (!trivcopy)
11430 : warnfmt = G_("%qD writing to an object with a deleted copy constructor");
11431 :
11432 1315 : else if (!trivial
11433 211 : && !VOID_TYPE_P (srctype)
11434 160 : && !is_byte_access_type (srctype)
11435 1412 : && !same_type_ignoring_top_level_qualifiers_p (desttype,
11436 : srctype))
11437 : {
11438 : /* Warn when copying into a non-trivial object from an object
11439 : of a different type other than void or char. */
11440 54 : warned = warning_at (loc, OPT_Wclass_memaccess,
11441 : "%qD copying an object of non-trivial type "
11442 : "%#qT from an array of %#qT",
11443 : fndecl, desttype, srctype);
11444 : }
11445 1261 : else if (fld
11446 432 : && !VOID_TYPE_P (srctype)
11447 384 : && !is_byte_access_type (srctype)
11448 1549 : && !same_type_ignoring_top_level_qualifiers_p (desttype,
11449 : srctype))
11450 : {
11451 216 : const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
11452 216 : warned = warning_at (loc, OPT_Wclass_memaccess,
11453 : "%qD copying an object of type %#qT with "
11454 : "%qs member %qD from an array of %#qT; use "
11455 : "assignment or copy-initialization instead",
11456 : fndecl, desttype, access, fld, srctype);
11457 : }
11458 1045 : else if (!trivial && vec_safe_length (args) > 2)
11459 : {
11460 157 : tree sz = maybe_constant_value ((*args)[2]);
11461 157 : if (!tree_fits_uhwi_p (sz))
11462 : break;
11463 :
11464 : /* Finally, warn on partial copies. */
11465 99 : unsigned HOST_WIDE_INT typesize
11466 99 : = tree_to_uhwi (TYPE_SIZE_UNIT (desttype));
11467 99 : if (typesize == 0)
11468 : break;
11469 96 : if (unsigned HOST_WIDE_INT partial = tree_to_uhwi (sz) % typesize)
11470 12 : warned = warning_at (loc, OPT_Wclass_memaccess,
11471 : (typesize - partial > 1
11472 : ? G_("%qD writing to an object of "
11473 : "a non-trivial type %#qT leaves %wu "
11474 : "bytes unchanged")
11475 : : G_("%qD writing to an object of "
11476 : "a non-trivial type %#qT leaves %wu "
11477 : "byte unchanged")),
11478 : fndecl, desttype, typesize - partial);
11479 : }
11480 : break;
11481 :
11482 261 : case BUILT_IN_REALLOC:
11483 :
11484 261 : if (!trivially_copyable_p (desttype))
11485 : warnfmt = G_("%qD moving an object of non-trivially copyable type "
11486 : "%#qT; use %<new%> and %<delete%> instead");
11487 138 : else if (!trivcopy)
11488 : warnfmt = G_("%qD moving an object of type %#qT with deleted copy "
11489 : "constructor; use %<new%> and %<delete%> instead");
11490 135 : else if (!get_dtor (desttype, tf_none))
11491 : warnfmt = G_("%qD moving an object of type %#qT with deleted "
11492 : "destructor");
11493 126 : else if (!trivial)
11494 : {
11495 33 : tree sz = maybe_constant_value ((*args)[1]);
11496 33 : if (TREE_CODE (sz) == INTEGER_CST
11497 33 : && tree_int_cst_lt (sz, TYPE_SIZE_UNIT (desttype)))
11498 : /* Finally, warn on reallocation into insufficient space. */
11499 7 : warned = warning_at (loc, OPT_Wclass_memaccess,
11500 : "%qD moving an object of non-trivial type "
11501 : "%#qT and size %E into a region of size %E",
11502 7 : fndecl, desttype, TYPE_SIZE_UNIT (desttype),
11503 : sz);
11504 : }
11505 : break;
11506 :
11507 : default:
11508 : return;
11509 : }
11510 :
11511 310 : if (warnfmt)
11512 : {
11513 1569 : if (suggest)
11514 1569 : warned = warning_at (loc, OPT_Wclass_memaccess,
11515 : warnfmt, fndecl, desttype, suggest);
11516 : else
11517 : warned = warning_at (loc, OPT_Wclass_memaccess,
11518 : warnfmt, fndecl, desttype);
11519 : }
11520 :
11521 3375 : if (warned)
11522 1879 : inform (location_of (desttype), "%#qT declared here", desttype);
11523 : }
11524 :
11525 : /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
11526 : If FN is the result of resolving an overloaded target built-in,
11527 : ORIG_FNDECL is the original function decl, otherwise it is null.
11528 : This function performs no overload resolution, conversion, or other
11529 : high-level operations. */
11530 :
11531 : tree
11532 225771108 : build_cxx_call (tree fn, int nargs, tree *argarray,
11533 : tsubst_flags_t complain, tree orig_fndecl)
11534 : {
11535 225771108 : tree fndecl;
11536 :
11537 : /* Remember roughly where this call is. */
11538 225771108 : location_t loc = cp_expr_loc_or_input_loc (fn);
11539 225771108 : fn = build_call_a (fn, nargs, argarray);
11540 225771108 : SET_EXPR_LOCATION (fn, loc);
11541 :
11542 225771108 : fndecl = get_callee_fndecl (fn);
11543 225771108 : if (!orig_fndecl)
11544 225771108 : orig_fndecl = fndecl;
11545 :
11546 : /* Check that arguments to builtin functions match the expectations. */
11547 225771108 : if (fndecl
11548 218365018 : && !processing_template_decl
11549 444084326 : && fndecl_built_in_p (fndecl))
11550 : {
11551 : int i;
11552 :
11553 : /* We need to take care that values to BUILT_IN_NORMAL
11554 : are reduced. */
11555 23799710 : for (i = 0; i < nargs; i++)
11556 15373309 : argarray[i] = maybe_constant_value (argarray[i]);
11557 :
11558 8426401 : if (!check_builtin_function_arguments (EXPR_LOCATION (fn), vNULL, fndecl,
11559 : orig_fndecl, nargs, argarray,
11560 : complain & tf_error))
11561 804 : return error_mark_node;
11562 8425597 : else if (fndecl_built_in_p (fndecl, BUILT_IN_CLEAR_PADDING))
11563 : {
11564 12552 : tree arg0 = argarray[0];
11565 12552 : STRIP_NOPS (arg0);
11566 12552 : if (TREE_CODE (arg0) == ADDR_EXPR
11567 264 : && DECL_P (TREE_OPERAND (arg0, 0))
11568 12795 : && same_type_ignoring_top_level_qualifiers_p
11569 243 : (TREE_TYPE (TREE_TYPE (argarray[0])),
11570 243 : TREE_TYPE (TREE_TYPE (arg0))))
11571 : /* For __builtin_clear_padding (&var) we know the type
11572 : is for a complete object, so there is no risk in clearing
11573 : padding that is reused in some derived class member. */;
11574 12316 : else if (!trivially_copyable_p (TREE_TYPE (TREE_TYPE (argarray[0]))))
11575 : {
11576 18 : error_at (EXPR_LOC_OR_LOC (argarray[0], input_location),
11577 : "argument %u in call to function %qE "
11578 : "has pointer to a non-trivially-copyable type (%qT)",
11579 18 : 1, fndecl, TREE_TYPE (argarray[0]));
11580 18 : return error_mark_node;
11581 : }
11582 : }
11583 : }
11584 :
11585 225770286 : if (VOID_TYPE_P (TREE_TYPE (fn)))
11586 44352834 : return maybe_contract_wrap_call (fndecl, fn);
11587 :
11588 : /* 5.2.2/11: If a function call is a prvalue of object type: if the
11589 : function call is either the operand of a decltype-specifier or the
11590 : right operand of a comma operator that is the operand of a
11591 : decltype-specifier, a temporary object is not introduced for the
11592 : prvalue. The type of the prvalue may be incomplete. */
11593 181417452 : if (!(complain & tf_decltype))
11594 : {
11595 144710212 : fn = require_complete_type (fn, complain);
11596 144710212 : if (fn == error_mark_node)
11597 : return error_mark_node;
11598 144710186 : fn = maybe_contract_wrap_call (fndecl, fn);
11599 :
11600 144710186 : if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
11601 : {
11602 18254147 : fn = build_cplus_new (TREE_TYPE (fn), fn, complain);
11603 18254147 : maybe_warn_parm_abi (TREE_TYPE (fn), loc);
11604 : }
11605 : }
11606 : else
11607 36707240 : fn = maybe_contract_wrap_call (fndecl, fn);
11608 181417426 : return convert_from_reference (fn);
11609 : }
11610 :
11611 : /* Returns the value to use for the in-charge parameter when making a
11612 : call to a function with the indicated NAME.
11613 :
11614 : FIXME:Can't we find a neater way to do this mapping? */
11615 :
11616 : tree
11617 32720 : in_charge_arg_for_name (tree name)
11618 : {
11619 32720 : if (IDENTIFIER_CTOR_P (name))
11620 : {
11621 18774 : if (name == complete_ctor_identifier)
11622 9387 : return integer_one_node;
11623 9387 : gcc_checking_assert (name == base_ctor_identifier);
11624 : }
11625 : else
11626 : {
11627 13946 : if (name == complete_dtor_identifier)
11628 6973 : return integer_two_node;
11629 6973 : else if (name == deleting_dtor_identifier)
11630 : /* The deleting dtor should now be handled by
11631 : build_delete_destructor_body. */
11632 0 : gcc_unreachable ();
11633 6973 : gcc_checking_assert (name == base_dtor_identifier);
11634 : }
11635 :
11636 16360 : return integer_zero_node;
11637 : }
11638 :
11639 : /* We've built up a constructor call RET. Complain if it delegates to the
11640 : constructor we're currently compiling. */
11641 :
11642 : static void
11643 344961 : check_self_delegation (tree ret)
11644 : {
11645 344961 : if (TREE_CODE (ret) == TARGET_EXPR)
11646 0 : ret = TARGET_EXPR_INITIAL (ret);
11647 344961 : tree fn = cp_get_callee_fndecl_nofold (ret);
11648 344961 : if (fn && DECL_ABSTRACT_ORIGIN (fn) == current_function_decl)
11649 46 : error ("constructor delegates to itself");
11650 344961 : }
11651 :
11652 : /* Build a call to a constructor, destructor, or an assignment
11653 : operator for INSTANCE, an expression with class type. NAME
11654 : indicates the special member function to call; *ARGS are the
11655 : arguments. ARGS may be NULL. This may change ARGS. BINFO
11656 : indicates the base of INSTANCE that is to be passed as the `this'
11657 : parameter to the member function called.
11658 :
11659 : FLAGS are the LOOKUP_* flags to use when processing the call.
11660 :
11661 : If NAME indicates a complete object constructor, INSTANCE may be
11662 : NULL_TREE. In this case, the caller will call build_cplus_new to
11663 : store the newly constructed object into a VAR_DECL. */
11664 :
11665 : tree
11666 49844092 : build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
11667 : tree binfo, int flags, tsubst_flags_t complain)
11668 : {
11669 49844092 : tree fns;
11670 : /* The type of the subobject to be constructed or destroyed. */
11671 49844092 : tree class_type;
11672 49844092 : vec<tree, va_gc> *allocated = NULL;
11673 49844092 : tree ret;
11674 :
11675 49844092 : gcc_assert (IDENTIFIER_CDTOR_P (name) || name == assign_op_identifier);
11676 :
11677 49844092 : if (error_operand_p (instance))
11678 0 : return error_mark_node;
11679 :
11680 49844092 : if (IDENTIFIER_DTOR_P (name))
11681 : {
11682 11403243 : gcc_assert (args == NULL || vec_safe_is_empty (*args));
11683 11403243 : if (!type_build_dtor_call (TREE_TYPE (instance)))
11684 : /* Shortcut to avoid lazy destructor declaration. */
11685 9661 : return build_trivial_dtor_call (instance);
11686 : }
11687 :
11688 49834431 : if (TYPE_P (binfo))
11689 : {
11690 : /* Resolve the name. */
11691 40496207 : if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
11692 24 : return error_mark_node;
11693 :
11694 40496183 : binfo = TYPE_BINFO (binfo);
11695 : }
11696 :
11697 40496183 : gcc_assert (binfo != NULL_TREE);
11698 :
11699 49834407 : class_type = BINFO_TYPE (binfo);
11700 :
11701 : /* Handle the special case where INSTANCE is NULL_TREE. */
11702 49834407 : if (name == complete_ctor_identifier && !instance)
11703 28771921 : instance = build_dummy_object (class_type);
11704 : else
11705 : {
11706 : /* Convert to the base class, if necessary. */
11707 21062486 : if (!same_type_ignoring_top_level_qualifiers_p
11708 21062486 : (TREE_TYPE (instance), BINFO_TYPE (binfo)))
11709 : {
11710 1571586 : if (IDENTIFIER_CDTOR_P (name))
11711 : /* For constructors and destructors, either the base is
11712 : non-virtual, or it is virtual but we are doing the
11713 : conversion from a constructor or destructor for the
11714 : complete object. In either case, we can convert
11715 : statically. */
11716 1552547 : instance = convert_to_base_statically (instance, binfo);
11717 : else
11718 : {
11719 : /* However, for assignment operators, we must convert
11720 : dynamically if the base is virtual. */
11721 19039 : gcc_checking_assert (name == assign_op_identifier);
11722 19039 : instance = build_base_path (PLUS_EXPR, instance,
11723 : binfo, /*nonnull=*/1, complain);
11724 : }
11725 : }
11726 : }
11727 :
11728 49834407 : gcc_assert (instance != NULL_TREE);
11729 :
11730 : /* In C++17, "If the initializer expression is a prvalue and the
11731 : cv-unqualified version of the source type is the same class as the class
11732 : of the destination, the initializer expression is used to initialize the
11733 : destination object." Handle that here to avoid doing overload
11734 : resolution. */
11735 49834407 : if (cxx_dialect >= cxx17
11736 49643414 : && args && vec_safe_length (*args) == 1
11737 81362955 : && !unsafe_return_slot_p (instance))
11738 : {
11739 30177708 : tree arg = (**args)[0];
11740 :
11741 1727181 : if (BRACE_ENCLOSED_INITIALIZER_P (arg)
11742 1727031 : && !TYPE_HAS_LIST_CTOR (class_type)
11743 1662670 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (arg)
11744 31840364 : && CONSTRUCTOR_NELTS (arg) == 1)
11745 1126796 : arg = CONSTRUCTOR_ELT (arg, 0)->value;
11746 :
11747 30177708 : if ((TREE_CODE (arg) == TARGET_EXPR
11748 14691124 : || TREE_CODE (arg) == CONSTRUCTOR)
11749 46264751 : && (same_type_ignoring_top_level_qualifiers_p
11750 16087043 : (class_type, TREE_TYPE (arg))))
11751 : {
11752 14886487 : if (is_dummy_object (instance))
11753 : return arg;
11754 210652 : else if (TREE_CODE (arg) == TARGET_EXPR)
11755 210652 : TARGET_EXPR_DIRECT_INIT_P (arg) = true;
11756 :
11757 210652 : if ((complain & tf_error)
11758 210650 : && (flags & LOOKUP_DELEGATING_CONS))
11759 0 : check_self_delegation (arg);
11760 : /* Avoid change of behavior on Wunused-var-2.C. */
11761 210652 : instance = mark_lvalue_use (instance);
11762 210652 : return cp_build_init_expr (instance, arg);
11763 : }
11764 : }
11765 :
11766 34947920 : fns = lookup_fnfields (binfo, name, 1, complain);
11767 :
11768 : /* When making a call to a constructor or destructor for a subobject
11769 : that uses virtual base classes, pass down a pointer to a VTT for
11770 : the subobject. */
11771 34947920 : if ((name == base_ctor_identifier
11772 32506990 : || name == base_dtor_identifier)
11773 36500533 : && CLASSTYPE_VBASECLASSES (class_type))
11774 : {
11775 46531 : tree vtt;
11776 46531 : tree sub_vtt;
11777 :
11778 : /* If the current function is a complete object constructor
11779 : or destructor, then we fetch the VTT directly.
11780 : Otherwise, we look it up using the VTT we were given. */
11781 46531 : vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
11782 46531 : vtt = decay_conversion (vtt, complain);
11783 46531 : if (vtt == error_mark_node)
11784 0 : return error_mark_node;
11785 46531 : vtt = build_if_in_charge (vtt, current_vtt_parm);
11786 46531 : if (BINFO_SUBVTT_INDEX (binfo))
11787 46379 : sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
11788 : else
11789 152 : sub_vtt = vtt;
11790 :
11791 46531 : if (args == NULL)
11792 : {
11793 29627 : allocated = make_tree_vector ();
11794 29627 : args = &allocated;
11795 : }
11796 :
11797 46531 : vec_safe_insert (*args, 0, sub_vtt);
11798 : }
11799 :
11800 69895840 : ret = build_new_method_call (instance, fns, args,
11801 34947920 : TYPE_BINFO (BINFO_TYPE (binfo)),
11802 : flags, /*fn=*/NULL,
11803 : complain);
11804 :
11805 34947920 : if (allocated != NULL)
11806 29627 : release_tree_vector (allocated);
11807 :
11808 34947920 : if ((complain & tf_error)
11809 30450298 : && (flags & LOOKUP_DELEGATING_CONS)
11810 345047 : && name == complete_ctor_identifier)
11811 344961 : check_self_delegation (ret);
11812 :
11813 : return ret;
11814 : }
11815 :
11816 : /* Return the NAME, as a C string. The NAME indicates a function that
11817 : is a member of TYPE. *FREE_P is set to true if the caller must
11818 : free the memory returned.
11819 :
11820 : Rather than go through all of this, we should simply set the names
11821 : of constructors and destructors appropriately, and dispense with
11822 : ctor_identifier, dtor_identifier, etc. */
11823 :
11824 : static char *
11825 91 : name_as_c_string (tree name, tree type, bool *free_p)
11826 : {
11827 91 : const char *pretty_name;
11828 :
11829 : /* Assume that we will not allocate memory. */
11830 91 : *free_p = false;
11831 : /* Constructors and destructors are special. */
11832 91 : if (IDENTIFIER_CDTOR_P (name))
11833 : {
11834 42 : pretty_name
11835 42 : = identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type)));
11836 : /* For a destructor, add the '~'. */
11837 42 : if (IDENTIFIER_DTOR_P (name))
11838 : {
11839 0 : pretty_name = concat ("~", pretty_name, NULL);
11840 : /* Remember that we need to free the memory allocated. */
11841 0 : *free_p = true;
11842 : }
11843 : }
11844 49 : else if (IDENTIFIER_CONV_OP_P (name))
11845 : {
11846 0 : pretty_name = concat ("operator ",
11847 0 : type_as_string_translate (TREE_TYPE (name),
11848 : TFF_PLAIN_IDENTIFIER),
11849 : NULL);
11850 : /* Remember that we need to free the memory allocated. */
11851 0 : *free_p = true;
11852 : }
11853 : else
11854 49 : pretty_name = identifier_to_locale (IDENTIFIER_POINTER (name));
11855 :
11856 91 : return const_cast<char *> (pretty_name);
11857 : }
11858 :
11859 : /* If CANDIDATES contains exactly one candidate, return it, otherwise
11860 : return NULL. */
11861 :
11862 : static z_candidate *
11863 677 : single_z_candidate (z_candidate *candidates)
11864 : {
11865 0 : if (candidates == NULL)
11866 : return NULL;
11867 :
11868 665 : if (candidates->next)
11869 0 : return NULL;
11870 :
11871 : return candidates;
11872 : }
11873 :
11874 : /* If CANDIDATE is invalid due to a bad argument type, return the
11875 : pertinent conversion_info.
11876 :
11877 : Otherwise, return NULL. */
11878 :
11879 : static const conversion_info *
11880 336 : maybe_get_bad_conversion_for_unmatched_call (const z_candidate *candidate)
11881 : {
11882 : /* Must be an rr_arg_conversion or rr_bad_arg_conversion. */
11883 336 : rejection_reason *r = candidate->reason;
11884 :
11885 336 : if (r == NULL)
11886 : return NULL;
11887 :
11888 336 : switch (r->code)
11889 : {
11890 : default:
11891 : return NULL;
11892 :
11893 50 : case rr_arg_conversion:
11894 50 : return &r->u.conversion;
11895 :
11896 6 : case rr_bad_arg_conversion:
11897 6 : return &r->u.bad_conversion;
11898 : }
11899 : }
11900 :
11901 : /* Issue an error and note complaining about a bad argument type at a
11902 : callsite with a single candidate FNDECL.
11903 :
11904 : ARG_LOC is the location of the argument (or UNKNOWN_LOCATION, in which
11905 : case input_location is used).
11906 : FROM_TYPE is the type of the actual argument; TO_TYPE is the type of
11907 : the formal parameter. */
11908 :
11909 : void
11910 148 : complain_about_bad_argument (location_t arg_loc,
11911 : tree from_type, tree to_type,
11912 : tree fndecl, int parmnum)
11913 : {
11914 148 : auto_diagnostic_group d;
11915 148 : range_label_for_type_mismatch rhs_label (from_type, to_type);
11916 148 : range_label *label = &rhs_label;
11917 148 : if (arg_loc == UNKNOWN_LOCATION)
11918 : {
11919 6 : arg_loc = input_location;
11920 6 : label = NULL;
11921 : }
11922 148 : gcc_rich_location richloc (arg_loc, label, highlight_colors::percent_h);
11923 148 : error_at (&richloc,
11924 : "cannot convert %qH to %qI",
11925 : from_type, to_type);
11926 148 : maybe_inform_about_fndecl_for_bogus_argument_init
11927 148 : (fndecl,
11928 : parmnum,
11929 : highlight_colors::percent_i);
11930 148 : }
11931 :
11932 : /* Subroutine of build_new_method_call_1, for where there are no viable
11933 : candidates for the call. */
11934 :
11935 : static void
11936 683 : complain_about_no_candidates_for_method_call (tree instance,
11937 : z_candidate *candidates,
11938 : tree explicit_targs,
11939 : tree basetype,
11940 : tree optype, tree name,
11941 : bool skip_first_for_error,
11942 : vec<tree, va_gc> *user_args)
11943 : {
11944 683 : auto_diagnostic_group d;
11945 683 : if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
11946 0 : cxx_incomplete_type_error (instance, basetype);
11947 683 : else if (optype)
11948 6 : error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
11949 : basetype, optype, build_tree_list_vec (user_args),
11950 6 : TREE_TYPE (instance));
11951 : else
11952 : {
11953 : /* Special-case for when there's a single candidate that's failing
11954 : due to a bad argument type. */
11955 677 : if (z_candidate *candidate = single_z_candidate (candidates))
11956 336 : if (const conversion_info *conv
11957 336 : = maybe_get_bad_conversion_for_unmatched_call (candidate))
11958 : {
11959 56 : tree from_type = conv->from;
11960 56 : if (!TYPE_P (conv->from))
11961 6 : from_type = lvalue_type (conv->from);
11962 56 : complain_about_bad_argument (conv->loc,
11963 56 : from_type, conv->to_type,
11964 56 : candidate->fn, conv->n_arg);
11965 56 : return;
11966 : }
11967 :
11968 621 : tree arglist = build_tree_list_vec (user_args);
11969 621 : tree errname = name;
11970 621 : bool twiddle = false;
11971 621 : if (IDENTIFIER_CDTOR_P (errname))
11972 : {
11973 319 : twiddle = IDENTIFIER_DTOR_P (errname);
11974 319 : errname = constructor_name (basetype);
11975 : }
11976 621 : if (explicit_targs)
11977 47 : errname = lookup_template_function (errname, explicit_targs);
11978 621 : if (skip_first_for_error)
11979 3 : arglist = TREE_CHAIN (arglist);
11980 1242 : error ("no matching function for call to %<%T::%s%E(%A)%#V%>",
11981 621 : basetype, &"~"[!twiddle], errname, arglist,
11982 621 : TREE_TYPE (instance));
11983 : }
11984 627 : print_z_candidates (location_of (name), candidates);
11985 683 : }
11986 :
11987 : /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
11988 : be set, upon return, to the function called. ARGS may be NULL.
11989 : This may change ARGS. */
11990 :
11991 : tree
11992 125353517 : build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args,
11993 : tree conversion_path, int flags,
11994 : tree *fn_p, tsubst_flags_t complain)
11995 : {
11996 125353517 : struct z_candidate *candidates = 0, *cand;
11997 125353517 : tree explicit_targs = NULL_TREE;
11998 125353517 : tree basetype = NULL_TREE;
11999 125353517 : tree access_binfo;
12000 125353517 : tree optype;
12001 125353517 : tree first_mem_arg = NULL_TREE;
12002 125353517 : tree name;
12003 125353517 : bool skip_first_for_error;
12004 125353517 : vec<tree, va_gc> *user_args;
12005 125353517 : tree call;
12006 125353517 : tree fn;
12007 125353517 : int template_only = 0;
12008 125353517 : bool any_viable_p;
12009 125353517 : tree orig_instance;
12010 125353517 : tree orig_fns;
12011 125353517 : vec<tree, va_gc> *orig_args = NULL;
12012 :
12013 125353517 : auto_cond_timevar tv (TV_OVERLOAD);
12014 :
12015 125353517 : gcc_assert (instance != NULL_TREE);
12016 :
12017 : /* We don't know what function we're going to call, yet. */
12018 125353517 : if (fn_p)
12019 33613056 : *fn_p = NULL_TREE;
12020 :
12021 125353517 : if (error_operand_p (instance)
12022 125353517 : || !fns || error_operand_p (fns))
12023 1409298 : return error_mark_node;
12024 :
12025 123944219 : if (!BASELINK_P (fns))
12026 : {
12027 0 : if (complain & tf_error)
12028 0 : error ("call to non-function %qD", fns);
12029 0 : return error_mark_node;
12030 : }
12031 :
12032 123944219 : orig_instance = instance;
12033 123944219 : orig_fns = fns;
12034 :
12035 : /* Dismantle the baselink to collect all the information we need. */
12036 123944219 : if (!conversion_path)
12037 56806358 : conversion_path = BASELINK_BINFO (fns);
12038 123944219 : access_binfo = BASELINK_ACCESS_BINFO (fns);
12039 123944219 : optype = BASELINK_OPTYPE (fns);
12040 123944219 : fns = BASELINK_FUNCTIONS (fns);
12041 123944219 : if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12042 : {
12043 2967778 : explicit_targs = TREE_OPERAND (fns, 1);
12044 2967778 : fns = TREE_OPERAND (fns, 0);
12045 2967778 : template_only = 1;
12046 : }
12047 123944219 : gcc_assert (OVL_P (fns));
12048 123944219 : fn = OVL_FIRST (fns);
12049 123944219 : name = DECL_NAME (fn);
12050 :
12051 123944219 : basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
12052 123944219 : gcc_assert (CLASS_TYPE_P (basetype));
12053 :
12054 123944219 : user_args = args == NULL ? NULL : *args;
12055 : /* Under DR 147 A::A() is an invalid constructor call,
12056 : not a functional cast. */
12057 123944219 : if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
12058 : {
12059 54 : if (! (complain & tf_error))
12060 0 : return error_mark_node;
12061 :
12062 54 : basetype = DECL_CONTEXT (fn);
12063 54 : name = constructor_name (basetype);
12064 54 : auto_diagnostic_group d;
12065 54 : if (permerror (input_location,
12066 : "cannot call constructor %<%T::%D%> directly",
12067 : basetype, name))
12068 54 : inform (input_location, "for a function-style cast, remove the "
12069 : "redundant %<::%D%>", name);
12070 54 : call = build_functional_cast (input_location, basetype,
12071 : build_tree_list_vec (user_args),
12072 : complain);
12073 54 : return call;
12074 54 : }
12075 :
12076 123944165 : if (processing_template_decl)
12077 7444598 : orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
12078 :
12079 : /* Process the argument list. */
12080 123808640 : if (args != NULL && *args != NULL)
12081 : {
12082 107443598 : *args = resolve_args (*args, complain);
12083 107443598 : if (*args == NULL)
12084 153 : return error_mark_node;
12085 : user_args = *args;
12086 : }
12087 :
12088 : /* Consider the object argument to be used even if we end up selecting a
12089 : static member function. */
12090 123944012 : instance = mark_type_use (instance);
12091 :
12092 : /* Figure out whether to skip the first argument for the error
12093 : message we will display to users if an error occurs. We don't
12094 : want to display any compiler-generated arguments. The "this"
12095 : pointer hasn't been added yet. However, we must remove the VTT
12096 : pointer if this is a call to a base-class constructor or
12097 : destructor. */
12098 123944012 : skip_first_for_error = false;
12099 123944012 : if (IDENTIFIER_CDTOR_P (name))
12100 : {
12101 : /* Callers should explicitly indicate whether they want to ctor
12102 : the complete object or just the part without virtual bases. */
12103 64088848 : gcc_assert (name != ctor_identifier);
12104 :
12105 : /* Remove the VTT pointer, if present. */
12106 61647924 : if ((name == base_ctor_identifier || name == base_dtor_identifier)
12107 65641461 : && CLASSTYPE_VBASECLASSES (basetype))
12108 : skip_first_for_error = true;
12109 :
12110 : /* It's OK to call destructors and constructors on cv-qualified
12111 : objects. Therefore, convert the INSTANCE to the unqualified
12112 : type, if necessary. */
12113 64088848 : if (!same_type_p (basetype, TREE_TYPE (instance)))
12114 : {
12115 664109 : instance = build_this (instance);
12116 664109 : instance = build_nop (build_pointer_type (basetype), instance);
12117 664109 : instance = build_fold_indirect_ref (instance);
12118 : }
12119 : }
12120 : else
12121 119710328 : gcc_assert (!DECL_DESTRUCTOR_P (fn) && !DECL_CONSTRUCTOR_P (fn));
12122 :
12123 : /* For the overload resolution we need to find the actual `this`
12124 : that would be captured if the call turns out to be to a
12125 : non-static member function. Do not actually capture it at this
12126 : point. */
12127 247888024 : if (DECL_CONSTRUCTOR_P (fn))
12128 : /* Constructors don't use the enclosing 'this'. */
12129 : first_mem_arg = instance;
12130 : else
12131 89750097 : first_mem_arg = maybe_resolve_dummy (instance, false);
12132 :
12133 123944012 : conversion_obstack_sentinel cos;
12134 :
12135 : /* The number of arguments artificial parms in ARGS; we subtract one because
12136 : there's no 'this' in ARGS. */
12137 123944012 : unsigned skip = num_artificial_parms_for (fn) - 1;
12138 :
12139 : /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
12140 : initializer, not T({ }). */
12141 123944012 : if (DECL_CONSTRUCTOR_P (fn)
12142 29077281 : && vec_safe_length (user_args) > skip
12143 150108322 : && DIRECT_LIST_INIT_P ((*user_args)[skip]))
12144 : {
12145 1721059 : tree init_list = (*user_args)[skip];
12146 1721059 : tree init = NULL_TREE;
12147 :
12148 1721059 : gcc_assert (user_args->length () == skip + 1
12149 : && !(flags & LOOKUP_ONLYCONVERTING));
12150 :
12151 : /* If the initializer list has no elements and T is a class type with
12152 : a default constructor, the object is value-initialized. Handle
12153 : this here so we don't need to handle it wherever we use
12154 : build_special_member_call. */
12155 1721059 : if (CONSTRUCTOR_NELTS (init_list) == 0
12156 207381 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
12157 : /* For a user-provided default constructor, use the normal
12158 : mechanisms so that protected access works. */
12159 207381 : && type_has_non_user_provided_default_constructor (basetype)
12160 1622106 : && !processing_template_decl)
12161 108425 : init = build_value_init (basetype, complain);
12162 :
12163 : /* If BASETYPE is an aggregate, we need to do aggregate
12164 : initialization. */
12165 1612634 : else if (CP_AGGREGATE_TYPE_P (basetype))
12166 : {
12167 41 : init = reshape_init (basetype, init_list, complain);
12168 41 : init = digest_init (basetype, init, complain);
12169 : }
12170 :
12171 108466 : if (init)
12172 : {
12173 108466 : if (is_dummy_object (instance))
12174 30195 : return get_target_expr (init, complain);
12175 78271 : return cp_build_init_expr (instance, init);
12176 : }
12177 :
12178 : /* Otherwise go ahead with overload resolution. */
12179 1612593 : add_list_candidates (fns, first_mem_arg, user_args,
12180 : basetype, explicit_targs, template_only,
12181 : conversion_path, access_binfo, flags,
12182 : &candidates, complain);
12183 : }
12184 : else
12185 122222953 : add_candidates (fns, first_mem_arg, user_args, optype,
12186 : explicit_targs, template_only, conversion_path,
12187 : access_binfo, flags, &candidates, complain);
12188 :
12189 123835546 : any_viable_p = false;
12190 123835546 : candidates = splice_viable (candidates, false, &any_viable_p);
12191 :
12192 123835546 : if (!any_viable_p)
12193 : {
12194 : /* [dcl.init], 17.6.2.2:
12195 :
12196 : Otherwise, if no constructor is viable, the destination type is
12197 : a (possibly cv-qualified) aggregate class A, and the initializer
12198 : is a parenthesized expression-list, the object is initialized as
12199 : follows...
12200 :
12201 : We achieve this by building up a CONSTRUCTOR, as for list-init,
12202 : and setting CONSTRUCTOR_IS_PAREN_INIT to distinguish between
12203 : the two. */
12204 40038 : if (DECL_CONSTRUCTOR_P (fn)
12205 34044 : && !(flags & LOOKUP_ONLYCONVERTING)
12206 34020 : && cxx_dialect >= cxx20
12207 33410 : && CP_AGGREGATE_TYPE_P (basetype)
12208 40038 : && !vec_safe_is_empty (user_args))
12209 : {
12210 : /* Create a CONSTRUCTOR from ARGS, e.g. {1, 2} from <1, 2>. */
12211 920 : tree ctor = build_constructor_from_vec (init_list_type_node,
12212 : user_args);
12213 920 : CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true;
12214 920 : CONSTRUCTOR_IS_PAREN_INIT (ctor) = true;
12215 920 : if (is_dummy_object (instance))
12216 : return ctor;
12217 : else
12218 : {
12219 610 : ctor = digest_init (basetype, ctor, complain);
12220 610 : if (ctor == error_mark_node)
12221 : return error_mark_node;
12222 417 : return cp_build_init_expr (instance, ctor);
12223 : }
12224 : }
12225 39118 : if (complain & tf_error)
12226 683 : complain_about_no_candidates_for_method_call (instance, candidates,
12227 : explicit_targs, basetype,
12228 : optype, name,
12229 : skip_first_for_error,
12230 : user_args);
12231 39118 : call = error_mark_node;
12232 : }
12233 : else
12234 : {
12235 123795508 : cand = tourney (candidates, complain);
12236 123795508 : if (cand == 0)
12237 : {
12238 187 : char *pretty_name;
12239 187 : bool free_p;
12240 187 : tree arglist;
12241 :
12242 187 : if (complain & tf_error)
12243 : {
12244 91 : pretty_name = name_as_c_string (name, basetype, &free_p);
12245 91 : arglist = build_tree_list_vec (user_args);
12246 91 : if (skip_first_for_error)
12247 0 : arglist = TREE_CHAIN (arglist);
12248 91 : auto_diagnostic_group d;
12249 182 : if (!any_strictly_viable (candidates))
12250 13 : error ("no matching function for call to %<%s(%A)%>",
12251 : pretty_name, arglist);
12252 : else
12253 78 : error ("call of overloaded %<%s(%A)%> is ambiguous",
12254 : pretty_name, arglist);
12255 91 : print_z_candidates (location_of (name), candidates);
12256 91 : if (free_p)
12257 0 : free (pretty_name);
12258 91 : }
12259 187 : call = error_mark_node;
12260 187 : if (fn_p)
12261 84 : *fn_p = error_mark_node;
12262 : }
12263 : else
12264 : {
12265 123795321 : fn = cand->fn;
12266 123795321 : call = NULL_TREE;
12267 :
12268 123795321 : if (!(flags & LOOKUP_NONVIRTUAL)
12269 99467923 : && DECL_PURE_VIRTUAL_P (fn)
12270 262550 : && instance == current_class_ref
12271 124015149 : && (complain & tf_warning))
12272 : {
12273 : /* This is not an error, it is runtime undefined
12274 : behavior. */
12275 219828 : if (!current_function_decl)
12276 3 : warning (0, "pure virtual %q#D called from "
12277 : "non-static data member initializer", fn);
12278 219825 : else if (DECL_CONSTRUCTOR_P (current_function_decl)
12279 219825 : || DECL_DESTRUCTOR_P (current_function_decl))
12280 9 : warning (0, (DECL_CONSTRUCTOR_P (current_function_decl)
12281 : ? G_("pure virtual %q#D called from constructor")
12282 : : G_("pure virtual %q#D called from destructor")),
12283 : fn);
12284 : }
12285 :
12286 138981830 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fn)
12287 217219562 : && !DECL_CONSTRUCTOR_P (fn)
12288 198353833 : && is_dummy_object (instance))
12289 : {
12290 73881 : instance = maybe_resolve_dummy (instance, true);
12291 73881 : if (instance == error_mark_node)
12292 : call = error_mark_node;
12293 73881 : else if (!is_dummy_object (instance))
12294 : {
12295 : /* We captured 'this' in the current lambda now that
12296 : we know we really need it. */
12297 73522 : cand->first_arg = instance;
12298 : }
12299 359 : else if (current_class_ptr && any_dependent_bases_p ())
12300 : /* We can't tell until instantiation time whether we can use
12301 : *this as the implicit object argument. */;
12302 : else
12303 : {
12304 321 : if (complain & tf_error)
12305 60 : error ("cannot call member function %qD without object",
12306 : fn);
12307 321 : call = error_mark_node;
12308 : }
12309 : }
12310 :
12311 123795321 : if (call != error_mark_node)
12312 : {
12313 : /* Now we know what function is being called. */
12314 123795000 : if (fn_p)
12315 32174411 : *fn_p = fn;
12316 : /* Build the actual CALL_EXPR. */
12317 123795000 : call = build_over_call (cand, flags, complain);
12318 :
12319 : /* Suppress warnings for if (my_struct.operator= (x)) where
12320 : my_struct is implicitly converted to bool. */
12321 123795000 : if (TREE_CODE (call) == MODIFY_EXPR)
12322 3234794 : suppress_warning (call, OPT_Wparentheses);
12323 :
12324 : /* In an expression of the form `a->f()' where `f' turns
12325 : out to be a static member function, `a' is
12326 : none-the-less evaluated. */
12327 123795000 : if (!is_dummy_object (instance))
12328 97491968 : call = keep_unused_object_arg (call, instance, fn);
12329 123795000 : if (call != error_mark_node
12330 245302926 : && DECL_DESTRUCTOR_P (cand->fn)
12331 153689294 : && !VOID_TYPE_P (TREE_TYPE (call)))
12332 : /* An explicit call of the form "x->~X()" has type
12333 : "void". However, on platforms where destructors
12334 : return "this" (i.e., those where
12335 : targetm.cxx.cdtor_returns_this is true), such calls
12336 : will appear to have a return value of pointer type
12337 : to the low-level call machinery. We do not want to
12338 : change the low-level machinery, since we want to be
12339 : able to optimize "delete f()" on such platforms as
12340 : "operator delete(~X(f()))" (rather than generating
12341 : "t = f(), ~X(t), operator delete (t)"). */
12342 18202336 : call = build_nop (void_type_node, call);
12343 : }
12344 : }
12345 : }
12346 :
12347 123834626 : if (processing_template_decl && call != error_mark_node)
12348 : {
12349 7444546 : bool cast_to_void = false;
12350 :
12351 7444546 : if (TREE_CODE (call) == COMPOUND_EXPR)
12352 6 : call = TREE_OPERAND (call, 1);
12353 7444540 : else if (TREE_CODE (call) == NOP_EXPR)
12354 : {
12355 0 : cast_to_void = true;
12356 0 : call = TREE_OPERAND (call, 0);
12357 : }
12358 7444546 : if (INDIRECT_REF_P (call))
12359 589827 : call = TREE_OPERAND (call, 0);
12360 :
12361 : /* Prune all but the selected function from the original overload
12362 : set so that we can avoid some duplicate work at instantiation time. */
12363 7444546 : if (really_overloaded_fn (fns))
12364 : {
12365 1814280 : if (DECL_TEMPLATE_INFO (fn)
12366 1814280 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
12367 : {
12368 : /* Use the selected template, not the specialization, so that
12369 : this looks like an actual lookup result for sake of
12370 : filter_memfn_lookup. */
12371 :
12372 263448 : if (OVL_SINGLE_P (fns))
12373 : /* If the original overload set consists of a single function
12374 : template, this isn't beneficial. */
12375 225561 : goto skip_prune;
12376 :
12377 37887 : fn = ovl_make (DECL_TI_TEMPLATE (fn));
12378 37887 : if (template_only)
12379 28666 : fn = lookup_template_function (fn, explicit_targs);
12380 : }
12381 1588719 : orig_fns = copy_node (orig_fns);
12382 1588719 : BASELINK_FUNCTIONS (orig_fns) = fn;
12383 1588719 : BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (orig_fns) = true;
12384 : }
12385 :
12386 5630266 : skip_prune:
12387 7444546 : call = (build_min_non_dep_call_vec
12388 7444546 : (call,
12389 7444546 : build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
12390 : orig_instance, orig_fns, NULL_TREE),
12391 : orig_args));
12392 7444546 : SET_EXPR_LOCATION (call, input_location);
12393 7444546 : call = convert_from_reference (call);
12394 7444546 : if (cast_to_void)
12395 0 : call = build_nop (void_type_node, call);
12396 : }
12397 :
12398 123834626 : if (orig_args != NULL)
12399 7309067 : release_tree_vector (orig_args);
12400 :
12401 : return call;
12402 125353517 : }
12403 :
12404 : /* Returns true iff standard conversion sequence ICS1 is a proper
12405 : subsequence of ICS2. */
12406 :
12407 : static bool
12408 155423097 : is_subseq (conversion *ics1, conversion *ics2)
12409 : {
12410 : /* We can assume that a conversion of the same code
12411 : between the same types indicates a subsequence since we only get
12412 : here if the types we are converting from are the same. */
12413 :
12414 155423097 : while (ics1->kind == ck_rvalue
12415 208186466 : || ics1->kind == ck_lvalue)
12416 52763369 : ics1 = next_conversion (ics1);
12417 :
12418 : while (1)
12419 : {
12420 225524352 : while (ics2->kind == ck_rvalue
12421 225524352 : || ics2->kind == ck_lvalue)
12422 52763369 : ics2 = next_conversion (ics2);
12423 :
12424 172760983 : if (ics2->kind == ck_user
12425 172760983 : || !has_next (ics2->kind))
12426 : /* At this point, ICS1 cannot be a proper subsequence of
12427 : ICS2. We can get a USER_CONV when we are comparing the
12428 : second standard conversion sequence of two user conversion
12429 : sequences. */
12430 : return false;
12431 :
12432 17341413 : ics2 = next_conversion (ics2);
12433 :
12434 17341413 : while (ics2->kind == ck_rvalue
12435 29532175 : || ics2->kind == ck_lvalue)
12436 12190762 : ics2 = next_conversion (ics2);
12437 :
12438 17341413 : if (ics2->kind == ics1->kind
12439 3572 : && same_type_p (ics2->type, ics1->type)
12440 17344940 : && (ics1->kind == ck_identity
12441 3527 : || same_type_p (next_conversion (ics2)->type,
12442 : next_conversion (ics1)->type)))
12443 3527 : return true;
12444 : }
12445 : }
12446 :
12447 : /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
12448 : be any _TYPE nodes. */
12449 :
12450 : bool
12451 444030476 : is_properly_derived_from (tree derived, tree base)
12452 : {
12453 444030476 : if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
12454 : return false;
12455 :
12456 : /* We only allow proper derivation here. The DERIVED_FROM_P macro
12457 : considers every class derived from itself. */
12458 422423714 : return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
12459 422423714 : && DERIVED_FROM_P (base, derived));
12460 : }
12461 :
12462 : /* We build the ICS for an implicit object parameter as a pointer
12463 : conversion sequence. However, such a sequence should be compared
12464 : as if it were a reference conversion sequence. If ICS is the
12465 : implicit conversion sequence for an implicit object parameter,
12466 : modify it accordingly. */
12467 :
12468 : static void
12469 254948640 : maybe_handle_implicit_object (conversion **ics)
12470 : {
12471 254948640 : if ((*ics)->this_p)
12472 : {
12473 : /* [over.match.funcs]
12474 :
12475 : For non-static member functions, the type of the
12476 : implicit object parameter is "reference to cv X"
12477 : where X is the class of which the function is a
12478 : member and cv is the cv-qualification on the member
12479 : function declaration. */
12480 16934790 : conversion *t = *ics;
12481 16934790 : tree reference_type;
12482 :
12483 : /* The `this' parameter is a pointer to a class type. Make the
12484 : implicit conversion talk about a reference to that same class
12485 : type. */
12486 16934790 : reference_type = TREE_TYPE (t->type);
12487 16934790 : reference_type = build_reference_type (reference_type);
12488 :
12489 16934790 : if (t->kind == ck_qual)
12490 3792265 : t = next_conversion (t);
12491 16934790 : if (t->kind == ck_ptr)
12492 573903 : t = next_conversion (t);
12493 16934790 : t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
12494 16934790 : t = direct_reference_binding (reference_type, t);
12495 16934790 : t->this_p = 1;
12496 16934790 : t->rvaluedness_matches_p = 0;
12497 16934790 : *ics = t;
12498 : }
12499 254948640 : }
12500 :
12501 : /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
12502 : and return the initial reference binding conversion. Otherwise,
12503 : leave *ICS unchanged and return NULL. */
12504 :
12505 : static conversion *
12506 254948640 : maybe_handle_ref_bind (conversion **ics)
12507 : {
12508 254948640 : if ((*ics)->kind == ck_ref_bind)
12509 : {
12510 77657015 : conversion *old_ics = *ics;
12511 77657015 : *ics = next_conversion (old_ics);
12512 77657015 : (*ics)->user_conv_p = old_ics->user_conv_p;
12513 77657015 : return old_ics;
12514 : }
12515 :
12516 : return NULL;
12517 : }
12518 :
12519 : /* Get the expression at the beginning of the conversion chain C. */
12520 :
12521 : static tree
12522 51 : conv_get_original_expr (conversion *c)
12523 : {
12524 60 : for (; c; c = next_conversion (c))
12525 60 : if (c->kind == ck_identity || c->kind == ck_ambig || c->kind == ck_aggr)
12526 51 : return c->u.expr;
12527 : return NULL_TREE;
12528 : }
12529 :
12530 : /* Return a tree representing the number of elements initialized by the
12531 : list-initialization C. The caller must check that C converts to an
12532 : array type. */
12533 :
12534 : static tree
12535 126 : nelts_initialized_by_list_init (conversion *c)
12536 : {
12537 : /* If the array we're converting to has a dimension, we'll use that. */
12538 126 : if (TYPE_DOMAIN (c->type))
12539 84 : return array_type_nelts_top (c->type);
12540 : else
12541 : {
12542 : /* Otherwise, we look at how many elements the constructor we're
12543 : initializing from has. */
12544 42 : tree ctor = conv_get_original_expr (c);
12545 72 : return size_int (CONSTRUCTOR_NELTS (ctor));
12546 : }
12547 : }
12548 :
12549 : /* True iff C is a conversion that binds a reference or a pointer to
12550 : an array of unknown bound. */
12551 :
12552 : static inline bool
12553 42391804 : conv_binds_to_array_of_unknown_bound (conversion *c)
12554 : {
12555 : /* ck_ref_bind won't have the reference stripped. */
12556 42391804 : tree type = non_reference (c->type);
12557 : /* ck_qual won't have the pointer stripped. */
12558 42391804 : type = strip_pointer_operator (type);
12559 42391804 : return (TREE_CODE (type) == ARRAY_TYPE
12560 42391804 : && TYPE_DOMAIN (type) == NULL_TREE);
12561 : }
12562 :
12563 : /* Compare two implicit conversion sequences according to the rules set out in
12564 : [over.ics.rank]. Return values:
12565 :
12566 : 1: ics1 is better than ics2
12567 : -1: ics2 is better than ics1
12568 : 0: ics1 and ics2 are indistinguishable */
12569 :
12570 : static int
12571 127474361 : compare_ics (conversion *ics1, conversion *ics2)
12572 : {
12573 127474361 : tree from_type1;
12574 127474361 : tree from_type2;
12575 127474361 : tree to_type1;
12576 127474361 : tree to_type2;
12577 127474361 : tree deref_from_type1 = NULL_TREE;
12578 127474361 : tree deref_from_type2 = NULL_TREE;
12579 127474361 : tree deref_to_type1 = NULL_TREE;
12580 127474361 : tree deref_to_type2 = NULL_TREE;
12581 127474361 : conversion_rank rank1, rank2;
12582 :
12583 : /* REF_BINDING is nonzero if the result of the conversion sequence
12584 : is a reference type. In that case REF_CONV is the reference
12585 : binding conversion. */
12586 127474361 : conversion *ref_conv1;
12587 127474361 : conversion *ref_conv2;
12588 :
12589 : /* Compare badness before stripping the reference conversion. */
12590 127474361 : if (ics1->bad_p > ics2->bad_p)
12591 : return -1;
12592 127474347 : else if (ics1->bad_p < ics2->bad_p)
12593 : return 1;
12594 :
12595 : /* Handle implicit object parameters. */
12596 127474320 : maybe_handle_implicit_object (&ics1);
12597 127474320 : maybe_handle_implicit_object (&ics2);
12598 :
12599 : /* Handle reference parameters. */
12600 127474320 : ref_conv1 = maybe_handle_ref_bind (&ics1);
12601 127474320 : ref_conv2 = maybe_handle_ref_bind (&ics2);
12602 :
12603 : /* List-initialization sequence L1 is a better conversion sequence than
12604 : list-initialization sequence L2 if L1 converts to
12605 : std::initializer_list<X> for some X and L2 does not. */
12606 127474320 : if (ics1->kind == ck_list && ics2->kind != ck_list)
12607 : return 1;
12608 127473819 : if (ics2->kind == ck_list && ics1->kind != ck_list)
12609 : return -1;
12610 :
12611 : /* [over.ics.rank]
12612 :
12613 : When comparing the basic forms of implicit conversion sequences (as
12614 : defined in _over.best.ics_)
12615 :
12616 : --a standard conversion sequence (_over.ics.scs_) is a better
12617 : conversion sequence than a user-defined conversion sequence
12618 : or an ellipsis conversion sequence, and
12619 :
12620 : --a user-defined conversion sequence (_over.ics.user_) is a
12621 : better conversion sequence than an ellipsis conversion sequence
12622 : (_over.ics.ellipsis_). */
12623 : /* Use BAD_CONVERSION_RANK because we already checked for a badness
12624 : mismatch. If both ICS are bad, we try to make a decision based on
12625 : what would have happened if they'd been good. This is not an
12626 : extension, we'll still give an error when we build up the call; this
12627 : just helps us give a more helpful error message. */
12628 127473563 : rank1 = BAD_CONVERSION_RANK (ics1);
12629 127473563 : rank2 = BAD_CONVERSION_RANK (ics2);
12630 :
12631 127473563 : if (rank1 > rank2)
12632 : return -1;
12633 116851737 : else if (rank1 < rank2)
12634 : return 1;
12635 :
12636 78170372 : if (ics1->ellipsis_p)
12637 : /* Both conversions are ellipsis conversions. */
12638 : return 0;
12639 :
12640 : /* User-defined conversion sequence U1 is a better conversion sequence
12641 : than another user-defined conversion sequence U2 if they contain the
12642 : same user-defined conversion operator or constructor and if the sec-
12643 : ond standard conversion sequence of U1 is better than the second
12644 : standard conversion sequence of U2. */
12645 :
12646 : /* Handle list-conversion with the same code even though it isn't always
12647 : ranked as a user-defined conversion and it doesn't have a second
12648 : standard conversion sequence; it will still have the desired effect.
12649 : Specifically, we need to do the reference binding comparison at the
12650 : end of this function. */
12651 :
12652 78170326 : if (ics1->user_conv_p || ics1->kind == ck_list
12653 77497974 : || ics1->kind == ck_aggr || ics2->kind == ck_aggr)
12654 : {
12655 672421 : conversion *t1 = strip_standard_conversion (ics1);
12656 672421 : conversion *t2 = strip_standard_conversion (ics2);
12657 :
12658 672421 : if (!t1 || !t2 || t1->kind != t2->kind)
12659 : return 0;
12660 672402 : else if (t1->kind == ck_user)
12661 : {
12662 634419 : tree f1 = t1->cand ? t1->cand->fn : t1->type;
12663 634419 : tree f2 = t2->cand ? t2->cand->fn : t2->type;
12664 634419 : if (f1 != f2)
12665 : return 0;
12666 : }
12667 : /* List-initialization sequence L1 is a better conversion sequence than
12668 : list-initialization sequence L2 if
12669 :
12670 : -- L1 and L2 convert to arrays of the same element type, and either
12671 : the number of elements n1 initialized by L1 is less than the number
12672 : of elements n2 initialized by L2, or n1=n2 and L2 converts to an array
12673 : of unknown bound and L1 does not. (Added in CWG 1307 and extended by
12674 : P0388R4.) */
12675 37983 : else if (t1->kind == ck_aggr
12676 37190 : && TREE_CODE (t1->type) == ARRAY_TYPE
12677 66 : && TREE_CODE (t2->type) == ARRAY_TYPE
12678 38049 : && same_type_p (TREE_TYPE (t1->type), TREE_TYPE (t2->type)))
12679 : {
12680 63 : tree n1 = nelts_initialized_by_list_init (t1);
12681 63 : tree n2 = nelts_initialized_by_list_init (t2);
12682 63 : if (tree_int_cst_lt (n1, n2))
12683 : return 1;
12684 24 : else if (tree_int_cst_lt (n2, n1))
12685 : return -1;
12686 : /* The n1 == n2 case. */
12687 24 : bool c1 = conv_binds_to_array_of_unknown_bound (t1);
12688 24 : bool c2 = conv_binds_to_array_of_unknown_bound (t2);
12689 24 : if (c1 && !c2)
12690 : return -1;
12691 6 : else if (!c1 && c2)
12692 : return 1;
12693 : else
12694 : return 0;
12695 : }
12696 : else
12697 : {
12698 : /* For ambiguous or aggregate conversions, use the target type as
12699 : a proxy for the conversion function. */
12700 37920 : if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
12701 : return 0;
12702 : }
12703 :
12704 : /* We can just fall through here, after setting up
12705 : FROM_TYPE1 and FROM_TYPE2. */
12706 669677 : from_type1 = t1->type;
12707 669677 : from_type2 = t2->type;
12708 669677 : }
12709 : else
12710 : {
12711 : conversion *t1;
12712 : conversion *t2;
12713 :
12714 : /* We're dealing with two standard conversion sequences.
12715 :
12716 : [over.ics.rank]
12717 :
12718 : Standard conversion sequence S1 is a better conversion
12719 : sequence than standard conversion sequence S2 if
12720 :
12721 : --S1 is a proper subsequence of S2 (comparing the conversion
12722 : sequences in the canonical form defined by _over.ics.scs_,
12723 : excluding any Lvalue Transformation; the identity
12724 : conversion sequence is considered to be a subsequence of
12725 : any non-identity conversion sequence */
12726 :
12727 : t1 = ics1;
12728 118771439 : while (t1->kind != ck_identity)
12729 41273534 : t1 = next_conversion (t1);
12730 77497905 : from_type1 = t1->type;
12731 :
12732 77497905 : t2 = ics2;
12733 118618844 : while (t2->kind != ck_identity)
12734 41120939 : t2 = next_conversion (t2);
12735 77497905 : from_type2 = t2->type;
12736 : }
12737 :
12738 : /* One sequence can only be a subsequence of the other if they start with
12739 : the same type. They can start with different types when comparing the
12740 : second standard conversion sequence in two user-defined conversion
12741 : sequences. */
12742 78167582 : if (same_type_p (from_type1, from_type2))
12743 : {
12744 77713283 : if (is_subseq (ics1, ics2))
12745 : return 1;
12746 77709814 : if (is_subseq (ics2, ics1))
12747 : return -1;
12748 : }
12749 :
12750 : /* [over.ics.rank]
12751 :
12752 : Or, if not that,
12753 :
12754 : --the rank of S1 is better than the rank of S2 (by the rules
12755 : defined below):
12756 :
12757 : Standard conversion sequences are ordered by their ranks: an Exact
12758 : Match is a better conversion than a Promotion, which is a better
12759 : conversion than a Conversion.
12760 :
12761 : Two conversion sequences with the same rank are indistinguishable
12762 : unless one of the following rules applies:
12763 :
12764 : --A conversion that does not a convert a pointer, pointer to member,
12765 : or std::nullptr_t to bool is better than one that does.
12766 :
12767 : The ICS_STD_RANK automatically handles the pointer-to-bool rule,
12768 : so that we do not have to check it explicitly. */
12769 78164055 : if (ics1->rank < ics2->rank)
12770 : return 1;
12771 78163975 : else if (ics2->rank < ics1->rank)
12772 : return -1;
12773 :
12774 78163975 : to_type1 = ics1->type;
12775 78163975 : to_type2 = ics2->type;
12776 :
12777 : /* A conversion from scalar arithmetic type to complex is worse than a
12778 : conversion between scalar arithmetic types. */
12779 78163975 : if (same_type_p (from_type1, from_type2)
12780 77709676 : && ARITHMETIC_TYPE_P (from_type1)
12781 18718653 : && ARITHMETIC_TYPE_P (to_type1)
12782 18718486 : && ARITHMETIC_TYPE_P (to_type2)
12783 78163975 : && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
12784 18718444 : != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
12785 : {
12786 106 : if (TREE_CODE (to_type1) == COMPLEX_TYPE)
12787 : return -1;
12788 : else
12789 : return 1;
12790 : }
12791 :
12792 78163869 : {
12793 : /* A conversion in either direction between floating-point type FP1 and
12794 : floating-point type FP2 is better than a conversion in the same
12795 : direction between FP1 and arithmetic type T3 if
12796 : - the floating-point conversion rank of FP1 is equal to the rank of
12797 : FP2, and
12798 : - T3 is not a floating-point type, or T3 is a floating-point type
12799 : whose rank is not equal to the rank of FP1, or the floating-point
12800 : conversion subrank of FP2 is greater than the subrank of T3. */
12801 78163869 : tree fp1 = from_type1;
12802 78163869 : tree fp2 = to_type1;
12803 78163869 : tree fp3 = from_type2;
12804 78163869 : tree t3 = to_type2;
12805 78163869 : int ret = 1;
12806 78163869 : if (TYPE_MAIN_VARIANT (fp2) == TYPE_MAIN_VARIANT (t3))
12807 : {
12808 70904258 : std::swap (fp1, fp2);
12809 70904258 : std::swap (fp3, t3);
12810 : }
12811 78163869 : if (TYPE_MAIN_VARIANT (fp1) == TYPE_MAIN_VARIANT (fp3)
12812 78163784 : && SCALAR_FLOAT_TYPE_P (fp1)
12813 : /* Only apply this rule if at least one of the 3 types is
12814 : extended floating-point type, otherwise keep them as
12815 : before for compatibility reasons with types like __float128.
12816 : float, double and long double alone have different conversion
12817 : ranks and so when just those 3 types are involved, this
12818 : rule doesn't trigger. */
12819 82848246 : && (extended_float_type_p (fp1)
12820 4574123 : || (SCALAR_FLOAT_TYPE_P (fp2) && extended_float_type_p (fp2))
12821 3984968 : || (SCALAR_FLOAT_TYPE_P (t3) && extended_float_type_p (t3))))
12822 : {
12823 699409 : if (TREE_CODE (fp2) != REAL_TYPE)
12824 : {
12825 22903 : ret = -ret;
12826 22903 : std::swap (fp2, t3);
12827 : }
12828 699409 : if (SCALAR_FLOAT_TYPE_P (fp2))
12829 : {
12830 : /* cp_compare_floating_point_conversion_ranks returns -1, 0 or 1
12831 : if the conversion rank is equal (-1 or 1 if the subrank is
12832 : different). */
12833 686891 : if (IN_RANGE (cp_compare_floating_point_conversion_ranks (fp1,
12834 : fp2),
12835 : -1, 1))
12836 : {
12837 : /* Conversion ranks of FP1 and FP2 are equal. */
12838 508797 : if (TREE_CODE (t3) != REAL_TYPE
12839 508797 : || !IN_RANGE (cp_compare_floating_point_conversion_ranks
12840 : (fp1, t3),
12841 : -1, 1))
12842 : /* FP1 <-> FP2 conversion is better. */
12843 508327 : return ret;
12844 470 : int c = cp_compare_floating_point_conversion_ranks (fp2, t3);
12845 470 : gcc_assert (IN_RANGE (c, -1, 1));
12846 470 : if (c == 1)
12847 : /* Conversion subrank of FP2 is greater than subrank of T3.
12848 : FP1 <-> FP2 conversion is better. */
12849 : return ret;
12850 470 : else if (c == -1)
12851 : /* Conversion subrank of FP2 is less than subrank of T3.
12852 : FP1 <-> T3 conversion is better. */
12853 0 : return -ret;
12854 : }
12855 178094 : else if (SCALAR_FLOAT_TYPE_P (t3)
12856 178094 : && IN_RANGE (cp_compare_floating_point_conversion_ranks
12857 : (fp1, t3),
12858 : -1, 1))
12859 : /* Conversion ranks of FP1 and FP2 are not equal, conversion
12860 : ranks of FP1 and T3 are equal.
12861 : FP1 <-> T3 conversion is better. */
12862 15708 : return -ret;
12863 : }
12864 : }
12865 : }
12866 :
12867 77639834 : if (TYPE_PTR_P (from_type1)
12868 23766500 : && TYPE_PTR_P (from_type2)
12869 23766484 : && TYPE_PTR_P (to_type1)
12870 23766460 : && TYPE_PTR_P (to_type2))
12871 : {
12872 23766460 : deref_from_type1 = TREE_TYPE (from_type1);
12873 23766460 : deref_from_type2 = TREE_TYPE (from_type2);
12874 23766460 : deref_to_type1 = TREE_TYPE (to_type1);
12875 23766460 : deref_to_type2 = TREE_TYPE (to_type2);
12876 : }
12877 : /* The rules for pointers to members A::* are just like the rules
12878 : for pointers A*, except opposite: if B is derived from A then
12879 : A::* converts to B::*, not vice versa. For that reason, we
12880 : switch the from_ and to_ variables here. */
12881 58 : else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2)
12882 58 : && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2))
12883 53873374 : || (TYPE_PTRMEMFUNC_P (from_type1)
12884 437 : && TYPE_PTRMEMFUNC_P (from_type2)
12885 437 : && TYPE_PTRMEMFUNC_P (to_type1)
12886 437 : && TYPE_PTRMEMFUNC_P (to_type2)))
12887 : {
12888 495 : deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
12889 495 : deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
12890 495 : deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
12891 495 : deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
12892 : }
12893 :
12894 23766955 : if (deref_from_type1 != NULL_TREE
12895 23766955 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
12896 290713 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
12897 : {
12898 : /* This was one of the pointer or pointer-like conversions.
12899 :
12900 : [over.ics.rank]
12901 :
12902 : --If class B is derived directly or indirectly from class A,
12903 : conversion of B* to A* is better than conversion of B* to
12904 : void*, and conversion of A* to void* is better than
12905 : conversion of B* to void*. */
12906 290713 : if (VOID_TYPE_P (deref_to_type1)
12907 57 : && VOID_TYPE_P (deref_to_type2))
12908 : {
12909 14 : if (is_properly_derived_from (deref_from_type1,
12910 : deref_from_type2))
12911 : return -1;
12912 14 : else if (is_properly_derived_from (deref_from_type2,
12913 : deref_from_type1))
12914 : return 1;
12915 : }
12916 290699 : else if (VOID_TYPE_P (deref_to_type1)
12917 290656 : || VOID_TYPE_P (deref_to_type2))
12918 : {
12919 47 : if (same_type_p (deref_from_type1, deref_from_type2))
12920 : {
12921 47 : if (VOID_TYPE_P (deref_to_type2))
12922 : {
12923 4 : if (is_properly_derived_from (deref_from_type1,
12924 : deref_to_type1))
12925 : return 1;
12926 : }
12927 : /* We know that DEREF_TO_TYPE1 is `void' here. */
12928 43 : else if (is_properly_derived_from (deref_from_type1,
12929 : deref_to_type2))
12930 : return -1;
12931 : }
12932 : }
12933 290652 : else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
12934 290652 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
12935 : {
12936 : /* [over.ics.rank]
12937 :
12938 : --If class B is derived directly or indirectly from class A
12939 : and class C is derived directly or indirectly from B,
12940 :
12941 : --conversion of C* to B* is better than conversion of C* to
12942 : A*,
12943 :
12944 : --conversion of B* to A* is better than conversion of C* to
12945 : A* */
12946 290652 : if (same_type_p (deref_from_type1, deref_from_type2))
12947 : {
12948 290646 : if (is_properly_derived_from (deref_to_type1,
12949 : deref_to_type2))
12950 : return 1;
12951 290646 : else if (is_properly_derived_from (deref_to_type2,
12952 : deref_to_type1))
12953 : return -1;
12954 : }
12955 6 : else if (same_type_p (deref_to_type1, deref_to_type2))
12956 : {
12957 6 : if (is_properly_derived_from (deref_from_type2,
12958 : deref_from_type1))
12959 : return 1;
12960 0 : else if (is_properly_derived_from (deref_from_type1,
12961 : deref_from_type2))
12962 : return -1;
12963 : }
12964 : }
12965 : }
12966 154698242 : else if (CLASS_TYPE_P (non_reference (from_type1))
12967 110064950 : && same_type_p (from_type1, from_type2))
12968 : {
12969 32345976 : tree from = non_reference (from_type1);
12970 :
12971 : /* [over.ics.rank]
12972 :
12973 : --binding of an expression of type C to a reference of type
12974 : B& is better than binding an expression of type C to a
12975 : reference of type A&
12976 :
12977 : --conversion of C to B is better than conversion of C to A, */
12978 32345976 : if (is_properly_derived_from (from, to_type1)
12979 32345976 : && is_properly_derived_from (from, to_type2))
12980 : {
12981 940591 : if (is_properly_derived_from (to_type1, to_type2))
12982 : return 1;
12983 936976 : else if (is_properly_derived_from (to_type2, to_type1))
12984 : return -1;
12985 : }
12986 : }
12987 90006290 : else if (CLASS_TYPE_P (non_reference (to_type1))
12988 45372998 : && same_type_p (to_type1, to_type2))
12989 : {
12990 5 : tree to = non_reference (to_type1);
12991 :
12992 : /* [over.ics.rank]
12993 :
12994 : --binding of an expression of type B to a reference of type
12995 : A& is better than binding an expression of type C to a
12996 : reference of type A&,
12997 :
12998 : --conversion of B to A is better than conversion of C to A */
12999 5 : if (is_properly_derived_from (from_type1, to)
13000 5 : && is_properly_derived_from (from_type2, to))
13001 : {
13002 3 : if (is_properly_derived_from (from_type2, from_type1))
13003 : return 1;
13004 3 : else if (is_properly_derived_from (from_type1, from_type2))
13005 : return -1;
13006 : }
13007 : }
13008 :
13009 : /* [over.ics.rank]
13010 :
13011 : --S1 and S2 differ only in their qualification conversion and yield
13012 : similar types T1 and T2 (_conv.qual_), respectively, and the cv-
13013 : qualification signature of type T1 is a proper subset of the cv-
13014 : qualification signature of type T2 */
13015 77530574 : if (ics1->kind == ck_qual
13016 351 : && ics2->kind == ck_qual
13017 77530925 : && same_type_p (from_type1, from_type2))
13018 : {
13019 351 : int result = comp_cv_qual_signature (to_type1, to_type2);
13020 351 : if (result != 0)
13021 : return result;
13022 : }
13023 :
13024 : /* [over.ics.rank]
13025 :
13026 : --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
13027 : to an implicit object parameter of a non-static member function
13028 : declared without a ref-qualifier, and either S1 binds an lvalue
13029 : reference to an lvalue and S2 binds an rvalue reference or S1 binds an
13030 : rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x
13031 : draft standard, 13.3.3.2)
13032 :
13033 : --S1 and S2 are reference bindings (_dcl.init.ref_), and the
13034 : types to which the references refer are the same type except for
13035 : top-level cv-qualifiers, and the type to which the reference
13036 : initialized by S2 refers is more cv-qualified than the type to
13037 : which the reference initialized by S1 refers.
13038 :
13039 : DR 1328 [over.match.best]: the context is an initialization by
13040 : conversion function for direct reference binding (13.3.1.6) of a
13041 : reference to function type, the return type of F1 is the same kind of
13042 : reference (i.e. lvalue or rvalue) as the reference being initialized,
13043 : and the return type of F2 is not. */
13044 :
13045 77530488 : if (ref_conv1 && ref_conv2)
13046 : {
13047 24468728 : if (!ref_conv1->this_p && !ref_conv2->this_p
13048 24249492 : && (ref_conv1->rvaluedness_matches_p
13049 24249492 : != ref_conv2->rvaluedness_matches_p)
13050 44549742 : && (same_type_p (ref_conv1->type, ref_conv2->type)
13051 11676969 : || (TYPE_REF_IS_RVALUE (ref_conv1->type)
13052 11676969 : != TYPE_REF_IS_RVALUE (ref_conv2->type))))
13053 : {
13054 11676697 : if (ref_conv1->bad_p
13055 11676697 : && !same_type_p (TREE_TYPE (ref_conv1->type),
13056 : TREE_TYPE (ref_conv2->type)))
13057 : /* Don't prefer a bad conversion that drops cv-quals to a bad
13058 : conversion with the wrong rvalueness. */
13059 : return 0;
13060 11675238 : return (ref_conv1->rvaluedness_matches_p
13061 11675238 : - ref_conv2->rvaluedness_matches_p);
13062 : }
13063 :
13064 21196073 : if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
13065 : {
13066 : /* Per P0388R4:
13067 :
13068 : void f (int(&)[]), // (1)
13069 : f (int(&)[1]), // (2)
13070 : f (int*); // (3)
13071 :
13072 : (2) is better than (1), but (3) should be equal to (1) and to
13073 : (2). For that reason we don't use ck_qual for (1) which would
13074 : give it the cr_exact rank while (3) remains ck_identity.
13075 : Therefore we compare (1) and (2) here. For (1) we'll have
13076 :
13077 : ck_ref_bind <- ck_identity
13078 : int[] & int[1]
13079 :
13080 : so to handle this we must look at ref_conv. */
13081 21195642 : bool c1 = conv_binds_to_array_of_unknown_bound (ref_conv1);
13082 21195642 : bool c2 = conv_binds_to_array_of_unknown_bound (ref_conv2);
13083 21195642 : if (c1 && !c2)
13084 : return -1;
13085 21195636 : else if (!c1 && c2)
13086 : return 1;
13087 :
13088 21195636 : int q1 = cp_type_quals (TREE_TYPE (ref_conv1->type));
13089 21195636 : int q2 = cp_type_quals (TREE_TYPE (ref_conv2->type));
13090 21195636 : if (ref_conv1->bad_p)
13091 : {
13092 : /* Prefer the one that drops fewer cv-quals. */
13093 1606 : tree ftype = next_conversion (ref_conv1)->type;
13094 1606 : int fquals = cp_type_quals (ftype);
13095 1606 : q1 ^= fquals;
13096 1606 : q2 ^= fquals;
13097 : }
13098 21195636 : return comp_cv_qualification (q2, q1);
13099 : }
13100 : }
13101 :
13102 : /* [over.ics.rank]
13103 :
13104 : Per CWG 1601:
13105 : -- A conversion that promotes an enumeration whose underlying type
13106 : is fixed to its underlying type is better than one that promotes to
13107 : the promoted underlying type, if the two are different. */
13108 44658149 : if (ics1->rank == cr_promotion
13109 144 : && ics2->rank == cr_promotion
13110 144 : && UNSCOPED_ENUM_P (from_type1)
13111 27 : && ENUM_FIXED_UNDERLYING_TYPE_P (from_type1)
13112 44658173 : && same_type_p (from_type1, from_type2))
13113 : {
13114 24 : tree utype = ENUM_UNDERLYING_TYPE (from_type1);
13115 24 : tree prom = type_promotes_to (from_type1);
13116 24 : if (!same_type_p (utype, prom))
13117 : {
13118 12 : if (same_type_p (to_type1, utype)
13119 12 : && same_type_p (to_type2, prom))
13120 : return 1;
13121 6 : else if (same_type_p (to_type2, utype)
13122 6 : && same_type_p (to_type1, prom))
13123 : return -1;
13124 : }
13125 : }
13126 :
13127 : /* Neither conversion sequence is better than the other. */
13128 : return 0;
13129 : }
13130 :
13131 : /* The source type for this standard conversion sequence. */
13132 :
13133 : static tree
13134 9 : source_type (conversion *t)
13135 : {
13136 9 : return strip_standard_conversion (t)->type;
13137 : }
13138 :
13139 : /* Note a warning about preferring WINNER to LOSER. We do this by storing
13140 : a pointer to LOSER and re-running joust to produce the warning if WINNER
13141 : is actually used. */
13142 :
13143 : static void
13144 398 : add_warning (struct z_candidate *winner, struct z_candidate *loser)
13145 : {
13146 398 : candidate_warning *cw = (candidate_warning *)
13147 0 : conversion_obstack_alloc (sizeof (candidate_warning));
13148 398 : cw->loser = loser;
13149 398 : cw->next = winner->warnings;
13150 398 : winner->warnings = cw;
13151 398 : }
13152 :
13153 : /* CAND is a constructor candidate in joust in C++17 and up. If it copies a
13154 : prvalue returned from a conversion function, return true. Otherwise, return
13155 : false. */
13156 :
13157 : static bool
13158 1580624 : joust_maybe_elide_copy (z_candidate *cand)
13159 : {
13160 1580624 : tree fn = cand->fn;
13161 3959096 : if (!DECL_COPY_CONSTRUCTOR_P (fn) && !DECL_MOVE_CONSTRUCTOR_P (fn))
13162 797775 : return false;
13163 782849 : conversion *conv = cand->convs[0];
13164 782849 : if (conv->kind == ck_ambig)
13165 : return false;
13166 782847 : gcc_checking_assert (conv->kind == ck_ref_bind);
13167 782847 : conv = next_conversion (conv);
13168 782847 : if (conv->kind == ck_user && !TYPE_REF_P (conv->type))
13169 : {
13170 106 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
13171 : (conv->type, DECL_CONTEXT (fn)));
13172 106 : z_candidate *uc = conv->cand;
13173 106 : if (DECL_CONV_FN_P (uc->fn))
13174 : return true;
13175 : }
13176 : return false;
13177 : }
13178 :
13179 : /* Return the class that CAND's implicit object parameter refers to. */
13180 :
13181 : static tree
13182 281137 : class_of_implicit_object (z_candidate *cand)
13183 : {
13184 281137 : if (!DECL_IOBJ_MEMBER_FUNCTION_P (cand->fn))
13185 : return NULL_TREE;
13186 :
13187 : /* "For conversion functions that are implicit object member functions,
13188 : the function is considered to be a member of the class of the implied
13189 : object argument for the purpose of defining the type of the implicit
13190 : object parameter." */
13191 281137 : if (DECL_CONV_FN_P (cand->fn))
13192 0 : return TYPE_MAIN_VARIANT (TREE_TYPE (cand->first_arg));
13193 :
13194 : /* "For non-conversion functions that are implicit object member
13195 : functions nominated by a using-declaration in a derived class, the
13196 : function is considered to be a member of the derived class for the
13197 : purpose of defining the type of the implicit object parameter."
13198 :
13199 : That derived class is reflected in the conversion_path binfo. */
13200 281137 : return BINFO_TYPE (cand->conversion_path);
13201 : }
13202 :
13203 : /* Return whether the first parameter of C1 matches the second parameter
13204 : of C2. */
13205 :
13206 : static bool
13207 526149 : reversed_match (z_candidate *c1, z_candidate *c2)
13208 : {
13209 526149 : tree fn1 = c1->fn;
13210 526149 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (c2->fn));
13211 526149 : tree parm2 = TREE_VALUE (TREE_CHAIN (parms2));
13212 526149 : if (DECL_IOBJ_MEMBER_FUNCTION_P (fn1))
13213 : {
13214 281137 : tree ctx = class_of_implicit_object (c1);
13215 281137 : return iobj_parm_corresponds_to (fn1, parm2, ctx);
13216 : }
13217 : else
13218 : {
13219 245012 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn1));
13220 245012 : tree parm1 = TREE_VALUE (parms1);
13221 245012 : return same_type_p (parm1, parm2);
13222 : }
13223 : }
13224 :
13225 : /* True if the defining declarations of the two candidates have equivalent
13226 : parameters. MATCH_KIND controls whether we're trying to compare the
13227 : original declarations (for a warning) or the actual candidates. */
13228 :
13229 : enum class pmatch { original, current };
13230 :
13231 : static bool
13232 6817888 : cand_parms_match (z_candidate *c1, z_candidate *c2, pmatch match_kind)
13233 : {
13234 6817888 : tree fn1 = c1->fn;
13235 6817888 : tree fn2 = c2->fn;
13236 6817888 : bool reversed = (match_kind == pmatch::current
13237 6817888 : && c1->reversed () != c2->reversed ());
13238 6817888 : if (fn1 == fn2 && !reversed)
13239 : return true;
13240 6817687 : if (identifier_p (fn1) || identifier_p (fn2))
13241 : return false;
13242 6817670 : if (match_kind == pmatch::original)
13243 : {
13244 : /* We don't look at c1->template_decl because that's only set for
13245 : primary templates, not e.g. non-template member functions of
13246 : class templates. */
13247 22 : tree t1 = most_general_template (fn1);
13248 22 : tree t2 = most_general_template (fn2);
13249 22 : if (t1 || t2)
13250 : {
13251 15 : if (!t1 || !t2)
13252 : return false;
13253 15 : if (t1 == t2)
13254 : return true;
13255 3 : fn1 = DECL_TEMPLATE_RESULT (t1);
13256 3 : fn2 = DECL_TEMPLATE_RESULT (t2);
13257 : }
13258 : }
13259 :
13260 6817658 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn1));
13261 6817658 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (fn2));
13262 :
13263 13430928 : if (DECL_FUNCTION_MEMBER_P (fn1)
13264 6818071 : && DECL_FUNCTION_MEMBER_P (fn2))
13265 : {
13266 204787 : tree base1 = DECL_CONTEXT (strip_inheriting_ctors (fn1));
13267 204787 : tree base2 = DECL_CONTEXT (strip_inheriting_ctors (fn2));
13268 204787 : if (base1 != base2)
13269 141028 : return false;
13270 :
13271 204610 : if (reversed)
13272 140851 : return (reversed_match (c1, c2)
13273 140851 : && reversed_match (c2, c1));
13274 :
13275 : /* Use object_parms_correspond to simplify comparing iobj/xobj/static
13276 : member functions. */
13277 63759 : if (!object_parms_correspond (fn1, fn2, base1))
13278 : return false;
13279 :
13280 : /* We just compared the object parameters, if they don't correspond
13281 : we already returned false. */
13282 191277 : auto skip_parms = [] (tree fn, tree parms)
13283 : {
13284 127518 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
13285 680 : return TREE_CHAIN (parms);
13286 : else
13287 126838 : return skip_artificial_parms_for (fn, parms);
13288 : };
13289 63759 : parms1 = skip_parms (fn1, parms1);
13290 63759 : parms2 = skip_parms (fn2, parms2);
13291 : }
13292 6612871 : else if (reversed)
13293 122487 : return (reversed_match (c1, c2)
13294 122487 : && reversed_match (c2, c1));
13295 6554143 : return compparms (parms1, parms2);
13296 : }
13297 :
13298 : /* True iff FN is a copy or move constructor or assignment operator. */
13299 :
13300 : static bool
13301 29938104 : sfk_copy_or_move (tree fn)
13302 : {
13303 29938104 : if (TREE_CODE (fn) != FUNCTION_DECL)
13304 : return false;
13305 29938014 : special_function_kind sfk = special_function_p (fn);
13306 29938014 : return sfk >= sfk_copy_constructor && sfk <= sfk_move_assignment;
13307 : }
13308 :
13309 : /* Compare two candidates for overloading as described in
13310 : [over.match.best]. Return values:
13311 :
13312 : 1: cand1 is better than cand2
13313 : -1: cand2 is better than cand1
13314 : 0: cand1 and cand2 are indistinguishable */
13315 :
13316 : static int
13317 95187627 : joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
13318 : tsubst_flags_t complain)
13319 : {
13320 95187627 : int winner = 0;
13321 95187627 : int off1 = 0, off2 = 0;
13322 95187627 : size_t i;
13323 95187627 : size_t len;
13324 :
13325 : /* Candidates that involve bad conversions are always worse than those
13326 : that don't. */
13327 95187627 : if (cand1->viable > cand2->viable)
13328 : return 1;
13329 76670082 : if (cand1->viable < cand2->viable)
13330 : return -1;
13331 :
13332 : /* If we have two pseudo-candidates for conversions to the same type,
13333 : or two candidates for the same function, arbitrarily pick one. */
13334 76670082 : if (cand1->fn == cand2->fn
13335 2645452 : && cand1->reversed () == cand2->reversed ()
13336 77665269 : && (IS_TYPE_OR_DECL_P (cand1->fn)))
13337 : return 1;
13338 :
13339 : /* Prefer a non-deleted function over an implicitly deleted move
13340 : constructor or assignment operator. This differs slightly from the
13341 : wording for issue 1402 (which says the move op is ignored by overload
13342 : resolution), but this way produces better error messages. */
13343 76670082 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13344 72354119 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13345 149017400 : && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn))
13346 : {
13347 3363876 : if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn)
13348 2824131 : && move_fn_p (cand1->fn))
13349 : return -1;
13350 5104337 : if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn)
13351 3791039 : && move_fn_p (cand2->fn))
13352 : return 1;
13353 : }
13354 :
13355 : /* a viable function F1
13356 : is defined to be a better function than another viable function F2 if
13357 : for all arguments i, ICSi(F1) is not a worse conversion sequence than
13358 : ICSi(F2), and then */
13359 :
13360 : /* for some argument j, ICSj(F1) is a better conversion sequence than
13361 : ICSj(F2) */
13362 :
13363 : /* For comparing static and non-static member functions, we ignore
13364 : the implicit object parameter of the non-static function. The
13365 : standard says to pretend that the static function has an object
13366 : parm, but that won't work with operator overloading. */
13367 76621744 : len = cand1->num_convs;
13368 76621744 : if (len != cand2->num_convs)
13369 : {
13370 374 : int static_1 = (TREE_CODE (cand1->fn) == FUNCTION_DECL
13371 374 : && DECL_STATIC_FUNCTION_P (cand1->fn));
13372 374 : int static_2 = (TREE_CODE (cand2->fn) == FUNCTION_DECL
13373 374 : && DECL_STATIC_FUNCTION_P (cand2->fn));
13374 :
13375 374 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13376 139 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13377 139 : && DECL_CONSTRUCTOR_P (cand1->fn)
13378 380 : && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
13379 : /* We're comparing a near-match list constructor and a near-match
13380 : non-list constructor. Just treat them as unordered. */
13381 : return 0;
13382 :
13383 368 : gcc_assert (static_1 != static_2);
13384 :
13385 368 : if (static_1)
13386 : {
13387 : /* C++23 [over.best.ics.general] says:
13388 : When the parameter is the implicit object parameter of a static
13389 : member function, the implicit conversion sequence is a standard
13390 : conversion sequence that is neither better nor worse than any
13391 : other standard conversion sequence. */
13392 46 : if (CONVERSION_RANK (cand2->convs[0]) >= cr_user)
13393 0 : winner = 1;
13394 : off2 = 1;
13395 : }
13396 : else
13397 : {
13398 322 : if (CONVERSION_RANK (cand1->convs[0]) >= cr_user)
13399 235 : winner = -1;
13400 322 : off1 = 1;
13401 322 : --len;
13402 : }
13403 : }
13404 :
13405 204045069 : for (i = 0; i < len; ++i)
13406 : {
13407 127424517 : conversion *t1 = cand1->convs[i + off1];
13408 127424517 : conversion *t2 = cand2->convs[i + off2];
13409 127424517 : int comp = compare_ics (t1, t2);
13410 :
13411 127424517 : if (comp != 0)
13412 : {
13413 65738900 : if ((complain & tf_warning)
13414 53916281 : && warn_sign_promo
13415 82 : && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
13416 : == cr_std + cr_promotion)
13417 50 : && t1->kind == ck_std
13418 31 : && t2->kind == ck_std
13419 31 : && TREE_CODE (t1->type) == INTEGER_TYPE
13420 31 : && TREE_CODE (t2->type) == INTEGER_TYPE
13421 31 : && (TYPE_PRECISION (t1->type)
13422 31 : == TYPE_PRECISION (t2->type))
13423 65738931 : && (TYPE_UNSIGNED (next_conversion (t1)->type)
13424 0 : || (TREE_CODE (next_conversion (t1)->type)
13425 : == ENUMERAL_TYPE)))
13426 : {
13427 31 : tree type = next_conversion (t1)->type;
13428 31 : tree type1, type2;
13429 31 : struct z_candidate *w, *l;
13430 31 : if (comp > 0)
13431 : type1 = t1->type, type2 = t2->type,
13432 : w = cand1, l = cand2;
13433 : else
13434 8 : type1 = t2->type, type2 = t1->type,
13435 8 : w = cand2, l = cand1;
13436 :
13437 31 : if (warn)
13438 : {
13439 9 : warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
13440 : type, type1, type2);
13441 9 : warning (OPT_Wsign_promo, " in call to %qD", w->fn);
13442 : }
13443 : else
13444 22 : add_warning (w, l);
13445 : }
13446 :
13447 65738900 : if (winner && comp != winner)
13448 : {
13449 : /* Ambiguity between normal and reversed comparison operators
13450 : with the same parameter types. P2468 decided not to go with
13451 : this approach to resolving the ambiguity, so pedwarn. */
13452 1186 : if ((complain & tf_warning_or_error)
13453 631 : && (cand1->reversed () != cand2->reversed ())
13454 1426 : && cand_parms_match (cand1, cand2, pmatch::original))
13455 : {
13456 225 : struct z_candidate *w, *l;
13457 225 : if (cand2->reversed ())
13458 : winner = 1, w = cand1, l = cand2;
13459 : else
13460 203 : winner = -1, w = cand2, l = cand1;
13461 225 : if (warn)
13462 : {
13463 22 : auto_diagnostic_group d;
13464 22 : if (pedwarn (input_location, 0,
13465 : "C++20 says that these are ambiguous, "
13466 : "even though the second is reversed:"))
13467 : {
13468 22 : print_z_candidate (input_location,
13469 : N_("candidate 1:"), w);
13470 22 : print_z_candidate (input_location,
13471 : N_("candidate 2:"), l);
13472 22 : if (w->fn == l->fn
13473 17 : && DECL_IOBJ_MEMBER_FUNCTION_P (w->fn)
13474 37 : && (type_memfn_quals (TREE_TYPE (w->fn))
13475 15 : & TYPE_QUAL_CONST) == 0)
13476 : {
13477 : /* Suggest adding const to
13478 : struct A { bool operator==(const A&); }; */
13479 12 : tree parmtype
13480 12 : = FUNCTION_FIRST_USER_PARMTYPE (w->fn);
13481 12 : parmtype = TREE_VALUE (parmtype);
13482 12 : if (TYPE_REF_P (parmtype)
13483 12 : && TYPE_READONLY (TREE_TYPE (parmtype))
13484 24 : && (same_type_ignoring_top_level_qualifiers_p
13485 12 : (TREE_TYPE (parmtype),
13486 12 : DECL_CONTEXT (w->fn))))
13487 12 : inform (DECL_SOURCE_LOCATION (w->fn),
13488 : "try making the operator a %<const%> "
13489 : "member function");
13490 : }
13491 : }
13492 22 : }
13493 : else
13494 203 : add_warning (w, l);
13495 225 : return winner;
13496 : }
13497 :
13498 961 : winner = 0;
13499 961 : goto tweak;
13500 : }
13501 : winner = comp;
13502 : }
13503 : }
13504 :
13505 : /* warn about confusing overload resolution for user-defined conversions,
13506 : either between a constructor and a conversion op, or between two
13507 : conversion ops. */
13508 76620552 : if ((complain & tf_warning)
13509 : /* In C++17, the constructor might have been elided, which means that
13510 : an originally null ->second_conv could become non-null. */
13511 61551723 : && winner && warn_conversion && cand1->second_conv && cand2->second_conv
13512 54 : && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
13513 76620579 : && winner != compare_ics (cand1->second_conv, cand2->second_conv))
13514 : {
13515 27 : struct z_candidate *w, *l;
13516 27 : bool give_warning = false;
13517 :
13518 27 : if (winner == 1)
13519 : w = cand1, l = cand2;
13520 : else
13521 6 : w = cand2, l = cand1;
13522 :
13523 : /* We don't want to complain about `X::operator T1 ()'
13524 : beating `X::operator T2 () const', when T2 is a no less
13525 : cv-qualified version of T1. */
13526 27 : if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
13527 39 : && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
13528 : {
13529 6 : tree t = TREE_TYPE (TREE_TYPE (l->fn));
13530 6 : tree f = TREE_TYPE (TREE_TYPE (w->fn));
13531 :
13532 6 : if (TREE_CODE (t) == TREE_CODE (f) && INDIRECT_TYPE_P (t))
13533 : {
13534 6 : t = TREE_TYPE (t);
13535 6 : f = TREE_TYPE (f);
13536 : }
13537 6 : if (!comp_ptr_ttypes (t, f))
13538 : give_warning = true;
13539 : }
13540 : else
13541 : give_warning = true;
13542 :
13543 : if (!give_warning)
13544 : /*NOP*/;
13545 21 : else if (warn)
13546 : {
13547 9 : tree source = source_type (w->convs[0]);
13548 9 : if (INDIRECT_TYPE_P (source))
13549 6 : source = TREE_TYPE (source);
13550 9 : auto_diagnostic_group d;
13551 9 : if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
13552 18 : && warning (OPT_Wconversion, " for conversion from %qH to %qI",
13553 9 : source, w->second_conv->type))
13554 : {
13555 9 : inform (input_location, " because conversion sequence "
13556 : "for the argument is better");
13557 : }
13558 9 : }
13559 : else
13560 12 : add_warning (w, l);
13561 : }
13562 :
13563 76620552 : if (winner)
13564 : return winner;
13565 :
13566 : /* DR 495 moved this tiebreaker above the template ones. */
13567 : /* or, if not that,
13568 : the context is an initialization by user-defined conversion (see
13569 : _dcl.init_ and _over.match.user_) and the standard conversion
13570 : sequence from the return type of F1 to the destination type (i.e.,
13571 : the type of the entity being initialized) is a better conversion
13572 : sequence than the standard conversion sequence from the return type
13573 : of F2 to the destination type. */
13574 :
13575 14969188 : if (cand1->second_conv)
13576 : {
13577 49817 : winner = compare_ics (cand1->second_conv, cand2->second_conv);
13578 49817 : if (winner)
13579 : return winner;
13580 : }
13581 :
13582 : /* CWG2735 (PR109247): A copy/move ctor/op= for which its operand uses an
13583 : explicit conversion (due to list-initialization) is worse. */
13584 14969052 : {
13585 14969052 : z_candidate *sp = nullptr;
13586 14969052 : if (sfk_copy_or_move (cand1->fn))
13587 504 : sp = cand1;
13588 14969052 : if (sfk_copy_or_move (cand2->fn))
13589 1973504 : sp = sp ? nullptr : cand2;
13590 14969003 : if (sp)
13591 : {
13592 3947820 : conversion *conv = sp->convs[!DECL_CONSTRUCTOR_P (sp->fn)];
13593 1973910 : if (conv->user_conv_p)
13594 1218 : for (; conv; conv = next_conversion (conv))
13595 1100 : if (conv->kind == ck_user
13596 491 : && DECL_P (conv->cand->fn)
13597 1591 : && DECL_NONCONVERTING_P (conv->cand->fn))
13598 379 : return (sp == cand1) ? -1 : 1;
13599 : }
13600 : }
13601 :
13602 : /* DR2327: C++17 copy elision in [over.match.ctor] (direct-init) context.
13603 : The standard currently says that only constructors are candidates, but if
13604 : one copies a prvalue returned by a conversion function we prefer that.
13605 :
13606 : Clang does something similar, as discussed at
13607 : http://lists.isocpp.org/core/2017/10/3166.php
13608 : http://lists.isocpp.org/core/2019/03/5721.php */
13609 7879571 : if (len == 1 && cxx_dialect >= cxx17
13610 7865523 : && DECL_P (cand1->fn)
13611 7865523 : && DECL_COMPLETE_CONSTRUCTOR_P (cand1->fn)
13612 16873893 : && !(cand1->flags & LOOKUP_ONLYCONVERTING))
13613 : {
13614 790312 : bool elided1 = joust_maybe_elide_copy (cand1);
13615 790312 : bool elided2 = joust_maybe_elide_copy (cand2);
13616 790312 : winner = elided1 - elided2;
13617 790312 : if (winner)
13618 : return winner;
13619 : }
13620 :
13621 : /* or, if not that,
13622 : F1 is a non-template function and F2 is a template function
13623 : specialization. */
13624 :
13625 14968678 : if (!cand1->template_decl && cand2->template_decl)
13626 : return 1;
13627 14805430 : else if (cand1->template_decl && !cand2->template_decl)
13628 : return -1;
13629 :
13630 : /* or, if not that,
13631 : F1 and F2 are template functions and the function template for F1 is
13632 : more specialized than the template for F2 according to the partial
13633 : ordering rules. */
13634 :
13635 12332865 : if (cand1->template_decl && cand2->template_decl)
13636 : {
13637 5503025 : winner = more_specialized_fn
13638 5503025 : (TI_TEMPLATE (cand1->template_decl),
13639 5503025 : TI_TEMPLATE (cand2->template_decl),
13640 : /* [temp.func.order]: The presence of unused ellipsis and default
13641 : arguments has no effect on the partial ordering of function
13642 : templates. add_function_candidate() will not have
13643 : counted the "this" argument for constructors. */
13644 11006050 : cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
13645 5503025 : if (winner)
13646 : return winner;
13647 : }
13648 :
13649 : /* F1 and F2 are non-template functions and
13650 : - they have the same non-object-parameter-type-lists ([dcl.fct]), and
13651 : - if they are member functions, both are direct members of the same
13652 : class, and
13653 : - if both are non-static member functions, they have the same types for
13654 : their object parameters, and
13655 : - F1 is more constrained than F2 according to the partial ordering of
13656 : constraints described in [temp.constr.order]. */
13657 8700985 : if (flag_concepts && DECL_P (cand1->fn) && DECL_P (cand2->fn)
13658 8700937 : && !cand1->template_decl && !cand2->template_decl
13659 15531164 : && cand_parms_match (cand1, cand2, pmatch::current))
13660 : {
13661 377003 : winner = more_constrained (cand1->fn, cand2->fn);
13662 377003 : if (winner)
13663 : return winner;
13664 : }
13665 :
13666 : /* F2 is a rewritten candidate (12.4.1.2) and F1 is not, or F1 and F2 are
13667 : rewritten candidates, and F2 is a synthesized candidate with reversed
13668 : order of parameters and F1 is not. */
13669 8707490 : if (cand1->rewritten ())
13670 : {
13671 1878141 : if (!cand2->rewritten ())
13672 : return -1;
13673 1289601 : if (!cand1->reversed () && cand2->reversed ())
13674 : return 1;
13675 1289601 : if (cand1->reversed () && !cand2->reversed ())
13676 : return -1;
13677 : }
13678 6829349 : else if (cand2->rewritten ())
13679 : return 1;
13680 :
13681 6767300 : if (deduction_guide_p (cand1->fn))
13682 : {
13683 19567 : gcc_assert (deduction_guide_p (cand2->fn));
13684 :
13685 : /* F1 and F2 are generated from class template argument deduction for a
13686 : class D, and F2 is generated from inheriting constructors from a base
13687 : class of D while F1 is not, and for each explicit function argument,
13688 : the corresponding parameters of F1 and F2 are either both ellipses or
13689 : have the same type. */
13690 19567 : bool inherited1 = inherited_guide_p (cand1->fn);
13691 19567 : bool inherited2 = inherited_guide_p (cand2->fn);
13692 19567 : if (int diff = inherited2 - inherited1)
13693 : {
13694 52 : for (i = 0; i < len; ++i)
13695 : {
13696 15 : conversion *t1 = cand1->convs[i + off1];
13697 15 : conversion *t2 = cand2->convs[i + off2];
13698 : /* ??? It seems the ellipses part of this tiebreaker isn't
13699 : needed since a mismatch should have broken the tie earlier
13700 : during ICS comparison. */
13701 15 : gcc_checking_assert (t1->ellipsis_p == t2->ellipsis_p);
13702 15 : if (!same_type_p (t1->type, t2->type))
13703 : break;
13704 : }
13705 39 : if (i == len)
13706 : return diff;
13707 : }
13708 :
13709 : /* F1 is generated from a deduction-guide (13.3.1.8) and F2 is not */
13710 : /* We distinguish between candidates from an explicit deduction guide and
13711 : candidates built from a constructor based on DECL_ARTIFICIAL. */
13712 19530 : int art1 = DECL_ARTIFICIAL (cand1->fn);
13713 19530 : int art2 = DECL_ARTIFICIAL (cand2->fn);
13714 19530 : if (art1 != art2)
13715 1050 : return art2 - art1;
13716 :
13717 18480 : if (art1)
13718 : {
13719 : /* Prefer the special copy guide over a declared copy/move
13720 : constructor. */
13721 18477 : if (copy_guide_p (cand1->fn))
13722 : return 1;
13723 78 : if (copy_guide_p (cand2->fn))
13724 : return -1;
13725 :
13726 : /* Prefer a candidate generated from a non-template constructor. */
13727 26 : int tg1 = template_guide_p (cand1->fn);
13728 26 : int tg2 = template_guide_p (cand2->fn);
13729 26 : if (tg1 != tg2)
13730 6 : return tg2 - tg1;
13731 : }
13732 : }
13733 :
13734 : /* F1 is a constructor for a class D, F2 is a constructor for a base class B
13735 : of D, and for all arguments the corresponding parameters of F1 and F2 have
13736 : the same type (CWG 2273/2277). */
13737 20243113 : if (DECL_INHERITED_CTOR (cand1->fn) || DECL_INHERITED_CTOR (cand2->fn))
13738 : {
13739 87 : tree base1 = DECL_CONTEXT (strip_inheriting_ctors (cand1->fn));
13740 87 : tree base2 = DECL_CONTEXT (strip_inheriting_ctors (cand2->fn));
13741 :
13742 87 : bool used1 = false;
13743 87 : bool used2 = false;
13744 87 : if (base1 == base2)
13745 : /* No difference. */;
13746 87 : else if (DERIVED_FROM_P (base1, base2))
13747 : used1 = true;
13748 18 : else if (DERIVED_FROM_P (base2, base1))
13749 : used2 = true;
13750 :
13751 78 : if (int diff = used2 - used1)
13752 : {
13753 105 : for (i = 0; i < len; ++i)
13754 : {
13755 36 : conversion *t1 = cand1->convs[i + off1];
13756 36 : conversion *t2 = cand2->convs[i + off2];
13757 36 : if (!same_type_p (t1->type, t2->type))
13758 : break;
13759 : }
13760 78 : if (i == len)
13761 : return diff;
13762 : }
13763 : }
13764 :
13765 : /* Check whether we can discard a builtin candidate, either because we
13766 : have two identical ones or matching builtin and non-builtin candidates.
13767 :
13768 : (Pedantically in the latter case the builtin which matched the user
13769 : function should not be added to the overload set, but we spot it here.
13770 :
13771 : [over.match.oper]
13772 : ... the builtin candidates include ...
13773 : - do not have the same parameter type list as any non-template
13774 : non-member candidate. */
13775 :
13776 6747687 : if (identifier_p (cand1->fn) || identifier_p (cand2->fn))
13777 : {
13778 93 : for (i = 0; i < len; ++i)
13779 69 : if (!same_type_p (cand1->convs[i]->type,
13780 : cand2->convs[i]->type))
13781 : break;
13782 43 : if (i == cand1->num_convs)
13783 : {
13784 24 : if (cand1->fn == cand2->fn)
13785 : /* Two built-in candidates; arbitrarily pick one. */
13786 : return 1;
13787 23161296 : else if (identifier_p (cand1->fn))
13788 : /* cand1 is built-in; prefer cand2. */
13789 : return -1;
13790 : else
13791 : /* cand2 is built-in; prefer cand1. */
13792 : return 1;
13793 : }
13794 : }
13795 :
13796 : /* For candidates of a multi-versioned function, make the version with
13797 : the highest priority win. This version will be checked for dispatching
13798 : first. If this version can be inlined into the caller, the front-end
13799 : will simply make a direct call to this function. */
13800 :
13801 6747663 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13802 6747641 : && DECL_FUNCTION_VERSIONED (cand1->fn)
13803 902 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13804 6748565 : && DECL_FUNCTION_VERSIONED (cand2->fn))
13805 : {
13806 902 : tree f1 = TREE_TYPE (cand1->fn);
13807 902 : tree f2 = TREE_TYPE (cand2->fn);
13808 902 : tree p1 = TYPE_ARG_TYPES (f1);
13809 902 : tree p2 = TYPE_ARG_TYPES (f2);
13810 :
13811 : /* Check if cand1->fn and cand2->fn are versions of the same function. It
13812 : is possible that cand1->fn and cand2->fn are function versions but of
13813 : different functions. Check types to see if they are versions of the same
13814 : function. */
13815 902 : if (compparms (p1, p2)
13816 902 : && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
13817 : {
13818 : /* Always make the version with the higher priority, more
13819 : specialized, win. */
13820 902 : gcc_assert (targetm.compare_version_priority);
13821 902 : if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
13822 : return 1;
13823 : else
13824 : return -1;
13825 : }
13826 : }
13827 :
13828 : /* If the two function declarations represent the same function (this can
13829 : happen with declarations in multiple scopes and arg-dependent lookup),
13830 : arbitrarily choose one. But first make sure the default args we're
13831 : using match. */
13832 6746739 : if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
13833 13493500 : && equal_functions (cand1->fn, cand2->fn))
13834 : {
13835 33 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
13836 33 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
13837 :
13838 66 : gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
13839 :
13840 47 : for (i = 0; i < len; ++i)
13841 : {
13842 : /* Don't crash if the fn is variadic. */
13843 14 : if (!parms1)
13844 : break;
13845 14 : parms1 = TREE_CHAIN (parms1);
13846 14 : parms2 = TREE_CHAIN (parms2);
13847 : }
13848 :
13849 33 : if (off1)
13850 0 : parms1 = TREE_CHAIN (parms1);
13851 33 : else if (off2)
13852 0 : parms2 = TREE_CHAIN (parms2);
13853 :
13854 64 : for (; parms1; ++i)
13855 : {
13856 74 : if (!cp_tree_equal (TREE_PURPOSE (parms1),
13857 37 : TREE_PURPOSE (parms2)))
13858 : {
13859 6 : if (warn)
13860 : {
13861 3 : if (complain & tf_error)
13862 : {
13863 3 : auto_diagnostic_group d;
13864 3 : if (permerror (input_location,
13865 : "default argument mismatch in "
13866 : "overload resolution"))
13867 : {
13868 3 : inform (DECL_SOURCE_LOCATION (cand1->fn),
13869 : " candidate 1: %q#F", cand1->fn);
13870 3 : inform (DECL_SOURCE_LOCATION (cand2->fn),
13871 : " candidate 2: %q#F", cand2->fn);
13872 : }
13873 3 : }
13874 : else
13875 : return 0;
13876 : }
13877 : else
13878 3 : add_warning (cand1, cand2);
13879 : break;
13880 : }
13881 31 : parms1 = TREE_CHAIN (parms1);
13882 31 : parms2 = TREE_CHAIN (parms2);
13883 : }
13884 :
13885 33 : return 1;
13886 : }
13887 :
13888 6747689 : tweak:
13889 :
13890 : /* Extension: If the worst conversion for one candidate is better than the
13891 : worst conversion for the other, take the first. */
13892 6747689 : if (!pedantic && (complain & tf_warning_or_error))
13893 : {
13894 : conversion_rank rank1 = cr_identity, rank2 = cr_identity;
13895 19709942 : struct z_candidate *w = 0, *l = 0;
13896 :
13897 19709942 : for (i = 0; i < len; ++i)
13898 : {
13899 13405682 : if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
13900 6157661 : rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
13901 13405682 : if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
13902 6157678 : rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
13903 : }
13904 6304260 : if (rank1 < rank2)
13905 56 : winner = 1, w = cand1, l = cand2;
13906 6304260 : if (rank1 > rank2)
13907 : winner = -1, w = cand2, l = cand1;
13908 6304152 : if (winner)
13909 : {
13910 : /* Don't choose a deleted function over ambiguity. */
13911 164 : if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
13912 : return 0;
13913 164 : if (warn)
13914 : {
13915 6 : auto_diagnostic_group d;
13916 6 : if (pedwarn (input_location, 0,
13917 : "ISO C++ says that these are ambiguous, even "
13918 : "though the worst conversion for the first is "
13919 : "better than the worst conversion for the second:"))
13920 : {
13921 3 : print_z_candidate (input_location, N_("candidate 1:"), w);
13922 3 : print_z_candidate (input_location, N_("candidate 2:"), l);
13923 : }
13924 6 : }
13925 : else
13926 158 : add_warning (w, l);
13927 164 : return winner;
13928 : }
13929 : }
13930 :
13931 : gcc_assert (!winner);
13932 : return 0;
13933 : }
13934 :
13935 : /* Given a list of candidates for overloading, find the best one, if any.
13936 : This algorithm has a worst case of O(2n) (winner is last), and a best
13937 : case of O(n/2) (totally ambiguous); much better than a sorting
13938 : algorithm. The candidates list is assumed to be sorted according
13939 : to viability (via splice_viable). */
13940 :
13941 : static struct z_candidate *
13942 311332666 : tourney (struct z_candidate *candidates, tsubst_flags_t complain)
13943 : {
13944 311332666 : struct z_candidate **champ = &candidates, **challenger;
13945 311332666 : int fate;
13946 311332666 : struct z_candidate *previous_worse_champ = nullptr;
13947 :
13948 : /* Walk through the list once, comparing each current champ to the next
13949 : candidate, knocking out a candidate or two with each comparison. */
13950 :
13951 391696640 : for (challenger = &candidates->next; *challenger && (*challenger)->viable; )
13952 : {
13953 80448736 : fate = joust (*champ, *challenger, 0, complain);
13954 80448736 : if (fate == 1)
13955 51071494 : challenger = &(*challenger)->next;
13956 29377242 : else if (fate == -1)
13957 : {
13958 22631417 : previous_worse_champ = *champ;
13959 22631417 : champ = challenger;
13960 22631417 : challenger = &(*challenger)->next;
13961 : }
13962 : else
13963 : {
13964 6745825 : previous_worse_champ = nullptr;
13965 6745825 : champ = &(*challenger)->next;
13966 6745825 : if (!*champ || !(*champ)->viable
13967 6661157 : || (*champ)->viable < (*challenger)->viable)
13968 : {
13969 : champ = nullptr;
13970 : break;
13971 : }
13972 6661063 : challenger = &(*champ)->next;
13973 : }
13974 : }
13975 :
13976 : /* Make sure the champ is better than all the candidates it hasn't yet
13977 : been compared to. */
13978 :
13979 311332666 : if (champ)
13980 36084016 : for (challenger = &candidates;
13981 347331920 : challenger != champ;
13982 36084016 : challenger = &(*challenger)->next)
13983 : {
13984 36085916 : if (*challenger == previous_worse_champ)
13985 : /* We already know this candidate is worse than the champ. */
13986 21347074 : continue;
13987 14738842 : fate = joust (*champ, *challenger, 0, complain);
13988 14738842 : if (fate != 1)
13989 : {
13990 : champ = nullptr;
13991 : break;
13992 : }
13993 : }
13994 :
13995 311247904 : if (!champ)
13996 86662 : return nullptr;
13997 :
13998 : /* Move the champ to the front of the candidate list. */
13999 :
14000 311246004 : if (champ != &candidates)
14001 : {
14002 23593022 : z_candidate *saved_champ = *champ;
14003 23593022 : *champ = saved_champ->next;
14004 23593022 : saved_champ->next = candidates;
14005 23593022 : candidates = saved_champ;
14006 : }
14007 :
14008 311246004 : return candidates;
14009 : }
14010 :
14011 : /* Returns nonzero if things of type FROM can be converted to TO. */
14012 :
14013 : bool
14014 5411503 : can_convert (tree to, tree from, tsubst_flags_t complain)
14015 : {
14016 5411503 : tree arg = NULL_TREE;
14017 : /* implicit_conversion only considers user-defined conversions
14018 : if it has an expression for the call argument list. */
14019 5411503 : if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to))
14020 107 : arg = build_stub_object (from);
14021 5411503 : return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain);
14022 : }
14023 :
14024 : /* Returns nonzero if things of type FROM can be converted to TO with a
14025 : standard conversion. */
14026 :
14027 : bool
14028 255 : can_convert_standard (tree to, tree from, tsubst_flags_t complain)
14029 : {
14030 255 : return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
14031 : }
14032 :
14033 : /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
14034 :
14035 : bool
14036 6311835 : can_convert_arg (tree to, tree from, tree arg, int flags,
14037 : tsubst_flags_t complain)
14038 : {
14039 6311835 : conversion *t;
14040 6311835 : bool ok_p;
14041 :
14042 6311835 : conversion_obstack_sentinel cos;
14043 : /* We want to discard any access checks done for this test,
14044 : as we might not be in the appropriate access context and
14045 : we'll do the check again when we actually perform the
14046 : conversion. */
14047 6311835 : push_deferring_access_checks (dk_deferred);
14048 :
14049 : /* Handle callers like check_local_shadow forgetting to
14050 : convert_from_reference. */
14051 6311835 : if (TYPE_REF_P (from) && arg)
14052 : {
14053 52 : arg = convert_from_reference (arg);
14054 52 : from = TREE_TYPE (arg);
14055 : }
14056 :
14057 6311835 : t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
14058 : flags, complain);
14059 6311835 : ok_p = (t && !t->bad_p);
14060 :
14061 : /* Discard the access checks now. */
14062 6311835 : pop_deferring_access_checks ();
14063 :
14064 12623670 : return ok_p;
14065 6311835 : }
14066 :
14067 : /* Like can_convert_arg, but allows dubious conversions as well. */
14068 :
14069 : bool
14070 192467857 : can_convert_arg_bad (tree to, tree from, tree arg, int flags,
14071 : tsubst_flags_t complain)
14072 : {
14073 192467857 : conversion *t;
14074 :
14075 192467857 : conversion_obstack_sentinel cos;
14076 : /* Try to perform the conversion. */
14077 192467857 : t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
14078 : flags, complain);
14079 :
14080 384935714 : return t != NULL;
14081 192467857 : }
14082 :
14083 : /* Return an IMPLICIT_CONV_EXPR from EXPR to TYPE with bits set from overload
14084 : resolution FLAGS. */
14085 :
14086 : tree
14087 13596110 : build_implicit_conv_flags (tree type, tree expr, int flags)
14088 : {
14089 : /* In a template, we are only concerned about determining the
14090 : type of non-dependent expressions, so we do not have to
14091 : perform the actual conversion. But for initializers, we
14092 : need to be able to perform it at instantiation
14093 : (or instantiate_non_dependent_expr) time. */
14094 13596110 : expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
14095 13596110 : if (!(flags & LOOKUP_ONLYCONVERTING))
14096 3908949 : IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
14097 13596110 : if (flags & LOOKUP_NO_NARROWING)
14098 21719 : IMPLICIT_CONV_EXPR_BRACED_INIT (expr) = true;
14099 13596110 : return expr;
14100 : }
14101 :
14102 : /* Convert EXPR to TYPE. Return the converted expression.
14103 :
14104 : Note that we allow bad conversions here because by the time we get to
14105 : this point we are committed to doing the conversion. If we end up
14106 : doing a bad conversion, convert_like will complain. */
14107 :
14108 : tree
14109 317425779 : perform_implicit_conversion_flags (tree type, tree expr,
14110 : tsubst_flags_t complain, int flags)
14111 : {
14112 317425779 : conversion *conv;
14113 317425779 : location_t loc = cp_expr_loc_or_input_loc (expr);
14114 :
14115 317425779 : if (error_operand_p (expr))
14116 430 : return error_mark_node;
14117 :
14118 317425349 : conversion_obstack_sentinel cos;
14119 :
14120 317425349 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
14121 : /*c_cast_p=*/false,
14122 : flags, complain);
14123 :
14124 317425349 : if (!conv)
14125 : {
14126 320459 : if (complain & tf_error)
14127 479 : implicit_conversion_error (loc, type, expr, flags);
14128 320459 : expr = error_mark_node;
14129 : }
14130 317104890 : else if (processing_template_decl && conv->kind != ck_identity)
14131 13578072 : expr = build_implicit_conv_flags (type, expr, flags);
14132 : else
14133 : {
14134 : /* Give a conversion call the same location as expr. */
14135 303526818 : iloc_sentinel il (loc);
14136 303526818 : expr = convert_like (conv, expr, complain);
14137 303526818 : }
14138 :
14139 317425349 : return expr;
14140 317425349 : }
14141 :
14142 : tree
14143 32318820 : perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
14144 : {
14145 32318820 : return perform_implicit_conversion_flags (type, expr, complain,
14146 32318820 : LOOKUP_IMPLICIT);
14147 : }
14148 :
14149 : /* Convert EXPR to TYPE (as a direct-initialization) if that is
14150 : permitted. If the conversion is valid, the converted expression is
14151 : returned. Otherwise, NULL_TREE is returned, except in the case
14152 : that TYPE is a class type; in that case, an error is issued. If
14153 : C_CAST_P is true, then this direct-initialization is taking
14154 : place as part of a static_cast being attempted as part of a C-style
14155 : cast. */
14156 :
14157 : tree
14158 49610050 : perform_direct_initialization_if_possible (tree type,
14159 : tree expr,
14160 : bool c_cast_p,
14161 : tsubst_flags_t complain)
14162 : {
14163 49610050 : conversion *conv;
14164 :
14165 49610050 : if (type == error_mark_node || error_operand_p (expr))
14166 : return error_mark_node;
14167 : /* [dcl.init]
14168 :
14169 : If the destination type is a (possibly cv-qualified) class type:
14170 :
14171 : -- If the initialization is direct-initialization ...,
14172 : constructors are considered.
14173 :
14174 : -- If overload resolution is successful, the selected constructor
14175 : is called to initialize the object, with the initializer expression
14176 : or expression-list as its argument(s).
14177 :
14178 : -- Otherwise, if no constructor is viable, the destination type is
14179 : a (possibly cv-qualified) aggregate class A, and the initializer is
14180 : a parenthesized expression-list, the object is initialized as
14181 : follows... */
14182 49610050 : if (CLASS_TYPE_P (type))
14183 : {
14184 3584184 : releasing_vec args (make_tree_vector_single (expr));
14185 3584184 : expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
14186 : &args, type, LOOKUP_NORMAL, complain);
14187 3584184 : return build_cplus_new (type, expr, complain);
14188 3584184 : }
14189 :
14190 46025866 : conversion_obstack_sentinel cos;
14191 :
14192 46025866 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
14193 : c_cast_p,
14194 : LOOKUP_NORMAL, complain);
14195 46025866 : if (!conv || conv->bad_p)
14196 : expr = NULL_TREE;
14197 41155134 : else if (processing_template_decl && conv->kind != ck_identity)
14198 : {
14199 : /* In a template, we are only concerned about determining the
14200 : type of non-dependent expressions, so we do not have to
14201 : perform the actual conversion. But for initializers, we
14202 : need to be able to perform it at instantiation
14203 : (or instantiate_non_dependent_expr) time. */
14204 653666 : expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
14205 653666 : IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
14206 : }
14207 : else
14208 40501468 : expr = convert_like (conv, expr, NULL_TREE, 0,
14209 : /*issue_conversion_warnings=*/false,
14210 : c_cast_p, /*nested_p=*/false, complain);
14211 :
14212 46025866 : return expr;
14213 46025866 : }
14214 :
14215 : /* When initializing a reference that lasts longer than a full-expression,
14216 : this special rule applies:
14217 :
14218 : [class.temporary]
14219 :
14220 : The temporary to which the reference is bound or the temporary
14221 : that is the complete object to which the reference is bound
14222 : persists for the lifetime of the reference.
14223 :
14224 : The temporaries created during the evaluation of the expression
14225 : initializing the reference, except the temporary to which the
14226 : reference is bound, are destroyed at the end of the
14227 : full-expression in which they are created.
14228 :
14229 : In that case, we store the converted expression into a new
14230 : VAR_DECL in a new scope.
14231 :
14232 : However, we want to be careful not to create temporaries when
14233 : they are not required. For example, given:
14234 :
14235 : struct B {};
14236 : struct D : public B {};
14237 : D f();
14238 : const B& b = f();
14239 :
14240 : there is no need to copy the return value from "f"; we can just
14241 : extend its lifetime. Similarly, given:
14242 :
14243 : struct S {};
14244 : struct T { operator S(); };
14245 : T t;
14246 : const S& s = t;
14247 :
14248 : we can extend the lifetime of the return value of the conversion
14249 : operator.
14250 :
14251 : The next several functions are involved in this lifetime extension. */
14252 :
14253 : /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
14254 : reference is being bound to a temporary. Create and return a new
14255 : VAR_DECL with the indicated TYPE; this variable will store the value to
14256 : which the reference is bound. */
14257 :
14258 : tree
14259 24266 : make_temporary_var_for_ref_to_temp (tree decl, tree type)
14260 : {
14261 24266 : tree var = create_temporary_var (type);
14262 :
14263 : /* Register the variable. */
14264 24266 : if (VAR_P (decl)
14265 24266 : && (TREE_STATIC (decl) || CP_DECL_THREAD_LOCAL_P (decl)))
14266 : {
14267 : /* Namespace-scope or local static; give it a mangled name. */
14268 :
14269 : /* If an initializer is visible to multiple translation units, those
14270 : translation units must agree on the addresses of the
14271 : temporaries. Therefore the temporaries must be given a consistent name
14272 : and vague linkage. The mangled name of a temporary is the name of the
14273 : non-temporary object in whose initializer they appear, prefixed with
14274 : GR and suffixed with a sequence number mangled using the usual rules
14275 : for a seq-id. Temporaries are numbered with a pre-order, depth-first,
14276 : left-to-right walk of the complete initializer. */
14277 810 : copy_linkage (var, decl);
14278 :
14279 810 : tree name = mangle_ref_init_variable (decl);
14280 810 : DECL_NAME (var) = name;
14281 810 : SET_DECL_ASSEMBLER_NAME (var, name);
14282 :
14283 : /* Set the context to make the variable mergeable in modules. */
14284 810 : DECL_CONTEXT (var) = current_scope ();
14285 : }
14286 : else
14287 : /* Create a new cleanup level if necessary. */
14288 23456 : maybe_push_cleanup_level (type);
14289 :
14290 24266 : return pushdecl (var);
14291 : }
14292 :
14293 : static tree extend_temps_r (tree *, int *, void *);
14294 :
14295 : /* EXPR is the initializer for a variable DECL of reference or
14296 : std::initializer_list type. Create, push and return a new VAR_DECL
14297 : for the initializer so that it will live as long as DECL. Any
14298 : cleanup for the new variable is returned through CLEANUP, and the
14299 : code to initialize the new variable is returned through INITP. */
14300 :
14301 : static tree
14302 24266 : set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
14303 : tree *initp, tree *cond_guard,
14304 : void *walk_data)
14305 : {
14306 24266 : tree init;
14307 24266 : tree type;
14308 24266 : tree var;
14309 :
14310 : /* Create the temporary variable. */
14311 24266 : type = TREE_TYPE (expr);
14312 24266 : var = make_temporary_var_for_ref_to_temp (decl, type);
14313 24266 : layout_decl (var, 0);
14314 : /* If the rvalue is the result of a function call it will be
14315 : a TARGET_EXPR. If it is some other construct (such as a
14316 : member access expression where the underlying object is
14317 : itself the result of a function call), turn it into a
14318 : TARGET_EXPR here. It is important that EXPR be a
14319 : TARGET_EXPR below since otherwise the INIT_EXPR will
14320 : attempt to make a bitwise copy of EXPR to initialize
14321 : VAR. */
14322 24266 : if (TREE_CODE (expr) != TARGET_EXPR)
14323 0 : expr = get_target_expr (expr);
14324 : else
14325 : {
14326 24266 : if (TREE_ADDRESSABLE (expr))
14327 24192 : TREE_ADDRESSABLE (var) = 1;
14328 24266 : if (DECL_MERGEABLE (TARGET_EXPR_SLOT (expr)))
14329 521 : DECL_MERGEABLE (var) = true;
14330 : }
14331 :
14332 24266 : if (TREE_CODE (decl) == FIELD_DECL
14333 24266 : && extra_warnings && !warning_suppressed_p (decl))
14334 : {
14335 3 : warning (OPT_Wextra, "a temporary bound to %qD only persists "
14336 : "until the constructor exits", decl);
14337 3 : suppress_warning (decl);
14338 : }
14339 :
14340 : /* Recursively extend temps in this initializer. The recursion needs to come
14341 : after creating the variable to conform to the mangling ABI, and before
14342 : maybe_constant_init because the extension might change its result. */
14343 24266 : if (walk_data)
14344 1437 : cp_walk_tree (&TARGET_EXPR_INITIAL (expr), extend_temps_r,
14345 : walk_data, nullptr);
14346 : else
14347 22829 : TARGET_EXPR_INITIAL (expr)
14348 45658 : = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups,
14349 : cond_guard);
14350 :
14351 : /* Any reference temp has a non-trivial initializer. */
14352 24266 : DECL_NONTRIVIALLY_INITIALIZED_P (var) = true;
14353 :
14354 : /* If the initializer is constant, put it in DECL_INITIAL so we get
14355 : static initialization and use in constant expressions. */
14356 24266 : init = maybe_constant_init (expr, var, /*manifestly_const_eval=*/true);
14357 : /* As in store_init_value. */
14358 24266 : init = cp_fully_fold (init);
14359 24266 : if (TREE_CONSTANT (init))
14360 : {
14361 1063 : if (literal_type_p (type)
14362 1037 : && CP_TYPE_CONST_NON_VOLATILE_P (type)
14363 1680 : && !TYPE_HAS_MUTABLE_P (type))
14364 : {
14365 : /* 5.19 says that a constant expression can include an
14366 : lvalue-rvalue conversion applied to "a glvalue of literal type
14367 : that refers to a non-volatile temporary object initialized
14368 : with a constant expression". Rather than try to communicate
14369 : that this VAR_DECL is a temporary, just mark it constexpr. */
14370 614 : DECL_DECLARED_CONSTEXPR_P (var) = true;
14371 614 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
14372 614 : TREE_CONSTANT (var) = true;
14373 614 : TREE_READONLY (var) = true;
14374 : }
14375 1063 : DECL_INITIAL (var) = init;
14376 1063 : init = NULL_TREE;
14377 : }
14378 : else
14379 : /* Create the INIT_EXPR that will initialize the temporary
14380 : variable. */
14381 23203 : init = split_nonconstant_init (var, expr);
14382 24266 : if (at_function_scope_p ())
14383 : {
14384 23578 : add_decl_expr (var);
14385 :
14386 23578 : if (TREE_STATIC (var))
14387 122 : init = add_stmt_to_compound (init, register_dtor_fn (var));
14388 : else
14389 : {
14390 : /* ??? Instead of rebuilding the cleanup, we could replace the slot
14391 : with var in TARGET_EXPR_CLEANUP (expr). */
14392 23456 : tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
14393 23456 : if (cleanup)
14394 : {
14395 19390 : if (cond_guard && cleanup != error_mark_node)
14396 : {
14397 23 : if (*cond_guard == NULL_TREE)
14398 : {
14399 23 : *cond_guard = build_local_temp (boolean_type_node);
14400 23 : add_decl_expr (*cond_guard);
14401 23 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION,
14402 : *cond_guard, NOP_EXPR,
14403 : boolean_false_node,
14404 : tf_warning_or_error);
14405 23 : finish_expr_stmt (set);
14406 : }
14407 23 : cleanup = build3 (COND_EXPR, void_type_node,
14408 : *cond_guard, cleanup, NULL_TREE);
14409 : }
14410 19390 : if (flag_exceptions && TREE_CODE (TREE_TYPE (var)) != ARRAY_TYPE)
14411 : {
14412 : /* The normal cleanup for this extended variable isn't pushed
14413 : until cp_finish_decl, so we need to retain a TARGET_EXPR
14414 : to clean it up in case a later initializer throws
14415 : (g++.dg/eh/ref-temp3.C).
14416 :
14417 : We don't do this for array temporaries because they have
14418 : the array cleanup region from build_vec_init.
14419 :
14420 : Unlike maybe_push_temp_cleanup, we don't actually need a
14421 : flag, but a TARGET_EXPR needs a TARGET_EXPR_SLOT.
14422 : Perhaps this could use WITH_CLEANUP_EXPR instead, but
14423 : gimplify.cc doesn't handle that, and front-end handling
14424 : was removed in r8-1725 and r8-1818.
14425 :
14426 : Alternately it might be preferable to flatten an
14427 : initialization with extended temps into a sequence of
14428 : (non-full-expression) statements, so we could immediately
14429 : push_cleanup here for only a single cleanup region, but we
14430 : don't have a mechanism for that in the front-end, only the
14431 : gimplifier. */
14432 19280 : tree targ = get_internal_target_expr (boolean_true_node);
14433 19280 : TARGET_EXPR_CLEANUP (targ) = cleanup;
14434 19280 : CLEANUP_EH_ONLY (targ) = true;
14435 : /* Don't actually initialize the bool. */
14436 19280 : init = (!init ? void_node
14437 19254 : : convert_to_void (init, ICV_STATEMENT, tf_none));
14438 19280 : TARGET_EXPR_INITIAL (targ) = init;
14439 19280 : init = targ;
14440 : }
14441 19390 : vec_safe_push (*cleanups, cleanup);
14442 : }
14443 : }
14444 :
14445 : /* We must be careful to destroy the temporary only
14446 : after its initialization has taken place. If the
14447 : initialization throws an exception, then the
14448 : destructor should not be run. We cannot simply
14449 : transform INIT into something like:
14450 :
14451 : (INIT, ({ CLEANUP_STMT; }))
14452 :
14453 : because emit_local_var always treats the
14454 : initializer as a full-expression. Thus, the
14455 : destructor would run too early; it would run at the
14456 : end of initializing the reference variable, rather
14457 : than at the end of the block enclosing the
14458 : reference variable.
14459 :
14460 : The solution is to pass back a cleanup expression
14461 : which the caller is responsible for attaching to
14462 : the statement tree. */
14463 : }
14464 : else
14465 : {
14466 688 : rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
14467 688 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
14468 : {
14469 88 : if (CP_DECL_THREAD_LOCAL_P (var))
14470 36 : tls_aggregates = tree_cons (NULL_TREE, var,
14471 : tls_aggregates);
14472 : else
14473 52 : static_aggregates = tree_cons (NULL_TREE, var,
14474 : static_aggregates);
14475 : }
14476 : else
14477 : /* Check whether the dtor is callable. */
14478 600 : cxx_maybe_build_cleanup (var, tf_warning_or_error);
14479 : }
14480 : /* Avoid -Wunused-variable warning (c++/38958). */
14481 24266 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
14482 24266 : && VAR_P (decl))
14483 19445 : TREE_USED (decl) = DECL_READ_P (decl) = true;
14484 :
14485 24266 : *initp = init;
14486 24266 : return var;
14487 : }
14488 :
14489 : /* Convert EXPR to the indicated reference TYPE, in a way suitable for
14490 : initializing a variable of that TYPE. */
14491 :
14492 : tree
14493 7864967 : initialize_reference (tree type, tree expr,
14494 : int flags, tsubst_flags_t complain)
14495 : {
14496 7864967 : conversion *conv;
14497 7864967 : location_t loc = cp_expr_loc_or_input_loc (expr);
14498 :
14499 7864967 : if (type == error_mark_node || error_operand_p (expr))
14500 : return error_mark_node;
14501 :
14502 7864899 : conversion_obstack_sentinel cos;
14503 :
14504 7864899 : conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
14505 : flags, complain);
14506 : /* If this conversion failed, we're in C++20, and we have something like
14507 : A& a(b) where A is an aggregate, try again, this time as A& a{b}. */
14508 7864899 : if ((!conv || conv->bad_p)
14509 667 : && (flags & LOOKUP_AGGREGATE_PAREN_INIT))
14510 : {
14511 4 : tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
14512 4 : CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
14513 4 : CONSTRUCTOR_IS_PAREN_INIT (e) = true;
14514 4 : conversion *c = reference_binding (type, TREE_TYPE (e), e,
14515 : /*c_cast_p=*/false, flags, complain);
14516 : /* If this worked, use it. */
14517 4 : if (c && !c->bad_p)
14518 : expr = e, conv = c;
14519 : }
14520 7865487 : if (!conv || conv->bad_p)
14521 : {
14522 664 : if (complain & tf_error)
14523 : {
14524 349 : if (conv)
14525 303 : convert_like (conv, expr, complain);
14526 46 : else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
14527 14 : && !TYPE_REF_IS_RVALUE (type)
14528 60 : && !lvalue_p (expr))
14529 6 : error_at (loc, "invalid initialization of non-const reference of "
14530 : "type %qH from an rvalue of type %qI",
14531 6 : type, TREE_TYPE (expr));
14532 : else
14533 40 : error_at (loc, "invalid initialization of reference of type "
14534 : "%qH from expression of type %qI", type,
14535 40 : TREE_TYPE (expr));
14536 : }
14537 664 : return error_mark_node;
14538 : }
14539 :
14540 7864235 : if (conv->kind == ck_ref_bind)
14541 : /* Perform the conversion. */
14542 7864232 : expr = convert_like (conv, expr, complain);
14543 3 : else if (conv->kind == ck_ambig)
14544 : /* We gave an error in build_user_type_conversion_1. */
14545 3 : expr = error_mark_node;
14546 : else
14547 0 : gcc_unreachable ();
14548 :
14549 : return expr;
14550 7864899 : }
14551 :
14552 : /* Return true if T is std::pair<const T&, const T&>. */
14553 :
14554 : static bool
14555 575830 : std_pair_ref_ref_p (tree t)
14556 : {
14557 : /* First, check if we have std::pair. */
14558 55108 : if (!NON_UNION_CLASS_TYPE_P (t)
14559 630478 : || !CLASSTYPE_TEMPLATE_INSTANTIATION (t))
14560 : return false;
14561 20433 : tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (t));
14562 20433 : if (!decl_in_std_namespace_p (tdecl))
14563 : return false;
14564 14884 : tree name = DECL_NAME (tdecl);
14565 14884 : if (!name || !id_equal (name, "pair"))
14566 : return false;
14567 :
14568 : /* Now see if the template arguments are both const T&. */
14569 351 : tree args = CLASSTYPE_TI_ARGS (t);
14570 351 : if (TREE_VEC_LENGTH (args) != 2)
14571 : return false;
14572 411 : for (int i = 0; i < 2; i++)
14573 441 : if (!TYPE_REF_OBJ_P (TREE_VEC_ELT (args, i))
14574 441 : || !CP_TYPE_CONST_P (TREE_TYPE (TREE_VEC_ELT (args, i))))
14575 321 : return false;
14576 :
14577 : return true;
14578 : }
14579 :
14580 : /* Return true if a class T has a reference member. */
14581 :
14582 : static bool
14583 216 : class_has_reference_member_p (tree t)
14584 : {
14585 216 : for (tree fields = TYPE_FIELDS (t);
14586 3380 : fields;
14587 3164 : fields = DECL_CHAIN (fields))
14588 3227 : if (TREE_CODE (fields) == FIELD_DECL
14589 196 : && !DECL_ARTIFICIAL (fields)
14590 3394 : && TYPE_REF_P (TREE_TYPE (fields)))
14591 : return true;
14592 : return false;
14593 : }
14594 :
14595 : /* A wrapper for the above suitable as a callback for dfs_walk_once. */
14596 :
14597 : static tree
14598 216 : class_has_reference_member_p_r (tree binfo, void *)
14599 : {
14600 216 : return (class_has_reference_member_p (BINFO_TYPE (binfo))
14601 216 : ? integer_one_node : NULL_TREE);
14602 : }
14603 :
14604 :
14605 : /* Return true if T (either a class or a function) has been marked as
14606 : not-dangling. */
14607 :
14608 : static bool
14609 1526 : no_dangling_p (tree t)
14610 : {
14611 1526 : t = lookup_attribute ("no_dangling", TYPE_ATTRIBUTES (t));
14612 1526 : if (!t)
14613 : return false;
14614 :
14615 75 : t = TREE_VALUE (t);
14616 75 : if (!t)
14617 : return true;
14618 :
14619 51 : t = build_converted_constant_bool_expr (TREE_VALUE (t), tf_warning_or_error);
14620 51 : t = cxx_constant_value (t);
14621 51 : return t == boolean_true_node;
14622 : }
14623 :
14624 : /* Return true if a class CTYPE is either std::reference_wrapper or
14625 : std::ref_view, or a reference wrapper class. We consider a class
14626 : a reference wrapper class if it has a reference member. We no
14627 : longer check that it has a constructor taking the same reference type
14628 : since that approach still generated too many false positives. */
14629 :
14630 : static bool
14631 446 : reference_like_class_p (tree ctype)
14632 : {
14633 446 : if (!CLASS_TYPE_P (ctype))
14634 : return false;
14635 :
14636 314 : if (no_dangling_p (ctype))
14637 : return true;
14638 :
14639 : /* Also accept a std::pair<const T&, const T&>. */
14640 290 : if (std_pair_ref_ref_p (ctype))
14641 : return true;
14642 :
14643 275 : tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (ctype));
14644 275 : if (decl_in_std_namespace_p (tdecl))
14645 : {
14646 38 : tree name = DECL_NAME (tdecl);
14647 38 : if (name
14648 38 : && (id_equal (name, "reference_wrapper")
14649 26 : || id_equal (name, "span")
14650 11 : || id_equal (name, "ref_view")))
14651 : return true;
14652 : }
14653 :
14654 : /* Avoid warning if CTYPE looks like std::span: it has a T* member and
14655 : a trivial destructor. For example,
14656 :
14657 : template<typename T>
14658 : struct Span {
14659 : T* data_;
14660 : std::size len_;
14661 : };
14662 :
14663 : is considered std::span-like. */
14664 248 : if (NON_UNION_CLASS_TYPE_P (ctype) && TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
14665 228 : for (tree field = next_aggregate_field (TYPE_FIELDS (ctype));
14666 367 : field; field = next_aggregate_field (DECL_CHAIN (field)))
14667 199 : if (TYPE_PTR_P (TREE_TYPE (field)))
14668 : return true;
14669 :
14670 : /* Some classes, such as std::tuple, have the reference member in its
14671 : (non-direct) base class. */
14672 188 : if (dfs_walk_once (TYPE_BINFO (ctype), class_has_reference_member_p_r,
14673 : nullptr, nullptr))
14674 : return true;
14675 :
14676 : return false;
14677 : }
14678 :
14679 : /* Helper for maybe_warn_dangling_reference to find a problematic temporary
14680 : in EXPR (as outlined in maybe_warn_dangling_reference), or NULL_TREE
14681 : if none found. For instance:
14682 :
14683 : const S& s = S().self(); // S()
14684 : const int& r = (42, f(1)); // temporary for passing 1 to f
14685 : const int& t = b ? f(1) : f(2); // temporary for 1
14686 : const int& u = b ? f(1) : f(g); // temporary for 1
14687 : const int& v = b ? f(g) : f(2); // temporary for 2
14688 : const int& w = b ? f(g) : f(g); // NULL_TREE
14689 : const int& y = (f(1), 42); // NULL_TREE
14690 : const int& z = f(f(1)); // temporary for 1
14691 :
14692 : EXPR is the initializer. If ARG_P is true, we're processing an argument
14693 : to a function; the point is to distinguish between, for example,
14694 :
14695 : Ref::inner (&TARGET_EXPR <D.2839, F::foo (fm)>)
14696 :
14697 : where we shouldn't warn, and
14698 :
14699 : Ref::inner (&TARGET_EXPR <D.2908, F::foo (&TARGET_EXPR <...>)>)
14700 :
14701 : where we should warn (Ref is a reference_like_class_p so we see through
14702 : it. */
14703 :
14704 : static tree
14705 3818 : do_warn_dangling_reference (tree expr, bool arg_p)
14706 : {
14707 3818 : STRIP_NOPS (expr);
14708 :
14709 3818 : if (arg_p && expr_represents_temporary_p (expr))
14710 : {
14711 : /* An attempt to reduce the number of -Wdangling-reference
14712 : false positives concerning reference wrappers (c++/107532).
14713 : When we encounter a reference_like_class_p, we don't warn
14714 : just yet; instead, we keep recursing to see if there were
14715 : any temporaries behind the reference-wrapper class. */
14716 : tree e = expr;
14717 354 : while (handled_component_p (e))
14718 15 : e = TREE_OPERAND (e, 0);
14719 339 : tree type = TREE_TYPE (e);
14720 : /* If the temporary represents a lambda, we don't really know
14721 : what's going on here. */
14722 461 : if (!reference_like_class_p (type) && !LAMBDA_TYPE_P (type))
14723 : return expr;
14724 : }
14725 :
14726 3586 : switch (TREE_CODE (expr))
14727 : {
14728 1793 : case CALL_EXPR:
14729 1793 : {
14730 1793 : tree fndecl = cp_get_callee_fndecl_nofold (expr);
14731 1793 : if (!fndecl
14732 1793 : || warning_suppressed_p (fndecl, OPT_Wdangling_reference)
14733 1782 : || !warning_enabled_at (DECL_SOURCE_LOCATION (fndecl),
14734 1782 : OPT_Wdangling_reference)
14735 : /* Don't emit a false positive for:
14736 : std::vector<int> v = ...;
14737 : std::vector<int>::const_iterator it = v.begin();
14738 : const int &r = *it++;
14739 : because R refers to one of the int elements of V, not to
14740 : a temporary object. Member operator* may return a reference
14741 : but probably not to one of its arguments. */
14742 1423 : || (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl)
14743 849 : && DECL_OVERLOADED_OPERATOR_P (fndecl)
14744 320 : && DECL_OVERLOADED_OPERATOR_IS (fndecl, INDIRECT_REF))
14745 3005 : || no_dangling_p (TREE_TYPE (fndecl)))
14746 605 : return NULL_TREE;
14747 :
14748 1188 : tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
14749 : /* If the function doesn't return a reference, don't warn. This
14750 : can be e.g.
14751 : const int& z = std::min({1, 2, 3, 4, 5, 6, 7});
14752 : which doesn't dangle: std::min here returns an int.
14753 :
14754 : If the function returns a std::pair<const T&, const T&>, we
14755 : warn, to detect e.g.
14756 : std::pair<const int&, const int&> v = std::minmax(1, 2);
14757 : which also creates a dangling reference, because std::minmax
14758 : returns std::pair<const T&, const T&>(b, a). */
14759 1188 : if (!(TYPE_REF_OBJ_P (rettype) || reference_like_class_p (rettype)))
14760 : return NULL_TREE;
14761 :
14762 : /* Here we're looking to see if any of the arguments is a temporary
14763 : initializing a reference parameter. */
14764 1592 : for (int i = 0; i < call_expr_nargs (expr); ++i)
14765 : {
14766 1210 : tree arg = CALL_EXPR_ARG (expr, i);
14767 : /* Check that this argument initializes a reference, except for
14768 : the argument initializing the object of a member function. */
14769 1210 : if (!DECL_IOBJ_MEMBER_FUNCTION_P (fndecl)
14770 1210 : && !TYPE_REF_P (TREE_TYPE (arg)))
14771 130 : continue;
14772 1080 : STRIP_NOPS (arg);
14773 1080 : if (TREE_CODE (arg) == ADDR_EXPR)
14774 699 : arg = TREE_OPERAND (arg, 0);
14775 : /* Recurse to see if the argument is a temporary. It could also
14776 : be another call taking a temporary and returning it and
14777 : initializing this reference parameter. */
14778 1080 : if ((arg = do_warn_dangling_reference (arg, /*arg_p=*/true)))
14779 : {
14780 : /* If we know the temporary could not bind to the return type,
14781 : don't warn. This is for scalars and empty classes only
14782 : because for other classes we can't be sure we are not
14783 : returning its sub-object. */
14784 277 : if ((SCALAR_TYPE_P (TREE_TYPE (arg))
14785 152 : || is_empty_class (TREE_TYPE (arg)))
14786 152 : && TYPE_REF_P (rettype)
14787 137 : && !reference_related_p (TREE_TYPE (rettype),
14788 137 : TREE_TYPE (arg)))
14789 21 : continue;
14790 256 : return arg;
14791 : }
14792 : /* Don't warn about member functions like:
14793 : std::any a(...);
14794 : S& s = a.emplace<S>({0}, 0);
14795 : which construct a new object and return a reference to it, but
14796 : we still want to detect:
14797 : struct S { const S& self () { return *this; } };
14798 : const S& s = S().self();
14799 : where 's' dangles. If we've gotten here, the object this function
14800 : is invoked on is not a temporary. */
14801 803 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl))
14802 : break;
14803 : }
14804 : return NULL_TREE;
14805 : }
14806 28 : case COMPOUND_EXPR:
14807 28 : return do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p);
14808 31 : case COND_EXPR:
14809 31 : if (tree t = do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p))
14810 : return t;
14811 16 : return do_warn_dangling_reference (TREE_OPERAND (expr, 2), arg_p);
14812 0 : case PAREN_EXPR:
14813 0 : return do_warn_dangling_reference (TREE_OPERAND (expr, 0), arg_p);
14814 122 : case TARGET_EXPR:
14815 122 : return do_warn_dangling_reference (TARGET_EXPR_INITIAL (expr), arg_p);
14816 : default:
14817 : return NULL_TREE;
14818 : }
14819 : }
14820 :
14821 : /* Implement -Wdangling-reference, to detect cases like
14822 :
14823 : int n = 1;
14824 : const int& r = std::max(n - 1, n + 1); // r is dangling
14825 :
14826 : This creates temporaries from the arguments, returns a reference to
14827 : one of the temporaries, but both temporaries are destroyed at the end
14828 : of the full expression.
14829 :
14830 : This works by checking if a reference is initialized with a function
14831 : that returns a reference, and at least one parameter of the function
14832 : is a reference that is bound to a temporary. It assumes that such a
14833 : function actually returns one of its arguments.
14834 :
14835 : DECL is the reference being initialized, INIT is the initializer. */
14836 :
14837 : static void
14838 120424185 : maybe_warn_dangling_reference (const_tree decl, tree init)
14839 : {
14840 120424185 : if (!warn_dangling_reference)
14841 : return;
14842 578066 : tree type = TREE_TYPE (decl);
14843 : /* Only warn if what we're initializing has type T&& or const T&, or
14844 : std::pair<const T&, const T&>. (A non-const lvalue reference can't
14845 : bind to a temporary.) */
14846 1153606 : if (!((TYPE_REF_OBJ_P (type)
14847 5442 : && (TYPE_REF_IS_RVALUE (type)
14848 5034 : || CP_TYPE_CONST_P (TREE_TYPE (type))))
14849 575540 : || std_pair_ref_ref_p (type)))
14850 : return;
14851 : /* Don't suppress the diagnostic just because the call comes from
14852 : a system header. If the DECL is not in a system header, or if
14853 : -Wsystem-headers was provided, warn. */
14854 2541 : auto wsh
14855 2541 : = make_temp_override (global_dc->m_warn_system_headers,
14856 2541 : (!in_system_header_at (DECL_SOURCE_LOCATION (decl))
14857 2541 : || global_dc->m_warn_system_headers));
14858 2541 : if (tree call = do_warn_dangling_reference (init, /*arg_p=*/false))
14859 : {
14860 211 : auto_diagnostic_group d;
14861 211 : if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wdangling_reference,
14862 : "possibly dangling reference to a temporary"))
14863 211 : inform (EXPR_LOCATION (call), "%qT temporary created here",
14864 211 : TREE_TYPE (call));
14865 211 : }
14866 2541 : }
14867 :
14868 : /* If *P is an xvalue expression, prevent temporary lifetime extension if it
14869 : gets used to initialize a reference. */
14870 :
14871 : static tree
14872 89 : prevent_lifetime_extension (tree t)
14873 : {
14874 89 : tree *p = &t;
14875 89 : while (TREE_CODE (*p) == COMPOUND_EXPR)
14876 0 : p = &TREE_OPERAND (*p, 1);
14877 98 : while (handled_component_p (*p))
14878 9 : p = &TREE_OPERAND (*p, 0);
14879 : /* Change a TARGET_EXPR from prvalue to xvalue. */
14880 89 : if (TREE_CODE (*p) == TARGET_EXPR)
14881 3 : *p = build2 (COMPOUND_EXPR, TREE_TYPE (*p), *p,
14882 3 : move (TARGET_EXPR_SLOT (*p)));
14883 89 : return t;
14884 : }
14885 :
14886 : /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
14887 : which is bound either to a reference or a std::initializer_list. */
14888 :
14889 : static tree
14890 1727711 : extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups,
14891 : tree *cond_guard)
14892 : {
14893 : /* CWG1299 (C++20): The temporary object to which the reference is bound or
14894 : the temporary object that is the complete object of a subobject to which
14895 : the reference is bound persists for the lifetime of the reference if the
14896 : glvalue to which the reference is bound was obtained through one of the
14897 : following:
14898 : - a temporary materialization conversion ([conv.rval]),
14899 : - ( expression ), where expression is one of these expressions,
14900 : - subscripting ([expr.sub]) of an array operand, where that operand is one
14901 : of these expressions,
14902 : - a class member access ([expr.ref]) using the . operator where the left
14903 : operand is one of these expressions and the right operand designates a
14904 : non-static data member of non-reference type,
14905 : - a pointer-to-member operation ([expr.mptr.oper]) using the .* operator
14906 : where the left operand is one of these expressions and the right operand
14907 : is a pointer to data member of non-reference type,
14908 : - a const_cast ([expr.const.cast]), static_cast ([expr.static.cast]),
14909 : dynamic_cast ([expr.dynamic.cast]), or reinterpret_cast
14910 : ([expr.reinterpret.cast]) converting, without a user-defined conversion,
14911 : a glvalue operand that is one of these expressions to a glvalue that
14912 : refers to the object designated by the operand, or to its complete
14913 : object or a subobject thereof,
14914 : - a conditional expression ([expr.cond]) that is a glvalue where the
14915 : second or third operand is one of these expressions, or
14916 : - a comma expression ([expr.comma]) that is a glvalue where the right
14917 : operand is one of these expressions. */
14918 :
14919 : /* FIXME several cases are still handled wrong (101572, 81420). */
14920 :
14921 1727711 : tree sub = init;
14922 1727711 : tree *p;
14923 1727711 : STRIP_NOPS (sub);
14924 1727711 : if (TREE_CODE (sub) == COMPOUND_EXPR)
14925 : {
14926 242 : TREE_OPERAND (sub, 1)
14927 242 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups,
14928 : cond_guard);
14929 242 : return init;
14930 : }
14931 1727469 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14932 1727469 : && TYPE_PTRDATAMEM_P (TREE_TYPE (tree_strip_nop_conversions
14933 : (TREE_OPERAND (sub, 1)))))
14934 : {
14935 : /* A pointer-to-member operation. */
14936 6 : TREE_OPERAND (sub, 0)
14937 6 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 0), cleanups,
14938 : cond_guard);
14939 6 : return init;
14940 : }
14941 1727463 : if (TREE_CODE (sub) == COND_EXPR)
14942 : {
14943 22082 : tree cur_cond_guard = NULL_TREE;
14944 22082 : if (TREE_OPERAND (sub, 1))
14945 22082 : TREE_OPERAND (sub, 1)
14946 44164 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups,
14947 : &cur_cond_guard);
14948 22082 : if (cur_cond_guard)
14949 : {
14950 9 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard,
14951 : NOP_EXPR, boolean_true_node,
14952 : tf_warning_or_error);
14953 9 : TREE_OPERAND (sub, 1)
14954 18 : = cp_build_compound_expr (set, TREE_OPERAND (sub, 1),
14955 : tf_warning_or_error);
14956 : }
14957 22082 : cur_cond_guard = NULL_TREE;
14958 22082 : if (TREE_OPERAND (sub, 2))
14959 22082 : TREE_OPERAND (sub, 2)
14960 44164 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 2), cleanups,
14961 : &cur_cond_guard);
14962 22082 : if (cur_cond_guard)
14963 : {
14964 12 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard,
14965 : NOP_EXPR, boolean_true_node,
14966 : tf_warning_or_error);
14967 12 : TREE_OPERAND (sub, 2)
14968 24 : = cp_build_compound_expr (set, TREE_OPERAND (sub, 2),
14969 : tf_warning_or_error);
14970 : }
14971 22082 : return init;
14972 : }
14973 1705381 : if (TREE_CODE (sub) != ADDR_EXPR)
14974 : return init;
14975 : /* Deal with binding to a subobject. */
14976 1101059 : for (p = &TREE_OPERAND (sub, 0);
14977 1252038 : TREE_CODE (*p) == COMPONENT_REF || TREE_CODE (*p) == ARRAY_REF; )
14978 150979 : p = &TREE_OPERAND (*p, 0);
14979 1101059 : if (TREE_CODE (*p) == TARGET_EXPR)
14980 : {
14981 22829 : tree subinit = NULL_TREE;
14982 22829 : *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit,
14983 : cond_guard, nullptr);
14984 22829 : recompute_tree_invariant_for_addr_expr (sub);
14985 22829 : if (init != sub)
14986 22811 : init = fold_convert (TREE_TYPE (init), sub);
14987 22829 : if (subinit)
14988 21884 : init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
14989 : }
14990 : return init;
14991 : }
14992 :
14993 : /* Data for extend_temps_r, mostly matching the parameters of
14994 : extend_ref_init_temps. */
14995 :
14996 : struct extend_temps_data
14997 : {
14998 : tree decl;
14999 : tree init;
15000 : vec<tree, va_gc> **cleanups;
15001 : tree* cond_guard;
15002 : hash_set<tree> *pset; // For avoiding redundant walk_tree.
15003 : hash_map<tree, tree> *var_map; // For remapping extended temps.
15004 : };
15005 :
15006 : /* Tree walk function for extend_all_temps. Generally parallel to
15007 : extend_ref_init_temps_1, but adapted for walk_tree. */
15008 :
15009 : tree
15010 67220 : extend_temps_r (tree *tp, int *walk_subtrees, void *data)
15011 : {
15012 67220 : extend_temps_data *d = (extend_temps_data *)data;
15013 :
15014 67220 : if (TREE_CODE (*tp) == VAR_DECL)
15015 : {
15016 8603 : if (tree *r = d->var_map->get (*tp))
15017 0 : *tp = *r;
15018 8603 : return NULL_TREE;
15019 : }
15020 :
15021 58602 : if (TYPE_P (*tp) || TREE_CODE (*tp) == CLEANUP_POINT_EXPR
15022 117218 : || d->pset->add (*tp))
15023 : {
15024 3496 : *walk_subtrees = 0;
15025 3496 : return NULL_TREE;
15026 : }
15027 :
15028 55121 : if (TREE_CODE (*tp) == COND_EXPR)
15029 : {
15030 8 : cp_walk_tree (&TREE_OPERAND (*tp, 0), extend_temps_r, d, nullptr);
15031 :
15032 24 : auto walk_arm = [d](tree &op)
15033 : {
15034 16 : tree cur_cond_guard = NULL_TREE;
15035 16 : auto ov = make_temp_override (d->cond_guard, &cur_cond_guard);
15036 16 : cp_walk_tree (&op, extend_temps_r, d, nullptr);
15037 16 : if (cur_cond_guard)
15038 : {
15039 2 : tree set = build2 (MODIFY_EXPR, boolean_type_node,
15040 : cur_cond_guard, boolean_true_node);
15041 2 : op = cp_build_compound_expr (set, op, tf_none);
15042 : }
15043 24 : };
15044 8 : walk_arm (TREE_OPERAND (*tp, 1));
15045 8 : walk_arm (TREE_OPERAND (*tp, 2));
15046 :
15047 8 : *walk_subtrees = 0;
15048 8 : return NULL_TREE;
15049 : }
15050 :
15051 55113 : tree *p = tp;
15052 :
15053 55113 : if (TREE_CODE (*tp) == ADDR_EXPR)
15054 12545 : for (p = &TREE_OPERAND (*tp, 0);
15055 13583 : TREE_CODE (*p) == COMPONENT_REF || TREE_CODE (*p) == ARRAY_REF; )
15056 1038 : p = &TREE_OPERAND (*p, 0);
15057 :
15058 55113 : if (TREE_CODE (*p) == TARGET_EXPR
15059 : /* An eliding TARGET_EXPR isn't a temporary at all. */
15060 2297 : && !TARGET_EXPR_ELIDING_P (*p)
15061 : /* A TARGET_EXPR with TARGET_EXPR_INTERNAL_P is an artificial variable
15062 : used during initialization that need not be extended. */
15063 56850 : && !TARGET_EXPR_INTERNAL_P (*p))
15064 : {
15065 : /* A CLEANUP_EH_ONLY expr should also have TARGET_EXPR_INTERNAL_P. */
15066 1437 : gcc_checking_assert (!CLEANUP_EH_ONLY (*p));
15067 :
15068 1437 : tree subinit = NULL_TREE;
15069 1437 : tree slot = TARGET_EXPR_SLOT (*p);
15070 1437 : *p = set_up_extended_ref_temp (d->decl, *p, d->cleanups, &subinit,
15071 : d->cond_guard, d);
15072 1437 : if (TREE_CODE (*tp) == ADDR_EXPR)
15073 1423 : recompute_tree_invariant_for_addr_expr (*tp);
15074 1437 : if (subinit)
15075 1345 : *tp = cp_build_compound_expr (subinit, *tp, tf_none);
15076 1437 : d->var_map->put (slot, *p);
15077 : }
15078 :
15079 : return NULL_TREE;
15080 : }
15081 :
15082 : /* Extend all the temporaries in a for-range-initializer. */
15083 :
15084 : static tree
15085 13379 : extend_all_temps (tree decl, tree init, vec<tree, va_gc> **cleanups)
15086 : {
15087 13379 : hash_set<tree> pset;
15088 13379 : hash_map<tree, tree> map;
15089 13379 : gcc_assert (!TREE_STATIC (decl));
15090 13379 : extend_temps_data d = { decl, init, cleanups, nullptr, &pset, &map };
15091 13379 : cp_walk_tree (&init, extend_temps_r, &d, nullptr);
15092 13379 : return init;
15093 13379 : }
15094 :
15095 : /* INIT is part of the initializer for DECL. If there are any
15096 : reference or initializer lists being initialized, extend their
15097 : lifetime to match that of DECL. */
15098 :
15099 : tree
15100 122684229 : extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups,
15101 : tree *cond_guard)
15102 : {
15103 122684229 : tree type = TREE_TYPE (init);
15104 122684229 : if (processing_template_decl)
15105 : return init;
15106 :
15107 : /* P2718R0 - in C++23 for-range-initializer, extend all temps. */
15108 120437564 : if (DECL_NAME (decl) == for_range__identifier
15109 112169 : && flag_range_for_ext_temps
15110 : /* Iterating expansion statement decl is static right now, but that
15111 : could change depending on CWG3044 and CWG3043. */
15112 120450977 : && !TREE_STATIC (decl))
15113 : {
15114 13379 : gcc_checking_assert (!cond_guard);
15115 13379 : return extend_all_temps (decl, init, cleanups);
15116 : }
15117 :
15118 120424185 : maybe_warn_dangling_reference (decl, init);
15119 :
15120 120424185 : if (TYPE_REF_P (type))
15121 1682720 : init = extend_ref_init_temps_1 (decl, init, cleanups, cond_guard);
15122 : else
15123 : {
15124 118741465 : tree ctor = init;
15125 118741465 : if (TREE_CODE (ctor) == TARGET_EXPR)
15126 2368195 : ctor = TARGET_EXPR_INITIAL (ctor);
15127 118741465 : if (TREE_CODE (ctor) == CONSTRUCTOR)
15128 : {
15129 : /* [dcl.init] When initializing an aggregate from a parenthesized list
15130 : of values... a temporary object bound to a reference does not have
15131 : its lifetime extended. */
15132 7004481 : if (CONSTRUCTOR_IS_PAREN_INIT (ctor))
15133 : return init;
15134 :
15135 7004117 : if (is_std_init_list (type))
15136 : {
15137 : /* The temporary array underlying a std::initializer_list
15138 : is handled like a reference temporary. */
15139 579 : tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
15140 579 : array = extend_ref_init_temps_1 (decl, array, cleanups,
15141 : cond_guard);
15142 579 : CONSTRUCTOR_ELT (ctor, 0)->value = array;
15143 : }
15144 : else
15145 : {
15146 7003538 : unsigned i;
15147 7003538 : constructor_elt *p;
15148 7003538 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
15149 68227192 : FOR_EACH_VEC_SAFE_ELT (elts, i, p)
15150 61223654 : p->value = extend_ref_init_temps (decl, p->value, cleanups,
15151 : cond_guard);
15152 : }
15153 7004117 : recompute_constructor_flags (ctor);
15154 7004117 : if (decl_maybe_constant_var_p (decl) && TREE_CONSTANT (ctor))
15155 2689236 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
15156 : }
15157 : }
15158 :
15159 : return init;
15160 : }
15161 :
15162 : /* Returns true iff an initializer for TYPE could contain temporaries that
15163 : need to be extended because they are bound to references or
15164 : std::initializer_list. */
15165 :
15166 : bool
15167 5194 : type_has_extended_temps (tree type)
15168 : {
15169 5194 : type = strip_array_types (type);
15170 5194 : if (TYPE_REF_P (type))
15171 : return true;
15172 5137 : if (CLASS_TYPE_P (type))
15173 : {
15174 2797 : if (is_std_init_list (type))
15175 : return true;
15176 2794 : for (tree f = next_aggregate_field (TYPE_FIELDS (type));
15177 6579 : f; f = next_aggregate_field (DECL_CHAIN (f)))
15178 3914 : if (type_has_extended_temps (TREE_TYPE (f)))
15179 : return true;
15180 : }
15181 : return false;
15182 : }
15183 :
15184 : /* Returns true iff TYPE is some variant of std::initializer_list. */
15185 :
15186 : bool
15187 187722011 : is_std_init_list (tree type)
15188 : {
15189 187722011 : if (!TYPE_P (type))
15190 : return false;
15191 187721988 : if (cxx_dialect == cxx98)
15192 : return false;
15193 : /* Look through typedefs. */
15194 187332542 : type = TYPE_MAIN_VARIANT (type);
15195 151843819 : return (CLASS_TYPE_P (type)
15196 151707981 : && CP_TYPE_CONTEXT (type) == std_node
15197 269499779 : && init_list_identifier == DECL_NAME (TYPE_NAME (type)));
15198 : }
15199 :
15200 : /* Returns true iff DECL is a list constructor: i.e. a constructor which
15201 : will accept an argument list of a single std::initializer_list<T>. */
15202 :
15203 : bool
15204 160836172 : is_list_ctor (tree decl)
15205 : {
15206 160836172 : tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
15207 160836172 : tree arg;
15208 :
15209 160836172 : if (!args || args == void_list_node)
15210 : return false;
15211 :
15212 128942063 : arg = non_reference (TREE_VALUE (args));
15213 128942063 : if (!is_std_init_list (arg))
15214 : return false;
15215 :
15216 2174594 : args = TREE_CHAIN (args);
15217 :
15218 3685223 : if (args && args != void_list_node && !TREE_PURPOSE (args))
15219 : /* There are more non-defaulted parms. */
15220 : return false;
15221 :
15222 : return true;
15223 : }
15224 :
15225 : /* We know that can_convert_arg_bad already said "no" when trying to convert
15226 : FROM to TO with ARG and FLAGS. Try to figure out if it was because
15227 : an explicit conversion function was skipped when looking for a way to
15228 : perform the conversion. At this point we've already printed an error. */
15229 :
15230 : void
15231 2055 : maybe_show_nonconverting_candidate (tree to, tree from, tree arg, int flags)
15232 : {
15233 2055 : if (!(flags & LOOKUP_ONLYCONVERTING))
15234 287 : return;
15235 :
15236 1768 : conversion_obstack_sentinel cos;
15237 1768 : conversion *c = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
15238 : flags & ~LOOKUP_ONLYCONVERTING, tf_none);
15239 1768 : if (c && !c->bad_p && c->user_conv_p)
15240 : /* Ay, the conversion would have worked in direct-init context. */
15241 778 : for (; c; c = next_conversion (c))
15242 521 : if (c->kind == ck_user
15243 254 : && DECL_P (c->cand->fn)
15244 775 : && DECL_NONCONVERTING_P (c->cand->fn))
15245 250 : inform (DECL_SOURCE_LOCATION (c->cand->fn), "explicit conversion "
15246 : "function was not considered");
15247 1768 : }
15248 :
15249 : /* We're converting EXPR to TYPE. If that conversion involves a conversion
15250 : function and we're binding EXPR to a reference parameter of that function,
15251 : return true. */
15252 :
15253 : bool
15254 171 : conv_binds_to_reference_parm_p (tree type, tree expr)
15255 : {
15256 171 : conversion_obstack_sentinel cos;
15257 171 : conversion *c = implicit_conversion (type, TREE_TYPE (expr), expr,
15258 : /*c_cast_p=*/false, LOOKUP_NORMAL,
15259 : tf_none);
15260 171 : if (c && !c->bad_p && c->user_conv_p)
15261 117 : for (; c; c = next_conversion (c))
15262 81 : if (c->kind == ck_user)
15263 244 : for (z_candidate *cand = c->cand; cand; cand = cand->next)
15264 208 : if (cand->viable == 1)
15265 81 : for (size_t i = 0; i < cand->num_convs; ++i)
15266 45 : if (cand->convs[i]->kind == ck_ref_bind
15267 45 : && conv_get_original_expr (cand->convs[i]) == expr)
15268 : return true;
15269 :
15270 : return false;
15271 171 : }
15272 :
15273 : #include "gt-cp-call.h"
|