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 3214919 : check_dtor_name (tree basetype, tree name)
236 : {
237 : /* Just accept something we've already complained about. */
238 3214919 : if (name == error_mark_node)
239 : return true;
240 :
241 3214919 : if (TREE_CODE (name) == TYPE_DECL)
242 84 : name = TREE_TYPE (name);
243 3214835 : 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 3214893 : 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 362647985 : build_addr_func (tree function, tsubst_flags_t complain)
282 : {
283 362647985 : tree type = TREE_TYPE (function);
284 :
285 : /* We have to do these by hand to avoid real pointer to member
286 : functions. */
287 362647985 : if (TREE_CODE (type) == METHOD_TYPE)
288 : {
289 55379653 : 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 55379580 : function = build_address (function);
297 : }
298 307268332 : else if (TREE_CODE (function) == FUNCTION_DECL
299 438718000 : && DECL_IMMEDIATE_FUNCTION_P (function))
300 1939167 : function = build_address (function);
301 : else
302 305329165 : 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 171018 : build_call_n (tree function, int n, ...)
315 : {
316 171018 : if (n == 0)
317 0 : return build_call_a (function, 0, NULL);
318 : else
319 : {
320 171018 : tree *argarray = XALLOCAVEC (tree, n);
321 171018 : va_list ap;
322 171018 : int i;
323 :
324 171018 : va_start (ap, n);
325 343201 : for (i = 0; i < n; i++)
326 172183 : argarray[i] = va_arg (ap, tree);
327 171018 : va_end (ap);
328 171018 : 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 176998176 : set_flags_from_callee (tree call)
337 : {
338 : /* Handle both CALL_EXPRs and AGGR_INIT_EXPRs. */
339 176998176 : 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 176998176 : bool nothrow = decl && TREE_NOTHROW (decl);
344 176998176 : tree callee = cp_get_callee (call);
345 176998176 : if (callee)
346 176998168 : 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 176998176 : if (cfun && cp_function_chain && !cp_unevaluated_operand)
352 : {
353 101551285 : if (!nothrow && at_function_scope_p ())
354 25099554 : cp_function_chain->can_throw = 1;
355 :
356 101551285 : if (decl && TREE_THIS_VOLATILE (decl))
357 3050146 : current_function_returns_abnormally = 1;
358 : }
359 :
360 176998176 : TREE_NOTHROW (call) = nothrow;
361 176998176 : }
362 :
363 : tree
364 170940801 : build_call_a (tree function, int n, tree *argarray)
365 : {
366 170940801 : tree decl;
367 170940801 : tree result_type;
368 170940801 : tree fntype;
369 170940801 : int i;
370 :
371 170940801 : function = build_addr_func (function, tf_warning_or_error);
372 :
373 170940801 : gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
374 170940801 : fntype = TREE_TYPE (TREE_TYPE (function));
375 170940801 : gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype));
376 170940801 : result_type = TREE_TYPE (fntype);
377 : /* An rvalue has no cv-qualifiers. */
378 170940801 : if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
379 99602884 : result_type = cv_unqualified (result_type);
380 :
381 170940801 : function = build_call_array_loc (input_location,
382 : result_type, function, n, argarray);
383 170940801 : set_flags_from_callee (function);
384 :
385 170940801 : decl = get_callee_fndecl (function);
386 :
387 170940801 : 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 216714 : gcc_assert (DECL_ARTIFICIAL (decl)
394 : || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
395 : "__", 2));
396 216714 : mark_used (decl);
397 : }
398 :
399 170940801 : require_complete_eh_spec_types (fntype, decl);
400 :
401 493050233 : 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 170940801 : if (!decl || !fndecl_built_in_p (decl))
407 342927495 : for (i = 0; i < n; i++)
408 : {
409 179913342 : tree arg = CALL_EXPR_ARG (function, i);
410 179913342 : if (is_empty_class (TREE_TYPE (arg))
411 179913342 : && simple_empty_class_p (TREE_TYPE (arg), arg, INIT_EXPR))
412 : {
413 5677768 : while (TREE_CODE (arg) == TARGET_EXPR)
414 : /* We're disconnecting the initializer from its target,
415 : don't create a temporary. */
416 2837704 : arg = TARGET_EXPR_INITIAL (arg);
417 2840064 : suppress_warning (arg, OPT_Wunused_result);
418 2840064 : tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg));
419 2840064 : arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t);
420 2840064 : CALL_EXPR_ARG (function, i) = arg;
421 : }
422 : }
423 :
424 170940801 : 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 65571112 : bool rewritten () const { return (flags & LOOKUP_REWRITTEN); }
543 1059818144 : 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 130699073 : null_ptr_cst_p (tree t)
551 : {
552 130699073 : 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 130699073 : if (NULLPTR_TYPE_P (type))
559 : return true;
560 :
561 121534803 : if (cxx_dialect >= cxx11)
562 : {
563 121137955 : STRIP_ANY_LOCATION_WRAPPER (t);
564 :
565 : /* Core issue 903 says only literal 0 is a null pointer constant. */
566 121137955 : if (TREE_CODE (t) == INTEGER_CST
567 13807260 : && !TREE_OVERFLOW (t)
568 13807260 : && TREE_CODE (type) == INTEGER_TYPE
569 13198954 : && integer_zerop (t)
570 129906095 : && !char_type_p (type))
571 8767844 : return true;
572 : }
573 396848 : else if (CP_INTEGRAL_TYPE_P (type))
574 : {
575 76647 : t = fold_non_dependent_expr (t, tf_none);
576 76647 : STRIP_NOPS (t);
577 76647 : if (integer_zerop (t) && !TREE_OVERFLOW (t))
578 54973 : return true;
579 : }
580 :
581 : return false;
582 : }
583 :
584 : /* Returns true iff T is a null member pointer value (4.11). */
585 :
586 : bool
587 102110187 : null_member_pointer_value_p (tree t)
588 : {
589 102110187 : tree type = TREE_TYPE (t);
590 102110187 : if (!type)
591 : return false;
592 101922212 : 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 101921103 : 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 738253763 : sufficient_parms_p (const_tree parmlist)
607 : {
608 748071242 : for (; parmlist && parmlist != void_list_node;
609 9817479 : parmlist = TREE_CHAIN (parmlist))
610 225179617 : if (!TREE_PURPOSE (parmlist)
611 225179617 : && !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 7999912962 : conversion_obstack_alloc (size_t n)
621 : {
622 7999912962 : void *p;
623 7999912962 : if (!conversion_obstack_initialized)
624 : {
625 86487 : gcc_obstack_init (&conversion_obstack);
626 86487 : conversion_obstack_initialized = true;
627 : }
628 7999912962 : p = obstack_alloc (&conversion_obstack, n);
629 7999912962 : memset (p, 0, n);
630 7999912962 : 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 2602308306 : conversion_obstack_sentinel (): p (conversion_obstack_alloc (0)) {}
639 1301140599 : ~conversion_obstack_sentinel () { obstack_free (&conversion_obstack, p); }
640 : };
641 :
642 : /* Allocate rejection reasons. */
643 :
644 : static struct rejection_reason *
645 767694164 : alloc_rejection (enum rejection_reason_code code)
646 : {
647 767694164 : struct rejection_reason *p;
648 0 : p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
649 767694164 : p->code = code;
650 767694164 : return p;
651 : }
652 :
653 : static struct rejection_reason *
654 196357513 : arity_rejection (tree first_arg, int expected, int actual, bool least_p = false)
655 : {
656 156426449 : struct rejection_reason *r = alloc_rejection (rr_arity);
657 196357513 : int adjust = first_arg != NULL_TREE;
658 196357513 : r->u.arity.expected = expected - adjust;
659 196357513 : r->u.arity.actual = actual - adjust;
660 196357513 : r->u.arity.least_p = least_p;
661 196357513 : return r;
662 : }
663 :
664 : static struct rejection_reason *
665 163826080 : arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to,
666 : location_t loc)
667 : {
668 7117752 : struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
669 163826080 : int adjust = first_arg != NULL_TREE;
670 163826080 : r->u.conversion.n_arg = n_arg - adjust;
671 163826080 : r->u.conversion.from = from;
672 163826080 : r->u.conversion.to_type = to;
673 163826080 : r->u.conversion.loc = loc;
674 163826080 : return r;
675 : }
676 :
677 : static struct rejection_reason *
678 22535874 : 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 22535874 : int adjust = first_arg != NULL_TREE;
683 22535874 : r->u.bad_conversion.n_arg = n_arg - adjust;
684 22535874 : r->u.bad_conversion.from = from;
685 22535874 : r->u.bad_conversion.to_type = to;
686 22535874 : r->u.bad_conversion.loc = loc;
687 22535874 : return r;
688 : }
689 :
690 : static struct rejection_reason *
691 2731 : explicit_conversion_rejection (tree from, tree to)
692 : {
693 2731 : struct rejection_reason *r = alloc_rejection (rr_explicit_conversion);
694 2731 : r->u.conversion.n_arg = 0;
695 2731 : r->u.conversion.from = from;
696 2731 : r->u.conversion.to_type = to;
697 2731 : r->u.conversion.loc = UNKNOWN_LOCATION;
698 2731 : 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 383210236 : 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 383210236 : size_t args_n_bytes = sizeof (*args) * nargs;
719 383210236 : tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes);
720 383210236 : struct rejection_reason *r = alloc_rejection (rr_template_unification);
721 383210236 : r->u.template_unification.tmpl = tmpl;
722 383210236 : r->u.template_unification.explicit_targs = explicit_targs;
723 383210236 : r->u.template_unification.num_targs = TREE_VEC_LENGTH (targs);
724 : /* Copy args to our own storage. */
725 383210236 : memcpy (args1, args, args_n_bytes);
726 383210236 : r->u.template_unification.args = args1;
727 383210236 : r->u.template_unification.nargs = nargs;
728 383210236 : r->u.template_unification.return_type = return_type;
729 383210236 : r->u.template_unification.strict = strict;
730 383210236 : r->u.template_unification.flags = flags;
731 383210236 : 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 756277 : invalid_copy_with_fn_template_rejection (void)
742 : {
743 0 : struct rejection_reason *r = alloc_rejection (rr_invalid_copy);
744 756277 : return r;
745 : }
746 :
747 : static struct rejection_reason *
748 806653 : inherited_ctor_rejection (void)
749 : {
750 0 : struct rejection_reason *r = alloc_rejection (rr_inherited_ctor);
751 806653 : return r;
752 : }
753 :
754 : /* Build a constraint failure record. */
755 :
756 : static struct rejection_reason *
757 198663 : constraint_failure (void)
758 : {
759 0 : struct rejection_reason *r = alloc_rejection (rr_constraint_failure);
760 198663 : return r;
761 : }
762 :
763 : /* Dynamically allocate a conversion. */
764 :
765 : static conversion *
766 2767006355 : alloc_conversion (conversion_kind kind)
767 : {
768 2767006355 : conversion *c;
769 0 : c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
770 2767006355 : c->kind = kind;
771 2767006355 : return c;
772 : }
773 :
774 : /* Make sure that all memory on the conversion obstack has been
775 : freed. */
776 :
777 : void
778 98753 : validate_conversion_obstack (void)
779 : {
780 98753 : if (conversion_obstack_initialized)
781 86153 : gcc_assert ((obstack_next_free (&conversion_obstack)
782 : == obstack_base (&conversion_obstack)));
783 98753 : }
784 :
785 : /* Dynamically allocate an array of N conversions. */
786 :
787 : static conversion **
788 1031197354 : 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 1520039023 : has_next (conversion_kind code)
797 : {
798 1428675525 : return !(code == ck_identity
799 1520039023 : || code == ck_ambig
800 : || code == ck_list
801 1428768346 : || code == ck_aggr
802 1520039023 : || code == ck_deferred_bad);
803 : }
804 :
805 : static conversion *
806 815864227 : build_conv (conversion_kind code, tree type, conversion *from)
807 : {
808 815864227 : conversion *t;
809 815864227 : conversion_rank rank = CONVERSION_RANK (from);
810 :
811 : /* Only call this function for conversions that use u.next. */
812 815864227 : 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 815864227 : t = alloc_conversion (code);
817 815864227 : t->type = type;
818 815864227 : t->u.next = from;
819 :
820 815864227 : switch (code)
821 : {
822 238682573 : case ck_ptr:
823 238682573 : case ck_pmem:
824 238682573 : case ck_base:
825 238682573 : case ck_std:
826 238682573 : if (rank < cr_std)
827 815864227 : rank = cr_std;
828 : break;
829 :
830 55132879 : case ck_qual:
831 55132879 : case ck_fnptr:
832 55132879 : if (rank < cr_exact)
833 815864227 : rank = cr_exact;
834 : break;
835 :
836 : default:
837 : break;
838 : }
839 815864227 : t->rank = rank;
840 815864227 : t->user_conv_p = (code == ck_user || from->user_conv_p);
841 815864227 : t->bad_p = from->bad_p;
842 815864227 : t->base_p = false;
843 815864227 : 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 97460 : build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
852 : {
853 97460 : tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
854 97460 : unsigned len = CONSTRUCTOR_NELTS (ctor);
855 97460 : conversion **subconvs = alloc_conversions (len);
856 97460 : conversion *t;
857 97460 : unsigned i;
858 97460 : tree val;
859 :
860 : /* Within a list-initialization we can have more user-defined
861 : conversions. */
862 97460 : flags &= ~LOOKUP_NO_CONVERSION;
863 : /* But no narrowing conversions. */
864 97460 : flags |= LOOKUP_NO_NARROWING;
865 :
866 : /* Can't make an array of these types. */
867 97460 : if (TYPE_REF_P (elttype)
868 97451 : || TREE_CODE (elttype) == FUNCTION_TYPE
869 97451 : || VOID_TYPE_P (elttype))
870 : return NULL;
871 :
872 361933 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
873 : {
874 278094 : 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 278043 : conversion *sub
936 278043 : = implicit_conversion (elttype, TREE_TYPE (val), val,
937 : false, flags, complain);
938 278043 : if (sub == NULL)
939 : return NULL;
940 :
941 264431 : subconvs[i] = sub;
942 : }
943 :
944 83839 : t = alloc_conversion (ck_list);
945 83839 : t->type = type;
946 83839 : t->u.list = subconvs;
947 83839 : t->rank = cr_exact;
948 :
949 329491 : for (i = 0; i < len; ++i)
950 : {
951 245652 : conversion *sub = subconvs[i];
952 245652 : if (sub->rank > t->rank)
953 63481 : t->rank = sub->rank;
954 245652 : if (sub->user_conv_p)
955 10356 : t->user_conv_p = true;
956 245652 : if (sub->bad_p)
957 63468 : 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 613049566 : next_conversion (conversion *conv)
969 : {
970 613049566 : if (conv == NULL
971 613049566 : || !has_next (conv->kind))
972 : return NULL;
973 600658707 : 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 1532658 : strip_standard_conversion (conversion *conv)
981 : {
982 1532658 : while (conv
983 1543108 : && conv->kind != ck_user
984 1585012 : && has_next (conv->kind))
985 10450 : conv = next_conversion (conv);
986 1532658 : 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 27840 : can_convert_array (tree atype, tree from, int flags, tsubst_flags_t complain)
994 : {
995 27840 : tree elttype = TREE_TYPE (atype);
996 27840 : unsigned i;
997 :
998 27840 : if (TREE_CODE (from) == CONSTRUCTOR)
999 : {
1000 31044 : for (i = 0; i < CONSTRUCTOR_NELTS (from); ++i)
1001 : {
1002 3249 : tree val = CONSTRUCTOR_ELT (from, i)->value;
1003 3249 : bool ok;
1004 3249 : if (TREE_CODE (elttype) == ARRAY_TYPE)
1005 18 : ok = can_convert_array (elttype, val, flags, complain);
1006 : else
1007 3231 : ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags,
1008 : complain);
1009 3249 : 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 869333 : field_in_pset (hash_set<tree, true> &pset, tree field)
1029 : {
1030 869333 : if (pset.contains (field))
1031 : return true;
1032 2598 : 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 1367225 : build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
1050 : {
1051 1367225 : unsigned HOST_WIDE_INT i = 0;
1052 1367225 : conversion *c;
1053 1367225 : tree field = next_aggregate_field (TYPE_FIELDS (type));
1054 1367225 : tree empty_ctor = NULL_TREE;
1055 1367225 : 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 1367225 : 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 1367225 : tree idx, val;
1069 3601185 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, val)
1070 : {
1071 866827 : if (!idx)
1072 : break;
1073 :
1074 866767 : gcc_checking_assert (TREE_CODE (idx) == FIELD_DECL);
1075 :
1076 866767 : tree ftype = TREE_TYPE (idx);
1077 866767 : bool ok;
1078 :
1079 866767 : if (TREE_CODE (ftype) == ARRAY_TYPE)
1080 1185 : ok = can_convert_array (ftype, val, flags, complain);
1081 : else
1082 865582 : ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
1083 : complain);
1084 :
1085 866767 : if (!ok)
1086 : return NULL;
1087 :
1088 : /* For unions, there should be just one initializer. */
1089 866758 : if (TREE_CODE (type) == UNION_TYPE)
1090 : {
1091 1367216 : field = NULL_TREE;
1092 1367216 : i = 1;
1093 : break;
1094 : }
1095 866735 : pset.add (idx);
1096 : }
1097 :
1098 2344287 : for (; field; field = next_aggregate_field (DECL_CHAIN (field)))
1099 : {
1100 977331 : tree ftype = TREE_TYPE (field);
1101 977331 : bool ok;
1102 :
1103 977331 : if (!pset.is_empty () && field_in_pset (pset, field))
1104 866735 : continue;
1105 110596 : 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 110557 : else if (DECL_INITIAL (field))
1113 44911 : val = get_nsdmi (field, /*ctor*/false, complain);
1114 65646 : else if (TYPE_REF_P (ftype))
1115 : /* Value-initialization of reference is ill-formed. */
1116 : return NULL;
1117 : else
1118 : {
1119 65640 : if (empty_ctor == NULL_TREE)
1120 46825 : empty_ctor = build_constructor (init_list_type_node, NULL);
1121 : val = empty_ctor;
1122 : }
1123 :
1124 110590 : if (TREE_CODE (ftype) == ARRAY_TYPE)
1125 26637 : ok = can_convert_array (ftype, val, flags, complain);
1126 : else
1127 83953 : ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
1128 : complain);
1129 :
1130 110590 : if (!ok)
1131 : return NULL;
1132 :
1133 110577 : if (TREE_CODE (type) == UNION_TYPE)
1134 : break;
1135 : }
1136 :
1137 1367197 : if (i < CONSTRUCTOR_NELTS (ctor))
1138 : return NULL;
1139 :
1140 1367170 : c = alloc_conversion (ck_aggr);
1141 1367170 : c->type = type;
1142 1367170 : c->rank = cr_exact;
1143 1367170 : c->user_conv_p = true;
1144 1367170 : c->check_narrowing = true;
1145 1367170 : c->u.expr = ctor;
1146 1367170 : return c;
1147 1367225 : }
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 57172 : build_complex_conv (tree type, tree ctor, int flags,
1205 : tsubst_flags_t complain)
1206 : {
1207 57172 : conversion *c;
1208 57172 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1209 57172 : tree elttype = TREE_TYPE (type);
1210 57172 : bool bad = false;
1211 57172 : bool user = false;
1212 57172 : enum conversion_rank rank = cr_exact;
1213 :
1214 57172 : if (len != 2)
1215 : return NULL;
1216 :
1217 57132 : flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1218 :
1219 285660 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
1220 : {
1221 114264 : conversion *sub
1222 114264 : = implicit_conversion (elttype, TREE_TYPE (e.value), e.value,
1223 : false, flags, complain);
1224 114264 : if (sub == NULL)
1225 : return NULL;
1226 :
1227 114264 : if (sub->rank > rank)
1228 44 : rank = sub->rank;
1229 114264 : if (sub->user_conv_p)
1230 0 : user = true;
1231 114264 : if (sub->bad_p)
1232 0 : bad = true;
1233 : }
1234 :
1235 57132 : c = alloc_conversion (ck_aggr);
1236 57132 : c->type = type;
1237 57132 : c->rank = rank;
1238 57132 : c->user_conv_p = user;
1239 57132 : c->bad_p = bad;
1240 57132 : c->u.expr = ctor;
1241 57132 : 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 1941906571 : build_identity_conv (tree type, tree expr)
1249 : {
1250 1941906571 : conversion *c;
1251 :
1252 0 : c = alloc_conversion (ck_identity);
1253 1941906571 : c->type = type;
1254 1941906571 : c->u.expr = expr;
1255 :
1256 1941906571 : 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 3483825618 : strip_top_quals (tree t)
1277 : {
1278 3483825618 : if (TREE_CODE (t) == ARRAY_TYPE)
1279 : return t;
1280 3477013665 : 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 1554978339 : standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
1290 : int flags, tsubst_flags_t complain)
1291 : {
1292 1554978339 : enum tree_code fcode, tcode;
1293 1554978339 : conversion *conv;
1294 1554978339 : bool fromref = false;
1295 1554978339 : tree qualified_to;
1296 :
1297 1554978339 : to = non_reference (to);
1298 1554978339 : if (TYPE_REF_P (from))
1299 : {
1300 80660 : fromref = true;
1301 80660 : from = TREE_TYPE (from);
1302 : }
1303 1554978339 : qualified_to = to;
1304 1554978339 : to = strip_top_quals (to);
1305 1554978339 : from = strip_top_quals (from);
1306 :
1307 1554978339 : if (expr && type_unknown_p (expr))
1308 : {
1309 348941 : if (TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
1310 : {
1311 38158 : tsubst_flags_t tflags = tf_conv;
1312 38158 : expr = instantiate_type (to, expr, tflags);
1313 38158 : if (expr == error_mark_node)
1314 : return NULL;
1315 23307 : from = TREE_TYPE (expr);
1316 : }
1317 310783 : else if (TREE_CODE (to) == BOOLEAN_TYPE)
1318 : {
1319 : /* Necessary for eg, TEMPLATE_ID_EXPRs (c++/50961). */
1320 7206 : expr = resolve_nondeduced_context (expr, complain);
1321 7206 : from = TREE_TYPE (expr);
1322 : }
1323 : }
1324 :
1325 1554963488 : fcode = TREE_CODE (from);
1326 1554963488 : tcode = TREE_CODE (to);
1327 :
1328 1554963488 : conv = build_identity_conv (from, expr);
1329 1554963488 : if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
1330 : {
1331 6835232 : from = type_decays_to (from);
1332 6835232 : fcode = TREE_CODE (from);
1333 : /* Tell convert_like that we're using the address. */
1334 6835232 : conv->rvaluedness_matches_p = true;
1335 6835232 : 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 1548128256 : else if (fromref || (expr && obvalue_p (expr)))
1341 : {
1342 362734513 : if (expr)
1343 : {
1344 362653865 : tree bitfield_type;
1345 362653865 : bitfield_type = is_bitfield_expr_with_lowered_type (expr);
1346 362653865 : if (bitfield_type)
1347 : {
1348 2907187 : from = strip_top_quals (bitfield_type);
1349 2907187 : fcode = TREE_CODE (from);
1350 : }
1351 : }
1352 362734513 : conv = build_conv (ck_rvalue, from, conv);
1353 : /* If we're performing copy-initialization, remember to skip
1354 : explicit constructors. */
1355 362734513 : if (flags & LOOKUP_ONLYCONVERTING)
1356 310695813 : conv->copy_init_p = true;
1357 : }
1358 :
1359 : /* Allow conversion between `__complex__' data types. */
1360 1554963488 : 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 2381859 : conversion *part_conv = standard_conversion
1366 2381859 : (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags,
1367 : complain);
1368 :
1369 2381859 : if (!part_conv)
1370 : conv = NULL;
1371 2381859 : else if (part_conv->kind == ck_identity)
1372 : /* Leave conv alone. */;
1373 : else
1374 : {
1375 277930 : conv = build_conv (part_conv->kind, to, conv);
1376 277930 : conv->rank = part_conv->rank;
1377 : }
1378 :
1379 : return conv;
1380 : }
1381 :
1382 1552581629 : if (same_type_p (from, to))
1383 : {
1384 949281446 : if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue)
1385 17735504 : conv->type = qualified_to;
1386 931545942 : else if (from != to)
1387 : /* Use TO in order to not lose TO in diagnostics. */
1388 81904176 : conv->type = to;
1389 : 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 388545909 : if ((tcode == POINTER_TYPE || TYPE_PTRMEM_P (to)
1397 388454346 : || NULLPTR_TYPE_P (to))
1398 609217217 : && ((expr && null_ptr_cst_p (expr))
1399 214963747 : || NULLPTR_TYPE_P (from)))
1400 5707565 : conv = build_conv (ck_std, to, conv);
1401 597592618 : else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
1402 596965776 : || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
1403 : {
1404 : /* For backwards brain damage compatibility, allow interconversion of
1405 : pointers and integers with a pedwarn. */
1406 2145756 : conv = build_conv (ck_std, to, conv);
1407 2145756 : conv->bad_p = true;
1408 : }
1409 595446862 : 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 364448 : conv = build_conv (ck_std, to, conv);
1414 364448 : conv->bad_p = true;
1415 : }
1416 595082414 : else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
1417 392363756 : || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from)))
1418 : {
1419 2765 : tree to_pointee;
1420 2765 : tree from_pointee;
1421 :
1422 2765 : if (tcode == POINTER_TYPE)
1423 : {
1424 202718658 : to_pointee = TREE_TYPE (to);
1425 202718658 : 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 202718658 : if (TREE_CODE (to_pointee) == FUNCTION_TYPE
1431 202718658 : && TYPE_QUALS (to_pointee))
1432 9 : to_pointee = build_qualified_type (to_pointee, TYPE_UNQUALIFIED);
1433 202718658 : if (TREE_CODE (from_pointee) == FUNCTION_TYPE
1434 202718658 : && TYPE_QUALS (from_pointee))
1435 54 : from_pointee = build_qualified_type (from_pointee, TYPE_UNQUALIFIED);
1436 : }
1437 : else
1438 : {
1439 2765 : to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
1440 2765 : from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
1441 : }
1442 :
1443 202721423 : if (tcode == POINTER_TYPE
1444 202721423 : && same_type_ignoring_top_level_qualifiers_p (from_pointee,
1445 : to_pointee))
1446 : ;
1447 142920750 : else if (VOID_TYPE_P (to_pointee)
1448 4610416 : && !TYPE_PTRDATAMEM_P (from)
1449 4610416 : && TREE_CODE (from_pointee) != FUNCTION_TYPE)
1450 : {
1451 4610007 : tree nfrom = TREE_TYPE (from);
1452 : /* Don't try to apply restrict to void. */
1453 4610007 : int quals = cp_type_quals (nfrom) & ~TYPE_QUAL_RESTRICT;
1454 4610007 : from_pointee = cp_build_qualified_type (void_type_node, quals);
1455 4610007 : from = build_pointer_type (from_pointee);
1456 4610007 : conv = build_conv (ck_ptr, from, conv);
1457 4610007 : }
1458 138310743 : else if (TYPE_PTRDATAMEM_P (from))
1459 : {
1460 2765 : tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
1461 2765 : tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
1462 :
1463 2765 : 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 : return NULL;
1474 : }
1475 67195304 : else if (CLASS_TYPE_P (from_pointee)
1476 67192931 : && 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 204621093 : && DERIVED_FROM_P (to_pointee, from_pointee))
1489 : {
1490 6220639 : from_pointee
1491 6220639 : = cp_build_qualified_type (to_pointee,
1492 : cp_type_quals (from_pointee));
1493 6220639 : from = build_pointer_type (from_pointee);
1494 6220639 : conv = build_conv (ck_ptr, from, conv);
1495 6220639 : conv->base_p = true;
1496 : }
1497 :
1498 202721187 : if (same_type_p (from, to))
1499 : /* OK */;
1500 194562433 : 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 194561809 : else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
1506 54869705 : conv = build_conv (ck_qual, to, conv);
1507 139692104 : 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 139691900 : else if (fnptr_conv_p (to, from))
1511 244678 : conv = build_conv (ck_fnptr, to, conv);
1512 : /* Allow conversions among compatible ObjC pointer types (base
1513 : conversions have been already handled above). */
1514 139447222 : else if (c_dialect_objc ()
1515 139447222 : && objc_compare_types (to, from, -4, NULL_TREE))
1516 0 : conv = build_conv (ck_ptr, to, conv);
1517 139447222 : else if (ptr_reasonably_similar (to_pointee, from_pointee))
1518 : {
1519 8600788 : conv = build_conv (ck_ptr, to, conv);
1520 8600788 : conv->bad_p = true;
1521 : }
1522 : else
1523 : return NULL;
1524 :
1525 277638952 : from = to;
1526 : }
1527 392360991 : else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
1528 : {
1529 86415 : tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
1530 86415 : tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
1531 86415 : tree fbase = class_of_this_parm (fromfn);
1532 86415 : 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 86415 : if (!same_type_p (fbase, tbase) && !DERIVED_FROM_P (fbase, tbase))
1537 : return NULL;
1538 :
1539 86254 : tree fstat = static_fn_type (fromfn);
1540 86254 : tree tstat = static_fn_type (tofn);
1541 86254 : if (same_type_p (tstat, fstat)
1542 86254 : || fnptr_conv_p (tstat, fstat))
1543 : /* OK */;
1544 : else
1545 : return NULL;
1546 :
1547 85986 : if (!same_type_p (fbase, tbase))
1548 : {
1549 85873 : tree first = to;
1550 85873 : 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 85873 : conv = build_conv (ck_pmem, first, conv);
1559 85873 : conv->base_p = true;
1560 : }
1561 85986 : if (fnptr_conv_p (tstat, fstat))
1562 113 : conv = build_conv (ck_fnptr, to, conv);
1563 : }
1564 392274576 : 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 12411035 : if (ARITHMETIC_TYPE_P (from)
1573 2191716 : || UNSCOPED_ENUM_P (from)
1574 4232727 : || fcode == POINTER_TYPE
1575 2971898 : || TYPE_PTRMEM_P (from)
1576 2971675 : || NULLPTR_TYPE_P (from))
1577 : {
1578 9439442 : conv = build_conv (ck_std, to, conv);
1579 9439442 : if (fcode == POINTER_TYPE
1580 8178613 : || TYPE_PTRDATAMEM_P (from)
1581 8178467 : || (TYPE_PTRMEMFUNC_P (from)
1582 77 : && conv->rank < cr_pbool)
1583 17617832 : || NULLPTR_TYPE_P (from))
1584 1261134 : conv->rank = cr_pbool;
1585 9439442 : if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING))
1586 40 : conv->bad_p = true;
1587 9439442 : if (flags & LOOKUP_NO_NARROWING)
1588 31567 : conv->check_narrowing = true;
1589 : 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 379863541 : else if (ARITHMETIC_TYPE_P (to))
1598 : {
1599 31812632 : if (! (INTEGRAL_CODE_P (fcode)
1600 25053517 : || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL)))
1601 230289444 : || 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 196993250 : if (!COMPLETE_TYPE_P (from))
1608 : return NULL;
1609 :
1610 196993244 : conv = build_conv (ck_std, to, conv);
1611 :
1612 196993244 : tree underlying_type = NULL_TREE;
1613 196993244 : if (TREE_CODE (from) == ENUMERAL_TYPE
1614 196993244 : && ENUM_FIXED_UNDERLYING_TYPE_P (from))
1615 1712772 : 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 196993244 : if ((same_type_p (to, type_promotes_to (from))
1623 184989709 : || (underlying_type && same_type_p (to, underlying_type)))
1624 196993263 : && next_conversion (conv)->rank <= cr_promotion)
1625 12003554 : 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 196993244 : if (fcode == REAL_TYPE
1637 196993244 : && tcode == REAL_TYPE
1638 17057582 : && (extended_float_type_p (from)
1639 16242819 : || extended_float_type_p (to))
1640 200902172 : && cp_compare_floating_point_conversion_ranks (from, to) >= 2)
1641 1792962 : conv->bad_p = true;
1642 : }
1643 174627614 : else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
1644 174627614 : && vector_types_convertible_p (from, to, false))
1645 4905 : return build_conv (ck_std, to, conv);
1646 174622709 : else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
1647 69740625 : && is_properly_derived_from (from, to))
1648 : {
1649 467200 : if (conv->kind == ck_rvalue)
1650 467149 : conv = next_conversion (conv);
1651 467200 : 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 467200 : conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
1657 : /* If we're performing copy-initialization, remember to skip
1658 : explicit constructors. */
1659 467200 : if (flags & LOOKUP_ONLYCONVERTING)
1660 467133 : conv->copy_init_p = true;
1661 : }
1662 : else
1663 : return NULL;
1664 :
1665 277638952 : if (flags & LOOKUP_NO_NARROWING)
1666 95516140 : 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 279865357 : reference_related_p (tree t1, tree t2)
1677 : {
1678 279865357 : if (t1 == error_mark_node || t2 == error_mark_node)
1679 : return false;
1680 :
1681 279865353 : t1 = TYPE_MAIN_VARIANT (t1);
1682 279865353 : 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 279865353 : return (similar_type_p (t1, t2)
1689 279865353 : || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1690 68186336 : && DERIVED_FROM_P (t1, t2)));
1691 : }
1692 :
1693 : /* Returns nonzero if T1 is reference-compatible with T2. */
1694 :
1695 : bool
1696 248234303 : 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 248234303 : tree ptype1 = build_pointer_type (t1);
1704 248234303 : tree ptype2 = build_pointer_type (t2);
1705 248234303 : conversion *conv = standard_conversion (ptype1, ptype2, NULL_TREE,
1706 : /*c_cast_p=*/false, 0, tf_none);
1707 248234303 : if (!conv || conv->bad_p)
1708 137276206 : 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 121744172 : 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 121744172 : if (!((TYPE_PTR_P (to) && TYPE_PTR_P (from))
1721 3095 : || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from))))
1722 : return false;
1723 :
1724 4784817 : conversion *conv = standard_conversion (to, from, NULL_TREE,
1725 : /*c_cast_p=*/false, 0, tf_none);
1726 14336912 : for (conversion *t = conv; t; t = next_conversion (t))
1727 4784833 : 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 10428 : handler_match_for_exception_type (tree handler_type, tree except_type)
1738 : {
1739 10428 : if (handler_type && TREE_CODE (handler_type) == HANDLER)
1740 4053 : handler_type = TREE_TYPE (handler_type);
1741 4053 : if (handler_type == NULL_TREE)
1742 : return true; /* ... */
1743 10346 : if (same_type_ignoring_top_level_qualifiers_p (handler_type, except_type))
1744 : return true;
1745 6787 : if (CLASS_TYPE_P (except_type) && CLASS_TYPE_P (handler_type))
1746 3774 : return publicly_uniquely_derived_p (handler_type, except_type);
1747 3013 : if (TYPE_PTR_P (handler_type) || TYPE_PTRMEM_P (handler_type))
1748 : {
1749 813 : if (TREE_CODE (except_type) == NULLPTR_TYPE)
1750 : return true;
1751 748 : if ((TYPE_PTR_P (handler_type) && TYPE_PTR_P (except_type))
1752 80 : || (TYPE_PTRDATAMEM_P (handler_type)
1753 10 : && TYPE_PTRDATAMEM_P (except_type))
1754 844 : || (TYPE_PTRMEMFUNC_P (handler_type)
1755 16 : && TYPE_PTRMEMFUNC_P (except_type)))
1756 : {
1757 720 : conversion *conv
1758 720 : = standard_conversion (handler_type, except_type, NULL_TREE,
1759 : /*c_cast_p=*/false, 0, tf_none);
1760 720 : if (conv && !conv->bad_p)
1761 : {
1762 464 : for (conversion *t = conv; t; t = next_conversion (t))
1763 418 : switch (t->kind)
1764 : {
1765 336 : case ck_ptr:
1766 : /* ...not involving conversions to pointers to private or
1767 : protected or ambiguous classes, */
1768 336 : if (CLASS_TYPE_P (TREE_TYPE (handler_type)))
1769 320 : return (publicly_uniquely_derived_p
1770 320 : (TREE_TYPE (handler_type),
1771 640 : TREE_TYPE (except_type)));
1772 98 : gcc_fallthrough ();
1773 98 : case ck_fnptr:
1774 98 : case ck_qual:
1775 98 : case ck_identity:
1776 98 : break;
1777 : default:
1778 : return false;
1779 : }
1780 : return true;
1781 : }
1782 : }
1783 : }
1784 : return false;
1785 : }
1786 :
1787 : /* A reference of the indicated TYPE is being bound directly to the
1788 : expression represented by the implicit conversion sequence CONV.
1789 : Return a conversion sequence for this binding. */
1790 :
1791 : static conversion *
1792 125508559 : direct_reference_binding (tree type, conversion *conv)
1793 : {
1794 125508559 : tree t;
1795 :
1796 125508559 : gcc_assert (TYPE_REF_P (type));
1797 125508559 : gcc_assert (!TYPE_REF_P (conv->type));
1798 :
1799 125508559 : t = TREE_TYPE (type);
1800 :
1801 125508559 : if (conv->kind == ck_identity)
1802 : /* Mark the identity conv as to not decay to rvalue. */
1803 125508559 : conv->rvaluedness_matches_p = true;
1804 :
1805 : /* [over.ics.rank]
1806 :
1807 : When a parameter of reference type binds directly
1808 : (_dcl.init.ref_) to an argument expression, the implicit
1809 : conversion sequence is the identity conversion, unless the
1810 : argument expression has a type that is a derived class of the
1811 : parameter type, in which case the implicit conversion sequence is
1812 : a derived-to-base Conversion.
1813 :
1814 : If the parameter binds directly to the result of applying a
1815 : conversion function to the argument expression, the implicit
1816 : conversion sequence is a user-defined conversion sequence
1817 : (_over.ics.user_), with the second standard conversion sequence
1818 : either an identity conversion or, if the conversion function
1819 : returns an entity of a type that is a derived class of the
1820 : parameter type, a derived-to-base conversion. */
1821 125508559 : if (is_properly_derived_from (conv->type, t))
1822 : {
1823 : /* Represent the derived-to-base conversion. */
1824 3764387 : conv = build_conv (ck_base, t, conv);
1825 : /* We will actually be binding to the base-class subobject in
1826 : the derived class, so we mark this conversion appropriately.
1827 : That way, convert_like knows not to generate a temporary. */
1828 3764387 : conv->need_temporary_p = false;
1829 : }
1830 121744172 : else if (involves_qualification_conversion_p (t, conv->type))
1831 : /* Represent the qualification conversion. After DR 2352
1832 : #1 and #2 were indistinguishable conversion sequences:
1833 :
1834 : void f(int*); // #1
1835 : void f(const int* const &); // #2
1836 : void g(int* p) { f(p); }
1837 :
1838 : because the types "int *" and "const int *const" are
1839 : reference-related and we were binding both directly and they
1840 : had the same rank. To break it up, we add a ck_qual under the
1841 : ck_ref_bind so that conversion sequence ranking chooses #1.
1842 :
1843 : We strip_top_quals here which is also what standard_conversion
1844 : does. Failure to do so would confuse comp_cv_qual_signature
1845 : into thinking that in
1846 :
1847 : void f(const int * const &); // #1
1848 : void f(const int *); // #2
1849 : int *x;
1850 : f(x);
1851 :
1852 : #2 is a better match than #1 even though they're ambiguous (97296). */
1853 17555 : conv = build_conv (ck_qual, strip_top_quals (t), conv);
1854 :
1855 125508559 : return build_conv (ck_ref_bind, type, conv);
1856 : }
1857 :
1858 : /* Returns the conversion path from type FROM to reference type TO for
1859 : purposes of reference binding. For lvalue binding, either pass a
1860 : reference type to FROM or an lvalue expression to EXPR. If the
1861 : reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1862 : the conversion returned. If C_CAST_P is true, this
1863 : conversion is coming from a C-style cast. */
1864 :
1865 : static conversion *
1866 247376228 : reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags,
1867 : tsubst_flags_t complain)
1868 : {
1869 247376228 : conversion *conv = NULL;
1870 247376228 : conversion *bad_direct_conv = nullptr;
1871 247376228 : tree to = TREE_TYPE (rto);
1872 247376228 : tree from = rfrom;
1873 247376228 : tree tfrom;
1874 247376228 : bool related_p;
1875 247376228 : bool compatible_p;
1876 247376228 : cp_lvalue_kind gl_kind;
1877 247376228 : bool is_lvalue;
1878 :
1879 247376228 : if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1880 : {
1881 52 : expr = instantiate_type (to, expr, tf_none);
1882 52 : if (expr == error_mark_node)
1883 : return NULL;
1884 52 : from = TREE_TYPE (expr);
1885 : }
1886 :
1887 247376228 : bool copy_list_init = false;
1888 247376228 : bool single_list_conv = false;
1889 247376228 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1890 : {
1891 104352 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
1892 : /* DR 1288: Otherwise, if the initializer list has a single element
1893 : of type E and ... [T's] referenced type is reference-related to E,
1894 : the object or reference is initialized from that element...
1895 :
1896 : ??? With P0388R4, we should bind 't' directly to U{}:
1897 : using U = A[2];
1898 : A (&&t)[] = {U{}};
1899 : because A[] and A[2] are reference-related. But we don't do it
1900 : because grok_reference_init has deduced the array size (to 1), and
1901 : A[1] and A[2] aren't reference-related. */
1902 104352 : if (CONSTRUCTOR_NELTS (expr) == 1
1903 59570 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
1904 : {
1905 4342 : tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1906 4342 : if (error_operand_p (elt))
1907 : return NULL;
1908 4337 : tree etype = TREE_TYPE (elt);
1909 4337 : if (reference_related_p (to, etype))
1910 : {
1911 739 : expr = elt;
1912 739 : from = etype;
1913 739 : goto skip;
1914 : }
1915 3598 : else if (CLASS_TYPE_P (etype) && TYPE_HAS_CONVERSION (etype))
1916 : /* CWG1996: jason's proposed drafting adds "or initializing T from E
1917 : would bind directly". We check that in the direct binding with
1918 : conversion code below. */
1919 : single_list_conv = true;
1920 : }
1921 : /* Otherwise, if T is a reference type, a prvalue temporary of the type
1922 : referenced by T is copy-list-initialized, and the reference is bound
1923 : to that temporary. */
1924 : copy_list_init = true;
1925 247376223 : skip:;
1926 : }
1927 :
1928 247376223 : if (TYPE_REF_P (from))
1929 : {
1930 63962 : from = TREE_TYPE (from);
1931 63962 : if (!TYPE_REF_IS_RVALUE (rfrom)
1932 63962 : || TREE_CODE (from) == FUNCTION_TYPE)
1933 : gl_kind = clk_ordinary;
1934 : else
1935 247376223 : gl_kind = clk_rvalueref;
1936 : }
1937 247312261 : else if (expr)
1938 243835526 : gl_kind = lvalue_kind (expr);
1939 3190953 : else if (CLASS_TYPE_P (from)
1940 3476735 : || TREE_CODE (from) == ARRAY_TYPE)
1941 : gl_kind = clk_class;
1942 : else
1943 : gl_kind = clk_none;
1944 :
1945 : /* Don't allow a class prvalue when LOOKUP_NO_TEMP_BIND. */
1946 247376223 : if ((flags & LOOKUP_NO_TEMP_BIND)
1947 3590701 : && (gl_kind & clk_class))
1948 : gl_kind = clk_none;
1949 :
1950 : /* Same mask as real_lvalue_p. */
1951 244626674 : is_lvalue = gl_kind && !(gl_kind & (clk_rvalueref|clk_class));
1952 :
1953 211566375 : tfrom = from;
1954 211566375 : if ((gl_kind & clk_bitfield) != 0)
1955 2029569 : tfrom = unlowered_expr_type (expr);
1956 :
1957 : /* Figure out whether or not the types are reference-related and
1958 : reference compatible. We have to do this after stripping
1959 : references from FROM. */
1960 247376223 : related_p = reference_related_p (to, tfrom);
1961 : /* If this is a C cast, first convert to an appropriately qualified
1962 : type, so that we can later do a const_cast to the desired type. */
1963 247376223 : if (related_p && c_cast_p
1964 247376223 : && !at_least_as_qualified_p (to, tfrom))
1965 196 : to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1966 247376223 : compatible_p = reference_compatible_p (to, tfrom);
1967 :
1968 : /* Directly bind reference when target expression's type is compatible with
1969 : the reference and expression is an lvalue. In DR391, the wording in
1970 : [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1971 : const and rvalue references to rvalues of compatible class type.
1972 : We should also do direct bindings for non-class xvalues. */
1973 247376223 : if ((related_p || compatible_p) && gl_kind)
1974 : {
1975 : /* [dcl.init.ref]
1976 :
1977 : If the initializer expression
1978 :
1979 : -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1980 : is reference-compatible with "cv2 T2,"
1981 :
1982 : the reference is bound directly to the initializer expression
1983 : lvalue.
1984 :
1985 : [...]
1986 : If the initializer expression is an rvalue, with T2 a class type,
1987 : and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1988 : is bound to the object represented by the rvalue or to a sub-object
1989 : within that object. */
1990 :
1991 112213926 : conv = build_identity_conv (tfrom, expr);
1992 112213926 : conv = direct_reference_binding (rto, conv);
1993 :
1994 112213926 : if (TYPE_REF_P (rfrom))
1995 : /* Handle rvalue reference to function properly. */
1996 16409 : conv->rvaluedness_matches_p
1997 16409 : = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom));
1998 : else
1999 112197517 : conv->rvaluedness_matches_p
2000 112197517 : = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
2001 :
2002 112213926 : if ((gl_kind & clk_bitfield) != 0
2003 112213926 : || ((gl_kind & clk_packed) != 0 && !TYPE_PACKED (to)))
2004 : /* For the purposes of overload resolution, we ignore the fact
2005 : this expression is a bitfield or packed field. (In particular,
2006 : [over.ics.ref] says specifically that a function with a
2007 : non-const reference parameter is viable even if the
2008 : argument is a bitfield.)
2009 :
2010 : However, when we actually call the function we must create
2011 : a temporary to which to bind the reference. If the
2012 : reference is volatile, or isn't const, then we cannot make
2013 : a temporary, so we just issue an error when the conversion
2014 : actually occurs. */
2015 110 : conv->need_temporary_p = true;
2016 :
2017 : /* Don't allow binding of lvalues (other than function lvalues) to
2018 : rvalue references. */
2019 82375858 : if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
2020 122362406 : && TREE_CODE (to) != FUNCTION_TYPE)
2021 10146494 : conv->bad_p = true;
2022 :
2023 : /* Nor the reverse. */
2024 29838068 : if (!is_lvalue && !TYPE_REF_IS_RVALUE (rto)
2025 : /* Unless it's really a C++20 lvalue being treated as an xvalue.
2026 : But in C++23, such an expression is just an xvalue, not a special
2027 : lvalue, so the binding is once again ill-formed. */
2028 18476341 : && !(cxx_dialect <= cxx20
2029 14743315 : && (gl_kind & clk_implicit_rval))
2030 17376244 : && (!CP_TYPE_CONST_NON_VOLATILE_P (to)
2031 17258946 : || (flags & LOOKUP_NO_RVAL_BIND))
2032 112331224 : && TREE_CODE (to) != FUNCTION_TYPE)
2033 117298 : conv->bad_p = true;
2034 :
2035 112213926 : if (!compatible_p)
2036 6623025 : conv->bad_p = true;
2037 :
2038 : return conv;
2039 : }
2040 : /* [class.conv.fct] A conversion function is never used to convert a
2041 : (possibly cv-qualified) object to the (possibly cv-qualified) same
2042 : object type (or a reference to it), to a (possibly cv-qualified) base
2043 : class of that type (or a reference to it).... */
2044 4511477 : else if (!related_p
2045 130650820 : && !(flags & LOOKUP_NO_CONVERSION)
2046 48538094 : && (CLASS_TYPE_P (from) || single_list_conv))
2047 : {
2048 17816637 : tree rexpr = expr;
2049 17816637 : if (single_list_conv)
2050 144 : rexpr = CONSTRUCTOR_ELT (expr, 0)->value;
2051 :
2052 : /* [dcl.init.ref]
2053 :
2054 : If the initializer expression
2055 :
2056 : -- has a class type (i.e., T2 is a class type) can be
2057 : implicitly converted to an lvalue of type "cv3 T3," where
2058 : "cv1 T1" is reference-compatible with "cv3 T3". (this
2059 : conversion is selected by enumerating the applicable
2060 : conversion functions (_over.match.ref_) and choosing the
2061 : best one through overload resolution. (_over.match_).
2062 :
2063 : the reference is bound to the lvalue result of the conversion
2064 : in the second case. */
2065 17816637 : z_candidate *cand = build_user_type_conversion_1 (rto, rexpr, flags,
2066 : complain);
2067 17816637 : if (cand)
2068 : {
2069 12668 : if (!cand->second_conv->bad_p)
2070 : return cand->second_conv;
2071 :
2072 : /* Direct reference binding wasn't successful and yielded a bad
2073 : conversion. Proceed with trying to go through a temporary
2074 : instead, and if that also fails then we'll return this bad
2075 : conversion rather than no conversion for sake of better
2076 : diagnostics. */
2077 : bad_direct_conv = cand->second_conv;
2078 : }
2079 : }
2080 :
2081 : /* From this point on, we conceptually need temporaries, even if we
2082 : elide them. Only the cases above are "direct bindings". */
2083 135150166 : if (flags & LOOKUP_NO_TEMP_BIND)
2084 : return bad_direct_conv ? bad_direct_conv : nullptr;
2085 :
2086 : /* [over.ics.rank]
2087 :
2088 : When a parameter of reference type is not bound directly to an
2089 : argument expression, the conversion sequence is the one required
2090 : to convert the argument expression to the underlying type of the
2091 : reference according to _over.best.ics_. Conceptually, this
2092 : conversion sequence corresponds to copy-initializing a temporary
2093 : of the underlying type with the argument expression. Any
2094 : difference in top-level cv-qualification is subsumed by the
2095 : initialization itself and does not constitute a conversion. */
2096 :
2097 131793864 : bool maybe_valid_p = true;
2098 :
2099 : /* [dcl.init.ref]
2100 :
2101 : Otherwise, the reference shall be an lvalue reference to a
2102 : non-volatile const type, or the reference shall be an rvalue
2103 : reference. */
2104 174510512 : if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto))
2105 : maybe_valid_p = false;
2106 :
2107 : /* [dcl.init.ref]
2108 :
2109 : Otherwise, a temporary of type "cv1 T1" is created and
2110 : initialized from the initializer expression using the rules for a
2111 : non-reference copy initialization. If T1 is reference-related to
2112 : T2, cv1 must be the same cv-qualification as, or greater
2113 : cv-qualification than, cv2; otherwise, the program is ill-formed. */
2114 131793864 : if (related_p && !at_least_as_qualified_p (to, from))
2115 : maybe_valid_p = false;
2116 :
2117 : /* We try below to treat an invalid reference binding as a bad conversion
2118 : to improve diagnostics, but doing so may cause otherwise unnecessary
2119 : instantiations that can lead to a hard error. So during the first pass
2120 : of overload resolution wherein we shortcut bad conversions, instead just
2121 : produce a special conversion indicating a second pass is necessary if
2122 : there's no strictly viable candidate. */
2123 131793864 : if (!maybe_valid_p && (flags & LOOKUP_SHORTCUT_BAD_CONVS))
2124 : {
2125 7725425 : if (bad_direct_conv)
2126 : return bad_direct_conv;
2127 :
2128 7724958 : conv = alloc_conversion (ck_deferred_bad);
2129 7724958 : conv->bad_p = true;
2130 7724958 : return conv;
2131 : }
2132 :
2133 : /* We're generating a temporary now, but don't bind any more in the
2134 : conversion (specifically, don't slice the temporary returned by a
2135 : conversion operator). */
2136 124068439 : flags |= LOOKUP_NO_TEMP_BIND;
2137 :
2138 : /* Core issue 899: When [copy-]initializing a temporary to be bound
2139 : to the first parameter of a copy constructor (12.8) called with
2140 : a single argument in the context of direct-initialization,
2141 : explicit conversion functions are also considered.
2142 :
2143 : So don't set LOOKUP_ONLYCONVERTING in that case. */
2144 124068439 : if (!(flags & LOOKUP_COPY_PARM))
2145 104783444 : flags |= LOOKUP_ONLYCONVERTING;
2146 :
2147 124068439 : if (!conv)
2148 124068439 : conv = implicit_conversion (to, from, expr, c_cast_p,
2149 : flags, complain);
2150 124068439 : if (!conv)
2151 : return bad_direct_conv ? bad_direct_conv : nullptr;
2152 :
2153 11957776 : if (conv->user_conv_p)
2154 : {
2155 8048065 : if (copy_list_init)
2156 : /* Remember this was copy-list-initialization. */
2157 73687 : conv->need_temporary_p = true;
2158 :
2159 : /* If initializing the temporary used a conversion function,
2160 : recalculate the second conversion sequence. */
2161 23202887 : for (conversion *t = conv; t; t = next_conversion (t))
2162 15611912 : if (t->kind == ck_user
2163 8018241 : && c_cast_p && !maybe_valid_p)
2164 : {
2165 12 : if (complain & tf_warning)
2166 12 : warning (OPT_Wcast_user_defined,
2167 : "casting %qT to %qT does not use %qD",
2168 12 : from, rto, t->cand->fn);
2169 : /* Don't let recalculation try to make this valid. */
2170 : break;
2171 : }
2172 15611900 : else if (t->kind == ck_user
2173 15611900 : && DECL_CONV_FN_P (t->cand->fn))
2174 : {
2175 457078 : tree ftype = TREE_TYPE (TREE_TYPE (t->cand->fn));
2176 : /* A prvalue of non-class type is cv-unqualified. */
2177 457078 : if (!TYPE_REF_P (ftype) && !CLASS_TYPE_P (ftype))
2178 4174 : ftype = cv_unqualified (ftype);
2179 457078 : int sflags = (flags|LOOKUP_NO_CONVERSION)&~LOOKUP_NO_TEMP_BIND;
2180 457078 : conversion *new_second
2181 457078 : = reference_binding (rto, ftype, NULL_TREE, c_cast_p,
2182 : sflags, complain);
2183 457078 : if (!new_second)
2184 : return bad_direct_conv ? bad_direct_conv : nullptr;
2185 457078 : conv = merge_conversion_sequences (t, new_second);
2186 457078 : gcc_assert (maybe_valid_p || conv->bad_p);
2187 : return conv;
2188 : }
2189 : }
2190 :
2191 11500698 : conv = build_conv (ck_ref_bind, rto, conv);
2192 : /* This reference binding, unlike those above, requires the
2193 : creation of a temporary. */
2194 11500698 : conv->need_temporary_p = true;
2195 11500698 : conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
2196 11500698 : conv->bad_p |= !maybe_valid_p;
2197 :
2198 11500698 : return conv;
2199 : }
2200 :
2201 : /* Returns the implicit conversion sequence (see [over.ics]) from type
2202 : FROM to type TO. The optional expression EXPR may affect the
2203 : conversion. FLAGS are the usual overloading flags. If C_CAST_P is
2204 : true, this conversion is coming from a C-style cast. */
2205 :
2206 : static conversion *
2207 1539994186 : implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
2208 : int flags, tsubst_flags_t complain)
2209 : {
2210 1539994271 : conversion *conv;
2211 :
2212 1539994271 : if (from == error_mark_node || to == error_mark_node
2213 1539992962 : || expr == error_mark_node)
2214 : return NULL;
2215 :
2216 : /* Other flags only apply to the primary function in overload
2217 : resolution, or after we've chosen one. */
2218 1539992962 : flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM
2219 : |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_NO_NARROWING
2220 : |LOOKUP_PROTECT|LOOKUP_NO_NON_INTEGRAL|LOOKUP_SHORTCUT_BAD_CONVS);
2221 :
2222 : /* FIXME: actually we don't want warnings either, but we can't just
2223 : have 'complain &= ~(tf_warning|tf_error)' because it would cause
2224 : the regression of, eg, g++.old-deja/g++.benjamin/16077.C.
2225 : We really ought not to issue that warning until we've committed
2226 : to that conversion. */
2227 1539992962 : complain &= ~tf_error;
2228 :
2229 : /* Call reshape_init early to remove redundant braces. */
2230 1539992962 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr) && CLASS_TYPE_P (to))
2231 : {
2232 2411148 : to = complete_type (to);
2233 2411148 : if (!COMPLETE_TYPE_P (to))
2234 : return nullptr;
2235 2411124 : if (!CLASSTYPE_NON_AGGREGATE (to))
2236 : {
2237 1367527 : expr = reshape_init (to, expr, complain);
2238 1367527 : if (expr == error_mark_node)
2239 : return nullptr;
2240 1367284 : from = TREE_TYPE (expr);
2241 : }
2242 : }
2243 :
2244 : /* An argument should have gone through convert_from_reference. */
2245 1539992695 : gcc_checking_assert (!expr || !TYPE_REF_P (from));
2246 :
2247 1539992695 : if (TYPE_REF_P (to))
2248 240416055 : conv = reference_binding (to, from, expr, c_cast_p, flags, complain);
2249 : else
2250 1299576640 : conv = standard_conversion (to, from, expr, c_cast_p, flags, complain);
2251 :
2252 1539992695 : if (conv)
2253 : return conv;
2254 :
2255 301110896 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
2256 : {
2257 4932319 : if (is_std_init_list (to) && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2258 97460 : return build_list_conv (to, expr, flags, complain);
2259 :
2260 : /* As an extension, allow list-initialization of _Complex. */
2261 4737390 : if (TREE_CODE (to) == COMPLEX_TYPE
2262 4794571 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2263 : {
2264 57172 : conv = build_complex_conv (to, expr, flags, complain);
2265 57172 : if (conv)
2266 : return conv;
2267 : }
2268 :
2269 : /* Allow conversion from an initializer-list with one element to a
2270 : scalar type. */
2271 4680258 : if (SCALAR_TYPE_P (to))
2272 : {
2273 2340966 : int nelts = CONSTRUCTOR_NELTS (expr);
2274 362808 : tree elt;
2275 :
2276 362808 : if (nelts == 0)
2277 1978158 : elt = build_value_init (to, tf_none);
2278 362808 : else if (nelts == 1 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2279 361934 : elt = CONSTRUCTOR_ELT (expr, 0)->value;
2280 : else
2281 874 : elt = error_mark_node;
2282 :
2283 2340966 : conv = implicit_conversion (to, TREE_TYPE (elt), elt,
2284 : c_cast_p, flags, complain);
2285 2340966 : if (conv)
2286 : {
2287 2338974 : conv->check_narrowing = true;
2288 2338974 : if (BRACE_ENCLOSED_INITIALIZER_P (elt))
2289 : /* Too many levels of braces, i.e. '{{1}}'. */
2290 16 : conv->bad_p = true;
2291 : return conv;
2292 : }
2293 : }
2294 2339292 : else if (TREE_CODE (to) == ARRAY_TYPE)
2295 1889 : return build_array_conv (to, expr, flags, complain);
2296 : }
2297 :
2298 298615441 : if (expr != NULL_TREE
2299 291497178 : && (MAYBE_CLASS_TYPE_P (from)
2300 155176604 : || MAYBE_CLASS_TYPE_P (to))
2301 510182481 : && (flags & LOOKUP_NO_CONVERSION) == 0)
2302 : {
2303 79542199 : struct z_candidate *cand;
2304 :
2305 54318984 : if (CLASS_TYPE_P (to)
2306 54318934 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
2307 81855246 : && !CLASSTYPE_NON_AGGREGATE (complete_type (to)))
2308 1367225 : return build_aggr_conv (to, expr, flags, complain);
2309 :
2310 78174974 : cand = build_user_type_conversion_1 (to, expr, flags, complain);
2311 78174974 : if (cand)
2312 : {
2313 931591 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
2314 921846 : && CONSTRUCTOR_NELTS (expr) == 1
2315 58986 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr)
2316 14987312 : && !is_list_ctor (cand->fn))
2317 : {
2318 : /* "If C is not an initializer-list constructor and the
2319 : initializer list has a single element of type cv U, where U is
2320 : X or a class derived from X, the implicit conversion sequence
2321 : has Exact Match rank if U is X, or Conversion rank if U is
2322 : derived from X." */
2323 58586 : tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
2324 58586 : tree elttype = TREE_TYPE (elt);
2325 58586 : if (reference_related_p (to, elttype))
2326 : return implicit_conversion (to, elttype, elt,
2327 : c_cast_p, flags, complain);
2328 : }
2329 14928241 : conv = cand->second_conv;
2330 : }
2331 :
2332 : /* We used to try to bind a reference to a temporary here, but that
2333 : is now handled after the recursive call to this function at the end
2334 : of reference_binding. */
2335 : return conv;
2336 : }
2337 :
2338 : return NULL;
2339 : }
2340 :
2341 : /* Like implicit_conversion, but return NULL if the conversion is bad.
2342 :
2343 : This is not static so that check_non_deducible_conversion can call it within
2344 : add_template_candidate_real as part of overload resolution; it should not be
2345 : called outside of overload resolution. */
2346 :
2347 : conversion *
2348 7003456 : good_conversion (tree to, tree from, tree expr,
2349 : int flags, tsubst_flags_t complain)
2350 : {
2351 7003456 : conversion *c = implicit_conversion (to, from, expr, /*cast*/false,
2352 : flags, complain);
2353 7003456 : if (c && c->bad_p)
2354 2881285 : c = NULL;
2355 7003456 : return c;
2356 : }
2357 :
2358 : /* Add a new entry to the list of candidates. Used by the add_*_candidate
2359 : functions. ARGS will not be changed until a single candidate is
2360 : selected. */
2361 :
2362 : static struct z_candidate *
2363 1071767016 : add_candidate (struct z_candidate **candidates,
2364 : tree fn, tree first_arg, const vec<tree, va_gc> *args,
2365 : size_t num_convs, conversion **convs,
2366 : tree access_path, tree conversion_path,
2367 : int viable, struct rejection_reason *reason,
2368 : int flags)
2369 : {
2370 1071767016 : struct z_candidate *cand = (struct z_candidate *)
2371 1071767016 : conversion_obstack_alloc (sizeof (struct z_candidate));
2372 :
2373 1071767016 : cand->fn = fn;
2374 1071767016 : cand->first_arg = first_arg;
2375 1071767016 : cand->args = args;
2376 1071767016 : cand->convs = convs;
2377 1071767016 : cand->num_convs = num_convs;
2378 1071767016 : cand->access_path = access_path;
2379 1071767016 : cand->conversion_path = conversion_path;
2380 1071767016 : cand->viable = viable;
2381 1071767016 : cand->reason = reason;
2382 1071767016 : cand->next = *candidates;
2383 1071767016 : cand->flags = flags;
2384 1071767016 : *candidates = cand;
2385 :
2386 1071767016 : if (convs && cand->reversed ())
2387 : /* Swap the conversions for comparison in joust; we'll swap them back
2388 : before build_over_call. */
2389 86025435 : std::swap (convs[0], convs[1]);
2390 :
2391 1071767016 : return cand;
2392 : }
2393 :
2394 : /* FN is a function from the overload set that we outright didn't even
2395 : consider (for some reason); add it to the list as an non-viable "ignored"
2396 : candidate. */
2397 :
2398 : static z_candidate *
2399 677883190 : add_ignored_candidate (z_candidate **candidates, tree fn)
2400 : {
2401 : /* No need to dynamically allocate these. */
2402 677883190 : static const rejection_reason reason_ignored = { rr_ignored, {} };
2403 :
2404 677883190 : struct z_candidate *cand = (struct z_candidate *)
2405 677877733 : conversion_obstack_alloc (sizeof (struct z_candidate));
2406 :
2407 677883190 : cand->fn = fn;
2408 677883190 : cand->reason = const_cast<rejection_reason *> (&reason_ignored);
2409 677883190 : cand->next = *candidates;
2410 677883190 : *candidates = cand;
2411 :
2412 677883190 : return cand;
2413 : }
2414 :
2415 : /* True iff CAND is a candidate added by add_ignored_candidate. */
2416 :
2417 : static bool
2418 600667764 : ignored_candidate_p (const z_candidate *cand)
2419 : {
2420 600661657 : return cand->reason && cand->reason->code == rr_ignored;
2421 : }
2422 :
2423 : /* Return the number of remaining arguments in the parameter list
2424 : beginning with ARG. */
2425 :
2426 : int
2427 156431294 : remaining_arguments (tree arg)
2428 : {
2429 156431294 : int n;
2430 :
2431 272599306 : for (n = 0; arg != NULL_TREE && arg != void_list_node;
2432 116168012 : arg = TREE_CHAIN (arg))
2433 116168012 : n++;
2434 :
2435 156431294 : return n;
2436 : }
2437 :
2438 : /* [over.match.copy]: When initializing a temporary object (12.2) to be bound
2439 : to the first parameter of a constructor where the parameter is of type
2440 : "reference to possibly cv-qualified T" and the constructor is called with a
2441 : single argument in the context of direct-initialization of an object of type
2442 : "cv2 T", explicit conversion functions are also considered.
2443 :
2444 : So set LOOKUP_COPY_PARM to let reference_binding know that
2445 : it's being called in that context. */
2446 :
2447 : int
2448 450239354 : conv_flags (int i, int nargs, tree fn, tree arg, int flags)
2449 : {
2450 450239354 : int lflags = flags;
2451 450239354 : tree t;
2452 467591980 : if (i == 0 && nargs == 1 && DECL_CONSTRUCTOR_P (fn)
2453 148946814 : && (t = FUNCTION_FIRST_USER_PARMTYPE (fn))
2454 599186168 : && (same_type_ignoring_top_level_qualifiers_p
2455 148946814 : (non_reference (TREE_VALUE (t)), DECL_CONTEXT (fn))))
2456 : {
2457 115695567 : if (!(flags & LOOKUP_ONLYCONVERTING))
2458 35208088 : lflags |= LOOKUP_COPY_PARM;
2459 115695567 : if ((flags & LOOKUP_LIST_INIT_CTOR)
2460 115695567 : && BRACE_ENCLOSED_INITIALIZER_P (arg))
2461 1835 : lflags |= LOOKUP_NO_CONVERSION;
2462 : }
2463 : else
2464 334543787 : lflags |= LOOKUP_ONLYCONVERTING;
2465 :
2466 450239354 : return lflags;
2467 : }
2468 :
2469 : /* Build an appropriate 'this' conversion for the method FN and class
2470 : type CTYPE from the value ARG (having type ARGTYPE) to the type PARMTYPE.
2471 : This function modifies PARMTYPE, ARGTYPE and ARG. */
2472 :
2473 : static conversion *
2474 95446648 : build_this_conversion (tree fn, tree ctype,
2475 : tree& parmtype, tree& argtype, tree& arg,
2476 : int flags, tsubst_flags_t complain)
2477 : {
2478 190893296 : gcc_assert (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
2479 : && !DECL_CONSTRUCTOR_P (fn));
2480 :
2481 : /* The type of the implicit object parameter ('this') for
2482 : overload resolution is not always the same as for the
2483 : function itself; conversion functions are considered to
2484 : be members of the class being converted, and functions
2485 : introduced by a using-declaration are considered to be
2486 : members of the class that uses them.
2487 :
2488 : Since build_over_call ignores the ICS for the `this'
2489 : parameter, we can just change the parm type. */
2490 95446648 : parmtype = cp_build_qualified_type (ctype,
2491 95446648 : cp_type_quals (TREE_TYPE (parmtype)));
2492 95446648 : bool this_p = true;
2493 95446648 : if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn)))
2494 : {
2495 : /* If the function has a ref-qualifier, the implicit
2496 : object parameter has reference type. */
2497 167640 : bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn));
2498 167640 : parmtype = cp_build_reference_type (parmtype, rv);
2499 : /* The special handling of 'this' conversions in compare_ics
2500 : does not apply if there is a ref-qualifier. */
2501 167640 : this_p = false;
2502 : }
2503 : else
2504 : {
2505 95279008 : parmtype = build_pointer_type (parmtype);
2506 : /* We don't use build_this here because we don't want to
2507 : capture the object argument until we've chosen a
2508 : non-static member function. */
2509 95279008 : arg = build_address (arg);
2510 95279008 : argtype = lvalue_type (arg);
2511 : }
2512 95446648 : flags |= LOOKUP_ONLYCONVERTING;
2513 95446648 : conversion *t = implicit_conversion (parmtype, argtype, arg,
2514 : /*c_cast_p=*/false, flags, complain);
2515 95446648 : t->this_p = this_p;
2516 95446648 : return t;
2517 : }
2518 :
2519 : /* Create an overload candidate for the function or method FN called
2520 : with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
2521 : FLAGS is passed on to implicit_conversion.
2522 :
2523 : This does not change ARGS.
2524 :
2525 : CTYPE, if non-NULL, is the type we want to pretend this function
2526 : comes from for purposes of overload resolution.
2527 :
2528 : SHORTCUT_BAD_CONVS controls how we handle "bad" argument conversions.
2529 : If true, we stop computing conversions upon seeing the first bad
2530 : conversion. This is used by add_candidates to avoid computing
2531 : more conversions than necessary in the presence of a strictly viable
2532 : candidate, while preserving the defacto behavior of overload resolution
2533 : when it turns out there are only non-strictly viable candidates. */
2534 :
2535 : static struct z_candidate *
2536 638146172 : add_function_candidate (struct z_candidate **candidates,
2537 : tree fn, tree ctype, tree first_arg,
2538 : const vec<tree, va_gc> *args, tree access_path,
2539 : tree conversion_path, int flags,
2540 : conversion **convs,
2541 : bool shortcut_bad_convs,
2542 : tsubst_flags_t complain)
2543 : {
2544 638146172 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
2545 638146172 : int i, len;
2546 638146172 : tree parmnode;
2547 638146172 : tree orig_first_arg = first_arg;
2548 638146172 : int skip;
2549 638146172 : int viable = 1;
2550 638146172 : struct rejection_reason *reason = NULL;
2551 :
2552 : /* The `this', `in_chrg' and VTT arguments to constructors are not
2553 : considered in overload resolution. */
2554 1276292344 : if (DECL_CONSTRUCTOR_P (fn))
2555 : {
2556 289811463 : if (ctor_omit_inherited_parms (fn))
2557 : /* Bring back parameters omitted from an inherited ctor. */
2558 150 : parmlist = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
2559 : else
2560 289811388 : parmlist = skip_artificial_parms_for (fn, parmlist);
2561 289811463 : skip = num_artificial_parms_for (fn);
2562 289811463 : if (skip > 0 && first_arg != NULL_TREE)
2563 : {
2564 289811463 : --skip;
2565 289811463 : first_arg = NULL_TREE;
2566 : }
2567 : }
2568 : else
2569 : skip = 0;
2570 :
2571 1234983838 : len = vec_safe_length (args) - skip + (first_arg != NULL_TREE ? 1 : 0);
2572 638146172 : if (!convs)
2573 548202806 : convs = alloc_conversions (len);
2574 :
2575 : /* 13.3.2 - Viable functions [over.match.viable]
2576 : First, to be a viable function, a candidate function shall have enough
2577 : parameters to agree in number with the arguments in the list.
2578 :
2579 : We need to check this first; otherwise, checking the ICSes might cause
2580 : us to produce an ill-formed template instantiation. */
2581 :
2582 638146172 : parmnode = parmlist;
2583 1304771637 : for (i = 0; i < len; ++i)
2584 : {
2585 743815481 : if (parmnode == NULL_TREE || parmnode == void_list_node)
2586 : break;
2587 666625465 : parmnode = TREE_CHAIN (parmnode);
2588 : }
2589 :
2590 638146172 : if ((i < len && parmnode)
2591 638146172 : || !sufficient_parms_p (parmnode))
2592 : {
2593 156426449 : int remaining = remaining_arguments (parmnode);
2594 156426449 : viable = 0;
2595 156426449 : reason = arity_rejection (first_arg, i + remaining, len);
2596 : }
2597 :
2598 : /* An inherited constructor (12.6.3 [class.inhctor.init]) that has a first
2599 : parameter of type "reference to cv C" (including such a constructor
2600 : instantiated from a template) is excluded from the set of candidate
2601 : functions when used to construct an object of type D with an argument list
2602 : containing a single argument if C is reference-related to D. */
2603 582750863 : if (viable && len == 1 && parmlist && DECL_CONSTRUCTOR_P (fn)
2604 151409871 : && flag_new_inheriting_ctors
2605 789547358 : && DECL_INHERITED_CTOR (fn))
2606 : {
2607 1065134 : tree ptype = non_reference (TREE_VALUE (parmlist));
2608 1065134 : tree dtype = DECL_CONTEXT (fn);
2609 2130268 : tree btype = DECL_INHERITED_CTOR_BASE (fn);
2610 1065134 : if (reference_related_p (ptype, dtype)
2611 1065134 : && reference_related_p (btype, ptype))
2612 : {
2613 806653 : viable = false;
2614 806653 : reason = inherited_ctor_rejection ();
2615 : }
2616 : }
2617 :
2618 : /* Second, for a function to be viable, its constraints must be
2619 : satisfied. */
2620 638146172 : if (flag_concepts && viable && !constraints_satisfied_p (fn))
2621 : {
2622 198654 : reason = constraint_failure ();
2623 198654 : viable = false;
2624 : }
2625 :
2626 : /* When looking for a function from a subobject from an implicit
2627 : copy/move constructor/operator=, don't consider anything that takes (a
2628 : reference to) an unrelated type. See c++/44909 and core 1092. */
2629 638146172 : if (viable && parmlist && (flags & LOOKUP_DEFAULTED))
2630 : {
2631 79894372 : if (DECL_CONSTRUCTOR_P (fn))
2632 : i = 1;
2633 20800645 : else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2634 20800645 : && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
2635 : i = 2;
2636 : else
2637 : i = 0;
2638 39947186 : if (i && len == i)
2639 : {
2640 22043108 : parmnode = chain_index (i-1, parmlist);
2641 22043108 : if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
2642 : ctype))
2643 4464752 : viable = 0;
2644 : }
2645 :
2646 : /* This only applies at the top level. */
2647 35482434 : flags &= ~LOOKUP_DEFAULTED;
2648 : }
2649 :
2650 638146172 : if (! viable)
2651 161896508 : goto out;
2652 :
2653 476249664 : if (shortcut_bad_convs)
2654 476115694 : flags |= LOOKUP_SHORTCUT_BAD_CONVS;
2655 : else
2656 133970 : flags &= ~LOOKUP_SHORTCUT_BAD_CONVS;
2657 :
2658 : /* Third, for F to be a viable function, there shall exist for each
2659 : argument an implicit conversion sequence that converts that argument
2660 : to the corresponding parameter of F. */
2661 :
2662 476249664 : parmnode = parmlist;
2663 :
2664 820531509 : for (i = 0; i < len; ++i)
2665 : {
2666 521528771 : tree argtype, to_type;
2667 521528771 : tree arg;
2668 :
2669 521528771 : if (parmnode == void_list_node)
2670 : break;
2671 :
2672 521528771 : if (convs[i])
2673 : {
2674 : /* Already set during deduction. */
2675 6426819 : parmnode = TREE_CHAIN (parmnode);
2676 6426819 : continue;
2677 : }
2678 :
2679 515101952 : if (i == 0 && first_arg != NULL_TREE)
2680 91354874 : arg = first_arg;
2681 : else
2682 423747078 : arg = const_cast<tree> (
2683 811205028 : (*args)[i + skip - (first_arg != NULL_TREE ? 1 : 0)]);
2684 515101952 : argtype = lvalue_type (arg);
2685 :
2686 515101952 : conversion *t;
2687 515101952 : if (parmnode)
2688 : {
2689 509466970 : tree parmtype = TREE_VALUE (parmnode);
2690 509466970 : if (i == 0
2691 410815263 : && DECL_IOBJ_MEMBER_FUNCTION_P (fn)
2692 992055310 : && !DECL_CONSTRUCTOR_P (fn))
2693 91341351 : t = build_this_conversion (fn, ctype, parmtype, argtype, arg,
2694 : flags, complain);
2695 : else
2696 : {
2697 418125619 : int lflags = conv_flags (i, len-skip, fn, arg, flags);
2698 418125619 : t = implicit_conversion (parmtype, argtype, arg,
2699 : /*c_cast_p=*/false, lflags, complain);
2700 : }
2701 509466970 : to_type = parmtype;
2702 509466970 : parmnode = TREE_CHAIN (parmnode);
2703 : }
2704 : else
2705 : {
2706 5634982 : t = build_identity_conv (argtype, arg);
2707 5634982 : t->ellipsis_p = true;
2708 5634982 : to_type = argtype;
2709 : }
2710 :
2711 515101952 : convs[i] = t;
2712 515101952 : if (! t)
2713 : {
2714 154786061 : viable = 0;
2715 154786061 : reason = arg_conversion_rejection (first_arg, i, argtype, to_type,
2716 154786061 : EXPR_LOCATION (arg));
2717 154786061 : break;
2718 : }
2719 :
2720 360315891 : if (t->bad_p)
2721 : {
2722 22493592 : viable = -1;
2723 22493592 : reason = bad_arg_conversion_rejection (first_arg, i, arg, to_type,
2724 22493592 : EXPR_LOCATION (arg));
2725 22493592 : if (shortcut_bad_convs)
2726 : break;
2727 : }
2728 : }
2729 :
2730 299002738 : out:
2731 638146172 : return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
2732 638146172 : access_path, conversion_path, viable, reason, flags);
2733 : }
2734 :
2735 : /* Create an overload candidate for the conversion function FN which will
2736 : be invoked for expression OBJ, producing a pointer-to-function which
2737 : will in turn be called with the argument list FIRST_ARG/ARGLIST,
2738 : and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
2739 : passed on to implicit_conversion.
2740 :
2741 : Actually, we don't really care about FN; we care about the type it
2742 : converts to. There may be multiple conversion functions that will
2743 : convert to that type, and we rely on build_user_type_conversion_1 to
2744 : choose the best one; so when we create our candidate, we record the type
2745 : instead of the function. */
2746 :
2747 : static struct z_candidate *
2748 119203 : add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
2749 : const vec<tree, va_gc> *arglist,
2750 : tree access_path, tree conversion_path,
2751 : tsubst_flags_t complain)
2752 : {
2753 119203 : tree totype = TREE_TYPE (TREE_TYPE (fn));
2754 119203 : int i, len, viable, flags;
2755 119203 : tree parmlist, parmnode;
2756 119203 : conversion **convs;
2757 119203 : struct rejection_reason *reason;
2758 :
2759 238627 : for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
2760 119424 : parmlist = TREE_TYPE (parmlist);
2761 119203 : parmlist = TYPE_ARG_TYPES (parmlist);
2762 :
2763 119203 : len = vec_safe_length (arglist) + 1;
2764 119203 : convs = alloc_conversions (len);
2765 119203 : parmnode = parmlist;
2766 119203 : viable = 1;
2767 119203 : flags = LOOKUP_IMPLICIT;
2768 119203 : reason = NULL;
2769 :
2770 : /* Don't bother looking up the same type twice. */
2771 119203 : if (*candidates && (*candidates)->fn == totype)
2772 : return NULL;
2773 :
2774 119188 : if (!constraints_satisfied_p (fn))
2775 : {
2776 9 : reason = constraint_failure ();
2777 9 : viable = 0;
2778 9 : return add_candidate (candidates, fn, obj, arglist, len, convs,
2779 9 : access_path, conversion_path, viable, reason, flags);
2780 : }
2781 :
2782 379199 : for (i = 0; i < len; ++i)
2783 : {
2784 260320 : tree arg, argtype, convert_type = NULL_TREE;
2785 260320 : conversion *t;
2786 :
2787 260320 : if (i == 0)
2788 : arg = obj;
2789 : else
2790 141141 : arg = (*arglist)[i - 1];
2791 260320 : argtype = lvalue_type (arg);
2792 :
2793 260320 : if (i == 0)
2794 : {
2795 119179 : t = build_identity_conv (argtype, NULL_TREE);
2796 119179 : t = build_conv (ck_user, totype, t);
2797 : /* Leave the 'cand' field null; we'll figure out the conversion in
2798 : convert_like if this candidate is chosen. */
2799 119179 : convert_type = totype;
2800 : }
2801 141141 : else if (parmnode == void_list_node)
2802 : break;
2803 140914 : else if (parmnode)
2804 : {
2805 140905 : t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
2806 : /*c_cast_p=*/false, flags, complain);
2807 140905 : convert_type = TREE_VALUE (parmnode);
2808 : }
2809 : else
2810 : {
2811 9 : t = build_identity_conv (argtype, arg);
2812 9 : t->ellipsis_p = true;
2813 9 : convert_type = argtype;
2814 : }
2815 :
2816 260093 : convs[i] = t;
2817 260093 : if (! t)
2818 : break;
2819 :
2820 260020 : if (t->bad_p)
2821 : {
2822 27 : viable = -1;
2823 72 : reason = bad_arg_conversion_rejection (NULL_TREE, i, arg, convert_type,
2824 27 : EXPR_LOCATION (arg));
2825 : }
2826 :
2827 260020 : if (i == 0)
2828 119179 : continue;
2829 :
2830 140841 : if (parmnode)
2831 140832 : parmnode = TREE_CHAIN (parmnode);
2832 : }
2833 :
2834 119179 : if (i < len
2835 119179 : || ! sufficient_parms_p (parmnode))
2836 : {
2837 324 : int remaining = remaining_arguments (parmnode);
2838 324 : viable = 0;
2839 324 : reason = arity_rejection (NULL_TREE, i + remaining, len);
2840 : }
2841 :
2842 119179 : return add_candidate (candidates, totype, obj, arglist, len, convs,
2843 119179 : access_path, conversion_path, viable, reason, flags);
2844 : }
2845 :
2846 : static void
2847 9564860 : build_builtin_candidate (struct z_candidate **candidates, tree fnname,
2848 : tree type1, tree type2, const vec<tree,va_gc> &args,
2849 : tree *argtypes, int flags, tsubst_flags_t complain)
2850 : {
2851 9564860 : conversion *t;
2852 9564860 : conversion **convs;
2853 9564860 : size_t num_convs;
2854 9564860 : int viable = 1;
2855 9564860 : tree types[2];
2856 9564860 : struct rejection_reason *reason = NULL;
2857 :
2858 9564860 : types[0] = type1;
2859 9564860 : types[1] = type2;
2860 :
2861 9564860 : num_convs = args.length ();
2862 9564860 : convs = alloc_conversions (num_convs);
2863 :
2864 : /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2865 : conversion ops are allowed. We handle that here by just checking for
2866 : boolean_type_node because other operators don't ask for it. COND_EXPR
2867 : also does contextual conversion to bool for the first operand, but we
2868 : handle that in build_conditional_expr, and type1 here is operand 2. */
2869 9564860 : if (type1 != boolean_type_node)
2870 8995082 : flags |= LOOKUP_ONLYCONVERTING;
2871 :
2872 27954160 : for (unsigned i = 0; i < 2 && i < num_convs; ++i)
2873 : {
2874 18389300 : t = implicit_conversion (types[i], argtypes[i], args[i],
2875 : /*c_cast_p=*/false, flags, complain);
2876 18389300 : if (! t)
2877 : {
2878 1922267 : viable = 0;
2879 : /* We need something for printing the candidate. */
2880 1922267 : t = build_identity_conv (types[i], NULL_TREE);
2881 1922267 : reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2882 1922267 : types[i], EXPR_LOCATION (args[i]));
2883 : }
2884 16467033 : else if (t->bad_p)
2885 : {
2886 252 : viable = 0;
2887 252 : reason = bad_arg_conversion_rejection (NULL_TREE, i, args[i],
2888 : types[i],
2889 252 : EXPR_LOCATION (args[i]));
2890 : }
2891 18389300 : convs[i] = t;
2892 : }
2893 :
2894 : /* For COND_EXPR we rearranged the arguments; undo that now. */
2895 9564860 : if (num_convs == 3)
2896 : {
2897 159 : convs[2] = convs[1];
2898 159 : convs[1] = convs[0];
2899 159 : t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
2900 : /*c_cast_p=*/false, flags,
2901 : complain);
2902 159 : if (t)
2903 159 : convs[0] = t;
2904 : else
2905 : {
2906 0 : viable = 0;
2907 0 : reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2],
2908 : boolean_type_node,
2909 0 : EXPR_LOCATION (args[2]));
2910 : }
2911 : }
2912 :
2913 9564860 : add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
2914 : num_convs, convs,
2915 : /*access_path=*/NULL_TREE,
2916 : /*conversion_path=*/NULL_TREE,
2917 : viable, reason, flags);
2918 9564860 : }
2919 :
2920 : static bool
2921 12 : is_complete (tree t)
2922 : {
2923 12 : return COMPLETE_TYPE_P (complete_type (t));
2924 : }
2925 :
2926 : /* Returns nonzero if TYPE is a promoted arithmetic type. */
2927 :
2928 : static bool
2929 3139 : promoted_arithmetic_type_p (tree type)
2930 : {
2931 : /* [over.built]
2932 :
2933 : In this section, the term promoted integral type is used to refer
2934 : to those integral types which are preserved by integral promotion
2935 : (including e.g. int and long but excluding e.g. char).
2936 : Similarly, the term promoted arithmetic type refers to promoted
2937 : integral types plus floating types. */
2938 3139 : return ((CP_INTEGRAL_TYPE_P (type)
2939 239 : && same_type_p (type_promotes_to (type), type))
2940 3139 : || SCALAR_FLOAT_TYPE_P (type));
2941 : }
2942 :
2943 : /* Create any builtin operator overload candidates for the operator in
2944 : question given the converted operand types TYPE1 and TYPE2. The other
2945 : args are passed through from add_builtin_candidates to
2946 : build_builtin_candidate.
2947 :
2948 : TYPE1 and TYPE2 may not be permissible, and we must filter them.
2949 : If CODE is requires candidates operands of the same type of the kind
2950 : of which TYPE1 and TYPE2 are, we add both candidates
2951 : CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2952 :
2953 : static void
2954 14098472 : add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
2955 : enum tree_code code2, tree fnname, tree type1,
2956 : tree type2, vec<tree,va_gc> &args, tree *argtypes,
2957 : int flags, tsubst_flags_t complain)
2958 : {
2959 14098472 : switch (code)
2960 : {
2961 21 : case POSTINCREMENT_EXPR:
2962 21 : case POSTDECREMENT_EXPR:
2963 21 : args[1] = integer_zero_node;
2964 21 : type2 = integer_type_node;
2965 21 : break;
2966 : default:
2967 : break;
2968 : }
2969 :
2970 14098472 : switch (code)
2971 : {
2972 :
2973 : /* 4 For every pair (T, VQ), where T is an arithmetic type other than bool,
2974 : and VQ is either volatile or empty, there exist candidate operator
2975 : functions of the form
2976 : VQ T& operator++(VQ T&);
2977 : T operator++(VQ T&, int);
2978 : 5 For every pair (T, VQ), where T is an arithmetic type other than bool,
2979 : and VQ is either volatile or empty, there exist candidate operator
2980 : functions of the form
2981 : VQ T& operator--(VQ T&);
2982 : T operator--(VQ T&, int);
2983 : 6 For every pair (T, VQ), where T is a cv-qualified or cv-unqualified object
2984 : type, and VQ is either volatile or empty, there exist candidate operator
2985 : functions of the form
2986 : T*VQ& operator++(T*VQ&);
2987 : T*VQ& operator--(T*VQ&);
2988 : T* operator++(T*VQ&, int);
2989 : T* operator--(T*VQ&, int); */
2990 :
2991 18 : case POSTDECREMENT_EXPR:
2992 18 : case PREDECREMENT_EXPR:
2993 18 : if (TREE_CODE (type1) == BOOLEAN_TYPE)
2994 : return;
2995 : /* FALLTHRU */
2996 39 : case POSTINCREMENT_EXPR:
2997 39 : case PREINCREMENT_EXPR:
2998 : /* P0002R1, Remove deprecated operator++(bool) added "other than bool"
2999 : to p4. */
3000 39 : if (TREE_CODE (type1) == BOOLEAN_TYPE && cxx_dialect >= cxx17)
3001 : return;
3002 33 : if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
3003 : {
3004 24 : type1 = build_reference_type (type1);
3005 24 : break;
3006 : }
3007 : return;
3008 :
3009 : /* 7 For every cv-qualified or cv-unqualified object type T, there
3010 : exist candidate operator functions of the form
3011 :
3012 : T& operator*(T*);
3013 :
3014 :
3015 : 8 For every function type T that does not have cv-qualifiers or
3016 : a ref-qualifier, there exist candidate operator functions of the form
3017 : T& operator*(T*); */
3018 :
3019 112997 : case INDIRECT_REF:
3020 112997 : if (TYPE_PTR_P (type1)
3021 112997 : && (TYPE_PTROB_P (type1)
3022 12 : || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
3023 : break;
3024 : return;
3025 :
3026 : /* 9 For every type T, there exist candidate operator functions of the form
3027 : T* operator+(T*);
3028 :
3029 : 10 For every floating-point or promoted integral type T, there exist
3030 : candidate operator functions of the form
3031 : T operator+(T);
3032 : T operator-(T); */
3033 :
3034 481 : case UNARY_PLUS_EXPR: /* unary + */
3035 481 : if (TYPE_PTR_P (type1))
3036 : break;
3037 : /* FALLTHRU */
3038 211237 : case NEGATE_EXPR:
3039 211237 : if (ARITHMETIC_TYPE_P (type1))
3040 : break;
3041 : return;
3042 :
3043 : /* 11 For every promoted integral type T, there exist candidate operator
3044 : functions of the form
3045 : T operator~(T); */
3046 :
3047 160441 : case BIT_NOT_EXPR:
3048 160441 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
3049 : break;
3050 : return;
3051 :
3052 : /* 12 For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, C1
3053 : is the same type as C2 or is a derived class of C2, and T is an object
3054 : type or a function type there exist candidate operator functions of the
3055 : form
3056 : CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
3057 : where CV12 is the union of CV1 and CV2. */
3058 :
3059 34 : case MEMBER_REF:
3060 34 : if (TYPE_PTR_P (type1) && TYPE_PTRMEM_P (type2))
3061 : {
3062 34 : tree c1 = TREE_TYPE (type1);
3063 34 : tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
3064 :
3065 31 : if (CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
3066 62 : && (TYPE_PTRMEMFUNC_P (type2)
3067 12 : || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
3068 : break;
3069 : }
3070 : return;
3071 :
3072 : /* 13 For every pair of types L and R, where each of L and R is a floating-point
3073 : or promoted integral type, there exist candidate operator functions of the
3074 : form
3075 : LR operator*(L, R);
3076 : LR operator/(L, R);
3077 : LR operator+(L, R);
3078 : LR operator-(L, R);
3079 : bool operator<(L, R);
3080 : bool operator>(L, R);
3081 : bool operator<=(L, R);
3082 : bool operator>=(L, R);
3083 : bool operator==(L, R);
3084 : bool operator!=(L, R);
3085 : where LR is the result of the usual arithmetic conversions between
3086 : types L and R.
3087 :
3088 : 14 For every integral type T there exists a candidate operator function of
3089 : the form
3090 :
3091 : std::strong_ordering operator<=>(T, T);
3092 :
3093 : 15 For every pair of floating-point types L and R, there exists a candidate
3094 : operator function of the form
3095 :
3096 : std::partial_ordering operator<=>(L, R);
3097 :
3098 : 16 For every cv-qualified or cv-unqualified object type T there exist
3099 : candidate operator functions of the form
3100 : T* operator+(T*, std::ptrdiff_t);
3101 : T& operator[](T*, std::ptrdiff_t);
3102 : T* operator-(T*, std::ptrdiff_t);
3103 : T* operator+(std::ptrdiff_t, T*);
3104 : T& operator[](std::ptrdiff_t, T*);
3105 :
3106 : 17 For every T, where T is a pointer to object type, there exist candidate
3107 : operator functions of the form
3108 : std::ptrdiff_t operator-(T, T);
3109 :
3110 : 18 For every T, where T is an enumeration type or a pointer type, there
3111 : exist candidate operator functions of the form
3112 : bool operator<(T, T);
3113 : bool operator>(T, T);
3114 : bool operator<=(T, T);
3115 : bool operator>=(T, T);
3116 : bool operator==(T, T);
3117 : bool operator!=(T, T);
3118 : R operator<=>(T, T);
3119 :
3120 : where R is the result type specified in [expr.spaceship].
3121 :
3122 : 19 For every T, where T is a pointer-to-member type or std::nullptr_t,
3123 : there exist candidate operator functions of the form
3124 : bool operator==(T, T);
3125 : bool operator!=(T, T); */
3126 :
3127 206425 : case MINUS_EXPR:
3128 206425 : if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
3129 : break;
3130 13 : if (TYPE_PTROB_P (type1)
3131 206411 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3132 : {
3133 4 : type2 = ptrdiff_type_node;
3134 4 : break;
3135 : }
3136 : /* FALLTHRU */
3137 897689 : case MULT_EXPR:
3138 897689 : case TRUNC_DIV_EXPR:
3139 897689 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3140 : break;
3141 : return;
3142 :
3143 : /* This isn't exactly what's specified above for operator<=>, but it's
3144 : close enough. In particular, we don't care about the return type
3145 : specified above; it doesn't participate in overload resolution and it
3146 : doesn't affect the semantics of the built-in operator. */
3147 6793183 : case SPACESHIP_EXPR:
3148 6793183 : case EQ_EXPR:
3149 6793183 : case NE_EXPR:
3150 221705 : if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
3151 7014888 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)))
3152 : break;
3153 6793149 : if (NULLPTR_TYPE_P (type1) && NULLPTR_TYPE_P (type2))
3154 : break;
3155 6793143 : if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (args[1]))
3156 : {
3157 : type2 = type1;
3158 : break;
3159 : }
3160 6793140 : if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (args[0]))
3161 : {
3162 : type1 = type2;
3163 : break;
3164 : }
3165 : /* Fall through. */
3166 7525111 : case LT_EXPR:
3167 7525111 : case GT_EXPR:
3168 7525111 : case LE_EXPR:
3169 7525111 : case GE_EXPR:
3170 7525111 : case MAX_EXPR:
3171 7525111 : case MIN_EXPR:
3172 7525111 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3173 : break;
3174 5810375 : if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3175 : break;
3176 5806662 : if (TREE_CODE (type1) == ENUMERAL_TYPE
3177 4158280 : && TREE_CODE (type2) == ENUMERAL_TYPE)
3178 : break;
3179 2805321 : if (TYPE_PTR_P (type1)
3180 2805321 : && null_ptr_cst_p (args[1]))
3181 : {
3182 : type2 = type1;
3183 : break;
3184 : }
3185 2805302 : if (null_ptr_cst_p (args[0])
3186 2805302 : && TYPE_PTR_P (type2))
3187 : {
3188 : type1 = type2;
3189 : break;
3190 : }
3191 : return;
3192 :
3193 247936 : case PLUS_EXPR:
3194 247936 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3195 : break;
3196 : /* FALLTHRU */
3197 182191 : case ARRAY_REF:
3198 182191 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
3199 : {
3200 6 : type1 = ptrdiff_type_node;
3201 6 : break;
3202 : }
3203 182185 : if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3204 : {
3205 37591 : type2 = ptrdiff_type_node;
3206 37591 : break;
3207 : }
3208 : return;
3209 :
3210 : /* 18For every pair of promoted integral types L and R, there exist candi-
3211 : date operator functions of the form
3212 : LR operator%(L, R);
3213 : LR operator&(L, R);
3214 : LR operator^(L, R);
3215 : LR operator|(L, R);
3216 : L operator<<(L, R);
3217 : L operator>>(L, R);
3218 : where LR is the result of the usual arithmetic conversions between
3219 : types L and R. */
3220 :
3221 3086722 : case TRUNC_MOD_EXPR:
3222 3086722 : case BIT_AND_EXPR:
3223 3086722 : case BIT_IOR_EXPR:
3224 3086722 : case BIT_XOR_EXPR:
3225 3086722 : case LSHIFT_EXPR:
3226 3086722 : case RSHIFT_EXPR:
3227 3086722 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3228 : break;
3229 : return;
3230 :
3231 : /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
3232 : type, VQ is either volatile or empty, and R is a promoted arithmetic
3233 : type, there exist candidate operator functions of the form
3234 : VQ L& operator=(VQ L&, R);
3235 : VQ L& operator*=(VQ L&, R);
3236 : VQ L& operator/=(VQ L&, R);
3237 : VQ L& operator+=(VQ L&, R);
3238 : VQ L& operator-=(VQ L&, R);
3239 :
3240 : 20For every pair T, VQ), where T is any type and VQ is either volatile
3241 : or empty, there exist candidate operator functions of the form
3242 : T*VQ& operator=(T*VQ&, T*);
3243 :
3244 : 21For every pair T, VQ), where T is a pointer to member type and VQ is
3245 : either volatile or empty, there exist candidate operator functions of
3246 : the form
3247 : VQ T& operator=(VQ T&, T);
3248 :
3249 : 22For every triple T, VQ, I), where T is a cv-qualified or cv-
3250 : unqualified complete object type, VQ is either volatile or empty, and
3251 : I is a promoted integral type, there exist candidate operator func-
3252 : tions of the form
3253 : T*VQ& operator+=(T*VQ&, I);
3254 : T*VQ& operator-=(T*VQ&, I);
3255 :
3256 : 23For every triple L, VQ, R), where L is an integral or enumeration
3257 : type, VQ is either volatile or empty, and R is a promoted integral
3258 : type, there exist candidate operator functions of the form
3259 :
3260 : VQ L& operator%=(VQ L&, R);
3261 : VQ L& operator<<=(VQ L&, R);
3262 : VQ L& operator>>=(VQ L&, R);
3263 : VQ L& operator&=(VQ L&, R);
3264 : VQ L& operator^=(VQ L&, R);
3265 : VQ L& operator|=(VQ L&, R); */
3266 :
3267 1800864 : case MODIFY_EXPR:
3268 1800864 : switch (code2)
3269 : {
3270 103608 : case PLUS_EXPR:
3271 103608 : case MINUS_EXPR:
3272 103608 : if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3273 : {
3274 0 : type2 = ptrdiff_type_node;
3275 0 : break;
3276 : }
3277 : /* FALLTHRU */
3278 103613 : case MULT_EXPR:
3279 103613 : case TRUNC_DIV_EXPR:
3280 103613 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3281 : break;
3282 : return;
3283 :
3284 1697251 : case TRUNC_MOD_EXPR:
3285 1697251 : case BIT_AND_EXPR:
3286 1697251 : case BIT_IOR_EXPR:
3287 1697251 : case BIT_XOR_EXPR:
3288 1697251 : case LSHIFT_EXPR:
3289 1697251 : case RSHIFT_EXPR:
3290 1697251 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3291 : break;
3292 : return;
3293 :
3294 0 : case NOP_EXPR:
3295 0 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3296 : break;
3297 0 : if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
3298 0 : || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3299 0 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
3300 0 : || ((TYPE_PTRMEMFUNC_P (type1)
3301 0 : || TYPE_PTR_P (type1))
3302 0 : && null_ptr_cst_p (args[1])))
3303 : {
3304 : type2 = type1;
3305 : break;
3306 : }
3307 : return;
3308 :
3309 0 : default:
3310 0 : gcc_unreachable ();
3311 : }
3312 1452900 : type1 = build_reference_type (type1);
3313 1452900 : break;
3314 :
3315 2925 : case COND_EXPR:
3316 : /* [over.built]
3317 :
3318 : For every pair of promoted arithmetic types L and R, there
3319 : exist candidate operator functions of the form
3320 :
3321 : LR operator?(bool, L, R);
3322 :
3323 : where LR is the result of the usual arithmetic conversions
3324 : between types L and R.
3325 :
3326 : For every type T, where T is a pointer or pointer-to-member
3327 : type, there exist candidate operator functions of the form T
3328 : operator?(bool, T, T); */
3329 :
3330 2925 : if (promoted_arithmetic_type_p (type1)
3331 2925 : && promoted_arithmetic_type_p (type2))
3332 : /* That's OK. */
3333 : break;
3334 :
3335 : /* Otherwise, the types should be pointers. */
3336 2900 : if (!((TYPE_PTR_OR_PTRMEM_P (type1) || null_ptr_cst_p (args[0]))
3337 369 : && (TYPE_PTR_OR_PTRMEM_P (type2) || null_ptr_cst_p (args[1]))))
3338 : return;
3339 :
3340 : /* We don't check that the two types are the same; the logic
3341 : below will actually create two candidates; one in which both
3342 : parameter types are TYPE1, and one in which both parameter
3343 : types are TYPE2. */
3344 : break;
3345 :
3346 3 : case REALPART_EXPR:
3347 3 : case IMAGPART_EXPR:
3348 3 : if (ARITHMETIC_TYPE_P (type1))
3349 : break;
3350 : return;
3351 :
3352 0 : default:
3353 0 : gcc_unreachable ();
3354 : }
3355 :
3356 : /* Make sure we don't create builtin candidates with dependent types. */
3357 8994957 : bool u1 = uses_template_parms (type1);
3358 8994957 : bool u2 = type2 ? uses_template_parms (type2) : false;
3359 8994944 : if (u1 || u2)
3360 : {
3361 : /* Try to recover if one of the types is non-dependent. But if
3362 : there's only one type, there's nothing we can do. */
3363 19 : if (!type2)
3364 : return;
3365 : /* And we lose if both are dependent. */
3366 16 : if (u1 && u2)
3367 : return;
3368 : /* Or if they have different forms. */
3369 9 : if (TREE_CODE (type1) != TREE_CODE (type2))
3370 : return;
3371 :
3372 9 : if (u1 && !u2)
3373 : type1 = type2;
3374 6 : else if (u2 && !u1)
3375 8994947 : type2 = type1;
3376 : }
3377 :
3378 : /* If we're dealing with two pointer types or two enumeral types,
3379 : we need candidates for both of them. */
3380 8755873 : if (type2 && !same_type_p (type1, type2)
3381 1940751 : && TREE_CODE (type1) == TREE_CODE (type2)
3382 9444908 : && (TYPE_REF_P (type1)
3383 449961 : || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3384 449845 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
3385 449827 : || TYPE_PTRMEMFUNC_P (type1)
3386 449827 : || MAYBE_CLASS_TYPE_P (type1)
3387 449827 : || TREE_CODE (type1) == ENUMERAL_TYPE))
3388 : {
3389 229 : if (TYPE_PTR_OR_PTRMEM_P (type1))
3390 : {
3391 134 : tree cptype = composite_pointer_type (input_location,
3392 : type1, type2,
3393 : error_mark_node,
3394 : error_mark_node,
3395 : CPO_CONVERSION,
3396 : tf_none);
3397 134 : if (cptype != error_mark_node)
3398 : {
3399 94 : build_builtin_candidate
3400 94 : (candidates, fnname, cptype, cptype, args, argtypes,
3401 : flags, complain);
3402 94 : return;
3403 : }
3404 : }
3405 :
3406 135 : build_builtin_candidate
3407 135 : (candidates, fnname, type1, type1, args, argtypes, flags, complain);
3408 135 : build_builtin_candidate
3409 135 : (candidates, fnname, type2, type2, args, argtypes, flags, complain);
3410 135 : return;
3411 : }
3412 :
3413 8994718 : build_builtin_candidate
3414 8994718 : (candidates, fnname, type1, type2, args, argtypes, flags, complain);
3415 : }
3416 :
3417 : tree
3418 857991041 : type_decays_to (tree type)
3419 : {
3420 857991041 : if (TREE_CODE (type) == ARRAY_TYPE)
3421 6870204 : return build_pointer_type (TREE_TYPE (type));
3422 851120837 : if (TREE_CODE (type) == FUNCTION_TYPE)
3423 37589 : return build_pointer_type (type);
3424 : return type;
3425 : }
3426 :
3427 : /* There are three conditions of builtin candidates:
3428 :
3429 : 1) bool-taking candidates. These are the same regardless of the input.
3430 : 2) pointer-pair taking candidates. These are generated for each type
3431 : one of the input types converts to.
3432 : 3) arithmetic candidates. According to the standard, we should generate
3433 : all of these, but I'm trying not to...
3434 :
3435 : Here we generate a superset of the possible candidates for this particular
3436 : case. That is a subset of the full set the standard defines, plus some
3437 : other cases which the standard disallows. add_builtin_candidate will
3438 : filter out the invalid set. */
3439 :
3440 : static void
3441 24517264 : add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
3442 : enum tree_code code2, tree fnname,
3443 : vec<tree, va_gc> *argv,
3444 : int flags, tsubst_flags_t complain)
3445 : {
3446 24517264 : int ref1;
3447 24517264 : int enum_p = 0;
3448 24517264 : tree type, argtypes[3], t;
3449 : /* TYPES[i] is the set of possible builtin-operator parameter types
3450 : we will consider for the Ith argument. */
3451 24517264 : vec<tree, va_gc> *types[2];
3452 24517264 : unsigned ix;
3453 24517264 : vec<tree, va_gc> &args = *argv;
3454 24517264 : unsigned len = args.length ();
3455 :
3456 68542243 : for (unsigned i = 0; i < len; ++i)
3457 : {
3458 44024979 : if (args[i])
3459 44024979 : argtypes[i] = unlowered_expr_type (args[i]);
3460 : else
3461 : argtypes[i] = NULL_TREE;
3462 : }
3463 :
3464 24517264 : switch (code)
3465 : {
3466 : /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
3467 : and VQ is either volatile or empty, there exist candidate operator
3468 : functions of the form
3469 : VQ T& operator++(VQ T&); */
3470 :
3471 : case POSTINCREMENT_EXPR:
3472 : case PREINCREMENT_EXPR:
3473 : case POSTDECREMENT_EXPR:
3474 : case PREDECREMENT_EXPR:
3475 : case MODIFY_EXPR:
3476 : ref1 = 1;
3477 : break;
3478 :
3479 : /* 24There also exist candidate operator functions of the form
3480 : bool operator!(bool);
3481 : bool operator&&(bool, bool);
3482 : bool operator||(bool, bool); */
3483 :
3484 501346 : case TRUTH_NOT_EXPR:
3485 501346 : build_builtin_candidate
3486 501346 : (candidates, fnname, boolean_type_node,
3487 : NULL_TREE, args, argtypes, flags, complain);
3488 13801132 : return;
3489 :
3490 68432 : case TRUTH_ORIF_EXPR:
3491 68432 : case TRUTH_ANDIF_EXPR:
3492 68432 : build_builtin_candidate
3493 68432 : (candidates, fnname, boolean_type_node,
3494 : boolean_type_node, args, argtypes, flags, complain);
3495 68432 : return;
3496 :
3497 : case ADDR_EXPR:
3498 : case COMPOUND_EXPR:
3499 : case COMPONENT_REF:
3500 : case CO_AWAIT_EXPR:
3501 : return;
3502 :
3503 6353528 : case COND_EXPR:
3504 6353528 : case EQ_EXPR:
3505 6353528 : case NE_EXPR:
3506 6353528 : case LT_EXPR:
3507 6353528 : case LE_EXPR:
3508 6353528 : case GT_EXPR:
3509 6353528 : case GE_EXPR:
3510 6353528 : case SPACESHIP_EXPR:
3511 6353528 : enum_p = 1;
3512 : /* Fall through. */
3513 :
3514 : default:
3515 : ref1 = 0;
3516 : }
3517 :
3518 21772901 : types[0] = make_tree_vector ();
3519 21772901 : types[1] = make_tree_vector ();
3520 :
3521 21772901 : if (len == 3)
3522 738 : len = 2;
3523 44548528 : for (unsigned i = 0; i < len; ++i)
3524 : {
3525 33331050 : if (MAYBE_CLASS_TYPE_P (argtypes[i]))
3526 : {
3527 15132691 : tree convs;
3528 :
3529 15132691 : if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
3530 : return;
3531 :
3532 11556624 : convs = lookup_conversions (argtypes[i]);
3533 :
3534 11556624 : if (code == COND_EXPR)
3535 : {
3536 1295 : if (lvalue_p (args[i]))
3537 940 : vec_safe_push (types[i], build_reference_type (argtypes[i]));
3538 :
3539 1295 : vec_safe_push (types[i], TYPE_MAIN_VARIANT (argtypes[i]));
3540 : }
3541 :
3542 11555329 : else if (! convs)
3543 : return;
3544 :
3545 11286204 : for (; convs; convs = TREE_CHAIN (convs))
3546 : {
3547 6708936 : type = TREE_TYPE (convs);
3548 :
3549 8305923 : if (i == 0 && ref1
3550 6708936 : && (!TYPE_REF_P (type)
3551 2599 : || CP_TYPE_CONST_P (TREE_TYPE (type))))
3552 1596987 : continue;
3553 :
3554 5111949 : if (code == COND_EXPR && TYPE_REF_P (type))
3555 24 : vec_safe_push (types[i], type);
3556 :
3557 5111949 : type = non_reference (type);
3558 5111949 : if (i != 0 || ! ref1)
3559 : {
3560 5111910 : type = cv_unqualified (type_decays_to (type));
3561 5111910 : if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
3562 656 : vec_safe_push (types[i], type);
3563 5111910 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3564 2878830 : type = type_promotes_to (type);
3565 : }
3566 :
3567 5111949 : if (! vec_member (type, types[i]))
3568 5103695 : vec_safe_push (types[i], type);
3569 : }
3570 : }
3571 : else
3572 : {
3573 18198359 : if (code == COND_EXPR && lvalue_p (args[i]))
3574 95 : vec_safe_push (types[i], build_reference_type (argtypes[i]));
3575 18198359 : type = non_reference (argtypes[i]);
3576 18198359 : if (i != 0 || ! ref1)
3577 : {
3578 16397612 : type = cv_unqualified (type_decays_to (type));
3579 16397612 : if (enum_p && UNSCOPED_ENUM_P (type))
3580 1951312 : vec_safe_push (types[i], type);
3581 16397612 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3582 9251806 : type = type_promotes_to (type);
3583 : }
3584 18198359 : vec_safe_push (types[i], type);
3585 : }
3586 : }
3587 :
3588 : /* Run through the possible parameter types of both arguments,
3589 : creating candidates with those parameter types. */
3590 22995682 : FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t)
3591 : {
3592 11778204 : unsigned jx;
3593 11778204 : tree u;
3594 :
3595 11778204 : if (!types[1]->is_empty ())
3596 25391917 : FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u)
3597 13613713 : add_builtin_candidate
3598 13613713 : (candidates, code, code2, fnname, t,
3599 : u, args, argtypes, flags, complain);
3600 : else
3601 484759 : add_builtin_candidate
3602 484759 : (candidates, code, code2, fnname, t,
3603 : NULL_TREE, args, argtypes, flags, complain);
3604 : }
3605 :
3606 11217478 : release_tree_vector (types[0]);
3607 11217478 : release_tree_vector (types[1]);
3608 : }
3609 :
3610 :
3611 : /* If TMPL can be successfully instantiated as indicated by
3612 : EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
3613 :
3614 : TMPL is the template. EXPLICIT_TARGS are any explicit template
3615 : arguments. ARGLIST is the arguments provided at the call-site.
3616 : This does not change ARGLIST. The RETURN_TYPE is the desired type
3617 : for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
3618 : as for add_function_candidate. If an OBJ is supplied, FLAGS and
3619 : CTYPE are ignored, and OBJ is as for add_conv_candidate.
3620 :
3621 : SHORTCUT_BAD_CONVS is as in add_function_candidate. */
3622 :
3623 : static struct z_candidate*
3624 513899179 : add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
3625 : tree ctype, tree explicit_targs, tree first_arg,
3626 : const vec<tree, va_gc> *arglist, tree return_type,
3627 : tree access_path, tree conversion_path,
3628 : int flags, tree obj, unification_kind_t strict,
3629 : bool shortcut_bad_convs, tsubst_flags_t complain)
3630 : {
3631 513899179 : int ntparms = DECL_NTPARMS (tmpl);
3632 513899179 : tree targs = make_tree_vec (ntparms);
3633 513899179 : unsigned int len = vec_safe_length (arglist);
3634 513899179 : unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
3635 513899179 : unsigned int skip_without_in_chrg = 0;
3636 513899179 : tree first_arg_without_in_chrg = first_arg;
3637 513899179 : tree *args_without_in_chrg;
3638 513899179 : unsigned int nargs_without_in_chrg;
3639 513899179 : unsigned int ia, ix;
3640 513899179 : tree arg;
3641 513899179 : struct z_candidate *cand;
3642 513899179 : tree fn;
3643 513899179 : struct rejection_reason *reason = NULL;
3644 513899179 : int errs;
3645 513899179 : conversion **convs = NULL;
3646 :
3647 : /* We don't do deduction on the in-charge parameter, the VTT
3648 : parameter or 'this'. */
3649 513899179 : if (DECL_IOBJ_MEMBER_FUNCTION_P (tmpl))
3650 : {
3651 52975344 : if (first_arg_without_in_chrg != NULL_TREE)
3652 : first_arg_without_in_chrg = NULL_TREE;
3653 12 : else if (return_type && strict == DEDUCE_CALL)
3654 : /* We're deducing for a call to the result of a template conversion
3655 : function, so the args don't contain 'this'; leave them alone. */;
3656 : else
3657 513899179 : ++skip_without_in_chrg;
3658 : }
3659 :
3660 513899179 : if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
3661 513899179 : || DECL_BASE_CONSTRUCTOR_P (tmpl))
3662 514898975 : && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
3663 : {
3664 261 : if (first_arg_without_in_chrg != NULL_TREE)
3665 : first_arg_without_in_chrg = NULL_TREE;
3666 : else
3667 261 : ++skip_without_in_chrg;
3668 : }
3669 :
3670 513899179 : if (len < skip_without_in_chrg)
3671 0 : return add_ignored_candidate (candidates, tmpl);
3672 :
3673 562198694 : if (DECL_CONSTRUCTOR_P (tmpl) && nargs == 2
3674 556853486 : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (first_arg),
3675 42954307 : TREE_TYPE ((*arglist)[0])))
3676 : {
3677 : /* 12.8/6 says, "A declaration of a constructor for a class X is
3678 : ill-formed if its first parameter is of type (optionally cv-qualified)
3679 : X and either there are no other parameters or else all other
3680 : parameters have default arguments. A member function template is never
3681 : instantiated to produce such a constructor signature."
3682 :
3683 : So if we're trying to copy an object of the containing class, don't
3684 : consider a template constructor that has a first parameter type that
3685 : is just a template parameter, as we would deduce a signature that we
3686 : would then reject in the code below. */
3687 2344076 : if (tree firstparm = FUNCTION_FIRST_USER_PARMTYPE (tmpl))
3688 : {
3689 2344076 : firstparm = TREE_VALUE (firstparm);
3690 2344076 : if (PACK_EXPANSION_P (firstparm))
3691 2122 : firstparm = PACK_EXPANSION_PATTERN (firstparm);
3692 2344076 : if (TREE_CODE (firstparm) == TEMPLATE_TYPE_PARM)
3693 : {
3694 755435 : gcc_assert (!explicit_targs);
3695 755435 : reason = invalid_copy_with_fn_template_rejection ();
3696 755435 : goto fail;
3697 : }
3698 : }
3699 : }
3700 :
3701 513143744 : nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
3702 513143744 : + (len - skip_without_in_chrg));
3703 513143744 : args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
3704 513143744 : ia = 0;
3705 513143744 : if (first_arg_without_in_chrg != NULL_TREE)
3706 : {
3707 10137 : args_without_in_chrg[ia] = first_arg_without_in_chrg;
3708 10137 : ++ia;
3709 : }
3710 513143744 : for (ix = skip_without_in_chrg;
3711 1370173214 : vec_safe_iterate (arglist, ix, &arg);
3712 : ++ix)
3713 : {
3714 857029470 : args_without_in_chrg[ia] = arg;
3715 857029470 : ++ia;
3716 : }
3717 513143744 : gcc_assert (ia == nargs_without_in_chrg);
3718 :
3719 513143744 : if (!obj)
3720 : {
3721 : /* Check that there's no obvious arity mismatch before proceeding with
3722 : deduction. This avoids substituting explicit template arguments
3723 : into the template or e.g. derived-to-base parm/arg unification
3724 : (which could result in an error outside the immediate context) when
3725 : the resulting candidate would be unviable anyway. */
3726 513143732 : int min_arity = 0, max_arity = 0;
3727 513143732 : tree parms = TYPE_ARG_TYPES (TREE_TYPE (tmpl));
3728 513143732 : parms = skip_artificial_parms_for (tmpl, parms);
3729 1924800116 : for (; parms != void_list_node; parms = TREE_CHAIN (parms))
3730 : {
3731 1803194522 : if (!parms || PACK_EXPANSION_P (TREE_VALUE (parms)))
3732 : {
3733 : max_arity = -1;
3734 : break;
3735 : }
3736 898512652 : if (TREE_PURPOSE (parms))
3737 : /* A parameter with a default argument. */
3738 10055960 : ++max_arity;
3739 : else
3740 888456692 : ++min_arity, ++max_arity;
3741 : }
3742 513143732 : if (ia < (unsigned)min_arity)
3743 : {
3744 : /* Too few arguments. */
3745 34770909 : reason = arity_rejection (NULL_TREE, min_arity, ia,
3746 : /*least_p=*/(max_arity == -1));
3747 34770909 : goto fail;
3748 : }
3749 478372823 : else if (max_arity != -1 && ia > (unsigned)max_arity)
3750 : {
3751 : /* Too many arguments. */
3752 5159831 : reason = arity_rejection (NULL_TREE, max_arity, ia);
3753 5159831 : goto fail;
3754 : }
3755 :
3756 473212992 : convs = alloc_conversions (nargs);
3757 :
3758 473212992 : if (shortcut_bad_convs
3759 473203622 : && DECL_IOBJ_MEMBER_FUNCTION_P (tmpl)
3760 547067846 : && !DECL_CONSTRUCTOR_P (tmpl))
3761 : {
3762 : /* Check the 'this' conversion before proceeding with deduction.
3763 : This is effectively an extension of the DR 1391 resolution
3764 : that we perform in check_non_deducible_conversions, though it's
3765 : convenient to do this extra check here instead of there. */
3766 4105297 : tree parmtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (tmpl)));
3767 4105297 : tree argtype = lvalue_type (first_arg);
3768 4105297 : tree arg = first_arg;
3769 4105297 : conversion *t = build_this_conversion (tmpl, ctype,
3770 : parmtype, argtype, arg,
3771 : flags, complain);
3772 4105297 : convs[0] = t;
3773 4105297 : if (t->bad_p)
3774 : {
3775 39427 : reason = bad_arg_conversion_rejection (first_arg, 0,
3776 : arg, parmtype,
3777 39427 : EXPR_LOCATION (arg));
3778 39427 : goto fail;
3779 : }
3780 : }
3781 : }
3782 :
3783 473173577 : errs = errorcount+sorrycount;
3784 946333603 : fn = fn_type_unification (tmpl, explicit_targs, targs,
3785 : args_without_in_chrg,
3786 : nargs_without_in_chrg,
3787 : return_type, strict, flags, convs,
3788 473173577 : false, complain & tf_decltype);
3789 :
3790 473160026 : if (fn == error_mark_node)
3791 : {
3792 : /* Don't repeat unification later if it already resulted in errors. */
3793 383210352 : if (errorcount+sorrycount == errs)
3794 383210236 : reason = template_unification_rejection (tmpl, explicit_targs,
3795 : targs, args_without_in_chrg,
3796 : nargs_without_in_chrg,
3797 : return_type, strict, flags);
3798 : else
3799 116 : reason = template_unification_error_rejection ();
3800 383210352 : goto fail;
3801 : }
3802 :
3803 : /* Now the explicit specifier might have been deduced; check if this
3804 : declaration is explicit. If it is and we're ignoring non-converting
3805 : constructors, don't add this function to the set of candidates. */
3806 89949674 : if (((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
3807 : == LOOKUP_ONLYCONVERTING)
3808 89949674 : && DECL_NONCONVERTING_P (fn))
3809 5457 : return add_ignored_candidate (candidates, fn);
3810 :
3811 179888434 : if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
3812 : {
3813 4814845 : tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
3814 9629663 : if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
3815 : ctype))
3816 : {
3817 : /* We're trying to produce a constructor with a prohibited signature,
3818 : as discussed above; handle here any cases we didn't catch then,
3819 : such as X(X<T>). */
3820 842 : reason = invalid_copy_with_fn_template_rejection ();
3821 842 : goto fail;
3822 : }
3823 : }
3824 :
3825 89943375 : if (obj != NULL_TREE)
3826 : /* Aha, this is a conversion function. */
3827 9 : cand = add_conv_candidate (candidates, fn, obj, arglist,
3828 : access_path, conversion_path, complain);
3829 : else
3830 89943366 : cand = add_function_candidate (candidates, fn, ctype,
3831 : first_arg, arglist, access_path,
3832 : conversion_path, flags, convs,
3833 : shortcut_bad_convs, complain);
3834 89943375 : if (DECL_TI_TEMPLATE (fn) != tmpl)
3835 : /* This situation can occur if a member template of a template
3836 : class is specialized. Then, instantiate_template might return
3837 : an instantiation of the specialization, in which case the
3838 : DECL_TI_TEMPLATE field will point at the original
3839 : specialization. For example:
3840 :
3841 : template <class T> struct S { template <class U> void f(U);
3842 : template <> void f(int) {}; };
3843 : S<double> sd;
3844 : sd.f(3);
3845 :
3846 : Here, TMPL will be template <class U> S<double>::f(U).
3847 : And, instantiate template will give us the specialization
3848 : template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
3849 : for this will point at template <class T> template <> S<T>::f(int),
3850 : so that we can find the definition. For the purposes of
3851 : overload resolution, however, we want the original TMPL. */
3852 5543889 : cand->template_decl = build_template_info (tmpl, targs);
3853 : else
3854 84399486 : cand->template_decl = DECL_TEMPLATE_INFO (fn);
3855 89943375 : cand->explicit_targs = explicit_targs;
3856 :
3857 89943375 : return cand;
3858 423936796 : fail:
3859 423936796 : int viable = (reason->code == rr_bad_arg_conversion ? -1 : 0);
3860 423936796 : return add_candidate (candidates, tmpl, first_arg, arglist, nargs, convs,
3861 423936796 : access_path, conversion_path, viable, reason, flags);
3862 : }
3863 :
3864 :
3865 : static struct z_candidate *
3866 513899167 : add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
3867 : tree explicit_targs, tree first_arg,
3868 : const vec<tree, va_gc> *arglist, tree return_type,
3869 : tree access_path, tree conversion_path, int flags,
3870 : unification_kind_t strict, bool shortcut_bad_convs,
3871 : tsubst_flags_t complain)
3872 : {
3873 513899167 : return
3874 0 : add_template_candidate_real (candidates, tmpl, ctype,
3875 : explicit_targs, first_arg, arglist,
3876 : return_type, access_path, conversion_path,
3877 : flags, NULL_TREE, strict, shortcut_bad_convs,
3878 0 : complain);
3879 : }
3880 :
3881 : /* Create an overload candidate for the conversion function template TMPL,
3882 : returning RETURN_TYPE, which will be invoked for expression OBJ to produce a
3883 : pointer-to-function which will in turn be called with the argument list
3884 : ARGLIST, and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
3885 : passed on to implicit_conversion. */
3886 :
3887 : static struct z_candidate *
3888 12 : add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
3889 : tree obj,
3890 : const vec<tree, va_gc> *arglist,
3891 : tree return_type, tree access_path,
3892 : tree conversion_path, tsubst_flags_t complain)
3893 : {
3894 12 : return
3895 12 : add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
3896 : NULL_TREE, arglist, return_type, access_path,
3897 : conversion_path, 0, obj, DEDUCE_CALL,
3898 12 : /*shortcut_bad_convs=*/false, complain);
3899 : }
3900 :
3901 : /* The CANDS are the set of candidates that were considered for
3902 : overload resolution. Sort CANDS so that the strictly viable
3903 : candidates appear first, followed by non-strictly viable candidates,
3904 : followed by non-viable candidates. Returns the first candidate
3905 : in this sorted list. If any of the candidates were viable, set
3906 : *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
3907 : considered viable only if it is strictly viable when setting
3908 : *ANY_VIABLE_P. */
3909 :
3910 : static struct z_candidate*
3911 327211189 : splice_viable (struct z_candidate *cands,
3912 : bool strict_p,
3913 : bool *any_viable_p)
3914 : {
3915 327211189 : z_candidate *strictly_viable = nullptr;
3916 327211189 : z_candidate **strictly_viable_tail = &strictly_viable;
3917 :
3918 327211189 : z_candidate *non_strictly_viable = nullptr;
3919 327211189 : z_candidate **non_strictly_viable_tail = &non_strictly_viable;
3920 :
3921 327211189 : z_candidate *non_viable = nullptr;
3922 327211189 : z_candidate **non_viable_tail = &non_viable;
3923 :
3924 327211189 : z_candidate *non_viable_ignored = nullptr;
3925 327211189 : z_candidate **non_viable_ignored_tail = &non_viable_ignored;
3926 :
3927 : /* Be strict inside templates, since build_over_call won't actually
3928 : do the conversions to get pedwarns. */
3929 327211189 : if (processing_template_decl)
3930 56903270 : strict_p = true;
3931 :
3932 1244003188 : for (z_candidate *cand = cands; cand; cand = cand->next)
3933 : {
3934 916791999 : if (!strict_p
3935 322282486 : && (cand->viable == 1 || TREE_CODE (cand->fn) == TEMPLATE_DECL))
3936 : /* Be strict in the presence of a viable candidate. Also if
3937 : there are template candidates, so that we get deduction errors
3938 : for them instead of silently preferring a bad conversion. */
3939 817748138 : strict_p = true;
3940 :
3941 : /* Move this candidate to the appropriate list according to
3942 : its viability. */
3943 916791999 : auto& tail = (cand->viable == 1 ? strictly_viable_tail
3944 : : cand->viable == -1 ? non_strictly_viable_tail
3945 1460340044 : : ignored_candidate_p (cand) ? non_viable_ignored_tail
3946 1517437884 : : non_viable_tail);
3947 916791999 : *tail = cand;
3948 916791999 : tail = &cand->next;
3949 : }
3950 :
3951 654422378 : *any_viable_p = (strictly_viable != nullptr
3952 327211189 : || (!strict_p && non_strictly_viable != nullptr));
3953 :
3954 : /* Combine the lists. */
3955 327211189 : *non_viable_ignored_tail = nullptr;
3956 327211189 : *non_viable_tail = non_viable_ignored;
3957 327211189 : *non_strictly_viable_tail = non_viable;
3958 327211189 : *strictly_viable_tail = non_strictly_viable;
3959 :
3960 327211189 : return strictly_viable;
3961 : }
3962 :
3963 : static bool
3964 313334301 : any_strictly_viable (struct z_candidate *cands)
3965 : {
3966 428159898 : for (; cands; cands = cands->next)
3967 123776240 : if (cands->viable == 1)
3968 : return true;
3969 : return false;
3970 : }
3971 :
3972 : /* OBJ is being used in an expression like "OBJ.f (...)". In other
3973 : words, it is about to become the "this" pointer for a member
3974 : function call. Take the address of the object. */
3975 :
3976 : static tree
3977 68534015 : build_this (tree obj)
3978 : {
3979 : /* In a template, we are only concerned about the type of the
3980 : expression, so we can take a shortcut. */
3981 68534015 : if (processing_template_decl)
3982 76587 : return build_address (obj);
3983 :
3984 68457428 : return cp_build_addr_expr (obj, tf_warning_or_error);
3985 : }
3986 :
3987 : /* Returns true iff functions are equivalent. Equivalent functions are
3988 : not '==' only if one is a function-local extern function or if
3989 : both are extern "C". */
3990 :
3991 : static inline int
3992 4873220 : equal_functions (tree fn1, tree fn2)
3993 : {
3994 4873220 : if (TREE_CODE (fn1) != TREE_CODE (fn2))
3995 : return 0;
3996 4857875 : if (TREE_CODE (fn1) == TEMPLATE_DECL)
3997 36051 : return fn1 == fn2;
3998 9643630 : if (DECL_LOCAL_DECL_P (fn1) || DECL_LOCAL_DECL_P (fn2)
3999 9643627 : || DECL_EXTERN_C_FUNCTION_P (fn1))
4000 3016581 : return decls_match (fn1, fn2);
4001 1805243 : return fn1 == fn2;
4002 : }
4003 :
4004 : /* Print information about a candidate FN being rejected due to INFO. */
4005 :
4006 : static void
4007 5309 : print_conversion_rejection (location_t loc, struct conversion_info *info,
4008 : tree fn)
4009 : {
4010 5309 : tree from = info->from;
4011 5309 : if (!TYPE_P (from))
4012 4369 : from = lvalue_type (from);
4013 5309 : if (info->n_arg == -1)
4014 : {
4015 : /* Conversion of implicit `this' argument failed. */
4016 24 : if (!TYPE_P (info->from))
4017 : /* A bad conversion for 'this' must be discarding cv-quals. */
4018 24 : inform (loc, "passing %qT as %<this%> "
4019 : "argument discards qualifiers",
4020 : from);
4021 : else
4022 0 : inform (loc, "no known conversion for implicit "
4023 : "%<this%> parameter from %qH to %qI",
4024 : from, info->to_type);
4025 : }
4026 5285 : else if (!TYPE_P (info->from))
4027 : {
4028 4345 : if (info->n_arg >= 0)
4029 4345 : inform (loc, "conversion of argument %d would be ill-formed:",
4030 : info->n_arg + 1);
4031 4345 : iloc_sentinel ils = loc;
4032 4345 : perform_implicit_conversion (info->to_type, info->from,
4033 : tf_warning_or_error);
4034 4345 : }
4035 940 : else if (info->n_arg == -2)
4036 : /* Conversion of conversion function return value failed. */
4037 33 : inform (loc, "no known conversion from %qH to %qI",
4038 : from, info->to_type);
4039 : else
4040 : {
4041 907 : if (TREE_CODE (fn) == FUNCTION_DECL)
4042 751 : loc = get_fndecl_argument_location (fn, info->n_arg);
4043 907 : inform (loc, "no known conversion for argument %d from %qH to %qI",
4044 907 : info->n_arg + 1, from, info->to_type);
4045 : }
4046 5309 : }
4047 :
4048 : /* Print information about a candidate with WANT parameters and we found
4049 : HAVE. */
4050 :
4051 : static void
4052 1457 : print_arity_information (location_t loc, unsigned int have, unsigned int want,
4053 : bool least_p)
4054 : {
4055 1457 : if (least_p)
4056 41 : inform_n (loc, want,
4057 : "candidate expects at least %d argument, %d provided",
4058 : "candidate expects at least %d arguments, %d provided",
4059 : want, have);
4060 : else
4061 1416 : inform_n (loc, want,
4062 : "candidate expects %d argument, %d provided",
4063 : "candidate expects %d arguments, %d provided",
4064 : want, have);
4065 1457 : }
4066 :
4067 : /* Print information about one overload candidate CANDIDATE. MSGSTR
4068 : is the text to print before the candidate itself.
4069 :
4070 : NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
4071 : to have been run through gettext by the caller. This wart makes
4072 : life simpler in print_z_candidates and for the translators. */
4073 :
4074 : static void
4075 13299 : print_z_candidate (location_t loc, const char *msgstr,
4076 : struct z_candidate *candidate)
4077 : {
4078 13299 : const char *msg = (msgstr == NULL
4079 13299 : ? ""
4080 13299 : : ACONCAT ((_(msgstr), " ", NULL)));
4081 13299 : tree fn = candidate->fn;
4082 13299 : if (flag_new_inheriting_ctors)
4083 13272 : fn = strip_inheriting_ctors (fn);
4084 13299 : location_t cloc = location_of (fn);
4085 :
4086 13299 : if (identifier_p (fn))
4087 : {
4088 214 : cloc = loc;
4089 214 : if (candidate->num_convs == 3)
4090 0 : inform (cloc, "%s%<%D(%T, %T, %T)%> (built-in)", msg, fn,
4091 0 : candidate->convs[0]->type,
4092 0 : candidate->convs[1]->type,
4093 0 : candidate->convs[2]->type);
4094 214 : else if (candidate->num_convs == 2)
4095 190 : inform (cloc, "%s%<%D(%T, %T)%> (built-in)", msg, fn,
4096 190 : candidate->convs[0]->type,
4097 190 : candidate->convs[1]->type);
4098 : else
4099 24 : inform (cloc, "%s%<%D(%T)%> (built-in)", msg, fn,
4100 24 : candidate->convs[0]->type);
4101 : }
4102 13085 : else if (TYPE_P (fn))
4103 15 : inform (cloc, "%s%qT (conversion)", msg, fn);
4104 13070 : else if (candidate->viable == -1)
4105 4376 : inform (cloc, "%s%#qD (near match)", msg, fn);
4106 8694 : else if (ignored_candidate_p (candidate))
4107 6 : inform (cloc, "%s%#qD (ignored)", msg, fn);
4108 8688 : else if (DECL_DELETED_FN (fn))
4109 60 : inform (cloc, "%s%#qD (deleted)", msg, fn);
4110 8628 : else if (candidate->reversed ())
4111 300 : inform (cloc, "%s%#qD (reversed)", msg, fn);
4112 8328 : else if (candidate->rewritten ())
4113 150 : inform (cloc, "%s%#qD (rewritten)", msg, fn);
4114 : else
4115 8178 : inform (cloc, "%s%#qD", msg, fn);
4116 :
4117 13299 : auto_diagnostic_nesting_level sentinel;
4118 :
4119 13299 : if (fn != candidate->fn)
4120 : {
4121 57 : cloc = location_of (candidate->fn);
4122 57 : inform (cloc, "inherited here");
4123 : }
4124 :
4125 : /* Give the user some information about why this candidate failed. */
4126 13299 : if (candidate->reason != NULL)
4127 : {
4128 10184 : struct rejection_reason *r = candidate->reason;
4129 :
4130 10184 : switch (r->code)
4131 : {
4132 1457 : case rr_arity:
4133 1457 : print_arity_information (cloc, r->u.arity.actual,
4134 1457 : r->u.arity.expected,
4135 : r->u.arity.least_p);
4136 1457 : break;
4137 913 : case rr_arg_conversion:
4138 913 : print_conversion_rejection (cloc, &r->u.conversion, fn);
4139 913 : break;
4140 4396 : case rr_bad_arg_conversion:
4141 4396 : print_conversion_rejection (cloc, &r->u.bad_conversion, fn);
4142 4396 : break;
4143 11 : case rr_explicit_conversion:
4144 11 : inform (cloc, "return type %qT of explicit conversion function "
4145 : "cannot be converted to %qT with a qualification "
4146 : "conversion", r->u.conversion.from,
4147 : r->u.conversion.to_type);
4148 11 : break;
4149 6 : case rr_template_conversion:
4150 6 : inform (cloc, "conversion from return type %qT of template "
4151 : "conversion function specialization to %qT is not an "
4152 : "exact match", r->u.conversion.from,
4153 : r->u.conversion.to_type);
4154 6 : break;
4155 3268 : case rr_template_unification:
4156 : /* We use template_unification_error_rejection if unification caused
4157 : actual non-SFINAE errors, in which case we don't need to repeat
4158 : them here. */
4159 3268 : if (r->u.template_unification.tmpl == NULL_TREE)
4160 : {
4161 45 : inform (cloc, "substitution of deduced template arguments "
4162 : "resulted in errors seen above");
4163 45 : break;
4164 : }
4165 : /* Re-run template unification with diagnostics. */
4166 3223 : inform (cloc, "template argument deduction/substitution failed:");
4167 3223 : {
4168 3223 : auto_diagnostic_nesting_level sentinel;
4169 3223 : fn_type_unification (r->u.template_unification.tmpl,
4170 : r->u.template_unification.explicit_targs,
4171 : (make_tree_vec
4172 : (r->u.template_unification.num_targs)),
4173 : r->u.template_unification.args,
4174 : r->u.template_unification.nargs,
4175 : r->u.template_unification.return_type,
4176 : r->u.template_unification.strict,
4177 : r->u.template_unification.flags,
4178 : NULL, true, false);
4179 3223 : }
4180 3223 : break;
4181 2 : case rr_invalid_copy:
4182 2 : inform (cloc,
4183 : "a constructor taking a single argument of its own "
4184 : "class type is invalid");
4185 2 : break;
4186 93 : case rr_constraint_failure:
4187 93 : diagnose_constraints (cloc, fn, NULL_TREE);
4188 93 : break;
4189 32 : case rr_inherited_ctor:
4190 32 : inform (cloc, "an inherited constructor is not a candidate for "
4191 : "initialization from an expression of the same or derived "
4192 : "type");
4193 32 : break;
4194 : case rr_ignored:
4195 : break;
4196 0 : case rr_none:
4197 0 : default:
4198 : /* This candidate didn't have any issues or we failed to
4199 : handle a particular code. Either way... */
4200 0 : gcc_unreachable ();
4201 : }
4202 : }
4203 13299 : }
4204 :
4205 : /* Print information about each overload candidate in CANDIDATES,
4206 : which is assumed to have gone through splice_viable and tourney
4207 : (if splice_viable succeeded). */
4208 :
4209 : static void
4210 5556 : print_z_candidates (location_t loc, struct z_candidate *candidates,
4211 : tristate only_viable_p /* = tristate::unknown () */)
4212 : {
4213 5556 : struct z_candidate *cand1;
4214 5556 : struct z_candidate **cand2;
4215 :
4216 5556 : if (!candidates)
4217 1011 : return;
4218 :
4219 : /* Remove non-viable deleted candidates. */
4220 4545 : cand1 = candidates;
4221 20421 : for (cand2 = &cand1; *cand2; )
4222 : {
4223 15876 : if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
4224 11431 : && !(*cand2)->viable
4225 19637 : && DECL_DELETED_FN ((*cand2)->fn))
4226 102 : *cand2 = (*cand2)->next;
4227 : else
4228 15774 : cand2 = &(*cand2)->next;
4229 : }
4230 : /* ...if there are any non-deleted ones. */
4231 4545 : if (cand1)
4232 4539 : candidates = cand1;
4233 :
4234 : /* There may be duplicates in the set of candidates. We put off
4235 : checking this condition as long as possible, since we have no way
4236 : to eliminate duplicates from a set of functions in less than n^2
4237 : time. Now we are about to emit an error message, so it is more
4238 : permissible to go slowly. */
4239 20019 : for (cand1 = candidates; cand1; cand1 = cand1->next)
4240 : {
4241 15474 : tree fn = cand1->fn;
4242 : /* Skip builtin candidates and conversion functions. */
4243 15474 : if (!DECL_P (fn))
4244 289 : continue;
4245 15185 : cand2 = &cand1->next;
4246 143319 : while (*cand2)
4247 : {
4248 128134 : if (DECL_P ((*cand2)->fn)
4249 128134 : && equal_functions (fn, (*cand2)->fn))
4250 306 : *cand2 = (*cand2)->next;
4251 : else
4252 127828 : cand2 = &(*cand2)->next;
4253 : }
4254 : }
4255 :
4256 : /* Unless otherwise specified, if there's a (strictly) viable candidate
4257 : then we assume we're being called as part of diagnosing ambiguity, in
4258 : which case we want to print only viable candidates since non-viable
4259 : candidates couldn't have contributed to the ambiguity. */
4260 4545 : if (only_viable_p.is_unknown ())
4261 4536 : only_viable_p = candidates->viable == 1;
4262 :
4263 4545 : auto_diagnostic_nesting_level sentinel;
4264 :
4265 4545 : int num_candidates = 0;
4266 22279 : for (auto iter = candidates; iter; iter = iter->next)
4267 : {
4268 13530 : if (only_viable_p.is_true () && iter->viable != 1)
4269 : break;
4270 13189 : ++num_candidates;
4271 : }
4272 :
4273 4545 : inform_num_candidates (loc, num_candidates);
4274 :
4275 4545 : auto_diagnostic_nesting_level sentinel2;
4276 :
4277 4545 : int candidate_idx = 0;
4278 22244 : for (; candidates; candidates = candidates->next)
4279 : {
4280 13526 : if (only_viable_p.is_true () && candidates->viable != 1)
4281 : break;
4282 13222 : if (ignored_candidate_p (candidates) && !flag_diagnostics_all_candidates)
4283 : {
4284 31 : inform (loc, "some candidates omitted; "
4285 : "use %<-fdiagnostics-all-candidates%> to display them");
4286 31 : break;
4287 : }
4288 13154 : pretty_printer pp;
4289 13154 : pp_printf (&pp, N_("candidate %i:"), candidate_idx + 1);
4290 13154 : const char *const msgstr = pp_formatted_text (&pp);
4291 13154 : print_z_candidate (loc, msgstr, candidates);
4292 13154 : ++candidate_idx;
4293 13154 : }
4294 4545 : }
4295 :
4296 : /* USER_SEQ is a user-defined conversion sequence, beginning with a
4297 : USER_CONV. STD_SEQ is the standard conversion sequence applied to
4298 : the result of the conversion function to convert it to the final
4299 : desired type. Merge the two sequences into a single sequence,
4300 : and return the merged sequence. */
4301 :
4302 : static conversion *
4303 15420766 : merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
4304 : {
4305 15420766 : conversion **t;
4306 15420766 : bool bad = user_seq->bad_p;
4307 :
4308 15420766 : gcc_assert (user_seq->kind == ck_user);
4309 :
4310 : /* Find the end of the second conversion sequence. */
4311 16201004 : for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
4312 : {
4313 : /* The entire sequence is a user-conversion sequence. */
4314 780238 : (*t)->user_conv_p = true;
4315 780238 : if (bad)
4316 3506 : (*t)->bad_p = true;
4317 : }
4318 :
4319 15420766 : if ((*t)->rvaluedness_matches_p)
4320 : /* We're binding a reference directly to the result of the conversion.
4321 : build_user_type_conversion_1 stripped the REFERENCE_TYPE from the return
4322 : type, but we want it back. */
4323 465580 : user_seq->type = TREE_TYPE (TREE_TYPE (user_seq->cand->fn));
4324 :
4325 : /* Replace the identity conversion with the user conversion
4326 : sequence. */
4327 15420766 : *t = user_seq;
4328 :
4329 15420766 : return std_seq;
4330 : }
4331 :
4332 : /* Handle overload resolution for initializing an object of class type from
4333 : an initializer list. First we look for a suitable constructor that
4334 : takes a std::initializer_list; if we don't find one, we then look for a
4335 : non-list constructor.
4336 :
4337 : Parameters are as for add_candidates, except that the arguments are in
4338 : the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
4339 : the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
4340 :
4341 : static void
4342 2548528 : add_list_candidates (tree fns, tree first_arg,
4343 : const vec<tree, va_gc> *args, tree totype,
4344 : tree explicit_targs, bool template_only,
4345 : tree conversion_path, tree access_path,
4346 : int flags,
4347 : struct z_candidate **candidates,
4348 : tsubst_flags_t complain)
4349 : {
4350 2548528 : gcc_assert (*candidates == NULL);
4351 :
4352 : /* We're looking for a ctor for list-initialization. */
4353 2548528 : flags |= LOOKUP_LIST_INIT_CTOR;
4354 : /* And we don't allow narrowing conversions. We also use this flag to
4355 : avoid the copy constructor call for copy-list-initialization. */
4356 2548528 : flags |= LOOKUP_NO_NARROWING;
4357 :
4358 5097056 : unsigned nart = num_artificial_parms_for (OVL_FIRST (fns)) - 1;
4359 2548528 : tree init_list = (*args)[nart];
4360 :
4361 : /* Always use the default constructor if the list is empty (DR 990). */
4362 2548528 : if (CONSTRUCTOR_NELTS (init_list) == 0
4363 2548528 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
4364 : ;
4365 2305137 : else if (CONSTRUCTOR_IS_DESIGNATED_INIT (init_list)
4366 2305137 : && !CP_AGGREGATE_TYPE_P (totype))
4367 : {
4368 53 : if (complain & tf_error)
4369 15 : error ("designated initializers cannot be used with a "
4370 : "non-aggregate type %qT", totype);
4371 11737 : return;
4372 : }
4373 : /* If the class has a list ctor, try passing the list as a single
4374 : argument first, but only consider list ctors. */
4375 2305084 : else if (TYPE_HAS_LIST_CTOR (totype))
4376 : {
4377 88347 : flags |= LOOKUP_LIST_ONLY;
4378 88347 : add_candidates (fns, first_arg, args, NULL_TREE,
4379 : explicit_targs, template_only, conversion_path,
4380 : access_path, flags, candidates, complain);
4381 176694 : if (any_strictly_viable (*candidates))
4382 : return;
4383 : }
4384 :
4385 : /* Expand the CONSTRUCTOR into a new argument vec. */
4386 2536791 : vec<tree, va_gc> *new_args;
4387 4829436 : vec_alloc (new_args, nart + CONSTRUCTOR_NELTS (init_list));
4388 5073585 : for (unsigned i = 0; i < nart; ++i)
4389 3 : new_args->quick_push ((*args)[i]);
4390 2536791 : new_args = append_ctor_to_tree_vector (new_args, init_list);
4391 :
4392 : /* We aren't looking for list-ctors anymore. */
4393 2536791 : flags &= ~LOOKUP_LIST_ONLY;
4394 : /* We allow more user-defined conversions within an init-list. */
4395 2536791 : flags &= ~LOOKUP_NO_CONVERSION;
4396 :
4397 2536791 : add_candidates (fns, first_arg, new_args, NULL_TREE,
4398 : explicit_targs, template_only, conversion_path,
4399 : access_path, flags, candidates, complain);
4400 : }
4401 :
4402 : /* Given C(std::initializer_list<A>), return A. */
4403 :
4404 : static tree
4405 1503 : list_ctor_element_type (tree fn)
4406 : {
4407 1503 : gcc_checking_assert (is_list_ctor (fn));
4408 :
4409 1503 : tree parm = FUNCTION_FIRST_USER_PARMTYPE (fn);
4410 1503 : parm = non_reference (TREE_VALUE (parm));
4411 1503 : return TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
4412 : }
4413 :
4414 : /* If EXPR is a braced-init-list where the elements all decay to the same type,
4415 : return that type. */
4416 :
4417 : static tree
4418 1551 : braced_init_element_type (tree expr)
4419 : {
4420 1551 : if (TREE_CODE (expr) == CONSTRUCTOR
4421 1551 : && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
4422 0 : return TREE_TYPE (TREE_TYPE (expr));
4423 1551 : if (!BRACE_ENCLOSED_INITIALIZER_P (expr))
4424 : return NULL_TREE;
4425 :
4426 1551 : tree elttype = NULL_TREE;
4427 15168 : for (constructor_elt &e: CONSTRUCTOR_ELTS (expr))
4428 : {
4429 10717 : tree type = TREE_TYPE (e.value);
4430 10717 : type = type_decays_to (type);
4431 10717 : if (!elttype)
4432 : elttype = type;
4433 9194 : else if (!same_type_p (type, elttype))
4434 : return NULL_TREE;
4435 : }
4436 : return elttype;
4437 : }
4438 :
4439 : /* True iff EXPR contains any temporaries with non-trivial destruction.
4440 :
4441 : ??? Also ignore classes with non-trivial but no-op destruction other than
4442 : std::allocator? */
4443 :
4444 : static bool
4445 193 : has_non_trivial_temporaries (tree expr)
4446 : {
4447 193 : auto_vec<tree*> temps;
4448 193 : cp_walk_tree_without_duplicates (&expr, find_temps_r, &temps);
4449 388 : for (tree *p : temps)
4450 : {
4451 65 : tree t = TREE_TYPE (*p);
4452 65 : if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (t)
4453 65 : && !is_std_allocator (t))
4454 : return true;
4455 : }
4456 : return false;
4457 193 : }
4458 :
4459 : /* Return number of initialized elements in CTOR. */
4460 :
4461 : unsigned HOST_WIDE_INT
4462 263 : count_ctor_elements (tree ctor)
4463 : {
4464 263 : unsigned HOST_WIDE_INT len = 0;
4465 2194 : for (constructor_elt &e: CONSTRUCTOR_ELTS (ctor))
4466 1405 : if (TREE_CODE (e.value) == RAW_DATA_CST)
4467 9 : len += RAW_DATA_LENGTH (e.value);
4468 : else
4469 1396 : ++len;
4470 263 : return len;
4471 : }
4472 :
4473 : /* We're initializing an array of ELTTYPE from INIT. If it seems useful,
4474 : return INIT as an array (of its own type) so the caller can initialize the
4475 : target array in a loop. */
4476 :
4477 : static tree
4478 19059 : maybe_init_list_as_array (tree elttype, tree init)
4479 : {
4480 : /* Only do this if the array can go in rodata but not once converted. */
4481 19059 : if (!TYPE_NON_AGGREGATE_CLASS (elttype))
4482 : return NULL_TREE;
4483 1551 : tree init_elttype = braced_init_element_type (init);
4484 1551 : if (!init_elttype || !SCALAR_TYPE_P (init_elttype) || !TREE_CONSTANT (init))
4485 : return NULL_TREE;
4486 :
4487 : /* Check with a stub expression to weed out special cases, and check whether
4488 : we call the same function for direct-init as copy-list-init. */
4489 367 : conversion_obstack_sentinel cos;
4490 367 : init_elttype = cp_build_qualified_type (init_elttype, TYPE_QUAL_CONST);
4491 367 : tree arg = build_stub_object (init_elttype);
4492 367 : conversion *c = implicit_conversion (elttype, init_elttype, arg, false,
4493 : LOOKUP_NORMAL, tf_none);
4494 367 : if (c && c->kind == ck_rvalue)
4495 0 : c = next_conversion (c);
4496 361 : if (!c || c->kind != ck_user)
4497 : return NULL_TREE;
4498 : /* Check that we actually can perform the conversion. */
4499 358 : if (convert_like (c, arg, tf_none) == error_mark_node)
4500 : /* Let the normal code give the error. */
4501 : return NULL_TREE;
4502 :
4503 : /* A glvalue initializer might be significant to a reference constructor
4504 : or conversion operator. */
4505 352 : if (!DECL_CONSTRUCTOR_P (c->cand->fn)
4506 352 : || (TYPE_REF_P (TREE_VALUE
4507 : (FUNCTION_FIRST_USER_PARMTYPE (c->cand->fn)))))
4508 240 : for (auto &ce : CONSTRUCTOR_ELTS (init))
4509 108 : if (non_mergeable_glvalue_p (ce.value))
4510 : return NULL_TREE;
4511 :
4512 349 : tree first = CONSTRUCTOR_ELT (init, 0)->value;
4513 349 : conversion *fc = implicit_conversion (elttype, init_elttype, first, false,
4514 : LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING,
4515 : tf_none);
4516 349 : if (fc && fc->kind == ck_rvalue)
4517 48 : fc = next_conversion (fc);
4518 349 : if (!fc || fc->kind != ck_user || fc->cand->fn != c->cand->fn)
4519 : return NULL_TREE;
4520 307 : first = convert_like (fc, first, tf_none);
4521 307 : if (first == error_mark_node)
4522 : /* Let the normal code give the error. */
4523 : return NULL_TREE;
4524 :
4525 : /* Don't do this if the conversion would be constant. */
4526 304 : first = maybe_constant_init (first);
4527 304 : if (TREE_CONSTANT (first))
4528 : return NULL_TREE;
4529 :
4530 : /* We can't do this if the conversion creates temporaries that need
4531 : to live until the whole array is initialized. */
4532 193 : if (has_non_trivial_temporaries (first))
4533 : return NULL_TREE;
4534 :
4535 : /* We can't do this if copying from the initializer_list would be
4536 : ill-formed. */
4537 193 : tree copy_argtypes = make_tree_vec (1);
4538 193 : TREE_VEC_ELT (copy_argtypes, 0)
4539 193 : = cp_build_qualified_type (elttype, TYPE_QUAL_CONST);
4540 193 : if (!is_xible (INIT_EXPR, elttype, copy_argtypes))
4541 : return NULL_TREE;
4542 :
4543 187 : unsigned HOST_WIDE_INT len = count_ctor_elements (init);
4544 187 : tree arr = build_array_of_n_type (init_elttype, len);
4545 187 : arr = finish_compound_literal (arr, init, tf_none);
4546 187 : DECL_MERGEABLE (TARGET_EXPR_SLOT (arr)) = true;
4547 187 : return arr;
4548 367 : }
4549 :
4550 : /* If we were going to call e.g. vector(initializer_list<string>) starting
4551 : with a list of string-literals (which is inefficient, see PR105838),
4552 : instead build an array of const char* and pass it to the range constructor.
4553 : But only do this for standard library types, where we can assume the
4554 : transformation makes sense.
4555 :
4556 : Really the container classes should have initializer_list<U> constructors to
4557 : get the same effect more simply; this is working around that lack. */
4558 :
4559 : static tree
4560 14963688 : maybe_init_list_as_range (tree fn, tree expr)
4561 : {
4562 14963688 : if (!processing_template_decl
4563 14431692 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
4564 902525 : && is_list_ctor (fn)
4565 14965344 : && decl_in_std_namespace_p (fn))
4566 : {
4567 1503 : tree to = list_ctor_element_type (fn);
4568 1503 : if (tree init = maybe_init_list_as_array (to, expr))
4569 : {
4570 55 : tree begin = decay_conversion (TARGET_EXPR_SLOT (init), tf_none);
4571 55 : tree nelts = array_type_nelts_top (TREE_TYPE (init));
4572 55 : tree end = cp_build_binary_op (input_location, PLUS_EXPR, begin,
4573 : nelts, tf_none);
4574 55 : begin = cp_build_compound_expr (init, begin, tf_none);
4575 55 : return build_constructor_va (init_list_type_node, 2,
4576 55 : NULL_TREE, begin, NULL_TREE, end);
4577 : }
4578 : }
4579 :
4580 : return NULL_TREE;
4581 : }
4582 :
4583 : /* Returns the best overload candidate to perform the requested
4584 : conversion. This function is used for three the overloading situations
4585 : described in [over.match.copy], [over.match.conv], and [over.match.ref].
4586 : If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
4587 : per [dcl.init.ref], so we ignore temporary bindings. */
4588 :
4589 : static struct z_candidate *
4590 96015884 : build_user_type_conversion_1 (tree totype, tree expr, int flags,
4591 : tsubst_flags_t complain)
4592 : {
4593 96015884 : struct z_candidate *candidates, *cand;
4594 96015884 : tree fromtype;
4595 96015884 : tree ctors = NULL_TREE;
4596 96015884 : tree conv_fns = NULL_TREE;
4597 96015884 : conversion *conv = NULL;
4598 96015884 : tree first_arg = NULL_TREE;
4599 96015884 : vec<tree, va_gc> *args = NULL;
4600 96015884 : bool any_viable_p;
4601 96015884 : int convflags;
4602 :
4603 96015884 : if (!expr)
4604 : return NULL;
4605 :
4606 96015878 : fromtype = TREE_TYPE (expr);
4607 :
4608 : /* We represent conversion within a hierarchy using RVALUE_CONV and
4609 : BASE_CONV, as specified by [over.best.ics]; these become plain
4610 : constructor calls, as specified in [dcl.init]. */
4611 96015878 : gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
4612 : || !DERIVED_FROM_P (totype, fromtype));
4613 :
4614 96015878 : if (CLASS_TYPE_P (totype))
4615 : /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
4616 : creating a garbage BASELINK; constructors can't be inherited. */
4617 52975709 : ctors = get_class_binding (totype, complete_ctor_identifier);
4618 :
4619 96015878 : tree to_nonref = non_reference (totype);
4620 96015878 : if (MAYBE_CLASS_TYPE_P (fromtype))
4621 : {
4622 66875529 : if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
4623 66865849 : (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
4624 54379035 : && DERIVED_FROM_P (to_nonref, fromtype)))
4625 : {
4626 : /* [class.conv.fct] A conversion function is never used to
4627 : convert a (possibly cv-qualified) object to the (possibly
4628 : cv-qualified) same object type (or a reference to it), to a
4629 : (possibly cv-qualified) base class of that type (or a
4630 : reference to it)... */
4631 : }
4632 : else
4633 66865846 : conv_fns = lookup_conversions (fromtype);
4634 : }
4635 :
4636 96015878 : candidates = 0;
4637 96015878 : flags |= LOOKUP_NO_CONVERSION;
4638 96015878 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4639 945877 : flags |= LOOKUP_NO_NARROWING;
4640 : /* Prevent add_candidates from treating a non-strictly viable candidate
4641 : as unviable. */
4642 96015878 : complain |= tf_conv;
4643 :
4644 : /* It's OK to bind a temporary for converting constructor arguments, but
4645 : not in converting the return value of a conversion operator. */
4646 96015878 : convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION
4647 96015878 : | (flags & LOOKUP_NO_NARROWING));
4648 96015878 : flags &= ~LOOKUP_NO_TEMP_BIND;
4649 :
4650 96015878 : if (ctors)
4651 : {
4652 52757254 : int ctorflags = flags;
4653 :
4654 52757254 : first_arg = build_dummy_object (totype);
4655 :
4656 : /* We should never try to call the abstract or base constructor
4657 : from here. */
4658 370286236 : gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_FIRST (ctors))
4659 : && !DECL_HAS_VTT_PARM_P (OVL_FIRST (ctors)));
4660 :
4661 52757254 : args = make_tree_vector_single (expr);
4662 52757254 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4663 : {
4664 : /* List-initialization. */
4665 945877 : add_list_candidates (ctors, first_arg, args, totype, NULL_TREE,
4666 945877 : false, TYPE_BINFO (totype), TYPE_BINFO (totype),
4667 : ctorflags, &candidates, complain);
4668 : }
4669 : else
4670 : {
4671 51811377 : add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
4672 51811377 : TYPE_BINFO (totype), TYPE_BINFO (totype),
4673 : ctorflags, &candidates, complain);
4674 : }
4675 :
4676 291541967 : for (cand = candidates; cand; cand = cand->next)
4677 : {
4678 238784713 : cand->second_conv = build_identity_conv (totype, NULL_TREE);
4679 :
4680 : /* If totype isn't a reference, and LOOKUP_ONLYCONVERTING is
4681 : set, then this is copy-initialization. In that case, "The
4682 : result of the call is then used to direct-initialize the
4683 : object that is the destination of the copy-initialization."
4684 : [dcl.init]
4685 :
4686 : We represent this in the conversion sequence with an
4687 : rvalue conversion, which means a constructor call. */
4688 238784713 : if (!TYPE_REF_P (totype)
4689 238784713 : && cxx_dialect < cxx17
4690 435618 : && (flags & LOOKUP_ONLYCONVERTING)
4691 377395 : && !(convflags & LOOKUP_NO_TEMP_BIND))
4692 117441 : cand->second_conv
4693 117441 : = build_conv (ck_rvalue, totype, cand->second_conv);
4694 : }
4695 : }
4696 :
4697 96015878 : if (conv_fns)
4698 : {
4699 30195812 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4700 0 : first_arg = CONSTRUCTOR_ELT (expr, 0)->value;
4701 : else
4702 96015878 : first_arg = expr;
4703 : }
4704 :
4705 131187153 : for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
4706 : {
4707 35171275 : tree conversion_path = TREE_PURPOSE (conv_fns);
4708 35171275 : struct z_candidate *old_candidates;
4709 :
4710 : /* If LOOKUP_NO_CONVERSION, don't consider a conversion function that
4711 : would need an additional user-defined conversion, i.e. if the return
4712 : type differs in class-ness from the desired type. So we avoid
4713 : considering operator bool when calling a copy constructor.
4714 :
4715 : This optimization avoids the failure in PR97600, and is allowed by
4716 : [temp.inst]/9: "If the function selected by overload resolution can be
4717 : determined without instantiating a class template definition, it is
4718 : unspecified whether that instantiation actually takes place." */
4719 35171275 : tree convtype = non_reference (TREE_TYPE (conv_fns));
4720 51836857 : if ((flags & LOOKUP_NO_CONVERSION)
4721 35171275 : && !WILDCARD_TYPE_P (convtype)
4722 66717980 : && (CLASS_TYPE_P (to_nonref)
4723 33358990 : != CLASS_TYPE_P (convtype)))
4724 16665582 : continue;
4725 :
4726 : /* If we are called to convert to a reference type, we are trying to
4727 : find a direct binding, so don't even consider temporaries. If
4728 : we don't find a direct binding, the caller will try again to
4729 : look for a temporary binding. */
4730 18505693 : if (TYPE_REF_P (totype))
4731 5143321 : convflags |= LOOKUP_NO_TEMP_BIND;
4732 :
4733 18505693 : old_candidates = candidates;
4734 18505693 : add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
4735 : NULL_TREE, false,
4736 18505693 : conversion_path, TYPE_BINFO (fromtype),
4737 : flags, &candidates, complain);
4738 :
4739 37010673 : for (cand = candidates; cand != old_candidates; cand = cand->next)
4740 : {
4741 18504980 : if (cand->viable == 0)
4742 : /* Already rejected, don't change to -1. */
4743 7001434 : continue;
4744 :
4745 11503546 : tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
4746 11503546 : conversion *ics
4747 11503546 : = implicit_conversion (totype,
4748 : rettype,
4749 : 0,
4750 : /*c_cast_p=*/false, convflags,
4751 : complain);
4752 :
4753 : /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
4754 : copy-initialization. In that case, "The result of the
4755 : call is then used to direct-initialize the object that is
4756 : the destination of the copy-initialization." [dcl.init]
4757 :
4758 : We represent this in the conversion sequence with an
4759 : rvalue conversion, which means a constructor call. But
4760 : don't add a second rvalue conversion if there's already
4761 : one there. Which there really shouldn't be, but it's
4762 : harmless since we'd add it here anyway. */
4763 4385794 : if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
4764 12214556 : && !(convflags & LOOKUP_NO_TEMP_BIND))
4765 259785 : ics = build_conv (ck_rvalue, totype, ics);
4766 :
4767 11503546 : cand->second_conv = ics;
4768 :
4769 11503546 : if (!ics)
4770 : {
4771 7117752 : cand->viable = 0;
4772 14232939 : cand->reason = arg_conversion_rejection (NULL_TREE, -2,
4773 : rettype, totype,
4774 7117752 : EXPR_LOCATION (expr));
4775 : }
4776 14615 : else if (TYPE_REF_P (totype) && !ics->rvaluedness_matches_p
4777 : /* Limit this to non-templates for now (PR90546). */
4778 1896 : && !cand->template_decl
4779 4387685 : && TREE_CODE (TREE_TYPE (totype)) != FUNCTION_TYPE)
4780 : {
4781 : /* If we are called to convert to a reference type, we are trying
4782 : to find a direct binding per [over.match.ref], so rvaluedness
4783 : must match for non-functions. */
4784 1885 : cand->viable = 0;
4785 : }
4786 4383909 : else if (DECL_NONCONVERTING_P (cand->fn)
4787 4383909 : && ics->rank > cr_exact)
4788 : {
4789 : /* 13.3.1.5: For direct-initialization, those explicit
4790 : conversion functions that are not hidden within S and
4791 : yield type T or a type that can be converted to type T
4792 : with a qualification conversion (4.4) are also candidate
4793 : functions. */
4794 : /* 13.3.1.6 doesn't have a parallel restriction, but it should;
4795 : I've raised this issue with the committee. --jason 9/2011 */
4796 2731 : cand->viable = -1;
4797 2731 : cand->reason = explicit_conversion_rejection (rettype, totype);
4798 : }
4799 4381178 : else if (cand->viable == 1 && ics->bad_p)
4800 : {
4801 2576 : cand->viable = -1;
4802 2576 : cand->reason
4803 5152 : = bad_arg_conversion_rejection (NULL_TREE, -2,
4804 : rettype, totype,
4805 2576 : EXPR_LOCATION (expr));
4806 : }
4807 4378602 : else if (primary_template_specialization_p (cand->fn)
4808 4378602 : && ics->rank > cr_exact)
4809 : {
4810 : /* 13.3.3.1.2: If the user-defined conversion is specified by
4811 : a specialization of a conversion function template, the
4812 : second standard conversion sequence shall have exact match
4813 : rank. */
4814 21 : cand->viable = -1;
4815 21 : cand->reason = template_conversion_rejection (rettype, totype);
4816 : }
4817 : }
4818 : }
4819 :
4820 96015878 : candidates = splice_viable (candidates, false, &any_viable_p);
4821 96015878 : if (!any_viable_p)
4822 : {
4823 81050844 : if (args)
4824 41452647 : release_tree_vector (args);
4825 : return NULL;
4826 : }
4827 :
4828 14965034 : cand = tourney (candidates, complain);
4829 14965034 : if (cand == NULL)
4830 : {
4831 1346 : if (complain & tf_error)
4832 : {
4833 71 : auto_diagnostic_group d;
4834 74 : error_at (cp_expr_loc_or_input_loc (expr),
4835 : "conversion from %qH to %qI is ambiguous",
4836 : fromtype, totype);
4837 71 : print_z_candidates (location_of (expr), candidates);
4838 71 : }
4839 :
4840 1346 : cand = candidates; /* any one will do */
4841 1346 : cand->second_conv = build_ambiguous_conv (totype, expr);
4842 1346 : cand->second_conv->user_conv_p = true;
4843 2692 : if (!any_strictly_viable (candidates))
4844 12 : cand->second_conv->bad_p = true;
4845 1346 : if (flags & LOOKUP_ONLYCONVERTING)
4846 1273 : cand->second_conv->need_temporary_p = true;
4847 : /* If there are viable candidates, don't set ICS_BAD_FLAG; an
4848 : ambiguous conversion is no worse than another user-defined
4849 : conversion. */
4850 :
4851 : return cand;
4852 : }
4853 :
4854 : /* Maybe pass { } as iterators instead of an initializer_list. */
4855 14963688 : if (tree iters = maybe_init_list_as_range (cand->fn, expr))
4856 110 : if (z_candidate *cand2
4857 55 : = build_user_type_conversion_1 (totype, iters, flags, tf_none))
4858 52 : if (cand2->viable == 1 && !is_list_ctor (cand2->fn))
4859 : {
4860 : cand = cand2;
4861 : expr = iters;
4862 : }
4863 :
4864 14963688 : tree convtype;
4865 29927376 : if (!DECL_CONSTRUCTOR_P (cand->fn))
4866 4371586 : convtype = non_reference (TREE_TYPE (TREE_TYPE (cand->fn)));
4867 10592102 : else if (cand->second_conv->kind == ck_rvalue)
4868 : /* DR 5: [in the first step of copy-initialization]...if the function
4869 : is a constructor, the call initializes a temporary of the
4870 : cv-unqualified version of the destination type. */
4871 12075 : convtype = cv_unqualified (totype);
4872 : else
4873 : convtype = totype;
4874 : /* Build the user conversion sequence. */
4875 14963688 : conv = build_conv
4876 14963688 : (ck_user,
4877 : convtype,
4878 14963688 : build_identity_conv (TREE_TYPE (expr), expr));
4879 14963688 : conv->cand = cand;
4880 14963688 : if (cand->viable == -1)
4881 11621 : conv->bad_p = true;
4882 :
4883 : /* Remember that this was a list-initialization. */
4884 14963688 : if (flags & LOOKUP_NO_NARROWING)
4885 3365229 : conv->check_narrowing = true;
4886 :
4887 : /* Combine it with the second conversion sequence. */
4888 14963688 : cand->second_conv = merge_conversion_sequences (conv,
4889 : cand->second_conv);
4890 :
4891 14963688 : return cand;
4892 : }
4893 :
4894 : /* Wrapper for above. */
4895 :
4896 : tree
4897 24162 : build_user_type_conversion (tree totype, tree expr, int flags,
4898 : tsubst_flags_t complain)
4899 : {
4900 24162 : struct z_candidate *cand;
4901 24162 : tree ret;
4902 :
4903 24162 : auto_cond_timevar tv (TV_OVERLOAD);
4904 :
4905 24162 : conversion_obstack_sentinel cos;
4906 :
4907 24162 : cand = build_user_type_conversion_1 (totype, expr, flags, complain);
4908 :
4909 24162 : if (cand)
4910 : {
4911 23932 : if (cand->second_conv->kind == ck_ambig)
4912 65 : ret = error_mark_node;
4913 : else
4914 : {
4915 23867 : expr = convert_like (cand->second_conv, expr, complain);
4916 23867 : ret = convert_from_reference (expr);
4917 : }
4918 : }
4919 : else
4920 : ret = NULL_TREE;
4921 :
4922 48324 : return ret;
4923 24162 : }
4924 :
4925 : /* Give a helpful diagnostic when implicit_conversion fails. */
4926 :
4927 : static void
4928 682 : implicit_conversion_error (location_t loc, tree type, tree expr,
4929 : int flags)
4930 : {
4931 682 : tsubst_flags_t complain = tf_warning_or_error;
4932 :
4933 : /* If expr has unknown type, then it is an overloaded function.
4934 : Call instantiate_type to get good error messages. */
4935 682 : if (TREE_TYPE (expr) == unknown_type_node)
4936 60 : instantiate_type (type, expr, complain);
4937 622 : else if (invalid_nonstatic_memfn_p (loc, expr, complain))
4938 : /* We gave an error. */;
4939 72 : else if (BRACE_ENCLOSED_INITIALIZER_P (expr)
4940 69 : && CONSTRUCTOR_IS_DESIGNATED_INIT (expr)
4941 637 : && !CP_AGGREGATE_TYPE_P (type))
4942 20 : error_at (loc, "designated initializers cannot be used with a "
4943 : "non-aggregate type %qT", type);
4944 : else
4945 : {
4946 597 : auto_diagnostic_group d;
4947 597 : if (is_stub_object (expr))
4948 : /* The expression is generated by a trait check, we don't have
4949 : a useful location to highlight the label. */
4950 17 : error_at (loc, "could not convert %qH to %qI",
4951 17 : TREE_TYPE (expr), type);
4952 : else
4953 : {
4954 580 : range_label_for_type_mismatch label (TREE_TYPE (expr), type);
4955 580 : gcc_rich_location rich_loc (loc, &label,
4956 580 : highlight_colors::percent_h);
4957 580 : error_at (&rich_loc, "could not convert %qE from %qH to %qI",
4958 580 : expr, TREE_TYPE (expr), type);
4959 580 : }
4960 597 : maybe_show_nonconverting_candidate (type, TREE_TYPE (expr), expr, flags);
4961 597 : }
4962 682 : }
4963 :
4964 : /* Worker for build_converted_constant_expr. */
4965 :
4966 : static tree
4967 326966890 : build_converted_constant_expr_internal (tree type, tree expr,
4968 : int flags, tsubst_flags_t complain)
4969 : {
4970 326966890 : conversion *conv;
4971 326966890 : tree t;
4972 326966890 : location_t loc = cp_expr_loc_or_input_loc (expr);
4973 :
4974 326966890 : if (error_operand_p (expr))
4975 65 : return error_mark_node;
4976 :
4977 326966825 : conversion_obstack_sentinel cos;
4978 :
4979 326966825 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
4980 : /*c_cast_p=*/false, flags, complain);
4981 :
4982 : /* A converted constant expression of type T is an expression, implicitly
4983 : converted to type T, where the converted expression is a constant
4984 : expression and the implicit conversion sequence contains only
4985 :
4986 : * user-defined conversions,
4987 : * lvalue-to-rvalue conversions (7.1),
4988 : * array-to-pointer conversions (7.2),
4989 : * function-to-pointer conversions (7.3),
4990 : * qualification conversions (7.5),
4991 : * integral promotions (7.6),
4992 : * integral conversions (7.8) other than narrowing conversions (11.6.4),
4993 : * null pointer conversions (7.11) from std::nullptr_t,
4994 : * null member pointer conversions (7.12) from std::nullptr_t, and
4995 : * function pointer conversions (7.13),
4996 :
4997 : and where the reference binding (if any) binds directly. */
4998 :
4999 326966825 : for (conversion *c = conv;
5000 378217563 : c && c->kind != ck_identity;
5001 51250738 : c = next_conversion (c))
5002 : {
5003 51250738 : switch (c->kind)
5004 : {
5005 : /* A conversion function is OK. If it isn't constexpr, we'll
5006 : complain later that the argument isn't constant. */
5007 : case ck_user:
5008 : /* List-initialization is OK. */
5009 : case ck_aggr:
5010 : /* The lvalue-to-rvalue conversion is OK. */
5011 : case ck_rvalue:
5012 : /* Array-to-pointer and function-to-pointer. */
5013 : case ck_lvalue:
5014 : /* Function pointer conversions. */
5015 : case ck_fnptr:
5016 : /* Qualification conversions. */
5017 : case ck_qual:
5018 : break;
5019 :
5020 763 : case ck_ref_bind:
5021 763 : if (c->need_temporary_p)
5022 : {
5023 6 : if (complain & tf_error)
5024 1 : error_at (loc, "initializing %qH with %qI in converted "
5025 : "constant expression does not bind directly",
5026 1 : type, next_conversion (c)->type);
5027 : conv = NULL;
5028 : }
5029 : break;
5030 :
5031 9398306 : case ck_base:
5032 9398306 : case ck_pmem:
5033 9398306 : case ck_ptr:
5034 9398306 : case ck_std:
5035 9398306 : t = next_conversion (c)->type;
5036 9398306 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (t)
5037 9398226 : && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5038 : /* Integral promotion or conversion. */
5039 : break;
5040 114 : if (NULLPTR_TYPE_P (t))
5041 : /* Conversion from nullptr to pointer or pointer-to-member. */
5042 : break;
5043 :
5044 114 : if (complain & tf_error)
5045 82 : error_at (loc, "conversion from %qH to %qI in a "
5046 : "converted constant expression", t, type);
5047 : /* fall through. */
5048 :
5049 : default:
5050 : conv = NULL;
5051 : break;
5052 : }
5053 : }
5054 :
5055 : /* Avoid confusing convert_nontype_argument by introducing
5056 : a redundant conversion to the same reference type. */
5057 326965353 : if (conv && conv->kind == ck_ref_bind
5058 326967582 : && REFERENCE_REF_P (expr))
5059 : {
5060 487 : tree ref = TREE_OPERAND (expr, 0);
5061 487 : if (same_type_p (type, TREE_TYPE (ref)))
5062 : return ref;
5063 : }
5064 :
5065 326966360 : if (conv)
5066 : {
5067 : /* Don't copy a class in a template. */
5068 67409 : if (CLASS_TYPE_P (type) && conv->kind == ck_rvalue
5069 327000039 : && processing_template_decl)
5070 393 : conv = next_conversion (conv);
5071 :
5072 : /* Issuing conversion warnings for value-dependent expressions is
5073 : likely too noisy. */
5074 326964888 : warning_sentinel w (warn_conversion);
5075 326964888 : conv->check_narrowing = true;
5076 326964888 : conv->check_narrowing_const_only = true;
5077 326964888 : expr = convert_like (conv, expr, complain);
5078 326964888 : }
5079 : else
5080 : {
5081 1472 : if (complain & tf_error)
5082 194 : implicit_conversion_error (loc, type, expr, flags);
5083 1472 : expr = error_mark_node;
5084 : }
5085 :
5086 : return expr;
5087 326966825 : }
5088 :
5089 : /* Subroutine of convert_nontype_argument.
5090 :
5091 : EXPR is an expression used in a context that requires a converted
5092 : constant-expression, such as a template non-type parameter. Do any
5093 : necessary conversions (that are permitted for converted
5094 : constant-expressions) to convert it to the desired type.
5095 :
5096 : This function doesn't consider explicit conversion functions. If
5097 : you mean to use "a contextually converted constant expression of type
5098 : bool", use build_converted_constant_bool_expr.
5099 :
5100 : If conversion is successful, returns the converted expression;
5101 : otherwise, returns error_mark_node. */
5102 :
5103 : tree
5104 191312747 : build_converted_constant_expr (tree type, tree expr, tsubst_flags_t complain)
5105 : {
5106 191312747 : return build_converted_constant_expr_internal (type, expr, LOOKUP_IMPLICIT,
5107 191312747 : complain);
5108 : }
5109 :
5110 : /* Used to create "a contextually converted constant expression of type
5111 : bool". This differs from build_converted_constant_expr in that it
5112 : also considers explicit conversion functions. */
5113 :
5114 : tree
5115 135654143 : build_converted_constant_bool_expr (tree expr, tsubst_flags_t complain)
5116 : {
5117 135654143 : return build_converted_constant_expr_internal (boolean_type_node, expr,
5118 135654143 : LOOKUP_NORMAL, complain);
5119 : }
5120 :
5121 : /* Do any initial processing on the arguments to a function call. */
5122 :
5123 : vec<tree, va_gc> *
5124 210380036 : resolve_args (vec<tree, va_gc> *args, tsubst_flags_t complain)
5125 : {
5126 210380036 : unsigned int ix;
5127 210380036 : tree arg;
5128 :
5129 389629712 : FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
5130 : {
5131 179250408 : if (error_operand_p (arg))
5132 : return NULL;
5133 179249858 : else if (VOID_TYPE_P (TREE_TYPE (arg)))
5134 : {
5135 155 : if (complain & tf_error)
5136 12 : error_at (cp_expr_loc_or_input_loc (arg),
5137 : "invalid use of void expression");
5138 : return NULL;
5139 : }
5140 179249703 : else if (invalid_nonstatic_memfn_p (EXPR_LOCATION (arg), arg, complain))
5141 : return NULL;
5142 :
5143 : /* Force auto deduction now. Omit tf_warning to avoid redundant
5144 : deprecated warning on deprecated-14.C. */
5145 179249676 : if (!mark_single_function (arg, complain & ~tf_warning))
5146 : return NULL;
5147 : }
5148 : return args;
5149 : }
5150 :
5151 : /* Perform overload resolution on FN, which is called with the ARGS.
5152 :
5153 : Return the candidate function selected by overload resolution, or
5154 : NULL if the event that overload resolution failed. In the case
5155 : that overload resolution fails, *CANDIDATES will be the set of
5156 : candidates considered, and ANY_VIABLE_P will be set to true or
5157 : false to indicate whether or not any of the candidates were
5158 : viable.
5159 :
5160 : The ARGS should already have gone through RESOLVE_ARGS before this
5161 : function is called. */
5162 :
5163 : static struct z_candidate *
5164 103359375 : perform_overload_resolution (tree fn,
5165 : const vec<tree, va_gc> *args,
5166 : struct z_candidate **candidates,
5167 : bool *any_viable_p, tsubst_flags_t complain)
5168 : {
5169 103359375 : struct z_candidate *cand;
5170 103359375 : tree explicit_targs;
5171 103359375 : int template_only;
5172 :
5173 103359375 : auto_cond_timevar tv (TV_OVERLOAD);
5174 :
5175 103359375 : explicit_targs = NULL_TREE;
5176 103359375 : template_only = 0;
5177 :
5178 103359375 : *candidates = NULL;
5179 103359375 : *any_viable_p = true;
5180 :
5181 : /* Check FN. */
5182 103359375 : gcc_assert (OVL_P (fn) || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
5183 :
5184 103359375 : if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
5185 : {
5186 36311815 : explicit_targs = TREE_OPERAND (fn, 1);
5187 36311815 : fn = TREE_OPERAND (fn, 0);
5188 36311815 : template_only = 1;
5189 : }
5190 :
5191 : /* Add the various candidate functions. */
5192 103359375 : add_candidates (fn, NULL_TREE, args, NULL_TREE,
5193 : explicit_targs, template_only,
5194 : /*conversion_path=*/NULL_TREE,
5195 : /*access_path=*/NULL_TREE,
5196 : LOOKUP_NORMAL,
5197 : candidates, complain & ~tf_any_viable);
5198 :
5199 103345857 : *candidates = splice_viable (*candidates, false, any_viable_p);
5200 103345857 : if (*any_viable_p)
5201 : {
5202 103157245 : if (complain & tf_any_viable)
5203 : cand = *candidates;
5204 : else
5205 103157059 : cand = tourney (*candidates, complain);
5206 : }
5207 : else
5208 : cand = NULL;
5209 :
5210 206691714 : return cand;
5211 103345857 : }
5212 :
5213 : /* Print an error message about being unable to build a call to FN with
5214 : ARGS. ANY_VIABLE_P indicates whether any candidate functions could
5215 : be located; CANDIDATES is a possibly empty list of such
5216 : functions. */
5217 :
5218 : static void
5219 2997 : print_error_for_call_failure (tree fn, const vec<tree, va_gc> *args,
5220 : struct z_candidate *candidates)
5221 : {
5222 2997 : tree targs = NULL_TREE;
5223 2997 : if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
5224 : {
5225 548 : targs = TREE_OPERAND (fn, 1);
5226 548 : fn = TREE_OPERAND (fn, 0);
5227 : }
5228 5994 : tree name = OVL_NAME (fn);
5229 2997 : location_t loc = location_of (name);
5230 2997 : if (targs)
5231 548 : name = lookup_template_function (name, targs);
5232 :
5233 2997 : auto_diagnostic_group d;
5234 5994 : if (!any_strictly_viable (candidates))
5235 2512 : error_at (loc, "no matching function for call to %<%D(%A)%>",
5236 : name, build_tree_list_vec (args));
5237 : else
5238 485 : error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous",
5239 : name, build_tree_list_vec (args));
5240 2997 : if (candidates)
5241 2997 : print_z_candidates (loc, candidates);
5242 2997 : }
5243 :
5244 : /* Perform overload resolution on the set of deduction guides DGUIDES
5245 : using ARGS. Returns the selected deduction guide, or error_mark_node
5246 : if overload resolution fails. */
5247 :
5248 : tree
5249 31194 : perform_dguide_overload_resolution (tree dguides, const vec<tree, va_gc> *args,
5250 : tsubst_flags_t complain)
5251 : {
5252 31194 : z_candidate *candidates;
5253 31194 : bool any_viable_p;
5254 31194 : tree result;
5255 :
5256 62388 : gcc_assert (deduction_guide_p (OVL_FIRST (dguides)));
5257 :
5258 31194 : conversion_obstack_sentinel cos;
5259 :
5260 31194 : z_candidate *cand = perform_overload_resolution (dguides, args, &candidates,
5261 : &any_viable_p, complain);
5262 31194 : if (!cand)
5263 : {
5264 969 : if (complain & tf_error)
5265 198 : print_error_for_call_failure (dguides, args, candidates);
5266 969 : result = error_mark_node;
5267 : }
5268 : else
5269 30225 : result = cand->fn;
5270 :
5271 62388 : return result;
5272 31194 : }
5273 :
5274 : /* Return an expression for a call to FN (a namespace-scope function,
5275 : or a static member function) with the ARGS. This may change
5276 : ARGS. */
5277 :
5278 : tree
5279 102248488 : build_new_function_call (tree fn, vec<tree, va_gc> **args,
5280 : tsubst_flags_t complain)
5281 : {
5282 102248488 : struct z_candidate *candidates, *cand;
5283 102248488 : bool any_viable_p;
5284 102248488 : tree result;
5285 :
5286 102248488 : if (args != NULL && *args != NULL)
5287 : {
5288 102248488 : *args = resolve_args (*args, complain & ~tf_any_viable);
5289 102248488 : if (*args == NULL)
5290 420 : return error_mark_node;
5291 : }
5292 :
5293 102248068 : if (flag_tm)
5294 3544 : tm_malloc_replacement (fn);
5295 :
5296 102248068 : conversion_obstack_sentinel cos;
5297 :
5298 102248068 : cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
5299 : complain);
5300 :
5301 102234550 : if (!cand)
5302 : {
5303 200526 : if (complain & tf_error)
5304 : {
5305 : // If there is a single (non-viable) function candidate,
5306 : // let the error be diagnosed by cp_build_function_call_vec.
5307 3213 : if (!any_viable_p && candidates && ! candidates->next
5308 1428 : && TREE_CODE (candidates->fn) == FUNCTION_DECL
5309 : /* A template-id callee consisting of a single (ignored)
5310 : non-template candidate needs to be diagnosed the
5311 : ordinary way. */
5312 435 : && (TREE_CODE (fn) != TEMPLATE_ID_EXPR
5313 33 : || candidates->template_decl))
5314 423 : return cp_build_function_call_vec (candidates->fn, args, complain);
5315 :
5316 : // Otherwise, emit notes for non-viable candidates.
5317 2790 : print_error_for_call_failure (fn, *args, candidates);
5318 : }
5319 200103 : result = error_mark_node;
5320 : }
5321 102034024 : else if (complain & tf_any_viable)
5322 186 : return void_node;
5323 : else
5324 102033838 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
5325 :
5326 102233941 : if (flag_coroutines
5327 100129316 : && result
5328 100129316 : && TREE_CODE (result) == CALL_EXPR
5329 163283496 : && DECL_BUILT_IN_CLASS (TREE_OPERAND (CALL_EXPR_FN (result), 0))
5330 61049555 : == BUILT_IN_NORMAL)
5331 8145060 : result = coro_validate_builtin_call (result);
5332 :
5333 : return result;
5334 102234550 : }
5335 :
5336 : /* Build a call to a global operator new. FNNAME is the name of the
5337 : operator (either "operator new" or "operator new[]") and ARGS are
5338 : the arguments provided. This may change ARGS. *SIZE points to the
5339 : total number of bytes required by the allocation, and is updated if
5340 : that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
5341 : be used. If this function determines that no cookie should be
5342 : used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK
5343 : is not NULL_TREE, it is evaluated before calculating the final
5344 : array size, and if it fails, the array size is replaced with
5345 : (size_t)-1 (usually triggering a std::bad_alloc exception). If FN
5346 : is non-NULL, it will be set, upon return, to the allocation
5347 : function called. */
5348 :
5349 : tree
5350 1080090 : build_operator_new_call (tree fnname, vec<tree, va_gc> **args,
5351 : tree *size, tree *cookie_size,
5352 : tree align_arg, tree size_check,
5353 : tree *fn, tsubst_flags_t complain)
5354 : {
5355 1080090 : tree original_size = *size;
5356 1080090 : tree fns;
5357 1080090 : struct z_candidate *candidates;
5358 1080090 : struct z_candidate *cand = NULL;
5359 1080090 : bool any_viable_p;
5360 :
5361 1080090 : if (fn)
5362 1078639 : *fn = NULL_TREE;
5363 : /* Set to (size_t)-1 if the size check fails. */
5364 1080090 : if (size_check != NULL_TREE)
5365 : {
5366 21055 : tree errval = TYPE_MAX_VALUE (sizetype);
5367 21055 : if (cxx_dialect >= cxx11 && flag_exceptions)
5368 20646 : errval = throw_bad_array_new_length ();
5369 21055 : *size = fold_build3 (COND_EXPR, sizetype, size_check,
5370 : original_size, errval);
5371 : }
5372 1080090 : vec_safe_insert (*args, 0, *size);
5373 1080090 : *args = resolve_args (*args, complain);
5374 1080090 : if (*args == NULL)
5375 0 : return error_mark_node;
5376 :
5377 1080090 : conversion_obstack_sentinel cos;
5378 :
5379 : /* Based on:
5380 :
5381 : [expr.new]
5382 :
5383 : If this lookup fails to find the name, or if the allocated type
5384 : is not a class type, the allocation function's name is looked
5385 : up in the global scope.
5386 :
5387 : we disregard block-scope declarations of "operator new". */
5388 1080090 : fns = lookup_qualified_name (global_namespace, fnname);
5389 :
5390 1080090 : if (align_arg)
5391 : {
5392 9387 : vec<tree, va_gc>* align_args
5393 9387 : = vec_copy_and_insert (*args, align_arg, 1);
5394 9387 : cand = perform_overload_resolution (fns, align_args, &candidates,
5395 : &any_viable_p, tf_none);
5396 9387 : if (cand)
5397 9364 : *args = align_args;
5398 : /* If no aligned allocation function matches, try again without the
5399 : alignment. */
5400 : }
5401 :
5402 : /* Figure out what function is being called. */
5403 9364 : if (!cand)
5404 1070726 : cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p,
5405 : complain);
5406 :
5407 : /* If no suitable function could be found, issue an error message
5408 : and give up. */
5409 1080090 : if (!cand)
5410 : {
5411 9 : if (complain & tf_error)
5412 9 : print_error_for_call_failure (fns, *args, candidates);
5413 9 : return error_mark_node;
5414 : }
5415 :
5416 : /* If a cookie is required, add some extra space. Whether
5417 : or not a cookie is required cannot be determined until
5418 : after we know which function was called. */
5419 1080081 : if (*cookie_size)
5420 : {
5421 268 : bool use_cookie = true;
5422 268 : tree arg_types;
5423 :
5424 268 : arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
5425 : /* Skip the size_t parameter. */
5426 268 : arg_types = TREE_CHAIN (arg_types);
5427 : /* Check the remaining parameters (if any). */
5428 268 : if (arg_types
5429 268 : && TREE_CHAIN (arg_types) == void_list_node
5430 331 : && same_type_p (TREE_VALUE (arg_types),
5431 : ptr_type_node))
5432 48 : use_cookie = false;
5433 : /* If we need a cookie, adjust the number of bytes allocated. */
5434 268 : if (use_cookie)
5435 : {
5436 : /* Update the total size. */
5437 220 : *size = size_binop (PLUS_EXPR, original_size, *cookie_size);
5438 220 : if (size_check)
5439 : {
5440 : /* Set to (size_t)-1 if the size check fails. */
5441 47 : gcc_assert (size_check != NULL_TREE);
5442 47 : *size = fold_build3 (COND_EXPR, sizetype, size_check,
5443 : *size, TYPE_MAX_VALUE (sizetype));
5444 : }
5445 : /* Update the argument list to reflect the adjusted size. */
5446 220 : (**args)[0] = *size;
5447 : }
5448 : else
5449 48 : *cookie_size = NULL_TREE;
5450 : }
5451 :
5452 : /* Tell our caller which function we decided to call. */
5453 1080081 : if (fn)
5454 1078630 : *fn = cand->fn;
5455 :
5456 : /* Build the CALL_EXPR. */
5457 1080081 : tree ret = build_over_call (cand, LOOKUP_NORMAL, complain);
5458 :
5459 : /* Set this flag for all callers of this function. In addition to
5460 : new-expressions, this is called for allocating coroutine state; treat
5461 : that as an implicit new-expression. */
5462 1080081 : tree call = extract_call_expr (ret);
5463 1080081 : if (TREE_CODE (call) == CALL_EXPR)
5464 1080081 : CALL_FROM_NEW_OR_DELETE_P (call) = 1;
5465 :
5466 : return ret;
5467 1080090 : }
5468 :
5469 : /* Evaluate side-effects from OBJ before evaluating call
5470 : to FN in RESULT expression.
5471 : This is for expressions of the form `obj->fn(...)'
5472 : where `fn' turns out to be a static member function and
5473 : `obj' needs to be evaluated. `fn' could be also static operator[]
5474 : or static operator(), in which cases the source expression
5475 : would be `obj[...]' or `obj(...)'. */
5476 :
5477 : tree
5478 80245862 : keep_unused_object_arg (tree result, tree obj, tree fn)
5479 : {
5480 80245862 : if (result == NULL_TREE
5481 80245862 : || result == error_mark_node
5482 79244949 : || DECL_OBJECT_MEMBER_FUNCTION_P (fn)
5483 82979605 : || !TREE_SIDE_EFFECTS (obj))
5484 : return result;
5485 :
5486 : /* But avoid the implicit lvalue-rvalue conversion when `obj' is
5487 : volatile. */
5488 89085 : tree a = obj;
5489 89085 : if (TREE_THIS_VOLATILE (a))
5490 57840 : a = build_this (a);
5491 89085 : if (TREE_SIDE_EFFECTS (a))
5492 31254 : return cp_build_compound_expr (a, result, tf_error);
5493 : return result;
5494 : }
5495 :
5496 : /* Build a new call to operator(). This may change ARGS. */
5497 :
5498 : tree
5499 2632704 : build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
5500 : {
5501 2632704 : struct z_candidate *candidates = 0, *cand;
5502 2632704 : tree fns, convs, first_mem_arg = NULL_TREE;
5503 2632704 : bool any_viable_p;
5504 2632704 : tree result = NULL_TREE;
5505 :
5506 2632704 : auto_cond_timevar tv (TV_OVERLOAD);
5507 :
5508 2632704 : obj = mark_lvalue_use (obj);
5509 :
5510 2632704 : if (error_operand_p (obj))
5511 0 : return error_mark_node;
5512 :
5513 2632704 : tree type = TREE_TYPE (obj);
5514 :
5515 2632704 : obj = prep_operand (obj);
5516 :
5517 2632704 : if (TYPE_PTRMEMFUNC_P (type))
5518 : {
5519 0 : if (complain & tf_error)
5520 : /* It's no good looking for an overloaded operator() on a
5521 : pointer-to-member-function. */
5522 0 : error ("pointer-to-member function %qE cannot be called without "
5523 : "an object; consider using %<.*%> or %<->*%>", obj);
5524 0 : return error_mark_node;
5525 : }
5526 :
5527 2632704 : if (TYPE_BINFO (type))
5528 : {
5529 2632680 : fns = lookup_fnfields (TYPE_BINFO (type), call_op_identifier, 1, complain);
5530 2632680 : if (fns == error_mark_node)
5531 : return error_mark_node;
5532 : }
5533 : else
5534 : fns = NULL_TREE;
5535 :
5536 2632694 : if (args != NULL && *args != NULL)
5537 : {
5538 2632694 : *args = resolve_args (*args, complain);
5539 2632694 : if (*args == NULL)
5540 110 : return error_mark_node;
5541 : }
5542 :
5543 2632584 : conversion_obstack_sentinel cos;
5544 :
5545 2632584 : if (fns)
5546 : {
5547 2627235 : first_mem_arg = obj;
5548 :
5549 2627235 : add_candidates (BASELINK_FUNCTIONS (fns),
5550 : first_mem_arg, *args, NULL_TREE,
5551 : NULL_TREE, false,
5552 2627235 : BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
5553 : LOOKUP_NORMAL, &candidates, complain);
5554 : }
5555 :
5556 2632584 : bool any_call_ops = candidates != nullptr;
5557 :
5558 2632584 : convs = lookup_conversions (type);
5559 :
5560 5457215 : for (; convs; convs = TREE_CHAIN (convs))
5561 : {
5562 192047 : tree totype = TREE_TYPE (convs);
5563 :
5564 158505 : if (TYPE_PTRFN_P (totype)
5565 33545 : || TYPE_REFFN_P (totype)
5566 225548 : || (TYPE_REF_P (totype)
5567 1188 : && TYPE_PTRFN_P (TREE_TYPE (totype))))
5568 317534 : for (tree fn : ovl_range (TREE_VALUE (convs)))
5569 : {
5570 158767 : if (DECL_NONCONVERTING_P (fn))
5571 3 : continue;
5572 :
5573 158764 : if (TREE_CODE (fn) == TEMPLATE_DECL)
5574 : {
5575 : /* Making this work broke PR 71117 and 85118, so until the
5576 : committee resolves core issue 2189, let's disable this
5577 : candidate if there are any call operators. */
5578 39570 : if (any_call_ops)
5579 39558 : continue;
5580 :
5581 12 : add_template_conv_candidate
5582 12 : (&candidates, fn, obj, *args, totype,
5583 : /*access_path=*/NULL_TREE,
5584 : /*conversion_path=*/NULL_TREE, complain);
5585 : }
5586 : else
5587 119194 : add_conv_candidate (&candidates, fn, obj,
5588 : *args, /*conversion_path=*/NULL_TREE,
5589 : /*access_path=*/NULL_TREE, complain);
5590 : }
5591 : }
5592 :
5593 : /* Be strict here because if we choose a bad conversion candidate, the
5594 : errors we get won't mention the call context. */
5595 2632584 : candidates = splice_viable (candidates, true, &any_viable_p);
5596 2632584 : if (!any_viable_p)
5597 : {
5598 40102 : if (complain & tf_error)
5599 : {
5600 282 : auto_diagnostic_group d;
5601 282 : error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
5602 : build_tree_list_vec (*args));
5603 282 : print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
5604 282 : }
5605 40102 : result = error_mark_node;
5606 : }
5607 : else
5608 : {
5609 2592482 : cand = tourney (candidates, complain);
5610 2592482 : if (cand == 0)
5611 : {
5612 22 : if (complain & tf_error)
5613 : {
5614 6 : auto_diagnostic_group d;
5615 12 : error ("call of %<(%T) (%A)%> is ambiguous",
5616 6 : TREE_TYPE (obj), build_tree_list_vec (*args));
5617 6 : print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
5618 6 : }
5619 22 : result = error_mark_node;
5620 : }
5621 2592460 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL
5622 2592401 : && DECL_OVERLOADED_OPERATOR_P (cand->fn)
5623 5184861 : && DECL_OVERLOADED_OPERATOR_IS (cand->fn, CALL_EXPR))
5624 : {
5625 2592401 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
5626 : /* In an expression of the form `a()' where cand->fn
5627 : which is operator() turns out to be a static member function,
5628 : `a' is none-the-less evaluated. */
5629 2592401 : result = keep_unused_object_arg (result, obj, cand->fn);
5630 : }
5631 : else
5632 : {
5633 59 : if (TREE_CODE (cand->fn) == FUNCTION_DECL)
5634 0 : obj = convert_like_with_context (cand->convs[0], obj, cand->fn,
5635 : -1, complain);
5636 : else
5637 : {
5638 59 : gcc_checking_assert (TYPE_P (cand->fn));
5639 59 : obj = convert_like (cand->convs[0], obj, complain);
5640 : }
5641 59 : obj = convert_from_reference (obj);
5642 59 : result = cp_build_function_call_vec (obj, args, complain);
5643 : }
5644 : }
5645 :
5646 2632584 : return result;
5647 2632704 : }
5648 :
5649 : /* Subroutine for preparing format strings suitable for the error
5650 : function. It concatenates a prefix (controlled by MATCH), ERRMSG,
5651 : and SUFFIX. */
5652 :
5653 : static const char *
5654 1466 : concat_op_error_string (bool match, const char *errmsg, const char *suffix)
5655 : {
5656 1466 : return concat (match
5657 : ? G_("ambiguous overload for ")
5658 : : G_("no match for "),
5659 0 : errmsg, suffix, nullptr);
5660 : }
5661 :
5662 : /* Called by op_error to prepare format strings suitable for the error
5663 : function. It concatenates a prefix (controlled by MATCH), ERRMSG,
5664 : and a suffix (controlled by NTYPES). */
5665 :
5666 : static const char *
5667 1445 : op_error_string (const char *errmsg, int ntypes, bool match)
5668 : {
5669 1445 : const char *suffix;
5670 0 : if (ntypes == 3)
5671 : suffix = G_(" (operand types are %qT, %qT, and %qT)");
5672 0 : else if (ntypes == 2)
5673 : suffix = G_(" (operand types are %qT and %qT)");
5674 : else
5675 0 : suffix = G_(" (operand type is %qT)");
5676 0 : return concat_op_error_string (match, errmsg, suffix);
5677 : }
5678 :
5679 : /* Similar to op_error_string, but a special-case for binary ops that
5680 : use %e for the args, rather than %qT. */
5681 :
5682 : static const char *
5683 21 : binop_error_string (const char *errmsg, bool match)
5684 : {
5685 21 : return concat_op_error_string (match, errmsg,
5686 21 : G_(" (operand types are %e and %e)"));
5687 : }
5688 :
5689 : static void
5690 1466 : op_error (const op_location_t &loc,
5691 : enum tree_code code, enum tree_code code2,
5692 : tree arg1, tree arg2, tree arg3, bool match)
5693 : {
5694 1466 : bool assop = code == MODIFY_EXPR;
5695 1466 : const char *opname = OVL_OP_INFO (assop, assop ? code2 : code)->name;
5696 :
5697 1466 : switch (code)
5698 : {
5699 0 : case COND_EXPR:
5700 0 : if (flag_diagnostics_show_caret)
5701 0 : error_at (loc, op_error_string (G_("ternary %<operator?:%>"),
5702 : 3, match),
5703 0 : TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
5704 : else
5705 0 : error_at (loc, op_error_string (G_("ternary %<operator?:%> "
5706 : "in %<%E ? %E : %E%>"), 3, match),
5707 : arg1, arg2, arg3,
5708 0 : TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
5709 : break;
5710 :
5711 0 : case POSTINCREMENT_EXPR:
5712 0 : case POSTDECREMENT_EXPR:
5713 0 : if (flag_diagnostics_show_caret)
5714 0 : error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
5715 0 : opname, TREE_TYPE (arg1));
5716 : else
5717 0 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
5718 : 1, match),
5719 0 : opname, arg1, opname, TREE_TYPE (arg1));
5720 : break;
5721 :
5722 13 : case ARRAY_REF:
5723 13 : if (flag_diagnostics_show_caret)
5724 0 : error_at (loc, op_error_string (G_("%<operator[]%>"), 2, match),
5725 0 : TREE_TYPE (arg1), TREE_TYPE (arg2));
5726 : else
5727 26 : error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
5728 : 2, match),
5729 13 : arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2));
5730 : break;
5731 :
5732 6 : case REALPART_EXPR:
5733 6 : case IMAGPART_EXPR:
5734 6 : if (flag_diagnostics_show_caret)
5735 0 : error_at (loc, op_error_string (G_("%qs"), 1, match),
5736 0 : opname, TREE_TYPE (arg1));
5737 : else
5738 12 : error_at (loc, op_error_string (G_("%qs in %<%s %E%>"), 1, match),
5739 6 : opname, opname, arg1, TREE_TYPE (arg1));
5740 : break;
5741 :
5742 0 : case CO_AWAIT_EXPR:
5743 0 : if (flag_diagnostics_show_caret)
5744 0 : error_at (loc, op_error_string (G_("%<operator %s%>"), 1, match),
5745 0 : opname, TREE_TYPE (arg1));
5746 : else
5747 0 : error_at (loc, op_error_string (G_("%<operator %s%> in %<%s%E%>"),
5748 : 1, match),
5749 0 : opname, opname, arg1, TREE_TYPE (arg1));
5750 : break;
5751 :
5752 1447 : default:
5753 1447 : if (arg2)
5754 1348 : if (flag_diagnostics_show_caret)
5755 : {
5756 21 : binary_op_rich_location richloc (loc, arg1, arg2, true);
5757 21 : pp_markup::element_quoted_type element_0
5758 21 : (TREE_TYPE (arg1), highlight_colors::lhs);
5759 21 : pp_markup::element_quoted_type element_1
5760 21 : (TREE_TYPE (arg2), highlight_colors::rhs);
5761 21 : error_at (&richloc,
5762 : binop_error_string (G_("%<operator%s%>"), match),
5763 : opname, &element_0, &element_1);
5764 21 : }
5765 : else
5766 2654 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
5767 : 2, match),
5768 : opname, arg1, opname, arg2,
5769 1327 : TREE_TYPE (arg1), TREE_TYPE (arg2));
5770 : else
5771 99 : if (flag_diagnostics_show_caret)
5772 0 : error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
5773 0 : opname, TREE_TYPE (arg1));
5774 : else
5775 198 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
5776 : 1, match),
5777 99 : opname, opname, arg1, TREE_TYPE (arg1));
5778 : break;
5779 : }
5780 1466 : }
5781 :
5782 : /* Return the implicit conversion sequence that could be used to
5783 : convert E1 to E2 in [expr.cond]. */
5784 :
5785 : static conversion *
5786 517070 : conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
5787 : {
5788 517070 : tree t1 = non_reference (TREE_TYPE (e1));
5789 517070 : tree t2 = non_reference (TREE_TYPE (e2));
5790 517070 : conversion *conv;
5791 517070 : bool good_base;
5792 :
5793 : /* [expr.cond]
5794 :
5795 : If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
5796 : implicitly converted (clause _conv_) to the type "lvalue reference to
5797 : T2", subject to the constraint that in the conversion the
5798 : reference must bind directly (_dcl.init.ref_) to an lvalue.
5799 :
5800 : If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
5801 : implicitly converted to the type "rvalue reference to T2", subject to
5802 : the constraint that the reference must bind directly. */
5803 517070 : if (glvalue_p (e2))
5804 : {
5805 507106 : tree rtype = cp_build_reference_type (t2, !lvalue_p (e2));
5806 507106 : conv = implicit_conversion (rtype,
5807 : t1,
5808 : e1,
5809 : /*c_cast_p=*/false,
5810 : LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
5811 : |LOOKUP_ONLYCONVERTING,
5812 : complain);
5813 507106 : if (conv && !conv->bad_p)
5814 : return conv;
5815 : }
5816 :
5817 : /* If E2 is a prvalue or if neither of the conversions above can be done
5818 : and at least one of the operands has (possibly cv-qualified) class
5819 : type: */
5820 407084 : if (!CLASS_TYPE_P (t1) && !CLASS_TYPE_P (t2))
5821 : return NULL;
5822 :
5823 : /* [expr.cond]
5824 :
5825 : If E1 and E2 have class type, and the underlying class types are
5826 : the same or one is a base class of the other: E1 can be converted
5827 : to match E2 if the class of T2 is the same type as, or a base
5828 : class of, the class of T1, and the cv-qualification of T2 is the
5829 : same cv-qualification as, or a greater cv-qualification than, the
5830 : cv-qualification of T1. If the conversion is applied, E1 is
5831 : changed to an rvalue of type T2 that still refers to the original
5832 : source class object (or the appropriate subobject thereof). */
5833 160981 : if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
5834 322069 : && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
5835 : {
5836 19595 : if (good_base && at_least_as_qualified_p (t2, t1))
5837 : {
5838 9686 : conv = build_identity_conv (t1, e1);
5839 9686 : if (!same_type_p (TYPE_MAIN_VARIANT (t1),
5840 : TYPE_MAIN_VARIANT (t2)))
5841 6 : conv = build_conv (ck_base, t2, conv);
5842 : else
5843 9680 : conv = build_conv (ck_rvalue, t2, conv);
5844 : return conv;
5845 : }
5846 : else
5847 : return NULL;
5848 : }
5849 : else
5850 : /* [expr.cond]
5851 :
5852 : Otherwise: E1 can be converted to match E2 if E1 can be implicitly
5853 : converted to the type that expression E2 would have if E2 were
5854 : converted to an rvalue (or the type it has, if E2 is an rvalue). */
5855 277856 : return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
5856 277856 : LOOKUP_IMPLICIT, complain);
5857 : }
5858 :
5859 : /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
5860 : arguments to the conditional expression. */
5861 :
5862 : tree
5863 7961375 : build_conditional_expr (const op_location_t &loc,
5864 : tree arg1, tree arg2, tree arg3,
5865 : tsubst_flags_t complain)
5866 : {
5867 7961375 : tree arg2_type;
5868 7961375 : tree arg3_type;
5869 7961375 : tree result = NULL_TREE;
5870 7961375 : tree result_type = NULL_TREE;
5871 7961375 : tree semantic_result_type = NULL_TREE;
5872 7961375 : bool is_glvalue = true;
5873 7961375 : struct z_candidate *candidates = 0;
5874 7961375 : struct z_candidate *cand;
5875 7961375 : tree orig_arg2, orig_arg3;
5876 :
5877 7961375 : auto_cond_timevar tv (TV_OVERLOAD);
5878 :
5879 : /* As a G++ extension, the second argument to the conditional can be
5880 : omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
5881 : c'.) If the second operand is omitted, make sure it is
5882 : calculated only once. */
5883 7961375 : if (!arg2)
5884 : {
5885 386 : if (complain & tf_error)
5886 380 : pedwarn (loc, OPT_Wpedantic,
5887 : "ISO C++ forbids omitting the middle term of "
5888 : "a %<?:%> expression");
5889 :
5890 386 : if ((complain & tf_warning) && !truth_value_p (TREE_CODE (arg1)))
5891 338 : warn_for_omitted_condop (loc, arg1);
5892 :
5893 : /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
5894 386 : if (glvalue_p (arg1))
5895 : {
5896 95 : arg1 = cp_stabilize_reference (arg1);
5897 95 : arg2 = arg1 = prevent_lifetime_extension (arg1);
5898 : }
5899 291 : else if (TREE_CODE (arg1) == TARGET_EXPR)
5900 : /* arg1 can't be a prvalue result of the conditional
5901 : expression, since it needs to be materialized for the
5902 : conversion to bool, so treat it as an xvalue in arg2. */
5903 6 : arg2 = move (TARGET_EXPR_SLOT (arg1));
5904 285 : else if (TREE_CODE (arg1) == EXCESS_PRECISION_EXPR)
5905 3 : arg2 = arg1 = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (arg1),
5906 3 : cp_save_expr (TREE_OPERAND (arg1, 0)));
5907 : else
5908 282 : arg2 = arg1 = cp_save_expr (arg1);
5909 : }
5910 :
5911 : /* If something has already gone wrong, just pass that fact up the
5912 : tree. */
5913 7961375 : if (error_operand_p (arg1)
5914 7961296 : || error_operand_p (arg2)
5915 15922621 : || error_operand_p (arg3))
5916 180 : return error_mark_node;
5917 :
5918 7961195 : conversion_obstack_sentinel cos;
5919 :
5920 7961195 : orig_arg2 = arg2;
5921 7961195 : orig_arg3 = arg3;
5922 :
5923 7961195 : if (gnu_vector_type_p (TREE_TYPE (arg1))
5924 7961195 : && (VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1))
5925 3 : || VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (arg1))))
5926 : {
5927 1860 : tree arg1_type = TREE_TYPE (arg1);
5928 :
5929 : /* If arg1 is another cond_expr choosing between -1 and 0,
5930 : then we can use its comparison. It may help to avoid
5931 : additional comparison, produce more accurate diagnostics
5932 : and enables folding. */
5933 1860 : if (TREE_CODE (arg1) == VEC_COND_EXPR
5934 1587 : && integer_minus_onep (TREE_OPERAND (arg1, 1))
5935 3447 : && integer_zerop (TREE_OPERAND (arg1, 2)))
5936 1587 : arg1 = TREE_OPERAND (arg1, 0);
5937 :
5938 1860 : arg1 = force_rvalue (arg1, complain);
5939 1860 : arg2 = force_rvalue (arg2, complain);
5940 1860 : arg3 = force_rvalue (arg3, complain);
5941 :
5942 : /* force_rvalue can return error_mark on valid arguments. */
5943 1860 : if (error_operand_p (arg1)
5944 1860 : || error_operand_p (arg2)
5945 3720 : || error_operand_p (arg3))
5946 0 : return error_mark_node;
5947 :
5948 1860 : arg2_type = TREE_TYPE (arg2);
5949 1860 : arg3_type = TREE_TYPE (arg3);
5950 :
5951 1860 : if (!VECTOR_TYPE_P (arg2_type)
5952 559 : && !VECTOR_TYPE_P (arg3_type))
5953 : {
5954 : /* Rely on the error messages of the scalar version. */
5955 541 : tree scal = build_conditional_expr (loc, integer_one_node,
5956 : orig_arg2, orig_arg3, complain);
5957 541 : if (scal == error_mark_node)
5958 : return error_mark_node;
5959 541 : tree stype = TREE_TYPE (scal);
5960 541 : tree ctype = TREE_TYPE (arg1_type);
5961 541 : if (TYPE_SIZE (stype) != TYPE_SIZE (ctype)
5962 541 : || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype)))
5963 : {
5964 9 : if (complain & tf_error)
5965 9 : error_at (loc, "inferred scalar type %qT is not an integer or "
5966 : "floating-point type of the same size as %qT", stype,
5967 9 : COMPARISON_CLASS_P (arg1)
5968 9 : ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0)))
5969 : : ctype);
5970 9 : return error_mark_node;
5971 : }
5972 :
5973 532 : tree vtype = build_opaque_vector_type (stype,
5974 532 : TYPE_VECTOR_SUBPARTS (arg1_type));
5975 : /* We could pass complain & tf_warning to unsafe_conversion_p,
5976 : but the warnings (like Wsign-conversion) have already been
5977 : given by the scalar build_conditional_expr_1. We still check
5978 : unsafe_conversion_p to forbid truncating long long -> float. */
5979 532 : if (unsafe_conversion_p (stype, arg2, NULL_TREE, false))
5980 : {
5981 0 : if (complain & tf_error)
5982 0 : error_at (loc, "conversion of scalar %qH to vector %qI "
5983 : "involves truncation", arg2_type, vtype);
5984 0 : return error_mark_node;
5985 : }
5986 532 : if (unsafe_conversion_p (stype, arg3, NULL_TREE, false))
5987 : {
5988 3 : if (complain & tf_error)
5989 3 : error_at (loc, "conversion of scalar %qH to vector %qI "
5990 : "involves truncation", arg3_type, vtype);
5991 3 : return error_mark_node;
5992 : }
5993 :
5994 529 : arg2 = cp_convert (stype, arg2, complain);
5995 529 : arg2 = save_expr (arg2);
5996 529 : arg2 = build_vector_from_val (vtype, arg2);
5997 529 : arg2_type = vtype;
5998 529 : arg3 = cp_convert (stype, arg3, complain);
5999 529 : arg3 = save_expr (arg3);
6000 529 : arg3 = build_vector_from_val (vtype, arg3);
6001 529 : arg3_type = vtype;
6002 : }
6003 :
6004 3678 : if ((gnu_vector_type_p (arg2_type) && !VECTOR_TYPE_P (arg3_type))
6005 3600 : || (gnu_vector_type_p (arg3_type) && !VECTOR_TYPE_P (arg2_type)))
6006 : {
6007 96 : enum stv_conv convert_flag =
6008 96 : scalar_to_vector (loc, VEC_COND_EXPR, arg2, arg3,
6009 : complain & tf_error);
6010 :
6011 96 : switch (convert_flag)
6012 : {
6013 0 : case stv_error:
6014 0 : return error_mark_node;
6015 15 : case stv_firstarg:
6016 15 : {
6017 15 : arg2 = save_expr (arg2);
6018 15 : arg2 = convert (TREE_TYPE (arg3_type), arg2);
6019 15 : arg2 = build_vector_from_val (arg3_type, arg2);
6020 15 : arg2_type = TREE_TYPE (arg2);
6021 15 : break;
6022 : }
6023 72 : case stv_secondarg:
6024 72 : {
6025 72 : arg3 = save_expr (arg3);
6026 72 : arg3 = convert (TREE_TYPE (arg2_type), arg3);
6027 72 : arg3 = build_vector_from_val (arg2_type, arg3);
6028 72 : arg3_type = TREE_TYPE (arg3);
6029 72 : break;
6030 : }
6031 : default:
6032 : break;
6033 : }
6034 : }
6035 :
6036 1848 : if (!gnu_vector_type_p (arg2_type)
6037 1845 : || !gnu_vector_type_p (arg3_type)
6038 1839 : || !same_type_p (arg2_type, arg3_type)
6039 1839 : || maybe_ne (TYPE_VECTOR_SUBPARTS (arg1_type),
6040 1848 : TYPE_VECTOR_SUBPARTS (arg2_type))
6041 3687 : || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type))
6042 : {
6043 9 : if (complain & tf_error)
6044 6 : error_at (loc,
6045 : "incompatible vector types in conditional expression: "
6046 6 : "%qT, %qT and %qT", TREE_TYPE (arg1),
6047 6 : TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3));
6048 9 : return error_mark_node;
6049 : }
6050 :
6051 1839 : if (!COMPARISON_CLASS_P (arg1))
6052 : {
6053 270 : tree cmp_type = truth_type_for (arg1_type);
6054 270 : arg1 = build2 (NE_EXPR, cmp_type, arg1, build_zero_cst (arg1_type));
6055 : }
6056 1839 : return build3_loc (loc, VEC_COND_EXPR, arg2_type, arg1, arg2, arg3);
6057 : }
6058 :
6059 : /* [expr.cond]
6060 :
6061 : The first expression is implicitly converted to bool (clause
6062 : _conv_). */
6063 7959335 : arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
6064 : LOOKUP_NORMAL);
6065 7959335 : if (error_operand_p (arg1))
6066 25 : return error_mark_node;
6067 :
6068 7959310 : arg2_type = unlowered_expr_type (arg2);
6069 7959310 : arg3_type = unlowered_expr_type (arg3);
6070 :
6071 7959310 : if ((TREE_CODE (arg2) == EXCESS_PRECISION_EXPR
6072 7959292 : || TREE_CODE (arg3) == EXCESS_PRECISION_EXPR)
6073 45 : && (TREE_CODE (arg2_type) == INTEGER_TYPE
6074 : || SCALAR_FLOAT_TYPE_P (arg2_type)
6075 : || TREE_CODE (arg2_type) == COMPLEX_TYPE)
6076 45 : && (TREE_CODE (arg3_type) == INTEGER_TYPE
6077 : || SCALAR_FLOAT_TYPE_P (arg3_type)
6078 : || TREE_CODE (arg3_type) == COMPLEX_TYPE))
6079 : {
6080 45 : semantic_result_type
6081 45 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
6082 45 : if (semantic_result_type == error_mark_node)
6083 : {
6084 0 : tree t1 = arg2_type;
6085 0 : tree t2 = arg3_type;
6086 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6087 0 : t1 = TREE_TYPE (t1);
6088 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6089 0 : t2 = TREE_TYPE (t2);
6090 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
6091 : && SCALAR_FLOAT_TYPE_P (t2)
6092 : && (extended_float_type_p (t1)
6093 : || extended_float_type_p (t2))
6094 : && cp_compare_floating_point_conversion_ranks
6095 : (t1, t2) == 3);
6096 0 : if (complain & tf_error)
6097 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
6098 : "have unordered conversion rank",
6099 : arg2_type, arg3_type);
6100 0 : return error_mark_node;
6101 : }
6102 45 : if (TREE_CODE (arg2) == EXCESS_PRECISION_EXPR)
6103 : {
6104 18 : arg2 = TREE_OPERAND (arg2, 0);
6105 18 : arg2_type = TREE_TYPE (arg2);
6106 : }
6107 45 : if (TREE_CODE (arg3) == EXCESS_PRECISION_EXPR)
6108 : {
6109 27 : arg3 = TREE_OPERAND (arg3, 0);
6110 27 : arg3_type = TREE_TYPE (arg3);
6111 : }
6112 : }
6113 :
6114 : /* [expr.cond]
6115 :
6116 : If either the second or the third operand has type (possibly
6117 : cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
6118 : array-to-pointer (_conv.array_), and function-to-pointer
6119 : (_conv.func_) standard conversions are performed on the second
6120 : and third operands. */
6121 7959310 : if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
6122 : {
6123 : /* 'void' won't help in resolving an overloaded expression on the
6124 : other side, so require it to resolve by itself. */
6125 128290 : if (arg2_type == unknown_type_node)
6126 : {
6127 18 : arg2 = resolve_nondeduced_context_or_error (arg2, complain);
6128 18 : arg2_type = TREE_TYPE (arg2);
6129 : }
6130 128290 : if (arg3_type == unknown_type_node)
6131 : {
6132 0 : arg3 = resolve_nondeduced_context_or_error (arg3, complain);
6133 0 : arg3_type = TREE_TYPE (arg3);
6134 : }
6135 :
6136 : /* [expr.cond]
6137 :
6138 : One of the following shall hold:
6139 :
6140 : --The second or the third operand (but not both) is a
6141 : throw-expression (_except.throw_); the result is of the type
6142 : and value category of the other.
6143 :
6144 : --Both the second and the third operands have type void; the
6145 : result is of type void and is a prvalue. */
6146 128290 : if (TREE_CODE (arg2) == THROW_EXPR
6147 84 : && TREE_CODE (arg3) != THROW_EXPR)
6148 : {
6149 72 : result_type = arg3_type;
6150 72 : is_glvalue = glvalue_p (arg3);
6151 : }
6152 128218 : else if (TREE_CODE (arg2) != THROW_EXPR
6153 128206 : && TREE_CODE (arg3) == THROW_EXPR)
6154 : {
6155 180 : result_type = arg2_type;
6156 180 : is_glvalue = glvalue_p (arg2);
6157 : }
6158 128038 : else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
6159 : {
6160 128009 : result_type = void_type_node;
6161 128009 : is_glvalue = false;
6162 : }
6163 : else
6164 : {
6165 29 : if (complain & tf_error)
6166 : {
6167 18 : if (VOID_TYPE_P (arg2_type))
6168 9 : error_at (cp_expr_loc_or_loc (arg3, loc),
6169 : "second operand to the conditional operator "
6170 : "is of type %<void%>, but the third operand is "
6171 : "neither a throw-expression nor of type %<void%>");
6172 : else
6173 9 : error_at (cp_expr_loc_or_loc (arg2, loc),
6174 : "third operand to the conditional operator "
6175 : "is of type %<void%>, but the second operand is "
6176 : "neither a throw-expression nor of type %<void%>");
6177 : }
6178 29 : return error_mark_node;
6179 : }
6180 :
6181 128261 : goto valid_operands;
6182 : }
6183 : /* [expr.cond]
6184 :
6185 : Otherwise, if the second and third operand have different types,
6186 : and either has (possibly cv-qualified) class type, or if both are
6187 : glvalues of the same value category and the same type except for
6188 : cv-qualification, an attempt is made to convert each of those operands
6189 : to the type of the other. */
6190 7831020 : else if (!same_type_p (arg2_type, arg3_type)
6191 7831020 : && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)
6192 1573685 : || (same_type_ignoring_top_level_qualifiers_p (arg2_type,
6193 : arg3_type)
6194 448523 : && glvalue_p (arg2) && glvalue_p (arg3)
6195 109703 : && lvalue_p (arg2) == lvalue_p (arg3))))
6196 : {
6197 258535 : conversion *conv2;
6198 258535 : conversion *conv3;
6199 258535 : bool converted = false;
6200 :
6201 258535 : conv2 = conditional_conversion (arg2, arg3, complain);
6202 258535 : conv3 = conditional_conversion (arg3, arg2, complain);
6203 :
6204 : /* [expr.cond]
6205 :
6206 : If both can be converted, or one can be converted but the
6207 : conversion is ambiguous, the program is ill-formed. If
6208 : neither can be converted, the operands are left unchanged and
6209 : further checking is performed as described below. If exactly
6210 : one conversion is possible, that conversion is applied to the
6211 : chosen operand and the converted operand is used in place of
6212 : the original operand for the remainder of this section. */
6213 258535 : if ((conv2 && !conv2->bad_p
6214 47332 : && conv3 && !conv3->bad_p)
6215 47310 : || (conv2 && conv2->kind == ck_ambig)
6216 258413 : || (conv3 && conv3->kind == ck_ambig))
6217 : {
6218 122 : if (complain & tf_error)
6219 : {
6220 6 : error_at (loc, "operands to %<?:%> have different types "
6221 : "%qT and %qT",
6222 : arg2_type, arg3_type);
6223 6 : if (conv2 && !conv2->bad_p && conv3 && !conv3->bad_p)
6224 3 : inform (loc, " and each type can be converted to the other");
6225 3 : else if (conv2 && conv2->kind == ck_ambig)
6226 3 : convert_like (conv2, arg2, complain);
6227 : else
6228 0 : convert_like (conv3, arg3, complain);
6229 : }
6230 122 : result = error_mark_node;
6231 : }
6232 258413 : else if (conv2 && !conv2->bad_p)
6233 : {
6234 47210 : arg2 = convert_like (conv2, arg2, complain);
6235 47210 : arg2 = convert_from_reference (arg2);
6236 47210 : arg2_type = TREE_TYPE (arg2);
6237 : /* Even if CONV2 is a valid conversion, the result of the
6238 : conversion may be invalid. For example, if ARG3 has type
6239 : "volatile X", and X does not have a copy constructor
6240 : accepting a "volatile X&", then even if ARG2 can be
6241 : converted to X, the conversion will fail. */
6242 47210 : if (error_operand_p (arg2))
6243 8 : result = error_mark_node;
6244 : converted = true;
6245 : }
6246 211203 : else if (conv3 && !conv3->bad_p)
6247 : {
6248 210465 : arg3 = convert_like (conv3, arg3, complain);
6249 210465 : arg3 = convert_from_reference (arg3);
6250 210465 : arg3_type = TREE_TYPE (arg3);
6251 210465 : if (error_operand_p (arg3))
6252 0 : result = error_mark_node;
6253 : converted = true;
6254 : }
6255 :
6256 130 : if (result)
6257 : return result;
6258 :
6259 : /* If, after the conversion, both operands have class type,
6260 : treat the cv-qualification of both operands as if it were the
6261 : union of the cv-qualification of the operands.
6262 :
6263 : The standard is not clear about what to do in this
6264 : circumstance. For example, if the first operand has type
6265 : "const X" and the second operand has a user-defined
6266 : conversion to "volatile X", what is the type of the second
6267 : operand after this step? Making it be "const X" (matching
6268 : the first operand) seems wrong, as that discards the
6269 : qualification without actually performing a copy. Leaving it
6270 : as "volatile X" seems wrong as that will result in the
6271 : conditional expression failing altogether, even though,
6272 : according to this step, the one operand could be converted to
6273 : the type of the other. */
6274 258405 : if (converted
6275 257667 : && CLASS_TYPE_P (arg2_type)
6276 270400 : && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
6277 0 : arg2_type = arg3_type =
6278 0 : cp_build_qualified_type (arg2_type,
6279 0 : cp_type_quals (arg2_type)
6280 0 : | cp_type_quals (arg3_type));
6281 : }
6282 :
6283 : /* [expr.cond]
6284 :
6285 : If the second and third operands are glvalues of the same value
6286 : category and have the same type, the result is of that type and
6287 : value category. */
6288 11611656 : if (((lvalue_p (arg2) && lvalue_p (arg3))
6289 4682383 : || (xvalue_p (arg2) && xvalue_p (arg3)))
6290 11028479 : && same_type_p (arg2_type, arg3_type))
6291 : {
6292 3102242 : result_type = arg2_type;
6293 3102242 : goto valid_operands;
6294 : }
6295 :
6296 : /* [expr.cond]
6297 :
6298 : Otherwise, the result is an rvalue. If the second and third
6299 : operand do not have the same type, and either has (possibly
6300 : cv-qualified) class type, overload resolution is used to
6301 : determine the conversions (if any) to be applied to the operands
6302 : (_over.match.oper_, _over.built_). */
6303 4728648 : is_glvalue = false;
6304 4728648 : if (!same_type_p (arg2_type, arg3_type)
6305 4728648 : && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
6306 : {
6307 738 : releasing_vec args;
6308 738 : conversion *conv;
6309 738 : bool any_viable_p;
6310 :
6311 : /* Rearrange the arguments so that add_builtin_candidate only has
6312 : to know about two args. In build_builtin_candidate, the
6313 : arguments are unscrambled. */
6314 738 : args->quick_push (arg2);
6315 738 : args->quick_push (arg3);
6316 738 : args->quick_push (arg1);
6317 738 : add_builtin_candidates (&candidates,
6318 : COND_EXPR,
6319 : NOP_EXPR,
6320 : ovl_op_identifier (false, COND_EXPR),
6321 : args,
6322 : LOOKUP_NORMAL, complain);
6323 :
6324 : /* [expr.cond]
6325 :
6326 : If the overload resolution fails, the program is
6327 : ill-formed. */
6328 738 : candidates = splice_viable (candidates, false, &any_viable_p);
6329 738 : if (!any_viable_p)
6330 : {
6331 615 : if (complain & tf_error)
6332 22 : error_at (loc, "operands to %<?:%> have different types %qT and %qT",
6333 : arg2_type, arg3_type);
6334 615 : return error_mark_node;
6335 : }
6336 123 : cand = tourney (candidates, complain);
6337 123 : if (!cand)
6338 : {
6339 0 : if (complain & tf_error)
6340 : {
6341 0 : auto_diagnostic_group d;
6342 0 : op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, false);
6343 0 : print_z_candidates (loc, candidates);
6344 0 : }
6345 0 : return error_mark_node;
6346 : }
6347 :
6348 : /* [expr.cond]
6349 :
6350 : Otherwise, the conversions thus determined are applied, and
6351 : the converted operands are used in place of the original
6352 : operands for the remainder of this section. */
6353 123 : conv = cand->convs[0];
6354 123 : arg1 = convert_like (conv, arg1, complain);
6355 123 : conv = cand->convs[1];
6356 123 : arg2 = convert_like (conv, arg2, complain);
6357 123 : arg2_type = TREE_TYPE (arg2);
6358 123 : conv = cand->convs[2];
6359 123 : arg3 = convert_like (conv, arg3, complain);
6360 123 : arg3_type = TREE_TYPE (arg3);
6361 738 : }
6362 :
6363 : /* [expr.cond]
6364 :
6365 : Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
6366 : and function-to-pointer (_conv.func_) standard conversions are
6367 : performed on the second and third operands.
6368 :
6369 : We need to force the lvalue-to-rvalue conversion here for class types,
6370 : so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
6371 : that isn't wrapped with a TARGET_EXPR plays havoc with exception
6372 : regions. */
6373 :
6374 4728033 : arg2 = force_rvalue (arg2, complain);
6375 4728033 : if (!CLASS_TYPE_P (arg2_type))
6376 4636017 : arg2_type = TREE_TYPE (arg2);
6377 :
6378 4728033 : arg3 = force_rvalue (arg3, complain);
6379 4728033 : if (!CLASS_TYPE_P (arg3_type))
6380 4636017 : arg3_type = TREE_TYPE (arg3);
6381 :
6382 4728033 : if (arg2 == error_mark_node || arg3 == error_mark_node)
6383 : return error_mark_node;
6384 :
6385 : /* [expr.cond]
6386 :
6387 : After those conversions, one of the following shall hold:
6388 :
6389 : --The second and third operands have the same type; the result is of
6390 : that type. */
6391 4727985 : if (same_type_p (arg2_type, arg3_type))
6392 : result_type = arg2_type;
6393 : /* [expr.cond]
6394 :
6395 : --The second and third operands have arithmetic or enumeration
6396 : type; the usual arithmetic conversions are performed to bring
6397 : them to a common type, and the result is of that type. */
6398 1032514 : else if ((ARITHMETIC_TYPE_P (arg2_type)
6399 106 : || UNSCOPED_ENUM_P (arg2_type))
6400 1009569 : && (ARITHMETIC_TYPE_P (arg3_type)
6401 71 : || UNSCOPED_ENUM_P (arg3_type)))
6402 : {
6403 : /* A conditional expression between a floating-point
6404 : type and an integer type should convert the integer type to
6405 : the evaluation format of the floating-point type, with
6406 : possible excess precision. */
6407 1009455 : tree eptype2 = arg2_type;
6408 1009455 : tree eptype3 = arg3_type;
6409 1009455 : tree eptype;
6410 831 : if (ANY_INTEGRAL_TYPE_P (arg2_type)
6411 1009458 : && (eptype = excess_precision_type (arg3_type)) != NULL_TREE)
6412 : {
6413 15 : eptype3 = eptype;
6414 15 : if (!semantic_result_type)
6415 15 : semantic_result_type
6416 15 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
6417 : }
6418 505 : else if (ANY_INTEGRAL_TYPE_P (arg3_type)
6419 1009440 : && (eptype = excess_precision_type (arg2_type)) != NULL_TREE)
6420 : {
6421 27 : eptype2 = eptype;
6422 27 : if (!semantic_result_type)
6423 27 : semantic_result_type
6424 27 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
6425 : }
6426 1009455 : result_type = type_after_usual_arithmetic_conversions (eptype2,
6427 : eptype3);
6428 1009455 : if (result_type == error_mark_node)
6429 : {
6430 0 : tree t1 = eptype2;
6431 0 : tree t2 = eptype3;
6432 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6433 0 : t1 = TREE_TYPE (t1);
6434 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6435 0 : t2 = TREE_TYPE (t2);
6436 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
6437 : && SCALAR_FLOAT_TYPE_P (t2)
6438 : && (extended_float_type_p (t1)
6439 : || extended_float_type_p (t2))
6440 : && cp_compare_floating_point_conversion_ranks
6441 : (t1, t2) == 3);
6442 0 : if (complain & tf_error)
6443 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
6444 : "have unordered conversion rank",
6445 : eptype2, eptype3);
6446 0 : return error_mark_node;
6447 : }
6448 1009455 : if (semantic_result_type == error_mark_node)
6449 : {
6450 0 : tree t1 = arg2_type;
6451 0 : tree t2 = arg3_type;
6452 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6453 0 : t1 = TREE_TYPE (t1);
6454 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6455 0 : t2 = TREE_TYPE (t2);
6456 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
6457 : && SCALAR_FLOAT_TYPE_P (t2)
6458 : && (extended_float_type_p (t1)
6459 : || extended_float_type_p (t2))
6460 : && cp_compare_floating_point_conversion_ranks
6461 : (t1, t2) == 3);
6462 0 : if (complain & tf_error)
6463 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
6464 : "have unordered conversion rank",
6465 : arg2_type, arg3_type);
6466 0 : return error_mark_node;
6467 : }
6468 :
6469 1009455 : if (complain & tf_warning)
6470 956057 : do_warn_double_promotion (result_type, arg2_type, arg3_type,
6471 : "implicit conversion from %qH to %qI to "
6472 : "match other result of conditional",
6473 : loc);
6474 :
6475 1009455 : if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
6476 102 : && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
6477 : {
6478 35 : tree stripped_orig_arg2 = tree_strip_any_location_wrapper (orig_arg2);
6479 35 : tree stripped_orig_arg3 = tree_strip_any_location_wrapper (orig_arg3);
6480 35 : if (TREE_CODE (stripped_orig_arg2) == CONST_DECL
6481 23 : && TREE_CODE (stripped_orig_arg3) == CONST_DECL
6482 58 : && (DECL_CONTEXT (stripped_orig_arg2)
6483 23 : == DECL_CONTEXT (stripped_orig_arg3)))
6484 : /* Two enumerators from the same enumeration can have different
6485 : types when the enumeration is still being defined. */;
6486 29 : else if (complain & (cxx_dialect >= cxx26
6487 29 : ? tf_warning_or_error : tf_warning))
6488 44 : emit_diagnostic ((cxx_dialect >= cxx26
6489 : ? diagnostics::kind::pedwarn
6490 : : diagnostics::kind::warning),
6491 26 : loc, OPT_Wenum_compare, "enumerated mismatch "
6492 : "in conditional expression: %qT vs %qT",
6493 : arg2_type, arg3_type);
6494 3 : else if (cxx_dialect >= cxx26)
6495 2 : return error_mark_node;
6496 : }
6497 1009420 : else if ((((complain & (cxx_dialect >= cxx26
6498 1009420 : ? tf_warning_or_error : tf_warning))
6499 960411 : && warn_deprecated_enum_float_conv)
6500 64530 : || (cxx_dialect >= cxx26
6501 2141 : && (complain & tf_warning_or_error) == 0))
6502 946928 : && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
6503 40 : && SCALAR_FLOAT_TYPE_P (arg3_type))
6504 946915 : || (SCALAR_FLOAT_TYPE_P (arg2_type)
6505 354 : && TREE_CODE (arg3_type) == ENUMERAL_TYPE)))
6506 : {
6507 23 : if (cxx_dialect >= cxx26 && (complain & tf_warning_or_error) == 0)
6508 4 : return error_mark_node;
6509 19 : if (cxx_dialect >= cxx26 && TREE_CODE (arg2_type) == ENUMERAL_TYPE)
6510 5 : pedwarn (loc, OPT_Wdeprecated_enum_float_conversion,
6511 : "conditional expression between enumeration type "
6512 : "%qT and floating-point type %qT", arg2_type, arg3_type);
6513 14 : else if (cxx_dialect >= cxx26)
6514 4 : pedwarn (loc, OPT_Wdeprecated_enum_float_conversion,
6515 : "conditional expression between floating-point type "
6516 : "%qT and enumeration type %qT", arg2_type, arg3_type);
6517 10 : else if (TREE_CODE (arg2_type) == ENUMERAL_TYPE)
6518 6 : warning_at (loc, OPT_Wdeprecated_enum_float_conversion,
6519 : "conditional expression between enumeration type "
6520 : "%qT and floating-point type %qT is deprecated",
6521 : arg2_type, arg3_type);
6522 : else
6523 4 : warning_at (loc, OPT_Wdeprecated_enum_float_conversion,
6524 : "conditional expression between floating-point "
6525 : "type %qT and enumeration type %qT is deprecated",
6526 : arg2_type, arg3_type);
6527 : }
6528 999642 : else if ((extra_warnings || warn_enum_conversion)
6529 1009402 : && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
6530 15 : && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
6531 9748 : || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
6532 8 : && !same_type_p (arg2_type,
6533 : type_promotes_to (arg3_type)))))
6534 : {
6535 17 : if (complain & tf_warning)
6536 : {
6537 12 : enum opt_code opt = (warn_enum_conversion
6538 17 : ? OPT_Wenum_conversion
6539 : : OPT_Wextra);
6540 17 : warning_at (loc, opt, "enumerated and "
6541 : "non-enumerated type in conditional expression");
6542 : }
6543 : }
6544 :
6545 1009449 : arg2 = perform_implicit_conversion (result_type, arg2, complain);
6546 1009449 : arg3 = perform_implicit_conversion (result_type, arg3, complain);
6547 : }
6548 : /* [expr.cond]
6549 :
6550 : --The second and third operands have pointer type, or one has
6551 : pointer type and the other is a null pointer constant; pointer
6552 : conversions (_conv.ptr_) and qualification conversions
6553 : (_conv.qual_) are performed to bring them to their composite
6554 : pointer type (_expr.rel_). The result is of the composite
6555 : pointer type.
6556 :
6557 : --The second and third operands have pointer to member type, or
6558 : one has pointer to member type and the other is a null pointer
6559 : constant; pointer to member conversions (_conv.mem_) and
6560 : qualification conversions (_conv.qual_) are performed to bring
6561 : them to a common type, whose cv-qualification shall match the
6562 : cv-qualification of either the second or the third operand.
6563 : The result is of the common type. */
6564 23059 : else if ((null_ptr_cst_p (arg2)
6565 184 : && TYPE_PTR_OR_PTRMEM_P (arg3_type))
6566 22875 : || (null_ptr_cst_p (arg3)
6567 21911 : && TYPE_PTR_OR_PTRMEM_P (arg2_type))
6568 964 : || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
6569 99 : || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type))
6570 23135 : || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
6571 : {
6572 23005 : result_type = composite_pointer_type (loc,
6573 : arg2_type, arg3_type, arg2,
6574 : arg3, CPO_CONDITIONAL_EXPR,
6575 : complain);
6576 23005 : if (result_type == error_mark_node)
6577 : return error_mark_node;
6578 22979 : arg2 = perform_implicit_conversion (result_type, arg2, complain);
6579 22979 : arg3 = perform_implicit_conversion (result_type, arg3, complain);
6580 : }
6581 :
6582 4727899 : if (!result_type)
6583 : {
6584 54 : if (complain & tf_error)
6585 3 : error_at (loc, "operands to %<?:%> have different types %qT and %qT",
6586 : arg2_type, arg3_type);
6587 54 : return error_mark_node;
6588 : }
6589 :
6590 4727899 : if (arg2 == error_mark_node || arg3 == error_mark_node)
6591 : return error_mark_node;
6592 :
6593 4727896 : valid_operands:
6594 7958399 : if (processing_template_decl && is_glvalue)
6595 : {
6596 : /* Let lvalue_kind know this was a glvalue. */
6597 114569 : tree arg = (result_type == arg2_type ? arg2 : arg3);
6598 114569 : result_type = cp_build_reference_type (result_type, xvalue_p (arg));
6599 : }
6600 :
6601 7958399 : result = build3_loc (loc, COND_EXPR, result_type, arg1, arg2, arg3);
6602 :
6603 : /* If the ARG2 and ARG3 are the same and don't have side-effects,
6604 : warn here, because the COND_EXPR will be turned into ARG2. */
6605 7958399 : if (warn_duplicated_branches
6606 153 : && (complain & tf_warning)
6607 7958552 : && (arg2 == arg3 || operand_equal_p (arg2, arg3,
6608 : OEP_ADDRESS_OF_SAME_FIELD)))
6609 57 : warning_at (EXPR_LOCATION (result), OPT_Wduplicated_branches,
6610 : "this condition has identical branches");
6611 :
6612 : /* We can't use result_type below, as fold might have returned a
6613 : throw_expr. */
6614 :
6615 7958399 : if (!is_glvalue)
6616 : {
6617 : /* Expand both sides into the same slot, hopefully the target of
6618 : the ?: expression. We used to check for TARGET_EXPRs here,
6619 : but now we sometimes wrap them in NOP_EXPRs so the test would
6620 : fail. */
6621 4856088 : if (CLASS_TYPE_P (TREE_TYPE (result)))
6622 : {
6623 92053 : result = get_target_expr (result, complain);
6624 : /* Tell gimplify_modify_expr_rhs not to strip this in
6625 : assignment context: we want both arms to initialize
6626 : the same temporary. */
6627 92053 : TARGET_EXPR_NO_ELIDE (result) = true;
6628 : }
6629 : /* If this expression is an rvalue, but might be mistaken for an
6630 : lvalue, we must add a NON_LVALUE_EXPR. */
6631 4856088 : result = rvalue (result);
6632 4856088 : if (semantic_result_type)
6633 87 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type,
6634 : result);
6635 : }
6636 : else
6637 : {
6638 3102311 : result = force_paren_expr (result);
6639 3102311 : gcc_assert (semantic_result_type == NULL_TREE);
6640 : }
6641 :
6642 : return result;
6643 7961375 : }
6644 :
6645 : /* OPERAND is an operand to an expression. Perform necessary steps
6646 : required before using it. If OPERAND is NULL_TREE, NULL_TREE is
6647 : returned. */
6648 :
6649 : static tree
6650 659673905 : prep_operand (tree operand)
6651 : {
6652 659673905 : if (operand)
6653 : {
6654 762512816 : if (CLASS_TYPE_P (TREE_TYPE (operand))
6655 408134826 : && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
6656 : /* Make sure the template type is instantiated now. */
6657 10713622 : instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
6658 : }
6659 :
6660 659673905 : return operand;
6661 : }
6662 :
6663 : /* True iff CONV represents a conversion sequence which no other can be better
6664 : than under [over.ics.rank]: in other words, a "conversion" to the exact same
6665 : type (including binding to a reference to the same type). This is stronger
6666 : than the standard's "identity" category, which also includes reference
6667 : bindings that add cv-qualifiers or change rvalueness. */
6668 :
6669 : static bool
6670 232490453 : perfect_conversion_p (conversion *conv)
6671 : {
6672 232490453 : if (CONVERSION_RANK (conv) != cr_identity)
6673 : return false;
6674 166287261 : if (conv->kind == ck_ref_bind)
6675 : {
6676 43512882 : if (!conv->rvaluedness_matches_p)
6677 : return false;
6678 30121115 : if (!same_type_p (TREE_TYPE (conv->type),
6679 : next_conversion (conv)->type))
6680 : return false;
6681 : }
6682 148943819 : if (conv->check_narrowing)
6683 : /* Brace elision is imperfect. */
6684 50 : return false;
6685 : return true;
6686 : }
6687 :
6688 : /* True if CAND represents a perfect match, i.e. all perfect conversions, so no
6689 : other candidate can be a better match. Since the template/non-template
6690 : tiebreaker comes immediately after the conversion comparison in
6691 : [over.match.best], a perfect non-template candidate is better than all
6692 : templates. */
6693 :
6694 : static bool
6695 548202806 : perfect_candidate_p (z_candidate *cand)
6696 : {
6697 548202806 : if (cand->viable < 1)
6698 : return false;
6699 : /* CWG1402 makes an implicitly deleted move op worse than other
6700 : candidates. */
6701 214125604 : if (DECL_DELETED_FN (cand->fn) && DECL_DEFAULTED_FN (cand->fn)
6702 213108158 : && move_fn_p (cand->fn))
6703 : return false;
6704 211436356 : int len = cand->num_convs;
6705 360380125 : for (int i = 0; i < len; ++i)
6706 232490453 : if (!perfect_conversion_p (cand->convs[i]))
6707 : return false;
6708 127889672 : if (conversion *conv = cand->second_conv)
6709 0 : if (!perfect_conversion_p (conv))
6710 : return false;
6711 : return true;
6712 : }
6713 :
6714 : /* True iff one of CAND's argument conversions is missing. */
6715 :
6716 : static bool
6717 22500292 : missing_conversion_p (const z_candidate *cand)
6718 : {
6719 40977450 : for (unsigned i = 0; i < cand->num_convs; ++i)
6720 : {
6721 27204102 : conversion *conv = cand->convs[i];
6722 27204102 : if (!conv)
6723 : return true;
6724 26201562 : if (conv->kind == ck_deferred_bad)
6725 : {
6726 : /* We don't know whether this conversion is outright invalid or
6727 : just bad, so conservatively assume it's missing. */
6728 7724404 : gcc_checking_assert (conv->bad_p);
6729 : return true;
6730 : }
6731 : }
6732 : return false;
6733 : }
6734 :
6735 : /* Add each of the viable functions in FNS (a FUNCTION_DECL or
6736 : OVERLOAD) to the CANDIDATES, returning an updated list of
6737 : CANDIDATES. The ARGS are the arguments provided to the call;
6738 : if FIRST_ARG is non-null it is the implicit object argument,
6739 : otherwise the first element of ARGS is used if needed. The
6740 : EXPLICIT_TARGS are explicit template arguments provided.
6741 : TEMPLATE_ONLY is true if only template functions should be
6742 : considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
6743 : add_function_candidate. */
6744 :
6745 : static void
6746 316639302 : add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args,
6747 : tree return_type,
6748 : tree explicit_targs, bool template_only,
6749 : tree conversion_path, tree access_path,
6750 : int flags,
6751 : struct z_candidate **candidates,
6752 : tsubst_flags_t complain)
6753 : {
6754 316639302 : tree ctype;
6755 316639302 : const vec<tree, va_gc> *non_static_args;
6756 316639302 : bool check_list_ctor = false;
6757 316639302 : bool check_converting = false;
6758 316639302 : unification_kind_t strict;
6759 316639302 : tree ne_context = NULL_TREE;
6760 316639302 : tree ne_fns = NULL_TREE;
6761 :
6762 316639302 : if (!fns)
6763 3397784 : return;
6764 :
6765 : /* Precalculate special handling of constructors and conversion ops. */
6766 313241518 : tree fn = OVL_FIRST (fns);
6767 313241518 : if (DECL_CONV_FN_P (fn))
6768 : {
6769 18507634 : check_list_ctor = false;
6770 18507634 : check_converting = (flags & LOOKUP_ONLYCONVERTING) != 0;
6771 18507634 : if (flags & LOOKUP_NO_CONVERSION)
6772 : /* We're doing return_type(x). */
6773 : strict = DEDUCE_CONV;
6774 : else
6775 : /* We're doing x.operator return_type(). */
6776 1941 : strict = DEDUCE_EXACT;
6777 : /* [over.match.funcs] For conversion functions, the function
6778 : is considered to be a member of the class of the implicit
6779 : object argument for the purpose of defining the type of
6780 : the implicit object parameter. */
6781 18507634 : ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg));
6782 : }
6783 : else
6784 : {
6785 589467768 : if (DECL_CONSTRUCTOR_P (fn))
6786 : {
6787 79433261 : check_list_ctor = (flags & LOOKUP_LIST_ONLY) != 0;
6788 : /* For list-initialization we consider explicit constructors
6789 : and complain if one is chosen. */
6790 79433261 : check_converting
6791 79433261 : = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
6792 : == LOOKUP_ONLYCONVERTING);
6793 : }
6794 294733884 : strict = DEDUCE_CALL;
6795 458938854 : ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
6796 : }
6797 :
6798 : /* P2468: Check if operator== is a rewrite target with first operand
6799 : (*args)[0]; for now just do the lookups. */
6800 313241518 : if ((flags & (LOOKUP_REWRITTEN | LOOKUP_REVERSED))
6801 313241518 : && DECL_OVERLOADED_OPERATOR_IS (fn, EQ_EXPR))
6802 : {
6803 6992021 : tree ne_name = ovl_op_identifier (false, NE_EXPR);
6804 6992021 : if (DECL_CLASS_SCOPE_P (fn))
6805 : {
6806 187227 : ne_context = DECL_CONTEXT (fn);
6807 187227 : ne_fns = lookup_fnfields (TREE_TYPE ((*args)[0]), ne_name,
6808 : 1, tf_none);
6809 187227 : if (ne_fns == error_mark_node || ne_fns == NULL_TREE)
6810 : ne_fns = NULL_TREE;
6811 : else
6812 19653 : ne_fns = BASELINK_FUNCTIONS (ne_fns);
6813 : }
6814 : else
6815 : {
6816 6804794 : ne_context = decl_namespace_context (fn);
6817 6804794 : ne_fns = lookup_qualified_name (ne_context, ne_name,
6818 : LOOK_want::NORMAL,
6819 : /*complain*/false);
6820 6804794 : if (ne_fns == error_mark_node
6821 2254525 : || !is_overloaded_fn (ne_fns))
6822 4550269 : ne_fns = NULL_TREE;
6823 : }
6824 : }
6825 :
6826 313241518 : if (first_arg)
6827 : non_static_args = args;
6828 : else
6829 : /* Delay creating the implicit this parameter until it is needed. */
6830 139087795 : non_static_args = NULL;
6831 :
6832 313241518 : bool seen_strictly_viable = any_strictly_viable (*candidates);
6833 : /* If there's a non-template perfect match, we don't need to consider
6834 : templates. So check non-templates first. This optimization is only
6835 : really needed for the defaulted copy constructor of tuple and the like
6836 : (96926), but it seems like we might as well enable it more generally. */
6837 313241518 : bool seen_perfect = false;
6838 313241518 : enum { templates, non_templates, either } which = either;
6839 313241518 : if (template_only)
6840 : which = templates;
6841 : else /*if (flags & LOOKUP_DEFAULTED)*/
6842 274188332 : which = non_templates;
6843 :
6844 : /* Template candidates that we'll potentially ignore if the
6845 : perfect candidate optimization succeeds. */
6846 313241518 : z_candidate *ignored_template_cands = nullptr;
6847 :
6848 : /* During overload resolution, we first consider each function under the
6849 : assumption that we'll eventually find a strictly viable candidate.
6850 : This allows us to circumvent our defacto behavior when checking
6851 : argument conversions and shortcut consideration of the candidate
6852 : upon encountering the first bad conversion. If this assumption
6853 : turns out to be false, and all candidates end up being non-strictly
6854 : viable, then we reconsider such candidates under the defacto behavior.
6855 : This trick is important for pruning member function overloads according
6856 : to their const/ref-qualifiers (since all 'this' conversions are at
6857 : worst bad) without breaking -fpermissive. */
6858 313241518 : z_candidate *bad_cands = nullptr;
6859 313241518 : bool shortcut_bad_convs = true;
6860 :
6861 146423168 : again:
6862 2655997707 : for (tree fn : lkp_range (fns))
6863 : {
6864 2196346572 : if (which == templates && TREE_CODE (fn) != TEMPLATE_DECL)
6865 : {
6866 320592741 : if (template_only)
6867 799545 : add_ignored_candidate (candidates, fn);
6868 320592741 : continue;
6869 : }
6870 1875753831 : if (which == non_templates && TREE_CODE (fn) == TEMPLATE_DECL)
6871 : {
6872 649966977 : add_ignored_candidate (&ignored_template_cands, fn);
6873 649966977 : continue;
6874 : }
6875 235674222 : if ((check_converting && DECL_NONCONVERTING_P (fn))
6876 1435772279 : || (check_list_ctor && !is_list_ctor (fn)))
6877 : {
6878 27111211 : add_ignored_candidate (candidates, fn);
6879 27111211 : continue;
6880 : }
6881 :
6882 1198675643 : tree fn_first_arg = NULL_TREE;
6883 1198675643 : const vec<tree, va_gc> *fn_args = args;
6884 :
6885 1198675643 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fn))
6886 : {
6887 : /* Figure out where the object arg comes from. If this
6888 : function is a non-static member and we didn't get an
6889 : implicit object argument, move it out of args. */
6890 434997852 : if (first_arg == NULL_TREE)
6891 : {
6892 8558881 : unsigned int ix;
6893 8558881 : tree arg;
6894 8558881 : vec<tree, va_gc> *tempvec;
6895 8558881 : vec_alloc (tempvec, args->length () - 1);
6896 23182654 : for (ix = 1; args->iterate (ix, &arg); ++ix)
6897 6064892 : tempvec->quick_push (arg);
6898 8558881 : non_static_args = tempvec;
6899 8558881 : first_arg = (*args)[0];
6900 : }
6901 :
6902 : fn_first_arg = first_arg;
6903 : fn_args = non_static_args;
6904 : }
6905 :
6906 : /* Don't bother reversing an operator with two identical parameters. */
6907 763677791 : else if (vec_safe_length (args) == 2 && (flags & LOOKUP_REVERSED))
6908 : {
6909 212249429 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
6910 212249429 : if (same_type_p (TREE_VALUE (parmlist),
6911 : TREE_VALUE (TREE_CHAIN (parmlist))))
6912 100736549 : continue;
6913 : }
6914 :
6915 : /* When considering reversed operator==, if there's a corresponding
6916 : operator!= in the same scope, it's not a rewrite target. */
6917 1097939094 : if (ne_context)
6918 : {
6919 150431062 : if (TREE_CODE (ne_context) == NAMESPACE_DECL)
6920 : {
6921 : /* With argument-dependent lookup, fns can span multiple
6922 : namespaces; make sure we look in the fn's namespace for a
6923 : corresponding operator!=. */
6924 150238592 : tree fn_ns = decl_namespace_context (fn);
6925 150238592 : if (fn_ns != ne_context)
6926 : {
6927 8335096 : ne_context = fn_ns;
6928 8335096 : tree ne_name = ovl_op_identifier (false, NE_EXPR);
6929 8335096 : ne_fns = lookup_qualified_name (ne_context, ne_name,
6930 : LOOK_want::NORMAL,
6931 : /*complain*/false);
6932 8335096 : if (ne_fns == error_mark_node
6933 5283315 : || !is_overloaded_fn (ne_fns))
6934 3051781 : ne_fns = NULL_TREE;
6935 : }
6936 : }
6937 150431062 : bool found = false;
6938 1622125469 : for (lkp_iterator ne (ne_fns); !found && ne; ++ne)
6939 1507531528 : if (0 && !ne.using_p ()
6940 : && DECL_NAMESPACE_SCOPE_P (fn)
6941 : && DECL_CONTEXT (*ne) != DECL_CONTEXT (fn))
6942 : /* ??? This kludge excludes inline namespace members for the H
6943 : test in spaceship-eq15.C, but I don't see why we would want
6944 : that behavior. Asked Core 2022-11-04. Disabling for now. */;
6945 1507531528 : else if (fns_correspond (fn, *ne))
6946 : {
6947 : found = true;
6948 : break;
6949 : }
6950 150431062 : if (found)
6951 35837121 : continue;
6952 : }
6953 :
6954 : /* Do not resolve any non-default function. Only the default version
6955 : is resolvable (for the target_version attribute semantics.) */
6956 1062101973 : if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
6957 : && TREE_CODE (fn) == FUNCTION_DECL
6958 : && !is_function_default_version (fn))
6959 : {
6960 : add_ignored_candidate (candidates, fn);
6961 : continue;
6962 : }
6963 :
6964 1062101973 : if (TREE_CODE (fn) == TEMPLATE_DECL)
6965 513899167 : add_template_candidate (candidates,
6966 : fn,
6967 : ctype,
6968 : explicit_targs,
6969 : fn_first_arg,
6970 : fn_args,
6971 : return_type,
6972 : access_path,
6973 : conversion_path,
6974 : flags,
6975 : strict,
6976 : shortcut_bad_convs,
6977 : complain);
6978 : else
6979 : {
6980 548202806 : add_function_candidate (candidates,
6981 : fn,
6982 : ctype,
6983 : fn_first_arg,
6984 : fn_args,
6985 : access_path,
6986 : conversion_path,
6987 : flags,
6988 : NULL,
6989 : shortcut_bad_convs,
6990 : complain);
6991 548202806 : if (perfect_candidate_p (*candidates))
6992 1062088422 : seen_perfect = true;
6993 : }
6994 :
6995 1062088422 : z_candidate *cand = *candidates;
6996 1062088422 : if (cand->viable == 1)
6997 299002240 : seen_strictly_viable = true;
6998 :
6999 1062088422 : if (cand->viable == -1
7000 22500790 : && shortcut_bad_convs
7001 1084588714 : && (missing_conversion_p (cand)
7002 13773348 : || TREE_CODE (cand->fn) == TEMPLATE_DECL))
7003 : {
7004 : /* This candidate has been tentatively marked non-strictly viable,
7005 : and we didn't compute all argument conversions for it (having
7006 : stopped at the first bad conversion). Move it to BAD_CANDS to
7007 : to fully reconsider later if we don't find any strictly viable
7008 : candidates. */
7009 8758698 : if (complain & (tf_error | tf_conv))
7010 : {
7011 8435125 : *candidates = cand->next;
7012 8435125 : cand->next = bad_cands;
7013 8435125 : bad_cands = cand;
7014 : }
7015 : else
7016 : /* But if we're in a SFINAE context, just mark this candidate as
7017 : unviable outright and avoid potentially reconsidering it.
7018 : This is safe to do because in a SFINAE context, performing a bad
7019 : conversion is always an error (even with -fpermissive), so a
7020 : non-strictly viable candidate is effectively unviable anyway. */
7021 323573 : cand->viable = 0;
7022 : }
7023 : }
7024 459651135 : if (which == non_templates && !seen_perfect)
7025 : {
7026 146304828 : which = templates;
7027 146304828 : ignored_template_cands = nullptr;
7028 146304828 : goto again;
7029 : }
7030 313346307 : else if (which == templates
7031 313346307 : && !seen_strictly_viable
7032 : && shortcut_bad_convs
7033 57719432 : && bad_cands)
7034 : {
7035 : /* None of the candidates are strictly viable, so consider again those
7036 : functions in BAD_CANDS, this time without shortcutting bad conversions
7037 : so that all their argument conversions are computed. */
7038 261528 : which = either;
7039 : fns = NULL_TREE;
7040 261528 : for (z_candidate *cand = bad_cands; cand; cand = cand->next)
7041 : {
7042 143188 : tree fn = cand->fn;
7043 143188 : if (tree ti = cand->template_decl)
7044 120 : fn = TI_TEMPLATE (ti);
7045 143188 : fns = ovl_make (fn, fns);
7046 : }
7047 118340 : shortcut_bad_convs = false;
7048 118340 : bad_cands = nullptr;
7049 118340 : goto again;
7050 : }
7051 :
7052 313227967 : if (complain & tf_error)
7053 : {
7054 : /* Remember any omitted candidates; we may want to print all candidates
7055 : as part of overload resolution failure diagnostics. */
7056 479743005 : for (z_candidate *omitted_cands : { ignored_template_cands, bad_cands })
7057 : {
7058 319828670 : z_candidate **omitted_cands_tail = &omitted_cands;
7059 368057396 : while (*omitted_cands_tail)
7060 48228726 : omitted_cands_tail = &(*omitted_cands_tail)->next;
7061 319828670 : *omitted_cands_tail = *candidates;
7062 319828670 : *candidates = omitted_cands;
7063 : }
7064 : }
7065 : }
7066 :
7067 : /* Returns 1 if P0145R2 says that the LHS of operator CODE is evaluated first,
7068 : -1 if the RHS is evaluated first, or 0 if the order is unspecified. */
7069 :
7070 : static int
7071 16496710 : op_is_ordered (tree_code code)
7072 : {
7073 16496051 : switch (code)
7074 : {
7075 : // 5. b @= a
7076 3720644 : case MODIFY_EXPR:
7077 3720644 : return (flag_strong_eval_order > 1 ? -1 : 0);
7078 :
7079 : // 6. a[b]
7080 423850 : case ARRAY_REF:
7081 423191 : return (flag_strong_eval_order > 1 ? 1 : 0);
7082 :
7083 : // 1. a.b
7084 : // Not overloadable (yet).
7085 : // 2. a->b
7086 : // Only one argument.
7087 : // 3. a->*b
7088 66541 : case MEMBER_REF:
7089 : // 7. a << b
7090 66541 : case LSHIFT_EXPR:
7091 : // 8. a >> b
7092 66541 : case RSHIFT_EXPR:
7093 : // a && b
7094 : // Predates P0145R3.
7095 66541 : case TRUTH_ANDIF_EXPR:
7096 : // a || b
7097 : // Predates P0145R3.
7098 66541 : case TRUTH_ORIF_EXPR:
7099 : // a , b
7100 : // Predates P0145R3.
7101 66541 : case COMPOUND_EXPR:
7102 66541 : return (flag_strong_eval_order ? 1 : 0);
7103 :
7104 : default:
7105 : return 0;
7106 : }
7107 : }
7108 :
7109 : /* Subroutine of build_new_op: Add to CANDIDATES all candidates for the
7110 : operator indicated by CODE/CODE2. This function calls itself recursively to
7111 : handle C++20 rewritten comparison operator candidates. Returns NULL_TREE
7112 : upon success, and error_mark_node if something went wrong that prevented
7113 : us from performing overload resolution (e.g. ambiguous member name lookup).
7114 :
7115 : LOOKUPS, if non-NULL, is the set of pertinent namespace-scope operator
7116 : overloads to consider. This parameter is used when instantiating a
7117 : dependent operator expression and has the same structure as
7118 : DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS. */
7119 :
7120 : static tree
7121 34831303 : add_operator_candidates (z_candidate **candidates,
7122 : tree_code code, tree_code code2,
7123 : vec<tree, va_gc> *arglist, tree lookups,
7124 : int flags, tsubst_flags_t complain)
7125 : {
7126 34831303 : z_candidate *start_candidates = *candidates;
7127 34831303 : bool ismodop = code2 != ERROR_MARK;
7128 34831303 : tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
7129 :
7130 : /* LOOKUP_REWRITTEN is set when we're looking for the == or <=> operator to
7131 : rewrite from, and also when we're looking for the e.g. < operator to use
7132 : on the result of <=>. In the latter case, we don't want the flag set in
7133 : the candidate, we just want to suppress looking for rewrites. */
7134 34831303 : bool rewritten = (flags & LOOKUP_REWRITTEN);
7135 34831303 : if (rewritten && code != EQ_EXPR && code != SPACESHIP_EXPR)
7136 940277 : flags &= ~LOOKUP_REWRITTEN;
7137 :
7138 32296565 : bool memonly = false;
7139 32296565 : switch (code)
7140 : {
7141 : /* =, ->, [], () must be non-static member functions. */
7142 6533198 : case MODIFY_EXPR:
7143 6533198 : if (code2 != NOP_EXPR)
7144 : break;
7145 : /* FALLTHRU */
7146 : case COMPONENT_REF:
7147 : case ARRAY_REF:
7148 : memonly = true;
7149 : break;
7150 :
7151 : default:
7152 : break;
7153 : }
7154 :
7155 : /* Add namespace-scope operators to the list of functions to
7156 : consider. */
7157 : if (!memonly)
7158 : {
7159 30567323 : tree fns;
7160 30567323 : if (!lookups)
7161 22617151 : fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE);
7162 : /* If LOOKUPS is non-NULL, then we're instantiating a dependent operator
7163 : expression, and LOOKUPS is the result of stage 1 name lookup. */
7164 7950172 : else if (tree found = purpose_member (fnname, lookups))
7165 1748271 : fns = TREE_VALUE (found);
7166 : else
7167 : fns = NULL_TREE;
7168 30567323 : fns = lookup_arg_dependent (fnname, fns, arglist);
7169 30567323 : add_candidates (fns, NULL_TREE, arglist, NULL_TREE,
7170 : NULL_TREE, false, NULL_TREE, NULL_TREE,
7171 : flags, candidates, complain);
7172 : }
7173 :
7174 : /* Add class-member operators to the candidate set. */
7175 34831270 : tree arg1_type = TREE_TYPE ((*arglist)[0]);
7176 34831270 : unsigned nargs = arglist->length () > 1 ? 2 : 1;
7177 29820980 : tree arg2_type = nargs > 1 ? TREE_TYPE ((*arglist)[1]) : NULL_TREE;
7178 34831270 : if (CLASS_TYPE_P (arg1_type))
7179 : {
7180 20156488 : tree fns = lookup_fnfields (arg1_type, fnname, 1, complain);
7181 20156488 : if (fns == error_mark_node)
7182 : return error_mark_node;
7183 20156476 : if (fns)
7184 : {
7185 8986793 : if (code == ARRAY_REF)
7186 : {
7187 427912 : vec<tree,va_gc> *restlist = make_tree_vector ();
7188 855824 : for (unsigned i = 1; i < nargs; ++i)
7189 427912 : vec_safe_push (restlist, (*arglist)[i]);
7190 427912 : z_candidate *save_cand = *candidates;
7191 855824 : add_candidates (BASELINK_FUNCTIONS (fns),
7192 427912 : (*arglist)[0], restlist, NULL_TREE,
7193 : NULL_TREE, false,
7194 427912 : BASELINK_BINFO (fns),
7195 427912 : BASELINK_ACCESS_BINFO (fns),
7196 : flags, candidates, complain);
7197 : /* Release the vec if we didn't add a candidate that uses it. */
7198 428510 : for (z_candidate *c = *candidates; c != save_cand; c = c->next)
7199 428510 : if (c->args == restlist)
7200 : {
7201 427912 : restlist = NULL;
7202 427912 : break;
7203 : }
7204 427912 : release_tree_vector (restlist);
7205 : }
7206 : else
7207 8558881 : add_candidates (BASELINK_FUNCTIONS (fns),
7208 : NULL_TREE, arglist, NULL_TREE,
7209 : NULL_TREE, false,
7210 8558881 : BASELINK_BINFO (fns),
7211 8558881 : BASELINK_ACCESS_BINFO (fns),
7212 : flags, candidates, complain);
7213 : }
7214 : }
7215 : /* Per [over.match.oper]3.2, if no operand has a class type, then
7216 : only non-member functions that have type T1 or reference to
7217 : cv-qualified-opt T1 for the first argument, if the first argument
7218 : has an enumeration type, or T2 or reference to cv-qualified-opt
7219 : T2 for the second argument, if the second argument has an
7220 : enumeration type. Filter out those that don't match. */
7221 14674782 : else if (! arg2_type || ! CLASS_TYPE_P (arg2_type))
7222 : {
7223 : struct z_candidate **candp, **next;
7224 :
7225 241933345 : for (candp = candidates; *candp != start_candidates; candp = next)
7226 : {
7227 227855312 : unsigned i;
7228 227855312 : z_candidate *cand = *candp;
7229 227855312 : next = &cand->next;
7230 :
7231 227855312 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
7232 :
7233 672410949 : for (i = 0; i < nargs; ++i)
7234 : {
7235 449728821 : tree parmtype = TREE_VALUE (parmlist);
7236 449728821 : tree argtype = unlowered_expr_type ((*arglist)[i]);
7237 :
7238 449728821 : if (TYPE_REF_P (parmtype))
7239 355612144 : parmtype = TREE_TYPE (parmtype);
7240 449728821 : if (TREE_CODE (argtype) == ENUMERAL_TYPE
7241 878410749 : && (same_type_ignoring_top_level_qualifiers_p
7242 428681928 : (argtype, parmtype)))
7243 : break;
7244 :
7245 444555637 : parmlist = TREE_CHAIN (parmlist);
7246 : }
7247 :
7248 : /* No argument has an appropriate type, so remove this
7249 : candidate function from the list. */
7250 227855312 : if (i == nargs)
7251 : {
7252 222682128 : *candp = cand->next;
7253 222682128 : next = candp;
7254 : }
7255 : }
7256 : }
7257 :
7258 34831258 : if (!rewritten)
7259 : {
7260 : /* The standard says to rewrite built-in candidates, too,
7261 : but there's no point. */
7262 24516526 : add_builtin_candidates (candidates, code, code2, fnname, arglist,
7263 : flags, complain);
7264 :
7265 : /* Maybe add C++20 rewritten comparison candidates. */
7266 24516526 : tree_code rewrite_code = ERROR_MARK;
7267 24516526 : if (cxx_dialect >= cxx20
7268 24214228 : && nargs == 2
7269 43842860 : && (OVERLOAD_TYPE_P (arg1_type) || OVERLOAD_TYPE_P (arg2_type)))
7270 19318714 : switch (code)
7271 : {
7272 1330454 : case LT_EXPR:
7273 1330454 : case LE_EXPR:
7274 1330454 : case GT_EXPR:
7275 1330454 : case GE_EXPR:
7276 1330454 : case SPACESHIP_EXPR:
7277 1330454 : rewrite_code = SPACESHIP_EXPR;
7278 1330454 : break;
7279 :
7280 : case NE_EXPR:
7281 : case EQ_EXPR:
7282 : rewrite_code = EQ_EXPR;
7283 : break;
7284 :
7285 : default:;
7286 : }
7287 :
7288 1330454 : if (rewrite_code)
7289 : {
7290 6290411 : tree r;
7291 6290411 : flags |= LOOKUP_REWRITTEN;
7292 6290411 : if (rewrite_code != code)
7293 : {
7294 : /* Add rewritten candidates in same order. */
7295 3083742 : r = add_operator_candidates (candidates, rewrite_code, ERROR_MARK,
7296 : arglist, lookups, flags, complain);
7297 3083742 : if (r == error_mark_node)
7298 : return error_mark_node;
7299 : }
7300 :
7301 6290405 : z_candidate *save_cand = *candidates;
7302 :
7303 : /* Add rewritten candidates in reverse order. */
7304 6290405 : flags |= LOOKUP_REVERSED;
7305 6290405 : vec<tree,va_gc> *revlist = make_tree_vector ();
7306 6290405 : revlist->quick_push ((*arglist)[1]);
7307 6290405 : revlist->quick_push ((*arglist)[0]);
7308 6290405 : r = add_operator_candidates (candidates, rewrite_code, ERROR_MARK,
7309 : revlist, lookups, flags, complain);
7310 6290405 : if (r == error_mark_node)
7311 : return error_mark_node;
7312 :
7313 : /* Release the vec if we didn't add a candidate that uses it. */
7314 6452436 : for (z_candidate *c = *candidates; c != save_cand; c = c->next)
7315 2969109 : if (c->args == revlist)
7316 : {
7317 : revlist = NULL;
7318 : break;
7319 : }
7320 6290405 : release_tree_vector (revlist);
7321 : }
7322 : }
7323 :
7324 : return NULL_TREE;
7325 : }
7326 :
7327 : tree
7328 219023686 : build_new_op (const op_location_t &loc, enum tree_code code, int flags,
7329 : tree arg1, tree arg2, tree arg3, tree lookups,
7330 : tree *overload, tsubst_flags_t complain)
7331 : {
7332 219023686 : struct z_candidate *candidates = 0, *cand;
7333 219023686 : releasing_vec arglist;
7334 219023686 : tree result = NULL_TREE;
7335 219023686 : bool result_valid_p = false;
7336 219023686 : enum tree_code code2 = ERROR_MARK;
7337 219023686 : enum tree_code code_orig_arg1 = ERROR_MARK;
7338 219023686 : enum tree_code code_orig_arg2 = ERROR_MARK;
7339 219023686 : bool strict_p;
7340 219023686 : bool any_viable_p;
7341 :
7342 219023686 : auto_cond_timevar tv (TV_OVERLOAD);
7343 :
7344 219023686 : if (error_operand_p (arg1)
7345 219019538 : || error_operand_p (arg2)
7346 438037185 : || error_operand_p (arg3))
7347 10187 : return error_mark_node;
7348 :
7349 219013499 : conversion_obstack_sentinel cos;
7350 :
7351 219013499 : bool ismodop = code == MODIFY_EXPR;
7352 219013499 : if (ismodop)
7353 : {
7354 11346880 : code2 = TREE_CODE (arg3);
7355 11346880 : arg3 = NULL_TREE;
7356 : }
7357 :
7358 219013499 : tree arg1_type = unlowered_expr_type (arg1);
7359 219013499 : tree arg2_type = arg2 ? unlowered_expr_type (arg2) : NULL_TREE;
7360 :
7361 219013499 : arg1 = prep_operand (arg1);
7362 :
7363 219013499 : switch (code)
7364 : {
7365 0 : case NEW_EXPR:
7366 0 : case VEC_NEW_EXPR:
7367 0 : case VEC_DELETE_EXPR:
7368 0 : case DELETE_EXPR:
7369 : /* Use build_operator_new_call and build_op_delete_call instead. */
7370 0 : gcc_unreachable ();
7371 :
7372 0 : case CALL_EXPR:
7373 : /* Use build_op_call instead. */
7374 0 : gcc_unreachable ();
7375 :
7376 18454483 : case TRUTH_ORIF_EXPR:
7377 18454483 : case TRUTH_ANDIF_EXPR:
7378 18454483 : case TRUTH_AND_EXPR:
7379 18454483 : case TRUTH_OR_EXPR:
7380 : /* These are saved for the sake of warn_logical_operator. */
7381 18454483 : code_orig_arg1 = TREE_CODE (arg1);
7382 18454483 : code_orig_arg2 = TREE_CODE (arg2);
7383 18454483 : break;
7384 51221123 : case GT_EXPR:
7385 51221123 : case LT_EXPR:
7386 51221123 : case GE_EXPR:
7387 51221123 : case LE_EXPR:
7388 51221123 : case EQ_EXPR:
7389 51221123 : case NE_EXPR:
7390 : /* These are saved for the sake of maybe_warn_bool_compare. */
7391 51221123 : code_orig_arg1 = TREE_CODE (arg1_type);
7392 51221123 : code_orig_arg2 = TREE_CODE (arg2_type);
7393 51221123 : break;
7394 :
7395 : default:
7396 : break;
7397 : }
7398 :
7399 219013499 : arg2 = prep_operand (arg2);
7400 219013499 : arg3 = prep_operand (arg3);
7401 :
7402 219013499 : if (code == COND_EXPR)
7403 : /* Use build_conditional_expr instead. */
7404 0 : gcc_unreachable ();
7405 219013499 : else if (! OVERLOAD_TYPE_P (arg1_type)
7406 413006329 : && (! arg2 || ! OVERLOAD_TYPE_P (arg2_type)))
7407 193556343 : goto builtin;
7408 :
7409 25457156 : if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
7410 : {
7411 314514 : arg2 = integer_zero_node;
7412 314514 : arg2_type = integer_type_node;
7413 : }
7414 :
7415 25457156 : arglist->quick_push (arg1);
7416 25457156 : if (arg2 != NULL_TREE)
7417 20446866 : arglist->quick_push (arg2);
7418 25457156 : if (arg3 != NULL_TREE)
7419 0 : arglist->quick_push (arg3);
7420 :
7421 25457156 : result = add_operator_candidates (&candidates, code, code2, arglist,
7422 : lookups, flags, complain);
7423 25457123 : if (result == error_mark_node)
7424 : return error_mark_node;
7425 :
7426 25457111 : switch (code)
7427 : {
7428 : case COMPOUND_EXPR:
7429 : case ADDR_EXPR:
7430 : /* For these, the built-in candidates set is empty
7431 : [over.match.oper]/3. We don't want non-strict matches
7432 : because exact matches are always possible with built-in
7433 : operators. The built-in candidate set for COMPONENT_REF
7434 : would be empty too, but since there are no such built-in
7435 : operators, we accept non-strict matches for them. */
7436 : strict_p = true;
7437 : break;
7438 :
7439 23543949 : default:
7440 23543949 : strict_p = false;
7441 23543949 : break;
7442 : }
7443 :
7444 25457111 : candidates = splice_viable (candidates, strict_p, &any_viable_p);
7445 25457111 : if (!any_viable_p)
7446 : {
7447 1964196 : switch (code)
7448 : {
7449 357 : case POSTINCREMENT_EXPR:
7450 357 : case POSTDECREMENT_EXPR:
7451 : /* Don't try anything fancy if we're not allowed to produce
7452 : errors. */
7453 357 : if (!(complain & tf_error))
7454 : return error_mark_node;
7455 :
7456 : /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
7457 : distinguish between prefix and postfix ++ and
7458 : operator++() was used for both, so we allow this with
7459 : -fpermissive. */
7460 : else
7461 : {
7462 42 : tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
7463 30 : const char *msg = (flag_permissive)
7464 42 : ? G_("no %<%D(int)%> declared for postfix %qs,"
7465 : " trying prefix operator instead")
7466 : : G_("no %<%D(int)%> declared for postfix %qs");
7467 42 : permerror (loc, msg, fnname, OVL_OP_INFO (false, code)->name);
7468 : }
7469 :
7470 42 : if (!flag_permissive)
7471 30 : return error_mark_node;
7472 :
7473 12 : if (code == POSTINCREMENT_EXPR)
7474 : code = PREINCREMENT_EXPR;
7475 : else
7476 0 : code = PREDECREMENT_EXPR;
7477 12 : result = build_new_op (loc, code, flags, arg1, NULL_TREE,
7478 : NULL_TREE, lookups, overload, complain);
7479 12 : break;
7480 :
7481 : /* The caller will deal with these. */
7482 : case ADDR_EXPR:
7483 : case COMPOUND_EXPR:
7484 : case COMPONENT_REF:
7485 : case CO_AWAIT_EXPR:
7486 : result = NULL_TREE;
7487 : result_valid_p = true;
7488 : break;
7489 :
7490 47945 : default:
7491 47945 : if (complain & tf_error)
7492 : {
7493 : /* If one of the arguments of the operator represents
7494 : an invalid use of member function pointer, try to report
7495 : a meaningful error ... */
7496 1346 : if (invalid_nonstatic_memfn_p (loc, arg1, tf_error)
7497 1343 : || invalid_nonstatic_memfn_p (loc, arg2, tf_error)
7498 2683 : || invalid_nonstatic_memfn_p (loc, arg3, tf_error))
7499 : /* We displayed the error message. */;
7500 : else
7501 : {
7502 : /* ... Otherwise, report the more generic
7503 : "no matching operator found" error */
7504 1337 : auto_diagnostic_group d;
7505 1337 : op_error (loc, code, code2, arg1, arg2, arg3, false);
7506 1337 : print_z_candidates (loc, candidates);
7507 1337 : }
7508 : }
7509 47945 : result = error_mark_node;
7510 47945 : break;
7511 : }
7512 : }
7513 : else
7514 : {
7515 23492915 : cand = tourney (candidates, complain);
7516 23492915 : if (cand == 0)
7517 : {
7518 184 : if (complain & tf_error)
7519 : {
7520 129 : auto_diagnostic_group d;
7521 129 : op_error (loc, code, code2, arg1, arg2, arg3, true);
7522 129 : print_z_candidates (loc, candidates);
7523 129 : }
7524 184 : result = error_mark_node;
7525 184 : if (overload)
7526 165 : *overload = error_mark_node;
7527 : }
7528 23492731 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
7529 : {
7530 19306559 : if (overload)
7531 : {
7532 14791957 : if (cand->rewritten ())
7533 : /* build_min_non_dep_op_overload needs to know whether the
7534 : candidate is rewritten/reversed. */
7535 1840238 : *overload = build_tree_list (build_int_cst (integer_type_node,
7536 1840238 : cand->flags),
7537 : cand->fn);
7538 : else
7539 12951719 : *overload = cand->fn;
7540 : }
7541 :
7542 19306559 : if (resolve_args (arglist, complain) == NULL)
7543 6 : result = error_mark_node;
7544 : else
7545 : {
7546 19306553 : tsubst_flags_t ocomplain = complain;
7547 19306553 : if (cand->rewritten ())
7548 : /* We'll wrap this call in another one. */
7549 1840844 : ocomplain &= ~tf_decltype;
7550 19306553 : if (cand->reversed ())
7551 : {
7552 : /* We swapped these in add_candidate, swap them back now. */
7553 21322 : std::swap (cand->convs[0], cand->convs[1]);
7554 21322 : if (cand->fn == current_function_decl)
7555 1 : warning_at (loc, 0, "in C++20 this comparison calls the "
7556 : "current function recursively with reversed "
7557 : "arguments");
7558 : }
7559 19306553 : result = build_over_call (cand, LOOKUP_NORMAL, ocomplain);
7560 : }
7561 :
7562 35812756 : if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn))
7563 : /* There won't be a CALL_EXPR. */;
7564 16498687 : else if (result && result != error_mark_node)
7565 : {
7566 16496051 : tree call = extract_call_expr (result);
7567 16496051 : CALL_EXPR_OPERATOR_SYNTAX (call) = true;
7568 :
7569 : /* Specify evaluation order as per P0145R2. */
7570 16496051 : CALL_EXPR_ORDERED_ARGS (call) = false;
7571 16496051 : switch (op_is_ordered (code))
7572 : {
7573 3692380 : case -1:
7574 3692380 : CALL_EXPR_REVERSE_ARGS (call) = true;
7575 3692380 : break;
7576 :
7577 488723 : case 1:
7578 488723 : CALL_EXPR_ORDERED_ARGS (call) = true;
7579 488723 : break;
7580 :
7581 : default:
7582 : break;
7583 : }
7584 : }
7585 :
7586 : /* If this was a C++20 rewritten comparison, adjust the result. */
7587 19306556 : if (cand->rewritten ())
7588 : {
7589 1840844 : switch (code)
7590 : {
7591 9892 : case EQ_EXPR:
7592 9892 : gcc_checking_assert (cand->reversed ());
7593 899204 : gcc_fallthrough ();
7594 899204 : case NE_EXPR:
7595 899204 : if (result == error_mark_node)
7596 : ;
7597 : /* If a rewritten operator== candidate is selected by
7598 : overload resolution for an operator @, its return type
7599 : shall be cv bool.... */
7600 899202 : else if (TREE_CODE (TREE_TYPE (result)) != BOOLEAN_TYPE)
7601 : {
7602 44 : if (complain & tf_error)
7603 : {
7604 6 : auto_diagnostic_group d;
7605 6 : error_at (loc, "return type of %qD is not %qs",
7606 : cand->fn, "bool");
7607 6 : inform (loc, "used as rewritten candidate for "
7608 : "comparison of %qT and %qT",
7609 : arg1_type, arg2_type);
7610 6 : }
7611 44 : result = error_mark_node;
7612 : }
7613 899158 : else if (code == NE_EXPR)
7614 : /* !(y == x) or !(x == y) */
7615 889287 : result = build1_loc (loc, TRUTH_NOT_EXPR,
7616 : boolean_type_node, result);
7617 : break;
7618 :
7619 : /* If a rewritten operator<=> candidate is selected by
7620 : overload resolution for an operator @, x @ y is
7621 : interpreted as 0 @ (y <=> x) if the selected candidate is
7622 : a synthesized candidate with reversed order of parameters,
7623 : or (x <=> y) @ 0 otherwise, using the selected rewritten
7624 : operator<=> candidate. */
7625 637 : case SPACESHIP_EXPR:
7626 637 : if (!cand->reversed ())
7627 : /* We're in the build_new_op call below for an outer
7628 : reversed call; we don't need to do anything more. */
7629 : break;
7630 941326 : gcc_fallthrough ();
7631 941326 : case LT_EXPR:
7632 941326 : case LE_EXPR:
7633 941326 : case GT_EXPR:
7634 941326 : case GE_EXPR:
7635 941326 : {
7636 941326 : tree lhs = result;
7637 941326 : tree rhs = integer_zero_node;
7638 941326 : if (cand->reversed ())
7639 2148 : std::swap (lhs, rhs);
7640 941326 : warning_sentinel ws (warn_zero_as_null_pointer_constant);
7641 941326 : result = build_new_op (loc, code,
7642 : LOOKUP_NORMAL|LOOKUP_REWRITTEN,
7643 : lhs, rhs, NULL_TREE, lookups,
7644 : NULL, complain);
7645 941326 : }
7646 941326 : break;
7647 :
7648 0 : default:
7649 0 : gcc_unreachable ();
7650 : }
7651 : }
7652 :
7653 : /* In an expression of the form `a[]' where cand->fn
7654 : which is operator[] turns out to be a static member function,
7655 : `a' is none-the-less evaluated. */
7656 19306556 : if (code == ARRAY_REF)
7657 427895 : result = keep_unused_object_arg (result, arg1, cand->fn);
7658 : }
7659 : else
7660 : {
7661 : /* Give any warnings we noticed during overload resolution. */
7662 4186172 : if (cand->warnings && (complain & tf_warning))
7663 : {
7664 : struct candidate_warning *w;
7665 0 : for (w = cand->warnings; w; w = w->next)
7666 0 : joust (cand, w->loser, 1, complain);
7667 : }
7668 :
7669 : /* Check for comparison of different enum types. */
7670 4186172 : switch (code)
7671 : {
7672 3288162 : case GT_EXPR:
7673 3288162 : case LT_EXPR:
7674 3288162 : case GE_EXPR:
7675 3288162 : case LE_EXPR:
7676 3288162 : case EQ_EXPR:
7677 3288162 : case NE_EXPR:
7678 3288162 : if (TREE_CODE (arg1_type) == ENUMERAL_TYPE
7679 3078412 : && TREE_CODE (arg2_type) == ENUMERAL_TYPE
7680 6288796 : && (TYPE_MAIN_VARIANT (arg1_type)
7681 3000634 : != TYPE_MAIN_VARIANT (arg2_type)))
7682 : {
7683 80 : if (cxx_dialect >= cxx26
7684 26 : && (complain & tf_warning_or_error) == 0)
7685 2 : result = error_mark_node;
7686 78 : else if (cxx_dialect >= cxx26 || (complain & tf_warning))
7687 130 : emit_diagnostic ((cxx_dialect >= cxx26
7688 : ? diagnostics::kind::pedwarn
7689 : : diagnostics::kind::warning),
7690 77 : loc, OPT_Wenum_compare,
7691 : "comparison between %q#T and %q#T",
7692 : arg1_type, arg2_type);
7693 : }
7694 : break;
7695 : default:
7696 : break;
7697 : }
7698 :
7699 : /* "If a built-in candidate is selected by overload resolution, the
7700 : operands of class type are converted to the types of the
7701 : corresponding parameters of the selected operation function,
7702 : except that the second standard conversion sequence of a
7703 : user-defined conversion sequence (12.3.3.1.2) is not applied." */
7704 4186172 : conversion *conv = cand->convs[0];
7705 4186172 : if (conv->user_conv_p)
7706 : {
7707 65021 : conv = strip_standard_conversion (conv);
7708 65021 : arg1 = convert_like (conv, arg1, complain);
7709 : }
7710 :
7711 4186172 : if (arg2)
7712 : {
7713 3689195 : conv = cand->convs[1];
7714 3689195 : if (conv->user_conv_p)
7715 : {
7716 11272 : conv = strip_standard_conversion (conv);
7717 11272 : arg2 = convert_like (conv, arg2, complain);
7718 : }
7719 : }
7720 :
7721 4186172 : if (arg3)
7722 : {
7723 0 : conv = cand->convs[2];
7724 0 : if (conv->user_conv_p)
7725 : {
7726 0 : conv = strip_standard_conversion (conv);
7727 0 : arg3 = convert_like (conv, arg3, complain);
7728 : }
7729 : }
7730 : }
7731 : }
7732 :
7733 25456763 : if (result || result_valid_p)
7734 : return result;
7735 :
7736 4186170 : builtin:
7737 197742513 : switch (code)
7738 : {
7739 4814117 : case MODIFY_EXPR:
7740 4814117 : return cp_build_modify_expr (loc, arg1, code2, arg2, complain);
7741 :
7742 20775877 : case INDIRECT_REF:
7743 20775877 : return cp_build_indirect_ref (loc, arg1, RO_UNARY_STAR, complain);
7744 :
7745 18452300 : case TRUTH_ANDIF_EXPR:
7746 18452300 : case TRUTH_ORIF_EXPR:
7747 18452300 : case TRUTH_AND_EXPR:
7748 18452300 : case TRUTH_OR_EXPR:
7749 18452300 : if ((complain & tf_warning) && !processing_template_decl)
7750 7746945 : warn_logical_operator (loc, code, boolean_type_node,
7751 : code_orig_arg1, arg1,
7752 : code_orig_arg2, arg2);
7753 : /* Fall through. */
7754 65002062 : case GT_EXPR:
7755 65002062 : case LT_EXPR:
7756 65002062 : case GE_EXPR:
7757 65002062 : case LE_EXPR:
7758 65002062 : case EQ_EXPR:
7759 65002062 : case NE_EXPR:
7760 65002062 : if ((complain & tf_warning)
7761 58923787 : && ((code_orig_arg1 == BOOLEAN_TYPE)
7762 58923787 : ^ (code_orig_arg2 == BOOLEAN_TYPE)))
7763 26521 : maybe_warn_bool_compare (loc, code, arg1, arg2);
7764 58923787 : if (complain & tf_warning && warn_tautological_compare)
7765 451575 : warn_tautological_cmp (loc, code, arg1, arg2);
7766 : /* Fall through. */
7767 135410081 : case SPACESHIP_EXPR:
7768 135410081 : case PLUS_EXPR:
7769 135410081 : case MINUS_EXPR:
7770 135410081 : case MULT_EXPR:
7771 135410081 : case TRUNC_DIV_EXPR:
7772 135410081 : case MAX_EXPR:
7773 135410081 : case MIN_EXPR:
7774 135410081 : case LSHIFT_EXPR:
7775 135410081 : case RSHIFT_EXPR:
7776 135410081 : case TRUNC_MOD_EXPR:
7777 135410081 : case BIT_AND_EXPR:
7778 135410081 : case BIT_IOR_EXPR:
7779 135410081 : case BIT_XOR_EXPR:
7780 135410081 : return cp_build_binary_op (loc, code, arg1, arg2, complain);
7781 :
7782 31249288 : case UNARY_PLUS_EXPR:
7783 31249288 : case NEGATE_EXPR:
7784 31249288 : case BIT_NOT_EXPR:
7785 31249288 : case TRUTH_NOT_EXPR:
7786 31249288 : case PREINCREMENT_EXPR:
7787 31249288 : case POSTINCREMENT_EXPR:
7788 31249288 : case PREDECREMENT_EXPR:
7789 31249288 : case POSTDECREMENT_EXPR:
7790 31249288 : case REALPART_EXPR:
7791 31249288 : case IMAGPART_EXPR:
7792 31249288 : case ABS_EXPR:
7793 31249288 : case CO_AWAIT_EXPR:
7794 31249288 : return cp_build_unary_op (code, arg1, false, complain);
7795 :
7796 2441179 : case ARRAY_REF:
7797 2441179 : return cp_build_array_ref (input_location, arg1, arg2, complain);
7798 :
7799 769 : case MEMBER_REF:
7800 769 : return build_m_component_ref (cp_build_indirect_ref (loc, arg1,
7801 : RO_ARROW_STAR,
7802 : complain),
7803 769 : arg2, complain);
7804 :
7805 : /* The caller will deal with these. */
7806 : case ADDR_EXPR:
7807 : case COMPONENT_REF:
7808 : case COMPOUND_EXPR:
7809 : return NULL_TREE;
7810 :
7811 0 : default:
7812 0 : gcc_unreachable ();
7813 : }
7814 : return NULL_TREE;
7815 219023650 : }
7816 :
7817 : /* Build a new call to operator[]. This may change ARGS. */
7818 :
7819 : tree
7820 704 : build_op_subscript (const op_location_t &loc, tree obj,
7821 : vec<tree, va_gc> **args, tree *overload,
7822 : tsubst_flags_t complain)
7823 : {
7824 704 : struct z_candidate *candidates = 0, *cand;
7825 704 : tree fns, first_mem_arg = NULL_TREE;
7826 704 : bool any_viable_p;
7827 704 : tree result = NULL_TREE;
7828 :
7829 704 : auto_cond_timevar tv (TV_OVERLOAD);
7830 :
7831 704 : obj = mark_lvalue_use (obj);
7832 :
7833 704 : if (error_operand_p (obj))
7834 0 : return error_mark_node;
7835 :
7836 704 : tree type = TREE_TYPE (obj);
7837 :
7838 704 : obj = prep_operand (obj);
7839 :
7840 704 : if (TYPE_BINFO (type))
7841 : {
7842 704 : fns = lookup_fnfields (TYPE_BINFO (type), ovl_op_identifier (ARRAY_REF),
7843 : 1, complain);
7844 704 : if (fns == error_mark_node)
7845 : return error_mark_node;
7846 : }
7847 : else
7848 : fns = NULL_TREE;
7849 :
7850 704 : if (args != NULL && *args != NULL)
7851 : {
7852 704 : *args = resolve_args (*args, complain);
7853 704 : if (*args == NULL)
7854 0 : return error_mark_node;
7855 : }
7856 :
7857 704 : conversion_obstack_sentinel cos;
7858 :
7859 704 : if (fns)
7860 : {
7861 702 : first_mem_arg = obj;
7862 :
7863 702 : add_candidates (BASELINK_FUNCTIONS (fns),
7864 : first_mem_arg, *args, NULL_TREE,
7865 : NULL_TREE, false,
7866 702 : BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
7867 : LOOKUP_NORMAL, &candidates, complain);
7868 : }
7869 :
7870 : /* Be strict here because if we choose a bad conversion candidate, the
7871 : errors we get won't mention the call context. */
7872 704 : candidates = splice_viable (candidates, true, &any_viable_p);
7873 704 : if (!any_viable_p)
7874 : {
7875 45 : if (complain & tf_error)
7876 : {
7877 1 : auto_diagnostic_group d;
7878 2 : error ("no match for call to %<%T::operator[] (%A)%>",
7879 1 : TREE_TYPE (obj), build_tree_list_vec (*args));
7880 1 : print_z_candidates (loc, candidates);
7881 1 : }
7882 45 : result = error_mark_node;
7883 : }
7884 : else
7885 : {
7886 659 : cand = tourney (candidates, complain);
7887 659 : if (cand == 0)
7888 : {
7889 0 : if (complain & tf_error)
7890 : {
7891 0 : auto_diagnostic_group d;
7892 0 : error ("call of %<%T::operator[] (%A)%> is ambiguous",
7893 0 : TREE_TYPE (obj), build_tree_list_vec (*args));
7894 0 : print_z_candidates (loc, candidates);
7895 0 : }
7896 0 : result = error_mark_node;
7897 : }
7898 659 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL
7899 659 : && DECL_OVERLOADED_OPERATOR_P (cand->fn)
7900 1318 : && DECL_OVERLOADED_OPERATOR_IS (cand->fn, ARRAY_REF))
7901 : {
7902 659 : if (overload)
7903 659 : *overload = cand->fn;
7904 659 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
7905 1318 : if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn))
7906 : /* There won't be a CALL_EXPR. */;
7907 659 : else if (result && result != error_mark_node)
7908 : {
7909 659 : tree call = extract_call_expr (result);
7910 659 : CALL_EXPR_OPERATOR_SYNTAX (call) = true;
7911 :
7912 : /* Specify evaluation order as per P0145R2. */
7913 659 : CALL_EXPR_ORDERED_ARGS (call) = op_is_ordered (ARRAY_REF) == 1;
7914 : }
7915 :
7916 : /* In an expression of the form `a[]' where cand->fn
7917 : which is operator[] turns out to be a static member function,
7918 : `a' is none-the-less evaluated. */
7919 659 : result = keep_unused_object_arg (result, obj, cand->fn);
7920 : }
7921 : else
7922 0 : gcc_unreachable ();
7923 : }
7924 :
7925 704 : return result;
7926 704 : }
7927 :
7928 : /* CALL was returned by some call-building function; extract the actual
7929 : CALL_EXPR from any bits that have been tacked on, e.g. by
7930 : convert_from_reference. */
7931 :
7932 : tree
7933 45593066 : extract_call_expr (tree call)
7934 : {
7935 45593748 : while (TREE_CODE (call) == COMPOUND_EXPR)
7936 682 : call = TREE_OPERAND (call, 1);
7937 45593066 : if (REFERENCE_REF_P (call))
7938 10416822 : call = TREE_OPERAND (call, 0);
7939 45593066 : if (TREE_CODE (call) == TARGET_EXPR)
7940 3092512 : call = TARGET_EXPR_INITIAL (call);
7941 :
7942 45593066 : if (TREE_CODE (call) != CALL_EXPR
7943 855193 : && TREE_CODE (call) != AGGR_INIT_EXPR
7944 749173 : && call != error_mark_node)
7945 749155 : return NULL_TREE;
7946 : return call;
7947 : }
7948 :
7949 : /* Returns true if FN has two parameters, of which the second has type
7950 : size_t. */
7951 :
7952 : static bool
7953 678821 : second_parm_is_size_t (tree fn)
7954 : {
7955 678821 : tree t = FUNCTION_ARG_CHAIN (fn);
7956 678821 : if (!t || !same_type_p (TREE_VALUE (t), size_type_node))
7957 : return false;
7958 15 : t = TREE_CHAIN (t);
7959 15 : if (t == void_list_node)
7960 9 : return true;
7961 : return false;
7962 : }
7963 :
7964 : /* True if T, an allocation function, has std::align_val_t as its second
7965 : argument. */
7966 :
7967 : bool
7968 352028 : aligned_allocation_fn_p (tree t)
7969 : {
7970 352028 : if (!aligned_new_threshold)
7971 : return false;
7972 :
7973 342206 : tree a = FUNCTION_ARG_CHAIN (t);
7974 342206 : return (a && same_type_p (TREE_VALUE (a), align_type_node));
7975 : }
7976 :
7977 : /* True if T is std::destroying_delete_t. */
7978 :
7979 : static bool
7980 5822663 : std_destroying_delete_t_p (tree t)
7981 : {
7982 5822663 : return (TYPE_CONTEXT (t) == std_node
7983 5822663 : && id_equal (TYPE_IDENTIFIER (t), "destroying_delete_t"));
7984 : }
7985 :
7986 : /* A deallocation function with at least two parameters whose second parameter
7987 : type is of type std::destroying_delete_t is a destroying operator delete. A
7988 : destroying operator delete shall be a class member function named operator
7989 : delete. [ Note: Array deletion cannot use a destroying operator
7990 : delete. --end note ] */
7991 :
7992 : tree
7993 5822678 : destroying_delete_p (tree t)
7994 : {
7995 5822678 : tree a = TYPE_ARG_TYPES (TREE_TYPE (t));
7996 5822678 : if (!a || !TREE_CHAIN (a))
7997 : return NULL_TREE;
7998 5822663 : tree type = TREE_VALUE (TREE_CHAIN (a));
7999 5822663 : return std_destroying_delete_t_p (type) ? type : NULL_TREE;
8000 : }
8001 :
8002 : struct dealloc_info
8003 : {
8004 : bool sized;
8005 : bool aligned;
8006 : tree destroying;
8007 : };
8008 :
8009 : /* Returns true iff T, an element of an OVERLOAD chain, is a usual deallocation
8010 : function (3.7.4.2 [basic.stc.dynamic.deallocation]). If so, and DI is
8011 : non-null, also set *DI. */
8012 :
8013 : static bool
8014 3975769 : usual_deallocation_fn_p (tree t, dealloc_info *di)
8015 : {
8016 3975769 : if (di) *di = dealloc_info();
8017 :
8018 : /* A template instance is never a usual deallocation function,
8019 : regardless of its signature. */
8020 3975769 : if (TREE_CODE (t) == TEMPLATE_DECL
8021 3975769 : || primary_template_specialization_p (t))
8022 : return false;
8023 :
8024 : /* A usual deallocation function is a deallocation function whose parameters
8025 : after the first are
8026 : - optionally, a parameter of type std::destroying_delete_t, then
8027 : - optionally, a parameter of type std::size_t, then
8028 : - optionally, a parameter of type std::align_val_t. */
8029 3975757 : bool global = DECL_NAMESPACE_SCOPE_P (t);
8030 3975757 : tree chain = FUNCTION_ARG_CHAIN (t);
8031 3975757 : if (chain && destroying_delete_p (t))
8032 : {
8033 86 : if (di) di->destroying = TREE_VALUE (chain);
8034 86 : chain = TREE_CHAIN (chain);
8035 : }
8036 3975757 : if (chain
8037 3975754 : && (!global || flag_sized_deallocation)
8038 7936152 : && same_type_p (TREE_VALUE (chain), size_type_node))
8039 : {
8040 1146168 : if (di) di->sized = true;
8041 1146168 : chain = TREE_CHAIN (chain);
8042 : }
8043 3975754 : if (chain && aligned_new_threshold
8044 7935119 : && same_type_p (TREE_VALUE (chain), align_type_node))
8045 : {
8046 1701397 : if (di) di->aligned = true;
8047 1701397 : chain = TREE_CHAIN (chain);
8048 : }
8049 3975757 : return (chain == void_list_node);
8050 : }
8051 :
8052 : /* Just return whether FN is a usual deallocation function. */
8053 :
8054 : bool
8055 8841 : usual_deallocation_fn_p (tree fn)
8056 : {
8057 8841 : return usual_deallocation_fn_p (fn, NULL);
8058 : }
8059 :
8060 : /* Build a call to operator delete. This has to be handled very specially,
8061 : because the restrictions on what signatures match are different from all
8062 : other call instances. For a normal delete, only a delete taking (void *)
8063 : or (void *, size_t) is accepted. For a placement delete, only an exact
8064 : match with the placement new is accepted.
8065 :
8066 : CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
8067 : ADDR is the pointer to be deleted.
8068 : SIZE is the size of the memory block to be deleted.
8069 : GLOBAL_P is true if the delete-expression should not consider
8070 : class-specific delete operators.
8071 : CORO_P is true if the allocation is for a coroutine, where the two argument
8072 : usual deallocation should be chosen in preference to the single argument
8073 : version in a class context.
8074 : PLACEMENT is the corresponding placement new call, or NULL_TREE.
8075 :
8076 : If this call to "operator delete" is being generated as part to
8077 : deallocate memory allocated via a new-expression (as per [expr.new]
8078 : which requires that if the initialization throws an exception then
8079 : we call a deallocation function), then ALLOC_FN is the allocation
8080 : function. */
8081 :
8082 : static tree
8083 1257843 : build_op_delete_call_1 (enum tree_code code, tree addr, tree size,
8084 : bool global_p, bool coro_p, tree placement,
8085 : tree alloc_fn, tsubst_flags_t complain)
8086 : {
8087 1257843 : tree fn = NULL_TREE;
8088 1257843 : tree fns, fnname, type, t;
8089 1257843 : dealloc_info di_fn = { };
8090 :
8091 1257843 : if (addr == error_mark_node)
8092 : return error_mark_node;
8093 :
8094 1257843 : type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
8095 :
8096 1257843 : fnname = ovl_op_identifier (false, code);
8097 :
8098 872548 : if (CLASS_TYPE_P (type)
8099 872512 : && COMPLETE_TYPE_P (complete_type (type))
8100 2130320 : && !global_p)
8101 : /* In [class.free]
8102 :
8103 : If the result of the lookup is ambiguous or inaccessible, or if
8104 : the lookup selects a placement deallocation function, the
8105 : program is ill-formed.
8106 :
8107 : Therefore, we ask lookup_fnfields to complain about ambiguity. */
8108 : {
8109 516262 : fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1, complain);
8110 516262 : if (fns == error_mark_node)
8111 : return error_mark_node;
8112 : }
8113 : else
8114 : fns = NULL_TREE;
8115 :
8116 516259 : if (fns == NULL_TREE)
8117 1256768 : fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE);
8118 :
8119 : /* Strip const and volatile from addr. */
8120 1257840 : tree oaddr = addr;
8121 1257840 : addr = cp_convert (ptr_type_node, addr, complain);
8122 :
8123 1257840 : tree excluded_destroying = NULL_TREE;
8124 :
8125 1257840 : if (placement)
8126 : {
8127 : /* "A declaration of a placement deallocation function matches the
8128 : declaration of a placement allocation function if it has the same
8129 : number of parameters and, after parameter transformations (8.3.5),
8130 : all parameter types except the first are identical."
8131 :
8132 : So we build up the function type we want and ask instantiate_type
8133 : to get it for us. */
8134 679705 : t = FUNCTION_ARG_CHAIN (alloc_fn);
8135 679705 : t = tree_cons (NULL_TREE, ptr_type_node, t);
8136 679705 : t = build_function_type (void_type_node, t);
8137 :
8138 679705 : fn = instantiate_type (t, fns, tf_none);
8139 679705 : if (fn == error_mark_node)
8140 : return NULL_TREE;
8141 :
8142 678821 : fn = MAYBE_BASELINK_FUNCTIONS (fn);
8143 :
8144 : /* "If the lookup finds the two-parameter form of a usual deallocation
8145 : function (3.7.4.2) and that function, considered as a placement
8146 : deallocation function, would have been selected as a match for the
8147 : allocation function, the program is ill-formed." */
8148 678821 : if (second_parm_is_size_t (fn))
8149 : {
8150 9 : const char *const msg1
8151 : = G_("exception cleanup for this placement new selects "
8152 : "non-placement %<operator delete%>");
8153 9 : const char *const msg2
8154 : = G_("%qD is a usual (non-placement) deallocation "
8155 : "function in C++14 (or with %<-fsized-deallocation%>)");
8156 :
8157 : /* But if the class has an operator delete (void *), then that is
8158 : the usual deallocation function, so we shouldn't complain
8159 : about using the operator delete (void *, size_t). */
8160 9 : if (DECL_CLASS_SCOPE_P (fn))
8161 18 : for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns)))
8162 : {
8163 9 : if (usual_deallocation_fn_p (elt)
8164 9 : && FUNCTION_ARG_CHAIN (elt) == void_list_node)
8165 3 : goto ok;
8166 : }
8167 : /* Before C++14 a two-parameter global deallocation function is
8168 : always a placement deallocation function, but warn if
8169 : -Wc++14-compat. */
8170 3 : else if (!flag_sized_deallocation)
8171 : {
8172 1 : if (complain & tf_warning)
8173 : {
8174 1 : auto_diagnostic_group d;
8175 1 : if (warning (OPT_Wc__14_compat, msg1))
8176 1 : inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
8177 1 : }
8178 1 : goto ok;
8179 : }
8180 :
8181 5 : if (complain & tf_warning_or_error)
8182 : {
8183 5 : auto_diagnostic_group d;
8184 5 : if (permerror (input_location, msg1))
8185 : {
8186 : /* Only mention C++14 for namespace-scope delete. */
8187 5 : if (DECL_NAMESPACE_SCOPE_P (fn))
8188 2 : inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
8189 : else
8190 3 : inform (DECL_SOURCE_LOCATION (fn),
8191 : "%qD is a usual (non-placement) deallocation "
8192 : "function", fn);
8193 : }
8194 5 : }
8195 : else
8196 0 : return error_mark_node;
8197 1256956 : ok:;
8198 : }
8199 : }
8200 : else
8201 : /* "Any non-placement deallocation function matches a non-placement
8202 : allocation function. If the lookup finds a single matching
8203 : deallocation function, that function will be called; otherwise, no
8204 : deallocation function will be called." */
8205 5123198 : for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns)))
8206 : {
8207 3966928 : dealloc_info di_elt;
8208 3966928 : if (usual_deallocation_fn_p (elt, &di_elt))
8209 : {
8210 : /* If we're called for an EH cleanup in a new-expression, we can't
8211 : use a destroying delete; the exception was thrown before the
8212 : object was constructed. */
8213 2290855 : if (alloc_fn && di_elt.destroying)
8214 : {
8215 13 : excluded_destroying = elt;
8216 1167642 : continue;
8217 : }
8218 :
8219 2290842 : if (!fn)
8220 : {
8221 578110 : fn = elt;
8222 578110 : di_fn = di_elt;
8223 578110 : continue;
8224 : }
8225 :
8226 : /* -- If any of the deallocation functions is a destroying
8227 : operator delete, all deallocation functions that are not
8228 : destroying operator deletes are eliminated from further
8229 : consideration. */
8230 1712732 : if (di_elt.destroying != di_fn.destroying)
8231 : {
8232 12 : if (di_elt.destroying)
8233 : {
8234 6 : fn = elt;
8235 6 : di_fn = di_elt;
8236 : }
8237 12 : continue;
8238 : }
8239 :
8240 : /* -- If the type has new-extended alignment, a function with a
8241 : parameter of type std::align_val_t is preferred; otherwise a
8242 : function without such a parameter is preferred. If exactly one
8243 : preferred function is found, that function is selected and the
8244 : selection process terminates. If more than one preferred
8245 : function is found, all non-preferred functions are eliminated
8246 : from further consideration. */
8247 1712720 : if (aligned_new_threshold)
8248 : {
8249 1712453 : bool want_align = type_has_new_extended_alignment (type);
8250 1712453 : if (di_elt.aligned != di_fn.aligned)
8251 : {
8252 589507 : if (want_align == di_elt.aligned)
8253 : {
8254 552116 : fn = elt;
8255 552116 : di_fn = di_elt;
8256 : }
8257 589507 : continue;
8258 : }
8259 : }
8260 :
8261 : /* -- If the deallocation functions have class scope, the one
8262 : without a parameter of type std::size_t is selected. */
8263 1123213 : bool want_size;
8264 1123213 : if (DECL_CLASS_SCOPE_P (fn) && !coro_p)
8265 : want_size = false;
8266 :
8267 : /* -- If the type is complete and if, for the second alternative
8268 : (delete array) only, the operand is a pointer to a class type
8269 : with a non-trivial destructor or a (possibly multi-dimensional)
8270 : array thereof, the function with a parameter of type std::size_t
8271 : is selected.
8272 :
8273 : -- Otherwise, it is unspecified whether a deallocation function
8274 : with a parameter of type std::size_t is selected. */
8275 : else
8276 : {
8277 1123105 : want_size = COMPLETE_TYPE_P (type);
8278 1123105 : if (code == VEC_DELETE_EXPR
8279 1123105 : && !TYPE_VEC_NEW_USES_COOKIE (type))
8280 : /* We need a cookie to determine the array size. */
8281 : want_size = false;
8282 : }
8283 1123213 : gcc_assert (di_fn.sized != di_elt.sized);
8284 1123213 : if (want_size == di_elt.sized)
8285 : {
8286 45883 : fn = elt;
8287 45883 : di_fn = di_elt;
8288 : }
8289 : }
8290 : }
8291 :
8292 : /* If we have a matching function, call it. */
8293 1256956 : if (fn)
8294 : {
8295 1256931 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8296 :
8297 : /* If the FN is a member function, make sure that it is
8298 : accessible. */
8299 1256931 : if (BASELINK_P (fns))
8300 1005 : perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn,
8301 : complain);
8302 :
8303 : /* Core issue 901: It's ok to new a type with deleted delete. */
8304 1256931 : if (DECL_DELETED_FN (fn) && alloc_fn)
8305 : return NULL_TREE;
8306 :
8307 1256925 : tree ret;
8308 1256925 : if (placement)
8309 : {
8310 : /* The placement args might not be suitable for overload
8311 : resolution at this point, so build the call directly. */
8312 678821 : int nargs = call_expr_nargs (placement);
8313 678821 : tree *argarray = XALLOCAVEC (tree, nargs);
8314 678821 : int i;
8315 678821 : argarray[0] = addr;
8316 1357694 : for (i = 1; i < nargs; i++)
8317 678873 : argarray[i] = CALL_EXPR_ARG (placement, i);
8318 678821 : if (!mark_used (fn, complain) && !(complain & tf_error))
8319 0 : return error_mark_node;
8320 678821 : ret = build_cxx_call (fn, nargs, argarray, complain);
8321 : }
8322 : else
8323 : {
8324 578104 : tree destroying = di_fn.destroying;
8325 578104 : if (destroying)
8326 : {
8327 : /* Strip const and volatile from addr but retain the type of the
8328 : object. */
8329 28 : tree rtype = TREE_TYPE (TREE_TYPE (oaddr));
8330 28 : rtype = cv_unqualified (rtype);
8331 28 : rtype = TYPE_POINTER_TO (rtype);
8332 28 : addr = cp_convert (rtype, oaddr, complain);
8333 28 : destroying = build_functional_cast (input_location,
8334 : destroying, NULL_TREE,
8335 : complain);
8336 : }
8337 :
8338 578104 : releasing_vec args;
8339 578104 : args->quick_push (addr);
8340 578104 : if (destroying)
8341 28 : args->quick_push (destroying);
8342 578104 : if (di_fn.sized)
8343 539088 : args->quick_push (size);
8344 578104 : if (di_fn.aligned)
8345 : {
8346 18699 : tree al = build_int_cst (align_type_node, TYPE_ALIGN_UNIT (type));
8347 18699 : args->quick_push (al);
8348 : }
8349 578104 : ret = cp_build_function_call_vec (fn, &args, complain);
8350 578104 : }
8351 :
8352 : /* Set this flag for all callers of this function. In addition to
8353 : delete-expressions, this is called for deallocating coroutine state;
8354 : treat that as an implicit delete-expression. This is also called for
8355 : the delete if the constructor throws in a new-expression, and for a
8356 : deleting destructor (which implements a delete-expression). */
8357 : /* But leave this flag off for destroying delete to avoid wrong
8358 : assumptions in the optimizers. */
8359 1256925 : tree call = extract_call_expr (ret);
8360 1256925 : if (TREE_CODE (call) == CALL_EXPR && !destroying_delete_p (fn))
8361 1256891 : CALL_FROM_NEW_OR_DELETE_P (call) = 1;
8362 :
8363 : return ret;
8364 : }
8365 :
8366 : /* If there's only a destroying delete that we can't use because the
8367 : object isn't constructed yet, and we used global new, use global
8368 : delete as well. */
8369 25 : if (excluded_destroying
8370 25 : && DECL_NAMESPACE_SCOPE_P (alloc_fn))
8371 7 : return build_op_delete_call (code, addr, size, true, placement,
8372 7 : alloc_fn, complain);
8373 :
8374 : /* [expr.new]
8375 :
8376 : If no unambiguous matching deallocation function can be found,
8377 : propagating the exception does not cause the object's memory to
8378 : be freed. */
8379 18 : if (alloc_fn)
8380 : {
8381 15 : if ((complain & tf_warning) && !placement)
8382 : {
8383 15 : auto_diagnostic_group d;
8384 15 : bool w = warning (0,
8385 : "no corresponding deallocation function for %qD",
8386 : alloc_fn);
8387 15 : if (w && excluded_destroying)
8388 3 : inform (DECL_SOURCE_LOCATION (excluded_destroying), "destroying "
8389 : "delete %qD cannot be used to release the allocated memory"
8390 : " if the initialization throws because the object is not "
8391 : "constructed yet", excluded_destroying);
8392 15 : }
8393 : return NULL_TREE;
8394 : }
8395 :
8396 3 : if (complain & tf_error)
8397 3 : error ("no suitable %<operator %s%> for %qT",
8398 3 : OVL_OP_INFO (false, code)->name, type);
8399 3 : return error_mark_node;
8400 : }
8401 :
8402 : /* Arguments as per build_op_delete_call_1 (). */
8403 :
8404 : tree
8405 1254582 : build_op_delete_call (enum tree_code code, tree addr, tree size, bool global_p,
8406 : tree placement, tree alloc_fn, tsubst_flags_t complain)
8407 : {
8408 1254582 : return build_op_delete_call_1 (code, addr, size, global_p, /*coro_p*/false,
8409 1254582 : placement, alloc_fn, complain);
8410 : }
8411 :
8412 : /* Arguments as per build_op_delete_call_1 (). */
8413 :
8414 : tree
8415 3261 : build_coroutine_op_delete_call (enum tree_code code, tree addr, tree size,
8416 : bool global_p, tree placement, tree alloc_fn,
8417 : tsubst_flags_t complain)
8418 : {
8419 3261 : return build_op_delete_call_1 (code, addr, size, global_p, /*coro_p*/true,
8420 3261 : placement, alloc_fn, complain);
8421 : }
8422 :
8423 : /* Issue diagnostics about a disallowed access of DECL, using DIAG_DECL
8424 : in the diagnostics.
8425 :
8426 : If ISSUE_ERROR is true, then issue an error about the access, followed
8427 : by a note showing the declaration. Otherwise, just show the note.
8428 :
8429 : DIAG_DECL and DIAG_LOCATION will almost always be the same.
8430 : DIAG_LOCATION is just another DECL. NO_ACCESS_REASON is an optional
8431 : parameter used to specify why DECL wasn't accessible (e.g. ak_private
8432 : would be because DECL was private). If not using NO_ACCESS_REASON,
8433 : then it must be ak_none, and the access failure reason will be
8434 : figured out by looking at the protection of DECL. */
8435 :
8436 : void
8437 1187 : complain_about_access (tree decl, tree diag_decl, tree diag_location,
8438 : bool issue_error, access_kind no_access_reason)
8439 : {
8440 : /* If we have not already figured out why DECL is inaccessible... */
8441 1187 : if (no_access_reason == ak_none)
8442 : {
8443 : /* Examine the access of DECL to find out why. */
8444 1005 : if (TREE_PRIVATE (decl))
8445 : no_access_reason = ak_private;
8446 225 : else if (TREE_PROTECTED (decl))
8447 180 : no_access_reason = ak_protected;
8448 : }
8449 :
8450 : /* Now generate an error message depending on calculated access. */
8451 1187 : auto_diagnostic_group d;
8452 1187 : if (no_access_reason == ak_private)
8453 : {
8454 962 : if (issue_error)
8455 948 : error ("%q#D is private within this context", diag_decl);
8456 962 : inform (DECL_SOURCE_LOCATION (diag_location), "declared private here");
8457 : }
8458 225 : else if (no_access_reason == ak_protected)
8459 : {
8460 180 : if (issue_error)
8461 166 : error ("%q#D is protected within this context", diag_decl);
8462 180 : inform (DECL_SOURCE_LOCATION (diag_location), "declared protected here");
8463 : }
8464 : /* Couldn't figure out why DECL is inaccessible, so just say it's
8465 : inaccessible. */
8466 : else
8467 : {
8468 45 : if (issue_error)
8469 45 : error ("%q#D is inaccessible within this context", diag_decl);
8470 45 : inform (DECL_SOURCE_LOCATION (diag_decl), "declared here");
8471 : }
8472 1187 : }
8473 :
8474 : /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
8475 : bitwise or of LOOKUP_* values. If any errors are warnings are
8476 : generated, set *DIAGNOSTIC_FN to "error" or "warning",
8477 : respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
8478 : to NULL. */
8479 :
8480 : static tree
8481 14292345 : build_temp (tree expr, tree type, int flags,
8482 : enum diagnostics::kind *diagnostic_kind, tsubst_flags_t complain)
8483 : {
8484 14292345 : int savew, savee;
8485 :
8486 14292345 : *diagnostic_kind = diagnostics::kind::unspecified;
8487 :
8488 : /* If the source is a packed field, calling the copy constructor will require
8489 : binding the field to the reference parameter to the copy constructor, and
8490 : we'll end up with an infinite loop. If we can use a bitwise copy, then
8491 : do that now. */
8492 14292345 : if ((lvalue_kind (expr) & clk_packed)
8493 6 : && CLASS_TYPE_P (TREE_TYPE (expr))
8494 14292351 : && !type_has_nontrivial_copy_init (TREE_TYPE (expr)))
8495 3 : return get_target_expr (expr, complain);
8496 :
8497 : /* In decltype, we might have decided not to wrap this call in a TARGET_EXPR.
8498 : But it turns out to be a subexpression, so perform temporary
8499 : materialization now. */
8500 14292342 : if (TREE_CODE (expr) == CALL_EXPR
8501 6 : && CLASS_TYPE_P (type)
8502 14292348 : && same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
8503 3 : expr = build_cplus_new (type, expr, complain);
8504 :
8505 14292342 : savew = warningcount + werrorcount, savee = errorcount;
8506 14292342 : releasing_vec args (make_tree_vector_single (expr));
8507 14292342 : expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
8508 : &args, type, flags, complain);
8509 14292342 : if (warningcount + werrorcount > savew)
8510 0 : *diagnostic_kind = diagnostics::kind::warning;
8511 14292342 : else if (errorcount > savee)
8512 78 : *diagnostic_kind = diagnostics::kind::error;
8513 14292342 : return expr;
8514 14292342 : }
8515 :
8516 : /* Get any location for EXPR, falling back to input_location.
8517 :
8518 : If the result is in a system header and is the virtual location for
8519 : a token coming from the expansion of a macro, unwind it to the
8520 : location of the expansion point of the macro (e.g. to avoid the
8521 : diagnostic being suppressed for expansions of NULL where "NULL" is
8522 : in a system header). */
8523 :
8524 : static location_t
8525 2428147 : get_location_for_expr_unwinding_for_system_header (tree expr)
8526 : {
8527 2428147 : location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
8528 2428147 : loc = expansion_point_location_if_in_system_header (loc);
8529 2428147 : return loc;
8530 : }
8531 :
8532 : /* Perform warnings about peculiar, but valid, conversions from/to NULL.
8533 : Also handle a subset of zero as null warnings.
8534 : EXPR is implicitly converted to type TOTYPE.
8535 : FN and ARGNUM are used for diagnostics. */
8536 :
8537 : static void
8538 547226895 : conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
8539 : {
8540 : /* Issue warnings about peculiar, but valid, uses of NULL. */
8541 547226895 : if (TREE_CODE (totype) != BOOLEAN_TYPE
8542 : && ARITHMETIC_TYPE_P (totype)
8543 187682001 : && null_node_p (expr))
8544 : {
8545 94 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8546 94 : if (fn)
8547 : {
8548 33 : auto_diagnostic_group d;
8549 33 : if (warning_at (loc, OPT_Wconversion_null,
8550 : "passing NULL to non-pointer argument %P of %qD",
8551 : argnum, fn))
8552 27 : inform (get_fndecl_argument_location (fn, argnum),
8553 : "declared here");
8554 33 : }
8555 : else
8556 61 : warning_at (loc, OPT_Wconversion_null,
8557 : "converting to non-pointer type %qT from NULL", totype);
8558 : }
8559 :
8560 : /* Issue warnings if "false" is converted to a NULL pointer */
8561 547226801 : else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
8562 547226801 : && TYPE_PTR_P (totype))
8563 : {
8564 7 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8565 7 : if (fn)
8566 : {
8567 3 : auto_diagnostic_group d;
8568 3 : if (warning_at (loc, OPT_Wconversion_null,
8569 : "converting %<false%> to pointer type for argument "
8570 : "%P of %qD", argnum, fn))
8571 3 : inform (get_fndecl_argument_location (fn, argnum),
8572 : "declared here");
8573 3 : }
8574 : else
8575 4 : warning_at (loc, OPT_Wconversion_null,
8576 : "converting %<false%> to pointer type %qT", totype);
8577 : }
8578 : /* Handle zero as null pointer warnings for cases other
8579 : than EQ_EXPR and NE_EXPR */
8580 504088675 : else if ((TYPE_PTR_OR_PTRMEM_P (totype) || NULLPTR_TYPE_P (totype))
8581 547542972 : && null_ptr_cst_p (expr))
8582 : {
8583 2428046 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8584 2428046 : maybe_warn_zero_as_null_pointer_constant (expr, loc);
8585 : }
8586 547226895 : }
8587 :
8588 : /* We gave a diagnostic during a conversion. If this was in the second
8589 : standard conversion sequence of a user-defined conversion sequence, say
8590 : which user-defined conversion. */
8591 :
8592 : static void
8593 5252 : maybe_print_user_conv_context (conversion *convs)
8594 : {
8595 5252 : if (convs->user_conv_p)
8596 167 : for (conversion *t = convs; t; t = next_conversion (t))
8597 142 : if (t->kind == ck_user)
8598 : {
8599 43 : print_z_candidate (0, N_(" after user-defined conversion:"),
8600 : t->cand);
8601 43 : break;
8602 : }
8603 5252 : }
8604 :
8605 : /* Locate the parameter with the given index within FNDECL.
8606 : ARGNUM is zero based, -1 indicates the `this' argument of a method.
8607 : Return the location of the FNDECL itself if there are problems. */
8608 :
8609 : location_t
8610 9105166 : get_fndecl_argument_location (tree fndecl, int argnum)
8611 : {
8612 : /* The locations of implicitly-declared functions are likely to be
8613 : more meaningful than those of their parameters. */
8614 9105166 : if (DECL_ARTIFICIAL (fndecl))
8615 297 : return DECL_SOURCE_LOCATION (fndecl);
8616 :
8617 9104869 : if (argnum == -1)
8618 51 : return DECL_SOURCE_LOCATION (fndecl);
8619 :
8620 9104818 : int i;
8621 9104818 : tree param;
8622 :
8623 : /* Locate param by index within DECL_ARGUMENTS (fndecl). */
8624 9104818 : for (i = 0, param = FUNCTION_FIRST_USER_PARM (fndecl);
8625 22865489 : i < argnum && param;
8626 13760671 : i++, param = TREE_CHAIN (param))
8627 : ;
8628 :
8629 : /* If something went wrong (e.g. if we have a builtin and thus no arguments),
8630 : return the location of FNDECL. */
8631 9104818 : if (param == NULL)
8632 40 : return DECL_SOURCE_LOCATION (fndecl);
8633 :
8634 9104778 : return DECL_SOURCE_LOCATION (param);
8635 : }
8636 :
8637 : /* If FNDECL is non-NULL, issue a note highlighting ARGNUM
8638 : within its declaration (or the fndecl itself if something went
8639 : wrong). */
8640 :
8641 : void
8642 7003 : maybe_inform_about_fndecl_for_bogus_argument_init (tree fn, int argnum,
8643 : const char *highlight_color)
8644 : {
8645 7003 : if (fn)
8646 : {
8647 1123 : gcc_rich_location richloc (get_fndecl_argument_location (fn, argnum));
8648 1123 : richloc.set_highlight_color (highlight_color);
8649 1123 : inform (&richloc,
8650 : "initializing argument %P of %qD", argnum, fn);
8651 1123 : }
8652 7003 : }
8653 :
8654 : /* Maybe warn about C++20 Conversions to arrays of unknown bound. C is
8655 : the conversion, EXPR is the expression we're converting. */
8656 :
8657 : static void
8658 39392900 : maybe_warn_array_conv (location_t loc, conversion *c, tree expr)
8659 : {
8660 39392900 : if (cxx_dialect >= cxx20)
8661 : return;
8662 :
8663 437092 : tree type = TREE_TYPE (expr);
8664 437092 : type = strip_pointer_operator (type);
8665 :
8666 437092 : if (TREE_CODE (type) != ARRAY_TYPE
8667 437092 : || TYPE_DOMAIN (type) == NULL_TREE)
8668 : return;
8669 :
8670 1858 : if (pedantic && conv_binds_to_array_of_unknown_bound (c))
8671 10 : pedwarn (loc, OPT_Wc__20_extensions,
8672 : "conversions to arrays of unknown bound "
8673 : "are only available with %<-std=c++20%> or %<-std=gnu++20%>");
8674 : }
8675 :
8676 : /* We call this recursively in convert_like_internal. */
8677 : static tree convert_like (conversion *, tree, tree, int, bool, bool, bool,
8678 : tsubst_flags_t);
8679 :
8680 : /* Adjust the result EXPR of a conversion to the expected type TOTYPE, which
8681 : must be equivalent but might be a typedef. */
8682 :
8683 : static tree
8684 916273102 : maybe_adjust_type_name (tree type, tree expr, conversion_kind kind)
8685 : {
8686 916273102 : if (expr == error_mark_node
8687 916273051 : || processing_template_decl)
8688 : return expr;
8689 :
8690 803878971 : tree etype = TREE_TYPE (expr);
8691 803878971 : if (etype == type)
8692 : return expr;
8693 :
8694 106874451 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p (etype, type)
8695 : || is_bitfield_expr_with_lowered_type (expr)
8696 : || seen_error ());
8697 :
8698 106874451 : if (SCALAR_TYPE_P (type)
8699 100999385 : && (kind == ck_rvalue
8700 : /* ??? We should be able to do this for ck_identity of more prvalue
8701 : expressions, but checking !obvalue_p here breaks, so for now let's
8702 : just handle NON_LVALUE_EXPR (such as the location wrapper for a
8703 : literal). Maybe we want to express already-rvalue in the
8704 : conversion somehow? */
8705 90588259 : || TREE_CODE (expr) == NON_LVALUE_EXPR))
8706 11972787 : expr = build_nop (type, expr);
8707 :
8708 : return expr;
8709 : }
8710 :
8711 : /* Perform the conversions in CONVS on the expression EXPR. FN and
8712 : ARGNUM are used for diagnostics. ARGNUM is zero based, -1
8713 : indicates the `this' argument of a method. INNER is nonzero when
8714 : being called to continue a conversion chain. It is negative when a
8715 : reference binding will be applied, positive otherwise. If
8716 : ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
8717 : conversions will be emitted if appropriate. If C_CAST_P is true,
8718 : this conversion is coming from a C-style cast; in that case,
8719 : conversions to inaccessible bases are permitted. */
8720 :
8721 : static tree
8722 1118519458 : convert_like_internal (conversion *convs, tree expr, tree fn, int argnum,
8723 : bool issue_conversion_warnings, bool c_cast_p,
8724 : bool nested_p, tsubst_flags_t complain)
8725 : {
8726 1118519458 : tree totype = convs->type;
8727 1118519458 : enum diagnostics::kind diag_kind;
8728 1118519458 : int flags;
8729 1118519458 : location_t loc = cp_expr_loc_or_input_loc (expr);
8730 1118519458 : const bool stub_object_p = is_stub_object (expr);
8731 :
8732 1118519458 : if (convs->bad_p && !(complain & tf_error))
8733 46653 : return error_mark_node;
8734 :
8735 1118472805 : gcc_checking_assert (!TYPE_REF_P (TREE_TYPE (expr)));
8736 :
8737 1118472805 : if (convs->bad_p
8738 6974 : && convs->kind != ck_user
8739 6879 : && convs->kind != ck_list
8740 6873 : && convs->kind != ck_ambig
8741 6873 : && (convs->kind != ck_ref_bind
8742 5259 : || (convs->user_conv_p && next_conversion (convs)->bad_p))
8743 1635 : && (convs->kind != ck_rvalue
8744 15 : || SCALAR_TYPE_P (totype))
8745 1118474431 : && convs->kind != ck_base)
8746 : {
8747 1626 : int complained = 0;
8748 1626 : conversion *t = convs;
8749 1626 : auto_diagnostic_group d;
8750 :
8751 : /* Give a helpful error if this is bad because of excess braces. */
8752 46 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8753 46 : && SCALAR_TYPE_P (totype)
8754 34 : && CONSTRUCTOR_NELTS (expr) > 0
8755 1660 : && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
8756 : {
8757 11 : complained = permerror (loc, "too many braces around initializer "
8758 : "for %qT", totype);
8759 33 : while (BRACE_ENCLOSED_INITIALIZER_P (expr)
8760 55 : && CONSTRUCTOR_NELTS (expr) == 1)
8761 22 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
8762 : }
8763 :
8764 : /* Give a helpful error if this is bad because a conversion to bool
8765 : from std::nullptr_t requires direct-initialization. */
8766 1626 : if (NULLPTR_TYPE_P (TREE_TYPE (expr))
8767 1626 : && TREE_CODE (totype) == BOOLEAN_TYPE)
8768 15 : complained = permerror (loc, "converting to %qH from %qI requires "
8769 : "direct-initialization",
8770 15 : totype, TREE_TYPE (expr));
8771 :
8772 1626 : if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (expr))
8773 105 : && SCALAR_FLOAT_TYPE_P (totype)
8774 1731 : && (extended_float_type_p (TREE_TYPE (expr))
8775 27 : || extended_float_type_p (totype)))
8776 105 : switch (cp_compare_floating_point_conversion_ranks (TREE_TYPE (expr),
8777 : totype))
8778 : {
8779 102 : case 2:
8780 102 : if (pedwarn (loc, OPT_Wnarrowing, "ISO C++ does not allow "
8781 : "converting to %qH from %qI with greater "
8782 102 : "conversion rank", totype, TREE_TYPE (expr)))
8783 1626 : complained = 1;
8784 21 : else if (!complained)
8785 1626 : complained = -1;
8786 : break;
8787 3 : case 3:
8788 3 : if (pedwarn (loc, OPT_Wnarrowing, "ISO C++ does not allow "
8789 : "converting to %qH from %qI with unordered "
8790 3 : "conversion rank", totype, TREE_TYPE (expr)))
8791 : complained = 1;
8792 0 : else if (!complained)
8793 1626 : complained = -1;
8794 : break;
8795 : default:
8796 : break;
8797 : }
8798 :
8799 3268 : for (; t ; t = next_conversion (t))
8800 : {
8801 3259 : if (t->kind == ck_user && t->cand->reason)
8802 : {
8803 52 : auto_diagnostic_group d;
8804 52 : complained = permerror (loc, "invalid user-defined conversion "
8805 52 : "from %qH to %qI", TREE_TYPE (expr),
8806 : totype);
8807 52 : if (complained)
8808 : {
8809 52 : auto_diagnostic_nesting_level sentinel;
8810 52 : print_z_candidate (loc, N_("candidate is:"), t->cand);
8811 52 : }
8812 52 : expr = convert_like (t, expr, fn, argnum,
8813 : /*issue_conversion_warnings=*/false,
8814 : /*c_cast_p=*/false, /*nested_p=*/true,
8815 : complain);
8816 52 : break;
8817 52 : }
8818 3207 : else if (t->kind == ck_user || !t->bad_p)
8819 : {
8820 1565 : expr = convert_like (t, expr, fn, argnum,
8821 : /*issue_conversion_warnings=*/false,
8822 : /*c_cast_p=*/false, /*nested_p=*/true,
8823 : complain);
8824 1565 : if (t->bad_p)
8825 0 : complained = 1;
8826 : break;
8827 : }
8828 1642 : else if (t->kind == ck_ambig)
8829 0 : return convert_like (t, expr, fn, argnum,
8830 : /*issue_conversion_warnings=*/false,
8831 : /*c_cast_p=*/false, /*nested_p=*/true,
8832 0 : complain);
8833 1642 : else if (t->kind == ck_identity)
8834 : break;
8835 : }
8836 1626 : if (!complained && stub_object_p)
8837 : {
8838 : /* An error diagnosed within a trait, don't give extra labels. */
8839 12 : error_at (loc, "invalid conversion from %qH to %qI",
8840 6 : TREE_TYPE (expr), totype);
8841 6 : complained = 1;
8842 : }
8843 1620 : else if (!complained && expr != error_mark_node)
8844 : {
8845 1435 : range_label_for_type_mismatch label (TREE_TYPE (expr), totype);
8846 1435 : gcc_rich_location richloc (loc, &label, highlight_colors::percent_h);
8847 1435 : complained = permerror (&richloc,
8848 : "invalid conversion from %qH to %qI",
8849 1435 : TREE_TYPE (expr), totype);
8850 1435 : if (complained)
8851 1380 : maybe_emit_indirection_note (loc, expr, totype);
8852 1435 : }
8853 1626 : if (convs->kind == ck_ref_bind)
8854 21 : expr = convert_to_reference (totype, expr, CONV_IMPLICIT,
8855 : LOOKUP_NORMAL, NULL_TREE,
8856 : complain);
8857 : else
8858 1605 : expr = cp_convert (totype, expr, complain);
8859 1626 : if (complained == 1)
8860 1548 : maybe_inform_about_fndecl_for_bogus_argument_init
8861 1548 : (fn, argnum, highlight_colors::percent_i);
8862 : return expr;
8863 1626 : }
8864 :
8865 1118471179 : if (issue_conversion_warnings && (complain & tf_warning))
8866 547226895 : conversion_null_warnings (totype, expr, fn, argnum);
8867 :
8868 1118471179 : switch (convs->kind)
8869 : {
8870 6674380 : case ck_user:
8871 6674380 : {
8872 6674380 : struct z_candidate *cand = convs->cand;
8873 :
8874 6674380 : if (cand == NULL)
8875 : /* We chose the surrogate function from add_conv_candidate, now we
8876 : actually need to build the conversion. */
8877 56 : cand = build_user_type_conversion_1 (totype, expr,
8878 : LOOKUP_NO_CONVERSION, complain);
8879 :
8880 6674380 : tree convfn = cand->fn;
8881 :
8882 : /* When converting from an init list we consider explicit
8883 : constructors, but actually trying to call one is an error. */
8884 7162090 : if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
8885 10087 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
8886 : /* Unless this is for direct-list-initialization. */
8887 10084 : && (!CONSTRUCTOR_IS_DIRECT_INIT (expr) || convs->need_temporary_p)
8888 : /* And in C++98 a default constructor can't be explicit. */
8889 6674620 : && cxx_dialect >= cxx11)
8890 : {
8891 238 : if (!(complain & tf_error))
8892 54 : return error_mark_node;
8893 184 : location_t loc = location_of (expr);
8894 184 : if (CONSTRUCTOR_NELTS (expr) == 0
8895 184 : && FUNCTION_FIRST_USER_PARMTYPE (convfn) != void_list_node)
8896 : {
8897 9 : auto_diagnostic_group d;
8898 9 : if (pedwarn (loc, 0, "converting to %qT from initializer list "
8899 : "would use explicit constructor %qD",
8900 : totype, convfn))
8901 : {
8902 9 : inform (DECL_SOURCE_LOCATION (convfn), "%qD declared here",
8903 : convfn);
8904 9 : inform (loc, "in C++11 and above a default constructor "
8905 : "can be explicit");
8906 : }
8907 9 : }
8908 : else
8909 : {
8910 175 : auto_diagnostic_group d;
8911 175 : error ("converting to %qT from initializer list would use "
8912 : "explicit constructor %qD", totype, convfn);
8913 175 : inform (DECL_SOURCE_LOCATION (convfn), "%qD declared here",
8914 : convfn);
8915 175 : }
8916 : }
8917 :
8918 : /* If we're initializing from {}, it's value-initialization. */
8919 879353 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8920 879331 : && CONSTRUCTOR_NELTS (expr) == 0
8921 150417 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype)
8922 6824716 : && !processing_template_decl)
8923 : {
8924 150390 : if (abstract_virtuals_error (NULL_TREE, totype, complain))
8925 12 : return error_mark_node;
8926 150378 : expr = build_value_init (totype, complain);
8927 150378 : expr = get_target_expr (expr, complain);
8928 150378 : if (expr != error_mark_node)
8929 150238 : TARGET_EXPR_LIST_INIT_P (expr) = true;
8930 : return expr;
8931 : }
8932 :
8933 : /* We don't know here whether EXPR is being used as an lvalue or
8934 : rvalue, but we know it's read. */
8935 6523936 : mark_exp_read (expr);
8936 :
8937 : /* Give the conversion call the location of EXPR rather than the
8938 : location of the context that caused the conversion. */
8939 6523936 : iloc_sentinel ils (loc);
8940 :
8941 : /* Pass LOOKUP_NO_CONVERSION so rvalue/base handling knows not to allow
8942 : any more UDCs. */
8943 6523936 : expr = build_over_call (cand, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
8944 : complain);
8945 :
8946 : /* If this is a constructor or a function returning an aggr type,
8947 : we need to build up a TARGET_EXPR. */
8948 13047872 : if (DECL_CONSTRUCTOR_P (convfn))
8949 : {
8950 2917283 : expr = build_cplus_new (totype, expr, complain);
8951 :
8952 : /* Remember that this was list-initialization. */
8953 2917283 : if (convs->check_narrowing && expr != error_mark_node)
8954 754081 : TARGET_EXPR_LIST_INIT_P (expr) = true;
8955 : }
8956 :
8957 6523936 : return expr;
8958 6523936 : }
8959 786846716 : case ck_identity:
8960 786846716 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
8961 : {
8962 1164346 : int nelts = CONSTRUCTOR_NELTS (expr);
8963 180346 : if (nelts == 0)
8964 984000 : expr = build_value_init (totype, complain);
8965 180346 : else if (nelts == 1)
8966 180346 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
8967 : else
8968 0 : gcc_unreachable ();
8969 : }
8970 786846716 : expr = mark_use (expr, /*rvalue_p=*/!convs->rvaluedness_matches_p,
8971 : /*read_p=*/true, UNKNOWN_LOCATION,
8972 : /*reject_builtin=*/true);
8973 :
8974 786846716 : if (type_unknown_p (expr))
8975 20670 : expr = instantiate_type (totype, expr, complain);
8976 786846716 : if (!nested_p && TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8977 103427 : expr = cp_convert (totype, TREE_OPERAND (expr, 0), complain);
8978 786846716 : if (expr == null_node
8979 786846716 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype))
8980 : /* If __null has been converted to an integer type, we do not want to
8981 : continue to warn about uses of EXPR as an integer, rather than as a
8982 : pointer. */
8983 152893 : expr = build_int_cst (totype, 0);
8984 786846716 : return maybe_adjust_type_name (totype, expr, convs->kind);
8985 53 : case ck_ambig:
8986 : /* We leave bad_p off ck_ambig because overload resolution considers
8987 : it valid, it just fails when we try to perform it. So we need to
8988 : check complain here, too. */
8989 53 : if (complain & tf_error)
8990 : {
8991 : /* Call build_user_type_conversion again for the error. */
8992 53 : auto_diagnostic_group d;
8993 3 : int flags = (convs->need_temporary_p
8994 53 : ? LOOKUP_IMPLICIT : LOOKUP_NORMAL);
8995 53 : build_user_type_conversion (totype, convs->u.expr, flags, complain);
8996 53 : gcc_assert (seen_error ());
8997 53 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
8998 53 : }
8999 53 : return error_mark_node;
9000 :
9001 17556 : case ck_list:
9002 17556 : {
9003 : /* Conversion to std::initializer_list<T>. */
9004 17556 : tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
9005 17556 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (expr);
9006 17556 : tree array;
9007 :
9008 17556 : if (tree init = maybe_init_list_as_array (elttype, expr))
9009 : {
9010 132 : elttype
9011 132 : = cp_build_qualified_type (elttype, (cp_type_quals (elttype)
9012 : | TYPE_QUAL_CONST));
9013 132 : tree index_type = TYPE_DOMAIN (TREE_TYPE (init));
9014 132 : array = build_cplus_array_type (elttype, index_type);
9015 132 : len = TREE_INT_CST_LOW (TYPE_MAX_VALUE (index_type)) + 1;
9016 132 : array = build_vec_init_expr (array, init, complain);
9017 132 : array = get_target_expr (array);
9018 132 : array = cp_build_addr_expr (array, complain);
9019 : }
9020 17424 : else if (len)
9021 : {
9022 16732 : tree val;
9023 16732 : unsigned ix;
9024 16732 : tree new_ctor = build_constructor (init_list_type_node, NULL);
9025 :
9026 : /* Convert all the elements. */
9027 144442 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
9028 : {
9029 110993 : if (TREE_CODE (val) == RAW_DATA_CST)
9030 : {
9031 : /* For conversion to initializer_list<unsigned char> or
9032 : initializer_list<char> or initializer_list<signed char>
9033 : we can optimize and keep RAW_DATA_CST with adjusted
9034 : type if we report narrowing errors if needed, for
9035 : others this converts each element separately. */
9036 48 : if (convs->u.list[ix]->kind == ck_std)
9037 : {
9038 18 : tree et = convs->u.list[ix]->type;
9039 18 : conversion *next = next_conversion (convs->u.list[ix]);
9040 18 : gcc_assert (et
9041 : && (TREE_CODE (et) == INTEGER_TYPE
9042 : || is_byte_access_type (et))
9043 : && TYPE_PRECISION (et) == CHAR_BIT
9044 : && next
9045 : && next->kind == ck_identity);
9046 18 : if (!TYPE_UNSIGNED (et)
9047 : /* For RAW_DATA_CST, TREE_TYPE (val) can be
9048 : either integer_type_node (when it has been
9049 : created by the lexer from CPP_EMBED) or
9050 : after digestion/conversion some integral
9051 : type with CHAR_BIT precision. For int with
9052 : precision higher than CHAR_BIT or unsigned char
9053 : diagnose narrowing conversions from
9054 : that int/unsigned char to signed char if any
9055 : byte has most significant bit set. */
9056 18 : && (TYPE_UNSIGNED (TREE_TYPE (val))
9057 12 : || (TYPE_PRECISION (TREE_TYPE (val))
9058 : > CHAR_BIT)))
9059 2460 : for (int i = 0; i < RAW_DATA_LENGTH (val); ++i)
9060 : {
9061 2454 : if (RAW_DATA_SCHAR_ELT (val, i) >= 0)
9062 2448 : continue;
9063 6 : else if (complain & tf_error)
9064 : {
9065 6 : location_t loc
9066 6 : = cp_expr_loc_or_input_loc (val);
9067 6 : int savederrorcount = errorcount;
9068 18 : permerror_opt (loc, OPT_Wnarrowing,
9069 : "narrowing conversion of "
9070 : "%qd from %qH to %qI",
9071 6 : RAW_DATA_UCHAR_ELT (val, i),
9072 6 : TREE_TYPE (val), et);
9073 6 : if (errorcount != savederrorcount)
9074 6 : return error_mark_node;
9075 : }
9076 : else
9077 0 : return error_mark_node;
9078 : }
9079 12 : tree sub = copy_node (val);
9080 12 : TREE_TYPE (sub) = et;
9081 12 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor),
9082 : NULL_TREE, sub);
9083 : }
9084 : else
9085 : {
9086 30 : conversion *conv = convs->u.list[ix];
9087 30 : gcc_assert (conv->kind == ck_list);
9088 15495 : for (int i = 0; i < RAW_DATA_LENGTH (val); ++i)
9089 : {
9090 15465 : tree elt
9091 15465 : = build_int_cst (TREE_TYPE (val),
9092 15465 : RAW_DATA_UCHAR_ELT (val, i));
9093 15465 : tree sub
9094 15465 : = convert_like (conv->u.list[i], elt,
9095 : fn, argnum, false, false,
9096 : /*nested_p=*/true, complain);
9097 15465 : if (sub == error_mark_node)
9098 : return sub;
9099 15465 : if (!check_narrowing (TREE_TYPE (sub), elt,
9100 : complain))
9101 0 : return error_mark_node;
9102 15465 : tree nc = new_ctor;
9103 15465 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (nc),
9104 : NULL_TREE, sub);
9105 15465 : if (!TREE_CONSTANT (sub))
9106 11679 : TREE_CONSTANT (new_ctor) = false;
9107 : }
9108 : }
9109 42 : len += RAW_DATA_LENGTH (val) - 1;
9110 42 : continue;
9111 42 : }
9112 110945 : tree sub = convert_like (convs->u.list[ix], val, fn,
9113 : argnum, false, false,
9114 : /*nested_p=*/true, complain);
9115 110945 : if (sub == error_mark_node)
9116 : return sub;
9117 1648 : if (!BRACE_ENCLOSED_INITIALIZER_P (val)
9118 110956 : && !check_narrowing (TREE_TYPE (sub), val, complain))
9119 0 : return error_mark_node;
9120 110936 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor),
9121 : NULL_TREE, sub);
9122 110936 : if (!TREE_CONSTANT (sub))
9123 11349 : TREE_CONSTANT (new_ctor) = false;
9124 : }
9125 : /* Build up the array. */
9126 16717 : elttype
9127 16717 : = cp_build_qualified_type (elttype, (cp_type_quals (elttype)
9128 : | TYPE_QUAL_CONST));
9129 16717 : array = build_array_of_n_type (elttype, len);
9130 16717 : array = finish_compound_literal (array, new_ctor, complain);
9131 : /* This is dubious now, should be blessed by P2752. */
9132 16717 : DECL_MERGEABLE (TARGET_EXPR_SLOT (array)) = true;
9133 16717 : array = cp_build_addr_expr (array, complain);
9134 : }
9135 : else
9136 692 : array = nullptr_node;
9137 :
9138 17541 : array = cp_convert (build_pointer_type (elttype), array, complain);
9139 17541 : if (array == error_mark_node)
9140 : return error_mark_node;
9141 :
9142 : /* Build up the initializer_list object. Note: fail gracefully
9143 : if the object cannot be completed because, for example, no
9144 : definition is provided (c++/80956). */
9145 17541 : totype = complete_type_or_maybe_complain (totype, NULL_TREE, complain);
9146 17541 : if (!totype)
9147 0 : return error_mark_node;
9148 17541 : tree field = next_aggregate_field (TYPE_FIELDS (totype));
9149 17541 : vec<constructor_elt, va_gc> *vec = NULL;
9150 17541 : CONSTRUCTOR_APPEND_ELT (vec, field, array);
9151 17541 : field = next_aggregate_field (DECL_CHAIN (field));
9152 17541 : CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
9153 17541 : tree new_ctor = build_constructor (totype, vec);
9154 17541 : return get_target_expr (new_ctor, complain);
9155 : }
9156 :
9157 1322035 : case ck_aggr:
9158 1322035 : if (TREE_CODE (totype) == COMPLEX_TYPE)
9159 : {
9160 28566 : tree real = CONSTRUCTOR_ELT (expr, 0)->value;
9161 28566 : tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
9162 28566 : real = perform_implicit_conversion (TREE_TYPE (totype),
9163 : real, complain);
9164 28566 : imag = perform_implicit_conversion (TREE_TYPE (totype),
9165 : imag, complain);
9166 28566 : expr = build2 (COMPLEX_EXPR, totype, real, imag);
9167 28566 : return expr;
9168 : }
9169 1293469 : expr = reshape_init (totype, expr, complain);
9170 1293469 : expr = get_target_expr (digest_init (totype, expr, complain),
9171 : complain);
9172 1293469 : if (expr != error_mark_node)
9173 1293458 : TARGET_EXPR_LIST_INIT_P (expr) = true;
9174 : return expr;
9175 :
9176 323610439 : default:
9177 323610439 : break;
9178 323610439 : };
9179 :
9180 323610439 : conversion *nc = next_conversion (convs);
9181 323610439 : if (convs->kind == ck_ref_bind && nc->kind == ck_qual
9182 8863 : && !convs->need_temporary_p)
9183 : /* direct_reference_binding might have inserted a ck_qual under
9184 : this ck_ref_bind for the benefit of conversion sequence ranking.
9185 : Don't actually perform that conversion. */
9186 6496 : nc = next_conversion (nc);
9187 :
9188 583839494 : expr = convert_like (nc, expr, fn, argnum,
9189 : convs->kind == ck_ref_bind
9190 : ? issue_conversion_warnings : false,
9191 : c_cast_p, /*nested_p=*/true, complain & ~tf_no_cleanup);
9192 323610439 : if (expr == error_mark_node)
9193 : return error_mark_node;
9194 :
9195 323609609 : switch (convs->kind)
9196 : {
9197 143494331 : case ck_rvalue:
9198 143494331 : expr = decay_conversion (expr, complain);
9199 143494331 : if (expr == error_mark_node)
9200 : {
9201 15 : if (complain & tf_error)
9202 : {
9203 12 : auto_diagnostic_group d;
9204 12 : maybe_print_user_conv_context (convs);
9205 12 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9206 12 : }
9207 15 : return error_mark_node;
9208 : }
9209 :
9210 143494316 : if ((complain & tf_warning) && fn
9211 41535592 : && warn_suggest_attribute_format)
9212 : {
9213 706 : tree rhstype = TREE_TYPE (expr);
9214 706 : const enum tree_code coder = TREE_CODE (rhstype);
9215 706 : const enum tree_code codel = TREE_CODE (totype);
9216 706 : if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
9217 250 : && coder == codel
9218 956 : && check_missing_format_attribute (totype, rhstype))
9219 6 : warning (OPT_Wsuggest_attribute_format,
9220 : "argument of function call might be a candidate "
9221 : "for a format attribute");
9222 : }
9223 :
9224 143494316 : if (! MAYBE_CLASS_TYPE_P (totype))
9225 129426386 : return maybe_adjust_type_name (totype, expr, convs->kind);
9226 :
9227 : /* Don't introduce copies when passing arguments along to the inherited
9228 : constructor. */
9229 14067930 : if (current_function_decl
9230 11137494 : && flag_new_inheriting_ctors
9231 36342438 : && DECL_INHERITED_CTOR (current_function_decl))
9232 : return expr;
9233 :
9234 14063232 : if (TREE_CODE (expr) == TARGET_EXPR
9235 14063232 : && TARGET_EXPR_LIST_INIT_P (expr))
9236 : /* Copy-list-initialization doesn't actually involve a copy. */
9237 : return expr;
9238 :
9239 : /* Fall through. */
9240 16118454 : case ck_base:
9241 16118454 : if (convs->kind == ck_base && !convs->need_temporary_p)
9242 : {
9243 : /* We are going to bind a reference directly to a base-class
9244 : subobject of EXPR. */
9245 : /* Build an expression for `*((base*) &expr)'. */
9246 1826109 : expr = convert_to_base (expr, totype,
9247 : !c_cast_p, /*nonnull=*/true, complain);
9248 1826109 : return expr;
9249 : }
9250 :
9251 : /* Copy-initialization where the cv-unqualified version of the source
9252 : type is the same class as, or a derived class of, the class of the
9253 : destination [is treated as direct-initialization]. [dcl.init] */
9254 14292345 : flags = LOOKUP_NORMAL;
9255 : /* This conversion is being done in the context of a user-defined
9256 : conversion (i.e. the second step of copy-initialization), so
9257 : don't allow any more. */
9258 14292345 : if (convs->user_conv_p)
9259 263937 : flags |= LOOKUP_NO_CONVERSION;
9260 : /* We might be performing a conversion of the argument
9261 : to the user-defined conversion, i.e., not a conversion of the
9262 : result of the user-defined conversion. In which case we skip
9263 : explicit constructors. */
9264 14292345 : if (convs->copy_init_p)
9265 14019187 : flags |= LOOKUP_ONLYCONVERTING;
9266 14292345 : expr = build_temp (expr, totype, flags, &diag_kind, complain);
9267 14292345 : if (diag_kind != diagnostics::kind::unspecified && complain)
9268 : {
9269 78 : auto_diagnostic_group d;
9270 78 : maybe_print_user_conv_context (convs);
9271 78 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9272 78 : }
9273 :
9274 14292345 : return build_cplus_new (totype, expr, complain);
9275 :
9276 63380612 : case ck_ref_bind:
9277 63380612 : {
9278 63380612 : tree ref_type = totype;
9279 :
9280 63380612 : if (convs->bad_p && !next_conversion (convs)->bad_p)
9281 : {
9282 5162 : tree extype = TREE_TYPE (expr);
9283 5162 : auto_diagnostic_group d;
9284 5162 : if (TYPE_REF_IS_RVALUE (ref_type)
9285 5162 : && lvalue_p (expr))
9286 1638 : error_at (loc, "cannot bind rvalue reference of type %qH to "
9287 : "lvalue of type %qI", totype, extype);
9288 6202 : else if (!TYPE_REF_IS_RVALUE (ref_type) && !lvalue_p (expr)
9289 5004 : && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
9290 : {
9291 1231 : conversion *next = next_conversion (convs);
9292 1231 : if (next->kind == ck_std)
9293 : {
9294 59 : next = next_conversion (next);
9295 59 : error_at (loc, "cannot bind non-const lvalue reference of "
9296 : "type %qH to a value of type %qI",
9297 : totype, next->type);
9298 : }
9299 1172 : else if (!CP_TYPE_CONST_P (TREE_TYPE (ref_type)))
9300 831 : error_at (loc, "cannot bind non-const lvalue reference of "
9301 : "type %qH to an rvalue of type %qI", totype, extype);
9302 : else // extype is volatile
9303 341 : error_at (loc, "cannot bind lvalue reference of type "
9304 : "%qH to an rvalue of type %qI", totype,
9305 : extype);
9306 : }
9307 2293 : else if (!reference_compatible_p (TREE_TYPE (totype), extype))
9308 : {
9309 : /* If we're converting from T[] to T[N], don't talk
9310 : about discarding qualifiers. (Converting from T[N] to
9311 : T[] is allowed by P0388R4.) */
9312 2293 : if (TREE_CODE (extype) == ARRAY_TYPE
9313 27 : && TYPE_DOMAIN (extype) == NULL_TREE
9314 15 : && TREE_CODE (TREE_TYPE (totype)) == ARRAY_TYPE
9315 2308 : && TYPE_DOMAIN (TREE_TYPE (totype)) != NULL_TREE)
9316 15 : error_at (loc, "cannot bind reference of type %qH to %qI "
9317 : "due to different array bounds", totype, extype);
9318 : else
9319 2278 : error_at (loc, "binding reference of type %qH to %qI "
9320 : "discards qualifiers", totype, extype);
9321 : }
9322 : else
9323 0 : gcc_unreachable ();
9324 5162 : maybe_print_user_conv_context (convs);
9325 5162 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9326 :
9327 5162 : return error_mark_node;
9328 5162 : }
9329 63375450 : else if (complain & tf_warning)
9330 37952864 : maybe_warn_array_conv (loc, convs, expr);
9331 :
9332 : /* If necessary, create a temporary.
9333 :
9334 : VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
9335 : that need temporaries, even when their types are reference
9336 : compatible with the type of reference being bound, so the
9337 : upcoming call to cp_build_addr_expr doesn't fail. */
9338 63375450 : if (convs->need_temporary_p
9339 60843332 : || TREE_CODE (expr) == CONSTRUCTOR
9340 60843126 : || TREE_CODE (expr) == VA_ARG_EXPR)
9341 : {
9342 : /* Otherwise, a temporary of type "cv1 T1" is created and
9343 : initialized from the initializer expression using the rules
9344 : for a non-reference copy-initialization (8.5). */
9345 :
9346 2532324 : tree type = TREE_TYPE (ref_type);
9347 2532324 : cp_lvalue_kind lvalue = lvalue_kind (expr);
9348 :
9349 2532324 : gcc_assert (similar_type_p (type, next_conversion (convs)->type));
9350 2532324 : if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
9351 3419533 : && !TYPE_REF_IS_RVALUE (ref_type))
9352 : {
9353 : /* If the reference is volatile or non-const, we
9354 : cannot create a temporary. */
9355 105 : if (complain & tf_error)
9356 : {
9357 103 : if (lvalue & clk_bitfield)
9358 30 : error_at (loc, "cannot bind bit-field %qE to %qT",
9359 : expr, ref_type);
9360 73 : else if (lvalue & clk_packed)
9361 9 : error_at (loc, "cannot bind packed field %qE to %qT",
9362 : expr, ref_type);
9363 : else
9364 64 : error_at (loc, "cannot bind rvalue %qE to %qT",
9365 : expr, ref_type);
9366 : }
9367 105 : return error_mark_node;
9368 : }
9369 : /* If the source is a packed field, and we must use a copy
9370 : constructor, then building the target expr will require
9371 : binding the field to the reference parameter to the
9372 : copy constructor, and we'll end up with an infinite
9373 : loop. If we can use a bitwise copy, then we'll be
9374 : OK. */
9375 2532219 : if ((lvalue & clk_packed)
9376 20 : && CLASS_TYPE_P (type)
9377 2532236 : && type_has_nontrivial_copy_init (type))
9378 : {
9379 6 : error_at (loc, "cannot bind packed field %qE to %qT",
9380 : expr, ref_type);
9381 6 : return error_mark_node;
9382 : }
9383 2532213 : if (lvalue & clk_bitfield)
9384 : {
9385 24 : expr = convert_bitfield_to_declared_type (expr);
9386 24 : expr = fold_convert (type, expr);
9387 : }
9388 :
9389 : /* Creating &TARGET_EXPR<> in a template would break when
9390 : tsubsting the expression, so use an IMPLICIT_CONV_EXPR
9391 : instead. This can happen even when there's no class
9392 : involved, e.g., when converting an integer to a reference
9393 : type. */
9394 2532213 : if (processing_template_decl)
9395 9661 : return build1 (IMPLICIT_CONV_EXPR, totype, expr);
9396 2522552 : expr = build_target_expr_with_type (expr, type, complain);
9397 : }
9398 :
9399 : /* Take the address of the thing to which we will bind the
9400 : reference. */
9401 63365678 : expr = cp_build_addr_expr (expr, complain);
9402 63365678 : if (expr == error_mark_node)
9403 : return error_mark_node;
9404 :
9405 : /* Convert it to a pointer to the type referred to by the
9406 : reference. This will adjust the pointer if a derived to
9407 : base conversion is being performed. */
9408 63365678 : expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
9409 : expr, complain);
9410 : /* Convert the pointer to the desired reference type. */
9411 63365678 : return build_nop (ref_type, expr);
9412 : }
9413 :
9414 2906700 : case ck_lvalue:
9415 2906700 : return decay_conversion (expr, complain);
9416 :
9417 241512 : case ck_fnptr:
9418 : /* ??? Should the address of a transaction-safe pointer point to the TM
9419 : clone, and this conversion look up the primary function? */
9420 241512 : return build_nop (totype, expr);
9421 :
9422 1637770 : case ck_qual:
9423 : /* Warn about deprecated conversion if appropriate. */
9424 1637770 : if (complain & tf_warning)
9425 : {
9426 1440036 : string_conv_p (totype, expr, 1);
9427 1440036 : maybe_warn_array_conv (loc, convs, expr);
9428 : }
9429 : break;
9430 :
9431 3052614 : case ck_ptr:
9432 3052614 : if (convs->base_p)
9433 218168 : expr = convert_to_base (expr, totype, !c_cast_p,
9434 : /*nonnull=*/false, complain);
9435 3052614 : return build_nop (totype, expr);
9436 :
9437 28825 : case ck_pmem:
9438 28825 : return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
9439 28825 : c_cast_p, complain);
9440 :
9441 : default:
9442 : break;
9443 : }
9444 :
9445 108423118 : if (convs->check_narrowing
9446 108423118 : && !check_narrowing (totype, expr, complain,
9447 : convs->check_narrowing_const_only))
9448 439 : return error_mark_node;
9449 :
9450 216845358 : warning_sentinel w (warn_zero_as_null_pointer_constant);
9451 108422679 : if (issue_conversion_warnings)
9452 85226909 : expr = cp_convert_and_check (totype, expr, complain);
9453 : else
9454 : {
9455 23195770 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
9456 39 : expr = TREE_OPERAND (expr, 0);
9457 23195770 : expr = cp_convert (totype, expr, complain);
9458 : }
9459 :
9460 108422679 : return expr;
9461 : }
9462 :
9463 : /* Return true if converting FROM to TO is unsafe in a template. */
9464 :
9465 : static bool
9466 1972729 : conv_unsafe_in_template_p (tree to, tree from)
9467 : {
9468 : /* Converting classes involves TARGET_EXPR. */
9469 1972729 : if (CLASS_TYPE_P (to) || CLASS_TYPE_P (from))
9470 : return true;
9471 :
9472 : /* Converting real to integer produces FIX_TRUNC_EXPR which tsubst
9473 : doesn't handle. */
9474 1558798 : if (SCALAR_FLOAT_TYPE_P (from) && INTEGRAL_OR_ENUMERATION_TYPE_P (to))
9475 : return true;
9476 :
9477 : /* Converting integer to real isn't a trivial conversion, either. */
9478 1558795 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (from) && SCALAR_FLOAT_TYPE_P (to))
9479 3 : return true;
9480 :
9481 : return false;
9482 : }
9483 :
9484 : /* Wrapper for convert_like_internal that handles creating
9485 : IMPLICIT_CONV_EXPR. */
9486 :
9487 : static tree
9488 1118933386 : convert_like (conversion *convs, tree expr, tree fn, int argnum,
9489 : bool issue_conversion_warnings, bool c_cast_p, bool nested_p,
9490 : tsubst_flags_t complain)
9491 : {
9492 : /* Creating &TARGET_EXPR<> in a template breaks when substituting,
9493 : and creating a CALL_EXPR in a template breaks in finish_call_expr
9494 : so use an IMPLICIT_CONV_EXPR for this conversion. We would have
9495 : created such codes e.g. when calling a user-defined conversion
9496 : function. */
9497 1118933386 : tree conv_expr = NULL_TREE;
9498 1118933386 : if (processing_template_decl
9499 113606706 : && convs->kind != ck_identity
9500 1120906115 : && conv_unsafe_in_template_p (convs->type, TREE_TYPE (expr)))
9501 : {
9502 413937 : conv_expr = build1 (IMPLICIT_CONV_EXPR, convs->type, expr);
9503 413937 : if (convs->kind != ck_ref_bind)
9504 29009 : conv_expr = convert_from_reference (conv_expr);
9505 413937 : if (!convs->bad_p)
9506 : return conv_expr;
9507 : /* Do the normal processing to give the bad_p errors. But we still
9508 : need to return the IMPLICIT_CONV_EXPR, unless we're returning
9509 : error_mark_node. */
9510 : }
9511 1118519458 : expr = convert_like_internal (convs, expr, fn, argnum,
9512 : issue_conversion_warnings, c_cast_p,
9513 : nested_p, complain);
9514 1118519458 : if (expr == error_mark_node)
9515 : return error_mark_node;
9516 1118464670 : return conv_expr ? conv_expr : expr;
9517 : }
9518 :
9519 : /* Convenience wrapper for convert_like. */
9520 :
9521 : static inline tree
9522 618121298 : convert_like (conversion *convs, tree expr, tsubst_flags_t complain)
9523 : {
9524 618121298 : return convert_like (convs, expr, NULL_TREE, 0,
9525 : /*issue_conversion_warnings=*/true,
9526 618121298 : /*c_cast_p=*/false, /*nested_p=*/false, complain);
9527 : }
9528 :
9529 : /* Convenience wrapper for convert_like. */
9530 :
9531 : static inline tree
9532 136722517 : convert_like_with_context (conversion *convs, tree expr, tree fn, int argnum,
9533 : tsubst_flags_t complain)
9534 : {
9535 0 : return convert_like (convs, expr, fn, argnum,
9536 : /*issue_conversion_warnings=*/true,
9537 : /*c_cast_p=*/false, /*nested_p=*/false, complain);
9538 : }
9539 :
9540 : /* ARG is being passed to a varargs function. Perform any conversions
9541 : required. Return the converted value. */
9542 :
9543 : tree
9544 1442368 : convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
9545 : {
9546 1442368 : tree arg_type = TREE_TYPE (arg);
9547 1442368 : location_t loc = cp_expr_loc_or_input_loc (arg);
9548 :
9549 : /* [expr.call]
9550 :
9551 : If the argument has integral or enumeration type that is subject
9552 : to the integral promotions (_conv.prom_), or a floating-point
9553 : type that is subject to the floating-point promotion
9554 : (_conv.fpprom_), the value of the argument is converted to the
9555 : promoted type before the call. */
9556 1442368 : if (SCALAR_FLOAT_TYPE_P (arg_type)
9557 48695 : && (TYPE_PRECISION (arg_type)
9558 48695 : < TYPE_PRECISION (double_type_node))
9559 12051 : && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type))
9560 1453741 : && !extended_float_type_p (arg_type))
9561 : {
9562 11367 : if ((complain & tf_warning)
9563 11364 : && warn_double_promotion && !c_inhibit_evaluation_warnings)
9564 3 : warning_at (loc, OPT_Wdouble_promotion,
9565 : "implicit conversion from %qH to %qI when passing "
9566 : "argument to function",
9567 : arg_type, double_type_node);
9568 11367 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
9569 3 : arg = TREE_OPERAND (arg, 0);
9570 11367 : arg = mark_rvalue_use (arg);
9571 11367 : arg = convert_to_real_nofold (double_type_node, arg);
9572 : }
9573 1431001 : else if (NULLPTR_TYPE_P (arg_type))
9574 : {
9575 80 : arg = mark_rvalue_use (arg);
9576 80 : if (TREE_SIDE_EFFECTS (arg))
9577 : {
9578 6 : warning_sentinel w(warn_unused_result);
9579 6 : arg = cp_build_compound_expr (arg, null_pointer_node, complain);
9580 6 : }
9581 : else
9582 74 : arg = null_pointer_node;
9583 : }
9584 1430921 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
9585 : {
9586 936509 : if (SCOPED_ENUM_P (arg_type))
9587 : {
9588 60 : tree prom = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg,
9589 : complain);
9590 60 : prom = cp_perform_integral_promotions (prom, complain);
9591 117 : if (abi_version_crosses (6)
9592 30 : && TYPE_MODE (TREE_TYPE (prom)) != TYPE_MODE (arg_type)
9593 90 : && (complain & tf_warning))
9594 60 : warning_at (loc, OPT_Wabi, "scoped enum %qT passed through %<...%>"
9595 : " as %qT before %<-fabi-version=6%>, %qT after",
9596 : arg_type,
9597 30 : TREE_TYPE (prom), ENUM_UNDERLYING_TYPE (arg_type));
9598 60 : if (!abi_version_at_least (6))
9599 1442368 : arg = prom;
9600 : }
9601 : else
9602 936449 : arg = cp_perform_integral_promotions (arg, complain);
9603 : }
9604 : else
9605 : /* [expr.call]
9606 :
9607 : The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
9608 : standard conversions are performed. */
9609 494412 : arg = decay_conversion (arg, complain);
9610 :
9611 1442368 : arg = require_complete_type (arg, complain);
9612 1442368 : arg_type = TREE_TYPE (arg);
9613 :
9614 1442368 : if (arg != error_mark_node
9615 : /* In a template (or ill-formed code), we can have an incomplete type
9616 : even after require_complete_type, in which case we don't know
9617 : whether it has trivial copy or not. */
9618 1442356 : && COMPLETE_TYPE_P (arg_type)
9619 2884724 : && !cp_unevaluated_operand)
9620 : {
9621 : /* [expr.call] 5.2.2/7:
9622 : Passing a potentially-evaluated argument of class type (Clause 9)
9623 : with a non-trivial copy constructor or a non-trivial destructor
9624 : with no corresponding parameter is conditionally-supported, with
9625 : implementation-defined semantics.
9626 :
9627 : We support it as pass-by-invisible-reference, just like a normal
9628 : value parameter.
9629 :
9630 : If the call appears in the context of a sizeof expression,
9631 : it is not potentially-evaluated. */
9632 697116 : if (type_has_nontrivial_copy_init (arg_type)
9633 697116 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type))
9634 : {
9635 37 : arg = force_rvalue (arg, complain);
9636 37 : if (complain & tf_warning)
9637 37 : warning (OPT_Wconditionally_supported,
9638 : "passing objects of non-trivially-copyable "
9639 : "type %q#T through %<...%> is conditionally supported",
9640 : arg_type);
9641 37 : return build1 (ADDR_EXPR, build_reference_type (arg_type), arg);
9642 : }
9643 : /* Build up a real lvalue-to-rvalue conversion in case the
9644 : copy constructor is trivial but not callable. */
9645 697079 : else if (CLASS_TYPE_P (arg_type))
9646 44256 : force_rvalue (arg, complain);
9647 :
9648 : }
9649 :
9650 : return arg;
9651 : }
9652 :
9653 : /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
9654 :
9655 : tree
9656 31298 : build_x_va_arg (location_t loc, tree expr, tree type)
9657 : {
9658 31298 : if (processing_template_decl)
9659 : {
9660 39 : tree r = build_min (VA_ARG_EXPR, type, expr);
9661 39 : SET_EXPR_LOCATION (r, loc);
9662 39 : return r;
9663 : }
9664 :
9665 31259 : type = complete_type_or_else (type, NULL_TREE);
9666 :
9667 31259 : if (expr == error_mark_node || !type)
9668 : return error_mark_node;
9669 :
9670 31238 : expr = mark_lvalue_use (expr);
9671 :
9672 31238 : if (TYPE_REF_P (type))
9673 : {
9674 6 : error ("cannot receive reference type %qT through %<...%>", type);
9675 6 : return error_mark_node;
9676 : }
9677 :
9678 31232 : if (type_has_nontrivial_copy_init (type)
9679 31232 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
9680 : {
9681 : /* conditionally-supported behavior [expr.call] 5.2.2/7. Let's treat
9682 : it as pass by invisible reference. */
9683 12 : warning_at (loc, OPT_Wconditionally_supported,
9684 : "receiving objects of non-trivially-copyable type %q#T "
9685 : "through %<...%> is conditionally-supported", type);
9686 :
9687 12 : tree ref = cp_build_reference_type (type, false);
9688 12 : expr = build_va_arg (loc, expr, ref);
9689 12 : return convert_from_reference (expr);
9690 : }
9691 :
9692 31220 : tree ret = build_va_arg (loc, expr, type);
9693 31220 : if (CLASS_TYPE_P (type))
9694 : /* Wrap the VA_ARG_EXPR in a TARGET_EXPR now so other code doesn't need to
9695 : know how to handle it. */
9696 12094 : ret = get_target_expr (ret);
9697 : return ret;
9698 : }
9699 :
9700 : /* TYPE has been given to va_arg. Apply the default conversions which
9701 : would have happened when passed via ellipsis. Return the promoted
9702 : type, or the passed type if there is no change. */
9703 :
9704 : tree
9705 2717245 : cxx_type_promotes_to (tree type)
9706 : {
9707 2717245 : tree promote;
9708 :
9709 : /* Perform the array-to-pointer and function-to-pointer
9710 : conversions. */
9711 2717245 : type = type_decays_to (type);
9712 :
9713 2717245 : promote = type_promotes_to (type);
9714 2717245 : if (same_type_p (type, promote))
9715 2717221 : promote = type;
9716 :
9717 2717245 : return promote;
9718 : }
9719 :
9720 : /* ARG is a default argument expression being passed to a parameter of
9721 : the indicated TYPE, which is a parameter to FN. PARMNUM is the
9722 : zero-based argument number. Do any required conversions. Return
9723 : the converted value. */
9724 :
9725 : static GTY(()) vec<tree, va_gc> *default_arg_context;
9726 : void
9727 8008816 : push_defarg_context (tree fn)
9728 8008816 : { vec_safe_push (default_arg_context, fn); }
9729 :
9730 : void
9731 8008816 : pop_defarg_context (void)
9732 8008816 : { default_arg_context->pop (); }
9733 :
9734 : tree
9735 1137969 : convert_default_arg (tree type, tree arg, tree fn, int parmnum,
9736 : tsubst_flags_t complain)
9737 : {
9738 1137969 : int i;
9739 1137969 : tree t;
9740 :
9741 : /* See through clones. */
9742 1137969 : fn = DECL_ORIGIN (fn);
9743 : /* And inheriting ctors. */
9744 1137969 : if (flag_new_inheriting_ctors)
9745 1137329 : fn = strip_inheriting_ctors (fn);
9746 :
9747 : /* Detect recursion. */
9748 1141364 : FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t)
9749 3401 : if (t == fn)
9750 : {
9751 6 : if (complain & tf_error)
9752 6 : error ("recursive evaluation of default argument for %q#D", fn);
9753 6 : return error_mark_node;
9754 : }
9755 :
9756 : /* If the ARG is an unparsed default argument expression, the
9757 : conversion cannot be performed. */
9758 1137963 : if (TREE_CODE (arg) == DEFERRED_PARSE)
9759 : {
9760 15 : if (complain & tf_error)
9761 15 : error ("call to %qD uses the default argument for parameter %P, which "
9762 : "is not yet defined", fn, parmnum);
9763 15 : return error_mark_node;
9764 : }
9765 :
9766 1137948 : push_defarg_context (fn);
9767 :
9768 1137948 : if (fn && DECL_TEMPLATE_INFO (fn))
9769 834551 : arg = tsubst_default_argument (fn, parmnum, type, arg, complain);
9770 :
9771 : /* Due to:
9772 :
9773 : [dcl.fct.default]
9774 :
9775 : The names in the expression are bound, and the semantic
9776 : constraints are checked, at the point where the default
9777 : expressions appears.
9778 :
9779 : we must not perform access checks here. */
9780 1137948 : push_deferring_access_checks (dk_no_check);
9781 : /* We must make a copy of ARG, in case subsequent processing
9782 : alters any part of it. */
9783 1137948 : arg = break_out_target_exprs (arg, /*clear location*/true);
9784 :
9785 1137948 : arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
9786 : ICR_DEFAULT_ARGUMENT, fn, parmnum,
9787 : complain);
9788 1137948 : arg = convert_for_arg_passing (type, arg, complain);
9789 1137948 : pop_deferring_access_checks();
9790 :
9791 1137948 : pop_defarg_context ();
9792 :
9793 1137948 : return arg;
9794 : }
9795 :
9796 : /* Returns the type which will really be used for passing an argument of
9797 : type TYPE. */
9798 :
9799 : tree
9800 676872313 : type_passed_as (tree type)
9801 : {
9802 : /* Pass classes with copy ctors by invisible reference. */
9803 676872313 : if (TREE_ADDRESSABLE (type))
9804 624942 : type = build_reference_type (type);
9805 :
9806 676872313 : return type;
9807 : }
9808 :
9809 : /* Actually perform the appropriate conversion. */
9810 :
9811 : tree
9812 142729793 : convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
9813 : {
9814 142729793 : tree bitfield_type;
9815 :
9816 : /* If VAL is a bitfield, then -- since it has already been converted
9817 : to TYPE -- it cannot have a precision greater than TYPE.
9818 :
9819 : If it has a smaller precision, we must widen it here. For
9820 : example, passing "int f:3;" to a function expecting an "int" will
9821 : not result in any conversion before this point.
9822 :
9823 : If the precision is the same we must not risk widening. For
9824 : example, the COMPONENT_REF for a 32-bit "long long" bitfield will
9825 : often have type "int", even though the C++ type for the field is
9826 : "long long". If the value is being passed to a function
9827 : expecting an "int", then no conversions will be required. But,
9828 : if we call convert_bitfield_to_declared_type, the bitfield will
9829 : be converted to "long long". */
9830 142729793 : bitfield_type = is_bitfield_expr_with_lowered_type (val);
9831 142729793 : if (bitfield_type
9832 142729793 : && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
9833 0 : val = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type), val);
9834 :
9835 142729793 : if (val == error_mark_node)
9836 : ;
9837 : /* Pass classes with copy ctors by invisible reference. */
9838 142726147 : else if (TREE_ADDRESSABLE (type))
9839 211298 : val = build1 (ADDR_EXPR, build_reference_type (type), val);
9840 142729793 : if (complain & tf_warning)
9841 131107550 : maybe_warn_parm_abi (type, cp_expr_loc_or_input_loc (val));
9842 :
9843 105397151 : if (complain & tf_warning)
9844 105397151 : warn_for_address_of_packed_member (type, val);
9845 :
9846 : /* gimplify_arg elides TARGET_EXPRs that initialize a function argument,
9847 : unless the initializer is a CONSTRUCTOR. In that case, we fail to
9848 : elide the copy anyway. See that function for more information. */
9849 7875222 : if (SIMPLE_TARGET_EXPR_P (val)
9850 149188623 : && TREE_CODE (TARGET_EXPR_INITIAL (val)) != CONSTRUCTOR)
9851 3800414 : set_target_expr_eliding (val);
9852 :
9853 142729793 : return val;
9854 : }
9855 :
9856 : /* Returns non-zero iff FN is a function with magic varargs, i.e. ones for
9857 : which just decay_conversion or no conversions at all should be done.
9858 : This is true for some builtins which don't act like normal functions.
9859 : Return 2 if just decay_conversion and removal of excess precision should
9860 : be done, 1 if just decay_conversion. Return 3 for special treatment of
9861 : the 3rd argument for __builtin_*_overflow_p. Return 4 for special
9862 : treatment of the 1st argument for
9863 : __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g. */
9864 :
9865 : int
9866 174162814 : magic_varargs_p (tree fn)
9867 : {
9868 174162814 : if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
9869 6085758 : switch (DECL_FUNCTION_CODE (fn))
9870 : {
9871 : case BUILT_IN_CLASSIFY_TYPE:
9872 : case BUILT_IN_CONSTANT_P:
9873 : case BUILT_IN_NEXT_ARG:
9874 : case BUILT_IN_VA_START:
9875 : return 1;
9876 :
9877 20842 : case BUILT_IN_ADD_OVERFLOW_P:
9878 20842 : case BUILT_IN_SUB_OVERFLOW_P:
9879 20842 : case BUILT_IN_MUL_OVERFLOW_P:
9880 20842 : return 3;
9881 :
9882 319308 : case BUILT_IN_ISFINITE:
9883 319308 : case BUILT_IN_ISINF:
9884 319308 : case BUILT_IN_ISINF_SIGN:
9885 319308 : case BUILT_IN_ISNAN:
9886 319308 : case BUILT_IN_ISNORMAL:
9887 319308 : case BUILT_IN_FPCLASSIFY:
9888 319308 : return 2;
9889 :
9890 101102 : case BUILT_IN_CLZG:
9891 101102 : case BUILT_IN_CTZG:
9892 101102 : case BUILT_IN_CLRSBG:
9893 101102 : case BUILT_IN_FFSG:
9894 101102 : case BUILT_IN_PARITYG:
9895 101102 : case BUILT_IN_POPCOUNTG:
9896 101102 : return 4;
9897 :
9898 5438019 : default:
9899 5438019 : return lookup_attribute ("type generic",
9900 10876038 : TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
9901 : }
9902 :
9903 : return 0;
9904 : }
9905 :
9906 : /* Returns the decl of the dispatcher function if FN is a function version. */
9907 :
9908 : tree
9909 240 : get_function_version_dispatcher (tree fn)
9910 : {
9911 240 : tree dispatcher_decl = NULL;
9912 :
9913 240 : if (DECL_LOCAL_DECL_P (fn))
9914 9 : fn = DECL_LOCAL_DECL_ALIAS (fn);
9915 :
9916 240 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
9917 : && DECL_FUNCTION_VERSIONED (fn));
9918 :
9919 240 : gcc_assert (targetm.get_function_versions_dispatcher);
9920 240 : dispatcher_decl = targetm.get_function_versions_dispatcher (fn);
9921 :
9922 240 : if (dispatcher_decl == NULL)
9923 : {
9924 9 : error_at (input_location, "use of multiversioned function "
9925 : "without a default");
9926 9 : return NULL;
9927 : }
9928 :
9929 231 : retrofit_lang_decl (dispatcher_decl);
9930 231 : gcc_assert (dispatcher_decl != NULL);
9931 : return dispatcher_decl;
9932 : }
9933 :
9934 : /* fn is a function version dispatcher that is marked used. Mark all the
9935 : semantically identical function versions it will dispatch as used. */
9936 :
9937 : void
9938 159 : mark_versions_used (tree fn)
9939 : {
9940 159 : struct cgraph_node *node;
9941 159 : struct cgraph_function_version_info *node_v;
9942 159 : struct cgraph_function_version_info *it_v;
9943 :
9944 159 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9945 :
9946 159 : node = cgraph_node::get (fn);
9947 159 : if (node == NULL)
9948 : return;
9949 :
9950 159 : gcc_assert (node->dispatcher_function);
9951 :
9952 159 : node_v = node->function_version ();
9953 159 : if (node_v == NULL)
9954 : return;
9955 :
9956 : /* All semantically identical versions are chained. Traverse and mark each
9957 : one of them as used. */
9958 159 : it_v = node_v->next;
9959 1146 : while (it_v != NULL)
9960 : {
9961 987 : mark_used (it_v->this_node->decl);
9962 987 : it_v = it_v->next;
9963 : }
9964 : }
9965 :
9966 : /* Build a call to "the copy constructor" for the type of A, even if it
9967 : wouldn't be selected by normal overload resolution. Used for
9968 : diagnostics. */
9969 :
9970 : static tree
9971 3 : call_copy_ctor (tree a, tsubst_flags_t complain)
9972 : {
9973 3 : tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (a));
9974 3 : tree binfo = TYPE_BINFO (ctype);
9975 3 : tree copy = get_copy_ctor (ctype, complain);
9976 3 : copy = build_baselink (binfo, binfo, copy, NULL_TREE);
9977 3 : tree ob = build_dummy_object (ctype);
9978 3 : releasing_vec args (make_tree_vector_single (a));
9979 3 : tree r = build_new_method_call (ob, copy, &args, NULL_TREE,
9980 : LOOKUP_NORMAL, NULL, complain);
9981 3 : return r;
9982 3 : }
9983 :
9984 : /* Return the base constructor corresponding to COMPLETE_CTOR or NULL_TREE. */
9985 :
9986 : static tree
9987 48 : base_ctor_for (tree complete_ctor)
9988 : {
9989 48 : tree clone;
9990 48 : FOR_EACH_CLONE (clone, DECL_CLONED_FUNCTION (complete_ctor))
9991 48 : if (DECL_BASE_CONSTRUCTOR_P (clone))
9992 : return clone;
9993 : return NULL_TREE;
9994 : }
9995 :
9996 : /* Try to make EXP suitable to be used as the initializer for a base subobject,
9997 : and return whether we were successful. EXP must have already been cleared
9998 : by unsafe_copy_elision_p{,_opt}. */
9999 :
10000 : static bool
10001 224 : make_base_init_ok (tree exp)
10002 : {
10003 224 : if (TREE_CODE (exp) == TARGET_EXPR)
10004 224 : exp = TARGET_EXPR_INITIAL (exp);
10005 227 : while (TREE_CODE (exp) == COMPOUND_EXPR)
10006 3 : exp = TREE_OPERAND (exp, 1);
10007 224 : if (TREE_CODE (exp) == COND_EXPR)
10008 : {
10009 3 : bool ret = make_base_init_ok (TREE_OPERAND (exp, 2));
10010 3 : if (tree op1 = TREE_OPERAND (exp, 1))
10011 : {
10012 3 : bool r1 = make_base_init_ok (op1);
10013 : /* If unsafe_copy_elision_p was false, the arms should match. */
10014 3 : gcc_assert (r1 == ret);
10015 : }
10016 : return ret;
10017 : }
10018 221 : if (TREE_CODE (exp) != AGGR_INIT_EXPR)
10019 : /* A trivial copy is OK. */
10020 : return true;
10021 70 : if (!AGGR_INIT_VIA_CTOR_P (exp))
10022 : /* unsafe_copy_elision_p_opt must have said this is OK. */
10023 : return true;
10024 48 : tree fn = cp_get_callee_fndecl_nofold (exp);
10025 48 : if (DECL_BASE_CONSTRUCTOR_P (fn))
10026 : return true;
10027 48 : gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (fn));
10028 48 : fn = base_ctor_for (fn);
10029 48 : if (!fn || DECL_HAS_VTT_PARM_P (fn))
10030 : /* The base constructor has more parameters, so we can't just change the
10031 : call target. It would be possible to splice in the appropriate
10032 : arguments, but probably not worth the complexity. */
10033 : return false;
10034 39 : mark_used (fn);
10035 39 : AGGR_INIT_EXPR_FN (exp) = build_address (fn);
10036 39 : return true;
10037 : }
10038 :
10039 : /* Return 2 if T refers to a base, 1 if a potentially-overlapping field,
10040 : neither of which can be used for return by invisible reference. We avoid
10041 : doing C++17 mandatory copy elision for either of these cases.
10042 :
10043 : This returns non-zero even if the type of T has no tail padding that other
10044 : data could be allocated into, because that depends on the particular ABI.
10045 : unsafe_copy_elision_p_opt does consider whether there is padding. */
10046 :
10047 : int
10048 42845190 : unsafe_return_slot_p (tree t)
10049 : {
10050 : /* Check empty bases separately, they don't have fields. */
10051 42845190 : if (is_empty_base_ref (t))
10052 : return 2;
10053 :
10054 : /* A delegating constructor might be used to initialize a base. */
10055 42360213 : if (current_function_decl
10056 57529092 : && DECL_CONSTRUCTOR_P (current_function_decl)
10057 46590431 : && (t == current_class_ref
10058 4058750 : || tree_strip_nop_conversions (t) == current_class_ptr))
10059 : return 2;
10060 :
10061 42138191 : STRIP_NOPS (t);
10062 42138191 : if (TREE_CODE (t) == ADDR_EXPR)
10063 66840 : t = TREE_OPERAND (t, 0);
10064 42138191 : if (TREE_CODE (t) == COMPONENT_REF)
10065 3546338 : t = TREE_OPERAND (t, 1);
10066 42138191 : if (TREE_CODE (t) != FIELD_DECL)
10067 : return false;
10068 3592648 : if (!CLASS_TYPE_P (TREE_TYPE (t)))
10069 : /* The middle-end will do the right thing for scalar types. */
10070 : return false;
10071 3054850 : if (DECL_FIELD_IS_BASE (t))
10072 : return 2;
10073 2233062 : if (lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (t)))
10074 163351 : return 1;
10075 : return 0;
10076 : }
10077 :
10078 : /* True IFF EXP is a prvalue that represents return by invisible reference. */
10079 :
10080 : static bool
10081 286173 : init_by_return_slot_p (tree exp)
10082 : {
10083 : /* Copy elision only happens with a TARGET_EXPR. */
10084 286176 : if (TREE_CODE (exp) != TARGET_EXPR)
10085 : return false;
10086 15620 : tree init = TARGET_EXPR_INITIAL (exp);
10087 : /* build_compound_expr pushes COMPOUND_EXPR inside TARGET_EXPR. */
10088 15626 : while (TREE_CODE (init) == COMPOUND_EXPR)
10089 6 : init = TREE_OPERAND (init, 1);
10090 15620 : if (TREE_CODE (init) == COND_EXPR)
10091 : {
10092 : /* We'll end up copying from each of the arms of the COND_EXPR directly
10093 : into the target, so look at them. */
10094 6 : if (tree op = TREE_OPERAND (init, 1))
10095 6 : if (init_by_return_slot_p (op))
10096 : return true;
10097 3 : return init_by_return_slot_p (TREE_OPERAND (init, 2));
10098 : }
10099 15614 : return (TREE_CODE (init) == AGGR_INIT_EXPR
10100 15614 : && !AGGR_INIT_VIA_CTOR_P (init));
10101 : }
10102 :
10103 : /* We can't elide a copy from a function returning by value to a
10104 : potentially-overlapping subobject, as the callee might clobber tail padding.
10105 : Return true iff this could be that case.
10106 :
10107 : Places that use this function (or _opt) to decide to elide a copy should
10108 : probably use make_safe_copy_elision instead. */
10109 :
10110 : bool
10111 2427414 : unsafe_copy_elision_p (tree target, tree exp)
10112 : {
10113 2427414 : return unsafe_return_slot_p (target) && init_by_return_slot_p (exp);
10114 : }
10115 :
10116 : /* As above, but for optimization allow more cases that are actually safe. */
10117 :
10118 : static bool
10119 8222808 : unsafe_copy_elision_p_opt (tree target, tree exp)
10120 : {
10121 8222808 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
10122 : /* It's safe to elide the copy for a class with no tail padding. */
10123 8222808 : if (!is_empty_class (type)
10124 8222808 : && tree_int_cst_equal (TYPE_SIZE (type), CLASSTYPE_SIZE (type)))
10125 : return false;
10126 2381104 : return unsafe_copy_elision_p (target, exp);
10127 : }
10128 :
10129 : /* Try to make EXP suitable to be used as the initializer for TARGET,
10130 : and return whether we were successful. */
10131 :
10132 : bool
10133 26 : make_safe_copy_elision (tree target, tree exp)
10134 : {
10135 26 : int uns = unsafe_return_slot_p (target);
10136 26 : if (!uns)
10137 : return true;
10138 26 : if (init_by_return_slot_p (exp))
10139 : return false;
10140 26 : if (uns == 1)
10141 : return true;
10142 22 : return make_base_init_ok (exp);
10143 : }
10144 :
10145 : /* True IFF the result of the conversion C is a prvalue. */
10146 :
10147 : static bool
10148 14272738 : conv_is_prvalue (conversion *c)
10149 : {
10150 14272738 : if (c->kind == ck_rvalue)
10151 : return true;
10152 14272738 : if (c->kind == ck_base && c->need_temporary_p)
10153 : return true;
10154 14272738 : if (c->kind == ck_user && !TYPE_REF_P (c->type))
10155 : return true;
10156 14138023 : if (c->kind == ck_identity && c->u.expr
10157 13885111 : && TREE_CODE (c->u.expr) == TARGET_EXPR)
10158 69 : return true;
10159 :
10160 : return false;
10161 : }
10162 :
10163 : /* True iff C is a conversion that binds a reference to a prvalue. */
10164 :
10165 : static bool
10166 14274040 : conv_binds_ref_to_prvalue (conversion *c)
10167 : {
10168 14274040 : if (c->kind != ck_ref_bind)
10169 : return false;
10170 14274040 : if (c->need_temporary_p)
10171 : return true;
10172 :
10173 14272738 : return conv_is_prvalue (next_conversion (c));
10174 : }
10175 :
10176 : /* True iff EXPR represents a (subobject of a) temporary. */
10177 :
10178 : static bool
10179 14945 : expr_represents_temporary_p (tree expr)
10180 : {
10181 15049 : while (handled_component_p (expr))
10182 104 : expr = TREE_OPERAND (expr, 0);
10183 14945 : return TREE_CODE (expr) == TARGET_EXPR;
10184 : }
10185 :
10186 : /* True iff C is a conversion that binds a reference to a temporary.
10187 : This is a superset of conv_binds_ref_to_prvalue: here we're also
10188 : interested in xvalues. */
10189 :
10190 : static bool
10191 14288 : conv_binds_ref_to_temporary (conversion *c)
10192 : {
10193 14288 : if (conv_binds_ref_to_prvalue (c))
10194 : return true;
10195 13801 : if (c->kind != ck_ref_bind)
10196 : return false;
10197 13801 : c = next_conversion (c);
10198 : /* This is the case for
10199 : struct Base {};
10200 : struct Derived : Base {};
10201 : const Base& b(Derived{});
10202 : where we bind 'b' to the Base subobject of a temporary object of type
10203 : Derived. The subobject is an xvalue; the whole object is a prvalue.
10204 :
10205 : The ck_base doesn't have to be present for cases like X{}.m. */
10206 13801 : if (c->kind == ck_base)
10207 14 : c = next_conversion (c);
10208 13722 : if (c->kind == ck_identity && c->u.expr
10209 27523 : && expr_represents_temporary_p (c->u.expr))
10210 : return true;
10211 : return false;
10212 : }
10213 :
10214 : /* Return tristate::TS_TRUE if converting EXPR to a reference type TYPE binds
10215 : the reference to a temporary. Return tristate::TS_FALSE if converting
10216 : EXPR to a reference type TYPE doesn't bind the reference to a temporary. If
10217 : the conversion is invalid or bad, return tristate::TS_UNKNOWN. DIRECT_INIT_P
10218 : says whether the conversion should be done in direct- or copy-initialization
10219 : context. */
10220 :
10221 : tristate
10222 14817 : ref_conv_binds_to_temporary (tree type, tree expr, bool direct_init_p/*=false*/)
10223 : {
10224 14817 : gcc_assert (TYPE_REF_P (type));
10225 :
10226 14817 : conversion_obstack_sentinel cos;
10227 :
10228 14817 : const int flags = direct_init_p ? LOOKUP_NORMAL : LOOKUP_IMPLICIT;
10229 14817 : conversion *conv = implicit_conversion (type, TREE_TYPE (expr), expr,
10230 : /*c_cast_p=*/false, flags, tf_none);
10231 14817 : if (!conv || conv->bad_p)
10232 529 : return tristate::unknown ();
10233 :
10234 14288 : if (conv_binds_ref_to_temporary (conv))
10235 : {
10236 : /* Actually perform the conversion to check access control. */
10237 499 : if (convert_like (conv, expr, tf_none) != error_mark_node)
10238 487 : return tristate (true);
10239 : else
10240 12 : return tristate::unknown ();
10241 : }
10242 :
10243 13789 : return tristate (false);
10244 14817 : }
10245 :
10246 : /* Call the trivial destructor for INSTANCE, which can be either an lvalue of
10247 : class type or a pointer to class type. If NO_PTR_DEREF is true and
10248 : INSTANCE has pointer type, clobber the pointer rather than what it points
10249 : to. */
10250 :
10251 : tree
10252 3345606 : build_trivial_dtor_call (tree instance, bool no_ptr_deref)
10253 : {
10254 3345606 : gcc_assert (!is_dummy_object (instance));
10255 :
10256 3345606 : if (!flag_lifetime_dse)
10257 : {
10258 58 : no_clobber:
10259 77 : return fold_convert (void_type_node, instance);
10260 : }
10261 :
10262 3390839 : if (INDIRECT_TYPE_P (TREE_TYPE (instance))
10263 3345548 : && (!no_ptr_deref || TYPE_REF_P (TREE_TYPE (instance))))
10264 : {
10265 3182220 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
10266 19 : goto no_clobber;
10267 3182201 : instance = cp_build_fold_indirect_ref (instance);
10268 : }
10269 :
10270 : /* A trivial destructor should still clobber the object. */
10271 3345529 : tree clobber = build_clobber (TREE_TYPE (instance), CLOBBER_OBJECT_END);
10272 3345529 : return build2 (MODIFY_EXPR, void_type_node,
10273 3345529 : instance, clobber);
10274 : }
10275 :
10276 : /* Return true if in an immediate function context, or an unevaluated operand,
10277 : or a default argument/member initializer, or a subexpression of an immediate
10278 : invocation. */
10279 :
10280 : bool
10281 16334076 : in_immediate_context ()
10282 : {
10283 16334076 : return (cp_unevaluated_operand != 0
10284 14649356 : || (current_function_decl != NULL_TREE
10285 24679728 : && DECL_IMMEDIATE_FUNCTION_P (current_function_decl))
10286 : /* DR 2631: default args and DMI aren't immediately evaluated.
10287 : Return true here so immediate_invocation_p returns false. */
10288 14289740 : || current_binding_level->kind == sk_function_parms
10289 14286759 : || current_binding_level->kind == sk_template_parms
10290 14207776 : || parsing_nsdmi ()
10291 30539262 : || in_consteval_if_p);
10292 : }
10293 :
10294 : /* Return true if a call to FN with number of arguments NARGS
10295 : is an immediate invocation. */
10296 :
10297 : bool
10298 215820592 : immediate_invocation_p (tree fn)
10299 : {
10300 215820592 : return (TREE_CODE (fn) == FUNCTION_DECL
10301 215820592 : && DECL_IMMEDIATE_FUNCTION_P (fn)
10302 218914978 : && !in_immediate_context ());
10303 : }
10304 :
10305 : /* Subroutine of the various build_*_call functions. Overload resolution
10306 : has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
10307 : ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
10308 : bitmask of various LOOKUP_* flags which apply to the call itself. */
10309 :
10310 : static tree
10311 231268163 : build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
10312 : {
10313 231268163 : tree fn = cand->fn;
10314 231268163 : const vec<tree, va_gc> *args = cand->args;
10315 231268163 : tree first_arg = cand->first_arg;
10316 231268163 : conversion **convs = cand->convs;
10317 231268163 : tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
10318 231268163 : int parmlen;
10319 231268163 : tree val;
10320 231268163 : int nargs;
10321 231268163 : tree *argarray;
10322 231268163 : bool already_used = false;
10323 :
10324 : /* In a template, there is no need to perform all of the work that
10325 : is normally done. We are only interested in the type of the call
10326 : expression, i.e., the return type of the function. Any semantic
10327 : errors will be deferred until the template is instantiated. */
10328 231268163 : if (processing_template_decl)
10329 : {
10330 30082360 : if (undeduced_auto_decl (fn))
10331 12883 : mark_used (fn, complain);
10332 : else
10333 : /* Otherwise set TREE_USED for the benefit of -Wunused-function.
10334 : See PR80598. */
10335 30069477 : TREE_USED (fn) = 1;
10336 :
10337 30082360 : tree return_type = TREE_TYPE (TREE_TYPE (fn));
10338 30082360 : tree callee;
10339 30082360 : if (first_arg == NULL_TREE)
10340 : {
10341 22491945 : callee = build_addr_func (fn, complain);
10342 22491945 : if (callee == error_mark_node)
10343 : return error_mark_node;
10344 : }
10345 : else
10346 : {
10347 7590415 : callee = build_baselink (cand->conversion_path, cand->access_path,
10348 : fn, NULL_TREE);
10349 7590415 : callee = build_min (COMPONENT_REF, TREE_TYPE (fn),
10350 : first_arg, callee, NULL_TREE);
10351 : }
10352 :
10353 30082360 : tree expr = build_call_vec (return_type, callee, args);
10354 30082360 : SET_EXPR_LOCATION (expr, input_location);
10355 30082360 : if (TREE_THIS_VOLATILE (fn) && cfun)
10356 2589593 : current_function_returns_abnormally = 1;
10357 30082360 : if (TREE_DEPRECATED (fn)
10358 30082360 : && warning_suppressed_at (input_location,
10359 : OPT_Wdeprecated_declarations))
10360 : /* Make the expr consistent with the location. */
10361 19926 : TREE_NO_WARNING (expr) = true;
10362 30082360 : if (immediate_invocation_p (fn))
10363 : {
10364 205968 : tree obj_arg = NULL_TREE;
10365 411936 : if (DECL_CONSTRUCTOR_P (fn))
10366 0 : obj_arg = first_arg;
10367 : /* Look through *(const T *)&obj. */
10368 0 : if (obj_arg && INDIRECT_REF_P (obj_arg))
10369 : {
10370 0 : tree addr = TREE_OPERAND (obj_arg, 0);
10371 0 : STRIP_NOPS (addr);
10372 0 : if (TREE_CODE (addr) == ADDR_EXPR)
10373 : {
10374 0 : tree typeo = TREE_TYPE (obj_arg);
10375 0 : tree typei = TREE_TYPE (TREE_OPERAND (addr, 0));
10376 0 : if (same_type_ignoring_top_level_qualifiers_p (typeo, typei))
10377 0 : obj_arg = TREE_OPERAND (addr, 0);
10378 : }
10379 : }
10380 205968 : fold_non_dependent_expr (expr, complain,
10381 : /*manifestly_const_eval=*/true,
10382 : obj_arg);
10383 : }
10384 30082360 : return convert_from_reference (expr);
10385 : }
10386 :
10387 : /* Give any warnings we noticed during overload resolution. */
10388 201185803 : if (cand->warnings && (complain & tf_warning))
10389 : {
10390 : struct candidate_warning *w;
10391 98 : for (w = cand->warnings; w; w = w->next)
10392 49 : joust (cand, w->loser, 1, complain);
10393 : }
10394 :
10395 : /* Core issue 2327: P0135 doesn't say how to handle the case where the
10396 : argument to the copy constructor ends up being a prvalue after
10397 : conversion. Let's do the normal processing, but pretend we aren't
10398 : actually using the copy constructor. */
10399 201185803 : bool force_elide = false;
10400 201185803 : if (cxx_dialect >= cxx17
10401 199144796 : && cand->num_convs == 1
10402 112650558 : && DECL_COMPLETE_CONSTRUCTOR_P (fn)
10403 39407484 : && (DECL_COPY_CONSTRUCTOR_P (fn)
10404 21378068 : || DECL_MOVE_CONSTRUCTOR_P (fn))
10405 14331284 : && !unsafe_return_slot_p (first_arg)
10406 215445489 : && conv_binds_ref_to_prvalue (convs[0]))
10407 : {
10408 135596 : force_elide = true;
10409 135596 : goto not_really_used;
10410 : }
10411 :
10412 : /* OK, we're actually calling this inherited constructor; set its deletedness
10413 : appropriately. We can get away with doing this here because calling is
10414 : the only way to refer to a constructor. */
10415 402100414 : if (DECL_INHERITED_CTOR (fn)
10416 29219370 : && !deduce_inheriting_ctor (fn))
10417 : {
10418 2 : if (complain & tf_error)
10419 2 : mark_used (fn);
10420 2 : return error_mark_node;
10421 : }
10422 :
10423 : /* Make =delete work with SFINAE. */
10424 201050205 : if (DECL_DELETED_FN (fn))
10425 : {
10426 1030474 : if (complain & tf_error)
10427 : {
10428 2300 : mark_used (fn);
10429 2300 : if (cand->next)
10430 : {
10431 2086 : if (flag_diagnostics_all_candidates)
10432 9 : print_z_candidates (input_location, cand, /*only_viable_p=*/false);
10433 : else
10434 2077 : inform (input_location,
10435 : "use %<-fdiagnostics-all-candidates%> to display "
10436 : "considered candidates");
10437 : }
10438 : }
10439 1030474 : return error_mark_node;
10440 : }
10441 :
10442 200019731 : if (DECL_FUNCTION_MEMBER_P (fn))
10443 : {
10444 106637525 : tree access_fn;
10445 : /* If FN is a template function, two cases must be considered.
10446 : For example:
10447 :
10448 : struct A {
10449 : protected:
10450 : template <class T> void f();
10451 : };
10452 : template <class T> struct B {
10453 : protected:
10454 : void g();
10455 : };
10456 : struct C : A, B<int> {
10457 : using A::f; // #1
10458 : using B<int>::g; // #2
10459 : };
10460 :
10461 : In case #1 where `A::f' is a member template, DECL_ACCESS is
10462 : recorded in the primary template but not in its specialization.
10463 : We check access of FN using its primary template.
10464 :
10465 : In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
10466 : because it is a member of class template B, DECL_ACCESS is
10467 : recorded in the specialization `B<int>::g'. We cannot use its
10468 : primary template because `B<T>::g' and `B<int>::g' may have
10469 : different access. */
10470 106637525 : if (DECL_TEMPLATE_INFO (fn)
10471 106637525 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
10472 8038557 : access_fn = DECL_TI_TEMPLATE (fn);
10473 : else
10474 : access_fn = fn;
10475 106637525 : if (!perform_or_defer_access_check (cand->access_path, access_fn,
10476 : fn, complain))
10477 21049 : return error_mark_node;
10478 : }
10479 :
10480 : /* If we're checking for implicit delete, don't bother with argument
10481 : conversions. */
10482 199998682 : if (flags & LOOKUP_SPECULATIVE)
10483 : {
10484 25968270 : if (cand->viable == 1)
10485 : return fn;
10486 178 : else if (!(complain & tf_error))
10487 : /* Reject bad conversions now. */
10488 146 : return error_mark_node;
10489 : /* else continue to get conversion error. */
10490 : }
10491 :
10492 174030412 : not_really_used:
10493 :
10494 : /* N3276 magic doesn't apply to nested calls. */
10495 174166040 : tsubst_flags_t decltype_flag = (complain & tf_decltype);
10496 174166040 : complain &= ~tf_decltype;
10497 : /* No-Cleanup doesn't apply to nested calls either. */
10498 174166040 : tsubst_flags_t no_cleanup_complain = complain;
10499 174166040 : complain &= ~tf_no_cleanup;
10500 :
10501 : /* Find maximum size of vector to hold converted arguments. */
10502 174166040 : parmlen = list_length (parm);
10503 327783129 : nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0);
10504 174166040 : if (parmlen > nargs)
10505 : nargs = parmlen;
10506 174166040 : argarray = XALLOCAVEC (tree, nargs);
10507 :
10508 348332077 : in_consteval_if_p_temp_override icip;
10509 : /* If the call is immediate function invocation, make sure
10510 : taking address of immediate functions is allowed in its arguments. */
10511 174166040 : if (immediate_invocation_p (STRIP_TEMPLATE (fn)))
10512 1557153 : in_consteval_if_p = true;
10513 :
10514 174166040 : int argarray_size = 0;
10515 174166040 : unsigned int arg_index = 0;
10516 174166040 : int conv_index = 0;
10517 174166040 : int param_index = 0;
10518 174166040 : tree parmd = DECL_ARGUMENTS (fn);
10519 :
10520 241947509 : auto consume_object_arg = [&arg_index, &first_arg, args]()
10521 : {
10522 67781469 : if (!first_arg)
10523 0 : return (*args)[arg_index++];
10524 67781469 : tree object_arg = first_arg;
10525 67781469 : first_arg = NULL_TREE;
10526 67781469 : return object_arg;
10527 174166040 : };
10528 :
10529 : /* The implicit parameters to a constructor are not considered by overload
10530 : resolution, and must be of the proper type. */
10531 174166040 : if (DECL_CONSTRUCTOR_P (fn))
10532 : {
10533 20765397 : tree object_arg = consume_object_arg ();
10534 20765397 : argarray[argarray_size++] = build_this (object_arg);
10535 20765397 : parm = TREE_CHAIN (parm);
10536 20765397 : if (parmd)
10537 20765397 : parmd = DECL_CHAIN (parmd);
10538 : /* We should never try to call the abstract constructor. */
10539 20765397 : gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
10540 :
10541 20765397 : if (DECL_HAS_VTT_PARM_P (fn))
10542 : {
10543 17626 : argarray[argarray_size++] = (*args)[arg_index];
10544 17626 : ++arg_index;
10545 17626 : parm = TREE_CHAIN (parm);
10546 17626 : if (parmd)
10547 17626 : parmd = DECL_CHAIN (parmd);
10548 : }
10549 : }
10550 : /* Bypass access control for 'this' parameter. */
10551 153400643 : else if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
10552 : {
10553 47006830 : tree arg = build_this (consume_object_arg ());
10554 47006830 : tree argtype = TREE_TYPE (arg);
10555 :
10556 47006830 : if (arg == error_mark_node)
10557 : return error_mark_node;
10558 47006827 : if (convs[conv_index++]->bad_p)
10559 : {
10560 1225 : if (complain & tf_error)
10561 : {
10562 105 : auto_diagnostic_group d;
10563 105 : if (permerror (input_location, "passing %qT as %<this%> "
10564 : "argument discards qualifiers",
10565 105 : TREE_TYPE (argtype)))
10566 102 : inform (DECL_SOURCE_LOCATION (fn), " in call to %qD", fn);
10567 105 : }
10568 : else
10569 : return error_mark_node;
10570 : }
10571 :
10572 : /* The class where FN is defined. */
10573 47005707 : tree ctx = DECL_CONTEXT (fn);
10574 :
10575 : /* See if the function member or the whole class type is declared
10576 : final and the call can be devirtualized. */
10577 47005707 : if (DECL_FINAL_P (fn) || CLASSTYPE_FINAL (ctx))
10578 133725 : flags |= LOOKUP_NONVIRTUAL;
10579 :
10580 : /* [class.mfct.non-static]: If a non-static member function of a class
10581 : X is called for an object that is not of type X, or of a type
10582 : derived from X, the behavior is undefined.
10583 :
10584 : So we can assume that anything passed as 'this' is non-null, and
10585 : optimize accordingly. */
10586 : /* Check that the base class is accessible. */
10587 47005707 : if (!accessible_base_p (TREE_TYPE (argtype),
10588 47005707 : BINFO_TYPE (cand->conversion_path), true))
10589 : {
10590 33 : if (complain & tf_error)
10591 30 : error ("%qT is not an accessible base of %qT",
10592 30 : BINFO_TYPE (cand->conversion_path),
10593 30 : TREE_TYPE (argtype));
10594 : else
10595 3 : return error_mark_node;
10596 : }
10597 : /* If fn was found by a using declaration, the conversion path
10598 : will be to the derived class, not the base declaring fn. We
10599 : must convert to the base. */
10600 47005704 : tree base_binfo = cand->conversion_path;
10601 47005704 : if (BINFO_TYPE (base_binfo) != ctx)
10602 : {
10603 122482 : base_binfo = lookup_base (base_binfo, ctx, ba_unique, NULL, complain);
10604 122482 : if (base_binfo == error_mark_node)
10605 : return error_mark_node;
10606 : }
10607 :
10608 : /* If we know the dynamic type of the object, look up the final overrider
10609 : in the BINFO. */
10610 47922151 : if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
10611 47508149 : && resolves_to_fixed_type_p (arg))
10612 : {
10613 1212 : tree ov = lookup_vfn_in_binfo (DECL_VINDEX (fn), base_binfo);
10614 :
10615 : /* And unwind base_binfo to match. If we don't find the type we're
10616 : looking for in BINFO_INHERITANCE_CHAIN, we're looking at diamond
10617 : inheritance; for now do a normal virtual call in that case. */
10618 1212 : tree octx = DECL_CONTEXT (ov);
10619 1212 : tree obinfo = base_binfo;
10620 2511 : while (obinfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (obinfo), octx))
10621 45 : obinfo = BINFO_INHERITANCE_CHAIN (obinfo);
10622 1212 : if (obinfo)
10623 : {
10624 1209 : fn = ov;
10625 1209 : base_binfo = obinfo;
10626 1209 : flags |= LOOKUP_NONVIRTUAL;
10627 : }
10628 : }
10629 :
10630 47005698 : tree converted_arg = build_base_path (PLUS_EXPR, arg,
10631 : base_binfo, 1, complain);
10632 :
10633 47005698 : argarray[argarray_size++] = converted_arg;
10634 47005698 : parm = TREE_CHAIN (parm);
10635 47005698 : if (parmd)
10636 47005698 : parmd = DECL_CHAIN (parmd);
10637 : }
10638 :
10639 310887425 : auto handle_arg = [fn, flags](tree type,
10640 : tree arg,
10641 : int const param_index,
10642 : conversion *conv,
10643 : tsubst_flags_t const arg_complain)
10644 : {
10645 : /* Set user_conv_p on the argument conversions, so rvalue/base handling
10646 : knows not to allow any more UDCs. This needs to happen after we
10647 : process cand->warnings. */
10648 136722517 : if (flags & LOOKUP_NO_CONVERSION)
10649 3716671 : conv->user_conv_p = true;
10650 :
10651 136722517 : if (arg_complain & tf_warning)
10652 99819308 : maybe_warn_pessimizing_move (arg, type, /*return_p=*/false);
10653 :
10654 136722517 : tree val = convert_like_with_context (conv, arg, fn,
10655 : param_index, arg_complain);
10656 136722517 : val = convert_for_arg_passing (type, val, arg_complain);
10657 136722517 : return val;
10658 174164908 : };
10659 :
10660 312023283 : auto handle_indeterminate_arg = [](tree parmd, tree val)
10661 : {
10662 137858375 : if (parmd
10663 137858375 : && lookup_attribute (NULL, "indeterminate", DECL_ATTRIBUTES (parmd)))
10664 : {
10665 48 : STRIP_NOPS (val);
10666 48 : if (TREE_CODE (val) == ADDR_EXPR
10667 48 : && TREE_CODE (TREE_OPERAND (val, 0)) == TARGET_EXPR)
10668 : {
10669 48 : val = TARGET_EXPR_SLOT (TREE_OPERAND (val, 0));
10670 48 : if (auto_var_p (val) && DECL_ARTIFICIAL (val))
10671 : {
10672 48 : tree id = get_identifier ("indeterminate");
10673 48 : DECL_ATTRIBUTES (val)
10674 96 : = tree_cons (build_tree_list (NULL_TREE, id), NULL_TREE,
10675 48 : DECL_ATTRIBUTES (val));
10676 : }
10677 : }
10678 : }
10679 137858375 : };
10680 :
10681 174164908 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
10682 : {
10683 9242 : gcc_assert (cand->num_convs > 0);
10684 9242 : tree object_arg = consume_object_arg ();
10685 9242 : val = handle_arg (TREE_VALUE (parm),
10686 : object_arg,
10687 : param_index++,
10688 9242 : convs[conv_index++],
10689 : complain);
10690 :
10691 9242 : if (val == error_mark_node)
10692 : return error_mark_node;
10693 : else
10694 : {
10695 9074 : argarray[argarray_size++] = val;
10696 9074 : handle_indeterminate_arg (parmd, val);
10697 : }
10698 9074 : parm = TREE_CHAIN (parm);
10699 9074 : if (parmd)
10700 9074 : parmd = DECL_CHAIN (parmd);
10701 : }
10702 :
10703 174164740 : gcc_assert (first_arg == NULL_TREE);
10704 310876189 : for (; arg_index < vec_safe_length (args) && parm;
10705 136711449 : parm = TREE_CHAIN (parm), ++arg_index, ++param_index, ++conv_index)
10706 : {
10707 136713275 : tree current_arg = (*args)[arg_index];
10708 :
10709 : /* If the argument is NULL and used to (implicitly) instantiate a
10710 : template function (and bind one of the template arguments to
10711 : the type of 'long int'), we don't want to warn about passing NULL
10712 : to non-pointer argument.
10713 : For example, if we have this template function:
10714 :
10715 : template<typename T> void func(T x) {}
10716 :
10717 : we want to warn (when -Wconversion is enabled) in this case:
10718 :
10719 : void foo() {
10720 : func<int>(NULL);
10721 : }
10722 :
10723 : but not in this case:
10724 :
10725 : void foo() {
10726 : func(NULL);
10727 : }
10728 : */
10729 136713275 : bool conversion_warning = !(null_node_p (current_arg)
10730 48293 : && DECL_TEMPLATE_INFO (fn)
10731 61 : && cand->template_decl
10732 30 : && !cand->explicit_targs);
10733 :
10734 : /* Also don't warn about (x <=> y) < 0 (c++/100903). */
10735 : if (conversion_warning
10736 136713260 : && integer_zerop (current_arg)
10737 10568167 : && warn_zero_as_null_pointer_constant
10738 195 : && !warning_enabled_at (DECL_SOURCE_LOCATION (fn),
10739 195 : OPT_Wzero_as_null_pointer_constant))
10740 120 : conversion_warning = false;
10741 :
10742 135 : tsubst_flags_t const arg_complain
10743 136713155 : = conversion_warning ? complain : complain & ~tf_warning;
10744 :
10745 136713275 : val = handle_arg (TREE_VALUE (parm),
10746 : current_arg,
10747 : param_index,
10748 136713275 : convs[conv_index],
10749 : arg_complain);
10750 :
10751 136713275 : if (val == error_mark_node)
10752 : return error_mark_node;
10753 : else
10754 : {
10755 136711449 : argarray[argarray_size++] = val;
10756 136711449 : handle_indeterminate_arg (parmd, val);
10757 : }
10758 136711449 : if (parmd)
10759 125490578 : parmd = DECL_CHAIN (parmd);
10760 : }
10761 :
10762 : /* Default arguments */
10763 175300766 : for (; parm && parm != void_list_node;
10764 1137852 : parm = TREE_CHAIN (parm), param_index++)
10765 : {
10766 1137973 : if (TREE_VALUE (parm) == error_mark_node)
10767 : return error_mark_node;
10768 2275922 : val = convert_default_arg (TREE_VALUE (parm),
10769 1137961 : TREE_PURPOSE (parm),
10770 : fn, param_index,
10771 : complain);
10772 1137961 : if (val == error_mark_node)
10773 : return error_mark_node;
10774 1137852 : argarray[argarray_size++] = val;
10775 1137852 : handle_indeterminate_arg (parmd, val);
10776 1137852 : if (parmd)
10777 1137852 : parmd = DECL_CHAIN (parmd);
10778 : }
10779 :
10780 : /* Ellipsis */
10781 174162793 : int magic = magic_varargs_p (fn);
10782 351389165 : for (; arg_index < vec_safe_length (args); ++arg_index)
10783 : {
10784 3063600 : tree a = (*args)[arg_index];
10785 3063600 : if ((magic == 3 && arg_index == 2) || (magic == 4 && arg_index == 0))
10786 : {
10787 : /* Do no conversions for certain magic varargs. */
10788 121908 : a = mark_type_use (a);
10789 121908 : if (TREE_CODE (a) == FUNCTION_DECL && reject_gcc_builtin (a))
10790 0 : return error_mark_node;
10791 : }
10792 2941692 : else if (magic != 0)
10793 : {
10794 : /* Don't truncate excess precision to the semantic type. */
10795 1499767 : if (magic == 1 && TREE_CODE (a) == EXCESS_PRECISION_EXPR)
10796 84 : a = TREE_OPERAND (a, 0);
10797 : /* For other magic varargs only do decay_conversion. */
10798 1499767 : a = decay_conversion (a, complain);
10799 : }
10800 1441925 : else if (DECL_CONSTRUCTOR_P (fn)
10801 387 : && vec_safe_length (args) == 1
10802 1442126 : && same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (fn),
10803 201 : TREE_TYPE (a)))
10804 : {
10805 : /* Avoid infinite recursion trying to call A(...). */
10806 3 : if (complain & tf_error)
10807 : /* Try to call the actual copy constructor for a good error. */
10808 3 : call_copy_ctor (a, complain);
10809 3 : return error_mark_node;
10810 : }
10811 : else
10812 1441922 : a = convert_arg_to_ellipsis (a, complain);
10813 3063597 : if (a == error_mark_node)
10814 : return error_mark_node;
10815 3063579 : argarray[argarray_size++] = a;
10816 : }
10817 :
10818 174162772 : gcc_assert (argarray_size <= nargs);
10819 174162772 : nargs = argarray_size;
10820 174162772 : icip.reset ();
10821 :
10822 : /* Avoid performing argument transformation if warnings are disabled.
10823 : When tf_warning is set and at least one of the warnings is active
10824 : the check_function_arguments function might warn about something. */
10825 :
10826 174162772 : bool warned_p = false;
10827 174162772 : if ((complain & tf_warning)
10828 113499666 : && (warn_nonnull
10829 111513383 : || warn_format
10830 111513380 : || warn_suggest_attribute_format
10831 111513284 : || warn_restrict))
10832 : {
10833 1988629 : tree *fargs = (!nargs ? argarray
10834 1638852 : : (tree *) alloca (nargs * sizeof (tree)));
10835 6046519 : for (int j = 0; j < nargs; j++)
10836 : {
10837 : /* For -Wformat undo the implicit passing by hidden reference
10838 : done by convert_arg_to_ellipsis. */
10839 4057890 : if (TREE_CODE (argarray[j]) == ADDR_EXPR
10840 4057890 : && TYPE_REF_P (TREE_TYPE (argarray[j])))
10841 1498 : fargs[j] = TREE_OPERAND (argarray[j], 0);
10842 : else
10843 4056392 : fargs[j] = argarray[j];
10844 : }
10845 :
10846 1988629 : warned_p = check_function_arguments (input_location, fn, TREE_TYPE (fn),
10847 : nargs, fargs, NULL,
10848 : cp_comp_parm_types);
10849 : }
10850 :
10851 348325544 : if (DECL_INHERITED_CTOR (fn))
10852 : {
10853 : /* Check for passing ellipsis arguments to an inherited constructor. We
10854 : could handle this by open-coding the inherited constructor rather than
10855 : defining it, but let's not bother now. */
10856 64744 : if (!cp_unevaluated_operand
10857 64321 : && cand->num_convs
10858 64321 : && cand->convs[cand->num_convs-1]->ellipsis_p)
10859 : {
10860 15 : if (complain & tf_error)
10861 : {
10862 15 : auto_diagnostic_group d;
10863 15 : sorry ("passing arguments to ellipsis of inherited constructor "
10864 : "%qD", cand->fn);
10865 15 : inform (DECL_SOURCE_LOCATION (cand->fn), "declared here");
10866 15 : }
10867 15 : return error_mark_node;
10868 : }
10869 :
10870 : /* A base constructor inheriting from a virtual base doesn't get the
10871 : inherited arguments, just this and __vtt. */
10872 64729 : if (ctor_omit_inherited_parms (fn))
10873 174162757 : nargs = 2;
10874 : }
10875 :
10876 : /* Avoid actually calling copy constructors and copy assignment operators,
10877 : if possible. */
10878 :
10879 174162757 : if (!force_elide
10880 174027729 : && (!flag_elide_constructors
10881 : /* It's unsafe to elide the operation when handling
10882 : a noexcept-expression, it may evaluate to the wrong
10883 : value (c++/53025, c++/96090). */
10884 174027525 : || cp_noexcept_operand != 0))
10885 : /* Do things the hard way. */;
10886 171400000 : else if (cand->num_convs == 1
10887 261731386 : && (DECL_COPY_CONSTRUCTOR_P (fn)
10888 169884566 : || DECL_MOVE_CONSTRUCTOR_P (fn)))
10889 : {
10890 8222808 : tree targ;
10891 8222808 : tree arg = argarray[num_artificial_parms_for (fn)];
10892 8222808 : tree fa = argarray[0];
10893 8222808 : bool trivial = trivial_fn_p (fn);
10894 :
10895 : /* Pull out the real argument, disregarding const-correctness. */
10896 8222808 : targ = arg;
10897 : /* Strip the reference binding for the constructor parameter. */
10898 0 : if (CONVERT_EXPR_P (targ)
10899 8222808 : && TYPE_REF_P (TREE_TYPE (targ)))
10900 8222808 : targ = TREE_OPERAND (targ, 0);
10901 : /* But don't strip any other reference bindings; binding a temporary to a
10902 : reference prevents copy elision. */
10903 13612258 : while ((CONVERT_EXPR_P (targ)
10904 11460095 : && !TYPE_REF_P (TREE_TYPE (targ)))
10905 28568524 : || TREE_CODE (targ) == NON_LVALUE_EXPR)
10906 10272137 : targ = TREE_OPERAND (targ, 0);
10907 8222808 : if (TREE_CODE (targ) == ADDR_EXPR)
10908 : {
10909 2498369 : targ = TREE_OPERAND (targ, 0);
10910 2498369 : if (!same_type_ignoring_top_level_qualifiers_p
10911 2498369 : (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
10912 : targ = NULL_TREE;
10913 : }
10914 : else
10915 : targ = NULL_TREE;
10916 :
10917 2497917 : if (targ)
10918 : arg = targ;
10919 : else
10920 5724891 : arg = cp_build_fold_indirect_ref (arg);
10921 :
10922 : /* In C++17 we shouldn't be copying a TARGET_EXPR except into a
10923 : potentially-overlapping subobject. */
10924 8222808 : if (CHECKING_P && cxx_dialect >= cxx17)
10925 8143340 : gcc_assert (TREE_CODE (arg) != TARGET_EXPR
10926 : || force_elide
10927 : /* It's from binding the ref parm to a packed field. */
10928 : || convs[0]->need_temporary_p
10929 : || seen_error ()
10930 : /* See unsafe_copy_elision_p. */
10931 : || unsafe_return_slot_p (fa));
10932 :
10933 8222808 : bool unsafe = unsafe_copy_elision_p_opt (fa, arg);
10934 8222808 : bool eliding_temp = (TREE_CODE (arg) == TARGET_EXPR && !unsafe);
10935 :
10936 : /* [class.copy]: the copy constructor is implicitly defined even if the
10937 : implementation elided its use. But don't warn about deprecation when
10938 : eliding a temporary, as then no copy is actually performed. */
10939 8222808 : warning_sentinel s (warn_deprecated_copy, eliding_temp);
10940 8222808 : if (force_elide)
10941 : /* The language says this isn't called. */;
10942 8087780 : else if (!trivial)
10943 : {
10944 1361530 : if (!mark_used (fn, complain) && !(complain & tf_error))
10945 0 : return error_mark_node;
10946 : already_used = true;
10947 : }
10948 : else
10949 6726250 : cp_handle_deprecated_or_unavailable (fn, complain);
10950 :
10951 197213 : if (eliding_temp && DECL_BASE_CONSTRUCTOR_P (fn)
10952 8223004 : && !make_base_init_ok (arg))
10953 : unsafe = true;
10954 :
10955 : /* If we're creating a temp and we already have one, don't create a
10956 : new one. If we're not creating a temp but we get one, use
10957 : INIT_EXPR to collapse the temp into our target. Otherwise, if the
10958 : ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
10959 : temp or an INIT_EXPR otherwise. */
10960 8222808 : if (is_dummy_object (fa))
10961 : {
10962 6065949 : if (TREE_CODE (arg) == TARGET_EXPR)
10963 : return arg;
10964 5900039 : else if (trivial)
10965 5328951 : return force_target_expr (DECL_CONTEXT (fn), arg, complain);
10966 : }
10967 2156859 : else if ((trivial || TREE_CODE (arg) == TARGET_EXPR)
10968 1372935 : && !unsafe)
10969 : {
10970 1372841 : tree to = cp_build_fold_indirect_ref (fa);
10971 1372841 : val = cp_build_init_expr (to, arg);
10972 1372841 : return val;
10973 : }
10974 8222808 : }
10975 163177192 : else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
10976 3230248 : && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR)
10977 165368128 : && trivial_fn_p (fn))
10978 : {
10979 1540568 : tree to = cp_build_fold_indirect_ref (argarray[0]);
10980 1540568 : tree type = TREE_TYPE (to);
10981 1540568 : tree as_base = CLASSTYPE_AS_BASE (type);
10982 1540568 : tree arg = argarray[1];
10983 1540568 : location_t loc = cp_expr_loc_or_input_loc (arg);
10984 :
10985 1540568 : if (is_really_empty_class (type, /*ignore_vptr*/true))
10986 : {
10987 : /* Avoid copying empty classes, but ensure op= returns an lvalue even
10988 : if the object argument isn't one. */
10989 238330 : to = force_lvalue (to, complain);
10990 238330 : val = build2 (COMPOUND_EXPR, type, arg, to);
10991 238330 : suppress_warning (val, OPT_Wunused);
10992 : }
10993 1302238 : else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
10994 : {
10995 1009326 : if (is_std_init_list (type)
10996 1009326 : && conv_binds_ref_to_prvalue (convs[1]))
10997 3 : warning_at (loc, OPT_Winit_list_lifetime,
10998 : "assignment from temporary %<initializer_list%> does "
10999 : "not extend the lifetime of the underlying array");
11000 1009326 : arg = cp_build_fold_indirect_ref (arg);
11001 1009326 : val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
11002 : }
11003 : else
11004 : {
11005 : /* We must only copy the non-tail padding parts. */
11006 292912 : tree arg0, arg2, t;
11007 292912 : tree array_type, alias_set;
11008 :
11009 292912 : arg2 = TYPE_SIZE_UNIT (as_base);
11010 : /* Ensure op= returns an lvalue even if the object argument isn't
11011 : one. */
11012 292912 : to = force_lvalue (to, complain);
11013 292912 : to = cp_stabilize_reference (to);
11014 292912 : arg0 = cp_build_addr_expr (to, complain);
11015 :
11016 292912 : array_type = build_array_type (unsigned_char_type_node,
11017 : build_index_type
11018 : (size_binop (MINUS_EXPR,
11019 : arg2, size_int (1))));
11020 292912 : alias_set = build_int_cst (build_pointer_type (type), 0);
11021 292912 : t = build2 (MODIFY_EXPR, void_type_node,
11022 : build2 (MEM_REF, array_type, arg0, alias_set),
11023 : build2 (MEM_REF, array_type, arg, alias_set));
11024 292912 : val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
11025 292912 : suppress_warning (val, OPT_Wunused);
11026 : }
11027 :
11028 1540568 : cp_handle_deprecated_or_unavailable (fn, complain);
11029 :
11030 1540568 : return val;
11031 : }
11032 161636624 : else if (trivial_fn_p (fn))
11033 : {
11034 7903704 : if (DECL_DESTRUCTOR_P (fn))
11035 3165595 : return build_trivial_dtor_call (argarray[0]);
11036 786257 : else if (default_ctor_p (fn))
11037 : {
11038 786257 : if (is_dummy_object (argarray[0]))
11039 309139 : return force_target_expr (DECL_CONTEXT (fn), void_node,
11040 309139 : no_cleanup_complain);
11041 : else
11042 477118 : return cp_build_fold_indirect_ref (argarray[0]);
11043 : }
11044 : }
11045 :
11046 161802635 : gcc_assert (!force_elide);
11047 :
11048 161802635 : if (!already_used
11049 161802635 : && !mark_used (fn, complain))
11050 791 : return error_mark_node;
11051 :
11052 : /* Warn if the built-in writes to an object of a non-trivial type. */
11053 161801841 : if (warn_class_memaccess
11054 2046933 : && vec_safe_length (args) >= 2
11055 162779311 : && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
11056 69655 : maybe_warn_class_memaccess (input_location, fn, args);
11057 :
11058 161801841 : if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
11059 : {
11060 501242 : tree t;
11061 501242 : tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
11062 501242 : DECL_CONTEXT (fn),
11063 : ba_any, NULL, complain);
11064 501242 : gcc_assert (binfo && binfo != error_mark_node);
11065 :
11066 501242 : argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
11067 : complain);
11068 501242 : if (TREE_SIDE_EFFECTS (argarray[0]))
11069 47640 : argarray[0] = save_expr (argarray[0]);
11070 501242 : t = build_pointer_type (TREE_TYPE (fn));
11071 501242 : fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
11072 501242 : TREE_TYPE (fn) = t;
11073 : }
11074 : else
11075 : {
11076 : /* If FN is marked deprecated or unavailable, then we've already
11077 : issued a diagnostic from mark_used above, so avoid redundantly
11078 : issuing another one from build_addr_func. */
11079 161300599 : auto w = make_temp_override (deprecated_state,
11080 161300599 : UNAVAILABLE_DEPRECATED_SUPPRESS);
11081 :
11082 161300599 : fn = build_addr_func (fn, complain);
11083 161300599 : if (fn == error_mark_node)
11084 0 : return error_mark_node;
11085 :
11086 : /* We're actually invoking the function. (Immediate functions get an
11087 : & when invoking it even though the user didn't use &.) */
11088 161300599 : ADDR_EXPR_DENOTES_CALL_P (fn) = true;
11089 161300599 : }
11090 :
11091 161801841 : tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag);
11092 161801841 : if (call == error_mark_node)
11093 : return call;
11094 161800867 : if (cand->flags & LOOKUP_LIST_INIT_CTOR)
11095 : {
11096 2037727 : tree c = extract_call_expr (call);
11097 : /* build_new_op will clear this when appropriate. */
11098 2037727 : CALL_EXPR_ORDERED_ARGS (c) = true;
11099 : }
11100 161800867 : if (warned_p)
11101 : {
11102 253 : tree c = extract_call_expr (call);
11103 253 : if (TREE_CODE (c) == CALL_EXPR)
11104 253 : suppress_warning (c /* Suppress all warnings. */);
11105 : }
11106 161800614 : else if (TREE_DEPRECATED (fn)
11107 161800614 : && warning_suppressed_at (input_location,
11108 : OPT_Wdeprecated_declarations))
11109 : {
11110 0 : tree c = extract_call_expr (call);
11111 0 : if (TREE_CODE (c) == CALL_EXPR)
11112 0 : TREE_NO_WARNING (c) = true;
11113 : }
11114 :
11115 : return call;
11116 : }
11117 :
11118 : namespace
11119 : {
11120 :
11121 : /* Return the DECL of the first non-static subobject of class TYPE
11122 : that satisfies the predicate PRED or null if none can be found. */
11123 :
11124 : template <class Predicate>
11125 : tree
11126 3556 : first_non_static_field (tree type, Predicate pred)
11127 : {
11128 3556 : if (!type || !CLASS_TYPE_P (type))
11129 : return NULL_TREE;
11130 :
11131 71398 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
11132 : {
11133 68436 : if (TREE_CODE (field) != FIELD_DECL)
11134 43330 : continue;
11135 25106 : if (TREE_STATIC (field))
11136 0 : continue;
11137 25106 : if (pred (field))
11138 : return field;
11139 : }
11140 :
11141 2962 : int i = 0;
11142 :
11143 3082 : for (tree base_binfo, binfo = TYPE_BINFO (type);
11144 3082 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11145 : {
11146 126 : tree base = TREE_TYPE (base_binfo);
11147 126 : if (pred (base))
11148 : return base;
11149 120 : if (tree field = first_non_static_field (base, pred))
11150 : return field;
11151 : }
11152 :
11153 : return NULL_TREE;
11154 : }
11155 :
11156 : struct NonPublicField
11157 : {
11158 24356 : bool operator() (const_tree t) const
11159 : {
11160 24356 : return DECL_P (t) && (TREE_PRIVATE (t) || TREE_PROTECTED (t));
11161 : }
11162 : };
11163 :
11164 : /* Return the DECL of the first non-public subobject of class TYPE
11165 : or null if none can be found. */
11166 :
11167 : static inline tree
11168 3262 : first_non_public_field (tree type)
11169 : {
11170 3262 : return first_non_static_field (type, NonPublicField ());
11171 : }
11172 :
11173 : struct NonTrivialField
11174 : {
11175 876 : bool operator() (const_tree t) const
11176 : {
11177 876 : return !trivial_type_p (DECL_P (t) ? TREE_TYPE (t) : t);
11178 : }
11179 : };
11180 :
11181 : /* Return the DECL of the first non-trivial subobject of class TYPE
11182 : or null if none can be found. */
11183 :
11184 : static inline tree
11185 174 : first_non_trivial_field (tree type)
11186 : {
11187 174 : return first_non_static_field (type, NonTrivialField ());
11188 : }
11189 :
11190 : } /* unnamed namespace */
11191 :
11192 : /* Return true if all copy and move assignment operator overloads for
11193 : class TYPE are trivial and at least one of them is not deleted and,
11194 : when ACCESS is set, accessible. Return false otherwise. Set
11195 : HASASSIGN to true when the TYPE has a (not necessarily trivial)
11196 : copy or move assignment. */
11197 :
11198 : static bool
11199 5021 : has_trivial_copy_assign_p (tree type, bool access, bool *hasassign)
11200 : {
11201 5021 : tree fns = get_class_binding (type, assign_op_identifier);
11202 5021 : bool all_trivial = true;
11203 :
11204 : /* Iterate over overloads of the assignment operator, checking
11205 : accessible copy assignments for triviality. */
11206 :
11207 16515 : for (tree f : ovl_range (fns))
11208 : {
11209 : /* Skip operators that aren't copy assignments. */
11210 8571 : if (!copy_fn_p (f))
11211 3046 : continue;
11212 :
11213 5525 : bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
11214 5813 : || accessible_p (TYPE_BINFO (type), f, true));
11215 :
11216 : /* Skip template assignment operators and deleted functions. */
11217 5525 : if (TREE_CODE (f) != FUNCTION_DECL || DECL_DELETED_FN (f))
11218 694 : continue;
11219 :
11220 4831 : if (accessible)
11221 4579 : *hasassign = true;
11222 :
11223 4579 : if (!accessible || !trivial_fn_p (f))
11224 : all_trivial = false;
11225 :
11226 : /* Break early when both properties have been determined. */
11227 4831 : if (*hasassign && !all_trivial)
11228 : break;
11229 : }
11230 :
11231 : /* Return true if they're all trivial and one of the expressions
11232 : TYPE() = TYPE() or TYPE() = (TYPE&)() is valid. */
11233 5021 : tree ref = cp_build_reference_type (type, false);
11234 5021 : return (all_trivial
11235 5021 : && (is_trivially_xible (MODIFY_EXPR, type, type)
11236 333 : || is_trivially_xible (MODIFY_EXPR, type, ref)));
11237 : }
11238 :
11239 : /* Return true if all copy and move ctor overloads for class TYPE are
11240 : trivial and at least one of them is not deleted and, when ACCESS is
11241 : set, accessible. Return false otherwise. Set each element of HASCTOR[]
11242 : to true when the TYPE has a (not necessarily trivial) default and copy
11243 : (or move) ctor, respectively. */
11244 :
11245 : static bool
11246 5021 : has_trivial_copy_p (tree type, bool access, bool hasctor[2])
11247 : {
11248 5021 : tree fns = get_class_binding (type, complete_ctor_identifier);
11249 5021 : bool all_trivial = true;
11250 :
11251 25141 : for (tree f : ovl_range (fns))
11252 : {
11253 : /* Skip template constructors. */
11254 12602 : if (TREE_CODE (f) != FUNCTION_DECL)
11255 105 : continue;
11256 :
11257 12497 : bool cpy_or_move_ctor_p = copy_fn_p (f);
11258 :
11259 : /* Skip ctors other than default, copy, and move. */
11260 12497 : if (!cpy_or_move_ctor_p && !default_ctor_p (f))
11261 2757 : continue;
11262 :
11263 9740 : if (DECL_DELETED_FN (f))
11264 306 : continue;
11265 :
11266 9434 : bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
11267 9626 : || accessible_p (TYPE_BINFO (type), f, true));
11268 :
11269 : if (accessible)
11270 9314 : hasctor[cpy_or_move_ctor_p] = true;
11271 :
11272 9434 : if (cpy_or_move_ctor_p && (!accessible || !trivial_fn_p (f)))
11273 : all_trivial = false;
11274 :
11275 : /* Break early when both properties have been determined. */
11276 9434 : if (hasctor[0] && hasctor[1] && !all_trivial)
11277 : break;
11278 : }
11279 :
11280 5021 : return all_trivial;
11281 : }
11282 :
11283 : /* Issue a warning on a call to the built-in function FNDECL if it is
11284 : a raw memory write whose destination is not an object of (something
11285 : like) trivial or standard layout type with a non-deleted assignment
11286 : and copy ctor. Detects const correctness violations, corrupting
11287 : references, virtual table pointers, and bypassing non-trivial
11288 : assignments. */
11289 :
11290 : static void
11291 69655 : maybe_warn_class_memaccess (location_t loc, tree fndecl,
11292 : const vec<tree, va_gc> *args)
11293 : {
11294 : /* Except for bcopy where it's second, the destination pointer is
11295 : the first argument for all functions handled here. Compute
11296 : the index of the destination and source arguments. */
11297 69655 : unsigned dstidx = DECL_FUNCTION_CODE (fndecl) == BUILT_IN_BCOPY;
11298 69655 : unsigned srcidx = !dstidx;
11299 :
11300 69655 : tree dest = (*args)[dstidx];
11301 69655 : if (!TREE_TYPE (dest)
11302 69655 : || (TREE_CODE (TREE_TYPE (dest)) != ARRAY_TYPE
11303 68481 : && !INDIRECT_TYPE_P (TREE_TYPE (dest))))
11304 66280 : return;
11305 :
11306 22330 : tree srctype = NULL_TREE;
11307 :
11308 : /* Determine the type of the pointed-to object and whether it's
11309 : a complete class type. */
11310 22330 : tree desttype = TREE_TYPE (TREE_TYPE (dest));
11311 :
11312 22330 : if (!desttype || !COMPLETE_TYPE_P (desttype) || !CLASS_TYPE_P (desttype))
11313 : return;
11314 :
11315 : /* Check to see if the raw memory call is made by a non-static member
11316 : function with THIS as the destination argument for the destination
11317 : type. If so, and if the class has no non-trivial bases or members,
11318 : be more permissive. */
11319 5123 : if (current_function_decl
11320 5123 : && DECL_OBJECT_MEMBER_FUNCTION_P (current_function_decl)
11321 5390 : && is_object_parameter (tree_strip_nop_conversions (dest)))
11322 : {
11323 204 : tree ctx = DECL_CONTEXT (current_function_decl);
11324 204 : bool special = same_type_ignoring_top_level_qualifiers_p (ctx, desttype);
11325 204 : tree binfo = TYPE_BINFO (ctx);
11326 :
11327 204 : if (special
11328 204 : && !BINFO_VTABLE (binfo)
11329 378 : && !first_non_trivial_field (desttype))
11330 : return;
11331 : }
11332 :
11333 : /* True if the class is trivial. */
11334 5021 : bool trivial = trivial_type_p (desttype);
11335 :
11336 : /* Set to true if DESTYPE has an accessible copy assignment. */
11337 5021 : bool hasassign = false;
11338 : /* True if all of the class' overloaded copy assignment operators
11339 : are all trivial (and not deleted) and at least one of them is
11340 : accessible. */
11341 5021 : bool trivassign = has_trivial_copy_assign_p (desttype, true, &hasassign);
11342 :
11343 : /* Set to true if DESTTYPE has an accessible default and copy ctor,
11344 : respectively. */
11345 5021 : bool hasctors[2] = { false, false };
11346 :
11347 : /* True if all of the class' overloaded copy constructors are all
11348 : trivial (and not deleted) and at least one of them is accessible. */
11349 5021 : bool trivcopy = has_trivial_copy_p (desttype, true, hasctors);
11350 :
11351 : /* Set FLD to the first private/protected member of the class. */
11352 5021 : tree fld = trivial ? first_non_public_field (desttype) : NULL_TREE;
11353 :
11354 : /* The warning format string. */
11355 5021 : const char *warnfmt = NULL;
11356 : /* A suggested alternative to offer instead of the raw memory call.
11357 : Empty string when none can be come up with. */
11358 5021 : const char *suggest = "";
11359 5021 : bool warned = false;
11360 :
11361 5021 : auto_diagnostic_group d;
11362 5021 : switch (DECL_FUNCTION_CODE (fndecl))
11363 : {
11364 560 : case BUILT_IN_MEMSET:
11365 560 : if (!integer_zerop (maybe_constant_value ((*args)[1])))
11366 : {
11367 : /* Diagnose setting non-copy-assignable or non-trivial types,
11368 : or types with a private member, to (potentially) non-zero
11369 : bytes. Since the value of the bytes being written is unknown,
11370 : suggest using assignment instead (if one exists). Also warn
11371 : for writes into objects for which zero-initialization doesn't
11372 : mean all bits clear (pointer-to-member data, where null is all
11373 : bits set). Since the value being written is (most likely)
11374 : non-zero, simply suggest assignment (but not copy assignment). */
11375 196 : suggest = "; use assignment instead";
11376 196 : if (!trivassign)
11377 : warnfmt = G_("%qD writing to an object of type %#qT with "
11378 : "no trivial copy-assignment");
11379 125 : else if (!trivial)
11380 : warnfmt = G_("%qD writing to an object of non-trivial type %#qT%s");
11381 85 : else if (fld)
11382 : {
11383 24 : const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
11384 24 : warned = warning_at (loc, OPT_Wclass_memaccess,
11385 : "%qD writing to an object of type %#qT with "
11386 : "%qs member %qD",
11387 : fndecl, desttype, access, fld);
11388 : }
11389 61 : else if (!zero_init_p (desttype))
11390 : warnfmt = G_("%qD writing to an object of type %#qT containing "
11391 : "a pointer to data member%s");
11392 :
11393 : break;
11394 : }
11395 : /* Fall through. */
11396 :
11397 481 : case BUILT_IN_BZERO:
11398 : /* Similarly to the above, diagnose clearing non-trivial or non-
11399 : standard layout objects, or objects of types with no assignmenmt.
11400 : Since the value being written is known to be zero, suggest either
11401 : copy assignment, copy ctor, or default ctor as an alternative,
11402 : depending on what's available. */
11403 :
11404 481 : if (hasassign && hasctors[0])
11405 : suggest = G_("; use assignment or value-initialization instead");
11406 49 : else if (hasassign)
11407 : suggest = G_("; use assignment instead");
11408 31 : else if (hasctors[0])
11409 16 : suggest = G_("; use value-initialization instead");
11410 :
11411 481 : if (!trivassign)
11412 : warnfmt = G_("%qD clearing an object of type %#qT with "
11413 : "no trivial copy-assignment%s");
11414 374 : else if (!trivial)
11415 : warnfmt = G_("%qD clearing an object of non-trivial type %#qT%s");
11416 304 : else if (!zero_init_p (desttype))
11417 : warnfmt = G_("%qD clearing an object of type %#qT containing "
11418 : "a pointer-to-member%s");
11419 : break;
11420 :
11421 2437 : case BUILT_IN_BCOPY:
11422 2437 : case BUILT_IN_MEMCPY:
11423 2437 : case BUILT_IN_MEMMOVE:
11424 2437 : case BUILT_IN_MEMPCPY:
11425 : /* Determine the type of the source object. */
11426 2437 : srctype = TREE_TYPE ((*args)[srcidx]);
11427 2437 : if (!srctype || !INDIRECT_TYPE_P (srctype))
11428 0 : srctype = void_type_node;
11429 : else
11430 2437 : srctype = TREE_TYPE (srctype);
11431 :
11432 : /* Since it's impossible to determine whether the byte copy is
11433 : being used in place of assignment to an existing object or
11434 : as a substitute for initialization, assume it's the former.
11435 : Determine the best alternative to use instead depending on
11436 : what's not deleted. */
11437 2437 : if (hasassign && hasctors[1])
11438 : suggest = G_("; use copy-assignment or copy-initialization instead");
11439 488 : else if (hasassign)
11440 : suggest = G_("; use copy-assignment instead");
11441 341 : else if (hasctors[1])
11442 290 : suggest = G_("; use copy-initialization instead");
11443 :
11444 2437 : if (!trivassign)
11445 : warnfmt = G_("%qD writing to an object of type %#qT with no trivial "
11446 : "copy-assignment%s");
11447 1639 : else if (!trivially_copyable_p (desttype))
11448 : warnfmt = G_("%qD writing to an object of non-trivially copyable "
11449 : "type %#qT%s");
11450 1315 : else if (!trivcopy)
11451 : warnfmt = G_("%qD writing to an object with a deleted copy constructor");
11452 :
11453 1315 : else if (!trivial
11454 211 : && !VOID_TYPE_P (srctype)
11455 160 : && !is_byte_access_type (srctype)
11456 1412 : && !same_type_ignoring_top_level_qualifiers_p (desttype,
11457 : srctype))
11458 : {
11459 : /* Warn when copying into a non-trivial object from an object
11460 : of a different type other than void or char. */
11461 54 : warned = warning_at (loc, OPT_Wclass_memaccess,
11462 : "%qD copying an object of non-trivial type "
11463 : "%#qT from an array of %#qT",
11464 : fndecl, desttype, srctype);
11465 : }
11466 1261 : else if (fld
11467 432 : && !VOID_TYPE_P (srctype)
11468 384 : && !is_byte_access_type (srctype)
11469 1549 : && !same_type_ignoring_top_level_qualifiers_p (desttype,
11470 : srctype))
11471 : {
11472 216 : const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
11473 216 : warned = warning_at (loc, OPT_Wclass_memaccess,
11474 : "%qD copying an object of type %#qT with "
11475 : "%qs member %qD from an array of %#qT; use "
11476 : "assignment or copy-initialization instead",
11477 : fndecl, desttype, access, fld, srctype);
11478 : }
11479 1045 : else if (!trivial && vec_safe_length (args) > 2)
11480 : {
11481 157 : tree sz = maybe_constant_value ((*args)[2]);
11482 157 : if (!tree_fits_uhwi_p (sz))
11483 : break;
11484 :
11485 : /* Finally, warn on partial copies. */
11486 99 : unsigned HOST_WIDE_INT typesize
11487 99 : = tree_to_uhwi (TYPE_SIZE_UNIT (desttype));
11488 99 : if (typesize == 0)
11489 : break;
11490 96 : if (unsigned HOST_WIDE_INT partial = tree_to_uhwi (sz) % typesize)
11491 12 : warned = warning_at (loc, OPT_Wclass_memaccess,
11492 : (typesize - partial > 1
11493 : ? G_("%qD writing to an object of "
11494 : "a non-trivial type %#qT leaves %wu "
11495 : "bytes unchanged")
11496 : : G_("%qD writing to an object of "
11497 : "a non-trivial type %#qT leaves %wu "
11498 : "byte unchanged")),
11499 : fndecl, desttype, typesize - partial);
11500 : }
11501 : break;
11502 :
11503 261 : case BUILT_IN_REALLOC:
11504 :
11505 261 : if (!trivially_copyable_p (desttype))
11506 : warnfmt = G_("%qD moving an object of non-trivially copyable type "
11507 : "%#qT; use %<new%> and %<delete%> instead");
11508 138 : else if (!trivcopy)
11509 : warnfmt = G_("%qD moving an object of type %#qT with deleted copy "
11510 : "constructor; use %<new%> and %<delete%> instead");
11511 135 : else if (!get_dtor (desttype, tf_none))
11512 : warnfmt = G_("%qD moving an object of type %#qT with deleted "
11513 : "destructor");
11514 126 : else if (!trivial)
11515 : {
11516 33 : tree sz = maybe_constant_value ((*args)[1]);
11517 33 : if (TREE_CODE (sz) == INTEGER_CST
11518 33 : && tree_int_cst_lt (sz, TYPE_SIZE_UNIT (desttype)))
11519 : /* Finally, warn on reallocation into insufficient space. */
11520 7 : warned = warning_at (loc, OPT_Wclass_memaccess,
11521 : "%qD moving an object of non-trivial type "
11522 : "%#qT and size %E into a region of size %E",
11523 7 : fndecl, desttype, TYPE_SIZE_UNIT (desttype),
11524 : sz);
11525 : }
11526 : break;
11527 :
11528 1646 : default:
11529 1646 : return;
11530 : }
11531 :
11532 310 : if (warnfmt)
11533 : {
11534 1569 : if (suggest)
11535 1569 : warned = warning_at (loc, OPT_Wclass_memaccess,
11536 : warnfmt, fndecl, desttype, suggest);
11537 : else
11538 : warned = warning_at (loc, OPT_Wclass_memaccess,
11539 : warnfmt, fndecl, desttype);
11540 : }
11541 :
11542 3375 : if (warned)
11543 1879 : inform (location_of (desttype), "%#qT declared here", desttype);
11544 5021 : }
11545 :
11546 : /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
11547 : If FN is the result of resolving an overloaded target built-in,
11548 : ORIG_FNDECL is the original function decl, otherwise it is null.
11549 : This function performs no overload resolution, conversion, or other
11550 : high-level operations. */
11551 :
11552 : tree
11553 170684073 : build_cxx_call (tree fn, int nargs, tree *argarray,
11554 : tsubst_flags_t complain, tree orig_fndecl)
11555 : {
11556 170684073 : tree fndecl;
11557 :
11558 : /* Remember roughly where this call is. */
11559 170684073 : location_t loc = cp_expr_loc_or_input_loc (fn);
11560 170684073 : fn = build_call_a (fn, nargs, argarray);
11561 170684073 : SET_EXPR_LOCATION (fn, loc);
11562 :
11563 170684073 : fndecl = get_callee_fndecl (fn);
11564 170684073 : if (!orig_fndecl)
11565 170684073 : orig_fndecl = fndecl;
11566 :
11567 : /* Check that arguments to builtin functions match the expectations. */
11568 170684073 : if (fndecl
11569 164023817 : && !processing_template_decl
11570 334653209 : && fndecl_built_in_p (fndecl))
11571 : {
11572 : int i;
11573 :
11574 : /* We need to take care that values to BUILT_IN_NORMAL
11575 : are reduced. */
11576 22510556 : for (i = 0; i < nargs; i++)
11577 14745745 : argarray[i] = maybe_constant_value (argarray[i]);
11578 :
11579 7764811 : if (!check_builtin_function_arguments (EXPR_LOCATION (fn), vNULL, fndecl,
11580 : orig_fndecl, nargs, argarray,
11581 : complain & tf_error))
11582 927 : return error_mark_node;
11583 7763884 : else if (fndecl_built_in_p (fndecl, BUILT_IN_CLEAR_PADDING))
11584 : {
11585 14503 : tree arg0 = argarray[0];
11586 14503 : STRIP_NOPS (arg0);
11587 14503 : if (TREE_CODE (arg0) == ADDR_EXPR
11588 264 : && DECL_P (TREE_OPERAND (arg0, 0))
11589 14746 : && same_type_ignoring_top_level_qualifiers_p
11590 243 : (TREE_TYPE (TREE_TYPE (argarray[0])),
11591 243 : TREE_TYPE (TREE_TYPE (arg0))))
11592 : /* For __builtin_clear_padding (&var) we know the type
11593 : is for a complete object, so there is no risk in clearing
11594 : padding that is reused in some derived class member. */;
11595 14267 : else if (!trivially_copyable_p (TREE_TYPE (TREE_TYPE (argarray[0]))))
11596 : {
11597 18 : error_at (EXPR_LOC_OR_LOC (argarray[0], input_location),
11598 : "argument %u in call to function %qE "
11599 : "has pointer to a non-trivially-copyable type (%qT)",
11600 18 : 1, fndecl, TREE_TYPE (argarray[0]));
11601 18 : return error_mark_node;
11602 : }
11603 : }
11604 : }
11605 :
11606 170683128 : if (VOID_TYPE_P (TREE_TYPE (fn)))
11607 37779376 : return maybe_contract_wrap_call (fndecl, fn);
11608 :
11609 : /* 5.2.2/11: If a function call is a prvalue of object type: if the
11610 : function call is either the operand of a decltype-specifier or the
11611 : right operand of a comma operator that is the operand of a
11612 : decltype-specifier, a temporary object is not introduced for the
11613 : prvalue. The type of the prvalue may be incomplete. */
11614 132903752 : if (!(complain & tf_decltype))
11615 : {
11616 109916528 : fn = require_complete_type (fn, complain);
11617 109916528 : if (fn == error_mark_node)
11618 : return error_mark_node;
11619 109916502 : fn = maybe_contract_wrap_call (fndecl, fn);
11620 :
11621 109916502 : if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
11622 : {
11623 12477552 : fn = build_cplus_new (TREE_TYPE (fn), fn, complain);
11624 12477552 : maybe_warn_parm_abi (TREE_TYPE (fn), loc);
11625 : }
11626 : }
11627 : else
11628 22987224 : fn = maybe_contract_wrap_call (fndecl, fn);
11629 132903726 : return convert_from_reference (fn);
11630 : }
11631 :
11632 : /* Returns the value to use for the in-charge parameter when making a
11633 : call to a function with the indicated NAME.
11634 :
11635 : FIXME:Can't we find a neater way to do this mapping? */
11636 :
11637 : tree
11638 36938 : in_charge_arg_for_name (tree name)
11639 : {
11640 36938 : if (IDENTIFIER_CTOR_P (name))
11641 : {
11642 21150 : if (name == complete_ctor_identifier)
11643 10575 : return integer_one_node;
11644 10575 : gcc_checking_assert (name == base_ctor_identifier);
11645 : }
11646 : else
11647 : {
11648 15788 : if (name == complete_dtor_identifier)
11649 7894 : return integer_two_node;
11650 7894 : else if (name == deleting_dtor_identifier)
11651 : /* The deleting dtor should now be handled by
11652 : build_delete_destructor_body. */
11653 0 : gcc_unreachable ();
11654 7894 : gcc_checking_assert (name == base_dtor_identifier);
11655 : }
11656 :
11657 18469 : return integer_zero_node;
11658 : }
11659 :
11660 : /* We've built up a constructor call RET. Complain if it delegates to the
11661 : constructor we're currently compiling. */
11662 :
11663 : static void
11664 365200 : check_self_delegation (tree ret)
11665 : {
11666 365200 : if (TREE_CODE (ret) == TARGET_EXPR)
11667 0 : ret = TARGET_EXPR_INITIAL (ret);
11668 365200 : tree fn = cp_get_callee_fndecl_nofold (ret);
11669 365200 : if (fn && DECL_ABSTRACT_ORIGIN (fn) == current_function_decl)
11670 46 : error ("constructor delegates to itself");
11671 365200 : }
11672 :
11673 : /* Build a call to a constructor, destructor, or an assignment
11674 : operator for INSTANCE, an expression with class type. NAME
11675 : indicates the special member function to call; *ARGS are the
11676 : arguments. ARGS may be NULL. This may change ARGS. BINFO
11677 : indicates the base of INSTANCE that is to be passed as the `this'
11678 : parameter to the member function called.
11679 :
11680 : FLAGS are the LOOKUP_* flags to use when processing the call.
11681 :
11682 : If NAME indicates a complete object constructor, INSTANCE may be
11683 : NULL_TREE. In this case, the caller will call build_cplus_new to
11684 : store the newly constructed object into a VAR_DECL. */
11685 :
11686 : tree
11687 40112118 : build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
11688 : tree binfo, int flags, tsubst_flags_t complain)
11689 : {
11690 40112118 : tree fns;
11691 : /* The type of the subobject to be constructed or destroyed. */
11692 40112118 : tree class_type;
11693 40112118 : vec<tree, va_gc> *allocated = NULL;
11694 40112118 : tree ret;
11695 :
11696 40112118 : gcc_assert (IDENTIFIER_CDTOR_P (name) || name == assign_op_identifier);
11697 :
11698 40112118 : if (error_operand_p (instance))
11699 0 : return error_mark_node;
11700 :
11701 40112118 : if (IDENTIFIER_DTOR_P (name))
11702 : {
11703 10927467 : gcc_assert (args == NULL || vec_safe_is_empty (*args));
11704 10927467 : if (!type_build_dtor_call (TREE_TYPE (instance)))
11705 : /* Shortcut to avoid lazy destructor declaration. */
11706 1485 : return build_trivial_dtor_call (instance);
11707 : }
11708 :
11709 40110633 : if (TYPE_P (binfo))
11710 : {
11711 : /* Resolve the name. */
11712 32057511 : if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
11713 6 : return error_mark_node;
11714 :
11715 32057505 : binfo = TYPE_BINFO (binfo);
11716 : }
11717 :
11718 32057505 : gcc_assert (binfo != NULL_TREE);
11719 :
11720 40110627 : class_type = BINFO_TYPE (binfo);
11721 :
11722 : /* Handle the special case where INSTANCE is NULL_TREE. */
11723 40110627 : if (name == complete_ctor_identifier && !instance)
11724 21272538 : instance = build_dummy_object (class_type);
11725 : else
11726 : {
11727 : /* Convert to the base class, if necessary. */
11728 18838089 : if (!same_type_ignoring_top_level_qualifiers_p
11729 18838089 : (TREE_TYPE (instance), BINFO_TYPE (binfo)))
11730 : {
11731 1659237 : if (IDENTIFIER_CDTOR_P (name))
11732 : /* For constructors and destructors, either the base is
11733 : non-virtual, or it is virtual but we are doing the
11734 : conversion from a constructor or destructor for the
11735 : complete object. In either case, we can convert
11736 : statically. */
11737 1639315 : instance = convert_to_base_statically (instance, binfo);
11738 : else
11739 : {
11740 : /* However, for assignment operators, we must convert
11741 : dynamically if the base is virtual. */
11742 19922 : gcc_checking_assert (name == assign_op_identifier);
11743 19922 : instance = build_base_path (PLUS_EXPR, instance,
11744 : binfo, /*nonnull=*/1, complain);
11745 : }
11746 : }
11747 : }
11748 :
11749 40110627 : gcc_assert (instance != NULL_TREE);
11750 :
11751 : /* In C++17, "If the initializer expression is a prvalue and the
11752 : cv-unqualified version of the source type is the same class as the class
11753 : of the destination, the initializer expression is used to initialize the
11754 : destination object." Handle that here to avoid doing overload
11755 : resolution. */
11756 40110627 : if (cxx_dialect >= cxx17
11757 39911246 : && args && vec_safe_length (*args) == 1
11758 64050002 : && !unsafe_return_slot_p (instance))
11759 : {
11760 22634862 : tree arg = (**args)[0];
11761 :
11762 1723698 : if (BRACE_ENCLOSED_INITIALIZER_P (arg)
11763 1723306 : && !TYPE_HAS_LIST_CTOR (class_type)
11764 1645932 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (arg)
11765 24280777 : && CONSTRUCTOR_NELTS (arg) == 1)
11766 1165341 : arg = CONSTRUCTOR_ELT (arg, 0)->value;
11767 :
11768 22634862 : if ((TREE_CODE (arg) == TARGET_EXPR
11769 10967669 : || TREE_CODE (arg) == CONSTRUCTOR)
11770 34860486 : && (same_type_ignoring_top_level_qualifiers_p
11771 12225624 : (class_type, TREE_TYPE (arg))))
11772 : {
11773 11042397 : if (is_dummy_object (instance))
11774 : return arg;
11775 233293 : else if (TREE_CODE (arg) == TARGET_EXPR)
11776 233293 : TARGET_EXPR_DIRECT_INIT_P (arg) = true;
11777 :
11778 233293 : if ((complain & tf_error)
11779 233291 : && (flags & LOOKUP_DELEGATING_CONS))
11780 0 : check_self_delegation (arg);
11781 : /* Avoid change of behavior on Wunused-var-2.C. */
11782 233293 : instance = mark_lvalue_use (instance);
11783 233293 : return cp_build_init_expr (instance, arg);
11784 : }
11785 : }
11786 :
11787 29068230 : fns = lookup_fnfields (binfo, name, 1, complain);
11788 :
11789 : /* When making a call to a constructor or destructor for a subobject
11790 : that uses virtual base classes, pass down a pointer to a VTT for
11791 : the subobject. */
11792 29068230 : if ((name == base_ctor_identifier
11793 26661354 : || name == base_dtor_identifier)
11794 30707625 : && CLASSTYPE_VBASECLASSES (class_type))
11795 : {
11796 48279 : tree vtt;
11797 48279 : tree sub_vtt;
11798 :
11799 : /* If the current function is a complete object constructor
11800 : or destructor, then we fetch the VTT directly.
11801 : Otherwise, we look it up using the VTT we were given. */
11802 48279 : vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
11803 48279 : vtt = decay_conversion (vtt, complain);
11804 48279 : if (vtt == error_mark_node)
11805 0 : return error_mark_node;
11806 48279 : vtt = build_if_in_charge (vtt, current_vtt_parm);
11807 48279 : if (BINFO_SUBVTT_INDEX (binfo))
11808 48099 : sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
11809 : else
11810 : sub_vtt = vtt;
11811 :
11812 48279 : if (args == NULL)
11813 : {
11814 30649 : allocated = make_tree_vector ();
11815 30649 : args = &allocated;
11816 : }
11817 :
11818 48279 : vec_safe_insert (*args, 0, sub_vtt);
11819 : }
11820 :
11821 58136460 : ret = build_new_method_call (instance, fns, args,
11822 29068230 : TYPE_BINFO (BINFO_TYPE (binfo)),
11823 : flags, /*fn=*/NULL,
11824 : complain);
11825 :
11826 29068230 : if (allocated != NULL)
11827 30649 : release_tree_vector (allocated);
11828 :
11829 29068230 : if ((complain & tf_error)
11830 25880523 : && (flags & LOOKUP_DELEGATING_CONS)
11831 365300 : && name == complete_ctor_identifier)
11832 365200 : check_self_delegation (ret);
11833 :
11834 : return ret;
11835 : }
11836 :
11837 : /* Return the NAME, as a C string. The NAME indicates a function that
11838 : is a member of TYPE. *FREE_P is set to true if the caller must
11839 : free the memory returned.
11840 :
11841 : Rather than go through all of this, we should simply set the names
11842 : of constructors and destructors appropriately, and dispense with
11843 : ctor_identifier, dtor_identifier, etc. */
11844 :
11845 : static char *
11846 93 : name_as_c_string (tree name, tree type, bool *free_p)
11847 : {
11848 93 : const char *pretty_name;
11849 :
11850 : /* Assume that we will not allocate memory. */
11851 93 : *free_p = false;
11852 : /* Constructors and destructors are special. */
11853 93 : if (IDENTIFIER_CDTOR_P (name))
11854 : {
11855 42 : pretty_name
11856 42 : = identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type)));
11857 : /* For a destructor, add the '~'. */
11858 42 : if (IDENTIFIER_DTOR_P (name))
11859 : {
11860 0 : pretty_name = concat ("~", pretty_name, NULL);
11861 : /* Remember that we need to free the memory allocated. */
11862 0 : *free_p = true;
11863 : }
11864 : }
11865 51 : else if (IDENTIFIER_CONV_OP_P (name))
11866 : {
11867 0 : pretty_name = concat ("operator ",
11868 0 : type_as_string_translate (TREE_TYPE (name),
11869 : TFF_PLAIN_IDENTIFIER),
11870 : NULL);
11871 : /* Remember that we need to free the memory allocated. */
11872 0 : *free_p = true;
11873 : }
11874 : else
11875 51 : pretty_name = identifier_to_locale (IDENTIFIER_POINTER (name));
11876 :
11877 93 : return const_cast<char *> (pretty_name);
11878 : }
11879 :
11880 : /* If CANDIDATES contains exactly one candidate, return it, otherwise
11881 : return NULL. */
11882 :
11883 : static z_candidate *
11884 683 : single_z_candidate (z_candidate *candidates)
11885 : {
11886 0 : if (candidates == NULL)
11887 : return NULL;
11888 :
11889 668 : if (candidates->next)
11890 0 : return NULL;
11891 :
11892 : return candidates;
11893 : }
11894 :
11895 : /* If CANDIDATE is invalid due to a bad argument type, return the
11896 : pertinent conversion_info.
11897 :
11898 : Otherwise, return NULL. */
11899 :
11900 : static const conversion_info *
11901 339 : maybe_get_bad_conversion_for_unmatched_call (const z_candidate *candidate)
11902 : {
11903 : /* Must be an rr_arg_conversion or rr_bad_arg_conversion. */
11904 339 : rejection_reason *r = candidate->reason;
11905 :
11906 339 : if (r == NULL)
11907 : return NULL;
11908 :
11909 339 : switch (r->code)
11910 : {
11911 : default:
11912 : return NULL;
11913 :
11914 52 : case rr_arg_conversion:
11915 52 : return &r->u.conversion;
11916 :
11917 6 : case rr_bad_arg_conversion:
11918 6 : return &r->u.bad_conversion;
11919 : }
11920 : }
11921 :
11922 : /* Issue an error and note complaining about a bad argument type at a
11923 : callsite with a single candidate FNDECL.
11924 :
11925 : ARG_LOC is the location of the argument (or UNKNOWN_LOCATION, in which
11926 : case input_location is used).
11927 : FROM_TYPE is the type of the actual argument; TO_TYPE is the type of
11928 : the formal parameter. */
11929 :
11930 : void
11931 150 : complain_about_bad_argument (location_t arg_loc,
11932 : tree from_type, tree to_type,
11933 : tree fndecl, int parmnum)
11934 : {
11935 150 : auto_diagnostic_group d;
11936 150 : range_label_for_type_mismatch rhs_label (from_type, to_type);
11937 150 : range_label *label = &rhs_label;
11938 150 : if (arg_loc == UNKNOWN_LOCATION)
11939 : {
11940 6 : arg_loc = input_location;
11941 6 : label = NULL;
11942 : }
11943 150 : gcc_rich_location richloc (arg_loc, label, highlight_colors::percent_h);
11944 150 : error_at (&richloc,
11945 : "cannot convert %qH to %qI",
11946 : from_type, to_type);
11947 150 : maybe_inform_about_fndecl_for_bogus_argument_init
11948 150 : (fndecl,
11949 : parmnum,
11950 : highlight_colors::percent_i);
11951 150 : }
11952 :
11953 : /* Subroutine of build_new_method_call_1, for where there are no viable
11954 : candidates for the call. */
11955 :
11956 : static void
11957 689 : complain_about_no_candidates_for_method_call (tree instance,
11958 : z_candidate *candidates,
11959 : tree explicit_targs,
11960 : tree basetype,
11961 : tree optype, tree name,
11962 : bool skip_first_for_error,
11963 : vec<tree, va_gc> *user_args)
11964 : {
11965 689 : auto_diagnostic_group d;
11966 689 : if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
11967 0 : cxx_incomplete_type_error (instance, basetype);
11968 689 : else if (optype)
11969 6 : error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
11970 : basetype, optype, build_tree_list_vec (user_args),
11971 6 : TREE_TYPE (instance));
11972 : else
11973 : {
11974 : /* Special-case for when there's a single candidate that's failing
11975 : due to a bad argument type. */
11976 683 : if (z_candidate *candidate = single_z_candidate (candidates))
11977 339 : if (const conversion_info *conv
11978 339 : = maybe_get_bad_conversion_for_unmatched_call (candidate))
11979 : {
11980 58 : tree from_type = conv->from;
11981 58 : if (!TYPE_P (conv->from))
11982 6 : from_type = lvalue_type (conv->from);
11983 58 : complain_about_bad_argument (conv->loc,
11984 58 : from_type, conv->to_type,
11985 58 : candidate->fn, conv->n_arg);
11986 58 : return;
11987 : }
11988 :
11989 625 : tree arglist = build_tree_list_vec (user_args);
11990 625 : tree errname = name;
11991 625 : bool twiddle = false;
11992 625 : if (IDENTIFIER_CDTOR_P (errname))
11993 : {
11994 322 : twiddle = IDENTIFIER_DTOR_P (errname);
11995 322 : errname = constructor_name (basetype);
11996 : }
11997 625 : if (explicit_targs)
11998 48 : errname = lookup_template_function (errname, explicit_targs);
11999 625 : if (skip_first_for_error)
12000 3 : arglist = TREE_CHAIN (arglist);
12001 1250 : error ("no matching function for call to %<%T::%s%E(%A)%#V%>",
12002 625 : basetype, &"~"[!twiddle], errname, arglist,
12003 625 : TREE_TYPE (instance));
12004 : }
12005 631 : print_z_candidates (location_of (name), candidates);
12006 689 : }
12007 :
12008 : /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
12009 : be set, upon return, to the function called. ARGS may be NULL.
12010 : This may change ARGS. */
12011 :
12012 : tree
12013 101347775 : build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args,
12014 : tree conversion_path, int flags,
12015 : tree *fn_p, tsubst_flags_t complain)
12016 : {
12017 101347775 : struct z_candidate *candidates = 0, *cand;
12018 101347775 : tree explicit_targs = NULL_TREE;
12019 101347775 : tree basetype = NULL_TREE;
12020 101347775 : tree access_binfo;
12021 101347775 : tree optype;
12022 101347775 : tree first_mem_arg = NULL_TREE;
12023 101347775 : tree name;
12024 101347775 : bool skip_first_for_error;
12025 101347775 : vec<tree, va_gc> *user_args;
12026 101347775 : tree call;
12027 101347775 : tree fn;
12028 101347775 : int template_only = 0;
12029 101347775 : bool any_viable_p;
12030 101347775 : tree orig_instance;
12031 101347775 : tree orig_fns;
12032 101347775 : vec<tree, va_gc> *orig_args = NULL;
12033 :
12034 101347775 : auto_cond_timevar tv (TV_OVERLOAD);
12035 :
12036 101347775 : gcc_assert (instance != NULL_TREE);
12037 :
12038 : /* We don't know what function we're going to call, yet. */
12039 101347775 : if (fn_p)
12040 28462224 : *fn_p = NULL_TREE;
12041 :
12042 101347775 : if (error_operand_p (instance)
12043 101347775 : || !fns || error_operand_p (fns))
12044 1466599 : return error_mark_node;
12045 :
12046 99881176 : if (!BASELINK_P (fns))
12047 : {
12048 0 : if (complain & tf_error)
12049 0 : error ("call to non-function %qD", fns);
12050 0 : return error_mark_node;
12051 : }
12052 :
12053 99881176 : orig_instance = instance;
12054 99881176 : orig_fns = fns;
12055 :
12056 : /* Dismantle the baselink to collect all the information we need. */
12057 99881176 : if (!conversion_path)
12058 43831474 : conversion_path = BASELINK_BINFO (fns);
12059 99881176 : access_binfo = BASELINK_ACCESS_BINFO (fns);
12060 99881176 : optype = BASELINK_OPTYPE (fns);
12061 99881176 : fns = BASELINK_FUNCTIONS (fns);
12062 99881176 : if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12063 : {
12064 2741397 : explicit_targs = TREE_OPERAND (fns, 1);
12065 2741397 : fns = TREE_OPERAND (fns, 0);
12066 2741397 : template_only = 1;
12067 : }
12068 99881176 : gcc_assert (OVL_P (fns));
12069 99881176 : fn = OVL_FIRST (fns);
12070 99881176 : name = DECL_NAME (fn);
12071 :
12072 99881176 : basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
12073 99881176 : gcc_assert (CLASS_TYPE_P (basetype));
12074 :
12075 99881176 : user_args = args == NULL ? NULL : *args;
12076 : /* Under DR 147 A::A() is an invalid constructor call,
12077 : not a functional cast. */
12078 99881176 : if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
12079 : {
12080 54 : if (! (complain & tf_error))
12081 0 : return error_mark_node;
12082 :
12083 54 : basetype = DECL_CONTEXT (fn);
12084 54 : name = constructor_name (basetype);
12085 54 : auto_diagnostic_group d;
12086 54 : if (permerror (input_location,
12087 : "cannot call constructor %<%T::%D%> directly",
12088 : basetype, name))
12089 54 : inform (input_location, "for a function-style cast, remove the "
12090 : "redundant %<::%D%>", name);
12091 54 : call = build_functional_cast (input_location, basetype,
12092 : build_tree_list_vec (user_args),
12093 : complain);
12094 54 : return call;
12095 54 : }
12096 :
12097 99881122 : if (processing_template_decl)
12098 7179642 : orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
12099 :
12100 : /* Process the argument list. */
12101 99795907 : if (args != NULL && *args != NULL)
12102 : {
12103 85071395 : *args = resolve_args (*args, complain);
12104 85071395 : if (*args == NULL)
12105 189 : return error_mark_node;
12106 : user_args = *args;
12107 : }
12108 :
12109 : /* Consider the object argument to be used even if we end up selecting a
12110 : static member function. */
12111 99880933 : instance = mark_type_use (instance);
12112 :
12113 : /* Figure out whether to skip the first argument for the error
12114 : message we will display to users if an error occurs. We don't
12115 : want to display any compiler-generated arguments. The "this"
12116 : pointer hasn't been added yet. However, we must remove the VTT
12117 : pointer if this is a call to a base-class constructor or
12118 : destructor. */
12119 99880933 : skip_first_for_error = false;
12120 99880933 : if (IDENTIFIER_CDTOR_P (name))
12121 : {
12122 : /* Callers should explicitly indicate whether they want to ctor
12123 : the complete object or just the part without virtual bases. */
12124 53284562 : gcc_assert (name != ctor_identifier);
12125 :
12126 : /* Remove the VTT pointer, if present. */
12127 50877692 : if ((name == base_ctor_identifier || name == base_dtor_identifier)
12128 54923957 : && CLASSTYPE_VBASECLASSES (basetype))
12129 : skip_first_for_error = true;
12130 :
12131 : /* It's OK to call destructors and constructors on cv-qualified
12132 : objects. Therefore, convert the INSTANCE to the unqualified
12133 : type, if necessary. */
12134 53284562 : if (!same_type_p (basetype, TREE_TYPE (instance)))
12135 : {
12136 703948 : instance = build_this (instance);
12137 703948 : instance = build_nop (build_pointer_type (basetype), instance);
12138 703948 : instance = build_fold_indirect_ref (instance);
12139 : }
12140 : }
12141 : else
12142 93192742 : gcc_assert (!DECL_DESTRUCTOR_P (fn) && !DECL_CONSTRUCTOR_P (fn));
12143 :
12144 : /* For the overload resolution we need to find the actual `this`
12145 : that would be captured if the call turns out to be to a
12146 : non-static member function. Do not actually capture it at this
12147 : point. */
12148 199761866 : if (DECL_CONSTRUCTOR_P (fn))
12149 : /* Constructors don't use the enclosing 'this'. */
12150 : first_mem_arg = instance;
12151 : else
12152 73158920 : first_mem_arg = maybe_resolve_dummy (instance, false);
12153 :
12154 99880933 : conversion_obstack_sentinel cos;
12155 :
12156 : /* The number of arguments artificial parms in ARGS; we subtract one because
12157 : there's no 'this' in ARGS. */
12158 99880933 : unsigned skip = num_artificial_parms_for (fn) - 1;
12159 :
12160 : /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
12161 : initializer, not T({ }). */
12162 99880933 : if (DECL_CONSTRUCTOR_P (fn)
12163 22828395 : && vec_safe_length (user_args) > skip
12164 120437512 : && DIRECT_LIST_INIT_P ((*user_args)[skip]))
12165 : {
12166 1725267 : tree init_list = (*user_args)[skip];
12167 1725267 : tree init = NULL_TREE;
12168 :
12169 1725267 : gcc_assert (user_args->length () == skip + 1
12170 : && !(flags & LOOKUP_ONLYCONVERTING));
12171 :
12172 : /* If the initializer list has no elements and T is a class type with
12173 : a default constructor, the object is value-initialized. Handle
12174 : this here so we don't need to handle it wherever we use
12175 : build_special_member_call. */
12176 1725267 : if (CONSTRUCTOR_NELTS (init_list) == 0
12177 195422 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
12178 : /* For a user-provided default constructor, use the normal
12179 : mechanisms so that protected access works. */
12180 195422 : && type_has_non_user_provided_default_constructor (basetype)
12181 1652423 : && !processing_template_decl)
12182 122575 : init = build_value_init (basetype, complain);
12183 :
12184 : /* If BASETYPE is an aggregate, we need to do aggregate
12185 : initialization. */
12186 1602692 : else if (CP_AGGREGATE_TYPE_P (basetype))
12187 : {
12188 41 : init = reshape_init (basetype, init_list, complain);
12189 41 : init = digest_init (basetype, init, complain);
12190 : }
12191 :
12192 122616 : if (init)
12193 : {
12194 122616 : if (is_dummy_object (instance))
12195 36030 : return get_target_expr (init, complain);
12196 86586 : return cp_build_init_expr (instance, init);
12197 : }
12198 :
12199 : /* Otherwise go ahead with overload resolution. */
12200 1602651 : add_list_candidates (fns, first_mem_arg, user_args,
12201 : basetype, explicit_targs, template_only,
12202 : conversion_path, access_binfo, flags,
12203 : &candidates, complain);
12204 : }
12205 : else
12206 98155666 : add_candidates (fns, first_mem_arg, user_args, optype,
12207 : explicit_targs, template_only, conversion_path,
12208 : access_binfo, flags, &candidates, complain);
12209 :
12210 99758317 : any_viable_p = false;
12211 99758317 : candidates = splice_viable (candidates, false, &any_viable_p);
12212 :
12213 99758317 : if (!any_viable_p)
12214 : {
12215 : /* [dcl.init], 17.6.2.2:
12216 :
12217 : Otherwise, if no constructor is viable, the destination type is
12218 : a (possibly cv-qualified) aggregate class A, and the initializer
12219 : is a parenthesized expression-list, the object is initialized as
12220 : follows...
12221 :
12222 : We achieve this by building up a CONSTRUCTOR, as for list-init,
12223 : and setting CONSTRUCTOR_IS_PAREN_INIT to distinguish between
12224 : the two. */
12225 26966 : if (DECL_CONSTRUCTOR_P (fn)
12226 20455 : && !(flags & LOOKUP_ONLYCONVERTING)
12227 20431 : && cxx_dialect >= cxx20
12228 19814 : && CP_AGGREGATE_TYPE_P (basetype)
12229 26966 : && !vec_safe_is_empty (user_args))
12230 : {
12231 : /* Create a CONSTRUCTOR from ARGS, e.g. {1, 2} from <1, 2>. */
12232 1171 : tree ctor = build_constructor_from_vec (init_list_type_node,
12233 : user_args);
12234 1171 : CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true;
12235 1171 : CONSTRUCTOR_IS_PAREN_INIT (ctor) = true;
12236 1171 : if (is_dummy_object (instance))
12237 : return ctor;
12238 : else
12239 : {
12240 677 : ctor = digest_init (basetype, ctor, complain);
12241 677 : if (ctor == error_mark_node)
12242 : return error_mark_node;
12243 457 : return cp_build_init_expr (instance, ctor);
12244 : }
12245 : }
12246 25795 : if (complain & tf_error)
12247 689 : complain_about_no_candidates_for_method_call (instance, candidates,
12248 : explicit_targs, basetype,
12249 : optype, name,
12250 : skip_first_for_error,
12251 : user_args);
12252 25795 : call = error_mark_node;
12253 : }
12254 : else
12255 : {
12256 99731351 : cand = tourney (candidates, complain);
12257 99731351 : if (cand == 0)
12258 : {
12259 193 : char *pretty_name;
12260 193 : bool free_p;
12261 193 : tree arglist;
12262 :
12263 193 : if (complain & tf_error)
12264 : {
12265 93 : pretty_name = name_as_c_string (name, basetype, &free_p);
12266 93 : arglist = build_tree_list_vec (user_args);
12267 93 : if (skip_first_for_error)
12268 0 : arglist = TREE_CHAIN (arglist);
12269 93 : auto_diagnostic_group d;
12270 186 : if (!any_strictly_viable (candidates))
12271 13 : error ("no matching function for call to %<%s(%A)%>",
12272 : pretty_name, arglist);
12273 : else
12274 80 : error ("call of overloaded %<%s(%A)%> is ambiguous",
12275 : pretty_name, arglist);
12276 93 : print_z_candidates (location_of (name), candidates);
12277 93 : if (free_p)
12278 0 : free (pretty_name);
12279 93 : }
12280 193 : call = error_mark_node;
12281 193 : if (fn_p)
12282 84 : *fn_p = error_mark_node;
12283 : }
12284 : else
12285 : {
12286 99731158 : fn = cand->fn;
12287 99731158 : call = NULL_TREE;
12288 :
12289 99731158 : if (!(flags & LOOKUP_NONVIRTUAL)
12290 76499519 : && DECL_PURE_VIRTUAL_P (fn)
12291 209949 : && instance == current_class_ref
12292 99895970 : && (complain & tf_warning))
12293 : {
12294 : /* This is not an error, it is runtime undefined
12295 : behavior. */
12296 164812 : if (!current_function_decl)
12297 3 : warning (0, "pure virtual %q#D called from "
12298 : "non-static data member initializer", fn);
12299 164809 : else if (DECL_CONSTRUCTOR_P (current_function_decl)
12300 164809 : || DECL_DESTRUCTOR_P (current_function_decl))
12301 9 : warning (0, (DECL_CONSTRUCTOR_P (current_function_decl)
12302 : ? G_("pure virtual %q#D called from constructor")
12303 : : G_("pure virtual %q#D called from destructor")),
12304 : fn);
12305 : }
12306 :
12307 114512050 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fn)
12308 169903760 : && !DECL_CONSTRUCTOR_P (fn)
12309 158104236 : && is_dummy_object (instance))
12310 : {
12311 52536 : instance = maybe_resolve_dummy (instance, true);
12312 52536 : if (instance == error_mark_node)
12313 : call = error_mark_node;
12314 52536 : else if (!is_dummy_object (instance))
12315 : {
12316 : /* We captured 'this' in the current lambda now that
12317 : we know we really need it. */
12318 52034 : cand->first_arg = instance;
12319 : }
12320 502 : else if (current_class_ptr && any_dependent_bases_p ())
12321 : /* We can't tell until instantiation time whether we can use
12322 : *this as the implicit object argument. */;
12323 : else
12324 : {
12325 463 : if (complain & tf_error)
12326 69 : error ("cannot call member function %qD without object",
12327 : fn);
12328 463 : call = error_mark_node;
12329 : }
12330 : }
12331 :
12332 99731158 : if (call != error_mark_node)
12333 : {
12334 : /* Now we know what function is being called. */
12335 99730695 : if (fn_p)
12336 26981570 : *fn_p = fn;
12337 : /* Build the actual CALL_EXPR. */
12338 99730695 : call = build_over_call (cand, flags, complain);
12339 :
12340 : /* Suppress warnings for if (my_struct.operator= (x)) where
12341 : my_struct is implicitly converted to bool. */
12342 99730695 : if (TREE_CODE (call) == MODIFY_EXPR)
12343 3184802 : suppress_warning (call, OPT_Wparentheses);
12344 :
12345 : /* In an expression of the form `a->f()' where `f' turns
12346 : out to be a static member function, `a' is
12347 : none-the-less evaluated. */
12348 99730695 : if (!is_dummy_object (instance))
12349 77224886 : call = keep_unused_object_arg (call, instance, fn);
12350 99730695 : if (call != error_mark_node
12351 197454522 : && DECL_DESTRUCTOR_P (cand->fn)
12352 126292295 : && !VOID_TYPE_P (TREE_TYPE (call)))
12353 : /* An explicit call of the form "x->~X()" has type
12354 : "void". However, on platforms where destructors
12355 : return "this" (i.e., those where
12356 : targetm.cxx.cdtor_returns_this is true), such calls
12357 : will appear to have a return value of pointer type
12358 : to the low-level call machinery. We do not want to
12359 : change the low-level machinery, since we want to be
12360 : able to optimize "delete f()" on such platforms as
12361 : "operator delete(~X(f()))" (rather than generating
12362 : "t = f(), ~X(t), operator delete (t)"). */
12363 15413753 : call = build_nop (void_type_node, call);
12364 : }
12365 : }
12366 : }
12367 :
12368 99757146 : if (processing_template_decl && call != error_mark_node)
12369 : {
12370 7179588 : bool cast_to_void = false;
12371 :
12372 7179588 : if (TREE_CODE (call) == COMPOUND_EXPR)
12373 10 : call = TREE_OPERAND (call, 1);
12374 7179578 : else if (TREE_CODE (call) == NOP_EXPR)
12375 : {
12376 0 : cast_to_void = true;
12377 0 : call = TREE_OPERAND (call, 0);
12378 : }
12379 7179588 : if (INDIRECT_REF_P (call))
12380 608745 : call = TREE_OPERAND (call, 0);
12381 :
12382 : /* Prune all but the selected function from the original overload
12383 : set so that we can avoid some duplicate work at instantiation time. */
12384 7179588 : if (really_overloaded_fn (fns))
12385 : {
12386 1643049 : if (DECL_TEMPLATE_INFO (fn)
12387 1643049 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
12388 : {
12389 : /* Use the selected template, not the specialization, so that
12390 : this looks like an actual lookup result for sake of
12391 : filter_memfn_lookup. */
12392 :
12393 280030 : if (OVL_SINGLE_P (fns))
12394 : /* If the original overload set consists of a single function
12395 : template, this isn't beneficial. */
12396 240082 : goto skip_prune;
12397 :
12398 39948 : fn = ovl_make (DECL_TI_TEMPLATE (fn));
12399 39948 : if (template_only)
12400 30258 : fn = lookup_template_function (fn, explicit_targs);
12401 : }
12402 1402967 : orig_fns = copy_node (orig_fns);
12403 1402967 : BASELINK_FUNCTIONS (orig_fns) = fn;
12404 1402967 : BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (orig_fns) = true;
12405 : }
12406 :
12407 5536539 : skip_prune:
12408 7179588 : call = (build_min_non_dep_call_vec
12409 7179588 : (call,
12410 7179588 : build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
12411 : orig_instance, orig_fns, NULL_TREE),
12412 : orig_args));
12413 7179588 : SET_EXPR_LOCATION (call, input_location);
12414 7179588 : call = convert_from_reference (call);
12415 7179588 : if (cast_to_void)
12416 0 : call = build_nop (void_type_node, call);
12417 : }
12418 :
12419 99757146 : if (orig_args != NULL)
12420 7094421 : release_tree_vector (orig_args);
12421 :
12422 : return call;
12423 101347775 : }
12424 :
12425 : /* Returns true iff standard conversion sequence ICS1 is a proper
12426 : subsequence of ICS2. */
12427 :
12428 : static bool
12429 79661569 : is_subseq (conversion *ics1, conversion *ics2)
12430 : {
12431 : /* We can assume that a conversion of the same code
12432 : between the same types indicates a subsequence since we only get
12433 : here if the types we are converting from are the same. */
12434 :
12435 79661569 : while (ics1->kind == ck_rvalue
12436 94761585 : || ics1->kind == ck_lvalue)
12437 15100016 : ics1 = next_conversion (ics1);
12438 :
12439 : while (1)
12440 : {
12441 106896863 : while (ics2->kind == ck_rvalue
12442 106896863 : || ics2->kind == ck_lvalue)
12443 15100016 : ics2 = next_conversion (ics2);
12444 :
12445 91796847 : if (ics2->kind == ck_user
12446 91796847 : || !has_next (ics2->kind))
12447 : /* At this point, ICS1 cannot be a proper subsequence of
12448 : ICS2. We can get a USER_CONV when we are comparing the
12449 : second standard conversion sequence of two user conversion
12450 : sequences. */
12451 : return false;
12452 :
12453 12142141 : ics2 = next_conversion (ics2);
12454 :
12455 12142141 : while (ics2->kind == ck_rvalue
12456 19217921 : || ics2->kind == ck_lvalue)
12457 7075780 : ics2 = next_conversion (ics2);
12458 :
12459 12142141 : if (ics2->kind == ics1->kind
12460 6908 : && same_type_p (ics2->type, ics1->type)
12461 12149004 : && (ics1->kind == ck_identity
12462 6863 : || same_type_p (next_conversion (ics2)->type,
12463 : next_conversion (ics1)->type)))
12464 : return true;
12465 : }
12466 : }
12467 :
12468 : /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
12469 : be any _TYPE nodes. */
12470 :
12471 : bool
12472 385406545 : is_properly_derived_from (tree derived, tree base)
12473 : {
12474 385406545 : if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
12475 : return false;
12476 :
12477 : /* We only allow proper derivation here. The DERIVED_FROM_P macro
12478 : considers every class derived from itself. */
12479 365335713 : return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
12480 365335713 : && DERIVED_FROM_P (base, derived));
12481 : }
12482 :
12483 : /* We build the ICS for an implicit object parameter as a pointer
12484 : conversion sequence. However, such a sequence should be compared
12485 : as if it were a reference conversion sequence. If ICS is the
12486 : implicit conversion sequence for an implicit object parameter,
12487 : modify it accordingly. */
12488 :
12489 : static void
12490 166667724 : maybe_handle_implicit_object (conversion **ics)
12491 : {
12492 166667724 : if ((*ics)->this_p)
12493 : {
12494 : /* [over.match.funcs]
12495 :
12496 : For non-static member functions, the type of the
12497 : implicit object parameter is "reference to cv X"
12498 : where X is the class of which the function is a
12499 : member and cv is the cv-qualification on the member
12500 : function declaration. */
12501 13294633 : conversion *t = *ics;
12502 13294633 : tree reference_type;
12503 :
12504 : /* The `this' parameter is a pointer to a class type. Make the
12505 : implicit conversion talk about a reference to that same class
12506 : type. */
12507 13294633 : reference_type = TREE_TYPE (t->type);
12508 13294633 : reference_type = build_reference_type (reference_type);
12509 :
12510 13294633 : if (t->kind == ck_qual)
12511 3185792 : t = next_conversion (t);
12512 13294633 : if (t->kind == ck_ptr)
12513 621961 : t = next_conversion (t);
12514 13294633 : t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
12515 13294633 : t = direct_reference_binding (reference_type, t);
12516 13294633 : t->this_p = 1;
12517 13294633 : t->rvaluedness_matches_p = 0;
12518 13294633 : *ics = t;
12519 : }
12520 166667724 : }
12521 :
12522 : /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
12523 : and return the initial reference binding conversion. Otherwise,
12524 : leave *ICS unchanged and return NULL. */
12525 :
12526 : static conversion *
12527 166667724 : maybe_handle_ref_bind (conversion **ics)
12528 : {
12529 166667724 : if ((*ics)->kind == ck_ref_bind)
12530 : {
12531 61359583 : conversion *old_ics = *ics;
12532 61359583 : *ics = next_conversion (old_ics);
12533 61359583 : (*ics)->user_conv_p = old_ics->user_conv_p;
12534 61359583 : return old_ics;
12535 : }
12536 :
12537 : return NULL;
12538 : }
12539 :
12540 : /* Get the expression at the beginning of the conversion chain C. */
12541 :
12542 : static tree
12543 51 : conv_get_original_expr (conversion *c)
12544 : {
12545 60 : for (; c; c = next_conversion (c))
12546 60 : if (c->kind == ck_identity || c->kind == ck_ambig || c->kind == ck_aggr)
12547 51 : return c->u.expr;
12548 : return NULL_TREE;
12549 : }
12550 :
12551 : /* Return a tree representing the number of elements initialized by the
12552 : list-initialization C. The caller must check that C converts to an
12553 : array type. */
12554 :
12555 : static tree
12556 126 : nelts_initialized_by_list_init (conversion *c)
12557 : {
12558 : /* If the array we're converting to has a dimension, we'll use that. */
12559 126 : if (TYPE_DOMAIN (c->type))
12560 84 : return array_type_nelts_top (c->type);
12561 : else
12562 : {
12563 : /* Otherwise, we look at how many elements the constructor we're
12564 : initializing from has. */
12565 42 : tree ctor = conv_get_original_expr (c);
12566 72 : return size_int (CONSTRUCTOR_NELTS (ctor));
12567 : }
12568 : }
12569 :
12570 : /* True iff C is a conversion that binds a reference or a pointer to
12571 : an array of unknown bound. */
12572 :
12573 : static inline bool
12574 30534002 : conv_binds_to_array_of_unknown_bound (conversion *c)
12575 : {
12576 : /* ck_ref_bind won't have the reference stripped. */
12577 30534002 : tree type = non_reference (c->type);
12578 : /* ck_qual won't have the pointer stripped. */
12579 30534002 : type = strip_pointer_operator (type);
12580 30534002 : return (TREE_CODE (type) == ARRAY_TYPE
12581 30534002 : && TYPE_DOMAIN (type) == NULL_TREE);
12582 : }
12583 :
12584 : /* Compare two implicit conversion sequences according to the rules set out in
12585 : [over.ics.rank]. Return values:
12586 :
12587 : 1: ics1 is better than ics2
12588 : -1: ics2 is better than ics1
12589 : 0: ics1 and ics2 are indistinguishable */
12590 :
12591 : static int
12592 83333903 : compare_ics (conversion *ics1, conversion *ics2)
12593 : {
12594 83333903 : tree from_type1;
12595 83333903 : tree from_type2;
12596 83333903 : tree to_type1;
12597 83333903 : tree to_type2;
12598 83333903 : tree deref_from_type1 = NULL_TREE;
12599 83333903 : tree deref_from_type2 = NULL_TREE;
12600 83333903 : tree deref_to_type1 = NULL_TREE;
12601 83333903 : tree deref_to_type2 = NULL_TREE;
12602 83333903 : conversion_rank rank1, rank2;
12603 :
12604 : /* REF_BINDING is nonzero if the result of the conversion sequence
12605 : is a reference type. In that case REF_CONV is the reference
12606 : binding conversion. */
12607 83333903 : conversion *ref_conv1;
12608 83333903 : conversion *ref_conv2;
12609 :
12610 : /* Compare badness before stripping the reference conversion. */
12611 83333903 : if (ics1->bad_p > ics2->bad_p)
12612 : return -1;
12613 83333889 : else if (ics1->bad_p < ics2->bad_p)
12614 : return 1;
12615 :
12616 : /* Handle implicit object parameters. */
12617 83333862 : maybe_handle_implicit_object (&ics1);
12618 83333862 : maybe_handle_implicit_object (&ics2);
12619 :
12620 : /* Handle reference parameters. */
12621 83333862 : ref_conv1 = maybe_handle_ref_bind (&ics1);
12622 83333862 : ref_conv2 = maybe_handle_ref_bind (&ics2);
12623 :
12624 : /* List-initialization sequence L1 is a better conversion sequence than
12625 : list-initialization sequence L2 if L1 converts to
12626 : std::initializer_list<X> for some X and L2 does not. */
12627 83333862 : if (ics1->kind == ck_list && ics2->kind != ck_list)
12628 : return 1;
12629 83333051 : if (ics2->kind == ck_list && ics1->kind != ck_list)
12630 : return -1;
12631 :
12632 : /* [over.ics.rank]
12633 :
12634 : When comparing the basic forms of implicit conversion sequences (as
12635 : defined in _over.best.ics_)
12636 :
12637 : --a standard conversion sequence (_over.ics.scs_) is a better
12638 : conversion sequence than a user-defined conversion sequence
12639 : or an ellipsis conversion sequence, and
12640 :
12641 : --a user-defined conversion sequence (_over.ics.user_) is a
12642 : better conversion sequence than an ellipsis conversion sequence
12643 : (_over.ics.ellipsis_). */
12644 : /* Use BAD_CONVERSION_RANK because we already checked for a badness
12645 : mismatch. If both ICS are bad, we try to make a decision based on
12646 : what would have happened if they'd been good. This is not an
12647 : extension, we'll still give an error when we build up the call; this
12648 : just helps us give a more helpful error message. */
12649 83332786 : rank1 = BAD_CONVERSION_RANK (ics1);
12650 83332786 : rank2 = BAD_CONVERSION_RANK (ics2);
12651 :
12652 83332786 : if (rank1 > rank2)
12653 : return -1;
12654 72345067 : else if (rank1 < rank2)
12655 : return 1;
12656 :
12657 40271852 : if (ics1->ellipsis_p)
12658 : /* Both conversions are ellipsis conversions. */
12659 : return 0;
12660 :
12661 : /* User-defined conversion sequence U1 is a better conversion sequence
12662 : than another user-defined conversion sequence U2 if they contain the
12663 : same user-defined conversion operator or constructor and if the sec-
12664 : ond standard conversion sequence of U1 is better than the second
12665 : standard conversion sequence of U2. */
12666 :
12667 : /* Handle list-conversion with the same code even though it isn't always
12668 : ranked as a user-defined conversion and it doesn't have a second
12669 : standard conversion sequence; it will still have the desired effect.
12670 : Specifically, we need to do the reference binding comparison at the
12671 : end of this function. */
12672 :
12673 40271806 : if (ics1->user_conv_p || ics1->kind == ck_list
12674 39543697 : || ics1->kind == ck_aggr || ics2->kind == ck_aggr)
12675 : {
12676 728178 : conversion *t1 = strip_standard_conversion (ics1);
12677 728178 : conversion *t2 = strip_standard_conversion (ics2);
12678 :
12679 728178 : if (!t1 || !t2 || t1->kind != t2->kind)
12680 : return 0;
12681 728159 : else if (t1->kind == ck_user)
12682 : {
12683 712452 : tree f1 = t1->cand ? t1->cand->fn : t1->type;
12684 712452 : tree f2 = t2->cand ? t2->cand->fn : t2->type;
12685 712452 : if (f1 != f2)
12686 : return 0;
12687 : }
12688 : /* List-initialization sequence L1 is a better conversion sequence than
12689 : list-initialization sequence L2 if
12690 :
12691 : -- L1 and L2 convert to arrays of the same element type, and either
12692 : the number of elements n1 initialized by L1 is less than the number
12693 : of elements n2 initialized by L2, or n1=n2 and L2 converts to an array
12694 : of unknown bound and L1 does not. (Added in CWG 1307 and extended by
12695 : P0388R4.) */
12696 15707 : else if (t1->kind == ck_aggr
12697 14695 : && TREE_CODE (t1->type) == ARRAY_TYPE
12698 66 : && TREE_CODE (t2->type) == ARRAY_TYPE
12699 15773 : && same_type_p (TREE_TYPE (t1->type), TREE_TYPE (t2->type)))
12700 : {
12701 63 : tree n1 = nelts_initialized_by_list_init (t1);
12702 63 : tree n2 = nelts_initialized_by_list_init (t2);
12703 63 : if (tree_int_cst_lt (n1, n2))
12704 : return 1;
12705 24 : else if (tree_int_cst_lt (n2, n1))
12706 : return -1;
12707 : /* The n1 == n2 case. */
12708 24 : bool c1 = conv_binds_to_array_of_unknown_bound (t1);
12709 24 : bool c2 = conv_binds_to_array_of_unknown_bound (t2);
12710 24 : if (c1 && !c2)
12711 : return -1;
12712 6 : else if (!c1 && c2)
12713 : return 1;
12714 : else
12715 6 : return 0;
12716 : }
12717 : else
12718 : {
12719 : /* For ambiguous or aggregate conversions, use the target type as
12720 : a proxy for the conversion function. */
12721 15644 : if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
12722 : return 0;
12723 : }
12724 :
12725 : /* We can just fall through here, after setting up
12726 : FROM_TYPE1 and FROM_TYPE2. */
12727 725121 : from_type1 = t1->type;
12728 725121 : from_type2 = t2->type;
12729 725121 : }
12730 : else
12731 : {
12732 : conversion *t1;
12733 : conversion *t2;
12734 :
12735 : /* We're dealing with two standard conversion sequences.
12736 :
12737 : [over.ics.rank]
12738 :
12739 : Standard conversion sequence S1 is a better conversion
12740 : sequence than standard conversion sequence S2 if
12741 :
12742 : --S1 is a proper subsequence of S2 (comparing the conversion
12743 : sequences in the canonical form defined by _over.ics.scs_,
12744 : excluding any Lvalue Transformation; the identity
12745 : conversion sequence is considered to be a subsequence of
12746 : any non-identity conversion sequence */
12747 :
12748 : t1 = ics1;
12749 56797460 : while (t1->kind != ck_identity)
12750 17253832 : t1 = next_conversion (t1);
12751 39543628 : from_type1 = t1->type;
12752 :
12753 39543628 : t2 = ics2;
12754 56690146 : while (t2->kind != ck_identity)
12755 17146518 : t2 = next_conversion (t2);
12756 39543628 : from_type2 = t2->type;
12757 : }
12758 :
12759 : /* One sequence can only be a subsequence of the other if they start with
12760 : the same type. They can start with different types when comparing the
12761 : second standard conversion sequence in two user-defined conversion
12762 : sequences. */
12763 40268749 : if (same_type_p (from_type1, from_type2))
12764 : {
12765 39834187 : if (is_subseq (ics1, ics2))
12766 : return 1;
12767 39827382 : if (is_subseq (ics2, ics1))
12768 : return -1;
12769 : }
12770 :
12771 : /* [over.ics.rank]
12772 :
12773 : Or, if not that,
12774 :
12775 : --the rank of S1 is better than the rank of S2 (by the rules
12776 : defined below):
12777 :
12778 : Standard conversion sequences are ordered by their ranks: an Exact
12779 : Match is a better conversion than a Promotion, which is a better
12780 : conversion than a Conversion.
12781 :
12782 : Two conversion sequences with the same rank are indistinguishable
12783 : unless one of the following rules applies:
12784 :
12785 : --A conversion that does not a convert a pointer, pointer to member,
12786 : or std::nullptr_t to bool is better than one that does.
12787 :
12788 : The ICS_STD_RANK automatically handles the pointer-to-bool rule,
12789 : so that we do not have to check it explicitly. */
12790 40261886 : if (ics1->rank < ics2->rank)
12791 : return 1;
12792 40261806 : else if (ics2->rank < ics1->rank)
12793 : return -1;
12794 :
12795 40261806 : to_type1 = ics1->type;
12796 40261806 : to_type2 = ics2->type;
12797 :
12798 : /* A conversion from scalar arithmetic type to complex is worse than a
12799 : conversion between scalar arithmetic types. */
12800 40261806 : if (same_type_p (from_type1, from_type2)
12801 39827244 : && ARITHMETIC_TYPE_P (from_type1)
12802 8101324 : && ARITHMETIC_TYPE_P (to_type1)
12803 8101151 : && ARITHMETIC_TYPE_P (to_type2)
12804 40261806 : && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
12805 8101097 : != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
12806 : {
12807 990 : if (TREE_CODE (to_type1) == COMPLEX_TYPE)
12808 : return -1;
12809 : else
12810 987 : return 1;
12811 : }
12812 :
12813 40260816 : {
12814 : /* A conversion in either direction between floating-point type FP1 and
12815 : floating-point type FP2 is better than a conversion in the same
12816 : direction between FP1 and arithmetic type T3 if
12817 : - the floating-point conversion rank of FP1 is equal to the rank of
12818 : FP2, and
12819 : - T3 is not a floating-point type, or T3 is a floating-point type
12820 : whose rank is not equal to the rank of FP1, or the floating-point
12821 : conversion subrank of FP2 is greater than the subrank of T3. */
12822 40260816 : tree fp1 = from_type1;
12823 40260816 : tree fp2 = to_type1;
12824 40260816 : tree fp3 = from_type2;
12825 40260816 : tree t3 = to_type2;
12826 40260816 : int ret = 1;
12827 40260816 : if (TYPE_MAIN_VARIANT (fp2) == TYPE_MAIN_VARIANT (t3))
12828 : {
12829 35313239 : std::swap (fp1, fp2);
12830 35313239 : std::swap (fp3, t3);
12831 : }
12832 40260816 : if (TYPE_MAIN_VARIANT (fp1) == TYPE_MAIN_VARIANT (fp3)
12833 40260729 : && SCALAR_FLOAT_TYPE_P (fp1)
12834 : /* Only apply this rule if at least one of the 3 types is
12835 : extended floating-point type, otherwise keep them as
12836 : before for compatibility reasons with types like __float128.
12837 : float, double and long double alone have different conversion
12838 : ranks and so when just those 3 types are involved, this
12839 : rule doesn't trigger. */
12840 44632107 : && (extended_float_type_p (fp1)
12841 4292269 : || (SCALAR_FLOAT_TYPE_P (fp2) && extended_float_type_p (fp2))
12842 4201867 : || (SCALAR_FLOAT_TYPE_P (t3) && extended_float_type_p (t3))))
12843 : {
12844 169424 : if (TREE_CODE (fp2) != REAL_TYPE)
12845 : {
12846 33444 : ret = -ret;
12847 33444 : std::swap (fp2, t3);
12848 : }
12849 169424 : if (SCALAR_FLOAT_TYPE_P (fp2))
12850 : {
12851 : /* cp_compare_floating_point_conversion_ranks returns -1, 0 or 1
12852 : if the conversion rank is equal (-1 or 1 if the subrank is
12853 : different). */
12854 151067 : if (IN_RANGE (cp_compare_floating_point_conversion_ranks (fp1,
12855 : fp2),
12856 : -1, 1))
12857 : {
12858 : /* Conversion ranks of FP1 and FP2 are equal. */
12859 94058 : if (TREE_CODE (t3) != REAL_TYPE
12860 94058 : || !IN_RANGE (cp_compare_floating_point_conversion_ranks
12861 : (fp1, t3),
12862 : -1, 1))
12863 : /* FP1 <-> FP2 conversion is better. */
12864 83333903 : return ret;
12865 746 : int c = cp_compare_floating_point_conversion_ranks (fp2, t3);
12866 746 : gcc_assert (IN_RANGE (c, -1, 1));
12867 746 : if (c == 1)
12868 : /* Conversion subrank of FP2 is greater than subrank of T3.
12869 : FP1 <-> FP2 conversion is better. */
12870 : return ret;
12871 746 : else if (c == -1)
12872 : /* Conversion subrank of FP2 is less than subrank of T3.
12873 : FP1 <-> T3 conversion is better. */
12874 0 : return -ret;
12875 : }
12876 57009 : else if (SCALAR_FLOAT_TYPE_P (t3)
12877 57009 : && IN_RANGE (cp_compare_floating_point_conversion_ranks
12878 : (fp1, t3),
12879 : -1, 1))
12880 : /* Conversion ranks of FP1 and FP2 are not equal, conversion
12881 : ranks of FP1 and T3 are equal.
12882 : FP1 <-> T3 conversion is better. */
12883 10013 : return -ret;
12884 : }
12885 : }
12886 : }
12887 :
12888 40157491 : if (TYPE_PTR_P (from_type1)
12889 5235953 : && TYPE_PTR_P (from_type2)
12890 5235937 : && TYPE_PTR_P (to_type1)
12891 5235913 : && TYPE_PTR_P (to_type2))
12892 : {
12893 5235913 : deref_from_type1 = TREE_TYPE (from_type1);
12894 5235913 : deref_from_type2 = TREE_TYPE (from_type2);
12895 5235913 : deref_to_type1 = TREE_TYPE (to_type1);
12896 5235913 : deref_to_type2 = TREE_TYPE (to_type2);
12897 : }
12898 : /* The rules for pointers to members A::* are just like the rules
12899 : for pointers A*, except opposite: if B is derived from A then
12900 : A::* converts to B::*, not vice versa. For that reason, we
12901 : switch the from_ and to_ variables here. */
12902 59 : else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2)
12903 59 : && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2))
12904 34921578 : || (TYPE_PTRMEMFUNC_P (from_type1)
12905 437 : && TYPE_PTRMEMFUNC_P (from_type2)
12906 437 : && TYPE_PTRMEMFUNC_P (to_type1)
12907 437 : && TYPE_PTRMEMFUNC_P (to_type2)))
12908 : {
12909 496 : deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
12910 496 : deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
12911 496 : deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
12912 496 : deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
12913 : }
12914 :
12915 5236409 : if (deref_from_type1 != NULL_TREE
12916 5236409 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
12917 305471 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
12918 : {
12919 : /* This was one of the pointer or pointer-like conversions.
12920 :
12921 : [over.ics.rank]
12922 :
12923 : --If class B is derived directly or indirectly from class A,
12924 : conversion of B* to A* is better than conversion of B* to
12925 : void*, and conversion of A* to void* is better than
12926 : conversion of B* to void*. */
12927 305471 : if (VOID_TYPE_P (deref_to_type1)
12928 57 : && VOID_TYPE_P (deref_to_type2))
12929 : {
12930 14 : if (is_properly_derived_from (deref_from_type1,
12931 : deref_from_type2))
12932 : return -1;
12933 14 : else if (is_properly_derived_from (deref_from_type2,
12934 : deref_from_type1))
12935 : return 1;
12936 : }
12937 305457 : else if (VOID_TYPE_P (deref_to_type1)
12938 305414 : || VOID_TYPE_P (deref_to_type2))
12939 : {
12940 47 : if (same_type_p (deref_from_type1, deref_from_type2))
12941 : {
12942 47 : if (VOID_TYPE_P (deref_to_type2))
12943 : {
12944 4 : if (is_properly_derived_from (deref_from_type1,
12945 : deref_to_type1))
12946 : return 1;
12947 : }
12948 : /* We know that DEREF_TO_TYPE1 is `void' here. */
12949 43 : else if (is_properly_derived_from (deref_from_type1,
12950 : deref_to_type2))
12951 : return -1;
12952 : }
12953 : }
12954 305410 : else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
12955 305410 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
12956 : {
12957 : /* [over.ics.rank]
12958 :
12959 : --If class B is derived directly or indirectly from class A
12960 : and class C is derived directly or indirectly from B,
12961 :
12962 : --conversion of C* to B* is better than conversion of C* to
12963 : A*,
12964 :
12965 : --conversion of B* to A* is better than conversion of C* to
12966 : A* */
12967 305410 : if (same_type_p (deref_from_type1, deref_from_type2))
12968 : {
12969 305404 : if (is_properly_derived_from (deref_to_type1,
12970 : deref_to_type2))
12971 : return 1;
12972 305404 : else if (is_properly_derived_from (deref_to_type2,
12973 : deref_to_type1))
12974 : return -1;
12975 : }
12976 6 : else if (same_type_p (deref_to_type1, deref_to_type2))
12977 : {
12978 6 : if (is_properly_derived_from (deref_from_type2,
12979 : deref_from_type1))
12980 : return 1;
12981 0 : else if (is_properly_derived_from (deref_from_type1,
12982 : deref_from_type2))
12983 : return -1;
12984 : }
12985 : }
12986 : }
12987 79704040 : else if (CLASS_TYPE_P (non_reference (from_type1))
12988 64345336 : && same_type_p (from_type1, from_type2))
12989 : {
12990 24121521 : tree from = non_reference (from_type1);
12991 :
12992 : /* [over.ics.rank]
12993 :
12994 : --binding of an expression of type C to a reference of type
12995 : B& is better than binding an expression of type C to a
12996 : reference of type A&
12997 :
12998 : --conversion of C to B is better than conversion of C to A, */
12999 24121521 : if (is_properly_derived_from (from, to_type1)
13000 24121521 : && is_properly_derived_from (from, to_type2))
13001 : {
13002 967581 : if (is_properly_derived_from (to_type1, to_type2))
13003 : return 1;
13004 963520 : else if (is_properly_derived_from (to_type2, to_type1))
13005 : return -1;
13006 : }
13007 : }
13008 31460998 : else if (CLASS_TYPE_P (non_reference (to_type1))
13009 16102294 : && same_type_p (to_type1, to_type2))
13010 : {
13011 7 : tree to = non_reference (to_type1);
13012 :
13013 : /* [over.ics.rank]
13014 :
13015 : --binding of an expression of type B to a reference of type
13016 : A& is better than binding an expression of type C to a
13017 : reference of type A&,
13018 :
13019 : --conversion of B to A is better than conversion of C to A */
13020 7 : if (is_properly_derived_from (from_type1, to)
13021 7 : && is_properly_derived_from (from_type2, to))
13022 : {
13023 3 : if (is_properly_derived_from (from_type2, from_type1))
13024 : return 1;
13025 3 : else if (is_properly_derived_from (from_type1, from_type2))
13026 : return -1;
13027 : }
13028 : }
13029 :
13030 : /* [over.ics.rank]
13031 :
13032 : --S1 and S2 differ only in their qualification conversion and yield
13033 : similar types T1 and T2 (_conv.qual_), respectively, and the cv-
13034 : qualification signature of type T1 is a proper subset of the cv-
13035 : qualification signature of type T2 */
13036 40067739 : if (ics1->kind == ck_qual
13037 355 : && ics2->kind == ck_qual
13038 40068094 : && same_type_p (from_type1, from_type2))
13039 : {
13040 355 : int result = comp_cv_qual_signature (to_type1, to_type2);
13041 355 : if (result != 0)
13042 : return result;
13043 : }
13044 :
13045 : /* [over.ics.rank]
13046 :
13047 : --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
13048 : to an implicit object parameter of a non-static member function
13049 : declared without a ref-qualifier, and either S1 binds an lvalue
13050 : reference to an lvalue and S2 binds an rvalue reference or S1 binds an
13051 : rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x
13052 : draft standard, 13.3.3.2)
13053 :
13054 : --S1 and S2 are reference bindings (_dcl.init.ref_), and the
13055 : types to which the references refer are the same type except for
13056 : top-level cv-qualifiers, and the type to which the reference
13057 : initialized by S2 refers is more cv-qualified than the type to
13058 : which the reference initialized by S1 refers.
13059 :
13060 : DR 1328 [over.match.best]: the context is an initialization by
13061 : conversion function for direct reference binding (13.3.1.6) of a
13062 : reference to function type, the return type of F1 is the same kind of
13063 : reference (i.e. lvalue or rvalue) as the reference being initialized,
13064 : and the return type of F2 is not. */
13065 :
13066 40067653 : if (ref_conv1 && ref_conv2)
13067 : {
13068 18416289 : if (!ref_conv1->this_p && !ref_conv2->this_p
13069 18154981 : && (ref_conv1->rvaluedness_matches_p
13070 18154981 : != ref_conv2->rvaluedness_matches_p)
13071 34696939 : && (same_type_p (ref_conv1->type, ref_conv2->type)
13072 9714943 : || (TYPE_REF_IS_RVALUE (ref_conv1->type)
13073 9714943 : != TYPE_REF_IS_RVALUE (ref_conv2->type))))
13074 : {
13075 9714671 : if (ref_conv1->bad_p
13076 9714671 : && !same_type_p (TREE_TYPE (ref_conv1->type),
13077 : TREE_TYPE (ref_conv2->type)))
13078 : /* Don't prefer a bad conversion that drops cv-quals to a bad
13079 : conversion with the wrong rvalueness. */
13080 : return 0;
13081 9713208 : return (ref_conv1->rvaluedness_matches_p
13082 9713208 : - ref_conv2->rvaluedness_matches_p);
13083 : }
13084 :
13085 15267322 : if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
13086 : {
13087 : /* Per P0388R4:
13088 :
13089 : void f (int(&)[]), // (1)
13090 : f (int(&)[1]), // (2)
13091 : f (int*); // (3)
13092 :
13093 : (2) is better than (1), but (3) should be equal to (1) and to
13094 : (2). For that reason we don't use ck_qual for (1) which would
13095 : give it the cr_exact rank while (3) remains ck_identity.
13096 : Therefore we compare (1) and (2) here. For (1) we'll have
13097 :
13098 : ck_ref_bind <- ck_identity
13099 : int[] & int[1]
13100 :
13101 : so to handle this we must look at ref_conv. */
13102 15266739 : bool c1 = conv_binds_to_array_of_unknown_bound (ref_conv1);
13103 15266739 : bool c2 = conv_binds_to_array_of_unknown_bound (ref_conv2);
13104 15266739 : if (c1 && !c2)
13105 : return -1;
13106 15266733 : else if (!c1 && c2)
13107 : return 1;
13108 :
13109 15266733 : int q1 = cp_type_quals (TREE_TYPE (ref_conv1->type));
13110 15266733 : int q2 = cp_type_quals (TREE_TYPE (ref_conv2->type));
13111 15266733 : if (ref_conv1->bad_p)
13112 : {
13113 : /* Prefer the one that drops fewer cv-quals. */
13114 1606 : tree ftype = next_conversion (ref_conv1)->type;
13115 1606 : int fquals = cp_type_quals (ftype);
13116 1606 : q1 ^= fquals;
13117 1606 : q2 ^= fquals;
13118 : }
13119 15266733 : return comp_cv_qualification (q2, q1);
13120 : }
13121 : }
13122 :
13123 : /* [over.ics.rank]
13124 :
13125 : Per CWG 1601:
13126 : -- A conversion that promotes an enumeration whose underlying type
13127 : is fixed to its underlying type is better than one that promotes to
13128 : the promoted underlying type, if the two are different. */
13129 15086243 : if (ics1->rank == cr_promotion
13130 146 : && ics2->rank == cr_promotion
13131 146 : && UNSCOPED_ENUM_P (from_type1)
13132 27 : && ENUM_FIXED_UNDERLYING_TYPE_P (from_type1)
13133 15086267 : && same_type_p (from_type1, from_type2))
13134 : {
13135 24 : tree utype = ENUM_UNDERLYING_TYPE (from_type1);
13136 24 : tree prom = type_promotes_to (from_type1);
13137 24 : if (!same_type_p (utype, prom))
13138 : {
13139 12 : if (same_type_p (to_type1, utype)
13140 12 : && same_type_p (to_type2, prom))
13141 : return 1;
13142 6 : else if (same_type_p (to_type2, utype)
13143 6 : && same_type_p (to_type1, prom))
13144 6 : return -1;
13145 : }
13146 : }
13147 :
13148 : /* Neither conversion sequence is better than the other. */
13149 : return 0;
13150 : }
13151 :
13152 : /* The source type for this standard conversion sequence. */
13153 :
13154 : static tree
13155 9 : source_type (conversion *t)
13156 : {
13157 9 : return strip_standard_conversion (t)->type;
13158 : }
13159 :
13160 : /* Note a warning about preferring WINNER to LOSER. We do this by storing
13161 : a pointer to LOSER and re-running joust to produce the warning if WINNER
13162 : is actually used. */
13163 :
13164 : static void
13165 494 : add_warning (struct z_candidate *winner, struct z_candidate *loser)
13166 : {
13167 494 : candidate_warning *cw = (candidate_warning *)
13168 0 : conversion_obstack_alloc (sizeof (candidate_warning));
13169 494 : cw->loser = loser;
13170 494 : cw->next = winner->warnings;
13171 494 : winner->warnings = cw;
13172 0 : }
13173 :
13174 : /* CAND is a constructor candidate in joust in C++17 and up. If it copies a
13175 : prvalue returned from a conversion function, return true. Otherwise, return
13176 : false. */
13177 :
13178 : static bool
13179 875038 : joust_maybe_elide_copy (z_candidate *cand)
13180 : {
13181 875038 : tree fn = cand->fn;
13182 2197011 : if (!DECL_COPY_CONSTRUCTOR_P (fn) && !DECL_MOVE_CONSTRUCTOR_P (fn))
13183 : return false;
13184 428298 : conversion *conv = cand->convs[0];
13185 428298 : if (conv->kind == ck_ambig)
13186 : return false;
13187 428296 : gcc_checking_assert (conv->kind == ck_ref_bind);
13188 428296 : conv = next_conversion (conv);
13189 428296 : if (conv->kind == ck_user && !TYPE_REF_P (conv->type))
13190 : {
13191 342 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
13192 : (conv->type, DECL_CONTEXT (fn)));
13193 342 : z_candidate *uc = conv->cand;
13194 342 : if (DECL_CONV_FN_P (uc->fn))
13195 233 : return true;
13196 : }
13197 : return false;
13198 : }
13199 :
13200 : /* Return the class that CAND's implicit object parameter refers to. */
13201 :
13202 : static tree
13203 295206 : class_of_implicit_object (z_candidate *cand)
13204 : {
13205 295206 : if (!DECL_IOBJ_MEMBER_FUNCTION_P (cand->fn))
13206 : return NULL_TREE;
13207 :
13208 : /* "For conversion functions that are implicit object member functions,
13209 : the function is considered to be a member of the class of the implied
13210 : object argument for the purpose of defining the type of the implicit
13211 : object parameter." */
13212 295206 : if (DECL_CONV_FN_P (cand->fn))
13213 0 : return TYPE_MAIN_VARIANT (TREE_TYPE (cand->first_arg));
13214 :
13215 : /* "For non-conversion functions that are implicit object member
13216 : functions nominated by a using-declaration in a derived class, the
13217 : function is considered to be a member of the derived class for the
13218 : purpose of defining the type of the implicit object parameter."
13219 :
13220 : That derived class is reflected in the conversion_path binfo. */
13221 295206 : return BINFO_TYPE (cand->conversion_path);
13222 : }
13223 :
13224 : /* Return whether the first parameter of C1 matches the second parameter
13225 : of C2. */
13226 :
13227 : static bool
13228 564794 : reversed_match (z_candidate *c1, z_candidate *c2)
13229 : {
13230 564794 : tree fn1 = c1->fn;
13231 564794 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (c2->fn));
13232 564794 : tree parm2 = TREE_VALUE (TREE_CHAIN (parms2));
13233 564794 : if (DECL_IOBJ_MEMBER_FUNCTION_P (fn1))
13234 : {
13235 295206 : tree ctx = class_of_implicit_object (c1);
13236 295206 : return iobj_parm_corresponds_to (fn1, parm2, ctx);
13237 : }
13238 : else
13239 : {
13240 269588 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn1));
13241 269588 : tree parm1 = TREE_VALUE (parms1);
13242 269588 : return same_type_p (parm1, parm2);
13243 : }
13244 : }
13245 :
13246 : /* True if the defining declarations of the two candidates have equivalent
13247 : parameters. MATCH_KIND controls whether we're trying to compare the
13248 : original declarations (for a warning) or the actual candidates. */
13249 :
13250 : enum class pmatch { original, current };
13251 :
13252 : static bool
13253 4967820 : cand_parms_match (z_candidate *c1, z_candidate *c2, pmatch match_kind)
13254 : {
13255 4967820 : tree fn1 = c1->fn;
13256 4967820 : tree fn2 = c2->fn;
13257 4967820 : bool reversed = (match_kind == pmatch::current
13258 4967820 : && c1->reversed () != c2->reversed ());
13259 4967820 : if (fn1 == fn2 && !reversed)
13260 : return true;
13261 4967511 : if (identifier_p (fn1) || identifier_p (fn2))
13262 : return false;
13263 4967506 : if (match_kind == pmatch::original)
13264 : {
13265 : /* We don't look at c1->template_decl because that's only set for
13266 : primary templates, not e.g. non-template member functions of
13267 : class templates. */
13268 22 : tree t1 = most_general_template (fn1);
13269 22 : tree t2 = most_general_template (fn2);
13270 22 : if (t1 || t2)
13271 : {
13272 15 : if (!t1 || !t2)
13273 : return false;
13274 15 : if (t1 == t2)
13275 : return true;
13276 3 : fn1 = DECL_TEMPLATE_RESULT (t1);
13277 3 : fn2 = DECL_TEMPLATE_RESULT (t2);
13278 : }
13279 : }
13280 :
13281 4967494 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn1));
13282 4967494 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (fn2));
13283 :
13284 9715225 : if (DECL_FUNCTION_MEMBER_P (fn1)
13285 4968118 : && DECL_FUNCTION_MEMBER_P (fn2))
13286 : {
13287 220373 : tree base1 = DECL_CONTEXT (strip_inheriting_ctors (fn1));
13288 220373 : tree base2 = DECL_CONTEXT (strip_inheriting_ctors (fn2));
13289 220373 : if (base1 != base2)
13290 148085 : return false;
13291 :
13292 220196 : if (reversed)
13293 147908 : return (reversed_match (c1, c2)
13294 147908 : && reversed_match (c2, c1));
13295 :
13296 : /* Use object_parms_correspond to simplify comparing iobj/xobj/static
13297 : member functions. */
13298 72288 : if (!object_parms_correspond (fn1, fn2, base1))
13299 : return false;
13300 :
13301 : /* We just compared the object parameters, if they don't correspond
13302 : we already returned false. */
13303 216864 : auto skip_parms = [] (tree fn, tree parms)
13304 : {
13305 144576 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
13306 1020 : return TREE_CHAIN (parms);
13307 : else
13308 143556 : return skip_artificial_parms_for (fn, parms);
13309 : };
13310 72288 : parms1 = skip_parms (fn1, parms1);
13311 72288 : parms2 = skip_parms (fn2, parms2);
13312 : }
13313 4747121 : else if (reversed)
13314 134759 : return (reversed_match (c1, c2)
13315 134759 : && reversed_match (c2, c1));
13316 4684650 : return compparms (parms1, parms2);
13317 : }
13318 :
13319 : /* True iff FN is a copy or move constructor or assignment operator. */
13320 :
13321 : static bool
13322 20572036 : sfk_copy_or_move (tree fn)
13323 : {
13324 20572036 : if (TREE_CODE (fn) != FUNCTION_DECL)
13325 : return false;
13326 20571946 : special_function_kind sfk = special_function_p (fn);
13327 20571946 : return sfk >= sfk_copy_constructor && sfk <= sfk_move_assignment;
13328 : }
13329 :
13330 : /* Compare two candidates for overloading as described in
13331 : [over.match.best]. Return values:
13332 :
13333 : 1: cand1 is better than cand2
13334 : -1: cand2 is better than cand1
13335 : 0: cand1 and cand2 are indistinguishable */
13336 :
13337 : static int
13338 78083430 : joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
13339 : tsubst_flags_t complain)
13340 : {
13341 78083430 : int winner = 0;
13342 78083430 : int off1 = 0, off2 = 0;
13343 78083430 : size_t i;
13344 78083430 : size_t len;
13345 :
13346 : /* Candidates that involve bad conversions are always worse than those
13347 : that don't. */
13348 78083430 : if (cand1->viable > cand2->viable)
13349 : return 1;
13350 62666086 : if (cand1->viable < cand2->viable)
13351 : return -1;
13352 :
13353 : /* If we have two pseudo-candidates for conversions to the same type,
13354 : or two candidates for the same function, arbitrarily pick one. */
13355 62666086 : if (cand1->fn == cand2->fn
13356 1894358 : && cand1->reversed () == cand2->reversed ()
13357 63501223 : && (IS_TYPE_OR_DECL_P (cand1->fn)))
13358 : return 1;
13359 :
13360 : /* Prefer a non-deleted function over an implicitly deleted move
13361 : constructor or assignment operator. This differs slightly from the
13362 : wording for issue 1402 (which says the move op is ignored by overload
13363 : resolution), but this way produces better error messages. */
13364 62666086 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13365 58297923 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13366 120954237 : && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn))
13367 : {
13368 2511732 : if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn)
13369 2097046 : && move_fn_p (cand1->fn))
13370 : return -1;
13371 3766612 : if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn)
13372 3041635 : && move_fn_p (cand2->fn))
13373 : return 1;
13374 : }
13375 :
13376 : /* a viable function F1
13377 : is defined to be a better function than another viable function F2 if
13378 : for all arguments i, ICSi(F1) is not a worse conversion sequence than
13379 : ICSi(F2), and then */
13380 :
13381 : /* for some argument j, ICSj(F1) is a better conversion sequence than
13382 : ICSj(F2) */
13383 :
13384 : /* For comparing static and non-static member functions, we ignore
13385 : the implicit object parameter of the non-static function. The
13386 : standard says to pretend that the static function has an object
13387 : parm, but that won't work with operator overloading. */
13388 62651398 : len = cand1->num_convs;
13389 62651398 : if (len != cand2->num_convs)
13390 : {
13391 758 : int static_1 = (TREE_CODE (cand1->fn) == FUNCTION_DECL
13392 758 : && DECL_STATIC_FUNCTION_P (cand1->fn));
13393 758 : int static_2 = (TREE_CODE (cand2->fn) == FUNCTION_DECL
13394 758 : && DECL_STATIC_FUNCTION_P (cand2->fn));
13395 :
13396 758 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13397 193 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13398 193 : && DECL_CONSTRUCTOR_P (cand1->fn)
13399 764 : && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
13400 : /* We're comparing a near-match list constructor and a near-match
13401 : non-list constructor. Just treat them as unordered. */
13402 : return 0;
13403 :
13404 752 : gcc_assert (static_1 != static_2);
13405 :
13406 752 : if (static_1)
13407 : {
13408 : /* C++23 [over.best.ics.general] says:
13409 : When the parameter is the implicit object parameter of a static
13410 : member function, the implicit conversion sequence is a standard
13411 : conversion sequence that is neither better nor worse than any
13412 : other standard conversion sequence. */
13413 72 : if (CONVERSION_RANK (cand2->convs[0]) >= cr_user)
13414 0 : winner = 1;
13415 : off2 = 1;
13416 : }
13417 : else
13418 : {
13419 680 : if (CONVERSION_RANK (cand1->convs[0]) >= cr_user)
13420 565 : winner = -1;
13421 680 : off1 = 1;
13422 680 : --len;
13423 : }
13424 : }
13425 :
13426 145932081 : for (i = 0; i < len; ++i)
13427 : {
13428 83282014 : conversion *t1 = cand1->convs[i + off1];
13429 83282014 : conversion *t2 = cand2->convs[i + off2];
13430 83282014 : int comp = compare_ics (t1, t2);
13431 :
13432 83282014 : if (comp != 0)
13433 : {
13434 56431541 : if ((complain & tf_warning)
13435 46600166 : && warn_sign_promo
13436 82 : && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
13437 : == cr_std + cr_promotion)
13438 50 : && t1->kind == ck_std
13439 31 : && t2->kind == ck_std
13440 31 : && TREE_CODE (t1->type) == INTEGER_TYPE
13441 31 : && TREE_CODE (t2->type) == INTEGER_TYPE
13442 31 : && (TYPE_PRECISION (t1->type)
13443 31 : == TYPE_PRECISION (t2->type))
13444 56431572 : && (TYPE_UNSIGNED (next_conversion (t1)->type)
13445 0 : || (TREE_CODE (next_conversion (t1)->type)
13446 : == ENUMERAL_TYPE)))
13447 : {
13448 31 : tree type = next_conversion (t1)->type;
13449 31 : tree type1, type2;
13450 31 : struct z_candidate *w, *l;
13451 31 : if (comp > 0)
13452 : type1 = t1->type, type2 = t2->type,
13453 : w = cand1, l = cand2;
13454 : else
13455 8 : type1 = t2->type, type2 = t1->type,
13456 8 : w = cand2, l = cand1;
13457 :
13458 31 : if (warn)
13459 : {
13460 9 : warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
13461 : type, type1, type2);
13462 9 : warning (OPT_Wsign_promo, " in call to %qD", w->fn);
13463 : }
13464 : else
13465 22 : add_warning (w, l);
13466 : }
13467 :
13468 56431541 : if (winner && comp != winner)
13469 : {
13470 : /* Ambiguity between normal and reversed comparison operators
13471 : with the same parameter types. P2468 decided not to go with
13472 : this approach to resolving the ambiguity, so pedwarn. */
13473 1325 : if ((complain & tf_warning_or_error)
13474 751 : && (cand1->reversed () != cand2->reversed ())
13475 1661 : && cand_parms_match (cand1, cand2, pmatch::original))
13476 : {
13477 321 : struct z_candidate *w, *l;
13478 321 : if (cand2->reversed ())
13479 : winner = 1, w = cand1, l = cand2;
13480 : else
13481 299 : winner = -1, w = cand2, l = cand1;
13482 321 : if (warn)
13483 : {
13484 22 : auto_diagnostic_group d;
13485 22 : if (pedwarn (input_location, 0,
13486 : "C++20 says that these are ambiguous, "
13487 : "even though the second is reversed:"))
13488 : {
13489 22 : print_z_candidate (input_location,
13490 : N_("candidate 1:"), w);
13491 22 : print_z_candidate (input_location,
13492 : N_("candidate 2:"), l);
13493 22 : if (w->fn == l->fn
13494 17 : && DECL_IOBJ_MEMBER_FUNCTION_P (w->fn)
13495 37 : && (type_memfn_quals (TREE_TYPE (w->fn))
13496 15 : & TYPE_QUAL_CONST) == 0)
13497 : {
13498 : /* Suggest adding const to
13499 : struct A { bool operator==(const A&); }; */
13500 12 : tree parmtype
13501 12 : = FUNCTION_FIRST_USER_PARMTYPE (w->fn);
13502 12 : parmtype = TREE_VALUE (parmtype);
13503 12 : if (TYPE_REF_P (parmtype)
13504 12 : && TYPE_READONLY (TREE_TYPE (parmtype))
13505 24 : && (same_type_ignoring_top_level_qualifiers_p
13506 12 : (TREE_TYPE (parmtype),
13507 12 : DECL_CONTEXT (w->fn))))
13508 12 : inform (DECL_SOURCE_LOCATION (w->fn),
13509 : "try making the operator a %<const%> "
13510 : "member function");
13511 : }
13512 : }
13513 22 : }
13514 : else
13515 299 : add_warning (w, l);
13516 : return winner;
13517 : }
13518 :
13519 1004 : winner = 0;
13520 1004 : goto tweak;
13521 : }
13522 : winner = comp;
13523 : }
13524 : }
13525 :
13526 : /* warn about confusing overload resolution for user-defined conversions,
13527 : either between a constructor and a conversion op, or between two
13528 : conversion ops. */
13529 62650067 : if ((complain & tf_warning)
13530 : /* In C++17, the constructor might have been elided, which means that
13531 : an originally null ->second_conv could become non-null. */
13532 51130543 : && winner && warn_conversion && cand1->second_conv && cand2->second_conv
13533 54 : && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
13534 62650094 : && winner != compare_ics (cand1->second_conv, cand2->second_conv))
13535 : {
13536 27 : struct z_candidate *w, *l;
13537 27 : bool give_warning = false;
13538 :
13539 27 : if (winner == 1)
13540 : w = cand1, l = cand2;
13541 : else
13542 6 : w = cand2, l = cand1;
13543 :
13544 : /* We don't want to complain about `X::operator T1 ()'
13545 : beating `X::operator T2 () const', when T2 is a no less
13546 : cv-qualified version of T1. */
13547 27 : if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
13548 39 : && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
13549 : {
13550 6 : tree t = TREE_TYPE (TREE_TYPE (l->fn));
13551 6 : tree f = TREE_TYPE (TREE_TYPE (w->fn));
13552 :
13553 6 : if (TREE_CODE (t) == TREE_CODE (f) && INDIRECT_TYPE_P (t))
13554 : {
13555 6 : t = TREE_TYPE (t);
13556 6 : f = TREE_TYPE (f);
13557 : }
13558 6 : if (!comp_ptr_ttypes (t, f))
13559 : give_warning = true;
13560 : }
13561 : else
13562 : give_warning = true;
13563 :
13564 : if (!give_warning)
13565 : /*NOP*/;
13566 21 : else if (warn)
13567 : {
13568 9 : tree source = source_type (w->convs[0]);
13569 9 : if (INDIRECT_TYPE_P (source))
13570 6 : source = TREE_TYPE (source);
13571 9 : auto_diagnostic_group d;
13572 9 : if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
13573 18 : && warning (OPT_Wconversion, " for conversion from %qH to %qI",
13574 9 : source, w->second_conv->type))
13575 : {
13576 9 : inform (input_location, " because conversion sequence "
13577 : "for the argument is better");
13578 : }
13579 9 : }
13580 : else
13581 12 : add_warning (w, l);
13582 : }
13583 :
13584 62650067 : if (winner)
13585 : return winner;
13586 :
13587 : /* DR 495 moved this tiebreaker above the template ones. */
13588 : /* or, if not that,
13589 : the context is an initialization by user-defined conversion (see
13590 : _dcl.init_ and _over.match.user_) and the standard conversion
13591 : sequence from the return type of F1 to the destination type (i.e.,
13592 : the type of the entity being initialized) is a better conversion
13593 : sequence than the standard conversion sequence from the return type
13594 : of F2 to the destination type. */
13595 :
13596 10286170 : if (cand1->second_conv)
13597 : {
13598 51862 : winner = compare_ics (cand1->second_conv, cand2->second_conv);
13599 51862 : if (winner)
13600 : return winner;
13601 : }
13602 :
13603 : /* CWG2735 (PR109247): A copy/move ctor/op= for which its operand uses an
13604 : explicit conversion (due to list-initialization) is worse. */
13605 10286018 : {
13606 10286018 : z_candidate *sp = nullptr;
13607 10286018 : if (sfk_copy_or_move (cand1->fn))
13608 676 : sp = cand1;
13609 10286018 : if (sfk_copy_or_move (cand2->fn))
13610 884872 : sp = sp ? nullptr : cand2;
13611 10285965 : if (sp)
13612 : {
13613 1770884 : conversion *conv = sp->convs[!DECL_CONSTRUCTOR_P (sp->fn)];
13614 885442 : if (conv->user_conv_p)
13615 2250 : for (; conv; conv = next_conversion (conv))
13616 1900 : if (conv->kind == ck_user
13617 775 : && DECL_P (conv->cand->fn)
13618 2675 : && DECL_NONCONVERTING_P (conv->cand->fn))
13619 425 : return (sp == cand1) ? -1 : 1;
13620 : }
13621 : }
13622 :
13623 : /* DR2327: C++17 copy elision in [over.match.ctor] (direct-init) context.
13624 : The standard currently says that only constructors are candidates, but if
13625 : one copies a prvalue returned by a conversion function we prefer that.
13626 :
13627 : Clang does something similar, as discussed at
13628 : http://lists.isocpp.org/core/2017/10/3166.php
13629 : http://lists.isocpp.org/core/2019/03/5721.php */
13630 6726140 : if (len == 1 && cxx_dialect >= cxx17
13631 6711540 : && DECL_P (cand1->fn)
13632 6711540 : && DECL_COMPLETE_CONSTRUCTOR_P (cand1->fn)
13633 11094069 : && !(cand1->flags & LOOKUP_ONLYCONVERTING))
13634 : {
13635 437519 : bool elided1 = joust_maybe_elide_copy (cand1);
13636 437519 : bool elided2 = joust_maybe_elide_copy (cand2);
13637 437519 : winner = elided1 - elided2;
13638 437519 : if (winner)
13639 : return winner;
13640 : }
13641 :
13642 : /* or, if not that,
13643 : F1 is a non-template function and F2 is a template function
13644 : specialization. */
13645 :
13646 10285360 : if (!cand1->template_decl && cand2->template_decl)
13647 : return 1;
13648 10225501 : else if (cand1->template_decl && !cand2->template_decl)
13649 : return -1;
13650 :
13651 : /* or, if not that,
13652 : F1 and F2 are template functions and the function template for F1 is
13653 : more specialized than the template for F2 according to the partial
13654 : ordering rules. */
13655 :
13656 9080632 : if (cand1->template_decl && cand2->template_decl)
13657 : {
13658 4100972 : winner = more_specialized_fn
13659 4100972 : (TI_TEMPLATE (cand1->template_decl),
13660 4100972 : TI_TEMPLATE (cand2->template_decl),
13661 : /* [temp.func.order]: The presence of unused ellipsis and default
13662 : arguments has no effect on the partial ordering of function
13663 : templates. add_function_candidate() will not have
13664 : counted the "this" argument for constructors. */
13665 8201944 : cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
13666 4100972 : if (winner)
13667 : return winner;
13668 : }
13669 :
13670 : /* F1 and F2 are non-template functions and
13671 : - they have the same non-object-parameter-type-lists ([dcl.fct]), and
13672 : - if they are member functions, both are direct members of the same
13673 : class, and
13674 : - if both are non-static member functions, they have the same types for
13675 : their object parameters, and
13676 : - F1 is more constrained than F2 according to the partial ordering of
13677 : constraints described in [temp.constr.order]. */
13678 6074398 : if (flag_concepts && DECL_P (cand1->fn) && DECL_P (cand2->fn)
13679 6074350 : && !cand1->template_decl && !cand2->template_decl
13680 11054421 : && cand_parms_match (cand1, cand2, pmatch::current))
13681 : {
13682 406781 : winner = more_constrained (cand1->fn, cand2->fn);
13683 406781 : if (winner)
13684 : return winner;
13685 : }
13686 :
13687 : /* F2 is a rewritten candidate (12.4.1.2) and F1 is not, or F1 and F2 are
13688 : rewritten candidates, and F2 is a synthesized candidate with reversed
13689 : order of parameters and F1 is not. */
13690 6078859 : if (cand1->rewritten ())
13691 : {
13692 1259020 : if (!cand2->rewritten ())
13693 : return -1;
13694 803678 : if (!cand1->reversed () && cand2->reversed ())
13695 : return 1;
13696 803678 : if (cand1->reversed () && !cand2->reversed ())
13697 : return -1;
13698 : }
13699 4819839 : else if (cand2->rewritten ())
13700 : return 1;
13701 :
13702 4751296 : if (deduction_guide_p (cand1->fn))
13703 : {
13704 4023 : gcc_assert (deduction_guide_p (cand2->fn));
13705 :
13706 : /* F1 and F2 are generated from class template argument deduction for a
13707 : class D, and F2 is generated from inheriting constructors from a base
13708 : class of D while F1 is not, and for each explicit function argument,
13709 : the corresponding parameters of F1 and F2 are either both ellipses or
13710 : have the same type. */
13711 4023 : bool inherited1 = inherited_guide_p (cand1->fn);
13712 4023 : bool inherited2 = inherited_guide_p (cand2->fn);
13713 4023 : if (int diff = inherited2 - inherited1)
13714 : {
13715 68 : for (i = 0; i < len; ++i)
13716 : {
13717 19 : conversion *t1 = cand1->convs[i + off1];
13718 19 : conversion *t2 = cand2->convs[i + off2];
13719 : /* ??? It seems the ellipses part of this tiebreaker isn't
13720 : needed since a mismatch should have broken the tie earlier
13721 : during ICS comparison. */
13722 19 : gcc_checking_assert (t1->ellipsis_p == t2->ellipsis_p);
13723 19 : if (!same_type_p (t1->type, t2->type))
13724 : break;
13725 : }
13726 51 : if (i == len)
13727 : return diff;
13728 : }
13729 :
13730 : /* F1 is generated from a deduction-guide (13.3.1.8) and F2 is not */
13731 : /* We distinguish between candidates from an explicit deduction guide and
13732 : candidates built from a constructor based on DECL_ARTIFICIAL. */
13733 3974 : int art1 = DECL_ARTIFICIAL (cand1->fn);
13734 3974 : int art2 = DECL_ARTIFICIAL (cand2->fn);
13735 3974 : if (art1 != art2)
13736 1401 : return art2 - art1;
13737 :
13738 2573 : if (art1)
13739 : {
13740 : /* Prefer the special copy guide over a declared copy/move
13741 : constructor. */
13742 2570 : if (copy_guide_p (cand1->fn))
13743 : return 1;
13744 86 : if (copy_guide_p (cand2->fn))
13745 : return -1;
13746 :
13747 : /* Prefer a candidate generated from a non-template constructor. */
13748 28 : int tg1 = template_guide_p (cand1->fn);
13749 28 : int tg2 = template_guide_p (cand2->fn);
13750 28 : if (tg1 != tg2)
13751 6 : return tg2 - tg1;
13752 : }
13753 : }
13754 :
13755 : /* F1 is a constructor for a class D, F2 is a constructor for a base class B
13756 : of D, and for all arguments the corresponding parameters of F1 and F2 have
13757 : the same type (CWG 2273/2277). */
13758 14241739 : if (DECL_INHERITED_CTOR (cand1->fn) || DECL_INHERITED_CTOR (cand2->fn))
13759 : {
13760 87 : tree base1 = DECL_CONTEXT (strip_inheriting_ctors (cand1->fn));
13761 87 : tree base2 = DECL_CONTEXT (strip_inheriting_ctors (cand2->fn));
13762 :
13763 87 : bool used1 = false;
13764 87 : bool used2 = false;
13765 87 : if (base1 == base2)
13766 : /* No difference. */;
13767 87 : else if (DERIVED_FROM_P (base1, base2))
13768 : used1 = true;
13769 18 : else if (DERIVED_FROM_P (base2, base1))
13770 : used2 = true;
13771 :
13772 78 : if (int diff = used2 - used1)
13773 : {
13774 105 : for (i = 0; i < len; ++i)
13775 : {
13776 36 : conversion *t1 = cand1->convs[i + off1];
13777 36 : conversion *t2 = cand2->convs[i + off2];
13778 36 : if (!same_type_p (t1->type, t2->type))
13779 : break;
13780 : }
13781 78 : if (i == len)
13782 : return diff;
13783 : }
13784 : }
13785 :
13786 : /* Check whether we can discard a builtin candidate, either because we
13787 : have two identical ones or matching builtin and non-builtin candidates.
13788 :
13789 : (Pedantically in the latter case the builtin which matched the user
13790 : function should not be added to the overload set, but we spot it here.
13791 :
13792 : [over.match.oper]
13793 : ... the builtin candidates include ...
13794 : - do not have the same parameter type list as any non-template
13795 : non-member candidate. */
13796 :
13797 4747229 : if (identifier_p (cand1->fn) || identifier_p (cand2->fn))
13798 : {
13799 93 : for (i = 0; i < len; ++i)
13800 69 : if (!same_type_p (cand1->convs[i]->type,
13801 : cand2->convs[i]->type))
13802 : break;
13803 43 : if (i == cand1->num_convs)
13804 : {
13805 24 : if (cand1->fn == cand2->fn)
13806 : /* Two built-in candidates; arbitrarily pick one. */
13807 : return 1;
13808 21 : else if (identifier_p (cand1->fn))
13809 : /* cand1 is built-in; prefer cand2. */
13810 21 : return -1;
13811 : else
13812 : /* cand2 is built-in; prefer cand1. */
13813 : return 1;
13814 : }
13815 : }
13816 :
13817 : /* For candidates of a multi-versioned function, make the version with
13818 : the highest priority win. This version will be checked for dispatching
13819 : first. If this version can be inlined into the caller, the front-end
13820 : will simply make a direct call to this function. */
13821 :
13822 4747205 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13823 4747183 : && DECL_FUNCTION_VERSIONED (cand1->fn)
13824 941 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13825 4748146 : && DECL_FUNCTION_VERSIONED (cand2->fn))
13826 : {
13827 941 : tree f1 = TREE_TYPE (cand1->fn);
13828 941 : tree f2 = TREE_TYPE (cand2->fn);
13829 941 : tree p1 = TYPE_ARG_TYPES (f1);
13830 941 : tree p2 = TYPE_ARG_TYPES (f2);
13831 :
13832 : /* Check if cand1->fn and cand2->fn are versions of the same function. It
13833 : is possible that cand1->fn and cand2->fn are function versions but of
13834 : different functions. Check types to see if they are versions of the same
13835 : function. */
13836 941 : if (compparms (p1, p2)
13837 941 : && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
13838 : {
13839 : /* Always make the version with the higher priority, more
13840 : specialized, win. */
13841 941 : gcc_assert (targetm.compare_version_priority);
13842 941 : if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
13843 : return 1;
13844 : else
13845 254 : return -1;
13846 : }
13847 : }
13848 :
13849 : /* If the two function declarations represent the same function (this can
13850 : happen with declarations in multiple scopes and arg-dependent lookup),
13851 : arbitrarily choose one. But first make sure the default args we're
13852 : using match. */
13853 4746242 : if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
13854 9492506 : && equal_functions (cand1->fn, cand2->fn))
13855 : {
13856 35 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
13857 35 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
13858 :
13859 70 : gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
13860 :
13861 50 : for (i = 0; i < len; ++i)
13862 : {
13863 : /* Don't crash if the fn is variadic. */
13864 15 : if (!parms1)
13865 : break;
13866 15 : parms1 = TREE_CHAIN (parms1);
13867 15 : parms2 = TREE_CHAIN (parms2);
13868 : }
13869 :
13870 35 : if (off1)
13871 0 : parms1 = TREE_CHAIN (parms1);
13872 35 : else if (off2)
13873 0 : parms2 = TREE_CHAIN (parms2);
13874 :
13875 69 : for (; parms1; ++i)
13876 : {
13877 80 : if (!cp_tree_equal (TREE_PURPOSE (parms1),
13878 40 : TREE_PURPOSE (parms2)))
13879 : {
13880 6 : if (warn)
13881 : {
13882 3 : if (complain & tf_error)
13883 : {
13884 3 : auto_diagnostic_group d;
13885 3 : if (permerror (input_location,
13886 : "default argument mismatch in "
13887 : "overload resolution"))
13888 : {
13889 3 : inform (DECL_SOURCE_LOCATION (cand1->fn),
13890 : " candidate 1: %q#F", cand1->fn);
13891 3 : inform (DECL_SOURCE_LOCATION (cand2->fn),
13892 : " candidate 2: %q#F", cand2->fn);
13893 : }
13894 3 : }
13895 : else
13896 : return 0;
13897 : }
13898 : else
13899 3 : add_warning (cand1, cand2);
13900 : break;
13901 : }
13902 34 : parms1 = TREE_CHAIN (parms1);
13903 34 : parms2 = TREE_CHAIN (parms2);
13904 : }
13905 :
13906 : return 1;
13907 : }
13908 :
13909 4747233 : tweak:
13910 :
13911 : /* Extension: If the worst conversion for one candidate is better than the
13912 : worst conversion for the other, take the first. */
13913 4747233 : if (!pedantic && (complain & tf_warning_or_error))
13914 : {
13915 : conversion_rank rank1 = cr_identity, rank2 = cr_identity;
13916 10228302 : struct z_candidate *w = 0, *l = 0;
13917 :
13918 10228302 : for (i = 0; i < len; ++i)
13919 : {
13920 5727734 : if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
13921 4414057 : rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
13922 5727734 : if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
13923 4414074 : rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
13924 : }
13925 4500568 : if (rank1 < rank2)
13926 56 : winner = 1, w = cand1, l = cand2;
13927 4500568 : if (rank1 > rank2)
13928 : winner = -1, w = cand2, l = cand1;
13929 4500460 : if (winner)
13930 : {
13931 : /* Don't choose a deleted function over ambiguity. */
13932 164 : if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
13933 : return 0;
13934 164 : if (warn)
13935 : {
13936 6 : auto_diagnostic_group d;
13937 6 : if (pedwarn (input_location, 0,
13938 : "ISO C++ says that these are ambiguous, even "
13939 : "though the worst conversion for the first is "
13940 : "better than the worst conversion for the second:"))
13941 : {
13942 3 : print_z_candidate (input_location, N_("candidate 1:"), w);
13943 3 : print_z_candidate (input_location, N_("candidate 2:"), l);
13944 : }
13945 6 : }
13946 : else
13947 158 : add_warning (w, l);
13948 : return winner;
13949 : }
13950 : }
13951 :
13952 : gcc_assert (!winner);
13953 : return 0;
13954 : }
13955 :
13956 : /* Given a list of candidates for overloading, find the best one, if any.
13957 : This algorithm has a worst case of O(2n) (winner is last), and a best
13958 : case of O(n/2) (totally ambiguous); much better than a sorting
13959 : algorithm. The candidates list is assumed to be sorted according
13960 : to viability (via splice_viable). */
13961 :
13962 : static struct z_candidate *
13963 243939623 : tourney (struct z_candidate *candidates, tsubst_flags_t complain)
13964 : {
13965 243939623 : struct z_candidate **champ = &candidates, **challenger;
13966 243939623 : int fate;
13967 243939623 : struct z_candidate *previous_worse_champ = nullptr;
13968 :
13969 : /* Walk through the list once, comparing each current champ to the next
13970 : candidate, knocking out a candidate or two with each comparison. */
13971 :
13972 311057096 : for (challenger = &candidates->next; *challenger && (*challenger)->viable; )
13973 : {
13974 67130227 : fate = joust (*champ, *challenger, 0, complain);
13975 67130227 : if (fate == 1)
13976 43489334 : challenger = &(*challenger)->next;
13977 23640893 : else if (fate == -1)
13978 : {
13979 18895530 : previous_worse_champ = *champ;
13980 18895530 : champ = challenger;
13981 18895530 : challenger = &(*challenger)->next;
13982 : }
13983 : else
13984 : {
13985 4745363 : previous_worse_champ = nullptr;
13986 4745363 : champ = &(*challenger)->next;
13987 4745363 : if (!*champ || !(*champ)->viable
13988 4732703 : || (*champ)->viable < (*challenger)->viable)
13989 : {
13990 : champ = nullptr;
13991 : break;
13992 : }
13993 4732609 : challenger = &(*champ)->next;
13994 : }
13995 : }
13996 :
13997 : /* Make sure the champ is better than all the candidates it hasn't yet
13998 : been compared to. */
13999 :
14000 243939623 : if (champ)
14001 28642200 : for (challenger = &candidates;
14002 272569069 : challenger != champ;
14003 28642200 : challenger = &(*challenger)->next)
14004 : {
14005 28644106 : if (*challenger == previous_worse_champ)
14006 : /* We already know this candidate is worse than the champ. */
14007 17690952 : continue;
14008 10953154 : fate = joust (*champ, *challenger, 0, complain);
14009 10953154 : if (fate != 1)
14010 : {
14011 : champ = nullptr;
14012 : break;
14013 : }
14014 : }
14015 :
14016 243926869 : if (!champ)
14017 : return nullptr;
14018 :
14019 : /* Move the champ to the front of the candidate list. */
14020 :
14021 243924963 : if (champ != &candidates)
14022 : {
14023 19371002 : z_candidate *saved_champ = *champ;
14024 19371002 : *champ = saved_champ->next;
14025 19371002 : saved_champ->next = candidates;
14026 19371002 : candidates = saved_champ;
14027 : }
14028 :
14029 243924963 : return candidates;
14030 : }
14031 :
14032 : /* Returns nonzero if things of type FROM can be converted to TO. */
14033 :
14034 : bool
14035 4492264 : can_convert (tree to, tree from, tsubst_flags_t complain)
14036 : {
14037 4492264 : tree arg = NULL_TREE;
14038 : /* implicit_conversion only considers user-defined conversions
14039 : if it has an expression for the call argument list. */
14040 4492264 : if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to))
14041 111 : arg = build_stub_object (from);
14042 4492264 : return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain);
14043 : }
14044 :
14045 : /* Returns nonzero if things of type FROM can be converted to TO with a
14046 : standard conversion. */
14047 :
14048 : bool
14049 263 : can_convert_standard (tree to, tree from, tsubst_flags_t complain)
14050 : {
14051 263 : return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
14052 : }
14053 :
14054 : /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
14055 :
14056 : bool
14057 5489870 : can_convert_arg (tree to, tree from, tree arg, int flags,
14058 : tsubst_flags_t complain)
14059 : {
14060 5489870 : conversion *t;
14061 5489870 : bool ok_p;
14062 :
14063 5489870 : conversion_obstack_sentinel cos;
14064 : /* We want to discard any access checks done for this test,
14065 : as we might not be in the appropriate access context and
14066 : we'll do the check again when we actually perform the
14067 : conversion. */
14068 5489870 : push_deferring_access_checks (dk_deferred);
14069 :
14070 : /* Handle callers like check_local_shadow forgetting to
14071 : convert_from_reference. */
14072 5489870 : if (TYPE_REF_P (from) && arg)
14073 : {
14074 61 : arg = convert_from_reference (arg);
14075 61 : from = TREE_TYPE (arg);
14076 : }
14077 :
14078 5489870 : t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
14079 : flags, complain);
14080 5489870 : ok_p = (t && !t->bad_p);
14081 :
14082 : /* Discard the access checks now. */
14083 5489870 : pop_deferring_access_checks ();
14084 :
14085 10979740 : return ok_p;
14086 5489870 : }
14087 :
14088 : /* Like can_convert_arg, but allows dubious conversions as well. */
14089 :
14090 : bool
14091 184337478 : can_convert_arg_bad (tree to, tree from, tree arg, int flags,
14092 : tsubst_flags_t complain)
14093 : {
14094 184337478 : conversion *t;
14095 :
14096 184337478 : conversion_obstack_sentinel cos;
14097 : /* Try to perform the conversion. */
14098 184337478 : t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
14099 : flags, complain);
14100 :
14101 368674956 : return t != NULL;
14102 184337478 : }
14103 :
14104 : /* Return an IMPLICIT_CONV_EXPR from EXPR to TYPE with bits set from overload
14105 : resolution FLAGS. */
14106 :
14107 : tree
14108 14557768 : build_implicit_conv_flags (tree type, tree expr, int flags)
14109 : {
14110 : /* In a template, we are only concerned about determining the
14111 : type of non-dependent expressions, so we do not have to
14112 : perform the actual conversion. But for initializers, we
14113 : need to be able to perform it at instantiation
14114 : (or instantiate_non_dependent_expr) time. */
14115 14557768 : expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
14116 14557768 : if (!(flags & LOOKUP_ONLYCONVERTING))
14117 6678202 : IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
14118 14557768 : if (flags & LOOKUP_NO_NARROWING)
14119 23456 : IMPLICIT_CONV_EXPR_BRACED_INIT (expr) = true;
14120 14557768 : return expr;
14121 : }
14122 :
14123 : /* Convert EXPR to TYPE. Return the converted expression.
14124 :
14125 : Note that we allow bad conversions here because by the time we get to
14126 : this point we are committed to doing the conversion. If we end up
14127 : doing a bad conversion, convert_like will complain. */
14128 :
14129 : tree
14130 299114956 : perform_implicit_conversion_flags (tree type, tree expr,
14131 : tsubst_flags_t complain, int flags)
14132 : {
14133 299114956 : conversion *conv;
14134 299114956 : location_t loc = cp_expr_loc_or_input_loc (expr);
14135 :
14136 299114956 : if (error_operand_p (expr))
14137 450 : return error_mark_node;
14138 :
14139 299114506 : conversion_obstack_sentinel cos;
14140 :
14141 299114506 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
14142 : /*c_cast_p=*/false,
14143 : flags, complain);
14144 :
14145 299114506 : if (!conv)
14146 : {
14147 262437 : if (complain & tf_error)
14148 488 : implicit_conversion_error (loc, type, expr, flags);
14149 262437 : expr = error_mark_node;
14150 : }
14151 298852069 : else if (processing_template_decl && conv->kind != ck_identity)
14152 14557768 : expr = build_implicit_conv_flags (type, expr, flags);
14153 : else
14154 : {
14155 : /* Give a conversion call the same location as expr. */
14156 284294301 : iloc_sentinel il (loc);
14157 284294301 : expr = convert_like (conv, expr, complain);
14158 284294301 : }
14159 :
14160 299114506 : return expr;
14161 299114506 : }
14162 :
14163 : tree
14164 13044696 : perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
14165 : {
14166 13044696 : return perform_implicit_conversion_flags (type, expr, complain,
14167 13044696 : LOOKUP_IMPLICIT);
14168 : }
14169 :
14170 : /* Convert EXPR to TYPE (as a direct-initialization) if that is
14171 : permitted. If the conversion is valid, the converted expression is
14172 : returned. Otherwise, NULL_TREE is returned, except in the case
14173 : that TYPE is a class type; in that case, an error is issued. If
14174 : C_CAST_P is true, then this direct-initialization is taking
14175 : place as part of a static_cast being attempted as part of a C-style
14176 : cast. */
14177 :
14178 : tree
14179 49088424 : perform_direct_initialization_if_possible (tree type,
14180 : tree expr,
14181 : bool c_cast_p,
14182 : tsubst_flags_t complain)
14183 : {
14184 49088424 : conversion *conv;
14185 :
14186 49088424 : if (type == error_mark_node || error_operand_p (expr))
14187 : return error_mark_node;
14188 : /* [dcl.init]
14189 :
14190 : If the destination type is a (possibly cv-qualified) class type:
14191 :
14192 : -- If the initialization is direct-initialization ...,
14193 : constructors are considered.
14194 :
14195 : -- If overload resolution is successful, the selected constructor
14196 : is called to initialize the object, with the initializer expression
14197 : or expression-list as its argument(s).
14198 :
14199 : -- Otherwise, if no constructor is viable, the destination type is
14200 : a (possibly cv-qualified) aggregate class A, and the initializer is
14201 : a parenthesized expression-list, the object is initialized as
14202 : follows... */
14203 49088424 : if (CLASS_TYPE_P (type))
14204 : {
14205 3235660 : releasing_vec args (make_tree_vector_single (expr));
14206 3235660 : expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
14207 : &args, type, LOOKUP_NORMAL, complain);
14208 3235660 : return build_cplus_new (type, expr, complain);
14209 3235660 : }
14210 :
14211 45852764 : conversion_obstack_sentinel cos;
14212 :
14213 45852764 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
14214 : c_cast_p,
14215 : LOOKUP_NORMAL, complain);
14216 45852764 : if (!conv || conv->bad_p)
14217 : expr = NULL_TREE;
14218 41057691 : else if (processing_template_decl && conv->kind != ck_identity)
14219 : {
14220 : /* In a template, we are only concerned about determining the
14221 : type of non-dependent expressions, so we do not have to
14222 : perform the actual conversion. But for initializers, we
14223 : need to be able to perform it at instantiation
14224 : (or instantiate_non_dependent_expr) time. */
14225 706586 : expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
14226 706586 : IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
14227 : }
14228 : else
14229 40351105 : expr = convert_like (conv, expr, NULL_TREE, 0,
14230 : /*issue_conversion_warnings=*/false,
14231 : c_cast_p, /*nested_p=*/false, complain);
14232 :
14233 45852764 : return expr;
14234 45852764 : }
14235 :
14236 : /* When initializing a reference that lasts longer than a full-expression,
14237 : this special rule applies:
14238 :
14239 : [class.temporary]
14240 :
14241 : The temporary to which the reference is bound or the temporary
14242 : that is the complete object to which the reference is bound
14243 : persists for the lifetime of the reference.
14244 :
14245 : The temporaries created during the evaluation of the expression
14246 : initializing the reference, except the temporary to which the
14247 : reference is bound, are destroyed at the end of the
14248 : full-expression in which they are created.
14249 :
14250 : In that case, we store the converted expression into a new
14251 : VAR_DECL in a new scope.
14252 :
14253 : However, we want to be careful not to create temporaries when
14254 : they are not required. For example, given:
14255 :
14256 : struct B {};
14257 : struct D : public B {};
14258 : D f();
14259 : const B& b = f();
14260 :
14261 : there is no need to copy the return value from "f"; we can just
14262 : extend its lifetime. Similarly, given:
14263 :
14264 : struct S {};
14265 : struct T { operator S(); };
14266 : T t;
14267 : const S& s = t;
14268 :
14269 : we can extend the lifetime of the return value of the conversion
14270 : operator.
14271 :
14272 : The next several functions are involved in this lifetime extension. */
14273 :
14274 : /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
14275 : reference is being bound to a temporary. Create and return a new
14276 : VAR_DECL with the indicated TYPE; this variable will store the value to
14277 : which the reference is bound. */
14278 :
14279 : tree
14280 9318 : make_temporary_var_for_ref_to_temp (tree decl, tree type)
14281 : {
14282 9318 : tree var = create_temporary_var (type);
14283 :
14284 : /* Register the variable. */
14285 9318 : if (VAR_P (decl)
14286 9318 : && (TREE_STATIC (decl) || CP_DECL_THREAD_LOCAL_P (decl)))
14287 : {
14288 : /* Namespace-scope or local static; give it a mangled name. */
14289 :
14290 : /* If an initializer is visible to multiple translation units, those
14291 : translation units must agree on the addresses of the
14292 : temporaries. Therefore the temporaries must be given a consistent name
14293 : and vague linkage. The mangled name of a temporary is the name of the
14294 : non-temporary object in whose initializer they appear, prefixed with
14295 : GR and suffixed with a sequence number mangled using the usual rules
14296 : for a seq-id. Temporaries are numbered with a pre-order, depth-first,
14297 : left-to-right walk of the complete initializer. */
14298 820 : copy_linkage (var, decl);
14299 :
14300 820 : tree name = mangle_ref_init_variable (decl);
14301 820 : DECL_NAME (var) = name;
14302 820 : SET_DECL_ASSEMBLER_NAME (var, name);
14303 :
14304 : /* Set the context to make the variable mergeable in modules. */
14305 820 : DECL_CONTEXT (var) = current_scope ();
14306 : }
14307 : else
14308 : /* Create a new cleanup level if necessary. */
14309 8498 : maybe_push_cleanup_level (type);
14310 :
14311 9318 : return pushdecl (var);
14312 : }
14313 :
14314 : static tree extend_temps_r (tree *, int *, void *);
14315 :
14316 : /* EXPR is the initializer for a variable DECL of reference or
14317 : std::initializer_list type. Create, push and return a new VAR_DECL
14318 : for the initializer so that it will live as long as DECL. Any
14319 : cleanup for the new variable is returned through CLEANUP, and the
14320 : code to initialize the new variable is returned through INITP. */
14321 :
14322 : static tree
14323 9318 : set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
14324 : tree *initp, tree *cond_guard,
14325 : void *walk_data)
14326 : {
14327 9318 : tree init;
14328 9318 : tree type;
14329 9318 : tree var;
14330 :
14331 : /* Create the temporary variable. */
14332 9318 : type = TREE_TYPE (expr);
14333 9318 : var = make_temporary_var_for_ref_to_temp (decl, type);
14334 9318 : layout_decl (var, 0);
14335 : /* If the rvalue is the result of a function call it will be
14336 : a TARGET_EXPR. If it is some other construct (such as a
14337 : member access expression where the underlying object is
14338 : itself the result of a function call), turn it into a
14339 : TARGET_EXPR here. It is important that EXPR be a
14340 : TARGET_EXPR below since otherwise the INIT_EXPR will
14341 : attempt to make a bitwise copy of EXPR to initialize
14342 : VAR. */
14343 9318 : if (TREE_CODE (expr) != TARGET_EXPR)
14344 0 : expr = get_target_expr (expr);
14345 : else
14346 : {
14347 9318 : if (TREE_ADDRESSABLE (expr))
14348 9235 : TREE_ADDRESSABLE (var) = 1;
14349 9318 : if (DECL_MERGEABLE (TARGET_EXPR_SLOT (expr)))
14350 633 : DECL_MERGEABLE (var) = true;
14351 : }
14352 :
14353 9318 : if (TREE_CODE (decl) == FIELD_DECL
14354 9318 : && extra_warnings && !warning_suppressed_p (decl))
14355 : {
14356 3 : warning (OPT_Wextra, "a temporary bound to %qD only persists "
14357 : "until the constructor exits", decl);
14358 3 : suppress_warning (decl);
14359 : }
14360 :
14361 : /* Recursively extend temps in this initializer. The recursion needs to come
14362 : after creating the variable to conform to the mangling ABI, and before
14363 : maybe_constant_init because the extension might change its result. */
14364 9318 : if (walk_data)
14365 2184 : cp_walk_tree (&TARGET_EXPR_INITIAL (expr), extend_temps_r,
14366 : walk_data, nullptr);
14367 : else
14368 7134 : TARGET_EXPR_INITIAL (expr)
14369 14268 : = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups,
14370 : cond_guard);
14371 :
14372 : /* Any reference temp has a non-trivial initializer. */
14373 9318 : DECL_NONTRIVIALLY_INITIALIZED_P (var) = true;
14374 :
14375 : /* If the initializer is constant, put it in DECL_INITIAL so we get
14376 : static initialization and use in constant expressions. */
14377 9318 : init = maybe_constant_init (expr, var, /*manifestly_const_eval=*/true);
14378 : /* As in store_init_value. */
14379 9318 : init = cp_fully_fold (init);
14380 9318 : if (TREE_CONSTANT (init))
14381 : {
14382 1287 : if (literal_type_p (type)
14383 1261 : && CP_TYPE_CONST_NON_VOLATILE_P (type)
14384 2040 : && !TYPE_HAS_MUTABLE_P (type))
14385 : {
14386 : /* 5.19 says that a constant expression can include an
14387 : lvalue-rvalue conversion applied to "a glvalue of literal type
14388 : that refers to a non-volatile temporary object initialized
14389 : with a constant expression". Rather than try to communicate
14390 : that this VAR_DECL is a temporary, just mark it constexpr. */
14391 750 : DECL_DECLARED_CONSTEXPR_P (var) = true;
14392 750 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
14393 750 : TREE_CONSTANT (var) = true;
14394 750 : TREE_READONLY (var) = true;
14395 : }
14396 1287 : DECL_INITIAL (var) = init;
14397 1287 : init = NULL_TREE;
14398 : }
14399 : else
14400 : /* Create the INIT_EXPR that will initialize the temporary
14401 : variable. */
14402 8031 : init = split_nonconstant_init (var, expr);
14403 9318 : if (at_function_scope_p ())
14404 : {
14405 8590 : add_decl_expr (var);
14406 :
14407 8590 : if (TREE_STATIC (var))
14408 92 : init = add_stmt_to_compound (init, register_dtor_fn (var));
14409 : else
14410 : {
14411 : /* ??? Instead of rebuilding the cleanup, we could replace the slot
14412 : with var in TARGET_EXPR_CLEANUP (expr). */
14413 8498 : tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
14414 8498 : if (cleanup)
14415 : {
14416 3509 : if (cond_guard && cleanup != error_mark_node)
14417 : {
14418 23 : if (*cond_guard == NULL_TREE)
14419 : {
14420 23 : *cond_guard = build_local_temp (boolean_type_node);
14421 23 : add_decl_expr (*cond_guard);
14422 23 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION,
14423 : *cond_guard, NOP_EXPR,
14424 : boolean_false_node,
14425 : tf_warning_or_error);
14426 23 : finish_expr_stmt (set);
14427 : }
14428 23 : cleanup = build3 (COND_EXPR, void_type_node,
14429 : *cond_guard, cleanup, NULL_TREE);
14430 : }
14431 3509 : if (flag_exceptions && TREE_CODE (TREE_TYPE (var)) != ARRAY_TYPE)
14432 : {
14433 : /* The normal cleanup for this extended variable isn't pushed
14434 : until cp_finish_decl, so we need to retain a TARGET_EXPR
14435 : to clean it up in case a later initializer throws
14436 : (g++.dg/eh/ref-temp3.C).
14437 :
14438 : We don't do this for array temporaries because they have
14439 : the array cleanup region from build_vec_init.
14440 :
14441 : Unlike maybe_push_temp_cleanup, we don't actually need a
14442 : flag, but a TARGET_EXPR needs a TARGET_EXPR_SLOT.
14443 : Perhaps this could use WITH_CLEANUP_EXPR instead, but
14444 : gimplify.cc doesn't handle that, and front-end handling
14445 : was removed in r8-1725 and r8-1818.
14446 :
14447 : Alternately it might be preferable to flatten an
14448 : initialization with extended temps into a sequence of
14449 : (non-full-expression) statements, so we could immediately
14450 : push_cleanup here for only a single cleanup region, but we
14451 : don't have a mechanism for that in the front-end, only the
14452 : gimplifier. */
14453 3401 : tree targ = get_internal_target_expr (boolean_true_node);
14454 3401 : TARGET_EXPR_CLEANUP (targ) = cleanup;
14455 3401 : CLEANUP_EH_ONLY (targ) = true;
14456 : /* Don't actually initialize the bool. */
14457 3401 : init = (!init ? void_node
14458 3375 : : convert_to_void (init, ICV_STATEMENT, tf_none));
14459 3401 : TARGET_EXPR_INITIAL (targ) = init;
14460 3401 : init = targ;
14461 : }
14462 3509 : vec_safe_push (*cleanups, cleanup);
14463 : }
14464 : }
14465 :
14466 : /* We must be careful to destroy the temporary only
14467 : after its initialization has taken place. If the
14468 : initialization throws an exception, then the
14469 : destructor should not be run. We cannot simply
14470 : transform INIT into something like:
14471 :
14472 : (INIT, ({ CLEANUP_STMT; }))
14473 :
14474 : because emit_local_var always treats the
14475 : initializer as a full-expression. Thus, the
14476 : destructor would run too early; it would run at the
14477 : end of initializing the reference variable, rather
14478 : than at the end of the block enclosing the
14479 : reference variable.
14480 :
14481 : The solution is to pass back a cleanup expression
14482 : which the caller is responsible for attaching to
14483 : the statement tree. */
14484 : }
14485 : else
14486 : {
14487 : /* Don't output reflection variables. */
14488 728 : if (consteval_only_p (var))
14489 2 : DECL_EXTERNAL (var) = true;
14490 :
14491 728 : rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
14492 728 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
14493 : {
14494 43 : if (CP_DECL_THREAD_LOCAL_P (var))
14495 12 : tls_aggregates = tree_cons (NULL_TREE, var,
14496 : tls_aggregates);
14497 : else
14498 31 : static_aggregates = tree_cons (NULL_TREE, var,
14499 : static_aggregates);
14500 : }
14501 : else
14502 : /* Check whether the dtor is callable. */
14503 685 : cxx_maybe_build_cleanup (var, tf_warning_or_error);
14504 : }
14505 : /* Avoid -Wunused-variable warning (c++/38958). */
14506 9318 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
14507 9318 : && VAR_P (decl))
14508 3483 : TREE_USED (decl) = DECL_READ_P (decl) = true;
14509 :
14510 9318 : *initp = init;
14511 9318 : return var;
14512 : }
14513 :
14514 : /* Convert EXPR to the indicated reference TYPE, in a way suitable for
14515 : initializing a variable of that TYPE. */
14516 :
14517 : tree
14518 6503168 : initialize_reference (tree type, tree expr,
14519 : int flags, tsubst_flags_t complain)
14520 : {
14521 6503168 : conversion *conv;
14522 6503168 : location_t loc = cp_expr_loc_or_input_loc (expr);
14523 :
14524 6503168 : if (type == error_mark_node || error_operand_p (expr))
14525 : return error_mark_node;
14526 :
14527 6503091 : conversion_obstack_sentinel cos;
14528 :
14529 6503091 : conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
14530 : flags, complain);
14531 : /* If this conversion failed, we're in C++20, and we have something like
14532 : A& a(b) where A is an aggregate, try again, this time as A& a{b}. */
14533 6503091 : if ((!conv || conv->bad_p)
14534 725 : && (flags & LOOKUP_AGGREGATE_PAREN_INIT))
14535 : {
14536 4 : tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
14537 4 : CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
14538 4 : CONSTRUCTOR_IS_PAREN_INIT (e) = true;
14539 4 : conversion *c = reference_binding (type, TREE_TYPE (e), e,
14540 : /*c_cast_p=*/false, flags, complain);
14541 : /* If this worked, use it. */
14542 4 : if (c && !c->bad_p)
14543 : expr = e, conv = c;
14544 : }
14545 6503091 : if (!conv || conv->bad_p)
14546 : {
14547 722 : if (complain & tf_error)
14548 : {
14549 366 : if (conv)
14550 313 : convert_like (conv, expr, complain);
14551 53 : else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
14552 21 : && !TYPE_REF_IS_RVALUE (type)
14553 74 : && !lvalue_p (expr))
14554 6 : error_at (loc, "invalid initialization of non-const reference of "
14555 : "type %qH from an rvalue of type %qI",
14556 6 : type, TREE_TYPE (expr));
14557 : else
14558 47 : error_at (loc, "invalid initialization of reference of type "
14559 : "%qH from expression of type %qI", type,
14560 47 : TREE_TYPE (expr));
14561 : }
14562 722 : return error_mark_node;
14563 : }
14564 :
14565 6502369 : if (conv->kind == ck_ref_bind)
14566 : /* Perform the conversion. */
14567 6502366 : expr = convert_like (conv, expr, complain);
14568 3 : else if (conv->kind == ck_ambig)
14569 : /* We gave an error in build_user_type_conversion_1. */
14570 3 : expr = error_mark_node;
14571 : else
14572 0 : gcc_unreachable ();
14573 :
14574 : return expr;
14575 6503091 : }
14576 :
14577 : /* Return true if T is std::pair<const T&, const T&>. */
14578 :
14579 : static bool
14580 617342 : std_pair_ref_ref_p (tree t)
14581 : {
14582 : /* First, check if we have std::pair. */
14583 58575 : if (!NON_UNION_CLASS_TYPE_P (t)
14584 675457 : || !CLASSTYPE_TEMPLATE_INSTANTIATION (t))
14585 : return false;
14586 21195 : tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (t));
14587 21195 : if (!decl_in_std_namespace_p (tdecl))
14588 : return false;
14589 15886 : tree name = DECL_NAME (tdecl);
14590 15886 : if (!name || !id_equal (name, "pair"))
14591 : return false;
14592 :
14593 : /* Now see if the template arguments are both const T&. */
14594 361 : tree args = CLASSTYPE_TI_ARGS (t);
14595 361 : if (TREE_VEC_LENGTH (args) != 2)
14596 : return false;
14597 421 : for (int i = 0; i < 2; i++)
14598 451 : if (!TYPE_REF_OBJ_P (TREE_VEC_ELT (args, i))
14599 451 : || !CP_TYPE_CONST_P (TREE_TYPE (TREE_VEC_ELT (args, i))))
14600 : return false;
14601 :
14602 : return true;
14603 : }
14604 :
14605 : /* Return true if a class T has a reference member. */
14606 :
14607 : static bool
14608 216 : class_has_reference_member_p (tree t)
14609 : {
14610 216 : for (tree fields = TYPE_FIELDS (t);
14611 3380 : fields;
14612 3164 : fields = DECL_CHAIN (fields))
14613 3227 : if (TREE_CODE (fields) == FIELD_DECL
14614 196 : && !DECL_ARTIFICIAL (fields)
14615 3394 : && TYPE_REF_P (TREE_TYPE (fields)))
14616 : return true;
14617 : return false;
14618 : }
14619 :
14620 : /* A wrapper for the above suitable as a callback for dfs_walk_once. */
14621 :
14622 : static tree
14623 216 : class_has_reference_member_p_r (tree binfo, void *)
14624 : {
14625 216 : return (class_has_reference_member_p (BINFO_TYPE (binfo))
14626 216 : ? integer_one_node : NULL_TREE);
14627 : }
14628 :
14629 :
14630 : /* Return true if T (either a class or a function) has been marked as
14631 : not-dangling. */
14632 :
14633 : static bool
14634 1557 : no_dangling_p (tree t)
14635 : {
14636 1557 : t = lookup_attribute ("no_dangling", TYPE_ATTRIBUTES (t));
14637 1557 : if (!t)
14638 : return false;
14639 :
14640 75 : t = TREE_VALUE (t);
14641 75 : if (!t)
14642 : return true;
14643 :
14644 51 : t = build_converted_constant_bool_expr (TREE_VALUE (t), tf_warning_or_error);
14645 51 : t = cxx_constant_value (t);
14646 51 : return t == boolean_true_node;
14647 : }
14648 :
14649 : /* Return true if a class CTYPE is either std::reference_wrapper or
14650 : std::ref_view, or a reference wrapper class. We consider a class
14651 : a reference wrapper class if it has a reference member. We no
14652 : longer check that it has a constructor taking the same reference type
14653 : since that approach still generated too many false positives. */
14654 :
14655 : static bool
14656 447 : reference_like_class_p (tree ctype)
14657 : {
14658 447 : if (!CLASS_TYPE_P (ctype))
14659 : return false;
14660 :
14661 314 : if (no_dangling_p (ctype))
14662 : return true;
14663 :
14664 : /* Also accept a std::pair<const T&, const T&>. */
14665 290 : if (std_pair_ref_ref_p (ctype))
14666 : return true;
14667 :
14668 275 : tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (ctype));
14669 275 : if (decl_in_std_namespace_p (tdecl))
14670 : {
14671 38 : tree name = DECL_NAME (tdecl);
14672 38 : if (name
14673 38 : && (id_equal (name, "reference_wrapper")
14674 26 : || id_equal (name, "span")
14675 11 : || id_equal (name, "ref_view")))
14676 : return true;
14677 : }
14678 :
14679 : /* Avoid warning if CTYPE looks like std::span: it has a T* member and
14680 : a trivial destructor. For example,
14681 :
14682 : template<typename T>
14683 : struct Span {
14684 : T* data_;
14685 : std::size len_;
14686 : };
14687 :
14688 : is considered std::span-like. */
14689 248 : if (NON_UNION_CLASS_TYPE_P (ctype) && TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
14690 228 : for (tree field = next_aggregate_field (TYPE_FIELDS (ctype));
14691 367 : field; field = next_aggregate_field (DECL_CHAIN (field)))
14692 199 : if (TYPE_PTR_P (TREE_TYPE (field)))
14693 : return true;
14694 :
14695 : /* Some classes, such as std::tuple, have the reference member in its
14696 : (non-direct) base class. */
14697 188 : if (dfs_walk_once (TYPE_BINFO (ctype), class_has_reference_member_p_r,
14698 : nullptr, nullptr))
14699 63 : return true;
14700 :
14701 : return false;
14702 : }
14703 :
14704 : /* Helper for maybe_warn_dangling_reference to find a problematic temporary
14705 : in EXPR (as outlined in maybe_warn_dangling_reference), or NULL_TREE
14706 : if none found. For instance:
14707 :
14708 : const S& s = S().self(); // S()
14709 : const int& r = (42, f(1)); // temporary for passing 1 to f
14710 : const int& t = b ? f(1) : f(2); // temporary for 1
14711 : const int& u = b ? f(1) : f(g); // temporary for 1
14712 : const int& v = b ? f(g) : f(2); // temporary for 2
14713 : const int& w = b ? f(g) : f(g); // NULL_TREE
14714 : const int& y = (f(1), 42); // NULL_TREE
14715 : const int& z = f(f(1)); // temporary for 1
14716 :
14717 : EXPR is the initializer. If ARG_P is true, we're processing an argument
14718 : to a function; the point is to distinguish between, for example,
14719 :
14720 : Ref::inner (&TARGET_EXPR <D.2839, F::foo (fm)>)
14721 :
14722 : where we shouldn't warn, and
14723 :
14724 : Ref::inner (&TARGET_EXPR <D.2908, F::foo (&TARGET_EXPR <...>)>)
14725 :
14726 : where we should warn (Ref is a reference_like_class_p so we see through
14727 : it. */
14728 :
14729 : static tree
14730 3715 : do_warn_dangling_reference (tree expr, bool arg_p)
14731 : {
14732 3881 : STRIP_NOPS (expr);
14733 :
14734 3881 : if (arg_p && expr_represents_temporary_p (expr))
14735 : {
14736 : /* An attempt to reduce the number of -Wdangling-reference
14737 : false positives concerning reference wrappers (c++/107532).
14738 : When we encounter a reference_like_class_p, we don't warn
14739 : just yet; instead, we keep recursing to see if there were
14740 : any temporaries behind the reference-wrapper class. */
14741 : tree e = expr;
14742 355 : while (handled_component_p (e))
14743 15 : e = TREE_OPERAND (e, 0);
14744 340 : tree type = TREE_TYPE (e);
14745 : /* If the temporary represents a lambda, we don't really know
14746 : what's going on here. */
14747 462 : if (!reference_like_class_p (type) && !LAMBDA_TYPE_P (type))
14748 : return expr;
14749 : }
14750 :
14751 3648 : switch (TREE_CODE (expr))
14752 : {
14753 1822 : case CALL_EXPR:
14754 1822 : {
14755 1822 : tree fndecl = cp_get_callee_fndecl_nofold (expr);
14756 1822 : if (!fndecl
14757 1822 : || warning_suppressed_p (fndecl, OPT_Wdangling_reference)
14758 2055 : || !warning_enabled_at (DECL_SOURCE_LOCATION (fndecl),
14759 1811 : OPT_Wdangling_reference)
14760 : /* Don't emit a false positive for:
14761 : std::vector<int> v = ...;
14762 : std::vector<int>::const_iterator it = v.begin();
14763 : const int &r = *it++;
14764 : because R refers to one of the int elements of V, not to
14765 : a temporary object. Member operator* may return a reference
14766 : but probably not to one of its arguments. */
14767 1452 : || (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl)
14768 852 : && DECL_OVERLOADED_OPERATOR_P (fndecl)
14769 319 : && DECL_OVERLOADED_OPERATOR_IS (fndecl, INDIRECT_REF))
14770 3065 : || no_dangling_p (TREE_TYPE (fndecl)))
14771 : return NULL_TREE;
14772 :
14773 1219 : tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
14774 : /* If the function doesn't return a reference, don't warn. This
14775 : can be e.g.
14776 : const int& z = std::min({1, 2, 3, 4, 5, 6, 7});
14777 : which doesn't dangle: std::min here returns an int.
14778 :
14779 : If the function returns a std::pair<const T&, const T&>, we
14780 : warn, to detect e.g.
14781 : std::pair<const int&, const int&> v = std::minmax(1, 2);
14782 : which also creates a dangling reference, because std::minmax
14783 : returns std::pair<const T&, const T&>(b, a). */
14784 1219 : if (!(TYPE_REF_OBJ_P (rettype) || reference_like_class_p (rettype)))
14785 : return NULL_TREE;
14786 :
14787 : /* Here we're looking to see if any of the arguments is a temporary
14788 : initializing a reference parameter. */
14789 1647 : for (int i = 0; i < call_expr_nargs (expr); ++i)
14790 : {
14791 1240 : tree arg = CALL_EXPR_ARG (expr, i);
14792 : /* Check that this argument initializes a reference, except for
14793 : the argument initializing the object of a member function. */
14794 1240 : if (!DECL_IOBJ_MEMBER_FUNCTION_P (fndecl)
14795 1240 : && !TYPE_REF_P (TREE_TYPE (arg)))
14796 130 : continue;
14797 1110 : STRIP_NOPS (arg);
14798 1110 : if (TREE_CODE (arg) == ADDR_EXPR)
14799 725 : arg = TREE_OPERAND (arg, 0);
14800 : /* Recurse to see if the argument is a temporary. It could also
14801 : be another call taking a temporary and returning it and
14802 : initializing this reference parameter. */
14803 1110 : if ((arg = do_warn_dangling_reference (arg, /*arg_p=*/true)))
14804 : {
14805 : /* If we know the temporary could not bind to the return type,
14806 : don't warn. This is for scalars and empty classes only
14807 : because for other classes we can't be sure we are not
14808 : returning its sub-object. */
14809 278 : if ((SCALAR_TYPE_P (TREE_TYPE (arg))
14810 152 : || is_empty_class (TREE_TYPE (arg)))
14811 153 : && TYPE_REF_P (rettype)
14812 138 : && !reference_related_p (TREE_TYPE (rettype),
14813 138 : TREE_TYPE (arg)))
14814 21 : continue;
14815 : return arg;
14816 : }
14817 : /* Don't warn about member functions like:
14818 : std::any a(...);
14819 : S& s = a.emplace<S>({0}, 0);
14820 : which construct a new object and return a reference to it, but
14821 : we still want to detect:
14822 : struct S { const S& self () { return *this; } };
14823 : const S& s = S().self();
14824 : where 's' dangles. If we've gotten here, the object this function
14825 : is invoked on is not a temporary. */
14826 832 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl))
14827 : break;
14828 : }
14829 : return NULL_TREE;
14830 : }
14831 28 : case COMPOUND_EXPR:
14832 28 : return do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p);
14833 31 : case COND_EXPR:
14834 31 : if (tree t = do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p))
14835 : return t;
14836 16 : return do_warn_dangling_reference (TREE_OPERAND (expr, 2), arg_p);
14837 0 : case PAREN_EXPR:
14838 0 : return do_warn_dangling_reference (TREE_OPERAND (expr, 0), arg_p);
14839 122 : case TARGET_EXPR:
14840 122 : return do_warn_dangling_reference (TARGET_EXPR_INITIAL (expr), arg_p);
14841 : default:
14842 : return NULL_TREE;
14843 : }
14844 : }
14845 :
14846 : /* Implement -Wdangling-reference, to detect cases like
14847 :
14848 : int n = 1;
14849 : const int& r = std::max(n - 1, n + 1); // r is dangling
14850 :
14851 : This creates temporaries from the arguments, returns a reference to
14852 : one of the temporaries, but both temporaries are destroyed at the end
14853 : of the full expression.
14854 :
14855 : This works by checking if a reference is initialized with a function
14856 : that returns a reference, and at least one parameter of the function
14857 : is a reference that is bound to a temporary. It assumes that such a
14858 : function actually returns one of its arguments.
14859 :
14860 : DECL is the reference being initialized, INIT is the initializer. */
14861 :
14862 : static void
14863 109406622 : maybe_warn_dangling_reference (const_tree decl, tree init)
14864 : {
14865 109406622 : if (!warn_dangling_reference)
14866 : return;
14867 619611 : tree type = TREE_TYPE (decl);
14868 : /* Only warn if what we're initializing has type T&& or const T&, or
14869 : std::pair<const T&, const T&>. (A non-const lvalue reference can't
14870 : bind to a temporary.) */
14871 1236663 : if (!((TYPE_REF_OBJ_P (type)
14872 5444 : && (TYPE_REF_IS_RVALUE (type)
14873 5004 : || CP_TYPE_CONST_P (TREE_TYPE (type))))
14874 617052 : || std_pair_ref_ref_p (type)))
14875 : return;
14876 : /* Don't suppress the diagnostic just because the call comes from
14877 : a system header. If the DECL is not in a system header, or if
14878 : -Wsystem-headers was provided, warn. */
14879 2574 : auto wsh
14880 2574 : = make_temp_override (global_dc->m_warn_system_headers,
14881 2574 : (!in_system_header_at (DECL_SOURCE_LOCATION (decl))
14882 2574 : || global_dc->m_warn_system_headers));
14883 2574 : if (tree call = do_warn_dangling_reference (init, /*arg_p=*/false))
14884 : {
14885 212 : auto_diagnostic_group d;
14886 212 : if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wdangling_reference,
14887 : "possibly dangling reference to a temporary"))
14888 212 : inform (EXPR_LOCATION (call), "%qT temporary created here",
14889 212 : TREE_TYPE (call));
14890 212 : }
14891 2574 : }
14892 :
14893 : /* If *P is an xvalue expression, prevent temporary lifetime extension if it
14894 : gets used to initialize a reference. */
14895 :
14896 : static tree
14897 95 : prevent_lifetime_extension (tree t)
14898 : {
14899 95 : tree *p = &t;
14900 95 : while (TREE_CODE (*p) == COMPOUND_EXPR)
14901 0 : p = &TREE_OPERAND (*p, 1);
14902 104 : while (handled_component_p (*p))
14903 9 : p = &TREE_OPERAND (*p, 0);
14904 : /* Change a TARGET_EXPR from prvalue to xvalue. */
14905 95 : if (TREE_CODE (*p) == TARGET_EXPR)
14906 3 : *p = build2 (COMPOUND_EXPR, TREE_TYPE (*p), *p,
14907 3 : move (TARGET_EXPR_SLOT (*p)));
14908 95 : return t;
14909 : }
14910 :
14911 : /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
14912 : which is bound either to a reference or a std::initializer_list. */
14913 :
14914 : static tree
14915 763752 : extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups,
14916 : tree *cond_guard)
14917 : {
14918 : /* CWG1299 (C++20): The temporary object to which the reference is bound or
14919 : the temporary object that is the complete object of a subobject to which
14920 : the reference is bound persists for the lifetime of the reference if the
14921 : glvalue to which the reference is bound was obtained through one of the
14922 : following:
14923 : - a temporary materialization conversion ([conv.rval]),
14924 : - ( expression ), where expression is one of these expressions,
14925 : - subscripting ([expr.sub]) of an array operand, where that operand is one
14926 : of these expressions,
14927 : - a class member access ([expr.ref]) using the . operator where the left
14928 : operand is one of these expressions and the right operand designates a
14929 : non-static data member of non-reference type,
14930 : - a pointer-to-member operation ([expr.mptr.oper]) using the .* operator
14931 : where the left operand is one of these expressions and the right operand
14932 : is a pointer to data member of non-reference type,
14933 : - a const_cast ([expr.const.cast]), static_cast ([expr.static.cast]),
14934 : dynamic_cast ([expr.dynamic.cast]), or reinterpret_cast
14935 : ([expr.reinterpret.cast]) converting, without a user-defined conversion,
14936 : a glvalue operand that is one of these expressions to a glvalue that
14937 : refers to the object designated by the operand, or to its complete
14938 : object or a subobject thereof,
14939 : - a conditional expression ([expr.cond]) that is a glvalue where the
14940 : second or third operand is one of these expressions, or
14941 : - a comma expression ([expr.comma]) that is a glvalue where the right
14942 : operand is one of these expressions. */
14943 :
14944 : /* FIXME several cases are still handled wrong (101572, 81420). */
14945 :
14946 763752 : tree sub = init;
14947 763752 : tree *p;
14948 763752 : STRIP_NOPS (sub);
14949 763752 : if (TREE_CODE (sub) == COMPOUND_EXPR)
14950 : {
14951 242 : TREE_OPERAND (sub, 1)
14952 242 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups,
14953 : cond_guard);
14954 242 : return init;
14955 : }
14956 763510 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14957 763510 : && TYPE_PTRDATAMEM_P (TREE_TYPE (tree_strip_nop_conversions
14958 : (TREE_OPERAND (sub, 1)))))
14959 : {
14960 : /* A pointer-to-member operation. */
14961 6 : TREE_OPERAND (sub, 0)
14962 6 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 0), cleanups,
14963 : cond_guard);
14964 6 : return init;
14965 : }
14966 763504 : if (TREE_CODE (sub) == COND_EXPR)
14967 : {
14968 23449 : tree cur_cond_guard = NULL_TREE;
14969 23449 : if (TREE_OPERAND (sub, 1))
14970 23449 : TREE_OPERAND (sub, 1)
14971 46898 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups,
14972 : &cur_cond_guard);
14973 23449 : if (cur_cond_guard)
14974 : {
14975 9 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard,
14976 : NOP_EXPR, boolean_true_node,
14977 : tf_warning_or_error);
14978 9 : TREE_OPERAND (sub, 1)
14979 18 : = cp_build_compound_expr (set, TREE_OPERAND (sub, 1),
14980 : tf_warning_or_error);
14981 : }
14982 23449 : cur_cond_guard = NULL_TREE;
14983 23449 : if (TREE_OPERAND (sub, 2))
14984 23449 : TREE_OPERAND (sub, 2)
14985 46898 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 2), cleanups,
14986 : &cur_cond_guard);
14987 23449 : if (cur_cond_guard)
14988 : {
14989 12 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard,
14990 : NOP_EXPR, boolean_true_node,
14991 : tf_warning_or_error);
14992 12 : TREE_OPERAND (sub, 2)
14993 24 : = cp_build_compound_expr (set, TREE_OPERAND (sub, 2),
14994 : tf_warning_or_error);
14995 : }
14996 23449 : return init;
14997 : }
14998 740055 : if (TREE_CODE (sub) != ADDR_EXPR)
14999 : return init;
15000 : /* Deal with binding to a subobject. */
15001 264062 : for (p = &TREE_OPERAND (sub, 0);
15002 277510 : TREE_CODE (*p) == COMPONENT_REF || TREE_CODE (*p) == ARRAY_REF; )
15003 13448 : p = &TREE_OPERAND (*p, 0);
15004 264062 : if (TREE_CODE (*p) == TARGET_EXPR)
15005 : {
15006 7134 : tree subinit = NULL_TREE;
15007 7134 : *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit,
15008 : cond_guard, nullptr);
15009 7134 : recompute_tree_invariant_for_addr_expr (sub);
15010 7134 : if (init != sub)
15011 7116 : init = fold_convert (TREE_TYPE (init), sub);
15012 7134 : if (subinit)
15013 6021 : init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
15014 : }
15015 : return init;
15016 : }
15017 :
15018 : /* Data for extend_temps_r, mostly matching the parameters of
15019 : extend_ref_init_temps. */
15020 :
15021 : struct extend_temps_data
15022 : {
15023 : tree decl;
15024 : tree init;
15025 : vec<tree, va_gc> **cleanups;
15026 : tree* cond_guard;
15027 : hash_set<tree> *pset; // For avoiding redundant walk_tree.
15028 : hash_map<tree, tree> *var_map; // For remapping extended temps.
15029 : };
15030 :
15031 : /* Tree walk function for extend_all_temps. Generally parallel to
15032 : extend_ref_init_temps_1, but adapted for walk_tree. */
15033 :
15034 : tree
15035 98570 : extend_temps_r (tree *tp, int *walk_subtrees, void *data)
15036 : {
15037 98570 : extend_temps_data *d = (extend_temps_data *)data;
15038 :
15039 98570 : if (TREE_CODE (*tp) == VAR_DECL)
15040 : {
15041 13302 : if (tree *r = d->var_map->get (*tp))
15042 0 : *tp = *r;
15043 : return NULL_TREE;
15044 : }
15045 :
15046 85249 : if (TYPE_P (*tp) || TREE_CODE (*tp) == CLEANUP_POINT_EXPR
15047 170516 : || d->pset->add (*tp))
15048 : {
15049 4956 : *walk_subtrees = 0;
15050 4956 : return NULL_TREE;
15051 : }
15052 :
15053 80312 : if (TREE_CODE (*tp) == COND_EXPR)
15054 : {
15055 8 : cp_walk_tree (&TREE_OPERAND (*tp, 0), extend_temps_r, d, nullptr);
15056 :
15057 24 : auto walk_arm = [d](tree &op)
15058 : {
15059 16 : tree cur_cond_guard = NULL_TREE;
15060 16 : auto ov = make_temp_override (d->cond_guard, &cur_cond_guard);
15061 16 : cp_walk_tree (&op, extend_temps_r, d, nullptr);
15062 16 : if (cur_cond_guard)
15063 : {
15064 2 : tree set = build2 (MODIFY_EXPR, boolean_type_node,
15065 : cur_cond_guard, boolean_true_node);
15066 2 : op = cp_build_compound_expr (set, op, tf_none);
15067 : }
15068 24 : };
15069 8 : walk_arm (TREE_OPERAND (*tp, 1));
15070 8 : walk_arm (TREE_OPERAND (*tp, 2));
15071 :
15072 8 : *walk_subtrees = 0;
15073 8 : return NULL_TREE;
15074 : }
15075 :
15076 80304 : tree *p = tp;
15077 :
15078 80304 : if (TREE_CODE (*tp) == ADDR_EXPR)
15079 18509 : for (p = &TREE_OPERAND (*tp, 0);
15080 19966 : TREE_CODE (*p) == COMPONENT_REF || TREE_CODE (*p) == ARRAY_REF; )
15081 1457 : p = &TREE_OPERAND (*p, 0);
15082 :
15083 80304 : if (TREE_CODE (*p) == TARGET_EXPR
15084 : /* An eliding TARGET_EXPR isn't a temporary at all. */
15085 3590 : && !TARGET_EXPR_ELIDING_P (*p)
15086 : /* A TARGET_EXPR with TARGET_EXPR_INTERNAL_P is an artificial variable
15087 : used during initialization that need not be extended. */
15088 82822 : && !TARGET_EXPR_INTERNAL_P (*p))
15089 : {
15090 : /* A CLEANUP_EH_ONLY expr should also have TARGET_EXPR_INTERNAL_P. */
15091 2184 : gcc_checking_assert (!CLEANUP_EH_ONLY (*p));
15092 :
15093 2184 : tree subinit = NULL_TREE;
15094 2184 : tree slot = TARGET_EXPR_SLOT (*p);
15095 2184 : *p = set_up_extended_ref_temp (d->decl, *p, d->cleanups, &subinit,
15096 : d->cond_guard, d);
15097 2184 : if (TREE_CODE (*tp) == ADDR_EXPR)
15098 2170 : recompute_tree_invariant_for_addr_expr (*tp);
15099 2184 : if (subinit)
15100 2036 : *tp = cp_build_compound_expr (subinit, *tp, tf_none);
15101 2184 : d->var_map->put (slot, *p);
15102 : }
15103 :
15104 : return NULL_TREE;
15105 : }
15106 :
15107 : /* Extend all the temporaries in a for-range-initializer. */
15108 :
15109 : static tree
15110 19879 : extend_all_temps (tree decl, tree init, vec<tree, va_gc> **cleanups)
15111 : {
15112 19879 : hash_set<tree> pset;
15113 19879 : hash_map<tree, tree> map;
15114 19879 : gcc_assert (!TREE_STATIC (decl));
15115 19879 : extend_temps_data d = { decl, init, cleanups, nullptr, &pset, &map };
15116 19879 : cp_walk_tree (&init, extend_temps_r, &d, nullptr);
15117 19879 : return init;
15118 19879 : }
15119 :
15120 : /* INIT is part of the initializer for DECL. If there are any
15121 : reference or initializer lists being initialized, extend their
15122 : lifetime to match that of DECL. */
15123 :
15124 : tree
15125 111907656 : extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups,
15126 : tree *cond_guard)
15127 : {
15128 111907656 : tree type = TREE_TYPE (init);
15129 111907656 : if (processing_template_decl)
15130 : return init;
15131 :
15132 : /* P2718R0 - in C++23 for-range-initializer, extend all temps. */
15133 109426501 : if (DECL_NAME (decl) == for_range__identifier
15134 94747 : && flag_range_for_ext_temps
15135 : /* Iterating expansion statement decl is static right now, but that
15136 : could change depending on CWG3044 and CWG3043. */
15137 109446438 : && !TREE_STATIC (decl))
15138 : {
15139 19879 : gcc_checking_assert (!cond_guard);
15140 19879 : return extend_all_temps (decl, init, cleanups);
15141 : }
15142 :
15143 109406622 : maybe_warn_dangling_reference (decl, init);
15144 :
15145 109406622 : if (TYPE_REF_P (type))
15146 715947 : init = extend_ref_init_temps_1 (decl, init, cleanups, cond_guard);
15147 : else
15148 : {
15149 108690675 : tree ctor = init;
15150 108690675 : if (TREE_CODE (ctor) == TARGET_EXPR)
15151 1560763 : ctor = TARGET_EXPR_INITIAL (ctor);
15152 108690675 : if (TREE_CODE (ctor) == CONSTRUCTOR)
15153 : {
15154 : /* [dcl.init] When initializing an aggregate from a parenthesized list
15155 : of values... a temporary object bound to a reference does not have
15156 : its lifetime extended. */
15157 5674158 : if (CONSTRUCTOR_IS_PAREN_INIT (ctor))
15158 : return init;
15159 :
15160 5673670 : if (is_std_init_list (type))
15161 : {
15162 : /* The temporary array underlying a std::initializer_list
15163 : is handled like a reference temporary. */
15164 659 : tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
15165 659 : array = extend_ref_init_temps_1 (decl, array, cleanups,
15166 : cond_guard);
15167 659 : CONSTRUCTOR_ELT (ctor, 0)->value = array;
15168 : }
15169 : else
15170 : {
15171 5673011 : unsigned i;
15172 5673011 : constructor_elt *p;
15173 5673011 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
15174 63135568 : FOR_EACH_VEC_SAFE_ELT (elts, i, p)
15175 57462557 : p->value = extend_ref_init_temps (decl, p->value, cleanups,
15176 : cond_guard);
15177 : }
15178 5673670 : recompute_constructor_flags (ctor);
15179 5673670 : if (decl_maybe_constant_var_p (decl) && TREE_CONSTANT (ctor))
15180 3160345 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
15181 : }
15182 : }
15183 :
15184 : return init;
15185 : }
15186 :
15187 : /* Returns true iff an initializer for TYPE could contain temporaries that
15188 : need to be extended because they are bound to references or
15189 : std::initializer_list. */
15190 :
15191 : bool
15192 0 : type_has_extended_temps (tree type)
15193 : {
15194 0 : type = strip_array_types (type);
15195 0 : if (TYPE_REF_P (type))
15196 : return true;
15197 0 : if (CLASS_TYPE_P (type))
15198 : {
15199 0 : if (is_std_init_list (type))
15200 : return true;
15201 0 : for (tree f = next_aggregate_field (TYPE_FIELDS (type));
15202 0 : f; f = next_aggregate_field (DECL_CHAIN (f)))
15203 0 : if (type_has_extended_temps (TREE_TYPE (f)))
15204 : return true;
15205 : }
15206 : return false;
15207 : }
15208 :
15209 : /* Returns true iff TYPE is some variant of std::initializer_list. */
15210 :
15211 : bool
15212 183380493 : is_std_init_list (tree type)
15213 : {
15214 183380493 : if (!TYPE_P (type))
15215 : return false;
15216 183380470 : if (cxx_dialect == cxx98)
15217 : return false;
15218 : /* Look through typedefs. */
15219 182961674 : type = TYPE_MAIN_VARIANT (type);
15220 146644507 : return (CLASS_TYPE_P (type)
15221 146497816 : && CP_TYPE_CONTEXT (type) == std_node
15222 264659372 : && init_list_identifier == DECL_NAME (TYPE_NAME (type)));
15223 : }
15224 :
15225 : /* Returns true iff DECL is a list constructor: i.e. a constructor which
15226 : will accept an argument list of a single std::initializer_list<T>. */
15227 :
15228 : bool
15229 159282773 : is_list_ctor (tree decl)
15230 : {
15231 159282773 : tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
15232 159282773 : tree arg;
15233 :
15234 159282773 : if (!args || args == void_list_node)
15235 : return false;
15236 :
15237 128201905 : arg = non_reference (TREE_VALUE (args));
15238 128201905 : if (!is_std_init_list (arg))
15239 : return false;
15240 :
15241 2308370 : args = TREE_CHAIN (args);
15242 :
15243 3929208 : if (args && args != void_list_node && !TREE_PURPOSE (args))
15244 : /* There are more non-defaulted parms. */
15245 634641 : return false;
15246 :
15247 : return true;
15248 : }
15249 :
15250 : /* We know that can_convert_arg_bad already said "no" when trying to convert
15251 : FROM to TO with ARG and FLAGS. Try to figure out if it was because
15252 : an explicit conversion function was skipped when looking for a way to
15253 : perform the conversion. At this point we've already printed an error. */
15254 :
15255 : void
15256 2128 : maybe_show_nonconverting_candidate (tree to, tree from, tree arg, int flags)
15257 : {
15258 2128 : if (!(flags & LOOKUP_ONLYCONVERTING))
15259 293 : return;
15260 :
15261 1835 : conversion_obstack_sentinel cos;
15262 1835 : conversion *c = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
15263 : flags & ~LOOKUP_ONLYCONVERTING, tf_none);
15264 1835 : if (c && !c->bad_p && c->user_conv_p)
15265 : /* Ay, the conversion would have worked in direct-init context. */
15266 859 : for (; c; c = next_conversion (c))
15267 575 : if (c->kind == ck_user
15268 281 : && DECL_P (c->cand->fn)
15269 856 : && DECL_NONCONVERTING_P (c->cand->fn))
15270 277 : inform (DECL_SOURCE_LOCATION (c->cand->fn), "explicit conversion "
15271 : "function was not considered");
15272 1835 : }
15273 :
15274 : /* We're converting EXPR to TYPE. If that conversion involves a conversion
15275 : function and we're binding EXPR to a reference parameter of that function,
15276 : return true. */
15277 :
15278 : bool
15279 171 : conv_binds_to_reference_parm_p (tree type, tree expr)
15280 : {
15281 171 : conversion_obstack_sentinel cos;
15282 171 : conversion *c = implicit_conversion (type, TREE_TYPE (expr), expr,
15283 : /*c_cast_p=*/false, LOOKUP_NORMAL,
15284 : tf_none);
15285 171 : if (c && !c->bad_p && c->user_conv_p)
15286 117 : for (; c; c = next_conversion (c))
15287 81 : if (c->kind == ck_user)
15288 244 : for (z_candidate *cand = c->cand; cand; cand = cand->next)
15289 208 : if (cand->viable == 1)
15290 81 : for (size_t i = 0; i < cand->num_convs; ++i)
15291 45 : if (cand->convs[i]->kind == ck_ref_bind
15292 45 : && conv_get_original_expr (cand->convs[i]) == expr)
15293 : return true;
15294 :
15295 : return false;
15296 171 : }
15297 :
15298 : #include "gt-cp-call.h"
|