Line data Source code
1 : /* Functions related to invoking C++ methods and overloaded functions.
2 : Copyright (C) 1987-2026 Free Software Foundation, Inc.
3 : Contributed by Michael Tiemann (tiemann@cygnus.com) and
4 : modified by Brendan Kehoe (brendan@cygnus.com).
5 :
6 : This file is part of GCC.
7 :
8 : GCC is free software; you can redistribute it and/or modify
9 : it under the terms of the GNU General Public License as published by
10 : the Free Software Foundation; either version 3, or (at your option)
11 : any later version.
12 :
13 : GCC is distributed in the hope that it will be useful,
14 : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : GNU General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with GCC; see the file COPYING3. If not see
20 : <http://www.gnu.org/licenses/>. */
21 :
22 :
23 : /* High-level class interface. */
24 :
25 : #include "config.h"
26 : #include "system.h"
27 : #include "coretypes.h"
28 : #include "target.h"
29 : #include "cp-tree.h"
30 : #include "timevar.h"
31 : #include "stringpool.h"
32 : #include "cgraph.h"
33 : #include "stor-layout.h"
34 : #include "trans-mem.h"
35 : #include "flags.h"
36 : #include "toplev.h"
37 : #include "intl.h"
38 : #include "convert.h"
39 : #include "langhooks.h"
40 : #include "c-family/c-objc.h"
41 : #include "internal-fn.h"
42 : #include "stringpool.h"
43 : #include "attribs.h"
44 : #include "decl.h"
45 : #include "c-family/c-type-mismatch.h"
46 : #include "tristate.h"
47 : #include "tree-pretty-print-markup.h"
48 : #include "contracts.h" // maybe_contract_wrap_call
49 :
50 : /* The various kinds of conversion. */
51 :
52 : enum conversion_kind {
53 : ck_identity,
54 : ck_lvalue,
55 : ck_fnptr,
56 : ck_qual,
57 : ck_std,
58 : ck_ptr,
59 : ck_pmem,
60 : ck_base,
61 : ck_ref_bind,
62 : ck_user,
63 : ck_ambig,
64 : ck_list,
65 : ck_aggr,
66 : ck_rvalue,
67 : /* When LOOKUP_SHORTCUT_BAD_CONVS is set, we may return a conversion of
68 : this kind whenever we know the true conversion is either bad or outright
69 : invalid, but we don't want to attempt to compute the bad conversion (for
70 : sake of avoiding unnecessary instantiation). bad_p should always be set
71 : for these. */
72 : ck_deferred_bad,
73 : };
74 :
75 : /* The rank of the conversion. Order of the enumerals matters; better
76 : conversions should come earlier in the list. */
77 :
78 : enum conversion_rank {
79 : cr_identity,
80 : cr_exact,
81 : cr_promotion,
82 : cr_std,
83 : cr_pbool,
84 : cr_user,
85 : cr_ellipsis,
86 : cr_bad
87 : };
88 :
89 : /* An implicit conversion sequence, in the sense of [over.best.ics].
90 : The first conversion to be performed is at the end of the chain.
91 : That conversion is always a cr_identity conversion. */
92 :
93 : struct conversion {
94 : /* The kind of conversion represented by this step. */
95 : conversion_kind kind;
96 : /* The rank of this conversion. */
97 : conversion_rank rank;
98 : bool user_conv_p : 1;
99 : bool ellipsis_p : 1;
100 : bool this_p : 1;
101 : /* True if this conversion would be permitted with a bending of
102 : language standards, e.g. disregarding pointer qualifiers or
103 : converting integers to pointers. */
104 : bool bad_p : 1;
105 : /* If KIND is ck_ref_bind or ck_base, true to indicate that a
106 : temporary should be created to hold the result of the
107 : conversion. If KIND is ck_ambig or ck_user, true means force
108 : copy-initialization. */
109 : bool need_temporary_p : 1;
110 : /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion
111 : from a pointer-to-derived to pointer-to-base is being performed. */
112 : bool base_p : 1;
113 : /* If KIND is ck_ref_bind, true when either an lvalue reference is
114 : being bound to an lvalue expression or an rvalue reference is
115 : being bound to an rvalue expression. If KIND is ck_rvalue or ck_base,
116 : true when we are treating an lvalue as an rvalue (12.8p33). If
117 : ck_identity, we will be binding a reference directly or decaying to
118 : a pointer. */
119 : bool rvaluedness_matches_p: 1;
120 : bool check_narrowing: 1;
121 : /* Whether check_narrowing should only check TREE_CONSTANTs; used
122 : in build_converted_constant_expr. */
123 : bool check_narrowing_const_only: 1;
124 : /* True if this conversion is taking place in a copy-initialization context
125 : and we should only consider converting constructors. Only set in
126 : ck_base and ck_rvalue. */
127 : bool copy_init_p : 1;
128 : /* The type of the expression resulting from the conversion. */
129 : tree type;
130 : union {
131 : /* The next conversion in the chain. Since the conversions are
132 : arranged from outermost to innermost, the NEXT conversion will
133 : actually be performed before this conversion. This variant is
134 : used only when KIND is neither ck_identity, ck_aggr, ck_ambig nor
135 : ck_list. Please use the next_conversion function instead
136 : of using this field directly. */
137 : conversion *next;
138 : /* The expression at the beginning of the conversion chain. This
139 : variant is used only if KIND is ck_identity, ck_aggr, or ck_ambig.
140 : You can use conv_get_original_expr to get this expression. */
141 : tree expr;
142 : /* The array of conversions for an initializer_list, so this
143 : variant is used only when KIN D is ck_list. */
144 : conversion **list;
145 : } u;
146 : /* The function candidate corresponding to this conversion
147 : sequence. This field is only used if KIND is ck_user. */
148 : struct z_candidate *cand;
149 : };
150 :
151 : #define CONVERSION_RANK(NODE) \
152 : ((NODE)->bad_p ? cr_bad \
153 : : (NODE)->ellipsis_p ? cr_ellipsis \
154 : : (NODE)->user_conv_p ? cr_user \
155 : : (NODE)->rank)
156 :
157 : #define BAD_CONVERSION_RANK(NODE) \
158 : ((NODE)->ellipsis_p ? cr_ellipsis \
159 : : (NODE)->user_conv_p ? cr_user \
160 : : (NODE)->rank)
161 :
162 : static struct obstack conversion_obstack;
163 : static bool conversion_obstack_initialized;
164 : struct rejection_reason;
165 :
166 : static struct z_candidate * tourney (struct z_candidate *, tsubst_flags_t);
167 : static int equal_functions (tree, tree);
168 : static int joust (struct z_candidate *, struct z_candidate *, bool,
169 : tsubst_flags_t);
170 : static int compare_ics (conversion *, conversion *);
171 : static void maybe_warn_class_memaccess (location_t, tree,
172 : const vec<tree, va_gc> *);
173 : static tree build_over_call (struct z_candidate *, int, tsubst_flags_t);
174 : static tree convert_like (conversion *, tree, tsubst_flags_t);
175 : static tree convert_like_with_context (conversion *, tree, tree, int,
176 : tsubst_flags_t);
177 : static void op_error (const op_location_t &, enum tree_code, enum tree_code,
178 : tree, tree, tree, bool);
179 : static struct z_candidate *build_user_type_conversion_1 (tree, tree, int,
180 : tsubst_flags_t);
181 : static void print_z_candidate (location_t, const char *, struct z_candidate *);
182 : static void print_z_candidates (location_t, struct z_candidate *,
183 : tristate = tristate::unknown ());
184 : static tree build_this (tree);
185 : static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
186 : static bool any_strictly_viable (struct z_candidate *);
187 : static struct z_candidate *add_template_candidate
188 : (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
189 : tree, tree, tree, int, unification_kind_t, bool, tsubst_flags_t);
190 : static struct z_candidate *add_template_candidate_real
191 : (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
192 : tree, tree, tree, int, tree, unification_kind_t, bool, tsubst_flags_t);
193 : static bool is_complete (tree);
194 : static struct z_candidate *add_conv_candidate
195 : (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, tree,
196 : tree, tsubst_flags_t);
197 : static struct z_candidate *add_function_candidate
198 : (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *, tree,
199 : tree, int, conversion**, bool, tsubst_flags_t);
200 : static conversion *implicit_conversion (tree, tree, tree, bool, int,
201 : tsubst_flags_t);
202 : static conversion *reference_binding (tree, tree, tree, bool, int,
203 : tsubst_flags_t);
204 : static conversion *build_conv (conversion_kind, tree, conversion *);
205 : static conversion *build_list_conv (tree, tree, int, tsubst_flags_t);
206 : static conversion *next_conversion (conversion *);
207 : static bool is_subseq (conversion *, conversion *);
208 : static conversion *maybe_handle_ref_bind (conversion **);
209 : static void maybe_handle_implicit_object (conversion **);
210 : static struct z_candidate *add_candidate
211 : (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, size_t,
212 : conversion **, tree, tree, int, struct rejection_reason *, int);
213 : static tree source_type (conversion *);
214 : static void add_warning (struct z_candidate *, struct z_candidate *);
215 : static conversion *direct_reference_binding (tree, conversion *);
216 : static bool promoted_arithmetic_type_p (tree);
217 : static conversion *conditional_conversion (tree, tree, tsubst_flags_t);
218 : static char *name_as_c_string (tree, tree, bool *);
219 : static tree prep_operand (tree);
220 : static void add_candidates (tree, tree, const vec<tree, va_gc> *, tree, tree,
221 : bool, tree, tree, int, struct z_candidate **,
222 : tsubst_flags_t);
223 : static conversion *merge_conversion_sequences (conversion *, conversion *);
224 : static tree build_temp (tree, tree, int, enum diagnostics::kind *,
225 : tsubst_flags_t);
226 : static conversion *build_identity_conv (tree, tree);
227 : static inline bool conv_binds_to_array_of_unknown_bound (conversion *);
228 : static bool conv_is_prvalue (conversion *);
229 : static tree prevent_lifetime_extension (tree);
230 :
231 : /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
232 : NAME can take many forms... */
233 :
234 : bool
235 3161441 : check_dtor_name (tree basetype, tree name)
236 : {
237 : /* Just accept something we've already complained about. */
238 3161441 : if (name == error_mark_node)
239 : return true;
240 :
241 3161441 : if (TREE_CODE (name) == TYPE_DECL)
242 84 : name = TREE_TYPE (name);
243 3161357 : 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 3161415 : 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 356702429 : build_addr_func (tree function, tsubst_flags_t complain)
282 : {
283 356702429 : tree type = TREE_TYPE (function);
284 :
285 : /* We have to do these by hand to avoid real pointer to member
286 : functions. */
287 356702429 : if (TREE_CODE (type) == METHOD_TYPE)
288 : {
289 54239489 : if (TREE_CODE (function) == OFFSET_REF)
290 : {
291 73 : tree object = build_address (TREE_OPERAND (function, 0));
292 73 : return get_member_function_from_ptrfunc (&object,
293 73 : TREE_OPERAND (function, 1),
294 : complain);
295 : }
296 54239416 : function = build_address (function);
297 : }
298 302462940 : else if (TREE_CODE (function) == FUNCTION_DECL
299 432020005 : && DECL_IMMEDIATE_FUNCTION_P (function))
300 1923201 : function = build_address (function);
301 : else
302 300539739 : 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 166339 : build_call_n (tree function, int n, ...)
315 : {
316 166339 : if (n == 0)
317 0 : return build_call_a (function, 0, NULL);
318 : else
319 : {
320 166339 : tree *argarray = XALLOCAVEC (tree, n);
321 166339 : va_list ap;
322 166339 : int i;
323 :
324 166339 : va_start (ap, n);
325 333843 : for (i = 0; i < n; i++)
326 167504 : argarray[i] = va_arg (ap, tree);
327 166339 : va_end (ap);
328 166339 : 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 174115987 : set_flags_from_callee (tree call)
337 : {
338 : /* Handle both CALL_EXPRs and AGGR_INIT_EXPRs. */
339 174115987 : 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 174115987 : bool nothrow = decl && TREE_NOTHROW (decl);
344 174115987 : tree callee = cp_get_callee (call);
345 174115987 : if (callee)
346 174115979 : 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 174115987 : if (cfun && cp_function_chain && !cp_unevaluated_operand)
352 : {
353 99607005 : if (!nothrow && at_function_scope_p ())
354 24584860 : cp_function_chain->can_throw = 1;
355 :
356 99607005 : if (decl && TREE_THIS_VOLATILE (decl))
357 2996391 : current_function_returns_abnormally = 1;
358 : }
359 :
360 174115987 : TREE_NOTHROW (call) = nothrow;
361 174115987 : }
362 :
363 : tree
364 168112658 : build_call_a (tree function, int n, tree *argarray)
365 : {
366 168112658 : tree decl;
367 168112658 : tree result_type;
368 168112658 : tree fntype;
369 168112658 : int i;
370 :
371 168112658 : function = build_addr_func (function, tf_warning_or_error);
372 :
373 168112658 : gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
374 168112658 : fntype = TREE_TYPE (TREE_TYPE (function));
375 168112658 : gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype));
376 168112658 : result_type = TREE_TYPE (fntype);
377 : /* An rvalue has no cv-qualifiers. */
378 168112658 : if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
379 97893884 : result_type = cv_unqualified (result_type);
380 :
381 168112658 : function = build_call_array_loc (input_location,
382 : result_type, function, n, argarray);
383 168112658 : set_flags_from_callee (function);
384 :
385 168112658 : decl = get_callee_fndecl (function);
386 :
387 168112658 : 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 213713 : gcc_assert (DECL_ARTIFICIAL (decl)
394 : || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
395 : "__", 2));
396 213713 : mark_used (decl);
397 : }
398 :
399 168112658 : require_complete_eh_spec_types (fntype, decl);
400 :
401 484912165 : 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 168112658 : if (!decl || !fndecl_built_in_p (decl))
407 337011719 : for (i = 0; i < n; i++)
408 : {
409 176646821 : tree arg = CALL_EXPR_ARG (function, i);
410 176646821 : if (is_empty_class (TREE_TYPE (arg))
411 176646821 : && simple_empty_class_p (TREE_TYPE (arg), arg, INIT_EXPR))
412 : {
413 5601402 : while (TREE_CODE (arg) == TARGET_EXPR)
414 : /* We're disconnecting the initializer from its target,
415 : don't create a temporary. */
416 2799521 : arg = TARGET_EXPR_INITIAL (arg);
417 2801881 : suppress_warning (arg, OPT_Wunused_result);
418 2801881 : tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg));
419 2801881 : arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t);
420 2801881 : CALL_EXPR_ARG (function, i) = arg;
421 : }
422 : }
423 :
424 168112658 : 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 64600742 : bool rewritten () const { return (flags & LOOKUP_REWRITTEN); }
543 1041277875 : 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 128029402 : null_ptr_cst_p (tree t)
551 : {
552 128029402 : 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 128029402 : if (NULLPTR_TYPE_P (type))
559 : return true;
560 :
561 118992755 : if (cxx_dialect >= cxx11)
562 : {
563 118637764 : STRIP_ANY_LOCATION_WRAPPER (t);
564 :
565 : /* Core issue 903 says only literal 0 is a null pointer constant. */
566 118637764 : if (TREE_CODE (t) == INTEGER_CST
567 13533801 : && !TREE_OVERFLOW (t)
568 13533801 : && TREE_CODE (type) == INTEGER_TYPE
569 12934853 : && integer_zerop (t)
570 127234129 : && !char_type_p (type))
571 : return true;
572 : }
573 354991 : else if (CP_INTEGRAL_TYPE_P (type))
574 : {
575 67026 : t = fold_non_dependent_expr (t, tf_none);
576 67026 : STRIP_NOPS (t);
577 67026 : 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 100847921 : null_member_pointer_value_p (tree t)
588 : {
589 100847921 : tree type = TREE_TYPE (t);
590 100847921 : if (!type)
591 : return false;
592 100661782 : else if (TYPE_PTRMEMFUNC_P (type))
593 1109 : return (TREE_CODE (t) == CONSTRUCTOR
594 564 : && CONSTRUCTOR_NELTS (t)
595 1670 : && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value));
596 100660673 : else if (TYPE_PTRDATAMEM_P (type))
597 5967 : return integer_all_onesp (t);
598 : else
599 : return false;
600 : }
601 :
602 : /* Returns nonzero if PARMLIST consists of only default parms,
603 : ellipsis, and/or undeduced parameter packs. */
604 :
605 : bool
606 725848446 : sufficient_parms_p (const_tree parmlist)
607 : {
608 735397350 : for (; parmlist && parmlist != void_list_node;
609 9548904 : parmlist = TREE_CHAIN (parmlist))
610 221230744 : if (!TREE_PURPOSE (parmlist)
611 221230744 : && !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 7861627963 : conversion_obstack_alloc (size_t n)
621 : {
622 7861627963 : void *p;
623 7861627963 : if (!conversion_obstack_initialized)
624 : {
625 87449 : gcc_obstack_init (&conversion_obstack);
626 87449 : conversion_obstack_initialized = true;
627 : }
628 7861627963 : p = obstack_alloc (&conversion_obstack, n);
629 7861627963 : memset (p, 0, n);
630 7861627963 : 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 2558757894 : conversion_obstack_sentinel (): p (conversion_obstack_alloc (0)) {}
639 1279365393 : ~conversion_obstack_sentinel () { obstack_free (&conversion_obstack, p); }
640 : };
641 :
642 : /* Allocate rejection reasons. */
643 :
644 : static struct rejection_reason *
645 753834475 : alloc_rejection (enum rejection_reason_code code)
646 : {
647 753834475 : struct rejection_reason *p;
648 0 : p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
649 753834475 : p->code = code;
650 753834475 : return p;
651 : }
652 :
653 : static struct rejection_reason *
654 192535923 : arity_rejection (tree first_arg, int expected, int actual, bool least_p = false)
655 : {
656 153185835 : struct rejection_reason *r = alloc_rejection (rr_arity);
657 192535923 : int adjust = first_arg != NULL_TREE;
658 192535923 : r->u.arity.expected = expected - adjust;
659 192535923 : r->u.arity.actual = actual - adjust;
660 192535923 : r->u.arity.least_p = least_p;
661 192535923 : return r;
662 : }
663 :
664 : static struct rejection_reason *
665 160751842 : arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to,
666 : location_t loc)
667 : {
668 6998513 : struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
669 160751842 : int adjust = first_arg != NULL_TREE;
670 160751842 : r->u.conversion.n_arg = n_arg - adjust;
671 160751842 : r->u.conversion.from = from;
672 160751842 : r->u.conversion.to_type = to;
673 160751842 : r->u.conversion.loc = loc;
674 160751842 : return r;
675 : }
676 :
677 : static struct rejection_reason *
678 22209864 : bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to,
679 : location_t loc)
680 : {
681 2603 : struct rejection_reason *r = alloc_rejection (rr_bad_arg_conversion);
682 22209864 : int adjust = first_arg != NULL_TREE;
683 22209864 : r->u.bad_conversion.n_arg = n_arg - adjust;
684 22209864 : r->u.bad_conversion.from = from;
685 22209864 : r->u.bad_conversion.to_type = to;
686 22209864 : r->u.bad_conversion.loc = loc;
687 22209864 : return r;
688 : }
689 :
690 : static struct rejection_reason *
691 2715 : explicit_conversion_rejection (tree from, tree to)
692 : {
693 2715 : struct rejection_reason *r = alloc_rejection (rr_explicit_conversion);
694 2715 : r->u.conversion.n_arg = 0;
695 2715 : r->u.conversion.from = from;
696 2715 : r->u.conversion.to_type = to;
697 2715 : r->u.conversion.loc = UNKNOWN_LOCATION;
698 2715 : 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 376610117 : 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 376610117 : size_t args_n_bytes = sizeof (*args) * nargs;
719 376610117 : tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes);
720 376610117 : struct rejection_reason *r = alloc_rejection (rr_template_unification);
721 376610117 : r->u.template_unification.tmpl = tmpl;
722 376610117 : r->u.template_unification.explicit_targs = explicit_targs;
723 376610117 : r->u.template_unification.num_targs = TREE_VEC_LENGTH (targs);
724 : /* Copy args to our own storage. */
725 376610117 : memcpy (args1, args, args_n_bytes);
726 376610117 : r->u.template_unification.args = args1;
727 376610117 : r->u.template_unification.nargs = nargs;
728 376610117 : r->u.template_unification.return_type = return_type;
729 376610117 : r->u.template_unification.strict = strict;
730 376610117 : r->u.template_unification.flags = flags;
731 376610117 : return r;
732 : }
733 :
734 : static struct rejection_reason *
735 116 : template_unification_error_rejection (void)
736 : {
737 0 : return alloc_rejection (rr_template_unification);
738 : }
739 :
740 : static struct rejection_reason *
741 723736 : invalid_copy_with_fn_template_rejection (void)
742 : {
743 0 : struct rejection_reason *r = alloc_rejection (rr_invalid_copy);
744 723736 : return r;
745 : }
746 :
747 : static struct rejection_reason *
748 802416 : inherited_ctor_rejection (void)
749 : {
750 0 : struct rejection_reason *r = alloc_rejection (rr_inherited_ctor);
751 802416 : return r;
752 : }
753 :
754 : /* Build a constraint failure record. */
755 :
756 : static struct rejection_reason *
757 197725 : constraint_failure (void)
758 : {
759 0 : struct rejection_reason *r = alloc_rejection (rr_constraint_failure);
760 197725 : return r;
761 : }
762 :
763 : /* Dynamically allocate a conversion. */
764 :
765 : static conversion *
766 2719332383 : alloc_conversion (conversion_kind kind)
767 : {
768 2719332383 : conversion *c;
769 0 : c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
770 2719332383 : c->kind = kind;
771 2719332383 : return c;
772 : }
773 :
774 : /* Make sure that all memory on the conversion obstack has been
775 : freed. */
776 :
777 : void
778 100226 : validate_conversion_obstack (void)
779 : {
780 100226 : if (conversion_obstack_initialized)
781 87115 : gcc_assert ((obstack_next_free (&conversion_obstack)
782 : == obstack_base (&conversion_obstack)));
783 100226 : }
784 :
785 : /* Dynamically allocate an array of N conversions. */
786 :
787 : static conversion **
788 1013082172 : 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 1492066455 : has_next (conversion_kind code)
797 : {
798 1402803094 : return !(code == ck_identity
799 1492066455 : || code == ck_ambig
800 : || code == ck_list
801 1402895389 : || code == ck_aggr
802 1492066455 : || code == ck_deferred_bad);
803 : }
804 :
805 : static conversion *
806 801827639 : build_conv (conversion_kind code, tree type, conversion *from)
807 : {
808 801827639 : conversion *t;
809 801827639 : conversion_rank rank = CONVERSION_RANK (from);
810 :
811 : /* Only call this function for conversions that use u.next. */
812 801827639 : 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 801827639 : t = alloc_conversion (code);
817 801827639 : t->type = type;
818 801827639 : t->u.next = from;
819 :
820 801827639 : switch (code)
821 : {
822 235135379 : case ck_ptr:
823 235135379 : case ck_pmem:
824 235135379 : case ck_base:
825 235135379 : case ck_std:
826 235135379 : if (rank < cr_std)
827 801827639 : rank = cr_std;
828 : break;
829 :
830 54121672 : case ck_qual:
831 54121672 : case ck_fnptr:
832 54121672 : if (rank < cr_exact)
833 801827639 : rank = cr_exact;
834 : break;
835 :
836 : default:
837 : break;
838 : }
839 801827639 : t->rank = rank;
840 801827639 : t->user_conv_p = (code == ck_user || from->user_conv_p);
841 801827639 : t->bad_p = from->bad_p;
842 801827639 : t->base_p = false;
843 801827639 : 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 95530 : build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
852 : {
853 95530 : tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
854 95530 : unsigned len = CONSTRUCTOR_NELTS (ctor);
855 95530 : conversion **subconvs = alloc_conversions (len);
856 95530 : conversion *t;
857 95530 : unsigned i;
858 95530 : tree val;
859 :
860 : /* Within a list-initialization we can have more user-defined
861 : conversions. */
862 95530 : flags &= ~LOOKUP_NO_CONVERSION;
863 : /* But no narrowing conversions. */
864 95530 : flags |= LOOKUP_NO_NARROWING;
865 :
866 : /* Can't make an array of these types. */
867 95530 : if (TYPE_REF_P (elttype)
868 95521 : || TREE_CODE (elttype) == FUNCTION_TYPE
869 95521 : || VOID_TYPE_P (elttype))
870 : return NULL;
871 :
872 355987 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
873 : {
874 273943 : 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 273892 : conversion *sub
936 273892 : = implicit_conversion (elttype, TREE_TYPE (val), val,
937 : false, flags, complain);
938 273892 : if (sub == NULL)
939 : return NULL;
940 :
941 260415 : subconvs[i] = sub;
942 : }
943 :
944 82044 : t = alloc_conversion (ck_list);
945 82044 : t->type = type;
946 82044 : t->u.list = subconvs;
947 82044 : t->rank = cr_exact;
948 :
949 323862 : for (i = 0; i < len; ++i)
950 : {
951 241818 : conversion *sub = subconvs[i];
952 241818 : if (sub->rank > t->rank)
953 61769 : t->rank = sub->rank;
954 241818 : if (sub->user_conv_p)
955 10356 : t->user_conv_p = true;
956 241818 : if (sub->bad_p)
957 61756 : 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 601204950 : next_conversion (conversion *conv)
969 : {
970 601204950 : if (conv == NULL
971 601204950 : || !has_next (conv->kind))
972 : return NULL;
973 589031102 : 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 1504925 : strip_standard_conversion (conversion *conv)
981 : {
982 1504925 : while (conv
983 1515375 : && conv->kind != ck_user
984 1557015 : && has_next (conv->kind))
985 10450 : conv = next_conversion (conv);
986 1504925 : 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 27604 : can_convert_array (tree atype, tree from, int flags, tsubst_flags_t complain)
994 : {
995 27604 : tree elttype = TREE_TYPE (atype);
996 27604 : unsigned i;
997 :
998 27604 : if (TREE_CODE (from) == CONSTRUCTOR)
999 : {
1000 30802 : for (i = 0; i < CONSTRUCTOR_NELTS (from); ++i)
1001 : {
1002 3243 : tree val = CONSTRUCTOR_ELT (from, i)->value;
1003 3243 : bool ok;
1004 3243 : if (TREE_CODE (elttype) == ARRAY_TYPE)
1005 18 : ok = can_convert_array (elttype, val, flags, complain);
1006 : else
1007 3225 : ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags,
1008 : complain);
1009 3243 : 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 861329 : field_in_pset (hash_set<tree, true> &pset, tree field)
1029 : {
1030 861329 : if (pset.contains (field))
1031 : return true;
1032 2593 : 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 1351068 : build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
1050 : {
1051 1351068 : unsigned HOST_WIDE_INT i = 0;
1052 1351068 : conversion *c;
1053 1351068 : tree field = next_aggregate_field (TYPE_FIELDS (type));
1054 1351068 : tree empty_ctor = NULL_TREE;
1055 1351068 : 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 1351068 : 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 1351068 : tree idx, val;
1069 2209804 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, val)
1070 : {
1071 858828 : if (!idx)
1072 : break;
1073 :
1074 858768 : gcc_checking_assert (TREE_CODE (idx) == FIELD_DECL);
1075 :
1076 858768 : tree ftype = TREE_TYPE (idx);
1077 858768 : bool ok;
1078 :
1079 858768 : if (TREE_CODE (ftype) == ARRAY_TYPE)
1080 1179 : ok = can_convert_array (ftype, val, flags, complain);
1081 : else
1082 857589 : ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
1083 : complain);
1084 :
1085 858768 : if (!ok)
1086 : return NULL;
1087 :
1088 : /* For unions, there should be just one initializer. */
1089 858759 : if (TREE_CODE (type) == UNION_TYPE)
1090 : {
1091 : field = NULL_TREE;
1092 : i = 1;
1093 : break;
1094 : }
1095 858736 : pset.add (idx);
1096 : }
1097 :
1098 2319716 : for (; field; field = next_aggregate_field (DECL_CHAIN (field)))
1099 : {
1100 968917 : tree ftype = TREE_TYPE (field);
1101 968917 : bool ok;
1102 :
1103 968917 : if (!pset.is_empty () && field_in_pset (pset, field))
1104 858736 : continue;
1105 110181 : if (i < CONSTRUCTOR_NELTS (ctor))
1106 : {
1107 39 : constructor_elt *ce = CONSTRUCTOR_ELT (ctor, i);
1108 39 : gcc_checking_assert (!ce->index);
1109 39 : val = ce->value;
1110 39 : ++i;
1111 : }
1112 110142 : else if (DECL_INITIAL (field))
1113 44911 : val = get_nsdmi (field, /*ctor*/false, complain);
1114 65231 : else if (TYPE_REF_P (ftype))
1115 : /* Value-initialization of reference is ill-formed. */
1116 : return NULL;
1117 : else
1118 : {
1119 65225 : if (empty_ctor == NULL_TREE)
1120 46502 : empty_ctor = build_constructor (init_list_type_node, NULL);
1121 : val = empty_ctor;
1122 : }
1123 :
1124 110175 : if (TREE_CODE (ftype) == ARRAY_TYPE)
1125 26407 : ok = can_convert_array (ftype, val, flags, complain);
1126 : else
1127 83768 : ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
1128 : complain);
1129 :
1130 110175 : if (!ok)
1131 : return NULL;
1132 :
1133 110162 : if (TREE_CODE (type) == UNION_TYPE)
1134 : break;
1135 : }
1136 :
1137 1351040 : if (i < CONSTRUCTOR_NELTS (ctor))
1138 : return NULL;
1139 :
1140 1351013 : c = alloc_conversion (ck_aggr);
1141 1351013 : c->type = type;
1142 1351013 : c->rank = cr_exact;
1143 1351013 : c->user_conv_p = true;
1144 1351013 : c->check_narrowing = true;
1145 1351013 : c->u.expr = ctor;
1146 1351013 : return c;
1147 1351068 : }
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 1889 : build_array_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
1154 : {
1155 1889 : conversion *c;
1156 1889 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1157 1889 : tree elttype = TREE_TYPE (type);
1158 1889 : bool bad = false;
1159 1889 : bool user = false;
1160 1889 : enum conversion_rank rank = cr_exact;
1161 :
1162 : /* We might need to propagate the size from the element to the array. */
1163 1889 : complete_type (type);
1164 :
1165 1889 : if (TYPE_DOMAIN (type)
1166 1889 : && !variably_modified_type_p (TYPE_DOMAIN (type), NULL_TREE))
1167 : {
1168 1815 : unsigned HOST_WIDE_INT alen = tree_to_uhwi (array_type_nelts_top (type));
1169 1815 : if (alen < len)
1170 : return NULL;
1171 : }
1172 :
1173 1842 : flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1174 :
1175 7707 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
1176 : {
1177 3030 : conversion *sub
1178 3030 : = implicit_conversion (elttype, TREE_TYPE (e.value), e.value,
1179 : false, flags, complain);
1180 3030 : if (sub == NULL)
1181 : return NULL;
1182 :
1183 2267 : if (sub->rank > rank)
1184 287 : rank = sub->rank;
1185 2267 : if (sub->user_conv_p)
1186 27 : user = true;
1187 2267 : if (sub->bad_p)
1188 218 : bad = true;
1189 : }
1190 :
1191 1079 : c = alloc_conversion (ck_aggr);
1192 1079 : c->type = type;
1193 1079 : c->rank = rank;
1194 1079 : c->user_conv_p = user;
1195 1079 : c->bad_p = bad;
1196 1079 : c->u.expr = ctor;
1197 1079 : 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 56626 : build_complex_conv (tree type, tree ctor, int flags,
1205 : tsubst_flags_t complain)
1206 : {
1207 56626 : conversion *c;
1208 56626 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1209 56626 : tree elttype = TREE_TYPE (type);
1210 56626 : bool bad = false;
1211 56626 : bool user = false;
1212 56626 : enum conversion_rank rank = cr_exact;
1213 :
1214 56626 : if (len != 2)
1215 : return NULL;
1216 :
1217 56586 : flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1218 :
1219 282930 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
1220 : {
1221 113172 : conversion *sub
1222 113172 : = implicit_conversion (elttype, TREE_TYPE (e.value), e.value,
1223 : false, flags, complain);
1224 113172 : if (sub == NULL)
1225 : return NULL;
1226 :
1227 113172 : if (sub->rank > rank)
1228 44 : rank = sub->rank;
1229 113172 : if (sub->user_conv_p)
1230 0 : user = true;
1231 113172 : if (sub->bad_p)
1232 0 : bad = true;
1233 : }
1234 :
1235 56586 : c = alloc_conversion (ck_aggr);
1236 56586 : c->type = type;
1237 56586 : c->rank = rank;
1238 56586 : c->user_conv_p = user;
1239 56586 : c->bad_p = bad;
1240 56586 : c->u.expr = ctor;
1241 56586 : 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 1908394570 : build_identity_conv (tree type, tree expr)
1249 : {
1250 1908394570 : conversion *c;
1251 :
1252 0 : c = alloc_conversion (ck_identity);
1253 1908394570 : c->type = type;
1254 1908394570 : c->u.expr = expr;
1255 :
1256 1908394570 : 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 1346 : build_ambiguous_conv (tree type, tree expr)
1265 : {
1266 1346 : conversion *c;
1267 :
1268 0 : c = alloc_conversion (ck_ambig);
1269 1346 : c->type = type;
1270 1346 : c->u.expr = expr;
1271 :
1272 1346 : return c;
1273 : }
1274 :
1275 : tree
1276 3423975376 : strip_top_quals (tree t)
1277 : {
1278 3423975376 : if (TREE_CODE (t) == ARRAY_TYPE)
1279 : return t;
1280 3417387721 : 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 1528248599 : standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
1290 : int flags, tsubst_flags_t complain)
1291 : {
1292 1528248599 : enum tree_code fcode, tcode;
1293 1528248599 : conversion *conv;
1294 1528248599 : bool fromref = false;
1295 1528248599 : tree qualified_to;
1296 :
1297 1528248599 : to = non_reference (to);
1298 1528248599 : if (TYPE_REF_P (from))
1299 : {
1300 80296 : fromref = true;
1301 80296 : from = TREE_TYPE (from);
1302 : }
1303 1528248599 : qualified_to = to;
1304 1528248599 : to = strip_top_quals (to);
1305 1528248599 : from = strip_top_quals (from);
1306 :
1307 1528248599 : if (expr && type_unknown_p (expr))
1308 : {
1309 278730 : if (TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
1310 : {
1311 32795 : tsubst_flags_t tflags = tf_conv;
1312 32795 : expr = instantiate_type (to, expr, tflags);
1313 32795 : if (expr == error_mark_node)
1314 : return NULL;
1315 21388 : from = TREE_TYPE (expr);
1316 : }
1317 245935 : else if (TREE_CODE (to) == BOOLEAN_TYPE)
1318 : {
1319 : /* Necessary for eg, TEMPLATE_ID_EXPRs (c++/50961). */
1320 5484 : expr = resolve_nondeduced_context (expr, complain);
1321 5484 : from = TREE_TYPE (expr);
1322 : }
1323 : }
1324 :
1325 1528237192 : fcode = TREE_CODE (from);
1326 1528237192 : tcode = TREE_CODE (to);
1327 :
1328 1528237192 : conv = build_identity_conv (from, expr);
1329 1528237192 : if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
1330 : {
1331 6610696 : from = type_decays_to (from);
1332 6610696 : fcode = TREE_CODE (from);
1333 : /* Tell convert_like that we're using the address. */
1334 6610696 : conv->rvaluedness_matches_p = true;
1335 6610696 : 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 1521626496 : else if (fromref || (expr && obvalue_p (expr)))
1341 : {
1342 355823611 : if (expr)
1343 : {
1344 355743327 : tree bitfield_type;
1345 355743327 : bitfield_type = is_bitfield_expr_with_lowered_type (expr);
1346 355743327 : if (bitfield_type)
1347 : {
1348 2672591 : from = strip_top_quals (bitfield_type);
1349 2672591 : fcode = TREE_CODE (from);
1350 : }
1351 : }
1352 355823611 : conv = build_conv (ck_rvalue, from, conv);
1353 : /* If we're performing copy-initialization, remember to skip
1354 : explicit constructors. */
1355 355823611 : if (flags & LOOKUP_ONLYCONVERTING)
1356 304648599 : conv->copy_init_p = true;
1357 : }
1358 :
1359 : /* Allow conversion between `__complex__' data types. */
1360 1528237192 : 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 2359594 : conversion *part_conv = standard_conversion
1366 2359594 : (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags,
1367 : complain);
1368 :
1369 2359594 : if (!part_conv)
1370 : conv = NULL;
1371 2359594 : else if (part_conv->kind == ck_identity)
1372 : /* Leave conv alone. */;
1373 : else
1374 : {
1375 275478 : conv = build_conv (part_conv->kind, to, conv);
1376 275478 : conv->rank = part_conv->rank;
1377 : }
1378 :
1379 2359594 : return conv;
1380 : }
1381 :
1382 1525877598 : if (same_type_p (from, to))
1383 : {
1384 932816351 : if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue)
1385 17432878 : conv->type = qualified_to;
1386 915383473 : else if (from != to)
1387 : /* Use TO in order to not lose TO in diagnostics. */
1388 80214392 : conv->type = to;
1389 932816351 : 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 382136927 : if ((tcode == POINTER_TYPE || TYPE_PTRMEM_P (to)
1397 382046291 : || NULLPTR_TYPE_P (to))
1398 598812769 : && ((expr && null_ptr_cst_p (expr))
1399 211059863 : || NULLPTR_TYPE_P (from)))
1400 5615983 : conv = build_conv (ck_std, to, conv);
1401 587445264 : else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
1402 586849062 : || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
1403 : {
1404 : /* For backwards brain damage compatibility, allow interconversion of
1405 : pointers and integers with a pedwarn. */
1406 2076375 : conv = build_conv (ck_std, to, conv);
1407 2076375 : conv->bad_p = true;
1408 : }
1409 585368889 : 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 356981 : conv = build_conv (ck_std, to, conv);
1414 356981 : conv->bad_p = true;
1415 : }
1416 585011908 : else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
1417 385841363 : || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from)))
1418 : {
1419 2755 : tree to_pointee;
1420 2755 : tree from_pointee;
1421 :
1422 2755 : if (tcode == POINTER_TYPE)
1423 : {
1424 199170545 : to_pointee = TREE_TYPE (to);
1425 199170545 : 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 199170545 : if (TREE_CODE (to_pointee) == FUNCTION_TYPE
1431 199170545 : && TYPE_QUALS (to_pointee))
1432 9 : to_pointee = build_qualified_type (to_pointee, TYPE_UNQUALIFIED);
1433 199170545 : if (TREE_CODE (from_pointee) == FUNCTION_TYPE
1434 199170545 : && TYPE_QUALS (from_pointee))
1435 54 : from_pointee = build_qualified_type (from_pointee, TYPE_UNQUALIFIED);
1436 : }
1437 : else
1438 : {
1439 2755 : to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
1440 2755 : from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
1441 : }
1442 :
1443 199173300 : if (tcode == POINTER_TYPE
1444 199173300 : && same_type_ignoring_top_level_qualifiers_p (from_pointee,
1445 : to_pointee))
1446 : ;
1447 140432630 : else if (VOID_TYPE_P (to_pointee)
1448 4516085 : && !TYPE_PTRDATAMEM_P (from)
1449 4516085 : && TREE_CODE (from_pointee) != FUNCTION_TYPE)
1450 : {
1451 4515676 : tree nfrom = TREE_TYPE (from);
1452 : /* Don't try to apply restrict to void. */
1453 4515676 : int quals = cp_type_quals (nfrom) & ~TYPE_QUAL_RESTRICT;
1454 4515676 : from_pointee = cp_build_qualified_type (void_type_node, quals);
1455 4515676 : from = build_pointer_type (from_pointee);
1456 4515676 : conv = build_conv (ck_ptr, from, conv);
1457 4515676 : }
1458 135916954 : else if (TYPE_PTRDATAMEM_P (from))
1459 : {
1460 2755 : tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
1461 2755 : tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
1462 :
1463 2755 : if (same_type_p (fbase, tbase))
1464 : /* No base conversion needed. */;
1465 619 : else if (DERIVED_FROM_P (fbase, tbase)
1466 1026 : && (same_type_ignoring_top_level_qualifiers_p
1467 407 : (from_pointee, to_pointee)))
1468 : {
1469 383 : from = build_ptrmem_type (tbase, from_pointee);
1470 383 : conv = build_conv (ck_pmem, from, conv);
1471 : }
1472 : else
1473 236 : return NULL;
1474 : }
1475 66319223 : else if (CLASS_TYPE_P (from_pointee)
1476 66316850 : && 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 201361478 : && DERIVED_FROM_P (to_pointee, from_pointee))
1489 : {
1490 6126105 : from_pointee
1491 6126105 : = cp_build_qualified_type (to_pointee,
1492 : cp_type_quals (from_pointee));
1493 6126105 : from = build_pointer_type (from_pointee);
1494 6126105 : conv = build_conv (ck_ptr, from, conv);
1495 6126105 : conv->base_p = true;
1496 : }
1497 :
1498 199173064 : if (same_type_p (from, to))
1499 : /* OK */;
1500 191164189 : 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 624 : conv = build_conv (ck_qual, to, conv);
1505 191163565 : else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
1506 53865730 : conv = build_conv (ck_qual, to, conv);
1507 137297835 : 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 137297631 : else if (fnptr_conv_p (to, from))
1511 238177 : conv = build_conv (ck_fnptr, to, conv);
1512 : /* Allow conversions among compatible ObjC pointer types (base
1513 : conversions have been already handled above). */
1514 137059454 : else if (c_dialect_objc ()
1515 137059454 : && objc_compare_types (to, from, -4, NULL_TREE))
1516 0 : conv = build_conv (ck_ptr, to, conv);
1517 137059454 : else if (ptr_reasonably_similar (to_pointee, from_pointee))
1518 : {
1519 8482736 : conv = build_conv (ck_ptr, to, conv);
1520 8482736 : conv->bad_p = true;
1521 : }
1522 : else
1523 : return NULL;
1524 :
1525 273410497 : from = to;
1526 : }
1527 385838608 : else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
1528 : {
1529 85554 : tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
1530 85554 : tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
1531 85554 : tree fbase = class_of_this_parm (fromfn);
1532 85554 : 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 85554 : if (!same_type_p (fbase, tbase) && !DERIVED_FROM_P (fbase, tbase))
1537 : return NULL;
1538 :
1539 85393 : tree fstat = static_fn_type (fromfn);
1540 85393 : tree tstat = static_fn_type (tofn);
1541 85393 : if (same_type_p (tstat, fstat)
1542 85393 : || fnptr_conv_p (tstat, fstat))
1543 : /* OK */;
1544 : else
1545 : return NULL;
1546 :
1547 85125 : if (!same_type_p (fbase, tbase))
1548 : {
1549 85054 : tree first = to;
1550 85054 : if (!same_type_p (tstat, fstat))
1551 : {
1552 0 : first = build_memfn_type (fstat,
1553 : tbase,
1554 : cp_type_quals (tbase),
1555 : type_memfn_rqual (tofn));
1556 0 : first = build_ptrmemfunc_type (build_pointer_type (first));
1557 : }
1558 85054 : conv = build_conv (ck_pmem, first, conv);
1559 85054 : conv->base_p = true;
1560 : }
1561 85125 : if (fnptr_conv_p (tstat, fstat))
1562 71 : conv = build_conv (ck_fnptr, to, conv);
1563 : }
1564 385753054 : else if (tcode == BOOLEAN_TYPE)
1565 : {
1566 : /* [conv.bool]
1567 :
1568 : A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
1569 : to member type can be converted to a prvalue of type bool. ...
1570 : For direct-initialization (8.5 [dcl.init]), a prvalue of type
1571 : std::nullptr_t can be converted to a prvalue of type bool; */
1572 6388805 : if (ARITHMETIC_TYPE_P (from)
1573 6320616 : || UNSCOPED_ENUM_P (from)
1574 4168036 : || fcode == POINTER_TYPE
1575 2932131 : || TYPE_PTRMEM_P (from)
1576 15074253 : || NULLPTR_TYPE_P (from))
1577 : {
1578 9210519 : conv = build_conv (ck_std, to, conv);
1579 9210519 : if (fcode == POINTER_TYPE
1580 7974614 : || TYPE_PTRDATAMEM_P (from)
1581 7974468 : || (TYPE_PTRMEMFUNC_P (from)
1582 77 : && conv->rank < cr_pbool)
1583 17184910 : || NULLPTR_TYPE_P (from))
1584 1236210 : conv->rank = cr_pbool;
1585 9210519 : if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING))
1586 40 : conv->bad_p = true;
1587 9210519 : if (flags & LOOKUP_NO_NARROWING)
1588 31385 : conv->check_narrowing = true;
1589 9210519 : return conv;
1590 : }
1591 :
1592 : return NULL;
1593 : }
1594 : /* We don't check for ENUMERAL_TYPE here because there are no standard
1595 : conversions to enum type. */
1596 : /* As an extension, allow conversion to complex type. */
1597 373610709 : else if (ARITHMETIC_TYPE_P (to))
1598 : {
1599 31212911 : if (! (INTEGRAL_CODE_P (fcode)
1600 24601439 : || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL)))
1601 226838208 : || SCOPED_ENUM_P (from))
1602 : return NULL;
1603 :
1604 : /* If we're parsing an enum with no fixed underlying type, we're
1605 : dealing with an incomplete type, which renders the conversion
1606 : ill-formed. */
1607 194223194 : if (!COMPLETE_TYPE_P (from))
1608 : return NULL;
1609 :
1610 194223188 : conv = build_conv (ck_std, to, conv);
1611 :
1612 194223188 : tree underlying_type = NULL_TREE;
1613 194223188 : if (TREE_CODE (from) == ENUMERAL_TYPE
1614 194223188 : && ENUM_FIXED_UNDERLYING_TYPE_P (from))
1615 1687091 : underlying_type = ENUM_UNDERLYING_TYPE (from);
1616 :
1617 : /* Give this a better rank if it's a promotion.
1618 :
1619 : To handle CWG 1601, also bump the rank if we are converting
1620 : an enumeration with a fixed underlying type to the underlying
1621 : type. */
1622 194223188 : if ((same_type_p (to, type_promotes_to (from))
1623 182429503 : || (underlying_type && same_type_p (to, underlying_type)))
1624 194223207 : && next_conversion (conv)->rank <= cr_promotion)
1625 11793704 : conv->rank = cr_promotion;
1626 :
1627 : /* A prvalue of floating-point type can be converted to a prvalue of
1628 : another floating-point type with a greater or equal conversion
1629 : rank ([conv.rank]). A prvalue of standard floating-point type can
1630 : be converted to a prvalue of another standard floating-point type.
1631 : For backwards compatibility with handling __float128 and other
1632 : non-standard floating point types, allow all implicit floating
1633 : point conversions if neither type is extended floating-point
1634 : type and if at least one of them is, fail if they have unordered
1635 : conversion rank or from has higher conversion rank. */
1636 194223188 : if (fcode == REAL_TYPE
1637 194223188 : && tcode == REAL_TYPE
1638 16805166 : && (extended_float_type_p (from)
1639 16089295 : || extended_float_type_p (to))
1640 198009249 : && cp_compare_floating_point_conversion_ranks (from, to) >= 2)
1641 1765602 : conv->bad_p = true;
1642 : }
1643 171373940 : else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
1644 171373940 : && vector_types_convertible_p (from, to, false))
1645 4905 : return build_conv (ck_std, to, conv);
1646 171369035 : else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
1647 68814143 : && is_properly_derived_from (from, to))
1648 : {
1649 456499 : if (conv->kind == ck_rvalue)
1650 456448 : conv = next_conversion (conv);
1651 456499 : conv = build_conv (ck_base, to, conv);
1652 : /* The derived-to-base conversion indicates the initialization
1653 : of a parameter with base type from an object of a derived
1654 : type. A temporary object is created to hold the result of
1655 : the conversion unless we're binding directly to a reference. */
1656 456499 : conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
1657 : /* If we're performing copy-initialization, remember to skip
1658 : explicit constructors. */
1659 456499 : if (flags & LOOKUP_ONLYCONVERTING)
1660 456432 : conv->copy_init_p = true;
1661 : }
1662 : else
1663 170912536 : return NULL;
1664 :
1665 273410497 : if (flags & LOOKUP_NO_NARROWING)
1666 94605548 : conv->check_narrowing = true;
1667 :
1668 : return conv;
1669 : }
1670 :
1671 : /* Returns nonzero if T1 is reference-related to T2.
1672 :
1673 : This is considered when a reference to T1 is initialized by a T2. */
1674 :
1675 : bool
1676 275421621 : reference_related_p (tree t1, tree t2)
1677 : {
1678 275421621 : if (t1 == error_mark_node || t2 == error_mark_node)
1679 : return false;
1680 :
1681 275421617 : t1 = TYPE_MAIN_VARIANT (t1);
1682 275421617 : t2 = TYPE_MAIN_VARIANT (t2);
1683 :
1684 : /* [dcl.init.ref]
1685 :
1686 : Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
1687 : to "cv2 T2" if T1 is similar to T2, or T1 is a base class of T2. */
1688 275421617 : return (similar_type_p (t1, t2)
1689 275421617 : || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1690 67309515 : && DERIVED_FROM_P (t1, t2)));
1691 : }
1692 :
1693 : /* Returns nonzero if T1 is reference-compatible with T2. */
1694 :
1695 : bool
1696 244270552 : reference_compatible_p (tree t1, tree t2)
1697 : {
1698 : /* [dcl.init.ref]
1699 :
1700 : "cv1 T1" is reference compatible with "cv2 T2" if
1701 : a prvalue of type "pointer to cv2 T2" can be converted to the type
1702 : "pointer to cv1 T1" via a standard conversion sequence. */
1703 244270552 : tree ptype1 = build_pointer_type (t1);
1704 244270552 : tree ptype2 = build_pointer_type (t2);
1705 244270552 : conversion *conv = standard_conversion (ptype1, ptype2, NULL_TREE,
1706 : /*c_cast_p=*/false, 0, tf_none);
1707 244270552 : if (!conv || conv->bad_p)
1708 134933410 : return false;
1709 : return true;
1710 : }
1711 :
1712 : /* Return true if converting FROM to TO would involve a qualification
1713 : conversion. */
1714 :
1715 : static bool
1716 119928416 : involves_qualification_conversion_p (tree to, tree from)
1717 : {
1718 : /* If we're not converting a pointer to another one, we won't get
1719 : a qualification conversion. */
1720 119928416 : if (!((TYPE_PTR_P (to) && TYPE_PTR_P (from))
1721 3075 : || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from))))
1722 : return false;
1723 :
1724 4720991 : conversion *conv = standard_conversion (to, from, NULL_TREE,
1725 : /*c_cast_p=*/false, 0, tf_none);
1726 9425132 : for (conversion *t = conv; t; t = next_conversion (t))
1727 4721007 : if (t->kind == ck_qual)
1728 : return true;
1729 :
1730 : return false;
1731 : }
1732 :
1733 : /* Return true if HANDLER is a match for exception object with EXCEPT_TYPE as
1734 : per [except.handle]/3. */
1735 :
1736 : bool
1737 3987 : handler_match_for_exception_type (tree handler, tree except_type)
1738 : {
1739 3987 : tree handler_type = HANDLER_TYPE (handler);
1740 3987 : if (handler_type == NULL_TREE)
1741 : return true; /* ... */
1742 3905 : if (same_type_ignoring_top_level_qualifiers_p (handler_type, except_type))
1743 : return true;
1744 584 : if (CLASS_TYPE_P (except_type) && CLASS_TYPE_P (handler_type))
1745 : {
1746 160 : base_kind b_kind;
1747 160 : tree binfo = lookup_base (except_type, handler_type, ba_check, &b_kind,
1748 : tf_none);
1749 160 : if (binfo && binfo != error_mark_node)
1750 74 : return true;
1751 : }
1752 510 : if (TYPE_PTR_P (handler_type) || TYPE_PTRDATAMEM_P (handler_type))
1753 : {
1754 120 : if (TREE_CODE (except_type) == NULLPTR_TYPE)
1755 : return true;
1756 108 : if ((TYPE_PTR_P (handler_type) && TYPE_PTR_P (except_type))
1757 18 : || (TYPE_PTRDATAMEM_P (handler_type)
1758 0 : && TYPE_PTRDATAMEM_P (except_type)))
1759 : {
1760 90 : conversion *conv
1761 90 : = standard_conversion (handler_type, except_type, NULL_TREE,
1762 : /*c_cast_p=*/false, 0, tf_none);
1763 90 : if (conv && !conv->bad_p)
1764 : {
1765 66 : for (conversion *t = conv; t; t = next_conversion (t))
1766 44 : switch (t->kind)
1767 : {
1768 44 : case ck_ptr:
1769 44 : case ck_fnptr:
1770 44 : case ck_qual:
1771 44 : case ck_identity:
1772 44 : break;
1773 : default:
1774 : return false;
1775 : }
1776 : return true;
1777 : }
1778 : }
1779 : }
1780 : return false;
1781 : }
1782 :
1783 : /* A reference of the indicated TYPE is being bound directly to the
1784 : expression represented by the implicit conversion sequence CONV.
1785 : Return a conversion sequence for this binding. */
1786 :
1787 : static conversion *
1788 123633907 : direct_reference_binding (tree type, conversion *conv)
1789 : {
1790 123633907 : tree t;
1791 :
1792 123633907 : gcc_assert (TYPE_REF_P (type));
1793 123633907 : gcc_assert (!TYPE_REF_P (conv->type));
1794 :
1795 123633907 : t = TREE_TYPE (type);
1796 :
1797 123633907 : if (conv->kind == ck_identity)
1798 : /* Mark the identity conv as to not decay to rvalue. */
1799 123633907 : conv->rvaluedness_matches_p = true;
1800 :
1801 : /* [over.ics.rank]
1802 :
1803 : When a parameter of reference type binds directly
1804 : (_dcl.init.ref_) to an argument expression, the implicit
1805 : conversion sequence is the identity conversion, unless the
1806 : argument expression has a type that is a derived class of the
1807 : parameter type, in which case the implicit conversion sequence is
1808 : a derived-to-base Conversion.
1809 :
1810 : If the parameter binds directly to the result of applying a
1811 : conversion function to the argument expression, the implicit
1812 : conversion sequence is a user-defined conversion sequence
1813 : (_over.ics.user_), with the second standard conversion sequence
1814 : either an identity conversion or, if the conversion function
1815 : returns an entity of a type that is a derived class of the
1816 : parameter type, a derived-to-base conversion. */
1817 123633907 : if (is_properly_derived_from (conv->type, t))
1818 : {
1819 : /* Represent the derived-to-base conversion. */
1820 3705491 : conv = build_conv (ck_base, t, conv);
1821 : /* We will actually be binding to the base-class subobject in
1822 : the derived class, so we mark this conversion appropriately.
1823 : That way, convert_like knows not to generate a temporary. */
1824 3705491 : conv->need_temporary_p = false;
1825 : }
1826 119928416 : else if (involves_qualification_conversion_p (t, conv->type))
1827 : /* Represent the qualification conversion. After DR 2352
1828 : #1 and #2 were indistinguishable conversion sequences:
1829 :
1830 : void f(int*); // #1
1831 : void f(const int* const &); // #2
1832 : void g(int* p) { f(p); }
1833 :
1834 : because the types "int *" and "const int *const" are
1835 : reference-related and we were binding both directly and they
1836 : had the same rank. To break it up, we add a ck_qual under the
1837 : ck_ref_bind so that conversion sequence ranking chooses #1.
1838 :
1839 : We strip_top_quals here which is also what standard_conversion
1840 : does. Failure to do so would confuse comp_cv_qual_signature
1841 : into thinking that in
1842 :
1843 : void f(const int * const &); // #1
1844 : void f(const int *); // #2
1845 : int *x;
1846 : f(x);
1847 :
1848 : #2 is a better match than #1 even though they're ambiguous (97296). */
1849 16866 : conv = build_conv (ck_qual, strip_top_quals (t), conv);
1850 :
1851 123633907 : return build_conv (ck_ref_bind, type, conv);
1852 : }
1853 :
1854 : /* Returns the conversion path from type FROM to reference type TO for
1855 : purposes of reference binding. For lvalue binding, either pass a
1856 : reference type to FROM or an lvalue expression to EXPR. If the
1857 : reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1858 : the conversion returned. If C_CAST_P is true, this
1859 : conversion is coming from a C-style cast. */
1860 :
1861 : static conversion *
1862 243425514 : reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags,
1863 : tsubst_flags_t complain)
1864 : {
1865 243425514 : conversion *conv = NULL;
1866 243425514 : conversion *bad_direct_conv = nullptr;
1867 243425514 : tree to = TREE_TYPE (rto);
1868 243425514 : tree from = rfrom;
1869 243425514 : tree tfrom;
1870 243425514 : bool related_p;
1871 243425514 : bool compatible_p;
1872 243425514 : cp_lvalue_kind gl_kind;
1873 243425514 : bool is_lvalue;
1874 :
1875 243425514 : if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1876 : {
1877 52 : expr = instantiate_type (to, expr, tf_none);
1878 52 : if (expr == error_mark_node)
1879 : return NULL;
1880 52 : from = TREE_TYPE (expr);
1881 : }
1882 :
1883 243425514 : bool copy_list_init = false;
1884 243425514 : bool single_list_conv = false;
1885 243425514 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1886 : {
1887 98884 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
1888 : /* DR 1288: Otherwise, if the initializer list has a single element
1889 : of type E and ... [T's] referenced type is reference-related to E,
1890 : the object or reference is initialized from that element...
1891 :
1892 : ??? With P0388R4, we should bind 't' directly to U{}:
1893 : using U = A[2];
1894 : A (&&t)[] = {U{}};
1895 : because A[] and A[2] are reference-related. But we don't do it
1896 : because grok_reference_init has deduced the array size (to 1), and
1897 : A[1] and A[2] aren't reference-related. */
1898 98884 : if (CONSTRUCTOR_NELTS (expr) == 1
1899 54497 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
1900 : {
1901 4342 : tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1902 4342 : if (error_operand_p (elt))
1903 : return NULL;
1904 4337 : tree etype = TREE_TYPE (elt);
1905 4337 : if (reference_related_p (to, etype))
1906 : {
1907 739 : expr = elt;
1908 739 : from = etype;
1909 739 : goto skip;
1910 : }
1911 3598 : else if (CLASS_TYPE_P (etype) && TYPE_HAS_CONVERSION (etype))
1912 : /* CWG1996: jason's proposed drafting adds "or initializing T from E
1913 : would bind directly". We check that in the direct binding with
1914 : conversion code below. */
1915 : single_list_conv = true;
1916 : }
1917 : /* Otherwise, if T is a reference type, a prvalue temporary of the type
1918 : referenced by T is copy-list-initialized, and the reference is bound
1919 : to that temporary. */
1920 : copy_list_init = true;
1921 243425509 : skip:;
1922 : }
1923 :
1924 243425509 : if (TYPE_REF_P (from))
1925 : {
1926 63507 : from = TREE_TYPE (from);
1927 63507 : if (!TYPE_REF_IS_RVALUE (rfrom)
1928 63507 : || TREE_CODE (from) == FUNCTION_TYPE)
1929 : gl_kind = clk_ordinary;
1930 : else
1931 : gl_kind = clk_rvalueref;
1932 : }
1933 243362002 : else if (expr)
1934 239957250 : gl_kind = lvalue_kind (expr);
1935 3123314 : else if (CLASS_TYPE_P (from)
1936 3404752 : || TREE_CODE (from) == ARRAY_TYPE)
1937 : gl_kind = clk_class;
1938 : else
1939 : gl_kind = clk_none;
1940 :
1941 : /* Don't allow a class prvalue when LOOKUP_NO_TEMP_BIND. */
1942 243425509 : if ((flags & LOOKUP_NO_TEMP_BIND)
1943 3527512 : && (gl_kind & clk_class))
1944 : gl_kind = clk_none;
1945 :
1946 : /* Same mask as real_lvalue_p. */
1947 240727324 : is_lvalue = gl_kind && !(gl_kind & (clk_rvalueref|clk_class));
1948 :
1949 208240462 : tfrom = from;
1950 208240462 : if ((gl_kind & clk_bitfield) != 0)
1951 1880021 : tfrom = unlowered_expr_type (expr);
1952 :
1953 : /* Figure out whether or not the types are reference-related and
1954 : reference compatible. We have to do this after stripping
1955 : references from FROM. */
1956 243425509 : related_p = reference_related_p (to, tfrom);
1957 : /* If this is a C cast, first convert to an appropriately qualified
1958 : type, so that we can later do a const_cast to the desired type. */
1959 243425509 : if (related_p && c_cast_p
1960 243425509 : && !at_least_as_qualified_p (to, tfrom))
1961 196 : to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1962 243425509 : compatible_p = reference_compatible_p (to, tfrom);
1963 :
1964 : /* Directly bind reference when target expression's type is compatible with
1965 : the reference and expression is an lvalue. In DR391, the wording in
1966 : [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1967 : const and rvalue references to rvalues of compatible class type.
1968 : We should also do direct bindings for non-class xvalues. */
1969 243425509 : if ((related_p || compatible_p) && gl_kind)
1970 : {
1971 : /* [dcl.init.ref]
1972 :
1973 : If the initializer expression
1974 :
1975 : -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1976 : is reference-compatible with "cv2 T2,"
1977 :
1978 : the reference is bound directly to the initializer expression
1979 : lvalue.
1980 :
1981 : [...]
1982 : If the initializer expression is an rvalue, with T2 a class type,
1983 : and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1984 : is bound to the object represented by the rvalue or to a sub-object
1985 : within that object. */
1986 :
1987 110591395 : conv = build_identity_conv (tfrom, expr);
1988 110591395 : conv = direct_reference_binding (rto, conv);
1989 :
1990 110591395 : if (TYPE_REF_P (rfrom))
1991 : /* Handle rvalue reference to function properly. */
1992 16318 : conv->rvaluedness_matches_p
1993 16318 : = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom));
1994 : else
1995 110575077 : conv->rvaluedness_matches_p
1996 110575077 : = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
1997 :
1998 110591395 : if ((gl_kind & clk_bitfield) != 0
1999 110591395 : || ((gl_kind & clk_packed) != 0 && !TYPE_PACKED (to)))
2000 : /* For the purposes of overload resolution, we ignore the fact
2001 : this expression is a bitfield or packed field. (In particular,
2002 : [over.ics.ref] says specifically that a function with a
2003 : non-const reference parameter is viable even if the
2004 : argument is a bitfield.)
2005 :
2006 : However, when we actually call the function we must create
2007 : a temporary to which to bind the reference. If the
2008 : reference is volatile, or isn't const, then we cannot make
2009 : a temporary, so we just issue an error when the conversion
2010 : actually occurs. */
2011 110 : conv->need_temporary_p = true;
2012 :
2013 : /* Don't allow binding of lvalues (other than function lvalues) to
2014 : rvalue references. */
2015 81213660 : if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
2016 120607448 : && TREE_CODE (to) != FUNCTION_TYPE)
2017 10014075 : conv->bad_p = true;
2018 :
2019 : /* Nor the reverse. */
2020 29377735 : if (!is_lvalue && !TYPE_REF_IS_RVALUE (rto)
2021 : /* Unless it's really a C++20 lvalue being treated as an xvalue.
2022 : But in C++23, such an expression is just an xvalue, not a special
2023 : lvalue, so the binding is once again ill-formed. */
2024 18189017 : && !(cxx_dialect <= cxx20
2025 14538083 : && (gl_kind & clk_implicit_rval))
2026 17110544 : && (!CP_TYPE_CONST_NON_VOLATILE_P (to)
2027 16994044 : || (flags & LOOKUP_NO_RVAL_BIND))
2028 110707895 : && TREE_CODE (to) != FUNCTION_TYPE)
2029 116500 : conv->bad_p = true;
2030 :
2031 110591395 : if (!compatible_p)
2032 6541647 : conv->bad_p = true;
2033 :
2034 110591395 : return conv;
2035 : }
2036 : /* [class.conv.fct] A conversion function is never used to convert a
2037 : (possibly cv-qualified) object to the (possibly cv-qualified) same
2038 : object type (or a reference to it), to a (possibly cv-qualified) base
2039 : class of that type (or a reference to it).... */
2040 4444712 : else if (!related_p
2041 128389402 : && !(flags & LOOKUP_NO_CONVERSION)
2042 47702110 : && (CLASS_TYPE_P (from) || single_list_conv))
2043 : {
2044 17592066 : tree rexpr = expr;
2045 17592066 : if (single_list_conv)
2046 144 : rexpr = CONSTRUCTOR_ELT (expr, 0)->value;
2047 :
2048 : /* [dcl.init.ref]
2049 :
2050 : If the initializer expression
2051 :
2052 : -- has a class type (i.e., T2 is a class type) can be
2053 : implicitly converted to an lvalue of type "cv3 T3," where
2054 : "cv1 T1" is reference-compatible with "cv3 T3". (this
2055 : conversion is selected by enumerating the applicable
2056 : conversion functions (_over.match.ref_) and choosing the
2057 : best one through overload resolution. (_over.match_).
2058 :
2059 : the reference is bound to the lvalue result of the conversion
2060 : in the second case. */
2061 17592066 : z_candidate *cand = build_user_type_conversion_1 (rto, rexpr, flags,
2062 : complain);
2063 17592066 : if (cand)
2064 : {
2065 12577 : if (!cand->second_conv->bad_p)
2066 : return cand->second_conv;
2067 :
2068 : /* Direct reference binding wasn't successful and yielded a bad
2069 : conversion. Proceed with trying to go through a temporary
2070 : instead, and if that also fails then we'll return this bad
2071 : conversion rather than no conversion for sake of better
2072 : diagnostics. */
2073 : bad_direct_conv = cand->second_conv;
2074 : }
2075 : }
2076 :
2077 : /* From this point on, we conceptually need temporaries, even if we
2078 : elide them. Only the cases above are "direct bindings". */
2079 132822074 : if (flags & LOOKUP_NO_TEMP_BIND)
2080 : return bad_direct_conv ? bad_direct_conv : nullptr;
2081 :
2082 : /* [over.ics.rank]
2083 :
2084 : When a parameter of reference type is not bound directly to an
2085 : argument expression, the conversion sequence is the one required
2086 : to convert the argument expression to the underlying type of the
2087 : reference according to _over.best.ics_. Conceptually, this
2088 : conversion sequence corresponds to copy-initializing a temporary
2089 : of the underlying type with the argument expression. Any
2090 : difference in top-level cv-qualification is subsumed by the
2091 : initialization itself and does not constitute a conversion. */
2092 :
2093 129526220 : bool maybe_valid_p = true;
2094 :
2095 : /* [dcl.init.ref]
2096 :
2097 : Otherwise, the reference shall be an lvalue reference to a
2098 : non-volatile const type, or the reference shall be an rvalue
2099 : reference. */
2100 171582661 : if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto))
2101 : maybe_valid_p = false;
2102 :
2103 : /* [dcl.init.ref]
2104 :
2105 : Otherwise, a temporary of type "cv1 T1" is created and
2106 : initialized from the initializer expression using the rules for a
2107 : non-reference copy initialization. If T1 is reference-related to
2108 : T2, cv1 must be the same cv-qualification as, or greater
2109 : cv-qualification than, cv2; otherwise, the program is ill-formed. */
2110 129526220 : if (related_p && !at_least_as_qualified_p (to, from))
2111 : maybe_valid_p = false;
2112 :
2113 : /* We try below to treat an invalid reference binding as a bad conversion
2114 : to improve diagnostics, but doing so may cause otherwise unnecessary
2115 : instantiations that can lead to a hard error. So during the first pass
2116 : of overload resolution wherein we shortcut bad conversions, instead just
2117 : produce a special conversion indicating a second pass is necessary if
2118 : there's no strictly viable candidate. */
2119 129526220 : if (!maybe_valid_p && (flags & LOOKUP_SHORTCUT_BAD_CONVS))
2120 : {
2121 7618540 : if (bad_direct_conv)
2122 : return bad_direct_conv;
2123 :
2124 7618073 : conv = alloc_conversion (ck_deferred_bad);
2125 7618073 : conv->bad_p = true;
2126 7618073 : return conv;
2127 : }
2128 :
2129 : /* We're generating a temporary now, but don't bind any more in the
2130 : conversion (specifically, don't slice the temporary returned by a
2131 : conversion operator). */
2132 121907680 : flags |= LOOKUP_NO_TEMP_BIND;
2133 :
2134 : /* Core issue 899: When [copy-]initializing a temporary to be bound
2135 : to the first parameter of a copy constructor (12.8) called with
2136 : a single argument in the context of direct-initialization,
2137 : explicit conversion functions are also considered.
2138 :
2139 : So don't set LOOKUP_ONLYCONVERTING in that case. */
2140 121907680 : if (!(flags & LOOKUP_COPY_PARM))
2141 102886703 : flags |= LOOKUP_ONLYCONVERTING;
2142 :
2143 121907680 : if (!conv)
2144 121907680 : conv = implicit_conversion (to, from, expr, c_cast_p,
2145 : flags, complain);
2146 121907680 : if (!conv)
2147 : return bad_direct_conv ? bad_direct_conv : nullptr;
2148 :
2149 11739976 : if (conv->user_conv_p)
2150 : {
2151 7877849 : if (copy_list_init)
2152 : /* Remember this was copy-list-initialization. */
2153 68417 : conv->need_temporary_p = true;
2154 :
2155 : /* If initializing the temporary used a conversion function,
2156 : recalculate the second conversion sequence. */
2157 22725233 : for (conversion *t = conv; t; t = next_conversion (t))
2158 15288108 : if (t->kind == ck_user
2159 7848287 : && c_cast_p && !maybe_valid_p)
2160 : {
2161 12 : if (complain & tf_warning)
2162 12 : warning (OPT_Wcast_user_defined,
2163 : "casting %qT to %qT does not use %qD",
2164 12 : from, rto, t->cand->fn);
2165 : /* Don't let recalculation try to make this valid. */
2166 : break;
2167 : }
2168 15288096 : else if (t->kind == ck_user
2169 15288096 : && DECL_CONV_FN_P (t->cand->fn))
2170 : {
2171 440712 : tree ftype = TREE_TYPE (TREE_TYPE (t->cand->fn));
2172 : /* A prvalue of non-class type is cv-unqualified. */
2173 440712 : if (!TYPE_REF_P (ftype) && !CLASS_TYPE_P (ftype))
2174 4174 : ftype = cv_unqualified (ftype);
2175 440712 : int sflags = (flags|LOOKUP_NO_CONVERSION)&~LOOKUP_NO_TEMP_BIND;
2176 440712 : conversion *new_second
2177 440712 : = reference_binding (rto, ftype, NULL_TREE, c_cast_p,
2178 : sflags, complain);
2179 440712 : if (!new_second)
2180 : return bad_direct_conv ? bad_direct_conv : nullptr;
2181 440712 : conv = merge_conversion_sequences (t, new_second);
2182 440712 : gcc_assert (maybe_valid_p || conv->bad_p);
2183 : return conv;
2184 : }
2185 : }
2186 :
2187 11299264 : conv = build_conv (ck_ref_bind, rto, conv);
2188 : /* This reference binding, unlike those above, requires the
2189 : creation of a temporary. */
2190 11299264 : conv->need_temporary_p = true;
2191 11299264 : conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
2192 11299264 : conv->bad_p |= !maybe_valid_p;
2193 :
2194 11299264 : return conv;
2195 : }
2196 :
2197 : /* Returns the implicit conversion sequence (see [over.ics]) from type
2198 : FROM to type TO. The optional expression EXPR may affect the
2199 : conversion. FLAGS are the usual overloading flags. If C_CAST_P is
2200 : true, this conversion is coming from a C-style cast. */
2201 :
2202 : static conversion *
2203 1513509335 : implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
2204 : int flags, tsubst_flags_t complain)
2205 : {
2206 1513509335 : conversion *conv;
2207 :
2208 1513509335 : if (from == error_mark_node || to == error_mark_node
2209 1513508026 : || expr == error_mark_node)
2210 : return NULL;
2211 :
2212 : /* Other flags only apply to the primary function in overload
2213 : resolution, or after we've chosen one. */
2214 1513508026 : flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM
2215 : |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_NO_NARROWING
2216 : |LOOKUP_PROTECT|LOOKUP_NO_NON_INTEGRAL|LOOKUP_SHORTCUT_BAD_CONVS);
2217 :
2218 : /* FIXME: actually we don't want warnings either, but we can't just
2219 : have 'complain &= ~(tf_warning|tf_error)' because it would cause
2220 : the regression of, eg, g++.old-deja/g++.benjamin/16077.C.
2221 : We really ought not to issue that warning until we've committed
2222 : to that conversion. */
2223 1513508026 : complain &= ~tf_error;
2224 :
2225 : /* Call reshape_init early to remove redundant braces. */
2226 1513508026 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr) && CLASS_TYPE_P (to))
2227 : {
2228 2378834 : to = complete_type (to);
2229 2378834 : if (!COMPLETE_TYPE_P (to))
2230 : return nullptr;
2231 2378810 : if (!CLASSTYPE_NON_AGGREGATE (to))
2232 : {
2233 1351367 : expr = reshape_init (to, expr, complain);
2234 1351367 : if (expr == error_mark_node)
2235 : return nullptr;
2236 1351127 : from = TREE_TYPE (expr);
2237 : }
2238 : }
2239 :
2240 : /* An argument should have gone through convert_from_reference. */
2241 1513507762 : gcc_checking_assert (!expr || !TYPE_REF_P (from));
2242 :
2243 1513507762 : if (TYPE_REF_P (to))
2244 236610390 : conv = reference_binding (to, from, expr, c_cast_p, flags, complain);
2245 : else
2246 1276897372 : conv = standard_conversion (to, from, expr, c_cast_p, flags, complain);
2247 :
2248 1513507762 : if (conv)
2249 : return conv;
2250 :
2251 295582774 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
2252 : {
2253 4843520 : if (is_std_init_list (to) && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2254 95530 : return build_list_conv (to, expr, flags, complain);
2255 :
2256 : /* As an extension, allow list-initialization of _Complex. */
2257 4652451 : if (TREE_CODE (to) == COMPLEX_TYPE
2258 4709086 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2259 : {
2260 56626 : conv = build_complex_conv (to, expr, flags, complain);
2261 56626 : if (conv)
2262 : return conv;
2263 : }
2264 :
2265 : /* Allow conversion from an initializer-list with one element to a
2266 : scalar type. */
2267 4595865 : if (SCALAR_TYPE_P (to))
2268 : {
2269 2287152 : int nelts = CONSTRUCTOR_NELTS (expr);
2270 355024 : tree elt;
2271 :
2272 355024 : if (nelts == 0)
2273 1932128 : elt = build_value_init (to, tf_none);
2274 355024 : else if (nelts == 1 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2275 354150 : elt = CONSTRUCTOR_ELT (expr, 0)->value;
2276 : else
2277 874 : elt = error_mark_node;
2278 :
2279 2287152 : conv = implicit_conversion (to, TREE_TYPE (elt), elt,
2280 : c_cast_p, flags, complain);
2281 2287152 : if (conv)
2282 : {
2283 2285160 : conv->check_narrowing = true;
2284 2285160 : if (BRACE_ENCLOSED_INITIALIZER_P (elt))
2285 : /* Too many levels of braces, i.e. '{{1}}'. */
2286 16 : conv->bad_p = true;
2287 2285160 : return conv;
2288 : }
2289 : }
2290 2308713 : else if (TREE_CODE (to) == ARRAY_TYPE)
2291 1889 : return build_array_conv (to, expr, flags, complain);
2292 : }
2293 :
2294 293143609 : if (expr != NULL_TREE
2295 286144585 : && (MAYBE_CLASS_TYPE_P (from)
2296 151710503 : || MAYBE_CLASS_TYPE_P (to))
2297 501192237 : && (flags & LOOKUP_NO_CONVERSION) == 0)
2298 : {
2299 78158045 : struct z_candidate *cand;
2300 :
2301 53339894 : if (CLASS_TYPE_P (to)
2302 53339844 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
2303 80440711 : && !CLASSTYPE_NON_AGGREGATE (complete_type (to)))
2304 1351068 : return build_aggr_conv (to, expr, flags, complain);
2305 :
2306 76806977 : cand = build_user_type_conversion_1 (to, expr, flags, complain);
2307 76806977 : if (cand)
2308 : {
2309 917476 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
2310 907822 : && CONSTRUCTOR_NELTS (expr) == 1
2311 58430 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr)
2312 14729284 : && !is_list_ctor (cand->fn))
2313 : {
2314 : /* "If C is not an initializer-list constructor and the
2315 : initializer list has a single element of type cv U, where U is
2316 : X or a class derived from X, the implicit conversion sequence
2317 : has Exact Match rank if U is X, or Conversion rank if U is
2318 : derived from X." */
2319 58030 : tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
2320 58030 : tree elttype = TREE_TYPE (elt);
2321 58030 : if (reference_related_p (to, elttype))
2322 85 : return implicit_conversion (to, elttype, elt,
2323 85 : c_cast_p, flags, complain);
2324 : }
2325 14670769 : conv = cand->second_conv;
2326 : }
2327 :
2328 : /* We used to try to bind a reference to a temporary here, but that
2329 : is now handled after the recursive call to this function at the end
2330 : of reference_binding. */
2331 76806892 : return conv;
2332 : }
2333 :
2334 : return NULL;
2335 : }
2336 :
2337 : /* Like implicit_conversion, but return NULL if the conversion is bad.
2338 :
2339 : This is not static so that check_non_deducible_conversion can call it within
2340 : add_template_candidate_real as part of overload resolution; it should not be
2341 : called outside of overload resolution. */
2342 :
2343 : conversion *
2344 6811336 : good_conversion (tree to, tree from, tree expr,
2345 : int flags, tsubst_flags_t complain)
2346 : {
2347 6811336 : conversion *c = implicit_conversion (to, from, expr, /*cast*/false,
2348 : flags, complain);
2349 6811336 : if (c && c->bad_p)
2350 2772950 : c = NULL;
2351 6811336 : return c;
2352 : }
2353 :
2354 : /* Add a new entry to the list of candidates. Used by the add_*_candidate
2355 : functions. ARGS will not be changed until a single candidate is
2356 : selected. */
2357 :
2358 : static struct z_candidate *
2359 1053040247 : add_candidate (struct z_candidate **candidates,
2360 : tree fn, tree first_arg, const vec<tree, va_gc> *args,
2361 : size_t num_convs, conversion **convs,
2362 : tree access_path, tree conversion_path,
2363 : int viable, struct rejection_reason *reason,
2364 : int flags)
2365 : {
2366 1053040247 : struct z_candidate *cand = (struct z_candidate *)
2367 1053040247 : conversion_obstack_alloc (sizeof (struct z_candidate));
2368 :
2369 1053040247 : cand->fn = fn;
2370 1053040247 : cand->first_arg = first_arg;
2371 1053040247 : cand->args = args;
2372 1053040247 : cand->convs = convs;
2373 1053040247 : cand->num_convs = num_convs;
2374 1053040247 : cand->access_path = access_path;
2375 1053040247 : cand->conversion_path = conversion_path;
2376 1053040247 : cand->viable = viable;
2377 1053040247 : cand->reason = reason;
2378 1053040247 : cand->next = *candidates;
2379 1053040247 : cand->flags = flags;
2380 1053040247 : *candidates = cand;
2381 :
2382 1053040247 : if (convs && cand->reversed ())
2383 : /* Swap the conversions for comparison in joust; we'll swap them back
2384 : before build_over_call. */
2385 84381930 : std::swap (convs[0], convs[1]);
2386 :
2387 1053040247 : return cand;
2388 : }
2389 :
2390 : /* FN is a function from the overload set that we outright didn't even
2391 : consider (for some reason); add it to the list as an non-viable "ignored"
2392 : candidate. */
2393 :
2394 : static z_candidate *
2395 666349128 : add_ignored_candidate (z_candidate **candidates, tree fn)
2396 : {
2397 : /* No need to dynamically allocate these. */
2398 666349128 : static const rejection_reason reason_ignored = { rr_ignored, {} };
2399 :
2400 666349128 : struct z_candidate *cand = (struct z_candidate *)
2401 666343671 : conversion_obstack_alloc (sizeof (struct z_candidate));
2402 :
2403 666349128 : cand->fn = fn;
2404 666349128 : cand->reason = const_cast<rejection_reason *> (&reason_ignored);
2405 666349128 : cand->next = *candidates;
2406 666349128 : *candidates = cand;
2407 :
2408 666349128 : return cand;
2409 : }
2410 :
2411 : /* True iff CAND is a candidate added by add_ignored_candidate. */
2412 :
2413 : static bool
2414 590743220 : ignored_candidate_p (const z_candidate *cand)
2415 : {
2416 590737117 : return cand->reason && cand->reason->code == rr_ignored;
2417 : }
2418 :
2419 : /* Return the number of remaining arguments in the parameter list
2420 : beginning with ARG. */
2421 :
2422 : int
2423 153190104 : remaining_arguments (tree arg)
2424 : {
2425 153190104 : int n;
2426 :
2427 266769385 : for (n = 0; arg != NULL_TREE && arg != void_list_node;
2428 113579281 : arg = TREE_CHAIN (arg))
2429 113579281 : n++;
2430 :
2431 153190104 : return n;
2432 : }
2433 :
2434 : /* [over.match.copy]: When initializing a temporary object (12.2) to be bound
2435 : to the first parameter of a constructor where the parameter is of type
2436 : "reference to possibly cv-qualified T" and the constructor is called with a
2437 : single argument in the context of direct-initialization of an object of type
2438 : "cv2 T", explicit conversion functions are also considered.
2439 :
2440 : So set LOOKUP_COPY_PARM to let reference_binding know that
2441 : it's being called in that context. */
2442 :
2443 : int
2444 441822319 : conv_flags (int i, int nargs, tree fn, tree arg, int flags)
2445 : {
2446 441822319 : int lflags = flags;
2447 441822319 : tree t;
2448 460339982 : if (i == 0 && nargs == 1 && DECL_CONSTRUCTOR_P (fn)
2449 146377605 : && (t = FUNCTION_FIRST_USER_PARMTYPE (fn))
2450 588199924 : && (same_type_ignoring_top_level_qualifiers_p
2451 146377605 : (non_reference (TREE_VALUE (t)), DECL_CONTEXT (fn))))
2452 : {
2453 113770560 : if (!(flags & LOOKUP_ONLYCONVERTING))
2454 34748910 : lflags |= LOOKUP_COPY_PARM;
2455 113770560 : if ((flags & LOOKUP_LIST_INIT_CTOR)
2456 113770560 : && BRACE_ENCLOSED_INITIALIZER_P (arg))
2457 1835 : lflags |= LOOKUP_NO_CONVERSION;
2458 : }
2459 : else
2460 328051759 : lflags |= LOOKUP_ONLYCONVERTING;
2461 :
2462 441822319 : return lflags;
2463 : }
2464 :
2465 : /* Build an appropriate 'this' conversion for the method FN and class
2466 : type CTYPE from the value ARG (having type ARGTYPE) to the type PARMTYPE.
2467 : This function modifies PARMTYPE, ARGTYPE and ARG. */
2468 :
2469 : static conversion *
2470 93621457 : build_this_conversion (tree fn, tree ctype,
2471 : tree& parmtype, tree& argtype, tree& arg,
2472 : int flags, tsubst_flags_t complain)
2473 : {
2474 187242914 : gcc_assert (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
2475 : && !DECL_CONSTRUCTOR_P (fn));
2476 :
2477 : /* The type of the implicit object parameter ('this') for
2478 : overload resolution is not always the same as for the
2479 : function itself; conversion functions are considered to
2480 : be members of the class being converted, and functions
2481 : introduced by a using-declaration are considered to be
2482 : members of the class that uses them.
2483 :
2484 : Since build_over_call ignores the ICS for the `this'
2485 : parameter, we can just change the parm type. */
2486 93621457 : parmtype = cp_build_qualified_type (ctype,
2487 93621457 : cp_type_quals (TREE_TYPE (parmtype)));
2488 93621457 : bool this_p = true;
2489 93621457 : if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn)))
2490 : {
2491 : /* If the function has a ref-qualifier, the implicit
2492 : object parameter has reference type. */
2493 165388 : bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn));
2494 165388 : parmtype = cp_build_reference_type (parmtype, rv);
2495 : /* The special handling of 'this' conversions in compare_ics
2496 : does not apply if there is a ref-qualifier. */
2497 165388 : this_p = false;
2498 : }
2499 : else
2500 : {
2501 93456069 : parmtype = build_pointer_type (parmtype);
2502 : /* We don't use build_this here because we don't want to
2503 : capture the object argument until we've chosen a
2504 : non-static member function. */
2505 93456069 : arg = build_address (arg);
2506 93456069 : argtype = lvalue_type (arg);
2507 : }
2508 93621457 : flags |= LOOKUP_ONLYCONVERTING;
2509 93621457 : conversion *t = implicit_conversion (parmtype, argtype, arg,
2510 : /*c_cast_p=*/false, flags, complain);
2511 93621457 : t->this_p = this_p;
2512 93621457 : return t;
2513 : }
2514 :
2515 : /* Create an overload candidate for the function or method FN called
2516 : with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
2517 : FLAGS is passed on to implicit_conversion.
2518 :
2519 : This does not change ARGS.
2520 :
2521 : CTYPE, if non-NULL, is the type we want to pretend this function
2522 : comes from for purposes of overload resolution.
2523 :
2524 : SHORTCUT_BAD_CONVS controls how we handle "bad" argument conversions.
2525 : If true, we stop computing conversions upon seeing the first bad
2526 : conversion. This is used by add_candidates to avoid computing
2527 : more conversions than necessary in the presence of a strictly viable
2528 : candidate, while preserving the defacto behavior of overload resolution
2529 : when it turns out there are only non-strictly viable candidates. */
2530 :
2531 : static struct z_candidate *
2532 626831385 : add_function_candidate (struct z_candidate **candidates,
2533 : tree fn, tree ctype, tree first_arg,
2534 : const vec<tree, va_gc> *args, tree access_path,
2535 : tree conversion_path, int flags,
2536 : conversion **convs,
2537 : bool shortcut_bad_convs,
2538 : tsubst_flags_t complain)
2539 : {
2540 626831385 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
2541 626831385 : int i, len;
2542 626831385 : tree parmnode;
2543 626831385 : tree orig_first_arg = first_arg;
2544 626831385 : int skip;
2545 626831385 : int viable = 1;
2546 626831385 : struct rejection_reason *reason = NULL;
2547 :
2548 : /* The `this', `in_chrg' and VTT arguments to constructors are not
2549 : considered in overload resolution. */
2550 1253662770 : if (DECL_CONSTRUCTOR_P (fn))
2551 : {
2552 284508694 : if (ctor_omit_inherited_parms (fn))
2553 : /* Bring back parameters omitted from an inherited ctor. */
2554 150 : parmlist = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
2555 : else
2556 284508619 : parmlist = skip_artificial_parms_for (fn, parmlist);
2557 284508694 : skip = num_artificial_parms_for (fn);
2558 284508694 : if (skip > 0 && first_arg != NULL_TREE)
2559 : {
2560 284508694 : --skip;
2561 284508694 : first_arg = NULL_TREE;
2562 : }
2563 : }
2564 : else
2565 : skip = 0;
2566 :
2567 1213150446 : len = vec_safe_length (args) - skip + (first_arg != NULL_TREE ? 1 : 0);
2568 626831385 : if (!convs)
2569 538133451 : convs = alloc_conversions (len);
2570 :
2571 : /* 13.3.2 - Viable functions [over.match.viable]
2572 : First, to be a viable function, a candidate function shall have enough
2573 : parameters to agree in number with the arguments in the list.
2574 :
2575 : We need to check this first; otherwise, checking the ICSes might cause
2576 : us to produce an ill-formed template instantiation. */
2577 :
2578 626831385 : parmnode = parmlist;
2579 1280168867 : for (i = 0; i < len; ++i)
2580 : {
2581 728959731 : if (parmnode == NULL_TREE || parmnode == void_list_node)
2582 : break;
2583 653337482 : parmnode = TREE_CHAIN (parmnode);
2584 : }
2585 :
2586 626831385 : if ((i < len && parmnode)
2587 626831385 : || !sufficient_parms_p (parmnode))
2588 : {
2589 153185835 : int remaining = remaining_arguments (parmnode);
2590 153185835 : viable = 0;
2591 153185835 : reason = arity_rejection (first_arg, i + remaining, len);
2592 : }
2593 :
2594 : /* An inherited constructor (12.6.3 [class.inhctor.init]) that has a first
2595 : parameter of type "reference to cv C" (including such a constructor
2596 : instantiated from a template) is excluded from the set of candidate
2597 : functions when used to construct an object of type D with an argument list
2598 : containing a single argument if C is reference-related to D. */
2599 573644394 : if (viable && len == 1 && parmlist && DECL_CONSTRUCTOR_P (fn)
2600 148842220 : && flag_new_inheriting_ctors
2601 775664920 : && DECL_INHERITED_CTOR (fn))
2602 : {
2603 1058855 : tree ptype = non_reference (TREE_VALUE (parmlist));
2604 1058855 : tree dtype = DECL_CONTEXT (fn);
2605 2117710 : tree btype = DECL_INHERITED_CTOR_BASE (fn);
2606 1058855 : if (reference_related_p (ptype, dtype)
2607 1058855 : && reference_related_p (btype, ptype))
2608 : {
2609 802416 : viable = false;
2610 802416 : reason = inherited_ctor_rejection ();
2611 : }
2612 : }
2613 :
2614 : /* Second, for a function to be viable, its constraints must be
2615 : satisfied. */
2616 626831385 : if (flag_concepts && viable && !constraints_satisfied_p (fn))
2617 : {
2618 197716 : reason = constraint_failure ();
2619 197716 : viable = false;
2620 : }
2621 :
2622 : /* When looking for a function from a subobject from an implicit
2623 : copy/move constructor/operator=, don't consider anything that takes (a
2624 : reference to) an unrelated type. See c++/44909 and core 1092. */
2625 626831385 : if (viable && parmlist && (flags & LOOKUP_DEFAULTED))
2626 : {
2627 78882822 : if (DECL_CONSTRUCTOR_P (fn))
2628 : i = 1;
2629 20529571 : else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2630 20529571 : && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
2631 : i = 2;
2632 : else
2633 : i = 0;
2634 39441411 : if (i && len == i)
2635 : {
2636 21769705 : parmnode = chain_index (i-1, parmlist);
2637 21769705 : if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
2638 : ctype))
2639 4407606 : viable = 0;
2640 : }
2641 :
2642 : /* This only applies at the top level. */
2643 35033805 : flags &= ~LOOKUP_DEFAULTED;
2644 : }
2645 :
2646 626831385 : if (! viable)
2647 158593573 : goto out;
2648 :
2649 468237812 : if (shortcut_bad_convs)
2650 468105706 : flags |= LOOKUP_SHORTCUT_BAD_CONVS;
2651 : else
2652 132106 : flags &= ~LOOKUP_SHORTCUT_BAD_CONVS;
2653 :
2654 : /* Third, for F to be a viable function, there shall exist for each
2655 : argument an implicit conversion sequence that converts that argument
2656 : to the corresponding parameter of F. */
2657 :
2658 468237812 : parmnode = parmlist;
2659 :
2660 805987823 : for (i = 0; i < len; ++i)
2661 : {
2662 511748457 : tree argtype, to_type;
2663 511748457 : tree arg;
2664 :
2665 511748457 : if (parmnode == void_list_node)
2666 : break;
2667 :
2668 511748457 : if (convs[i])
2669 : {
2670 : /* Already set during deduction. */
2671 6285716 : parmnode = TREE_CHAIN (parmnode);
2672 6285716 : continue;
2673 : }
2674 :
2675 505462741 : if (i == 0 && first_arg != NULL_TREE)
2676 89619022 : arg = first_arg;
2677 : else
2678 415843719 : arg = const_cast<tree> (
2679 796325128 : (*args)[i + skip - (first_arg != NULL_TREE ? 1 : 0)]);
2680 505462741 : argtype = lvalue_type (arg);
2681 :
2682 505462741 : conversion *t;
2683 505462741 : if (parmnode)
2684 : {
2685 499935980 : tree parmtype = TREE_VALUE (parmnode);
2686 499935980 : if (i == 0
2687 403793404 : && DECL_IOBJ_MEMBER_FUNCTION_P (fn)
2688 973904128 : && !DECL_CONSTRUCTOR_P (fn))
2689 89605499 : t = build_this_conversion (fn, ctype, parmtype, argtype, arg,
2690 : flags, complain);
2691 : else
2692 : {
2693 410330481 : int lflags = conv_flags (i, len-skip, fn, arg, flags);
2694 410330481 : t = implicit_conversion (parmtype, argtype, arg,
2695 : /*c_cast_p=*/false, lflags, complain);
2696 : }
2697 499935980 : to_type = parmtype;
2698 499935980 : parmnode = TREE_CHAIN (parmnode);
2699 : }
2700 : else
2701 : {
2702 5526761 : t = build_identity_conv (argtype, arg);
2703 5526761 : t->ellipsis_p = true;
2704 5526761 : to_type = argtype;
2705 : }
2706 :
2707 505462741 : convs[i] = t;
2708 505462741 : if (! t)
2709 : {
2710 151862518 : viable = 0;
2711 151862518 : reason = arg_conversion_rejection (first_arg, i, argtype, to_type,
2712 151862518 : EXPR_LOCATION (arg));
2713 151862518 : break;
2714 : }
2715 :
2716 353600223 : if (t->bad_p)
2717 : {
2718 22168004 : viable = -1;
2719 22168004 : reason = bad_arg_conversion_rejection (first_arg, i, arg, to_type,
2720 22168004 : EXPR_LOCATION (arg));
2721 22168004 : if (shortcut_bad_convs)
2722 : break;
2723 : }
2724 : }
2725 :
2726 294239366 : out:
2727 626831385 : return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
2728 626831385 : access_path, conversion_path, viable, reason, flags);
2729 : }
2730 :
2731 : /* Create an overload candidate for the conversion function FN which will
2732 : be invoked for expression OBJ, producing a pointer-to-function which
2733 : will in turn be called with the argument list FIRST_ARG/ARGLIST,
2734 : and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
2735 : passed on to implicit_conversion.
2736 :
2737 : Actually, we don't really care about FN; we care about the type it
2738 : converts to. There may be multiple conversion functions that will
2739 : convert to that type, and we rely on build_user_type_conversion_1 to
2740 : choose the best one; so when we create our candidate, we record the type
2741 : instead of the function. */
2742 :
2743 : static struct z_candidate *
2744 118580 : add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
2745 : const vec<tree, va_gc> *arglist,
2746 : tree access_path, tree conversion_path,
2747 : tsubst_flags_t complain)
2748 : {
2749 118580 : tree totype = TREE_TYPE (TREE_TYPE (fn));
2750 118580 : int i, len, viable, flags;
2751 118580 : tree parmlist, parmnode;
2752 118580 : conversion **convs;
2753 118580 : struct rejection_reason *reason;
2754 :
2755 237381 : for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
2756 118801 : parmlist = TREE_TYPE (parmlist);
2757 118580 : parmlist = TYPE_ARG_TYPES (parmlist);
2758 :
2759 118580 : len = vec_safe_length (arglist) + 1;
2760 118580 : convs = alloc_conversions (len);
2761 118580 : parmnode = parmlist;
2762 118580 : viable = 1;
2763 118580 : flags = LOOKUP_IMPLICIT;
2764 118580 : reason = NULL;
2765 :
2766 : /* Don't bother looking up the same type twice. */
2767 118580 : if (*candidates && (*candidates)->fn == totype)
2768 : return NULL;
2769 :
2770 118565 : if (!constraints_satisfied_p (fn))
2771 : {
2772 9 : reason = constraint_failure ();
2773 9 : viable = 0;
2774 9 : return add_candidate (candidates, fn, obj, arglist, len, convs,
2775 9 : access_path, conversion_path, viable, reason, flags);
2776 : }
2777 :
2778 377114 : for (i = 0; i < len; ++i)
2779 : {
2780 258858 : tree arg, argtype, convert_type = NULL_TREE;
2781 258858 : conversion *t;
2782 :
2783 258858 : if (i == 0)
2784 : arg = obj;
2785 : else
2786 140302 : arg = (*arglist)[i - 1];
2787 258858 : argtype = lvalue_type (arg);
2788 :
2789 258858 : if (i == 0)
2790 : {
2791 118556 : t = build_identity_conv (argtype, NULL_TREE);
2792 118556 : t = build_conv (ck_user, totype, t);
2793 : /* Leave the 'cand' field null; we'll figure out the conversion in
2794 : convert_like if this candidate is chosen. */
2795 118556 : convert_type = totype;
2796 : }
2797 140302 : else if (parmnode == void_list_node)
2798 : break;
2799 140075 : else if (parmnode)
2800 : {
2801 140066 : t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
2802 : /*c_cast_p=*/false, flags, complain);
2803 140066 : convert_type = TREE_VALUE (parmnode);
2804 : }
2805 : else
2806 : {
2807 9 : t = build_identity_conv (argtype, arg);
2808 9 : t->ellipsis_p = true;
2809 9 : convert_type = argtype;
2810 : }
2811 :
2812 258631 : convs[i] = t;
2813 258631 : if (! t)
2814 : break;
2815 :
2816 258558 : if (t->bad_p)
2817 : {
2818 27 : viable = -1;
2819 72 : reason = bad_arg_conversion_rejection (NULL_TREE, i, arg, convert_type,
2820 27 : EXPR_LOCATION (arg));
2821 : }
2822 :
2823 258558 : if (i == 0)
2824 118556 : continue;
2825 :
2826 140002 : if (parmnode)
2827 139993 : parmnode = TREE_CHAIN (parmnode);
2828 : }
2829 :
2830 118556 : if (i < len
2831 118556 : || ! sufficient_parms_p (parmnode))
2832 : {
2833 324 : int remaining = remaining_arguments (parmnode);
2834 324 : viable = 0;
2835 324 : reason = arity_rejection (NULL_TREE, i + remaining, len);
2836 : }
2837 :
2838 118556 : return add_candidate (candidates, totype, obj, arglist, len, convs,
2839 118556 : access_path, conversion_path, viable, reason, flags);
2840 : }
2841 :
2842 : static void
2843 9367495 : build_builtin_candidate (struct z_candidate **candidates, tree fnname,
2844 : tree type1, tree type2, const vec<tree,va_gc> &args,
2845 : tree *argtypes, int flags, tsubst_flags_t complain)
2846 : {
2847 9367495 : conversion *t;
2848 9367495 : conversion **convs;
2849 9367495 : size_t num_convs;
2850 9367495 : int viable = 1;
2851 9367495 : tree types[2];
2852 9367495 : struct rejection_reason *reason = NULL;
2853 :
2854 9367495 : types[0] = type1;
2855 9367495 : types[1] = type2;
2856 :
2857 9367495 : num_convs = args.length ();
2858 9367495 : convs = alloc_conversions (num_convs);
2859 :
2860 : /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2861 : conversion ops are allowed. We handle that here by just checking for
2862 : boolean_type_node because other operators don't ask for it. COND_EXPR
2863 : also does contextual conversion to bool for the first operand, but we
2864 : handle that in build_conditional_expr, and type1 here is operand 2. */
2865 9367495 : if (type1 != boolean_type_node)
2866 8806264 : flags |= LOOKUP_ONLYCONVERTING;
2867 :
2868 27375624 : for (unsigned i = 0; i < 2 && i < num_convs; ++i)
2869 : {
2870 18008129 : t = implicit_conversion (types[i], argtypes[i], args[i],
2871 : /*c_cast_p=*/false, flags, complain);
2872 18008129 : if (! t)
2873 : {
2874 1890811 : viable = 0;
2875 : /* We need something for printing the candidate. */
2876 1890811 : t = build_identity_conv (types[i], NULL_TREE);
2877 1890811 : reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2878 1890811 : types[i], EXPR_LOCATION (args[i]));
2879 : }
2880 16117318 : else if (t->bad_p)
2881 : {
2882 188 : viable = 0;
2883 188 : reason = bad_arg_conversion_rejection (NULL_TREE, i, args[i],
2884 : types[i],
2885 188 : EXPR_LOCATION (args[i]));
2886 : }
2887 18008129 : convs[i] = t;
2888 : }
2889 :
2890 : /* For COND_EXPR we rearranged the arguments; undo that now. */
2891 9367495 : if (num_convs == 3)
2892 : {
2893 159 : convs[2] = convs[1];
2894 159 : convs[1] = convs[0];
2895 159 : t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
2896 : /*c_cast_p=*/false, flags,
2897 : complain);
2898 159 : if (t)
2899 159 : convs[0] = t;
2900 : else
2901 : {
2902 0 : viable = 0;
2903 0 : reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2],
2904 : boolean_type_node,
2905 0 : EXPR_LOCATION (args[2]));
2906 : }
2907 : }
2908 :
2909 9367495 : add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
2910 : num_convs, convs,
2911 : /*access_path=*/NULL_TREE,
2912 : /*conversion_path=*/NULL_TREE,
2913 : viable, reason, flags);
2914 9367495 : }
2915 :
2916 : static bool
2917 12 : is_complete (tree t)
2918 : {
2919 12 : return COMPLETE_TYPE_P (complete_type (t));
2920 : }
2921 :
2922 : /* Returns nonzero if TYPE is a promoted arithmetic type. */
2923 :
2924 : static bool
2925 3087 : promoted_arithmetic_type_p (tree type)
2926 : {
2927 : /* [over.built]
2928 :
2929 : In this section, the term promoted integral type is used to refer
2930 : to those integral types which are preserved by integral promotion
2931 : (including e.g. int and long but excluding e.g. char).
2932 : Similarly, the term promoted arithmetic type refers to promoted
2933 : integral types plus floating types. */
2934 3087 : return ((CP_INTEGRAL_TYPE_P (type)
2935 239 : && same_type_p (type_promotes_to (type), type))
2936 3087 : || SCALAR_FLOAT_TYPE_P (type));
2937 : }
2938 :
2939 : /* Create any builtin operator overload candidates for the operator in
2940 : question given the converted operand types TYPE1 and TYPE2. The other
2941 : args are passed through from add_builtin_candidates to
2942 : build_builtin_candidate.
2943 :
2944 : TYPE1 and TYPE2 may not be permissible, and we must filter them.
2945 : If CODE is requires candidates operands of the same type of the kind
2946 : of which TYPE1 and TYPE2 are, we add both candidates
2947 : CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2948 :
2949 : static void
2950 13812256 : add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
2951 : enum tree_code code2, tree fnname, tree type1,
2952 : tree type2, vec<tree,va_gc> &args, tree *argtypes,
2953 : int flags, tsubst_flags_t complain)
2954 : {
2955 13812256 : switch (code)
2956 : {
2957 21 : case POSTINCREMENT_EXPR:
2958 21 : case POSTDECREMENT_EXPR:
2959 21 : args[1] = integer_zero_node;
2960 21 : type2 = integer_type_node;
2961 21 : break;
2962 : default:
2963 : break;
2964 : }
2965 :
2966 13812256 : switch (code)
2967 : {
2968 :
2969 : /* 4 For every pair (T, VQ), where T is an arithmetic type other than bool,
2970 : and VQ is either volatile or empty, there exist candidate operator
2971 : functions of the form
2972 : VQ T& operator++(VQ T&);
2973 : T operator++(VQ T&, int);
2974 : 5 For every pair (T, VQ), where T is an arithmetic type other than bool,
2975 : and VQ is either volatile or empty, there exist candidate operator
2976 : functions of the form
2977 : VQ T& operator--(VQ T&);
2978 : T operator--(VQ T&, int);
2979 : 6 For every pair (T, VQ), where T is a cv-qualified or cv-unqualified object
2980 : type, and VQ is either volatile or empty, there exist candidate operator
2981 : functions of the form
2982 : T*VQ& operator++(T*VQ&);
2983 : T*VQ& operator--(T*VQ&);
2984 : T* operator++(T*VQ&, int);
2985 : T* operator--(T*VQ&, int); */
2986 :
2987 18 : case POSTDECREMENT_EXPR:
2988 18 : case PREDECREMENT_EXPR:
2989 18 : if (TREE_CODE (type1) == BOOLEAN_TYPE)
2990 : return;
2991 : /* FALLTHRU */
2992 39 : case POSTINCREMENT_EXPR:
2993 39 : case PREINCREMENT_EXPR:
2994 : /* P0002R1, Remove deprecated operator++(bool) added "other than bool"
2995 : to p4. */
2996 39 : if (TREE_CODE (type1) == BOOLEAN_TYPE && cxx_dialect >= cxx17)
2997 : return;
2998 33 : if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
2999 : {
3000 24 : type1 = build_reference_type (type1);
3001 24 : break;
3002 : }
3003 : return;
3004 :
3005 : /* 7 For every cv-qualified or cv-unqualified object type T, there
3006 : exist candidate operator functions of the form
3007 :
3008 : T& operator*(T*);
3009 :
3010 :
3011 : 8 For every function type T that does not have cv-qualifiers or
3012 : a ref-qualifier, there exist candidate operator functions of the form
3013 : T& operator*(T*); */
3014 :
3015 111996 : case INDIRECT_REF:
3016 111996 : if (TYPE_PTR_P (type1)
3017 111996 : && (TYPE_PTROB_P (type1)
3018 12 : || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
3019 : break;
3020 : return;
3021 :
3022 : /* 9 For every type T, there exist candidate operator functions of the form
3023 : T* operator+(T*);
3024 :
3025 : 10 For every floating-point or promoted integral type T, there exist
3026 : candidate operator functions of the form
3027 : T operator+(T);
3028 : T operator-(T); */
3029 :
3030 481 : case UNARY_PLUS_EXPR: /* unary + */
3031 481 : if (TYPE_PTR_P (type1))
3032 : break;
3033 : /* FALLTHRU */
3034 205749 : case NEGATE_EXPR:
3035 205749 : if (ARITHMETIC_TYPE_P (type1))
3036 : break;
3037 : return;
3038 :
3039 : /* 11 For every promoted integral type T, there exist candidate operator
3040 : functions of the form
3041 : T operator~(T); */
3042 :
3043 156519 : case BIT_NOT_EXPR:
3044 156519 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
3045 : break;
3046 : return;
3047 :
3048 : /* 12 For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, C1
3049 : is the same type as C2 or is a derived class of C2, and T is an object
3050 : type or a function type there exist candidate operator functions of the
3051 : form
3052 : CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
3053 : where CV12 is the union of CV1 and CV2. */
3054 :
3055 34 : case MEMBER_REF:
3056 34 : if (TYPE_PTR_P (type1) && TYPE_PTRMEM_P (type2))
3057 : {
3058 34 : tree c1 = TREE_TYPE (type1);
3059 34 : tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
3060 :
3061 31 : if (CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
3062 62 : && (TYPE_PTRMEMFUNC_P (type2)
3063 12 : || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
3064 : break;
3065 : }
3066 : return;
3067 :
3068 : /* 13 For every pair of types L and R, where each of L and R is a floating-point
3069 : or promoted integral type, there exist candidate operator functions of the
3070 : form
3071 : LR operator*(L, R);
3072 : LR operator/(L, R);
3073 : LR operator+(L, R);
3074 : LR operator-(L, R);
3075 : bool operator<(L, R);
3076 : bool operator>(L, R);
3077 : bool operator<=(L, R);
3078 : bool operator>=(L, R);
3079 : bool operator==(L, R);
3080 : bool operator!=(L, R);
3081 : where LR is the result of the usual arithmetic conversions between
3082 : types L and R.
3083 :
3084 : 14 For every integral type T there exists a candidate operator function of
3085 : the form
3086 :
3087 : std::strong_ordering operator<=>(T, T);
3088 :
3089 : 15 For every pair of floating-point types L and R, there exists a candidate
3090 : operator function of the form
3091 :
3092 : std::partial_ordering operator<=>(L, R);
3093 :
3094 : 16 For every cv-qualified or cv-unqualified object type T there exist
3095 : candidate operator functions of the form
3096 : T* operator+(T*, std::ptrdiff_t);
3097 : T& operator[](T*, std::ptrdiff_t);
3098 : T* operator-(T*, std::ptrdiff_t);
3099 : T* operator+(std::ptrdiff_t, T*);
3100 : T& operator[](std::ptrdiff_t, T*);
3101 :
3102 : 17 For every T, where T is a pointer to object type, there exist candidate
3103 : operator functions of the form
3104 : std::ptrdiff_t operator-(T, T);
3105 :
3106 : 18 For every T, where T is an enumeration type or a pointer type, there
3107 : exist candidate operator functions of the form
3108 : bool operator<(T, T);
3109 : bool operator>(T, T);
3110 : bool operator<=(T, T);
3111 : bool operator>=(T, T);
3112 : bool operator==(T, T);
3113 : bool operator!=(T, T);
3114 : R operator<=>(T, T);
3115 :
3116 : where R is the result type specified in [expr.spaceship].
3117 :
3118 : 19 For every T, where T is a pointer-to-member type or std::nullptr_t,
3119 : there exist candidate operator functions of the form
3120 : bool operator==(T, T);
3121 : bool operator!=(T, T); */
3122 :
3123 201640 : case MINUS_EXPR:
3124 201640 : if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
3125 : break;
3126 13 : if (TYPE_PTROB_P (type1)
3127 201626 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3128 : {
3129 4 : type2 = ptrdiff_type_node;
3130 4 : break;
3131 : }
3132 : /* FALLTHRU */
3133 878331 : case MULT_EXPR:
3134 878331 : case TRUNC_DIV_EXPR:
3135 878331 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3136 : break;
3137 : return;
3138 :
3139 : /* This isn't exactly what's specified above for operator<=>, but it's
3140 : close enough. In particular, we don't care about the return type
3141 : specified above; it doesn't participate in overload resolution and it
3142 : doesn't affect the semantics of the built-in operator. */
3143 6638572 : case SPACESHIP_EXPR:
3144 6638572 : case EQ_EXPR:
3145 6638572 : case NE_EXPR:
3146 217186 : if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
3147 6855758 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)))
3148 : break;
3149 6638538 : if (NULLPTR_TYPE_P (type1) && NULLPTR_TYPE_P (type2))
3150 : break;
3151 6638532 : if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (args[1]))
3152 : {
3153 : type2 = type1;
3154 : break;
3155 : }
3156 6638529 : if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (args[0]))
3157 : {
3158 : type1 = type2;
3159 : break;
3160 : }
3161 : /* Fall through. */
3162 7352674 : case LT_EXPR:
3163 7352674 : case GT_EXPR:
3164 7352674 : case LE_EXPR:
3165 7352674 : case GE_EXPR:
3166 7352674 : case MAX_EXPR:
3167 7352674 : case MIN_EXPR:
3168 7352674 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3169 : break;
3170 5673036 : if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3171 : break;
3172 5669323 : if (TREE_CODE (type1) == ENUMERAL_TYPE
3173 4058481 : && TREE_CODE (type2) == ENUMERAL_TYPE)
3174 : break;
3175 2744688 : if (TYPE_PTR_P (type1)
3176 2744688 : && null_ptr_cst_p (args[1]))
3177 : {
3178 : type2 = type1;
3179 : break;
3180 : }
3181 2744669 : if (null_ptr_cst_p (args[0])
3182 2744669 : && TYPE_PTR_P (type2))
3183 : {
3184 : type1 = type2;
3185 : break;
3186 : }
3187 : return;
3188 :
3189 242236 : case PLUS_EXPR:
3190 242236 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3191 : break;
3192 : /* FALLTHRU */
3193 178572 : case ARRAY_REF:
3194 178572 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
3195 : {
3196 6 : type1 = ptrdiff_type_node;
3197 6 : break;
3198 : }
3199 178566 : if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3200 : {
3201 36715 : type2 = ptrdiff_type_node;
3202 36715 : break;
3203 : }
3204 : return;
3205 :
3206 : /* 18For every pair of promoted integral types L and R, there exist candi-
3207 : date operator functions of the form
3208 : LR operator%(L, R);
3209 : LR operator&(L, R);
3210 : LR operator^(L, R);
3211 : LR operator|(L, R);
3212 : L operator<<(L, R);
3213 : L operator>>(L, R);
3214 : where LR is the result of the usual arithmetic conversions between
3215 : types L and R. */
3216 :
3217 3037444 : case TRUNC_MOD_EXPR:
3218 3037444 : case BIT_AND_EXPR:
3219 3037444 : case BIT_IOR_EXPR:
3220 3037444 : case BIT_XOR_EXPR:
3221 3037444 : case LSHIFT_EXPR:
3222 3037444 : case RSHIFT_EXPR:
3223 3037444 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3224 : break;
3225 : return;
3226 :
3227 : /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
3228 : type, VQ is either volatile or empty, and R is a promoted arithmetic
3229 : type, there exist candidate operator functions of the form
3230 : VQ L& operator=(VQ L&, R);
3231 : VQ L& operator*=(VQ L&, R);
3232 : VQ L& operator/=(VQ L&, R);
3233 : VQ L& operator+=(VQ L&, R);
3234 : VQ L& operator-=(VQ L&, R);
3235 :
3236 : 20For every pair T, VQ), where T is any type and VQ is either volatile
3237 : or empty, there exist candidate operator functions of the form
3238 : T*VQ& operator=(T*VQ&, T*);
3239 :
3240 : 21For every pair T, VQ), where T is a pointer to member type and VQ is
3241 : either volatile or empty, there exist candidate operator functions of
3242 : the form
3243 : VQ T& operator=(VQ T&, T);
3244 :
3245 : 22For every triple T, VQ, I), where T is a cv-qualified or cv-
3246 : unqualified complete object type, VQ is either volatile or empty, and
3247 : I is a promoted integral type, there exist candidate operator func-
3248 : tions of the form
3249 : T*VQ& operator+=(T*VQ&, I);
3250 : T*VQ& operator-=(T*VQ&, I);
3251 :
3252 : 23For every triple L, VQ, R), where L is an integral or enumeration
3253 : type, VQ is either volatile or empty, and R is a promoted integral
3254 : type, there exist candidate operator functions of the form
3255 :
3256 : VQ L& operator%=(VQ L&, R);
3257 : VQ L& operator<<=(VQ L&, R);
3258 : VQ L& operator>>=(VQ L&, R);
3259 : VQ L& operator&=(VQ L&, R);
3260 : VQ L& operator^=(VQ L&, R);
3261 : VQ L& operator|=(VQ L&, R); */
3262 :
3263 1772442 : case MODIFY_EXPR:
3264 1772442 : switch (code2)
3265 : {
3266 102607 : case PLUS_EXPR:
3267 102607 : case MINUS_EXPR:
3268 102607 : if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3269 : {
3270 0 : type2 = ptrdiff_type_node;
3271 0 : break;
3272 : }
3273 : /* FALLTHRU */
3274 102612 : case MULT_EXPR:
3275 102612 : case TRUNC_DIV_EXPR:
3276 102612 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3277 : break;
3278 : return;
3279 :
3280 1669830 : case TRUNC_MOD_EXPR:
3281 1669830 : case BIT_AND_EXPR:
3282 1669830 : case BIT_IOR_EXPR:
3283 1669830 : case BIT_XOR_EXPR:
3284 1669830 : case LSHIFT_EXPR:
3285 1669830 : case RSHIFT_EXPR:
3286 1669830 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3287 : break;
3288 : return;
3289 :
3290 0 : case NOP_EXPR:
3291 0 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3292 : break;
3293 0 : if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
3294 0 : || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3295 0 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
3296 0 : || ((TYPE_PTRMEMFUNC_P (type1)
3297 0 : || TYPE_PTR_P (type1))
3298 0 : && null_ptr_cst_p (args[1])))
3299 : {
3300 : type2 = type1;
3301 : break;
3302 : }
3303 : return;
3304 :
3305 0 : default:
3306 0 : gcc_unreachable ();
3307 : }
3308 1427845 : type1 = build_reference_type (type1);
3309 1427845 : break;
3310 :
3311 2873 : case COND_EXPR:
3312 : /* [over.built]
3313 :
3314 : For every pair of promoted arithmetic types L and R, there
3315 : exist candidate operator functions of the form
3316 :
3317 : LR operator?(bool, L, R);
3318 :
3319 : where LR is the result of the usual arithmetic conversions
3320 : between types L and R.
3321 :
3322 : For every type T, where T is a pointer or pointer-to-member
3323 : type, there exist candidate operator functions of the form T
3324 : operator?(bool, T, T); */
3325 :
3326 2873 : if (promoted_arithmetic_type_p (type1)
3327 2873 : && promoted_arithmetic_type_p (type2))
3328 : /* That's OK. */
3329 : break;
3330 :
3331 : /* Otherwise, the types should be pointers. */
3332 2848 : if (!((TYPE_PTR_OR_PTRMEM_P (type1) || null_ptr_cst_p (args[0]))
3333 369 : && (TYPE_PTR_OR_PTRMEM_P (type2) || null_ptr_cst_p (args[1]))))
3334 2714 : return;
3335 :
3336 : /* We don't check that the two types are the same; the logic
3337 : below will actually create two candidates; one in which both
3338 : parameter types are TYPE1, and one in which both parameter
3339 : types are TYPE2. */
3340 : break;
3341 :
3342 3 : case REALPART_EXPR:
3343 3 : case IMAGPART_EXPR:
3344 3 : if (ARITHMETIC_TYPE_P (type1))
3345 : break;
3346 : return;
3347 :
3348 0 : default:
3349 0 : gcc_unreachable ();
3350 : }
3351 :
3352 : /* Make sure we don't create builtin candidates with dependent types. */
3353 8806139 : bool u1 = uses_template_parms (type1);
3354 8806139 : bool u2 = type2 ? uses_template_parms (type2) : false;
3355 8806126 : if (u1 || u2)
3356 : {
3357 : /* Try to recover if one of the types is non-dependent. But if
3358 : there's only one type, there's nothing we can do. */
3359 19 : if (!type2)
3360 : return;
3361 : /* And we lose if both are dependent. */
3362 16 : if (u1 && u2)
3363 : return;
3364 : /* Or if they have different forms. */
3365 9 : if (TREE_CODE (type1) != TREE_CODE (type2))
3366 : return;
3367 :
3368 9 : if (u1 && !u2)
3369 : type1 = type2;
3370 6 : else if (u2 && !u1)
3371 8806129 : type2 = type1;
3372 : }
3373 :
3374 : /* If we're dealing with two pointer types or two enumeral types,
3375 : we need candidates for both of them. */
3376 8573037 : if (type2 && !same_type_p (type1, type2)
3377 1905977 : && TREE_CODE (type1) == TREE_CODE (type2)
3378 9247247 : && (TYPE_REF_P (type1)
3379 441118 : || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3380 441002 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
3381 440984 : || TYPE_PTRMEMFUNC_P (type1)
3382 440984 : || MAYBE_CLASS_TYPE_P (type1)
3383 440984 : || TREE_CODE (type1) == ENUMERAL_TYPE))
3384 : {
3385 229 : if (TYPE_PTR_OR_PTRMEM_P (type1))
3386 : {
3387 134 : tree cptype = composite_pointer_type (input_location,
3388 : type1, type2,
3389 : error_mark_node,
3390 : error_mark_node,
3391 : CPO_CONVERSION,
3392 : tf_none);
3393 134 : if (cptype != error_mark_node)
3394 : {
3395 94 : build_builtin_candidate
3396 94 : (candidates, fnname, cptype, cptype, args, argtypes,
3397 : flags, complain);
3398 94 : return;
3399 : }
3400 : }
3401 :
3402 135 : build_builtin_candidate
3403 135 : (candidates, fnname, type1, type1, args, argtypes, flags, complain);
3404 135 : build_builtin_candidate
3405 135 : (candidates, fnname, type2, type2, args, argtypes, flags, complain);
3406 135 : return;
3407 : }
3408 :
3409 8805900 : build_builtin_candidate
3410 8805900 : (candidates, fnname, type1, type2, args, argtypes, flags, complain);
3411 : }
3412 :
3413 : tree
3414 843737945 : type_decays_to (tree type)
3415 : {
3416 843737945 : if (TREE_CODE (type) == ARRAY_TYPE)
3417 6643201 : return build_pointer_type (TREE_TYPE (type));
3418 837094744 : if (TREE_CODE (type) == FUNCTION_TYPE)
3419 37340 : return build_pointer_type (type);
3420 : return type;
3421 : }
3422 :
3423 : /* There are three conditions of builtin candidates:
3424 :
3425 : 1) bool-taking candidates. These are the same regardless of the input.
3426 : 2) pointer-pair taking candidates. These are generated for each type
3427 : one of the input types converts to.
3428 : 3) arithmetic candidates. According to the standard, we should generate
3429 : all of these, but I'm trying not to...
3430 :
3431 : Here we generate a superset of the possible candidates for this particular
3432 : case. That is a subset of the full set the standard defines, plus some
3433 : other cases which the standard disallows. add_builtin_candidate will
3434 : filter out the invalid set. */
3435 :
3436 : static void
3437 24101170 : add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
3438 : enum tree_code code2, tree fnname,
3439 : vec<tree, va_gc> *argv,
3440 : int flags, tsubst_flags_t complain)
3441 : {
3442 24101170 : int ref1;
3443 24101170 : int enum_p = 0;
3444 24101170 : tree type, argtypes[3], t;
3445 : /* TYPES[i] is the set of possible builtin-operator parameter types
3446 : we will consider for the Ith argument. */
3447 24101170 : vec<tree, va_gc> *types[2];
3448 24101170 : unsigned ix;
3449 24101170 : vec<tree, va_gc> &args = *argv;
3450 24101170 : unsigned len = args.length ();
3451 :
3452 67355763 : for (unsigned i = 0; i < len; ++i)
3453 : {
3454 43254593 : if (args[i])
3455 43254593 : argtypes[i] = unlowered_expr_type (args[i]);
3456 : else
3457 0 : argtypes[i] = NULL_TREE;
3458 : }
3459 :
3460 24101170 : switch (code)
3461 : {
3462 : /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
3463 : and VQ is either volatile or empty, there exist candidate operator
3464 : functions of the form
3465 : VQ T& operator++(VQ T&); */
3466 :
3467 : case POSTINCREMENT_EXPR:
3468 : case PREINCREMENT_EXPR:
3469 : case POSTDECREMENT_EXPR:
3470 : case PREDECREMENT_EXPR:
3471 : case MODIFY_EXPR:
3472 : ref1 = 1;
3473 : break;
3474 :
3475 : /* 24There also exist candidate operator functions of the form
3476 : bool operator!(bool);
3477 : bool operator&&(bool, bool);
3478 : bool operator||(bool, bool); */
3479 :
3480 493769 : case TRUTH_NOT_EXPR:
3481 493769 : build_builtin_candidate
3482 493769 : (candidates, fnname, boolean_type_node,
3483 : NULL_TREE, args, argtypes, flags, complain);
3484 13595930 : return;
3485 :
3486 67462 : case TRUTH_ORIF_EXPR:
3487 67462 : case TRUTH_ANDIF_EXPR:
3488 67462 : build_builtin_candidate
3489 67462 : (candidates, fnname, boolean_type_node,
3490 : boolean_type_node, args, argtypes, flags, complain);
3491 67462 : return;
3492 :
3493 : case ADDR_EXPR:
3494 : case COMPOUND_EXPR:
3495 : case COMPONENT_REF:
3496 : case CO_AWAIT_EXPR:
3497 : return;
3498 :
3499 6229865 : case COND_EXPR:
3500 6229865 : case EQ_EXPR:
3501 6229865 : case NE_EXPR:
3502 6229865 : case LT_EXPR:
3503 6229865 : case LE_EXPR:
3504 6229865 : case GT_EXPR:
3505 6229865 : case GE_EXPR:
3506 6229865 : case SPACESHIP_EXPR:
3507 6229865 : enum_p = 1;
3508 : /* Fall through. */
3509 :
3510 : default:
3511 : ref1 = 0;
3512 : }
3513 :
3514 21389677 : types[0] = make_tree_vector ();
3515 21389677 : types[1] = make_tree_vector ();
3516 :
3517 21389677 : if (len == 3)
3518 722 : len = 2;
3519 43729403 : for (unsigned i = 0; i < len; ++i)
3520 : {
3521 32730394 : if (MAYBE_CLASS_TYPE_P (argtypes[i]))
3522 : {
3523 14884551 : tree convs;
3524 :
3525 14884551 : if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
3526 : return;
3527 :
3528 11391156 : convs = lookup_conversions (argtypes[i]);
3529 :
3530 11391156 : if (code == COND_EXPR)
3531 : {
3532 1263 : if (lvalue_p (args[i]))
3533 916 : vec_safe_push (types[i], build_reference_type (argtypes[i]));
3534 :
3535 1263 : vec_safe_push (types[i], TYPE_MAIN_VARIANT (argtypes[i]));
3536 : }
3537 :
3538 11389893 : else if (! convs)
3539 : return;
3540 :
3541 11069123 : for (; convs; convs = TREE_CHAIN (convs))
3542 : {
3543 6575240 : type = TREE_TYPE (convs);
3544 :
3545 8133712 : if (i == 0 && ref1
3546 6575240 : && (!TYPE_REF_P (type)
3547 2599 : || CP_TYPE_CONST_P (TREE_TYPE (type))))
3548 1558472 : continue;
3549 :
3550 5016768 : if (code == COND_EXPR && TYPE_REF_P (type))
3551 24 : vec_safe_push (types[i], type);
3552 :
3553 5016768 : type = non_reference (type);
3554 5016768 : if (i != 0 || ! ref1)
3555 : {
3556 5016729 : type = cv_unqualified (type_decays_to (type));
3557 5016729 : if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
3558 656 : vec_safe_push (types[i], type);
3559 5016729 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3560 2828286 : type = type_promotes_to (type);
3561 : }
3562 :
3563 5016768 : if (! vec_member (type, types[i]))
3564 5008514 : vec_safe_push (types[i], type);
3565 : }
3566 : }
3567 : else
3568 : {
3569 17845843 : if (code == COND_EXPR && lvalue_p (args[i]))
3570 95 : vec_safe_push (types[i], build_reference_type (argtypes[i]));
3571 17845843 : type = non_reference (argtypes[i]);
3572 17845843 : if (i != 0 || ! ref1)
3573 : {
3574 16073518 : type = cv_unqualified (type_decays_to (type));
3575 16073518 : if (enum_p && UNSCOPED_ENUM_P (type))
3576 1906671 : vec_safe_push (types[i], type);
3577 16073518 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3578 9076111 : type = type_promotes_to (type);
3579 : }
3580 17845843 : vec_safe_push (types[i], type);
3581 : }
3582 : }
3583 :
3584 : /* Run through the possible parameter types of both arguments,
3585 : creating candidates with those parameter types. */
3586 22545406 : FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t)
3587 : {
3588 11546397 : unsigned jx;
3589 11546397 : tree u;
3590 :
3591 11546397 : if (!types[1]->is_empty ())
3592 24884305 : FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u)
3593 13337908 : add_builtin_candidate
3594 13337908 : (candidates, code, code2, fnname, t,
3595 : u, args, argtypes, flags, complain);
3596 : else
3597 474348 : add_builtin_candidate
3598 474348 : (candidates, code, code2, fnname, t,
3599 : NULL_TREE, args, argtypes, flags, complain);
3600 : }
3601 :
3602 10999009 : release_tree_vector (types[0]);
3603 10999009 : release_tree_vector (types[1]);
3604 : }
3605 :
3606 :
3607 : /* If TMPL can be successfully instantiated as indicated by
3608 : EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
3609 :
3610 : TMPL is the template. EXPLICIT_TARGS are any explicit template
3611 : arguments. ARGLIST is the arguments provided at the call-site.
3612 : This does not change ARGLIST. The RETURN_TYPE is the desired type
3613 : for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
3614 : as for add_function_candidate. If an OBJ is supplied, FLAGS and
3615 : CTYPE are ignored, and OBJ is as for add_conv_candidate.
3616 :
3617 : SHORTCUT_BAD_CONVS is as in add_function_candidate. */
3618 :
3619 : static struct z_candidate*
3620 505439753 : add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
3621 : tree ctype, tree explicit_targs, tree first_arg,
3622 : const vec<tree, va_gc> *arglist, tree return_type,
3623 : tree access_path, tree conversion_path,
3624 : int flags, tree obj, unification_kind_t strict,
3625 : bool shortcut_bad_convs, tsubst_flags_t complain)
3626 : {
3627 505439753 : int ntparms = DECL_NTPARMS (tmpl);
3628 505439753 : tree targs = make_tree_vec (ntparms);
3629 505439753 : unsigned int len = vec_safe_length (arglist);
3630 505439753 : unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
3631 505439753 : unsigned int skip_without_in_chrg = 0;
3632 505439753 : tree first_arg_without_in_chrg = first_arg;
3633 505439753 : tree *args_without_in_chrg;
3634 505439753 : unsigned int nargs_without_in_chrg;
3635 505439753 : unsigned int ia, ix;
3636 505439753 : tree arg;
3637 505439753 : struct z_candidate *cand;
3638 505439753 : tree fn;
3639 505439753 : struct rejection_reason *reason = NULL;
3640 505439753 : int errs;
3641 505439753 : conversion **convs = NULL;
3642 :
3643 : /* We don't do deduction on the in-charge parameter, the VTT
3644 : parameter or 'this'. */
3645 505439753 : if (DECL_IOBJ_MEMBER_FUNCTION_P (tmpl))
3646 : {
3647 51970603 : if (first_arg_without_in_chrg != NULL_TREE)
3648 : first_arg_without_in_chrg = NULL_TREE;
3649 12 : else if (return_type && strict == DEDUCE_CALL)
3650 : /* We're deducing for a call to the result of a template conversion
3651 : function, so the args don't contain 'this'; leave them alone. */;
3652 : else
3653 505439753 : ++skip_without_in_chrg;
3654 : }
3655 :
3656 505439753 : if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
3657 505439753 : || DECL_BASE_CONSTRUCTOR_P (tmpl))
3658 506429396 : && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
3659 : {
3660 261 : if (first_arg_without_in_chrg != NULL_TREE)
3661 : first_arg_without_in_chrg = NULL_TREE;
3662 : else
3663 261 : ++skip_without_in_chrg;
3664 : }
3665 :
3666 505439753 : if (len < skip_without_in_chrg)
3667 0 : return add_ignored_candidate (candidates, tmpl);
3668 :
3669 552842091 : if (DECL_CONSTRUCTOR_P (tmpl) && nargs == 2
3670 547577856 : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (first_arg),
3671 42138103 : TREE_TYPE ((*arglist)[0])))
3672 : {
3673 : /* 12.8/6 says, "A declaration of a constructor for a class X is
3674 : ill-formed if its first parameter is of type (optionally cv-qualified)
3675 : X and either there are no other parameters or else all other
3676 : parameters have default arguments. A member function template is never
3677 : instantiated to produce such a constructor signature."
3678 :
3679 : So if we're trying to copy an object of the containing class, don't
3680 : consider a template constructor that has a first parameter type that
3681 : is just a template parameter, as we would deduce a signature that we
3682 : would then reject in the code below. */
3683 2287272 : if (tree firstparm = FUNCTION_FIRST_USER_PARMTYPE (tmpl))
3684 : {
3685 2287272 : firstparm = TREE_VALUE (firstparm);
3686 2287272 : if (PACK_EXPANSION_P (firstparm))
3687 2122 : firstparm = PACK_EXPANSION_PATTERN (firstparm);
3688 2287272 : if (TREE_CODE (firstparm) == TEMPLATE_TYPE_PARM)
3689 : {
3690 722894 : gcc_assert (!explicit_targs);
3691 722894 : reason = invalid_copy_with_fn_template_rejection ();
3692 722894 : goto fail;
3693 : }
3694 : }
3695 : }
3696 :
3697 504716859 : nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
3698 504716859 : + (len - skip_without_in_chrg));
3699 504716859 : args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
3700 504716859 : ia = 0;
3701 504716859 : if (first_arg_without_in_chrg != NULL_TREE)
3702 : {
3703 10137 : args_without_in_chrg[ia] = first_arg_without_in_chrg;
3704 10137 : ++ia;
3705 : }
3706 842522934 : for (ix = skip_without_in_chrg;
3707 1347239793 : vec_safe_iterate (arglist, ix, &arg);
3708 : ++ix)
3709 : {
3710 842522934 : args_without_in_chrg[ia] = arg;
3711 842522934 : ++ia;
3712 : }
3713 504716859 : gcc_assert (ia == nargs_without_in_chrg);
3714 :
3715 504716859 : if (!obj)
3716 : {
3717 : /* Check that there's no obvious arity mismatch before proceeding with
3718 : deduction. This avoids substituting explicit template arguments
3719 : into the template or e.g. derived-to-base parm/arg unification
3720 : (which could result in an error outside the immediate context) when
3721 : the resulting candidate would be unviable anyway. */
3722 504716847 : int min_arity = 0, max_arity = 0;
3723 504716847 : tree parms = TYPE_ARG_TYPES (TREE_TYPE (tmpl));
3724 504716847 : parms = skip_artificial_parms_for (tmpl, parms);
3725 1892694365 : for (; parms != void_list_node; parms = TREE_CHAIN (parms))
3726 : {
3727 1772601300 : if (!parms || PACK_EXPANSION_P (TREE_VALUE (parms)))
3728 : {
3729 : max_arity = -1;
3730 : break;
3731 : }
3732 883260671 : if (TREE_PURPOSE (parms))
3733 : /* A parameter with a default argument. */
3734 9822884 : ++max_arity;
3735 : else
3736 873437787 : ++min_arity, ++max_arity;
3737 : }
3738 504716847 : if (ia < (unsigned)min_arity)
3739 : {
3740 : /* Too few arguments. */
3741 34256942 : reason = arity_rejection (NULL_TREE, min_arity, ia,
3742 : /*least_p=*/(max_arity == -1));
3743 34256942 : goto fail;
3744 : }
3745 470459905 : else if (max_arity != -1 && ia > (unsigned)max_arity)
3746 : {
3747 : /* Too many arguments. */
3748 5092822 : reason = arity_rejection (NULL_TREE, max_arity, ia);
3749 5092822 : goto fail;
3750 : }
3751 :
3752 465367083 : convs = alloc_conversions (nargs);
3753 :
3754 465367083 : if (shortcut_bad_convs
3755 465357713 : && DECL_IOBJ_MEMBER_FUNCTION_P (tmpl)
3756 537872889 : && !DECL_CONSTRUCTOR_P (tmpl))
3757 : {
3758 : /* Check the 'this' conversion before proceeding with deduction.
3759 : This is effectively an extension of the DR 1391 resolution
3760 : that we perform in check_non_deducible_conversions, though it's
3761 : convenient to do this extra check here instead of there. */
3762 4015958 : tree parmtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (tmpl)));
3763 4015958 : tree argtype = lvalue_type (first_arg);
3764 4015958 : tree arg = first_arg;
3765 4015958 : conversion *t = build_this_conversion (tmpl, ctype,
3766 : parmtype, argtype, arg,
3767 : flags, complain);
3768 4015958 : convs[0] = t;
3769 4015958 : if (t->bad_p)
3770 : {
3771 39069 : reason = bad_arg_conversion_rejection (first_arg, 0,
3772 : arg, parmtype,
3773 39069 : EXPR_LOCATION (arg));
3774 39069 : goto fail;
3775 : }
3776 : }
3777 : }
3778 :
3779 465328026 : errs = errorcount+sorrycount;
3780 930642501 : fn = fn_type_unification (tmpl, explicit_targs, targs,
3781 : args_without_in_chrg,
3782 : nargs_without_in_chrg,
3783 : return_type, strict, flags, convs,
3784 465328026 : false, complain & tf_decltype);
3785 :
3786 465314475 : if (fn == error_mark_node)
3787 : {
3788 : /* Don't repeat unification later if it already resulted in errors. */
3789 376610233 : if (errorcount+sorrycount == errs)
3790 376610117 : reason = template_unification_rejection (tmpl, explicit_targs,
3791 : targs, args_without_in_chrg,
3792 : nargs_without_in_chrg,
3793 : return_type, strict, flags);
3794 : else
3795 116 : reason = template_unification_error_rejection ();
3796 376610233 : goto fail;
3797 : }
3798 :
3799 : /* Now the explicit specifier might have been deduced; check if this
3800 : declaration is explicit. If it is and we're ignoring non-converting
3801 : constructors, don't add this function to the set of candidates. */
3802 88704242 : if (((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
3803 : == LOOKUP_ONLYCONVERTING)
3804 88704242 : && DECL_NONCONVERTING_P (fn))
3805 5457 : return add_ignored_candidate (candidates, fn);
3806 :
3807 177397570 : if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
3808 : {
3809 4711682 : tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
3810 9423337 : if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
3811 : ctype))
3812 : {
3813 : /* We're trying to produce a constructor with a prohibited signature,
3814 : as discussed above; handle here any cases we didn't catch then,
3815 : such as X(X<T>). */
3816 842 : reason = invalid_copy_with_fn_template_rejection ();
3817 842 : goto fail;
3818 : }
3819 : }
3820 :
3821 88697943 : if (obj != NULL_TREE)
3822 : /* Aha, this is a conversion function. */
3823 9 : cand = add_conv_candidate (candidates, fn, obj, arglist,
3824 : access_path, conversion_path, complain);
3825 : else
3826 88697934 : cand = add_function_candidate (candidates, fn, ctype,
3827 : first_arg, arglist, access_path,
3828 : conversion_path, flags, convs,
3829 : shortcut_bad_convs, complain);
3830 88697943 : if (DECL_TI_TEMPLATE (fn) != tmpl)
3831 : /* This situation can occur if a member template of a template
3832 : class is specialized. Then, instantiate_template might return
3833 : an instantiation of the specialization, in which case the
3834 : DECL_TI_TEMPLATE field will point at the original
3835 : specialization. For example:
3836 :
3837 : template <class T> struct S { template <class U> void f(U);
3838 : template <> void f(int) {}; };
3839 : S<double> sd;
3840 : sd.f(3);
3841 :
3842 : Here, TMPL will be template <class U> S<double>::f(U).
3843 : And, instantiate template will give us the specialization
3844 : template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
3845 : for this will point at template <class T> template <> S<T>::f(int),
3846 : so that we can find the definition. For the purposes of
3847 : overload resolution, however, we want the original TMPL. */
3848 5430866 : cand->template_decl = build_template_info (tmpl, targs);
3849 : else
3850 83267077 : cand->template_decl = DECL_TEMPLATE_INFO (fn);
3851 88697943 : cand->explicit_targs = explicit_targs;
3852 :
3853 88697943 : return cand;
3854 416722802 : fail:
3855 416722802 : int viable = (reason->code == rr_bad_arg_conversion ? -1 : 0);
3856 416722802 : return add_candidate (candidates, tmpl, first_arg, arglist, nargs, convs,
3857 416722802 : access_path, conversion_path, viable, reason, flags);
3858 : }
3859 :
3860 :
3861 : static struct z_candidate *
3862 505439741 : add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
3863 : tree explicit_targs, tree first_arg,
3864 : const vec<tree, va_gc> *arglist, tree return_type,
3865 : tree access_path, tree conversion_path, int flags,
3866 : unification_kind_t strict, bool shortcut_bad_convs,
3867 : tsubst_flags_t complain)
3868 : {
3869 505439741 : return
3870 0 : add_template_candidate_real (candidates, tmpl, ctype,
3871 : explicit_targs, first_arg, arglist,
3872 : return_type, access_path, conversion_path,
3873 : flags, NULL_TREE, strict, shortcut_bad_convs,
3874 505426190 : complain);
3875 : }
3876 :
3877 : /* Create an overload candidate for the conversion function template TMPL,
3878 : returning RETURN_TYPE, which will be invoked for expression OBJ to produce a
3879 : pointer-to-function which will in turn be called with the argument list
3880 : ARGLIST, and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
3881 : passed on to implicit_conversion. */
3882 :
3883 : static struct z_candidate *
3884 12 : add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
3885 : tree obj,
3886 : const vec<tree, va_gc> *arglist,
3887 : tree return_type, tree access_path,
3888 : tree conversion_path, tsubst_flags_t complain)
3889 : {
3890 12 : return
3891 12 : add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
3892 : NULL_TREE, arglist, return_type, access_path,
3893 : conversion_path, 0, obj, DEDUCE_CALL,
3894 12 : /*shortcut_bad_convs=*/false, complain);
3895 : }
3896 :
3897 : /* The CANDS are the set of candidates that were considered for
3898 : overload resolution. Sort CANDS so that the strictly viable
3899 : candidates appear first, followed by non-strictly viable candidates,
3900 : followed by non-viable candidates. Returns the first candidate
3901 : in this sorted list. If any of the candidates were viable, set
3902 : *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
3903 : considered viable only if it is strictly viable when setting
3904 : *ANY_VIABLE_P. */
3905 :
3906 : static struct z_candidate*
3907 321941721 : splice_viable (struct z_candidate *cands,
3908 : bool strict_p,
3909 : bool *any_viable_p)
3910 : {
3911 321941721 : z_candidate *strictly_viable = nullptr;
3912 321941721 : z_candidate **strictly_viable_tail = &strictly_viable;
3913 :
3914 321941721 : z_candidate *non_strictly_viable = nullptr;
3915 321941721 : z_candidate **non_strictly_viable_tail = &non_strictly_viable;
3916 :
3917 321941721 : z_candidate *non_viable = nullptr;
3918 321941721 : z_candidate **non_viable_tail = &non_viable;
3919 :
3920 321941721 : z_candidate *non_viable_ignored = nullptr;
3921 321941721 : z_candidate **non_viable_ignored_tail = &non_viable_ignored;
3922 :
3923 : /* Be strict inside templates, since build_over_call won't actually
3924 : do the conversions to get pedwarns. */
3925 321941721 : if (processing_template_decl)
3926 56173331 : strict_p = true;
3927 :
3928 1223773258 : for (z_candidate *cand = cands; cand; cand = cand->next)
3929 : {
3930 901831537 : if (!strict_p
3931 316839519 : && (cand->viable == 1 || TREE_CODE (cand->fn) == TEMPLATE_DECL))
3932 : /* Be strict in the presence of a viable candidate. Also if
3933 : there are template candidates, so that we get deduction errors
3934 : for them instead of silently preferring a bad conversion. */
3935 804490481 : strict_p = true;
3936 :
3937 : /* Move this candidate to the appropriate list according to
3938 : its viability. */
3939 901831537 : auto& tail = (cand->viable == 1 ? strictly_viable_tail
3940 : : cand->viable == -1 ? non_strictly_viable_tail
3941 1436455934 : : ignored_candidate_p (cand) ? non_viable_ignored_tail
3942 1436455934 : : non_viable_tail);
3943 901831537 : *tail = cand;
3944 901831537 : tail = &cand->next;
3945 : }
3946 :
3947 643883442 : *any_viable_p = (strictly_viable != nullptr
3948 321941721 : || (!strict_p && non_strictly_viable != nullptr));
3949 :
3950 : /* Combine the lists. */
3951 321941721 : *non_viable_ignored_tail = nullptr;
3952 321941721 : *non_viable_tail = non_viable_ignored;
3953 321941721 : *non_strictly_viable_tail = non_viable;
3954 321941721 : *strictly_viable_tail = non_strictly_viable;
3955 :
3956 321941721 : return strictly_viable;
3957 : }
3958 :
3959 : static bool
3960 308201126 : any_strictly_viable (struct z_candidate *cands)
3961 : {
3962 421412309 : for (; cands; cands = cands->next)
3963 121991323 : if (cands->viable == 1)
3964 : return true;
3965 : return false;
3966 : }
3967 :
3968 : /* OBJ is being used in an expression like "OBJ.f (...)". In other
3969 : words, it is about to become the "this" pointer for a member
3970 : function call. Take the address of the object. */
3971 :
3972 : static tree
3973 67162203 : build_this (tree obj)
3974 : {
3975 : /* In a template, we are only concerned about the type of the
3976 : expression, so we can take a shortcut. */
3977 67162203 : if (processing_template_decl)
3978 75853 : return build_address (obj);
3979 :
3980 67086350 : return cp_build_addr_expr (obj, tf_warning_or_error);
3981 : }
3982 :
3983 : /* Returns true iff functions are equivalent. Equivalent functions are
3984 : not '==' only if one is a function-local extern function or if
3985 : both are extern "C". */
3986 :
3987 : static inline int
3988 4817429 : equal_functions (tree fn1, tree fn2)
3989 : {
3990 4817429 : if (TREE_CODE (fn1) != TREE_CODE (fn2))
3991 : return 0;
3992 4802084 : if (TREE_CODE (fn1) == TEMPLATE_DECL)
3993 36051 : return fn1 == fn2;
3994 9532048 : if (DECL_LOCAL_DECL_P (fn1) || DECL_LOCAL_DECL_P (fn2)
3995 9532045 : || DECL_EXTERN_C_FUNCTION_P (fn1))
3996 2988500 : return decls_match (fn1, fn2);
3997 1777533 : return fn1 == fn2;
3998 : }
3999 :
4000 : /* Print information about a candidate FN being rejected due to INFO. */
4001 :
4002 : static void
4003 5309 : print_conversion_rejection (location_t loc, struct conversion_info *info,
4004 : tree fn)
4005 : {
4006 5309 : tree from = info->from;
4007 5309 : if (!TYPE_P (from))
4008 4369 : from = lvalue_type (from);
4009 5309 : if (info->n_arg == -1)
4010 : {
4011 : /* Conversion of implicit `this' argument failed. */
4012 24 : if (!TYPE_P (info->from))
4013 : /* A bad conversion for 'this' must be discarding cv-quals. */
4014 24 : inform (loc, "passing %qT as %<this%> "
4015 : "argument discards qualifiers",
4016 : from);
4017 : else
4018 0 : inform (loc, "no known conversion for implicit "
4019 : "%<this%> parameter from %qH to %qI",
4020 : from, info->to_type);
4021 : }
4022 5285 : else if (!TYPE_P (info->from))
4023 : {
4024 4345 : if (info->n_arg >= 0)
4025 4345 : inform (loc, "conversion of argument %d would be ill-formed:",
4026 : info->n_arg + 1);
4027 4345 : iloc_sentinel ils = loc;
4028 4345 : perform_implicit_conversion (info->to_type, info->from,
4029 : tf_warning_or_error);
4030 4345 : }
4031 940 : else if (info->n_arg == -2)
4032 : /* Conversion of conversion function return value failed. */
4033 33 : inform (loc, "no known conversion from %qH to %qI",
4034 : from, info->to_type);
4035 : else
4036 : {
4037 907 : if (TREE_CODE (fn) == FUNCTION_DECL)
4038 751 : loc = get_fndecl_argument_location (fn, info->n_arg);
4039 907 : inform (loc, "no known conversion for argument %d from %qH to %qI",
4040 907 : info->n_arg + 1, from, info->to_type);
4041 : }
4042 5309 : }
4043 :
4044 : /* Print information about a candidate with WANT parameters and we found
4045 : HAVE. */
4046 :
4047 : static void
4048 1457 : print_arity_information (location_t loc, unsigned int have, unsigned int want,
4049 : bool least_p)
4050 : {
4051 1457 : if (least_p)
4052 41 : inform_n (loc, want,
4053 : "candidate expects at least %d argument, %d provided",
4054 : "candidate expects at least %d arguments, %d provided",
4055 : want, have);
4056 : else
4057 1416 : inform_n (loc, want,
4058 : "candidate expects %d argument, %d provided",
4059 : "candidate expects %d arguments, %d provided",
4060 : want, have);
4061 1457 : }
4062 :
4063 : /* Print information about one overload candidate CANDIDATE. MSGSTR
4064 : is the text to print before the candidate itself.
4065 :
4066 : NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
4067 : to have been run through gettext by the caller. This wart makes
4068 : life simpler in print_z_candidates and for the translators. */
4069 :
4070 : static void
4071 13297 : print_z_candidate (location_t loc, const char *msgstr,
4072 : struct z_candidate *candidate)
4073 : {
4074 13297 : const char *msg = (msgstr == NULL
4075 13297 : ? ""
4076 13297 : : ACONCAT ((_(msgstr), " ", NULL)));
4077 13297 : tree fn = candidate->fn;
4078 13297 : if (flag_new_inheriting_ctors)
4079 13270 : fn = strip_inheriting_ctors (fn);
4080 13297 : location_t cloc = location_of (fn);
4081 :
4082 13297 : if (identifier_p (fn))
4083 : {
4084 214 : cloc = loc;
4085 214 : if (candidate->num_convs == 3)
4086 0 : inform (cloc, "%s%<%D(%T, %T, %T)%> (built-in)", msg, fn,
4087 0 : candidate->convs[0]->type,
4088 0 : candidate->convs[1]->type,
4089 0 : candidate->convs[2]->type);
4090 214 : else if (candidate->num_convs == 2)
4091 190 : inform (cloc, "%s%<%D(%T, %T)%> (built-in)", msg, fn,
4092 190 : candidate->convs[0]->type,
4093 190 : candidate->convs[1]->type);
4094 : else
4095 24 : inform (cloc, "%s%<%D(%T)%> (built-in)", msg, fn,
4096 24 : candidate->convs[0]->type);
4097 : }
4098 13083 : else if (TYPE_P (fn))
4099 15 : inform (cloc, "%s%qT (conversion)", msg, fn);
4100 13068 : else if (candidate->viable == -1)
4101 4376 : inform (cloc, "%s%#qD (near match)", msg, fn);
4102 8692 : else if (ignored_candidate_p (candidate))
4103 6 : inform (cloc, "%s%#qD (ignored)", msg, fn);
4104 8686 : else if (DECL_DELETED_FN (fn))
4105 60 : inform (cloc, "%s%#qD (deleted)", msg, fn);
4106 8626 : else if (candidate->reversed ())
4107 300 : inform (cloc, "%s%#qD (reversed)", msg, fn);
4108 8326 : else if (candidate->rewritten ())
4109 150 : inform (cloc, "%s%#qD (rewritten)", msg, fn);
4110 : else
4111 8176 : inform (cloc, "%s%#qD", msg, fn);
4112 :
4113 13297 : auto_diagnostic_nesting_level sentinel;
4114 :
4115 13297 : if (fn != candidate->fn)
4116 : {
4117 57 : cloc = location_of (candidate->fn);
4118 57 : inform (cloc, "inherited here");
4119 : }
4120 :
4121 : /* Give the user some information about why this candidate failed. */
4122 13297 : if (candidate->reason != NULL)
4123 : {
4124 10184 : struct rejection_reason *r = candidate->reason;
4125 :
4126 10184 : switch (r->code)
4127 : {
4128 1457 : case rr_arity:
4129 1457 : print_arity_information (cloc, r->u.arity.actual,
4130 1457 : r->u.arity.expected,
4131 1457 : r->u.arity.least_p);
4132 1457 : break;
4133 913 : case rr_arg_conversion:
4134 913 : print_conversion_rejection (cloc, &r->u.conversion, fn);
4135 913 : break;
4136 4396 : case rr_bad_arg_conversion:
4137 4396 : print_conversion_rejection (cloc, &r->u.bad_conversion, fn);
4138 4396 : break;
4139 11 : case rr_explicit_conversion:
4140 11 : inform (cloc, "return type %qT of explicit conversion function "
4141 : "cannot be converted to %qT with a qualification "
4142 : "conversion", r->u.conversion.from,
4143 : r->u.conversion.to_type);
4144 11 : break;
4145 6 : case rr_template_conversion:
4146 6 : inform (cloc, "conversion from return type %qT of template "
4147 : "conversion function specialization to %qT is not an "
4148 : "exact match", r->u.conversion.from,
4149 : r->u.conversion.to_type);
4150 6 : break;
4151 3268 : case rr_template_unification:
4152 : /* We use template_unification_error_rejection if unification caused
4153 : actual non-SFINAE errors, in which case we don't need to repeat
4154 : them here. */
4155 3268 : if (r->u.template_unification.tmpl == NULL_TREE)
4156 : {
4157 45 : inform (cloc, "substitution of deduced template arguments "
4158 : "resulted in errors seen above");
4159 45 : break;
4160 : }
4161 : /* Re-run template unification with diagnostics. */
4162 3223 : inform (cloc, "template argument deduction/substitution failed:");
4163 3223 : {
4164 3223 : auto_diagnostic_nesting_level sentinel;
4165 3223 : fn_type_unification (r->u.template_unification.tmpl,
4166 : r->u.template_unification.explicit_targs,
4167 : (make_tree_vec
4168 : (r->u.template_unification.num_targs)),
4169 : r->u.template_unification.args,
4170 : r->u.template_unification.nargs,
4171 : r->u.template_unification.return_type,
4172 : r->u.template_unification.strict,
4173 : r->u.template_unification.flags,
4174 : NULL, true, false);
4175 3223 : }
4176 3223 : break;
4177 2 : case rr_invalid_copy:
4178 2 : inform (cloc,
4179 : "a constructor taking a single argument of its own "
4180 : "class type is invalid");
4181 2 : break;
4182 93 : case rr_constraint_failure:
4183 93 : diagnose_constraints (cloc, fn, NULL_TREE);
4184 93 : break;
4185 32 : case rr_inherited_ctor:
4186 32 : inform (cloc, "an inherited constructor is not a candidate for "
4187 : "initialization from an expression of the same or derived "
4188 : "type");
4189 32 : break;
4190 : case rr_ignored:
4191 : break;
4192 0 : case rr_none:
4193 0 : default:
4194 : /* This candidate didn't have any issues or we failed to
4195 : handle a particular code. Either way... */
4196 0 : gcc_unreachable ();
4197 : }
4198 : }
4199 13297 : }
4200 :
4201 : /* Print information about each overload candidate in CANDIDATES,
4202 : which is assumed to have gone through splice_viable and tourney
4203 : (if splice_viable succeeded). */
4204 :
4205 : static void
4206 5552 : print_z_candidates (location_t loc, struct z_candidate *candidates,
4207 : tristate only_viable_p /* = tristate::unknown () */)
4208 : {
4209 5552 : struct z_candidate *cand1;
4210 5552 : struct z_candidate **cand2;
4211 :
4212 5552 : if (!candidates)
4213 1008 : return;
4214 :
4215 : /* Remove non-viable deleted candidates. */
4216 4544 : cand1 = candidates;
4217 20418 : for (cand2 = &cand1; *cand2; )
4218 : {
4219 15874 : if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
4220 11429 : && !(*cand2)->viable
4221 19635 : && DECL_DELETED_FN ((*cand2)->fn))
4222 102 : *cand2 = (*cand2)->next;
4223 : else
4224 15772 : cand2 = &(*cand2)->next;
4225 : }
4226 : /* ...if there are any non-deleted ones. */
4227 4544 : if (cand1)
4228 4538 : candidates = cand1;
4229 :
4230 : /* There may be duplicates in the set of candidates. We put off
4231 : checking this condition as long as possible, since we have no way
4232 : to eliminate duplicates from a set of functions in less than n^2
4233 : time. Now we are about to emit an error message, so it is more
4234 : permissible to go slowly. */
4235 20016 : for (cand1 = candidates; cand1; cand1 = cand1->next)
4236 : {
4237 15472 : tree fn = cand1->fn;
4238 : /* Skip builtin candidates and conversion functions. */
4239 15472 : if (!DECL_P (fn))
4240 289 : continue;
4241 15183 : cand2 = &cand1->next;
4242 143316 : while (*cand2)
4243 : {
4244 128133 : if (DECL_P ((*cand2)->fn)
4245 128133 : && equal_functions (fn, (*cand2)->fn))
4246 306 : *cand2 = (*cand2)->next;
4247 : else
4248 127827 : cand2 = &(*cand2)->next;
4249 : }
4250 : }
4251 :
4252 : /* Unless otherwise specified, if there's a (strictly) viable candidate
4253 : then we assume we're being called as part of diagnosing ambiguity, in
4254 : which case we want to print only viable candidates since non-viable
4255 : candidates couldn't have contributed to the ambiguity. */
4256 4544 : if (only_viable_p.is_unknown ())
4257 4535 : only_viable_p = candidates->viable == 1;
4258 :
4259 4544 : auto_diagnostic_nesting_level sentinel;
4260 :
4261 4544 : int num_candidates = 0;
4262 17731 : for (auto iter = candidates; iter; iter = iter->next)
4263 : {
4264 13528 : if (only_viable_p.is_true () && iter->viable != 1)
4265 : break;
4266 13187 : ++num_candidates;
4267 : }
4268 :
4269 4544 : inform_num_candidates (loc, num_candidates);
4270 :
4271 4544 : auto_diagnostic_nesting_level sentinel2;
4272 :
4273 4544 : int candidate_idx = 0;
4274 22240 : for (; candidates; candidates = candidates->next)
4275 : {
4276 13524 : if (only_viable_p.is_true () && candidates->viable != 1)
4277 : break;
4278 13220 : if (ignored_candidate_p (candidates) && !flag_diagnostics_all_candidates)
4279 : {
4280 31 : inform (loc, "some candidates omitted; "
4281 : "use %<-fdiagnostics-all-candidates%> to display them");
4282 31 : break;
4283 : }
4284 13152 : pretty_printer pp;
4285 13152 : pp_printf (&pp, N_("candidate %i:"), candidate_idx + 1);
4286 13152 : const char *const msgstr = pp_formatted_text (&pp);
4287 13152 : print_z_candidate (loc, msgstr, candidates);
4288 13152 : ++candidate_idx;
4289 13152 : }
4290 4544 : }
4291 :
4292 : /* USER_SEQ is a user-defined conversion sequence, beginning with a
4293 : USER_CONV. STD_SEQ is the standard conversion sequence applied to
4294 : the result of the conversion function to convert it to the final
4295 : desired type. Merge the two sequences into a single sequence,
4296 : and return the merged sequence. */
4297 :
4298 : static conversion *
4299 15146381 : merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
4300 : {
4301 15146381 : conversion **t;
4302 15146381 : bool bad = user_seq->bad_p;
4303 :
4304 15146381 : gcc_assert (user_seq->kind == ck_user);
4305 :
4306 : /* Find the end of the second conversion sequence. */
4307 15905231 : for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
4308 : {
4309 : /* The entire sequence is a user-conversion sequence. */
4310 758850 : (*t)->user_conv_p = true;
4311 758850 : if (bad)
4312 3506 : (*t)->bad_p = true;
4313 : }
4314 :
4315 15146381 : if ((*t)->rvaluedness_matches_p)
4316 : /* We're binding a reference directly to the result of the conversion.
4317 : build_user_type_conversion_1 stripped the REFERENCE_TYPE from the return
4318 : type, but we want it back. */
4319 449123 : user_seq->type = TREE_TYPE (TREE_TYPE (user_seq->cand->fn));
4320 :
4321 : /* Replace the identity conversion with the user conversion
4322 : sequence. */
4323 15146381 : *t = user_seq;
4324 :
4325 15146381 : return std_seq;
4326 : }
4327 :
4328 : /* Handle overload resolution for initializing an object of class type from
4329 : an initializer list. First we look for a suitable constructor that
4330 : takes a std::initializer_list; if we don't find one, we then look for a
4331 : non-list constructor.
4332 :
4333 : Parameters are as for add_candidates, except that the arguments are in
4334 : the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
4335 : the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
4336 :
4337 : static void
4338 2516147 : add_list_candidates (tree fns, tree first_arg,
4339 : const vec<tree, va_gc> *args, tree totype,
4340 : tree explicit_targs, bool template_only,
4341 : tree conversion_path, tree access_path,
4342 : int flags,
4343 : struct z_candidate **candidates,
4344 : tsubst_flags_t complain)
4345 : {
4346 2516147 : gcc_assert (*candidates == NULL);
4347 :
4348 : /* We're looking for a ctor for list-initialization. */
4349 2516147 : flags |= LOOKUP_LIST_INIT_CTOR;
4350 : /* And we don't allow narrowing conversions. We also use this flag to
4351 : avoid the copy constructor call for copy-list-initialization. */
4352 2516147 : flags |= LOOKUP_NO_NARROWING;
4353 :
4354 5032294 : unsigned nart = num_artificial_parms_for (OVL_FIRST (fns)) - 1;
4355 2516147 : tree init_list = (*args)[nart];
4356 :
4357 : /* Always use the default constructor if the list is empty (DR 990). */
4358 2516147 : if (CONSTRUCTOR_NELTS (init_list) == 0
4359 2516147 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
4360 : ;
4361 2275343 : else if (CONSTRUCTOR_IS_DESIGNATED_INIT (init_list)
4362 2275343 : && !CP_AGGREGATE_TYPE_P (totype))
4363 : {
4364 48 : if (complain & tf_error)
4365 12 : error ("designated initializers cannot be used with a "
4366 : "non-aggregate type %qT", totype);
4367 11658 : return;
4368 : }
4369 : /* If the class has a list ctor, try passing the list as a single
4370 : argument first, but only consider list ctors. */
4371 2275295 : else if (TYPE_HAS_LIST_CTOR (totype))
4372 : {
4373 86426 : flags |= LOOKUP_LIST_ONLY;
4374 86426 : add_candidates (fns, first_arg, args, NULL_TREE,
4375 : explicit_targs, template_only, conversion_path,
4376 : access_path, flags, candidates, complain);
4377 172852 : if (any_strictly_viable (*candidates))
4378 : return;
4379 : }
4380 :
4381 : /* Expand the CONSTRUCTOR into a new argument vec. */
4382 2504489 : vec<tree, va_gc> *new_args;
4383 4767419 : vec_alloc (new_args, nart + CONSTRUCTOR_NELTS (init_list));
4384 2504492 : for (unsigned i = 0; i < nart; ++i)
4385 3 : new_args->quick_push ((*args)[i]);
4386 2504489 : new_args = append_ctor_to_tree_vector (new_args, init_list);
4387 :
4388 : /* We aren't looking for list-ctors anymore. */
4389 2504489 : flags &= ~LOOKUP_LIST_ONLY;
4390 : /* We allow more user-defined conversions within an init-list. */
4391 2504489 : flags &= ~LOOKUP_NO_CONVERSION;
4392 :
4393 2504489 : add_candidates (fns, first_arg, new_args, NULL_TREE,
4394 : explicit_targs, template_only, conversion_path,
4395 : access_path, flags, candidates, complain);
4396 : }
4397 :
4398 : /* Given C(std::initializer_list<A>), return A. */
4399 :
4400 : static tree
4401 1503 : list_ctor_element_type (tree fn)
4402 : {
4403 1503 : gcc_checking_assert (is_list_ctor (fn));
4404 :
4405 1503 : tree parm = FUNCTION_FIRST_USER_PARMTYPE (fn);
4406 1503 : parm = non_reference (TREE_VALUE (parm));
4407 1503 : return TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
4408 : }
4409 :
4410 : /* If EXPR is a braced-init-list where the elements all decay to the same type,
4411 : return that type. */
4412 :
4413 : static tree
4414 1548 : braced_init_element_type (tree expr)
4415 : {
4416 1548 : if (TREE_CODE (expr) == CONSTRUCTOR
4417 1548 : && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
4418 0 : return TREE_TYPE (TREE_TYPE (expr));
4419 1548 : if (!BRACE_ENCLOSED_INITIALIZER_P (expr))
4420 : return NULL_TREE;
4421 :
4422 1548 : tree elttype = NULL_TREE;
4423 15153 : for (constructor_elt &e: CONSTRUCTOR_ELTS (expr))
4424 : {
4425 10711 : tree type = TREE_TYPE (e.value);
4426 10711 : type = type_decays_to (type);
4427 10711 : if (!elttype)
4428 : elttype = type;
4429 9191 : else if (!same_type_p (type, elttype))
4430 : return NULL_TREE;
4431 : }
4432 : return elttype;
4433 : }
4434 :
4435 : /* True iff EXPR contains any temporaries with non-trivial destruction.
4436 :
4437 : ??? Also ignore classes with non-trivial but no-op destruction other than
4438 : std::allocator? */
4439 :
4440 : static bool
4441 193 : has_non_trivial_temporaries (tree expr)
4442 : {
4443 193 : auto_vec<tree*> temps;
4444 193 : cp_walk_tree_without_duplicates (&expr, find_temps_r, &temps);
4445 388 : for (tree *p : temps)
4446 : {
4447 65 : tree t = TREE_TYPE (*p);
4448 65 : if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (t)
4449 65 : && !is_std_allocator (t))
4450 : return true;
4451 : }
4452 : return false;
4453 193 : }
4454 :
4455 : /* Return number of initialized elements in CTOR. */
4456 :
4457 : unsigned HOST_WIDE_INT
4458 263 : count_ctor_elements (tree ctor)
4459 : {
4460 263 : unsigned HOST_WIDE_INT len = 0;
4461 2194 : for (constructor_elt &e: CONSTRUCTOR_ELTS (ctor))
4462 1405 : if (TREE_CODE (e.value) == RAW_DATA_CST)
4463 9 : len += RAW_DATA_LENGTH (e.value);
4464 : else
4465 1396 : ++len;
4466 263 : return len;
4467 : }
4468 :
4469 : /* We're initializing an array of ELTTYPE from INIT. If it seems useful,
4470 : return INIT as an array (of its own type) so the caller can initialize the
4471 : target array in a loop. */
4472 :
4473 : static tree
4474 18976 : maybe_init_list_as_array (tree elttype, tree init)
4475 : {
4476 : /* Only do this if the array can go in rodata but not once converted. */
4477 18976 : if (!TYPE_NON_AGGREGATE_CLASS (elttype))
4478 : return NULL_TREE;
4479 1548 : tree init_elttype = braced_init_element_type (init);
4480 1548 : if (!init_elttype || !SCALAR_TYPE_P (init_elttype) || !TREE_CONSTANT (init))
4481 : return NULL_TREE;
4482 :
4483 : /* Check with a stub expression to weed out special cases, and check whether
4484 : we call the same function for direct-init as copy-list-init. */
4485 367 : conversion_obstack_sentinel cos;
4486 367 : init_elttype = cp_build_qualified_type (init_elttype, TYPE_QUAL_CONST);
4487 367 : tree arg = build_stub_object (init_elttype);
4488 367 : conversion *c = implicit_conversion (elttype, init_elttype, arg, false,
4489 : LOOKUP_NORMAL, tf_none);
4490 367 : if (c && c->kind == ck_rvalue)
4491 0 : c = next_conversion (c);
4492 361 : if (!c || c->kind != ck_user)
4493 : return NULL_TREE;
4494 : /* Check that we actually can perform the conversion. */
4495 358 : if (convert_like (c, arg, tf_none) == error_mark_node)
4496 : /* Let the normal code give the error. */
4497 : return NULL_TREE;
4498 :
4499 : /* A glvalue initializer might be significant to a reference constructor
4500 : or conversion operator. */
4501 352 : if (!DECL_CONSTRUCTOR_P (c->cand->fn)
4502 352 : || (TYPE_REF_P (TREE_VALUE
4503 : (FUNCTION_FIRST_USER_PARMTYPE (c->cand->fn)))))
4504 240 : for (auto &ce : CONSTRUCTOR_ELTS (init))
4505 108 : if (non_mergeable_glvalue_p (ce.value))
4506 : return NULL_TREE;
4507 :
4508 349 : tree first = CONSTRUCTOR_ELT (init, 0)->value;
4509 349 : conversion *fc = implicit_conversion (elttype, init_elttype, first, false,
4510 : LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING,
4511 : tf_none);
4512 349 : if (fc && fc->kind == ck_rvalue)
4513 48 : fc = next_conversion (fc);
4514 349 : if (!fc || fc->kind != ck_user || fc->cand->fn != c->cand->fn)
4515 : return NULL_TREE;
4516 307 : first = convert_like (fc, first, tf_none);
4517 307 : if (first == error_mark_node)
4518 : /* Let the normal code give the error. */
4519 : return NULL_TREE;
4520 :
4521 : /* Don't do this if the conversion would be constant. */
4522 304 : first = maybe_constant_init (first);
4523 304 : if (TREE_CONSTANT (first))
4524 : return NULL_TREE;
4525 :
4526 : /* We can't do this if the conversion creates temporaries that need
4527 : to live until the whole array is initialized. */
4528 193 : if (has_non_trivial_temporaries (first))
4529 : return NULL_TREE;
4530 :
4531 : /* We can't do this if copying from the initializer_list would be
4532 : ill-formed. */
4533 193 : tree copy_argtypes = make_tree_vec (1);
4534 193 : TREE_VEC_ELT (copy_argtypes, 0)
4535 193 : = cp_build_qualified_type (elttype, TYPE_QUAL_CONST);
4536 193 : if (!is_xible (INIT_EXPR, elttype, copy_argtypes))
4537 : return NULL_TREE;
4538 :
4539 187 : unsigned HOST_WIDE_INT len = count_ctor_elements (init);
4540 187 : tree arr = build_array_of_n_type (init_elttype, len);
4541 187 : arr = finish_compound_literal (arr, init, tf_none);
4542 187 : DECL_MERGEABLE (TARGET_EXPR_SLOT (arr)) = true;
4543 187 : return arr;
4544 367 : }
4545 :
4546 : /* If we were going to call e.g. vector(initializer_list<string>) starting
4547 : with a list of string-literals (which is inefficient, see PR105838),
4548 : instead build an array of const char* and pass it to the range constructor.
4549 : But only do this for standard library types, where we can assume the
4550 : transformation makes sense.
4551 :
4552 : Really the container classes should have initializer_list<U> constructors to
4553 : get the same effect more simply; this is working around that lack. */
4554 :
4555 : static tree
4556 14705669 : maybe_init_list_as_range (tree fn, tree expr)
4557 : {
4558 14705669 : if (!processing_template_decl
4559 14179714 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
4560 888683 : && is_list_ctor (fn)
4561 14707325 : && decl_in_std_namespace_p (fn))
4562 : {
4563 1503 : tree to = list_ctor_element_type (fn);
4564 1503 : if (tree init = maybe_init_list_as_array (to, expr))
4565 : {
4566 55 : tree begin = decay_conversion (TARGET_EXPR_SLOT (init), tf_none);
4567 55 : tree nelts = array_type_nelts_top (TREE_TYPE (init));
4568 55 : tree end = cp_build_binary_op (input_location, PLUS_EXPR, begin,
4569 : nelts, tf_none);
4570 55 : begin = cp_build_compound_expr (init, begin, tf_none);
4571 55 : return build_constructor_va (init_list_type_node, 2,
4572 55 : NULL_TREE, begin, NULL_TREE, end);
4573 : }
4574 : }
4575 :
4576 : return NULL_TREE;
4577 : }
4578 :
4579 : /* Returns the best overload candidate to perform the requested
4580 : conversion. This function is used for three the overloading situations
4581 : described in [over.match.copy], [over.match.conv], and [over.match.ref].
4582 : If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
4583 : per [dcl.init.ref], so we ignore temporary bindings. */
4584 :
4585 : static struct z_candidate *
4586 94422860 : build_user_type_conversion_1 (tree totype, tree expr, int flags,
4587 : tsubst_flags_t complain)
4588 : {
4589 94422860 : struct z_candidate *candidates, *cand;
4590 94422860 : tree fromtype;
4591 94422860 : tree ctors = NULL_TREE;
4592 94422860 : tree conv_fns = NULL_TREE;
4593 94422860 : conversion *conv = NULL;
4594 94422860 : tree first_arg = NULL_TREE;
4595 94422860 : vec<tree, va_gc> *args = NULL;
4596 94422860 : bool any_viable_p;
4597 94422860 : int convflags;
4598 :
4599 94422860 : if (!expr)
4600 : return NULL;
4601 :
4602 94422854 : fromtype = TREE_TYPE (expr);
4603 :
4604 : /* We represent conversion within a hierarchy using RVALUE_CONV and
4605 : BASE_CONV, as specified by [over.best.ics]; these become plain
4606 : constructor calls, as specified in [dcl.init]. */
4607 94422854 : gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
4608 : || !DERIVED_FROM_P (totype, fromtype));
4609 :
4610 94422854 : if (CLASS_TYPE_P (totype))
4611 : /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
4612 : creating a garbage BASELINK; constructors can't be inherited. */
4613 52012320 : ctors = get_class_binding (totype, complete_ctor_identifier);
4614 :
4615 94422854 : tree to_nonref = non_reference (totype);
4616 94422854 : if (MAYBE_CLASS_TYPE_P (fromtype))
4617 : {
4618 65932983 : if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
4619 65923394 : (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
4620 53686930 : && DERIVED_FROM_P (to_nonref, fromtype)))
4621 : {
4622 : /* [class.conv.fct] A conversion function is never used to
4623 : convert a (possibly cv-qualified) object to the (possibly
4624 : cv-qualified) same object type (or a reference to it), to a
4625 : (possibly cv-qualified) base class of that type (or a
4626 : reference to it)... */
4627 : }
4628 : else
4629 65923391 : conv_fns = lookup_conversions (fromtype);
4630 : }
4631 :
4632 94422854 : candidates = 0;
4633 94422854 : flags |= LOOKUP_NO_CONVERSION;
4634 94422854 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4635 931653 : flags |= LOOKUP_NO_NARROWING;
4636 : /* Prevent add_candidates from treating a non-strictly viable candidate
4637 : as unviable. */
4638 94422854 : complain |= tf_conv;
4639 :
4640 : /* It's OK to bind a temporary for converting constructor arguments, but
4641 : not in converting the return value of a conversion operator. */
4642 94422854 : convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION
4643 94422854 : | (flags & LOOKUP_NO_NARROWING));
4644 94422854 : flags &= ~LOOKUP_NO_TEMP_BIND;
4645 :
4646 94422854 : if (ctors)
4647 : {
4648 51796042 : int ctorflags = flags;
4649 :
4650 51796042 : first_arg = build_dummy_object (totype);
4651 :
4652 : /* We should never try to call the abstract or base constructor
4653 : from here. */
4654 363547870 : gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_FIRST (ctors))
4655 : && !DECL_HAS_VTT_PARM_P (OVL_FIRST (ctors)));
4656 :
4657 51796042 : args = make_tree_vector_single (expr);
4658 51796042 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4659 : {
4660 : /* List-initialization. */
4661 931653 : add_list_candidates (ctors, first_arg, args, totype, NULL_TREE,
4662 931653 : false, TYPE_BINFO (totype), TYPE_BINFO (totype),
4663 : ctorflags, &candidates, complain);
4664 : }
4665 : else
4666 : {
4667 50864389 : add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
4668 50864389 : TYPE_BINFO (totype), TYPE_BINFO (totype),
4669 : ctorflags, &candidates, complain);
4670 : }
4671 :
4672 286068112 : for (cand = candidates; cand; cand = cand->next)
4673 : {
4674 234272070 : cand->second_conv = build_identity_conv (totype, NULL_TREE);
4675 :
4676 : /* If totype isn't a reference, and LOOKUP_ONLYCONVERTING is
4677 : set, then this is copy-initialization. In that case, "The
4678 : result of the call is then used to direct-initialize the
4679 : object that is the destination of the copy-initialization."
4680 : [dcl.init]
4681 :
4682 : We represent this in the conversion sequence with an
4683 : rvalue conversion, which means a constructor call. */
4684 234272070 : if (!TYPE_REF_P (totype)
4685 234272070 : && cxx_dialect < cxx17
4686 416898 : && (flags & LOOKUP_ONLYCONVERTING)
4687 361013 : && !(convflags & LOOKUP_NO_TEMP_BIND))
4688 114060 : cand->second_conv
4689 114060 : = build_conv (ck_rvalue, totype, cand->second_conv);
4690 : }
4691 : }
4692 :
4693 94422854 : if (conv_fns)
4694 : {
4695 29779333 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4696 0 : first_arg = CONSTRUCTOR_ELT (expr, 0)->value;
4697 : else
4698 94422854 : first_arg = expr;
4699 : }
4700 :
4701 129103213 : for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
4702 : {
4703 34680359 : tree conversion_path = TREE_PURPOSE (conv_fns);
4704 34680359 : struct z_candidate *old_candidates;
4705 :
4706 : /* If LOOKUP_NO_CONVERSION, don't consider a conversion function that
4707 : would need an additional user-defined conversion, i.e. if the return
4708 : type differs in class-ness from the desired type. So we avoid
4709 : considering operator bool when calling a copy constructor.
4710 :
4711 : This optimization avoids the failure in PR97600, and is allowed by
4712 : [temp.inst]/9: "If the function selected by overload resolution can be
4713 : determined without instantiating a class template definition, it is
4714 : unspecified whether that instantiation actually takes place." */
4715 34680359 : tree convtype = non_reference (TREE_TYPE (conv_fns));
4716 51144562 : if ((flags & LOOKUP_NO_CONVERSION)
4717 34680359 : && !WILDCARD_TYPE_P (convtype)
4718 65811112 : && (CLASS_TYPE_P (to_nonref)
4719 32905556 : != CLASS_TYPE_P (convtype)))
4720 16464203 : continue;
4721 :
4722 : /* If we are called to convert to a reference type, we are trying to
4723 : find a direct binding, so don't even consider temporaries. If
4724 : we don't find a direct binding, the caller will try again to
4725 : look for a temporary binding. */
4726 18216156 : if (TYPE_REF_P (totype))
4727 5060541 : convflags |= LOOKUP_NO_TEMP_BIND;
4728 :
4729 18216156 : old_candidates = candidates;
4730 18216156 : add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
4731 : NULL_TREE, false,
4732 18216156 : conversion_path, TYPE_BINFO (fromtype),
4733 : flags, &candidates, complain);
4734 :
4735 36431599 : for (cand = candidates; cand != old_candidates; cand = cand->next)
4736 : {
4737 18215443 : if (cand->viable == 0)
4738 : /* Already rejected, don't change to -1. */
4739 6895070 : continue;
4740 :
4741 11320373 : tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
4742 11320373 : conversion *ics
4743 11320373 : = implicit_conversion (totype,
4744 : rettype,
4745 : 0,
4746 : /*c_cast_p=*/false, convflags,
4747 : complain);
4748 :
4749 : /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
4750 : copy-initialization. In that case, "The result of the
4751 : call is then used to direct-initialize the object that is
4752 : the destination of the copy-initialization." [dcl.init]
4753 :
4754 : We represent this in the conversion sequence with an
4755 : rvalue conversion, which means a constructor call. But
4756 : don't add a second rvalue conversion if there's already
4757 : one there. Which there really shouldn't be, but it's
4758 : harmless since we'd add it here anyway. */
4759 4321860 : if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
4760 12010468 : && !(convflags & LOOKUP_NO_TEMP_BIND))
4761 255236 : ics = build_conv (ck_rvalue, totype, ics);
4762 :
4763 11320373 : cand->second_conv = ics;
4764 :
4765 11320373 : if (!ics)
4766 : {
4767 6998513 : cand->viable = 0;
4768 13994521 : cand->reason = arg_conversion_rejection (NULL_TREE, -2,
4769 : rettype, totype,
4770 6998513 : EXPR_LOCATION (expr));
4771 : }
4772 14524 : else if (TYPE_REF_P (totype) && !ics->rvaluedness_matches_p
4773 : /* Limit this to non-templates for now (PR90546). */
4774 1896 : && !cand->template_decl
4775 4323751 : && TREE_CODE (TREE_TYPE (totype)) != FUNCTION_TYPE)
4776 : {
4777 : /* If we are called to convert to a reference type, we are trying
4778 : to find a direct binding per [over.match.ref], so rvaluedness
4779 : must match for non-functions. */
4780 1885 : cand->viable = 0;
4781 : }
4782 4319975 : else if (DECL_NONCONVERTING_P (cand->fn)
4783 4319975 : && ics->rank > cr_exact)
4784 : {
4785 : /* 13.3.1.5: For direct-initialization, those explicit
4786 : conversion functions that are not hidden within S and
4787 : yield type T or a type that can be converted to type T
4788 : with a qualification conversion (4.4) are also candidate
4789 : functions. */
4790 : /* 13.3.1.6 doesn't have a parallel restriction, but it should;
4791 : I've raised this issue with the committee. --jason 9/2011 */
4792 2715 : cand->viable = -1;
4793 2715 : cand->reason = explicit_conversion_rejection (rettype, totype);
4794 : }
4795 4317260 : else if (cand->viable == 1 && ics->bad_p)
4796 : {
4797 2576 : cand->viable = -1;
4798 2576 : cand->reason
4799 5152 : = bad_arg_conversion_rejection (NULL_TREE, -2,
4800 : rettype, totype,
4801 2576 : EXPR_LOCATION (expr));
4802 : }
4803 4314684 : else if (primary_template_specialization_p (cand->fn)
4804 4314684 : && ics->rank > cr_exact)
4805 : {
4806 : /* 13.3.3.1.2: If the user-defined conversion is specified by
4807 : a specialization of a conversion function template, the
4808 : second standard conversion sequence shall have exact match
4809 : rank. */
4810 21 : cand->viable = -1;
4811 21 : cand->reason = template_conversion_rejection (rettype, totype);
4812 : }
4813 : }
4814 : }
4815 :
4816 94422854 : candidates = splice_viable (candidates, false, &any_viable_p);
4817 94422854 : if (!any_viable_p)
4818 : {
4819 79715839 : if (args)
4820 40706453 : release_tree_vector (args);
4821 79715839 : return NULL;
4822 : }
4823 :
4824 14707015 : cand = tourney (candidates, complain);
4825 14707015 : if (cand == NULL)
4826 : {
4827 1346 : if (complain & tf_error)
4828 : {
4829 71 : auto_diagnostic_group d;
4830 74 : error_at (cp_expr_loc_or_input_loc (expr),
4831 : "conversion from %qH to %qI is ambiguous",
4832 : fromtype, totype);
4833 71 : print_z_candidates (location_of (expr), candidates);
4834 71 : }
4835 :
4836 1346 : cand = candidates; /* any one will do */
4837 1346 : cand->second_conv = build_ambiguous_conv (totype, expr);
4838 1346 : cand->second_conv->user_conv_p = true;
4839 2692 : if (!any_strictly_viable (candidates))
4840 12 : cand->second_conv->bad_p = true;
4841 1346 : if (flags & LOOKUP_ONLYCONVERTING)
4842 1273 : cand->second_conv->need_temporary_p = true;
4843 : /* If there are viable candidates, don't set ICS_BAD_FLAG; an
4844 : ambiguous conversion is no worse than another user-defined
4845 : conversion. */
4846 :
4847 1346 : return cand;
4848 : }
4849 :
4850 : /* Maybe pass { } as iterators instead of an initializer_list. */
4851 14705669 : if (tree iters = maybe_init_list_as_range (cand->fn, expr))
4852 110 : if (z_candidate *cand2
4853 55 : = build_user_type_conversion_1 (totype, iters, flags, tf_none))
4854 52 : if (cand2->viable == 1 && !is_list_ctor (cand2->fn))
4855 : {
4856 : cand = cand2;
4857 : expr = iters;
4858 : }
4859 :
4860 14705669 : tree convtype;
4861 29411338 : if (!DECL_CONSTRUCTOR_P (cand->fn))
4862 4307670 : convtype = non_reference (TREE_TYPE (TREE_TYPE (cand->fn)));
4863 10397999 : else if (cand->second_conv->kind == ck_rvalue)
4864 : /* DR 5: [in the first step of copy-initialization]...if the function
4865 : is a constructor, the call initializes a temporary of the
4866 : cv-unqualified version of the destination type. */
4867 11693 : convtype = cv_unqualified (totype);
4868 : else
4869 : convtype = totype;
4870 : /* Build the user conversion sequence. */
4871 14705669 : conv = build_conv
4872 14705669 : (ck_user,
4873 : convtype,
4874 14705669 : build_identity_conv (TREE_TYPE (expr), expr));
4875 14705669 : conv->cand = cand;
4876 14705669 : if (cand->viable == -1)
4877 11429 : conv->bad_p = true;
4878 :
4879 : /* Remember that this was a list-initialization. */
4880 14705669 : if (flags & LOOKUP_NO_NARROWING)
4881 3324419 : conv->check_narrowing = true;
4882 :
4883 : /* Combine it with the second conversion sequence. */
4884 14705669 : cand->second_conv = merge_conversion_sequences (conv,
4885 : cand->second_conv);
4886 :
4887 14705669 : return cand;
4888 : }
4889 :
4890 : /* Wrapper for above. */
4891 :
4892 : tree
4893 23706 : build_user_type_conversion (tree totype, tree expr, int flags,
4894 : tsubst_flags_t complain)
4895 : {
4896 23706 : struct z_candidate *cand;
4897 23706 : tree ret;
4898 :
4899 23706 : auto_cond_timevar tv (TV_OVERLOAD);
4900 :
4901 23706 : conversion_obstack_sentinel cos;
4902 :
4903 23706 : cand = build_user_type_conversion_1 (totype, expr, flags, complain);
4904 :
4905 23706 : if (cand)
4906 : {
4907 23476 : if (cand->second_conv->kind == ck_ambig)
4908 65 : ret = error_mark_node;
4909 : else
4910 : {
4911 23411 : expr = convert_like (cand->second_conv, expr, complain);
4912 23411 : ret = convert_from_reference (expr);
4913 : }
4914 : }
4915 : else
4916 : ret = NULL_TREE;
4917 :
4918 47412 : return ret;
4919 23706 : }
4920 :
4921 : /* Give a helpful diagnostic when implicit_conversion fails. */
4922 :
4923 : static void
4924 680 : implicit_conversion_error (location_t loc, tree type, tree expr,
4925 : int flags)
4926 : {
4927 680 : tsubst_flags_t complain = tf_warning_or_error;
4928 :
4929 : /* If expr has unknown type, then it is an overloaded function.
4930 : Call instantiate_type to get good error messages. */
4931 680 : if (TREE_TYPE (expr) == unknown_type_node)
4932 60 : instantiate_type (type, expr, complain);
4933 620 : else if (invalid_nonstatic_memfn_p (loc, expr, complain))
4934 : /* We gave an error. */;
4935 70 : else if (BRACE_ENCLOSED_INITIALIZER_P (expr)
4936 67 : && CONSTRUCTOR_IS_DESIGNATED_INIT (expr)
4937 633 : && !CP_AGGREGATE_TYPE_P (type))
4938 18 : error_at (loc, "designated initializers cannot be used with a "
4939 : "non-aggregate type %qT", type);
4940 : else
4941 : {
4942 597 : auto_diagnostic_group d;
4943 597 : if (is_stub_object (expr))
4944 : /* The expression is generated by a trait check, we don't have
4945 : a useful location to highlight the label. */
4946 17 : error_at (loc, "could not convert %qH to %qI",
4947 17 : TREE_TYPE (expr), type);
4948 : else
4949 : {
4950 580 : range_label_for_type_mismatch label (TREE_TYPE (expr), type);
4951 580 : gcc_rich_location rich_loc (loc, &label,
4952 580 : highlight_colors::percent_h);
4953 580 : error_at (&rich_loc, "could not convert %qE from %qH to %qI",
4954 580 : expr, TREE_TYPE (expr), type);
4955 580 : }
4956 597 : maybe_show_nonconverting_candidate (type, TREE_TYPE (expr), expr, flags);
4957 597 : }
4958 680 : }
4959 :
4960 : /* Worker for build_converted_constant_expr. */
4961 :
4962 : static tree
4963 321890581 : build_converted_constant_expr_internal (tree type, tree expr,
4964 : int flags, tsubst_flags_t complain)
4965 : {
4966 321890581 : conversion *conv;
4967 321890581 : tree t;
4968 321890581 : location_t loc = cp_expr_loc_or_input_loc (expr);
4969 :
4970 321890581 : if (error_operand_p (expr))
4971 65 : return error_mark_node;
4972 :
4973 321890516 : conversion_obstack_sentinel cos;
4974 :
4975 321890516 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
4976 : /*c_cast_p=*/false, flags, complain);
4977 :
4978 : /* A converted constant expression of type T is an expression, implicitly
4979 : converted to type T, where the converted expression is a constant
4980 : expression and the implicit conversion sequence contains only
4981 :
4982 : * user-defined conversions,
4983 : * lvalue-to-rvalue conversions (7.1),
4984 : * array-to-pointer conversions (7.2),
4985 : * function-to-pointer conversions (7.3),
4986 : * qualification conversions (7.5),
4987 : * integral promotions (7.6),
4988 : * integral conversions (7.8) other than narrowing conversions (11.6.4),
4989 : * null pointer conversions (7.11) from std::nullptr_t,
4990 : * null member pointer conversions (7.12) from std::nullptr_t, and
4991 : * function pointer conversions (7.13),
4992 :
4993 : and where the reference binding (if any) binds directly. */
4994 :
4995 321890516 : for (conversion *c = conv;
4996 372287249 : c && c->kind != ck_identity;
4997 50396733 : c = next_conversion (c))
4998 : {
4999 50396733 : switch (c->kind)
5000 : {
5001 : /* A conversion function is OK. If it isn't constexpr, we'll
5002 : complain later that the argument isn't constant. */
5003 : case ck_user:
5004 : /* List-initialization is OK. */
5005 : case ck_aggr:
5006 : /* The lvalue-to-rvalue conversion is OK. */
5007 : case ck_rvalue:
5008 : /* Array-to-pointer and function-to-pointer. */
5009 : case ck_lvalue:
5010 : /* Function pointer conversions. */
5011 : case ck_fnptr:
5012 : /* Qualification conversions. */
5013 : case ck_qual:
5014 : break;
5015 :
5016 763 : case ck_ref_bind:
5017 763 : if (c->need_temporary_p)
5018 : {
5019 6 : if (complain & tf_error)
5020 1 : error_at (loc, "initializing %qH with %qI in converted "
5021 : "constant expression does not bind directly",
5022 1 : type, next_conversion (c)->type);
5023 : conv = NULL;
5024 : }
5025 : break;
5026 :
5027 9283999 : case ck_base:
5028 9283999 : case ck_pmem:
5029 9283999 : case ck_ptr:
5030 9283999 : case ck_std:
5031 9283999 : t = next_conversion (c)->type;
5032 9283999 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (t)
5033 9283919 : && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5034 : /* Integral promotion or conversion. */
5035 : break;
5036 114 : if (NULLPTR_TYPE_P (t))
5037 : /* Conversion from nullptr to pointer or pointer-to-member. */
5038 : break;
5039 :
5040 114 : if (complain & tf_error)
5041 82 : error_at (loc, "conversion from %qH to %qI in a "
5042 : "converted constant expression", t, type);
5043 : /* fall through. */
5044 :
5045 : default:
5046 : conv = NULL;
5047 : break;
5048 : }
5049 : }
5050 :
5051 : /* Avoid confusing convert_nontype_argument by introducing
5052 : a redundant conversion to the same reference type. */
5053 321889044 : if (conv && conv->kind == ck_ref_bind
5054 321891273 : && REFERENCE_REF_P (expr))
5055 : {
5056 487 : tree ref = TREE_OPERAND (expr, 0);
5057 487 : if (same_type_p (type, TREE_TYPE (ref)))
5058 : return ref;
5059 : }
5060 :
5061 321890051 : if (conv)
5062 : {
5063 : /* Don't copy a class in a template. */
5064 67409 : if (CLASS_TYPE_P (type) && conv->kind == ck_rvalue
5065 321923730 : && processing_template_decl)
5066 393 : conv = next_conversion (conv);
5067 :
5068 : /* Issuing conversion warnings for value-dependent expressions is
5069 : likely too noisy. */
5070 321888579 : warning_sentinel w (warn_conversion);
5071 321888579 : conv->check_narrowing = true;
5072 321888579 : conv->check_narrowing_const_only = true;
5073 321888579 : expr = convert_like (conv, expr, complain);
5074 321888579 : }
5075 : else
5076 : {
5077 1472 : if (complain & tf_error)
5078 194 : implicit_conversion_error (loc, type, expr, flags);
5079 1472 : expr = error_mark_node;
5080 : }
5081 :
5082 : return expr;
5083 321890516 : }
5084 :
5085 : /* Subroutine of convert_nontype_argument.
5086 :
5087 : EXPR is an expression used in a context that requires a converted
5088 : constant-expression, such as a template non-type parameter. Do any
5089 : necessary conversions (that are permitted for converted
5090 : constant-expressions) to convert it to the desired type.
5091 :
5092 : This function doesn't consider explicit conversion functions. If
5093 : you mean to use "a contextually converted constant expression of type
5094 : bool", use build_converted_constant_bool_expr.
5095 :
5096 : If conversion is successful, returns the converted expression;
5097 : otherwise, returns error_mark_node. */
5098 :
5099 : tree
5100 188205181 : build_converted_constant_expr (tree type, tree expr, tsubst_flags_t complain)
5101 : {
5102 188205181 : return build_converted_constant_expr_internal (type, expr, LOOKUP_IMPLICIT,
5103 188205181 : complain);
5104 : }
5105 :
5106 : /* Used to create "a contextually converted constant expression of type
5107 : bool". This differs from build_converted_constant_expr in that it
5108 : also considers explicit conversion functions. */
5109 :
5110 : tree
5111 133685400 : build_converted_constant_bool_expr (tree expr, tsubst_flags_t complain)
5112 : {
5113 133685400 : return build_converted_constant_expr_internal (boolean_type_node, expr,
5114 133685400 : LOOKUP_NORMAL, complain);
5115 : }
5116 :
5117 : /* Do any initial processing on the arguments to a function call. */
5118 :
5119 : vec<tree, va_gc> *
5120 207087599 : resolve_args (vec<tree, va_gc> *args, tsubst_flags_t complain)
5121 : {
5122 207087599 : unsigned int ix;
5123 207087599 : tree arg;
5124 :
5125 383288456 : FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
5126 : {
5127 176201600 : if (error_operand_p (arg))
5128 : return NULL;
5129 176201039 : else if (VOID_TYPE_P (TREE_TYPE (arg)))
5130 : {
5131 155 : if (complain & tf_error)
5132 12 : error_at (cp_expr_loc_or_input_loc (arg),
5133 : "invalid use of void expression");
5134 155 : return NULL;
5135 : }
5136 176200884 : else if (invalid_nonstatic_memfn_p (EXPR_LOCATION (arg), arg, complain))
5137 : return NULL;
5138 :
5139 : /* Force auto deduction now. Omit tf_warning to avoid redundant
5140 : deprecated warning on deprecated-14.C. */
5141 176200857 : if (!mark_single_function (arg, complain & ~tf_warning))
5142 : return NULL;
5143 : }
5144 : return args;
5145 : }
5146 :
5147 : /* Perform overload resolution on FN, which is called with the ARGS.
5148 :
5149 : Return the candidate function selected by overload resolution, or
5150 : NULL if the event that overload resolution failed. In the case
5151 : that overload resolution fails, *CANDIDATES will be the set of
5152 : candidates considered, and ANY_VIABLE_P will be set to true or
5153 : false to indicate whether or not any of the candidates were
5154 : viable.
5155 :
5156 : The ARGS should already have gone through RESOLVE_ARGS before this
5157 : function is called. */
5158 :
5159 : static struct z_candidate *
5160 101867655 : perform_overload_resolution (tree fn,
5161 : const vec<tree, va_gc> *args,
5162 : struct z_candidate **candidates,
5163 : bool *any_viable_p, tsubst_flags_t complain)
5164 : {
5165 101867655 : struct z_candidate *cand;
5166 101867655 : tree explicit_targs;
5167 101867655 : int template_only;
5168 :
5169 101867655 : auto_cond_timevar tv (TV_OVERLOAD);
5170 :
5171 101867655 : explicit_targs = NULL_TREE;
5172 101867655 : template_only = 0;
5173 :
5174 101867655 : *candidates = NULL;
5175 101867655 : *any_viable_p = true;
5176 :
5177 : /* Check FN. */
5178 101867655 : gcc_assert (OVL_P (fn) || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
5179 :
5180 101867655 : if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
5181 : {
5182 35841947 : explicit_targs = TREE_OPERAND (fn, 1);
5183 35841947 : fn = TREE_OPERAND (fn, 0);
5184 35841947 : template_only = 1;
5185 : }
5186 :
5187 : /* Add the various candidate functions. */
5188 101867655 : add_candidates (fn, NULL_TREE, args, NULL_TREE,
5189 : explicit_targs, template_only,
5190 : /*conversion_path=*/NULL_TREE,
5191 : /*access_path=*/NULL_TREE,
5192 : LOOKUP_NORMAL,
5193 : candidates, complain & ~tf_any_viable);
5194 :
5195 101854137 : *candidates = splice_viable (*candidates, false, any_viable_p);
5196 101854137 : if (*any_viable_p)
5197 : {
5198 101667666 : if (complain & tf_any_viable)
5199 : cand = *candidates;
5200 : else
5201 101667480 : cand = tourney (*candidates, complain);
5202 : }
5203 : else
5204 : cand = NULL;
5205 :
5206 203708274 : return cand;
5207 101854137 : }
5208 :
5209 : /* Print an error message about being unable to build a call to FN with
5210 : ARGS. ANY_VIABLE_P indicates whether any candidate functions could
5211 : be located; CANDIDATES is a possibly empty list of such
5212 : functions. */
5213 :
5214 : static void
5215 2996 : print_error_for_call_failure (tree fn, const vec<tree, va_gc> *args,
5216 : struct z_candidate *candidates)
5217 : {
5218 2996 : tree targs = NULL_TREE;
5219 2996 : if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
5220 : {
5221 548 : targs = TREE_OPERAND (fn, 1);
5222 548 : fn = TREE_OPERAND (fn, 0);
5223 : }
5224 5992 : tree name = OVL_NAME (fn);
5225 2996 : location_t loc = location_of (name);
5226 2996 : if (targs)
5227 548 : name = lookup_template_function (name, targs);
5228 :
5229 2996 : auto_diagnostic_group d;
5230 5992 : if (!any_strictly_viable (candidates))
5231 2512 : error_at (loc, "no matching function for call to %<%D(%A)%>",
5232 : name, build_tree_list_vec (args));
5233 : else
5234 484 : error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous",
5235 : name, build_tree_list_vec (args));
5236 2996 : if (candidates)
5237 2996 : print_z_candidates (loc, candidates);
5238 2996 : }
5239 :
5240 : /* Perform overload resolution on the set of deduction guides DGUIDES
5241 : using ARGS. Returns the selected deduction guide, or error_mark_node
5242 : if overload resolution fails. */
5243 :
5244 : tree
5245 31165 : perform_dguide_overload_resolution (tree dguides, const vec<tree, va_gc> *args,
5246 : tsubst_flags_t complain)
5247 : {
5248 31165 : z_candidate *candidates;
5249 31165 : bool any_viable_p;
5250 31165 : tree result;
5251 :
5252 62330 : gcc_assert (deduction_guide_p (OVL_FIRST (dguides)));
5253 :
5254 31165 : conversion_obstack_sentinel cos;
5255 :
5256 31165 : z_candidate *cand = perform_overload_resolution (dguides, args, &candidates,
5257 : &any_viable_p, complain);
5258 31165 : if (!cand)
5259 : {
5260 969 : if (complain & tf_error)
5261 198 : print_error_for_call_failure (dguides, args, candidates);
5262 969 : result = error_mark_node;
5263 : }
5264 : else
5265 30196 : result = cand->fn;
5266 :
5267 62330 : return result;
5268 31165 : }
5269 :
5270 : /* Return an expression for a call to FN (a namespace-scope function,
5271 : or a static member function) with the ARGS. This may change
5272 : ARGS. */
5273 :
5274 : tree
5275 100778422 : build_new_function_call (tree fn, vec<tree, va_gc> **args,
5276 : tsubst_flags_t complain)
5277 : {
5278 100778422 : struct z_candidate *candidates, *cand;
5279 100778422 : bool any_viable_p;
5280 100778422 : tree result;
5281 :
5282 100778422 : if (args != NULL && *args != NULL)
5283 : {
5284 100778422 : *args = resolve_args (*args, complain & ~tf_any_viable);
5285 100778422 : if (*args == NULL)
5286 431 : return error_mark_node;
5287 : }
5288 :
5289 100777991 : if (flag_tm)
5290 3544 : tm_malloc_replacement (fn);
5291 :
5292 100777991 : conversion_obstack_sentinel cos;
5293 :
5294 100777991 : cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
5295 : complain);
5296 :
5297 100764473 : if (!cand)
5298 : {
5299 198424 : if (complain & tf_error)
5300 : {
5301 : // If there is a single (non-viable) function candidate,
5302 : // let the error be diagnosed by cp_build_function_call_vec.
5303 3212 : if (!any_viable_p && candidates && ! candidates->next
5304 1428 : && TREE_CODE (candidates->fn) == FUNCTION_DECL
5305 : /* A template-id callee consisting of a single (ignored)
5306 : non-template candidate needs to be diagnosed the
5307 : ordinary way. */
5308 435 : && (TREE_CODE (fn) != TEMPLATE_ID_EXPR
5309 33 : || candidates->template_decl))
5310 423 : return cp_build_function_call_vec (candidates->fn, args, complain);
5311 :
5312 : // Otherwise, emit notes for non-viable candidates.
5313 2789 : print_error_for_call_failure (fn, *args, candidates);
5314 : }
5315 198001 : result = error_mark_node;
5316 : }
5317 100566049 : else if (complain & tf_any_viable)
5318 186 : return void_node;
5319 : else
5320 100565863 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
5321 :
5322 100763864 : if (flag_coroutines
5323 98704478 : && result
5324 98704478 : && TREE_CODE (result) == CALL_EXPR
5325 160937685 : && DECL_BUILT_IN_CLASS (TREE_OPERAND (CALL_EXPR_FN (result), 0))
5326 60173821 : == BUILT_IN_NORMAL)
5327 7973783 : result = coro_validate_builtin_call (result);
5328 :
5329 : return result;
5330 100764473 : }
5331 :
5332 : /* Build a call to a global operator new. FNNAME is the name of the
5333 : operator (either "operator new" or "operator new[]") and ARGS are
5334 : the arguments provided. This may change ARGS. *SIZE points to the
5335 : total number of bytes required by the allocation, and is updated if
5336 : that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
5337 : be used. If this function determines that no cookie should be
5338 : used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK
5339 : is not NULL_TREE, it is evaluated before calculating the final
5340 : array size, and if it fails, the array size is replaced with
5341 : (size_t)-1 (usually triggering a std::bad_alloc exception). If FN
5342 : is non-NULL, it will be set, upon return, to the allocation
5343 : function called. */
5344 :
5345 : tree
5346 1058476 : build_operator_new_call (tree fnname, vec<tree, va_gc> **args,
5347 : tree *size, tree *cookie_size,
5348 : tree align_arg, tree size_check,
5349 : tree *fn, tsubst_flags_t complain)
5350 : {
5351 1058476 : tree original_size = *size;
5352 1058476 : tree fns;
5353 1058476 : struct z_candidate *candidates;
5354 1058476 : struct z_candidate *cand = NULL;
5355 1058476 : bool any_viable_p;
5356 :
5357 1058476 : if (fn)
5358 1057025 : *fn = NULL_TREE;
5359 : /* Set to (size_t)-1 if the size check fails. */
5360 1058476 : if (size_check != NULL_TREE)
5361 : {
5362 20705 : tree errval = TYPE_MAX_VALUE (sizetype);
5363 20705 : if (cxx_dialect >= cxx11 && flag_exceptions)
5364 20338 : errval = throw_bad_array_new_length ();
5365 20705 : *size = fold_build3 (COND_EXPR, sizetype, size_check,
5366 : original_size, errval);
5367 : }
5368 1058476 : vec_safe_insert (*args, 0, *size);
5369 1058476 : *args = resolve_args (*args, complain);
5370 1058476 : if (*args == NULL)
5371 0 : return error_mark_node;
5372 :
5373 1058476 : conversion_obstack_sentinel cos;
5374 :
5375 : /* Based on:
5376 :
5377 : [expr.new]
5378 :
5379 : If this lookup fails to find the name, or if the allocated type
5380 : is not a class type, the allocation function's name is looked
5381 : up in the global scope.
5382 :
5383 : we disregard block-scope declarations of "operator new". */
5384 1058476 : fns = lookup_qualified_name (global_namespace, fnname);
5385 :
5386 1058476 : if (align_arg)
5387 : {
5388 9296 : vec<tree, va_gc>* align_args
5389 9296 : = vec_copy_and_insert (*args, align_arg, 1);
5390 9296 : cand = perform_overload_resolution (fns, align_args, &candidates,
5391 : &any_viable_p, tf_none);
5392 9296 : if (cand)
5393 9273 : *args = align_args;
5394 : /* If no aligned allocation function matches, try again without the
5395 : alignment. */
5396 : }
5397 :
5398 : /* Figure out what function is being called. */
5399 9273 : if (!cand)
5400 1049203 : cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p,
5401 : complain);
5402 :
5403 : /* If no suitable function could be found, issue an error message
5404 : and give up. */
5405 1058476 : if (!cand)
5406 : {
5407 9 : if (complain & tf_error)
5408 9 : print_error_for_call_failure (fns, *args, candidates);
5409 9 : return error_mark_node;
5410 : }
5411 :
5412 : /* If a cookie is required, add some extra space. Whether
5413 : or not a cookie is required cannot be determined until
5414 : after we know which function was called. */
5415 1058467 : if (*cookie_size)
5416 : {
5417 268 : bool use_cookie = true;
5418 268 : tree arg_types;
5419 :
5420 268 : arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
5421 : /* Skip the size_t parameter. */
5422 268 : arg_types = TREE_CHAIN (arg_types);
5423 : /* Check the remaining parameters (if any). */
5424 268 : if (arg_types
5425 268 : && TREE_CHAIN (arg_types) == void_list_node
5426 331 : && same_type_p (TREE_VALUE (arg_types),
5427 : ptr_type_node))
5428 48 : use_cookie = false;
5429 : /* If we need a cookie, adjust the number of bytes allocated. */
5430 268 : if (use_cookie)
5431 : {
5432 : /* Update the total size. */
5433 220 : *size = size_binop (PLUS_EXPR, original_size, *cookie_size);
5434 220 : if (size_check)
5435 : {
5436 : /* Set to (size_t)-1 if the size check fails. */
5437 47 : gcc_assert (size_check != NULL_TREE);
5438 47 : *size = fold_build3 (COND_EXPR, sizetype, size_check,
5439 : *size, TYPE_MAX_VALUE (sizetype));
5440 : }
5441 : /* Update the argument list to reflect the adjusted size. */
5442 220 : (**args)[0] = *size;
5443 : }
5444 : else
5445 48 : *cookie_size = NULL_TREE;
5446 : }
5447 :
5448 : /* Tell our caller which function we decided to call. */
5449 1058467 : if (fn)
5450 1057016 : *fn = cand->fn;
5451 :
5452 : /* Build the CALL_EXPR. */
5453 1058467 : tree ret = build_over_call (cand, LOOKUP_NORMAL, complain);
5454 :
5455 : /* Set this flag for all callers of this function. In addition to
5456 : new-expressions, this is called for allocating coroutine state; treat
5457 : that as an implicit new-expression. */
5458 1058467 : tree call = extract_call_expr (ret);
5459 1058467 : if (TREE_CODE (call) == CALL_EXPR)
5460 1058467 : CALL_FROM_NEW_OR_DELETE_P (call) = 1;
5461 :
5462 : return ret;
5463 1058476 : }
5464 :
5465 : /* Evaluate side-effects from OBJ before evaluating call
5466 : to FN in RESULT expression.
5467 : This is for expressions of the form `obj->fn(...)'
5468 : where `fn' turns out to be a static member function and
5469 : `obj' needs to be evaluated. `fn' could be also static operator[]
5470 : or static operator(), in which cases the source expression
5471 : would be `obj[...]' or `obj(...)'. */
5472 :
5473 : tree
5474 78841613 : keep_unused_object_arg (tree result, tree obj, tree fn)
5475 : {
5476 78841613 : if (result == NULL_TREE
5477 78841613 : || result == error_mark_node
5478 77852510 : || DECL_OBJECT_MEMBER_FUNCTION_P (fn)
5479 81534168 : || !TREE_SIDE_EFFECTS (obj))
5480 : return result;
5481 :
5482 : /* But avoid the implicit lvalue-rvalue conversion when `obj' is
5483 : volatile. */
5484 88146 : tree a = obj;
5485 88146 : if (TREE_THIS_VOLATILE (a))
5486 57288 : a = build_this (a);
5487 88146 : if (TREE_SIDE_EFFECTS (a))
5488 30867 : return cp_build_compound_expr (a, result, tf_error);
5489 : return result;
5490 : }
5491 :
5492 : /* Build a new call to operator(). This may change ARGS. */
5493 :
5494 : tree
5495 2580848 : build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
5496 : {
5497 2580848 : struct z_candidate *candidates = 0, *cand;
5498 2580848 : tree fns, convs, first_mem_arg = NULL_TREE;
5499 2580848 : bool any_viable_p;
5500 2580848 : tree result = NULL_TREE;
5501 :
5502 2580848 : auto_cond_timevar tv (TV_OVERLOAD);
5503 :
5504 2580848 : obj = mark_lvalue_use (obj);
5505 :
5506 2580848 : if (error_operand_p (obj))
5507 0 : return error_mark_node;
5508 :
5509 2580848 : tree type = TREE_TYPE (obj);
5510 :
5511 2580848 : obj = prep_operand (obj);
5512 :
5513 2580848 : if (TYPE_PTRMEMFUNC_P (type))
5514 : {
5515 0 : if (complain & tf_error)
5516 : /* It's no good looking for an overloaded operator() on a
5517 : pointer-to-member-function. */
5518 0 : error ("pointer-to-member function %qE cannot be called without "
5519 : "an object; consider using %<.*%> or %<->*%>", obj);
5520 0 : return error_mark_node;
5521 : }
5522 :
5523 2580848 : if (TYPE_BINFO (type))
5524 : {
5525 2580824 : fns = lookup_fnfields (TYPE_BINFO (type), call_op_identifier, 1, complain);
5526 2580824 : if (fns == error_mark_node)
5527 : return error_mark_node;
5528 : }
5529 : else
5530 : fns = NULL_TREE;
5531 :
5532 2580838 : if (args != NULL && *args != NULL)
5533 : {
5534 2580838 : *args = resolve_args (*args, complain);
5535 2580838 : if (*args == NULL)
5536 110 : return error_mark_node;
5537 : }
5538 :
5539 2580728 : conversion_obstack_sentinel cos;
5540 :
5541 2580728 : if (fns)
5542 : {
5543 2575379 : first_mem_arg = obj;
5544 :
5545 2575379 : add_candidates (BASELINK_FUNCTIONS (fns),
5546 : first_mem_arg, *args, NULL_TREE,
5547 : NULL_TREE, false,
5548 2575379 : BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
5549 : LOOKUP_NORMAL, &candidates, complain);
5550 : }
5551 :
5552 2580728 : bool any_call_ops = candidates != nullptr;
5553 :
5554 2580728 : convs = lookup_conversions (type);
5555 :
5556 2771876 : for (; convs; convs = TREE_CHAIN (convs))
5557 : {
5558 191148 : tree totype = TREE_TYPE (convs);
5559 :
5560 157697 : if (TYPE_PTRFN_P (totype)
5561 33454 : || TYPE_REFFN_P (totype)
5562 224558 : || (TYPE_REF_P (totype)
5563 1188 : && TYPE_PTRFN_P (TREE_TYPE (totype))))
5564 315918 : for (tree fn : ovl_range (TREE_VALUE (convs)))
5565 : {
5566 157959 : if (DECL_NONCONVERTING_P (fn))
5567 3 : continue;
5568 :
5569 157956 : if (TREE_CODE (fn) == TEMPLATE_DECL)
5570 : {
5571 : /* Making this work broke PR 71117 and 85118, so until the
5572 : committee resolves core issue 2189, let's disable this
5573 : candidate if there are any call operators. */
5574 39385 : if (any_call_ops)
5575 39373 : continue;
5576 :
5577 12 : add_template_conv_candidate
5578 12 : (&candidates, fn, obj, *args, totype,
5579 : /*access_path=*/NULL_TREE,
5580 : /*conversion_path=*/NULL_TREE, complain);
5581 : }
5582 : else
5583 118571 : add_conv_candidate (&candidates, fn, obj,
5584 : *args, /*conversion_path=*/NULL_TREE,
5585 : /*access_path=*/NULL_TREE, complain);
5586 : }
5587 : }
5588 :
5589 : /* Be strict here because if we choose a bad conversion candidate, the
5590 : errors we get won't mention the call context. */
5591 2580728 : candidates = splice_viable (candidates, true, &any_viable_p);
5592 2580728 : if (!any_viable_p)
5593 : {
5594 40157 : if (complain & tf_error)
5595 : {
5596 282 : auto_diagnostic_group d;
5597 282 : error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
5598 : build_tree_list_vec (*args));
5599 282 : print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
5600 282 : }
5601 40157 : result = error_mark_node;
5602 : }
5603 : else
5604 : {
5605 2540571 : cand = tourney (candidates, complain);
5606 2540571 : if (cand == 0)
5607 : {
5608 22 : if (complain & tf_error)
5609 : {
5610 6 : auto_diagnostic_group d;
5611 12 : error ("call of %<(%T) (%A)%> is ambiguous",
5612 6 : TREE_TYPE (obj), build_tree_list_vec (*args));
5613 6 : print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
5614 6 : }
5615 22 : result = error_mark_node;
5616 : }
5617 2540549 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL
5618 2540490 : && DECL_OVERLOADED_OPERATOR_P (cand->fn)
5619 5081039 : && DECL_OVERLOADED_OPERATOR_IS (cand->fn, CALL_EXPR))
5620 : {
5621 2540490 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
5622 : /* In an expression of the form `a()' where cand->fn
5623 : which is operator() turns out to be a static member function,
5624 : `a' is none-the-less evaluated. */
5625 2540490 : result = keep_unused_object_arg (result, obj, cand->fn);
5626 : }
5627 : else
5628 : {
5629 59 : if (TREE_CODE (cand->fn) == FUNCTION_DECL)
5630 0 : obj = convert_like_with_context (cand->convs[0], obj, cand->fn,
5631 : -1, complain);
5632 : else
5633 : {
5634 59 : gcc_checking_assert (TYPE_P (cand->fn));
5635 59 : obj = convert_like (cand->convs[0], obj, complain);
5636 : }
5637 59 : obj = convert_from_reference (obj);
5638 59 : result = cp_build_function_call_vec (obj, args, complain);
5639 : }
5640 : }
5641 :
5642 2580728 : return result;
5643 2580848 : }
5644 :
5645 : /* Subroutine for preparing format strings suitable for the error
5646 : function. It concatenates a prefix (controlled by MATCH), ERRMSG,
5647 : and SUFFIX. */
5648 :
5649 : static const char *
5650 1466 : concat_op_error_string (bool match, const char *errmsg, const char *suffix)
5651 : {
5652 1466 : return concat (match
5653 : ? G_("ambiguous overload for ")
5654 : : G_("no match for "),
5655 0 : errmsg, suffix, nullptr);
5656 : }
5657 :
5658 : /* Called by op_error to prepare format strings suitable for the error
5659 : function. It concatenates a prefix (controlled by MATCH), ERRMSG,
5660 : and a suffix (controlled by NTYPES). */
5661 :
5662 : static const char *
5663 1445 : op_error_string (const char *errmsg, int ntypes, bool match)
5664 : {
5665 1445 : const char *suffix;
5666 0 : if (ntypes == 3)
5667 : suffix = G_(" (operand types are %qT, %qT, and %qT)");
5668 0 : else if (ntypes == 2)
5669 : suffix = G_(" (operand types are %qT and %qT)");
5670 : else
5671 0 : suffix = G_(" (operand type is %qT)");
5672 0 : return concat_op_error_string (match, errmsg, suffix);
5673 : }
5674 :
5675 : /* Similar to op_error_string, but a special-case for binary ops that
5676 : use %e for the args, rather than %qT. */
5677 :
5678 : static const char *
5679 21 : binop_error_string (const char *errmsg, bool match)
5680 : {
5681 21 : return concat_op_error_string (match, errmsg,
5682 21 : G_(" (operand types are %e and %e)"));
5683 : }
5684 :
5685 : static void
5686 1466 : op_error (const op_location_t &loc,
5687 : enum tree_code code, enum tree_code code2,
5688 : tree arg1, tree arg2, tree arg3, bool match)
5689 : {
5690 1466 : bool assop = code == MODIFY_EXPR;
5691 1466 : const char *opname = OVL_OP_INFO (assop, assop ? code2 : code)->name;
5692 :
5693 1466 : switch (code)
5694 : {
5695 0 : case COND_EXPR:
5696 0 : if (flag_diagnostics_show_caret)
5697 0 : error_at (loc, op_error_string (G_("ternary %<operator?:%>"),
5698 : 3, match),
5699 0 : TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
5700 : else
5701 0 : error_at (loc, op_error_string (G_("ternary %<operator?:%> "
5702 : "in %<%E ? %E : %E%>"), 3, match),
5703 : arg1, arg2, arg3,
5704 0 : TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
5705 : break;
5706 :
5707 0 : case POSTINCREMENT_EXPR:
5708 0 : case POSTDECREMENT_EXPR:
5709 0 : if (flag_diagnostics_show_caret)
5710 0 : error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
5711 0 : opname, TREE_TYPE (arg1));
5712 : else
5713 0 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
5714 : 1, match),
5715 0 : opname, arg1, opname, TREE_TYPE (arg1));
5716 : break;
5717 :
5718 13 : case ARRAY_REF:
5719 13 : if (flag_diagnostics_show_caret)
5720 0 : error_at (loc, op_error_string (G_("%<operator[]%>"), 2, match),
5721 0 : TREE_TYPE (arg1), TREE_TYPE (arg2));
5722 : else
5723 26 : error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
5724 : 2, match),
5725 13 : arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2));
5726 : break;
5727 :
5728 6 : case REALPART_EXPR:
5729 6 : case IMAGPART_EXPR:
5730 6 : if (flag_diagnostics_show_caret)
5731 0 : error_at (loc, op_error_string (G_("%qs"), 1, match),
5732 0 : opname, TREE_TYPE (arg1));
5733 : else
5734 12 : error_at (loc, op_error_string (G_("%qs in %<%s %E%>"), 1, match),
5735 6 : opname, opname, arg1, TREE_TYPE (arg1));
5736 : break;
5737 :
5738 0 : case CO_AWAIT_EXPR:
5739 0 : if (flag_diagnostics_show_caret)
5740 0 : error_at (loc, op_error_string (G_("%<operator %s%>"), 1, match),
5741 0 : opname, TREE_TYPE (arg1));
5742 : else
5743 0 : error_at (loc, op_error_string (G_("%<operator %s%> in %<%s%E%>"),
5744 : 1, match),
5745 0 : opname, opname, arg1, TREE_TYPE (arg1));
5746 : break;
5747 :
5748 1447 : default:
5749 1447 : if (arg2)
5750 1348 : if (flag_diagnostics_show_caret)
5751 : {
5752 21 : binary_op_rich_location richloc (loc, arg1, arg2, true);
5753 21 : pp_markup::element_quoted_type element_0
5754 21 : (TREE_TYPE (arg1), highlight_colors::lhs);
5755 21 : pp_markup::element_quoted_type element_1
5756 21 : (TREE_TYPE (arg2), highlight_colors::rhs);
5757 21 : error_at (&richloc,
5758 : binop_error_string (G_("%<operator%s%>"), match),
5759 : opname, &element_0, &element_1);
5760 21 : }
5761 : else
5762 2654 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
5763 : 2, match),
5764 : opname, arg1, opname, arg2,
5765 1327 : TREE_TYPE (arg1), TREE_TYPE (arg2));
5766 : else
5767 99 : if (flag_diagnostics_show_caret)
5768 0 : error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
5769 0 : opname, TREE_TYPE (arg1));
5770 : else
5771 198 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
5772 : 1, match),
5773 99 : opname, opname, arg1, TREE_TYPE (arg1));
5774 : break;
5775 : }
5776 1466 : }
5777 :
5778 : /* Return the implicit conversion sequence that could be used to
5779 : convert E1 to E2 in [expr.cond]. */
5780 :
5781 : static conversion *
5782 509862 : conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
5783 : {
5784 509862 : tree t1 = non_reference (TREE_TYPE (e1));
5785 509862 : tree t2 = non_reference (TREE_TYPE (e2));
5786 509862 : conversion *conv;
5787 509862 : bool good_base;
5788 :
5789 : /* [expr.cond]
5790 :
5791 : If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
5792 : implicitly converted (clause _conv_) to the type "lvalue reference to
5793 : T2", subject to the constraint that in the conversion the
5794 : reference must bind directly (_dcl.init.ref_) to an lvalue.
5795 :
5796 : If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
5797 : implicitly converted to the type "rvalue reference to T2", subject to
5798 : the constraint that the reference must bind directly. */
5799 509862 : if (glvalue_p (e2))
5800 : {
5801 499989 : tree rtype = cp_build_reference_type (t2, !lvalue_p (e2));
5802 499989 : conv = implicit_conversion (rtype,
5803 : t1,
5804 : e1,
5805 : /*c_cast_p=*/false,
5806 : LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
5807 : |LOOKUP_ONLYCONVERTING,
5808 : complain);
5809 499989 : if (conv && !conv->bad_p)
5810 : return conv;
5811 : }
5812 :
5813 : /* If E2 is a prvalue or if neither of the conversions above can be done
5814 : and at least one of the operands has (possibly cv-qualified) class
5815 : type: */
5816 401201 : if (!CLASS_TYPE_P (t1) && !CLASS_TYPE_P (t2))
5817 : return NULL;
5818 :
5819 : /* [expr.cond]
5820 :
5821 : If E1 and E2 have class type, and the underlying class types are
5822 : the same or one is a base class of the other: E1 can be converted
5823 : to match E2 if the class of T2 is the same type as, or a base
5824 : class of, the class of T1, and the cv-qualification of T2 is the
5825 : same cv-qualification as, or a greater cv-qualification than, the
5826 : cv-qualification of T1. If the conversion is applied, E1 is
5827 : changed to an rvalue of type T2 that still refers to the original
5828 : source class object (or the appropriate subobject thereof). */
5829 158553 : if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
5830 317213 : && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
5831 : {
5832 19371 : if (good_base && at_least_as_qualified_p (t2, t1))
5833 : {
5834 9595 : conv = build_identity_conv (t1, e1);
5835 9595 : if (!same_type_p (TYPE_MAIN_VARIANT (t1),
5836 : TYPE_MAIN_VARIANT (t2)))
5837 6 : conv = build_conv (ck_base, t2, conv);
5838 : else
5839 9589 : conv = build_conv (ck_rvalue, t2, conv);
5840 9595 : return conv;
5841 : }
5842 : else
5843 9776 : return NULL;
5844 : }
5845 : else
5846 : /* [expr.cond]
5847 :
5848 : Otherwise: E1 can be converted to match E2 if E1 can be implicitly
5849 : converted to the type that expression E2 would have if E2 were
5850 : converted to an rvalue (or the type it has, if E2 is an rvalue). */
5851 273480 : return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
5852 273480 : LOOKUP_IMPLICIT, complain);
5853 : }
5854 :
5855 : /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
5856 : arguments to the conditional expression. */
5857 :
5858 : tree
5859 7815534 : build_conditional_expr (const op_location_t &loc,
5860 : tree arg1, tree arg2, tree arg3,
5861 : tsubst_flags_t complain)
5862 : {
5863 7815534 : tree arg2_type;
5864 7815534 : tree arg3_type;
5865 7815534 : tree result = NULL_TREE;
5866 7815534 : tree result_type = NULL_TREE;
5867 7815534 : tree semantic_result_type = NULL_TREE;
5868 7815534 : bool is_glvalue = true;
5869 7815534 : struct z_candidate *candidates = 0;
5870 7815534 : struct z_candidate *cand;
5871 7815534 : tree orig_arg2, orig_arg3;
5872 :
5873 7815534 : auto_cond_timevar tv (TV_OVERLOAD);
5874 :
5875 : /* As a G++ extension, the second argument to the conditional can be
5876 : omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
5877 : c'.) If the second operand is omitted, make sure it is
5878 : calculated only once. */
5879 7815534 : if (!arg2)
5880 : {
5881 386 : if (complain & tf_error)
5882 380 : pedwarn (loc, OPT_Wpedantic,
5883 : "ISO C++ forbids omitting the middle term of "
5884 : "a %<?:%> expression");
5885 :
5886 386 : if ((complain & tf_warning) && !truth_value_p (TREE_CODE (arg1)))
5887 338 : warn_for_omitted_condop (loc, arg1);
5888 :
5889 : /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
5890 386 : if (glvalue_p (arg1))
5891 : {
5892 95 : arg1 = cp_stabilize_reference (arg1);
5893 95 : arg2 = arg1 = prevent_lifetime_extension (arg1);
5894 : }
5895 291 : else if (TREE_CODE (arg1) == TARGET_EXPR)
5896 : /* arg1 can't be a prvalue result of the conditional
5897 : expression, since it needs to be materialized for the
5898 : conversion to bool, so treat it as an xvalue in arg2. */
5899 6 : arg2 = move (TARGET_EXPR_SLOT (arg1));
5900 285 : else if (TREE_CODE (arg1) == EXCESS_PRECISION_EXPR)
5901 3 : arg2 = arg1 = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (arg1),
5902 3 : cp_save_expr (TREE_OPERAND (arg1, 0)));
5903 : else
5904 282 : arg2 = arg1 = cp_save_expr (arg1);
5905 : }
5906 :
5907 : /* If something has already gone wrong, just pass that fact up the
5908 : tree. */
5909 7815534 : if (error_operand_p (arg1)
5910 7815444 : || error_operand_p (arg2)
5911 15630928 : || error_operand_p (arg3))
5912 191 : return error_mark_node;
5913 :
5914 7815343 : conversion_obstack_sentinel cos;
5915 :
5916 7815343 : orig_arg2 = arg2;
5917 7815343 : orig_arg3 = arg3;
5918 :
5919 7815343 : if (gnu_vector_type_p (TREE_TYPE (arg1))
5920 7815343 : && (VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1))
5921 3 : || VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (arg1))))
5922 : {
5923 1852 : tree arg1_type = TREE_TYPE (arg1);
5924 :
5925 : /* If arg1 is another cond_expr choosing between -1 and 0,
5926 : then we can use its comparison. It may help to avoid
5927 : additional comparison, produce more accurate diagnostics
5928 : and enables folding. */
5929 1852 : if (TREE_CODE (arg1) == VEC_COND_EXPR
5930 1579 : && integer_minus_onep (TREE_OPERAND (arg1, 1))
5931 3431 : && integer_zerop (TREE_OPERAND (arg1, 2)))
5932 1579 : arg1 = TREE_OPERAND (arg1, 0);
5933 :
5934 1852 : arg1 = force_rvalue (arg1, complain);
5935 1852 : arg2 = force_rvalue (arg2, complain);
5936 1852 : arg3 = force_rvalue (arg3, complain);
5937 :
5938 : /* force_rvalue can return error_mark on valid arguments. */
5939 1852 : if (error_operand_p (arg1)
5940 1852 : || error_operand_p (arg2)
5941 3704 : || error_operand_p (arg3))
5942 0 : return error_mark_node;
5943 :
5944 1852 : arg2_type = TREE_TYPE (arg2);
5945 1852 : arg3_type = TREE_TYPE (arg3);
5946 :
5947 1852 : if (!VECTOR_TYPE_P (arg2_type)
5948 559 : && !VECTOR_TYPE_P (arg3_type))
5949 : {
5950 : /* Rely on the error messages of the scalar version. */
5951 541 : tree scal = build_conditional_expr (loc, integer_one_node,
5952 : orig_arg2, orig_arg3, complain);
5953 541 : if (scal == error_mark_node)
5954 : return error_mark_node;
5955 541 : tree stype = TREE_TYPE (scal);
5956 541 : tree ctype = TREE_TYPE (arg1_type);
5957 541 : if (TYPE_SIZE (stype) != TYPE_SIZE (ctype)
5958 541 : || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype)))
5959 : {
5960 9 : if (complain & tf_error)
5961 9 : error_at (loc, "inferred scalar type %qT is not an integer or "
5962 : "floating-point type of the same size as %qT", stype,
5963 9 : COMPARISON_CLASS_P (arg1)
5964 9 : ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0)))
5965 : : ctype);
5966 9 : return error_mark_node;
5967 : }
5968 :
5969 532 : tree vtype = build_opaque_vector_type (stype,
5970 532 : TYPE_VECTOR_SUBPARTS (arg1_type));
5971 : /* We could pass complain & tf_warning to unsafe_conversion_p,
5972 : but the warnings (like Wsign-conversion) have already been
5973 : given by the scalar build_conditional_expr_1. We still check
5974 : unsafe_conversion_p to forbid truncating long long -> float. */
5975 532 : if (unsafe_conversion_p (stype, arg2, NULL_TREE, false))
5976 : {
5977 0 : if (complain & tf_error)
5978 0 : error_at (loc, "conversion of scalar %qH to vector %qI "
5979 : "involves truncation", arg2_type, vtype);
5980 0 : return error_mark_node;
5981 : }
5982 532 : if (unsafe_conversion_p (stype, arg3, NULL_TREE, false))
5983 : {
5984 3 : if (complain & tf_error)
5985 3 : error_at (loc, "conversion of scalar %qH to vector %qI "
5986 : "involves truncation", arg3_type, vtype);
5987 3 : return error_mark_node;
5988 : }
5989 :
5990 529 : arg2 = cp_convert (stype, arg2, complain);
5991 529 : arg2 = save_expr (arg2);
5992 529 : arg2 = build_vector_from_val (vtype, arg2);
5993 529 : arg2_type = vtype;
5994 529 : arg3 = cp_convert (stype, arg3, complain);
5995 529 : arg3 = save_expr (arg3);
5996 529 : arg3 = build_vector_from_val (vtype, arg3);
5997 529 : arg3_type = vtype;
5998 : }
5999 :
6000 3662 : if ((gnu_vector_type_p (arg2_type) && !VECTOR_TYPE_P (arg3_type))
6001 3584 : || (gnu_vector_type_p (arg3_type) && !VECTOR_TYPE_P (arg2_type)))
6002 : {
6003 96 : enum stv_conv convert_flag =
6004 96 : scalar_to_vector (loc, VEC_COND_EXPR, arg2, arg3,
6005 : complain & tf_error);
6006 :
6007 96 : switch (convert_flag)
6008 : {
6009 0 : case stv_error:
6010 0 : return error_mark_node;
6011 15 : case stv_firstarg:
6012 15 : {
6013 15 : arg2 = save_expr (arg2);
6014 15 : arg2 = convert (TREE_TYPE (arg3_type), arg2);
6015 15 : arg2 = build_vector_from_val (arg3_type, arg2);
6016 15 : arg2_type = TREE_TYPE (arg2);
6017 15 : break;
6018 : }
6019 72 : case stv_secondarg:
6020 72 : {
6021 72 : arg3 = save_expr (arg3);
6022 72 : arg3 = convert (TREE_TYPE (arg2_type), arg3);
6023 72 : arg3 = build_vector_from_val (arg2_type, arg3);
6024 72 : arg3_type = TREE_TYPE (arg3);
6025 72 : break;
6026 : }
6027 : default:
6028 : break;
6029 : }
6030 : }
6031 :
6032 1840 : if (!gnu_vector_type_p (arg2_type)
6033 1837 : || !gnu_vector_type_p (arg3_type)
6034 1831 : || !same_type_p (arg2_type, arg3_type)
6035 1831 : || maybe_ne (TYPE_VECTOR_SUBPARTS (arg1_type),
6036 1831 : TYPE_VECTOR_SUBPARTS (arg2_type))
6037 3671 : || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type))
6038 : {
6039 9 : if (complain & tf_error)
6040 6 : error_at (loc,
6041 : "incompatible vector types in conditional expression: "
6042 6 : "%qT, %qT and %qT", TREE_TYPE (arg1),
6043 6 : TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3));
6044 9 : return error_mark_node;
6045 : }
6046 :
6047 1831 : if (!COMPARISON_CLASS_P (arg1))
6048 : {
6049 270 : tree cmp_type = truth_type_for (arg1_type);
6050 270 : arg1 = build2 (NE_EXPR, cmp_type, arg1, build_zero_cst (arg1_type));
6051 : }
6052 1831 : return build3_loc (loc, VEC_COND_EXPR, arg2_type, arg1, arg2, arg3);
6053 : }
6054 :
6055 : /* [expr.cond]
6056 :
6057 : The first expression is implicitly converted to bool (clause
6058 : _conv_). */
6059 7813491 : arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
6060 : LOOKUP_NORMAL);
6061 7813491 : if (error_operand_p (arg1))
6062 25 : return error_mark_node;
6063 :
6064 7813466 : arg2_type = unlowered_expr_type (arg2);
6065 7813466 : arg3_type = unlowered_expr_type (arg3);
6066 :
6067 7813466 : if ((TREE_CODE (arg2) == EXCESS_PRECISION_EXPR
6068 7813448 : || TREE_CODE (arg3) == EXCESS_PRECISION_EXPR)
6069 45 : && (TREE_CODE (arg2_type) == INTEGER_TYPE
6070 : || SCALAR_FLOAT_TYPE_P (arg2_type)
6071 : || TREE_CODE (arg2_type) == COMPLEX_TYPE)
6072 45 : && (TREE_CODE (arg3_type) == INTEGER_TYPE
6073 : || SCALAR_FLOAT_TYPE_P (arg3_type)
6074 : || TREE_CODE (arg3_type) == COMPLEX_TYPE))
6075 : {
6076 45 : semantic_result_type
6077 45 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
6078 45 : if (semantic_result_type == error_mark_node)
6079 : {
6080 0 : tree t1 = arg2_type;
6081 0 : tree t2 = arg3_type;
6082 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6083 0 : t1 = TREE_TYPE (t1);
6084 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6085 0 : t2 = TREE_TYPE (t2);
6086 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
6087 : && SCALAR_FLOAT_TYPE_P (t2)
6088 : && (extended_float_type_p (t1)
6089 : || extended_float_type_p (t2))
6090 : && cp_compare_floating_point_conversion_ranks
6091 : (t1, t2) == 3);
6092 0 : if (complain & tf_error)
6093 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
6094 : "have unordered conversion rank",
6095 : arg2_type, arg3_type);
6096 0 : return error_mark_node;
6097 : }
6098 45 : if (TREE_CODE (arg2) == EXCESS_PRECISION_EXPR)
6099 : {
6100 18 : arg2 = TREE_OPERAND (arg2, 0);
6101 18 : arg2_type = TREE_TYPE (arg2);
6102 : }
6103 45 : if (TREE_CODE (arg3) == EXCESS_PRECISION_EXPR)
6104 : {
6105 27 : arg3 = TREE_OPERAND (arg3, 0);
6106 27 : arg3_type = TREE_TYPE (arg3);
6107 : }
6108 : }
6109 :
6110 : /* [expr.cond]
6111 :
6112 : If either the second or the third operand has type (possibly
6113 : cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
6114 : array-to-pointer (_conv.array_), and function-to-pointer
6115 : (_conv.func_) standard conversions are performed on the second
6116 : and third operands. */
6117 7813466 : if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
6118 : {
6119 : /* 'void' won't help in resolving an overloaded expression on the
6120 : other side, so require it to resolve by itself. */
6121 126136 : if (arg2_type == unknown_type_node)
6122 : {
6123 18 : arg2 = resolve_nondeduced_context_or_error (arg2, complain);
6124 18 : arg2_type = TREE_TYPE (arg2);
6125 : }
6126 126136 : if (arg3_type == unknown_type_node)
6127 : {
6128 0 : arg3 = resolve_nondeduced_context_or_error (arg3, complain);
6129 0 : arg3_type = TREE_TYPE (arg3);
6130 : }
6131 :
6132 : /* [expr.cond]
6133 :
6134 : One of the following shall hold:
6135 :
6136 : --The second or the third operand (but not both) is a
6137 : throw-expression (_except.throw_); the result is of the type
6138 : and value category of the other.
6139 :
6140 : --Both the second and the third operands have type void; the
6141 : result is of type void and is a prvalue. */
6142 126136 : if (TREE_CODE (arg2) == THROW_EXPR
6143 84 : && TREE_CODE (arg3) != THROW_EXPR)
6144 : {
6145 72 : result_type = arg3_type;
6146 72 : is_glvalue = glvalue_p (arg3);
6147 : }
6148 126064 : else if (TREE_CODE (arg2) != THROW_EXPR
6149 126052 : && TREE_CODE (arg3) == THROW_EXPR)
6150 : {
6151 180 : result_type = arg2_type;
6152 180 : is_glvalue = glvalue_p (arg2);
6153 : }
6154 125884 : else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
6155 : {
6156 125855 : result_type = void_type_node;
6157 125855 : is_glvalue = false;
6158 : }
6159 : else
6160 : {
6161 29 : if (complain & tf_error)
6162 : {
6163 18 : if (VOID_TYPE_P (arg2_type))
6164 9 : error_at (cp_expr_loc_or_loc (arg3, loc),
6165 : "second operand to the conditional operator "
6166 : "is of type %<void%>, but the third operand is "
6167 : "neither a throw-expression nor of type %<void%>");
6168 : else
6169 9 : error_at (cp_expr_loc_or_loc (arg2, loc),
6170 : "third operand to the conditional operator "
6171 : "is of type %<void%>, but the second operand is "
6172 : "neither a throw-expression nor of type %<void%>");
6173 : }
6174 29 : return error_mark_node;
6175 : }
6176 :
6177 126107 : goto valid_operands;
6178 : }
6179 : /* [expr.cond]
6180 :
6181 : Otherwise, if the second and third operand have different types,
6182 : and either has (possibly cv-qualified) class type, or if both are
6183 : glvalues of the same value category and the same type except for
6184 : cv-qualification, an attempt is made to convert each of those operands
6185 : to the type of the other. */
6186 7687330 : else if (!same_type_p (arg2_type, arg3_type)
6187 7687330 : && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)
6188 1534439 : || (same_type_ignoring_top_level_qualifiers_p (arg2_type,
6189 : arg3_type)
6190 442918 : && glvalue_p (arg2) && glvalue_p (arg3)
6191 108420 : && lvalue_p (arg2) == lvalue_p (arg3))))
6192 : {
6193 254931 : conversion *conv2;
6194 254931 : conversion *conv3;
6195 254931 : bool converted = false;
6196 :
6197 254931 : conv2 = conditional_conversion (arg2, arg3, complain);
6198 254931 : conv3 = conditional_conversion (arg3, arg2, complain);
6199 :
6200 : /* [expr.cond]
6201 :
6202 : If both can be converted, or one can be converted but the
6203 : conversion is ambiguous, the program is ill-formed. If
6204 : neither can be converted, the operands are left unchanged and
6205 : further checking is performed as described below. If exactly
6206 : one conversion is possible, that conversion is applied to the
6207 : chosen operand and the converted operand is used in place of
6208 : the original operand for the remainder of this section. */
6209 254931 : if ((conv2 && !conv2->bad_p
6210 46453 : && conv3 && !conv3->bad_p)
6211 46431 : || (conv2 && conv2->kind == ck_ambig)
6212 254809 : || (conv3 && conv3->kind == ck_ambig))
6213 : {
6214 122 : if (complain & tf_error)
6215 : {
6216 6 : error_at (loc, "operands to %<?:%> have different types "
6217 : "%qT and %qT",
6218 : arg2_type, arg3_type);
6219 6 : if (conv2 && !conv2->bad_p && conv3 && !conv3->bad_p)
6220 3 : inform (loc, " and each type can be converted to the other");
6221 3 : else if (conv2 && conv2->kind == ck_ambig)
6222 3 : convert_like (conv2, arg2, complain);
6223 : else
6224 0 : convert_like (conv3, arg3, complain);
6225 : }
6226 122 : result = error_mark_node;
6227 : }
6228 254809 : else if (conv2 && !conv2->bad_p)
6229 : {
6230 46331 : arg2 = convert_like (conv2, arg2, complain);
6231 46331 : arg2 = convert_from_reference (arg2);
6232 46331 : arg2_type = TREE_TYPE (arg2);
6233 : /* Even if CONV2 is a valid conversion, the result of the
6234 : conversion may be invalid. For example, if ARG3 has type
6235 : "volatile X", and X does not have a copy constructor
6236 : accepting a "volatile X&", then even if ARG2 can be
6237 : converted to X, the conversion will fail. */
6238 46331 : if (error_operand_p (arg2))
6239 8 : result = error_mark_node;
6240 8 : converted = true;
6241 : }
6242 208478 : else if (conv3 && !conv3->bad_p)
6243 : {
6244 207756 : arg3 = convert_like (conv3, arg3, complain);
6245 207756 : arg3 = convert_from_reference (arg3);
6246 207756 : arg3_type = TREE_TYPE (arg3);
6247 207756 : if (error_operand_p (arg3))
6248 0 : result = error_mark_node;
6249 0 : converted = true;
6250 : }
6251 :
6252 130 : if (result)
6253 : return result;
6254 :
6255 : /* If, after the conversion, both operands have class type,
6256 : treat the cv-qualification of both operands as if it were the
6257 : union of the cv-qualification of the operands.
6258 :
6259 : The standard is not clear about what to do in this
6260 : circumstance. For example, if the first operand has type
6261 : "const X" and the second operand has a user-defined
6262 : conversion to "volatile X", what is the type of the second
6263 : operand after this step? Making it be "const X" (matching
6264 : the first operand) seems wrong, as that discards the
6265 : qualification without actually performing a copy. Leaving it
6266 : as "volatile X" seems wrong as that will result in the
6267 : conditional expression failing altogether, even though,
6268 : according to this step, the one operand could be converted to
6269 : the type of the other. */
6270 254801 : if (converted
6271 254079 : && CLASS_TYPE_P (arg2_type)
6272 266663 : && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
6273 0 : arg2_type = arg3_type =
6274 0 : cp_build_qualified_type (arg2_type,
6275 0 : cp_type_quals (arg2_type)
6276 0 : | cp_type_quals (arg3_type));
6277 : }
6278 :
6279 : /* [expr.cond]
6280 :
6281 : If the second and third operands are glvalues of the same value
6282 : category and have the same type, the result is of that type and
6283 : value category. */
6284 11405951 : if (((lvalue_p (arg2) && lvalue_p (arg3))
6285 4592934 : || (xvalue_p (arg2) && xvalue_p (arg3)))
6286 10830005 : && same_type_p (arg2_type, arg3_type))
6287 : {
6288 3048355 : result_type = arg2_type;
6289 3048355 : goto valid_operands;
6290 : }
6291 :
6292 : /* [expr.cond]
6293 :
6294 : Otherwise, the result is an rvalue. If the second and third
6295 : operand do not have the same type, and either has (possibly
6296 : cv-qualified) class type, overload resolution is used to
6297 : determine the conversions (if any) to be applied to the operands
6298 : (_over.match.oper_, _over.built_). */
6299 4638845 : is_glvalue = false;
6300 4638845 : if (!same_type_p (arg2_type, arg3_type)
6301 4638845 : && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
6302 : {
6303 722 : releasing_vec args;
6304 722 : conversion *conv;
6305 722 : bool any_viable_p;
6306 :
6307 : /* Rearrange the arguments so that add_builtin_candidate only has
6308 : to know about two args. In build_builtin_candidate, the
6309 : arguments are unscrambled. */
6310 722 : args->quick_push (arg2);
6311 722 : args->quick_push (arg3);
6312 722 : args->quick_push (arg1);
6313 722 : add_builtin_candidates (&candidates,
6314 : COND_EXPR,
6315 : NOP_EXPR,
6316 : ovl_op_identifier (false, COND_EXPR),
6317 : args,
6318 : LOOKUP_NORMAL, complain);
6319 :
6320 : /* [expr.cond]
6321 :
6322 : If the overload resolution fails, the program is
6323 : ill-formed. */
6324 722 : candidates = splice_viable (candidates, false, &any_viable_p);
6325 722 : if (!any_viable_p)
6326 : {
6327 599 : if (complain & tf_error)
6328 22 : error_at (loc, "operands to %<?:%> have different types %qT and %qT",
6329 : arg2_type, arg3_type);
6330 599 : return error_mark_node;
6331 : }
6332 123 : cand = tourney (candidates, complain);
6333 123 : if (!cand)
6334 : {
6335 0 : if (complain & tf_error)
6336 : {
6337 0 : auto_diagnostic_group d;
6338 0 : op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, false);
6339 0 : print_z_candidates (loc, candidates);
6340 0 : }
6341 0 : return error_mark_node;
6342 : }
6343 :
6344 : /* [expr.cond]
6345 :
6346 : Otherwise, the conversions thus determined are applied, and
6347 : the converted operands are used in place of the original
6348 : operands for the remainder of this section. */
6349 123 : conv = cand->convs[0];
6350 123 : arg1 = convert_like (conv, arg1, complain);
6351 123 : conv = cand->convs[1];
6352 123 : arg2 = convert_like (conv, arg2, complain);
6353 123 : arg2_type = TREE_TYPE (arg2);
6354 123 : conv = cand->convs[2];
6355 123 : arg3 = convert_like (conv, arg3, complain);
6356 123 : arg3_type = TREE_TYPE (arg3);
6357 722 : }
6358 :
6359 : /* [expr.cond]
6360 :
6361 : Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
6362 : and function-to-pointer (_conv.func_) standard conversions are
6363 : performed on the second and third operands.
6364 :
6365 : We need to force the lvalue-to-rvalue conversion here for class types,
6366 : so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
6367 : that isn't wrapped with a TARGET_EXPR plays havoc with exception
6368 : regions. */
6369 :
6370 4638246 : arg2 = force_rvalue (arg2, complain);
6371 4638246 : if (!CLASS_TYPE_P (arg2_type))
6372 4547073 : arg2_type = TREE_TYPE (arg2);
6373 :
6374 4638246 : arg3 = force_rvalue (arg3, complain);
6375 4638246 : if (!CLASS_TYPE_P (arg3_type))
6376 4547073 : arg3_type = TREE_TYPE (arg3);
6377 :
6378 4638246 : if (arg2 == error_mark_node || arg3 == error_mark_node)
6379 : return error_mark_node;
6380 :
6381 : /* [expr.cond]
6382 :
6383 : After those conversions, one of the following shall hold:
6384 :
6385 : --The second and third operands have the same type; the result is of
6386 : that type. */
6387 4638198 : if (same_type_p (arg2_type, arg3_type))
6388 : result_type = arg2_type;
6389 : /* [expr.cond]
6390 :
6391 : --The second and third operands have arithmetic or enumeration
6392 : type; the usual arithmetic conversions are performed to bring
6393 : them to a common type, and the result is of that type. */
6394 23444 : else if ((ARITHMETIC_TYPE_P (arg2_type)
6395 22613 : || UNSCOPED_ENUM_P (arg2_type))
6396 1000814 : && (ARITHMETIC_TYPE_P (arg3_type)
6397 185 : || UNSCOPED_ENUM_P (arg3_type)))
6398 : {
6399 : /* A conditional expression between a floating-point
6400 : type and an integer type should convert the integer type to
6401 : the evaluation format of the floating-point type, with
6402 : possible excess precision. */
6403 977256 : tree eptype2 = arg2_type;
6404 977256 : tree eptype3 = arg3_type;
6405 977256 : tree eptype;
6406 831 : if (ANY_INTEGRAL_TYPE_P (arg2_type)
6407 977259 : && (eptype = excess_precision_type (arg3_type)) != NULL_TREE)
6408 : {
6409 15 : eptype3 = eptype;
6410 15 : if (!semantic_result_type)
6411 15 : semantic_result_type
6412 15 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
6413 : }
6414 505 : else if (ANY_INTEGRAL_TYPE_P (arg3_type)
6415 977241 : && (eptype = excess_precision_type (arg2_type)) != NULL_TREE)
6416 : {
6417 27 : eptype2 = eptype;
6418 27 : if (!semantic_result_type)
6419 27 : semantic_result_type
6420 27 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
6421 : }
6422 977256 : result_type = type_after_usual_arithmetic_conversions (eptype2,
6423 : eptype3);
6424 977256 : if (result_type == error_mark_node)
6425 : {
6426 0 : tree t1 = eptype2;
6427 0 : tree t2 = eptype3;
6428 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6429 0 : t1 = TREE_TYPE (t1);
6430 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6431 0 : t2 = TREE_TYPE (t2);
6432 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
6433 : && SCALAR_FLOAT_TYPE_P (t2)
6434 : && (extended_float_type_p (t1)
6435 : || extended_float_type_p (t2))
6436 : && cp_compare_floating_point_conversion_ranks
6437 : (t1, t2) == 3);
6438 0 : if (complain & tf_error)
6439 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
6440 : "have unordered conversion rank",
6441 : eptype2, eptype3);
6442 0 : return error_mark_node;
6443 : }
6444 977256 : if (semantic_result_type == error_mark_node)
6445 : {
6446 0 : tree t1 = arg2_type;
6447 0 : tree t2 = arg3_type;
6448 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6449 0 : t1 = TREE_TYPE (t1);
6450 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6451 0 : t2 = TREE_TYPE (t2);
6452 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
6453 : && SCALAR_FLOAT_TYPE_P (t2)
6454 : && (extended_float_type_p (t1)
6455 : || extended_float_type_p (t2))
6456 : && cp_compare_floating_point_conversion_ranks
6457 : (t1, t2) == 3);
6458 0 : if (complain & tf_error)
6459 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
6460 : "have unordered conversion rank",
6461 : arg2_type, arg3_type);
6462 0 : return error_mark_node;
6463 : }
6464 :
6465 977256 : if (complain & tf_warning)
6466 924497 : do_warn_double_promotion (result_type, arg2_type, arg3_type,
6467 : "implicit conversion from %qH to %qI to "
6468 : "match other result of conditional",
6469 : loc);
6470 :
6471 977256 : if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
6472 102 : && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
6473 : {
6474 35 : tree stripped_orig_arg2 = tree_strip_any_location_wrapper (orig_arg2);
6475 35 : tree stripped_orig_arg3 = tree_strip_any_location_wrapper (orig_arg3);
6476 35 : if (TREE_CODE (stripped_orig_arg2) == CONST_DECL
6477 23 : && TREE_CODE (stripped_orig_arg3) == CONST_DECL
6478 58 : && (DECL_CONTEXT (stripped_orig_arg2)
6479 23 : == DECL_CONTEXT (stripped_orig_arg3)))
6480 : /* Two enumerators from the same enumeration can have different
6481 : types when the enumeration is still being defined. */;
6482 29 : else if (complain & (cxx_dialect >= cxx26
6483 29 : ? tf_warning_or_error : tf_warning))
6484 44 : emit_diagnostic ((cxx_dialect >= cxx26
6485 : ? diagnostics::kind::pedwarn
6486 : : diagnostics::kind::warning),
6487 26 : loc, OPT_Wenum_compare, "enumerated mismatch "
6488 : "in conditional expression: %qT vs %qT",
6489 : arg2_type, arg3_type);
6490 3 : else if (cxx_dialect >= cxx26)
6491 2 : return error_mark_node;
6492 : }
6493 977221 : else if ((((complain & (cxx_dialect >= cxx26
6494 977221 : ? tf_warning_or_error : tf_warning))
6495 928811 : && warn_deprecated_enum_float_conv)
6496 63614 : || (cxx_dialect >= cxx26
6497 2121 : && (complain & tf_warning_or_error) == 0))
6498 915629 : && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
6499 40 : && SCALAR_FLOAT_TYPE_P (arg3_type))
6500 915616 : || (SCALAR_FLOAT_TYPE_P (arg2_type)
6501 354 : && TREE_CODE (arg3_type) == ENUMERAL_TYPE)))
6502 : {
6503 23 : if (cxx_dialect >= cxx26 && (complain & tf_warning_or_error) == 0)
6504 4 : return error_mark_node;
6505 19 : if (cxx_dialect >= cxx26 && TREE_CODE (arg2_type) == ENUMERAL_TYPE)
6506 5 : pedwarn (loc, OPT_Wdeprecated_enum_float_conversion,
6507 : "conditional expression between enumeration type "
6508 : "%qT and floating-point type %qT", arg2_type, arg3_type);
6509 14 : else if (cxx_dialect >= cxx26)
6510 4 : pedwarn (loc, OPT_Wdeprecated_enum_float_conversion,
6511 : "conditional expression between floating-point type "
6512 : "%qT and enumeration type %qT", arg2_type, arg3_type);
6513 10 : else if (TREE_CODE (arg2_type) == ENUMERAL_TYPE)
6514 6 : warning_at (loc, OPT_Wdeprecated_enum_float_conversion,
6515 : "conditional expression between enumeration type "
6516 : "%qT and floating-point type %qT is deprecated",
6517 : arg2_type, arg3_type);
6518 : else
6519 4 : warning_at (loc, OPT_Wdeprecated_enum_float_conversion,
6520 : "conditional expression between floating-point "
6521 : "type %qT and enumeration type %qT is deprecated",
6522 : arg2_type, arg3_type);
6523 : }
6524 967475 : else if ((extra_warnings || warn_enum_conversion)
6525 977203 : && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
6526 15 : && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
6527 9716 : || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
6528 8 : && !same_type_p (arg2_type,
6529 : type_promotes_to (arg3_type)))))
6530 : {
6531 17 : if (complain & tf_warning)
6532 : {
6533 12 : enum opt_code opt = (warn_enum_conversion
6534 17 : ? OPT_Wenum_conversion
6535 : : OPT_Wextra);
6536 17 : warning_at (loc, opt, "enumerated and "
6537 : "non-enumerated type in conditional expression");
6538 : }
6539 : }
6540 :
6541 977250 : arg2 = perform_implicit_conversion (result_type, arg2, complain);
6542 977250 : arg3 = perform_implicit_conversion (result_type, arg3, complain);
6543 : }
6544 : /* [expr.cond]
6545 :
6546 : --The second and third operands have pointer type, or one has
6547 : pointer type and the other is a null pointer constant; pointer
6548 : conversions (_conv.ptr_) and qualification conversions
6549 : (_conv.qual_) are performed to bring them to their composite
6550 : pointer type (_expr.rel_). The result is of the composite
6551 : pointer type.
6552 :
6553 : --The second and third operands have pointer to member type, or
6554 : one has pointer to member type and the other is a null pointer
6555 : constant; pointer to member conversions (_conv.mem_) and
6556 : qualification conversions (_conv.qual_) are performed to bring
6557 : them to a common type, whose cv-qualification shall match the
6558 : cv-qualification of either the second or the third operand.
6559 : The result is of the common type. */
6560 22625 : else if ((null_ptr_cst_p (arg2)
6561 175 : && TYPE_PTR_OR_PTRMEM_P (arg3_type))
6562 22450 : || (null_ptr_cst_p (arg3)
6563 21486 : && TYPE_PTR_OR_PTRMEM_P (arg2_type))
6564 964 : || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
6565 99 : || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type))
6566 22701 : || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
6567 : {
6568 22571 : result_type = composite_pointer_type (loc,
6569 : arg2_type, arg3_type, arg2,
6570 : arg3, CPO_CONDITIONAL_EXPR,
6571 : complain);
6572 22571 : if (result_type == error_mark_node)
6573 : return error_mark_node;
6574 22545 : arg2 = perform_implicit_conversion (result_type, arg2, complain);
6575 22545 : arg3 = perform_implicit_conversion (result_type, arg3, complain);
6576 : }
6577 :
6578 4638112 : if (!result_type)
6579 : {
6580 54 : if (complain & tf_error)
6581 3 : error_at (loc, "operands to %<?:%> have different types %qT and %qT",
6582 : arg2_type, arg3_type);
6583 54 : return error_mark_node;
6584 : }
6585 :
6586 4638112 : if (arg2 == error_mark_node || arg3 == error_mark_node)
6587 : return error_mark_node;
6588 :
6589 4638109 : valid_operands:
6590 7812571 : if (processing_template_decl && is_glvalue)
6591 : {
6592 : /* Let lvalue_kind know this was a glvalue. */
6593 112471 : tree arg = (result_type == arg2_type ? arg2 : arg3);
6594 112471 : result_type = cp_build_reference_type (result_type, xvalue_p (arg));
6595 : }
6596 :
6597 7812571 : result = build3_loc (loc, COND_EXPR, result_type, arg1, arg2, arg3);
6598 :
6599 : /* If the ARG2 and ARG3 are the same and don't have side-effects,
6600 : warn here, because the COND_EXPR will be turned into ARG2. */
6601 7812571 : if (warn_duplicated_branches
6602 153 : && (complain & tf_warning)
6603 7812724 : && (arg2 == arg3 || operand_equal_p (arg2, arg3,
6604 : OEP_ADDRESS_OF_SAME_FIELD)))
6605 57 : warning_at (EXPR_LOCATION (result), OPT_Wduplicated_branches,
6606 : "this condition has identical branches");
6607 :
6608 : /* We can't use result_type below, as fold might have returned a
6609 : throw_expr. */
6610 :
6611 7812571 : if (!is_glvalue)
6612 : {
6613 : /* Expand both sides into the same slot, hopefully the target of
6614 : the ?: expression. We used to check for TARGET_EXPRs here,
6615 : but now we sometimes wrap them in NOP_EXPRs so the test would
6616 : fail. */
6617 4764147 : if (CLASS_TYPE_P (TREE_TYPE (result)))
6618 : {
6619 91210 : result = get_target_expr (result, complain);
6620 : /* Tell gimplify_modify_expr_rhs not to strip this in
6621 : assignment context: we want both arms to initialize
6622 : the same temporary. */
6623 91210 : TARGET_EXPR_NO_ELIDE (result) = true;
6624 : }
6625 : /* If this expression is an rvalue, but might be mistaken for an
6626 : lvalue, we must add a NON_LVALUE_EXPR. */
6627 4764147 : result = rvalue (result);
6628 4764147 : if (semantic_result_type)
6629 87 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type,
6630 : result);
6631 : }
6632 : else
6633 : {
6634 3048424 : result = force_paren_expr (result);
6635 3048424 : gcc_assert (semantic_result_type == NULL_TREE);
6636 : }
6637 :
6638 : return result;
6639 7815534 : }
6640 :
6641 : /* OPERAND is an operand to an expression. Perform necessary steps
6642 : required before using it. If OPERAND is NULL_TREE, NULL_TREE is
6643 : returned. */
6644 :
6645 : static tree
6646 646499038 : prep_operand (tree operand)
6647 : {
6648 646499038 : if (operand)
6649 : {
6650 746970576 : if (CLASS_TYPE_P (TREE_TYPE (operand))
6651 399921451 : && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
6652 : /* Make sure the template type is instantiated now. */
6653 10523864 : instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
6654 : }
6655 :
6656 646499038 : return operand;
6657 : }
6658 :
6659 : /* True iff CONV represents a conversion sequence which no other can be better
6660 : than under [over.ics.rank]: in other words, a "conversion" to the exact same
6661 : type (including binding to a reference to the same type). This is stronger
6662 : than the standard's "identity" category, which also includes reference
6663 : bindings that add cv-qualifiers or change rvalueness. */
6664 :
6665 : static bool
6666 228173430 : perfect_conversion_p (conversion *conv)
6667 : {
6668 228173430 : if (CONVERSION_RANK (conv) != cr_identity)
6669 : return false;
6670 163252520 : if (conv->kind == ck_ref_bind)
6671 : {
6672 42852867 : if (!conv->rvaluedness_matches_p)
6673 : return false;
6674 29657327 : if (!same_type_p (TREE_TYPE (conv->type),
6675 : next_conversion (conv)->type))
6676 : return false;
6677 : }
6678 146190628 : if (conv->check_narrowing)
6679 : /* Brace elision is imperfect. */
6680 : return false;
6681 : return true;
6682 : }
6683 :
6684 : /* True if CAND represents a perfect match, i.e. all perfect conversions, so no
6685 : other candidate can be a better match. Since the template/non-template
6686 : tiebreaker comes immediately after the conversion comparison in
6687 : [over.match.best], a perfect non-template candidate is better than all
6688 : templates. */
6689 :
6690 : static bool
6691 538133451 : perfect_candidate_p (z_candidate *cand)
6692 : {
6693 538133451 : if (cand->viable < 1)
6694 : return false;
6695 : /* CWG1402 makes an implicitly deleted move op worse than other
6696 : candidates. */
6697 210539424 : if (DECL_DELETED_FN (cand->fn) && DECL_DEFAULTED_FN (cand->fn)
6698 209538025 : && move_fn_p (cand->fn))
6699 : return false;
6700 207885117 : int len = cand->num_convs;
6701 354075695 : for (int i = 0; i < len; ++i)
6702 228173430 : if (!perfect_conversion_p (cand->convs[i]))
6703 : return false;
6704 125902265 : if (conversion *conv = cand->second_conv)
6705 0 : if (!perfect_conversion_p (conv))
6706 : return false;
6707 : return true;
6708 : }
6709 :
6710 : /* True iff one of CAND's argument conversions is missing. */
6711 :
6712 : static bool
6713 22174997 : missing_conversion_p (const z_candidate *cand)
6714 : {
6715 40340173 : for (unsigned i = 0; i < cand->num_convs; ++i)
6716 : {
6717 26765161 : conversion *conv = cand->convs[i];
6718 26765161 : if (!conv)
6719 : return true;
6720 25782695 : if (conv->kind == ck_deferred_bad)
6721 : {
6722 : /* We don't know whether this conversion is outright invalid or
6723 : just bad, so conservatively assume it's missing. */
6724 7617519 : gcc_checking_assert (conv->bad_p);
6725 : return true;
6726 : }
6727 : }
6728 : return false;
6729 : }
6730 :
6731 : /* Add each of the viable functions in FNS (a FUNCTION_DECL or
6732 : OVERLOAD) to the CANDIDATES, returning an updated list of
6733 : CANDIDATES. The ARGS are the arguments provided to the call;
6734 : if FIRST_ARG is non-null it is the implicit object argument,
6735 : otherwise the first element of ARGS is used if needed. The
6736 : EXPLICIT_TARGS are explicit template arguments provided.
6737 : TEMPLATE_ONLY is true if only template functions should be
6738 : considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
6739 : add_function_candidate. */
6740 :
6741 : static void
6742 311464252 : add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args,
6743 : tree return_type,
6744 : tree explicit_targs, bool template_only,
6745 : tree conversion_path, tree access_path,
6746 : int flags,
6747 : struct z_candidate **candidates,
6748 : tsubst_flags_t complain)
6749 : {
6750 311464252 : tree ctype;
6751 311464252 : const vec<tree, va_gc> *non_static_args;
6752 311464252 : bool check_list_ctor = false;
6753 311464252 : bool check_converting = false;
6754 311464252 : unification_kind_t strict;
6755 311464252 : tree ne_context = NULL_TREE;
6756 311464252 : tree ne_fns = NULL_TREE;
6757 :
6758 311464252 : if (!fns)
6759 3353987 : return;
6760 :
6761 : /* Precalculate special handling of constructors and conversion ops. */
6762 308110265 : tree fn = OVL_FIRST (fns);
6763 308110265 : if (DECL_CONV_FN_P (fn))
6764 : {
6765 18218082 : check_list_ctor = false;
6766 18218082 : check_converting = (flags & LOOKUP_ONLYCONVERTING) != 0;
6767 18218082 : if (flags & LOOKUP_NO_CONVERSION)
6768 : /* We're doing return_type(x). */
6769 : strict = DEDUCE_CONV;
6770 : else
6771 : /* We're doing x.operator return_type(). */
6772 1926 : strict = DEDUCE_EXACT;
6773 : /* [over.match.funcs] For conversion functions, the function
6774 : is considered to be a member of the class of the implicit
6775 : object argument for the purpose of defining the type of
6776 : the implicit object parameter. */
6777 18218082 : ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg));
6778 : }
6779 : else
6780 : {
6781 579784366 : if (DECL_CONSTRUCTOR_P (fn))
6782 : {
6783 78048548 : check_list_ctor = (flags & LOOKUP_LIST_ONLY) != 0;
6784 : /* For list-initialization we consider explicit constructors
6785 : and complain if one is chosen. */
6786 78048548 : check_converting
6787 78048548 : = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
6788 : == LOOKUP_ONLYCONVERTING);
6789 : }
6790 289892183 : strict = DEDUCE_CALL;
6791 451212831 : ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
6792 : }
6793 :
6794 : /* P2468: Check if operator== is a rewrite target with first operand
6795 : (*args)[0]; for now just do the lookups. */
6796 308110265 : if ((flags & (LOOKUP_REWRITTEN | LOOKUP_REVERSED))
6797 308110265 : && DECL_OVERLOADED_OPERATOR_IS (fn, EQ_EXPR))
6798 : {
6799 6856352 : tree ne_name = ovl_op_identifier (false, NE_EXPR);
6800 6856352 : if (DECL_CLASS_SCOPE_P (fn))
6801 : {
6802 184877 : ne_context = DECL_CONTEXT (fn);
6803 184877 : ne_fns = lookup_fnfields (TREE_TYPE ((*args)[0]), ne_name,
6804 : 1, tf_none);
6805 184877 : if (ne_fns == error_mark_node || ne_fns == NULL_TREE)
6806 : ne_fns = NULL_TREE;
6807 : else
6808 19193 : ne_fns = BASELINK_FUNCTIONS (ne_fns);
6809 : }
6810 : else
6811 : {
6812 6671475 : ne_context = decl_namespace_context (fn);
6813 6671475 : ne_fns = lookup_qualified_name (ne_context, ne_name,
6814 : LOOK_want::NORMAL,
6815 : /*complain*/false);
6816 6671475 : if (ne_fns == error_mark_node
6817 2186879 : || !is_overloaded_fn (ne_fns))
6818 4484596 : ne_fns = NULL_TREE;
6819 : }
6820 : }
6821 :
6822 308110265 : if (first_arg)
6823 : non_static_args = args;
6824 : else
6825 : /* Delay creating the implicit this parameter until it is needed. */
6826 136970902 : non_static_args = NULL;
6827 :
6828 308110265 : bool seen_strictly_viable = any_strictly_viable (*candidates);
6829 : /* If there's a non-template perfect match, we don't need to consider
6830 : templates. So check non-templates first. This optimization is only
6831 : really needed for the defaulted copy constructor of tuple and the like
6832 : (96926), but it seems like we might as well enable it more generally. */
6833 308110265 : bool seen_perfect = false;
6834 308110265 : enum { templates, non_templates, either } which = either;
6835 308110265 : if (template_only)
6836 : which = templates;
6837 : else /*if (flags & LOOKUP_DEFAULTED)*/
6838 269559493 : which = non_templates;
6839 :
6840 : /* Template candidates that we'll potentially ignore if the
6841 : perfect candidate optimization succeeds. */
6842 308110265 : z_candidate *ignored_template_cands = nullptr;
6843 :
6844 : /* During overload resolution, we first consider each function under the
6845 : assumption that we'll eventually find a strictly viable candidate.
6846 : This allows us to circumvent our defacto behavior when checking
6847 : argument conversions and shortcut consideration of the candidate
6848 : upon encountering the first bad conversion. If this assumption
6849 : turns out to be false, and all candidates end up being non-strictly
6850 : viable, then we reconsider such candidates under the defacto behavior.
6851 : This trick is important for pruning member function overloads according
6852 : to their const/ref-qualifiers (since all 'this' conversions are at
6853 : worst bad) without breaking -fpermissive. */
6854 308110265 : z_candidate *bad_cands = nullptr;
6855 308110265 : bool shortcut_bad_convs = true;
6856 :
6857 451890463 : again:
6858 2609173053 : for (tree fn : lkp_range (fns))
6859 : {
6860 2157296141 : if (which == templates && TREE_CODE (fn) != TEMPLATE_DECL)
6861 : {
6862 314183844 : if (template_only)
6863 791992 : add_ignored_candidate (candidates, fn);
6864 314183844 : continue;
6865 : }
6866 1843112297 : if (which == non_templates && TREE_CODE (fn) == TEMPLATE_DECL)
6867 : {
6868 638965422 : add_ignored_candidate (&ignored_template_cands, fn);
6869 638965422 : continue;
6870 : }
6871 230937373 : if ((check_converting && DECL_NONCONVERTING_P (fn))
6872 1409889545 : || (check_list_ctor && !is_list_ctor (fn)))
6873 : {
6874 26586257 : add_ignored_candidate (candidates, fn);
6875 26586257 : continue;
6876 : }
6877 :
6878 1177560618 : tree fn_first_arg = NULL_TREE;
6879 1177560618 : const vec<tree, va_gc> *fn_args = args;
6880 :
6881 1177560618 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fn))
6882 : {
6883 : /* Figure out where the object arg comes from. If this
6884 : function is a non-static member and we didn't get an
6885 : implicit object argument, move it out of args. */
6886 426954105 : if (first_arg == NULL_TREE)
6887 : {
6888 8399367 : unsigned int ix;
6889 8399367 : tree arg;
6890 8399367 : vec<tree, va_gc> *tempvec;
6891 8399367 : vec_alloc (tempvec, args->length () - 1);
6892 22734485 : for (ix = 1; args->iterate (ix, &arg); ++ix)
6893 5935751 : tempvec->quick_push (arg);
6894 8399367 : non_static_args = tempvec;
6895 8399367 : first_arg = (*args)[0];
6896 : }
6897 :
6898 : fn_first_arg = first_arg;
6899 : fn_args = non_static_args;
6900 : }
6901 :
6902 : /* Don't bother reversing an operator with two identical parameters. */
6903 750606513 : else if (vec_safe_length (args) == 2 && (flags & LOOKUP_REVERSED))
6904 : {
6905 208201062 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
6906 208201062 : if (same_type_p (TREE_VALUE (parmlist),
6907 : TREE_VALUE (TREE_CHAIN (parmlist))))
6908 98828994 : continue;
6909 : }
6910 :
6911 : /* When considering reversed operator==, if there's a corresponding
6912 : operator!= in the same scope, it's not a rewrite target. */
6913 1078731624 : if (ne_context)
6914 : {
6915 147484012 : if (TREE_CODE (ne_context) == NAMESPACE_DECL)
6916 : {
6917 : /* With argument-dependent lookup, fns can span multiple
6918 : namespaces; make sure we look in the fn's namespace for a
6919 : corresponding operator!=. */
6920 147293924 : tree fn_ns = decl_namespace_context (fn);
6921 147293924 : if (fn_ns != ne_context)
6922 : {
6923 8157949 : ne_context = fn_ns;
6924 8157949 : tree ne_name = ovl_op_identifier (false, NE_EXPR);
6925 8157949 : ne_fns = lookup_qualified_name (ne_context, ne_name,
6926 : LOOK_want::NORMAL,
6927 : /*complain*/false);
6928 8157949 : if (ne_fns == error_mark_node
6929 5187368 : || !is_overloaded_fn (ne_fns))
6930 2970581 : ne_fns = NULL_TREE;
6931 : }
6932 : }
6933 147484012 : bool found = false;
6934 1589093590 : for (lkp_iterator ne (ne_fns); !found && ne; ++ne)
6935 1476768010 : if (0 && !ne.using_p ()
6936 : && DECL_NAMESPACE_SCOPE_P (fn)
6937 : && DECL_CONTEXT (*ne) != DECL_CONTEXT (fn))
6938 : /* ??? This kludge excludes inline namespace members for the H
6939 : test in spaceship-eq15.C, but I don't see why we would want
6940 : that behavior. Asked Core 2022-11-04. Disabling for now. */;
6941 1476768010 : else if (fns_correspond (fn, *ne))
6942 : {
6943 : found = true;
6944 : break;
6945 : }
6946 147484012 : if (found)
6947 35158432 : continue;
6948 : }
6949 :
6950 : /* Do not resolve any non-default function. Only the default version
6951 : is resolvable (for the target_version attribute semantics.) */
6952 1043573192 : if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
6953 : && TREE_CODE (fn) == FUNCTION_DECL
6954 : && !is_function_default_version (fn))
6955 : {
6956 : add_ignored_candidate (candidates, fn);
6957 : continue;
6958 : }
6959 :
6960 1043573192 : if (TREE_CODE (fn) == TEMPLATE_DECL)
6961 505439741 : add_template_candidate (candidates,
6962 : fn,
6963 : ctype,
6964 : explicit_targs,
6965 : fn_first_arg,
6966 : fn_args,
6967 : return_type,
6968 : access_path,
6969 : conversion_path,
6970 : flags,
6971 : strict,
6972 : shortcut_bad_convs,
6973 : complain);
6974 : else
6975 : {
6976 538133451 : add_function_candidate (candidates,
6977 : fn,
6978 : ctype,
6979 : fn_first_arg,
6980 : fn_args,
6981 : access_path,
6982 : conversion_path,
6983 : flags,
6984 : NULL,
6985 : shortcut_bad_convs,
6986 : complain);
6987 538133451 : if (perfect_candidate_p (*candidates))
6988 1043559641 : seen_perfect = true;
6989 : }
6990 :
6991 1043559641 : z_candidate *cand = *candidates;
6992 1043559641 : if (cand->viable == 1)
6993 294238868 : seen_strictly_viable = true;
6994 :
6995 1043559641 : if (cand->viable == -1
6996 22175495 : && shortcut_bad_convs
6997 1065734638 : && (missing_conversion_p (cand)
6998 13575012 : || TREE_CODE (cand->fn) == TEMPLATE_DECL))
6999 : {
7000 : /* This candidate has been tentatively marked non-strictly viable,
7001 : and we didn't compute all argument conversions for it (having
7002 : stopped at the first bad conversion). Move it to BAD_CANDS to
7003 : to fully reconsider later if we don't find any strictly viable
7004 : candidates. */
7005 8631557 : if (complain & (tf_error | tf_conv))
7006 : {
7007 8311323 : *candidates = cand->next;
7008 8311323 : cand->next = bad_cands;
7009 8311323 : bad_cands = cand;
7010 : }
7011 : else
7012 : /* But if we're in a SFINAE context, just mark this candidate as
7013 : unviable outright and avoid potentially reconsidering it.
7014 : This is safe to do because in a SFINAE context, performing a bad
7015 : conversion is always an error (even with -fpermissive), so a
7016 : non-strictly viable candidate is effectively unviable anyway. */
7017 320234 : cand->viable = 0;
7018 : }
7019 : }
7020 451876912 : if (which == non_templates && !seen_perfect)
7021 : {
7022 143663288 : which = templates;
7023 143663288 : ignored_template_cands = nullptr;
7024 143663288 : goto again;
7025 : }
7026 308213624 : else if (which == templates
7027 308213624 : && !seen_strictly_viable
7028 : && shortcut_bad_convs
7029 56696317 : && bad_cands)
7030 : {
7031 : /* None of the candidates are strictly viable, so consider again those
7032 : functions in BAD_CANDS, this time without shortcutting bad conversions
7033 : so that all their argument conversions are computed. */
7034 258234 : which = either;
7035 : fns = NULL_TREE;
7036 258234 : for (z_candidate *cand = bad_cands; cand; cand = cand->next)
7037 : {
7038 141324 : tree fn = cand->fn;
7039 141324 : if (tree ti = cand->template_decl)
7040 120 : fn = TI_TEMPLATE (ti);
7041 141324 : fns = ovl_make (fn, fns);
7042 : }
7043 116910 : shortcut_bad_convs = false;
7044 116910 : bad_cands = nullptr;
7045 116910 : goto again;
7046 : }
7047 :
7048 308096714 : if (complain & tf_error)
7049 : {
7050 : /* Remember any omitted candidates; we may want to print all candidates
7051 : as part of overload resolution failure diagnostics. */
7052 470981457 : for (z_candidate *omitted_cands : { ignored_template_cands, bad_cands })
7053 : {
7054 313987638 : z_candidate **omitted_cands_tail = &omitted_cands;
7055 361521567 : while (*omitted_cands_tail)
7056 47533929 : omitted_cands_tail = &(*omitted_cands_tail)->next;
7057 313987638 : *omitted_cands_tail = *candidates;
7058 313987638 : *candidates = omitted_cands;
7059 : }
7060 : }
7061 : }
7062 :
7063 : /* Returns 1 if P0145R2 says that the LHS of operator CODE is evaluated first,
7064 : -1 if the RHS is evaluated first, or 0 if the order is unspecified. */
7065 :
7066 : static int
7067 16250986 : op_is_ordered (tree_code code)
7068 : {
7069 16250327 : switch (code)
7070 : {
7071 : // 5. b @= a
7072 3645788 : case MODIFY_EXPR:
7073 3645788 : return (flag_strong_eval_order > 1 ? -1 : 0);
7074 :
7075 : // 6. a[b]
7076 418616 : case ARRAY_REF:
7077 417957 : return (flag_strong_eval_order > 1 ? 1 : 0);
7078 :
7079 : // 1. a.b
7080 : // Not overloadable (yet).
7081 : // 2. a->b
7082 : // Only one argument.
7083 : // 3. a->*b
7084 61269 : case MEMBER_REF:
7085 : // 7. a << b
7086 61269 : case LSHIFT_EXPR:
7087 : // 8. a >> b
7088 61269 : case RSHIFT_EXPR:
7089 : // a && b
7090 : // Predates P0145R3.
7091 61269 : case TRUTH_ANDIF_EXPR:
7092 : // a || b
7093 : // Predates P0145R3.
7094 61269 : case TRUTH_ORIF_EXPR:
7095 : // a , b
7096 : // Predates P0145R3.
7097 61269 : case COMPOUND_EXPR:
7098 61269 : return (flag_strong_eval_order ? 1 : 0);
7099 :
7100 : default:
7101 : return 0;
7102 : }
7103 : }
7104 :
7105 : /* Subroutine of build_new_op: Add to CANDIDATES all candidates for the
7106 : operator indicated by CODE/CODE2. This function calls itself recursively to
7107 : handle C++20 rewritten comparison operator candidates. Returns NULL_TREE
7108 : upon success, and error_mark_node if something went wrong that prevented
7109 : us from performing overload resolution (e.g. ambiguous member name lookup).
7110 :
7111 : LOOKUPS, if non-NULL, is the set of pertinent namespace-scope operator
7112 : overloads to consider. This parameter is used when instantiating a
7113 : dependent operator expression and has the same structure as
7114 : DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS. */
7115 :
7116 : static tree
7117 34231168 : add_operator_candidates (z_candidate **candidates,
7118 : tree_code code, tree_code code2,
7119 : vec<tree, va_gc> *arglist, tree lookups,
7120 : int flags, tsubst_flags_t complain)
7121 : {
7122 34231168 : z_candidate *start_candidates = *candidates;
7123 34231168 : bool ismodop = code2 != ERROR_MARK;
7124 34231168 : tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
7125 :
7126 : /* LOOKUP_REWRITTEN is set when we're looking for the == or <=> operator to
7127 : rewrite from, and also when we're looking for the e.g. < operator to use
7128 : on the result of <=>. In the latter case, we don't want the flag set in
7129 : the candidate, we just want to suppress looking for rewrites. */
7130 34231168 : bool rewritten = (flags & LOOKUP_REWRITTEN);
7131 34231168 : if (rewritten && code != EQ_EXPR && code != SPACESHIP_EXPR)
7132 928162 : flags &= ~LOOKUP_REWRITTEN;
7133 :
7134 31735053 : bool memonly = false;
7135 31735053 : switch (code)
7136 : {
7137 : /* =, ->, [], () must be non-static member functions. */
7138 6397467 : case MODIFY_EXPR:
7139 6397467 : if (code2 != NOP_EXPR)
7140 : break;
7141 : /* FALLTHRU */
7142 : case COMPONENT_REF:
7143 : case ARRAY_REF:
7144 : memonly = true;
7145 : break;
7146 :
7147 : default:
7148 : break;
7149 : }
7150 :
7151 : /* Add namespace-scope operators to the list of functions to
7152 : consider. */
7153 : if (!memonly)
7154 : {
7155 30057867 : tree fns;
7156 30057867 : if (!lookups)
7157 22238796 : fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE);
7158 : /* If LOOKUPS is non-NULL, then we're instantiating a dependent operator
7159 : expression, and LOOKUPS is the result of stage 1 name lookup. */
7160 7819071 : else if (tree found = purpose_member (fnname, lookups))
7161 1684442 : fns = TREE_VALUE (found);
7162 : else
7163 : fns = NULL_TREE;
7164 30057867 : fns = lookup_arg_dependent (fnname, fns, arglist);
7165 30057867 : add_candidates (fns, NULL_TREE, arglist, NULL_TREE,
7166 : NULL_TREE, false, NULL_TREE, NULL_TREE,
7167 : flags, candidates, complain);
7168 : }
7169 :
7170 : /* Add class-member operators to the candidate set. */
7171 34231135 : tree arg1_type = TREE_TYPE ((*arglist)[0]);
7172 34231135 : unsigned nargs = arglist->length () > 1 ? 2 : 1;
7173 29282663 : tree arg2_type = nargs > 1 ? TREE_TYPE ((*arglist)[1]) : NULL_TREE;
7174 34231135 : if (CLASS_TYPE_P (arg1_type))
7175 : {
7176 19853095 : tree fns = lookup_fnfields (arg1_type, fnname, 1, complain);
7177 19853095 : if (fns == error_mark_node)
7178 : return error_mark_node;
7179 19853083 : if (fns)
7180 : {
7181 8822025 : if (code == ARRAY_REF)
7182 : {
7183 422658 : vec<tree,va_gc> *restlist = make_tree_vector ();
7184 845316 : for (unsigned i = 1; i < nargs; ++i)
7185 422658 : vec_safe_push (restlist, (*arglist)[i]);
7186 422658 : z_candidate *save_cand = *candidates;
7187 845316 : add_candidates (BASELINK_FUNCTIONS (fns),
7188 422658 : (*arglist)[0], restlist, NULL_TREE,
7189 : NULL_TREE, false,
7190 422658 : BASELINK_BINFO (fns),
7191 422658 : BASELINK_ACCESS_BINFO (fns),
7192 : flags, candidates, complain);
7193 : /* Release the vec if we didn't add a candidate that uses it. */
7194 423256 : for (z_candidate *c = *candidates; c != save_cand; c = c->next)
7195 423256 : if (c->args == restlist)
7196 : {
7197 422658 : restlist = NULL;
7198 422658 : break;
7199 : }
7200 422658 : release_tree_vector (restlist);
7201 : }
7202 : else
7203 8399367 : add_candidates (BASELINK_FUNCTIONS (fns),
7204 : NULL_TREE, arglist, NULL_TREE,
7205 : NULL_TREE, false,
7206 8399367 : BASELINK_BINFO (fns),
7207 8399367 : BASELINK_ACCESS_BINFO (fns),
7208 : flags, candidates, complain);
7209 : }
7210 : }
7211 : /* Per [over.match.oper]3.2, if no operand has a class type, then
7212 : only non-member functions that have type T1 or reference to
7213 : cv-qualified-opt T1 for the first argument, if the first argument
7214 : has an enumeration type, or T2 or reference to cv-qualified-opt
7215 : T2 for the second argument, if the second argument has an
7216 : enumeration type. Filter out those that don't match. */
7217 14378040 : else if (! arg2_type || ! CLASS_TYPE_P (arg2_type))
7218 : {
7219 : struct z_candidate **candp, **next;
7220 :
7221 236702663 : for (candp = candidates; *candp != start_candidates; candp = next)
7222 : {
7223 222909790 : unsigned i;
7224 222909790 : z_candidate *cand = *candp;
7225 222909790 : next = &cand->next;
7226 :
7227 222909790 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
7228 :
7229 657737875 : for (i = 0; i < nargs; ++i)
7230 : {
7231 439925556 : tree parmtype = TREE_VALUE (parmlist);
7232 439925556 : tree argtype = unlowered_expr_type ((*arglist)[i]);
7233 :
7234 439925556 : if (TYPE_REF_P (parmtype))
7235 347783658 : parmtype = TREE_TYPE (parmtype);
7236 439925556 : if (TREE_CODE (argtype) == ENUMERAL_TYPE
7237 859107086 : && (same_type_ignoring_top_level_qualifiers_p
7238 419181530 : (argtype, parmtype)))
7239 : break;
7240 :
7241 434828085 : parmlist = TREE_CHAIN (parmlist);
7242 : }
7243 :
7244 : /* No argument has an appropriate type, so remove this
7245 : candidate function from the list. */
7246 222909790 : if (i == nargs)
7247 : {
7248 217812319 : *candp = cand->next;
7249 217812319 : next = candp;
7250 : }
7251 : }
7252 : }
7253 :
7254 34231123 : if (!rewritten)
7255 : {
7256 : /* The standard says to rewrite built-in candidates, too,
7257 : but there's no point. */
7258 24100448 : add_builtin_candidates (candidates, code, code2, fnname, arglist,
7259 : flags, complain);
7260 :
7261 : /* Maybe add C++20 rewritten comparison candidates. */
7262 24100448 : tree_code rewrite_code = ERROR_MARK;
7263 24100448 : if (cxx_dialect >= cxx20
7264 23807476 : && nargs == 2
7265 43081022 : && (OVERLOAD_TYPE_P (arg1_type) || OVERLOAD_TYPE_P (arg2_type)))
7266 18972954 : switch (code)
7267 : {
7268 1310243 : case LT_EXPR:
7269 1310243 : case LE_EXPR:
7270 1310243 : case GT_EXPR:
7271 1310243 : case GE_EXPR:
7272 1310243 : case SPACESHIP_EXPR:
7273 1310243 : rewrite_code = SPACESHIP_EXPR;
7274 1310243 : break;
7275 :
7276 : case NE_EXPR:
7277 : case EQ_EXPR:
7278 : rewrite_code = EQ_EXPR;
7279 : break;
7280 :
7281 : default:;
7282 : }
7283 :
7284 1310243 : if (rewrite_code)
7285 : {
7286 6168281 : tree r;
7287 6168281 : flags |= LOOKUP_REWRITTEN;
7288 6168281 : if (rewrite_code != code)
7289 : {
7290 : /* Add rewritten candidates in same order. */
7291 3033930 : r = add_operator_candidates (candidates, rewrite_code, ERROR_MARK,
7292 : arglist, lookups, flags, complain);
7293 3033930 : if (r == error_mark_node)
7294 : return error_mark_node;
7295 : }
7296 :
7297 6168275 : z_candidate *save_cand = *candidates;
7298 :
7299 : /* Add rewritten candidates in reverse order. */
7300 6168275 : flags |= LOOKUP_REVERSED;
7301 6168275 : vec<tree,va_gc> *revlist = make_tree_vector ();
7302 6168275 : revlist->quick_push ((*arglist)[1]);
7303 6168275 : revlist->quick_push ((*arglist)[0]);
7304 6168275 : r = add_operator_candidates (candidates, rewrite_code, ERROR_MARK,
7305 : revlist, lookups, flags, complain);
7306 6168275 : if (r == error_mark_node)
7307 : return error_mark_node;
7308 :
7309 : /* Release the vec if we didn't add a candidate that uses it. */
7310 6328226 : for (z_candidate *c = *candidates; c != save_cand; c = c->next)
7311 2929165 : if (c->args == revlist)
7312 : {
7313 : revlist = NULL;
7314 : break;
7315 : }
7316 6168275 : release_tree_vector (revlist);
7317 : }
7318 : }
7319 :
7320 : return NULL_TREE;
7321 : }
7322 :
7323 : tree
7324 214649448 : build_new_op (const op_location_t &loc, enum tree_code code, int flags,
7325 : tree arg1, tree arg2, tree arg3, tree lookups,
7326 : tree *overload, tsubst_flags_t complain)
7327 : {
7328 214649448 : struct z_candidate *candidates = 0, *cand;
7329 214649448 : releasing_vec arglist;
7330 214649448 : tree result = NULL_TREE;
7331 214649448 : bool result_valid_p = false;
7332 214649448 : enum tree_code code2 = ERROR_MARK;
7333 214649448 : enum tree_code code_orig_arg1 = ERROR_MARK;
7334 214649448 : enum tree_code code_orig_arg2 = ERROR_MARK;
7335 214649448 : bool strict_p;
7336 214649448 : bool any_viable_p;
7337 :
7338 214649448 : auto_cond_timevar tv (TV_OVERLOAD);
7339 :
7340 214649448 : if (error_operand_p (arg1)
7341 214645212 : || error_operand_p (arg2)
7342 429288610 : || error_operand_p (arg3))
7343 10286 : return error_mark_node;
7344 :
7345 214639162 : conversion_obstack_sentinel cos;
7346 :
7347 214639162 : bool ismodop = code == MODIFY_EXPR;
7348 214639162 : if (ismodop)
7349 : {
7350 10992656 : code2 = TREE_CODE (arg3);
7351 10992656 : arg3 = NULL_TREE;
7352 : }
7353 :
7354 214639162 : tree arg1_type = unlowered_expr_type (arg1);
7355 214639162 : tree arg2_type = arg2 ? unlowered_expr_type (arg2) : NULL_TREE;
7356 :
7357 214639162 : arg1 = prep_operand (arg1);
7358 :
7359 214639162 : switch (code)
7360 : {
7361 0 : case NEW_EXPR:
7362 0 : case VEC_NEW_EXPR:
7363 0 : case VEC_DELETE_EXPR:
7364 0 : case DELETE_EXPR:
7365 : /* Use build_operator_new_call and build_op_delete_call instead. */
7366 0 : gcc_unreachable ();
7367 :
7368 0 : case CALL_EXPR:
7369 : /* Use build_op_call instead. */
7370 0 : gcc_unreachable ();
7371 :
7372 18126015 : case TRUTH_ORIF_EXPR:
7373 18126015 : case TRUTH_ANDIF_EXPR:
7374 18126015 : case TRUTH_AND_EXPR:
7375 18126015 : case TRUTH_OR_EXPR:
7376 : /* These are saved for the sake of warn_logical_operator. */
7377 18126015 : code_orig_arg1 = TREE_CODE (arg1);
7378 18126015 : code_orig_arg2 = TREE_CODE (arg2);
7379 18126015 : break;
7380 50162995 : case GT_EXPR:
7381 50162995 : case LT_EXPR:
7382 50162995 : case GE_EXPR:
7383 50162995 : case LE_EXPR:
7384 50162995 : case EQ_EXPR:
7385 50162995 : case NE_EXPR:
7386 : /* These are saved for the sake of maybe_warn_bool_compare. */
7387 50162995 : code_orig_arg1 = TREE_CODE (arg1_type);
7388 50162995 : code_orig_arg2 = TREE_CODE (arg2_type);
7389 50162995 : break;
7390 :
7391 : default:
7392 : break;
7393 : }
7394 :
7395 214639162 : arg2 = prep_operand (arg2);
7396 214639162 : arg3 = prep_operand (arg3);
7397 :
7398 214639162 : if (code == COND_EXPR)
7399 : /* Use build_conditional_expr instead. */
7400 0 : gcc_unreachable ();
7401 214639162 : else if (! OVERLOAD_TYPE_P (arg1_type)
7402 404676565 : && (! arg2 || ! OVERLOAD_TYPE_P (arg2_type)))
7403 189610199 : goto builtin;
7404 :
7405 25028963 : if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
7406 : {
7407 311055 : arg2 = integer_zero_node;
7408 311055 : arg2_type = integer_type_node;
7409 : }
7410 :
7411 25028963 : arglist->quick_push (arg1);
7412 25028963 : if (arg2 != NULL_TREE)
7413 20080491 : arglist->quick_push (arg2);
7414 25028963 : if (arg3 != NULL_TREE)
7415 0 : arglist->quick_push (arg3);
7416 :
7417 25028963 : result = add_operator_candidates (&candidates, code, code2, arglist,
7418 : lookups, flags, complain);
7419 25028930 : if (result == error_mark_node)
7420 : return error_mark_node;
7421 :
7422 25028918 : switch (code)
7423 : {
7424 : case COMPOUND_EXPR:
7425 : case ADDR_EXPR:
7426 : /* For these, the built-in candidates set is empty
7427 : [over.match.oper]/3. We don't want non-strict matches
7428 : because exact matches are always possible with built-in
7429 : operators. The built-in candidate set for COMPONENT_REF
7430 : would be empty too, but since there are no such built-in
7431 : operators, we accept non-strict matches for them. */
7432 : strict_p = true;
7433 : break;
7434 :
7435 23137326 : default:
7436 23137326 : strict_p = false;
7437 23137326 : break;
7438 : }
7439 :
7440 25028918 : candidates = splice_viable (candidates, strict_p, &any_viable_p);
7441 25028918 : if (!any_viable_p)
7442 : {
7443 1942270 : switch (code)
7444 : {
7445 357 : case POSTINCREMENT_EXPR:
7446 357 : case POSTDECREMENT_EXPR:
7447 : /* Don't try anything fancy if we're not allowed to produce
7448 : errors. */
7449 357 : if (!(complain & tf_error))
7450 : return error_mark_node;
7451 :
7452 : /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
7453 : distinguish between prefix and postfix ++ and
7454 : operator++() was used for both, so we allow this with
7455 : -fpermissive. */
7456 : else
7457 : {
7458 42 : tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
7459 30 : const char *msg = (flag_permissive)
7460 42 : ? G_("no %<%D(int)%> declared for postfix %qs,"
7461 : " trying prefix operator instead")
7462 : : G_("no %<%D(int)%> declared for postfix %qs");
7463 42 : permerror (loc, msg, fnname, OVL_OP_INFO (false, code)->name);
7464 : }
7465 :
7466 42 : if (!flag_permissive)
7467 30 : return error_mark_node;
7468 :
7469 12 : if (code == POSTINCREMENT_EXPR)
7470 : code = PREINCREMENT_EXPR;
7471 : else
7472 0 : code = PREDECREMENT_EXPR;
7473 12 : result = build_new_op (loc, code, flags, arg1, NULL_TREE,
7474 : NULL_TREE, lookups, overload, complain);
7475 12 : break;
7476 :
7477 : /* The caller will deal with these. */
7478 : case ADDR_EXPR:
7479 : case COMPOUND_EXPR:
7480 : case COMPONENT_REF:
7481 : case CO_AWAIT_EXPR:
7482 : result = NULL_TREE;
7483 : result_valid_p = true;
7484 : break;
7485 :
7486 47589 : default:
7487 47589 : if (complain & tf_error)
7488 : {
7489 : /* If one of the arguments of the operator represents
7490 : an invalid use of member function pointer, try to report
7491 : a meaningful error ... */
7492 1346 : if (invalid_nonstatic_memfn_p (loc, arg1, tf_error)
7493 1343 : || invalid_nonstatic_memfn_p (loc, arg2, tf_error)
7494 2683 : || invalid_nonstatic_memfn_p (loc, arg3, tf_error))
7495 : /* We displayed the error message. */;
7496 : else
7497 : {
7498 : /* ... Otherwise, report the more generic
7499 : "no matching operator found" error */
7500 1337 : auto_diagnostic_group d;
7501 1337 : op_error (loc, code, code2, arg1, arg2, arg3, false);
7502 1337 : print_z_candidates (loc, candidates);
7503 1337 : }
7504 : }
7505 47589 : result = error_mark_node;
7506 47589 : break;
7507 : }
7508 : }
7509 : else
7510 : {
7511 23086648 : cand = tourney (candidates, complain);
7512 23086648 : if (cand == 0)
7513 : {
7514 184 : if (complain & tf_error)
7515 : {
7516 129 : auto_diagnostic_group d;
7517 129 : op_error (loc, code, code2, arg1, arg2, arg3, true);
7518 129 : print_z_candidates (loc, candidates);
7519 129 : }
7520 184 : result = error_mark_node;
7521 184 : if (overload)
7522 165 : *overload = error_mark_node;
7523 : }
7524 23086464 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
7525 : {
7526 19000066 : if (overload)
7527 : {
7528 14580127 : if (cand->rewritten ())
7529 : /* build_min_non_dep_op_overload needs to know whether the
7530 : candidate is rewritten/reversed. */
7531 1816063 : *overload = build_tree_list (build_int_cst (integer_type_node,
7532 1816063 : cand->flags),
7533 : cand->fn);
7534 : else
7535 12764064 : *overload = cand->fn;
7536 : }
7537 :
7538 19000066 : if (resolve_args (arglist, complain) == NULL)
7539 6 : result = error_mark_node;
7540 : else
7541 : {
7542 19000060 : tsubst_flags_t ocomplain = complain;
7543 19000060 : if (cand->rewritten ())
7544 : /* We'll wrap this call in another one. */
7545 1816669 : ocomplain &= ~tf_decltype;
7546 19000060 : if (cand->reversed ())
7547 : {
7548 : /* We swapped these in add_candidate, swap them back now. */
7549 20996 : std::swap (cand->convs[0], cand->convs[1]);
7550 20996 : if (cand->fn == current_function_decl)
7551 1 : warning_at (loc, 0, "in C++20 this comparison calls the "
7552 : "current function recursively with reversed "
7553 : "arguments");
7554 : }
7555 19000060 : result = build_over_call (cand, LOOKUP_NORMAL, ocomplain);
7556 : }
7557 :
7558 35260519 : if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn))
7559 : /* There won't be a CALL_EXPR. */;
7560 16252963 : else if (result && result != error_mark_node)
7561 : {
7562 16250327 : tree call = extract_call_expr (result);
7563 16250327 : CALL_EXPR_OPERATOR_SYNTAX (call) = true;
7564 :
7565 : /* Specify evaluation order as per P0145R2. */
7566 16250327 : CALL_EXPR_ORDERED_ARGS (call) = false;
7567 16250327 : switch (op_is_ordered (code))
7568 : {
7569 3620536 : case -1:
7570 3620536 : CALL_EXPR_REVERSE_ARGS (call) = true;
7571 3620536 : break;
7572 :
7573 478217 : case 1:
7574 478217 : CALL_EXPR_ORDERED_ARGS (call) = true;
7575 478217 : break;
7576 :
7577 : default:
7578 : break;
7579 : }
7580 : }
7581 :
7582 : /* If this was a C++20 rewritten comparison, adjust the result. */
7583 19000063 : if (cand->rewritten ())
7584 : {
7585 1816669 : switch (code)
7586 : {
7587 9820 : case EQ_EXPR:
7588 9820 : gcc_checking_assert (cand->reversed ());
7589 887144 : gcc_fallthrough ();
7590 887144 : case NE_EXPR:
7591 887144 : if (result == error_mark_node)
7592 : ;
7593 : /* If a rewritten operator== candidate is selected by
7594 : overload resolution for an operator @, its return type
7595 : shall be cv bool.... */
7596 887142 : else if (TREE_CODE (TREE_TYPE (result)) != BOOLEAN_TYPE)
7597 : {
7598 44 : if (complain & tf_error)
7599 : {
7600 6 : auto_diagnostic_group d;
7601 6 : error_at (loc, "return type of %qD is not %qs",
7602 : cand->fn, "bool");
7603 6 : inform (loc, "used as rewritten candidate for "
7604 : "comparison of %qT and %qT",
7605 : arg1_type, arg2_type);
7606 6 : }
7607 44 : result = error_mark_node;
7608 : }
7609 887098 : else if (code == NE_EXPR)
7610 : /* !(y == x) or !(x == y) */
7611 877299 : result = build1_loc (loc, TRUTH_NOT_EXPR,
7612 : boolean_type_node, result);
7613 : break;
7614 :
7615 : /* If a rewritten operator<=> candidate is selected by
7616 : overload resolution for an operator @, x @ y is
7617 : interpreted as 0 @ (y <=> x) if the selected candidate is
7618 : a synthesized candidate with reversed order of parameters,
7619 : or (x <=> y) @ 0 otherwise, using the selected rewritten
7620 : operator<=> candidate. */
7621 637 : case SPACESHIP_EXPR:
7622 637 : if (!cand->reversed ())
7623 : /* We're in the build_new_op call below for an outer
7624 : reversed call; we don't need to do anything more. */
7625 : break;
7626 929211 : gcc_fallthrough ();
7627 929211 : case LT_EXPR:
7628 929211 : case LE_EXPR:
7629 929211 : case GT_EXPR:
7630 929211 : case GE_EXPR:
7631 929211 : {
7632 929211 : tree lhs = result;
7633 929211 : tree rhs = integer_zero_node;
7634 929211 : if (cand->reversed ())
7635 2148 : std::swap (lhs, rhs);
7636 929211 : warning_sentinel ws (warn_zero_as_null_pointer_constant);
7637 929211 : result = build_new_op (loc, code,
7638 : LOOKUP_NORMAL|LOOKUP_REWRITTEN,
7639 : lhs, rhs, NULL_TREE, lookups,
7640 : NULL, complain);
7641 929211 : }
7642 929211 : break;
7643 :
7644 0 : default:
7645 0 : gcc_unreachable ();
7646 : }
7647 : }
7648 :
7649 : /* In an expression of the form `a[]' where cand->fn
7650 : which is operator[] turns out to be a static member function,
7651 : `a' is none-the-less evaluated. */
7652 19000063 : if (code == ARRAY_REF)
7653 422641 : result = keep_unused_object_arg (result, arg1, cand->fn);
7654 : }
7655 : else
7656 : {
7657 : /* Give any warnings we noticed during overload resolution. */
7658 4086398 : if (cand->warnings && (complain & tf_warning))
7659 : {
7660 : struct candidate_warning *w;
7661 0 : for (w = cand->warnings; w; w = w->next)
7662 0 : joust (cand, w->loser, 1, complain);
7663 : }
7664 :
7665 : /* Check for comparison of different enum types. */
7666 4086398 : switch (code)
7667 : {
7668 3204681 : case GT_EXPR:
7669 3204681 : case LT_EXPR:
7670 3204681 : case GE_EXPR:
7671 3204681 : case LE_EXPR:
7672 3204681 : case EQ_EXPR:
7673 3204681 : case NE_EXPR:
7674 3204681 : if (TREE_CODE (arg1_type) == ENUMERAL_TYPE
7675 2999912 : && TREE_CODE (arg2_type) == ENUMERAL_TYPE
7676 6128609 : && (TYPE_MAIN_VARIANT (arg1_type)
7677 2923928 : != TYPE_MAIN_VARIANT (arg2_type)))
7678 : {
7679 80 : if (cxx_dialect >= cxx26
7680 26 : && (complain & tf_warning_or_error) == 0)
7681 2 : result = error_mark_node;
7682 78 : else if (cxx_dialect >= cxx26 || (complain & tf_warning))
7683 130 : emit_diagnostic ((cxx_dialect >= cxx26
7684 : ? diagnostics::kind::pedwarn
7685 : : diagnostics::kind::warning),
7686 77 : loc, OPT_Wenum_compare,
7687 : "comparison between %q#T and %q#T",
7688 : arg1_type, arg2_type);
7689 : }
7690 : break;
7691 : default:
7692 : break;
7693 : }
7694 :
7695 : /* "If a built-in candidate is selected by overload resolution, the
7696 : operands of class type are converted to the types of the
7697 : corresponding parameters of the selected operation function,
7698 : except that the second standard conversion sequence of a
7699 : user-defined conversion sequence (12.3.3.1.2) is not applied." */
7700 4086398 : conversion *conv = cand->convs[0];
7701 4086398 : if (conv->user_conv_p)
7702 : {
7703 64716 : conv = strip_standard_conversion (conv);
7704 64716 : arg1 = convert_like (conv, arg1, complain);
7705 : }
7706 :
7707 4086398 : if (arg2)
7708 : {
7709 3596998 : conv = cand->convs[1];
7710 3596998 : if (conv->user_conv_p)
7711 : {
7712 11256 : conv = strip_standard_conversion (conv);
7713 11256 : arg2 = convert_like (conv, arg2, complain);
7714 : }
7715 : }
7716 :
7717 4086398 : if (arg3)
7718 : {
7719 0 : conv = cand->convs[2];
7720 0 : if (conv->user_conv_p)
7721 : {
7722 0 : conv = strip_standard_conversion (conv);
7723 0 : arg3 = convert_like (conv, arg3, complain);
7724 : }
7725 : }
7726 : }
7727 : }
7728 :
7729 25028570 : if (result || result_valid_p)
7730 : return result;
7731 :
7732 4086396 : builtin:
7733 193696595 : switch (code)
7734 : {
7735 4595624 : case MODIFY_EXPR:
7736 4595624 : return cp_build_modify_expr (loc, arg1, code2, arg2, complain);
7737 :
7738 20321954 : case INDIRECT_REF:
7739 20321954 : return cp_build_indirect_ref (loc, arg1, RO_UNARY_STAR, complain);
7740 :
7741 18123832 : case TRUTH_ANDIF_EXPR:
7742 18123832 : case TRUTH_ORIF_EXPR:
7743 18123832 : case TRUTH_AND_EXPR:
7744 18123832 : case TRUTH_OR_EXPR:
7745 18123832 : if ((complain & tf_warning) && !processing_template_decl)
7746 7603530 : warn_logical_operator (loc, code, boolean_type_node,
7747 : code_orig_arg1, arg1,
7748 : code_orig_arg2, arg2);
7749 : /* Fall through. */
7750 63672555 : case GT_EXPR:
7751 63672555 : case LT_EXPR:
7752 63672555 : case GE_EXPR:
7753 63672555 : case LE_EXPR:
7754 63672555 : case EQ_EXPR:
7755 63672555 : case NE_EXPR:
7756 63672555 : if ((complain & tf_warning)
7757 57678954 : && ((code_orig_arg1 == BOOLEAN_TYPE)
7758 57678954 : ^ (code_orig_arg2 == BOOLEAN_TYPE)))
7759 26339 : maybe_warn_bool_compare (loc, code, arg1, arg2);
7760 57678954 : if (complain & tf_warning && warn_tautological_compare)
7761 450652 : warn_tautological_cmp (loc, code, arg1, arg2);
7762 : /* Fall through. */
7763 132605541 : case SPACESHIP_EXPR:
7764 132605541 : case PLUS_EXPR:
7765 132605541 : case MINUS_EXPR:
7766 132605541 : case MULT_EXPR:
7767 132605541 : case TRUNC_DIV_EXPR:
7768 132605541 : case MAX_EXPR:
7769 132605541 : case MIN_EXPR:
7770 132605541 : case LSHIFT_EXPR:
7771 132605541 : case RSHIFT_EXPR:
7772 132605541 : case TRUNC_MOD_EXPR:
7773 132605541 : case BIT_AND_EXPR:
7774 132605541 : case BIT_IOR_EXPR:
7775 132605541 : case BIT_XOR_EXPR:
7776 132605541 : return cp_build_binary_op (loc, code, arg1, arg2, complain);
7777 :
7778 30768218 : case UNARY_PLUS_EXPR:
7779 30768218 : case NEGATE_EXPR:
7780 30768218 : case BIT_NOT_EXPR:
7781 30768218 : case TRUTH_NOT_EXPR:
7782 30768218 : case PREINCREMENT_EXPR:
7783 30768218 : case POSTINCREMENT_EXPR:
7784 30768218 : case PREDECREMENT_EXPR:
7785 30768218 : case POSTDECREMENT_EXPR:
7786 30768218 : case REALPART_EXPR:
7787 30768218 : case IMAGPART_EXPR:
7788 30768218 : case ABS_EXPR:
7789 30768218 : case CO_AWAIT_EXPR:
7790 30768218 : return cp_build_unary_op (code, arg1, false, complain);
7791 :
7792 2396210 : case ARRAY_REF:
7793 2396210 : return cp_build_array_ref (input_location, arg1, arg2, complain);
7794 :
7795 769 : case MEMBER_REF:
7796 769 : return build_m_component_ref (cp_build_indirect_ref (loc, arg1,
7797 : RO_ARROW_STAR,
7798 : complain),
7799 769 : arg2, complain);
7800 :
7801 : /* The caller will deal with these. */
7802 : case ADDR_EXPR:
7803 : case COMPONENT_REF:
7804 : case COMPOUND_EXPR:
7805 : return NULL_TREE;
7806 :
7807 0 : default:
7808 0 : gcc_unreachable ();
7809 : }
7810 : return NULL_TREE;
7811 214649412 : }
7812 :
7813 : /* Build a new call to operator[]. This may change ARGS. */
7814 :
7815 : tree
7816 704 : build_op_subscript (const op_location_t &loc, tree obj,
7817 : vec<tree, va_gc> **args, tree *overload,
7818 : tsubst_flags_t complain)
7819 : {
7820 704 : struct z_candidate *candidates = 0, *cand;
7821 704 : tree fns, first_mem_arg = NULL_TREE;
7822 704 : bool any_viable_p;
7823 704 : tree result = NULL_TREE;
7824 :
7825 704 : auto_cond_timevar tv (TV_OVERLOAD);
7826 :
7827 704 : obj = mark_lvalue_use (obj);
7828 :
7829 704 : if (error_operand_p (obj))
7830 0 : return error_mark_node;
7831 :
7832 704 : tree type = TREE_TYPE (obj);
7833 :
7834 704 : obj = prep_operand (obj);
7835 :
7836 704 : if (TYPE_BINFO (type))
7837 : {
7838 704 : fns = lookup_fnfields (TYPE_BINFO (type), ovl_op_identifier (ARRAY_REF),
7839 : 1, complain);
7840 704 : if (fns == error_mark_node)
7841 : return error_mark_node;
7842 : }
7843 : else
7844 : fns = NULL_TREE;
7845 :
7846 704 : if (args != NULL && *args != NULL)
7847 : {
7848 704 : *args = resolve_args (*args, complain);
7849 704 : if (*args == NULL)
7850 0 : return error_mark_node;
7851 : }
7852 :
7853 704 : conversion_obstack_sentinel cos;
7854 :
7855 704 : if (fns)
7856 : {
7857 702 : first_mem_arg = obj;
7858 :
7859 702 : add_candidates (BASELINK_FUNCTIONS (fns),
7860 : first_mem_arg, *args, NULL_TREE,
7861 : NULL_TREE, false,
7862 702 : BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
7863 : LOOKUP_NORMAL, &candidates, complain);
7864 : }
7865 :
7866 : /* Be strict here because if we choose a bad conversion candidate, the
7867 : errors we get won't mention the call context. */
7868 704 : candidates = splice_viable (candidates, true, &any_viable_p);
7869 704 : if (!any_viable_p)
7870 : {
7871 45 : if (complain & tf_error)
7872 : {
7873 1 : auto_diagnostic_group d;
7874 2 : error ("no match for call to %<%T::operator[] (%A)%>",
7875 1 : TREE_TYPE (obj), build_tree_list_vec (*args));
7876 1 : print_z_candidates (loc, candidates);
7877 1 : }
7878 45 : result = error_mark_node;
7879 : }
7880 : else
7881 : {
7882 659 : cand = tourney (candidates, complain);
7883 659 : if (cand == 0)
7884 : {
7885 0 : if (complain & tf_error)
7886 : {
7887 0 : auto_diagnostic_group d;
7888 0 : error ("call of %<%T::operator[] (%A)%> is ambiguous",
7889 0 : TREE_TYPE (obj), build_tree_list_vec (*args));
7890 0 : print_z_candidates (loc, candidates);
7891 0 : }
7892 0 : result = error_mark_node;
7893 : }
7894 659 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL
7895 659 : && DECL_OVERLOADED_OPERATOR_P (cand->fn)
7896 1318 : && DECL_OVERLOADED_OPERATOR_IS (cand->fn, ARRAY_REF))
7897 : {
7898 659 : if (overload)
7899 659 : *overload = cand->fn;
7900 659 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
7901 1318 : if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn))
7902 : /* There won't be a CALL_EXPR. */;
7903 659 : else if (result && result != error_mark_node)
7904 : {
7905 659 : tree call = extract_call_expr (result);
7906 659 : CALL_EXPR_OPERATOR_SYNTAX (call) = true;
7907 :
7908 : /* Specify evaluation order as per P0145R2. */
7909 659 : CALL_EXPR_ORDERED_ARGS (call) = op_is_ordered (ARRAY_REF) == 1;
7910 : }
7911 :
7912 : /* In an expression of the form `a[]' where cand->fn
7913 : which is operator[] turns out to be a static member function,
7914 : `a' is none-the-less evaluated. */
7915 659 : result = keep_unused_object_arg (result, obj, cand->fn);
7916 : }
7917 : else
7918 0 : gcc_unreachable ();
7919 : }
7920 :
7921 704 : return result;
7922 704 : }
7923 :
7924 : /* CALL was returned by some call-building function; extract the actual
7925 : CALL_EXPR from any bits that have been tacked on, e.g. by
7926 : convert_from_reference. */
7927 :
7928 : tree
7929 44824982 : extract_call_expr (tree call)
7930 : {
7931 44825664 : while (TREE_CODE (call) == COMPOUND_EXPR)
7932 682 : call = TREE_OPERAND (call, 1);
7933 44824982 : if (REFERENCE_REF_P (call))
7934 10227487 : call = TREE_OPERAND (call, 0);
7935 44824982 : if (TREE_CODE (call) == TARGET_EXPR)
7936 3050955 : call = TARGET_EXPR_INITIAL (call);
7937 :
7938 44824982 : if (TREE_CODE (call) != CALL_EXPR
7939 849976 : && TREE_CODE (call) != AGGR_INIT_EXPR
7940 746327 : && call != error_mark_node)
7941 746309 : return NULL_TREE;
7942 : return call;
7943 : }
7944 :
7945 : /* Returns true if FN has two parameters, of which the second has type
7946 : size_t. */
7947 :
7948 : static bool
7949 664043 : second_parm_is_size_t (tree fn)
7950 : {
7951 664043 : tree t = FUNCTION_ARG_CHAIN (fn);
7952 664043 : if (!t || !same_type_p (TREE_VALUE (t), size_type_node))
7953 664028 : return false;
7954 15 : t = TREE_CHAIN (t);
7955 15 : if (t == void_list_node)
7956 : return true;
7957 : return false;
7958 : }
7959 :
7960 : /* True if T, an allocation function, has std::align_val_t as its second
7961 : argument. */
7962 :
7963 : bool
7964 342284 : aligned_allocation_fn_p (tree t)
7965 : {
7966 342284 : if (!aligned_new_threshold)
7967 : return false;
7968 :
7969 332990 : tree a = FUNCTION_ARG_CHAIN (t);
7970 332990 : return (a && same_type_p (TREE_VALUE (a), align_type_node));
7971 : }
7972 :
7973 : /* True if T is std::destroying_delete_t. */
7974 :
7975 : static bool
7976 5731085 : std_destroying_delete_t_p (tree t)
7977 : {
7978 5731085 : return (TYPE_CONTEXT (t) == std_node
7979 5731085 : && id_equal (TYPE_IDENTIFIER (t), "destroying_delete_t"));
7980 : }
7981 :
7982 : /* A deallocation function with at least two parameters whose second parameter
7983 : type is of type std::destroying_delete_t is a destroying operator delete. A
7984 : destroying operator delete shall be a class member function named operator
7985 : delete. [ Note: Array deletion cannot use a destroying operator
7986 : delete. --end note ] */
7987 :
7988 : tree
7989 5731100 : destroying_delete_p (tree t)
7990 : {
7991 5731100 : tree a = TYPE_ARG_TYPES (TREE_TYPE (t));
7992 5731100 : if (!a || !TREE_CHAIN (a))
7993 : return NULL_TREE;
7994 5731085 : tree type = TREE_VALUE (TREE_CHAIN (a));
7995 5731085 : return std_destroying_delete_t_p (type) ? type : NULL_TREE;
7996 : }
7997 :
7998 : struct dealloc_info
7999 : {
8000 : bool sized;
8001 : bool aligned;
8002 : tree destroying;
8003 : };
8004 :
8005 : /* Returns true iff T, an element of an OVERLOAD chain, is a usual deallocation
8006 : function (3.7.4.2 [basic.stc.dynamic.deallocation]). If so, and DI is
8007 : non-null, also set *DI. */
8008 :
8009 : static bool
8010 3918831 : usual_deallocation_fn_p (tree t, dealloc_info *di)
8011 : {
8012 3918831 : if (di) *di = dealloc_info();
8013 :
8014 : /* A template instance is never a usual deallocation function,
8015 : regardless of its signature. */
8016 3918831 : if (TREE_CODE (t) == TEMPLATE_DECL
8017 3918831 : || primary_template_specialization_p (t))
8018 12 : return false;
8019 :
8020 : /* A usual deallocation function is a deallocation function whose parameters
8021 : after the first are
8022 : - optionally, a parameter of type std::destroying_delete_t, then
8023 : - optionally, a parameter of type std::size_t, then
8024 : - optionally, a parameter of type std::align_val_t. */
8025 3918819 : bool global = DECL_NAMESPACE_SCOPE_P (t);
8026 3918819 : tree chain = FUNCTION_ARG_CHAIN (t);
8027 3918819 : if (chain && destroying_delete_p (t))
8028 : {
8029 86 : if (di) di->destroying = TREE_VALUE (chain);
8030 86 : chain = TREE_CHAIN (chain);
8031 : }
8032 3918819 : if (chain
8033 3918816 : && (!global || flag_sized_deallocation)
8034 7823455 : && same_type_p (TREE_VALUE (chain), size_type_node))
8035 : {
8036 1130116 : if (di) di->sized = true;
8037 1130116 : chain = TREE_CHAIN (chain);
8038 : }
8039 3918816 : if (chain && aligned_new_threshold
8040 7822422 : && same_type_p (TREE_VALUE (chain), align_type_node))
8041 : {
8042 1677460 : if (di) di->aligned = true;
8043 1677460 : chain = TREE_CHAIN (chain);
8044 : }
8045 3918819 : return (chain == void_list_node);
8046 : }
8047 :
8048 : /* Just return whether FN is a usual deallocation function. */
8049 :
8050 : bool
8051 8841 : usual_deallocation_fn_p (tree fn)
8052 : {
8053 8841 : return usual_deallocation_fn_p (fn, NULL);
8054 : }
8055 :
8056 : /* Build a call to operator delete. This has to be handled very specially,
8057 : because the restrictions on what signatures match are different from all
8058 : other call instances. For a normal delete, only a delete taking (void *)
8059 : or (void *, size_t) is accepted. For a placement delete, only an exact
8060 : match with the placement new is accepted.
8061 :
8062 : CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
8063 : ADDR is the pointer to be deleted.
8064 : SIZE is the size of the memory block to be deleted.
8065 : GLOBAL_P is true if the delete-expression should not consider
8066 : class-specific delete operators.
8067 : CORO_P is true if the allocation is for a coroutine, where the two argument
8068 : usual deallocation should be chosen in preference to the single argument
8069 : version in a class context.
8070 : PLACEMENT is the corresponding placement new call, or NULL_TREE.
8071 :
8072 : If this call to "operator delete" is being generated as part to
8073 : deallocate memory allocated via a new-expression (as per [expr.new]
8074 : which requires that if the initialization throws an exception then
8075 : we call a deallocation function), then ALLOC_FN is the allocation
8076 : function. */
8077 :
8078 : static tree
8079 1234616 : build_op_delete_call_1 (enum tree_code code, tree addr, tree size,
8080 : bool global_p, bool coro_p, tree placement,
8081 : tree alloc_fn, tsubst_flags_t complain)
8082 : {
8083 1234616 : tree fn = NULL_TREE;
8084 1234616 : tree fns, fnname, type, t;
8085 1234616 : dealloc_info di_fn = { };
8086 :
8087 1234616 : if (addr == error_mark_node)
8088 : return error_mark_node;
8089 :
8090 1234616 : type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
8091 :
8092 1234616 : fnname = ovl_op_identifier (false, code);
8093 :
8094 860444 : if (CLASS_TYPE_P (type)
8095 860408 : && COMPLETE_TYPE_P (complete_type (type))
8096 2094989 : && !global_p)
8097 : /* In [class.free]
8098 :
8099 : If the result of the lookup is ambiguous or inaccessible, or if
8100 : the lookup selects a placement deallocation function, the
8101 : program is ill-formed.
8102 :
8103 : Therefore, we ask lookup_fnfields to complain about ambiguity. */
8104 : {
8105 508534 : fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1, complain);
8106 508534 : if (fns == error_mark_node)
8107 : return error_mark_node;
8108 : }
8109 : else
8110 : fns = NULL_TREE;
8111 :
8112 508531 : if (fns == NULL_TREE)
8113 1233541 : fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE);
8114 :
8115 : /* Strip const and volatile from addr. */
8116 1234613 : tree oaddr = addr;
8117 1234613 : addr = cp_convert (ptr_type_node, addr, complain);
8118 :
8119 1234613 : tree excluded_destroying = NULL_TREE;
8120 :
8121 1234613 : if (placement)
8122 : {
8123 : /* "A declaration of a placement deallocation function matches the
8124 : declaration of a placement allocation function if it has the same
8125 : number of parameters and, after parameter transformations (8.3.5),
8126 : all parameter types except the first are identical."
8127 :
8128 : So we build up the function type we want and ask instantiate_type
8129 : to get it for us. */
8130 664927 : t = FUNCTION_ARG_CHAIN (alloc_fn);
8131 664927 : t = tree_cons (NULL_TREE, ptr_type_node, t);
8132 664927 : t = build_function_type (void_type_node, t);
8133 :
8134 664927 : fn = instantiate_type (t, fns, tf_none);
8135 664927 : if (fn == error_mark_node)
8136 : return NULL_TREE;
8137 :
8138 664043 : fn = MAYBE_BASELINK_FUNCTIONS (fn);
8139 :
8140 : /* "If the lookup finds the two-parameter form of a usual deallocation
8141 : function (3.7.4.2) and that function, considered as a placement
8142 : deallocation function, would have been selected as a match for the
8143 : allocation function, the program is ill-formed." */
8144 664043 : if (second_parm_is_size_t (fn))
8145 : {
8146 9 : const char *const msg1
8147 : = G_("exception cleanup for this placement new selects "
8148 : "non-placement %<operator delete%>");
8149 9 : const char *const msg2
8150 : = G_("%qD is a usual (non-placement) deallocation "
8151 : "function in C++14 (or with %<-fsized-deallocation%>)");
8152 :
8153 : /* But if the class has an operator delete (void *), then that is
8154 : the usual deallocation function, so we shouldn't complain
8155 : about using the operator delete (void *, size_t). */
8156 9 : if (DECL_CLASS_SCOPE_P (fn))
8157 18 : for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns)))
8158 : {
8159 9 : if (usual_deallocation_fn_p (elt)
8160 9 : && FUNCTION_ARG_CHAIN (elt) == void_list_node)
8161 3 : goto ok;
8162 : }
8163 : /* Before C++14 a two-parameter global deallocation function is
8164 : always a placement deallocation function, but warn if
8165 : -Wc++14-compat. */
8166 3 : else if (!flag_sized_deallocation)
8167 : {
8168 1 : if (complain & tf_warning)
8169 : {
8170 1 : auto_diagnostic_group d;
8171 1 : if (warning (OPT_Wc__14_compat, msg1))
8172 1 : inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
8173 1 : }
8174 1 : goto ok;
8175 : }
8176 :
8177 5 : if (complain & tf_warning_or_error)
8178 : {
8179 5 : auto_diagnostic_group d;
8180 5 : if (permerror (input_location, msg1))
8181 : {
8182 : /* Only mention C++14 for namespace-scope delete. */
8183 5 : if (DECL_NAMESPACE_SCOPE_P (fn))
8184 2 : inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
8185 : else
8186 3 : inform (DECL_SOURCE_LOCATION (fn),
8187 : "%qD is a usual (non-placement) deallocation "
8188 : "function", fn);
8189 : }
8190 5 : }
8191 : else
8192 0 : return error_mark_node;
8193 1233729 : ok:;
8194 : }
8195 : }
8196 : else
8197 : /* "Any non-placement deallocation function matches a non-placement
8198 : allocation function. If the lookup finds a single matching
8199 : deallocation function, that function will be called; otherwise, no
8200 : deallocation function will be called." */
8201 5049362 : for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns)))
8202 : {
8203 3909990 : dealloc_info di_elt;
8204 3909990 : if (usual_deallocation_fn_p (elt, &di_elt))
8205 : {
8206 : /* If we're called for an EH cleanup in a new-expression, we can't
8207 : use a destroying delete; the exception was thrown before the
8208 : object was constructed. */
8209 2258328 : if (alloc_fn && di_elt.destroying)
8210 : {
8211 13 : excluded_destroying = elt;
8212 1150985 : continue;
8213 : }
8214 :
8215 2258315 : if (!fn)
8216 : {
8217 569661 : fn = elt;
8218 569661 : di_fn = di_elt;
8219 569661 : continue;
8220 : }
8221 :
8222 : /* -- If any of the deallocation functions is a destroying
8223 : operator delete, all deallocation functions that are not
8224 : destroying operator deletes are eliminated from further
8225 : consideration. */
8226 1688654 : if (di_elt.destroying != di_fn.destroying)
8227 : {
8228 12 : if (di_elt.destroying)
8229 : {
8230 6 : fn = elt;
8231 6 : di_fn = di_elt;
8232 : }
8233 12 : continue;
8234 : }
8235 :
8236 : /* -- If the type has new-extended alignment, a function with a
8237 : parameter of type std::align_val_t is preferred; otherwise a
8238 : function without such a parameter is preferred. If exactly one
8239 : preferred function is found, that function is selected and the
8240 : selection process terminates. If more than one preferred
8241 : function is found, all non-preferred functions are eliminated
8242 : from further consideration. */
8243 1688642 : if (aligned_new_threshold)
8244 : {
8245 1688375 : bool want_align = type_has_new_extended_alignment (type);
8246 1688375 : if (di_elt.aligned != di_fn.aligned)
8247 : {
8248 581299 : if (want_align == di_elt.aligned)
8249 : {
8250 544272 : fn = elt;
8251 544272 : di_fn = di_elt;
8252 : }
8253 581299 : continue;
8254 : }
8255 : }
8256 :
8257 : /* -- If the deallocation functions have class scope, the one
8258 : without a parameter of type std::size_t is selected. */
8259 1107343 : bool want_size;
8260 1107343 : if (DECL_CLASS_SCOPE_P (fn) && !coro_p)
8261 : want_size = false;
8262 :
8263 : /* -- If the type is complete and if, for the second alternative
8264 : (delete array) only, the operand is a pointer to a class type
8265 : with a non-trivial destructor or a (possibly multi-dimensional)
8266 : array thereof, the function with a parameter of type std::size_t
8267 : is selected.
8268 :
8269 : -- Otherwise, it is unspecified whether a deallocation function
8270 : with a parameter of type std::size_t is selected. */
8271 : else
8272 : {
8273 1107235 : want_size = COMPLETE_TYPE_P (type);
8274 1107235 : if (code == VEC_DELETE_EXPR
8275 1107235 : && !TYPE_VEC_NEW_USES_COOKIE (type))
8276 : /* We need a cookie to determine the array size. */
8277 : want_size = false;
8278 : }
8279 1107343 : gcc_assert (di_fn.sized != di_elt.sized);
8280 1107343 : if (want_size == di_elt.sized)
8281 : {
8282 45051 : fn = elt;
8283 45051 : di_fn = di_elt;
8284 : }
8285 : }
8286 : }
8287 :
8288 : /* If we have a matching function, call it. */
8289 1233729 : if (fn)
8290 : {
8291 1233704 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8292 :
8293 : /* If the FN is a member function, make sure that it is
8294 : accessible. */
8295 1233704 : if (BASELINK_P (fns))
8296 1005 : perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn,
8297 : complain);
8298 :
8299 : /* Core issue 901: It's ok to new a type with deleted delete. */
8300 1233704 : if (DECL_DELETED_FN (fn) && alloc_fn)
8301 : return NULL_TREE;
8302 :
8303 1233698 : tree ret;
8304 1233698 : if (placement)
8305 : {
8306 : /* The placement args might not be suitable for overload
8307 : resolution at this point, so build the call directly. */
8308 664043 : int nargs = call_expr_nargs (placement);
8309 664043 : tree *argarray = XALLOCAVEC (tree, nargs);
8310 664043 : int i;
8311 664043 : argarray[0] = addr;
8312 1328138 : for (i = 1; i < nargs; i++)
8313 664095 : argarray[i] = CALL_EXPR_ARG (placement, i);
8314 664043 : if (!mark_used (fn, complain) && !(complain & tf_error))
8315 0 : return error_mark_node;
8316 664043 : ret = build_cxx_call (fn, nargs, argarray, complain);
8317 : }
8318 : else
8319 : {
8320 569655 : tree destroying = di_fn.destroying;
8321 569655 : if (destroying)
8322 : {
8323 : /* Strip const and volatile from addr but retain the type of the
8324 : object. */
8325 28 : tree rtype = TREE_TYPE (TREE_TYPE (oaddr));
8326 28 : rtype = cv_unqualified (rtype);
8327 28 : rtype = TYPE_POINTER_TO (rtype);
8328 28 : addr = cp_convert (rtype, oaddr, complain);
8329 28 : destroying = build_functional_cast (input_location,
8330 : destroying, NULL_TREE,
8331 : complain);
8332 : }
8333 :
8334 569655 : releasing_vec args;
8335 569655 : args->quick_push (addr);
8336 569655 : if (destroying)
8337 28 : args->quick_push (destroying);
8338 569655 : if (di_fn.sized)
8339 531569 : args->quick_push (size);
8340 569655 : if (di_fn.aligned)
8341 : {
8342 18517 : tree al = build_int_cst (align_type_node, TYPE_ALIGN_UNIT (type));
8343 18517 : args->quick_push (al);
8344 : }
8345 569655 : ret = cp_build_function_call_vec (fn, &args, complain);
8346 569655 : }
8347 :
8348 : /* Set this flag for all callers of this function. In addition to
8349 : delete-expressions, this is called for deallocating coroutine state;
8350 : treat that as an implicit delete-expression. This is also called for
8351 : the delete if the constructor throws in a new-expression, and for a
8352 : deleting destructor (which implements a delete-expression). */
8353 : /* But leave this flag off for destroying delete to avoid wrong
8354 : assumptions in the optimizers. */
8355 1233698 : tree call = extract_call_expr (ret);
8356 1233698 : if (TREE_CODE (call) == CALL_EXPR && !destroying_delete_p (fn))
8357 1233664 : CALL_FROM_NEW_OR_DELETE_P (call) = 1;
8358 :
8359 1233698 : return ret;
8360 : }
8361 :
8362 : /* If there's only a destroying delete that we can't use because the
8363 : object isn't constructed yet, and we used global new, use global
8364 : delete as well. */
8365 25 : if (excluded_destroying
8366 25 : && DECL_NAMESPACE_SCOPE_P (alloc_fn))
8367 7 : return build_op_delete_call (code, addr, size, true, placement,
8368 7 : alloc_fn, complain);
8369 :
8370 : /* [expr.new]
8371 :
8372 : If no unambiguous matching deallocation function can be found,
8373 : propagating the exception does not cause the object's memory to
8374 : be freed. */
8375 18 : if (alloc_fn)
8376 : {
8377 15 : if ((complain & tf_warning) && !placement)
8378 : {
8379 15 : auto_diagnostic_group d;
8380 15 : bool w = warning (0,
8381 : "no corresponding deallocation function for %qD",
8382 : alloc_fn);
8383 15 : if (w && excluded_destroying)
8384 3 : inform (DECL_SOURCE_LOCATION (excluded_destroying), "destroying "
8385 : "delete %qD cannot be used to release the allocated memory"
8386 : " if the initialization throws because the object is not "
8387 : "constructed yet", excluded_destroying);
8388 15 : }
8389 15 : return NULL_TREE;
8390 : }
8391 :
8392 3 : if (complain & tf_error)
8393 3 : error ("no suitable %<operator %s%> for %qT",
8394 3 : OVL_OP_INFO (false, code)->name, type);
8395 3 : return error_mark_node;
8396 : }
8397 :
8398 : /* Arguments as per build_op_delete_call_1 (). */
8399 :
8400 : tree
8401 1231355 : build_op_delete_call (enum tree_code code, tree addr, tree size, bool global_p,
8402 : tree placement, tree alloc_fn, tsubst_flags_t complain)
8403 : {
8404 1231355 : return build_op_delete_call_1 (code, addr, size, global_p, /*coro_p*/false,
8405 1231355 : placement, alloc_fn, complain);
8406 : }
8407 :
8408 : /* Arguments as per build_op_delete_call_1 (). */
8409 :
8410 : tree
8411 3261 : build_coroutine_op_delete_call (enum tree_code code, tree addr, tree size,
8412 : bool global_p, tree placement, tree alloc_fn,
8413 : tsubst_flags_t complain)
8414 : {
8415 3261 : return build_op_delete_call_1 (code, addr, size, global_p, /*coro_p*/true,
8416 3261 : placement, alloc_fn, complain);
8417 : }
8418 :
8419 : /* Issue diagnostics about a disallowed access of DECL, using DIAG_DECL
8420 : in the diagnostics.
8421 :
8422 : If ISSUE_ERROR is true, then issue an error about the access, followed
8423 : by a note showing the declaration. Otherwise, just show the note.
8424 :
8425 : DIAG_DECL and DIAG_LOCATION will almost always be the same.
8426 : DIAG_LOCATION is just another DECL. NO_ACCESS_REASON is an optional
8427 : parameter used to specify why DECL wasn't accessible (e.g. ak_private
8428 : would be because DECL was private). If not using NO_ACCESS_REASON,
8429 : then it must be ak_none, and the access failure reason will be
8430 : figured out by looking at the protection of DECL. */
8431 :
8432 : void
8433 1181 : complain_about_access (tree decl, tree diag_decl, tree diag_location,
8434 : bool issue_error, access_kind no_access_reason)
8435 : {
8436 : /* If we have not already figured out why DECL is inaccessible... */
8437 1181 : if (no_access_reason == ak_none)
8438 : {
8439 : /* Examine the access of DECL to find out why. */
8440 999 : if (TREE_PRIVATE (decl))
8441 : no_access_reason = ak_private;
8442 225 : else if (TREE_PROTECTED (decl))
8443 180 : no_access_reason = ak_protected;
8444 : }
8445 :
8446 : /* Now generate an error message depending on calculated access. */
8447 1181 : auto_diagnostic_group d;
8448 1181 : if (no_access_reason == ak_private)
8449 : {
8450 956 : if (issue_error)
8451 942 : error ("%q#D is private within this context", diag_decl);
8452 956 : inform (DECL_SOURCE_LOCATION (diag_location), "declared private here");
8453 : }
8454 225 : else if (no_access_reason == ak_protected)
8455 : {
8456 180 : if (issue_error)
8457 166 : error ("%q#D is protected within this context", diag_decl);
8458 180 : inform (DECL_SOURCE_LOCATION (diag_location), "declared protected here");
8459 : }
8460 : /* Couldn't figure out why DECL is inaccessible, so just say it's
8461 : inaccessible. */
8462 : else
8463 : {
8464 45 : if (issue_error)
8465 45 : error ("%q#D is inaccessible within this context", diag_decl);
8466 45 : inform (DECL_SOURCE_LOCATION (diag_decl), "declared here");
8467 : }
8468 1181 : }
8469 :
8470 : /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
8471 : bitwise or of LOOKUP_* values. If any errors are warnings are
8472 : generated, set *DIAGNOSTIC_FN to "error" or "warning",
8473 : respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
8474 : to NULL. */
8475 :
8476 : static tree
8477 14032824 : build_temp (tree expr, tree type, int flags,
8478 : enum diagnostics::kind *diagnostic_kind, tsubst_flags_t complain)
8479 : {
8480 14032824 : int savew, savee;
8481 :
8482 14032824 : *diagnostic_kind = diagnostics::kind::unspecified;
8483 :
8484 : /* If the source is a packed field, calling the copy constructor will require
8485 : binding the field to the reference parameter to the copy constructor, and
8486 : we'll end up with an infinite loop. If we can use a bitwise copy, then
8487 : do that now. */
8488 14032824 : if ((lvalue_kind (expr) & clk_packed)
8489 6 : && CLASS_TYPE_P (TREE_TYPE (expr))
8490 14032830 : && !type_has_nontrivial_copy_init (TREE_TYPE (expr)))
8491 3 : return get_target_expr (expr, complain);
8492 :
8493 : /* In decltype, we might have decided not to wrap this call in a TARGET_EXPR.
8494 : But it turns out to be a subexpression, so perform temporary
8495 : materialization now. */
8496 14032821 : if (TREE_CODE (expr) == CALL_EXPR
8497 6 : && CLASS_TYPE_P (type)
8498 14032827 : && same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
8499 3 : expr = build_cplus_new (type, expr, complain);
8500 :
8501 14032821 : savew = warningcount + werrorcount, savee = errorcount;
8502 14032821 : releasing_vec args (make_tree_vector_single (expr));
8503 14032821 : expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
8504 : &args, type, flags, complain);
8505 14032821 : if (warningcount + werrorcount > savew)
8506 0 : *diagnostic_kind = diagnostics::kind::warning;
8507 14032821 : else if (errorcount > savee)
8508 78 : *diagnostic_kind = diagnostics::kind::error;
8509 14032821 : return expr;
8510 14032821 : }
8511 :
8512 : /* Get any location for EXPR, falling back to input_location.
8513 :
8514 : If the result is in a system header and is the virtual location for
8515 : a token coming from the expansion of a macro, unwind it to the
8516 : location of the expansion point of the macro (e.g. to avoid the
8517 : diagnostic being suppressed for expansions of NULL where "NULL" is
8518 : in a system header). */
8519 :
8520 : static location_t
8521 2386862 : get_location_for_expr_unwinding_for_system_header (tree expr)
8522 : {
8523 2386862 : location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
8524 2386862 : loc = expansion_point_location_if_in_system_header (loc);
8525 2386862 : return loc;
8526 : }
8527 :
8528 : /* Perform warnings about peculiar, but valid, conversions from/to NULL.
8529 : Also handle a subset of zero as null warnings.
8530 : EXPR is implicitly converted to type TOTYPE.
8531 : FN and ARGNUM are used for diagnostics. */
8532 :
8533 : static void
8534 537875918 : conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
8535 : {
8536 : /* Issue warnings about peculiar, but valid, uses of NULL. */
8537 537875918 : if (TREE_CODE (totype) != BOOLEAN_TYPE
8538 325146268 : && ARITHMETIC_TYPE_P (totype)
8539 722570159 : && null_node_p (expr))
8540 : {
8541 94 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8542 94 : if (fn)
8543 : {
8544 33 : auto_diagnostic_group d;
8545 33 : if (warning_at (loc, OPT_Wconversion_null,
8546 : "passing NULL to non-pointer argument %P of %qD",
8547 : argnum, fn))
8548 27 : inform (get_fndecl_argument_location (fn, argnum),
8549 : "declared here");
8550 33 : }
8551 : else
8552 61 : warning_at (loc, OPT_Wconversion_null,
8553 : "converting to non-pointer type %qT from NULL", totype);
8554 : }
8555 :
8556 : /* Issue warnings if "false" is converted to a NULL pointer */
8557 537875824 : else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
8558 537875824 : && TYPE_PTR_P (totype))
8559 : {
8560 7 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8561 7 : if (fn)
8562 : {
8563 3 : auto_diagnostic_group d;
8564 3 : if (warning_at (loc, OPT_Wconversion_null,
8565 : "converting %<false%> to pointer type for argument "
8566 : "%P of %qD", argnum, fn))
8567 3 : inform (get_fndecl_argument_location (fn, argnum),
8568 : "declared here");
8569 3 : }
8570 : else
8571 4 : warning_at (loc, OPT_Wconversion_null,
8572 : "converting %<false%> to pointer type %qT", totype);
8573 : }
8574 : /* Handle zero as null pointer warnings for cases other
8575 : than EQ_EXPR and NE_EXPR */
8576 495578022 : else if ((TYPE_PTR_OR_PTRMEM_P (totype) || NULLPTR_TYPE_P (totype))
8577 538189042 : && null_ptr_cst_p (expr))
8578 : {
8579 2386761 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8580 2386761 : maybe_warn_zero_as_null_pointer_constant (expr, loc);
8581 : }
8582 537875918 : }
8583 :
8584 : /* We gave a diagnostic during a conversion. If this was in the second
8585 : standard conversion sequence of a user-defined conversion sequence, say
8586 : which user-defined conversion. */
8587 :
8588 : static void
8589 5252 : maybe_print_user_conv_context (conversion *convs)
8590 : {
8591 5252 : if (convs->user_conv_p)
8592 167 : for (conversion *t = convs; t; t = next_conversion (t))
8593 142 : if (t->kind == ck_user)
8594 : {
8595 43 : print_z_candidate (0, N_(" after user-defined conversion:"),
8596 : t->cand);
8597 43 : break;
8598 : }
8599 5252 : }
8600 :
8601 : /* Locate the parameter with the given index within FNDECL.
8602 : ARGNUM is zero based, -1 indicates the `this' argument of a method.
8603 : Return the location of the FNDECL itself if there are problems. */
8604 :
8605 : location_t
8606 8955669 : get_fndecl_argument_location (tree fndecl, int argnum)
8607 : {
8608 : /* The locations of implicitly-declared functions are likely to be
8609 : more meaningful than those of their parameters. */
8610 8955669 : if (DECL_ARTIFICIAL (fndecl))
8611 297 : return DECL_SOURCE_LOCATION (fndecl);
8612 :
8613 8955372 : if (argnum == -1)
8614 51 : return DECL_SOURCE_LOCATION (fndecl);
8615 :
8616 8955321 : int i;
8617 8955321 : tree param;
8618 :
8619 : /* Locate param by index within DECL_ARGUMENTS (fndecl). */
8620 8955321 : for (i = 0, param = FUNCTION_FIRST_USER_PARM (fndecl);
8621 22498914 : i < argnum && param;
8622 13543593 : i++, param = TREE_CHAIN (param))
8623 : ;
8624 :
8625 : /* If something went wrong (e.g. if we have a builtin and thus no arguments),
8626 : return the location of FNDECL. */
8627 8955321 : if (param == NULL)
8628 38 : return DECL_SOURCE_LOCATION (fndecl);
8629 :
8630 8955283 : return DECL_SOURCE_LOCATION (param);
8631 : }
8632 :
8633 : /* If FNDECL is non-NULL, issue a note highlighting ARGNUM
8634 : within its declaration (or the fndecl itself if something went
8635 : wrong). */
8636 :
8637 : void
8638 7003 : maybe_inform_about_fndecl_for_bogus_argument_init (tree fn, int argnum,
8639 : const char *highlight_color)
8640 : {
8641 7003 : if (fn)
8642 : {
8643 1123 : gcc_rich_location richloc (get_fndecl_argument_location (fn, argnum));
8644 1123 : richloc.set_highlight_color (highlight_color);
8645 1123 : inform (&richloc,
8646 : "initializing argument %P of %qD", argnum, fn);
8647 1123 : }
8648 7003 : }
8649 :
8650 : /* Maybe warn about C++20 Conversions to arrays of unknown bound. C is
8651 : the conversion, EXPR is the expression we're converting. */
8652 :
8653 : static void
8654 38663108 : maybe_warn_array_conv (location_t loc, conversion *c, tree expr)
8655 : {
8656 38663108 : if (cxx_dialect >= cxx20)
8657 : return;
8658 :
8659 426801 : tree type = TREE_TYPE (expr);
8660 426801 : type = strip_pointer_operator (type);
8661 :
8662 426801 : if (TREE_CODE (type) != ARRAY_TYPE
8663 426801 : || TYPE_DOMAIN (type) == NULL_TREE)
8664 : return;
8665 :
8666 1858 : if (pedantic && conv_binds_to_array_of_unknown_bound (c))
8667 10 : pedwarn (loc, OPT_Wc__20_extensions,
8668 : "conversions to arrays of unknown bound "
8669 : "are only available with %<-std=c++20%> or %<-std=gnu++20%>");
8670 : }
8671 :
8672 : /* We call this recursively in convert_like_internal. */
8673 : static tree convert_like (conversion *, tree, tree, int, bool, bool, bool,
8674 : tsubst_flags_t);
8675 :
8676 : /* Adjust the result EXPR of a conversion to the expected type TOTYPE, which
8677 : must be equivalent but might be a typedef. */
8678 :
8679 : static tree
8680 901208240 : maybe_adjust_type_name (tree type, tree expr, conversion_kind kind)
8681 : {
8682 901208240 : if (expr == error_mark_node
8683 901208189 : || processing_template_decl)
8684 : return expr;
8685 :
8686 790635369 : tree etype = TREE_TYPE (expr);
8687 790635369 : if (etype == type)
8688 : return expr;
8689 :
8690 105071802 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p (etype, type)
8691 : || is_bitfield_expr_with_lowered_type (expr)
8692 : || seen_error ());
8693 :
8694 102366926 : if (SCALAR_TYPE_P (type)
8695 201663429 : && (kind == ck_rvalue
8696 : /* ??? We should be able to do this for ck_identity of more prvalue
8697 : expressions, but checking !obvalue_p here breaks, so for now let's
8698 : just handle NON_LVALUE_EXPR (such as the location wrapper for a
8699 : literal). Maybe we want to express already-rvalue in the
8700 : conversion somehow? */
8701 89106647 : || TREE_CODE (expr) == NON_LVALUE_EXPR))
8702 11700721 : expr = build_nop (type, expr);
8703 :
8704 : return expr;
8705 : }
8706 :
8707 : /* Perform the conversions in CONVS on the expression EXPR. FN and
8708 : ARGNUM are used for diagnostics. ARGNUM is zero based, -1
8709 : indicates the `this' argument of a method. INNER is nonzero when
8710 : being called to continue a conversion chain. It is negative when a
8711 : reference binding will be applied, positive otherwise. If
8712 : ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
8713 : conversions will be emitted if appropriate. If C_CAST_P is true,
8714 : this conversion is coming from a C-style cast; in that case,
8715 : conversions to inaccessible bases are permitted. */
8716 :
8717 : static tree
8718 1100384130 : convert_like_internal (conversion *convs, tree expr, tree fn, int argnum,
8719 : bool issue_conversion_warnings, bool c_cast_p,
8720 : bool nested_p, tsubst_flags_t complain)
8721 : {
8722 1100384130 : tree totype = convs->type;
8723 1100384130 : enum diagnostics::kind diag_kind;
8724 1100384130 : int flags;
8725 1100384130 : location_t loc = cp_expr_loc_or_input_loc (expr);
8726 1100384130 : const bool stub_object_p = is_stub_object (expr);
8727 :
8728 1100384130 : if (convs->bad_p && !(complain & tf_error))
8729 46221 : return error_mark_node;
8730 :
8731 1100337909 : gcc_checking_assert (!TYPE_REF_P (TREE_TYPE (expr)));
8732 :
8733 1100337909 : if (convs->bad_p
8734 6974 : && convs->kind != ck_user
8735 6879 : && convs->kind != ck_list
8736 6873 : && convs->kind != ck_ambig
8737 6873 : && (convs->kind != ck_ref_bind
8738 5259 : || (convs->user_conv_p && next_conversion (convs)->bad_p))
8739 1635 : && (convs->kind != ck_rvalue
8740 15 : || SCALAR_TYPE_P (totype))
8741 1100339535 : && convs->kind != ck_base)
8742 : {
8743 1626 : int complained = 0;
8744 1626 : conversion *t = convs;
8745 1626 : auto_diagnostic_group d;
8746 :
8747 : /* Give a helpful error if this is bad because of excess braces. */
8748 46 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8749 46 : && SCALAR_TYPE_P (totype)
8750 34 : && CONSTRUCTOR_NELTS (expr) > 0
8751 1660 : && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
8752 : {
8753 11 : complained = permerror (loc, "too many braces around initializer "
8754 : "for %qT", totype);
8755 33 : while (BRACE_ENCLOSED_INITIALIZER_P (expr)
8756 55 : && CONSTRUCTOR_NELTS (expr) == 1)
8757 22 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
8758 : }
8759 :
8760 : /* Give a helpful error if this is bad because a conversion to bool
8761 : from std::nullptr_t requires direct-initialization. */
8762 1626 : if (NULLPTR_TYPE_P (TREE_TYPE (expr))
8763 1626 : && TREE_CODE (totype) == BOOLEAN_TYPE)
8764 15 : complained = permerror (loc, "converting to %qH from %qI requires "
8765 : "direct-initialization",
8766 15 : totype, TREE_TYPE (expr));
8767 :
8768 1626 : if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (expr))
8769 105 : && SCALAR_FLOAT_TYPE_P (totype)
8770 1731 : && (extended_float_type_p (TREE_TYPE (expr))
8771 27 : || extended_float_type_p (totype)))
8772 105 : switch (cp_compare_floating_point_conversion_ranks (TREE_TYPE (expr),
8773 : totype))
8774 : {
8775 102 : case 2:
8776 102 : if (pedwarn (loc, OPT_Wnarrowing, "ISO C++ does not allow "
8777 : "converting to %qH from %qI with greater "
8778 102 : "conversion rank", totype, TREE_TYPE (expr)))
8779 1626 : complained = 1;
8780 21 : else if (!complained)
8781 21 : complained = -1;
8782 : break;
8783 3 : case 3:
8784 3 : if (pedwarn (loc, OPT_Wnarrowing, "ISO C++ does not allow "
8785 : "converting to %qH from %qI with unordered "
8786 3 : "conversion rank", totype, TREE_TYPE (expr)))
8787 : complained = 1;
8788 0 : else if (!complained)
8789 21 : complained = -1;
8790 : break;
8791 : default:
8792 : break;
8793 : }
8794 :
8795 3268 : for (; t ; t = next_conversion (t))
8796 : {
8797 3259 : if (t->kind == ck_user && t->cand->reason)
8798 : {
8799 52 : auto_diagnostic_group d;
8800 52 : complained = permerror (loc, "invalid user-defined conversion "
8801 52 : "from %qH to %qI", TREE_TYPE (expr),
8802 : totype);
8803 52 : if (complained)
8804 : {
8805 52 : auto_diagnostic_nesting_level sentinel;
8806 52 : print_z_candidate (loc, N_("candidate is:"), t->cand);
8807 52 : }
8808 52 : expr = convert_like (t, expr, fn, argnum,
8809 : /*issue_conversion_warnings=*/false,
8810 : /*c_cast_p=*/false, /*nested_p=*/true,
8811 : complain);
8812 52 : break;
8813 52 : }
8814 3207 : else if (t->kind == ck_user || !t->bad_p)
8815 : {
8816 1565 : expr = convert_like (t, expr, fn, argnum,
8817 : /*issue_conversion_warnings=*/false,
8818 : /*c_cast_p=*/false, /*nested_p=*/true,
8819 : complain);
8820 1565 : if (t->bad_p)
8821 0 : complained = 1;
8822 : break;
8823 : }
8824 1642 : else if (t->kind == ck_ambig)
8825 0 : return convert_like (t, expr, fn, argnum,
8826 : /*issue_conversion_warnings=*/false,
8827 : /*c_cast_p=*/false, /*nested_p=*/true,
8828 0 : complain);
8829 1642 : else if (t->kind == ck_identity)
8830 : break;
8831 : }
8832 1626 : if (!complained && stub_object_p)
8833 : {
8834 : /* An error diagnosed within a trait, don't give extra labels. */
8835 12 : error_at (loc, "invalid conversion from %qH to %qI",
8836 6 : TREE_TYPE (expr), totype);
8837 6 : complained = 1;
8838 : }
8839 1620 : else if (!complained && expr != error_mark_node)
8840 : {
8841 1435 : range_label_for_type_mismatch label (TREE_TYPE (expr), totype);
8842 1435 : gcc_rich_location richloc (loc, &label, highlight_colors::percent_h);
8843 1435 : complained = permerror (&richloc,
8844 : "invalid conversion from %qH to %qI",
8845 1435 : TREE_TYPE (expr), totype);
8846 1435 : if (complained)
8847 1380 : maybe_emit_indirection_note (loc, expr, totype);
8848 1435 : }
8849 1626 : if (convs->kind == ck_ref_bind)
8850 21 : expr = convert_to_reference (totype, expr, CONV_IMPLICIT,
8851 : LOOKUP_NORMAL, NULL_TREE,
8852 : complain);
8853 : else
8854 1605 : expr = cp_convert (totype, expr, complain);
8855 1626 : if (complained == 1)
8856 1548 : maybe_inform_about_fndecl_for_bogus_argument_init
8857 1548 : (fn, argnum, highlight_colors::percent_i);
8858 1626 : return expr;
8859 1626 : }
8860 :
8861 1100336283 : if (issue_conversion_warnings && (complain & tf_warning))
8862 537875918 : conversion_null_warnings (totype, expr, fn, argnum);
8863 :
8864 1100336283 : switch (convs->kind)
8865 : {
8866 6566825 : case ck_user:
8867 6566825 : {
8868 6566825 : struct z_candidate *cand = convs->cand;
8869 :
8870 6566825 : if (cand == NULL)
8871 : /* We chose the surrogate function from add_conv_candidate, now we
8872 : actually need to build the conversion. */
8873 56 : cand = build_user_type_conversion_1 (totype, expr,
8874 : LOOKUP_NO_CONVERSION, complain);
8875 :
8876 6566825 : tree convfn = cand->fn;
8877 :
8878 : /* When converting from an init list we consider explicit
8879 : constructors, but actually trying to call one is an error. */
8880 7049730 : if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
8881 9996 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
8882 : /* Unless this is for direct-list-initialization. */
8883 9993 : && (!CONSTRUCTOR_IS_DIRECT_INIT (expr) || convs->need_temporary_p)
8884 : /* And in C++98 a default constructor can't be explicit. */
8885 6567065 : && cxx_dialect >= cxx11)
8886 : {
8887 238 : if (!(complain & tf_error))
8888 54 : return error_mark_node;
8889 184 : location_t loc = location_of (expr);
8890 184 : if (CONSTRUCTOR_NELTS (expr) == 0
8891 184 : && FUNCTION_FIRST_USER_PARMTYPE (convfn) != void_list_node)
8892 : {
8893 9 : auto_diagnostic_group d;
8894 9 : if (pedwarn (loc, 0, "converting to %qT from initializer list "
8895 : "would use explicit constructor %qD",
8896 : totype, convfn))
8897 : {
8898 9 : inform (DECL_SOURCE_LOCATION (convfn), "%qD declared here",
8899 : convfn);
8900 9 : inform (loc, "in C++11 and above a default constructor "
8901 : "can be explicit");
8902 : }
8903 9 : }
8904 : else
8905 : {
8906 175 : auto_diagnostic_group d;
8907 175 : error ("converting to %qT from initializer list would use "
8908 : "explicit constructor %qD", totype, convfn);
8909 175 : inform (DECL_SOURCE_LOCATION (convfn), "%qD declared here",
8910 : convfn);
8911 175 : }
8912 : }
8913 :
8914 : /* If we're initializing from {}, it's value-initialization. */
8915 865602 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8916 865580 : && CONSTRUCTOR_NELTS (expr) == 0
8917 148847 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype)
8918 6715591 : && !processing_template_decl)
8919 : {
8920 148820 : if (abstract_virtuals_error (NULL_TREE, totype, complain))
8921 12 : return error_mark_node;
8922 148808 : expr = build_value_init (totype, complain);
8923 148808 : expr = get_target_expr (expr, complain);
8924 148808 : if (expr != error_mark_node)
8925 148668 : TARGET_EXPR_LIST_INIT_P (expr) = true;
8926 148808 : return expr;
8927 : }
8928 :
8929 : /* We don't know here whether EXPR is being used as an lvalue or
8930 : rvalue, but we know it's read. */
8931 6417951 : mark_exp_read (expr);
8932 :
8933 : /* Give the conversion call the location of EXPR rather than the
8934 : location of the context that caused the conversion. */
8935 6417951 : iloc_sentinel ils (loc);
8936 :
8937 : /* Pass LOOKUP_NO_CONVERSION so rvalue/base handling knows not to allow
8938 : any more UDCs. */
8939 6417951 : expr = build_over_call (cand, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
8940 : complain);
8941 :
8942 : /* If this is a constructor or a function returning an aggr type,
8943 : we need to build up a TARGET_EXPR. */
8944 12835902 : if (DECL_CONSTRUCTOR_P (convfn))
8945 : {
8946 2867223 : expr = build_cplus_new (totype, expr, complain);
8947 :
8948 : /* Remember that this was list-initialization. */
8949 2867223 : if (convs->check_narrowing && expr != error_mark_node)
8950 741896 : TARGET_EXPR_LIST_INIT_P (expr) = true;
8951 : }
8952 :
8953 6417951 : return expr;
8954 6417951 : }
8955 773980170 : case ck_identity:
8956 773980170 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
8957 : {
8958 1137486 : int nelts = CONSTRUCTOR_NELTS (expr);
8959 176454 : if (nelts == 0)
8960 961032 : expr = build_value_init (totype, complain);
8961 176454 : else if (nelts == 1)
8962 176454 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
8963 : else
8964 0 : gcc_unreachable ();
8965 : }
8966 773980170 : expr = mark_use (expr, /*rvalue_p=*/!convs->rvaluedness_matches_p,
8967 : /*read_p=*/true, UNKNOWN_LOCATION,
8968 : /*reject_builtin=*/true);
8969 :
8970 773980170 : if (type_unknown_p (expr))
8971 18783 : expr = instantiate_type (totype, expr, complain);
8972 773980170 : if (!nested_p && TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8973 72614 : expr = cp_convert (totype, TREE_OPERAND (expr, 0), complain);
8974 773980170 : if (expr == null_node
8975 773980170 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype))
8976 : /* If __null has been converted to an integer type, we do not want to
8977 : continue to warn about uses of EXPR as an integer, rather than as a
8978 : pointer. */
8979 150426 : expr = build_int_cst (totype, 0);
8980 773980170 : return maybe_adjust_type_name (totype, expr, convs->kind);
8981 53 : case ck_ambig:
8982 : /* We leave bad_p off ck_ambig because overload resolution considers
8983 : it valid, it just fails when we try to perform it. So we need to
8984 : check complain here, too. */
8985 53 : if (complain & tf_error)
8986 : {
8987 : /* Call build_user_type_conversion again for the error. */
8988 53 : auto_diagnostic_group d;
8989 3 : int flags = (convs->need_temporary_p
8990 53 : ? LOOKUP_IMPLICIT : LOOKUP_NORMAL);
8991 53 : build_user_type_conversion (totype, convs->u.expr, flags, complain);
8992 53 : gcc_assert (seen_error ());
8993 53 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
8994 53 : }
8995 53 : return error_mark_node;
8996 :
8997 17473 : case ck_list:
8998 17473 : {
8999 : /* Conversion to std::initializer_list<T>. */
9000 17473 : tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
9001 17473 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (expr);
9002 17473 : tree array;
9003 :
9004 17473 : if (tree init = maybe_init_list_as_array (elttype, expr))
9005 : {
9006 132 : elttype
9007 132 : = cp_build_qualified_type (elttype, (cp_type_quals (elttype)
9008 : | TYPE_QUAL_CONST));
9009 132 : tree index_type = TYPE_DOMAIN (TREE_TYPE (init));
9010 132 : array = build_cplus_array_type (elttype, index_type);
9011 132 : len = TREE_INT_CST_LOW (TYPE_MAX_VALUE (index_type)) + 1;
9012 132 : array = build_vec_init_expr (array, init, complain);
9013 132 : array = get_target_expr (array);
9014 132 : array = cp_build_addr_expr (array, complain);
9015 : }
9016 17341 : else if (len)
9017 : {
9018 16649 : tree val;
9019 16649 : unsigned ix;
9020 16649 : tree new_ctor = build_constructor (init_list_type_node, NULL);
9021 :
9022 : /* Convert all the elements. */
9023 127215 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
9024 : {
9025 110581 : if (TREE_CODE (val) == RAW_DATA_CST)
9026 : {
9027 : /* For conversion to initializer_list<unsigned char> or
9028 : initializer_list<char> or initializer_list<signed char>
9029 : we can optimize and keep RAW_DATA_CST with adjusted
9030 : type if we report narrowing errors if needed, for
9031 : others this converts each element separately. */
9032 48 : if (convs->u.list[ix]->kind == ck_std)
9033 : {
9034 18 : tree et = convs->u.list[ix]->type;
9035 18 : conversion *next = next_conversion (convs->u.list[ix]);
9036 18 : gcc_assert (et
9037 : && (TREE_CODE (et) == INTEGER_TYPE
9038 : || is_byte_access_type (et))
9039 : && TYPE_PRECISION (et) == CHAR_BIT
9040 : && next
9041 : && next->kind == ck_identity);
9042 18 : if (!TYPE_UNSIGNED (et)
9043 : /* For RAW_DATA_CST, TREE_TYPE (val) can be
9044 : either integer_type_node (when it has been
9045 : created by the lexer from CPP_EMBED) or
9046 : after digestion/conversion some integral
9047 : type with CHAR_BIT precision. For int with
9048 : precision higher than CHAR_BIT or unsigned char
9049 : diagnose narrowing conversions from
9050 : that int/unsigned char to signed char if any
9051 : byte has most significant bit set. */
9052 18 : && (TYPE_UNSIGNED (TREE_TYPE (val))
9053 12 : || (TYPE_PRECISION (TREE_TYPE (val))
9054 : > CHAR_BIT)))
9055 2460 : for (int i = 0; i < RAW_DATA_LENGTH (val); ++i)
9056 : {
9057 2454 : if (RAW_DATA_SCHAR_ELT (val, i) >= 0)
9058 2448 : continue;
9059 6 : else if (complain & tf_error)
9060 : {
9061 6 : location_t loc
9062 6 : = cp_expr_loc_or_input_loc (val);
9063 6 : int savederrorcount = errorcount;
9064 18 : permerror_opt (loc, OPT_Wnarrowing,
9065 : "narrowing conversion of "
9066 : "%qd from %qH to %qI",
9067 6 : RAW_DATA_UCHAR_ELT (val, i),
9068 6 : TREE_TYPE (val), et);
9069 6 : if (errorcount != savederrorcount)
9070 6 : return error_mark_node;
9071 : }
9072 : else
9073 0 : return error_mark_node;
9074 : }
9075 12 : tree sub = copy_node (val);
9076 12 : TREE_TYPE (sub) = et;
9077 12 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor),
9078 : NULL_TREE, sub);
9079 : }
9080 : else
9081 : {
9082 30 : conversion *conv = convs->u.list[ix];
9083 30 : gcc_assert (conv->kind == ck_list);
9084 15495 : for (int i = 0; i < RAW_DATA_LENGTH (val); ++i)
9085 : {
9086 15465 : tree elt
9087 15465 : = build_int_cst (TREE_TYPE (val),
9088 15465 : RAW_DATA_UCHAR_ELT (val, i));
9089 15465 : tree sub
9090 15465 : = convert_like (conv->u.list[i], elt,
9091 : fn, argnum, false, false,
9092 : /*nested_p=*/true, complain);
9093 15465 : if (sub == error_mark_node)
9094 : return sub;
9095 15465 : if (!check_narrowing (TREE_TYPE (sub), elt,
9096 : complain))
9097 0 : return error_mark_node;
9098 15465 : tree nc = new_ctor;
9099 15465 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (nc),
9100 : NULL_TREE, sub);
9101 15465 : if (!TREE_CONSTANT (sub))
9102 11679 : TREE_CONSTANT (new_ctor) = false;
9103 : }
9104 : }
9105 42 : len += RAW_DATA_LENGTH (val) - 1;
9106 42 : continue;
9107 42 : }
9108 110533 : tree sub = convert_like (convs->u.list[ix], val, fn,
9109 : argnum, false, false,
9110 : /*nested_p=*/true, complain);
9111 110533 : if (sub == error_mark_node)
9112 : return sub;
9113 1648 : if (!BRACE_ENCLOSED_INITIALIZER_P (val)
9114 110544 : && !check_narrowing (TREE_TYPE (sub), val, complain))
9115 0 : return error_mark_node;
9116 110524 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor),
9117 : NULL_TREE, sub);
9118 110524 : if (!TREE_CONSTANT (sub))
9119 11343 : TREE_CONSTANT (new_ctor) = false;
9120 : }
9121 : /* Build up the array. */
9122 16634 : elttype
9123 16634 : = cp_build_qualified_type (elttype, (cp_type_quals (elttype)
9124 : | TYPE_QUAL_CONST));
9125 16634 : array = build_array_of_n_type (elttype, len);
9126 16634 : array = finish_compound_literal (array, new_ctor, complain);
9127 : /* This is dubious now, should be blessed by P2752. */
9128 16634 : DECL_MERGEABLE (TARGET_EXPR_SLOT (array)) = true;
9129 16634 : array = cp_build_addr_expr (array, complain);
9130 : }
9131 : else
9132 692 : array = nullptr_node;
9133 :
9134 17458 : array = cp_convert (build_pointer_type (elttype), array, complain);
9135 17458 : if (array == error_mark_node)
9136 : return error_mark_node;
9137 :
9138 : /* Build up the initializer_list object. Note: fail gracefully
9139 : if the object cannot be completed because, for example, no
9140 : definition is provided (c++/80956). */
9141 17458 : totype = complete_type_or_maybe_complain (totype, NULL_TREE, complain);
9142 17458 : if (!totype)
9143 0 : return error_mark_node;
9144 17458 : tree field = next_aggregate_field (TYPE_FIELDS (totype));
9145 17458 : vec<constructor_elt, va_gc> *vec = NULL;
9146 17458 : CONSTRUCTOR_APPEND_ELT (vec, field, array);
9147 17458 : field = next_aggregate_field (DECL_CHAIN (field));
9148 17458 : CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
9149 17458 : tree new_ctor = build_constructor (totype, vec);
9150 17458 : return get_target_expr (new_ctor, complain);
9151 : }
9152 :
9153 1306340 : case ck_aggr:
9154 1306340 : if (TREE_CODE (totype) == COMPLEX_TYPE)
9155 : {
9156 28293 : tree real = CONSTRUCTOR_ELT (expr, 0)->value;
9157 28293 : tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
9158 28293 : real = perform_implicit_conversion (TREE_TYPE (totype),
9159 : real, complain);
9160 28293 : imag = perform_implicit_conversion (TREE_TYPE (totype),
9161 : imag, complain);
9162 28293 : expr = build2 (COMPLEX_EXPR, totype, real, imag);
9163 28293 : return expr;
9164 : }
9165 1278047 : expr = reshape_init (totype, expr, complain);
9166 1278047 : expr = get_target_expr (digest_init (totype, expr, complain),
9167 : complain);
9168 1278047 : if (expr != error_mark_node)
9169 1278036 : TARGET_EXPR_LIST_INIT_P (expr) = true;
9170 : return expr;
9171 :
9172 318465422 : default:
9173 318465422 : break;
9174 318465422 : };
9175 :
9176 318465422 : conversion *nc = next_conversion (convs);
9177 318465422 : if (convs->kind == ck_ref_bind && nc->kind == ck_qual
9178 8618 : && !convs->need_temporary_p)
9179 : /* direct_reference_binding might have inserted a ck_qual under
9180 : this ck_ref_bind for the benefit of conversion sequence ranking.
9181 : Don't actually perform that conversion. */
9182 6267 : nc = next_conversion (nc);
9183 :
9184 318465422 : expr = convert_like (nc, expr, fn, argnum,
9185 : convs->kind == ck_ref_bind
9186 318465422 : ? issue_conversion_warnings : false,
9187 : c_cast_p, /*nested_p=*/true, complain & ~tf_no_cleanup);
9188 318465422 : if (expr == error_mark_node)
9189 : return error_mark_node;
9190 :
9191 318464592 : switch (convs->kind)
9192 : {
9193 141043267 : case ck_rvalue:
9194 141043267 : expr = decay_conversion (expr, complain);
9195 141043267 : if (expr == error_mark_node)
9196 : {
9197 15 : if (complain & tf_error)
9198 : {
9199 12 : auto_diagnostic_group d;
9200 12 : maybe_print_user_conv_context (convs);
9201 12 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9202 12 : }
9203 15 : return error_mark_node;
9204 : }
9205 :
9206 141043252 : if ((complain & tf_warning) && fn
9207 40702775 : && warn_suggest_attribute_format)
9208 : {
9209 706 : tree rhstype = TREE_TYPE (expr);
9210 706 : const enum tree_code coder = TREE_CODE (rhstype);
9211 706 : const enum tree_code codel = TREE_CODE (totype);
9212 706 : if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
9213 250 : && coder == codel
9214 956 : && check_missing_format_attribute (totype, rhstype))
9215 6 : warning (OPT_Wsuggest_attribute_format,
9216 : "argument of function call might be a candidate "
9217 : "for a format attribute");
9218 : }
9219 :
9220 141043252 : if (! MAYBE_CLASS_TYPE_P (totype))
9221 127228070 : return maybe_adjust_type_name (totype, expr, convs->kind);
9222 :
9223 : /* Don't introduce copies when passing arguments along to the inherited
9224 : constructor. */
9225 13815182 : if (current_function_decl
9226 10920534 : && flag_new_inheriting_ctors
9227 35655770 : && DECL_INHERITED_CTOR (current_function_decl))
9228 : return expr;
9229 :
9230 13810500 : if (TREE_CODE (expr) == TARGET_EXPR
9231 13810500 : && TARGET_EXPR_LIST_INIT_P (expr))
9232 : /* Copy-list-initialization doesn't actually involve a copy. */
9233 : return expr;
9234 :
9235 : /* Fall through. */
9236 15827412 : case ck_base:
9237 15827412 : if (convs->kind == ck_base && !convs->need_temporary_p)
9238 : {
9239 : /* We are going to bind a reference directly to a base-class
9240 : subobject of EXPR. */
9241 : /* Build an expression for `*((base*) &expr)'. */
9242 1794588 : expr = convert_to_base (expr, totype,
9243 1794588 : !c_cast_p, /*nonnull=*/true, complain);
9244 1794588 : return expr;
9245 : }
9246 :
9247 : /* Copy-initialization where the cv-unqualified version of the source
9248 : type is the same class as, or a derived class of, the class of the
9249 : destination [is treated as direct-initialization]. [dcl.init] */
9250 14032824 : flags = LOOKUP_NORMAL;
9251 : /* This conversion is being done in the context of a user-defined
9252 : conversion (i.e. the second step of copy-initialization), so
9253 : don't allow any more. */
9254 14032824 : if (convs->user_conv_p)
9255 259301 : flags |= LOOKUP_NO_CONVERSION;
9256 : /* We might be performing a conversion of the argument
9257 : to the user-defined conversion, i.e., not a conversion of the
9258 : result of the user-defined conversion. In which case we skip
9259 : explicit constructors. */
9260 14032824 : if (convs->copy_init_p)
9261 13764468 : flags |= LOOKUP_ONLYCONVERTING;
9262 14032824 : expr = build_temp (expr, totype, flags, &diag_kind, complain);
9263 14032824 : if (diag_kind != diagnostics::kind::unspecified && complain)
9264 : {
9265 78 : auto_diagnostic_group d;
9266 78 : maybe_print_user_conv_context (convs);
9267 78 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9268 78 : }
9269 :
9270 14032824 : return build_cplus_new (totype, expr, complain);
9271 :
9272 62396583 : case ck_ref_bind:
9273 62396583 : {
9274 62396583 : tree ref_type = totype;
9275 :
9276 62396583 : if (convs->bad_p && !next_conversion (convs)->bad_p)
9277 : {
9278 5162 : tree extype = TREE_TYPE (expr);
9279 5162 : auto_diagnostic_group d;
9280 5162 : if (TYPE_REF_IS_RVALUE (ref_type)
9281 5162 : && lvalue_p (expr))
9282 1638 : error_at (loc, "cannot bind rvalue reference of type %qH to "
9283 : "lvalue of type %qI", totype, extype);
9284 6202 : else if (!TYPE_REF_IS_RVALUE (ref_type) && !lvalue_p (expr)
9285 5004 : && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
9286 : {
9287 1231 : conversion *next = next_conversion (convs);
9288 1231 : if (next->kind == ck_std)
9289 : {
9290 59 : next = next_conversion (next);
9291 59 : error_at (loc, "cannot bind non-const lvalue reference of "
9292 : "type %qH to a value of type %qI",
9293 : totype, next->type);
9294 : }
9295 1172 : else if (!CP_TYPE_CONST_P (TREE_TYPE (ref_type)))
9296 831 : error_at (loc, "cannot bind non-const lvalue reference of "
9297 : "type %qH to an rvalue of type %qI", totype, extype);
9298 : else // extype is volatile
9299 341 : error_at (loc, "cannot bind lvalue reference of type "
9300 : "%qH to an rvalue of type %qI", totype,
9301 : extype);
9302 : }
9303 2293 : else if (!reference_compatible_p (TREE_TYPE (totype), extype))
9304 : {
9305 : /* If we're converting from T[] to T[N], don't talk
9306 : about discarding qualifiers. (Converting from T[N] to
9307 : T[] is allowed by P0388R4.) */
9308 2293 : if (TREE_CODE (extype) == ARRAY_TYPE
9309 27 : && TYPE_DOMAIN (extype) == NULL_TREE
9310 15 : && TREE_CODE (TREE_TYPE (totype)) == ARRAY_TYPE
9311 2308 : && TYPE_DOMAIN (TREE_TYPE (totype)) != NULL_TREE)
9312 15 : error_at (loc, "cannot bind reference of type %qH to %qI "
9313 : "due to different array bounds", totype, extype);
9314 : else
9315 2278 : error_at (loc, "binding reference of type %qH to %qI "
9316 : "discards qualifiers", totype, extype);
9317 : }
9318 : else
9319 0 : gcc_unreachable ();
9320 5162 : maybe_print_user_conv_context (convs);
9321 5162 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9322 :
9323 5162 : return error_mark_node;
9324 5162 : }
9325 62391421 : else if (complain & tf_warning)
9326 37251953 : maybe_warn_array_conv (loc, convs, expr);
9327 :
9328 : /* If necessary, create a temporary.
9329 :
9330 : VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
9331 : that need temporaries, even when their types are reference
9332 : compatible with the type of reference being bound, so the
9333 : upcoming call to cp_build_addr_expr doesn't fail. */
9334 62391421 : if (convs->need_temporary_p
9335 59902440 : || TREE_CODE (expr) == CONSTRUCTOR
9336 59902234 : || TREE_CODE (expr) == VA_ARG_EXPR)
9337 : {
9338 : /* Otherwise, a temporary of type "cv1 T1" is created and
9339 : initialized from the initializer expression using the rules
9340 : for a non-reference copy-initialization (8.5). */
9341 :
9342 2489187 : tree type = TREE_TYPE (ref_type);
9343 2489187 : cp_lvalue_kind lvalue = lvalue_kind (expr);
9344 :
9345 2489187 : gcc_assert (similar_type_p (type, next_conversion (convs)->type));
9346 2489187 : if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
9347 3364614 : && !TYPE_REF_IS_RVALUE (ref_type))
9348 : {
9349 : /* If the reference is volatile or non-const, we
9350 : cannot create a temporary. */
9351 105 : if (complain & tf_error)
9352 : {
9353 103 : if (lvalue & clk_bitfield)
9354 30 : error_at (loc, "cannot bind bit-field %qE to %qT",
9355 : expr, ref_type);
9356 73 : else if (lvalue & clk_packed)
9357 9 : error_at (loc, "cannot bind packed field %qE to %qT",
9358 : expr, ref_type);
9359 : else
9360 64 : error_at (loc, "cannot bind rvalue %qE to %qT",
9361 : expr, ref_type);
9362 : }
9363 105 : return error_mark_node;
9364 : }
9365 : /* If the source is a packed field, and we must use a copy
9366 : constructor, then building the target expr will require
9367 : binding the field to the reference parameter to the
9368 : copy constructor, and we'll end up with an infinite
9369 : loop. If we can use a bitwise copy, then we'll be
9370 : OK. */
9371 2489082 : if ((lvalue & clk_packed)
9372 20 : && CLASS_TYPE_P (type)
9373 2489099 : && type_has_nontrivial_copy_init (type))
9374 : {
9375 6 : error_at (loc, "cannot bind packed field %qE to %qT",
9376 : expr, ref_type);
9377 6 : return error_mark_node;
9378 : }
9379 2489076 : if (lvalue & clk_bitfield)
9380 : {
9381 24 : expr = convert_bitfield_to_declared_type (expr);
9382 24 : expr = fold_convert (type, expr);
9383 : }
9384 :
9385 : /* Creating &TARGET_EXPR<> in a template would break when
9386 : tsubsting the expression, so use an IMPLICIT_CONV_EXPR
9387 : instead. This can happen even when there's no class
9388 : involved, e.g., when converting an integer to a reference
9389 : type. */
9390 2489076 : if (processing_template_decl)
9391 9570 : return build1 (IMPLICIT_CONV_EXPR, totype, expr);
9392 2479506 : expr = build_target_expr_with_type (expr, type, complain);
9393 : }
9394 :
9395 : /* Take the address of the thing to which we will bind the
9396 : reference. */
9397 62381740 : expr = cp_build_addr_expr (expr, complain);
9398 62381740 : if (expr == error_mark_node)
9399 : return error_mark_node;
9400 :
9401 : /* Convert it to a pointer to the type referred to by the
9402 : reference. This will adjust the pointer if a derived to
9403 : base conversion is being performed. */
9404 62381740 : expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
9405 : expr, complain);
9406 : /* Convert the pointer to the desired reference type. */
9407 62381740 : return build_nop (ref_type, expr);
9408 : }
9409 :
9410 2850635 : case ck_lvalue:
9411 2850635 : return decay_conversion (expr, complain);
9412 :
9413 235025 : case ck_fnptr:
9414 : /* ??? Should the address of a transaction-safe pointer point to the TM
9415 : clone, and this conversion look up the primary function? */
9416 235025 : return build_nop (totype, expr);
9417 :
9418 1604969 : case ck_qual:
9419 : /* Warn about deprecated conversion if appropriate. */
9420 1604969 : if (complain & tf_warning)
9421 : {
9422 1411155 : string_conv_p (totype, expr, 1);
9423 1411155 : maybe_warn_array_conv (loc, convs, expr);
9424 : }
9425 : break;
9426 :
9427 2982611 : case ck_ptr:
9428 2982611 : if (convs->base_p)
9429 214516 : expr = convert_to_base (expr, totype, !c_cast_p,
9430 : /*nonnull=*/false, complain);
9431 2982611 : return build_nop (totype, expr);
9432 :
9433 28552 : case ck_pmem:
9434 28552 : return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
9435 28552 : c_cast_p, complain);
9436 :
9437 : default:
9438 : break;
9439 : }
9440 :
9441 106884335 : if (convs->check_narrowing
9442 162504433 : && !check_narrowing (totype, expr, complain,
9443 55620098 : convs->check_narrowing_const_only))
9444 501 : return error_mark_node;
9445 :
9446 213767668 : warning_sentinel w (warn_zero_as_null_pointer_constant);
9447 106883834 : if (issue_conversion_warnings)
9448 84060019 : expr = cp_convert_and_check (totype, expr, complain);
9449 : else
9450 : {
9451 22823815 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
9452 39 : expr = TREE_OPERAND (expr, 0);
9453 22823815 : expr = cp_convert (totype, expr, complain);
9454 : }
9455 :
9456 106883834 : return expr;
9457 : }
9458 :
9459 : /* Return true if converting FROM to TO is unsafe in a template. */
9460 :
9461 : static bool
9462 1945831 : conv_unsafe_in_template_p (tree to, tree from)
9463 : {
9464 : /* Converting classes involves TARGET_EXPR. */
9465 1945831 : if (CLASS_TYPE_P (to) || CLASS_TYPE_P (from))
9466 : return true;
9467 :
9468 : /* Converting real to integer produces FIX_TRUNC_EXPR which tsubst
9469 : doesn't handle. */
9470 1536026 : if (SCALAR_FLOAT_TYPE_P (from) && INTEGRAL_OR_ENUMERATION_TYPE_P (to))
9471 : return true;
9472 :
9473 : /* Converting integer to real isn't a trivial conversion, either. */
9474 1536023 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (from) && SCALAR_FLOAT_TYPE_P (to))
9475 3 : return true;
9476 :
9477 : return false;
9478 : }
9479 :
9480 : /* Wrapper for convert_like_internal that handles creating
9481 : IMPLICIT_CONV_EXPR. */
9482 :
9483 : static tree
9484 1100793932 : convert_like (conversion *convs, tree expr, tree fn, int argnum,
9485 : bool issue_conversion_warnings, bool c_cast_p, bool nested_p,
9486 : tsubst_flags_t complain)
9487 : {
9488 : /* Creating &TARGET_EXPR<> in a template breaks when substituting,
9489 : and creating a CALL_EXPR in a template breaks in finish_call_expr
9490 : so use an IMPLICIT_CONV_EXPR for this conversion. We would have
9491 : created such codes e.g. when calling a user-defined conversion
9492 : function. */
9493 1100793932 : tree conv_expr = NULL_TREE;
9494 1100793932 : if (processing_template_decl
9495 111769302 : && convs->kind != ck_identity
9496 1102739763 : && conv_unsafe_in_template_p (convs->type, TREE_TYPE (expr)))
9497 : {
9498 409811 : conv_expr = build1 (IMPLICIT_CONV_EXPR, convs->type, expr);
9499 409811 : if (convs->kind != ck_ref_bind)
9500 28736 : conv_expr = convert_from_reference (conv_expr);
9501 409811 : if (!convs->bad_p)
9502 : return conv_expr;
9503 : /* Do the normal processing to give the bad_p errors. But we still
9504 : need to return the IMPLICIT_CONV_EXPR, unless we're returning
9505 : error_mark_node. */
9506 : }
9507 1100384130 : expr = convert_like_internal (convs, expr, fn, argnum,
9508 : issue_conversion_warnings, c_cast_p,
9509 : nested_p, complain);
9510 1100384130 : if (expr == error_mark_node)
9511 : return error_mark_node;
9512 1100329714 : return conv_expr ? conv_expr : expr;
9513 : }
9514 :
9515 : /* Convenience wrapper for convert_like. */
9516 :
9517 : static inline tree
9518 608167934 : convert_like (conversion *convs, tree expr, tsubst_flags_t complain)
9519 : {
9520 608167934 : return convert_like (convs, expr, NULL_TREE, 0,
9521 : /*issue_conversion_warnings=*/true,
9522 608167934 : /*c_cast_p=*/false, /*nested_p=*/false, complain);
9523 : }
9524 :
9525 : /* Convenience wrapper for convert_like. */
9526 :
9527 : static inline tree
9528 134311197 : convert_like_with_context (conversion *convs, tree expr, tree fn, int argnum,
9529 : tsubst_flags_t complain)
9530 : {
9531 0 : return convert_like (convs, expr, fn, argnum,
9532 : /*issue_conversion_warnings=*/true,
9533 : /*c_cast_p=*/false, /*nested_p=*/false, complain);
9534 : }
9535 :
9536 : /* ARG is being passed to a varargs function. Perform any conversions
9537 : required. Return the converted value. */
9538 :
9539 : tree
9540 1421119 : convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
9541 : {
9542 1421119 : tree arg_type = TREE_TYPE (arg);
9543 1421119 : location_t loc = cp_expr_loc_or_input_loc (arg);
9544 :
9545 : /* [expr.call]
9546 :
9547 : If the argument has integral or enumeration type that is subject
9548 : to the integral promotions (_conv.prom_), or a floating-point
9549 : type that is subject to the floating-point promotion
9550 : (_conv.fpprom_), the value of the argument is converted to the
9551 : promoted type before the call. */
9552 1421119 : if (SCALAR_FLOAT_TYPE_P (arg_type)
9553 47714 : && (TYPE_PRECISION (arg_type)
9554 47714 : < TYPE_PRECISION (double_type_node))
9555 11724 : && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type))
9556 1432165 : && !extended_float_type_p (arg_type))
9557 : {
9558 11040 : if ((complain & tf_warning)
9559 11037 : && warn_double_promotion && !c_inhibit_evaluation_warnings)
9560 3 : warning_at (loc, OPT_Wdouble_promotion,
9561 : "implicit conversion from %qH to %qI when passing "
9562 : "argument to function",
9563 : arg_type, double_type_node);
9564 11040 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
9565 3 : arg = TREE_OPERAND (arg, 0);
9566 11040 : arg = mark_rvalue_use (arg);
9567 11040 : arg = convert_to_real_nofold (double_type_node, arg);
9568 : }
9569 1410079 : else if (NULLPTR_TYPE_P (arg_type))
9570 : {
9571 80 : arg = mark_rvalue_use (arg);
9572 80 : if (TREE_SIDE_EFFECTS (arg))
9573 : {
9574 6 : warning_sentinel w(warn_unused_result);
9575 6 : arg = cp_build_compound_expr (arg, null_pointer_node, complain);
9576 6 : }
9577 : else
9578 74 : arg = null_pointer_node;
9579 : }
9580 1409999 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
9581 : {
9582 923012 : if (SCOPED_ENUM_P (arg_type))
9583 : {
9584 60 : tree prom = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg,
9585 : complain);
9586 60 : prom = cp_perform_integral_promotions (prom, complain);
9587 117 : if (abi_version_crosses (6)
9588 30 : && TYPE_MODE (TREE_TYPE (prom)) != TYPE_MODE (arg_type)
9589 90 : && (complain & tf_warning))
9590 60 : warning_at (loc, OPT_Wabi, "scoped enum %qT passed through %<...%>"
9591 : " as %qT before %<-fabi-version=6%>, %qT after",
9592 : arg_type,
9593 30 : TREE_TYPE (prom), ENUM_UNDERLYING_TYPE (arg_type));
9594 60 : if (!abi_version_at_least (6))
9595 1421119 : arg = prom;
9596 : }
9597 : else
9598 922952 : arg = cp_perform_integral_promotions (arg, complain);
9599 : }
9600 : else
9601 : /* [expr.call]
9602 :
9603 : The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
9604 : standard conversions are performed. */
9605 486987 : arg = decay_conversion (arg, complain);
9606 :
9607 1421119 : arg = require_complete_type (arg, complain);
9608 1421119 : arg_type = TREE_TYPE (arg);
9609 :
9610 1421119 : if (arg != error_mark_node
9611 : /* In a template (or ill-formed code), we can have an incomplete type
9612 : even after require_complete_type, in which case we don't know
9613 : whether it has trivial copy or not. */
9614 1421107 : && COMPLETE_TYPE_P (arg_type)
9615 2842226 : && !cp_unevaluated_operand)
9616 : {
9617 : /* [expr.call] 5.2.2/7:
9618 : Passing a potentially-evaluated argument of class type (Clause 9)
9619 : with a non-trivial copy constructor or a non-trivial destructor
9620 : with no corresponding parameter is conditionally-supported, with
9621 : implementation-defined semantics.
9622 :
9623 : We support it as pass-by-invisible-reference, just like a normal
9624 : value parameter.
9625 :
9626 : If the call appears in the context of a sizeof expression,
9627 : it is not potentially-evaluated. */
9628 686511 : if (type_has_nontrivial_copy_init (arg_type)
9629 686511 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type))
9630 : {
9631 37 : arg = force_rvalue (arg, complain);
9632 37 : if (complain & tf_warning)
9633 37 : warning (OPT_Wconditionally_supported,
9634 : "passing objects of non-trivially-copyable "
9635 : "type %q#T through %<...%> is conditionally supported",
9636 : arg_type);
9637 37 : return build1 (ADDR_EXPR, build_reference_type (arg_type), arg);
9638 : }
9639 : /* Build up a real lvalue-to-rvalue conversion in case the
9640 : copy constructor is trivial but not callable. */
9641 686474 : else if (CLASS_TYPE_P (arg_type))
9642 44172 : force_rvalue (arg, complain);
9643 :
9644 : }
9645 :
9646 : return arg;
9647 : }
9648 :
9649 : /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
9650 :
9651 : tree
9652 31298 : build_x_va_arg (location_t loc, tree expr, tree type)
9653 : {
9654 31298 : if (processing_template_decl)
9655 : {
9656 39 : tree r = build_min (VA_ARG_EXPR, type, expr);
9657 39 : SET_EXPR_LOCATION (r, loc);
9658 39 : return r;
9659 : }
9660 :
9661 31259 : type = complete_type_or_else (type, NULL_TREE);
9662 :
9663 31259 : if (expr == error_mark_node || !type)
9664 : return error_mark_node;
9665 :
9666 31238 : expr = mark_lvalue_use (expr);
9667 :
9668 31238 : if (TYPE_REF_P (type))
9669 : {
9670 6 : error ("cannot receive reference type %qT through %<...%>", type);
9671 6 : return error_mark_node;
9672 : }
9673 :
9674 31232 : if (type_has_nontrivial_copy_init (type)
9675 31232 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
9676 : {
9677 : /* conditionally-supported behavior [expr.call] 5.2.2/7. Let's treat
9678 : it as pass by invisible reference. */
9679 12 : warning_at (loc, OPT_Wconditionally_supported,
9680 : "receiving objects of non-trivially-copyable type %q#T "
9681 : "through %<...%> is conditionally-supported", type);
9682 :
9683 12 : tree ref = cp_build_reference_type (type, false);
9684 12 : expr = build_va_arg (loc, expr, ref);
9685 12 : return convert_from_reference (expr);
9686 : }
9687 :
9688 31220 : tree ret = build_va_arg (loc, expr, type);
9689 31220 : if (CLASS_TYPE_P (type))
9690 : /* Wrap the VA_ARG_EXPR in a TARGET_EXPR now so other code doesn't need to
9691 : know how to handle it. */
9692 12094 : ret = get_target_expr (ret);
9693 : return ret;
9694 : }
9695 :
9696 : /* TYPE has been given to va_arg. Apply the default conversions which
9697 : would have happened when passed via ellipsis. Return the promoted
9698 : type, or the passed type if there is no change. */
9699 :
9700 : tree
9701 2651637 : cxx_type_promotes_to (tree type)
9702 : {
9703 2651637 : tree promote;
9704 :
9705 : /* Perform the array-to-pointer and function-to-pointer
9706 : conversions. */
9707 2651637 : type = type_decays_to (type);
9708 :
9709 2651637 : promote = type_promotes_to (type);
9710 2651637 : if (same_type_p (type, promote))
9711 2651613 : promote = type;
9712 :
9713 2651637 : return promote;
9714 : }
9715 :
9716 : /* ARG is a default argument expression being passed to a parameter of
9717 : the indicated TYPE, which is a parameter to FN. PARMNUM is the
9718 : zero-based argument number. Do any required conversions. Return
9719 : the converted value. */
9720 :
9721 : static GTY(()) vec<tree, va_gc> *default_arg_context;
9722 : void
9723 7857632 : push_defarg_context (tree fn)
9724 7857632 : { vec_safe_push (default_arg_context, fn); }
9725 :
9726 : void
9727 7857632 : pop_defarg_context (void)
9728 7857632 : { default_arg_context->pop (); }
9729 :
9730 : tree
9731 1103634 : convert_default_arg (tree type, tree arg, tree fn, int parmnum,
9732 : tsubst_flags_t complain)
9733 : {
9734 1103634 : int i;
9735 1103634 : tree t;
9736 :
9737 : /* See through clones. */
9738 1103634 : fn = DECL_ORIGIN (fn);
9739 : /* And inheriting ctors. */
9740 1103634 : if (flag_new_inheriting_ctors)
9741 1102994 : fn = strip_inheriting_ctors (fn);
9742 :
9743 : /* Detect recursion. */
9744 1107013 : FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t)
9745 3385 : if (t == fn)
9746 : {
9747 6 : if (complain & tf_error)
9748 6 : error ("recursive evaluation of default argument for %q#D", fn);
9749 6 : return error_mark_node;
9750 : }
9751 :
9752 : /* If the ARG is an unparsed default argument expression, the
9753 : conversion cannot be performed. */
9754 1103628 : if (TREE_CODE (arg) == DEFERRED_PARSE)
9755 : {
9756 15 : if (complain & tf_error)
9757 15 : error ("call to %qD uses the default argument for parameter %P, which "
9758 : "is not yet defined", fn, parmnum);
9759 15 : return error_mark_node;
9760 : }
9761 :
9762 1103613 : push_defarg_context (fn);
9763 :
9764 1103613 : if (fn && DECL_TEMPLATE_INFO (fn))
9765 804961 : arg = tsubst_default_argument (fn, parmnum, type, arg, complain);
9766 :
9767 : /* Due to:
9768 :
9769 : [dcl.fct.default]
9770 :
9771 : The names in the expression are bound, and the semantic
9772 : constraints are checked, at the point where the default
9773 : expressions appears.
9774 :
9775 : we must not perform access checks here. */
9776 1103613 : push_deferring_access_checks (dk_no_check);
9777 : /* We must make a copy of ARG, in case subsequent processing
9778 : alters any part of it. */
9779 1103613 : arg = break_out_target_exprs (arg, /*clear location*/true);
9780 :
9781 1103613 : arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
9782 : ICR_DEFAULT_ARGUMENT, fn, parmnum,
9783 : complain);
9784 1103613 : arg = convert_for_arg_passing (type, arg, complain);
9785 1103613 : pop_deferring_access_checks();
9786 :
9787 1103613 : pop_defarg_context ();
9788 :
9789 1103613 : return arg;
9790 : }
9791 :
9792 : /* Returns the type which will really be used for passing an argument of
9793 : type TYPE. */
9794 :
9795 : tree
9796 665742900 : type_passed_as (tree type)
9797 : {
9798 : /* Pass classes with copy ctors by invisible reference. */
9799 665742900 : if (TREE_ADDRESSABLE (type))
9800 613769 : type = build_reference_type (type);
9801 :
9802 665742900 : return type;
9803 : }
9804 :
9805 : /* Actually perform the appropriate conversion. */
9806 :
9807 : tree
9808 140219329 : convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
9809 : {
9810 140219329 : tree bitfield_type;
9811 :
9812 : /* If VAL is a bitfield, then -- since it has already been converted
9813 : to TYPE -- it cannot have a precision greater than TYPE.
9814 :
9815 : If it has a smaller precision, we must widen it here. For
9816 : example, passing "int f:3;" to a function expecting an "int" will
9817 : not result in any conversion before this point.
9818 :
9819 : If the precision is the same we must not risk widening. For
9820 : example, the COMPONENT_REF for a 32-bit "long long" bitfield will
9821 : often have type "int", even though the C++ type for the field is
9822 : "long long". If the value is being passed to a function
9823 : expecting an "int", then no conversions will be required. But,
9824 : if we call convert_bitfield_to_declared_type, the bitfield will
9825 : be converted to "long long". */
9826 140219329 : bitfield_type = is_bitfield_expr_with_lowered_type (val);
9827 140219329 : if (bitfield_type
9828 140219329 : && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
9829 0 : val = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type), val);
9830 :
9831 140219329 : if (val == error_mark_node)
9832 : ;
9833 : /* Pass classes with copy ctors by invisible reference. */
9834 140215683 : else if (TREE_ADDRESSABLE (type))
9835 205955 : val = build1 (ADDR_EXPR, build_reference_type (type), val);
9836 140219329 : if (complain & tf_warning)
9837 128551870 : maybe_warn_parm_abi (type, cp_expr_loc_or_input_loc (val));
9838 :
9839 103304595 : if (complain & tf_warning)
9840 103304595 : warn_for_address_of_packed_member (type, val);
9841 :
9842 : /* gimplify_arg elides TARGET_EXPRs that initialize a function argument,
9843 : unless the initializer is a CONSTRUCTOR. In that case, we fail to
9844 : elide the copy anyway. See that function for more information. */
9845 7729645 : if (SIMPLE_TARGET_EXPR_P (val)
9846 146552242 : && TREE_CODE (TARGET_EXPR_INITIAL (val)) != CONSTRUCTOR)
9847 3708635 : set_target_expr_eliding (val);
9848 :
9849 140219329 : return val;
9850 : }
9851 :
9852 : /* Returns non-zero iff FN is a function with magic varargs, i.e. ones for
9853 : which just decay_conversion or no conversions at all should be done.
9854 : This is true for some builtins which don't act like normal functions.
9855 : Return 2 if just decay_conversion and removal of excess precision should
9856 : be done, 1 if just decay_conversion. Return 3 for special treatment of
9857 : the 3rd argument for __builtin_*_overflow_p. Return 4 for special
9858 : treatment of the 1st argument for
9859 : __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g. */
9860 :
9861 : int
9862 171276598 : magic_varargs_p (tree fn)
9863 : {
9864 171276598 : if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
9865 5945377 : switch (DECL_FUNCTION_CODE (fn))
9866 : {
9867 : case BUILT_IN_CLASSIFY_TYPE:
9868 : case BUILT_IN_CONSTANT_P:
9869 : case BUILT_IN_NEXT_ARG:
9870 : case BUILT_IN_VA_START:
9871 : return 1;
9872 :
9873 20842 : case BUILT_IN_ADD_OVERFLOW_P:
9874 20842 : case BUILT_IN_SUB_OVERFLOW_P:
9875 20842 : case BUILT_IN_MUL_OVERFLOW_P:
9876 20842 : return 3;
9877 :
9878 293335 : case BUILT_IN_ISFINITE:
9879 293335 : case BUILT_IN_ISINF:
9880 293335 : case BUILT_IN_ISINF_SIGN:
9881 293335 : case BUILT_IN_ISNAN:
9882 293335 : case BUILT_IN_ISNORMAL:
9883 293335 : case BUILT_IN_FPCLASSIFY:
9884 293335 : return 2;
9885 :
9886 100040 : case BUILT_IN_CLZG:
9887 100040 : case BUILT_IN_CTZG:
9888 100040 : case BUILT_IN_CLRSBG:
9889 100040 : case BUILT_IN_FFSG:
9890 100040 : case BUILT_IN_PARITYG:
9891 100040 : case BUILT_IN_POPCOUNTG:
9892 100040 : return 4;
9893 :
9894 5328877 : default:
9895 5328877 : return lookup_attribute ("type generic",
9896 10657754 : TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
9897 : }
9898 :
9899 : return 0;
9900 : }
9901 :
9902 : /* Returns the decl of the dispatcher function if FN is a function version. */
9903 :
9904 : tree
9905 240 : get_function_version_dispatcher (tree fn)
9906 : {
9907 240 : tree dispatcher_decl = NULL;
9908 :
9909 240 : if (DECL_LOCAL_DECL_P (fn))
9910 9 : fn = DECL_LOCAL_DECL_ALIAS (fn);
9911 :
9912 240 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
9913 : && DECL_FUNCTION_VERSIONED (fn));
9914 :
9915 240 : gcc_assert (targetm.get_function_versions_dispatcher);
9916 240 : dispatcher_decl = targetm.get_function_versions_dispatcher (fn);
9917 :
9918 240 : if (dispatcher_decl == NULL)
9919 : {
9920 9 : error_at (input_location, "use of multiversioned function "
9921 : "without a default");
9922 9 : return NULL;
9923 : }
9924 :
9925 231 : retrofit_lang_decl (dispatcher_decl);
9926 231 : gcc_assert (dispatcher_decl != NULL);
9927 : return dispatcher_decl;
9928 : }
9929 :
9930 : /* fn is a function version dispatcher that is marked used. Mark all the
9931 : semantically identical function versions it will dispatch as used. */
9932 :
9933 : void
9934 159 : mark_versions_used (tree fn)
9935 : {
9936 159 : struct cgraph_node *node;
9937 159 : struct cgraph_function_version_info *node_v;
9938 159 : struct cgraph_function_version_info *it_v;
9939 :
9940 159 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9941 :
9942 159 : node = cgraph_node::get (fn);
9943 159 : if (node == NULL)
9944 : return;
9945 :
9946 159 : gcc_assert (node->dispatcher_function);
9947 :
9948 159 : node_v = node->function_version ();
9949 159 : if (node_v == NULL)
9950 : return;
9951 :
9952 : /* All semantically identical versions are chained. Traverse and mark each
9953 : one of them as used. */
9954 159 : it_v = node_v->next;
9955 1146 : while (it_v != NULL)
9956 : {
9957 987 : mark_used (it_v->this_node->decl);
9958 987 : it_v = it_v->next;
9959 : }
9960 : }
9961 :
9962 : /* Build a call to "the copy constructor" for the type of A, even if it
9963 : wouldn't be selected by normal overload resolution. Used for
9964 : diagnostics. */
9965 :
9966 : static tree
9967 3 : call_copy_ctor (tree a, tsubst_flags_t complain)
9968 : {
9969 3 : tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (a));
9970 3 : tree binfo = TYPE_BINFO (ctype);
9971 3 : tree copy = get_copy_ctor (ctype, complain);
9972 3 : copy = build_baselink (binfo, binfo, copy, NULL_TREE);
9973 3 : tree ob = build_dummy_object (ctype);
9974 3 : releasing_vec args (make_tree_vector_single (a));
9975 3 : tree r = build_new_method_call (ob, copy, &args, NULL_TREE,
9976 : LOOKUP_NORMAL, NULL, complain);
9977 3 : return r;
9978 3 : }
9979 :
9980 : /* Return the base constructor corresponding to COMPLETE_CTOR or NULL_TREE. */
9981 :
9982 : static tree
9983 42 : base_ctor_for (tree complete_ctor)
9984 : {
9985 42 : tree clone;
9986 42 : FOR_EACH_CLONE (clone, DECL_CLONED_FUNCTION (complete_ctor))
9987 42 : if (DECL_BASE_CONSTRUCTOR_P (clone))
9988 : return clone;
9989 : return NULL_TREE;
9990 : }
9991 :
9992 : /* Try to make EXP suitable to be used as the initializer for a base subobject,
9993 : and return whether we were successful. EXP must have already been cleared
9994 : by unsafe_copy_elision_p{,_opt}. */
9995 :
9996 : static bool
9997 218 : make_base_init_ok (tree exp)
9998 : {
9999 218 : if (TREE_CODE (exp) == TARGET_EXPR)
10000 218 : exp = TARGET_EXPR_INITIAL (exp);
10001 221 : while (TREE_CODE (exp) == COMPOUND_EXPR)
10002 3 : exp = TREE_OPERAND (exp, 1);
10003 218 : if (TREE_CODE (exp) == COND_EXPR)
10004 : {
10005 3 : bool ret = make_base_init_ok (TREE_OPERAND (exp, 2));
10006 3 : if (tree op1 = TREE_OPERAND (exp, 1))
10007 : {
10008 3 : bool r1 = make_base_init_ok (op1);
10009 : /* If unsafe_copy_elision_p was false, the arms should match. */
10010 3 : gcc_assert (r1 == ret);
10011 : }
10012 3 : return ret;
10013 : }
10014 215 : if (TREE_CODE (exp) != AGGR_INIT_EXPR)
10015 : /* A trivial copy is OK. */
10016 : return true;
10017 64 : if (!AGGR_INIT_VIA_CTOR_P (exp))
10018 : /* unsafe_copy_elision_p_opt must have said this is OK. */
10019 : return true;
10020 42 : tree fn = cp_get_callee_fndecl_nofold (exp);
10021 42 : if (DECL_BASE_CONSTRUCTOR_P (fn))
10022 : return true;
10023 42 : gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (fn));
10024 42 : fn = base_ctor_for (fn);
10025 42 : if (!fn || DECL_HAS_VTT_PARM_P (fn))
10026 : /* The base constructor has more parameters, so we can't just change the
10027 : call target. It would be possible to splice in the appropriate
10028 : arguments, but probably not worth the complexity. */
10029 : return false;
10030 33 : mark_used (fn);
10031 33 : AGGR_INIT_EXPR_FN (exp) = build_address (fn);
10032 33 : return true;
10033 : }
10034 :
10035 : /* Return 2 if T refers to a base, 1 if a potentially-overlapping field,
10036 : neither of which can be used for return by invisible reference. We avoid
10037 : doing C++17 mandatory copy elision for either of these cases.
10038 :
10039 : This returns non-zero even if the type of T has no tail padding that other
10040 : data could be allocated into, because that depends on the particular ABI.
10041 : unsafe_copy_elision_p_opt does consider whether there is padding. */
10042 :
10043 : int
10044 42083638 : unsafe_return_slot_p (tree t)
10045 : {
10046 : /* Check empty bases separately, they don't have fields. */
10047 42083638 : if (is_empty_base_ref (t))
10048 : return 2;
10049 :
10050 : /* A delegating constructor might be used to initialize a base. */
10051 41609711 : if (current_function_decl
10052 56364926 : && DECL_CONSTRUCTOR_P (current_function_decl)
10053 45785592 : && (t == current_class_ref
10054 4006900 : || tree_strip_nop_conversions (t) == current_class_ptr))
10055 218829 : return 2;
10056 :
10057 41390882 : STRIP_NOPS (t);
10058 41390882 : if (TREE_CODE (t) == ADDR_EXPR)
10059 65498 : t = TREE_OPERAND (t, 0);
10060 41390882 : if (TREE_CODE (t) == COMPONENT_REF)
10061 3465833 : t = TREE_OPERAND (t, 1);
10062 41390882 : if (TREE_CODE (t) != FIELD_DECL)
10063 : return false;
10064 3511734 : if (!CLASS_TYPE_P (TREE_TYPE (t)))
10065 : /* The middle-end will do the right thing for scalar types. */
10066 : return false;
10067 3016965 : if (DECL_FIELD_IS_BASE (t))
10068 : return 2;
10069 2206318 : if (lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (t)))
10070 : return 1;
10071 : return 0;
10072 : }
10073 :
10074 : /* True IFF EXP is a prvalue that represents return by invisible reference. */
10075 :
10076 : static bool
10077 279806 : init_by_return_slot_p (tree exp)
10078 : {
10079 : /* Copy elision only happens with a TARGET_EXPR. */
10080 279809 : if (TREE_CODE (exp) != TARGET_EXPR)
10081 : return false;
10082 15384 : tree init = TARGET_EXPR_INITIAL (exp);
10083 : /* build_compound_expr pushes COMPOUND_EXPR inside TARGET_EXPR. */
10084 15390 : while (TREE_CODE (init) == COMPOUND_EXPR)
10085 6 : init = TREE_OPERAND (init, 1);
10086 15384 : if (TREE_CODE (init) == COND_EXPR)
10087 : {
10088 : /* We'll end up copying from each of the arms of the COND_EXPR directly
10089 : into the target, so look at them. */
10090 6 : if (tree op = TREE_OPERAND (init, 1))
10091 6 : if (init_by_return_slot_p (op))
10092 : return true;
10093 3 : return init_by_return_slot_p (TREE_OPERAND (init, 2));
10094 : }
10095 15378 : return (TREE_CODE (init) == AGGR_INIT_EXPR
10096 15378 : && !AGGR_INIT_VIA_CTOR_P (init));
10097 : }
10098 :
10099 : /* We can't elide a copy from a function returning by value to a
10100 : potentially-overlapping subobject, as the callee might clobber tail padding.
10101 : Return true iff this could be that case.
10102 :
10103 : Places that use this function (or _opt) to decide to elide a copy should
10104 : probably use make_safe_copy_elision instead. */
10105 :
10106 : bool
10107 2387634 : unsafe_copy_elision_p (tree target, tree exp)
10108 : {
10109 2387634 : return unsafe_return_slot_p (target) && init_by_return_slot_p (exp);
10110 : }
10111 :
10112 : /* As above, but for optimization allow more cases that are actually safe. */
10113 :
10114 : static bool
10115 8073036 : unsafe_copy_elision_p_opt (tree target, tree exp)
10116 : {
10117 8073036 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
10118 : /* It's safe to elide the copy for a class with no tail padding. */
10119 8073036 : if (!is_empty_class (type)
10120 8073036 : && tree_int_cst_equal (TYPE_SIZE (type), CLASSTYPE_SIZE (type)))
10121 : return false;
10122 2341733 : return unsafe_copy_elision_p (target, exp);
10123 : }
10124 :
10125 : /* Try to make EXP suitable to be used as the initializer for TARGET,
10126 : and return whether we were successful. */
10127 :
10128 : bool
10129 23 : make_safe_copy_elision (tree target, tree exp)
10130 : {
10131 23 : int uns = unsafe_return_slot_p (target);
10132 23 : if (!uns)
10133 : return true;
10134 23 : if (init_by_return_slot_p (exp))
10135 : return false;
10136 23 : if (uns == 1)
10137 : return true;
10138 19 : return make_base_init_ok (exp);
10139 : }
10140 :
10141 : /* True IFF the result of the conversion C is a prvalue. */
10142 :
10143 : static bool
10144 14051543 : conv_is_prvalue (conversion *c)
10145 : {
10146 14051543 : if (c->kind == ck_rvalue)
10147 : return true;
10148 14051543 : if (c->kind == ck_base && c->need_temporary_p)
10149 : return true;
10150 14051543 : if (c->kind == ck_user && !TYPE_REF_P (c->type))
10151 : return true;
10152 13918144 : if (c->kind == ck_identity && c->u.expr
10153 13672024 : && TREE_CODE (c->u.expr) == TARGET_EXPR)
10154 69 : return true;
10155 :
10156 : return false;
10157 : }
10158 :
10159 : /* True iff C is a conversion that binds a reference to a prvalue. */
10160 :
10161 : static bool
10162 14052845 : conv_binds_ref_to_prvalue (conversion *c)
10163 : {
10164 14052845 : if (c->kind != ck_ref_bind)
10165 : return false;
10166 14052845 : if (c->need_temporary_p)
10167 : return true;
10168 :
10169 14051543 : return conv_is_prvalue (next_conversion (c));
10170 : }
10171 :
10172 : /* True iff EXPR represents a (subobject of a) temporary. */
10173 :
10174 : static bool
10175 14812 : expr_represents_temporary_p (tree expr)
10176 : {
10177 14916 : while (handled_component_p (expr))
10178 104 : expr = TREE_OPERAND (expr, 0);
10179 14812 : return TREE_CODE (expr) == TARGET_EXPR;
10180 : }
10181 :
10182 : /* True iff C is a conversion that binds a reference to a temporary.
10183 : This is a superset of conv_binds_ref_to_prvalue: here we're also
10184 : interested in xvalues. */
10185 :
10186 : static bool
10187 14155 : conv_binds_ref_to_temporary (conversion *c)
10188 : {
10189 14155 : if (conv_binds_ref_to_prvalue (c))
10190 : return true;
10191 13668 : if (c->kind != ck_ref_bind)
10192 : return false;
10193 13668 : c = next_conversion (c);
10194 : /* This is the case for
10195 : struct Base {};
10196 : struct Derived : Base {};
10197 : const Base& b(Derived{});
10198 : where we bind 'b' to the Base subobject of a temporary object of type
10199 : Derived. The subobject is an xvalue; the whole object is a prvalue.
10200 :
10201 : The ck_base doesn't have to be present for cases like X{}.m. */
10202 13668 : if (c->kind == ck_base)
10203 14 : c = next_conversion (c);
10204 13589 : if (c->kind == ck_identity && c->u.expr
10205 27257 : && expr_represents_temporary_p (c->u.expr))
10206 : return true;
10207 : return false;
10208 : }
10209 :
10210 : /* Return tristate::TS_TRUE if converting EXPR to a reference type TYPE binds
10211 : the reference to a temporary. Return tristate::TS_FALSE if converting
10212 : EXPR to a reference type TYPE doesn't bind the reference to a temporary. If
10213 : the conversion is invalid or bad, return tristate::TS_UNKNOWN. DIRECT_INIT_P
10214 : says whether the conversion should be done in direct- or copy-initialization
10215 : context. */
10216 :
10217 : tristate
10218 14684 : ref_conv_binds_to_temporary (tree type, tree expr, bool direct_init_p/*=false*/)
10219 : {
10220 14684 : gcc_assert (TYPE_REF_P (type));
10221 :
10222 14684 : conversion_obstack_sentinel cos;
10223 :
10224 14684 : const int flags = direct_init_p ? LOOKUP_NORMAL : LOOKUP_IMPLICIT;
10225 14684 : conversion *conv = implicit_conversion (type, TREE_TYPE (expr), expr,
10226 : /*c_cast_p=*/false, flags, tf_none);
10227 14684 : if (!conv || conv->bad_p)
10228 529 : return tristate::unknown ();
10229 :
10230 14155 : if (conv_binds_ref_to_temporary (conv))
10231 : {
10232 : /* Actually perform the conversion to check access control. */
10233 499 : if (convert_like (conv, expr, tf_none) != error_mark_node)
10234 487 : return tristate (true);
10235 : else
10236 12 : return tristate::unknown ();
10237 : }
10238 :
10239 13656 : return tristate (false);
10240 14684 : }
10241 :
10242 : /* Call the trivial destructor for INSTANCE, which can be either an lvalue of
10243 : class type or a pointer to class type. If NO_PTR_DEREF is true and
10244 : INSTANCE has pointer type, clobber the pointer rather than what it points
10245 : to. */
10246 :
10247 : tree
10248 3310476 : build_trivial_dtor_call (tree instance, bool no_ptr_deref)
10249 : {
10250 3310476 : gcc_assert (!is_dummy_object (instance));
10251 :
10252 3310476 : if (!flag_lifetime_dse)
10253 : {
10254 58 : no_clobber:
10255 77 : return fold_convert (void_type_node, instance);
10256 : }
10257 :
10258 3355263 : if (INDIRECT_TYPE_P (TREE_TYPE (instance))
10259 3310418 : && (!no_ptr_deref || TYPE_REF_P (TREE_TYPE (instance))))
10260 : {
10261 3150037 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
10262 19 : goto no_clobber;
10263 3150018 : instance = cp_build_fold_indirect_ref (instance);
10264 : }
10265 :
10266 : /* A trivial destructor should still clobber the object. */
10267 3310399 : tree clobber = build_clobber (TREE_TYPE (instance), CLOBBER_OBJECT_END);
10268 3310399 : return build2 (MODIFY_EXPR, void_type_node,
10269 3310399 : instance, clobber);
10270 : }
10271 :
10272 : /* Return true if in an immediate function context, or an unevaluated operand,
10273 : or a default argument/member initializer, or a subexpression of an immediate
10274 : invocation. */
10275 :
10276 : bool
10277 15485672 : in_immediate_context ()
10278 : {
10279 15485672 : return (cp_unevaluated_operand != 0
10280 13819743 : || (current_function_decl != NULL_TREE
10281 23095962 : && DECL_IMMEDIATE_FUNCTION_P (current_function_decl))
10282 : /* DR 2631: default args and DMI aren't immediately evaluated.
10283 : Return true here so immediate_invocation_p returns false. */
10284 13463260 : || current_binding_level->kind == sk_function_parms
10285 13460298 : || current_binding_level->kind == sk_template_parms
10286 13393007 : || parsing_nsdmi ()
10287 28876098 : || in_consteval_if_p);
10288 : }
10289 :
10290 : /* Return true if a call to FN with number of arguments NARGS
10291 : is an immediate invocation. */
10292 :
10293 : bool
10294 212234415 : immediate_invocation_p (tree fn)
10295 : {
10296 212234415 : return (TREE_CODE (fn) == FUNCTION_DECL
10297 212234415 : && DECL_IMMEDIATE_FUNCTION_P (fn)
10298 215297442 : && !in_immediate_context ());
10299 : }
10300 :
10301 : /* Subroutine of the various build_*_call functions. Overload resolution
10302 : has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
10303 : ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
10304 : bitmask of various LOOKUP_* flags which apply to the call itself. */
10305 :
10306 : static tree
10307 227610015 : build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
10308 : {
10309 227610015 : tree fn = cand->fn;
10310 227610015 : const vec<tree, va_gc> *args = cand->args;
10311 227610015 : tree first_arg = cand->first_arg;
10312 227610015 : conversion **convs = cand->convs;
10313 227610015 : tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
10314 227610015 : int parmlen;
10315 227610015 : tree val;
10316 227610015 : int nargs;
10317 227610015 : tree *argarray;
10318 227610015 : bool already_used = false;
10319 :
10320 : /* In a template, there is no need to perform all of the work that
10321 : is normally done. We are only interested in the type of the call
10322 : expression, i.e., the return type of the function. Any semantic
10323 : errors will be deferred until the template is instantiated. */
10324 227610015 : if (processing_template_decl)
10325 : {
10326 29657312 : if (undeduced_auto_decl (fn))
10327 12769 : mark_used (fn, complain);
10328 : else
10329 : /* Otherwise set TREE_USED for the benefit of -Wunused-function.
10330 : See PR80598. */
10331 29644543 : TREE_USED (fn) = 1;
10332 :
10333 29657312 : tree return_type = TREE_TYPE (TREE_TYPE (fn));
10334 29657312 : tree callee;
10335 29657312 : if (first_arg == NULL_TREE)
10336 : {
10337 22169308 : callee = build_addr_func (fn, complain);
10338 22169308 : if (callee == error_mark_node)
10339 : return error_mark_node;
10340 : }
10341 : else
10342 : {
10343 7488004 : callee = build_baselink (cand->conversion_path, cand->access_path,
10344 : fn, NULL_TREE);
10345 7488004 : callee = build_min (COMPONENT_REF, TREE_TYPE (fn),
10346 : first_arg, callee, NULL_TREE);
10347 : }
10348 :
10349 29657312 : tree expr = build_call_vec (return_type, callee, args);
10350 29657312 : SET_EXPR_LOCATION (expr, input_location);
10351 29657312 : if (TREE_THIS_VOLATILE (fn) && cfun)
10352 2556891 : current_function_returns_abnormally = 1;
10353 29657312 : if (TREE_DEPRECATED (fn)
10354 29657312 : && warning_suppressed_at (input_location,
10355 : OPT_Wdeprecated_declarations))
10356 : /* Make the expr consistent with the location. */
10357 19618 : TREE_NO_WARNING (expr) = true;
10358 29657312 : if (immediate_invocation_p (fn))
10359 : {
10360 204054 : tree obj_arg = NULL_TREE;
10361 408108 : if (DECL_CONSTRUCTOR_P (fn))
10362 0 : obj_arg = first_arg;
10363 : /* Look through *(const T *)&obj. */
10364 0 : if (obj_arg && INDIRECT_REF_P (obj_arg))
10365 : {
10366 0 : tree addr = TREE_OPERAND (obj_arg, 0);
10367 0 : STRIP_NOPS (addr);
10368 0 : if (TREE_CODE (addr) == ADDR_EXPR)
10369 : {
10370 0 : tree typeo = TREE_TYPE (obj_arg);
10371 0 : tree typei = TREE_TYPE (TREE_OPERAND (addr, 0));
10372 0 : if (same_type_ignoring_top_level_qualifiers_p (typeo, typei))
10373 0 : obj_arg = TREE_OPERAND (addr, 0);
10374 : }
10375 : }
10376 204054 : fold_non_dependent_expr (expr, complain,
10377 : /*manifestly_const_eval=*/true,
10378 : obj_arg);
10379 : }
10380 29657312 : return convert_from_reference (expr);
10381 : }
10382 :
10383 : /* Give any warnings we noticed during overload resolution. */
10384 197952703 : if (cand->warnings && (complain & tf_warning))
10385 : {
10386 : struct candidate_warning *w;
10387 98 : for (w = cand->warnings; w; w = w->next)
10388 49 : joust (cand, w->loser, 1, complain);
10389 : }
10390 :
10391 : /* Core issue 2327: P0135 doesn't say how to handle the case where the
10392 : argument to the copy constructor ends up being a prvalue after
10393 : conversion. Let's do the normal processing, but pretend we aren't
10394 : actually using the copy constructor. */
10395 197952703 : bool force_elide = false;
10396 197952703 : if (cxx_dialect >= cxx17
10397 195974295 : && cand->num_convs == 1
10398 110945176 : && DECL_COMPLETE_CONSTRUCTOR_P (fn)
10399 38790502 : && (DECL_COPY_CONSTRUCTOR_P (fn)
10400 21059380 : || DECL_MOVE_CONSTRUCTOR_P (fn))
10401 14109263 : && !unsafe_return_slot_p (first_arg)
10402 211991327 : && conv_binds_ref_to_prvalue (convs[0]))
10403 : {
10404 134280 : force_elide = true;
10405 134280 : goto not_really_used;
10406 : }
10407 :
10408 : /* OK, we're actually calling this inherited constructor; set its deletedness
10409 : appropriately. We can get away with doing this here because calling is
10410 : the only way to refer to a constructor. */
10411 395636846 : if (DECL_INHERITED_CTOR (fn)
10412 28751570 : && !deduce_inheriting_ctor (fn))
10413 : {
10414 2 : if (complain & tf_error)
10415 2 : mark_used (fn);
10416 2 : return error_mark_node;
10417 : }
10418 :
10419 : /* Make =delete work with SFINAE. */
10420 197818421 : if (DECL_DELETED_FN (fn))
10421 : {
10422 1018524 : if (complain & tf_error)
10423 : {
10424 2300 : mark_used (fn);
10425 2300 : if (cand->next)
10426 : {
10427 2086 : if (flag_diagnostics_all_candidates)
10428 9 : print_z_candidates (input_location, cand, /*only_viable_p=*/false);
10429 : else
10430 2077 : inform (input_location,
10431 : "use %<-fdiagnostics-all-candidates%> to display "
10432 : "considered candidates");
10433 : }
10434 : }
10435 1018524 : return error_mark_node;
10436 : }
10437 :
10438 196799897 : if (DECL_FUNCTION_MEMBER_P (fn))
10439 : {
10440 104762047 : tree access_fn;
10441 : /* If FN is a template function, two cases must be considered.
10442 : For example:
10443 :
10444 : struct A {
10445 : protected:
10446 : template <class T> void f();
10447 : };
10448 : template <class T> struct B {
10449 : protected:
10450 : void g();
10451 : };
10452 : struct C : A, B<int> {
10453 : using A::f; // #1
10454 : using B<int>::g; // #2
10455 : };
10456 :
10457 : In case #1 where `A::f' is a member template, DECL_ACCESS is
10458 : recorded in the primary template but not in its specialization.
10459 : We check access of FN using its primary template.
10460 :
10461 : In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
10462 : because it is a member of class template B, DECL_ACCESS is
10463 : recorded in the specialization `B<int>::g'. We cannot use its
10464 : primary template because `B<T>::g' and `B<int>::g' may have
10465 : different access. */
10466 104762047 : if (DECL_TEMPLATE_INFO (fn)
10467 104762047 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
10468 7898633 : access_fn = DECL_TI_TEMPLATE (fn);
10469 : else
10470 : access_fn = fn;
10471 104762047 : if (!perform_or_defer_access_check (cand->access_path, access_fn,
10472 : fn, complain))
10473 20731 : return error_mark_node;
10474 : }
10475 :
10476 : /* If we're checking for implicit delete, don't bother with argument
10477 : conversions. */
10478 196779166 : if (flags & LOOKUP_SPECULATIVE)
10479 : {
10480 25633660 : if (cand->viable == 1)
10481 : return fn;
10482 178 : else if (!(complain & tf_error))
10483 : /* Reject bad conversions now. */
10484 146 : return error_mark_node;
10485 : /* else continue to get conversion error. */
10486 : }
10487 :
10488 171145506 : not_really_used:
10489 :
10490 : /* N3276 magic doesn't apply to nested calls. */
10491 171279818 : tsubst_flags_t decltype_flag = (complain & tf_decltype);
10492 171279818 : complain &= ~tf_decltype;
10493 : /* No-Cleanup doesn't apply to nested calls either. */
10494 171279818 : tsubst_flags_t no_cleanup_complain = complain;
10495 171279818 : complain &= ~tf_no_cleanup;
10496 :
10497 : /* Find maximum size of vector to hold converted arguments. */
10498 171279818 : parmlen = list_length (parm);
10499 322356998 : nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0);
10500 171279818 : if (parmlen > nargs)
10501 : nargs = parmlen;
10502 171279818 : argarray = XALLOCAVEC (tree, nargs);
10503 :
10504 342559633 : in_consteval_if_p_temp_override icip;
10505 : /* If the call is immediate function invocation, make sure
10506 : taking address of immediate functions is allowed in its arguments. */
10507 171279818 : if (immediate_invocation_p (STRIP_TEMPLATE (fn)))
10508 1538253 : in_consteval_if_p = true;
10509 :
10510 171279818 : int argarray_size = 0;
10511 171279818 : unsigned int arg_index = 0;
10512 171279818 : int conv_index = 0;
10513 171279818 : int param_index = 0;
10514 171279818 : tree parmd = DECL_ARGUMENTS (fn);
10515 :
10516 237703957 : auto consume_object_arg = [&arg_index, &first_arg, args]()
10517 : {
10518 66424139 : if (!first_arg)
10519 0 : return (*args)[arg_index++];
10520 66424139 : tree object_arg = first_arg;
10521 66424139 : first_arg = NULL_TREE;
10522 66424139 : return object_arg;
10523 171279818 : };
10524 :
10525 : /* The implicit parameters to a constructor are not considered by overload
10526 : resolution, and must be of the proper type. */
10527 171279818 : if (DECL_CONSTRUCTOR_P (fn))
10528 : {
10529 20403163 : tree object_arg = consume_object_arg ();
10530 20403163 : argarray[argarray_size++] = build_this (object_arg);
10531 20403163 : parm = TREE_CHAIN (parm);
10532 20403163 : if (parmd)
10533 20403163 : parmd = DECL_CHAIN (parmd);
10534 : /* We should never try to call the abstract constructor. */
10535 20403163 : gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
10536 :
10537 20403163 : if (DECL_HAS_VTT_PARM_P (fn))
10538 : {
10539 17406 : argarray[argarray_size++] = (*args)[arg_index];
10540 17406 : ++arg_index;
10541 17406 : parm = TREE_CHAIN (parm);
10542 17406 : if (parmd)
10543 17406 : parmd = DECL_CHAIN (parmd);
10544 : }
10545 : }
10546 : /* Bypass access control for 'this' parameter. */
10547 150876655 : else if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
10548 : {
10549 46011734 : tree arg = build_this (consume_object_arg ());
10550 46011734 : tree argtype = TREE_TYPE (arg);
10551 :
10552 46011734 : if (arg == error_mark_node)
10553 : return error_mark_node;
10554 46011731 : if (convs[conv_index++]->bad_p)
10555 : {
10556 1219 : if (complain & tf_error)
10557 : {
10558 105 : auto_diagnostic_group d;
10559 105 : if (permerror (input_location, "passing %qT as %<this%> "
10560 : "argument discards qualifiers",
10561 105 : TREE_TYPE (argtype)))
10562 102 : inform (DECL_SOURCE_LOCATION (fn), " in call to %qD", fn);
10563 105 : }
10564 : else
10565 : return error_mark_node;
10566 : }
10567 :
10568 : /* The class where FN is defined. */
10569 46010617 : tree ctx = DECL_CONTEXT (fn);
10570 :
10571 : /* See if the function member or the whole class type is declared
10572 : final and the call can be devirtualized. */
10573 46010617 : if (DECL_FINAL_P (fn) || CLASSTYPE_FINAL (ctx))
10574 132323 : flags |= LOOKUP_NONVIRTUAL;
10575 :
10576 : /* [class.mfct.non-static]: If a non-static member function of a class
10577 : X is called for an object that is not of type X, or of a type
10578 : derived from X, the behavior is undefined.
10579 :
10580 : So we can assume that anything passed as 'this' is non-null, and
10581 : optimize accordingly. */
10582 : /* Check that the base class is accessible. */
10583 46010617 : if (!accessible_base_p (TREE_TYPE (argtype),
10584 46010617 : BINFO_TYPE (cand->conversion_path), true))
10585 : {
10586 33 : if (complain & tf_error)
10587 30 : error ("%qT is not an accessible base of %qT",
10588 30 : BINFO_TYPE (cand->conversion_path),
10589 30 : TREE_TYPE (argtype));
10590 : else
10591 3 : return error_mark_node;
10592 : }
10593 : /* If fn was found by a using declaration, the conversion path
10594 : will be to the derived class, not the base declaring fn. We
10595 : must convert to the base. */
10596 46010614 : tree base_binfo = cand->conversion_path;
10597 46010614 : if (BINFO_TYPE (base_binfo) != ctx)
10598 : {
10599 119748 : base_binfo = lookup_base (base_binfo, ctx, ba_unique, NULL, complain);
10600 119748 : if (base_binfo == error_mark_node)
10601 : return error_mark_node;
10602 : }
10603 :
10604 : /* If we know the dynamic type of the object, look up the final overrider
10605 : in the BINFO. */
10606 46913205 : if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
10607 46504501 : && resolves_to_fixed_type_p (arg))
10608 : {
10609 1209 : tree ov = lookup_vfn_in_binfo (DECL_VINDEX (fn), base_binfo);
10610 :
10611 : /* And unwind base_binfo to match. If we don't find the type we're
10612 : looking for in BINFO_INHERITANCE_CHAIN, we're looking at diamond
10613 : inheritance; for now do a normal virtual call in that case. */
10614 1209 : tree octx = DECL_CONTEXT (ov);
10615 1209 : tree obinfo = base_binfo;
10616 2505 : while (obinfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (obinfo), octx))
10617 45 : obinfo = BINFO_INHERITANCE_CHAIN (obinfo);
10618 1209 : if (obinfo)
10619 : {
10620 1206 : fn = ov;
10621 1206 : base_binfo = obinfo;
10622 1206 : flags |= LOOKUP_NONVIRTUAL;
10623 : }
10624 : }
10625 :
10626 46010608 : tree converted_arg = build_base_path (PLUS_EXPR, arg,
10627 : base_binfo, 1, complain);
10628 :
10629 46010608 : argarray[argarray_size++] = converted_arg;
10630 46010608 : parm = TREE_CHAIN (parm);
10631 46010608 : if (parmd)
10632 46010608 : parmd = DECL_CHAIN (parmd);
10633 : }
10634 :
10635 305589889 : auto handle_arg = [fn, flags](tree type,
10636 : tree arg,
10637 : int const param_index,
10638 : conversion *conv,
10639 : tsubst_flags_t const arg_complain)
10640 : {
10641 : /* Set user_conv_p on the argument conversions, so rvalue/base handling
10642 : knows not to allow any more UDCs. This needs to happen after we
10643 : process cand->warnings. */
10644 134311197 : if (flags & LOOKUP_NO_CONVERSION)
10645 3654024 : conv->user_conv_p = true;
10646 :
10647 134311197 : if (arg_complain & tf_warning)
10648 97821154 : maybe_warn_pessimizing_move (arg, type, /*return_p=*/false);
10649 :
10650 134311197 : tree val = convert_like_with_context (conv, arg, fn,
10651 : param_index, arg_complain);
10652 134311197 : val = convert_for_arg_passing (type, val, arg_complain);
10653 134311197 : return val;
10654 171278692 : };
10655 :
10656 306691412 : auto handle_indeterminate_arg = [](tree parmd, tree val)
10657 : {
10658 135412720 : if (parmd
10659 135412720 : && lookup_attribute (NULL, "indeterminate", DECL_ATTRIBUTES (parmd)))
10660 : {
10661 48 : STRIP_NOPS (val);
10662 48 : if (TREE_CODE (val) == ADDR_EXPR
10663 48 : && TREE_CODE (TREE_OPERAND (val, 0)) == TARGET_EXPR)
10664 : {
10665 48 : val = TARGET_EXPR_SLOT (TREE_OPERAND (val, 0));
10666 48 : if (auto_var_p (val) && DECL_ARTIFICIAL (val))
10667 : {
10668 48 : tree id = get_identifier ("indeterminate");
10669 48 : DECL_ATTRIBUTES (val)
10670 96 : = tree_cons (build_tree_list (NULL_TREE, id), NULL_TREE,
10671 48 : DECL_ATTRIBUTES (val));
10672 : }
10673 : }
10674 : }
10675 135412720 : };
10676 :
10677 171278692 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
10678 : {
10679 9242 : gcc_assert (cand->num_convs > 0);
10680 9242 : tree object_arg = consume_object_arg ();
10681 9242 : val = handle_arg (TREE_VALUE (parm),
10682 : object_arg,
10683 : param_index++,
10684 9242 : convs[conv_index++],
10685 : complain);
10686 :
10687 9242 : if (val == error_mark_node)
10688 : return error_mark_node;
10689 : else
10690 : {
10691 9074 : argarray[argarray_size++] = val;
10692 9074 : handle_indeterminate_arg (parmd, val);
10693 : }
10694 9074 : parm = TREE_CHAIN (parm);
10695 9074 : if (parmd)
10696 9074 : parmd = DECL_CHAIN (parmd);
10697 : }
10698 :
10699 171278524 : gcc_assert (first_arg == NULL_TREE);
10700 305578653 : for (; arg_index < vec_safe_length (args) && parm;
10701 134300129 : parm = TREE_CHAIN (parm), ++arg_index, ++param_index, ++conv_index)
10702 : {
10703 134301955 : tree current_arg = (*args)[arg_index];
10704 :
10705 : /* If the argument is NULL and used to (implicitly) instantiate a
10706 : template function (and bind one of the template arguments to
10707 : the type of 'long int'), we don't want to warn about passing NULL
10708 : to non-pointer argument.
10709 : For example, if we have this template function:
10710 :
10711 : template<typename T> void func(T x) {}
10712 :
10713 : we want to warn (when -Wconversion is enabled) in this case:
10714 :
10715 : void foo() {
10716 : func<int>(NULL);
10717 : }
10718 :
10719 : but not in this case:
10720 :
10721 : void foo() {
10722 : func(NULL);
10723 : }
10724 : */
10725 134301955 : bool conversion_warning = !(null_node_p (current_arg)
10726 46926 : && DECL_TEMPLATE_INFO (fn)
10727 61 : && cand->template_decl
10728 30 : && !cand->explicit_targs);
10729 :
10730 : /* Also don't warn about (x <=> y) < 0 (c++/100903). */
10731 134301940 : if (conversion_warning
10732 134301940 : && integer_zerop (current_arg)
10733 10377186 : && warn_zero_as_null_pointer_constant
10734 195 : && !warning_enabled_at (DECL_SOURCE_LOCATION (fn),
10735 195 : OPT_Wzero_as_null_pointer_constant))
10736 : conversion_warning = false;
10737 :
10738 135 : tsubst_flags_t const arg_complain
10739 134301835 : = conversion_warning ? complain : complain & ~tf_warning;
10740 :
10741 134301955 : val = handle_arg (TREE_VALUE (parm),
10742 : current_arg,
10743 : param_index,
10744 134301955 : convs[conv_index],
10745 : arg_complain);
10746 :
10747 134301955 : if (val == error_mark_node)
10748 : return error_mark_node;
10749 : else
10750 : {
10751 134300129 : argarray[argarray_size++] = val;
10752 134300129 : handle_indeterminate_arg (parmd, val);
10753 : }
10754 134300129 : if (parmd)
10755 123330147 : parmd = DECL_CHAIN (parmd);
10756 : }
10757 :
10758 : /* Default arguments */
10759 172380215 : for (; parm && parm != void_list_node;
10760 1103517 : parm = TREE_CHAIN (parm), param_index++)
10761 : {
10762 1103638 : if (TREE_VALUE (parm) == error_mark_node)
10763 : return error_mark_node;
10764 2207252 : val = convert_default_arg (TREE_VALUE (parm),
10765 1103626 : TREE_PURPOSE (parm),
10766 : fn, param_index,
10767 : complain);
10768 1103626 : if (val == error_mark_node)
10769 : return error_mark_node;
10770 1103517 : argarray[argarray_size++] = val;
10771 1103517 : handle_indeterminate_arg (parmd, val);
10772 1103517 : if (parmd)
10773 1103517 : parmd = DECL_CHAIN (parmd);
10774 : }
10775 :
10776 : /* Ellipsis */
10777 171276577 : int magic = magic_varargs_p (fn);
10778 174266113 : for (; arg_index < vec_safe_length (args); ++arg_index)
10779 : {
10780 2989557 : tree a = (*args)[arg_index];
10781 2989557 : if ((magic == 3 && arg_index == 2) || (magic == 4 && arg_index == 0))
10782 : {
10783 : /* Do no conversions for certain magic varargs. */
10784 120846 : a = mark_type_use (a);
10785 120846 : if (TREE_CODE (a) == FUNCTION_DECL && reject_gcc_builtin (a))
10786 0 : return error_mark_node;
10787 : }
10788 2868711 : else if (magic != 0)
10789 : {
10790 : /* Don't truncate excess precision to the semantic type. */
10791 1448035 : if (magic == 1 && TREE_CODE (a) == EXCESS_PRECISION_EXPR)
10792 84 : a = TREE_OPERAND (a, 0);
10793 : /* For other magic varargs only do decay_conversion. */
10794 1448035 : a = decay_conversion (a, complain);
10795 : }
10796 1420676 : else if (DECL_CONSTRUCTOR_P (fn)
10797 387 : && vec_safe_length (args) == 1
10798 1420877 : && same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (fn),
10799 201 : TREE_TYPE (a)))
10800 : {
10801 : /* Avoid infinite recursion trying to call A(...). */
10802 3 : if (complain & tf_error)
10803 : /* Try to call the actual copy constructor for a good error. */
10804 3 : call_copy_ctor (a, complain);
10805 3 : return error_mark_node;
10806 : }
10807 : else
10808 1420673 : a = convert_arg_to_ellipsis (a, complain);
10809 2989554 : if (a == error_mark_node)
10810 : return error_mark_node;
10811 2989536 : argarray[argarray_size++] = a;
10812 : }
10813 :
10814 171276556 : gcc_assert (argarray_size <= nargs);
10815 171276556 : nargs = argarray_size;
10816 171276556 : icip.reset ();
10817 :
10818 : /* Avoid performing argument transformation if warnings are disabled.
10819 : When tf_warning is set and at least one of the warnings is active
10820 : the check_function_arguments function might warn about something. */
10821 :
10822 171276556 : bool warned_p = false;
10823 171276556 : if ((complain & tf_warning)
10824 111320316 : && (warn_nonnull
10825 109363582 : || warn_format
10826 109363579 : || warn_suggest_attribute_format
10827 109363483 : || warn_restrict))
10828 : {
10829 1959080 : tree *fargs = (!nargs ? argarray
10830 1618289 : : (tree *) alloca (nargs * sizeof (tree)));
10831 5960078 : for (int j = 0; j < nargs; j++)
10832 : {
10833 : /* For -Wformat undo the implicit passing by hidden reference
10834 : done by convert_arg_to_ellipsis. */
10835 4000998 : if (TREE_CODE (argarray[j]) == ADDR_EXPR
10836 4000998 : && TYPE_REF_P (TREE_TYPE (argarray[j])))
10837 1498 : fargs[j] = TREE_OPERAND (argarray[j], 0);
10838 : else
10839 3999500 : fargs[j] = argarray[j];
10840 : }
10841 :
10842 1959080 : warned_p = check_function_arguments (input_location, fn, TREE_TYPE (fn),
10843 : nargs, fargs, NULL,
10844 : cp_comp_parm_types);
10845 : }
10846 :
10847 342553112 : if (DECL_INHERITED_CTOR (fn))
10848 : {
10849 : /* Check for passing ellipsis arguments to an inherited constructor. We
10850 : could handle this by open-coding the inherited constructor rather than
10851 : defining it, but let's not bother now. */
10852 63763 : if (!cp_unevaluated_operand
10853 63340 : && cand->num_convs
10854 63340 : && cand->convs[cand->num_convs-1]->ellipsis_p)
10855 : {
10856 15 : if (complain & tf_error)
10857 : {
10858 15 : auto_diagnostic_group d;
10859 15 : sorry ("passing arguments to ellipsis of inherited constructor "
10860 : "%qD", cand->fn);
10861 15 : inform (DECL_SOURCE_LOCATION (cand->fn), "declared here");
10862 15 : }
10863 15 : return error_mark_node;
10864 : }
10865 :
10866 : /* A base constructor inheriting from a virtual base doesn't get the
10867 : inherited arguments, just this and __vtt. */
10868 63748 : if (ctor_omit_inherited_parms (fn))
10869 171276541 : nargs = 2;
10870 : }
10871 :
10872 : /* Avoid actually calling copy constructors and copy assignment operators,
10873 : if possible. */
10874 :
10875 171276541 : if (!force_elide
10876 171142829 : && (!flag_elide_constructors
10877 : /* It's unsafe to elide the operation when handling
10878 : a noexcept-expression, it may evaluate to the wrong
10879 : value (c++/53025, c++/96090). */
10880 171142625 : || cp_noexcept_operand != 0))
10881 : /* Do things the hard way. */;
10882 168552266 : else if (cand->num_convs == 1
10883 257454276 : && (DECL_COPY_CONSTRUCTOR_P (fn)
10884 167237992 : || DECL_MOVE_CONSTRUCTOR_P (fn)))
10885 : {
10886 8073036 : tree targ;
10887 8073036 : tree arg = argarray[num_artificial_parms_for (fn)];
10888 8073036 : tree fa = argarray[0];
10889 8073036 : bool trivial = trivial_fn_p (fn);
10890 :
10891 : /* Pull out the real argument, disregarding const-correctness. */
10892 8073036 : targ = arg;
10893 : /* Strip the reference binding for the constructor parameter. */
10894 0 : if (CONVERT_EXPR_P (targ)
10895 8073036 : && TYPE_REF_P (TREE_TYPE (targ)))
10896 8073036 : targ = TREE_OPERAND (targ, 0);
10897 : /* But don't strip any other reference bindings; binding a temporary to a
10898 : reference prevents copy elision. */
10899 13381964 : while ((CONVERT_EXPR_P (targ)
10900 11253466 : && !TYPE_REF_P (TREE_TYPE (targ)))
10901 28043131 : || TREE_CODE (targ) == NON_LVALUE_EXPR)
10902 10079339 : targ = TREE_OPERAND (targ, 0);
10903 8073036 : if (TREE_CODE (targ) == ADDR_EXPR)
10904 : {
10905 2435161 : targ = TREE_OPERAND (targ, 0);
10906 2435161 : if (!same_type_ignoring_top_level_qualifiers_p
10907 2435161 : (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
10908 : targ = NULL_TREE;
10909 : }
10910 : else
10911 : targ = NULL_TREE;
10912 :
10913 2434709 : if (targ)
10914 : arg = targ;
10915 : else
10916 5638327 : arg = cp_build_fold_indirect_ref (arg);
10917 :
10918 : /* In C++17 we shouldn't be copying a TARGET_EXPR except into a
10919 : potentially-overlapping subobject. */
10920 8073036 : if (CHECKING_P && cxx_dialect >= cxx17)
10921 7996680 : gcc_assert (TREE_CODE (arg) != TARGET_EXPR
10922 : || force_elide
10923 : /* It's from binding the ref parm to a packed field. */
10924 : || convs[0]->need_temporary_p
10925 : || seen_error ()
10926 : /* See unsafe_copy_elision_p. */
10927 : || unsafe_return_slot_p (fa));
10928 :
10929 8073036 : bool unsafe = unsafe_copy_elision_p_opt (fa, arg);
10930 8073036 : bool eliding_temp = (TREE_CODE (arg) == TARGET_EXPR && !unsafe);
10931 :
10932 : /* [class.copy]: the copy constructor is implicitly defined even if the
10933 : implementation elided its use. But don't warn about deprecation when
10934 : eliding a temporary, as then no copy is actually performed. */
10935 8073036 : warning_sentinel s (warn_deprecated_copy, eliding_temp);
10936 8073036 : if (force_elide)
10937 : /* The language says this isn't called. */;
10938 7939324 : else if (!trivial)
10939 : {
10940 1337093 : if (!mark_used (fn, complain) && !(complain & tf_error))
10941 0 : return error_mark_node;
10942 : already_used = true;
10943 : }
10944 : else
10945 6602231 : cp_handle_deprecated_or_unavailable (fn, complain);
10946 :
10947 194076 : if (eliding_temp && DECL_BASE_CONSTRUCTOR_P (fn)
10948 8073229 : && !make_base_init_ok (arg))
10949 : unsafe = true;
10950 :
10951 : /* If we're creating a temp and we already have one, don't create a
10952 : new one. If we're not creating a temp but we get one, use
10953 : INIT_EXPR to collapse the temp into our target. Otherwise, if the
10954 : ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
10955 : temp or an INIT_EXPR otherwise. */
10956 8073036 : if (is_dummy_object (fa))
10957 : {
10958 5944520 : if (TREE_CODE (arg) == TARGET_EXPR)
10959 : return arg;
10960 5781332 : else if (trivial)
10961 5221853 : return force_target_expr (DECL_CONTEXT (fn), arg, complain);
10962 : }
10963 2128516 : else if ((trivial || TREE_CODE (arg) == TARGET_EXPR)
10964 1357207 : && !unsafe)
10965 : {
10966 1357113 : tree to = cp_build_fold_indirect_ref (fa);
10967 1357113 : val = cp_build_init_expr (to, arg);
10968 1357113 : return val;
10969 : }
10970 8073036 : }
10971 160479230 : else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
10972 3136489 : && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR)
10973 162601978 : && trivial_fn_p (fn))
10974 : {
10975 1492181 : tree to = cp_build_fold_indirect_ref (argarray[0]);
10976 1492181 : tree type = TREE_TYPE (to);
10977 1492181 : tree as_base = CLASSTYPE_AS_BASE (type);
10978 1492181 : tree arg = argarray[1];
10979 1492181 : location_t loc = cp_expr_loc_or_input_loc (arg);
10980 :
10981 1492181 : if (is_really_empty_class (type, /*ignore_vptr*/true))
10982 : {
10983 : /* Avoid copying empty classes, but ensure op= returns an lvalue even
10984 : if the object argument isn't one. */
10985 236117 : to = force_lvalue (to, complain);
10986 236117 : val = build2 (COMPOUND_EXPR, type, arg, to);
10987 236117 : suppress_warning (val, OPT_Wunused);
10988 : }
10989 1256064 : else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
10990 : {
10991 967520 : if (is_std_init_list (type)
10992 967520 : && conv_binds_ref_to_prvalue (convs[1]))
10993 3 : warning_at (loc, OPT_Winit_list_lifetime,
10994 : "assignment from temporary %<initializer_list%> does "
10995 : "not extend the lifetime of the underlying array");
10996 967520 : arg = cp_build_fold_indirect_ref (arg);
10997 967520 : val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
10998 : }
10999 : else
11000 : {
11001 : /* We must only copy the non-tail padding parts. */
11002 288544 : tree arg0, arg2, t;
11003 288544 : tree array_type, alias_set;
11004 :
11005 288544 : arg2 = TYPE_SIZE_UNIT (as_base);
11006 : /* Ensure op= returns an lvalue even if the object argument isn't
11007 : one. */
11008 288544 : to = force_lvalue (to, complain);
11009 288544 : to = cp_stabilize_reference (to);
11010 288544 : arg0 = cp_build_addr_expr (to, complain);
11011 :
11012 288544 : array_type = build_array_type (unsigned_char_type_node,
11013 : build_index_type
11014 : (size_binop (MINUS_EXPR,
11015 : arg2, size_int (1))));
11016 288544 : alias_set = build_int_cst (build_pointer_type (type), 0);
11017 288544 : t = build2 (MODIFY_EXPR, void_type_node,
11018 : build2 (MEM_REF, array_type, arg0, alias_set),
11019 : build2 (MEM_REF, array_type, arg, alias_set));
11020 288544 : val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
11021 288544 : suppress_warning (val, OPT_Wunused);
11022 : }
11023 :
11024 1492181 : cp_handle_deprecated_or_unavailable (fn, complain);
11025 :
11026 1492181 : return val;
11027 : }
11028 158987049 : else if (trivial_fn_p (fn))
11029 : {
11030 7821916 : if (DECL_DESTRUCTOR_P (fn))
11031 3133619 : return build_trivial_dtor_call (argarray[0]);
11032 777339 : else if (default_ctor_p (fn))
11033 : {
11034 777339 : if (is_dummy_object (argarray[0]))
11035 305649 : return force_target_expr (DECL_CONTEXT (fn), void_node,
11036 305649 : no_cleanup_complain);
11037 : else
11038 471690 : return cp_build_fold_indirect_ref (argarray[0]);
11039 : }
11040 : }
11041 :
11042 159131248 : gcc_assert (!force_elide);
11043 :
11044 159131248 : if (!already_used
11045 159131248 : && !mark_used (fn, complain))
11046 791 : return error_mark_node;
11047 :
11048 : /* Warn if the built-in writes to an object of a non-trivial type. */
11049 159130454 : if (warn_class_memaccess
11050 2016853 : && vec_safe_length (args) >= 2
11051 160092761 : && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
11052 69427 : maybe_warn_class_memaccess (input_location, fn, args);
11053 :
11054 159130454 : if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
11055 : {
11056 492687 : tree t;
11057 492687 : tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
11058 492687 : DECL_CONTEXT (fn),
11059 : ba_any, NULL, complain);
11060 492687 : gcc_assert (binfo && binfo != error_mark_node);
11061 :
11062 492687 : argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
11063 : complain);
11064 492687 : if (TREE_SIDE_EFFECTS (argarray[0]))
11065 46472 : argarray[0] = save_expr (argarray[0]);
11066 492687 : t = build_pointer_type (TREE_TYPE (fn));
11067 492687 : fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
11068 492687 : TREE_TYPE (fn) = t;
11069 : }
11070 : else
11071 : {
11072 : /* If FN is marked deprecated or unavailable, then we've already
11073 : issued a diagnostic from mark_used above, so avoid redundantly
11074 : issuing another one from build_addr_func. */
11075 158637767 : auto w = make_temp_override (deprecated_state,
11076 158637767 : UNAVAILABLE_DEPRECATED_SUPPRESS);
11077 :
11078 158637767 : fn = build_addr_func (fn, complain);
11079 158637767 : if (fn == error_mark_node)
11080 0 : return error_mark_node;
11081 :
11082 : /* We're actually invoking the function. (Immediate functions get an
11083 : & when invoking it even though the user didn't use &.) */
11084 158637767 : ADDR_EXPR_DENOTES_CALL_P (fn) = true;
11085 158637767 : }
11086 :
11087 159130454 : tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag);
11088 159130454 : if (call == error_mark_node)
11089 : return call;
11090 159129480 : if (cand->flags & LOOKUP_LIST_INIT_CTOR)
11091 : {
11092 2010229 : tree c = extract_call_expr (call);
11093 : /* build_new_op will clear this when appropriate. */
11094 2010229 : CALL_EXPR_ORDERED_ARGS (c) = true;
11095 : }
11096 159129480 : if (warned_p)
11097 : {
11098 253 : tree c = extract_call_expr (call);
11099 253 : if (TREE_CODE (c) == CALL_EXPR)
11100 253 : suppress_warning (c /* Suppress all warnings. */);
11101 : }
11102 159129227 : else if (TREE_DEPRECATED (fn)
11103 159129227 : && warning_suppressed_at (input_location,
11104 : OPT_Wdeprecated_declarations))
11105 : {
11106 0 : tree c = extract_call_expr (call);
11107 0 : if (TREE_CODE (c) == CALL_EXPR)
11108 0 : TREE_NO_WARNING (c) = true;
11109 : }
11110 :
11111 : return call;
11112 : }
11113 :
11114 : namespace
11115 : {
11116 :
11117 : /* Return the DECL of the first non-static subobject of class TYPE
11118 : that satisfies the predicate PRED or null if none can be found. */
11119 :
11120 : template <class Predicate>
11121 : tree
11122 3556 : first_non_static_field (tree type, Predicate pred)
11123 : {
11124 3556 : if (!type || !CLASS_TYPE_P (type))
11125 : return NULL_TREE;
11126 :
11127 71398 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
11128 : {
11129 68436 : if (TREE_CODE (field) != FIELD_DECL)
11130 43330 : continue;
11131 25106 : if (TREE_STATIC (field))
11132 0 : continue;
11133 25106 : if (pred (field))
11134 : return field;
11135 : }
11136 :
11137 2962 : int i = 0;
11138 :
11139 3082 : for (tree base_binfo, binfo = TYPE_BINFO (type);
11140 3082 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11141 : {
11142 126 : tree base = TREE_TYPE (base_binfo);
11143 126 : if (pred (base))
11144 : return base;
11145 120 : if (tree field = first_non_static_field (base, pred))
11146 : return field;
11147 : }
11148 :
11149 : return NULL_TREE;
11150 : }
11151 :
11152 : struct NonPublicField
11153 : {
11154 24356 : bool operator() (const_tree t) const
11155 : {
11156 24356 : return DECL_P (t) && (TREE_PRIVATE (t) || TREE_PROTECTED (t));
11157 : }
11158 : };
11159 :
11160 : /* Return the DECL of the first non-public subobject of class TYPE
11161 : or null if none can be found. */
11162 :
11163 : static inline tree
11164 3262 : first_non_public_field (tree type)
11165 : {
11166 3262 : return first_non_static_field (type, NonPublicField ());
11167 : }
11168 :
11169 : struct NonTrivialField
11170 : {
11171 876 : bool operator() (const_tree t) const
11172 : {
11173 876 : return !trivial_type_p (DECL_P (t) ? TREE_TYPE (t) : t);
11174 : }
11175 : };
11176 :
11177 : /* Return the DECL of the first non-trivial subobject of class TYPE
11178 : or null if none can be found. */
11179 :
11180 : static inline tree
11181 174 : first_non_trivial_field (tree type)
11182 : {
11183 174 : return first_non_static_field (type, NonTrivialField ());
11184 : }
11185 :
11186 : } /* unnamed namespace */
11187 :
11188 : /* Return true if all copy and move assignment operator overloads for
11189 : class TYPE are trivial and at least one of them is not deleted and,
11190 : when ACCESS is set, accessible. Return false otherwise. Set
11191 : HASASSIGN to true when the TYPE has a (not necessarily trivial)
11192 : copy or move assignment. */
11193 :
11194 : static bool
11195 5021 : has_trivial_copy_assign_p (tree type, bool access, bool *hasassign)
11196 : {
11197 5021 : tree fns = get_class_binding (type, assign_op_identifier);
11198 5021 : bool all_trivial = true;
11199 :
11200 : /* Iterate over overloads of the assignment operator, checking
11201 : accessible copy assignments for triviality. */
11202 :
11203 16515 : for (tree f : ovl_range (fns))
11204 : {
11205 : /* Skip operators that aren't copy assignments. */
11206 8571 : if (!copy_fn_p (f))
11207 3046 : continue;
11208 :
11209 5525 : bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
11210 5813 : || accessible_p (TYPE_BINFO (type), f, true));
11211 :
11212 : /* Skip template assignment operators and deleted functions. */
11213 5525 : if (TREE_CODE (f) != FUNCTION_DECL || DECL_DELETED_FN (f))
11214 694 : continue;
11215 :
11216 4831 : if (accessible)
11217 4579 : *hasassign = true;
11218 :
11219 4579 : if (!accessible || !trivial_fn_p (f))
11220 : all_trivial = false;
11221 :
11222 : /* Break early when both properties have been determined. */
11223 4831 : if (*hasassign && !all_trivial)
11224 : break;
11225 : }
11226 :
11227 : /* Return true if they're all trivial and one of the expressions
11228 : TYPE() = TYPE() or TYPE() = (TYPE&)() is valid. */
11229 5021 : tree ref = cp_build_reference_type (type, false);
11230 5021 : return (all_trivial
11231 5021 : && (is_trivially_xible (MODIFY_EXPR, type, type)
11232 333 : || is_trivially_xible (MODIFY_EXPR, type, ref)));
11233 : }
11234 :
11235 : /* Return true if all copy and move ctor overloads for class TYPE are
11236 : trivial and at least one of them is not deleted and, when ACCESS is
11237 : set, accessible. Return false otherwise. Set each element of HASCTOR[]
11238 : to true when the TYPE has a (not necessarily trivial) default and copy
11239 : (or move) ctor, respectively. */
11240 :
11241 : static bool
11242 5021 : has_trivial_copy_p (tree type, bool access, bool hasctor[2])
11243 : {
11244 5021 : tree fns = get_class_binding (type, complete_ctor_identifier);
11245 5021 : bool all_trivial = true;
11246 :
11247 25141 : for (tree f : ovl_range (fns))
11248 : {
11249 : /* Skip template constructors. */
11250 12602 : if (TREE_CODE (f) != FUNCTION_DECL)
11251 105 : continue;
11252 :
11253 12497 : bool cpy_or_move_ctor_p = copy_fn_p (f);
11254 :
11255 : /* Skip ctors other than default, copy, and move. */
11256 12497 : if (!cpy_or_move_ctor_p && !default_ctor_p (f))
11257 2757 : continue;
11258 :
11259 9740 : if (DECL_DELETED_FN (f))
11260 306 : continue;
11261 :
11262 9434 : bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
11263 9626 : || accessible_p (TYPE_BINFO (type), f, true));
11264 :
11265 9314 : if (accessible)
11266 9314 : hasctor[cpy_or_move_ctor_p] = true;
11267 :
11268 9434 : if (cpy_or_move_ctor_p && (!accessible || !trivial_fn_p (f)))
11269 : all_trivial = false;
11270 :
11271 : /* Break early when both properties have been determined. */
11272 9434 : if (hasctor[0] && hasctor[1] && !all_trivial)
11273 : break;
11274 : }
11275 :
11276 5021 : return all_trivial;
11277 : }
11278 :
11279 : /* Issue a warning on a call to the built-in function FNDECL if it is
11280 : a raw memory write whose destination is not an object of (something
11281 : like) trivial or standard layout type with a non-deleted assignment
11282 : and copy ctor. Detects const correctness violations, corrupting
11283 : references, virtual table pointers, and bypassing non-trivial
11284 : assignments. */
11285 :
11286 : static void
11287 69427 : maybe_warn_class_memaccess (location_t loc, tree fndecl,
11288 : const vec<tree, va_gc> *args)
11289 : {
11290 : /* Except for bcopy where it's second, the destination pointer is
11291 : the first argument for all functions handled here. Compute
11292 : the index of the destination and source arguments. */
11293 69427 : unsigned dstidx = DECL_FUNCTION_CODE (fndecl) == BUILT_IN_BCOPY;
11294 69427 : unsigned srcidx = !dstidx;
11295 :
11296 69427 : tree dest = (*args)[dstidx];
11297 69427 : if (!TREE_TYPE (dest)
11298 69427 : || (TREE_CODE (TREE_TYPE (dest)) != ARRAY_TYPE
11299 68253 : && !INDIRECT_TYPE_P (TREE_TYPE (dest))))
11300 66052 : return;
11301 :
11302 22218 : tree srctype = NULL_TREE;
11303 :
11304 : /* Determine the type of the pointed-to object and whether it's
11305 : a complete class type. */
11306 22218 : tree desttype = TREE_TYPE (TREE_TYPE (dest));
11307 :
11308 22218 : if (!desttype || !COMPLETE_TYPE_P (desttype) || !CLASS_TYPE_P (desttype))
11309 : return;
11310 :
11311 : /* Check to see if the raw memory call is made by a non-static member
11312 : function with THIS as the destination argument for the destination
11313 : type. If so, and if the class has no non-trivial bases or members,
11314 : be more permissive. */
11315 5123 : if (current_function_decl
11316 5123 : && DECL_OBJECT_MEMBER_FUNCTION_P (current_function_decl)
11317 5390 : && is_object_parameter (tree_strip_nop_conversions (dest)))
11318 : {
11319 204 : tree ctx = DECL_CONTEXT (current_function_decl);
11320 204 : bool special = same_type_ignoring_top_level_qualifiers_p (ctx, desttype);
11321 204 : tree binfo = TYPE_BINFO (ctx);
11322 :
11323 204 : if (special
11324 204 : && !BINFO_VTABLE (binfo)
11325 378 : && !first_non_trivial_field (desttype))
11326 : return;
11327 : }
11328 :
11329 : /* True if the class is trivial. */
11330 5021 : bool trivial = trivial_type_p (desttype);
11331 :
11332 : /* Set to true if DESTYPE has an accessible copy assignment. */
11333 5021 : bool hasassign = false;
11334 : /* True if all of the class' overloaded copy assignment operators
11335 : are all trivial (and not deleted) and at least one of them is
11336 : accessible. */
11337 5021 : bool trivassign = has_trivial_copy_assign_p (desttype, true, &hasassign);
11338 :
11339 : /* Set to true if DESTTYPE has an accessible default and copy ctor,
11340 : respectively. */
11341 5021 : bool hasctors[2] = { false, false };
11342 :
11343 : /* True if all of the class' overloaded copy constructors are all
11344 : trivial (and not deleted) and at least one of them is accessible. */
11345 5021 : bool trivcopy = has_trivial_copy_p (desttype, true, hasctors);
11346 :
11347 : /* Set FLD to the first private/protected member of the class. */
11348 5021 : tree fld = trivial ? first_non_public_field (desttype) : NULL_TREE;
11349 :
11350 : /* The warning format string. */
11351 5021 : const char *warnfmt = NULL;
11352 : /* A suggested alternative to offer instead of the raw memory call.
11353 : Empty string when none can be come up with. */
11354 5021 : const char *suggest = "";
11355 5021 : bool warned = false;
11356 :
11357 5021 : auto_diagnostic_group d;
11358 5021 : switch (DECL_FUNCTION_CODE (fndecl))
11359 : {
11360 560 : case BUILT_IN_MEMSET:
11361 560 : if (!integer_zerop (maybe_constant_value ((*args)[1])))
11362 : {
11363 : /* Diagnose setting non-copy-assignable or non-trivial types,
11364 : or types with a private member, to (potentially) non-zero
11365 : bytes. Since the value of the bytes being written is unknown,
11366 : suggest using assignment instead (if one exists). Also warn
11367 : for writes into objects for which zero-initialization doesn't
11368 : mean all bits clear (pointer-to-member data, where null is all
11369 : bits set). Since the value being written is (most likely)
11370 : non-zero, simply suggest assignment (but not copy assignment). */
11371 196 : suggest = "; use assignment instead";
11372 196 : if (!trivassign)
11373 : warnfmt = G_("%qD writing to an object of type %#qT with "
11374 : "no trivial copy-assignment");
11375 125 : else if (!trivial)
11376 : warnfmt = G_("%qD writing to an object of non-trivial type %#qT%s");
11377 85 : else if (fld)
11378 : {
11379 24 : const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
11380 24 : warned = warning_at (loc, OPT_Wclass_memaccess,
11381 : "%qD writing to an object of type %#qT with "
11382 : "%qs member %qD",
11383 : fndecl, desttype, access, fld);
11384 : }
11385 61 : else if (!zero_init_p (desttype))
11386 : warnfmt = G_("%qD writing to an object of type %#qT containing "
11387 : "a pointer to data member%s");
11388 :
11389 : break;
11390 : }
11391 : /* Fall through. */
11392 :
11393 481 : case BUILT_IN_BZERO:
11394 : /* Similarly to the above, diagnose clearing non-trivial or non-
11395 : standard layout objects, or objects of types with no assignmenmt.
11396 : Since the value being written is known to be zero, suggest either
11397 : copy assignment, copy ctor, or default ctor as an alternative,
11398 : depending on what's available. */
11399 :
11400 481 : if (hasassign && hasctors[0])
11401 : suggest = G_("; use assignment or value-initialization instead");
11402 49 : else if (hasassign)
11403 : suggest = G_("; use assignment instead");
11404 31 : else if (hasctors[0])
11405 16 : suggest = G_("; use value-initialization instead");
11406 :
11407 481 : if (!trivassign)
11408 : warnfmt = G_("%qD clearing an object of type %#qT with "
11409 : "no trivial copy-assignment%s");
11410 374 : else if (!trivial)
11411 : warnfmt = G_("%qD clearing an object of non-trivial type %#qT%s");
11412 304 : else if (!zero_init_p (desttype))
11413 : warnfmt = G_("%qD clearing an object of type %#qT containing "
11414 : "a pointer-to-member%s");
11415 : break;
11416 :
11417 2437 : case BUILT_IN_BCOPY:
11418 2437 : case BUILT_IN_MEMCPY:
11419 2437 : case BUILT_IN_MEMMOVE:
11420 2437 : case BUILT_IN_MEMPCPY:
11421 : /* Determine the type of the source object. */
11422 2437 : srctype = TREE_TYPE ((*args)[srcidx]);
11423 2437 : if (!srctype || !INDIRECT_TYPE_P (srctype))
11424 0 : srctype = void_type_node;
11425 : else
11426 2437 : srctype = TREE_TYPE (srctype);
11427 :
11428 : /* Since it's impossible to determine whether the byte copy is
11429 : being used in place of assignment to an existing object or
11430 : as a substitute for initialization, assume it's the former.
11431 : Determine the best alternative to use instead depending on
11432 : what's not deleted. */
11433 2437 : if (hasassign && hasctors[1])
11434 : suggest = G_("; use copy-assignment or copy-initialization instead");
11435 488 : else if (hasassign)
11436 : suggest = G_("; use copy-assignment instead");
11437 341 : else if (hasctors[1])
11438 290 : suggest = G_("; use copy-initialization instead");
11439 :
11440 2437 : if (!trivassign)
11441 : warnfmt = G_("%qD writing to an object of type %#qT with no trivial "
11442 : "copy-assignment%s");
11443 1639 : else if (!trivially_copyable_p (desttype))
11444 : warnfmt = G_("%qD writing to an object of non-trivially copyable "
11445 : "type %#qT%s");
11446 1315 : else if (!trivcopy)
11447 : warnfmt = G_("%qD writing to an object with a deleted copy constructor");
11448 :
11449 1315 : else if (!trivial
11450 211 : && !VOID_TYPE_P (srctype)
11451 160 : && !is_byte_access_type (srctype)
11452 1412 : && !same_type_ignoring_top_level_qualifiers_p (desttype,
11453 : srctype))
11454 : {
11455 : /* Warn when copying into a non-trivial object from an object
11456 : of a different type other than void or char. */
11457 54 : warned = warning_at (loc, OPT_Wclass_memaccess,
11458 : "%qD copying an object of non-trivial type "
11459 : "%#qT from an array of %#qT",
11460 : fndecl, desttype, srctype);
11461 : }
11462 1261 : else if (fld
11463 432 : && !VOID_TYPE_P (srctype)
11464 384 : && !is_byte_access_type (srctype)
11465 1549 : && !same_type_ignoring_top_level_qualifiers_p (desttype,
11466 : srctype))
11467 : {
11468 216 : const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
11469 216 : warned = warning_at (loc, OPT_Wclass_memaccess,
11470 : "%qD copying an object of type %#qT with "
11471 : "%qs member %qD from an array of %#qT; use "
11472 : "assignment or copy-initialization instead",
11473 : fndecl, desttype, access, fld, srctype);
11474 : }
11475 1045 : else if (!trivial && vec_safe_length (args) > 2)
11476 : {
11477 157 : tree sz = maybe_constant_value ((*args)[2]);
11478 157 : if (!tree_fits_uhwi_p (sz))
11479 : break;
11480 :
11481 : /* Finally, warn on partial copies. */
11482 99 : unsigned HOST_WIDE_INT typesize
11483 99 : = tree_to_uhwi (TYPE_SIZE_UNIT (desttype));
11484 99 : if (typesize == 0)
11485 : break;
11486 96 : if (unsigned HOST_WIDE_INT partial = tree_to_uhwi (sz) % typesize)
11487 12 : warned = warning_at (loc, OPT_Wclass_memaccess,
11488 : (typesize - partial > 1
11489 : ? G_("%qD writing to an object of "
11490 : "a non-trivial type %#qT leaves %wu "
11491 : "bytes unchanged")
11492 : : G_("%qD writing to an object of "
11493 : "a non-trivial type %#qT leaves %wu "
11494 : "byte unchanged")),
11495 : fndecl, desttype, typesize - partial);
11496 : }
11497 : break;
11498 :
11499 261 : case BUILT_IN_REALLOC:
11500 :
11501 261 : if (!trivially_copyable_p (desttype))
11502 : warnfmt = G_("%qD moving an object of non-trivially copyable type "
11503 : "%#qT; use %<new%> and %<delete%> instead");
11504 138 : else if (!trivcopy)
11505 : warnfmt = G_("%qD moving an object of type %#qT with deleted copy "
11506 : "constructor; use %<new%> and %<delete%> instead");
11507 135 : else if (!get_dtor (desttype, tf_none))
11508 : warnfmt = G_("%qD moving an object of type %#qT with deleted "
11509 : "destructor");
11510 126 : else if (!trivial)
11511 : {
11512 33 : tree sz = maybe_constant_value ((*args)[1]);
11513 33 : if (TREE_CODE (sz) == INTEGER_CST
11514 33 : && tree_int_cst_lt (sz, TYPE_SIZE_UNIT (desttype)))
11515 : /* Finally, warn on reallocation into insufficient space. */
11516 7 : warned = warning_at (loc, OPT_Wclass_memaccess,
11517 : "%qD moving an object of non-trivial type "
11518 : "%#qT and size %E into a region of size %E",
11519 7 : fndecl, desttype, TYPE_SIZE_UNIT (desttype),
11520 : sz);
11521 : }
11522 : break;
11523 :
11524 1646 : default:
11525 1646 : return;
11526 : }
11527 :
11528 310 : if (warnfmt)
11529 : {
11530 1569 : if (suggest)
11531 1569 : warned = warning_at (loc, OPT_Wclass_memaccess,
11532 : warnfmt, fndecl, desttype, suggest);
11533 : else
11534 : warned = warning_at (loc, OPT_Wclass_memaccess,
11535 : warnfmt, fndecl, desttype);
11536 : }
11537 :
11538 3375 : if (warned)
11539 1879 : inform (location_of (desttype), "%#qT declared here", desttype);
11540 5021 : }
11541 :
11542 : /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
11543 : If FN is the result of resolving an overloaded target built-in,
11544 : ORIG_FNDECL is the original function decl, otherwise it is null.
11545 : This function performs no overload resolution, conversion, or other
11546 : high-level operations. */
11547 :
11548 : tree
11549 167861248 : build_cxx_call (tree fn, int nargs, tree *argarray,
11550 : tsubst_flags_t complain, tree orig_fndecl)
11551 : {
11552 167861248 : tree fndecl;
11553 :
11554 : /* Remember roughly where this call is. */
11555 167861248 : location_t loc = cp_expr_loc_or_input_loc (fn);
11556 167861248 : fn = build_call_a (fn, nargs, argarray);
11557 167861248 : SET_EXPR_LOCATION (fn, loc);
11558 :
11559 167861248 : fndecl = get_callee_fndecl (fn);
11560 167861248 : if (!orig_fndecl)
11561 167861248 : orig_fndecl = fndecl;
11562 :
11563 : /* Check that arguments to builtin functions match the expectations. */
11564 167861248 : if (fndecl
11565 161319585 : && !processing_template_decl
11566 329127387 : && fndecl_built_in_p (fndecl))
11567 : {
11568 : int i;
11569 :
11570 : /* We need to take care that values to BUILT_IN_NORMAL
11571 : are reduced. */
11572 22010696 : for (i = 0; i < nargs; i++)
11573 14421458 : argarray[i] = maybe_constant_value (argarray[i]);
11574 :
11575 7589238 : if (!check_builtin_function_arguments (EXPR_LOCATION (fn), vNULL, fndecl,
11576 : orig_fndecl, nargs, argarray,
11577 : complain & tf_error))
11578 927 : return error_mark_node;
11579 7588311 : else if (fndecl_built_in_p (fndecl, BUILT_IN_CLEAR_PADDING))
11580 : {
11581 14372 : tree arg0 = argarray[0];
11582 14372 : STRIP_NOPS (arg0);
11583 14372 : if (TREE_CODE (arg0) == ADDR_EXPR
11584 264 : && DECL_P (TREE_OPERAND (arg0, 0))
11585 14615 : && same_type_ignoring_top_level_qualifiers_p
11586 243 : (TREE_TYPE (TREE_TYPE (argarray[0])),
11587 243 : TREE_TYPE (TREE_TYPE (arg0))))
11588 : /* For __builtin_clear_padding (&var) we know the type
11589 : is for a complete object, so there is no risk in clearing
11590 : padding that is reused in some derived class member. */;
11591 14136 : else if (!trivially_copyable_p (TREE_TYPE (TREE_TYPE (argarray[0]))))
11592 : {
11593 18 : error_at (EXPR_LOC_OR_LOC (argarray[0], input_location),
11594 : "argument %u in call to function %qE "
11595 : "has pointer to a non-trivially-copyable type (%qT)",
11596 18 : 1, fndecl, TREE_TYPE (argarray[0]));
11597 18 : return error_mark_node;
11598 : }
11599 : }
11600 : }
11601 :
11602 167860303 : if (VOID_TYPE_P (TREE_TYPE (fn)))
11603 37089070 : return maybe_contract_wrap_call (fndecl, fn);
11604 :
11605 : /* 5.2.2/11: If a function call is a prvalue of object type: if the
11606 : function call is either the operand of a decltype-specifier or the
11607 : right operand of a comma operator that is the operand of a
11608 : decltype-specifier, a temporary object is not introduced for the
11609 : prvalue. The type of the prvalue may be incomplete. */
11610 130771233 : if (!(complain & tf_decltype))
11611 : {
11612 108050107 : fn = require_complete_type (fn, complain);
11613 108050107 : if (fn == error_mark_node)
11614 : return error_mark_node;
11615 108050081 : fn = maybe_contract_wrap_call (fndecl, fn);
11616 :
11617 108050081 : if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
11618 : {
11619 12229023 : fn = build_cplus_new (TREE_TYPE (fn), fn, complain);
11620 12229023 : maybe_warn_parm_abi (TREE_TYPE (fn), loc);
11621 : }
11622 : }
11623 : else
11624 22721126 : fn = maybe_contract_wrap_call (fndecl, fn);
11625 130771207 : return convert_from_reference (fn);
11626 : }
11627 :
11628 : /* Returns the value to use for the in-charge parameter when making a
11629 : call to a function with the indicated NAME.
11630 :
11631 : FIXME:Can't we find a neater way to do this mapping? */
11632 :
11633 : tree
11634 35488 : in_charge_arg_for_name (tree name)
11635 : {
11636 35488 : if (IDENTIFIER_CTOR_P (name))
11637 : {
11638 20420 : if (name == complete_ctor_identifier)
11639 10210 : return integer_one_node;
11640 10210 : gcc_checking_assert (name == base_ctor_identifier);
11641 : }
11642 : else
11643 : {
11644 15068 : if (name == complete_dtor_identifier)
11645 7534 : return integer_two_node;
11646 7534 : else if (name == deleting_dtor_identifier)
11647 : /* The deleting dtor should now be handled by
11648 : build_delete_destructor_body. */
11649 0 : gcc_unreachable ();
11650 7534 : gcc_checking_assert (name == base_dtor_identifier);
11651 : }
11652 :
11653 17744 : return integer_zero_node;
11654 : }
11655 :
11656 : /* We've built up a constructor call RET. Complain if it delegates to the
11657 : constructor we're currently compiling. */
11658 :
11659 : static void
11660 359904 : check_self_delegation (tree ret)
11661 : {
11662 359904 : if (TREE_CODE (ret) == TARGET_EXPR)
11663 0 : ret = TARGET_EXPR_INITIAL (ret);
11664 359904 : tree fn = cp_get_callee_fndecl_nofold (ret);
11665 359904 : if (fn && DECL_ABSTRACT_ORIGIN (fn) == current_function_decl)
11666 46 : error ("constructor delegates to itself");
11667 359904 : }
11668 :
11669 : /* Build a call to a constructor, destructor, or an assignment
11670 : operator for INSTANCE, an expression with class type. NAME
11671 : indicates the special member function to call; *ARGS are the
11672 : arguments. ARGS may be NULL. This may change ARGS. BINFO
11673 : indicates the base of INSTANCE that is to be passed as the `this'
11674 : parameter to the member function called.
11675 :
11676 : FLAGS are the LOOKUP_* flags to use when processing the call.
11677 :
11678 : If NAME indicates a complete object constructor, INSTANCE may be
11679 : NULL_TREE. In this case, the caller will call build_cplus_new to
11680 : store the newly constructed object into a VAR_DECL. */
11681 :
11682 : tree
11683 39404391 : build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
11684 : tree binfo, int flags, tsubst_flags_t complain)
11685 : {
11686 39404391 : tree fns;
11687 : /* The type of the subobject to be constructed or destroyed. */
11688 39404391 : tree class_type;
11689 39404391 : vec<tree, va_gc> *allocated = NULL;
11690 39404391 : tree ret;
11691 :
11692 39404391 : gcc_assert (IDENTIFIER_CDTOR_P (name) || name == assign_op_identifier);
11693 :
11694 39404391 : if (error_operand_p (instance))
11695 0 : return error_mark_node;
11696 :
11697 39404391 : if (IDENTIFIER_DTOR_P (name))
11698 : {
11699 10731011 : gcc_assert (args == NULL || vec_safe_is_empty (*args));
11700 10731011 : if (!type_build_dtor_call (TREE_TYPE (instance)))
11701 : /* Shortcut to avoid lazy destructor declaration. */
11702 1477 : return build_trivial_dtor_call (instance);
11703 : }
11704 :
11705 39402914 : if (TYPE_P (binfo))
11706 : {
11707 : /* Resolve the name. */
11708 31489173 : if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
11709 6 : return error_mark_node;
11710 :
11711 31489167 : binfo = TYPE_BINFO (binfo);
11712 : }
11713 :
11714 31489167 : gcc_assert (binfo != NULL_TREE);
11715 :
11716 39402908 : class_type = BINFO_TYPE (binfo);
11717 :
11718 : /* Handle the special case where INSTANCE is NULL_TREE. */
11719 39402908 : if (name == complete_ctor_identifier && !instance)
11720 20892470 : instance = build_dummy_object (class_type);
11721 : else
11722 : {
11723 : /* Convert to the base class, if necessary. */
11724 18510438 : if (!same_type_ignoring_top_level_qualifiers_p
11725 18510438 : (TREE_TYPE (instance), BINFO_TYPE (binfo)))
11726 : {
11727 1632335 : if (IDENTIFIER_CDTOR_P (name))
11728 : /* For constructors and destructors, either the base is
11729 : non-virtual, or it is virtual but we are doing the
11730 : conversion from a constructor or destructor for the
11731 : complete object. In either case, we can convert
11732 : statically. */
11733 1612595 : instance = convert_to_base_statically (instance, binfo);
11734 : else
11735 : {
11736 : /* However, for assignment operators, we must convert
11737 : dynamically if the base is virtual. */
11738 19740 : gcc_checking_assert (name == assign_op_identifier);
11739 19740 : instance = build_base_path (PLUS_EXPR, instance,
11740 : binfo, /*nonnull=*/1, complain);
11741 : }
11742 : }
11743 : }
11744 :
11745 39402908 : gcc_assert (instance != NULL_TREE);
11746 :
11747 : /* In C++17, "If the initializer expression is a prvalue and the
11748 : cv-unqualified version of the source type is the same class as the class
11749 : of the destination, the initializer expression is used to initialize the
11750 : destination object." Handle that here to avoid doing overload
11751 : resolution. */
11752 39402908 : if (cxx_dialect >= cxx17
11753 39211393 : && args && vec_safe_length (*args) == 1
11754 62929493 : && !unsafe_return_slot_p (instance))
11755 : {
11756 22241514 : tree arg = (**args)[0];
11757 :
11758 1703888 : if (BRACE_ENCLOSED_INITIALIZER_P (arg)
11759 1703496 : && !TYPE_HAS_LIST_CTOR (class_type)
11760 1627960 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (arg)
11761 23869460 : && CONSTRUCTOR_NELTS (arg) == 1)
11762 1153847 : arg = CONSTRUCTOR_ELT (arg, 0)->value;
11763 :
11764 22241514 : if ((TREE_CODE (arg) == TARGET_EXPR
11765 10780207 : || TREE_CODE (arg) == CONSTRUCTOR)
11766 34252936 : && (same_type_ignoring_top_level_qualifiers_p
11767 12011422 : (class_type, TREE_TYPE (arg))))
11768 : {
11769 10847624 : if (is_dummy_object (instance))
11770 : return arg;
11771 230199 : else if (TREE_CODE (arg) == TARGET_EXPR)
11772 230199 : TARGET_EXPR_DIRECT_INIT_P (arg) = true;
11773 :
11774 230199 : if ((complain & tf_error)
11775 230197 : && (flags & LOOKUP_DELEGATING_CONS))
11776 0 : check_self_delegation (arg);
11777 : /* Avoid change of behavior on Wunused-var-2.C. */
11778 230199 : instance = mark_lvalue_use (instance);
11779 230199 : return cp_build_init_expr (instance, arg);
11780 : }
11781 : }
11782 :
11783 28555284 : fns = lookup_fnfields (binfo, name, 1, complain);
11784 :
11785 : /* When making a call to a constructor or destructor for a subobject
11786 : that uses virtual base classes, pass down a pointer to a VTT for
11787 : the subobject. */
11788 28555284 : if ((name == base_ctor_identifier
11789 26183778 : || name == base_dtor_identifier)
11790 30167959 : && CLASSTYPE_VBASECLASSES (class_type))
11791 : {
11792 47622 : tree vtt;
11793 47622 : tree sub_vtt;
11794 :
11795 : /* If the current function is a complete object constructor
11796 : or destructor, then we fetch the VTT directly.
11797 : Otherwise, we look it up using the VTT we were given. */
11798 47622 : vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
11799 47622 : vtt = decay_conversion (vtt, complain);
11800 47622 : if (vtt == error_mark_node)
11801 0 : return error_mark_node;
11802 47622 : vtt = build_if_in_charge (vtt, current_vtt_parm);
11803 47622 : if (BINFO_SUBVTT_INDEX (binfo))
11804 47442 : sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
11805 : else
11806 180 : sub_vtt = vtt;
11807 :
11808 47622 : if (args == NULL)
11809 : {
11810 30212 : allocated = make_tree_vector ();
11811 30212 : args = &allocated;
11812 : }
11813 :
11814 47622 : vec_safe_insert (*args, 0, sub_vtt);
11815 : }
11816 :
11817 57110568 : ret = build_new_method_call (instance, fns, args,
11818 28555284 : TYPE_BINFO (BINFO_TYPE (binfo)),
11819 : flags, /*fn=*/NULL,
11820 : complain);
11821 :
11822 28555284 : if (allocated != NULL)
11823 30212 : release_tree_vector (allocated);
11824 :
11825 28555284 : if ((complain & tf_error)
11826 25405867 : && (flags & LOOKUP_DELEGATING_CONS)
11827 360004 : && name == complete_ctor_identifier)
11828 359904 : check_self_delegation (ret);
11829 :
11830 : return ret;
11831 : }
11832 :
11833 : /* Return the NAME, as a C string. The NAME indicates a function that
11834 : is a member of TYPE. *FREE_P is set to true if the caller must
11835 : free the memory returned.
11836 :
11837 : Rather than go through all of this, we should simply set the names
11838 : of constructors and destructors appropriately, and dispense with
11839 : ctor_identifier, dtor_identifier, etc. */
11840 :
11841 : static char *
11842 93 : name_as_c_string (tree name, tree type, bool *free_p)
11843 : {
11844 93 : const char *pretty_name;
11845 :
11846 : /* Assume that we will not allocate memory. */
11847 93 : *free_p = false;
11848 : /* Constructors and destructors are special. */
11849 93 : if (IDENTIFIER_CDTOR_P (name))
11850 : {
11851 42 : pretty_name
11852 42 : = identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type)));
11853 : /* For a destructor, add the '~'. */
11854 42 : if (IDENTIFIER_DTOR_P (name))
11855 : {
11856 0 : pretty_name = concat ("~", pretty_name, NULL);
11857 : /* Remember that we need to free the memory allocated. */
11858 0 : *free_p = true;
11859 : }
11860 : }
11861 51 : else if (IDENTIFIER_CONV_OP_P (name))
11862 : {
11863 0 : pretty_name = concat ("operator ",
11864 0 : type_as_string_translate (TREE_TYPE (name),
11865 : TFF_PLAIN_IDENTIFIER),
11866 : NULL);
11867 : /* Remember that we need to free the memory allocated. */
11868 0 : *free_p = true;
11869 : }
11870 : else
11871 51 : pretty_name = identifier_to_locale (IDENTIFIER_POINTER (name));
11872 :
11873 93 : return const_cast<char *> (pretty_name);
11874 : }
11875 :
11876 : /* If CANDIDATES contains exactly one candidate, return it, otherwise
11877 : return NULL. */
11878 :
11879 : static z_candidate *
11880 680 : single_z_candidate (z_candidate *candidates)
11881 : {
11882 0 : if (candidates == NULL)
11883 : return NULL;
11884 :
11885 668 : if (candidates->next)
11886 0 : return NULL;
11887 :
11888 : return candidates;
11889 : }
11890 :
11891 : /* If CANDIDATE is invalid due to a bad argument type, return the
11892 : pertinent conversion_info.
11893 :
11894 : Otherwise, return NULL. */
11895 :
11896 : static const conversion_info *
11897 339 : maybe_get_bad_conversion_for_unmatched_call (const z_candidate *candidate)
11898 : {
11899 : /* Must be an rr_arg_conversion or rr_bad_arg_conversion. */
11900 339 : rejection_reason *r = candidate->reason;
11901 :
11902 339 : if (r == NULL)
11903 : return NULL;
11904 :
11905 339 : switch (r->code)
11906 : {
11907 : default:
11908 : return NULL;
11909 :
11910 52 : case rr_arg_conversion:
11911 52 : return &r->u.conversion;
11912 :
11913 6 : case rr_bad_arg_conversion:
11914 6 : return &r->u.bad_conversion;
11915 : }
11916 : }
11917 :
11918 : /* Issue an error and note complaining about a bad argument type at a
11919 : callsite with a single candidate FNDECL.
11920 :
11921 : ARG_LOC is the location of the argument (or UNKNOWN_LOCATION, in which
11922 : case input_location is used).
11923 : FROM_TYPE is the type of the actual argument; TO_TYPE is the type of
11924 : the formal parameter. */
11925 :
11926 : void
11927 150 : complain_about_bad_argument (location_t arg_loc,
11928 : tree from_type, tree to_type,
11929 : tree fndecl, int parmnum)
11930 : {
11931 150 : auto_diagnostic_group d;
11932 150 : range_label_for_type_mismatch rhs_label (from_type, to_type);
11933 150 : range_label *label = &rhs_label;
11934 150 : if (arg_loc == UNKNOWN_LOCATION)
11935 : {
11936 6 : arg_loc = input_location;
11937 6 : label = NULL;
11938 : }
11939 150 : gcc_rich_location richloc (arg_loc, label, highlight_colors::percent_h);
11940 150 : error_at (&richloc,
11941 : "cannot convert %qH to %qI",
11942 : from_type, to_type);
11943 150 : maybe_inform_about_fndecl_for_bogus_argument_init
11944 150 : (fndecl,
11945 : parmnum,
11946 : highlight_colors::percent_i);
11947 150 : }
11948 :
11949 : /* Subroutine of build_new_method_call_1, for where there are no viable
11950 : candidates for the call. */
11951 :
11952 : static void
11953 686 : complain_about_no_candidates_for_method_call (tree instance,
11954 : z_candidate *candidates,
11955 : tree explicit_targs,
11956 : tree basetype,
11957 : tree optype, tree name,
11958 : bool skip_first_for_error,
11959 : vec<tree, va_gc> *user_args)
11960 : {
11961 686 : auto_diagnostic_group d;
11962 686 : if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
11963 0 : cxx_incomplete_type_error (instance, basetype);
11964 686 : else if (optype)
11965 6 : error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
11966 : basetype, optype, build_tree_list_vec (user_args),
11967 6 : TREE_TYPE (instance));
11968 : else
11969 : {
11970 : /* Special-case for when there's a single candidate that's failing
11971 : due to a bad argument type. */
11972 680 : if (z_candidate *candidate = single_z_candidate (candidates))
11973 339 : if (const conversion_info *conv
11974 339 : = maybe_get_bad_conversion_for_unmatched_call (candidate))
11975 : {
11976 58 : tree from_type = conv->from;
11977 58 : if (!TYPE_P (conv->from))
11978 6 : from_type = lvalue_type (conv->from);
11979 58 : complain_about_bad_argument (conv->loc,
11980 58 : from_type, conv->to_type,
11981 58 : candidate->fn, conv->n_arg);
11982 58 : return;
11983 : }
11984 :
11985 622 : tree arglist = build_tree_list_vec (user_args);
11986 622 : tree errname = name;
11987 622 : bool twiddle = false;
11988 622 : if (IDENTIFIER_CDTOR_P (errname))
11989 : {
11990 319 : twiddle = IDENTIFIER_DTOR_P (errname);
11991 319 : errname = constructor_name (basetype);
11992 : }
11993 622 : if (explicit_targs)
11994 48 : errname = lookup_template_function (errname, explicit_targs);
11995 622 : if (skip_first_for_error)
11996 3 : arglist = TREE_CHAIN (arglist);
11997 1244 : error ("no matching function for call to %<%T::%s%E(%A)%#V%>",
11998 622 : basetype, &"~"[!twiddle], errname, arglist,
11999 622 : TREE_TYPE (instance));
12000 : }
12001 628 : print_z_candidates (location_of (name), candidates);
12002 686 : }
12003 :
12004 : /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
12005 : be set, upon return, to the function called. ARGS may be NULL.
12006 : This may change ARGS. */
12007 :
12008 : tree
12009 99615711 : build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args,
12010 : tree conversion_path, int flags,
12011 : tree *fn_p, tsubst_flags_t complain)
12012 : {
12013 99615711 : struct z_candidate *candidates = 0, *cand;
12014 99615711 : tree explicit_targs = NULL_TREE;
12015 99615711 : tree basetype = NULL_TREE;
12016 99615711 : tree access_binfo;
12017 99615711 : tree optype;
12018 99615711 : tree first_mem_arg = NULL_TREE;
12019 99615711 : tree name;
12020 99615711 : bool skip_first_for_error;
12021 99615711 : vec<tree, va_gc> *user_args;
12022 99615711 : tree call;
12023 99615711 : tree fn;
12024 99615711 : int template_only = 0;
12025 99615711 : bool any_viable_p;
12026 99615711 : tree orig_instance;
12027 99615711 : tree orig_fns;
12028 99615711 : vec<tree, va_gc> *orig_args = NULL;
12029 :
12030 99615711 : auto_cond_timevar tv (TV_OVERLOAD);
12031 :
12032 99615711 : gcc_assert (instance != NULL_TREE);
12033 :
12034 : /* We don't know what function we're going to call, yet. */
12035 99615711 : if (fn_p)
12036 28089934 : *fn_p = NULL_TREE;
12037 :
12038 99615711 : if (error_operand_p (instance)
12039 99615711 : || !fns || error_operand_p (fns))
12040 1440842 : return error_mark_node;
12041 :
12042 98174869 : if (!BASELINK_P (fns))
12043 : {
12044 0 : if (complain & tf_error)
12045 0 : error ("call to non-function %qD", fns);
12046 0 : return error_mark_node;
12047 : }
12048 :
12049 98174869 : orig_instance = instance;
12050 98174869 : orig_fns = fns;
12051 :
12052 : /* Dismantle the baselink to collect all the information we need. */
12053 98174869 : if (!conversion_path)
12054 42984646 : conversion_path = BASELINK_BINFO (fns);
12055 98174869 : access_binfo = BASELINK_ACCESS_BINFO (fns);
12056 98174869 : optype = BASELINK_OPTYPE (fns);
12057 98174869 : fns = BASELINK_FUNCTIONS (fns);
12058 98174869 : if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12059 : {
12060 2708851 : explicit_targs = TREE_OPERAND (fns, 1);
12061 2708851 : fns = TREE_OPERAND (fns, 0);
12062 2708851 : template_only = 1;
12063 : }
12064 98174869 : gcc_assert (OVL_P (fns));
12065 98174869 : fn = OVL_FIRST (fns);
12066 98174869 : name = DECL_NAME (fn);
12067 :
12068 98174869 : basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
12069 98174869 : gcc_assert (CLASS_TYPE_P (basetype));
12070 :
12071 98174869 : user_args = args == NULL ? NULL : *args;
12072 : /* Under DR 147 A::A() is an invalid constructor call,
12073 : not a functional cast. */
12074 98174869 : if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
12075 : {
12076 54 : if (! (complain & tf_error))
12077 0 : return error_mark_node;
12078 :
12079 54 : basetype = DECL_CONTEXT (fn);
12080 54 : name = constructor_name (basetype);
12081 54 : auto_diagnostic_group d;
12082 54 : if (permerror (input_location,
12083 : "cannot call constructor %<%T::%D%> directly",
12084 : basetype, name))
12085 54 : inform (input_location, "for a function-style cast, remove the "
12086 : "redundant %<::%D%>", name);
12087 54 : call = build_functional_cast (input_location, basetype,
12088 : build_tree_list_vec (user_args),
12089 : complain);
12090 54 : return call;
12091 54 : }
12092 :
12093 98174815 : if (processing_template_decl)
12094 7078837 : orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
12095 :
12096 : /* Process the argument list. */
12097 98090419 : if (args != NULL && *args != NULL)
12098 : {
12099 83629032 : *args = resolve_args (*args, complain);
12100 83629032 : if (*args == NULL)
12101 189 : return error_mark_node;
12102 : user_args = *args;
12103 : }
12104 :
12105 : /* Consider the object argument to be used even if we end up selecting a
12106 : static member function. */
12107 98174626 : instance = mark_type_use (instance);
12108 :
12109 : /* Figure out whether to skip the first argument for the error
12110 : message we will display to users if an error occurs. We don't
12111 : want to display any compiler-generated arguments. The "this"
12112 : pointer hasn't been added yet. However, we must remove the VTT
12113 : pointer if this is a call to a base-class constructor or
12114 : destructor. */
12115 98174626 : skip_first_for_error = false;
12116 98174626 : if (IDENTIFIER_CDTOR_P (name))
12117 : {
12118 : /* Callers should explicitly indicate whether they want to ctor
12119 : the complete object or just the part without virtual bases. */
12120 52460609 : gcc_assert (name != ctor_identifier);
12121 :
12122 : /* Remove the VTT pointer, if present. */
12123 50089109 : if ((name == base_ctor_identifier || name == base_dtor_identifier)
12124 54073284 : && CLASSTYPE_VBASECLASSES (basetype))
12125 : skip_first_for_error = true;
12126 :
12127 : /* It's OK to call destructors and constructors on cv-qualified
12128 : objects. Therefore, convert the INSTANCE to the unqualified
12129 : type, if necessary. */
12130 52460609 : if (!same_type_p (basetype, TREE_TYPE (instance)))
12131 : {
12132 690018 : instance = build_this (instance);
12133 690018 : instance = build_nop (build_pointer_type (basetype), instance);
12134 690018 : instance = build_fold_indirect_ref (instance);
12135 : }
12136 : }
12137 : else
12138 91428034 : gcc_assert (!DECL_DESTRUCTOR_P (fn) && !DECL_CONSTRUCTOR_P (fn));
12139 :
12140 : /* For the overload resolution we need to find the actual `this`
12141 : that would be captured if the call turns out to be to a
12142 : non-static member function. Do not actually capture it at this
12143 : point. */
12144 196349252 : if (DECL_CONSTRUCTOR_P (fn))
12145 : /* Constructors don't use the enclosing 'this'. */
12146 : first_mem_arg = instance;
12147 : else
12148 71875920 : first_mem_arg = maybe_resolve_dummy (instance, false);
12149 :
12150 98174626 : conversion_obstack_sentinel cos;
12151 :
12152 : /* The number of arguments artificial parms in ARGS; we subtract one because
12153 : there's no 'this' in ARGS. */
12154 98174626 : unsigned skip = num_artificial_parms_for (fn) - 1;
12155 :
12156 : /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
12157 : initializer, not T({ }). */
12158 98174626 : if (DECL_CONSTRUCTOR_P (fn)
12159 22473021 : && vec_safe_length (user_args) > skip
12160 118403277 : && DIRECT_LIST_INIT_P ((*user_args)[skip]))
12161 : {
12162 1705462 : tree init_list = (*user_args)[skip];
12163 1705462 : tree init = NULL_TREE;
12164 :
12165 1705462 : gcc_assert (user_args->length () == skip + 1
12166 : && !(flags & LOOKUP_ONLYCONVERTING));
12167 :
12168 : /* If the initializer list has no elements and T is a class type with
12169 : a default constructor, the object is value-initialized. Handle
12170 : this here so we don't need to handle it wherever we use
12171 : build_special_member_call. */
12172 1705462 : if (CONSTRUCTOR_NELTS (init_list) == 0
12173 192939 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
12174 : /* For a user-provided default constructor, use the normal
12175 : mechanisms so that protected access works. */
12176 192939 : && type_has_non_user_provided_default_constructor (basetype)
12177 1633453 : && !processing_template_decl)
12178 120927 : init = build_value_init (basetype, complain);
12179 :
12180 : /* If BASETYPE is an aggregate, we need to do aggregate
12181 : initialization. */
12182 1584535 : else if (CP_AGGREGATE_TYPE_P (basetype))
12183 : {
12184 41 : init = reshape_init (basetype, init_list, complain);
12185 41 : init = digest_init (basetype, init, complain);
12186 : }
12187 :
12188 120968 : if (init)
12189 : {
12190 120968 : if (is_dummy_object (instance))
12191 35766 : return get_target_expr (init, complain);
12192 85202 : return cp_build_init_expr (instance, init);
12193 : }
12194 :
12195 : /* Otherwise go ahead with overload resolution. */
12196 1584494 : add_list_candidates (fns, first_mem_arg, user_args,
12197 : basetype, explicit_targs, template_only,
12198 : conversion_path, access_binfo, flags,
12199 : &candidates, complain);
12200 : }
12201 : else
12202 96469164 : add_candidates (fns, first_mem_arg, user_args, optype,
12203 : explicit_targs, template_only, conversion_path,
12204 : access_binfo, flags, &candidates, complain);
12205 :
12206 98053658 : any_viable_p = false;
12207 98053658 : candidates = splice_viable (candidates, false, &any_viable_p);
12208 :
12209 98053658 : if (!any_viable_p)
12210 : {
12211 : /* [dcl.init], 17.6.2.2:
12212 :
12213 : Otherwise, if no constructor is viable, the destination type is
12214 : a (possibly cv-qualified) aggregate class A, and the initializer
12215 : is a parenthesized expression-list, the object is initialized as
12216 : follows...
12217 :
12218 : We achieve this by building up a CONSTRUCTOR, as for list-init,
12219 : and setting CONSTRUCTOR_IS_PAREN_INIT to distinguish between
12220 : the two. */
12221 26477 : if (DECL_CONSTRUCTOR_P (fn)
12222 20073 : && !(flags & LOOKUP_ONLYCONVERTING)
12223 20049 : && cxx_dialect >= cxx20
12224 19432 : && CP_AGGREGATE_TYPE_P (basetype)
12225 26477 : && !vec_safe_is_empty (user_args))
12226 : {
12227 : /* Create a CONSTRUCTOR from ARGS, e.g. {1, 2} from <1, 2>. */
12228 1171 : tree ctor = build_constructor_from_vec (init_list_type_node,
12229 : user_args);
12230 1171 : CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true;
12231 1171 : CONSTRUCTOR_IS_PAREN_INIT (ctor) = true;
12232 1171 : if (is_dummy_object (instance))
12233 : return ctor;
12234 : else
12235 : {
12236 677 : ctor = digest_init (basetype, ctor, complain);
12237 677 : if (ctor == error_mark_node)
12238 : return error_mark_node;
12239 457 : return cp_build_init_expr (instance, ctor);
12240 : }
12241 : }
12242 25306 : if (complain & tf_error)
12243 686 : complain_about_no_candidates_for_method_call (instance, candidates,
12244 : explicit_targs, basetype,
12245 : optype, name,
12246 : skip_first_for_error,
12247 : user_args);
12248 25306 : call = error_mark_node;
12249 : }
12250 : else
12251 : {
12252 98027181 : cand = tourney (candidates, complain);
12253 98027181 : if (cand == 0)
12254 : {
12255 193 : char *pretty_name;
12256 193 : bool free_p;
12257 193 : tree arglist;
12258 :
12259 193 : if (complain & tf_error)
12260 : {
12261 93 : pretty_name = name_as_c_string (name, basetype, &free_p);
12262 93 : arglist = build_tree_list_vec (user_args);
12263 93 : if (skip_first_for_error)
12264 0 : arglist = TREE_CHAIN (arglist);
12265 93 : auto_diagnostic_group d;
12266 186 : if (!any_strictly_viable (candidates))
12267 13 : error ("no matching function for call to %<%s(%A)%>",
12268 : pretty_name, arglist);
12269 : else
12270 80 : error ("call of overloaded %<%s(%A)%> is ambiguous",
12271 : pretty_name, arglist);
12272 93 : print_z_candidates (location_of (name), candidates);
12273 93 : if (free_p)
12274 0 : free (pretty_name);
12275 93 : }
12276 193 : call = error_mark_node;
12277 193 : if (fn_p)
12278 84 : *fn_p = error_mark_node;
12279 : }
12280 : else
12281 : {
12282 98026988 : fn = cand->fn;
12283 98026988 : call = NULL_TREE;
12284 :
12285 98026988 : if (!(flags & LOOKUP_NONVIRTUAL)
12286 75157520 : && DECL_PURE_VIRTUAL_P (fn)
12287 206800 : && instance == current_class_ref
12288 98189618 : && (complain & tf_warning))
12289 : {
12290 : /* This is not an error, it is runtime undefined
12291 : behavior. */
12292 162630 : if (!current_function_decl)
12293 3 : warning (0, "pure virtual %q#D called from "
12294 : "non-static data member initializer", fn);
12295 162627 : else if (DECL_CONSTRUCTOR_P (current_function_decl)
12296 162627 : || DECL_DESTRUCTOR_P (current_function_decl))
12297 9 : warning (0, (DECL_CONSTRUCTOR_P (current_function_decl)
12298 : ? G_("pure virtual %q#D called from constructor")
12299 : : G_("pure virtual %q#D called from destructor")),
12300 : fn);
12301 : }
12302 :
12303 112598119 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fn)
12304 166914942 : && !DECL_CONSTRUCTOR_P (fn)
12305 155326934 : && is_dummy_object (instance))
12306 : {
12307 52057 : instance = maybe_resolve_dummy (instance, true);
12308 52057 : if (instance == error_mark_node)
12309 : call = error_mark_node;
12310 52057 : else if (!is_dummy_object (instance))
12311 : {
12312 : /* We captured 'this' in the current lambda now that
12313 : we know we really need it. */
12314 51555 : cand->first_arg = instance;
12315 : }
12316 502 : else if (current_class_ptr && any_dependent_bases_p ())
12317 : /* We can't tell until instantiation time whether we can use
12318 : *this as the implicit object argument. */;
12319 : else
12320 : {
12321 463 : if (complain & tf_error)
12322 69 : error ("cannot call member function %qD without object",
12323 : fn);
12324 463 : call = error_mark_node;
12325 : }
12326 : }
12327 :
12328 98026988 : if (call != error_mark_node)
12329 : {
12330 : /* Now we know what function is being called. */
12331 98026525 : if (fn_p)
12332 26635168 : *fn_p = fn;
12333 : /* Build the actual CALL_EXPR. */
12334 98026525 : call = build_over_call (cand, flags, complain);
12335 :
12336 : /* Suppress warnings for if (my_struct.operator= (x)) where
12337 : my_struct is implicitly converted to bool. */
12338 98026525 : if (TREE_CODE (call) == MODIFY_EXPR)
12339 3152560 : suppress_warning (call, OPT_Wparentheses);
12340 :
12341 : /* In an expression of the form `a->f()' where `f' turns
12342 : out to be a static member function, `a' is
12343 : none-the-less evaluated. */
12344 98026525 : if (!is_dummy_object (instance))
12345 75877802 : call = keep_unused_object_arg (call, instance, fn);
12346 98026525 : if (call != error_mark_node
12347 194069822 : && DECL_DESTRUCTOR_P (cand->fn)
12348 124187479 : && !VOID_TYPE_P (TREE_TYPE (call)))
12349 : /* An explicit call of the form "x->~X()" has type
12350 : "void". However, on platforms where destructors
12351 : return "this" (i.e., those where
12352 : targetm.cxx.cdtor_returns_this is true), such calls
12353 : will appear to have a return value of pointer type
12354 : to the low-level call machinery. We do not want to
12355 : change the low-level machinery, since we want to be
12356 : able to optimize "delete f()" on such platforms as
12357 : "operator delete(~X(f()))" (rather than generating
12358 : "t = f(), ~X(t), operator delete (t)"). */
12359 15212065 : call = build_nop (void_type_node, call);
12360 : }
12361 : }
12362 : }
12363 :
12364 98052487 : if (processing_template_decl && call != error_mark_node)
12365 : {
12366 7078783 : bool cast_to_void = false;
12367 :
12368 7078783 : if (TREE_CODE (call) == COMPOUND_EXPR)
12369 10 : call = TREE_OPERAND (call, 1);
12370 7078773 : else if (TREE_CODE (call) == NOP_EXPR)
12371 : {
12372 0 : cast_to_void = true;
12373 0 : call = TREE_OPERAND (call, 0);
12374 : }
12375 7078783 : if (INDIRECT_REF_P (call))
12376 598824 : call = TREE_OPERAND (call, 0);
12377 :
12378 : /* Prune all but the selected function from the original overload
12379 : set so that we can avoid some duplicate work at instantiation time. */
12380 7078783 : if (really_overloaded_fn (fns))
12381 : {
12382 1617459 : if (DECL_TEMPLATE_INFO (fn)
12383 1617459 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
12384 : {
12385 : /* Use the selected template, not the specialization, so that
12386 : this looks like an actual lookup result for sake of
12387 : filter_memfn_lookup. */
12388 :
12389 277029 : if (OVL_SINGLE_P (fns))
12390 : /* If the original overload set consists of a single function
12391 : template, this isn't beneficial. */
12392 237453 : goto skip_prune;
12393 :
12394 39576 : fn = ovl_make (DECL_TI_TEMPLATE (fn));
12395 39576 : if (template_only)
12396 29977 : fn = lookup_template_function (fn, explicit_targs);
12397 : }
12398 1380006 : orig_fns = copy_node (orig_fns);
12399 1380006 : BASELINK_FUNCTIONS (orig_fns) = fn;
12400 1380006 : BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (orig_fns) = true;
12401 : }
12402 :
12403 5461324 : skip_prune:
12404 7078783 : call = (build_min_non_dep_call_vec
12405 7078783 : (call,
12406 7078783 : build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
12407 : orig_instance, orig_fns, NULL_TREE),
12408 : orig_args));
12409 7078783 : SET_EXPR_LOCATION (call, input_location);
12410 7078783 : call = convert_from_reference (call);
12411 7078783 : if (cast_to_void)
12412 0 : call = build_nop (void_type_node, call);
12413 : }
12414 :
12415 98052487 : if (orig_args != NULL)
12416 6994435 : release_tree_vector (orig_args);
12417 :
12418 : return call;
12419 99615711 : }
12420 :
12421 : /* Returns true iff standard conversion sequence ICS1 is a proper
12422 : subsequence of ICS2. */
12423 :
12424 : static bool
12425 77766069 : is_subseq (conversion *ics1, conversion *ics2)
12426 : {
12427 : /* We can assume that a conversion of the same code
12428 : between the same types indicates a subsequence since we only get
12429 : here if the types we are converting from are the same. */
12430 :
12431 77766069 : while (ics1->kind == ck_rvalue
12432 91962162 : || ics1->kind == ck_lvalue)
12433 14196093 : ics1 = next_conversion (ics1);
12434 :
12435 : while (1)
12436 : {
12437 103889234 : while (ics2->kind == ck_rvalue
12438 103889234 : || ics2->kind == ck_lvalue)
12439 14196093 : ics2 = next_conversion (ics2);
12440 :
12441 89693141 : if (ics2->kind == ck_user
12442 89693141 : || !has_next (ics2->kind))
12443 : /* At this point, ICS1 cannot be a proper subsequence of
12444 : ICS2. We can get a USER_CONV when we are comparing the
12445 : second standard conversion sequence of two user conversion
12446 : sequences. */
12447 : return false;
12448 :
12449 11933903 : ics2 = next_conversion (ics2);
12450 :
12451 11933903 : while (ics2->kind == ck_rvalue
12452 18865013 : || ics2->kind == ck_lvalue)
12453 6931110 : ics2 = next_conversion (ics2);
12454 :
12455 11933903 : if (ics2->kind == ics1->kind
12456 6876 : && same_type_p (ics2->type, ics1->type)
12457 11940734 : && (ics1->kind == ck_identity
12458 6831 : || same_type_p (next_conversion (ics2)->type,
12459 : next_conversion (ics1)->type)))
12460 6831 : return true;
12461 : }
12462 : }
12463 :
12464 : /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
12465 : be any _TYPE nodes. */
12466 :
12467 : bool
12468 379770364 : is_properly_derived_from (tree derived, tree base)
12469 : {
12470 379770364 : if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
12471 : return false;
12472 :
12473 : /* We only allow proper derivation here. The DERIVED_FROM_P macro
12474 : considers every class derived from itself. */
12475 360017949 : return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
12476 360017949 : && DERIVED_FROM_P (base, derived));
12477 : }
12478 :
12479 : /* We build the ICS for an implicit object parameter as a pointer
12480 : conversion sequence. However, such a sequence should be compared
12481 : as if it were a reference conversion sequence. If ICS is the
12482 : implicit conversion sequence for an implicit object parameter,
12483 : modify it accordingly. */
12484 :
12485 : static void
12486 163323782 : maybe_handle_implicit_object (conversion **ics)
12487 : {
12488 163323782 : if ((*ics)->this_p)
12489 : {
12490 : /* [over.match.funcs]
12491 :
12492 : For non-static member functions, the type of the
12493 : implicit object parameter is "reference to cv X"
12494 : where X is the class of which the function is a
12495 : member and cv is the cv-qualification on the member
12496 : function declaration. */
12497 13042512 : conversion *t = *ics;
12498 13042512 : tree reference_type;
12499 :
12500 : /* The `this' parameter is a pointer to a class type. Make the
12501 : implicit conversion talk about a reference to that same class
12502 : type. */
12503 13042512 : reference_type = TREE_TYPE (t->type);
12504 13042512 : reference_type = build_reference_type (reference_type);
12505 :
12506 13042512 : if (t->kind == ck_qual)
12507 3141793 : t = next_conversion (t);
12508 13042512 : if (t->kind == ck_ptr)
12509 615231 : t = next_conversion (t);
12510 13042512 : t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
12511 13042512 : t = direct_reference_binding (reference_type, t);
12512 13042512 : t->this_p = 1;
12513 13042512 : t->rvaluedness_matches_p = 0;
12514 13042512 : *ics = t;
12515 : }
12516 163323782 : }
12517 :
12518 : /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
12519 : and return the initial reference binding conversion. Otherwise,
12520 : leave *ICS unchanged and return NULL. */
12521 :
12522 : static conversion *
12523 163323782 : maybe_handle_ref_bind (conversion **ics)
12524 : {
12525 163323782 : if ((*ics)->kind == ck_ref_bind)
12526 : {
12527 60422957 : conversion *old_ics = *ics;
12528 60422957 : *ics = next_conversion (old_ics);
12529 60422957 : (*ics)->user_conv_p = old_ics->user_conv_p;
12530 60422957 : return old_ics;
12531 : }
12532 :
12533 : return NULL;
12534 : }
12535 :
12536 : /* Get the expression at the beginning of the conversion chain C. */
12537 :
12538 : static tree
12539 51 : conv_get_original_expr (conversion *c)
12540 : {
12541 60 : for (; c; c = next_conversion (c))
12542 60 : if (c->kind == ck_identity || c->kind == ck_ambig || c->kind == ck_aggr)
12543 51 : return c->u.expr;
12544 : return NULL_TREE;
12545 : }
12546 :
12547 : /* Return a tree representing the number of elements initialized by the
12548 : list-initialization C. The caller must check that C converts to an
12549 : array type. */
12550 :
12551 : static tree
12552 126 : nelts_initialized_by_list_init (conversion *c)
12553 : {
12554 : /* If the array we're converting to has a dimension, we'll use that. */
12555 126 : if (TYPE_DOMAIN (c->type))
12556 84 : return array_type_nelts_top (c->type);
12557 : else
12558 : {
12559 : /* Otherwise, we look at how many elements the constructor we're
12560 : initializing from has. */
12561 42 : tree ctor = conv_get_original_expr (c);
12562 72 : return size_int (CONSTRUCTOR_NELTS (ctor));
12563 : }
12564 : }
12565 :
12566 : /* True iff C is a conversion that binds a reference or a pointer to
12567 : an array of unknown bound. */
12568 :
12569 : static inline bool
12570 30030844 : conv_binds_to_array_of_unknown_bound (conversion *c)
12571 : {
12572 : /* ck_ref_bind won't have the reference stripped. */
12573 30030844 : tree type = non_reference (c->type);
12574 : /* ck_qual won't have the pointer stripped. */
12575 30030844 : type = strip_pointer_operator (type);
12576 30030844 : return (TREE_CODE (type) == ARRAY_TYPE
12577 30030844 : && TYPE_DOMAIN (type) == NULL_TREE);
12578 : }
12579 :
12580 : /* Compare two implicit conversion sequences according to the rules set out in
12581 : [over.ics.rank]. Return values:
12582 :
12583 : 1: ics1 is better than ics2
12584 : -1: ics2 is better than ics1
12585 : 0: ics1 and ics2 are indistinguishable */
12586 :
12587 : static int
12588 81661932 : compare_ics (conversion *ics1, conversion *ics2)
12589 : {
12590 81661932 : tree from_type1;
12591 81661932 : tree from_type2;
12592 81661932 : tree to_type1;
12593 81661932 : tree to_type2;
12594 81661932 : tree deref_from_type1 = NULL_TREE;
12595 81661932 : tree deref_from_type2 = NULL_TREE;
12596 81661932 : tree deref_to_type1 = NULL_TREE;
12597 81661932 : tree deref_to_type2 = NULL_TREE;
12598 81661932 : conversion_rank rank1, rank2;
12599 :
12600 : /* REF_BINDING is nonzero if the result of the conversion sequence
12601 : is a reference type. In that case REF_CONV is the reference
12602 : binding conversion. */
12603 81661932 : conversion *ref_conv1;
12604 81661932 : conversion *ref_conv2;
12605 :
12606 : /* Compare badness before stripping the reference conversion. */
12607 81661932 : if (ics1->bad_p > ics2->bad_p)
12608 : return -1;
12609 81661918 : else if (ics1->bad_p < ics2->bad_p)
12610 : return 1;
12611 :
12612 : /* Handle implicit object parameters. */
12613 81661891 : maybe_handle_implicit_object (&ics1);
12614 81661891 : maybe_handle_implicit_object (&ics2);
12615 :
12616 : /* Handle reference parameters. */
12617 81661891 : ref_conv1 = maybe_handle_ref_bind (&ics1);
12618 81661891 : ref_conv2 = maybe_handle_ref_bind (&ics2);
12619 :
12620 : /* List-initialization sequence L1 is a better conversion sequence than
12621 : list-initialization sequence L2 if L1 converts to
12622 : std::initializer_list<X> for some X and L2 does not. */
12623 81661891 : if (ics1->kind == ck_list && ics2->kind != ck_list)
12624 : return 1;
12625 81661080 : if (ics2->kind == ck_list && ics1->kind != ck_list)
12626 : return -1;
12627 :
12628 : /* [over.ics.rank]
12629 :
12630 : When comparing the basic forms of implicit conversion sequences (as
12631 : defined in _over.best.ics_)
12632 :
12633 : --a standard conversion sequence (_over.ics.scs_) is a better
12634 : conversion sequence than a user-defined conversion sequence
12635 : or an ellipsis conversion sequence, and
12636 :
12637 : --a user-defined conversion sequence (_over.ics.user_) is a
12638 : better conversion sequence than an ellipsis conversion sequence
12639 : (_over.ics.ellipsis_). */
12640 : /* Use BAD_CONVERSION_RANK because we already checked for a badness
12641 : mismatch. If both ICS are bad, we try to make a decision based on
12642 : what would have happened if they'd been good. This is not an
12643 : extension, we'll still give an error when we build up the call; this
12644 : just helps us give a more helpful error message. */
12645 81660815 : rank1 = BAD_CONVERSION_RANK (ics1);
12646 81660815 : rank2 = BAD_CONVERSION_RANK (ics2);
12647 :
12648 81660815 : if (rank1 > rank2)
12649 : return -1;
12650 70834203 : else if (rank1 < rank2)
12651 : return 1;
12652 :
12653 39315640 : if (ics1->ellipsis_p)
12654 : /* Both conversions are ellipsis conversions. */
12655 : return 0;
12656 :
12657 : /* User-defined conversion sequence U1 is a better conversion sequence
12658 : than another user-defined conversion sequence U2 if they contain the
12659 : same user-defined conversion operator or constructor and if the sec-
12660 : ond standard conversion sequence of U1 is better than the second
12661 : standard conversion sequence of U2. */
12662 :
12663 : /* Handle list-conversion with the same code even though it isn't always
12664 : ranked as a user-defined conversion and it doesn't have a second
12665 : standard conversion sequence; it will still have the desired effect.
12666 : Specifically, we need to do the reference binding comparison at the
12667 : end of this function. */
12668 :
12669 39315594 : if (ics1->user_conv_p || ics1->kind == ck_list
12670 38601191 : || ics1->kind == ck_aggr || ics2->kind == ck_aggr)
12671 : {
12672 714472 : conversion *t1 = strip_standard_conversion (ics1);
12673 714472 : conversion *t2 = strip_standard_conversion (ics2);
12674 :
12675 714472 : if (!t1 || !t2 || t1->kind != t2->kind)
12676 : return 0;
12677 714453 : else if (t1->kind == ck_user)
12678 : {
12679 698878 : tree f1 = t1->cand ? t1->cand->fn : t1->type;
12680 698878 : tree f2 = t2->cand ? t2->cand->fn : t2->type;
12681 698878 : if (f1 != f2)
12682 : return 0;
12683 : }
12684 : /* List-initialization sequence L1 is a better conversion sequence than
12685 : list-initialization sequence L2 if
12686 :
12687 : -- L1 and L2 convert to arrays of the same element type, and either
12688 : the number of elements n1 initialized by L1 is less than the number
12689 : of elements n2 initialized by L2, or n1=n2 and L2 converts to an array
12690 : of unknown bound and L1 does not. (Added in CWG 1307 and extended by
12691 : P0388R4.) */
12692 15575 : else if (t1->kind == ck_aggr
12693 14563 : && TREE_CODE (t1->type) == ARRAY_TYPE
12694 66 : && TREE_CODE (t2->type) == ARRAY_TYPE
12695 15641 : && same_type_p (TREE_TYPE (t1->type), TREE_TYPE (t2->type)))
12696 : {
12697 63 : tree n1 = nelts_initialized_by_list_init (t1);
12698 63 : tree n2 = nelts_initialized_by_list_init (t2);
12699 63 : if (tree_int_cst_lt (n1, n2))
12700 : return 1;
12701 24 : else if (tree_int_cst_lt (n2, n1))
12702 : return -1;
12703 : /* The n1 == n2 case. */
12704 24 : bool c1 = conv_binds_to_array_of_unknown_bound (t1);
12705 24 : bool c2 = conv_binds_to_array_of_unknown_bound (t2);
12706 24 : if (c1 && !c2)
12707 : return -1;
12708 6 : else if (!c1 && c2)
12709 : return 1;
12710 : else
12711 : return 0;
12712 : }
12713 : else
12714 : {
12715 : /* For ambiguous or aggregate conversions, use the target type as
12716 : a proxy for the conversion function. */
12717 15512 : if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
12718 : return 0;
12719 : }
12720 :
12721 : /* We can just fall through here, after setting up
12722 : FROM_TYPE1 and FROM_TYPE2. */
12723 711459 : from_type1 = t1->type;
12724 711459 : from_type2 = t2->type;
12725 711459 : }
12726 : else
12727 : {
12728 : conversion *t1;
12729 : conversion *t2;
12730 :
12731 : /* We're dealing with two standard conversion sequences.
12732 :
12733 : [over.ics.rank]
12734 :
12735 : Standard conversion sequence S1 is a better conversion
12736 : sequence than standard conversion sequence S2 if
12737 :
12738 : --S1 is a proper subsequence of S2 (comparing the conversion
12739 : sequences in the canonical form defined by _over.ics.scs_,
12740 : excluding any Lvalue Transformation; the identity
12741 : conversion sequence is considered to be a subsequence of
12742 : any non-identity conversion sequence */
12743 :
12744 : t1 = ics1;
12745 55225069 : while (t1->kind != ck_identity)
12746 16623947 : t1 = next_conversion (t1);
12747 38601122 : from_type1 = t1->type;
12748 :
12749 38601122 : t2 = ics2;
12750 55119523 : while (t2->kind != ck_identity)
12751 16518401 : t2 = next_conversion (t2);
12752 38601122 : from_type2 = t2->type;
12753 : }
12754 :
12755 : /* One sequence can only be a subsequence of the other if they start with
12756 : the same type. They can start with different types when comparing the
12757 : second standard conversion sequence in two user-defined conversion
12758 : sequences. */
12759 39312581 : if (same_type_p (from_type1, from_type2))
12760 : {
12761 38886421 : if (is_subseq (ics1, ics2))
12762 : return 1;
12763 38879648 : if (is_subseq (ics2, ics1))
12764 : return -1;
12765 : }
12766 :
12767 : /* [over.ics.rank]
12768 :
12769 : Or, if not that,
12770 :
12771 : --the rank of S1 is better than the rank of S2 (by the rules
12772 : defined below):
12773 :
12774 : Standard conversion sequences are ordered by their ranks: an Exact
12775 : Match is a better conversion than a Promotion, which is a better
12776 : conversion than a Conversion.
12777 :
12778 : Two conversion sequences with the same rank are indistinguishable
12779 : unless one of the following rules applies:
12780 :
12781 : --A conversion that does not a convert a pointer, pointer to member,
12782 : or std::nullptr_t to bool is better than one that does.
12783 :
12784 : The ICS_STD_RANK automatically handles the pointer-to-bool rule,
12785 : so that we do not have to check it explicitly. */
12786 39305750 : if (ics1->rank < ics2->rank)
12787 : return 1;
12788 39305670 : else if (ics2->rank < ics1->rank)
12789 : return -1;
12790 :
12791 39305670 : to_type1 = ics1->type;
12792 39305670 : to_type2 = ics2->type;
12793 :
12794 : /* A conversion from scalar arithmetic type to complex is worse than a
12795 : conversion between scalar arithmetic types. */
12796 39305670 : if (same_type_p (from_type1, from_type2)
12797 38879510 : && ARITHMETIC_TYPE_P (from_type1)
12798 7984466 : && ARITHMETIC_TYPE_P (to_type1)
12799 7984293 : && ARITHMETIC_TYPE_P (to_type2)
12800 39305670 : && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
12801 7984239 : != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
12802 : {
12803 974 : if (TREE_CODE (to_type1) == COMPLEX_TYPE)
12804 : return -1;
12805 : else
12806 : return 1;
12807 : }
12808 :
12809 39304696 : {
12810 : /* A conversion in either direction between floating-point type FP1 and
12811 : floating-point type FP2 is better than a conversion in the same
12812 : direction between FP1 and arithmetic type T3 if
12813 : - the floating-point conversion rank of FP1 is equal to the rank of
12814 : FP2, and
12815 : - T3 is not a floating-point type, or T3 is a floating-point type
12816 : whose rank is not equal to the rank of FP1, or the floating-point
12817 : conversion subrank of FP2 is greater than the subrank of T3. */
12818 39304696 : tree fp1 = from_type1;
12819 39304696 : tree fp2 = to_type1;
12820 39304696 : tree fp3 = from_type2;
12821 39304696 : tree t3 = to_type2;
12822 39304696 : int ret = 1;
12823 39304696 : if (TYPE_MAIN_VARIANT (fp2) == TYPE_MAIN_VARIANT (t3))
12824 : {
12825 34412945 : std::swap (fp1, fp2);
12826 34412945 : std::swap (fp3, t3);
12827 : }
12828 39304696 : if (TYPE_MAIN_VARIANT (fp1) == TYPE_MAIN_VARIANT (fp3)
12829 39304609 : && SCALAR_FLOAT_TYPE_P (fp1)
12830 : /* Only apply this rule if at least one of the 3 types is
12831 : extended floating-point type, otherwise keep them as
12832 : before for compatibility reasons with types like __float128.
12833 : float, double and long double alone have different conversion
12834 : ranks and so when just those 3 types are involved, this
12835 : rule doesn't trigger. */
12836 43626897 : && (extended_float_type_p (fp1)
12837 4250657 : || (SCALAR_FLOAT_TYPE_P (fp2) && extended_float_type_p (fp2))
12838 4160746 : || (SCALAR_FLOAT_TYPE_P (t3) && extended_float_type_p (t3))))
12839 : {
12840 161455 : if (TREE_CODE (fp2) != REAL_TYPE)
12841 : {
12842 33180 : ret = -ret;
12843 33180 : std::swap (fp2, t3);
12844 : }
12845 161455 : if (SCALAR_FLOAT_TYPE_P (fp2))
12846 : {
12847 : /* cp_compare_floating_point_conversion_ranks returns -1, 0 or 1
12848 : if the conversion rank is equal (-1 or 1 if the subrank is
12849 : different). */
12850 143242 : if (IN_RANGE (cp_compare_floating_point_conversion_ranks (fp1,
12851 : fp2),
12852 : -1, 1))
12853 : {
12854 : /* Conversion ranks of FP1 and FP2 are equal. */
12855 93506 : if (TREE_CODE (t3) != REAL_TYPE
12856 93506 : || !IN_RANGE (cp_compare_floating_point_conversion_ranks
12857 : (fp1, t3),
12858 : -1, 1))
12859 : /* FP1 <-> FP2 conversion is better. */
12860 92760 : return ret;
12861 746 : int c = cp_compare_floating_point_conversion_ranks (fp2, t3);
12862 746 : gcc_assert (IN_RANGE (c, -1, 1));
12863 746 : if (c == 1)
12864 : /* Conversion subrank of FP2 is greater than subrank of T3.
12865 : FP1 <-> FP2 conversion is better. */
12866 : return ret;
12867 746 : else if (c == -1)
12868 : /* Conversion subrank of FP2 is less than subrank of T3.
12869 : FP1 <-> T3 conversion is better. */
12870 0 : return -ret;
12871 : }
12872 49736 : else if (SCALAR_FLOAT_TYPE_P (t3)
12873 49736 : && IN_RANGE (cp_compare_floating_point_conversion_ranks
12874 : (fp1, t3),
12875 : -1, 1))
12876 : /* Conversion ranks of FP1 and FP2 are not equal, conversion
12877 : ranks of FP1 and T3 are equal.
12878 : FP1 <-> T3 conversion is better. */
12879 9966 : return -ret;
12880 : }
12881 : }
12882 : }
12883 :
12884 39201970 : if (TYPE_PTR_P (from_type1)
12885 4882103 : && TYPE_PTR_P (from_type2)
12886 4882087 : && TYPE_PTR_P (to_type1)
12887 4882063 : && TYPE_PTR_P (to_type2))
12888 : {
12889 4882063 : deref_from_type1 = TREE_TYPE (from_type1);
12890 4882063 : deref_from_type2 = TREE_TYPE (from_type2);
12891 4882063 : deref_to_type1 = TREE_TYPE (to_type1);
12892 4882063 : deref_to_type2 = TREE_TYPE (to_type2);
12893 : }
12894 : /* The rules for pointers to members A::* are just like the rules
12895 : for pointers A*, except opposite: if B is derived from A then
12896 : A::* converts to B::*, not vice versa. For that reason, we
12897 : switch the from_ and to_ variables here. */
12898 59 : else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2)
12899 59 : && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2))
12900 34319907 : || (TYPE_PTRMEMFUNC_P (from_type1)
12901 437 : && TYPE_PTRMEMFUNC_P (from_type2)
12902 437 : && TYPE_PTRMEMFUNC_P (to_type1)
12903 437 : && TYPE_PTRMEMFUNC_P (to_type2)))
12904 : {
12905 496 : deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
12906 496 : deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
12907 496 : deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
12908 496 : deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
12909 : }
12910 :
12911 4882559 : if (deref_from_type1 != NULL_TREE
12912 4882559 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
12913 301476 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
12914 : {
12915 : /* This was one of the pointer or pointer-like conversions.
12916 :
12917 : [over.ics.rank]
12918 :
12919 : --If class B is derived directly or indirectly from class A,
12920 : conversion of B* to A* is better than conversion of B* to
12921 : void*, and conversion of A* to void* is better than
12922 : conversion of B* to void*. */
12923 301476 : if (VOID_TYPE_P (deref_to_type1)
12924 57 : && VOID_TYPE_P (deref_to_type2))
12925 : {
12926 14 : if (is_properly_derived_from (deref_from_type1,
12927 : deref_from_type2))
12928 : return -1;
12929 14 : else if (is_properly_derived_from (deref_from_type2,
12930 : deref_from_type1))
12931 : return 1;
12932 : }
12933 301462 : else if (VOID_TYPE_P (deref_to_type1)
12934 301419 : || VOID_TYPE_P (deref_to_type2))
12935 : {
12936 47 : if (same_type_p (deref_from_type1, deref_from_type2))
12937 : {
12938 47 : if (VOID_TYPE_P (deref_to_type2))
12939 : {
12940 4 : if (is_properly_derived_from (deref_from_type1,
12941 : deref_to_type1))
12942 : return 1;
12943 : }
12944 : /* We know that DEREF_TO_TYPE1 is `void' here. */
12945 43 : else if (is_properly_derived_from (deref_from_type1,
12946 : deref_to_type2))
12947 : return -1;
12948 : }
12949 : }
12950 301415 : else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
12951 301415 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
12952 : {
12953 : /* [over.ics.rank]
12954 :
12955 : --If class B is derived directly or indirectly from class A
12956 : and class C is derived directly or indirectly from B,
12957 :
12958 : --conversion of C* to B* is better than conversion of C* to
12959 : A*,
12960 :
12961 : --conversion of B* to A* is better than conversion of C* to
12962 : A* */
12963 301415 : if (same_type_p (deref_from_type1, deref_from_type2))
12964 : {
12965 301409 : if (is_properly_derived_from (deref_to_type1,
12966 : deref_to_type2))
12967 : return 1;
12968 301409 : else if (is_properly_derived_from (deref_to_type2,
12969 : deref_to_type1))
12970 : return -1;
12971 : }
12972 6 : else if (same_type_p (deref_to_type1, deref_to_type2))
12973 : {
12974 6 : if (is_properly_derived_from (deref_from_type2,
12975 : deref_from_type1))
12976 : return 1;
12977 0 : else if (is_properly_derived_from (deref_from_type1,
12978 : deref_from_type2))
12979 : return -1;
12980 : }
12981 : }
12982 : }
12983 77800988 : else if (CLASS_TYPE_P (non_reference (from_type1))
12984 63005373 : && same_type_p (from_type1, from_type2))
12985 : {
12986 23740443 : tree from = non_reference (from_type1);
12987 :
12988 : /* [over.ics.rank]
12989 :
12990 : --binding of an expression of type C to a reference of type
12991 : B& is better than binding an expression of type C to a
12992 : reference of type A&
12993 :
12994 : --conversion of C to B is better than conversion of C to A, */
12995 23740443 : if (is_properly_derived_from (from, to_type1)
12996 23740443 : && is_properly_derived_from (from, to_type2))
12997 : {
12998 951367 : if (is_properly_derived_from (to_type1, to_type2))
12999 : return 1;
13000 947306 : else if (is_properly_derived_from (to_type2, to_type1))
13001 : return -1;
13002 : }
13003 : }
13004 30320102 : else if (CLASS_TYPE_P (non_reference (to_type1))
13005 15524487 : && same_type_p (to_type1, to_type2))
13006 : {
13007 7 : tree to = non_reference (to_type1);
13008 :
13009 : /* [over.ics.rank]
13010 :
13011 : --binding of an expression of type B to a reference of type
13012 : A& is better than binding an expression of type C to a
13013 : reference of type A&,
13014 :
13015 : --conversion of B to A is better than conversion of C to A */
13016 7 : if (is_properly_derived_from (from_type1, to)
13017 7 : && is_properly_derived_from (from_type2, to))
13018 : {
13019 3 : if (is_properly_derived_from (from_type2, from_type1))
13020 : return 1;
13021 3 : else if (is_properly_derived_from (from_type1, from_type2))
13022 : return -1;
13023 : }
13024 : }
13025 :
13026 : /* [over.ics.rank]
13027 :
13028 : --S1 and S2 differ only in their qualification conversion and yield
13029 : similar types T1 and T2 (_conv.qual_), respectively, and the cv-
13030 : qualification signature of type T1 is a proper subset of the cv-
13031 : qualification signature of type T2 */
13032 39113490 : if (ics1->kind == ck_qual
13033 355 : && ics2->kind == ck_qual
13034 39113845 : && same_type_p (from_type1, from_type2))
13035 : {
13036 355 : int result = comp_cv_qual_signature (to_type1, to_type2);
13037 355 : if (result != 0)
13038 : return result;
13039 : }
13040 :
13041 : /* [over.ics.rank]
13042 :
13043 : --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
13044 : to an implicit object parameter of a non-static member function
13045 : declared without a ref-qualifier, and either S1 binds an lvalue
13046 : reference to an lvalue and S2 binds an rvalue reference or S1 binds an
13047 : rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x
13048 : draft standard, 13.3.3.2)
13049 :
13050 : --S1 and S2 are reference bindings (_dcl.init.ref_), and the
13051 : types to which the references refer are the same type except for
13052 : top-level cv-qualifiers, and the type to which the reference
13053 : initialized by S2 refers is more cv-qualified than the type to
13054 : which the reference initialized by S1 refers.
13055 :
13056 : DR 1328 [over.match.best]: the context is an initialization by
13057 : conversion function for direct reference binding (13.3.1.6) of a
13058 : reference to function type, the return type of F1 is the same kind of
13059 : reference (i.e. lvalue or rvalue) as the reference being initialized,
13060 : and the return type of F2 is not. */
13061 :
13062 39113404 : if (ref_conv1 && ref_conv2)
13063 : {
13064 18150227 : if (!ref_conv1->this_p && !ref_conv2->this_p
13065 17898791 : && (ref_conv1->rvaluedness_matches_p
13066 17898791 : != ref_conv2->rvaluedness_matches_p)
13067 34164188 : && (same_type_p (ref_conv1->type, ref_conv2->type)
13068 9574357 : || (TYPE_REF_IS_RVALUE (ref_conv1->type)
13069 9574357 : != TYPE_REF_IS_RVALUE (ref_conv2->type))))
13070 : {
13071 9574085 : if (ref_conv1->bad_p
13072 9574085 : && !same_type_p (TREE_TYPE (ref_conv1->type),
13073 : TREE_TYPE (ref_conv2->type)))
13074 : /* Don't prefer a bad conversion that drops cv-quals to a bad
13075 : conversion with the wrong rvalueness. */
13076 : return 0;
13077 9572622 : return (ref_conv1->rvaluedness_matches_p
13078 9572622 : - ref_conv2->rvaluedness_matches_p);
13079 : }
13080 :
13081 15015743 : if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
13082 : {
13083 : /* Per P0388R4:
13084 :
13085 : void f (int(&)[]), // (1)
13086 : f (int(&)[1]), // (2)
13087 : f (int*); // (3)
13088 :
13089 : (2) is better than (1), but (3) should be equal to (1) and to
13090 : (2). For that reason we don't use ck_qual for (1) which would
13091 : give it the cr_exact rank while (3) remains ck_identity.
13092 : Therefore we compare (1) and (2) here. For (1) we'll have
13093 :
13094 : ck_ref_bind <- ck_identity
13095 : int[] & int[1]
13096 :
13097 : so to handle this we must look at ref_conv. */
13098 15015160 : bool c1 = conv_binds_to_array_of_unknown_bound (ref_conv1);
13099 15015160 : bool c2 = conv_binds_to_array_of_unknown_bound (ref_conv2);
13100 15015160 : if (c1 && !c2)
13101 : return -1;
13102 15015154 : else if (!c1 && c2)
13103 : return 1;
13104 :
13105 15015154 : int q1 = cp_type_quals (TREE_TYPE (ref_conv1->type));
13106 15015154 : int q2 = cp_type_quals (TREE_TYPE (ref_conv2->type));
13107 15015154 : if (ref_conv1->bad_p)
13108 : {
13109 : /* Prefer the one that drops fewer cv-quals. */
13110 1606 : tree ftype = next_conversion (ref_conv1)->type;
13111 1606 : int fquals = cp_type_quals (ftype);
13112 1606 : q1 ^= fquals;
13113 1606 : q2 ^= fquals;
13114 : }
13115 15015154 : return comp_cv_qualification (q2, q1);
13116 : }
13117 : }
13118 :
13119 : /* [over.ics.rank]
13120 :
13121 : Per CWG 1601:
13122 : -- A conversion that promotes an enumeration whose underlying type
13123 : is fixed to its underlying type is better than one that promotes to
13124 : the promoted underlying type, if the two are different. */
13125 14524159 : if (ics1->rank == cr_promotion
13126 147 : && ics2->rank == cr_promotion
13127 147 : && UNSCOPED_ENUM_P (from_type1)
13128 27 : && ENUM_FIXED_UNDERLYING_TYPE_P (from_type1)
13129 14524183 : && same_type_p (from_type1, from_type2))
13130 : {
13131 24 : tree utype = ENUM_UNDERLYING_TYPE (from_type1);
13132 24 : tree prom = type_promotes_to (from_type1);
13133 24 : if (!same_type_p (utype, prom))
13134 : {
13135 12 : if (same_type_p (to_type1, utype)
13136 12 : && same_type_p (to_type2, prom))
13137 : return 1;
13138 6 : else if (same_type_p (to_type2, utype)
13139 6 : && same_type_p (to_type1, prom))
13140 : return -1;
13141 : }
13142 : }
13143 :
13144 : /* Neither conversion sequence is better than the other. */
13145 : return 0;
13146 : }
13147 :
13148 : /* The source type for this standard conversion sequence. */
13149 :
13150 : static tree
13151 9 : source_type (conversion *t)
13152 : {
13153 9 : return strip_standard_conversion (t)->type;
13154 : }
13155 :
13156 : /* Note a warning about preferring WINNER to LOSER. We do this by storing
13157 : a pointer to LOSER and re-running joust to produce the warning if WINNER
13158 : is actually used. */
13159 :
13160 : static void
13161 494 : add_warning (struct z_candidate *winner, struct z_candidate *loser)
13162 : {
13163 494 : candidate_warning *cw = (candidate_warning *)
13164 0 : conversion_obstack_alloc (sizeof (candidate_warning));
13165 494 : cw->loser = loser;
13166 494 : cw->next = winner->warnings;
13167 494 : winner->warnings = cw;
13168 494 : }
13169 :
13170 : /* CAND is a constructor candidate in joust in C++17 and up. If it copies a
13171 : prvalue returned from a conversion function, return true. Otherwise, return
13172 : false. */
13173 :
13174 : static bool
13175 863690 : joust_maybe_elide_copy (z_candidate *cand)
13176 : {
13177 863690 : tree fn = cand->fn;
13178 2168374 : if (!DECL_COPY_CONSTRUCTOR_P (fn) && !DECL_MOVE_CONSTRUCTOR_P (fn))
13179 440799 : return false;
13180 422891 : conversion *conv = cand->convs[0];
13181 422891 : if (conv->kind == ck_ambig)
13182 : return false;
13183 422889 : gcc_checking_assert (conv->kind == ck_ref_bind);
13184 422889 : conv = next_conversion (conv);
13185 422889 : if (conv->kind == ck_user && !TYPE_REF_P (conv->type))
13186 : {
13187 342 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
13188 : (conv->type, DECL_CONTEXT (fn)));
13189 342 : z_candidate *uc = conv->cand;
13190 342 : if (DECL_CONV_FN_P (uc->fn))
13191 : return true;
13192 : }
13193 : return false;
13194 : }
13195 :
13196 : /* Return the class that CAND's implicit object parameter refers to. */
13197 :
13198 : static tree
13199 291046 : class_of_implicit_object (z_candidate *cand)
13200 : {
13201 291046 : if (!DECL_IOBJ_MEMBER_FUNCTION_P (cand->fn))
13202 : return NULL_TREE;
13203 :
13204 : /* "For conversion functions that are implicit object member functions,
13205 : the function is considered to be a member of the class of the implied
13206 : object argument for the purpose of defining the type of the implicit
13207 : object parameter." */
13208 291046 : if (DECL_CONV_FN_P (cand->fn))
13209 0 : return TYPE_MAIN_VARIANT (TREE_TYPE (cand->first_arg));
13210 :
13211 : /* "For non-conversion functions that are implicit object member
13212 : functions nominated by a using-declaration in a derived class, the
13213 : function is considered to be a member of the derived class for the
13214 : purpose of defining the type of the implicit object parameter."
13215 :
13216 : That derived class is reflected in the conversion_path binfo. */
13217 291046 : return BINFO_TYPE (cand->conversion_path);
13218 : }
13219 :
13220 : /* Return whether the first parameter of C1 matches the second parameter
13221 : of C2. */
13222 :
13223 : static bool
13224 554062 : reversed_match (z_candidate *c1, z_candidate *c2)
13225 : {
13226 554062 : tree fn1 = c1->fn;
13227 554062 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (c2->fn));
13228 554062 : tree parm2 = TREE_VALUE (TREE_CHAIN (parms2));
13229 554062 : if (DECL_IOBJ_MEMBER_FUNCTION_P (fn1))
13230 : {
13231 291046 : tree ctx = class_of_implicit_object (c1);
13232 291046 : return iobj_parm_corresponds_to (fn1, parm2, ctx);
13233 : }
13234 : else
13235 : {
13236 263016 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn1));
13237 263016 : tree parm1 = TREE_VALUE (parms1);
13238 263016 : return same_type_p (parm1, parm2);
13239 : }
13240 : }
13241 :
13242 : /* True if the defining declarations of the two candidates have equivalent
13243 : parameters. MATCH_KIND controls whether we're trying to compare the
13244 : original declarations (for a warning) or the actual candidates. */
13245 :
13246 : enum class pmatch { original, current };
13247 :
13248 : static bool
13249 4908013 : cand_parms_match (z_candidate *c1, z_candidate *c2, pmatch match_kind)
13250 : {
13251 4908013 : tree fn1 = c1->fn;
13252 4908013 : tree fn2 = c2->fn;
13253 4908013 : bool reversed = (match_kind == pmatch::current
13254 4908013 : && c1->reversed () != c2->reversed ());
13255 4908013 : if (fn1 == fn2 && !reversed)
13256 : return true;
13257 4907716 : if (identifier_p (fn1) || identifier_p (fn2))
13258 : return false;
13259 4907699 : if (match_kind == pmatch::original)
13260 : {
13261 : /* We don't look at c1->template_decl because that's only set for
13262 : primary templates, not e.g. non-template member functions of
13263 : class templates. */
13264 22 : tree t1 = most_general_template (fn1);
13265 22 : tree t2 = most_general_template (fn2);
13266 22 : if (t1 || t2)
13267 : {
13268 15 : if (!t1 || !t2)
13269 : return false;
13270 15 : if (t1 == t2)
13271 : return true;
13272 3 : fn1 = DECL_TEMPLATE_RESULT (t1);
13273 3 : fn2 = DECL_TEMPLATE_RESULT (t2);
13274 : }
13275 : }
13276 :
13277 4907687 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn1));
13278 4907687 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (fn2));
13279 :
13280 9599207 : if (DECL_FUNCTION_MEMBER_P (fn1)
13281 4908311 : && DECL_FUNCTION_MEMBER_P (fn2))
13282 : {
13283 216777 : tree base1 = DECL_CONTEXT (strip_inheriting_ctors (fn1));
13284 216777 : tree base2 = DECL_CONTEXT (strip_inheriting_ctors (fn2));
13285 216777 : if (base1 != base2)
13286 146005 : return false;
13287 :
13288 216600 : if (reversed)
13289 145828 : return (reversed_match (c1, c2)
13290 145828 : && reversed_match (c2, c1));
13291 :
13292 : /* Use object_parms_correspond to simplify comparing iobj/xobj/static
13293 : member functions. */
13294 70772 : if (!object_parms_correspond (fn1, fn2, base1))
13295 : return false;
13296 :
13297 : /* We just compared the object parameters, if they don't correspond
13298 : we already returned false. */
13299 212316 : auto skip_parms = [] (tree fn, tree parms)
13300 : {
13301 141544 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
13302 1020 : return TREE_CHAIN (parms);
13303 : else
13304 140524 : return skip_artificial_parms_for (fn, parms);
13305 : };
13306 70772 : parms1 = skip_parms (fn1, parms1);
13307 70772 : parms2 = skip_parms (fn2, parms2);
13308 : }
13309 4690910 : else if (reversed)
13310 131473 : return (reversed_match (c1, c2)
13311 131473 : && reversed_match (c2, c1));
13312 4630209 : return compparms (parms1, parms2);
13313 : }
13314 :
13315 : /* True iff FN is a copy or move constructor or assignment operator. */
13316 :
13317 : static bool
13318 20303884 : sfk_copy_or_move (tree fn)
13319 : {
13320 20303884 : if (TREE_CODE (fn) != FUNCTION_DECL)
13321 : return false;
13322 20303794 : special_function_kind sfk = special_function_p (fn);
13323 20303794 : return sfk >= sfk_copy_constructor && sfk <= sfk_move_assignment;
13324 : }
13325 :
13326 : /* Compare two candidates for overloading as described in
13327 : [over.match.best]. Return values:
13328 :
13329 : 1: cand1 is better than cand2
13330 : -1: cand2 is better than cand1
13331 : 0: cand1 and cand2 are indistinguishable */
13332 :
13333 : static int
13334 76882767 : joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
13335 : tsubst_flags_t complain)
13336 : {
13337 76882767 : int winner = 0;
13338 76882767 : int off1 = 0, off2 = 0;
13339 76882767 : size_t i;
13340 76882767 : size_t len;
13341 :
13342 : /* Candidates that involve bad conversions are always worse than those
13343 : that don't. */
13344 76882767 : if (cand1->viable > cand2->viable)
13345 : return 1;
13346 61690083 : if (cand1->viable < cand2->viable)
13347 : return -1;
13348 :
13349 : /* If we have two pseudo-candidates for conversions to the same type,
13350 : or two candidates for the same function, arbitrarily pick one. */
13351 61690083 : if (cand1->fn == cand2->fn
13352 1863990 : && cand1->reversed () == cand2->reversed ()
13353 62506287 : && (IS_TYPE_OR_DECL_P (cand1->fn)))
13354 : return 1;
13355 :
13356 : /* Prefer a non-deleted function over an implicitly deleted move
13357 : constructor or assignment operator. This differs slightly from the
13358 : wording for issue 1402 (which says the move op is ignored by overload
13359 : resolution), but this way produces better error messages. */
13360 61690083 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13361 57399548 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13362 119079891 : && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn))
13363 : {
13364 2474209 : if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn)
13365 2064822 : && move_fn_p (cand1->fn))
13366 : return -1;
13367 3707463 : if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn)
13368 2998673 : && move_fn_p (cand2->fn))
13369 : return 1;
13370 : }
13371 :
13372 : /* a viable function F1
13373 : is defined to be a better function than another viable function F2 if
13374 : for all arguments i, ICSi(F1) is not a worse conversion sequence than
13375 : ICSi(F2), and then */
13376 :
13377 : /* for some argument j, ICSj(F1) is a better conversion sequence than
13378 : ICSj(F2) */
13379 :
13380 : /* For comparing static and non-static member functions, we ignore
13381 : the implicit object parameter of the non-static function. The
13382 : standard says to pretend that the static function has an object
13383 : parm, but that won't work with operator overloading. */
13384 61675536 : len = cand1->num_convs;
13385 61675536 : if (len != cand2->num_convs)
13386 : {
13387 758 : int static_1 = (TREE_CODE (cand1->fn) == FUNCTION_DECL
13388 758 : && DECL_STATIC_FUNCTION_P (cand1->fn));
13389 758 : int static_2 = (TREE_CODE (cand2->fn) == FUNCTION_DECL
13390 758 : && DECL_STATIC_FUNCTION_P (cand2->fn));
13391 :
13392 758 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13393 193 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13394 193 : && DECL_CONSTRUCTOR_P (cand1->fn)
13395 764 : && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
13396 : /* We're comparing a near-match list constructor and a near-match
13397 : non-list constructor. Just treat them as unordered. */
13398 : return 0;
13399 :
13400 752 : gcc_assert (static_1 != static_2);
13401 :
13402 752 : if (static_1)
13403 : {
13404 : /* C++23 [over.best.ics.general] says:
13405 : When the parameter is the implicit object parameter of a static
13406 : member function, the implicit conversion sequence is a standard
13407 : conversion sequence that is neither better nor worse than any
13408 : other standard conversion sequence. */
13409 72 : if (CONVERSION_RANK (cand2->convs[0]) >= cr_user)
13410 0 : winner = 1;
13411 : off2 = 1;
13412 : }
13413 : else
13414 : {
13415 680 : if (CONVERSION_RANK (cand1->convs[0]) >= cr_user)
13416 565 : winner = -1;
13417 680 : off1 = 1;
13418 680 : --len;
13419 : }
13420 : }
13421 :
13422 143284428 : for (i = 0; i < len; ++i)
13423 : {
13424 81610223 : conversion *t1 = cand1->convs[i + off1];
13425 81610223 : conversion *t2 = cand2->convs[i + off2];
13426 81610223 : int comp = compare_ics (t1, t2);
13427 :
13428 81610223 : if (comp != 0)
13429 : {
13430 55527476 : if ((complain & tf_warning)
13431 45824904 : && warn_sign_promo
13432 82 : && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
13433 : == cr_std + cr_promotion)
13434 50 : && t1->kind == ck_std
13435 31 : && t2->kind == ck_std
13436 31 : && TREE_CODE (t1->type) == INTEGER_TYPE
13437 31 : && TREE_CODE (t2->type) == INTEGER_TYPE
13438 31 : && (TYPE_PRECISION (t1->type)
13439 31 : == TYPE_PRECISION (t2->type))
13440 55527507 : && (TYPE_UNSIGNED (next_conversion (t1)->type)
13441 0 : || (TREE_CODE (next_conversion (t1)->type)
13442 : == ENUMERAL_TYPE)))
13443 : {
13444 31 : tree type = next_conversion (t1)->type;
13445 31 : tree type1, type2;
13446 31 : struct z_candidate *w, *l;
13447 31 : if (comp > 0)
13448 : type1 = t1->type, type2 = t2->type,
13449 : w = cand1, l = cand2;
13450 : else
13451 8 : type1 = t2->type, type2 = t1->type,
13452 8 : w = cand2, l = cand1;
13453 :
13454 31 : if (warn)
13455 : {
13456 9 : warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
13457 : type, type1, type2);
13458 9 : warning (OPT_Wsign_promo, " in call to %qD", w->fn);
13459 : }
13460 : else
13461 22 : add_warning (w, l);
13462 : }
13463 :
13464 55527476 : if (winner && comp != winner)
13465 : {
13466 : /* Ambiguity between normal and reversed comparison operators
13467 : with the same parameter types. P2468 decided not to go with
13468 : this approach to resolving the ambiguity, so pedwarn. */
13469 1325 : if ((complain & tf_warning_or_error)
13470 751 : && (cand1->reversed () != cand2->reversed ())
13471 1661 : && cand_parms_match (cand1, cand2, pmatch::original))
13472 : {
13473 321 : struct z_candidate *w, *l;
13474 321 : if (cand2->reversed ())
13475 : winner = 1, w = cand1, l = cand2;
13476 : else
13477 299 : winner = -1, w = cand2, l = cand1;
13478 321 : if (warn)
13479 : {
13480 22 : auto_diagnostic_group d;
13481 22 : if (pedwarn (input_location, 0,
13482 : "C++20 says that these are ambiguous, "
13483 : "even though the second is reversed:"))
13484 : {
13485 22 : print_z_candidate (input_location,
13486 : N_("candidate 1:"), w);
13487 22 : print_z_candidate (input_location,
13488 : N_("candidate 2:"), l);
13489 22 : if (w->fn == l->fn
13490 17 : && DECL_IOBJ_MEMBER_FUNCTION_P (w->fn)
13491 37 : && (type_memfn_quals (TREE_TYPE (w->fn))
13492 15 : & TYPE_QUAL_CONST) == 0)
13493 : {
13494 : /* Suggest adding const to
13495 : struct A { bool operator==(const A&); }; */
13496 12 : tree parmtype
13497 12 : = FUNCTION_FIRST_USER_PARMTYPE (w->fn);
13498 12 : parmtype = TREE_VALUE (parmtype);
13499 12 : if (TYPE_REF_P (parmtype)
13500 12 : && TYPE_READONLY (TREE_TYPE (parmtype))
13501 24 : && (same_type_ignoring_top_level_qualifiers_p
13502 12 : (TREE_TYPE (parmtype),
13503 12 : DECL_CONTEXT (w->fn))))
13504 12 : inform (DECL_SOURCE_LOCATION (w->fn),
13505 : "try making the operator a %<const%> "
13506 : "member function");
13507 : }
13508 : }
13509 22 : }
13510 : else
13511 299 : add_warning (w, l);
13512 321 : return winner;
13513 : }
13514 :
13515 1004 : winner = 0;
13516 1004 : goto tweak;
13517 : }
13518 : winner = comp;
13519 : }
13520 : }
13521 :
13522 : /* warn about confusing overload resolution for user-defined conversions,
13523 : either between a constructor and a conversion op, or between two
13524 : conversion ops. */
13525 61674205 : if ((complain & tf_warning)
13526 : /* In C++17, the constructor might have been elided, which means that
13527 : an originally null ->second_conv could become non-null. */
13528 50300987 : && winner && warn_conversion && cand1->second_conv && cand2->second_conv
13529 54 : && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
13530 61674232 : && winner != compare_ics (cand1->second_conv, cand2->second_conv))
13531 : {
13532 27 : struct z_candidate *w, *l;
13533 27 : bool give_warning = false;
13534 :
13535 27 : if (winner == 1)
13536 : w = cand1, l = cand2;
13537 : else
13538 6 : w = cand2, l = cand1;
13539 :
13540 : /* We don't want to complain about `X::operator T1 ()'
13541 : beating `X::operator T2 () const', when T2 is a no less
13542 : cv-qualified version of T1. */
13543 27 : if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
13544 39 : && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
13545 : {
13546 6 : tree t = TREE_TYPE (TREE_TYPE (l->fn));
13547 6 : tree f = TREE_TYPE (TREE_TYPE (w->fn));
13548 :
13549 6 : if (TREE_CODE (t) == TREE_CODE (f) && INDIRECT_TYPE_P (t))
13550 : {
13551 6 : t = TREE_TYPE (t);
13552 6 : f = TREE_TYPE (f);
13553 : }
13554 6 : if (!comp_ptr_ttypes (t, f))
13555 : give_warning = true;
13556 : }
13557 : else
13558 : give_warning = true;
13559 :
13560 : if (!give_warning)
13561 : /*NOP*/;
13562 21 : else if (warn)
13563 : {
13564 9 : tree source = source_type (w->convs[0]);
13565 9 : if (INDIRECT_TYPE_P (source))
13566 6 : source = TREE_TYPE (source);
13567 9 : auto_diagnostic_group d;
13568 9 : if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
13569 18 : && warning (OPT_Wconversion, " for conversion from %qH to %qI",
13570 9 : source, w->second_conv->type))
13571 : {
13572 9 : inform (input_location, " because conversion sequence "
13573 : "for the argument is better");
13574 : }
13575 9 : }
13576 : else
13577 12 : add_warning (w, l);
13578 : }
13579 :
13580 61674205 : if (winner)
13581 : return winner;
13582 :
13583 : /* DR 495 moved this tiebreaker above the template ones. */
13584 : /* or, if not that,
13585 : the context is an initialization by user-defined conversion (see
13586 : _dcl.init_ and _over.match.user_) and the standard conversion
13587 : sequence from the return type of F1 to the destination type (i.e.,
13588 : the type of the entity being initialized) is a better conversion
13589 : sequence than the standard conversion sequence from the return type
13590 : of F2 to the destination type. */
13591 :
13592 10152094 : if (cand1->second_conv)
13593 : {
13594 51682 : winner = compare_ics (cand1->second_conv, cand2->second_conv);
13595 51682 : if (winner)
13596 : return winner;
13597 : }
13598 :
13599 : /* CWG2735 (PR109247): A copy/move ctor/op= for which its operand uses an
13600 : explicit conversion (due to list-initialization) is worse. */
13601 10151942 : {
13602 10151942 : z_candidate *sp = nullptr;
13603 10151942 : if (sfk_copy_or_move (cand1->fn))
13604 633 : sp = cand1;
13605 10151942 : if (sfk_copy_or_move (cand2->fn))
13606 872098 : sp = sp ? nullptr : cand2;
13607 10151889 : if (sp)
13608 : {
13609 1745250 : conversion *conv = sp->convs[!DECL_CONSTRUCTOR_P (sp->fn)];
13610 872625 : if (conv->user_conv_p)
13611 2164 : for (; conv; conv = next_conversion (conv))
13612 1814 : if (conv->kind == ck_user
13613 732 : && DECL_P (conv->cand->fn)
13614 2546 : && DECL_NONCONVERTING_P (conv->cand->fn))
13615 388 : return (sp == cand1) ? -1 : 1;
13616 : }
13617 : }
13618 :
13619 : /* DR2327: C++17 copy elision in [over.match.ctor] (direct-init) context.
13620 : The standard currently says that only constructors are candidates, but if
13621 : one copies a prvalue returned by a conversion function we prefer that.
13622 :
13623 : Clang does something similar, as discussed at
13624 : http://lists.isocpp.org/core/2017/10/3166.php
13625 : http://lists.isocpp.org/core/2019/03/5721.php */
13626 6647378 : if (len == 1 && cxx_dialect >= cxx17
13627 6633157 : && DECL_P (cand1->fn)
13628 6633157 : && DECL_COMPLETE_CONSTRUCTOR_P (cand1->fn)
13629 10950253 : && !(cand1->flags & LOOKUP_ONLYCONVERTING))
13630 : {
13631 431845 : bool elided1 = joust_maybe_elide_copy (cand1);
13632 431845 : bool elided2 = joust_maybe_elide_copy (cand2);
13633 431845 : winner = elided1 - elided2;
13634 431845 : if (winner)
13635 : return winner;
13636 : }
13637 :
13638 : /* or, if not that,
13639 : F1 is a non-template function and F2 is a template function
13640 : specialization. */
13641 :
13642 10151327 : if (!cand1->template_decl && cand2->template_decl)
13643 : return 1;
13644 10092572 : else if (cand1->template_decl && !cand2->template_decl)
13645 : return -1;
13646 :
13647 : /* or, if not that,
13648 : F1 and F2 are template functions and the function template for F1 is
13649 : more specialized than the template for F2 according to the partial
13650 : ordering rules. */
13651 :
13652 8964763 : if (cand1->template_decl && cand2->template_decl)
13653 : {
13654 4044910 : winner = more_specialized_fn
13655 4044910 : (TI_TEMPLATE (cand1->template_decl),
13656 4044910 : TI_TEMPLATE (cand2->template_decl),
13657 : /* [temp.func.order]: The presence of unused ellipsis and default
13658 : arguments has no effect on the partial ordering of function
13659 : templates. add_function_candidate() will not have
13660 : counted the "this" argument for constructors. */
13661 8089820 : cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
13662 4044910 : if (winner)
13663 : return winner;
13664 : }
13665 :
13666 : /* F1 and F2 are non-template functions and
13667 : - they have the same non-object-parameter-type-lists ([dcl.fct]), and
13668 : - if they are member functions, both are direct members of the same
13669 : class, and
13670 : - if both are non-static member functions, they have the same types for
13671 : their object parameters, and
13672 : - F1 is more constrained than F2 according to the partial ordering of
13673 : constraints described in [temp.constr.order]. */
13674 6001556 : if (flag_concepts && DECL_P (cand1->fn) && DECL_P (cand2->fn)
13675 6001508 : && !cand1->template_decl && !cand2->template_decl
13676 10921730 : && cand_parms_match (cand1, cand2, pmatch::current))
13677 : {
13678 399209 : winner = more_constrained (cand1->fn, cand2->fn);
13679 399209 : if (winner)
13680 : return winner;
13681 : }
13682 :
13683 : /* F2 is a rewritten candidate (12.4.1.2) and F1 is not, or F1 and F2 are
13684 : rewritten candidates, and F2 is a synthesized candidate with reversed
13685 : order of parameters and F1 is not. */
13686 6006083 : if (cand1->rewritten ())
13687 : {
13688 1243685 : if (!cand2->rewritten ())
13689 : return -1;
13690 794598 : if (!cand1->reversed () && cand2->reversed ())
13691 : return 1;
13692 794598 : if (cand1->reversed () && !cand2->reversed ())
13693 : return -1;
13694 : }
13695 4762398 : else if (cand2->rewritten ())
13696 : return 1;
13697 :
13698 4695498 : if (deduction_guide_p (cand1->fn))
13699 : {
13700 4015 : gcc_assert (deduction_guide_p (cand2->fn));
13701 :
13702 : /* F1 and F2 are generated from class template argument deduction for a
13703 : class D, and F2 is generated from inheriting constructors from a base
13704 : class of D while F1 is not, and for each explicit function argument,
13705 : the corresponding parameters of F1 and F2 are either both ellipses or
13706 : have the same type. */
13707 4015 : bool inherited1 = inherited_guide_p (cand1->fn);
13708 4015 : bool inherited2 = inherited_guide_p (cand2->fn);
13709 4015 : if (int diff = inherited2 - inherited1)
13710 : {
13711 68 : for (i = 0; i < len; ++i)
13712 : {
13713 19 : conversion *t1 = cand1->convs[i + off1];
13714 19 : conversion *t2 = cand2->convs[i + off2];
13715 : /* ??? It seems the ellipses part of this tiebreaker isn't
13716 : needed since a mismatch should have broken the tie earlier
13717 : during ICS comparison. */
13718 19 : gcc_checking_assert (t1->ellipsis_p == t2->ellipsis_p);
13719 19 : if (!same_type_p (t1->type, t2->type))
13720 : break;
13721 : }
13722 51 : if (i == len)
13723 : return diff;
13724 : }
13725 :
13726 : /* F1 is generated from a deduction-guide (13.3.1.8) and F2 is not */
13727 : /* We distinguish between candidates from an explicit deduction guide and
13728 : candidates built from a constructor based on DECL_ARTIFICIAL. */
13729 3966 : int art1 = DECL_ARTIFICIAL (cand1->fn);
13730 3966 : int art2 = DECL_ARTIFICIAL (cand2->fn);
13731 3966 : if (art1 != art2)
13732 1409 : return art2 - art1;
13733 :
13734 2557 : if (art1)
13735 : {
13736 : /* Prefer the special copy guide over a declared copy/move
13737 : constructor. */
13738 2554 : if (copy_guide_p (cand1->fn))
13739 : return 1;
13740 86 : if (copy_guide_p (cand2->fn))
13741 : return -1;
13742 :
13743 : /* Prefer a candidate generated from a non-template constructor. */
13744 28 : int tg1 = template_guide_p (cand1->fn);
13745 28 : int tg2 = template_guide_p (cand2->fn);
13746 28 : if (tg1 != tg2)
13747 6 : return tg2 - tg1;
13748 : }
13749 : }
13750 :
13751 : /* F1 is a constructor for a class D, F2 is a constructor for a base class B
13752 : of D, and for all arguments the corresponding parameters of F1 and F2 have
13753 : the same type (CWG 2273/2277). */
13754 14074369 : if (DECL_INHERITED_CTOR (cand1->fn) || DECL_INHERITED_CTOR (cand2->fn))
13755 : {
13756 87 : tree base1 = DECL_CONTEXT (strip_inheriting_ctors (cand1->fn));
13757 87 : tree base2 = DECL_CONTEXT (strip_inheriting_ctors (cand2->fn));
13758 :
13759 87 : bool used1 = false;
13760 87 : bool used2 = false;
13761 87 : if (base1 == base2)
13762 : /* No difference. */;
13763 87 : else if (DERIVED_FROM_P (base1, base2))
13764 : used1 = true;
13765 18 : else if (DERIVED_FROM_P (base2, base1))
13766 : used2 = true;
13767 :
13768 78 : if (int diff = used2 - used1)
13769 : {
13770 105 : for (i = 0; i < len; ++i)
13771 : {
13772 36 : conversion *t1 = cand1->convs[i + off1];
13773 36 : conversion *t2 = cand2->convs[i + off2];
13774 36 : if (!same_type_p (t1->type, t2->type))
13775 : break;
13776 : }
13777 78 : if (i == len)
13778 : return diff;
13779 : }
13780 : }
13781 :
13782 : /* Check whether we can discard a builtin candidate, either because we
13783 : have two identical ones or matching builtin and non-builtin candidates.
13784 :
13785 : (Pedantically in the latter case the builtin which matched the user
13786 : function should not be added to the overload set, but we spot it here.
13787 :
13788 : [over.match.oper]
13789 : ... the builtin candidates include ...
13790 : - do not have the same parameter type list as any non-template
13791 : non-member candidate. */
13792 :
13793 4691439 : if (identifier_p (cand1->fn) || identifier_p (cand2->fn))
13794 : {
13795 93 : for (i = 0; i < len; ++i)
13796 69 : if (!same_type_p (cand1->convs[i]->type,
13797 : cand2->convs[i]->type))
13798 : break;
13799 43 : if (i == cand1->num_convs)
13800 : {
13801 24 : if (cand1->fn == cand2->fn)
13802 : /* Two built-in candidates; arbitrarily pick one. */
13803 : return 1;
13804 17707906 : else if (identifier_p (cand1->fn))
13805 : /* cand1 is built-in; prefer cand2. */
13806 : return -1;
13807 : else
13808 : /* cand2 is built-in; prefer cand1. */
13809 : return 1;
13810 : }
13811 : }
13812 :
13813 : /* For candidates of a multi-versioned function, make the version with
13814 : the highest priority win. This version will be checked for dispatching
13815 : first. If this version can be inlined into the caller, the front-end
13816 : will simply make a direct call to this function. */
13817 :
13818 4691415 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13819 4691393 : && DECL_FUNCTION_VERSIONED (cand1->fn)
13820 941 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13821 4692356 : && DECL_FUNCTION_VERSIONED (cand2->fn))
13822 : {
13823 941 : tree f1 = TREE_TYPE (cand1->fn);
13824 941 : tree f2 = TREE_TYPE (cand2->fn);
13825 941 : tree p1 = TYPE_ARG_TYPES (f1);
13826 941 : tree p2 = TYPE_ARG_TYPES (f2);
13827 :
13828 : /* Check if cand1->fn and cand2->fn are versions of the same function. It
13829 : is possible that cand1->fn and cand2->fn are function versions but of
13830 : different functions. Check types to see if they are versions of the same
13831 : function. */
13832 941 : if (compparms (p1, p2)
13833 941 : && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
13834 : {
13835 : /* Always make the version with the higher priority, more
13836 : specialized, win. */
13837 941 : gcc_assert (targetm.compare_version_priority);
13838 941 : if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
13839 : return 1;
13840 : else
13841 : return -1;
13842 : }
13843 : }
13844 :
13845 : /* If the two function declarations represent the same function (this can
13846 : happen with declarations in multiple scopes and arg-dependent lookup),
13847 : arbitrarily choose one. But first make sure the default args we're
13848 : using match. */
13849 4690452 : if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
13850 9380926 : && equal_functions (cand1->fn, cand2->fn))
13851 : {
13852 35 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
13853 35 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
13854 :
13855 70 : gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
13856 :
13857 50 : for (i = 0; i < len; ++i)
13858 : {
13859 : /* Don't crash if the fn is variadic. */
13860 15 : if (!parms1)
13861 : break;
13862 15 : parms1 = TREE_CHAIN (parms1);
13863 15 : parms2 = TREE_CHAIN (parms2);
13864 : }
13865 :
13866 35 : if (off1)
13867 0 : parms1 = TREE_CHAIN (parms1);
13868 35 : else if (off2)
13869 0 : parms2 = TREE_CHAIN (parms2);
13870 :
13871 69 : for (; parms1; ++i)
13872 : {
13873 80 : if (!cp_tree_equal (TREE_PURPOSE (parms1),
13874 40 : TREE_PURPOSE (parms2)))
13875 : {
13876 6 : if (warn)
13877 : {
13878 3 : if (complain & tf_error)
13879 : {
13880 3 : auto_diagnostic_group d;
13881 3 : if (permerror (input_location,
13882 : "default argument mismatch in "
13883 : "overload resolution"))
13884 : {
13885 3 : inform (DECL_SOURCE_LOCATION (cand1->fn),
13886 : " candidate 1: %q#F", cand1->fn);
13887 3 : inform (DECL_SOURCE_LOCATION (cand2->fn),
13888 : " candidate 2: %q#F", cand2->fn);
13889 : }
13890 3 : }
13891 : else
13892 : return 0;
13893 : }
13894 : else
13895 3 : add_warning (cand1, cand2);
13896 : break;
13897 : }
13898 34 : parms1 = TREE_CHAIN (parms1);
13899 34 : parms2 = TREE_CHAIN (parms2);
13900 : }
13901 :
13902 35 : return 1;
13903 : }
13904 :
13905 4691443 : tweak:
13906 :
13907 : /* Extension: If the worst conversion for one candidate is better than the
13908 : worst conversion for the other, take the first. */
13909 4691443 : if (!pedantic && (complain & tf_warning_or_error))
13910 : {
13911 : conversion_rank rank1 = cr_identity, rank2 = cr_identity;
13912 10088556 : struct z_candidate *w = 0, *l = 0;
13913 :
13914 10088556 : for (i = 0; i < len; ++i)
13915 : {
13916 5642067 : if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
13917 4362516 : rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
13918 5642067 : if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
13919 4362533 : rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
13920 : }
13921 4446489 : if (rank1 < rank2)
13922 56 : winner = 1, w = cand1, l = cand2;
13923 4446489 : if (rank1 > rank2)
13924 : winner = -1, w = cand2, l = cand1;
13925 4446381 : if (winner)
13926 : {
13927 : /* Don't choose a deleted function over ambiguity. */
13928 164 : if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
13929 : return 0;
13930 164 : if (warn)
13931 : {
13932 6 : auto_diagnostic_group d;
13933 6 : if (pedwarn (input_location, 0,
13934 : "ISO C++ says that these are ambiguous, even "
13935 : "though the worst conversion for the first is "
13936 : "better than the worst conversion for the second:"))
13937 : {
13938 3 : print_z_candidate (input_location, N_("candidate 1:"), w);
13939 3 : print_z_candidate (input_location, N_("candidate 2:"), l);
13940 : }
13941 6 : }
13942 : else
13943 158 : add_warning (w, l);
13944 164 : return winner;
13945 : }
13946 : }
13947 :
13948 : gcc_assert (!winner);
13949 : return 0;
13950 : }
13951 :
13952 : /* Given a list of candidates for overloading, find the best one, if any.
13953 : This algorithm has a worst case of O(2n) (winner is last), and a best
13954 : case of O(n/2) (totally ambiguous); much better than a sorting
13955 : algorithm. The candidates list is assumed to be sorted according
13956 : to viability (via splice_viable). */
13957 :
13958 : static struct z_candidate *
13959 240029677 : tourney (struct z_candidate *candidates, tsubst_flags_t complain)
13960 : {
13961 240029677 : struct z_candidate **champ = &candidates, **challenger;
13962 240029677 : int fate;
13963 240029677 : struct z_candidate *previous_worse_champ = nullptr;
13964 :
13965 : /* Walk through the list once, comparing each current champ to the next
13966 : candidate, knocking out a candidate or two with each comparison. */
13967 :
13968 306081054 : for (challenger = &candidates->next; *challenger && (*challenger)->viable; )
13969 : {
13970 66064170 : fate = joust (*champ, *challenger, 0, complain);
13971 66064170 : if (fate == 1)
13972 42758608 : challenger = &(*challenger)->next;
13973 23305562 : else if (fate == -1)
13974 : {
13975 18615989 : previous_worse_champ = *champ;
13976 18615989 : champ = challenger;
13977 18615989 : challenger = &(*challenger)->next;
13978 : }
13979 : else
13980 : {
13981 4689573 : previous_worse_champ = nullptr;
13982 4689573 : champ = &(*challenger)->next;
13983 4689573 : if (!*champ || !(*champ)->viable
13984 4676874 : || (*champ)->viable < (*challenger)->viable)
13985 : {
13986 : champ = nullptr;
13987 : break;
13988 : }
13989 4676780 : challenger = &(*champ)->next;
13990 : }
13991 : }
13992 :
13993 : /* Make sure the champ is better than all the candidates it hasn't yet
13994 : been compared to. */
13995 :
13996 240029677 : if (champ)
13997 28245099 : for (challenger = &candidates;
13998 268261983 : challenger != champ;
13999 28245099 : challenger = &(*challenger)->next)
14000 : {
14001 28247005 : if (*challenger == previous_worse_champ)
14002 : /* We already know this candidate is worse than the champ. */
14003 17428457 : continue;
14004 10818548 : fate = joust (*champ, *challenger, 0, complain);
14005 10818548 : if (fate != 1)
14006 : {
14007 : champ = nullptr;
14008 : break;
14009 : }
14010 : }
14011 :
14012 240016884 : if (!champ)
14013 14699 : return nullptr;
14014 :
14015 : /* Move the champ to the front of the candidate list. */
14016 :
14017 240014978 : if (champ != &candidates)
14018 : {
14019 19090348 : z_candidate *saved_champ = *champ;
14020 19090348 : *champ = saved_champ->next;
14021 19090348 : saved_champ->next = candidates;
14022 19090348 : candidates = saved_champ;
14023 : }
14024 :
14025 240014978 : return candidates;
14026 : }
14027 :
14028 : /* Returns nonzero if things of type FROM can be converted to TO. */
14029 :
14030 : bool
14031 4417380 : can_convert (tree to, tree from, tsubst_flags_t complain)
14032 : {
14033 4417380 : tree arg = NULL_TREE;
14034 : /* implicit_conversion only considers user-defined conversions
14035 : if it has an expression for the call argument list. */
14036 4417380 : if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to))
14037 109 : arg = build_stub_object (from);
14038 4417380 : return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain);
14039 : }
14040 :
14041 : /* Returns nonzero if things of type FROM can be converted to TO with a
14042 : standard conversion. */
14043 :
14044 : bool
14045 257 : can_convert_standard (tree to, tree from, tsubst_flags_t complain)
14046 : {
14047 257 : return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
14048 : }
14049 :
14050 : /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
14051 :
14052 : bool
14053 5406568 : can_convert_arg (tree to, tree from, tree arg, int flags,
14054 : tsubst_flags_t complain)
14055 : {
14056 5406568 : conversion *t;
14057 5406568 : bool ok_p;
14058 :
14059 5406568 : conversion_obstack_sentinel cos;
14060 : /* We want to discard any access checks done for this test,
14061 : as we might not be in the appropriate access context and
14062 : we'll do the check again when we actually perform the
14063 : conversion. */
14064 5406568 : push_deferring_access_checks (dk_deferred);
14065 :
14066 : /* Handle callers like check_local_shadow forgetting to
14067 : convert_from_reference. */
14068 5406568 : if (TYPE_REF_P (from) && arg)
14069 : {
14070 61 : arg = convert_from_reference (arg);
14071 61 : from = TREE_TYPE (arg);
14072 : }
14073 :
14074 5406568 : t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
14075 : flags, complain);
14076 5406568 : ok_p = (t && !t->bad_p);
14077 :
14078 : /* Discard the access checks now. */
14079 5406568 : pop_deferring_access_checks ();
14080 :
14081 10813136 : return ok_p;
14082 5406568 : }
14083 :
14084 : /* Like can_convert_arg, but allows dubious conversions as well. */
14085 :
14086 : bool
14087 181396786 : can_convert_arg_bad (tree to, tree from, tree arg, int flags,
14088 : tsubst_flags_t complain)
14089 : {
14090 181396786 : conversion *t;
14091 :
14092 181396786 : conversion_obstack_sentinel cos;
14093 : /* Try to perform the conversion. */
14094 181396786 : t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
14095 : flags, complain);
14096 :
14097 362793572 : return t != NULL;
14098 181396786 : }
14099 :
14100 : /* Return an IMPLICIT_CONV_EXPR from EXPR to TYPE with bits set from overload
14101 : resolution FLAGS. */
14102 :
14103 : tree
14104 14292117 : build_implicit_conv_flags (tree type, tree expr, int flags)
14105 : {
14106 : /* In a template, we are only concerned about determining the
14107 : type of non-dependent expressions, so we do not have to
14108 : perform the actual conversion. But for initializers, we
14109 : need to be able to perform it at instantiation
14110 : (or instantiate_non_dependent_expr) time. */
14111 14292117 : expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
14112 14292117 : if (!(flags & LOOKUP_ONLYCONVERTING))
14113 6543968 : IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
14114 14292117 : if (flags & LOOKUP_NO_NARROWING)
14115 23023 : IMPLICIT_CONV_EXPR_BRACED_INIT (expr) = true;
14116 14292117 : return expr;
14117 : }
14118 :
14119 : /* Convert EXPR to TYPE. Return the converted expression.
14120 :
14121 : Note that we allow bad conversions here because by the time we get to
14122 : this point we are committed to doing the conversion. If we end up
14123 : doing a bad conversion, convert_like will complain. */
14124 :
14125 : tree
14126 294100993 : perform_implicit_conversion_flags (tree type, tree expr,
14127 : tsubst_flags_t complain, int flags)
14128 : {
14129 294100993 : conversion *conv;
14130 294100993 : location_t loc = cp_expr_loc_or_input_loc (expr);
14131 :
14132 294100993 : if (error_operand_p (expr))
14133 450 : return error_mark_node;
14134 :
14135 294100543 : conversion_obstack_sentinel cos;
14136 :
14137 294100543 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
14138 : /*c_cast_p=*/false,
14139 : flags, complain);
14140 :
14141 294100543 : if (!conv)
14142 : {
14143 258132 : if (complain & tf_error)
14144 486 : implicit_conversion_error (loc, type, expr, flags);
14145 258132 : expr = error_mark_node;
14146 : }
14147 293842411 : else if (processing_template_decl && conv->kind != ck_identity)
14148 14292117 : expr = build_implicit_conv_flags (type, expr, flags);
14149 : else
14150 : {
14151 : /* Give a conversion call the same location as expr. */
14152 279550294 : iloc_sentinel il (loc);
14153 279550294 : expr = convert_like (conv, expr, complain);
14154 279550294 : }
14155 :
14156 294100543 : return expr;
14157 294100543 : }
14158 :
14159 : tree
14160 12789262 : perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
14161 : {
14162 12789262 : return perform_implicit_conversion_flags (type, expr, complain,
14163 12789262 : LOOKUP_IMPLICIT);
14164 : }
14165 :
14166 : /* Convert EXPR to TYPE (as a direct-initialization) if that is
14167 : permitted. If the conversion is valid, the converted expression is
14168 : returned. Otherwise, NULL_TREE is returned, except in the case
14169 : that TYPE is a class type; in that case, an error is issued. If
14170 : C_CAST_P is true, then this direct-initialization is taking
14171 : place as part of a static_cast being attempted as part of a C-style
14172 : cast. */
14173 :
14174 : tree
14175 48279023 : perform_direct_initialization_if_possible (tree type,
14176 : tree expr,
14177 : bool c_cast_p,
14178 : tsubst_flags_t complain)
14179 : {
14180 48279023 : conversion *conv;
14181 :
14182 48279023 : if (type == error_mark_node || error_operand_p (expr))
14183 : return error_mark_node;
14184 : /* [dcl.init]
14185 :
14186 : If the destination type is a (possibly cv-qualified) class type:
14187 :
14188 : -- If the initialization is direct-initialization ...,
14189 : constructors are considered.
14190 :
14191 : -- If overload resolution is successful, the selected constructor
14192 : is called to initialize the object, with the initializer expression
14193 : or expression-list as its argument(s).
14194 :
14195 : -- Otherwise, if no constructor is viable, the destination type is
14196 : a (possibly cv-qualified) aggregate class A, and the initializer is
14197 : a parenthesized expression-list, the object is initialized as
14198 : follows... */
14199 48279023 : if (CLASS_TYPE_P (type))
14200 : {
14201 3187855 : releasing_vec args (make_tree_vector_single (expr));
14202 3187855 : expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
14203 : &args, type, LOOKUP_NORMAL, complain);
14204 3187855 : return build_cplus_new (type, expr, complain);
14205 3187855 : }
14206 :
14207 45091168 : conversion_obstack_sentinel cos;
14208 :
14209 45091168 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
14210 : c_cast_p,
14211 : LOOKUP_NORMAL, complain);
14212 45091168 : if (!conv || conv->bad_p)
14213 : expr = NULL_TREE;
14214 40415467 : else if (processing_template_decl && conv->kind != ck_identity)
14215 : {
14216 : /* In a template, we are only concerned about determining the
14217 : type of non-dependent expressions, so we do not have to
14218 : perform the actual conversion. But for initializers, we
14219 : need to be able to perform it at instantiation
14220 : (or instantiate_non_dependent_expr) time. */
14221 693703 : expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
14222 693703 : IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
14223 : }
14224 : else
14225 39721764 : expr = convert_like (conv, expr, NULL_TREE, 0,
14226 : /*issue_conversion_warnings=*/false,
14227 : c_cast_p, /*nested_p=*/false, complain);
14228 :
14229 45091168 : return expr;
14230 45091168 : }
14231 :
14232 : /* When initializing a reference that lasts longer than a full-expression,
14233 : this special rule applies:
14234 :
14235 : [class.temporary]
14236 :
14237 : The temporary to which the reference is bound or the temporary
14238 : that is the complete object to which the reference is bound
14239 : persists for the lifetime of the reference.
14240 :
14241 : The temporaries created during the evaluation of the expression
14242 : initializing the reference, except the temporary to which the
14243 : reference is bound, are destroyed at the end of the
14244 : full-expression in which they are created.
14245 :
14246 : In that case, we store the converted expression into a new
14247 : VAR_DECL in a new scope.
14248 :
14249 : However, we want to be careful not to create temporaries when
14250 : they are not required. For example, given:
14251 :
14252 : struct B {};
14253 : struct D : public B {};
14254 : D f();
14255 : const B& b = f();
14256 :
14257 : there is no need to copy the return value from "f"; we can just
14258 : extend its lifetime. Similarly, given:
14259 :
14260 : struct S {};
14261 : struct T { operator S(); };
14262 : T t;
14263 : const S& s = t;
14264 :
14265 : we can extend the lifetime of the return value of the conversion
14266 : operator.
14267 :
14268 : The next several functions are involved in this lifetime extension. */
14269 :
14270 : /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
14271 : reference is being bound to a temporary. Create and return a new
14272 : VAR_DECL with the indicated TYPE; this variable will store the value to
14273 : which the reference is bound. */
14274 :
14275 : tree
14276 9185 : make_temporary_var_for_ref_to_temp (tree decl, tree type)
14277 : {
14278 9185 : tree var = create_temporary_var (type);
14279 :
14280 : /* Register the variable. */
14281 9185 : if (VAR_P (decl)
14282 9185 : && (TREE_STATIC (decl) || CP_DECL_THREAD_LOCAL_P (decl)))
14283 : {
14284 : /* Namespace-scope or local static; give it a mangled name. */
14285 :
14286 : /* If an initializer is visible to multiple translation units, those
14287 : translation units must agree on the addresses of the
14288 : temporaries. Therefore the temporaries must be given a consistent name
14289 : and vague linkage. The mangled name of a temporary is the name of the
14290 : non-temporary object in whose initializer they appear, prefixed with
14291 : GR and suffixed with a sequence number mangled using the usual rules
14292 : for a seq-id. Temporaries are numbered with a pre-order, depth-first,
14293 : left-to-right walk of the complete initializer. */
14294 817 : copy_linkage (var, decl);
14295 :
14296 817 : tree name = mangle_ref_init_variable (decl);
14297 817 : DECL_NAME (var) = name;
14298 817 : SET_DECL_ASSEMBLER_NAME (var, name);
14299 :
14300 : /* Set the context to make the variable mergeable in modules. */
14301 817 : DECL_CONTEXT (var) = current_scope ();
14302 : }
14303 : else
14304 : /* Create a new cleanup level if necessary. */
14305 8368 : maybe_push_cleanup_level (type);
14306 :
14307 9185 : return pushdecl (var);
14308 : }
14309 :
14310 : static tree extend_temps_r (tree *, int *, void *);
14311 :
14312 : /* EXPR is the initializer for a variable DECL of reference or
14313 : std::initializer_list type. Create, push and return a new VAR_DECL
14314 : for the initializer so that it will live as long as DECL. Any
14315 : cleanup for the new variable is returned through CLEANUP, and the
14316 : code to initialize the new variable is returned through INITP. */
14317 :
14318 : static tree
14319 9185 : set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
14320 : tree *initp, tree *cond_guard,
14321 : void *walk_data)
14322 : {
14323 9185 : tree init;
14324 9185 : tree type;
14325 9185 : tree var;
14326 :
14327 : /* Create the temporary variable. */
14328 9185 : type = TREE_TYPE (expr);
14329 9185 : var = make_temporary_var_for_ref_to_temp (decl, type);
14330 9185 : layout_decl (var, 0);
14331 : /* If the rvalue is the result of a function call it will be
14332 : a TARGET_EXPR. If it is some other construct (such as a
14333 : member access expression where the underlying object is
14334 : itself the result of a function call), turn it into a
14335 : TARGET_EXPR here. It is important that EXPR be a
14336 : TARGET_EXPR below since otherwise the INIT_EXPR will
14337 : attempt to make a bitwise copy of EXPR to initialize
14338 : VAR. */
14339 9185 : if (TREE_CODE (expr) != TARGET_EXPR)
14340 0 : expr = get_target_expr (expr);
14341 : else
14342 : {
14343 9185 : if (TREE_ADDRESSABLE (expr))
14344 9102 : TREE_ADDRESSABLE (var) = 1;
14345 9185 : if (DECL_MERGEABLE (TARGET_EXPR_SLOT (expr)))
14346 624 : DECL_MERGEABLE (var) = true;
14347 : }
14348 :
14349 9185 : if (TREE_CODE (decl) == FIELD_DECL
14350 9185 : && extra_warnings && !warning_suppressed_p (decl))
14351 : {
14352 3 : warning (OPT_Wextra, "a temporary bound to %qD only persists "
14353 : "until the constructor exits", decl);
14354 3 : suppress_warning (decl);
14355 : }
14356 :
14357 : /* Recursively extend temps in this initializer. The recursion needs to come
14358 : after creating the variable to conform to the mangling ABI, and before
14359 : maybe_constant_init because the extension might change its result. */
14360 9185 : if (walk_data)
14361 2076 : cp_walk_tree (&TARGET_EXPR_INITIAL (expr), extend_temps_r,
14362 : walk_data, nullptr);
14363 : else
14364 7109 : TARGET_EXPR_INITIAL (expr)
14365 14218 : = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups,
14366 : cond_guard);
14367 :
14368 : /* Any reference temp has a non-trivial initializer. */
14369 9185 : DECL_NONTRIVIALLY_INITIALIZED_P (var) = true;
14370 :
14371 : /* If the initializer is constant, put it in DECL_INITIAL so we get
14372 : static initialization and use in constant expressions. */
14373 9185 : init = maybe_constant_init (expr, var, /*manifestly_const_eval=*/true);
14374 : /* As in store_init_value. */
14375 9185 : init = cp_fully_fold (init);
14376 9185 : if (TREE_CONSTANT (init))
14377 : {
14378 1281 : if (literal_type_p (type)
14379 1255 : && CP_TYPE_CONST_NON_VOLATILE_P (type)
14380 2028 : && !TYPE_HAS_MUTABLE_P (type))
14381 : {
14382 : /* 5.19 says that a constant expression can include an
14383 : lvalue-rvalue conversion applied to "a glvalue of literal type
14384 : that refers to a non-volatile temporary object initialized
14385 : with a constant expression". Rather than try to communicate
14386 : that this VAR_DECL is a temporary, just mark it constexpr. */
14387 744 : DECL_DECLARED_CONSTEXPR_P (var) = true;
14388 744 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
14389 744 : TREE_CONSTANT (var) = true;
14390 744 : TREE_READONLY (var) = true;
14391 : }
14392 1281 : DECL_INITIAL (var) = init;
14393 1281 : init = NULL_TREE;
14394 : }
14395 : else
14396 : /* Create the INIT_EXPR that will initialize the temporary
14397 : variable. */
14398 7904 : init = split_nonconstant_init (var, expr);
14399 9185 : if (at_function_scope_p ())
14400 : {
14401 8460 : add_decl_expr (var);
14402 :
14403 8460 : if (TREE_STATIC (var))
14404 92 : init = add_stmt_to_compound (init, register_dtor_fn (var));
14405 : else
14406 : {
14407 : /* ??? Instead of rebuilding the cleanup, we could replace the slot
14408 : with var in TARGET_EXPR_CLEANUP (expr). */
14409 8368 : tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
14410 8368 : if (cleanup)
14411 : {
14412 3493 : if (cond_guard && cleanup != error_mark_node)
14413 : {
14414 23 : if (*cond_guard == NULL_TREE)
14415 : {
14416 23 : *cond_guard = build_local_temp (boolean_type_node);
14417 23 : add_decl_expr (*cond_guard);
14418 23 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION,
14419 : *cond_guard, NOP_EXPR,
14420 : boolean_false_node,
14421 : tf_warning_or_error);
14422 23 : finish_expr_stmt (set);
14423 : }
14424 23 : cleanup = build3 (COND_EXPR, void_type_node,
14425 : *cond_guard, cleanup, NULL_TREE);
14426 : }
14427 3493 : if (flag_exceptions && TREE_CODE (TREE_TYPE (var)) != ARRAY_TYPE)
14428 : {
14429 : /* The normal cleanup for this extended variable isn't pushed
14430 : until cp_finish_decl, so we need to retain a TARGET_EXPR
14431 : to clean it up in case a later initializer throws
14432 : (g++.dg/eh/ref-temp3.C).
14433 :
14434 : We don't do this for array temporaries because they have
14435 : the array cleanup region from build_vec_init.
14436 :
14437 : Unlike maybe_push_temp_cleanup, we don't actually need a
14438 : flag, but a TARGET_EXPR needs a TARGET_EXPR_SLOT.
14439 : Perhaps this could use WITH_CLEANUP_EXPR instead, but
14440 : gimplify.cc doesn't handle that, and front-end handling
14441 : was removed in r8-1725 and r8-1818.
14442 :
14443 : Alternately it might be preferable to flatten an
14444 : initialization with extended temps into a sequence of
14445 : (non-full-expression) statements, so we could immediately
14446 : push_cleanup here for only a single cleanup region, but we
14447 : don't have a mechanism for that in the front-end, only the
14448 : gimplifier. */
14449 3385 : tree targ = get_internal_target_expr (boolean_true_node);
14450 3385 : TARGET_EXPR_CLEANUP (targ) = cleanup;
14451 3385 : CLEANUP_EH_ONLY (targ) = true;
14452 : /* Don't actually initialize the bool. */
14453 3385 : init = (!init ? void_node
14454 3359 : : convert_to_void (init, ICV_STATEMENT, tf_none));
14455 3385 : TARGET_EXPR_INITIAL (targ) = init;
14456 3385 : init = targ;
14457 : }
14458 3493 : vec_safe_push (*cleanups, cleanup);
14459 : }
14460 : }
14461 :
14462 : /* We must be careful to destroy the temporary only
14463 : after its initialization has taken place. If the
14464 : initialization throws an exception, then the
14465 : destructor should not be run. We cannot simply
14466 : transform INIT into something like:
14467 :
14468 : (INIT, ({ CLEANUP_STMT; }))
14469 :
14470 : because emit_local_var always treats the
14471 : initializer as a full-expression. Thus, the
14472 : destructor would run too early; it would run at the
14473 : end of initializing the reference variable, rather
14474 : than at the end of the block enclosing the
14475 : reference variable.
14476 :
14477 : The solution is to pass back a cleanup expression
14478 : which the caller is responsible for attaching to
14479 : the statement tree. */
14480 : }
14481 : else
14482 : {
14483 : /* Don't output reflection variables. */
14484 725 : if (consteval_only_p (var))
14485 2 : DECL_EXTERNAL (var) = true;
14486 :
14487 725 : rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
14488 725 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
14489 : {
14490 40 : if (CP_DECL_THREAD_LOCAL_P (var))
14491 12 : tls_aggregates = tree_cons (NULL_TREE, var,
14492 : tls_aggregates);
14493 : else
14494 28 : static_aggregates = tree_cons (NULL_TREE, var,
14495 : static_aggregates);
14496 : }
14497 : else
14498 : /* Check whether the dtor is callable. */
14499 685 : cxx_maybe_build_cleanup (var, tf_warning_or_error);
14500 : }
14501 : /* Avoid -Wunused-variable warning (c++/38958). */
14502 9185 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
14503 9185 : && VAR_P (decl))
14504 3464 : TREE_USED (decl) = DECL_READ_P (decl) = true;
14505 :
14506 9185 : *initp = init;
14507 9185 : return var;
14508 : }
14509 :
14510 : /* Convert EXPR to the indicated reference TYPE, in a way suitable for
14511 : initializing a variable of that TYPE. */
14512 :
14513 : tree
14514 6374485 : initialize_reference (tree type, tree expr,
14515 : int flags, tsubst_flags_t complain)
14516 : {
14517 6374485 : conversion *conv;
14518 6374485 : location_t loc = cp_expr_loc_or_input_loc (expr);
14519 :
14520 6374485 : if (type == error_mark_node || error_operand_p (expr))
14521 : return error_mark_node;
14522 :
14523 6374408 : conversion_obstack_sentinel cos;
14524 :
14525 6374408 : conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
14526 : flags, complain);
14527 : /* If this conversion failed, we're in C++20, and we have something like
14528 : A& a(b) where A is an aggregate, try again, this time as A& a{b}. */
14529 6374408 : if ((!conv || conv->bad_p)
14530 725 : && (flags & LOOKUP_AGGREGATE_PAREN_INIT))
14531 : {
14532 4 : tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
14533 4 : CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
14534 4 : CONSTRUCTOR_IS_PAREN_INIT (e) = true;
14535 4 : conversion *c = reference_binding (type, TREE_TYPE (e), e,
14536 : /*c_cast_p=*/false, flags, complain);
14537 : /* If this worked, use it. */
14538 4 : if (c && !c->bad_p)
14539 : expr = e, conv = c;
14540 : }
14541 6375044 : if (!conv || conv->bad_p)
14542 : {
14543 722 : if (complain & tf_error)
14544 : {
14545 366 : if (conv)
14546 313 : convert_like (conv, expr, complain);
14547 53 : else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
14548 21 : && !TYPE_REF_IS_RVALUE (type)
14549 74 : && !lvalue_p (expr))
14550 6 : error_at (loc, "invalid initialization of non-const reference of "
14551 : "type %qH from an rvalue of type %qI",
14552 6 : type, TREE_TYPE (expr));
14553 : else
14554 47 : error_at (loc, "invalid initialization of reference of type "
14555 : "%qH from expression of type %qI", type,
14556 47 : TREE_TYPE (expr));
14557 : }
14558 722 : return error_mark_node;
14559 : }
14560 :
14561 6373686 : if (conv->kind == ck_ref_bind)
14562 : /* Perform the conversion. */
14563 6373683 : expr = convert_like (conv, expr, complain);
14564 3 : else if (conv->kind == ck_ambig)
14565 : /* We gave an error in build_user_type_conversion_1. */
14566 3 : expr = error_mark_node;
14567 : else
14568 0 : gcc_unreachable ();
14569 :
14570 : return expr;
14571 6374408 : }
14572 :
14573 : /* Return true if T is std::pair<const T&, const T&>. */
14574 :
14575 : static bool
14576 615326 : std_pair_ref_ref_p (tree t)
14577 : {
14578 : /* First, check if we have std::pair. */
14579 57663 : if (!NON_UNION_CLASS_TYPE_P (t)
14580 672529 : || !CLASSTYPE_TEMPLATE_INSTANTIATION (t))
14581 : return false;
14582 20763 : tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (t));
14583 20763 : if (!decl_in_std_namespace_p (tdecl))
14584 : return false;
14585 15502 : tree name = DECL_NAME (tdecl);
14586 15502 : if (!name || !id_equal (name, "pair"))
14587 : return false;
14588 :
14589 : /* Now see if the template arguments are both const T&. */
14590 361 : tree args = CLASSTYPE_TI_ARGS (t);
14591 361 : if (TREE_VEC_LENGTH (args) != 2)
14592 : return false;
14593 421 : for (int i = 0; i < 2; i++)
14594 451 : if (!TYPE_REF_OBJ_P (TREE_VEC_ELT (args, i))
14595 451 : || !CP_TYPE_CONST_P (TREE_TYPE (TREE_VEC_ELT (args, i))))
14596 331 : return false;
14597 :
14598 : return true;
14599 : }
14600 :
14601 : /* Return true if a class T has a reference member. */
14602 :
14603 : static bool
14604 216 : class_has_reference_member_p (tree t)
14605 : {
14606 216 : for (tree fields = TYPE_FIELDS (t);
14607 3380 : fields;
14608 3164 : fields = DECL_CHAIN (fields))
14609 3227 : if (TREE_CODE (fields) == FIELD_DECL
14610 196 : && !DECL_ARTIFICIAL (fields)
14611 3394 : && TYPE_REF_P (TREE_TYPE (fields)))
14612 : return true;
14613 : return false;
14614 : }
14615 :
14616 : /* A wrapper for the above suitable as a callback for dfs_walk_once. */
14617 :
14618 : static tree
14619 216 : class_has_reference_member_p_r (tree binfo, void *)
14620 : {
14621 216 : return (class_has_reference_member_p (BINFO_TYPE (binfo))
14622 216 : ? integer_one_node : NULL_TREE);
14623 : }
14624 :
14625 :
14626 : /* Return true if T (either a class or a function) has been marked as
14627 : not-dangling. */
14628 :
14629 : static bool
14630 1557 : no_dangling_p (tree t)
14631 : {
14632 1557 : t = lookup_attribute ("no_dangling", TYPE_ATTRIBUTES (t));
14633 1557 : if (!t)
14634 : return false;
14635 :
14636 75 : t = TREE_VALUE (t);
14637 75 : if (!t)
14638 : return true;
14639 :
14640 51 : t = build_converted_constant_bool_expr (TREE_VALUE (t), tf_warning_or_error);
14641 51 : t = cxx_constant_value (t);
14642 51 : return t == boolean_true_node;
14643 : }
14644 :
14645 : /* Return true if a class CTYPE is either std::reference_wrapper or
14646 : std::ref_view, or a reference wrapper class. We consider a class
14647 : a reference wrapper class if it has a reference member. We no
14648 : longer check that it has a constructor taking the same reference type
14649 : since that approach still generated too many false positives. */
14650 :
14651 : static bool
14652 447 : reference_like_class_p (tree ctype)
14653 : {
14654 447 : if (!CLASS_TYPE_P (ctype))
14655 : return false;
14656 :
14657 314 : if (no_dangling_p (ctype))
14658 : return true;
14659 :
14660 : /* Also accept a std::pair<const T&, const T&>. */
14661 290 : if (std_pair_ref_ref_p (ctype))
14662 : return true;
14663 :
14664 275 : tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (ctype));
14665 275 : if (decl_in_std_namespace_p (tdecl))
14666 : {
14667 38 : tree name = DECL_NAME (tdecl);
14668 38 : if (name
14669 38 : && (id_equal (name, "reference_wrapper")
14670 26 : || id_equal (name, "span")
14671 11 : || id_equal (name, "ref_view")))
14672 : return true;
14673 : }
14674 :
14675 : /* Avoid warning if CTYPE looks like std::span: it has a T* member and
14676 : a trivial destructor. For example,
14677 :
14678 : template<typename T>
14679 : struct Span {
14680 : T* data_;
14681 : std::size len_;
14682 : };
14683 :
14684 : is considered std::span-like. */
14685 248 : if (NON_UNION_CLASS_TYPE_P (ctype) && TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
14686 228 : for (tree field = next_aggregate_field (TYPE_FIELDS (ctype));
14687 367 : field; field = next_aggregate_field (DECL_CHAIN (field)))
14688 199 : if (TYPE_PTR_P (TREE_TYPE (field)))
14689 : return true;
14690 :
14691 : /* Some classes, such as std::tuple, have the reference member in its
14692 : (non-direct) base class. */
14693 188 : if (dfs_walk_once (TYPE_BINFO (ctype), class_has_reference_member_p_r,
14694 : nullptr, nullptr))
14695 : return true;
14696 :
14697 : return false;
14698 : }
14699 :
14700 : /* Helper for maybe_warn_dangling_reference to find a problematic temporary
14701 : in EXPR (as outlined in maybe_warn_dangling_reference), or NULL_TREE
14702 : if none found. For instance:
14703 :
14704 : const S& s = S().self(); // S()
14705 : const int& r = (42, f(1)); // temporary for passing 1 to f
14706 : const int& t = b ? f(1) : f(2); // temporary for 1
14707 : const int& u = b ? f(1) : f(g); // temporary for 1
14708 : const int& v = b ? f(g) : f(2); // temporary for 2
14709 : const int& w = b ? f(g) : f(g); // NULL_TREE
14710 : const int& y = (f(1), 42); // NULL_TREE
14711 : const int& z = f(f(1)); // temporary for 1
14712 :
14713 : EXPR is the initializer. If ARG_P is true, we're processing an argument
14714 : to a function; the point is to distinguish between, for example,
14715 :
14716 : Ref::inner (&TARGET_EXPR <D.2839, F::foo (fm)>)
14717 :
14718 : where we shouldn't warn, and
14719 :
14720 : Ref::inner (&TARGET_EXPR <D.2908, F::foo (&TARGET_EXPR <...>)>)
14721 :
14722 : where we should warn (Ref is a reference_like_class_p so we see through
14723 : it. */
14724 :
14725 : static tree
14726 3881 : do_warn_dangling_reference (tree expr, bool arg_p)
14727 : {
14728 3881 : STRIP_NOPS (expr);
14729 :
14730 3881 : if (arg_p && expr_represents_temporary_p (expr))
14731 : {
14732 : /* An attempt to reduce the number of -Wdangling-reference
14733 : false positives concerning reference wrappers (c++/107532).
14734 : When we encounter a reference_like_class_p, we don't warn
14735 : just yet; instead, we keep recursing to see if there were
14736 : any temporaries behind the reference-wrapper class. */
14737 : tree e = expr;
14738 355 : while (handled_component_p (e))
14739 15 : e = TREE_OPERAND (e, 0);
14740 340 : tree type = TREE_TYPE (e);
14741 : /* If the temporary represents a lambda, we don't really know
14742 : what's going on here. */
14743 462 : if (!reference_like_class_p (type) && !LAMBDA_TYPE_P (type))
14744 : return expr;
14745 : }
14746 :
14747 3648 : switch (TREE_CODE (expr))
14748 : {
14749 1822 : case CALL_EXPR:
14750 1822 : {
14751 1822 : tree fndecl = cp_get_callee_fndecl_nofold (expr);
14752 1822 : if (!fndecl
14753 1822 : || warning_suppressed_p (fndecl, OPT_Wdangling_reference)
14754 1811 : || !warning_enabled_at (DECL_SOURCE_LOCATION (fndecl),
14755 1811 : OPT_Wdangling_reference)
14756 : /* Don't emit a false positive for:
14757 : std::vector<int> v = ...;
14758 : std::vector<int>::const_iterator it = v.begin();
14759 : const int &r = *it++;
14760 : because R refers to one of the int elements of V, not to
14761 : a temporary object. Member operator* may return a reference
14762 : but probably not to one of its arguments. */
14763 1452 : || (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl)
14764 852 : && DECL_OVERLOADED_OPERATOR_P (fndecl)
14765 319 : && DECL_OVERLOADED_OPERATOR_IS (fndecl, INDIRECT_REF))
14766 3065 : || no_dangling_p (TREE_TYPE (fndecl)))
14767 603 : return NULL_TREE;
14768 :
14769 1219 : tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
14770 : /* If the function doesn't return a reference, don't warn. This
14771 : can be e.g.
14772 : const int& z = std::min({1, 2, 3, 4, 5, 6, 7});
14773 : which doesn't dangle: std::min here returns an int.
14774 :
14775 : If the function returns a std::pair<const T&, const T&>, we
14776 : warn, to detect e.g.
14777 : std::pair<const int&, const int&> v = std::minmax(1, 2);
14778 : which also creates a dangling reference, because std::minmax
14779 : returns std::pair<const T&, const T&>(b, a). */
14780 1219 : if (!(TYPE_REF_OBJ_P (rettype) || reference_like_class_p (rettype)))
14781 : return NULL_TREE;
14782 :
14783 : /* Here we're looking to see if any of the arguments is a temporary
14784 : initializing a reference parameter. */
14785 1647 : for (int i = 0; i < call_expr_nargs (expr); ++i)
14786 : {
14787 1240 : tree arg = CALL_EXPR_ARG (expr, i);
14788 : /* Check that this argument initializes a reference, except for
14789 : the argument initializing the object of a member function. */
14790 1240 : if (!DECL_IOBJ_MEMBER_FUNCTION_P (fndecl)
14791 1240 : && !TYPE_REF_P (TREE_TYPE (arg)))
14792 130 : continue;
14793 1110 : STRIP_NOPS (arg);
14794 1110 : if (TREE_CODE (arg) == ADDR_EXPR)
14795 725 : arg = TREE_OPERAND (arg, 0);
14796 : /* Recurse to see if the argument is a temporary. It could also
14797 : be another call taking a temporary and returning it and
14798 : initializing this reference parameter. */
14799 1110 : if ((arg = do_warn_dangling_reference (arg, /*arg_p=*/true)))
14800 : {
14801 : /* If we know the temporary could not bind to the return type,
14802 : don't warn. This is for scalars and empty classes only
14803 : because for other classes we can't be sure we are not
14804 : returning its sub-object. */
14805 278 : if ((SCALAR_TYPE_P (TREE_TYPE (arg))
14806 152 : || is_empty_class (TREE_TYPE (arg)))
14807 153 : && TYPE_REF_P (rettype)
14808 138 : && !reference_related_p (TREE_TYPE (rettype),
14809 138 : TREE_TYPE (arg)))
14810 21 : continue;
14811 257 : return arg;
14812 : }
14813 : /* Don't warn about member functions like:
14814 : std::any a(...);
14815 : S& s = a.emplace<S>({0}, 0);
14816 : which construct a new object and return a reference to it, but
14817 : we still want to detect:
14818 : struct S { const S& self () { return *this; } };
14819 : const S& s = S().self();
14820 : where 's' dangles. If we've gotten here, the object this function
14821 : is invoked on is not a temporary. */
14822 832 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl))
14823 : break;
14824 : }
14825 : return NULL_TREE;
14826 : }
14827 28 : case COMPOUND_EXPR:
14828 28 : return do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p);
14829 31 : case COND_EXPR:
14830 31 : if (tree t = do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p))
14831 : return t;
14832 16 : return do_warn_dangling_reference (TREE_OPERAND (expr, 2), arg_p);
14833 0 : case PAREN_EXPR:
14834 0 : return do_warn_dangling_reference (TREE_OPERAND (expr, 0), arg_p);
14835 122 : case TARGET_EXPR:
14836 122 : return do_warn_dangling_reference (TARGET_EXPR_INITIAL (expr), arg_p);
14837 : default:
14838 : return NULL_TREE;
14839 : }
14840 : }
14841 :
14842 : /* Implement -Wdangling-reference, to detect cases like
14843 :
14844 : int n = 1;
14845 : const int& r = std::max(n - 1, n + 1); // r is dangling
14846 :
14847 : This creates temporaries from the arguments, returns a reference to
14848 : one of the temporaries, but both temporaries are destroyed at the end
14849 : of the full expression.
14850 :
14851 : This works by checking if a reference is initialized with a function
14852 : that returns a reference, and at least one parameter of the function
14853 : is a reference that is bound to a temporary. It assumes that such a
14854 : function actually returns one of its arguments.
14855 :
14856 : DECL is the reference being initialized, INIT is the initializer. */
14857 :
14858 : static void
14859 107912515 : maybe_warn_dangling_reference (const_tree decl, tree init)
14860 : {
14861 107912515 : if (!warn_dangling_reference)
14862 : return;
14863 617595 : tree type = TREE_TYPE (decl);
14864 : /* Only warn if what we're initializing has type T&& or const T&, or
14865 : std::pair<const T&, const T&>. (A non-const lvalue reference can't
14866 : bind to a temporary.) */
14867 1232631 : if (!((TYPE_REF_OBJ_P (type)
14868 5444 : && (TYPE_REF_IS_RVALUE (type)
14869 5004 : || CP_TYPE_CONST_P (TREE_TYPE (type))))
14870 615036 : || std_pair_ref_ref_p (type)))
14871 : return;
14872 : /* Don't suppress the diagnostic just because the call comes from
14873 : a system header. If the DECL is not in a system header, or if
14874 : -Wsystem-headers was provided, warn. */
14875 2574 : auto wsh
14876 2574 : = make_temp_override (global_dc->m_warn_system_headers,
14877 2574 : (!in_system_header_at (DECL_SOURCE_LOCATION (decl))
14878 2574 : || global_dc->m_warn_system_headers));
14879 2574 : if (tree call = do_warn_dangling_reference (init, /*arg_p=*/false))
14880 : {
14881 212 : auto_diagnostic_group d;
14882 212 : if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wdangling_reference,
14883 : "possibly dangling reference to a temporary"))
14884 212 : inform (EXPR_LOCATION (call), "%qT temporary created here",
14885 212 : TREE_TYPE (call));
14886 212 : }
14887 2574 : }
14888 :
14889 : /* If *P is an xvalue expression, prevent temporary lifetime extension if it
14890 : gets used to initialize a reference. */
14891 :
14892 : static tree
14893 95 : prevent_lifetime_extension (tree t)
14894 : {
14895 95 : tree *p = &t;
14896 95 : while (TREE_CODE (*p) == COMPOUND_EXPR)
14897 0 : p = &TREE_OPERAND (*p, 1);
14898 104 : while (handled_component_p (*p))
14899 9 : p = &TREE_OPERAND (*p, 0);
14900 : /* Change a TARGET_EXPR from prvalue to xvalue. */
14901 95 : if (TREE_CODE (*p) == TARGET_EXPR)
14902 3 : *p = build2 (COMPOUND_EXPR, TREE_TYPE (*p), *p,
14903 3 : move (TARGET_EXPR_SLOT (*p)));
14904 95 : return t;
14905 : }
14906 :
14907 : /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
14908 : which is bound either to a reference or a std::initializer_list. */
14909 :
14910 : static tree
14911 727231 : extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups,
14912 : tree *cond_guard)
14913 : {
14914 : /* CWG1299 (C++20): The temporary object to which the reference is bound or
14915 : the temporary object that is the complete object of a subobject to which
14916 : the reference is bound persists for the lifetime of the reference if the
14917 : glvalue to which the reference is bound was obtained through one of the
14918 : following:
14919 : - a temporary materialization conversion ([conv.rval]),
14920 : - ( expression ), where expression is one of these expressions,
14921 : - subscripting ([expr.sub]) of an array operand, where that operand is one
14922 : of these expressions,
14923 : - a class member access ([expr.ref]) using the . operator where the left
14924 : operand is one of these expressions and the right operand designates a
14925 : non-static data member of non-reference type,
14926 : - a pointer-to-member operation ([expr.mptr.oper]) using the .* operator
14927 : where the left operand is one of these expressions and the right operand
14928 : is a pointer to data member of non-reference type,
14929 : - a const_cast ([expr.const.cast]), static_cast ([expr.static.cast]),
14930 : dynamic_cast ([expr.dynamic.cast]), or reinterpret_cast
14931 : ([expr.reinterpret.cast]) converting, without a user-defined conversion,
14932 : a glvalue operand that is one of these expressions to a glvalue that
14933 : refers to the object designated by the operand, or to its complete
14934 : object or a subobject thereof,
14935 : - a conditional expression ([expr.cond]) that is a glvalue where the
14936 : second or third operand is one of these expressions, or
14937 : - a comma expression ([expr.comma]) that is a glvalue where the right
14938 : operand is one of these expressions. */
14939 :
14940 : /* FIXME several cases are still handled wrong (101572, 81420). */
14941 :
14942 727231 : tree sub = init;
14943 727231 : tree *p;
14944 727231 : STRIP_NOPS (sub);
14945 727231 : if (TREE_CODE (sub) == COMPOUND_EXPR)
14946 : {
14947 242 : TREE_OPERAND (sub, 1)
14948 242 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups,
14949 : cond_guard);
14950 242 : return init;
14951 : }
14952 726989 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14953 726989 : && TYPE_PTRDATAMEM_P (TREE_TYPE (tree_strip_nop_conversions
14954 : (TREE_OPERAND (sub, 1)))))
14955 : {
14956 : /* A pointer-to-member operation. */
14957 6 : TREE_OPERAND (sub, 0)
14958 6 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 0), cleanups,
14959 : cond_guard);
14960 6 : return init;
14961 : }
14962 726983 : if (TREE_CODE (sub) == COND_EXPR)
14963 : {
14964 22775 : tree cur_cond_guard = NULL_TREE;
14965 22775 : if (TREE_OPERAND (sub, 1))
14966 22775 : TREE_OPERAND (sub, 1)
14967 45550 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups,
14968 : &cur_cond_guard);
14969 22775 : if (cur_cond_guard)
14970 : {
14971 9 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard,
14972 : NOP_EXPR, boolean_true_node,
14973 : tf_warning_or_error);
14974 9 : TREE_OPERAND (sub, 1)
14975 18 : = cp_build_compound_expr (set, TREE_OPERAND (sub, 1),
14976 : tf_warning_or_error);
14977 : }
14978 22775 : cur_cond_guard = NULL_TREE;
14979 22775 : if (TREE_OPERAND (sub, 2))
14980 22775 : TREE_OPERAND (sub, 2)
14981 45550 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 2), cleanups,
14982 : &cur_cond_guard);
14983 22775 : if (cur_cond_guard)
14984 : {
14985 12 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard,
14986 : NOP_EXPR, boolean_true_node,
14987 : tf_warning_or_error);
14988 12 : TREE_OPERAND (sub, 2)
14989 24 : = cp_build_compound_expr (set, TREE_OPERAND (sub, 2),
14990 : tf_warning_or_error);
14991 : }
14992 22775 : return init;
14993 : }
14994 704208 : if (TREE_CODE (sub) != ADDR_EXPR)
14995 : return init;
14996 : /* Deal with binding to a subobject. */
14997 234122 : for (p = &TREE_OPERAND (sub, 0);
14998 247392 : TREE_CODE (*p) == COMPONENT_REF || TREE_CODE (*p) == ARRAY_REF; )
14999 13270 : p = &TREE_OPERAND (*p, 0);
15000 234122 : if (TREE_CODE (*p) == TARGET_EXPR)
15001 : {
15002 7109 : tree subinit = NULL_TREE;
15003 7109 : *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit,
15004 : cond_guard, nullptr);
15005 7109 : recompute_tree_invariant_for_addr_expr (sub);
15006 7109 : if (init != sub)
15007 7091 : init = fold_convert (TREE_TYPE (init), sub);
15008 7109 : if (subinit)
15009 6002 : init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
15010 : }
15011 : return init;
15012 : }
15013 :
15014 : /* Data for extend_temps_r, mostly matching the parameters of
15015 : extend_ref_init_temps. */
15016 :
15017 : struct extend_temps_data
15018 : {
15019 : tree decl;
15020 : tree init;
15021 : vec<tree, va_gc> **cleanups;
15022 : tree* cond_guard;
15023 : hash_set<tree> *pset; // For avoiding redundant walk_tree.
15024 : hash_map<tree, tree> *var_map; // For remapping extended temps.
15025 : };
15026 :
15027 : /* Tree walk function for extend_all_temps. Generally parallel to
15028 : extend_ref_init_temps_1, but adapted for walk_tree. */
15029 :
15030 : tree
15031 96338 : extend_temps_r (tree *tp, int *walk_subtrees, void *data)
15032 : {
15033 96338 : extend_temps_data *d = (extend_temps_data *)data;
15034 :
15035 96338 : if (TREE_CODE (*tp) == VAR_DECL)
15036 : {
15037 12942 : if (tree *r = d->var_map->get (*tp))
15038 0 : *tp = *r;
15039 12942 : return NULL_TREE;
15040 : }
15041 :
15042 83377 : if (TYPE_P (*tp) || TREE_CODE (*tp) == CLEANUP_POINT_EXPR
15043 166772 : || d->pset->add (*tp))
15044 : {
15045 4740 : *walk_subtrees = 0;
15046 4740 : return NULL_TREE;
15047 : }
15048 :
15049 78656 : if (TREE_CODE (*tp) == COND_EXPR)
15050 : {
15051 8 : cp_walk_tree (&TREE_OPERAND (*tp, 0), extend_temps_r, d, nullptr);
15052 :
15053 24 : auto walk_arm = [d](tree &op)
15054 : {
15055 16 : tree cur_cond_guard = NULL_TREE;
15056 16 : auto ov = make_temp_override (d->cond_guard, &cur_cond_guard);
15057 16 : cp_walk_tree (&op, extend_temps_r, d, nullptr);
15058 16 : if (cur_cond_guard)
15059 : {
15060 2 : tree set = build2 (MODIFY_EXPR, boolean_type_node,
15061 : cur_cond_guard, boolean_true_node);
15062 2 : op = cp_build_compound_expr (set, op, tf_none);
15063 : }
15064 24 : };
15065 8 : walk_arm (TREE_OPERAND (*tp, 1));
15066 8 : walk_arm (TREE_OPERAND (*tp, 2));
15067 :
15068 8 : *walk_subtrees = 0;
15069 8 : return NULL_TREE;
15070 : }
15071 :
15072 78648 : tree *p = tp;
15073 :
15074 78648 : if (TREE_CODE (*tp) == ADDR_EXPR)
15075 17993 : for (p = &TREE_OPERAND (*tp, 0);
15076 19442 : TREE_CODE (*p) == COMPONENT_REF || TREE_CODE (*p) == ARRAY_REF; )
15077 1449 : p = &TREE_OPERAND (*p, 0);
15078 :
15079 78648 : if (TREE_CODE (*p) == TARGET_EXPR
15080 : /* An eliding TARGET_EXPR isn't a temporary at all. */
15081 3482 : && !TARGET_EXPR_ELIDING_P (*p)
15082 : /* A TARGET_EXPR with TARGET_EXPR_INTERNAL_P is an artificial variable
15083 : used during initialization that need not be extended. */
15084 81058 : && !TARGET_EXPR_INTERNAL_P (*p))
15085 : {
15086 : /* A CLEANUP_EH_ONLY expr should also have TARGET_EXPR_INTERNAL_P. */
15087 2076 : gcc_checking_assert (!CLEANUP_EH_ONLY (*p));
15088 :
15089 2076 : tree subinit = NULL_TREE;
15090 2076 : tree slot = TARGET_EXPR_SLOT (*p);
15091 2076 : *p = set_up_extended_ref_temp (d->decl, *p, d->cleanups, &subinit,
15092 : d->cond_guard, d);
15093 2076 : if (TREE_CODE (*tp) == ADDR_EXPR)
15094 2062 : recompute_tree_invariant_for_addr_expr (*tp);
15095 2076 : if (subinit)
15096 1928 : *tp = cp_build_compound_expr (subinit, *tp, tf_none);
15097 2076 : d->var_map->put (slot, *p);
15098 : }
15099 :
15100 : return NULL_TREE;
15101 : }
15102 :
15103 : /* Extend all the temporaries in a for-range-initializer. */
15104 :
15105 : static tree
15106 19647 : extend_all_temps (tree decl, tree init, vec<tree, va_gc> **cleanups)
15107 : {
15108 19647 : hash_set<tree> pset;
15109 19647 : hash_map<tree, tree> map;
15110 19647 : gcc_assert (!TREE_STATIC (decl));
15111 19647 : extend_temps_data d = { decl, init, cleanups, nullptr, &pset, &map };
15112 19647 : cp_walk_tree (&init, extend_temps_r, &d, nullptr);
15113 19647 : return init;
15114 19647 : }
15115 :
15116 : /* INIT is part of the initializer for DECL. If there are any
15117 : reference or initializer lists being initialized, extend their
15118 : lifetime to match that of DECL. */
15119 :
15120 : tree
15121 110374635 : extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups,
15122 : tree *cond_guard)
15123 : {
15124 110374635 : tree type = TREE_TYPE (init);
15125 110374635 : if (processing_template_decl)
15126 : return init;
15127 :
15128 : /* P2718R0 - in C++23 for-range-initializer, extend all temps. */
15129 107932162 : if (DECL_NAME (decl) == for_range__identifier
15130 93851 : && flag_range_for_ext_temps
15131 : /* Iterating expansion statement decl is static right now, but that
15132 : could change depending on CWG3044 and CWG3043. */
15133 107951867 : && !TREE_STATIC (decl))
15134 : {
15135 19647 : gcc_checking_assert (!cond_guard);
15136 19647 : return extend_all_temps (decl, init, cleanups);
15137 : }
15138 :
15139 107912515 : maybe_warn_dangling_reference (decl, init);
15140 :
15141 107912515 : if (TYPE_REF_P (type))
15142 680783 : init = extend_ref_init_temps_1 (decl, init, cleanups, cond_guard);
15143 : else
15144 : {
15145 107231732 : tree ctor = init;
15146 107231732 : if (TREE_CODE (ctor) == TARGET_EXPR)
15147 1517256 : ctor = TARGET_EXPR_INITIAL (ctor);
15148 107231732 : if (TREE_CODE (ctor) == CONSTRUCTOR)
15149 : {
15150 : /* [dcl.init] When initializing an aggregate from a parenthesized list
15151 : of values... a temporary object bound to a reference does not have
15152 : its lifetime extended. */
15153 5573630 : if (CONSTRUCTOR_IS_PAREN_INIT (ctor))
15154 : return init;
15155 :
15156 5573142 : if (is_std_init_list (type))
15157 : {
15158 : /* The temporary array underlying a std::initializer_list
15159 : is handled like a reference temporary. */
15160 650 : tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
15161 650 : array = extend_ref_init_temps_1 (decl, array, cleanups,
15162 : cond_guard);
15163 650 : CONSTRUCTOR_ELT (ctor, 0)->value = array;
15164 : }
15165 : else
15166 : {
15167 5572492 : unsigned i;
15168 5572492 : constructor_elt *p;
15169 5572492 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
15170 62437722 : FOR_EACH_VEC_SAFE_ELT (elts, i, p)
15171 56865230 : p->value = extend_ref_init_temps (decl, p->value, cleanups,
15172 : cond_guard);
15173 : }
15174 5573142 : recompute_constructor_flags (ctor);
15175 5573142 : if (decl_maybe_constant_var_p (decl) && TREE_CONSTANT (ctor))
15176 3114548 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
15177 : }
15178 : }
15179 :
15180 : return init;
15181 : }
15182 :
15183 : /* Returns true iff an initializer for TYPE could contain temporaries that
15184 : need to be extended because they are bound to references or
15185 : std::initializer_list. */
15186 :
15187 : bool
15188 0 : type_has_extended_temps (tree type)
15189 : {
15190 0 : type = strip_array_types (type);
15191 0 : if (TYPE_REF_P (type))
15192 : return true;
15193 0 : if (CLASS_TYPE_P (type))
15194 : {
15195 0 : if (is_std_init_list (type))
15196 : return true;
15197 0 : for (tree f = next_aggregate_field (TYPE_FIELDS (type));
15198 0 : f; f = next_aggregate_field (DECL_CHAIN (f)))
15199 0 : if (type_has_extended_temps (TREE_TYPE (f)))
15200 : return true;
15201 : }
15202 : return false;
15203 : }
15204 :
15205 : /* Returns true iff TYPE is some variant of std::initializer_list. */
15206 :
15207 : bool
15208 180441218 : is_std_init_list (tree type)
15209 : {
15210 180441218 : if (!TYPE_P (type))
15211 : return false;
15212 180441195 : if (cxx_dialect == cxx98)
15213 : return false;
15214 : /* Look through typedefs. */
15215 180070803 : type = TYPE_MAIN_VARIANT (type);
15216 144325663 : return (CLASS_TYPE_P (type)
15217 144181983 : && CP_TYPE_CONTEXT (type) == std_node
15218 260555474 : && init_list_identifier == DECL_NAME (TYPE_NAME (type)));
15219 : }
15220 :
15221 : /* Returns true iff DECL is a list constructor: i.e. a constructor which
15222 : will accept an argument list of a single std::initializer_list<T>. */
15223 :
15224 : bool
15225 156867007 : is_list_ctor (tree decl)
15226 : {
15227 156867007 : tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
15228 156867007 : tree arg;
15229 :
15230 156867007 : if (!args || args == void_list_node)
15231 : return false;
15232 :
15233 126283354 : arg = non_reference (TREE_VALUE (args));
15234 126283354 : if (!is_std_init_list (arg))
15235 : return false;
15236 :
15237 2269535 : args = TREE_CHAIN (args);
15238 :
15239 3865168 : if (args && args != void_list_node && !TREE_PURPOSE (args))
15240 : /* There are more non-defaulted parms. */
15241 : return false;
15242 :
15243 : return true;
15244 : }
15245 :
15246 : /* We know that can_convert_arg_bad already said "no" when trying to convert
15247 : FROM to TO with ARG and FLAGS. Try to figure out if it was because
15248 : an explicit conversion function was skipped when looking for a way to
15249 : perform the conversion. At this point we've already printed an error. */
15250 :
15251 : void
15252 2128 : maybe_show_nonconverting_candidate (tree to, tree from, tree arg, int flags)
15253 : {
15254 2128 : if (!(flags & LOOKUP_ONLYCONVERTING))
15255 293 : return;
15256 :
15257 1835 : conversion_obstack_sentinel cos;
15258 1835 : conversion *c = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
15259 : flags & ~LOOKUP_ONLYCONVERTING, tf_none);
15260 1835 : if (c && !c->bad_p && c->user_conv_p)
15261 : /* Ay, the conversion would have worked in direct-init context. */
15262 859 : for (; c; c = next_conversion (c))
15263 575 : if (c->kind == ck_user
15264 281 : && DECL_P (c->cand->fn)
15265 856 : && DECL_NONCONVERTING_P (c->cand->fn))
15266 277 : inform (DECL_SOURCE_LOCATION (c->cand->fn), "explicit conversion "
15267 : "function was not considered");
15268 1835 : }
15269 :
15270 : /* We're converting EXPR to TYPE. If that conversion involves a conversion
15271 : function and we're binding EXPR to a reference parameter of that function,
15272 : return true. */
15273 :
15274 : bool
15275 171 : conv_binds_to_reference_parm_p (tree type, tree expr)
15276 : {
15277 171 : conversion_obstack_sentinel cos;
15278 171 : conversion *c = implicit_conversion (type, TREE_TYPE (expr), expr,
15279 : /*c_cast_p=*/false, LOOKUP_NORMAL,
15280 : tf_none);
15281 171 : if (c && !c->bad_p && c->user_conv_p)
15282 117 : for (; c; c = next_conversion (c))
15283 81 : if (c->kind == ck_user)
15284 244 : for (z_candidate *cand = c->cand; cand; cand = cand->next)
15285 208 : if (cand->viable == 1)
15286 81 : for (size_t i = 0; i < cand->num_convs; ++i)
15287 45 : if (cand->convs[i]->kind == ck_ref_bind
15288 45 : && conv_get_original_expr (cand->convs[i]) == expr)
15289 : return true;
15290 :
15291 : return false;
15292 171 : }
15293 :
15294 : #include "gt-cp-call.h"
|