Line data Source code
1 : /* Functions related to invoking -*- C++ -*- methods and overloaded functions.
2 : Copyright (C) 1987-2023 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 "gcc-rich-location.h"
46 :
47 : /* The various kinds of conversion. */
48 :
49 : enum conversion_kind {
50 : ck_identity,
51 : ck_lvalue,
52 : ck_fnptr,
53 : ck_qual,
54 : ck_std,
55 : ck_ptr,
56 : ck_pmem,
57 : ck_base,
58 : ck_ref_bind,
59 : ck_user,
60 : ck_ambig,
61 : ck_list,
62 : ck_aggr,
63 : ck_rvalue,
64 : /* When LOOKUP_SHORTCUT_BAD_CONVS is set, we may return a conversion of
65 : this kind whenever we know the true conversion is either bad or outright
66 : invalid, but we don't want to attempt to compute the bad conversion (for
67 : sake of avoiding unnecessary instantiation). bad_p should always be set
68 : for these. */
69 : ck_deferred_bad,
70 : };
71 :
72 : /* The rank of the conversion. Order of the enumerals matters; better
73 : conversions should come earlier in the list. */
74 :
75 : enum conversion_rank {
76 : cr_identity,
77 : cr_exact,
78 : cr_promotion,
79 : cr_std,
80 : cr_pbool,
81 : cr_user,
82 : cr_ellipsis,
83 : cr_bad
84 : };
85 :
86 : /* An implicit conversion sequence, in the sense of [over.best.ics].
87 : The first conversion to be performed is at the end of the chain.
88 : That conversion is always a cr_identity conversion. */
89 :
90 : struct conversion {
91 : /* The kind of conversion represented by this step. */
92 : conversion_kind kind;
93 : /* The rank of this conversion. */
94 : conversion_rank rank;
95 : BOOL_BITFIELD user_conv_p : 1;
96 : BOOL_BITFIELD ellipsis_p : 1;
97 : BOOL_BITFIELD this_p : 1;
98 : /* True if this conversion would be permitted with a bending of
99 : language standards, e.g. disregarding pointer qualifiers or
100 : converting integers to pointers. */
101 : BOOL_BITFIELD bad_p : 1;
102 : /* If KIND is ck_ref_bind or ck_base, true to indicate that a
103 : temporary should be created to hold the result of the
104 : conversion. If KIND is ck_ambig or ck_user, true means force
105 : copy-initialization. */
106 : BOOL_BITFIELD need_temporary_p : 1;
107 : /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion
108 : from a pointer-to-derived to pointer-to-base is being performed. */
109 : BOOL_BITFIELD base_p : 1;
110 : /* If KIND is ck_ref_bind, true when either an lvalue reference is
111 : being bound to an lvalue expression or an rvalue reference is
112 : being bound to an rvalue expression. If KIND is ck_rvalue or ck_base,
113 : true when we are treating an lvalue as an rvalue (12.8p33). If
114 : ck_identity, we will be binding a reference directly or decaying to
115 : a pointer. */
116 : BOOL_BITFIELD rvaluedness_matches_p: 1;
117 : BOOL_BITFIELD check_narrowing: 1;
118 : /* Whether check_narrowing should only check TREE_CONSTANTs; used
119 : in build_converted_constant_expr. */
120 : BOOL_BITFIELD check_narrowing_const_only: 1;
121 : /* True if this conversion is taking place in a copy-initialization context
122 : and we should only consider converting constructors. Only set in
123 : ck_base and ck_rvalue. */
124 : BOOL_BITFIELD copy_init_p : 1;
125 : /* The type of the expression resulting from the conversion. */
126 : tree type;
127 : union {
128 : /* The next conversion in the chain. Since the conversions are
129 : arranged from outermost to innermost, the NEXT conversion will
130 : actually be performed before this conversion. This variant is
131 : used only when KIND is neither ck_identity, ck_aggr, ck_ambig nor
132 : ck_list. Please use the next_conversion function instead
133 : of using this field directly. */
134 : conversion *next;
135 : /* The expression at the beginning of the conversion chain. This
136 : variant is used only if KIND is ck_identity, ck_aggr, or ck_ambig.
137 : You can use conv_get_original_expr to get this expression. */
138 : tree expr;
139 : /* The array of conversions for an initializer_list, so this
140 : variant is used only when KIN D is ck_list. */
141 : conversion **list;
142 : } u;
143 : /* The function candidate corresponding to this conversion
144 : sequence. This field is only used if KIND is ck_user. */
145 : struct z_candidate *cand;
146 : };
147 :
148 : #define CONVERSION_RANK(NODE) \
149 : ((NODE)->bad_p ? cr_bad \
150 : : (NODE)->ellipsis_p ? cr_ellipsis \
151 : : (NODE)->user_conv_p ? cr_user \
152 : : (NODE)->rank)
153 :
154 : #define BAD_CONVERSION_RANK(NODE) \
155 : ((NODE)->ellipsis_p ? cr_ellipsis \
156 : : (NODE)->user_conv_p ? cr_user \
157 : : (NODE)->rank)
158 :
159 : static struct obstack conversion_obstack;
160 : static bool conversion_obstack_initialized;
161 : struct rejection_reason;
162 :
163 : static struct z_candidate * tourney (struct z_candidate *, tsubst_flags_t);
164 : static int equal_functions (tree, tree);
165 : static int joust (struct z_candidate *, struct z_candidate *, bool,
166 : tsubst_flags_t);
167 : static int compare_ics (conversion *, conversion *);
168 : static void maybe_warn_class_memaccess (location_t, tree,
169 : const vec<tree, va_gc> *);
170 : static tree build_over_call (struct z_candidate *, int, tsubst_flags_t);
171 : static tree convert_like (conversion *, tree, tsubst_flags_t);
172 : static tree convert_like_with_context (conversion *, tree, tree, int,
173 : tsubst_flags_t);
174 : static void op_error (const op_location_t &, enum tree_code, enum tree_code,
175 : tree, tree, tree, bool);
176 : static struct z_candidate *build_user_type_conversion_1 (tree, tree, int,
177 : tsubst_flags_t);
178 : static void print_z_candidate (location_t, const char *, struct z_candidate *);
179 : static void print_z_candidates (location_t, struct z_candidate *);
180 : static tree build_this (tree);
181 : static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
182 : static bool any_strictly_viable (struct z_candidate *);
183 : static struct z_candidate *add_template_candidate
184 : (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
185 : tree, tree, tree, int, unification_kind_t, bool, tsubst_flags_t);
186 : static struct z_candidate *add_template_candidate_real
187 : (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
188 : tree, tree, tree, int, tree, unification_kind_t, bool, tsubst_flags_t);
189 : static bool is_complete (tree);
190 : static struct z_candidate *add_conv_candidate
191 : (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, tree,
192 : tree, tsubst_flags_t);
193 : static struct z_candidate *add_function_candidate
194 : (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *, tree,
195 : tree, int, conversion**, bool, tsubst_flags_t);
196 : static conversion *implicit_conversion (tree, tree, tree, bool, int,
197 : tsubst_flags_t);
198 : static conversion *reference_binding (tree, tree, tree, bool, int,
199 : tsubst_flags_t);
200 : static conversion *build_conv (conversion_kind, tree, conversion *);
201 : static conversion *build_list_conv (tree, tree, int, tsubst_flags_t);
202 : static conversion *next_conversion (conversion *);
203 : static bool is_subseq (conversion *, conversion *);
204 : static conversion *maybe_handle_ref_bind (conversion **);
205 : static void maybe_handle_implicit_object (conversion **);
206 : static struct z_candidate *add_candidate
207 : (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, size_t,
208 : conversion **, tree, tree, int, struct rejection_reason *, int);
209 : static tree source_type (conversion *);
210 : static void add_warning (struct z_candidate *, struct z_candidate *);
211 : static conversion *direct_reference_binding (tree, conversion *);
212 : static bool promoted_arithmetic_type_p (tree);
213 : static conversion *conditional_conversion (tree, tree, tsubst_flags_t);
214 : static char *name_as_c_string (tree, tree, bool *);
215 : static tree prep_operand (tree);
216 : static void add_candidates (tree, tree, const vec<tree, va_gc> *, tree, tree,
217 : bool, tree, tree, int, struct z_candidate **,
218 : tsubst_flags_t);
219 : static conversion *merge_conversion_sequences (conversion *, conversion *);
220 : static tree build_temp (tree, tree, int, diagnostic_t *, tsubst_flags_t);
221 : static conversion *build_identity_conv (tree, tree);
222 : static inline bool conv_binds_to_array_of_unknown_bound (conversion *);
223 : static bool conv_is_prvalue (conversion *);
224 : static tree prevent_lifetime_extension (tree);
225 :
226 : /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
227 : NAME can take many forms... */
228 :
229 : bool
230 1909046 : check_dtor_name (tree basetype, tree name)
231 : {
232 : /* Just accept something we've already complained about. */
233 1909046 : if (name == error_mark_node)
234 : return true;
235 :
236 1909046 : if (TREE_CODE (name) == TYPE_DECL)
237 87 : name = TREE_TYPE (name);
238 1908959 : else if (TYPE_P (name))
239 : /* OK */;
240 23 : else if (identifier_p (name))
241 : {
242 23 : if ((MAYBE_CLASS_TYPE_P (basetype)
243 0 : || TREE_CODE (basetype) == ENUMERAL_TYPE)
244 46 : && name == constructor_name (basetype))
245 : return true;
246 :
247 : /* Otherwise lookup the name, it could be an unrelated typedef
248 : of the correct type. */
249 8 : name = lookup_name (name, LOOK_want::TYPE);
250 8 : if (!name)
251 : return false;
252 4 : name = TREE_TYPE (name);
253 4 : if (name == error_mark_node)
254 : return false;
255 : }
256 : else
257 : {
258 : /* In the case of:
259 :
260 : template <class T> struct S { ~S(); };
261 : int i;
262 : i.~S();
263 :
264 : NAME will be a class template. */
265 0 : gcc_assert (DECL_CLASS_TEMPLATE_P (name));
266 : return false;
267 : }
268 :
269 1909023 : return same_type_p (TYPE_MAIN_VARIANT (basetype), TYPE_MAIN_VARIANT (name));
270 : }
271 :
272 : /* We want the address of a function or method. We avoid creating a
273 : pointer-to-member function. */
274 :
275 : tree
276 132810874 : build_addr_func (tree function, tsubst_flags_t complain)
277 : {
278 132810874 : tree type = TREE_TYPE (function);
279 :
280 : /* We have to do these by hand to avoid real pointer to member
281 : functions. */
282 132810874 : if (TREE_CODE (type) == METHOD_TYPE)
283 : {
284 21090480 : if (TREE_CODE (function) == OFFSET_REF)
285 : {
286 57 : tree object = build_address (TREE_OPERAND (function, 0));
287 57 : return get_member_function_from_ptrfunc (&object,
288 57 : TREE_OPERAND (function, 1),
289 : complain);
290 : }
291 21090423 : function = build_address (function);
292 : }
293 111720394 : else if (TREE_CODE (function) == FUNCTION_DECL
294 162517250 : && DECL_IMMEDIATE_FUNCTION_P (function))
295 2062 : function = build_address (function);
296 : else
297 111718332 : function = decay_conversion (function, complain, /*reject_builtin=*/false);
298 :
299 : return function;
300 : }
301 :
302 : /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
303 : POINTER_TYPE to those. Note, pointer to member function types
304 : (TYPE_PTRMEMFUNC_P) must be handled by our callers. There are
305 : two variants. build_call_a is the primitive taking an array of
306 : arguments, while build_call_n is a wrapper that handles varargs. */
307 :
308 : tree
309 116422 : build_call_n (tree function, int n, ...)
310 : {
311 116422 : if (n == 0)
312 0 : return build_call_a (function, 0, NULL);
313 : else
314 : {
315 116422 : tree *argarray = XALLOCAVEC (tree, n);
316 116422 : va_list ap;
317 116422 : int i;
318 :
319 116422 : va_start (ap, n);
320 232844 : for (i = 0; i < n; i++)
321 116422 : argarray[i] = va_arg (ap, tree);
322 116422 : va_end (ap);
323 116422 : return build_call_a (function, n, argarray);
324 : }
325 : }
326 :
327 : /* Update various flags in cfun and the call itself based on what is being
328 : called. Split out of build_call_a so that bot_manip can use it too. */
329 :
330 : void
331 61666479 : set_flags_from_callee (tree call)
332 : {
333 : /* Handle both CALL_EXPRs and AGGR_INIT_EXPRs. */
334 61666479 : tree decl = cp_get_callee_fndecl_nofold (call);
335 :
336 : /* We check both the decl and the type; a function may be known not to
337 : throw without being declared throw(). */
338 61666479 : bool nothrow = decl && TREE_NOTHROW (decl);
339 61666479 : tree callee = cp_get_callee (call);
340 61666479 : if (callee)
341 61666474 : nothrow |= TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (callee)));
342 5 : else if (TREE_CODE (call) == CALL_EXPR
343 5 : && internal_fn_flags (CALL_EXPR_IFN (call)) & ECF_NOTHROW)
344 : nothrow = true;
345 :
346 61666479 : if (cfun && cp_function_chain && !cp_unevaluated_operand)
347 : {
348 49258498 : if (!nothrow && at_function_scope_p ())
349 13072448 : cp_function_chain->can_throw = 1;
350 :
351 49258498 : if (decl && TREE_THIS_VOLATILE (decl))
352 2035546 : current_function_returns_abnormally = 1;
353 : }
354 :
355 61666479 : TREE_NOTHROW (call) = nothrow;
356 61666479 : }
357 :
358 : tree
359 60776355 : build_call_a (tree function, int n, tree *argarray)
360 : {
361 60776355 : tree decl;
362 60776355 : tree result_type;
363 60776355 : tree fntype;
364 60776355 : int i;
365 :
366 60776355 : function = build_addr_func (function, tf_warning_or_error);
367 :
368 60776355 : gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
369 60776355 : fntype = TREE_TYPE (TREE_TYPE (function));
370 60776355 : gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype));
371 60776355 : result_type = TREE_TYPE (fntype);
372 : /* An rvalue has no cv-qualifiers. */
373 60776355 : if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
374 47218443 : result_type = cv_unqualified (result_type);
375 :
376 60776355 : function = build_call_array_loc (input_location,
377 : result_type, function, n, argarray);
378 60776355 : set_flags_from_callee (function);
379 :
380 60776355 : decl = get_callee_fndecl (function);
381 :
382 60776355 : if (decl && !TREE_USED (decl))
383 : {
384 : /* We invoke build_call directly for several library
385 : functions. These may have been declared normally if
386 : we're building libgcc, so we can't just check
387 : DECL_ARTIFICIAL. */
388 75267 : gcc_assert (DECL_ARTIFICIAL (decl)
389 : || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
390 : "__", 2));
391 75267 : mark_used (decl);
392 : }
393 :
394 60776355 : require_complete_eh_spec_types (fntype, decl);
395 :
396 176629582 : TREE_HAS_CONSTRUCTOR (function) = (decl && DECL_CONSTRUCTOR_P (decl));
397 :
398 : /* Don't pass empty class objects by value. This is useful
399 : for tags in STL, which are used to control overload resolution.
400 : We don't need to handle other cases of copying empty classes. */
401 60776355 : if (!decl || !fndecl_built_in_p (decl))
402 123549177 : for (i = 0; i < n; i++)
403 : {
404 67978295 : tree arg = CALL_EXPR_ARG (function, i);
405 67978295 : if (is_empty_class (TREE_TYPE (arg))
406 67978295 : && simple_empty_class_p (TREE_TYPE (arg), arg, INIT_EXPR))
407 : {
408 3390838 : while (TREE_CODE (arg) == TARGET_EXPR)
409 : /* We're disconnecting the initializer from its target,
410 : don't create a temporary. */
411 1694882 : arg = TARGET_EXPR_INITIAL (arg);
412 1695956 : tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg));
413 1695956 : arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t);
414 1695956 : CALL_EXPR_ARG (function, i) = arg;
415 : }
416 : }
417 :
418 60776355 : return function;
419 : }
420 :
421 : /* New overloading code. */
422 :
423 : struct z_candidate;
424 :
425 : struct candidate_warning {
426 : z_candidate *loser;
427 : candidate_warning *next;
428 : };
429 :
430 : /* Information for providing diagnostics about why overloading failed. */
431 :
432 : enum rejection_reason_code {
433 : rr_none,
434 : rr_arity,
435 : rr_explicit_conversion,
436 : rr_template_conversion,
437 : rr_arg_conversion,
438 : rr_bad_arg_conversion,
439 : rr_template_unification,
440 : rr_invalid_copy,
441 : rr_inherited_ctor,
442 : rr_constraint_failure
443 : };
444 :
445 : struct conversion_info {
446 : /* The index of the argument, 0-based. */
447 : int n_arg;
448 : /* The actual argument or its type. */
449 : tree from;
450 : /* The type of the parameter. */
451 : tree to_type;
452 : /* The location of the argument. */
453 : location_t loc;
454 : };
455 :
456 : struct rejection_reason {
457 : enum rejection_reason_code code;
458 : union {
459 : /* Information about an arity mismatch. */
460 : struct {
461 : /* The expected number of arguments. */
462 : int expected;
463 : /* The actual number of arguments in the call. */
464 : int actual;
465 : /* Whether EXPECTED should be treated as a lower bound. */
466 : bool least_p;
467 : } arity;
468 : /* Information about an argument conversion mismatch. */
469 : struct conversion_info conversion;
470 : /* Same, but for bad argument conversions. */
471 : struct conversion_info bad_conversion;
472 : /* Information about template unification failures. These are the
473 : parameters passed to fn_type_unification. */
474 : struct {
475 : tree tmpl;
476 : tree explicit_targs;
477 : int num_targs;
478 : const tree *args;
479 : unsigned int nargs;
480 : tree return_type;
481 : unification_kind_t strict;
482 : int flags;
483 : } template_unification;
484 : /* Information about template instantiation failures. These are the
485 : parameters passed to instantiate_template. */
486 : struct {
487 : tree tmpl;
488 : tree targs;
489 : } template_instantiation;
490 : } u;
491 : };
492 :
493 : struct z_candidate {
494 : /* The FUNCTION_DECL that will be called if this candidate is
495 : selected by overload resolution. */
496 : tree fn;
497 : /* If not NULL_TREE, the first argument to use when calling this
498 : function. */
499 : tree first_arg;
500 : /* The rest of the arguments to use when calling this function. If
501 : there are no further arguments this may be NULL or it may be an
502 : empty vector. */
503 : const vec<tree, va_gc> *args;
504 : /* The implicit conversion sequences for each of the arguments to
505 : FN. */
506 : conversion **convs;
507 : /* The number of implicit conversion sequences. */
508 : size_t num_convs;
509 : /* If FN is a user-defined conversion, the standard conversion
510 : sequence from the type returned by FN to the desired destination
511 : type. */
512 : conversion *second_conv;
513 : struct rejection_reason *reason;
514 : /* If FN is a member function, the binfo indicating the path used to
515 : qualify the name of FN at the call site. This path is used to
516 : determine whether or not FN is accessible if it is selected by
517 : overload resolution. The DECL_CONTEXT of FN will always be a
518 : (possibly improper) base of this binfo. */
519 : tree access_path;
520 : /* If FN is a non-static member function, the binfo indicating the
521 : subobject to which the `this' pointer should be converted if FN
522 : is selected by overload resolution. The type pointed to by
523 : the `this' pointer must correspond to the most derived class
524 : indicated by the CONVERSION_PATH. */
525 : tree conversion_path;
526 : tree template_decl;
527 : tree explicit_targs;
528 : candidate_warning *warnings;
529 : z_candidate *next;
530 : int viable;
531 :
532 : /* The flags active in add_candidate. */
533 : int flags;
534 :
535 14884512 : bool rewritten () const { return (flags & LOOKUP_REWRITTEN); }
536 384388690 : bool reversed () const { return (flags & LOOKUP_REVERSED); }
537 : };
538 :
539 : /* Returns true iff T is a null pointer constant in the sense of
540 : [conv.ptr]. */
541 :
542 : bool
543 51197040 : null_ptr_cst_p (tree t)
544 : {
545 51197040 : tree type = TREE_TYPE (t);
546 :
547 : /* [conv.ptr]
548 :
549 : A null pointer constant is an integer literal ([lex.icon]) with value
550 : zero or a prvalue of type std::nullptr_t. */
551 51197040 : if (NULLPTR_TYPE_P (type))
552 : return true;
553 :
554 46941116 : if (cxx_dialect >= cxx11)
555 : {
556 46694607 : STRIP_ANY_LOCATION_WRAPPER (t);
557 :
558 : /* Core issue 903 says only literal 0 is a null pointer constant. */
559 46694607 : if (TREE_CODE (t) == INTEGER_CST
560 5610035 : && !TREE_OVERFLOW (t)
561 5610035 : && TREE_CODE (type) == INTEGER_TYPE
562 5170829 : && integer_zerop (t)
563 50170717 : && !char_type_p (type))
564 : return true;
565 : }
566 246509 : else if (CP_INTEGRAL_TYPE_P (type))
567 : {
568 43055 : t = fold_non_dependent_expr (t, tf_none);
569 43055 : STRIP_NOPS (t);
570 43055 : if (integer_zerop (t) && !TREE_OVERFLOW (t))
571 : return true;
572 : }
573 :
574 : return false;
575 : }
576 :
577 : /* Returns true iff T is a null member pointer value (4.11). */
578 :
579 : bool
580 34654654 : null_member_pointer_value_p (tree t)
581 : {
582 34654654 : tree type = TREE_TYPE (t);
583 34654654 : if (!type)
584 : return false;
585 34355968 : else if (TYPE_PTRMEMFUNC_P (type))
586 472 : return (TREE_CODE (t) == CONSTRUCTOR
587 221 : && CONSTRUCTOR_NELTS (t)
588 690 : && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value));
589 34355496 : else if (TYPE_PTRDATAMEM_P (type))
590 3998 : return integer_all_onesp (t);
591 : else
592 : return false;
593 : }
594 :
595 : /* Returns nonzero if PARMLIST consists of only default parms,
596 : ellipsis, and/or undeduced parameter packs. */
597 :
598 : bool
599 294134142 : sufficient_parms_p (const_tree parmlist)
600 : {
601 299795938 : for (; parmlist && parmlist != void_list_node;
602 5661796 : parmlist = TREE_CHAIN (parmlist))
603 105366553 : if (!TREE_PURPOSE (parmlist)
604 105366553 : && !PACK_EXPANSION_P (TREE_VALUE (parmlist)))
605 : return false;
606 : return true;
607 : }
608 :
609 : /* Allocate N bytes of memory from the conversion obstack. The memory
610 : is zeroed before being returned. */
611 :
612 : static void *
613 2609180958 : conversion_obstack_alloc (size_t n)
614 : {
615 2609180958 : void *p;
616 2609180958 : if (!conversion_obstack_initialized)
617 : {
618 75418 : gcc_obstack_init (&conversion_obstack);
619 75418 : conversion_obstack_initialized = true;
620 : }
621 2609180958 : p = obstack_alloc (&conversion_obstack, n);
622 2609180958 : memset (p, 0, n);
623 2609180958 : return p;
624 : }
625 :
626 : /* RAII class to discard anything added to conversion_obstack. */
627 :
628 : struct conversion_obstack_sentinel
629 : {
630 : void *p;
631 416 : conversion_obstack_sentinel (): p (conversion_obstack_alloc (0)) {}
632 208 : ~conversion_obstack_sentinel () { obstack_free (&conversion_obstack, p); }
633 : };
634 :
635 : /* Allocate rejection reasons. */
636 :
637 : static struct rejection_reason *
638 254119658 : alloc_rejection (enum rejection_reason_code code)
639 : {
640 254119658 : struct rejection_reason *p;
641 0 : p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
642 254119658 : p->code = code;
643 254119658 : return p;
644 : }
645 :
646 : static struct rejection_reason *
647 62869306 : arity_rejection (tree first_arg, int expected, int actual, bool least_p = false)
648 : {
649 62644627 : struct rejection_reason *r = alloc_rejection (rr_arity);
650 62869306 : int adjust = first_arg != NULL_TREE;
651 62869306 : r->u.arity.expected = expected - adjust;
652 62869306 : r->u.arity.actual = actual - adjust;
653 62869306 : r->u.arity.least_p = least_p;
654 62869306 : return r;
655 : }
656 :
657 : static struct rejection_reason *
658 48899880 : arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to,
659 : location_t loc)
660 : {
661 1641351 : struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
662 48899880 : int adjust = first_arg != NULL_TREE;
663 48899880 : r->u.conversion.n_arg = n_arg - adjust;
664 48899880 : r->u.conversion.from = from;
665 48899880 : r->u.conversion.to_type = to;
666 48899880 : r->u.conversion.loc = loc;
667 48899880 : return r;
668 : }
669 :
670 : static struct rejection_reason *
671 7533964 : bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to,
672 : location_t loc)
673 : {
674 1071 : struct rejection_reason *r = alloc_rejection (rr_bad_arg_conversion);
675 7533964 : int adjust = first_arg != NULL_TREE;
676 7533964 : r->u.bad_conversion.n_arg = n_arg - adjust;
677 7533964 : r->u.bad_conversion.from = from;
678 7533964 : r->u.bad_conversion.to_type = to;
679 7533964 : r->u.bad_conversion.loc = loc;
680 7533964 : return r;
681 : }
682 :
683 : static struct rejection_reason *
684 490 : explicit_conversion_rejection (tree from, tree to)
685 : {
686 490 : struct rejection_reason *r = alloc_rejection (rr_explicit_conversion);
687 490 : r->u.conversion.n_arg = 0;
688 490 : r->u.conversion.from = from;
689 490 : r->u.conversion.to_type = to;
690 490 : r->u.conversion.loc = UNKNOWN_LOCATION;
691 490 : return r;
692 : }
693 :
694 : static struct rejection_reason *
695 13 : template_conversion_rejection (tree from, tree to)
696 : {
697 13 : struct rejection_reason *r = alloc_rejection (rr_template_conversion);
698 13 : r->u.conversion.n_arg = 0;
699 13 : r->u.conversion.from = from;
700 13 : r->u.conversion.to_type = to;
701 13 : r->u.conversion.loc = UNKNOWN_LOCATION;
702 13 : return r;
703 : }
704 :
705 : static struct rejection_reason *
706 134481993 : template_unification_rejection (tree tmpl, tree explicit_targs, tree targs,
707 : const tree *args, unsigned int nargs,
708 : tree return_type, unification_kind_t strict,
709 : int flags)
710 : {
711 134481993 : size_t args_n_bytes = sizeof (*args) * nargs;
712 134481993 : tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes);
713 134481993 : struct rejection_reason *r = alloc_rejection (rr_template_unification);
714 134481993 : r->u.template_unification.tmpl = tmpl;
715 134481993 : r->u.template_unification.explicit_targs = explicit_targs;
716 134481993 : r->u.template_unification.num_targs = TREE_VEC_LENGTH (targs);
717 : /* Copy args to our own storage. */
718 134481993 : memcpy (args1, args, args_n_bytes);
719 134481993 : r->u.template_unification.args = args1;
720 134481993 : r->u.template_unification.nargs = nargs;
721 134481993 : r->u.template_unification.return_type = return_type;
722 134481993 : r->u.template_unification.strict = strict;
723 134481993 : r->u.template_unification.flags = flags;
724 134481993 : return r;
725 : }
726 :
727 : static struct rejection_reason *
728 69 : template_unification_error_rejection (void)
729 : {
730 0 : return alloc_rejection (rr_template_unification);
731 : }
732 :
733 : static struct rejection_reason *
734 87469 : invalid_copy_with_fn_template_rejection (void)
735 : {
736 0 : struct rejection_reason *r = alloc_rejection (rr_invalid_copy);
737 87469 : return r;
738 : }
739 :
740 : static struct rejection_reason *
741 235531 : inherited_ctor_rejection (void)
742 : {
743 0 : struct rejection_reason *r = alloc_rejection (rr_inherited_ctor);
744 235531 : return r;
745 : }
746 :
747 : /* Build a constraint failure record. */
748 :
749 : static struct rejection_reason *
750 10943 : constraint_failure (void)
751 : {
752 0 : struct rejection_reason *r = alloc_rejection (rr_constraint_failure);
753 10943 : return r;
754 : }
755 :
756 : /* Dynamically allocate a conversion. */
757 :
758 : static conversion *
759 1013741464 : alloc_conversion (conversion_kind kind)
760 : {
761 1013741464 : conversion *c;
762 0 : c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
763 1013741464 : c->kind = kind;
764 1013741464 : return c;
765 : }
766 :
767 : /* Make sure that all memory on the conversion obstack has been
768 : freed. */
769 :
770 : void
771 88202 : validate_conversion_obstack (void)
772 : {
773 88202 : if (conversion_obstack_initialized)
774 75217 : gcc_assert ((obstack_next_free (&conversion_obstack)
775 : == obstack_base (&conversion_obstack)));
776 88202 : }
777 :
778 : /* Dynamically allocate an array of N conversions. */
779 :
780 : static conversion **
781 378709692 : alloc_conversions (size_t n)
782 : {
783 0 : return (conversion **) conversion_obstack_alloc (n * sizeof (conversion *));
784 : }
785 :
786 : /* True iff the active member of conversion::u for code CODE is NEXT. */
787 :
788 : static inline bool
789 566462296 : has_next (conversion_kind code)
790 : {
791 535703374 : return !(code == ck_identity
792 566462296 : || code == ck_ambig
793 : || code == ck_list
794 535732690 : || code == ck_aggr
795 566462296 : || code == ck_deferred_bad);
796 : }
797 :
798 : static conversion *
799 285212236 : build_conv (conversion_kind code, tree type, conversion *from)
800 : {
801 285212236 : conversion *t;
802 285212236 : conversion_rank rank = CONVERSION_RANK (from);
803 :
804 : /* Only call this function for conversions that use u.next. */
805 285212236 : gcc_assert (from == NULL || has_next (code));
806 :
807 : /* Note that the caller is responsible for filling in t->cand for
808 : user-defined conversions. */
809 285212236 : t = alloc_conversion (code);
810 285212236 : t->type = type;
811 285212236 : t->u.next = from;
812 :
813 285212236 : switch (code)
814 : {
815 77099961 : case ck_ptr:
816 77099961 : case ck_pmem:
817 77099961 : case ck_base:
818 77099961 : case ck_std:
819 77099961 : if (rank < cr_std)
820 285212236 : rank = cr_std;
821 : break;
822 :
823 17111800 : case ck_qual:
824 17111800 : case ck_fnptr:
825 17111800 : if (rank < cr_exact)
826 285212236 : rank = cr_exact;
827 : break;
828 :
829 : default:
830 : break;
831 : }
832 285212236 : t->rank = rank;
833 285212236 : t->user_conv_p = (code == ck_user || from->user_conv_p);
834 285212236 : t->bad_p = from->bad_p;
835 285212236 : t->base_p = false;
836 285212236 : return t;
837 : }
838 :
839 : /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
840 : specialization of std::initializer_list<T>, if such a conversion is
841 : possible. */
842 :
843 : static conversion *
844 44603 : build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
845 : {
846 44603 : tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
847 44603 : unsigned len = CONSTRUCTOR_NELTS (ctor);
848 44603 : conversion **subconvs = alloc_conversions (len);
849 44603 : conversion *t;
850 44603 : unsigned i;
851 44603 : tree val;
852 :
853 : /* Within a list-initialization we can have more user-defined
854 : conversions. */
855 44603 : flags &= ~LOOKUP_NO_CONVERSION;
856 : /* But no narrowing conversions. */
857 44603 : flags |= LOOKUP_NO_NARROWING;
858 :
859 : /* Can't make an array of these types. */
860 44603 : if (TYPE_REF_P (elttype)
861 44603 : || TREE_CODE (elttype) == FUNCTION_TYPE
862 44597 : || VOID_TYPE_P (elttype))
863 : return NULL;
864 :
865 136018 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
866 : {
867 98864 : conversion *sub
868 98864 : = implicit_conversion (elttype, TREE_TYPE (val), val,
869 : false, flags, complain);
870 98864 : if (sub == NULL)
871 : return NULL;
872 :
873 91421 : subconvs[i] = sub;
874 : }
875 :
876 37154 : t = alloc_conversion (ck_list);
877 37154 : t->type = type;
878 37154 : t->u.list = subconvs;
879 37154 : t->rank = cr_exact;
880 :
881 114937 : for (i = 0; i < len; ++i)
882 : {
883 77783 : conversion *sub = subconvs[i];
884 77783 : if (sub->rank > t->rank)
885 34128 : t->rank = sub->rank;
886 77783 : if (sub->user_conv_p)
887 2579 : t->user_conv_p = true;
888 77783 : if (sub->bad_p)
889 34174 : t->bad_p = true;
890 : }
891 :
892 : return t;
893 : }
894 :
895 : /* Return the next conversion of the conversion chain (if applicable),
896 : or NULL otherwise. Please use this function instead of directly
897 : accessing fields of struct conversion. */
898 :
899 : static conversion *
900 247086978 : next_conversion (conversion *conv)
901 : {
902 247086978 : if (conv == NULL
903 247086978 : || !has_next (conv->kind))
904 : return NULL;
905 243314437 : return conv->u.next;
906 : }
907 :
908 : /* Strip to the first ck_user, ck_ambig, ck_list, ck_aggr or ck_identity
909 : encountered. */
910 :
911 : static conversion *
912 444539 : strip_standard_conversion (conversion *conv)
913 : {
914 444539 : while (conv
915 446759 : && conv->kind != ck_user
916 450274 : && has_next (conv->kind))
917 2220 : conv = next_conversion (conv);
918 444539 : return conv;
919 : }
920 :
921 : /* Subroutine of build_aggr_conv: check whether FROM is a valid aggregate
922 : initializer for array type ATYPE. */
923 :
924 : static bool
925 335 : can_convert_array (tree atype, tree from, int flags, tsubst_flags_t complain)
926 : {
927 335 : tree elttype = TREE_TYPE (atype);
928 335 : unsigned i;
929 :
930 335 : if (TREE_CODE (from) == CONSTRUCTOR)
931 : {
932 805 : for (i = 0; i < CONSTRUCTOR_NELTS (from); ++i)
933 : {
934 509 : tree val = CONSTRUCTOR_ELT (from, i)->value;
935 509 : bool ok;
936 509 : if (TREE_CODE (elttype) == ARRAY_TYPE)
937 18 : ok = can_convert_array (elttype, val, flags, complain);
938 : else
939 491 : ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags,
940 : complain);
941 509 : if (!ok)
942 : return false;
943 : }
944 : return true;
945 : }
946 :
947 39 : if (char_type_p (TYPE_MAIN_VARIANT (elttype))
948 39 : && TREE_CODE (tree_strip_any_location_wrapper (from)) == STRING_CST)
949 39 : return array_string_literal_compatible_p (atype, from);
950 :
951 : /* No other valid way to aggregate initialize an array. */
952 : return false;
953 : }
954 :
955 : /* Helper for build_aggr_conv. Return true if FIELD is in PSET, or if
956 : FIELD has ANON_AGGR_TYPE_P and any initializable field in there recursively
957 : is in PSET. */
958 :
959 : static bool
960 397440 : field_in_pset (hash_set<tree, true> &pset, tree field)
961 : {
962 397440 : if (pset.contains (field))
963 : return true;
964 75 : if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
965 0 : for (field = TYPE_FIELDS (TREE_TYPE (field));
966 0 : field; field = DECL_CHAIN (field))
967 : {
968 0 : field = next_aggregate_field (field);
969 0 : if (field == NULL_TREE)
970 : break;
971 0 : if (field_in_pset (pset, field))
972 : return true;
973 : }
974 : return false;
975 : }
976 :
977 : /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
978 : aggregate class, if such a conversion is possible. */
979 :
980 : static conversion *
981 660514 : build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
982 : {
983 660514 : unsigned HOST_WIDE_INT i = 0;
984 660514 : conversion *c;
985 660514 : tree field = next_aggregate_field (TYPE_FIELDS (type));
986 660514 : tree empty_ctor = NULL_TREE;
987 660514 : hash_set<tree, true> pset;
988 :
989 : /* We already called reshape_init in implicit_conversion, but it might not
990 : have done anything in the case of parenthesized aggr init. */
991 :
992 : /* The conversions within the init-list aren't affected by the enclosing
993 : context; they're always simple copy-initialization. */
994 660514 : flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
995 :
996 : /* For designated initializers, verify that each initializer is convertible
997 : to corresponding TREE_TYPE (ce->index) and mark those FIELD_DECLs as
998 : visited. In the following loop then ignore already visited
999 : FIELD_DECLs. */
1000 660514 : tree idx, val;
1001 1057879 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, val)
1002 : {
1003 397404 : if (!idx)
1004 : break;
1005 :
1006 397392 : gcc_checking_assert (TREE_CODE (idx) == FIELD_DECL);
1007 :
1008 397392 : tree ftype = TREE_TYPE (idx);
1009 397392 : bool ok;
1010 :
1011 397392 : if (TREE_CODE (ftype) == ARRAY_TYPE)
1012 278 : ok = can_convert_array (ftype, val, flags, complain);
1013 : else
1014 397114 : ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
1015 : complain);
1016 :
1017 397392 : if (!ok)
1018 : return NULL;
1019 :
1020 : /* For unions, there should be just one initializer. */
1021 397383 : if (TREE_CODE (type) == UNION_TYPE)
1022 : {
1023 : field = NULL_TREE;
1024 : i = 1;
1025 : break;
1026 : }
1027 397365 : pset.add (idx);
1028 : }
1029 :
1030 1059509 : for (; field; field = next_aggregate_field (DECL_CHAIN (field)))
1031 : {
1032 399040 : tree ftype = TREE_TYPE (field);
1033 399040 : bool ok;
1034 :
1035 399040 : if (!pset.is_empty () && field_in_pset (pset, field))
1036 397365 : continue;
1037 1675 : if (i < CONSTRUCTOR_NELTS (ctor))
1038 : {
1039 3 : constructor_elt *ce = CONSTRUCTOR_ELT (ctor, i);
1040 3 : gcc_checking_assert (!ce->index);
1041 3 : val = ce->value;
1042 3 : ++i;
1043 : }
1044 1672 : else if (DECL_INITIAL (field))
1045 153 : val = get_nsdmi (field, /*ctor*/false, complain);
1046 1519 : else if (TYPE_REF_P (ftype))
1047 : /* Value-initialization of reference is ill-formed. */
1048 : return NULL;
1049 : else
1050 : {
1051 1513 : if (empty_ctor == NULL_TREE)
1052 1083 : empty_ctor = build_constructor (init_list_type_node, NULL);
1053 : val = empty_ctor;
1054 : }
1055 :
1056 1669 : if (TREE_CODE (ftype) == ARRAY_TYPE)
1057 39 : ok = can_convert_array (ftype, val, flags, complain);
1058 : else
1059 1630 : ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
1060 : complain);
1061 :
1062 1669 : if (!ok)
1063 : return NULL;
1064 :
1065 1663 : if (TREE_CODE (type) == UNION_TYPE)
1066 : break;
1067 : }
1068 :
1069 660493 : if (i < CONSTRUCTOR_NELTS (ctor))
1070 : return NULL;
1071 :
1072 660483 : c = alloc_conversion (ck_aggr);
1073 660483 : c->type = type;
1074 660483 : c->rank = cr_exact;
1075 660483 : c->user_conv_p = true;
1076 660483 : c->check_narrowing = true;
1077 660483 : c->u.expr = ctor;
1078 660483 : return c;
1079 660514 : }
1080 :
1081 : /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
1082 : array type, if such a conversion is possible. */
1083 :
1084 : static conversion *
1085 332 : build_array_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
1086 : {
1087 332 : conversion *c;
1088 332 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1089 332 : tree elttype = TREE_TYPE (type);
1090 332 : bool bad = false;
1091 332 : bool user = false;
1092 332 : enum conversion_rank rank = cr_exact;
1093 :
1094 : /* We might need to propagate the size from the element to the array. */
1095 332 : complete_type (type);
1096 :
1097 332 : if (TYPE_DOMAIN (type)
1098 332 : && !variably_modified_type_p (TYPE_DOMAIN (type), NULL_TREE))
1099 : {
1100 306 : unsigned HOST_WIDE_INT alen = tree_to_uhwi (array_type_nelts_top (type));
1101 306 : if (alen < len)
1102 : return NULL;
1103 : }
1104 :
1105 322 : flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1106 :
1107 1430 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
1108 : {
1109 541 : conversion *sub
1110 541 : = implicit_conversion (elttype, TREE_TYPE (e.value), e.value,
1111 : false, flags, complain);
1112 541 : if (sub == NULL)
1113 : return NULL;
1114 :
1115 524 : if (sub->rank > rank)
1116 103 : rank = sub->rank;
1117 524 : if (sub->user_conv_p)
1118 17 : user = true;
1119 524 : if (sub->bad_p)
1120 60 : bad = true;
1121 : }
1122 :
1123 305 : c = alloc_conversion (ck_aggr);
1124 305 : c->type = type;
1125 305 : c->rank = rank;
1126 305 : c->user_conv_p = user;
1127 305 : c->bad_p = bad;
1128 305 : c->u.expr = ctor;
1129 305 : return c;
1130 : }
1131 :
1132 : /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
1133 : complex type, if such a conversion is possible. */
1134 :
1135 : static conversion *
1136 41298 : build_complex_conv (tree type, tree ctor, int flags,
1137 : tsubst_flags_t complain)
1138 : {
1139 41298 : conversion *c;
1140 41298 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1141 41298 : tree elttype = TREE_TYPE (type);
1142 41298 : bool bad = false;
1143 41298 : bool user = false;
1144 41298 : enum conversion_rank rank = cr_exact;
1145 :
1146 41298 : if (len != 2)
1147 : return NULL;
1148 :
1149 41256 : flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1150 :
1151 206280 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
1152 : {
1153 82512 : conversion *sub
1154 82512 : = implicit_conversion (elttype, TREE_TYPE (e.value), e.value,
1155 : false, flags, complain);
1156 82512 : if (sub == NULL)
1157 : return NULL;
1158 :
1159 82512 : if (sub->rank > rank)
1160 44 : rank = sub->rank;
1161 82512 : if (sub->user_conv_p)
1162 0 : user = true;
1163 82512 : if (sub->bad_p)
1164 0 : bad = true;
1165 : }
1166 :
1167 41256 : c = alloc_conversion (ck_aggr);
1168 41256 : c->type = type;
1169 41256 : c->rank = rank;
1170 41256 : c->user_conv_p = user;
1171 41256 : c->bad_p = bad;
1172 41256 : c->u.expr = ctor;
1173 41256 : return c;
1174 : }
1175 :
1176 : /* Build a representation of the identity conversion from EXPR to
1177 : itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
1178 :
1179 : static conversion *
1180 725378987 : build_identity_conv (tree type, tree expr)
1181 : {
1182 725378987 : conversion *c;
1183 :
1184 0 : c = alloc_conversion (ck_identity);
1185 725378987 : c->type = type;
1186 725378987 : c->u.expr = expr;
1187 :
1188 725378987 : return c;
1189 : }
1190 :
1191 : /* Converting from EXPR to TYPE was ambiguous in the sense that there
1192 : were multiple user-defined conversions to accomplish the job.
1193 : Build a conversion that indicates that ambiguity. */
1194 :
1195 : static conversion *
1196 1368 : build_ambiguous_conv (tree type, tree expr)
1197 : {
1198 1368 : conversion *c;
1199 :
1200 0 : c = alloc_conversion (ck_ambig);
1201 1368 : c->type = type;
1202 1368 : c->u.expr = expr;
1203 :
1204 1368 : return c;
1205 : }
1206 :
1207 : tree
1208 1357337465 : strip_top_quals (tree t)
1209 : {
1210 1357337465 : if (TREE_CODE (t) == ARRAY_TYPE)
1211 : return t;
1212 1354450694 : return cp_build_qualified_type (t, 0);
1213 : }
1214 :
1215 : /* Returns the standard conversion path (see [conv]) from type FROM to type
1216 : TO, if any. For proper handling of null pointer constants, you must
1217 : also pass the expression EXPR to convert from. If C_CAST_P is true,
1218 : this conversion is coming from a C-style cast. */
1219 :
1220 : static conversion *
1221 600936161 : standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
1222 : int flags, tsubst_flags_t complain)
1223 : {
1224 600936161 : enum tree_code fcode, tcode;
1225 600936161 : conversion *conv;
1226 600936161 : bool fromref = false;
1227 600936161 : tree qualified_to;
1228 :
1229 600936161 : to = non_reference (to);
1230 600936161 : if (TYPE_REF_P (from))
1231 : {
1232 28448 : fromref = true;
1233 28448 : from = TREE_TYPE (from);
1234 : }
1235 600936161 : qualified_to = to;
1236 600936161 : to = strip_top_quals (to);
1237 600936161 : from = strip_top_quals (from);
1238 :
1239 600936161 : if (expr && type_unknown_p (expr))
1240 : {
1241 107188 : if (TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
1242 : {
1243 15603 : tsubst_flags_t tflags = tf_conv;
1244 15603 : expr = instantiate_type (to, expr, tflags);
1245 15603 : if (expr == error_mark_node)
1246 : return NULL;
1247 11590 : from = TREE_TYPE (expr);
1248 : }
1249 91585 : else if (TREE_CODE (to) == BOOLEAN_TYPE)
1250 : {
1251 : /* Necessary for eg, TEMPLATE_ID_EXPRs (c++/50961). */
1252 1905 : expr = resolve_nondeduced_context (expr, complain);
1253 1905 : from = TREE_TYPE (expr);
1254 : }
1255 : }
1256 :
1257 600932148 : fcode = TREE_CODE (from);
1258 600932148 : tcode = TREE_CODE (to);
1259 :
1260 600932148 : conv = build_identity_conv (from, expr);
1261 600932148 : if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
1262 : {
1263 2908508 : from = type_decays_to (from);
1264 2908508 : fcode = TREE_CODE (from);
1265 : /* Tell convert_like that we're using the address. */
1266 2908508 : conv->rvaluedness_matches_p = true;
1267 2908508 : conv = build_conv (ck_lvalue, from, conv);
1268 : }
1269 : /* Wrapping a ck_rvalue around a class prvalue (as a result of using
1270 : obvalue_p) seems odd, since it's already a prvalue, but that's how we
1271 : express the copy constructor call required by copy-initialization. */
1272 598023640 : else if (fromref || (expr && obvalue_p (expr)))
1273 : {
1274 141850544 : if (expr)
1275 : {
1276 141822282 : tree bitfield_type;
1277 141822282 : bitfield_type = is_bitfield_expr_with_lowered_type (expr);
1278 141822282 : if (bitfield_type)
1279 : {
1280 245434 : from = strip_top_quals (bitfield_type);
1281 245434 : fcode = TREE_CODE (from);
1282 : }
1283 : }
1284 141850544 : conv = build_conv (ck_rvalue, from, conv);
1285 : /* If we're performing copy-initialization, remember to skip
1286 : explicit constructors. */
1287 141850544 : if (flags & LOOKUP_ONLYCONVERTING)
1288 119340592 : conv->copy_init_p = true;
1289 : }
1290 :
1291 : /* Allow conversion between `__complex__' data types. */
1292 600932148 : if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
1293 : {
1294 : /* The standard conversion sequence to convert FROM to TO is
1295 : the standard conversion sequence to perform componentwise
1296 : conversion. */
1297 1539680 : conversion *part_conv = standard_conversion
1298 1539680 : (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags,
1299 : complain);
1300 :
1301 1539680 : if (!part_conv)
1302 : conv = NULL;
1303 1539680 : else if (part_conv->kind == ck_identity)
1304 : /* Leave conv alone. */;
1305 : else
1306 : {
1307 84843 : conv = build_conv (part_conv->kind, to, conv);
1308 84843 : conv->rank = part_conv->rank;
1309 : }
1310 :
1311 1539680 : return conv;
1312 : }
1313 :
1314 599392468 : if (same_type_p (from, to))
1315 : {
1316 411795248 : if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue)
1317 6849542 : conv->type = qualified_to;
1318 411795248 : return conv;
1319 : }
1320 :
1321 : /* [conv.ptr]
1322 : A null pointer constant can be converted to a pointer type; ... A
1323 : null pointer constant of integral type can be converted to an
1324 : rvalue of type std::nullptr_t. */
1325 119364958 : if ((tcode == POINTER_TYPE || TYPE_PTRMEM_P (to)
1326 119299936 : || NULLPTR_TYPE_P (to))
1327 188072597 : && ((expr && null_ptr_cst_p (expr))
1328 66664961 : || NULLPTR_TYPE_P (from)))
1329 2042682 : conv = build_conv (ck_std, to, conv);
1330 185554538 : else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
1331 185265200 : || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
1332 : {
1333 : /* For backwards brain damage compatibility, allow interconversion of
1334 : pointers and integers with a pedwarn. */
1335 1176443 : conv = build_conv (ck_std, to, conv);
1336 1176443 : conv->bad_p = true;
1337 : }
1338 184378095 : else if (UNSCOPED_ENUM_P (to) && fcode == INTEGER_TYPE)
1339 : {
1340 : /* For backwards brain damage compatibility, allow interconversion of
1341 : enums and integers with a pedwarn. */
1342 342282 : conv = build_conv (ck_std, to, conv);
1343 342282 : conv->bad_p = true;
1344 : }
1345 184035813 : else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
1346 119941774 : || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from)))
1347 : {
1348 694 : tree to_pointee;
1349 694 : tree from_pointee;
1350 :
1351 694 : if (tcode == POINTER_TYPE)
1352 : {
1353 64094039 : to_pointee = TREE_TYPE (to);
1354 64094039 : from_pointee = TREE_TYPE (from);
1355 :
1356 : /* Since this is the target of a pointer, it can't have function
1357 : qualifiers, so any TYPE_QUALS must be for attributes const or
1358 : noreturn. Strip them. */
1359 64094039 : if (TREE_CODE (to_pointee) == FUNCTION_TYPE
1360 64094039 : && TYPE_QUALS (to_pointee))
1361 12 : to_pointee = build_qualified_type (to_pointee, TYPE_UNQUALIFIED);
1362 64094039 : if (TREE_CODE (from_pointee) == FUNCTION_TYPE
1363 64094039 : && TYPE_QUALS (from_pointee))
1364 18 : from_pointee = build_qualified_type (from_pointee, TYPE_UNQUALIFIED);
1365 : }
1366 : else
1367 : {
1368 694 : to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
1369 694 : from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
1370 : }
1371 :
1372 64094733 : if (tcode == POINTER_TYPE
1373 64094733 : && same_type_ignoring_top_level_qualifiers_p (from_pointee,
1374 : to_pointee))
1375 : ;
1376 45220490 : else if (VOID_TYPE_P (to_pointee)
1377 2182933 : && !TYPE_PTRDATAMEM_P (from)
1378 2182933 : && TREE_CODE (from_pointee) != FUNCTION_TYPE)
1379 : {
1380 2182670 : tree nfrom = TREE_TYPE (from);
1381 : /* Don't try to apply restrict to void. */
1382 2182670 : int quals = cp_type_quals (nfrom) & ~TYPE_QUAL_RESTRICT;
1383 2182670 : from_pointee = cp_build_qualified_type (void_type_node, quals);
1384 2182670 : from = build_pointer_type (from_pointee);
1385 2182670 : conv = build_conv (ck_ptr, from, conv);
1386 2182670 : }
1387 43037820 : else if (TYPE_PTRDATAMEM_P (from))
1388 : {
1389 694 : tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
1390 694 : tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
1391 :
1392 694 : if (same_type_p (fbase, tbase))
1393 : /* No base conversion needed. */;
1394 585 : else if (DERIVED_FROM_P (fbase, tbase)
1395 1006 : && (same_type_ignoring_top_level_qualifiers_p
1396 421 : (from_pointee, to_pointee)))
1397 : {
1398 393 : from = build_ptrmem_type (tbase, from_pointee);
1399 393 : conv = build_conv (ck_pmem, from, conv);
1400 : }
1401 : else
1402 192 : return NULL;
1403 : }
1404 15383430 : else if (CLASS_TYPE_P (from_pointee)
1405 15381582 : && CLASS_TYPE_P (to_pointee)
1406 : /* [conv.ptr]
1407 :
1408 : An rvalue of type "pointer to cv D," where D is a
1409 : class type, can be converted to an rvalue of type
1410 : "pointer to cv B," where B is a base class (clause
1411 : _class.derived_) of D. If B is an inaccessible
1412 : (clause _class.access_) or ambiguous
1413 : (_class.member.lookup_) base class of D, a program
1414 : that necessitates this conversion is ill-formed.
1415 : Therefore, we use DERIVED_FROM_P, and do not check
1416 : access or uniqueness. */
1417 58364708 : && DERIVED_FROM_P (to_pointee, from_pointee))
1418 : {
1419 3062841 : from_pointee
1420 3062841 : = cp_build_qualified_type (to_pointee,
1421 : cp_type_quals (from_pointee));
1422 3062841 : from = build_pointer_type (from_pointee);
1423 3062841 : conv = build_conv (ck_ptr, from, conv);
1424 3062841 : conv->base_p = true;
1425 : }
1426 :
1427 64094541 : if (same_type_p (from, to))
1428 : /* OK */;
1429 60030856 : else if (c_cast_p && comp_ptr_ttypes_const (to, from, bounds_either))
1430 : /* In a C-style cast, we ignore CV-qualification because we
1431 : are allowed to perform a static_cast followed by a
1432 : const_cast. */
1433 508 : conv = build_conv (ck_qual, to, conv);
1434 60030348 : else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
1435 16869614 : conv = build_conv (ck_qual, to, conv);
1436 43160734 : else if (expr && string_conv_p (to, expr, 0))
1437 : /* converting from string constant to char *. */
1438 235 : conv = build_conv (ck_qual, to, conv);
1439 43160499 : else if (fnptr_conv_p (to, from))
1440 232632 : conv = build_conv (ck_fnptr, to, conv);
1441 : /* Allow conversions among compatible ObjC pointer types (base
1442 : conversions have been already handled above). */
1443 42927867 : else if (c_dialect_objc ()
1444 42927867 : && objc_compare_types (to, from, -4, NULL_TREE))
1445 0 : conv = build_conv (ck_ptr, to, conv);
1446 42927867 : else if (ptr_reasonably_similar (to_pointee, from_pointee))
1447 : {
1448 3894873 : conv = build_conv (ck_ptr, to, conv);
1449 3894873 : conv->bad_p = true;
1450 : }
1451 : else
1452 : return NULL;
1453 :
1454 86112554 : from = to;
1455 : }
1456 119941080 : else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
1457 : {
1458 63121 : tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
1459 63121 : tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
1460 63121 : tree fbase = class_of_this_parm (fromfn);
1461 63121 : tree tbase = class_of_this_parm (tofn);
1462 :
1463 : /* If FBASE and TBASE are equivalent but incomplete, DERIVED_FROM_P
1464 : yields false. But a pointer to member of incomplete class is OK. */
1465 63121 : if (!same_type_p (fbase, tbase) && !DERIVED_FROM_P (fbase, tbase))
1466 : return NULL;
1467 :
1468 62929 : tree fstat = static_fn_type (fromfn);
1469 62929 : tree tstat = static_fn_type (tofn);
1470 62929 : if (same_type_p (tstat, fstat)
1471 62929 : || fnptr_conv_p (tstat, fstat))
1472 : /* OK */;
1473 : else
1474 : return NULL;
1475 :
1476 62670 : if (!same_type_p (fbase, tbase))
1477 : {
1478 62610 : from = build_memfn_type (fstat,
1479 : tbase,
1480 : cp_type_quals (tbase),
1481 : type_memfn_rqual (tofn));
1482 62610 : from = build_ptrmemfunc_type (build_pointer_type (from));
1483 62610 : conv = build_conv (ck_pmem, from, conv);
1484 62610 : conv->base_p = true;
1485 : }
1486 62670 : if (fnptr_conv_p (tstat, fstat))
1487 60 : conv = build_conv (ck_fnptr, to, conv);
1488 : }
1489 119877959 : else if (tcode == BOOLEAN_TYPE)
1490 : {
1491 : /* [conv.bool]
1492 :
1493 : A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
1494 : to member type can be converted to a prvalue of type bool. ...
1495 : For direct-initialization (8.5 [dcl.init]), a prvalue of type
1496 : std::nullptr_t can be converted to a prvalue of type bool; */
1497 3774095 : if (ARITHMETIC_TYPE_P (from)
1498 3774079 : || UNSCOPED_ENUM_P (from)
1499 2265044 : || fcode == POINTER_TYPE
1500 1535268 : || TYPE_PTRMEM_P (from)
1501 8154020 : || NULLPTR_TYPE_P (from))
1502 : {
1503 5084039 : conv = build_conv (ck_std, to, conv);
1504 5084039 : if (fcode == POINTER_TYPE
1505 4354263 : || TYPE_PTRDATAMEM_P (from)
1506 4354111 : || (TYPE_PTRMEMFUNC_P (from)
1507 88 : && conv->rank < cr_pbool)
1508 9438062 : || NULLPTR_TYPE_P (from))
1509 730091 : conv->rank = cr_pbool;
1510 5084039 : if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING))
1511 36 : conv->bad_p = true;
1512 5084039 : if (flags & LOOKUP_NO_NARROWING)
1513 23769 : conv->check_narrowing = true;
1514 5084039 : return conv;
1515 : }
1516 :
1517 : return NULL;
1518 : }
1519 : /* We don't check for ENUMERAL_TYPE here because there are no standard
1520 : conversions to enum type. */
1521 : /* As an extension, allow conversion to complex type. */
1522 113258967 : else if (ARITHMETIC_TYPE_P (to))
1523 : {
1524 16049974 : if (! (INTEGRAL_CODE_P (fcode)
1525 14763080 : || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL)))
1526 73712190 : || SCOPED_ENUM_P (from))
1527 : return NULL;
1528 :
1529 : /* If we're parsing an enum with no fixed underlying type, we're
1530 : dealing with an incomplete type, which renders the conversion
1531 : ill-formed. */
1532 57111633 : if (!COMPLETE_TYPE_P (from))
1533 : return NULL;
1534 :
1535 57111627 : conv = build_conv (ck_std, to, conv);
1536 :
1537 57111627 : tree underlying_type = NULL_TREE;
1538 57111627 : if (TREE_CODE (from) == ENUMERAL_TYPE
1539 57111627 : && ENUM_FIXED_UNDERLYING_TYPE_P (from))
1540 1143337 : underlying_type = ENUM_UNDERLYING_TYPE (from);
1541 :
1542 : /* Give this a better rank if it's a promotion.
1543 :
1544 : To handle CWG 1601, also bump the rank if we are converting
1545 : an enumeration with a fixed underlying type to the underlying
1546 : type. */
1547 57111627 : if ((same_type_p (to, type_promotes_to (from))
1548 48353454 : || (underlying_type && same_type_p (to, underlying_type)))
1549 57111645 : && next_conversion (conv)->rank <= cr_promotion)
1550 8758191 : conv->rank = cr_promotion;
1551 :
1552 : /* A prvalue of floating-point type can be converted to a prvalue of
1553 : another floating-point type with a greater or equal conversion
1554 : rank ([conv.rank]). A prvalue of standard floating-point type can
1555 : be converted to a prvalue of another standard floating-point type.
1556 : For backwards compatibility with handling __float128 and other
1557 : non-standard floating point types, allow all implicit floating
1558 : point conversions if neither type is extended floating-point
1559 : type and if at least one of them is, fail if they have unordered
1560 : conversion rank or from has higher conversion rank. */
1561 57111627 : if (fcode == REAL_TYPE
1562 57111627 : && tcode == REAL_TYPE
1563 9523471 : && (extended_float_type_p (from)
1564 9420030 : || extended_float_type_p (to))
1565 57261240 : && cp_compare_floating_point_conversion_ranks (from, to) >= 2)
1566 27535 : conv->bad_p = true;
1567 : }
1568 54309857 : else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
1569 54309857 : && vector_types_convertible_p (from, to, false))
1570 4506 : return build_conv (ck_std, to, conv);
1571 54305351 : else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
1572 68935200 : && is_properly_derived_from (from, to))
1573 : {
1574 315303 : if (conv->kind == ck_rvalue)
1575 315248 : conv = next_conversion (conv);
1576 315303 : conv = build_conv (ck_base, to, conv);
1577 : /* The derived-to-base conversion indicates the initialization
1578 : of a parameter with base type from an object of a derived
1579 : type. A temporary object is created to hold the result of
1580 : the conversion unless we're binding directly to a reference. */
1581 315303 : conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
1582 : /* If we're performing copy-initialization, remember to skip
1583 : explicit constructors. */
1584 315303 : if (flags & LOOKUP_ONLYCONVERTING)
1585 315240 : conv->copy_init_p = true;
1586 : }
1587 : else
1588 53990048 : return NULL;
1589 :
1590 86112554 : if (flags & LOOKUP_NO_NARROWING)
1591 1884635 : conv->check_narrowing = true;
1592 :
1593 : return conv;
1594 : }
1595 :
1596 : /* Returns nonzero if T1 is reference-related to T2. */
1597 :
1598 : bool
1599 84449244 : reference_related_p (tree t1, tree t2)
1600 : {
1601 84449244 : if (t1 == error_mark_node || t2 == error_mark_node)
1602 : return false;
1603 :
1604 84449238 : t1 = TYPE_MAIN_VARIANT (t1);
1605 84449238 : t2 = TYPE_MAIN_VARIANT (t2);
1606 :
1607 : /* [dcl.init.ref]
1608 :
1609 : Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
1610 : to "cv2 T2" if T1 is similar to T2, or T1 is a base class of T2. */
1611 84449238 : return (similar_type_p (t1, t2)
1612 84449238 : || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1613 15368960 : && DERIVED_FROM_P (t1, t2)));
1614 : }
1615 :
1616 : /* Returns nonzero if T1 is reference-compatible with T2. */
1617 :
1618 : bool
1619 73339341 : reference_compatible_p (tree t1, tree t2)
1620 : {
1621 : /* [dcl.init.ref]
1622 :
1623 : "cv1 T1" is reference compatible with "cv2 T2" if
1624 : a prvalue of type "pointer to cv2 T2" can be converted to the type
1625 : "pointer to cv1 T1" via a standard conversion sequence. */
1626 73339341 : tree ptype1 = build_pointer_type (t1);
1627 73339341 : tree ptype2 = build_pointer_type (t2);
1628 73339341 : conversion *conv = standard_conversion (ptype1, ptype2, NULL_TREE,
1629 : /*c_cast_p=*/false, 0, tf_none);
1630 73339341 : if (!conv || conv->bad_p)
1631 41683786 : return false;
1632 : return true;
1633 : }
1634 :
1635 : /* Return true if converting FROM to TO would involve a qualification
1636 : conversion. */
1637 :
1638 : static bool
1639 35324925 : involves_qualification_conversion_p (tree to, tree from)
1640 : {
1641 : /* If we're not convering a pointer to another one, we won't get
1642 : a qualification conversion. */
1643 35324925 : if (!((TYPE_PTR_P (to) && TYPE_PTR_P (from))
1644 1618 : || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from))))
1645 : return false;
1646 :
1647 1546984 : conversion *conv = standard_conversion (to, from, NULL_TREE,
1648 : /*c_cast_p=*/false, 0, tf_none);
1649 3085231 : for (conversion *t = conv; t; t = next_conversion (t))
1650 1546998 : if (t->kind == ck_qual)
1651 : return true;
1652 :
1653 : return false;
1654 : }
1655 :
1656 : /* A reference of the indicated TYPE is being bound directly to the
1657 : expression represented by the implicit conversion sequence CONV.
1658 : Return a conversion sequence for this binding. */
1659 :
1660 : static conversion *
1661 37059766 : direct_reference_binding (tree type, conversion *conv)
1662 : {
1663 37059766 : tree t;
1664 :
1665 37059766 : gcc_assert (TYPE_REF_P (type));
1666 37059766 : gcc_assert (!TYPE_REF_P (conv->type));
1667 :
1668 37059766 : t = TREE_TYPE (type);
1669 :
1670 37059766 : if (conv->kind == ck_identity)
1671 : /* Mark the identity conv as to not decay to rvalue. */
1672 37059766 : conv->rvaluedness_matches_p = true;
1673 :
1674 : /* [over.ics.rank]
1675 :
1676 : When a parameter of reference type binds directly
1677 : (_dcl.init.ref_) to an argument expression, the implicit
1678 : conversion sequence is the identity conversion, unless the
1679 : argument expression has a type that is a derived class of the
1680 : parameter type, in which case the implicit conversion sequence is
1681 : a derived-to-base Conversion.
1682 :
1683 : If the parameter binds directly to the result of applying a
1684 : conversion function to the argument expression, the implicit
1685 : conversion sequence is a user-defined conversion sequence
1686 : (_over.ics.user_), with the second standard conversion sequence
1687 : either an identity conversion or, if the conversion function
1688 : returns an entity of a type that is a derived class of the
1689 : parameter type, a derived-to-base conversion. */
1690 37059766 : if (is_properly_derived_from (conv->type, t))
1691 : {
1692 : /* Represent the derived-to-base conversion. */
1693 1734841 : conv = build_conv (ck_base, t, conv);
1694 : /* We will actually be binding to the base-class subobject in
1695 : the derived class, so we mark this conversion appropriately.
1696 : That way, convert_like knows not to generate a temporary. */
1697 1734841 : conv->need_temporary_p = false;
1698 : }
1699 35324925 : else if (involves_qualification_conversion_p (t, conv->type))
1700 : /* Represent the qualification conversion. After DR 2352
1701 : #1 and #2 were indistinguishable conversion sequences:
1702 :
1703 : void f(int*); // #1
1704 : void f(const int* const &); // #2
1705 : void g(int* p) { f(p); }
1706 :
1707 : because the types "int *" and "const int *const" are
1708 : reference-related and we were binding both directly and they
1709 : had the same rank. To break it up, we add a ck_qual under the
1710 : ck_ref_bind so that conversion sequence ranking chooses #1.
1711 :
1712 : We strip_top_quals here which is also what standard_conversion
1713 : does. Failure to do so would confuse comp_cv_qual_signature
1714 : into thinking that in
1715 :
1716 : void f(const int * const &); // #1
1717 : void f(const int *); // #2
1718 : int *x;
1719 : f(x);
1720 :
1721 : #2 is a better match than #1 even though they're ambiguous (97296). */
1722 8751 : conv = build_conv (ck_qual, strip_top_quals (t), conv);
1723 :
1724 37059766 : return build_conv (ck_ref_bind, type, conv);
1725 : }
1726 :
1727 : /* Returns the conversion path from type FROM to reference type TO for
1728 : purposes of reference binding. For lvalue binding, either pass a
1729 : reference type to FROM or an lvalue expression to EXPR. If the
1730 : reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1731 : the conversion returned. If C_CAST_P is true, this
1732 : conversion is coming from a C-style cast. */
1733 :
1734 : static conversion *
1735 73157150 : reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags,
1736 : tsubst_flags_t complain)
1737 : {
1738 73157150 : conversion *conv = NULL;
1739 73157150 : tree to = TREE_TYPE (rto);
1740 73157150 : tree from = rfrom;
1741 73157150 : tree tfrom;
1742 73157150 : bool related_p;
1743 73157150 : bool compatible_p;
1744 73157150 : cp_lvalue_kind gl_kind;
1745 73157150 : bool is_lvalue;
1746 :
1747 73157150 : if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1748 : {
1749 52 : expr = instantiate_type (to, expr, tf_none);
1750 52 : if (expr == error_mark_node)
1751 : return NULL;
1752 52 : from = TREE_TYPE (expr);
1753 : }
1754 :
1755 73157150 : bool copy_list_init = false;
1756 73157150 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1757 : {
1758 106250 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
1759 : /* DR 1288: Otherwise, if the initializer list has a single element
1760 : of type E and ... [T's] referenced type is reference-related to E,
1761 : the object or reference is initialized from that element...
1762 :
1763 : ??? With P0388R4, we should bind 't' directly to U{}:
1764 : using U = A[2];
1765 : A (&&t)[] = {U{}};
1766 : because A[] and A[2] are reference-related. But we don't do it
1767 : because grok_reference_init has deduced the array size (to 1), and
1768 : A[1] and A[2] aren't reference-related. */
1769 106250 : if (CONSTRUCTOR_NELTS (expr) == 1
1770 4051 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
1771 : {
1772 1076 : tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1773 1076 : if (error_operand_p (elt))
1774 : return NULL;
1775 1073 : tree etype = TREE_TYPE (elt);
1776 1073 : if (reference_related_p (to, etype))
1777 : {
1778 181 : expr = elt;
1779 181 : from = etype;
1780 181 : goto skip;
1781 : }
1782 : }
1783 : /* Otherwise, if T is a reference type, a prvalue temporary of the type
1784 : referenced by T is copy-list-initialized, and the reference is bound
1785 : to that temporary. */
1786 : copy_list_init = true;
1787 73157147 : skip:;
1788 : }
1789 :
1790 73157147 : if (TYPE_REF_P (from))
1791 : {
1792 36243 : from = TREE_TYPE (from);
1793 36243 : if (!TYPE_REF_IS_RVALUE (rfrom)
1794 36243 : || TREE_CODE (from) == FUNCTION_TYPE)
1795 : gl_kind = clk_ordinary;
1796 : else
1797 : gl_kind = clk_rvalueref;
1798 : }
1799 73120904 : else if (expr)
1800 72199507 : gl_kind = lvalue_kind (expr);
1801 879884 : else if (CLASS_TYPE_P (from)
1802 921397 : || TREE_CODE (from) == ARRAY_TYPE)
1803 : gl_kind = clk_class;
1804 : else
1805 : gl_kind = clk_none;
1806 :
1807 : /* Don't allow a class prvalue when LOOKUP_NO_TEMP_BIND. */
1808 73157147 : if ((flags & LOOKUP_NO_TEMP_BIND)
1809 909262 : && (gl_kind & clk_class))
1810 : gl_kind = clk_none;
1811 :
1812 : /* Same mask as real_lvalue_p. */
1813 72388759 : is_lvalue = gl_kind && !(gl_kind & (clk_rvalueref|clk_class));
1814 :
1815 73157147 : tfrom = from;
1816 73157147 : if ((gl_kind & clk_bitfield) != 0)
1817 108804 : tfrom = unlowered_expr_type (expr);
1818 :
1819 : /* Figure out whether or not the types are reference-related and
1820 : reference compatible. We have to do this after stripping
1821 : references from FROM. */
1822 73157147 : related_p = reference_related_p (to, tfrom);
1823 : /* If this is a C cast, first convert to an appropriately qualified
1824 : type, so that we can later do a const_cast to the desired type. */
1825 73157147 : if (related_p && c_cast_p
1826 73157147 : && !at_least_as_qualified_p (to, tfrom))
1827 67 : to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1828 73157147 : compatible_p = reference_compatible_p (to, tfrom);
1829 :
1830 : /* Directly bind reference when target expression's type is compatible with
1831 : the reference and expression is an lvalue. In DR391, the wording in
1832 : [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1833 : const and rvalue references to rvalues of compatible class type.
1834 : We should also do direct bindings for non-class xvalues. */
1835 73157147 : if ((related_p || compatible_p) && gl_kind)
1836 : {
1837 : /* [dcl.init.ref]
1838 :
1839 : If the initializer expression
1840 :
1841 : -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1842 : is reference-compatible with "cv2 T2,"
1843 :
1844 : the reference is bound directly to the initializer expression
1845 : lvalue.
1846 :
1847 : [...]
1848 : If the initializer expression is an rvalue, with T2 a class type,
1849 : and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1850 : is bound to the object represented by the rvalue or to a sub-object
1851 : within that object. */
1852 :
1853 31878428 : conv = build_identity_conv (tfrom, expr);
1854 31878428 : conv = direct_reference_binding (rto, conv);
1855 :
1856 31878428 : if (TYPE_REF_P (rfrom))
1857 : /* Handle rvalue reference to function properly. */
1858 8278 : conv->rvaluedness_matches_p
1859 8278 : = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom));
1860 : else
1861 31870150 : conv->rvaluedness_matches_p
1862 31870150 : = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
1863 :
1864 31878428 : if ((gl_kind & clk_bitfield) != 0
1865 31878428 : || ((gl_kind & clk_packed) != 0 && !TYPE_PACKED (to)))
1866 : /* For the purposes of overload resolution, we ignore the fact
1867 : this expression is a bitfield or packed field. (In particular,
1868 : [over.ics.ref] says specifically that a function with a
1869 : non-const reference parameter is viable even if the
1870 : argument is a bitfield.)
1871 :
1872 : However, when we actually call the function we must create
1873 : a temporary to which to bind the reference. If the
1874 : reference is volatile, or isn't const, then we cannot make
1875 : a temporary, so we just issue an error when the conversion
1876 : actually occurs. */
1877 130 : conv->need_temporary_p = true;
1878 :
1879 : /* Don't allow binding of lvalues (other than function lvalues) to
1880 : rvalue references. */
1881 22870874 : if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
1882 35490432 : && TREE_CODE (to) != FUNCTION_TYPE)
1883 3611243 : conv->bad_p = true;
1884 :
1885 : /* Nor the reverse. */
1886 9007554 : if (!is_lvalue && !TYPE_REF_IS_RVALUE (rto)
1887 : /* Unless it's really a C++20 lvalue being treated as an xvalue.
1888 : But in C++23, such an expression is just an xvalue, not a special
1889 : lvalue, so the binding is once again ill-formed. */
1890 4889114 : && !(cxx_dialect <= cxx20
1891 4845275 : && (gl_kind & clk_implicit_rval))
1892 4409689 : && (!CP_TYPE_CONST_NON_VOLATILE_P (to)
1893 4396948 : || (flags & LOOKUP_NO_RVAL_BIND))
1894 31891169 : && TREE_CODE (to) != FUNCTION_TYPE)
1895 12741 : conv->bad_p = true;
1896 :
1897 31878428 : if (!compatible_p)
1898 2512898 : conv->bad_p = true;
1899 :
1900 31878428 : return conv;
1901 : }
1902 : /* [class.conv.fct] A conversion function is never used to convert a
1903 : (possibly cv-qualified) object to the (possibly cv-qualified) same
1904 : object type (or a reference to it), to a (possibly cv-qualified) base
1905 : class of that type (or a reference to it).... */
1906 12446048 : else if (CLASS_TYPE_P (from) && !related_p
1907 53548752 : && !(flags & LOOKUP_NO_CONVERSION))
1908 : {
1909 : /* [dcl.init.ref]
1910 :
1911 : If the initializer expression
1912 :
1913 : -- has a class type (i.e., T2 is a class type) can be
1914 : implicitly converted to an lvalue of type "cv3 T3," where
1915 : "cv1 T1" is reference-compatible with "cv3 T3". (this
1916 : conversion is selected by enumerating the applicable
1917 : conversion functions (_over.match.ref_) and choosing the
1918 : best one through overload resolution. (_over.match_).
1919 :
1920 : the reference is bound to the lvalue result of the conversion
1921 : in the second case. */
1922 3460634 : z_candidate *cand = build_user_type_conversion_1 (rto, expr, flags,
1923 : complain);
1924 3460634 : if (cand)
1925 7858 : return cand->second_conv;
1926 : }
1927 :
1928 : /* From this point on, we conceptually need temporaries, even if we
1929 : elide them. Only the cases above are "direct bindings". */
1930 41270861 : if (flags & LOOKUP_NO_TEMP_BIND)
1931 : return NULL;
1932 :
1933 : /* [over.ics.rank]
1934 :
1935 : When a parameter of reference type is not bound directly to an
1936 : argument expression, the conversion sequence is the one required
1937 : to convert the argument expression to the underlying type of the
1938 : reference according to _over.best.ics_. Conceptually, this
1939 : conversion sequence corresponds to copy-initializing a temporary
1940 : of the underlying type with the argument expression. Any
1941 : difference in top-level cv-qualification is subsumed by the
1942 : initialization itself and does not constitute a conversion. */
1943 :
1944 40388470 : bool maybe_valid_p = true;
1945 :
1946 : /* [dcl.init.ref]
1947 :
1948 : Otherwise, the reference shall be an lvalue reference to a
1949 : non-volatile const type, or the reference shall be an rvalue
1950 : reference. */
1951 52039996 : if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto))
1952 : maybe_valid_p = false;
1953 :
1954 : /* [dcl.init.ref]
1955 :
1956 : Otherwise, a temporary of type "cv1 T1" is created and
1957 : initialized from the initializer expression using the rules for a
1958 : non-reference copy initialization. If T1 is reference-related to
1959 : T2, cv1 must be the same cv-qualification as, or greater
1960 : cv-qualification than, cv2; otherwise, the program is ill-formed. */
1961 40388470 : if (related_p && !at_least_as_qualified_p (to, from))
1962 : maybe_valid_p = false;
1963 :
1964 : /* We try below to treat an invalid reference binding as a bad conversion
1965 : to improve diagnostics, but doing so may cause otherwise unnecessary
1966 : instantiations that can lead to a hard error. So during the first pass
1967 : of overload resolution wherein we shortcut bad conversions, instead just
1968 : produce a special conversion indicating a second pass is necessary if
1969 : there's no strictly viable candidate. */
1970 40388470 : if (!maybe_valid_p && (flags & LOOKUP_SHORTCUT_BAD_CONVS))
1971 : {
1972 2409675 : conv = alloc_conversion (ck_deferred_bad);
1973 2409675 : conv->bad_p = true;
1974 2409675 : return conv;
1975 : }
1976 :
1977 : /* We're generating a temporary now, but don't bind any more in the
1978 : conversion (specifically, don't slice the temporary returned by a
1979 : conversion operator). */
1980 37978795 : flags |= LOOKUP_NO_TEMP_BIND;
1981 :
1982 : /* Core issue 899: When [copy-]initializing a temporary to be bound
1983 : to the first parameter of a copy constructor (12.8) called with
1984 : a single argument in the context of direct-initialization,
1985 : explicit conversion functions are also considered.
1986 :
1987 : So don't set LOOKUP_ONLYCONVERTING in that case. */
1988 37978795 : if (!(flags & LOOKUP_COPY_PARM))
1989 33067367 : flags |= LOOKUP_ONLYCONVERTING;
1990 :
1991 37978795 : if (!conv)
1992 37978795 : conv = implicit_conversion (to, from, expr, c_cast_p,
1993 : flags, complain);
1994 37978795 : if (!conv)
1995 : return NULL;
1996 :
1997 4405065 : if (conv->user_conv_p)
1998 : {
1999 2366826 : if (copy_list_init)
2000 : /* Remember this was copy-list-initialization. */
2001 46082 : conv->need_temporary_p = true;
2002 :
2003 : /* If initializing the temporary used a conversion function,
2004 : recalculate the second conversion sequence. */
2005 6806924 : for (conversion *t = conv; t; t = next_conversion (t))
2006 4572785 : if (t->kind == ck_user
2007 4572785 : && DECL_CONV_FN_P (t->cand->fn))
2008 : {
2009 132687 : tree ftype = TREE_TYPE (TREE_TYPE (t->cand->fn));
2010 : /* A prvalue of non-class type is cv-unqualified. */
2011 132687 : if (!TYPE_REF_P (ftype) && !CLASS_TYPE_P (ftype))
2012 266 : ftype = cv_unqualified (ftype);
2013 132687 : int sflags = (flags|LOOKUP_NO_CONVERSION)&~LOOKUP_NO_TEMP_BIND;
2014 132687 : conversion *new_second
2015 132687 : = reference_binding (rto, ftype, NULL_TREE, c_cast_p,
2016 : sflags, complain);
2017 132687 : if (!new_second)
2018 : return NULL;
2019 132687 : conv = merge_conversion_sequences (t, new_second);
2020 132687 : gcc_assert (maybe_valid_p || conv->bad_p);
2021 : return conv;
2022 : }
2023 : }
2024 :
2025 4272378 : conv = build_conv (ck_ref_bind, rto, conv);
2026 : /* This reference binding, unlike those above, requires the
2027 : creation of a temporary. */
2028 4272378 : conv->need_temporary_p = true;
2029 4272378 : conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
2030 4272378 : conv->bad_p |= !maybe_valid_p;
2031 :
2032 4272378 : return conv;
2033 : }
2034 :
2035 : /* Most of the implementation of implicit_conversion, with the same
2036 : parameters. */
2037 :
2038 : static conversion *
2039 594356767 : implicit_conversion_1 (tree to, tree from, tree expr, bool c_cast_p,
2040 : int flags, tsubst_flags_t complain)
2041 : {
2042 594356767 : conversion *conv;
2043 :
2044 594356767 : if (from == error_mark_node || to == error_mark_node
2045 594356084 : || expr == error_mark_node)
2046 : return NULL;
2047 :
2048 : /* Other flags only apply to the primary function in overload
2049 : resolution, or after we've chosen one. */
2050 594356084 : flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM
2051 : |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_NO_NARROWING
2052 : |LOOKUP_PROTECT|LOOKUP_NO_NON_INTEGRAL|LOOKUP_SHORTCUT_BAD_CONVS);
2053 :
2054 : /* FIXME: actually we don't want warnings either, but we can't just
2055 : have 'complain &= ~(tf_warning|tf_error)' because it would cause
2056 : the regression of, eg, g++.old-deja/g++.benjamin/16077.C.
2057 : We really ought not to issue that warning until we've committed
2058 : to that conversion. */
2059 594356084 : complain &= ~tf_error;
2060 :
2061 : /* Call reshape_init early to remove redundant braces. */
2062 589366609 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr)
2063 2071960 : && CLASS_TYPE_P (to)
2064 1045595 : && COMPLETE_TYPE_P (complete_type (to))
2065 595401667 : && !CLASSTYPE_NON_AGGREGATE (to))
2066 : {
2067 660699 : expr = reshape_init (to, expr, complain);
2068 660699 : if (expr == error_mark_node)
2069 : return NULL;
2070 660545 : from = TREE_TYPE (expr);
2071 : }
2072 :
2073 594355930 : if (TYPE_REF_P (to))
2074 69845774 : conv = reference_binding (to, from, expr, c_cast_p, flags, complain);
2075 : else
2076 524510156 : conv = standard_conversion (to, from, expr, c_cast_p, flags, complain);
2077 :
2078 594355930 : if (conv)
2079 : return conv;
2080 :
2081 91884106 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
2082 : {
2083 1967000 : if (is_std_init_list (to) && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2084 44603 : return build_list_conv (to, expr, flags, complain);
2085 :
2086 : /* As an extension, allow list-initialization of _Complex. */
2087 1922397 : if (TREE_CODE (to) == COMPLEX_TYPE
2088 1922397 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2089 : {
2090 41298 : conv = build_complex_conv (to, expr, flags, complain);
2091 41298 : if (conv)
2092 : return conv;
2093 : }
2094 :
2095 : /* Allow conversion from an initializer-list with one element to a
2096 : scalar type. */
2097 1881141 : if (SCALAR_TYPE_P (to))
2098 : {
2099 879076 : int nelts = CONSTRUCTOR_NELTS (expr);
2100 182250 : tree elt;
2101 :
2102 182250 : if (nelts == 0)
2103 696826 : elt = build_value_init (to, tf_none);
2104 182250 : else if (nelts == 1 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2105 181827 : elt = CONSTRUCTOR_ELT (expr, 0)->value;
2106 : else
2107 423 : elt = error_mark_node;
2108 :
2109 879076 : conv = implicit_conversion (to, TREE_TYPE (elt), elt,
2110 : c_cast_p, flags, complain);
2111 879076 : if (conv)
2112 : {
2113 878322 : conv->check_narrowing = true;
2114 878322 : if (BRACE_ENCLOSED_INITIALIZER_P (elt))
2115 : /* Too many levels of braces, i.e. '{{1}}'. */
2116 14 : conv->bad_p = true;
2117 878322 : return conv;
2118 : }
2119 : }
2120 1002065 : else if (TREE_CODE (to) == ARRAY_TYPE)
2121 332 : return build_array_conv (to, expr, flags, complain);
2122 : }
2123 :
2124 90919593 : if (expr != NULL_TREE
2125 89277723 : && (MAYBE_CLASS_TYPE_P (from)
2126 61495638 : || MAYBE_CLASS_TYPE_P (to))
2127 147587640 : && (flags & LOOKUP_NO_CONVERSION) == 0)
2128 : {
2129 22206226 : struct z_candidate *cand;
2130 :
2131 17064715 : if (CLASS_TYPE_P (to)
2132 17064667 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
2133 23206753 : && !CLASSTYPE_NON_AGGREGATE (complete_type (to)))
2134 660514 : return build_aggr_conv (to, expr, flags, complain);
2135 :
2136 21545712 : cand = build_user_type_conversion_1 (to, expr, flags, complain);
2137 21545712 : if (cand)
2138 : {
2139 339403 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
2140 339391 : && CONSTRUCTOR_NELTS (expr) == 1
2141 8462 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr)
2142 4632982 : && !is_list_ctor (cand->fn))
2143 : {
2144 : /* "If C is not an initializer-list constructor and the
2145 : initializer list has a single element of type cv U, where U is
2146 : X or a class derived from X, the implicit conversion sequence
2147 : has Exact Match rank if U is X, or Conversion rank if U is
2148 : derived from X." */
2149 8176 : tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
2150 8176 : tree elttype = TREE_TYPE (elt);
2151 8176 : if (reference_related_p (to, elttype))
2152 65 : return implicit_conversion (to, elttype, elt,
2153 65 : c_cast_p, flags, complain);
2154 : }
2155 4624455 : conv = cand->second_conv;
2156 : }
2157 :
2158 : /* We used to try to bind a reference to a temporary here, but that
2159 : is now handled after the recursive call to this function at the end
2160 : of reference_binding. */
2161 21545647 : return conv;
2162 : }
2163 :
2164 : return NULL;
2165 : }
2166 :
2167 : /* Returns the implicit conversion sequence (see [over.ics]) from type
2168 : FROM to type TO. The optional expression EXPR may affect the
2169 : conversion. FLAGS are the usual overloading flags. If C_CAST_P is
2170 : true, this conversion is coming from a C-style cast. */
2171 :
2172 : static conversion *
2173 594356767 : implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
2174 : int flags, tsubst_flags_t complain)
2175 : {
2176 594356767 : conversion *conv = implicit_conversion_1 (to, from, expr, c_cast_p,
2177 : flags, complain);
2178 594356767 : if (!conv || conv->bad_p)
2179 : return conv;
2180 499762276 : if (conv_is_prvalue (conv)
2181 85291382 : && CLASS_TYPE_P (conv->type)
2182 509952304 : && CLASSTYPE_PURE_VIRTUALS (conv->type))
2183 15718 : conv->bad_p = true;
2184 : return conv;
2185 : }
2186 :
2187 : /* Like implicit_conversion, but return NULL if the conversion is bad.
2188 :
2189 : This is not static so that check_non_deducible_conversion can call it within
2190 : add_template_candidate_real as part of overload resolution; it should not be
2191 : called outside of overload resolution. */
2192 :
2193 : conversion *
2194 2894474 : good_conversion (tree to, tree from, tree expr,
2195 : int flags, tsubst_flags_t complain)
2196 : {
2197 2894474 : conversion *c = implicit_conversion (to, from, expr, /*cast*/false,
2198 : flags, complain);
2199 2894474 : if (c && c->bad_p)
2200 1269765 : c = NULL;
2201 2894474 : return c;
2202 : }
2203 :
2204 : /* Add a new entry to the list of candidates. Used by the add_*_candidate
2205 : functions. ARGS will not be changed until a single candidate is
2206 : selected. */
2207 :
2208 : static struct z_candidate *
2209 378965231 : add_candidate (struct z_candidate **candidates,
2210 : tree fn, tree first_arg, const vec<tree, va_gc> *args,
2211 : size_t num_convs, conversion **convs,
2212 : tree access_path, tree conversion_path,
2213 : int viable, struct rejection_reason *reason,
2214 : int flags)
2215 : {
2216 378965231 : struct z_candidate *cand = (struct z_candidate *)
2217 378965231 : conversion_obstack_alloc (sizeof (struct z_candidate));
2218 :
2219 378965231 : cand->fn = fn;
2220 378965231 : cand->first_arg = first_arg;
2221 378965231 : cand->args = args;
2222 378965231 : cand->convs = convs;
2223 378965231 : cand->num_convs = num_convs;
2224 378965231 : cand->access_path = access_path;
2225 378965231 : cand->conversion_path = conversion_path;
2226 378965231 : cand->viable = viable;
2227 378965231 : cand->reason = reason;
2228 378965231 : cand->next = *candidates;
2229 378965231 : cand->flags = flags;
2230 378965231 : *candidates = cand;
2231 :
2232 378965231 : if (convs && cand->reversed ())
2233 : /* Swap the conversions for comparison in joust; we'll swap them back
2234 : before build_over_call. */
2235 1174313 : std::swap (convs[0], convs[1]);
2236 :
2237 378965231 : return cand;
2238 : }
2239 :
2240 : /* Return the number of remaining arguments in the parameter list
2241 : beginning with ARG. */
2242 :
2243 : int
2244 62646599 : remaining_arguments (tree arg)
2245 : {
2246 62646599 : int n;
2247 :
2248 109051793 : for (n = 0; arg != NULL_TREE && arg != void_list_node;
2249 46405194 : arg = TREE_CHAIN (arg))
2250 46405194 : n++;
2251 :
2252 62646599 : return n;
2253 : }
2254 :
2255 : /* [over.match.copy]: When initializing a temporary object (12.2) to be bound
2256 : to the first parameter of a constructor where the parameter is of type
2257 : "reference to possibly cv-qualified T" and the constructor is called with a
2258 : single argument in the context of direct-initialization of an object of type
2259 : "cv2 T", explicit conversion functions are also considered.
2260 :
2261 : So set LOOKUP_COPY_PARM to let reference_binding know that
2262 : it's being called in that context. */
2263 :
2264 : int
2265 155542415 : conv_flags (int i, int nargs, tree fn, tree arg, int flags)
2266 : {
2267 155542415 : int lflags = flags;
2268 155542415 : tree t;
2269 159317272 : if (i == 0 && nargs == 1 && DECL_CONSTRUCTOR_P (fn)
2270 44418158 : && (t = FUNCTION_FIRST_USER_PARMTYPE (fn))
2271 199960573 : && (same_type_ignoring_top_level_qualifiers_p
2272 44418158 : (non_reference (TREE_VALUE (t)), DECL_CONTEXT (fn))))
2273 : {
2274 35036497 : if (!(flags & LOOKUP_ONLYCONVERTING))
2275 11797639 : lflags |= LOOKUP_COPY_PARM;
2276 35036497 : if ((flags & LOOKUP_LIST_INIT_CTOR)
2277 35036497 : && BRACE_ENCLOSED_INITIALIZER_P (arg))
2278 341 : lflags |= LOOKUP_NO_CONVERSION;
2279 : }
2280 : else
2281 120505918 : lflags |= LOOKUP_ONLYCONVERTING;
2282 :
2283 155542415 : return lflags;
2284 : }
2285 :
2286 : /* Build an appropriate 'this' conversion for the method FN and class
2287 : type CTYPE from the value ARG (having type ARGTYPE) to the type PARMTYPE.
2288 : This function modifies PARMTYPE, ARGTYPE and ARG. */
2289 :
2290 : static conversion *
2291 37424440 : build_this_conversion (tree fn, tree ctype,
2292 : tree& parmtype, tree& argtype, tree& arg,
2293 : int flags, tsubst_flags_t complain)
2294 : {
2295 74848880 : gcc_assert (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2296 : && !DECL_CONSTRUCTOR_P (fn));
2297 :
2298 : /* The type of the implicit object parameter ('this') for
2299 : overload resolution is not always the same as for the
2300 : function itself; conversion functions are considered to
2301 : be members of the class being converted, and functions
2302 : introduced by a using-declaration are considered to be
2303 : members of the class that uses them.
2304 :
2305 : Since build_over_call ignores the ICS for the `this'
2306 : parameter, we can just change the parm type. */
2307 37424440 : parmtype = cp_build_qualified_type (ctype,
2308 37424440 : cp_type_quals (TREE_TYPE (parmtype)));
2309 37424440 : bool this_p = true;
2310 37424440 : if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn)))
2311 : {
2312 : /* If the function has a ref-qualifier, the implicit
2313 : object parameter has reference type. */
2314 13482 : bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn));
2315 13482 : parmtype = cp_build_reference_type (parmtype, rv);
2316 : /* The special handling of 'this' conversions in compare_ics
2317 : does not apply if there is a ref-qualifier. */
2318 13482 : this_p = false;
2319 : }
2320 : else
2321 : {
2322 37410958 : parmtype = build_pointer_type (parmtype);
2323 : /* We don't use build_this here because we don't want to
2324 : capture the object argument until we've chosen a
2325 : non-static member function. */
2326 37410958 : arg = build_address (arg);
2327 37410958 : argtype = lvalue_type (arg);
2328 : }
2329 37424440 : flags |= LOOKUP_ONLYCONVERTING;
2330 37424440 : conversion *t = implicit_conversion (parmtype, argtype, arg,
2331 : /*c_cast_p=*/false, flags, complain);
2332 37424440 : t->this_p = this_p;
2333 37424440 : return t;
2334 : }
2335 :
2336 : /* Create an overload candidate for the function or method FN called
2337 : with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
2338 : FLAGS is passed on to implicit_conversion.
2339 :
2340 : This does not change ARGS.
2341 :
2342 : CTYPE, if non-NULL, is the type we want to pretend this function
2343 : comes from for purposes of overload resolution.
2344 :
2345 : SHORTCUT_BAD_CONVS controls how we handle "bad" argument conversions.
2346 : If true, we stop computing conversions upon seeing the first bad
2347 : conversion. This is used by add_candidates to avoid computing
2348 : more conversions than necessary in the presence of a strictly viable
2349 : candidate, while preserving the defacto behavior of overload resolution
2350 : when it turns out there are only non-strictly viable candidates. */
2351 :
2352 : static struct z_candidate *
2353 238811057 : add_function_candidate (struct z_candidate **candidates,
2354 : tree fn, tree ctype, tree first_arg,
2355 : const vec<tree, va_gc> *args, tree access_path,
2356 : tree conversion_path, int flags,
2357 : conversion **convs,
2358 : bool shortcut_bad_convs,
2359 : tsubst_flags_t complain)
2360 : {
2361 238811057 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
2362 238811057 : int i, len;
2363 238811057 : tree parmnode;
2364 238811057 : tree orig_first_arg = first_arg;
2365 238811057 : int skip;
2366 238811057 : int viable = 1;
2367 238811057 : struct rejection_reason *reason = NULL;
2368 :
2369 : /* The `this', `in_chrg' and VTT arguments to constructors are not
2370 : considered in overload resolution. */
2371 477622114 : if (DECL_CONSTRUCTOR_P (fn))
2372 : {
2373 103686781 : if (ctor_omit_inherited_parms (fn))
2374 : /* Bring back parameters omitted from an inherited ctor. */
2375 150 : parmlist = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
2376 : else
2377 103686706 : parmlist = skip_artificial_parms_for (fn, parmlist);
2378 103686781 : skip = num_artificial_parms_for (fn);
2379 103686781 : if (skip > 0 && first_arg != NULL_TREE)
2380 : {
2381 103686781 : --skip;
2382 103686781 : first_arg = NULL_TREE;
2383 : }
2384 : }
2385 : else
2386 : skip = 0;
2387 :
2388 461459380 : len = vec_safe_length (args) - skip + (first_arg != NULL_TREE ? 1 : 0);
2389 238811057 : if (!convs)
2390 212220349 : convs = alloc_conversions (len);
2391 :
2392 : /* 13.3.2 - Viable functions [over.match.viable]
2393 : First, to be a viable function, a candidate function shall have enough
2394 : parameters to agree in number with the arguments in the list.
2395 :
2396 : We need to check this first; otherwise, checking the ICSes might cause
2397 : us to produce an ill-formed template instantiation. */
2398 :
2399 238811057 : parmnode = parmlist;
2400 484633156 : for (i = 0; i < len; ++i)
2401 : {
2402 274641548 : if (parmnode == NULL_TREE || parmnode == void_list_node)
2403 : break;
2404 245822099 : parmnode = TREE_CHAIN (parmnode);
2405 : }
2406 :
2407 238811057 : if ((i < len && parmnode)
2408 238811057 : || !sufficient_parms_p (parmnode))
2409 : {
2410 62644627 : int remaining = remaining_arguments (parmnode);
2411 62644627 : viable = 0;
2412 62644627 : reason = arity_rejection (first_arg, i + remaining, len);
2413 : }
2414 :
2415 : /* An inherited constructor (12.6.3 [class.inhctor.init]) that has a first
2416 : parameter of type "reference to cv C" (including such a constructor
2417 : instantiated from a template) is excluded from the set of candidate
2418 : functions when used to construct an object of type D with an argument list
2419 : containing a single argument if C is reference-related to D. */
2420 212099356 : if (viable && len == 1 && parmlist && DECL_CONSTRUCTOR_P (fn)
2421 45598809 : && flag_new_inheriting_ctors
2422 284401151 : && DECL_INHERITED_CTOR (fn))
2423 : {
2424 345938 : tree ptype = non_reference (TREE_VALUE (parmlist));
2425 345938 : tree dtype = DECL_CONTEXT (fn);
2426 691876 : tree btype = DECL_INHERITED_CTOR_BASE (fn);
2427 345938 : if (reference_related_p (ptype, dtype)
2428 345938 : && reference_related_p (btype, ptype))
2429 : {
2430 235531 : viable = false;
2431 235531 : reason = inherited_ctor_rejection ();
2432 : }
2433 : }
2434 :
2435 : /* Second, for a function to be viable, its constraints must be
2436 : satisfied. */
2437 238811057 : if (flag_concepts && viable && !constraints_satisfied_p (fn))
2438 : {
2439 10940 : reason = constraint_failure ();
2440 10940 : viable = false;
2441 : }
2442 :
2443 : /* When looking for a function from a subobject from an implicit
2444 : copy/move constructor/operator=, don't consider anything that takes (a
2445 : reference to) an unrelated type. See c++/44909 and core 1092. */
2446 238811057 : if (viable && parmlist && (flags & LOOKUP_DEFAULTED))
2447 : {
2448 38769780 : if (DECL_CONSTRUCTOR_P (fn))
2449 : i = 1;
2450 10198539 : else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2451 10198539 : && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
2452 : i = 2;
2453 : else
2454 : i = 0;
2455 19384890 : if (i && len == i)
2456 : {
2457 10217850 : parmnode = chain_index (i-1, parmlist);
2458 10217850 : if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
2459 : ctype))
2460 19384890 : viable = 0;
2461 : }
2462 :
2463 : /* This only applies at the top level. */
2464 19384890 : flags &= ~LOOKUP_DEFAULTED;
2465 : }
2466 :
2467 238811057 : if (! viable)
2468 64887496 : goto out;
2469 :
2470 173923561 : if (shortcut_bad_convs)
2471 173887050 : flags |= LOOKUP_SHORTCUT_BAD_CONVS;
2472 : else
2473 36511 : flags &= ~LOOKUP_SHORTCUT_BAD_CONVS;
2474 :
2475 : /* Third, for F to be a viable function, there shall exist for each
2476 : argument an implicit conversion sequence that converts that argument
2477 : to the corresponding parameter of F. */
2478 :
2479 173923561 : parmnode = parmlist;
2480 :
2481 308476597 : for (i = 0; i < len; ++i)
2482 : {
2483 189148770 : tree argtype, to_type;
2484 189148770 : tree arg;
2485 :
2486 189148770 : if (parmnode == void_list_node)
2487 : break;
2488 :
2489 189148770 : if (convs[i])
2490 : {
2491 : /* Already set during deduction. */
2492 2190567 : parmnode = TREE_CHAIN (parmnode);
2493 2190567 : continue;
2494 : }
2495 :
2496 186958203 : if (i == 0 && first_arg != NULL_TREE)
2497 36288342 : arg = first_arg;
2498 : else
2499 288806233 : arg = CONST_CAST_TREE (
2500 : (*args)[i + skip - (first_arg != NULL_TREE ? 1 : 0)]);
2501 186958203 : argtype = lvalue_type (arg);
2502 :
2503 186958203 : conversion *t;
2504 186958203 : if (parmnode)
2505 : {
2506 183435425 : tree parmtype = TREE_VALUE (parmnode);
2507 183435425 : if (i == 0
2508 147769868 : && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2509 347030335 : && !DECL_CONSTRUCTOR_P (fn))
2510 36288342 : t = build_this_conversion (fn, ctype, parmtype, argtype, arg,
2511 : flags, complain);
2512 : else
2513 : {
2514 147147083 : int lflags = conv_flags (i, len-skip, fn, arg, flags);
2515 147147083 : t = implicit_conversion (parmtype, argtype, arg,
2516 : /*c_cast_p=*/false, lflags, complain);
2517 : }
2518 183435425 : to_type = parmtype;
2519 183435425 : parmnode = TREE_CHAIN (parmnode);
2520 : }
2521 : else
2522 : {
2523 3522778 : t = build_identity_conv (argtype, arg);
2524 3522778 : t->ellipsis_p = true;
2525 3522778 : to_type = argtype;
2526 : }
2527 :
2528 186958203 : convs[i] = t;
2529 186958203 : if (! t)
2530 : {
2531 47113069 : viable = 0;
2532 47113069 : reason = arg_conversion_rejection (first_arg, i, argtype, to_type,
2533 47113069 : EXPR_LOCATION (arg));
2534 47113069 : break;
2535 : }
2536 :
2537 139845134 : if (t->bad_p)
2538 : {
2539 7513413 : viable = -1;
2540 7513413 : reason = bad_arg_conversion_rejection (first_arg, i, arg, to_type,
2541 7513413 : EXPR_LOCATION (arg));
2542 7513413 : if (shortcut_bad_convs)
2543 : break;
2544 : }
2545 : }
2546 :
2547 119327827 : out:
2548 238811057 : return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
2549 238811057 : access_path, conversion_path, viable, reason, flags);
2550 : }
2551 :
2552 : /* Create an overload candidate for the conversion function FN which will
2553 : be invoked for expression OBJ, producing a pointer-to-function which
2554 : will in turn be called with the argument list FIRST_ARG/ARGLIST,
2555 : and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
2556 : passed on to implicit_conversion.
2557 :
2558 : Actually, we don't really care about FN; we care about the type it
2559 : converts to. There may be multiple conversion functions that will
2560 : convert to that type, and we rely on build_user_type_conversion_1 to
2561 : choose the best one; so when we create our candidate, we record the type
2562 : instead of the function. */
2563 :
2564 : static struct z_candidate *
2565 2472 : add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
2566 : const vec<tree, va_gc> *arglist,
2567 : tree access_path, tree conversion_path,
2568 : tsubst_flags_t complain)
2569 : {
2570 2472 : tree totype = TREE_TYPE (TREE_TYPE (fn));
2571 2472 : int i, len, viable, flags;
2572 2472 : tree parmlist, parmnode;
2573 2472 : conversion **convs;
2574 2472 : struct rejection_reason *reason;
2575 :
2576 4958 : for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
2577 2486 : parmlist = TREE_TYPE (parmlist);
2578 2472 : parmlist = TYPE_ARG_TYPES (parmlist);
2579 :
2580 2472 : len = vec_safe_length (arglist) + 1;
2581 2472 : convs = alloc_conversions (len);
2582 2472 : parmnode = parmlist;
2583 2472 : viable = 1;
2584 2472 : flags = LOOKUP_IMPLICIT;
2585 2472 : reason = NULL;
2586 :
2587 : /* Don't bother looking up the same type twice. */
2588 2472 : if (*candidates && (*candidates)->fn == totype)
2589 : return NULL;
2590 :
2591 2452 : if (!constraints_satisfied_p (fn))
2592 : {
2593 3 : reason = constraint_failure ();
2594 3 : viable = 0;
2595 3 : return add_candidate (candidates, fn, obj, arglist, len, convs,
2596 3 : access_path, conversion_path, viable, reason, flags);
2597 : }
2598 :
2599 6513 : for (i = 0; i < len; ++i)
2600 : {
2601 4098 : tree arg, argtype, convert_type = NULL_TREE;
2602 4098 : conversion *t;
2603 :
2604 4098 : if (i == 0)
2605 : arg = obj;
2606 : else
2607 1649 : arg = (*arglist)[i - 1];
2608 4098 : argtype = lvalue_type (arg);
2609 :
2610 4098 : if (i == 0)
2611 : {
2612 2449 : t = build_identity_conv (argtype, NULL_TREE);
2613 2449 : t = build_conv (ck_user, totype, t);
2614 : /* Leave the 'cand' field null; we'll figure out the conversion in
2615 : convert_like if this candidate is chosen. */
2616 2449 : convert_type = totype;
2617 : }
2618 1649 : else if (parmnode == void_list_node)
2619 : break;
2620 1628 : else if (parmnode)
2621 : {
2622 1625 : t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
2623 : /*c_cast_p=*/false, flags, complain);
2624 1625 : convert_type = TREE_VALUE (parmnode);
2625 : }
2626 : else
2627 : {
2628 3 : t = build_identity_conv (argtype, arg);
2629 3 : t->ellipsis_p = true;
2630 3 : convert_type = argtype;
2631 : }
2632 :
2633 4077 : convs[i] = t;
2634 4077 : if (! t)
2635 : break;
2636 :
2637 4064 : if (t->bad_p)
2638 : {
2639 10 : viable = -1;
2640 30 : reason = bad_arg_conversion_rejection (NULL_TREE, i, arg, convert_type,
2641 10 : EXPR_LOCATION (arg));
2642 : }
2643 :
2644 4064 : if (i == 0)
2645 2449 : continue;
2646 :
2647 1615 : if (parmnode)
2648 1612 : parmnode = TREE_CHAIN (parmnode);
2649 : }
2650 :
2651 2449 : if (i < len
2652 2449 : || ! sufficient_parms_p (parmnode))
2653 : {
2654 46 : int remaining = remaining_arguments (parmnode);
2655 46 : viable = 0;
2656 46 : reason = arity_rejection (NULL_TREE, i + remaining, len);
2657 : }
2658 :
2659 2449 : return add_candidate (candidates, totype, obj, arglist, len, convs,
2660 2449 : access_path, conversion_path, viable, reason, flags);
2661 : }
2662 :
2663 : static void
2664 5338149 : build_builtin_candidate (struct z_candidate **candidates, tree fnname,
2665 : tree type1, tree type2, const vec<tree,va_gc> &args,
2666 : tree *argtypes, int flags, tsubst_flags_t complain)
2667 : {
2668 5338149 : conversion *t;
2669 5338149 : conversion **convs;
2670 5338149 : size_t num_convs;
2671 5338149 : int viable = 1;
2672 5338149 : tree types[2];
2673 5338149 : struct rejection_reason *reason = NULL;
2674 :
2675 5338149 : types[0] = type1;
2676 5338149 : types[1] = type2;
2677 :
2678 5338149 : num_convs = args.length ();
2679 5338149 : convs = alloc_conversions (num_convs);
2680 :
2681 : /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2682 : conversion ops are allowed. We handle that here by just checking for
2683 : boolean_type_node because other operators don't ask for it. COND_EXPR
2684 : also does contextual conversion to bool for the first operand, but we
2685 : handle that in build_conditional_expr, and type1 here is operand 2. */
2686 5338149 : if (type1 != boolean_type_node)
2687 4949914 : flags |= LOOKUP_ONLYCONVERTING;
2688 :
2689 15573245 : for (unsigned i = 0; i < 2 && i < num_convs; ++i)
2690 : {
2691 10235096 : t = implicit_conversion (types[i], argtypes[i], args[i],
2692 : /*c_cast_p=*/false, flags, complain);
2693 10235096 : if (! t)
2694 : {
2695 145460 : viable = 0;
2696 : /* We need something for printing the candidate. */
2697 145460 : t = build_identity_conv (types[i], NULL_TREE);
2698 145460 : reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2699 145460 : types[i], EXPR_LOCATION (args[i]));
2700 : }
2701 10089636 : else if (t->bad_p)
2702 : {
2703 71 : viable = 0;
2704 71 : reason = bad_arg_conversion_rejection (NULL_TREE, i, args[i],
2705 : types[i],
2706 71 : EXPR_LOCATION (args[i]));
2707 : }
2708 10235096 : convs[i] = t;
2709 : }
2710 :
2711 : /* For COND_EXPR we rearranged the arguments; undo that now. */
2712 5338149 : if (num_convs == 3)
2713 : {
2714 31 : convs[2] = convs[1];
2715 31 : convs[1] = convs[0];
2716 31 : t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
2717 : /*c_cast_p=*/false, flags,
2718 : complain);
2719 31 : if (t)
2720 31 : convs[0] = t;
2721 : else
2722 : {
2723 0 : viable = 0;
2724 0 : reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2],
2725 : boolean_type_node,
2726 0 : EXPR_LOCATION (args[2]));
2727 : }
2728 : }
2729 :
2730 5338149 : add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
2731 : num_convs, convs,
2732 : /*access_path=*/NULL_TREE,
2733 : /*conversion_path=*/NULL_TREE,
2734 : viable, reason, flags);
2735 5338149 : }
2736 :
2737 : static bool
2738 8 : is_complete (tree t)
2739 : {
2740 8 : return COMPLETE_TYPE_P (complete_type (t));
2741 : }
2742 :
2743 : /* Returns nonzero if TYPE is a promoted arithmetic type. */
2744 :
2745 : static bool
2746 3486 : promoted_arithmetic_type_p (tree type)
2747 : {
2748 : /* [over.built]
2749 :
2750 : In this section, the term promoted integral type is used to refer
2751 : to those integral types which are preserved by integral promotion
2752 : (including e.g. int and long but excluding e.g. char).
2753 : Similarly, the term promoted arithmetic type refers to promoted
2754 : integral types plus floating types. */
2755 3486 : return ((CP_INTEGRAL_TYPE_P (type)
2756 202 : && same_type_p (type_promotes_to (type), type))
2757 3486 : || SCALAR_FLOAT_TYPE_P (type));
2758 : }
2759 :
2760 : /* Create any builtin operator overload candidates for the operator in
2761 : question given the converted operand types TYPE1 and TYPE2. The other
2762 : args are passed through from add_builtin_candidates to
2763 : build_builtin_candidate.
2764 :
2765 : TYPE1 and TYPE2 may not be permissible, and we must filter them.
2766 : If CODE is requires candidates operands of the same type of the kind
2767 : of which TYPE1 and TYPE2 are, we add both candidates
2768 : CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2769 :
2770 : static void
2771 7810515 : add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
2772 : enum tree_code code2, tree fnname, tree type1,
2773 : tree type2, vec<tree,va_gc> &args, tree *argtypes,
2774 : int flags, tsubst_flags_t complain)
2775 : {
2776 7810515 : switch (code)
2777 : {
2778 30 : case POSTINCREMENT_EXPR:
2779 30 : case POSTDECREMENT_EXPR:
2780 30 : args[1] = integer_zero_node;
2781 30 : type2 = integer_type_node;
2782 30 : break;
2783 : default:
2784 : break;
2785 : }
2786 :
2787 7810515 : switch (code)
2788 : {
2789 :
2790 : /* 4 For every pair (T, VQ), where T is an arithmetic type other than bool,
2791 : and VQ is either volatile or empty, there exist candidate operator
2792 : functions of the form
2793 : VQ T& operator++(VQ T&);
2794 : T operator++(VQ T&, int);
2795 : 5 For every pair (T, VQ), where T is an arithmetic type other than bool,
2796 : and VQ is either volatile or empty, there exist candidate operator
2797 : functions of the form
2798 : VQ T& operator--(VQ T&);
2799 : T operator--(VQ T&, int);
2800 : 6 For every pair (T, VQ), where T is a cv-qualified or cv-unqualified object
2801 : type, and VQ is either volatile or empty, there exist candidate operator
2802 : functions of the form
2803 : T*VQ& operator++(T*VQ&);
2804 : T*VQ& operator--(T*VQ&);
2805 : T* operator++(T*VQ&, int);
2806 : T* operator--(T*VQ&, int); */
2807 :
2808 26 : case POSTDECREMENT_EXPR:
2809 26 : case PREDECREMENT_EXPR:
2810 26 : if (TREE_CODE (type1) == BOOLEAN_TYPE)
2811 : return;
2812 : /* FALLTHRU */
2813 58 : case POSTINCREMENT_EXPR:
2814 58 : case PREINCREMENT_EXPR:
2815 : /* P0002R1, Remove deprecated operator++(bool) added "other than bool"
2816 : to p4. */
2817 58 : if (TREE_CODE (type1) == BOOLEAN_TYPE && cxx_dialect >= cxx17)
2818 : return;
2819 54 : if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
2820 : {
2821 44 : type1 = build_reference_type (type1);
2822 44 : break;
2823 : }
2824 : return;
2825 :
2826 : /* 7 For every cv-qualified or cv-unqualified object type T, there
2827 : exist candidate operator functions of the form
2828 :
2829 : T& operator*(T*);
2830 :
2831 :
2832 : 8 For every function type T that does not have cv-qualifiers or
2833 : a ref-qualifier, there exist candidate operator functions of the form
2834 : T& operator*(T*); */
2835 :
2836 60507 : case INDIRECT_REF:
2837 60507 : if (TYPE_PTR_P (type1)
2838 60507 : && (TYPE_PTROB_P (type1)
2839 14 : || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
2840 : break;
2841 : return;
2842 :
2843 : /* 9 For every type T, there exist candidate operator functions of the form
2844 : T* operator+(T*);
2845 :
2846 : 10 For every floating-point or promoted integral type T, there exist
2847 : candidate operator functions of the form
2848 : T operator+(T);
2849 : T operator-(T); */
2850 :
2851 171 : case UNARY_PLUS_EXPR: /* unary + */
2852 171 : if (TYPE_PTR_P (type1))
2853 : break;
2854 : /* FALLTHRU */
2855 18312 : case NEGATE_EXPR:
2856 18312 : if (ARITHMETIC_TYPE_P (type1))
2857 : break;
2858 : return;
2859 :
2860 : /* 11 For every promoted integral type T, there exist candidate operator
2861 : functions of the form
2862 : T operator~(T); */
2863 :
2864 81632 : case BIT_NOT_EXPR:
2865 81632 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
2866 : break;
2867 : return;
2868 :
2869 : /* 12 For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, C1
2870 : is the same type as C2 or is a derived class of C2, and T is an object
2871 : type or a function type there exist candidate operator functions of the
2872 : form
2873 : CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
2874 : where CV12 is the union of CV1 and CV2. */
2875 :
2876 27 : case MEMBER_REF:
2877 27 : if (TYPE_PTR_P (type1) && TYPE_PTRMEM_P (type2))
2878 : {
2879 27 : tree c1 = TREE_TYPE (type1);
2880 27 : tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
2881 :
2882 23 : if (CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
2883 46 : && (TYPE_PTRMEMFUNC_P (type2)
2884 8 : || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
2885 : break;
2886 : }
2887 : return;
2888 :
2889 : /* 13 For every pair of types L and R, where each of L and R is a floating-point
2890 : or promoted integral type, there exist candidate operator functions of the
2891 : form
2892 : LR operator*(L, R);
2893 : LR operator/(L, R);
2894 : LR operator+(L, R);
2895 : LR operator-(L, R);
2896 : bool operator<(L, R);
2897 : bool operator>(L, R);
2898 : bool operator<=(L, R);
2899 : bool operator>=(L, R);
2900 : bool operator==(L, R);
2901 : bool operator!=(L, R);
2902 : where LR is the result of the usual arithmetic conversions between
2903 : types L and R.
2904 :
2905 : 14 For every integral type T there exists a candidate operator function of
2906 : the form
2907 :
2908 : std::strong_ordering operator<=>(T, T);
2909 :
2910 : 15 For every pair of floating-point types L and R, there exists a candidate
2911 : operator function of the form
2912 :
2913 : std::partial_ordering operator<=>(L, R);
2914 :
2915 : 16 For every cv-qualified or cv-unqualified object type T there exist
2916 : candidate operator functions of the form
2917 : T* operator+(T*, std::ptrdiff_t);
2918 : T& operator[](T*, std::ptrdiff_t);
2919 : T* operator-(T*, std::ptrdiff_t);
2920 : T* operator+(std::ptrdiff_t, T*);
2921 : T& operator[](std::ptrdiff_t, T*);
2922 :
2923 : 17 For every T, where T is a pointer to object type, there exist candidate
2924 : operator functions of the form
2925 : std::ptrdiff_t operator-(T, T);
2926 :
2927 : 18 For every T, where T is an enumeration type or a pointer type, there
2928 : exist candidate operator functions of the form
2929 : bool operator<(T, T);
2930 : bool operator>(T, T);
2931 : bool operator<=(T, T);
2932 : bool operator>=(T, T);
2933 : bool operator==(T, T);
2934 : bool operator!=(T, T);
2935 : R operator<=>(T, T);
2936 :
2937 : where R is the result type specified in [expr.spaceship].
2938 :
2939 : 19 For every T, where T is a pointer-to-member type or std::nullptr_t,
2940 : there exist candidate operator functions of the form
2941 : bool operator==(T, T);
2942 : bool operator!=(T, T); */
2943 :
2944 107008 : case MINUS_EXPR:
2945 107008 : if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
2946 : break;
2947 9 : if (TYPE_PTROB_P (type1)
2948 106987 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2949 : {
2950 7 : type2 = ptrdiff_type_node;
2951 7 : break;
2952 : }
2953 : /* FALLTHRU */
2954 153012 : case MULT_EXPR:
2955 153012 : case TRUNC_DIV_EXPR:
2956 153012 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2957 : break;
2958 : return;
2959 :
2960 : /* This isn't exactly what's specified above for operator<=>, but it's
2961 : close enough. In particular, we don't care about the return type
2962 : specified above; it doesn't participate in overload resolution and it
2963 : doesn't affect the semantics of the built-in operator. */
2964 4999792 : case SPACESHIP_EXPR:
2965 4999792 : case EQ_EXPR:
2966 4999792 : case NE_EXPR:
2967 43701 : if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2968 5043493 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)))
2969 : break;
2970 4999765 : if (NULLPTR_TYPE_P (type1) && NULLPTR_TYPE_P (type2))
2971 : break;
2972 4999759 : if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (args[1]))
2973 : {
2974 : type2 = type1;
2975 : break;
2976 : }
2977 4999755 : if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (args[0]))
2978 : {
2979 : type1 = type2;
2980 : break;
2981 : }
2982 : /* Fall through. */
2983 5192302 : case LT_EXPR:
2984 5192302 : case GT_EXPR:
2985 5192302 : case LE_EXPR:
2986 5192302 : case GE_EXPR:
2987 5192302 : case MAX_EXPR:
2988 5192302 : case MIN_EXPR:
2989 5192302 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2990 : break;
2991 3919860 : if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2992 : break;
2993 3919634 : if (TREE_CODE (type1) == ENUMERAL_TYPE
2994 2617024 : && TREE_CODE (type2) == ENUMERAL_TYPE)
2995 : break;
2996 2446176 : if (TYPE_PTR_P (type1)
2997 2446176 : && null_ptr_cst_p (args[1]))
2998 : {
2999 : type2 = type1;
3000 : break;
3001 : }
3002 2446152 : if (null_ptr_cst_p (args[0])
3003 2446152 : && TYPE_PTR_P (type2))
3004 : {
3005 : type1 = type2;
3006 : break;
3007 : }
3008 : return;
3009 :
3010 126393 : case PLUS_EXPR:
3011 126393 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3012 : break;
3013 : /* FALLTHRU */
3014 147654 : case ARRAY_REF:
3015 147654 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
3016 : {
3017 7 : type1 = ptrdiff_type_node;
3018 7 : break;
3019 : }
3020 196655 : if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3021 : {
3022 26541 : type2 = ptrdiff_type_node;
3023 26541 : break;
3024 : }
3025 : return;
3026 :
3027 : /* 18For every pair of promoted integral types L and R, there exist candi-
3028 : date operator functions of the form
3029 : LR operator%(L, R);
3030 : LR operator&(L, R);
3031 : LR operator^(L, R);
3032 : LR operator|(L, R);
3033 : L operator<<(L, R);
3034 : L operator>>(L, R);
3035 : where LR is the result of the usual arithmetic conversions between
3036 : types L and R. */
3037 :
3038 1516389 : case TRUNC_MOD_EXPR:
3039 1516389 : case BIT_AND_EXPR:
3040 1516389 : case BIT_IOR_EXPR:
3041 1516389 : case BIT_XOR_EXPR:
3042 1516389 : case LSHIFT_EXPR:
3043 1516389 : case RSHIFT_EXPR:
3044 1516389 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3045 : break;
3046 : return;
3047 :
3048 : /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
3049 : type, VQ is either volatile or empty, and R is a promoted arithmetic
3050 : type, there exist candidate operator functions of the form
3051 : VQ L& operator=(VQ L&, R);
3052 : VQ L& operator*=(VQ L&, R);
3053 : VQ L& operator/=(VQ L&, R);
3054 : VQ L& operator+=(VQ L&, R);
3055 : VQ L& operator-=(VQ L&, R);
3056 :
3057 : 20For every pair T, VQ), where T is any type and VQ is either volatile
3058 : or empty, there exist candidate operator functions of the form
3059 : T*VQ& operator=(T*VQ&, T*);
3060 :
3061 : 21For every pair T, VQ), where T is a pointer to member type and VQ is
3062 : either volatile or empty, there exist candidate operator functions of
3063 : the form
3064 : VQ T& operator=(VQ T&, T);
3065 :
3066 : 22For every triple T, VQ, I), where T is a cv-qualified or cv-
3067 : unqualified complete object type, VQ is either volatile or empty, and
3068 : I is a promoted integral type, there exist candidate operator func-
3069 : tions of the form
3070 : T*VQ& operator+=(T*VQ&, I);
3071 : T*VQ& operator-=(T*VQ&, I);
3072 :
3073 : 23For every triple L, VQ, R), where L is an integral or enumeration
3074 : type, VQ is either volatile or empty, and R is a promoted integral
3075 : type, there exist candidate operator functions of the form
3076 :
3077 : VQ L& operator%=(VQ L&, R);
3078 : VQ L& operator<<=(VQ L&, R);
3079 : VQ L& operator>>=(VQ L&, R);
3080 : VQ L& operator&=(VQ L&, R);
3081 : VQ L& operator^=(VQ L&, R);
3082 : VQ L& operator|=(VQ L&, R); */
3083 :
3084 590824 : case MODIFY_EXPR:
3085 590824 : switch (code2)
3086 : {
3087 131 : case PLUS_EXPR:
3088 131 : case MINUS_EXPR:
3089 131 : if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3090 : {
3091 0 : type2 = ptrdiff_type_node;
3092 0 : break;
3093 : }
3094 : /* FALLTHRU */
3095 136 : case MULT_EXPR:
3096 136 : case TRUNC_DIV_EXPR:
3097 136 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3098 : break;
3099 : return;
3100 :
3101 590688 : case TRUNC_MOD_EXPR:
3102 590688 : case BIT_AND_EXPR:
3103 590688 : case BIT_IOR_EXPR:
3104 590688 : case BIT_XOR_EXPR:
3105 590688 : case LSHIFT_EXPR:
3106 590688 : case RSHIFT_EXPR:
3107 590688 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3108 : break;
3109 : return;
3110 :
3111 0 : case NOP_EXPR:
3112 0 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3113 : break;
3114 0 : if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
3115 0 : || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3116 0 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
3117 0 : || ((TYPE_PTRMEMFUNC_P (type1)
3118 0 : || TYPE_PTR_P (type1))
3119 0 : && null_ptr_cst_p (args[1])))
3120 : {
3121 : type2 = type1;
3122 : break;
3123 : }
3124 : return;
3125 :
3126 0 : default:
3127 0 : gcc_unreachable ();
3128 : }
3129 590771 : type1 = build_reference_type (type1);
3130 590771 : break;
3131 :
3132 3299 : case COND_EXPR:
3133 : /* [over.built]
3134 :
3135 : For every pair of promoted arithmetic types L and R, there
3136 : exist candidate operator functions of the form
3137 :
3138 : LR operator?(bool, L, R);
3139 :
3140 : where LR is the result of the usual arithmetic conversions
3141 : between types L and R.
3142 :
3143 : For every type T, where T is a pointer or pointer-to-member
3144 : type, there exist candidate operator functions of the form T
3145 : operator?(bool, T, T); */
3146 :
3147 3299 : if (promoted_arithmetic_type_p (type1)
3148 3299 : && promoted_arithmetic_type_p (type2))
3149 : /* That's OK. */
3150 : break;
3151 :
3152 : /* Otherwise, the types should be pointers. */
3153 3284 : if (!TYPE_PTR_OR_PTRMEM_P (type1) || !TYPE_PTR_OR_PTRMEM_P (type2))
3154 : return;
3155 :
3156 : /* We don't check that the two types are the same; the logic
3157 : below will actually create two candidates; one in which both
3158 : parameter types are TYPE1, and one in which both parameter
3159 : types are TYPE2. */
3160 : break;
3161 :
3162 4 : case REALPART_EXPR:
3163 4 : case IMAGPART_EXPR:
3164 4 : if (ARITHMETIC_TYPE_P (type1))
3165 : break;
3166 : return;
3167 :
3168 0 : default:
3169 0 : gcc_unreachable ();
3170 : }
3171 :
3172 : /* Make sure we don't create builtin candidates with dependent types. */
3173 4949804 : bool u1 = uses_template_parms (type1);
3174 4949804 : bool u2 = type2 ? uses_template_parms (type2) : false;
3175 4949804 : if (u1 || u2)
3176 : {
3177 : /* Try to recover if one of the types is non-dependent. But if
3178 : there's only one type, there's nothing we can do. */
3179 24 : if (!type2)
3180 : return;
3181 : /* And we lose if both are dependent. */
3182 20 : if (u1 && u2)
3183 : return;
3184 : /* Or if they have different forms. */
3185 11 : if (TREE_CODE (type1) != TREE_CODE (type2))
3186 : return;
3187 :
3188 11 : if (u1 && !u2)
3189 : type1 = type2;
3190 8 : else if (u2 && !u1)
3191 4949791 : type2 = type1;
3192 : }
3193 :
3194 : /* If we're dealing with two pointer types or two enumeral types,
3195 : we need candidates for both of them. */
3196 4861055 : if (type2 && !same_type_p (type1, type2)
3197 785803 : && TREE_CODE (type1) == TREE_CODE (type2)
3198 5118016 : && (TYPE_REF_P (type1)
3199 168225 : || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3200 168151 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
3201 168137 : || TYPE_PTRMEMFUNC_P (type1)
3202 168137 : || MAYBE_CLASS_TYPE_P (type1)
3203 168137 : || TREE_CODE (type1) == ENUMERAL_TYPE))
3204 : {
3205 181 : if (TYPE_PTR_OR_PTRMEM_P (type1))
3206 : {
3207 88 : tree cptype = composite_pointer_type (input_location,
3208 : type1, type2,
3209 : error_mark_node,
3210 : error_mark_node,
3211 : CPO_CONVERSION,
3212 : tf_none);
3213 88 : if (cptype != error_mark_node)
3214 : {
3215 58 : build_builtin_candidate
3216 58 : (candidates, fnname, cptype, cptype, args, argtypes,
3217 : flags, complain);
3218 58 : return;
3219 : }
3220 : }
3221 :
3222 123 : build_builtin_candidate
3223 123 : (candidates, fnname, type1, type1, args, argtypes, flags, complain);
3224 123 : build_builtin_candidate
3225 123 : (candidates, fnname, type2, type2, args, argtypes, flags, complain);
3226 123 : return;
3227 : }
3228 :
3229 4949610 : build_builtin_candidate
3230 4949610 : (candidates, fnname, type1, type2, args, argtypes, flags, complain);
3231 : }
3232 :
3233 : tree
3234 459954849 : type_decays_to (tree type)
3235 : {
3236 459954849 : if (TREE_CODE (type) == ARRAY_TYPE)
3237 2925099 : return build_pointer_type (TREE_TYPE (type));
3238 457029750 : if (TREE_CODE (type) == FUNCTION_TYPE)
3239 24971 : return build_pointer_type (type);
3240 : return type;
3241 : }
3242 :
3243 : /* There are three conditions of builtin candidates:
3244 :
3245 : 1) bool-taking candidates. These are the same regardless of the input.
3246 : 2) pointer-pair taking candidates. These are generated for each type
3247 : one of the input types converts to.
3248 : 3) arithmetic candidates. According to the standard, we should generate
3249 : all of these, but I'm trying not to...
3250 :
3251 : Here we generate a superset of the possible candidates for this particular
3252 : case. That is a subset of the full set the standard defines, plus some
3253 : other cases which the standard disallows. add_builtin_candidate will
3254 : filter out the invalid set. */
3255 :
3256 : static void
3257 8002577 : add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
3258 : enum tree_code code2, tree fnname,
3259 : vec<tree, va_gc> *argv,
3260 : int flags, tsubst_flags_t complain)
3261 : {
3262 8002577 : int ref1;
3263 8002577 : int enum_p = 0;
3264 8002577 : tree type, argtypes[3], t;
3265 : /* TYPES[i] is the set of possible builtin-operator parameter types
3266 : we will consider for the Ith argument. */
3267 8002577 : vec<tree, va_gc> *types[2];
3268 8002577 : unsigned ix;
3269 8002577 : vec<tree, va_gc> &args = *argv;
3270 8002577 : unsigned len = args.length ();
3271 :
3272 21733951 : for (unsigned i = 0; i < len; ++i)
3273 : {
3274 13731374 : if (args[i])
3275 13731374 : argtypes[i] = unlowered_expr_type (args[i]);
3276 : else
3277 0 : argtypes[i] = NULL_TREE;
3278 : }
3279 :
3280 8002577 : switch (code)
3281 : {
3282 : /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
3283 : and VQ is either volatile or empty, there exist candidate operator
3284 : functions of the form
3285 : VQ T& operator++(VQ T&); */
3286 :
3287 : case POSTINCREMENT_EXPR:
3288 : case PREINCREMENT_EXPR:
3289 : case POSTDECREMENT_EXPR:
3290 : case PREDECREMENT_EXPR:
3291 : case MODIFY_EXPR:
3292 : ref1 = 1;
3293 : break;
3294 :
3295 : /* 24There also exist candidate operator functions of the form
3296 : bool operator!(bool);
3297 : bool operator&&(bool, bool);
3298 : bool operator||(bool, bool); */
3299 :
3300 352466 : case TRUTH_NOT_EXPR:
3301 352466 : build_builtin_candidate
3302 352466 : (candidates, fnname, boolean_type_node,
3303 : NULL_TREE, args, argtypes, flags, complain);
3304 3804190 : return;
3305 :
3306 35769 : case TRUTH_ORIF_EXPR:
3307 35769 : case TRUTH_ANDIF_EXPR:
3308 35769 : build_builtin_candidate
3309 35769 : (candidates, fnname, boolean_type_node,
3310 : boolean_type_node, args, argtypes, flags, complain);
3311 35769 : return;
3312 :
3313 : case ADDR_EXPR:
3314 : case COMPOUND_EXPR:
3315 : case COMPONENT_REF:
3316 : case CO_AWAIT_EXPR:
3317 : return;
3318 :
3319 2177570 : case COND_EXPR:
3320 2177570 : case EQ_EXPR:
3321 2177570 : case NE_EXPR:
3322 2177570 : case LT_EXPR:
3323 2177570 : case LE_EXPR:
3324 2177570 : case GT_EXPR:
3325 2177570 : case GE_EXPR:
3326 2177570 : case SPACESHIP_EXPR:
3327 2177570 : enum_p = 1;
3328 : /* Fall through. */
3329 :
3330 : default:
3331 : ref1 = 0;
3332 : }
3333 :
3334 6204039 : types[0] = make_tree_vector ();
3335 6204039 : types[1] = make_tree_vector ();
3336 :
3337 6204039 : if (len == 3)
3338 921 : len = 2;
3339 15190007 : for (unsigned i = 0; i < len; ++i)
3340 : {
3341 10639154 : if (MAYBE_CLASS_TYPE_P (argtypes[i]))
3342 : {
3343 2493280 : tree convs;
3344 :
3345 2493280 : if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
3346 : return;
3347 :
3348 2039142 : convs = lookup_conversions (argtypes[i]);
3349 :
3350 2039142 : if (code == COND_EXPR)
3351 : {
3352 1586 : if (lvalue_p (args[i]))
3353 1330 : vec_safe_push (types[i], build_reference_type (argtypes[i]));
3354 :
3355 1586 : vec_safe_push (types[i], TYPE_MAIN_VARIANT (argtypes[i]));
3356 : }
3357 :
3358 2037556 : else if (! convs)
3359 : return;
3360 :
3361 1842216 : for (; convs; convs = TREE_CHAIN (convs))
3362 : {
3363 1002122 : type = TREE_TYPE (convs);
3364 :
3365 1305474 : if (i == 0 && ref1
3366 1002122 : && (!TYPE_REF_P (type)
3367 56 : || CP_TYPE_CONST_P (TREE_TYPE (type))))
3368 303352 : continue;
3369 :
3370 698770 : if (code == COND_EXPR && TYPE_REF_P (type))
3371 0 : vec_safe_push (types[i], type);
3372 :
3373 698770 : type = non_reference (type);
3374 698770 : if (i != 0 || ! ref1)
3375 : {
3376 698714 : type = cv_unqualified (type_decays_to (type));
3377 698714 : if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
3378 0 : vec_safe_push (types[i], type);
3379 698714 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3380 279174 : type = type_promotes_to (type);
3381 : }
3382 :
3383 698770 : if (! vec_member (type, types[i]))
3384 697466 : vec_safe_push (types[i], type);
3385 : }
3386 : }
3387 : else
3388 : {
3389 8145874 : if (code == COND_EXPR && lvalue_p (args[i]))
3390 205 : vec_safe_push (types[i], build_reference_type (argtypes[i]));
3391 8145874 : type = non_reference (argtypes[i]);
3392 8145874 : if (i != 0 || ! ref1)
3393 : {
3394 7555054 : type = cv_unqualified (type_decays_to (type));
3395 7555054 : if (enum_p && UNSCOPED_ENUM_P (type))
3396 2301833 : vec_safe_push (types[i], type);
3397 7555054 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3398 6337327 : type = type_promotes_to (type);
3399 : }
3400 8145874 : vec_safe_push (types[i], type);
3401 : }
3402 : }
3403 :
3404 : /* Run through the possible parameter types of both arguments,
3405 : creating candidates with those parameter types. */
3406 10072622 : FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t)
3407 : {
3408 5521769 : unsigned jx;
3409 5521769 : tree u;
3410 :
3411 5521769 : if (!types[1]->is_empty ())
3412 13171775 : FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u)
3413 7650006 : add_builtin_candidate
3414 7650006 : (candidates, code, code2, fnname, t,
3415 : u, args, argtypes, flags, complain);
3416 : else
3417 160509 : add_builtin_candidate
3418 160509 : (candidates, code, code2, fnname, t,
3419 : NULL_TREE, args, argtypes, flags, complain);
3420 : }
3421 :
3422 4550853 : release_tree_vector (types[0]);
3423 4550853 : release_tree_vector (types[1]);
3424 : }
3425 :
3426 :
3427 : /* If TMPL can be successfully instantiated as indicated by
3428 : EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
3429 :
3430 : TMPL is the template. EXPLICIT_TARGS are any explicit template
3431 : arguments. ARGLIST is the arguments provided at the call-site.
3432 : This does not change ARGLIST. The RETURN_TYPE is the desired type
3433 : for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
3434 : as for add_function_candidate. If an OBJ is supplied, FLAGS and
3435 : CTYPE are ignored, and OBJ is as for add_conv_candidate.
3436 :
3437 : SHORTCUT_BAD_CONVS is as in add_function_candidate. */
3438 :
3439 : static struct z_candidate*
3440 161416109 : add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
3441 : tree ctype, tree explicit_targs, tree first_arg,
3442 : const vec<tree, va_gc> *arglist, tree return_type,
3443 : tree access_path, tree conversion_path,
3444 : int flags, tree obj, unification_kind_t strict,
3445 : bool shortcut_bad_convs, tsubst_flags_t complain)
3446 : {
3447 161416109 : int ntparms = DECL_NTPARMS (tmpl);
3448 161416109 : tree targs = make_tree_vec (ntparms);
3449 161416109 : unsigned int len = vec_safe_length (arglist);
3450 161416109 : unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
3451 161416109 : unsigned int skip_without_in_chrg = 0;
3452 161416109 : tree first_arg_without_in_chrg = first_arg;
3453 161416109 : tree *args_without_in_chrg;
3454 161416109 : unsigned int nargs_without_in_chrg;
3455 161416109 : unsigned int ia, ix;
3456 161416109 : tree arg;
3457 161416109 : struct z_candidate *cand;
3458 161416109 : tree fn;
3459 161416109 : struct rejection_reason *reason = NULL;
3460 161416109 : int errs;
3461 161416109 : conversion **convs = NULL;
3462 :
3463 : /* We don't do deduction on the in-charge parameter, the VTT
3464 : parameter or 'this'. */
3465 161416109 : if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
3466 : {
3467 25038603 : if (first_arg_without_in_chrg != NULL_TREE)
3468 : first_arg_without_in_chrg = NULL_TREE;
3469 13 : else if (return_type && strict == DEDUCE_CALL)
3470 : /* We're deducing for a call to the result of a template conversion
3471 : function, so the args don't contain 'this'; leave them alone. */;
3472 : else
3473 161416109 : ++skip_without_in_chrg;
3474 : }
3475 :
3476 161416109 : if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
3477 161416109 : || DECL_BASE_CONSTRUCTOR_P (tmpl))
3478 162046877 : && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
3479 : {
3480 77 : if (first_arg_without_in_chrg != NULL_TREE)
3481 : first_arg_without_in_chrg = NULL_TREE;
3482 : else
3483 77 : ++skip_without_in_chrg;
3484 : }
3485 :
3486 161416109 : if (len < skip_without_in_chrg)
3487 : return NULL;
3488 :
3489 185317806 : if (DECL_CONSTRUCTOR_P (tmpl) && nargs == 2
3490 179011330 : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (first_arg),
3491 17595221 : TREE_TYPE ((*arglist)[0])))
3492 : {
3493 : /* 12.8/6 says, "A declaration of a constructor for a class X is
3494 : ill-formed if its first parameter is of type (optionally cv-qualified)
3495 : X and either there are no other parameters or else all other
3496 : parameters have default arguments. A member function template is never
3497 : instantiated to produce such a constructor signature."
3498 :
3499 : So if we're trying to copy an object of the containing class, don't
3500 : consider a template constructor that has a first parameter type that
3501 : is just a template parameter, as we would deduce a signature that we
3502 : would then reject in the code below. */
3503 389981 : if (tree firstparm = FUNCTION_FIRST_USER_PARMTYPE (tmpl))
3504 : {
3505 389981 : firstparm = TREE_VALUE (firstparm);
3506 389981 : if (PACK_EXPANSION_P (firstparm))
3507 515 : firstparm = PACK_EXPANSION_PATTERN (firstparm);
3508 389981 : if (TREE_CODE (firstparm) == TEMPLATE_TYPE_PARM)
3509 : {
3510 87344 : gcc_assert (!explicit_targs);
3511 87344 : reason = invalid_copy_with_fn_template_rejection ();
3512 87344 : goto fail;
3513 : }
3514 : }
3515 : }
3516 :
3517 161328765 : nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
3518 161328765 : + (len - skip_without_in_chrg));
3519 161328765 : args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
3520 161328765 : ia = 0;
3521 161328765 : if (first_arg_without_in_chrg != NULL_TREE)
3522 : {
3523 0 : args_without_in_chrg[ia] = first_arg_without_in_chrg;
3524 0 : ++ia;
3525 : }
3526 266218746 : for (ix = skip_without_in_chrg;
3527 427547511 : vec_safe_iterate (arglist, ix, &arg);
3528 : ++ix)
3529 : {
3530 266218746 : args_without_in_chrg[ia] = arg;
3531 266218746 : ++ia;
3532 : }
3533 161328765 : gcc_assert (ia == nargs_without_in_chrg);
3534 :
3535 161328765 : if (!obj && explicit_targs)
3536 : {
3537 : /* Check that there's no obvious arity mismatch before proceeding with
3538 : deduction. This avoids substituting explicit template arguments
3539 : into the template (which could result in an error outside the
3540 : immediate context) when the resulting candidate would be unviable
3541 : anyway. */
3542 18075295 : int min_arity = 0, max_arity = 0;
3543 18075295 : tree parms = TYPE_ARG_TYPES (TREE_TYPE (tmpl));
3544 18075295 : parms = skip_artificial_parms_for (tmpl, parms);
3545 46179118 : for (; parms != void_list_node; parms = TREE_CHAIN (parms))
3546 : {
3547 22790251 : if (!parms || PACK_EXPANSION_P (TREE_VALUE (parms)))
3548 : {
3549 : max_arity = -1;
3550 : break;
3551 : }
3552 10028528 : if (TREE_PURPOSE (parms))
3553 : /* A parameter with a default argument. */
3554 161146 : ++max_arity;
3555 : else
3556 9867382 : ++min_arity, ++max_arity;
3557 : }
3558 18075295 : if (ia < (unsigned)min_arity)
3559 : {
3560 : /* Too few arguments. */
3561 146390 : reason = arity_rejection (NULL_TREE, min_arity, ia,
3562 : /*least_p=*/(max_arity == -1));
3563 146390 : goto fail;
3564 : }
3565 17928905 : else if (max_arity != -1 && ia > (unsigned)max_arity)
3566 : {
3567 : /* Too many arguments. */
3568 78243 : reason = arity_rejection (NULL_TREE, max_arity, ia);
3569 78243 : goto fail;
3570 : }
3571 : }
3572 :
3573 161104132 : errs = errorcount+sorrycount;
3574 161104132 : if (!obj)
3575 : {
3576 161104119 : convs = alloc_conversions (nargs);
3577 :
3578 161104119 : if (shortcut_bad_convs
3579 161104030 : && DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl)
3580 211005017 : && !DECL_CONSTRUCTOR_P (tmpl))
3581 : {
3582 : /* Check the 'this' conversion before proceeding with deduction.
3583 : This is effectively an extension of the DR 1391 resolution
3584 : that we perform in check_non_deducible_conversions, though it's
3585 : convenient to do this extra check here instead of there. */
3586 1136098 : tree parmtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (tmpl)));
3587 1136098 : tree argtype = lvalue_type (first_arg);
3588 1136098 : tree arg = first_arg;
3589 1136098 : conversion *t = build_this_conversion (tmpl, ctype,
3590 : parmtype, argtype, arg,
3591 : flags, complain);
3592 1136098 : convs[0] = t;
3593 1136098 : if (t->bad_p)
3594 : {
3595 19409 : reason = bad_arg_conversion_rejection (first_arg, 0,
3596 : arg, parmtype,
3597 19409 : EXPR_LOCATION (arg));
3598 19409 : goto fail;
3599 : }
3600 : }
3601 : }
3602 322157696 : fn = fn_type_unification (tmpl, explicit_targs, targs,
3603 : args_without_in_chrg,
3604 : nargs_without_in_chrg,
3605 : return_type, strict, flags, convs,
3606 161084723 : false, complain & tf_decltype);
3607 :
3608 161072973 : if (fn == error_mark_node)
3609 : {
3610 : /* Don't repeat unification later if it already resulted in errors. */
3611 134482062 : if (errorcount+sorrycount == errs)
3612 134481993 : reason = template_unification_rejection (tmpl, explicit_targs,
3613 : targs, args_without_in_chrg,
3614 : nargs_without_in_chrg,
3615 : return_type, strict, flags);
3616 : else
3617 69 : reason = template_unification_error_rejection ();
3618 134482062 : goto fail;
3619 : }
3620 :
3621 : /* Now the explicit specifier might have been deduced; check if this
3622 : declaration is explicit. If it is and we're ignoring non-converting
3623 : constructors, don't add this function to the set of candidates. */
3624 26590911 : if (((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
3625 : == LOOKUP_ONLYCONVERTING)
3626 26590911 : && DECL_NONCONVERTING_P (fn))
3627 : return NULL;
3628 :
3629 53181684 : if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
3630 : {
3631 1422368 : tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
3632 2844709 : if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
3633 : ctype))
3634 : {
3635 : /* We're trying to produce a constructor with a prohibited signature,
3636 : as discussed above; handle here any cases we didn't catch then,
3637 : such as X(X<T>). */
3638 125 : reason = invalid_copy_with_fn_template_rejection ();
3639 125 : goto fail;
3640 : }
3641 : }
3642 :
3643 26590717 : if (obj != NULL_TREE)
3644 : /* Aha, this is a conversion function. */
3645 9 : cand = add_conv_candidate (candidates, fn, obj, arglist,
3646 : access_path, conversion_path, complain);
3647 : else
3648 26590708 : cand = add_function_candidate (candidates, fn, ctype,
3649 : first_arg, arglist, access_path,
3650 : conversion_path, flags, convs,
3651 : shortcut_bad_convs, complain);
3652 26590717 : if (DECL_TI_TEMPLATE (fn) != tmpl)
3653 : /* This situation can occur if a member template of a template
3654 : class is specialized. Then, instantiate_template might return
3655 : an instantiation of the specialization, in which case the
3656 : DECL_TI_TEMPLATE field will point at the original
3657 : specialization. For example:
3658 :
3659 : template <class T> struct S { template <class U> void f(U);
3660 : template <> void f(int) {}; };
3661 : S<double> sd;
3662 : sd.f(3);
3663 :
3664 : Here, TMPL will be template <class U> S<double>::f(U).
3665 : And, instantiate template will give us the specialization
3666 : template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
3667 : for this will point at template <class T> template <> S<T>::f(int),
3668 : so that we can find the definition. For the purposes of
3669 : overload resolution, however, we want the original TMPL. */
3670 2083422 : cand->template_decl = build_template_info (tmpl, targs);
3671 : else
3672 24507295 : cand->template_decl = DECL_TEMPLATE_INFO (fn);
3673 26590717 : cand->explicit_targs = explicit_targs;
3674 :
3675 26590717 : return cand;
3676 134813573 : fail:
3677 134813573 : int viable = (reason->code == rr_bad_arg_conversion ? -1 : 0);
3678 134813573 : return add_candidate (candidates, tmpl, first_arg, arglist, nargs, convs,
3679 134813573 : access_path, conversion_path, viable, reason, flags);
3680 : }
3681 :
3682 :
3683 : static struct z_candidate *
3684 161416096 : add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
3685 : tree explicit_targs, tree first_arg,
3686 : const vec<tree, va_gc> *arglist, tree return_type,
3687 : tree access_path, tree conversion_path, int flags,
3688 : unification_kind_t strict, bool shortcut_bad_convs,
3689 : tsubst_flags_t complain)
3690 : {
3691 161416096 : return
3692 0 : add_template_candidate_real (candidates, tmpl, ctype,
3693 : explicit_targs, first_arg, arglist,
3694 : return_type, access_path, conversion_path,
3695 : flags, NULL_TREE, strict, shortcut_bad_convs,
3696 0 : complain);
3697 : }
3698 :
3699 : /* Create an overload candidate for the conversion function template TMPL,
3700 : returning RETURN_TYPE, which will be invoked for expression OBJ to produce a
3701 : pointer-to-function which will in turn be called with the argument list
3702 : ARGLIST, and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
3703 : passed on to implicit_conversion. */
3704 :
3705 : static struct z_candidate *
3706 13 : add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
3707 : tree obj,
3708 : const vec<tree, va_gc> *arglist,
3709 : tree return_type, tree access_path,
3710 : tree conversion_path, tsubst_flags_t complain)
3711 : {
3712 13 : return
3713 13 : add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
3714 : NULL_TREE, arglist, return_type, access_path,
3715 : conversion_path, 0, obj, DEDUCE_CALL,
3716 13 : /*shortcut_bad_convs=*/false, complain);
3717 : }
3718 :
3719 : /* The CANDS are the set of candidates that were considered for
3720 : overload resolution. Return the set of viable candidates, or CANDS
3721 : if none are viable. If any of the candidates were viable, set
3722 : *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
3723 : considered viable only if it is strictly viable. */
3724 :
3725 : static struct z_candidate*
3726 117036452 : splice_viable (struct z_candidate *cands,
3727 : bool strict_p,
3728 : bool *any_viable_p)
3729 : {
3730 117036452 : struct z_candidate *viable;
3731 117036452 : struct z_candidate **last_viable;
3732 117036452 : struct z_candidate **cand;
3733 117036452 : bool found_strictly_viable = false;
3734 :
3735 : /* Be strict inside templates, since build_over_call won't actually
3736 : do the conversions to get pedwarns. */
3737 117036452 : if (processing_template_decl)
3738 22154317 : strict_p = true;
3739 :
3740 117036452 : viable = NULL;
3741 117036452 : last_viable = &viable;
3742 117036452 : *any_viable_p = false;
3743 :
3744 117036452 : cand = &cands;
3745 405369078 : while (*cand)
3746 : {
3747 288332626 : struct z_candidate *c = *cand;
3748 288332626 : if (!strict_p
3749 114666803 : && (c->viable == 1 || TREE_CODE (c->fn) == TEMPLATE_DECL))
3750 : {
3751 : /* Be strict in the presence of a viable candidate. Also if
3752 : there are template candidates, so that we get deduction errors
3753 : for them instead of silently preferring a bad conversion. */
3754 84400288 : strict_p = true;
3755 84400288 : if (viable && !found_strictly_viable)
3756 : {
3757 : /* Put any spliced near matches back onto the main list so
3758 : that we see them if there is no strict match. */
3759 1292708 : *any_viable_p = false;
3760 1292708 : *last_viable = cands;
3761 1292708 : cands = viable;
3762 1292708 : viable = NULL;
3763 1292708 : last_viable = &viable;
3764 : }
3765 : }
3766 :
3767 288332626 : if (strict_p ? c->viable == 1 : c->viable)
3768 : {
3769 124311541 : *last_viable = c;
3770 124311541 : *cand = c->next;
3771 124311541 : c->next = NULL;
3772 124311541 : last_viable = &c->next;
3773 124311541 : *any_viable_p = true;
3774 124311541 : if (c->viable == 1)
3775 122932282 : found_strictly_viable = true;
3776 : }
3777 : else
3778 164021085 : cand = &c->next;
3779 : }
3780 :
3781 117036452 : return viable ? viable : cands;
3782 : }
3783 :
3784 : static bool
3785 112323576 : any_strictly_viable (struct z_candidate *cands)
3786 : {
3787 122665032 : for (; cands; cands = cands->next)
3788 10760229 : if (cands->viable == 1)
3789 : return true;
3790 : return false;
3791 : }
3792 :
3793 : /* OBJ is being used in an expression like "OBJ.f (...)". In other
3794 : words, it is about to become the "this" pointer for a member
3795 : function call. Take the address of the object. */
3796 :
3797 : static tree
3798 24696595 : build_this (tree obj)
3799 : {
3800 : /* In a template, we are only concerned about the type of the
3801 : expression, so we can take a shortcut. */
3802 24696595 : if (processing_template_decl)
3803 21 : return build_address (obj);
3804 :
3805 24696574 : return cp_build_addr_expr (obj, tf_warning_or_error);
3806 : }
3807 :
3808 : /* Returns true iff functions are equivalent. Equivalent functions are
3809 : not '==' only if one is a function-local extern function or if
3810 : both are extern "C". */
3811 :
3812 : static inline int
3813 2923936 : equal_functions (tree fn1, tree fn2)
3814 : {
3815 2923936 : if (TREE_CODE (fn1) != TREE_CODE (fn2))
3816 : return 0;
3817 2915275 : if (TREE_CODE (fn1) == TEMPLATE_DECL)
3818 27045 : return fn1 == fn2;
3819 5776440 : if (DECL_LOCAL_DECL_P (fn1) || DECL_LOCAL_DECL_P (fn2)
3820 5776436 : || DECL_EXTERN_C_FUNCTION_P (fn1))
3821 2201962 : return decls_match (fn1, fn2);
3822 686268 : return fn1 == fn2;
3823 : }
3824 :
3825 : /* Print information about a candidate FN being rejected due to INFO. */
3826 :
3827 : static void
3828 5121 : print_conversion_rejection (location_t loc, struct conversion_info *info,
3829 : tree fn)
3830 : {
3831 5121 : tree from = info->from;
3832 5121 : if (!TYPE_P (from))
3833 4280 : from = lvalue_type (from);
3834 5121 : if (info->n_arg == -1)
3835 : {
3836 : /* Conversion of implicit `this' argument failed. */
3837 24 : if (!TYPE_P (info->from))
3838 : /* A bad conversion for 'this' must be discarding cv-quals. */
3839 24 : inform (loc, " passing %qT as %<this%> "
3840 : "argument discards qualifiers",
3841 : from);
3842 : else
3843 0 : inform (loc, " no known conversion for implicit "
3844 : "%<this%> parameter from %qH to %qI",
3845 : from, info->to_type);
3846 : }
3847 5097 : else if (!TYPE_P (info->from))
3848 : {
3849 4256 : if (info->n_arg >= 0)
3850 4256 : inform (loc, " conversion of argument %d would be ill-formed:",
3851 : info->n_arg + 1);
3852 4256 : iloc_sentinel ils = loc;
3853 4256 : perform_implicit_conversion (info->to_type, info->from,
3854 : tf_warning_or_error);
3855 4256 : }
3856 841 : else if (info->n_arg == -2)
3857 : /* Conversion of conversion function return value failed. */
3858 35 : inform (loc, " no known conversion from %qH to %qI",
3859 : from, info->to_type);
3860 : else
3861 : {
3862 806 : if (TREE_CODE (fn) == FUNCTION_DECL)
3863 656 : loc = get_fndecl_argument_location (fn, info->n_arg);
3864 806 : inform (loc, " no known conversion for argument %d from %qH to %qI",
3865 806 : info->n_arg + 1, from, info->to_type);
3866 : }
3867 5121 : }
3868 :
3869 : /* Print information about a candidate with WANT parameters and we found
3870 : HAVE. */
3871 :
3872 : static void
3873 522 : print_arity_information (location_t loc, unsigned int have, unsigned int want,
3874 : bool least_p)
3875 : {
3876 522 : if (least_p)
3877 0 : inform_n (loc, want,
3878 : " candidate expects at least %d argument, %d provided",
3879 : " candidate expects at least %d arguments, %d provided",
3880 : want, have);
3881 : else
3882 522 : inform_n (loc, want,
3883 : " candidate expects %d argument, %d provided",
3884 : " candidate expects %d arguments, %d provided",
3885 : want, have);
3886 522 : }
3887 :
3888 : /* Print information about one overload candidate CANDIDATE. MSGSTR
3889 : is the text to print before the candidate itself.
3890 :
3891 : NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
3892 : to have been run through gettext by the caller. This wart makes
3893 : life simpler in print_z_candidates and for the translators. */
3894 :
3895 : static void
3896 11205 : print_z_candidate (location_t loc, const char *msgstr,
3897 : struct z_candidate *candidate)
3898 : {
3899 11205 : const char *msg = (msgstr == NULL
3900 11205 : ? ""
3901 11205 : : ACONCAT ((_(msgstr), " ", NULL)));
3902 11205 : tree fn = candidate->fn;
3903 11205 : if (flag_new_inheriting_ctors)
3904 11178 : fn = strip_inheriting_ctors (fn);
3905 11205 : location_t cloc = location_of (fn);
3906 :
3907 11205 : if (identifier_p (fn))
3908 : {
3909 212 : cloc = loc;
3910 212 : if (candidate->num_convs == 3)
3911 0 : inform (cloc, "%s%<%D(%T, %T, %T)%> (built-in)", msg, fn,
3912 0 : candidate->convs[0]->type,
3913 0 : candidate->convs[1]->type,
3914 0 : candidate->convs[2]->type);
3915 212 : else if (candidate->num_convs == 2)
3916 185 : inform (cloc, "%s%<%D(%T, %T)%> (built-in)", msg, fn,
3917 185 : candidate->convs[0]->type,
3918 185 : candidate->convs[1]->type);
3919 : else
3920 27 : inform (cloc, "%s%<%D(%T)%> (built-in)", msg, fn,
3921 27 : candidate->convs[0]->type);
3922 : }
3923 10993 : else if (TYPE_P (fn))
3924 10 : inform (cloc, "%s%qT (conversion)", msg, fn);
3925 10983 : else if (candidate->viable == -1)
3926 4293 : inform (cloc, "%s%#qD (near match)", msg, fn);
3927 6690 : else if (DECL_DELETED_FN (fn))
3928 34 : inform (cloc, "%s%#qD (deleted)", msg, fn);
3929 6656 : else if (candidate->reversed ())
3930 32 : inform (cloc, "%s%#qD (reversed)", msg, fn);
3931 6624 : else if (candidate->rewritten ())
3932 0 : inform (cloc, "%s%#qD (rewritten)", msg, fn);
3933 : else
3934 6624 : inform (cloc, "%s%#qD", msg, fn);
3935 11205 : if (fn != candidate->fn)
3936 : {
3937 27 : cloc = location_of (candidate->fn);
3938 27 : inform (cloc, " inherited here");
3939 : }
3940 : /* Give the user some information about why this candidate failed. */
3941 11205 : if (candidate->reason != NULL)
3942 : {
3943 8255 : struct rejection_reason *r = candidate->reason;
3944 :
3945 8255 : switch (r->code)
3946 : {
3947 522 : case rr_arity:
3948 522 : print_arity_information (cloc, r->u.arity.actual,
3949 522 : r->u.arity.expected,
3950 522 : r->u.arity.least_p);
3951 522 : break;
3952 812 : case rr_arg_conversion:
3953 812 : print_conversion_rejection (cloc, &r->u.conversion, fn);
3954 812 : break;
3955 4309 : case rr_bad_arg_conversion:
3956 4309 : print_conversion_rejection (cloc, &r->u.bad_conversion, fn);
3957 4309 : break;
3958 10 : case rr_explicit_conversion:
3959 10 : inform (cloc, " return type %qT of explicit conversion function "
3960 : "cannot be converted to %qT with a qualification "
3961 : "conversion", r->u.conversion.from,
3962 : r->u.conversion.to_type);
3963 10 : break;
3964 6 : case rr_template_conversion:
3965 6 : inform (cloc, " conversion from return type %qT of template "
3966 : "conversion function specialization to %qT is not an "
3967 : "exact match", r->u.conversion.from,
3968 : r->u.conversion.to_type);
3969 6 : break;
3970 2557 : case rr_template_unification:
3971 : /* We use template_unification_error_rejection if unification caused
3972 : actual non-SFINAE errors, in which case we don't need to repeat
3973 : them here. */
3974 2557 : if (r->u.template_unification.tmpl == NULL_TREE)
3975 : {
3976 27 : inform (cloc, " substitution of deduced template arguments "
3977 : "resulted in errors seen above");
3978 27 : break;
3979 : }
3980 : /* Re-run template unification with diagnostics. */
3981 2530 : inform (cloc, " template argument deduction/substitution failed:");
3982 2530 : fn_type_unification (r->u.template_unification.tmpl,
3983 : r->u.template_unification.explicit_targs,
3984 : (make_tree_vec
3985 : (r->u.template_unification.num_targs)),
3986 : r->u.template_unification.args,
3987 : r->u.template_unification.nargs,
3988 : r->u.template_unification.return_type,
3989 : r->u.template_unification.strict,
3990 : r->u.template_unification.flags,
3991 : NULL, true, false);
3992 2530 : break;
3993 2 : case rr_invalid_copy:
3994 2 : inform (cloc,
3995 : " a constructor taking a single argument of its own "
3996 : "class type is invalid");
3997 2 : break;
3998 25 : case rr_constraint_failure:
3999 25 : diagnose_constraints (cloc, fn, NULL_TREE);
4000 25 : break;
4001 12 : case rr_inherited_ctor:
4002 12 : inform (cloc, " an inherited constructor is not a candidate for "
4003 : "initialization from an expression of the same or derived "
4004 : "type");
4005 12 : break;
4006 0 : case rr_none:
4007 0 : default:
4008 : /* This candidate didn't have any issues or we failed to
4009 : handle a particular code. Either way... */
4010 0 : gcc_unreachable ();
4011 : }
4012 : }
4013 11205 : }
4014 :
4015 : static void
4016 4444 : print_z_candidates (location_t loc, struct z_candidate *candidates)
4017 : {
4018 4444 : struct z_candidate *cand1;
4019 4444 : struct z_candidate **cand2;
4020 :
4021 4444 : if (!candidates)
4022 816 : return;
4023 :
4024 : /* Remove non-viable deleted candidates. */
4025 3628 : cand1 = candidates;
4026 14806 : for (cand2 = &cand1; *cand2; )
4027 : {
4028 11178 : if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
4029 8346 : && !(*cand2)->viable
4030 12435 : && DECL_DELETED_FN ((*cand2)->fn))
4031 71 : *cand2 = (*cand2)->next;
4032 : else
4033 11107 : cand2 = &(*cand2)->next;
4034 : }
4035 : /* ...if there are any non-deleted ones. */
4036 3628 : if (cand1)
4037 3624 : candidates = cand1;
4038 :
4039 : /* There may be duplicates in the set of candidates. We put off
4040 : checking this condition as long as possible, since we have no way
4041 : to eliminate duplicates from a set of functions in less than n^2
4042 : time. Now we are about to emit an error message, so it is more
4043 : permissible to go slowly. */
4044 14716 : for (cand1 = candidates; cand1; cand1 = cand1->next)
4045 : {
4046 11088 : tree fn = cand1->fn;
4047 : /* Skip builtin candidates and conversion functions. */
4048 11088 : if (!DECL_P (fn))
4049 222 : continue;
4050 10866 : cand2 = &cand1->next;
4051 68737 : while (*cand2)
4052 : {
4053 57871 : if (DECL_P ((*cand2)->fn)
4054 57871 : && equal_functions (fn, (*cand2)->fn))
4055 23 : *cand2 = (*cand2)->next;
4056 : else
4057 57848 : cand2 = &(*cand2)->next;
4058 : }
4059 : }
4060 :
4061 14716 : for (; candidates; candidates = candidates->next)
4062 11088 : print_z_candidate (loc, N_("candidate:"), candidates);
4063 : }
4064 :
4065 : /* USER_SEQ is a user-defined conversion sequence, beginning with a
4066 : USER_CONV. STD_SEQ is the standard conversion sequence applied to
4067 : the result of the conversion function to convert it to the final
4068 : desired type. Merge the two sequences into a single sequence,
4069 : and return the merged sequence. */
4070 :
4071 : static conversion *
4072 4772798 : merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
4073 : {
4074 4772798 : conversion **t;
4075 4772798 : bool bad = user_seq->bad_p;
4076 :
4077 4772798 : gcc_assert (user_seq->kind == ck_user);
4078 :
4079 : /* Find the end of the second conversion sequence. */
4080 4984884 : for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
4081 : {
4082 : /* The entire sequence is a user-conversion sequence. */
4083 212086 : (*t)->user_conv_p = true;
4084 212086 : if (bad)
4085 1501 : (*t)->bad_p = true;
4086 : }
4087 :
4088 4772798 : if ((*t)->rvaluedness_matches_p)
4089 : /* We're binding a reference directly to the result of the conversion.
4090 : build_user_type_conversion_1 stripped the REFERENCE_TYPE from the return
4091 : type, but we want it back. */
4092 140285 : user_seq->type = TREE_TYPE (TREE_TYPE (user_seq->cand->fn));
4093 :
4094 : /* Replace the identity conversion with the user conversion
4095 : sequence. */
4096 4772798 : *t = user_seq;
4097 :
4098 4772798 : return std_seq;
4099 : }
4100 :
4101 : /* Handle overload resolution for initializing an object of class type from
4102 : an initializer list. First we look for a suitable constructor that
4103 : takes a std::initializer_list; if we don't find one, we then look for a
4104 : non-list constructor.
4105 :
4106 : Parameters are as for add_candidates, except that the arguments are in
4107 : the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
4108 : the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
4109 :
4110 : static void
4111 612509 : add_list_candidates (tree fns, tree first_arg,
4112 : const vec<tree, va_gc> *args, tree totype,
4113 : tree explicit_targs, bool template_only,
4114 : tree conversion_path, tree access_path,
4115 : int flags,
4116 : struct z_candidate **candidates,
4117 : tsubst_flags_t complain)
4118 : {
4119 612509 : gcc_assert (*candidates == NULL);
4120 :
4121 : /* We're looking for a ctor for list-initialization. */
4122 612509 : flags |= LOOKUP_LIST_INIT_CTOR;
4123 : /* And we don't allow narrowing conversions. We also use this flag to
4124 : avoid the copy constructor call for copy-list-initialization. */
4125 612509 : flags |= LOOKUP_NO_NARROWING;
4126 :
4127 1225018 : unsigned nart = num_artificial_parms_for (OVL_FIRST (fns)) - 1;
4128 612509 : tree init_list = (*args)[nart];
4129 :
4130 : /* Always use the default constructor if the list is empty (DR 990). */
4131 612509 : if (CONSTRUCTOR_NELTS (init_list) == 0
4132 612509 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
4133 : ;
4134 543794 : else if (CONSTRUCTOR_IS_DESIGNATED_INIT (init_list)
4135 543794 : && !CP_AGGREGATE_TYPE_P (totype))
4136 : {
4137 30 : if (complain & tf_error)
4138 4 : error ("designated initializers cannot be used with a "
4139 : "non-aggregate type %qT", totype);
4140 1485 : return;
4141 : }
4142 : /* If the class has a list ctor, try passing the list as a single
4143 : argument first, but only consider list ctors. */
4144 543764 : else if (TYPE_HAS_LIST_CTOR (totype))
4145 : {
4146 42863 : flags |= LOOKUP_LIST_ONLY;
4147 42863 : add_candidates (fns, first_arg, args, NULL_TREE,
4148 : explicit_targs, template_only, conversion_path,
4149 : access_path, flags, candidates, complain);
4150 85726 : if (any_strictly_viable (*candidates))
4151 : return;
4152 : }
4153 :
4154 : /* Expand the CONSTRUCTOR into a new argument vec. */
4155 611024 : vec<tree, va_gc> *new_args;
4156 1153137 : vec_alloc (new_args, nart + CONSTRUCTOR_NELTS (init_list));
4157 611027 : for (unsigned i = 0; i < nart; ++i)
4158 3 : new_args->quick_push ((*args)[i]);
4159 1587692 : for (unsigned i = 0; i < CONSTRUCTOR_NELTS (init_list); ++i)
4160 976668 : new_args->quick_push (CONSTRUCTOR_ELT (init_list, i)->value);
4161 :
4162 : /* We aren't looking for list-ctors anymore. */
4163 611024 : flags &= ~LOOKUP_LIST_ONLY;
4164 : /* We allow more user-defined conversions within an init-list. */
4165 611024 : flags &= ~LOOKUP_NO_CONVERSION;
4166 :
4167 611024 : add_candidates (fns, first_arg, new_args, NULL_TREE,
4168 : explicit_targs, template_only, conversion_path,
4169 : access_path, flags, candidates, complain);
4170 : }
4171 :
4172 : /* Given C(std::initializer_list<A>), return A. */
4173 :
4174 : static tree
4175 878 : list_ctor_element_type (tree fn)
4176 : {
4177 878 : gcc_checking_assert (is_list_ctor (fn));
4178 :
4179 878 : tree parm = FUNCTION_FIRST_USER_PARMTYPE (fn);
4180 878 : parm = non_reference (TREE_VALUE (parm));
4181 878 : return TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
4182 : }
4183 :
4184 : /* If EXPR is a braced-init-list where the elements all decay to the same type,
4185 : return that type. */
4186 :
4187 : static tree
4188 807 : braced_init_element_type (tree expr)
4189 : {
4190 807 : if (TREE_CODE (expr) == CONSTRUCTOR
4191 807 : && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
4192 0 : return TREE_TYPE (TREE_TYPE (expr));
4193 807 : if (!BRACE_ENCLOSED_INITIALIZER_P (expr))
4194 : return NULL_TREE;
4195 :
4196 807 : tree elttype = NULL_TREE;
4197 4934 : for (constructor_elt &e: CONSTRUCTOR_ELTS (expr))
4198 : {
4199 2625 : tree type = TREE_TYPE (e.value);
4200 2625 : type = type_decays_to (type);
4201 2625 : if (!elttype)
4202 : elttype = type;
4203 1828 : else if (!same_type_p (type, elttype))
4204 : return NULL_TREE;
4205 : }
4206 : return elttype;
4207 : }
4208 :
4209 : /* True iff EXPR contains any temporaries with non-trivial destruction.
4210 :
4211 : ??? Also ignore classes with non-trivial but no-op destruction other than
4212 : std::allocator? */
4213 :
4214 : static bool
4215 148 : has_non_trivial_temporaries (tree expr)
4216 : {
4217 148 : auto_vec<tree*> temps;
4218 148 : cp_walk_tree_without_duplicates (&expr, find_temps_r, &temps);
4219 265 : for (tree *p : temps)
4220 : {
4221 39 : tree t = TREE_TYPE (*p);
4222 39 : if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (t)
4223 39 : && !is_std_allocator (t))
4224 : return true;
4225 : }
4226 : return false;
4227 148 : }
4228 :
4229 : /* We're initializing an array of ELTTYPE from INIT. If it seems useful,
4230 : return INIT as an array (of its own type) so the caller can initialize the
4231 : target array in a loop. */
4232 :
4233 : static tree
4234 3012 : maybe_init_list_as_array (tree elttype, tree init)
4235 : {
4236 : /* Only do this if the array can go in rodata but not once converted. */
4237 3012 : if (!TYPE_NON_AGGREGATE_CLASS (elttype))
4238 : return NULL_TREE;
4239 807 : tree init_elttype = braced_init_element_type (init);
4240 807 : if (!init_elttype || !SCALAR_TYPE_P (init_elttype) || !TREE_CONSTANT (init))
4241 : return NULL_TREE;
4242 :
4243 : /* Check with a stub expression to weed out special cases, and check whether
4244 : we call the same function for direct-init as copy-list-init. */
4245 208 : conversion_obstack_sentinel cos;
4246 208 : tree arg = build_stub_object (init_elttype);
4247 208 : conversion *c = implicit_conversion (elttype, init_elttype, arg, false,
4248 : LOOKUP_NORMAL, tf_none);
4249 208 : if (c && c->kind == ck_rvalue)
4250 0 : c = next_conversion (c);
4251 202 : if (!c || c->kind != ck_user)
4252 : return NULL_TREE;
4253 :
4254 199 : tree first = CONSTRUCTOR_ELT (init, 0)->value;
4255 199 : conversion *fc = implicit_conversion (elttype, init_elttype, first, false,
4256 : LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING,
4257 : tf_none);
4258 199 : if (fc && fc->kind == ck_rvalue)
4259 40 : fc = next_conversion (fc);
4260 199 : if (!fc || fc->kind != ck_user || fc->cand->fn != c->cand->fn)
4261 : return NULL_TREE;
4262 157 : first = convert_like (fc, first, tf_none);
4263 157 : if (first == error_mark_node)
4264 : /* Let the normal code give the error. */
4265 : return NULL_TREE;
4266 :
4267 : /* Don't do this if the conversion would be constant. */
4268 151 : first = maybe_constant_init (first);
4269 151 : if (TREE_CONSTANT (first))
4270 : return NULL_TREE;
4271 :
4272 : /* We can't do this if the conversion creates temporaries that need
4273 : to live until the whole array is initialized. */
4274 148 : if (has_non_trivial_temporaries (first))
4275 : return NULL_TREE;
4276 :
4277 : /* We can't do this if copying from the initializer_list would be
4278 : ill-formed. */
4279 148 : tree copy_argtypes = make_tree_vec (1);
4280 148 : TREE_VEC_ELT (copy_argtypes, 0)
4281 148 : = cp_build_qualified_type (elttype, TYPE_QUAL_CONST);
4282 148 : if (!is_xible (INIT_EXPR, elttype, copy_argtypes))
4283 : return NULL_TREE;
4284 :
4285 142 : init_elttype = cp_build_qualified_type (init_elttype, TYPE_QUAL_CONST);
4286 284 : tree arr = build_array_of_n_type (init_elttype, CONSTRUCTOR_NELTS (init));
4287 142 : arr = finish_compound_literal (arr, init, tf_none);
4288 142 : DECL_MERGEABLE (TARGET_EXPR_SLOT (arr)) = true;
4289 142 : return arr;
4290 208 : }
4291 :
4292 : /* If we were going to call e.g. vector(initializer_list<string>) starting
4293 : with a list of string-literals (which is inefficient, see PR105838),
4294 : instead build an array of const char* and pass it to the range constructor.
4295 : But only do this for standard library types, where we can assume the
4296 : transformation makes sense.
4297 :
4298 : Really the container classes should have initializer_list<U> constructors to
4299 : get the same effect more simply; this is working around that lack. */
4300 :
4301 : static tree
4302 4640111 : maybe_init_list_as_range (tree fn, tree expr)
4303 : {
4304 4640111 : if (!processing_template_decl
4305 4572623 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
4306 339170 : && is_list_ctor (fn)
4307 4641118 : && decl_in_std_namespace_p (fn))
4308 : {
4309 878 : tree to = list_ctor_element_type (fn);
4310 878 : if (tree init = maybe_init_list_as_array (to, expr))
4311 : {
4312 33 : tree begin = decay_conversion (TARGET_EXPR_SLOT (init), tf_none);
4313 33 : tree nelts = array_type_nelts_top (TREE_TYPE (init));
4314 33 : tree end = cp_build_binary_op (input_location, PLUS_EXPR, begin,
4315 : nelts, tf_none);
4316 33 : begin = cp_build_compound_expr (init, begin, tf_none);
4317 33 : return build_constructor_va (init_list_type_node, 2,
4318 33 : NULL_TREE, begin, NULL_TREE, end);
4319 : }
4320 : }
4321 :
4322 : return NULL_TREE;
4323 : }
4324 :
4325 : /* Returns the best overload candidate to perform the requested
4326 : conversion. This function is used for three the overloading situations
4327 : described in [over.match.copy], [over.match.conv], and [over.match.ref].
4328 : If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
4329 : per [dcl.init.ref], so we ignore temporary bindings. */
4330 :
4331 : static struct z_candidate *
4332 25015572 : build_user_type_conversion_1 (tree totype, tree expr, int flags,
4333 : tsubst_flags_t complain)
4334 : {
4335 25015572 : struct z_candidate *candidates, *cand;
4336 25015572 : tree fromtype;
4337 25015572 : tree ctors = NULL_TREE;
4338 25015572 : tree conv_fns = NULL_TREE;
4339 25015572 : conversion *conv = NULL;
4340 25015572 : tree first_arg = NULL_TREE;
4341 25015572 : vec<tree, va_gc> *args = NULL;
4342 25015572 : bool any_viable_p;
4343 25015572 : int convflags;
4344 :
4345 25015572 : if (!expr)
4346 : return NULL;
4347 :
4348 25015564 : fromtype = TREE_TYPE (expr);
4349 :
4350 : /* We represent conversion within a hierarchy using RVALUE_CONV and
4351 : BASE_CONV, as specified by [over.best.ics]; these become plain
4352 : constructor calls, as specified in [dcl.init]. */
4353 25015564 : gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
4354 : || !DERIVED_FROM_P (totype, fromtype));
4355 :
4356 25015564 : if (CLASS_TYPE_P (totype))
4357 : /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
4358 : creating a garbage BASELINK; constructors can't be inherited. */
4359 16413128 : ctors = get_class_binding (totype, complete_ctor_identifier);
4360 :
4361 25015564 : tree to_nonref = non_reference (totype);
4362 25015564 : if (MAYBE_CLASS_TYPE_P (fromtype))
4363 : {
4364 13587016 : if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
4365 13566203 : (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
4366 10710784 : && DERIVED_FROM_P (to_nonref, fromtype)))
4367 : {
4368 : /* [class.conv.fct] A conversion function is never used to
4369 : convert a (possibly cv-qualified) object to the (possibly
4370 : cv-qualified) same object type (or a reference to it), to a
4371 : (possibly cv-qualified) base class of that type (or a
4372 : reference to it)... */
4373 : }
4374 : else
4375 13566199 : conv_fns = lookup_conversions (fromtype);
4376 : }
4377 :
4378 25015564 : candidates = 0;
4379 25015564 : flags |= LOOKUP_NO_CONVERSION;
4380 25015564 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4381 340046 : flags |= LOOKUP_NO_NARROWING;
4382 : /* Prevent add_candidates from treating a non-strictly viable candidate
4383 : as unviable. */
4384 25015564 : complain |= tf_conv;
4385 :
4386 : /* It's OK to bind a temporary for converting constructor arguments, but
4387 : not in converting the return value of a conversion operator. */
4388 25015564 : convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION
4389 25015564 : | (flags & LOOKUP_NO_NARROWING));
4390 25015564 : flags &= ~LOOKUP_NO_TEMP_BIND;
4391 :
4392 25015564 : if (ctors)
4393 : {
4394 16323452 : int ctorflags = flags;
4395 :
4396 16323452 : first_arg = build_dummy_object (totype);
4397 :
4398 : /* We should never try to call the abstract or base constructor
4399 : from here. */
4400 114922916 : gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_FIRST (ctors))
4401 : && !DECL_HAS_VTT_PARM_P (OVL_FIRST (ctors)));
4402 :
4403 16323452 : args = make_tree_vector_single (expr);
4404 16323452 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4405 : {
4406 : /* List-initialization. */
4407 340046 : add_list_candidates (ctors, first_arg, args, totype, NULL_TREE,
4408 340046 : false, TYPE_BINFO (totype), TYPE_BINFO (totype),
4409 : ctorflags, &candidates, complain);
4410 : }
4411 : else
4412 : {
4413 15983406 : add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
4414 15983406 : TYPE_BINFO (totype), TYPE_BINFO (totype),
4415 : ctorflags, &candidates, complain);
4416 : }
4417 :
4418 95378903 : for (cand = candidates; cand; cand = cand->next)
4419 : {
4420 79055451 : cand->second_conv = build_identity_conv (totype, NULL_TREE);
4421 :
4422 : /* If totype isn't a reference, and LOOKUP_ONLYCONVERTING is
4423 : set, then this is copy-initialization. In that case, "The
4424 : result of the call is then used to direct-initialize the
4425 : object that is the destination of the copy-initialization."
4426 : [dcl.init]
4427 :
4428 : We represent this in the conversion sequence with an
4429 : rvalue conversion, which means a constructor call. */
4430 79055451 : if (!TYPE_REF_P (totype)
4431 79055451 : && cxx_dialect < cxx17
4432 772347 : && (flags & LOOKUP_ONLYCONVERTING)
4433 669914 : && !(convflags & LOOKUP_NO_TEMP_BIND))
4434 185891 : cand->second_conv
4435 185891 : = build_conv (ck_rvalue, totype, cand->second_conv);
4436 : }
4437 : }
4438 :
4439 25015564 : if (conv_fns)
4440 : {
4441 5163023 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4442 0 : first_arg = CONSTRUCTOR_ELT (expr, 0)->value;
4443 : else
4444 25015564 : first_arg = expr;
4445 : }
4446 :
4447 30389152 : for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
4448 : {
4449 5373588 : tree conversion_path = TREE_PURPOSE (conv_fns);
4450 5373588 : struct z_candidate *old_candidates;
4451 :
4452 : /* If LOOKUP_NO_CONVERSION, don't consider a conversion function that
4453 : would need an addional user-defined conversion, i.e. if the return
4454 : type differs in class-ness from the desired type. So we avoid
4455 : considering operator bool when calling a copy constructor.
4456 :
4457 : This optimization avoids the failure in PR97600, and is allowed by
4458 : [temp.inst]/9: "If the function selected by overload resolution can be
4459 : determined without instantiating a class template definition, it is
4460 : unspecified whether that instantiation actually takes place." */
4461 5373588 : tree convtype = non_reference (TREE_TYPE (conv_fns));
4462 6766823 : if ((flags & LOOKUP_NO_CONVERSION)
4463 5373588 : && !WILDCARD_TYPE_P (convtype)
4464 10536376 : && (CLASS_TYPE_P (to_nonref)
4465 5268188 : != CLASS_TYPE_P (convtype)))
4466 1393235 : continue;
4467 :
4468 : /* If we are called to convert to a reference type, we are trying to
4469 : find a direct binding, so don't even consider temporaries. If
4470 : we don't find a direct binding, the caller will try again to
4471 : look for a temporary binding. */
4472 3980353 : if (TYPE_REF_P (totype))
4473 873537 : convflags |= LOOKUP_NO_TEMP_BIND;
4474 :
4475 3980353 : old_candidates = candidates;
4476 3980353 : add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
4477 : NULL_TREE, false,
4478 3980353 : conversion_path, TYPE_BINFO (fromtype),
4479 : flags, &candidates, complain);
4480 :
4481 7432299 : for (cand = candidates; cand != old_candidates; cand = cand->next)
4482 : {
4483 3451946 : if (cand->viable == 0)
4484 : /* Already rejected, don't change to -1. */
4485 32424 : continue;
4486 :
4487 3419522 : tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
4488 3419522 : conversion *ics
4489 3419522 : = implicit_conversion (totype,
4490 : rettype,
4491 : 0,
4492 : /*c_cast_p=*/false, convflags,
4493 : complain);
4494 :
4495 : /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
4496 : copy-initialization. In that case, "The result of the
4497 : call is then used to direct-initialize the object that is
4498 : the destination of the copy-initialization." [dcl.init]
4499 :
4500 : We represent this in the conversion sequence with an
4501 : rvalue conversion, which means a constructor call. But
4502 : don't add a second rvalue conversion if there's already
4503 : one there. Which there really shouldn't be, but it's
4504 : harmless since we'd add it here anyway. */
4505 1778171 : if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
4506 3612000 : && !(convflags & LOOKUP_NO_TEMP_BIND))
4507 60015 : ics = build_conv (ck_rvalue, totype, ics);
4508 :
4509 3419522 : cand->second_conv = ics;
4510 :
4511 3419522 : if (!ics)
4512 : {
4513 1641351 : cand->viable = 0;
4514 3282310 : cand->reason = arg_conversion_rejection (NULL_TREE, -2,
4515 : rettype, totype,
4516 1641351 : EXPR_LOCATION (expr));
4517 : }
4518 8092 : else if (TYPE_REF_P (totype) && !ics->rvaluedness_matches_p
4519 : /* Limit this to non-templates for now (PR90546). */
4520 173 : && !cand->template_decl
4521 1778338 : && TREE_CODE (TREE_TYPE (totype)) != FUNCTION_TYPE)
4522 : {
4523 : /* If we are called to convert to a reference type, we are trying
4524 : to find a direct binding per [over.match.ref], so rvaluedness
4525 : must match for non-functions. */
4526 161 : cand->viable = 0;
4527 : }
4528 1778010 : else if (DECL_NONCONVERTING_P (cand->fn)
4529 1778010 : && ics->rank > cr_exact)
4530 : {
4531 : /* 13.3.1.5: For direct-initialization, those explicit
4532 : conversion functions that are not hidden within S and
4533 : yield type T or a type that can be converted to type T
4534 : with a qualification conversion (4.4) are also candidate
4535 : functions. */
4536 : /* 13.3.1.6 doesn't have a parallel restriction, but it should;
4537 : I've raised this issue with the committee. --jason 9/2011 */
4538 490 : cand->viable = -1;
4539 490 : cand->reason = explicit_conversion_rejection (rettype, totype);
4540 : }
4541 1777520 : else if (cand->viable == 1 && ics->bad_p)
4542 : {
4543 1061 : cand->viable = -1;
4544 1061 : cand->reason
4545 2122 : = bad_arg_conversion_rejection (NULL_TREE, -2,
4546 : rettype, totype,
4547 1061 : EXPR_LOCATION (expr));
4548 : }
4549 1776459 : else if (primary_template_specialization_p (cand->fn)
4550 1776459 : && ics->rank > cr_exact)
4551 : {
4552 : /* 13.3.3.1.2: If the user-defined conversion is specified by
4553 : a specialization of a conversion function template, the
4554 : second standard conversion sequence shall have exact match
4555 : rank. */
4556 13 : cand->viable = -1;
4557 13 : cand->reason = template_conversion_rejection (rettype, totype);
4558 : }
4559 : }
4560 : }
4561 :
4562 25015564 : candidates = splice_viable (candidates, false, &any_viable_p);
4563 25015564 : if (!any_viable_p)
4564 : {
4565 20374085 : if (args)
4566 13265840 : release_tree_vector (args);
4567 20374085 : return NULL;
4568 : }
4569 :
4570 4641479 : cand = tourney (candidates, complain);
4571 4641479 : if (cand == NULL)
4572 : {
4573 1368 : if (complain & tf_error)
4574 : {
4575 80 : auto_diagnostic_group d;
4576 84 : error_at (cp_expr_loc_or_input_loc (expr),
4577 : "conversion from %qH to %qI is ambiguous",
4578 : fromtype, totype);
4579 80 : print_z_candidates (location_of (expr), candidates);
4580 80 : }
4581 :
4582 1368 : cand = candidates; /* any one will do */
4583 1368 : cand->second_conv = build_ambiguous_conv (totype, expr);
4584 1368 : cand->second_conv->user_conv_p = true;
4585 2736 : if (!any_strictly_viable (candidates))
4586 13 : cand->second_conv->bad_p = true;
4587 1368 : if (flags & LOOKUP_ONLYCONVERTING)
4588 1282 : cand->second_conv->need_temporary_p = true;
4589 : /* If there are viable candidates, don't set ICS_BAD_FLAG; an
4590 : ambiguous conversion is no worse than another user-defined
4591 : conversion. */
4592 :
4593 1368 : return cand;
4594 : }
4595 :
4596 : /* Maybe pass { } as iterators instead of an initializer_list. */
4597 4640111 : if (tree iters = maybe_init_list_as_range (cand->fn, expr))
4598 66 : if (z_candidate *cand2
4599 33 : = build_user_type_conversion_1 (totype, iters, flags, tf_none))
4600 33 : if (cand2->viable == 1 && !is_list_ctor (cand2->fn))
4601 : {
4602 : cand = cand2;
4603 : expr = iters;
4604 : }
4605 :
4606 4640111 : tree convtype;
4607 9280222 : if (!DECL_CONSTRUCTOR_P (cand->fn))
4608 1776170 : convtype = non_reference (TREE_TYPE (TREE_TYPE (cand->fn)));
4609 2863941 : else if (cand->second_conv->kind == ck_rvalue)
4610 : /* DR 5: [in the first step of copy-initialization]...if the function
4611 : is a constructor, the call initializes a temporary of the
4612 : cv-unqualified version of the destination type. */
4613 6933 : convtype = cv_unqualified (totype);
4614 : else
4615 : convtype = totype;
4616 : /* Build the user conversion sequence. */
4617 4640111 : conv = build_conv
4618 4640111 : (ck_user,
4619 : convtype,
4620 4640111 : build_identity_conv (TREE_TYPE (expr), expr));
4621 4640111 : conv->cand = cand;
4622 4640111 : if (cand->viable == -1)
4623 23459 : conv->bad_p = true;
4624 :
4625 : /* Remember that this was a list-initialization. */
4626 4640111 : if (flags & LOOKUP_NO_NARROWING)
4627 696122 : conv->check_narrowing = true;
4628 :
4629 : /* Combine it with the second conversion sequence. */
4630 4640111 : cand->second_conv = merge_conversion_sequences (conv,
4631 : cand->second_conv);
4632 :
4633 4640111 : return cand;
4634 : }
4635 :
4636 : /* Wrapper for above. */
4637 :
4638 : tree
4639 9136 : build_user_type_conversion (tree totype, tree expr, int flags,
4640 : tsubst_flags_t complain)
4641 : {
4642 9136 : struct z_candidate *cand;
4643 9136 : tree ret;
4644 :
4645 9136 : auto_cond_timevar tv (TV_OVERLOAD);
4646 9136 : cand = build_user_type_conversion_1 (totype, expr, flags, complain);
4647 :
4648 9136 : if (cand)
4649 : {
4650 9011 : if (cand->second_conv->kind == ck_ambig)
4651 72 : ret = error_mark_node;
4652 : else
4653 : {
4654 8939 : expr = convert_like (cand->second_conv, expr, complain);
4655 8939 : ret = convert_from_reference (expr);
4656 : }
4657 : }
4658 : else
4659 : ret = NULL_TREE;
4660 :
4661 18272 : return ret;
4662 9136 : }
4663 :
4664 : /* Give a helpful diagnostic when implicit_conversion fails. */
4665 :
4666 : static void
4667 569 : implicit_conversion_error (location_t loc, tree type, tree expr)
4668 : {
4669 569 : tsubst_flags_t complain = tf_warning_or_error;
4670 :
4671 : /* If expr has unknown type, then it is an overloaded function.
4672 : Call instantiate_type to get good error messages. */
4673 569 : if (TREE_TYPE (expr) == unknown_type_node)
4674 75 : instantiate_type (type, expr, complain);
4675 494 : else if (invalid_nonstatic_memfn_p (loc, expr, complain))
4676 : /* We gave an error. */;
4677 51 : else if (BRACE_ENCLOSED_INITIALIZER_P (expr)
4678 51 : && CONSTRUCTOR_IS_DESIGNATED_INIT (expr)
4679 497 : && !CP_AGGREGATE_TYPE_P (type))
4680 10 : error_at (loc, "designated initializers cannot be used with a "
4681 : "non-aggregate type %qT", type);
4682 : else
4683 : {
4684 477 : range_label_for_type_mismatch label (TREE_TYPE (expr), type);
4685 477 : gcc_rich_location rich_loc (loc, &label);
4686 477 : error_at (&rich_loc, "could not convert %qE from %qH to %qI",
4687 477 : expr, TREE_TYPE (expr), type);
4688 477 : }
4689 569 : }
4690 :
4691 : /* Worker for build_converted_constant_expr. */
4692 :
4693 : static tree
4694 157473375 : build_converted_constant_expr_internal (tree type, tree expr,
4695 : int flags, tsubst_flags_t complain)
4696 : {
4697 157473375 : conversion *conv;
4698 157473375 : void *p;
4699 157473375 : tree t;
4700 157473375 : location_t loc = cp_expr_loc_or_input_loc (expr);
4701 :
4702 157473375 : if (error_operand_p (expr))
4703 41 : return error_mark_node;
4704 :
4705 : /* Get the high-water mark for the CONVERSION_OBSTACK. */
4706 157473334 : p = conversion_obstack_alloc (0);
4707 :
4708 157473334 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
4709 : /*c_cast_p=*/false, flags, complain);
4710 :
4711 : /* A converted constant expression of type T is an expression, implicitly
4712 : converted to type T, where the converted expression is a constant
4713 : expression and the implicit conversion sequence contains only
4714 :
4715 : * user-defined conversions,
4716 : * lvalue-to-rvalue conversions (7.1),
4717 : * array-to-pointer conversions (7.2),
4718 : * function-to-pointer conversions (7.3),
4719 : * qualification conversions (7.5),
4720 : * integral promotions (7.6),
4721 : * integral conversions (7.8) other than narrowing conversions (11.6.4),
4722 : * null pointer conversions (7.11) from std::nullptr_t,
4723 : * null member pointer conversions (7.12) from std::nullptr_t, and
4724 : * function pointer conversions (7.13),
4725 :
4726 : and where the reference binding (if any) binds directly. */
4727 :
4728 157473334 : for (conversion *c = conv;
4729 176412827 : c && c->kind != ck_identity;
4730 18939493 : c = next_conversion (c))
4731 : {
4732 18939493 : switch (c->kind)
4733 : {
4734 : /* A conversion function is OK. If it isn't constexpr, we'll
4735 : complain later that the argument isn't constant. */
4736 : case ck_user:
4737 : /* List-initialization is OK. */
4738 : case ck_aggr:
4739 : /* The lvalue-to-rvalue conversion is OK. */
4740 : case ck_rvalue:
4741 : /* Array-to-pointer and function-to-pointer. */
4742 : case ck_lvalue:
4743 : /* Function pointer conversions. */
4744 : case ck_fnptr:
4745 : /* Qualification conversions. */
4746 : case ck_qual:
4747 : break;
4748 :
4749 263 : case ck_ref_bind:
4750 263 : if (c->need_temporary_p)
4751 : {
4752 0 : if (complain & tf_error)
4753 0 : error_at (loc, "initializing %qH with %qI in converted "
4754 : "constant expression does not bind directly",
4755 0 : type, next_conversion (c)->type);
4756 : conv = NULL;
4757 : }
4758 : break;
4759 :
4760 4006940 : case ck_base:
4761 4006940 : case ck_pmem:
4762 4006940 : case ck_ptr:
4763 4006940 : case ck_std:
4764 4006940 : t = next_conversion (c)->type;
4765 4006940 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (t)
4766 4006879 : && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4767 : /* Integral promotion or conversion. */
4768 : break;
4769 79 : if (NULLPTR_TYPE_P (t))
4770 : /* Conversion from nullptr to pointer or pointer-to-member. */
4771 : break;
4772 :
4773 79 : if (complain & tf_error)
4774 63 : error_at (loc, "conversion from %qH to %qI in a "
4775 : "converted constant expression", t, type);
4776 : /* fall through. */
4777 :
4778 : default:
4779 : conv = NULL;
4780 : break;
4781 : }
4782 : }
4783 :
4784 : /* Avoid confusing convert_nontype_argument by introducing
4785 : a redundant conversion to the same reference type. */
4786 157473142 : if (conv && conv->kind == ck_ref_bind
4787 157473597 : && REFERENCE_REF_P (expr))
4788 : {
4789 87 : tree ref = TREE_OPERAND (expr, 0);
4790 87 : if (same_type_p (type, TREE_TYPE (ref)))
4791 : return ref;
4792 : }
4793 :
4794 157473255 : if (conv)
4795 : {
4796 : /* Don't copy a class in a template. */
4797 733 : if (CLASS_TYPE_P (type) && conv->kind == ck_rvalue
4798 157473636 : && processing_template_decl)
4799 19 : conv = next_conversion (conv);
4800 :
4801 : /* Issuing conversion warnings for value-dependent expressions is
4802 : likely too noisy. */
4803 157473063 : warning_sentinel w (warn_conversion);
4804 157473063 : conv->check_narrowing = true;
4805 157473063 : conv->check_narrowing_const_only = true;
4806 157473063 : expr = convert_like (conv, expr, complain);
4807 157473063 : }
4808 : else
4809 : {
4810 192 : if (complain & tf_error)
4811 144 : implicit_conversion_error (loc, type, expr);
4812 192 : expr = error_mark_node;
4813 : }
4814 :
4815 : /* Free all the conversions we allocated. */
4816 157473255 : obstack_free (&conversion_obstack, p);
4817 :
4818 : return expr;
4819 : }
4820 :
4821 : /* Subroutine of convert_nontype_argument.
4822 :
4823 : EXPR is an expression used in a context that requires a converted
4824 : constant-expression, such as a template non-type parameter. Do any
4825 : necessary conversions (that are permitted for converted
4826 : constant-expressions) to convert it to the desired type.
4827 :
4828 : This function doesn't consider explicit conversion functions. If
4829 : you mean to use "a contextually converted constant expression of type
4830 : bool", use build_converted_constant_bool_expr.
4831 :
4832 : If conversion is successful, returns the converted expression;
4833 : otherwise, returns error_mark_node. */
4834 :
4835 : tree
4836 86471771 : build_converted_constant_expr (tree type, tree expr, tsubst_flags_t complain)
4837 : {
4838 86471771 : return build_converted_constant_expr_internal (type, expr, LOOKUP_IMPLICIT,
4839 86471771 : complain);
4840 : }
4841 :
4842 : /* Used to create "a contextually converted constant expression of type
4843 : bool". This differs from build_converted_constant_expr in that it
4844 : also considers explicit conversion functions. */
4845 :
4846 : tree
4847 71001604 : build_converted_constant_bool_expr (tree expr, tsubst_flags_t complain)
4848 : {
4849 71001604 : return build_converted_constant_expr_internal (boolean_type_node, expr,
4850 71001604 : LOOKUP_NORMAL, complain);
4851 : }
4852 :
4853 : /* Do any initial processing on the arguments to a function call. */
4854 :
4855 : vec<tree, va_gc> *
4856 81284550 : resolve_args (vec<tree, va_gc> *args, tsubst_flags_t complain)
4857 : {
4858 81284550 : unsigned int ix;
4859 81284550 : tree arg;
4860 :
4861 151019779 : FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
4862 : {
4863 69737299 : if (error_operand_p (arg))
4864 : return NULL;
4865 69735334 : else if (VOID_TYPE_P (TREE_TYPE (arg)))
4866 : {
4867 71 : if (complain & tf_error)
4868 16 : error_at (cp_expr_loc_or_input_loc (arg),
4869 : "invalid use of void expression");
4870 71 : return NULL;
4871 : }
4872 69735263 : else if (invalid_nonstatic_memfn_p (EXPR_LOCATION (arg), arg, complain))
4873 : return NULL;
4874 :
4875 : /* Force auto deduction now. Omit tf_warning to avoid redundant
4876 : deprecated warning on deprecated-14.C. */
4877 69735229 : if (!mark_single_function (arg, complain & ~tf_warning))
4878 : return NULL;
4879 : }
4880 : return args;
4881 : }
4882 :
4883 : /* Perform overload resolution on FN, which is called with the ARGS.
4884 :
4885 : Return the candidate function selected by overload resolution, or
4886 : NULL if the event that overload resolution failed. In the case
4887 : that overload resolution fails, *CANDIDATES will be the set of
4888 : candidates considered, and ANY_VIABLE_P will be set to true or
4889 : false to indicate whether or not any of the candidates were
4890 : viable.
4891 :
4892 : The ARGS should already have gone through RESOLVE_ARGS before this
4893 : function is called. */
4894 :
4895 : static struct z_candidate *
4896 35294330 : perform_overload_resolution (tree fn,
4897 : const vec<tree, va_gc> *args,
4898 : struct z_candidate **candidates,
4899 : bool *any_viable_p, tsubst_flags_t complain)
4900 : {
4901 35294330 : struct z_candidate *cand;
4902 35294330 : tree explicit_targs;
4903 35294330 : int template_only;
4904 :
4905 35294330 : auto_cond_timevar tv (TV_OVERLOAD);
4906 :
4907 35294330 : explicit_targs = NULL_TREE;
4908 35294330 : template_only = 0;
4909 :
4910 35294330 : *candidates = NULL;
4911 35294330 : *any_viable_p = true;
4912 :
4913 : /* Check FN. */
4914 35294330 : gcc_assert (OVL_P (fn) || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
4915 :
4916 35294330 : if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4917 : {
4918 5888110 : explicit_targs = TREE_OPERAND (fn, 1);
4919 5888110 : fn = TREE_OPERAND (fn, 0);
4920 5888110 : template_only = 1;
4921 : }
4922 :
4923 : /* Add the various candidate functions. */
4924 35294330 : add_candidates (fn, NULL_TREE, args, NULL_TREE,
4925 : explicit_targs, template_only,
4926 : /*conversion_path=*/NULL_TREE,
4927 : /*access_path=*/NULL_TREE,
4928 : LOOKUP_NORMAL,
4929 : candidates, complain);
4930 :
4931 35282613 : *candidates = splice_viable (*candidates, false, any_viable_p);
4932 35282613 : if (*any_viable_p)
4933 35273834 : cand = tourney (*candidates, complain);
4934 : else
4935 : cand = NULL;
4936 :
4937 70565226 : return cand;
4938 35282613 : }
4939 :
4940 : /* Print an error message about being unable to build a call to FN with
4941 : ARGS. ANY_VIABLE_P indicates whether any candidate functions could
4942 : be located; CANDIDATES is a possibly empty list of such
4943 : functions. */
4944 :
4945 : static void
4946 2480 : print_error_for_call_failure (tree fn, const vec<tree, va_gc> *args,
4947 : struct z_candidate *candidates)
4948 : {
4949 2480 : tree targs = NULL_TREE;
4950 2480 : if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4951 : {
4952 357 : targs = TREE_OPERAND (fn, 1);
4953 357 : fn = TREE_OPERAND (fn, 0);
4954 : }
4955 4960 : tree name = OVL_NAME (fn);
4956 2480 : location_t loc = location_of (name);
4957 2480 : if (targs)
4958 357 : name = lookup_template_function (name, targs);
4959 :
4960 2480 : auto_diagnostic_group d;
4961 4960 : if (!any_strictly_viable (candidates))
4962 2042 : error_at (loc, "no matching function for call to %<%D(%A)%>",
4963 : name, build_tree_list_vec (args));
4964 : else
4965 438 : error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous",
4966 : name, build_tree_list_vec (args));
4967 2480 : if (candidates)
4968 2471 : print_z_candidates (loc, candidates);
4969 2480 : }
4970 :
4971 : /* Perform overload resolution on the set of deduction guides DGUIDES
4972 : using ARGS. Returns the selected deduction guide, or error_mark_node
4973 : if overload resolution fails. */
4974 :
4975 : tree
4976 3777 : perform_dguide_overload_resolution (tree dguides, const vec<tree, va_gc> *args,
4977 : tsubst_flags_t complain)
4978 : {
4979 3777 : z_candidate *candidates;
4980 3777 : bool any_viable_p;
4981 3777 : tree result;
4982 :
4983 7554 : gcc_assert (deduction_guide_p (OVL_FIRST (dguides)));
4984 :
4985 : /* Get the high-water mark for the CONVERSION_OBSTACK. */
4986 3777 : void *p = conversion_obstack_alloc (0);
4987 :
4988 3777 : z_candidate *cand = perform_overload_resolution (dguides, args, &candidates,
4989 : &any_viable_p, complain);
4990 3777 : if (!cand)
4991 : {
4992 566 : if (complain & tf_error)
4993 104 : print_error_for_call_failure (dguides, args, candidates);
4994 566 : result = error_mark_node;
4995 : }
4996 : else
4997 3211 : result = cand->fn;
4998 :
4999 : /* Free all the conversions we allocated. */
5000 3777 : obstack_free (&conversion_obstack, p);
5001 :
5002 3777 : return result;
5003 : }
5004 :
5005 : /* Return an expression for a call to FN (a namespace-scope function,
5006 : or a static member function) with the ARGS. This may change
5007 : ARGS. */
5008 :
5009 : tree
5010 35078839 : build_new_function_call (tree fn, vec<tree, va_gc> **args,
5011 : tsubst_flags_t complain)
5012 : {
5013 35078839 : struct z_candidate *candidates, *cand;
5014 35078839 : bool any_viable_p;
5015 35078839 : void *p;
5016 35078839 : tree result;
5017 :
5018 35078839 : if (args != NULL && *args != NULL)
5019 : {
5020 35078839 : *args = resolve_args (*args, complain);
5021 35078839 : if (*args == NULL)
5022 338 : return error_mark_node;
5023 : }
5024 :
5025 35078501 : if (flag_tm)
5026 2338 : tm_malloc_replacement (fn);
5027 :
5028 : /* Get the high-water mark for the CONVERSION_OBSTACK. */
5029 35078501 : p = conversion_obstack_alloc (0);
5030 :
5031 35078501 : cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
5032 : complain);
5033 :
5034 35066784 : if (!cand)
5035 : {
5036 13081 : if (complain & tf_error)
5037 : {
5038 : // If there is a single (non-viable) function candidate,
5039 : // let the error be diagnosed by cp_build_function_call_vec.
5040 2837 : if (!any_viable_p && candidates && ! candidates->next
5041 1163 : && (TREE_CODE (candidates->fn) == FUNCTION_DECL))
5042 473 : return cp_build_function_call_vec (candidates->fn, args, complain);
5043 :
5044 : // Otherwise, emit notes for non-viable candidates.
5045 2364 : print_error_for_call_failure (fn, *args, candidates);
5046 : }
5047 12608 : result = error_mark_node;
5048 : }
5049 : else
5050 : {
5051 35053703 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
5052 : }
5053 :
5054 35066311 : if (flag_coroutines
5055 3474300 : && result
5056 3474300 : && TREE_CODE (result) == CALL_EXPR
5057 37129560 : && DECL_BUILT_IN_CLASS (TREE_OPERAND (CALL_EXPR_FN (result), 0))
5058 2063249 : == BUILT_IN_NORMAL)
5059 334255 : result = coro_validate_builtin_call (result);
5060 :
5061 : /* Free all the conversions we allocated. */
5062 35066311 : obstack_free (&conversion_obstack, p);
5063 :
5064 : return result;
5065 : }
5066 :
5067 : /* Build a call to a global operator new. FNNAME is the name of the
5068 : operator (either "operator new" or "operator new[]") and ARGS are
5069 : the arguments provided. This may change ARGS. *SIZE points to the
5070 : total number of bytes required by the allocation, and is updated if
5071 : that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
5072 : be used. If this function determines that no cookie should be
5073 : used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK
5074 : is not NULL_TREE, it is evaluated before calculating the final
5075 : array size, and if it fails, the array size is replaced with
5076 : (size_t)-1 (usually triggering a std::bad_alloc exception). If FN
5077 : is non-NULL, it will be set, upon return, to the allocation
5078 : function called. */
5079 :
5080 : tree
5081 212051 : build_operator_new_call (tree fnname, vec<tree, va_gc> **args,
5082 : tree *size, tree *cookie_size,
5083 : tree align_arg, tree size_check,
5084 : tree *fn, tsubst_flags_t complain)
5085 : {
5086 212051 : tree original_size = *size;
5087 212051 : tree fns;
5088 212051 : struct z_candidate *candidates;
5089 212051 : struct z_candidate *cand = NULL;
5090 212051 : bool any_viable_p;
5091 :
5092 212051 : if (fn)
5093 210955 : *fn = NULL_TREE;
5094 : /* Set to (size_t)-1 if the size check fails. */
5095 212051 : if (size_check != NULL_TREE)
5096 : {
5097 8522 : tree errval = TYPE_MAX_VALUE (sizetype);
5098 8522 : if (cxx_dialect >= cxx11 && flag_exceptions)
5099 8241 : errval = throw_bad_array_new_length ();
5100 8522 : *size = fold_build3 (COND_EXPR, sizetype, size_check,
5101 : original_size, errval);
5102 : }
5103 212051 : vec_safe_insert (*args, 0, *size);
5104 212051 : *args = resolve_args (*args, complain);
5105 212051 : if (*args == NULL)
5106 1 : return error_mark_node;
5107 :
5108 : /* Based on:
5109 :
5110 : [expr.new]
5111 :
5112 : If this lookup fails to find the name, or if the allocated type
5113 : is not a class type, the allocation function's name is looked
5114 : up in the global scope.
5115 :
5116 : we disregard block-scope declarations of "operator new". */
5117 212050 : fns = lookup_qualified_name (global_namespace, fnname);
5118 :
5119 212050 : if (align_arg)
5120 : {
5121 25 : vec<tree, va_gc>* align_args
5122 25 : = vec_copy_and_insert (*args, align_arg, 1);
5123 25 : cand = perform_overload_resolution (fns, align_args, &candidates,
5124 : &any_viable_p, tf_none);
5125 25 : if (cand)
5126 23 : *args = align_args;
5127 : /* If no aligned allocation function matches, try again without the
5128 : alignment. */
5129 : }
5130 :
5131 : /* Figure out what function is being called. */
5132 23 : if (!cand)
5133 212027 : cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p,
5134 : complain);
5135 :
5136 : /* If no suitable function could be found, issue an error message
5137 : and give up. */
5138 212050 : if (!cand)
5139 : {
5140 12 : if (complain & tf_error)
5141 12 : print_error_for_call_failure (fns, *args, candidates);
5142 12 : return error_mark_node;
5143 : }
5144 :
5145 : /* If a cookie is required, add some extra space. Whether
5146 : or not a cookie is required cannot be determined until
5147 : after we know which function was called. */
5148 212038 : if (*cookie_size)
5149 : {
5150 282 : bool use_cookie = true;
5151 282 : tree arg_types;
5152 :
5153 282 : arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
5154 : /* Skip the size_t parameter. */
5155 282 : arg_types = TREE_CHAIN (arg_types);
5156 : /* Check the remaining parameters (if any). */
5157 282 : if (arg_types
5158 282 : && TREE_CHAIN (arg_types) == void_list_node
5159 348 : && same_type_p (TREE_VALUE (arg_types),
5160 : ptr_type_node))
5161 48 : use_cookie = false;
5162 : /* If we need a cookie, adjust the number of bytes allocated. */
5163 282 : if (use_cookie)
5164 : {
5165 : /* Update the total size. */
5166 234 : *size = size_binop (PLUS_EXPR, original_size, *cookie_size);
5167 234 : if (size_check)
5168 : {
5169 : /* Set to (size_t)-1 if the size check fails. */
5170 40 : gcc_assert (size_check != NULL_TREE);
5171 40 : *size = fold_build3 (COND_EXPR, sizetype, size_check,
5172 : *size, TYPE_MAX_VALUE (sizetype));
5173 : }
5174 : /* Update the argument list to reflect the adjusted size. */
5175 234 : (**args)[0] = *size;
5176 : }
5177 : else
5178 48 : *cookie_size = NULL_TREE;
5179 : }
5180 :
5181 : /* Tell our caller which function we decided to call. */
5182 212038 : if (fn)
5183 210943 : *fn = cand->fn;
5184 :
5185 : /* Build the CALL_EXPR. */
5186 212038 : tree ret = build_over_call (cand, LOOKUP_NORMAL, complain);
5187 :
5188 : /* Set this flag for all callers of this function. In addition to
5189 : new-expressions, this is called for allocating coroutine state; treat
5190 : that as an implicit new-expression. */
5191 212038 : tree call = extract_call_expr (ret);
5192 212038 : if (TREE_CODE (call) == CALL_EXPR)
5193 212038 : CALL_FROM_NEW_OR_DELETE_P (call) = 1;
5194 :
5195 : return ret;
5196 : }
5197 :
5198 : /* Evaluate side-effects from OBJ before evaluating call
5199 : to FN in RESULT expression.
5200 : This is for expressions of the form `obj->fn(...)'
5201 : where `fn' turns out to be a static member function and
5202 : `obj' needs to be evaluated. `fn' could be also static operator[]
5203 : or static operator(), in which cases the source expression
5204 : would be `obj[...]' or `obj(...)'. */
5205 :
5206 : tree
5207 35120708 : keep_unused_object_arg (tree result, tree obj, tree fn)
5208 : {
5209 35120708 : if (result == NULL_TREE
5210 35120708 : || result == error_mark_node
5211 34704340 : || TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
5212 35994794 : || !TREE_SIDE_EFFECTS (obj))
5213 : return result;
5214 :
5215 : /* But avoid the implicit lvalue-rvalue conversion when `obj' is
5216 : volatile. */
5217 236 : tree a = obj;
5218 236 : if (TREE_THIS_VOLATILE (a))
5219 31 : a = build_this (a);
5220 236 : if (TREE_SIDE_EFFECTS (a))
5221 214 : return cp_build_compound_expr (a, result, tf_error);
5222 : return result;
5223 : }
5224 :
5225 : /* Build a new call to operator(). This may change ARGS. */
5226 :
5227 : tree
5228 413397 : build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
5229 : {
5230 413397 : struct z_candidate *candidates = 0, *cand;
5231 413397 : tree fns, convs, first_mem_arg = NULL_TREE;
5232 413397 : bool any_viable_p;
5233 413397 : tree result = NULL_TREE;
5234 413397 : void *p;
5235 :
5236 413397 : auto_cond_timevar tv (TV_OVERLOAD);
5237 :
5238 413397 : obj = mark_lvalue_use (obj);
5239 :
5240 413397 : if (error_operand_p (obj))
5241 0 : return error_mark_node;
5242 :
5243 413397 : tree type = TREE_TYPE (obj);
5244 :
5245 413397 : obj = prep_operand (obj);
5246 :
5247 413397 : if (TYPE_PTRMEMFUNC_P (type))
5248 : {
5249 0 : if (complain & tf_error)
5250 : /* It's no good looking for an overloaded operator() on a
5251 : pointer-to-member-function. */
5252 0 : error ("pointer-to-member function %qE cannot be called without "
5253 : "an object; consider using %<.*%> or %<->*%>", obj);
5254 0 : return error_mark_node;
5255 : }
5256 :
5257 413397 : if (TYPE_BINFO (type))
5258 : {
5259 413378 : fns = lookup_fnfields (TYPE_BINFO (type), call_op_identifier, 1, complain);
5260 413378 : if (fns == error_mark_node)
5261 : return error_mark_node;
5262 : }
5263 : else
5264 : fns = NULL_TREE;
5265 :
5266 413386 : if (args != NULL && *args != NULL)
5267 : {
5268 413386 : *args = resolve_args (*args, complain);
5269 413386 : if (*args == NULL)
5270 1606 : return error_mark_node;
5271 : }
5272 :
5273 : /* Get the high-water mark for the CONVERSION_OBSTACK. */
5274 411780 : p = conversion_obstack_alloc (0);
5275 :
5276 411780 : if (fns)
5277 : {
5278 410417 : first_mem_arg = obj;
5279 :
5280 410417 : add_candidates (BASELINK_FUNCTIONS (fns),
5281 : first_mem_arg, *args, NULL_TREE,
5282 : NULL_TREE, false,
5283 410417 : BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
5284 : LOOKUP_NORMAL, &candidates, complain);
5285 : }
5286 :
5287 411780 : bool any_call_ops = candidates != nullptr;
5288 :
5289 411780 : convs = lookup_conversions (type);
5290 :
5291 427229 : for (; convs; convs = TREE_CHAIN (convs))
5292 : {
5293 15449 : tree totype = TREE_TYPE (convs);
5294 :
5295 6938 : if (TYPE_PTRFN_P (totype)
5296 8515 : || TYPE_REFFN_P (totype)
5297 23935 : || (TYPE_REF_P (totype)
5298 329 : && TYPE_PTRFN_P (TREE_TYPE (totype))))
5299 20931 : for (tree fn : ovl_range (TREE_VALUE (convs)))
5300 : {
5301 6977 : if (DECL_NONCONVERTING_P (fn))
5302 3 : continue;
5303 :
5304 6974 : if (TREE_CODE (fn) == TEMPLATE_DECL)
5305 : {
5306 : /* Making this work broke PR 71117 and 85118, so until the
5307 : committee resolves core issue 2189, let's disable this
5308 : candidate if there are any call operators. */
5309 4511 : if (any_call_ops)
5310 4498 : continue;
5311 :
5312 13 : add_template_conv_candidate
5313 13 : (&candidates, fn, obj, *args, totype,
5314 : /*access_path=*/NULL_TREE,
5315 : /*conversion_path=*/NULL_TREE, complain);
5316 : }
5317 : else
5318 2463 : add_conv_candidate (&candidates, fn, obj,
5319 : *args, /*conversion_path=*/NULL_TREE,
5320 : /*access_path=*/NULL_TREE, complain);
5321 : }
5322 : }
5323 :
5324 : /* Be strict here because if we choose a bad conversion candidate, the
5325 : errors we get won't mention the call context. */
5326 411780 : candidates = splice_viable (candidates, true, &any_viable_p);
5327 411780 : if (!any_viable_p)
5328 : {
5329 3255 : if (complain & tf_error)
5330 : {
5331 161 : auto_diagnostic_group d;
5332 161 : error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
5333 : build_tree_list_vec (*args));
5334 161 : print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
5335 161 : }
5336 3255 : result = error_mark_node;
5337 : }
5338 : else
5339 : {
5340 408525 : cand = tourney (candidates, complain);
5341 408525 : if (cand == 0)
5342 : {
5343 16 : if (complain & tf_error)
5344 : {
5345 8 : auto_diagnostic_group d;
5346 16 : error ("call of %<(%T) (%A)%> is ambiguous",
5347 8 : TREE_TYPE (obj), build_tree_list_vec (*args));
5348 8 : print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
5349 8 : }
5350 16 : result = error_mark_node;
5351 : }
5352 408509 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL
5353 408449 : && DECL_OVERLOADED_OPERATOR_P (cand->fn)
5354 816958 : && DECL_OVERLOADED_OPERATOR_IS (cand->fn, CALL_EXPR))
5355 : {
5356 408449 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
5357 : /* In an expression of the form `a()' where cand->fn
5358 : which is operator() turns out to be a static member function,
5359 : `a' is none-the-less evaluated. */
5360 408449 : result = keep_unused_object_arg (result, obj, cand->fn);
5361 : }
5362 : else
5363 : {
5364 60 : if (TREE_CODE (cand->fn) == FUNCTION_DECL)
5365 0 : obj = convert_like_with_context (cand->convs[0], obj, cand->fn,
5366 : -1, complain);
5367 : else
5368 : {
5369 60 : gcc_checking_assert (TYPE_P (cand->fn));
5370 60 : obj = convert_like (cand->convs[0], obj, complain);
5371 : }
5372 60 : obj = convert_from_reference (obj);
5373 60 : result = cp_build_function_call_vec (obj, args, complain);
5374 : }
5375 : }
5376 :
5377 : /* Free all the conversions we allocated. */
5378 411780 : obstack_free (&conversion_obstack, p);
5379 :
5380 : return result;
5381 413397 : }
5382 :
5383 : /* Called by op_error to prepare format strings suitable for the error
5384 : function. It concatenates a prefix (controlled by MATCH), ERRMSG,
5385 : and a suffix (controlled by NTYPES). */
5386 :
5387 : static const char *
5388 1211 : op_error_string (const char *errmsg, int ntypes, bool match)
5389 : {
5390 1211 : const char *msg;
5391 :
5392 2307 : const char *msgp = concat (match ? G_("ambiguous overload for ")
5393 : : G_("no match for "), errmsg, NULL);
5394 :
5395 1211 : if (ntypes == 3)
5396 0 : msg = concat (msgp, G_(" (operand types are %qT, %qT, and %qT)"), NULL);
5397 1211 : else if (ntypes == 2)
5398 1092 : msg = concat (msgp, G_(" (operand types are %qT and %qT)"), NULL);
5399 : else
5400 119 : msg = concat (msgp, G_(" (operand type is %qT)"), NULL);
5401 :
5402 1211 : return msg;
5403 : }
5404 :
5405 : static void
5406 1211 : op_error (const op_location_t &loc,
5407 : enum tree_code code, enum tree_code code2,
5408 : tree arg1, tree arg2, tree arg3, bool match)
5409 : {
5410 1211 : bool assop = code == MODIFY_EXPR;
5411 1211 : const char *opname = OVL_OP_INFO (assop, assop ? code2 : code)->name;
5412 :
5413 1211 : switch (code)
5414 : {
5415 0 : case COND_EXPR:
5416 0 : if (flag_diagnostics_show_caret)
5417 0 : error_at (loc, op_error_string (G_("ternary %<operator?:%>"),
5418 : 3, match),
5419 0 : TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
5420 : else
5421 0 : error_at (loc, op_error_string (G_("ternary %<operator?:%> "
5422 : "in %<%E ? %E : %E%>"), 3, match),
5423 : arg1, arg2, arg3,
5424 0 : TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
5425 : break;
5426 :
5427 0 : case POSTINCREMENT_EXPR:
5428 0 : case POSTDECREMENT_EXPR:
5429 0 : if (flag_diagnostics_show_caret)
5430 0 : error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
5431 0 : opname, TREE_TYPE (arg1));
5432 : else
5433 0 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
5434 : 1, match),
5435 0 : opname, arg1, opname, TREE_TYPE (arg1));
5436 : break;
5437 :
5438 17 : case ARRAY_REF:
5439 17 : if (flag_diagnostics_show_caret)
5440 0 : error_at (loc, op_error_string (G_("%<operator[]%>"), 2, match),
5441 0 : TREE_TYPE (arg1), TREE_TYPE (arg2));
5442 : else
5443 17 : error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
5444 : 2, match),
5445 17 : arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2));
5446 : break;
5447 :
5448 8 : case REALPART_EXPR:
5449 8 : case IMAGPART_EXPR:
5450 8 : if (flag_diagnostics_show_caret)
5451 0 : error_at (loc, op_error_string (G_("%qs"), 1, match),
5452 0 : opname, TREE_TYPE (arg1));
5453 : else
5454 8 : error_at (loc, op_error_string (G_("%qs in %<%s %E%>"), 1, match),
5455 8 : opname, opname, arg1, TREE_TYPE (arg1));
5456 : break;
5457 :
5458 0 : case CO_AWAIT_EXPR:
5459 0 : if (flag_diagnostics_show_caret)
5460 0 : error_at (loc, op_error_string (G_("%<operator %s%>"), 1, match),
5461 0 : opname, TREE_TYPE (arg1));
5462 : else
5463 0 : error_at (loc, op_error_string (G_("%<operator %s%> in %<%s%E%>"),
5464 : 1, match),
5465 0 : opname, opname, arg1, TREE_TYPE (arg1));
5466 : break;
5467 :
5468 1186 : default:
5469 1186 : if (arg2)
5470 1075 : if (flag_diagnostics_show_caret)
5471 : {
5472 20 : binary_op_rich_location richloc (loc, arg1, arg2, true);
5473 20 : error_at (&richloc,
5474 : op_error_string (G_("%<operator%s%>"), 2, match),
5475 20 : opname, TREE_TYPE (arg1), TREE_TYPE (arg2));
5476 20 : }
5477 : else
5478 1055 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
5479 : 2, match),
5480 : opname, arg1, opname, arg2,
5481 1055 : TREE_TYPE (arg1), TREE_TYPE (arg2));
5482 : else
5483 111 : if (flag_diagnostics_show_caret)
5484 0 : error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
5485 0 : opname, TREE_TYPE (arg1));
5486 : else
5487 111 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
5488 : 1, match),
5489 111 : opname, opname, arg1, TREE_TYPE (arg1));
5490 : break;
5491 : }
5492 1211 : }
5493 :
5494 : /* Return the implicit conversion sequence that could be used to
5495 : convert E1 to E2 in [expr.cond]. */
5496 :
5497 : static conversion *
5498 105694 : conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
5499 : {
5500 105694 : tree t1 = non_reference (TREE_TYPE (e1));
5501 105694 : tree t2 = non_reference (TREE_TYPE (e2));
5502 105694 : conversion *conv;
5503 105694 : bool good_base;
5504 :
5505 : /* [expr.cond]
5506 :
5507 : If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
5508 : implicitly converted (clause _conv_) to the type "lvalue reference to
5509 : T2", subject to the constraint that in the conversion the
5510 : reference must bind directly (_dcl.init.ref_) to an lvalue.
5511 :
5512 : If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
5513 : implicitly converted to the type "rvalue reference to T2", subject to
5514 : the constraint that the reference must bind directly. */
5515 105694 : if (glvalue_p (e2))
5516 : {
5517 84605 : tree rtype = cp_build_reference_type (t2, !lvalue_p (e2));
5518 84605 : conv = implicit_conversion (rtype,
5519 : t1,
5520 : e1,
5521 : /*c_cast_p=*/false,
5522 : LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
5523 : |LOOKUP_ONLYCONVERTING,
5524 : complain);
5525 84605 : if (conv && !conv->bad_p)
5526 : return conv;
5527 : }
5528 :
5529 : /* If E2 is a prvalue or if neither of the conversions above can be done
5530 : and at least one of the operands has (possibly cv-qualified) class
5531 : type: */
5532 96283 : if (!CLASS_TYPE_P (t1) && !CLASS_TYPE_P (t2))
5533 : return NULL;
5534 :
5535 : /* [expr.cond]
5536 :
5537 : If E1 and E2 have class type, and the underlying class types are
5538 : the same or one is a base class of the other: E1 can be converted
5539 : to match E2 if the class of T2 is the same type as, or a base
5540 : class of, the class of T1, and the cv-qualification of T2 is the
5541 : same cv-qualification as, or a greater cv-qualification than, the
5542 : cv-qualification of T1. If the conversion is applied, E1 is
5543 : changed to an rvalue of type T2 that still refers to the original
5544 : source class object (or the appropriate subobject thereof). */
5545 66651 : if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
5546 133334 : && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
5547 : {
5548 41721 : if (good_base && at_least_as_qualified_p (t2, t1))
5549 : {
5550 20821 : conv = build_identity_conv (t1, e1);
5551 20821 : if (!same_type_p (TYPE_MAIN_VARIANT (t1),
5552 : TYPE_MAIN_VARIANT (t2)))
5553 8 : conv = build_conv (ck_base, t2, conv);
5554 : else
5555 20813 : conv = build_conv (ck_rvalue, t2, conv);
5556 20821 : return conv;
5557 : }
5558 : else
5559 20900 : return NULL;
5560 : }
5561 : else
5562 : /* [expr.cond]
5563 :
5564 : Otherwise: E1 can be converted to match E2 if E1 can be implicitly
5565 : converted to the type that expression E2 would have if E2 were
5566 : converted to an rvalue (or the type it has, if E2 is an rvalue). */
5567 45269 : return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
5568 45269 : LOOKUP_IMPLICIT, complain);
5569 : }
5570 :
5571 : /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
5572 : arguments to the conditional expression. */
5573 :
5574 : tree
5575 2279235 : build_conditional_expr (const op_location_t &loc,
5576 : tree arg1, tree arg2, tree arg3,
5577 : tsubst_flags_t complain)
5578 : {
5579 2279235 : tree arg2_type;
5580 2279235 : tree arg3_type;
5581 2279235 : tree result = NULL_TREE;
5582 2279235 : tree result_type = NULL_TREE;
5583 2279235 : tree semantic_result_type = NULL_TREE;
5584 2279235 : bool is_glvalue = true;
5585 2279235 : struct z_candidate *candidates = 0;
5586 2279235 : struct z_candidate *cand;
5587 2279235 : void *p;
5588 2279235 : tree orig_arg2, orig_arg3;
5589 :
5590 2279235 : auto_cond_timevar tv (TV_OVERLOAD);
5591 :
5592 : /* As a G++ extension, the second argument to the conditional can be
5593 : omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
5594 : c'.) If the second operand is omitted, make sure it is
5595 : calculated only once. */
5596 2279235 : if (!arg2)
5597 : {
5598 429 : if (complain & tf_error)
5599 409 : pedwarn (loc, OPT_Wpedantic,
5600 : "ISO C++ forbids omitting the middle term of "
5601 : "a %<?:%> expression");
5602 :
5603 429 : if ((complain & tf_warning) && !truth_value_p (TREE_CODE (arg1)))
5604 361 : warn_for_omitted_condop (loc, arg1);
5605 :
5606 : /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
5607 429 : if (glvalue_p (arg1))
5608 : {
5609 107 : arg1 = cp_stabilize_reference (arg1);
5610 107 : arg2 = arg1 = prevent_lifetime_extension (arg1);
5611 : }
5612 322 : else if (TREE_CODE (arg1) == TARGET_EXPR)
5613 : /* arg1 can't be a prvalue result of the conditional
5614 : expression, since it needs to be materialized for the
5615 : conversion to bool, so treat it as an xvalue in arg2. */
5616 8 : arg2 = move (TARGET_EXPR_SLOT (arg1));
5617 314 : else if (TREE_CODE (arg1) == EXCESS_PRECISION_EXPR)
5618 4 : arg2 = arg1 = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (arg1),
5619 4 : cp_save_expr (TREE_OPERAND (arg1, 0)));
5620 : else
5621 310 : arg2 = arg1 = cp_save_expr (arg1);
5622 : }
5623 :
5624 : /* If something has already gone wrong, just pass that fact up the
5625 : tree. */
5626 2279235 : if (error_operand_p (arg1)
5627 2279171 : || error_operand_p (arg2)
5628 4558365 : || error_operand_p (arg3))
5629 132 : return error_mark_node;
5630 :
5631 2279103 : orig_arg2 = arg2;
5632 2279103 : orig_arg3 = arg3;
5633 :
5634 2279103 : if (gnu_vector_type_p (TREE_TYPE (arg1))
5635 2279103 : && VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1)))
5636 : {
5637 853 : tree arg1_type = TREE_TYPE (arg1);
5638 :
5639 : /* If arg1 is another cond_expr choosing between -1 and 0,
5640 : then we can use its comparison. It may help to avoid
5641 : additional comparison, produce more accurate diagnostics
5642 : and enables folding. */
5643 853 : if (TREE_CODE (arg1) == VEC_COND_EXPR
5644 761 : && integer_minus_onep (TREE_OPERAND (arg1, 1))
5645 1614 : && integer_zerop (TREE_OPERAND (arg1, 2)))
5646 761 : arg1 = TREE_OPERAND (arg1, 0);
5647 :
5648 853 : arg1 = force_rvalue (arg1, complain);
5649 853 : arg2 = force_rvalue (arg2, complain);
5650 853 : arg3 = force_rvalue (arg3, complain);
5651 :
5652 : /* force_rvalue can return error_mark on valid arguments. */
5653 853 : if (error_operand_p (arg1)
5654 853 : || error_operand_p (arg2)
5655 1706 : || error_operand_p (arg3))
5656 0 : return error_mark_node;
5657 :
5658 853 : arg2_type = TREE_TYPE (arg2);
5659 853 : arg3_type = TREE_TYPE (arg3);
5660 :
5661 853 : if (!VECTOR_TYPE_P (arg2_type)
5662 78 : && !VECTOR_TYPE_P (arg3_type))
5663 : {
5664 : /* Rely on the error messages of the scalar version. */
5665 68 : tree scal = build_conditional_expr (loc, integer_one_node,
5666 : orig_arg2, orig_arg3, complain);
5667 68 : if (scal == error_mark_node)
5668 : return error_mark_node;
5669 68 : tree stype = TREE_TYPE (scal);
5670 68 : tree ctype = TREE_TYPE (arg1_type);
5671 68 : if (TYPE_SIZE (stype) != TYPE_SIZE (ctype)
5672 68 : || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype)))
5673 : {
5674 9 : if (complain & tf_error)
5675 9 : error_at (loc, "inferred scalar type %qT is not an integer or "
5676 : "floating-point type of the same size as %qT", stype,
5677 9 : COMPARISON_CLASS_P (arg1)
5678 9 : ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0)))
5679 : : ctype);
5680 9 : return error_mark_node;
5681 : }
5682 :
5683 59 : tree vtype = build_opaque_vector_type (stype,
5684 59 : TYPE_VECTOR_SUBPARTS (arg1_type));
5685 : /* We could pass complain & tf_warning to unsafe_conversion_p,
5686 : but the warnings (like Wsign-conversion) have already been
5687 : given by the scalar build_conditional_expr_1. We still check
5688 : unsafe_conversion_p to forbid truncating long long -> float. */
5689 59 : if (unsafe_conversion_p (stype, arg2, NULL_TREE, false))
5690 : {
5691 0 : if (complain & tf_error)
5692 0 : error_at (loc, "conversion of scalar %qH to vector %qI "
5693 : "involves truncation", arg2_type, vtype);
5694 0 : return error_mark_node;
5695 : }
5696 59 : if (unsafe_conversion_p (stype, arg3, NULL_TREE, false))
5697 : {
5698 3 : if (complain & tf_error)
5699 3 : error_at (loc, "conversion of scalar %qH to vector %qI "
5700 : "involves truncation", arg3_type, vtype);
5701 3 : return error_mark_node;
5702 : }
5703 :
5704 56 : arg2 = cp_convert (stype, arg2, complain);
5705 56 : arg2 = save_expr (arg2);
5706 56 : arg2 = build_vector_from_val (vtype, arg2);
5707 56 : arg2_type = vtype;
5708 56 : arg3 = cp_convert (stype, arg3, complain);
5709 56 : arg3 = save_expr (arg3);
5710 56 : arg3 = build_vector_from_val (vtype, arg3);
5711 56 : arg3_type = vtype;
5712 : }
5713 :
5714 1672 : if ((gnu_vector_type_p (arg2_type) && !VECTOR_TYPE_P (arg3_type))
5715 1619 : || (gnu_vector_type_p (arg3_type) && !VECTOR_TYPE_P (arg2_type)))
5716 : {
5717 63 : enum stv_conv convert_flag =
5718 63 : scalar_to_vector (loc, VEC_COND_EXPR, arg2, arg3,
5719 : complain & tf_error);
5720 :
5721 63 : switch (convert_flag)
5722 : {
5723 0 : case stv_error:
5724 0 : return error_mark_node;
5725 7 : case stv_firstarg:
5726 7 : {
5727 7 : arg2 = save_expr (arg2);
5728 7 : arg2 = convert (TREE_TYPE (arg3_type), arg2);
5729 7 : arg2 = build_vector_from_val (arg3_type, arg2);
5730 7 : arg2_type = TREE_TYPE (arg2);
5731 7 : break;
5732 : }
5733 47 : case stv_secondarg:
5734 47 : {
5735 47 : arg3 = save_expr (arg3);
5736 47 : arg3 = convert (TREE_TYPE (arg2_type), arg3);
5737 47 : arg3 = build_vector_from_val (arg2_type, arg3);
5738 47 : arg3_type = TREE_TYPE (arg3);
5739 47 : break;
5740 : }
5741 : default:
5742 : break;
5743 : }
5744 : }
5745 :
5746 841 : if (!gnu_vector_type_p (arg2_type)
5747 838 : || !gnu_vector_type_p (arg3_type)
5748 832 : || !same_type_p (arg2_type, arg3_type)
5749 832 : || maybe_ne (TYPE_VECTOR_SUBPARTS (arg1_type),
5750 832 : TYPE_VECTOR_SUBPARTS (arg2_type))
5751 1673 : || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type))
5752 : {
5753 9 : if (complain & tf_error)
5754 6 : error_at (loc,
5755 : "incompatible vector types in conditional expression: "
5756 6 : "%qT, %qT and %qT", TREE_TYPE (arg1),
5757 6 : TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3));
5758 9 : return error_mark_node;
5759 : }
5760 :
5761 832 : if (!COMPARISON_CLASS_P (arg1))
5762 : {
5763 89 : tree cmp_type = truth_type_for (arg1_type);
5764 89 : arg1 = build2 (NE_EXPR, cmp_type, arg1, build_zero_cst (arg1_type));
5765 : }
5766 832 : return build3_loc (loc, VEC_COND_EXPR, arg2_type, arg1, arg2, arg3);
5767 : }
5768 :
5769 : /* [expr.cond]
5770 :
5771 : The first expression is implicitly converted to bool (clause
5772 : _conv_). */
5773 2278250 : arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
5774 : LOOKUP_NORMAL);
5775 2278250 : if (error_operand_p (arg1))
5776 31 : return error_mark_node;
5777 :
5778 2278219 : arg2_type = unlowered_expr_type (arg2);
5779 2278219 : arg3_type = unlowered_expr_type (arg3);
5780 :
5781 2278219 : if ((TREE_CODE (arg2) == EXCESS_PRECISION_EXPR
5782 2278197 : || TREE_CODE (arg3) == EXCESS_PRECISION_EXPR)
5783 38 : && (TREE_CODE (arg2_type) == INTEGER_TYPE
5784 38 : || SCALAR_FLOAT_TYPE_P (arg2_type)
5785 0 : || TREE_CODE (arg2_type) == COMPLEX_TYPE)
5786 38 : && (TREE_CODE (arg3_type) == INTEGER_TYPE
5787 38 : || SCALAR_FLOAT_TYPE_P (arg3_type)
5788 0 : || TREE_CODE (arg3_type) == COMPLEX_TYPE))
5789 : {
5790 38 : semantic_result_type
5791 38 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
5792 38 : if (semantic_result_type == error_mark_node)
5793 : {
5794 0 : tree t1 = arg2_type;
5795 0 : tree t2 = arg3_type;
5796 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
5797 0 : t1 = TREE_TYPE (t1);
5798 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
5799 0 : t2 = TREE_TYPE (t2);
5800 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
5801 : && SCALAR_FLOAT_TYPE_P (t2)
5802 : && (extended_float_type_p (t1)
5803 : || extended_float_type_p (t2))
5804 : && cp_compare_floating_point_conversion_ranks
5805 : (t1, t2) == 3);
5806 0 : if (complain & tf_error)
5807 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
5808 : "have unordered conversion rank",
5809 : arg2_type, arg3_type);
5810 0 : return error_mark_node;
5811 : }
5812 38 : if (TREE_CODE (arg2) == EXCESS_PRECISION_EXPR)
5813 : {
5814 22 : arg2 = TREE_OPERAND (arg2, 0);
5815 22 : arg2_type = TREE_TYPE (arg2);
5816 : }
5817 38 : if (TREE_CODE (arg3) == EXCESS_PRECISION_EXPR)
5818 : {
5819 16 : arg3 = TREE_OPERAND (arg3, 0);
5820 16 : arg3_type = TREE_TYPE (arg3);
5821 : }
5822 : }
5823 :
5824 : /* [expr.cond]
5825 :
5826 : If either the second or the third operand has type (possibly
5827 : cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
5828 : array-to-pointer (_conv.array_), and function-to-pointer
5829 : (_conv.func_) standard conversions are performed on the second
5830 : and third operands. */
5831 2278219 : if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
5832 : {
5833 : /* 'void' won't help in resolving an overloaded expression on the
5834 : other side, so require it to resolve by itself. */
5835 6294 : if (arg2_type == unknown_type_node)
5836 : {
5837 9 : arg2 = resolve_nondeduced_context_or_error (arg2, complain);
5838 9 : arg2_type = TREE_TYPE (arg2);
5839 : }
5840 6294 : if (arg3_type == unknown_type_node)
5841 : {
5842 0 : arg3 = resolve_nondeduced_context_or_error (arg3, complain);
5843 0 : arg3_type = TREE_TYPE (arg3);
5844 : }
5845 :
5846 : /* [expr.cond]
5847 :
5848 : One of the following shall hold:
5849 :
5850 : --The second or the third operand (but not both) is a
5851 : throw-expression (_except.throw_); the result is of the type
5852 : and value category of the other.
5853 :
5854 : --Both the second and the third operands have type void; the
5855 : result is of type void and is a prvalue. */
5856 6294 : if (TREE_CODE (arg2) == THROW_EXPR
5857 84 : && TREE_CODE (arg3) != THROW_EXPR)
5858 : {
5859 68 : result_type = arg3_type;
5860 68 : is_glvalue = glvalue_p (arg3);
5861 : }
5862 6226 : else if (TREE_CODE (arg2) != THROW_EXPR
5863 6210 : && TREE_CODE (arg3) == THROW_EXPR)
5864 : {
5865 205 : result_type = arg2_type;
5866 205 : is_glvalue = glvalue_p (arg2);
5867 : }
5868 6021 : else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
5869 : {
5870 5986 : result_type = void_type_node;
5871 5986 : is_glvalue = false;
5872 : }
5873 : else
5874 : {
5875 35 : if (complain & tf_error)
5876 : {
5877 24 : if (VOID_TYPE_P (arg2_type))
5878 12 : error_at (cp_expr_loc_or_loc (arg3, loc),
5879 : "second operand to the conditional operator "
5880 : "is of type %<void%>, but the third operand is "
5881 : "neither a throw-expression nor of type %<void%>");
5882 : else
5883 12 : error_at (cp_expr_loc_or_loc (arg2, loc),
5884 : "third operand to the conditional operator "
5885 : "is of type %<void%>, but the second operand is "
5886 : "neither a throw-expression nor of type %<void%>");
5887 : }
5888 35 : return error_mark_node;
5889 : }
5890 :
5891 6259 : goto valid_operands;
5892 : }
5893 : /* [expr.cond]
5894 :
5895 : Otherwise, if the second and third operand have different types,
5896 : and either has (possibly cv-qualified) class type, or if both are
5897 : glvalues of the same value category and the same type except for
5898 : cv-qualification, an attempt is made to convert each of those operands
5899 : to the type of the other. */
5900 2271925 : else if (!same_type_p (arg2_type, arg3_type)
5901 2271925 : && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)
5902 598405 : || (same_type_ignoring_top_level_qualifiers_p (arg2_type,
5903 : arg3_type)
5904 166477 : && glvalue_p (arg2) && glvalue_p (arg3)
5905 9357 : && lvalue_p (arg2) == lvalue_p (arg3))))
5906 : {
5907 52847 : conversion *conv2;
5908 52847 : conversion *conv3;
5909 52847 : bool converted = false;
5910 :
5911 : /* Get the high-water mark for the CONVERSION_OBSTACK. */
5912 52847 : p = conversion_obstack_alloc (0);
5913 :
5914 52847 : conv2 = conditional_conversion (arg2, arg3, complain);
5915 52847 : conv3 = conditional_conversion (arg3, arg2, complain);
5916 :
5917 : /* [expr.cond]
5918 :
5919 : If both can be converted, or one can be converted but the
5920 : conversion is ambiguous, the program is ill-formed. If
5921 : neither can be converted, the operands are left unchanged and
5922 : further checking is performed as described below. If exactly
5923 : one conversion is possible, that conversion is applied to the
5924 : chosen operand and the converted operand is used in place of
5925 : the original operand for the remainder of this section. */
5926 52847 : if ((conv2 && !conv2->bad_p
5927 38639 : && conv3 && !conv3->bad_p)
5928 38399 : || (conv2 && conv2->kind == ck_ambig)
5929 52579 : || (conv3 && conv3->kind == ck_ambig))
5930 : {
5931 268 : if (complain & tf_error)
5932 : {
5933 8 : error_at (loc, "operands to %<?:%> have different types "
5934 : "%qT and %qT",
5935 : arg2_type, arg3_type);
5936 8 : if (conv2 && !conv2->bad_p && conv3 && !conv3->bad_p)
5937 4 : inform (loc, " and each type can be converted to the other");
5938 4 : else if (conv2 && conv2->kind == ck_ambig)
5939 4 : convert_like (conv2, arg2, complain);
5940 : else
5941 0 : convert_like (conv3, arg3, complain);
5942 : }
5943 268 : result = error_mark_node;
5944 : }
5945 52579 : else if (conv2 && !conv2->bad_p)
5946 : {
5947 38371 : arg2 = convert_like (conv2, arg2, complain);
5948 38371 : arg2 = convert_from_reference (arg2);
5949 38371 : arg2_type = TREE_TYPE (arg2);
5950 : /* Even if CONV2 is a valid conversion, the result of the
5951 : conversion may be invalid. For example, if ARG3 has type
5952 : "volatile X", and X does not have a copy constructor
5953 : accepting a "volatile X&", then even if ARG2 can be
5954 : converted to X, the conversion will fail. */
5955 38371 : if (error_operand_p (arg2))
5956 0 : result = error_mark_node;
5957 : converted = true;
5958 : }
5959 14208 : else if (conv3 && !conv3->bad_p)
5960 : {
5961 13287 : arg3 = convert_like (conv3, arg3, complain);
5962 13287 : arg3 = convert_from_reference (arg3);
5963 13287 : arg3_type = TREE_TYPE (arg3);
5964 13287 : if (error_operand_p (arg3))
5965 0 : result = error_mark_node;
5966 : converted = true;
5967 : }
5968 :
5969 : /* Free all the conversions we allocated. */
5970 52847 : obstack_free (&conversion_obstack, p);
5971 :
5972 52847 : if (result)
5973 : return result;
5974 :
5975 : /* If, after the conversion, both operands have class type,
5976 : treat the cv-qualification of both operands as if it were the
5977 : union of the cv-qualification of the operands.
5978 :
5979 : The standard is not clear about what to do in this
5980 : circumstance. For example, if the first operand has type
5981 : "const X" and the second operand has a user-defined
5982 : conversion to "volatile X", what is the type of the second
5983 : operand after this step? Making it be "const X" (matching
5984 : the first operand) seems wrong, as that discards the
5985 : qualification without actually performing a copy. Leaving it
5986 : as "volatile X" seems wrong as that will result in the
5987 : conditional expression failing altogether, even though,
5988 : according to this step, the one operand could be converted to
5989 : the type of the other. */
5990 52579 : if (converted
5991 51658 : && CLASS_TYPE_P (arg2_type)
5992 75010 : && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
5993 0 : arg2_type = arg3_type =
5994 0 : cp_build_qualified_type (arg2_type,
5995 0 : cp_type_quals (arg2_type)
5996 0 : | cp_type_quals (arg3_type));
5997 : }
5998 :
5999 : /* [expr.cond]
6000 :
6001 : If the second and third operands are glvalues of the same value
6002 : category and have the same type, the result is of that type and
6003 : value category. */
6004 2729325 : if (((lvalue_p (arg2) && lvalue_p (arg3))
6005 2023547 : || (xvalue_p (arg2) && xvalue_p (arg3)))
6006 2535021 : && same_type_p (arg2_type, arg3_type))
6007 : {
6008 243984 : result_type = arg2_type;
6009 243984 : goto valid_operands;
6010 : }
6011 :
6012 : /* [expr.cond]
6013 :
6014 : Otherwise, the result is an rvalue. If the second and third
6015 : operand do not have the same type, and either has (possibly
6016 : cv-qualified) class type, overload resolution is used to
6017 : determine the conversions (if any) to be applied to the operands
6018 : (_over.match.oper_, _over.built_). */
6019 2027673 : is_glvalue = false;
6020 2027673 : if (!same_type_p (arg2_type, arg3_type)
6021 2027673 : && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
6022 : {
6023 921 : releasing_vec args;
6024 921 : conversion *conv;
6025 921 : bool any_viable_p;
6026 :
6027 : /* Rearrange the arguments so that add_builtin_candidate only has
6028 : to know about two args. In build_builtin_candidate, the
6029 : arguments are unscrambled. */
6030 921 : args->quick_push (arg2);
6031 921 : args->quick_push (arg3);
6032 921 : args->quick_push (arg1);
6033 921 : add_builtin_candidates (&candidates,
6034 : COND_EXPR,
6035 : NOP_EXPR,
6036 : ovl_op_identifier (false, COND_EXPR),
6037 : args,
6038 : LOOKUP_NORMAL, complain);
6039 :
6040 : /* [expr.cond]
6041 :
6042 : If the overload resolution fails, the program is
6043 : ill-formed. */
6044 921 : candidates = splice_viable (candidates, false, &any_viable_p);
6045 921 : if (!any_viable_p)
6046 : {
6047 898 : if (complain & tf_error)
6048 18 : error_at (loc, "operands to %<?:%> have different types %qT and %qT",
6049 : arg2_type, arg3_type);
6050 898 : return error_mark_node;
6051 : }
6052 23 : cand = tourney (candidates, complain);
6053 23 : if (!cand)
6054 : {
6055 0 : if (complain & tf_error)
6056 : {
6057 0 : auto_diagnostic_group d;
6058 0 : op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
6059 0 : print_z_candidates (loc, candidates);
6060 0 : }
6061 0 : return error_mark_node;
6062 : }
6063 :
6064 : /* [expr.cond]
6065 :
6066 : Otherwise, the conversions thus determined are applied, and
6067 : the converted operands are used in place of the original
6068 : operands for the remainder of this section. */
6069 23 : conv = cand->convs[0];
6070 23 : arg1 = convert_like (conv, arg1, complain);
6071 23 : conv = cand->convs[1];
6072 23 : arg2 = convert_like (conv, arg2, complain);
6073 23 : arg2_type = TREE_TYPE (arg2);
6074 23 : conv = cand->convs[2];
6075 23 : arg3 = convert_like (conv, arg3, complain);
6076 23 : arg3_type = TREE_TYPE (arg3);
6077 921 : }
6078 :
6079 : /* [expr.cond]
6080 :
6081 : Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
6082 : and function-to-pointer (_conv.func_) standard conversions are
6083 : performed on the second and third operands.
6084 :
6085 : We need to force the lvalue-to-rvalue conversion here for class types,
6086 : so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
6087 : that isn't wrapped with a TARGET_EXPR plays havoc with exception
6088 : regions. */
6089 :
6090 2026775 : arg2 = force_rvalue (arg2, complain);
6091 2026775 : if (!CLASS_TYPE_P (arg2_type))
6092 1989044 : arg2_type = TREE_TYPE (arg2);
6093 :
6094 2026775 : arg3 = force_rvalue (arg3, complain);
6095 2026775 : if (!CLASS_TYPE_P (arg3_type))
6096 1989044 : arg3_type = TREE_TYPE (arg3);
6097 :
6098 2026775 : if (arg2 == error_mark_node || arg3 == error_mark_node)
6099 : return error_mark_node;
6100 :
6101 : /* [expr.cond]
6102 :
6103 : After those conversions, one of the following shall hold:
6104 :
6105 : --The second and third operands have the same type; the result is of
6106 : that type. */
6107 2026719 : if (same_type_p (arg2_type, arg3_type))
6108 : result_type = arg2_type;
6109 : /* [expr.cond]
6110 :
6111 : --The second and third operands have arithmetic or enumeration
6112 : type; the usual arithmetic conversions are performed to bring
6113 : them to a common type, and the result is of that type. */
6114 394979 : else if ((ARITHMETIC_TYPE_P (arg2_type)
6115 8745 : || UNSCOPED_ENUM_P (arg2_type))
6116 395081 : && (ARITHMETIC_TYPE_P (arg3_type)
6117 184 : || UNSCOPED_ENUM_P (arg3_type)))
6118 : {
6119 : /* A conditional expression between a floating-point
6120 : type and an integer type should convert the integer type to
6121 : the evaluation format of the floating-point type, with
6122 : possible excess precision. */
6123 386224 : tree eptype2 = arg2_type;
6124 386224 : tree eptype3 = arg3_type;
6125 386224 : tree eptype;
6126 559 : if (ANY_INTEGRAL_TYPE_P (arg2_type)
6127 386228 : && (eptype = excess_precision_type (arg3_type)) != NULL_TREE)
6128 : {
6129 4 : eptype3 = eptype;
6130 4 : if (!semantic_result_type)
6131 4 : semantic_result_type
6132 4 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
6133 : }
6134 275 : else if (ANY_INTEGRAL_TYPE_P (arg3_type)
6135 386220 : && (eptype = excess_precision_type (arg2_type)) != NULL_TREE)
6136 : {
6137 4 : eptype2 = eptype;
6138 4 : if (!semantic_result_type)
6139 4 : semantic_result_type
6140 4 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
6141 : }
6142 386224 : result_type = type_after_usual_arithmetic_conversions (eptype2,
6143 : eptype3);
6144 386224 : if (result_type == error_mark_node)
6145 : {
6146 0 : tree t1 = eptype2;
6147 0 : tree t2 = eptype3;
6148 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6149 0 : t1 = TREE_TYPE (t1);
6150 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6151 0 : t2 = TREE_TYPE (t2);
6152 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
6153 : && SCALAR_FLOAT_TYPE_P (t2)
6154 : && (extended_float_type_p (t1)
6155 : || extended_float_type_p (t2))
6156 : && cp_compare_floating_point_conversion_ranks
6157 : (t1, t2) == 3);
6158 0 : if (complain & tf_error)
6159 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
6160 : "have unordered conversion rank",
6161 : eptype2, eptype3);
6162 0 : return error_mark_node;
6163 : }
6164 386224 : if (semantic_result_type == error_mark_node)
6165 : {
6166 0 : tree t1 = arg2_type;
6167 0 : tree t2 = arg3_type;
6168 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6169 0 : t1 = TREE_TYPE (t1);
6170 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6171 0 : t2 = TREE_TYPE (t2);
6172 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
6173 : && SCALAR_FLOAT_TYPE_P (t2)
6174 : && (extended_float_type_p (t1)
6175 : || extended_float_type_p (t2))
6176 : && cp_compare_floating_point_conversion_ranks
6177 : (t1, t2) == 3);
6178 0 : if (complain & tf_error)
6179 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
6180 : "have unordered conversion rank",
6181 : arg2_type, arg3_type);
6182 0 : return error_mark_node;
6183 : }
6184 :
6185 386224 : if (complain & tf_warning)
6186 376343 : do_warn_double_promotion (result_type, arg2_type, arg3_type,
6187 : "implicit conversion from %qH to %qI to "
6188 : "match other result of conditional",
6189 : loc);
6190 :
6191 386224 : if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
6192 102 : && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
6193 : {
6194 36 : tree stripped_orig_arg2 = tree_strip_any_location_wrapper (orig_arg2);
6195 36 : tree stripped_orig_arg3 = tree_strip_any_location_wrapper (orig_arg3);
6196 36 : if (TREE_CODE (stripped_orig_arg2) == CONST_DECL
6197 28 : && TREE_CODE (stripped_orig_arg3) == CONST_DECL
6198 64 : && (DECL_CONTEXT (stripped_orig_arg2)
6199 28 : == DECL_CONTEXT (stripped_orig_arg3)))
6200 : /* Two enumerators from the same enumeration can have different
6201 : types when the enumeration is still being defined. */;
6202 28 : else if (complain & tf_warning)
6203 28 : warning_at (loc, OPT_Wenum_compare, "enumerated mismatch "
6204 : "in conditional expression: %qT vs %qT",
6205 : arg2_type, arg3_type);
6206 : }
6207 386188 : else if ((complain & tf_warning)
6208 376307 : && warn_deprecated_enum_float_conv
6209 27556 : && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
6210 7 : && SCALAR_FLOAT_TYPE_P (arg3_type))
6211 27553 : || (SCALAR_FLOAT_TYPE_P (arg2_type)
6212 93 : && TREE_CODE (arg3_type) == ENUMERAL_TYPE)))
6213 : {
6214 5 : if (TREE_CODE (arg2_type) == ENUMERAL_TYPE)
6215 3 : warning_at (loc, OPT_Wdeprecated_enum_float_conversion,
6216 : "conditional expression between enumeration type "
6217 : "%qT and floating-point type %qT is deprecated",
6218 : arg2_type, arg3_type);
6219 : else
6220 2 : warning_at (loc, OPT_Wdeprecated_enum_float_conversion,
6221 : "conditional expression between floating-point "
6222 : "type %qT and enumeration type %qT is deprecated",
6223 : arg2_type, arg3_type);
6224 : }
6225 377959 : else if ((extra_warnings || warn_enum_conversion)
6226 386198 : && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
6227 25 : && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
6228 8218 : || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
6229 14 : && !same_type_p (arg2_type,
6230 : type_promotes_to (arg3_type)))))
6231 : {
6232 31 : if (complain & tf_warning)
6233 : {
6234 62 : enum opt_code opt = (warn_enum_conversion
6235 31 : ? OPT_Wenum_conversion
6236 : : OPT_Wextra);
6237 31 : warning_at (loc, opt, "enumerated and "
6238 : "non-enumerated type in conditional expression");
6239 : }
6240 : }
6241 :
6242 772448 : arg2 = perform_implicit_conversion (result_type, arg2, complain);
6243 386224 : arg3 = perform_implicit_conversion (result_type, arg3, complain);
6244 : }
6245 : /* [expr.cond]
6246 :
6247 : --The second and third operands have pointer type, or one has
6248 : pointer type and the other is a null pointer constant; pointer
6249 : conversions (_conv.ptr_) and qualification conversions
6250 : (_conv.qual_) are performed to bring them to their composite
6251 : pointer type (_expr.rel_). The result is of the composite
6252 : pointer type.
6253 :
6254 : --The second and third operands have pointer to member type, or
6255 : one has pointer to member type and the other is a null pointer
6256 : constant; pointer to member conversions (_conv.mem_) and
6257 : qualification conversions (_conv.qual_) are performed to bring
6258 : them to a common type, whose cv-qualification shall match the
6259 : cv-qualification of either the second or the third operand.
6260 : The result is of the common type. */
6261 8755 : else if ((null_ptr_cst_p (arg2)
6262 321 : && TYPE_PTR_OR_PTRMEM_P (arg3_type))
6263 8434 : || (null_ptr_cst_p (arg3)
6264 7405 : && TYPE_PTR_OR_PTRMEM_P (arg2_type))
6265 1029 : || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
6266 103 : || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type))
6267 8851 : || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
6268 : {
6269 8668 : result_type = composite_pointer_type (loc,
6270 : arg2_type, arg3_type, arg2,
6271 : arg3, CPO_CONDITIONAL_EXPR,
6272 : complain);
6273 8668 : if (result_type == error_mark_node)
6274 : return error_mark_node;
6275 17296 : arg2 = perform_implicit_conversion (result_type, arg2, complain);
6276 8648 : arg3 = perform_implicit_conversion (result_type, arg3, complain);
6277 : }
6278 :
6279 2026612 : if (!result_type)
6280 : {
6281 87 : if (complain & tf_error)
6282 4 : error_at (loc, "operands to %<?:%> have different types %qT and %qT",
6283 : arg2_type, arg3_type);
6284 87 : return error_mark_node;
6285 : }
6286 :
6287 2026612 : if (arg2 == error_mark_node || arg3 == error_mark_node)
6288 : return error_mark_node;
6289 :
6290 2026609 : valid_operands:
6291 2276852 : if (processing_template_decl && is_glvalue)
6292 : {
6293 : /* Let lvalue_kind know this was a glvalue. */
6294 53644 : tree arg = (result_type == arg2_type ? arg2 : arg3);
6295 53644 : result_type = cp_build_reference_type (result_type, xvalue_p (arg));
6296 : }
6297 :
6298 2276852 : result = build3_loc (loc, COND_EXPR, result_type, arg1, arg2, arg3);
6299 :
6300 : /* If the ARG2 and ARG3 are the same and don't have side-effects,
6301 : warn here, because the COND_EXPR will be turned into ARG2. */
6302 2276852 : if (warn_duplicated_branches
6303 162 : && (complain & tf_warning)
6304 2277014 : && (arg2 == arg3 || operand_equal_p (arg2, arg3,
6305 : OEP_ADDRESS_OF_SAME_FIELD)))
6306 60 : warning_at (EXPR_LOCATION (result), OPT_Wduplicated_branches,
6307 : "this condition has identical branches");
6308 :
6309 : /* We can't use result_type below, as fold might have returned a
6310 : throw_expr. */
6311 :
6312 2276852 : if (!is_glvalue)
6313 : {
6314 : /* Expand both sides into the same slot, hopefully the target of
6315 : the ?: expression. We used to check for TARGET_EXPRs here,
6316 : but now we sometimes wrap them in NOP_EXPRs so the test would
6317 : fail. */
6318 2032796 : if (CLASS_TYPE_P (TREE_TYPE (result)))
6319 : {
6320 37783 : result = get_target_expr (result, complain);
6321 : /* Tell gimplify_modify_expr_rhs not to strip this in
6322 : assignment context: we want both arms to initialize
6323 : the same temporary. */
6324 37783 : TARGET_EXPR_NO_ELIDE (result) = true;
6325 : }
6326 : /* If this expression is an rvalue, but might be mistaken for an
6327 : lvalue, we must add a NON_LVALUE_EXPR. */
6328 2032796 : result = rvalue (result);
6329 2032796 : if (semantic_result_type)
6330 46 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type,
6331 : result);
6332 : }
6333 : else
6334 : {
6335 244056 : result = force_paren_expr (result);
6336 244056 : gcc_assert (semantic_result_type == NULL_TREE);
6337 : }
6338 :
6339 : return result;
6340 2279235 : }
6341 :
6342 : /* OPERAND is an operand to an expression. Perform necessary steps
6343 : required before using it. If OPERAND is NULL_TREE, NULL_TREE is
6344 : returned. */
6345 :
6346 : static tree
6347 319754041 : prep_operand (tree operand)
6348 : {
6349 319754041 : if (operand)
6350 : {
6351 366374258 : if (CLASS_TYPE_P (TREE_TYPE (operand))
6352 188376683 : && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
6353 : /* Make sure the template type is instantiated now. */
6354 1841766 : instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
6355 : }
6356 :
6357 319754041 : return operand;
6358 : }
6359 :
6360 : /* True iff CONV represents a conversion sequence which no other can be better
6361 : than under [over.ics.rank]: in other words, a "conversion" to the exact same
6362 : type (including binding to a reference to the same type). This is stronger
6363 : than the standard's "identity" category, which also includes reference
6364 : bindings that add cv-qualifiers or change rvalueness. */
6365 :
6366 : static bool
6367 97355539 : perfect_conversion_p (conversion *conv)
6368 : {
6369 97355539 : if (CONVERSION_RANK (conv) != cr_identity)
6370 : return false;
6371 69480806 : if (conv->kind == ck_ref_bind)
6372 : {
6373 15000879 : if (!conv->rvaluedness_matches_p)
6374 : return false;
6375 11232047 : if (!same_type_p (TREE_TYPE (conv->type),
6376 : next_conversion (conv)->type))
6377 : return false;
6378 : }
6379 64657592 : if (conv->check_narrowing)
6380 : /* Brace elision is imperfect. */
6381 : return false;
6382 : return true;
6383 : }
6384 :
6385 : /* True if CAND represents a perfect match, i.e. all perfect conversions, so no
6386 : other candidate can be a better match. Since the template/non-template
6387 : tiebreaker comes immediately after the conversion comparison in
6388 : [over.match.best], a perfect non-template candidate is better than all
6389 : templates. */
6390 :
6391 : static bool
6392 212220349 : perfect_candidate_p (z_candidate *cand)
6393 : {
6394 212220349 : if (cand->viable < 1)
6395 : return false;
6396 : /* CWG1402 makes an implicitly deleted move op worse than other
6397 : candidates. */
6398 94679110 : if (DECL_DELETED_FN (cand->fn) && DECL_DEFAULTED_FN (cand->fn)
6399 94262602 : && move_fn_p (cand->fn))
6400 : return false;
6401 93463994 : int len = cand->num_convs;
6402 158121548 : for (int i = 0; i < len; ++i)
6403 97355539 : if (!perfect_conversion_p (cand->convs[i]))
6404 : return false;
6405 60766009 : if (conversion *conv = cand->second_conv)
6406 0 : if (!perfect_conversion_p (conv))
6407 : return false;
6408 : return true;
6409 : }
6410 :
6411 : /* True iff one of CAND's argument conversions is missing. */
6412 :
6413 : static bool
6414 7502074 : missing_conversion_p (const z_candidate *cand)
6415 : {
6416 14020123 : for (unsigned i = 0; i < cand->num_convs; ++i)
6417 : {
6418 9485705 : conversion *conv = cand->convs[i];
6419 9485705 : if (!conv)
6420 : return true;
6421 8927694 : if (conv->kind == ck_deferred_bad)
6422 : {
6423 : /* We don't know whether this conversion is outright invalid or
6424 : just bad, so conservatively assume it's missing. */
6425 2409645 : gcc_checking_assert (conv->bad_p);
6426 : return true;
6427 : }
6428 : }
6429 : return false;
6430 : }
6431 :
6432 : /* Add each of the viable functions in FNS (a FUNCTION_DECL or
6433 : OVERLOAD) to the CANDIDATES, returning an updated list of
6434 : CANDIDATES. The ARGS are the arguments provided to the call;
6435 : if FIRST_ARG is non-null it is the implicit object argument,
6436 : otherwise the first element of ARGS is used if needed. The
6437 : EXPLICIT_TARGS are explicit template arguments provided.
6438 : TEMPLATE_ONLY is true if only template functions should be
6439 : considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
6440 : add_function_candidate. */
6441 :
6442 : static void
6443 113553686 : add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args,
6444 : tree return_type,
6445 : tree explicit_targs, bool template_only,
6446 : tree conversion_path, tree access_path,
6447 : int flags,
6448 : struct z_candidate **candidates,
6449 : tsubst_flags_t complain)
6450 : {
6451 113553686 : tree ctype;
6452 113553686 : const vec<tree, va_gc> *non_static_args;
6453 113553686 : bool check_list_ctor = false;
6454 113553686 : bool check_converting = false;
6455 113553686 : unification_kind_t strict;
6456 113553686 : tree ne_fns = NULL_TREE;
6457 :
6458 113553686 : if (!fns)
6459 : return;
6460 :
6461 : /* Precalculate special handling of constructors and conversion ops. */
6462 112276788 : tree fn = OVL_FIRST (fns);
6463 112276788 : if (DECL_CONV_FN_P (fn))
6464 : {
6465 3980525 : check_list_ctor = false;
6466 3980525 : check_converting = (flags & LOOKUP_ONLYCONVERTING) != 0;
6467 3980525 : if (flags & LOOKUP_NO_CONVERSION)
6468 : /* We're doing return_type(x). */
6469 : strict = DEDUCE_CONV;
6470 : else
6471 : /* We're doing x.operator return_type(). */
6472 172 : strict = DEDUCE_EXACT;
6473 : /* [over.match.funcs] For conversion functions, the function
6474 : is considered to be a member of the class of the implicit
6475 : object argument for the purpose of defining the type of
6476 : the implicit object parameter. */
6477 3980525 : ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg));
6478 : }
6479 : else
6480 : {
6481 216592526 : if (DECL_CONSTRUCTOR_P (fn))
6482 : {
6483 26969447 : check_list_ctor = (flags & LOOKUP_LIST_ONLY) != 0;
6484 : /* For list-initialization we consider explicit constructors
6485 : and complain if one is chosen. */
6486 26969447 : check_converting
6487 26969447 : = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
6488 : == LOOKUP_ONLYCONVERTING);
6489 : }
6490 108296263 : strict = DEDUCE_CALL;
6491 175097904 : ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
6492 : }
6493 :
6494 : /* P2468: Check if operator== is a rewrite target with first operand
6495 : (*args)[0]; for now just do the lookups. */
6496 112276788 : if ((flags & (LOOKUP_REWRITTEN | LOOKUP_REVERSED))
6497 112276788 : && DECL_OVERLOADED_OPERATOR_IS (fn, EQ_EXPR))
6498 : {
6499 143266 : tree ne_name = ovl_op_identifier (false, NE_EXPR);
6500 143266 : if (DECL_CLASS_SCOPE_P (fn))
6501 : {
6502 8140 : ne_fns = lookup_fnfields (TREE_TYPE ((*args)[0]), ne_name,
6503 : 1, tf_none);
6504 8140 : if (ne_fns == error_mark_node || ne_fns == NULL_TREE)
6505 : ne_fns = NULL_TREE;
6506 : else
6507 4472 : ne_fns = BASELINK_FUNCTIONS (ne_fns);
6508 : }
6509 : else
6510 : {
6511 135126 : tree context = decl_namespace_context (fn);
6512 135126 : ne_fns = lookup_qualified_name (context, ne_name, LOOK_want::NORMAL,
6513 : /*complain*/false);
6514 135126 : if (ne_fns == error_mark_node
6515 85954 : || !is_overloaded_fn (ne_fns))
6516 112186362 : ne_fns = NULL_TREE;
6517 : }
6518 : }
6519 :
6520 112276788 : if (first_arg)
6521 : non_static_args = args;
6522 : else
6523 : /* Delay creating the implicit this parameter until it is needed. */
6524 42999475 : non_static_args = NULL;
6525 :
6526 112276788 : bool seen_strictly_viable = any_strictly_viable (*candidates);
6527 : /* If there's a non-template perfect match, we don't need to consider
6528 : templates. So check non-templates first. This optimization is only
6529 : really needed for the defaulted copy constructor of tuple and the like
6530 : (96926), but it seems like we might as well enable it more generally. */
6531 112276788 : bool seen_perfect = false;
6532 112276788 : enum { templates, non_templates, either } which = either;
6533 112276788 : if (template_only)
6534 : which = templates;
6535 : else /*if (flags & LOOKUP_DEFAULTED)*/
6536 100290322 : which = non_templates;
6537 :
6538 : /* During overload resolution, we first consider each function under the
6539 : assumption that we'll eventually find a strictly viable candidate.
6540 : This allows us to circumvent our defacto behavior when checking
6541 : argument conversions and shortcut consideration of the candidate
6542 : upon encountering the first bad conversion. If this assumption
6543 : turns out to be false, and all candidates end up being non-strictly
6544 : viable, then we reconsider such candidates under the defacto behavior.
6545 : This trick is important for pruning member function overloads according
6546 : to their const/ref-qualifiers (since all 'this' conversions are at
6547 : worst bad) without breaking -fpermissive. */
6548 : tree bad_fns = NULL_TREE;
6549 : bool shortcut_bad_convs = true;
6550 :
6551 151814783 : again:
6552 961012386 : for (tree fn : lkp_range (fns))
6553 : {
6554 657394570 : if (check_converting && DECL_NONCONVERTING_P (fn))
6555 12682843 : continue;
6556 644711727 : if (check_list_ctor && !is_list_ctor (fn))
6557 1348306 : continue;
6558 643363421 : if (which == templates && TREE_CODE (fn) != TEMPLATE_DECL)
6559 91989025 : continue;
6560 551374396 : if (which == non_templates && TREE_CODE (fn) == TEMPLATE_DECL)
6561 176389539 : continue;
6562 :
6563 374984857 : tree fn_first_arg = NULL_TREE;
6564 374984857 : const vec<tree, va_gc> *fn_args = args;
6565 :
6566 374984857 : if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
6567 : {
6568 : /* Figure out where the object arg comes from. If this
6569 : function is a non-static member and we didn't get an
6570 : implicit object argument, move it out of args. */
6571 165579108 : if (first_arg == NULL_TREE)
6572 : {
6573 1504853 : unsigned int ix;
6574 1504853 : tree arg;
6575 1504853 : vec<tree, va_gc> *tempvec;
6576 1504853 : vec_alloc (tempvec, args->length () - 1);
6577 3950907 : for (ix = 1; args->iterate (ix, &arg); ++ix)
6578 941201 : tempvec->quick_push (arg);
6579 1504853 : non_static_args = tempvec;
6580 1504853 : first_arg = (*args)[0];
6581 : }
6582 :
6583 : fn_first_arg = first_arg;
6584 : fn_args = non_static_args;
6585 : }
6586 :
6587 : /* Don't bother reversing an operator with two identical parameters. */
6588 209405749 : else if (vec_safe_length (args) == 2 && (flags & LOOKUP_REVERSED))
6589 : {
6590 2471978 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
6591 2471978 : if (same_type_p (TREE_VALUE (parmlist),
6592 : TREE_VALUE (TREE_CHAIN (parmlist))))
6593 1205858 : continue;
6594 : }
6595 :
6596 : /* When considering reversed operator==, if there's a corresponding
6597 : operator!= in the same scope, it's not a rewrite target. */
6598 373778999 : if (ne_fns)
6599 : {
6600 1198261 : bool found = false;
6601 7210500 : for (lkp_iterator ne (ne_fns); !found && ne; ++ne)
6602 4956532 : if (0 && !ne.using_p ()
6603 : && DECL_NAMESPACE_SCOPE_P (fn)
6604 : && DECL_CONTEXT (*ne) != DECL_CONTEXT (fn))
6605 : /* ??? This kludge excludes inline namespace members for the H
6606 : test in spaceship-eq15.C, but I don't see why we would want
6607 : that behavior. Asked Core 2022-11-04. Disabling for now. */;
6608 4956532 : else if (fns_correspond (fn, *ne))
6609 : {
6610 : found = true;
6611 : break;
6612 : }
6613 1198261 : if (found)
6614 142554 : continue;
6615 : }
6616 :
6617 373636445 : if (TREE_CODE (fn) == TEMPLATE_DECL)
6618 : {
6619 161416096 : if (!add_template_candidate (candidates,
6620 : fn,
6621 : ctype,
6622 : explicit_targs,
6623 : fn_first_arg,
6624 : fn_args,
6625 : return_type,
6626 : access_path,
6627 : conversion_path,
6628 : flags,
6629 : strict,
6630 : shortcut_bad_convs,
6631 : complain))
6632 69 : continue;
6633 : }
6634 : else
6635 : {
6636 212220349 : add_function_candidate (candidates,
6637 : fn,
6638 : ctype,
6639 : fn_first_arg,
6640 : fn_args,
6641 : access_path,
6642 : conversion_path,
6643 : flags,
6644 : NULL,
6645 : shortcut_bad_convs,
6646 : complain);
6647 212220349 : if (perfect_candidate_p (*candidates))
6648 373624626 : seen_perfect = true;
6649 : }
6650 :
6651 373624626 : z_candidate *cand = *candidates;
6652 373624626 : if (cand->viable == 1)
6653 119327350 : seen_strictly_viable = true;
6654 :
6655 373624626 : if (cand->viable == -1
6656 7502551 : && shortcut_bad_convs
6657 381126700 : && missing_conversion_p (cand))
6658 : {
6659 : /* This candidate has been tentatively marked non-strictly viable,
6660 : and we didn't compute all argument conversions for it (having
6661 : stopped at the first bad conversion). Add the function to BAD_FNS
6662 : to fully reconsider later if we don't find any strictly viable
6663 : candidates. */
6664 2967656 : if (complain & (tf_error | tf_conv))
6665 : {
6666 2929899 : bad_fns = lookup_add (fn, bad_fns);
6667 2929899 : *candidates = (*candidates)->next;
6668 : }
6669 : else
6670 : /* But if we're in a SFINAE context, just mark this candidate as
6671 : unviable outright and avoid potentially reconsidering it.
6672 : This is safe to do because in a SFINAE context, performing a bad
6673 : conversion is always an error (even with -fpermissive), so a
6674 : non-strictly viable candidate is effectively unviable anyway. */
6675 37757 : cand->viable = 0;
6676 : }
6677 : }
6678 151803033 : if (which == non_templates && !seen_perfect)
6679 : {
6680 39526638 : which = templates;
6681 39526638 : goto again;
6682 : }
6683 112276395 : else if (which == templates
6684 112276395 : && !seen_strictly_viable
6685 : && shortcut_bad_convs
6686 17273878 : && bad_fns)
6687 : {
6688 : /* None of the candidates are strictly viable, so consider again those
6689 : functions in BAD_FNS, this time without shortcutting bad conversions
6690 : so that all their argument conversions are computed. */
6691 11357 : which = either;
6692 11357 : fns = bad_fns;
6693 11357 : shortcut_bad_convs = false;
6694 11357 : goto again;
6695 : }
6696 : }
6697 :
6698 : /* Returns 1 if P0145R2 says that the LHS of operator CODE is evaluated first,
6699 : -1 if the RHS is evaluated first, or 0 if the order is unspecified. */
6700 :
6701 : static int
6702 4252833 : op_is_ordered (tree_code code)
6703 : {
6704 4252771 : switch (code)
6705 : {
6706 : // 5. b @= a
6707 1076255 : case MODIFY_EXPR:
6708 1076255 : return (flag_strong_eval_order > 1 ? -1 : 0);
6709 :
6710 : // 6. a[b]
6711 227870 : case ARRAY_REF:
6712 227808 : return (flag_strong_eval_order > 1 ? 1 : 0);
6713 :
6714 : // 1. a.b
6715 : // Not overloadable (yet).
6716 : // 2. a->b
6717 : // Only one argument.
6718 : // 3. a->*b
6719 20817 : case MEMBER_REF:
6720 : // 7. a << b
6721 20817 : case LSHIFT_EXPR:
6722 : // 8. a >> b
6723 20817 : case RSHIFT_EXPR:
6724 : // a && b
6725 : // Predates P0145R3.
6726 20817 : case TRUTH_ANDIF_EXPR:
6727 : // a || b
6728 : // Predates P0145R3.
6729 20817 : case TRUTH_ORIF_EXPR:
6730 : // a , b
6731 : // Predates P0145R3.
6732 20817 : case COMPOUND_EXPR:
6733 20817 : return (flag_strong_eval_order ? 1 : 0);
6734 :
6735 : default:
6736 : return 0;
6737 : }
6738 : }
6739 :
6740 : /* Subroutine of build_new_op: Add to CANDIDATES all candidates for the
6741 : operator indicated by CODE/CODE2. This function calls itself recursively to
6742 : handle C++20 rewritten comparison operator candidates.
6743 :
6744 : LOOKUPS, if non-NULL, is the set of pertinent namespace-scope operator
6745 : overloads to consider. This parameter is used when instantiating a
6746 : dependent operator expression and has the same structure as
6747 : DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS. */
6748 :
6749 : static tree
6750 8280214 : add_operator_candidates (z_candidate **candidates,
6751 : tree_code code, tree_code code2,
6752 : vec<tree, va_gc> *arglist, tree lookups,
6753 : int flags, tsubst_flags_t complain)
6754 : {
6755 8280214 : z_candidate *start_candidates = *candidates;
6756 8280214 : bool ismodop = code2 != ERROR_MARK;
6757 15209810 : tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
6758 :
6759 : /* LOOKUP_REWRITTEN is set when we're looking for the == or <=> operator to
6760 : rewrite from, and also when we're looking for the e.g. < operator to use
6761 : on the result of <=>. In the latter case, we don't want the flag set in
6762 : the candidate, we just want to suppress looking for rewrites. */
6763 8280214 : bool rewritten = (flags & LOOKUP_REWRITTEN);
6764 8280214 : if (rewritten && code != EQ_EXPR && code != SPACESHIP_EXPR)
6765 29942 : flags &= ~LOOKUP_REWRITTEN;
6766 :
6767 8183072 : bool memonly = false;
6768 8183072 : switch (code)
6769 : {
6770 : /* =, ->, [], () must be non-static member functions. */
6771 1350618 : case MODIFY_EXPR:
6772 1350618 : if (code2 != NOP_EXPR)
6773 : break;
6774 : /* FALLTHRU */
6775 : case COMPONENT_REF:
6776 : case ARRAY_REF:
6777 : memonly = true;
6778 : break;
6779 :
6780 : default:
6781 : break;
6782 : }
6783 :
6784 : /* Add namespace-scope operators to the list of functions to
6785 : consider. */
6786 : if (!memonly)
6787 : {
6788 7477190 : tree fns;
6789 7477190 : if (!lookups)
6790 6853621 : fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE);
6791 : /* If LOOKUPS is non-NULL, then we're instantiating a dependent operator
6792 : expression, and LOOKUPS is the result of stage 1 name lookup. */
6793 623569 : else if (tree found = purpose_member (fnname, lookups))
6794 238349 : fns = TREE_VALUE (found);
6795 : else
6796 : fns = NULL_TREE;
6797 7477190 : fns = lookup_arg_dependent (fnname, fns, arglist);
6798 7477190 : add_candidates (fns, NULL_TREE, arglist, NULL_TREE,
6799 : NULL_TREE, false, NULL_TREE, NULL_TREE,
6800 : flags, candidates, complain);
6801 : }
6802 :
6803 : /* Add class-member operators to the candidate set. */
6804 8280181 : tree arg1_type = TREE_TYPE ((*arglist)[0]);
6805 8280181 : unsigned nargs = arglist->length () > 1 ? 2 : 1;
6806 6005477 : tree arg2_type = nargs > 1 ? TREE_TYPE ((*arglist)[1]) : NULL_TREE;
6807 8280181 : if (CLASS_TYPE_P (arg1_type))
6808 : {
6809 3805212 : tree fns = lookup_fnfields (arg1_type, fnname, 1, complain);
6810 3805212 : if (fns == error_mark_node)
6811 : return error_mark_node;
6812 3805205 : if (fns)
6813 : {
6814 1732663 : if (code == ARRAY_REF)
6815 : {
6816 227810 : vec<tree,va_gc> *restlist = make_tree_vector ();
6817 455620 : for (unsigned i = 1; i < nargs; ++i)
6818 227810 : vec_safe_push (restlist, (*arglist)[i]);
6819 227810 : z_candidate *save_cand = *candidates;
6820 455620 : add_candidates (BASELINK_FUNCTIONS (fns),
6821 227810 : (*arglist)[0], restlist, NULL_TREE,
6822 : NULL_TREE, false,
6823 227810 : BASELINK_BINFO (fns),
6824 227810 : BASELINK_ACCESS_BINFO (fns),
6825 : flags, candidates, complain);
6826 : /* Release the vec if we didn't add a candidate that uses it. */
6827 227810 : for (z_candidate *c = *candidates; c != save_cand; c = c->next)
6828 227810 : if (c->args == restlist)
6829 : {
6830 227810 : restlist = NULL;
6831 227810 : break;
6832 : }
6833 227810 : release_tree_vector (restlist);
6834 : }
6835 : else
6836 1504853 : add_candidates (BASELINK_FUNCTIONS (fns),
6837 : NULL_TREE, arglist, NULL_TREE,
6838 : NULL_TREE, false,
6839 1504853 : BASELINK_BINFO (fns),
6840 1504853 : BASELINK_ACCESS_BINFO (fns),
6841 : flags, candidates, complain);
6842 : }
6843 : }
6844 : /* Per [over.match.oper]3.2, if no operand has a class type, then
6845 : only non-member functions that have type T1 or reference to
6846 : cv-qualified-opt T1 for the first argument, if the first argument
6847 : has an enumeration type, or T2 or reference to cv-qualified-opt
6848 : T2 for the second argument, if the second argument has an
6849 : enumeration type. Filter out those that don't match. */
6850 4474969 : else if (! arg2_type || ! CLASS_TYPE_P (arg2_type))
6851 : {
6852 : struct z_candidate **candp, **next;
6853 :
6854 94237439 : for (candp = candidates; *candp != start_candidates; candp = next)
6855 : {
6856 89822037 : unsigned i;
6857 89822037 : z_candidate *cand = *candp;
6858 89822037 : next = &cand->next;
6859 :
6860 89822037 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
6861 :
6862 264972850 : for (i = 0; i < nargs; ++i)
6863 : {
6864 177272841 : tree parmtype = TREE_VALUE (parmlist);
6865 177272841 : tree argtype = unlowered_expr_type ((*arglist)[i]);
6866 :
6867 177272841 : if (TYPE_REF_P (parmtype))
6868 132324199 : parmtype = TREE_TYPE (parmtype);
6869 177272841 : if (TREE_CODE (argtype) == ENUMERAL_TYPE
6870 347880104 : && (same_type_ignoring_top_level_qualifiers_p
6871 170607263 : (argtype, parmtype)))
6872 : break;
6873 :
6874 175150813 : parmlist = TREE_CHAIN (parmlist);
6875 : }
6876 :
6877 : /* No argument has an appropriate type, so remove this
6878 : candidate function from the list. */
6879 89822037 : if (i == nargs)
6880 : {
6881 87700009 : *candp = cand->next;
6882 87700009 : next = candp;
6883 : }
6884 : }
6885 : }
6886 :
6887 8280174 : if (!rewritten)
6888 : {
6889 : /* The standard says to rewrite built-in candidates, too,
6890 : but there's no point. */
6891 8001656 : add_builtin_candidates (candidates, code, code2, fnname, arglist,
6892 : flags, complain);
6893 :
6894 : /* Maybe add C++20 rewritten comparison candidates. */
6895 8001656 : tree_code rewrite_code = ERROR_MARK;
6896 8001656 : if (cxx_dialect >= cxx20
6897 568745 : && nargs == 2
6898 8450741 : && (OVERLOAD_TYPE_P (arg1_type) || OVERLOAD_TYPE_P (arg2_type)))
6899 448554 : switch (code)
6900 : {
6901 51091 : case LT_EXPR:
6902 51091 : case LE_EXPR:
6903 51091 : case GT_EXPR:
6904 51091 : case GE_EXPR:
6905 51091 : case SPACESHIP_EXPR:
6906 51091 : rewrite_code = SPACESHIP_EXPR;
6907 51091 : break;
6908 :
6909 : case NE_EXPR:
6910 : case EQ_EXPR:
6911 : rewrite_code = EQ_EXPR;
6912 : break;
6913 :
6914 : default:;
6915 : }
6916 :
6917 51091 : if (rewrite_code)
6918 : {
6919 157749 : flags |= LOOKUP_REWRITTEN;
6920 157749 : if (rewrite_code != code)
6921 : /* Add rewritten candidates in same order. */
6922 90756 : add_operator_candidates (candidates, rewrite_code, ERROR_MARK,
6923 : arglist, lookups, flags, complain);
6924 :
6925 157749 : z_candidate *save_cand = *candidates;
6926 :
6927 : /* Add rewritten candidates in reverse order. */
6928 157749 : flags |= LOOKUP_REVERSED;
6929 157749 : vec<tree,va_gc> *revlist = make_tree_vector ();
6930 157749 : revlist->quick_push ((*arglist)[1]);
6931 157749 : revlist->quick_push ((*arglist)[0]);
6932 157749 : add_operator_candidates (candidates, rewrite_code, ERROR_MARK,
6933 : revlist, lookups, flags, complain);
6934 :
6935 : /* Release the vec if we didn't add a candidate that uses it. */
6936 163049 : for (z_candidate *c = *candidates; c != save_cand; c = c->next)
6937 86541 : if (c->args == revlist)
6938 : {
6939 : revlist = NULL;
6940 : break;
6941 : }
6942 157749 : release_tree_vector (revlist);
6943 : }
6944 : }
6945 :
6946 : return NULL_TREE;
6947 : }
6948 :
6949 : tree
6950 106456048 : build_new_op (const op_location_t &loc, enum tree_code code, int flags,
6951 : tree arg1, tree arg2, tree arg3, tree lookups,
6952 : tree *overload, tsubst_flags_t complain)
6953 : {
6954 106456048 : struct z_candidate *candidates = 0, *cand;
6955 106456048 : releasing_vec arglist;
6956 106456048 : tree result = NULL_TREE;
6957 106456048 : bool result_valid_p = false;
6958 106456048 : enum tree_code code2 = ERROR_MARK;
6959 106456048 : enum tree_code code_orig_arg1 = ERROR_MARK;
6960 106456048 : enum tree_code code_orig_arg2 = ERROR_MARK;
6961 106456048 : void *p;
6962 106456048 : bool strict_p;
6963 106456048 : bool any_viable_p;
6964 :
6965 106456048 : auto_cond_timevar tv (TV_OVERLOAD);
6966 :
6967 106456048 : if (error_operand_p (arg1)
6968 106452865 : || error_operand_p (arg2)
6969 212902907 : || error_operand_p (arg3))
6970 9189 : return error_mark_node;
6971 :
6972 106446859 : bool ismodop = code == MODIFY_EXPR;
6973 106446859 : if (ismodop)
6974 : {
6975 4043872 : code2 = TREE_CODE (arg3);
6976 4043872 : arg3 = NULL_TREE;
6977 : }
6978 :
6979 106446859 : tree arg1_type = unlowered_expr_type (arg1);
6980 106446859 : tree arg2_type = arg2 ? unlowered_expr_type (arg2) : NULL_TREE;
6981 :
6982 106446859 : arg1 = prep_operand (arg1);
6983 :
6984 106446859 : switch (code)
6985 : {
6986 0 : case NEW_EXPR:
6987 0 : case VEC_NEW_EXPR:
6988 0 : case VEC_DELETE_EXPR:
6989 0 : case DELETE_EXPR:
6990 : /* Use build_operator_new_call and build_op_delete_call instead. */
6991 0 : gcc_unreachable ();
6992 :
6993 0 : case CALL_EXPR:
6994 : /* Use build_op_call instead. */
6995 0 : gcc_unreachable ();
6996 :
6997 8307445 : case TRUTH_ORIF_EXPR:
6998 8307445 : case TRUTH_ANDIF_EXPR:
6999 8307445 : case TRUTH_AND_EXPR:
7000 8307445 : case TRUTH_OR_EXPR:
7001 : /* These are saved for the sake of warn_logical_operator. */
7002 8307445 : code_orig_arg1 = TREE_CODE (arg1);
7003 8307445 : code_orig_arg2 = TREE_CODE (arg2);
7004 8307445 : break;
7005 22181802 : case GT_EXPR:
7006 22181802 : case LT_EXPR:
7007 22181802 : case GE_EXPR:
7008 22181802 : case LE_EXPR:
7009 22181802 : case EQ_EXPR:
7010 22181802 : case NE_EXPR:
7011 : /* These are saved for the sake of maybe_warn_bool_compare. */
7012 22181802 : code_orig_arg1 = TREE_CODE (arg1_type);
7013 22181802 : code_orig_arg2 = TREE_CODE (arg2_type);
7014 22181802 : break;
7015 :
7016 : default:
7017 : break;
7018 : }
7019 :
7020 106446859 : arg2 = prep_operand (arg2);
7021 106446859 : arg3 = prep_operand (arg3);
7022 :
7023 106446859 : if (code == COND_EXPR)
7024 : /* Use build_conditional_expr instead. */
7025 0 : gcc_unreachable ();
7026 106446859 : else if (! OVERLOAD_TYPE_P (arg1_type)
7027 205010499 : && (! arg2 || ! OVERLOAD_TYPE_P (arg2_type)))
7028 98415150 : goto builtin;
7029 :
7030 8031709 : if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
7031 : {
7032 18463 : arg2 = integer_zero_node;
7033 18463 : arg2_type = integer_type_node;
7034 : }
7035 :
7036 8031709 : arglist->quick_push (arg1);
7037 8031709 : if (arg2 != NULL_TREE)
7038 5757005 : arglist->quick_push (arg2);
7039 8031709 : if (arg3 != NULL_TREE)
7040 0 : arglist->quick_push (arg3);
7041 :
7042 : /* Get the high-water mark for the CONVERSION_OBSTACK. */
7043 8031709 : p = conversion_obstack_alloc (0);
7044 :
7045 8031709 : result = add_operator_candidates (&candidates, code, code2, arglist,
7046 : lookups, flags, complain);
7047 8031676 : if (result == error_mark_node)
7048 7 : goto user_defined_result_ready;
7049 :
7050 8031669 : switch (code)
7051 : {
7052 : case COMPOUND_EXPR:
7053 : case ADDR_EXPR:
7054 : /* For these, the built-in candidates set is empty
7055 : [over.match.oper]/3. We don't want non-strict matches
7056 : because exact matches are always possible with built-in
7057 : operators. The built-in candidate set for COMPONENT_REF
7058 : would be empty too, but since there are no such built-in
7059 : operators, we accept non-strict matches for them. */
7060 : strict_p = true;
7061 : break;
7062 :
7063 6743190 : default:
7064 6743190 : strict_p = false;
7065 6743190 : break;
7066 : }
7067 :
7068 8031669 : candidates = splice_viable (candidates, strict_p, &any_viable_p);
7069 8031669 : if (!any_viable_p)
7070 : {
7071 1295680 : switch (code)
7072 : {
7073 64 : case POSTINCREMENT_EXPR:
7074 64 : case POSTDECREMENT_EXPR:
7075 : /* Don't try anything fancy if we're not allowed to produce
7076 : errors. */
7077 64 : if (!(complain & tf_error))
7078 : return error_mark_node;
7079 :
7080 : /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
7081 : distinguish between prefix and postfix ++ and
7082 : operator++() was used for both, so we allow this with
7083 : -fpermissive. */
7084 : else
7085 : {
7086 49 : tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
7087 98 : const char *msg = (flag_permissive)
7088 49 : ? G_("no %<%D(int)%> declared for postfix %qs,"
7089 : " trying prefix operator instead")
7090 : : G_("no %<%D(int)%> declared for postfix %qs");
7091 49 : permerror (loc, msg, fnname, OVL_OP_INFO (false, code)->name);
7092 : }
7093 :
7094 49 : if (!flag_permissive)
7095 33 : return error_mark_node;
7096 :
7097 16 : if (code == POSTINCREMENT_EXPR)
7098 : code = PREINCREMENT_EXPR;
7099 : else
7100 0 : code = PREDECREMENT_EXPR;
7101 16 : result = build_new_op (loc, code, flags, arg1, NULL_TREE,
7102 : NULL_TREE, lookups, overload, complain);
7103 16 : break;
7104 :
7105 : /* The caller will deal with these. */
7106 : case ADDR_EXPR:
7107 : case COMPOUND_EXPR:
7108 : case COMPONENT_REF:
7109 : case CO_AWAIT_EXPR:
7110 : result = NULL_TREE;
7111 : result_valid_p = true;
7112 : break;
7113 :
7114 4162 : default:
7115 4162 : if (complain & tf_error)
7116 : {
7117 : /* If one of the arguments of the operator represents
7118 : an invalid use of member function pointer, try to report
7119 : a meaningful error ... */
7120 1107 : if (invalid_nonstatic_memfn_p (loc, arg1, tf_error)
7121 1103 : || invalid_nonstatic_memfn_p (loc, arg2, tf_error)
7122 2203 : || invalid_nonstatic_memfn_p (loc, arg3, tf_error))
7123 : /* We displayed the error message. */;
7124 : else
7125 : {
7126 : /* ... Otherwise, report the more generic
7127 : "no matching operator found" error */
7128 1096 : auto_diagnostic_group d;
7129 1096 : op_error (loc, code, code2, arg1, arg2, arg3, FALSE);
7130 1096 : print_z_candidates (loc, candidates);
7131 1096 : }
7132 : }
7133 4162 : result = error_mark_node;
7134 4162 : break;
7135 : }
7136 : }
7137 : else
7138 : {
7139 6735989 : cand = tourney (candidates, complain);
7140 6735989 : if (cand == 0)
7141 : {
7142 136 : if (complain & tf_error)
7143 : {
7144 115 : auto_diagnostic_group d;
7145 115 : op_error (loc, code, code2, arg1, arg2, arg3, TRUE);
7146 115 : print_z_candidates (loc, candidates);
7147 115 : }
7148 136 : result = error_mark_node;
7149 136 : if (overload)
7150 125 : *overload = error_mark_node;
7151 : }
7152 6735853 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
7153 : {
7154 4525728 : if (overload)
7155 4042172 : *overload = cand->fn;
7156 :
7157 4525728 : if (resolve_args (arglist, complain) == NULL)
7158 2 : result = error_mark_node;
7159 : else
7160 : {
7161 4525726 : tsubst_flags_t ocomplain = complain;
7162 4525726 : if (cand->rewritten ())
7163 : /* We'll wrap this call in another one. */
7164 53903 : ocomplain &= ~tf_decltype;
7165 4525726 : if (cand->reversed ())
7166 : {
7167 : /* We swapped these in add_candidate, swap them back now. */
7168 2704 : std::swap (cand->convs[0], cand->convs[1]);
7169 2704 : if (cand->fn == current_function_decl)
7170 1 : warning_at (loc, 0, "in C++20 this comparison calls the "
7171 : "current function recursively with reversed "
7172 : "arguments");
7173 : }
7174 4525726 : result = build_over_call (cand, LOOKUP_NORMAL, ocomplain);
7175 : }
7176 :
7177 8779387 : if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn))
7178 : /* There won't be a CALL_EXPR. */;
7179 4253658 : else if (result && result != error_mark_node)
7180 : {
7181 4252771 : tree call = extract_call_expr (result);
7182 4252771 : CALL_EXPR_OPERATOR_SYNTAX (call) = true;
7183 :
7184 : /* Specify evaluation order as per P0145R2. */
7185 4252771 : CALL_EXPR_ORDERED_ARGS (call) = false;
7186 4252771 : switch (op_is_ordered (code))
7187 : {
7188 1048671 : case -1:
7189 1048671 : CALL_EXPR_REVERSE_ARGS (call) = true;
7190 1048671 : break;
7191 :
7192 243350 : case 1:
7193 243350 : CALL_EXPR_ORDERED_ARGS (call) = true;
7194 243350 : break;
7195 :
7196 : default:
7197 : break;
7198 : }
7199 : }
7200 :
7201 : /* If this was a C++20 rewritten comparison, adjust the result. */
7202 4525724 : if (cand->rewritten ())
7203 : {
7204 : /* FIXME build_min_non_dep_op_overload can't handle rewrites. */
7205 53903 : if (overload)
7206 53832 : *overload = NULL_TREE;
7207 53903 : switch (code)
7208 : {
7209 1245 : case EQ_EXPR:
7210 1245 : gcc_checking_assert (cand->reversed ());
7211 23783 : gcc_fallthrough ();
7212 23783 : case NE_EXPR:
7213 23783 : if (result == error_mark_node)
7214 : ;
7215 : /* If a rewritten operator== candidate is selected by
7216 : overload resolution for an operator @, its return type
7217 : shall be cv bool.... */
7218 23783 : else if (TREE_CODE (TREE_TYPE (result)) != BOOLEAN_TYPE)
7219 : {
7220 5 : if (complain & tf_error)
7221 : {
7222 2 : auto_diagnostic_group d;
7223 2 : error_at (loc, "return type of %qD is not %qs",
7224 : cand->fn, "bool");
7225 2 : inform (loc, "used as rewritten candidate for "
7226 : "comparison of %qT and %qT",
7227 : arg1_type, arg2_type);
7228 2 : }
7229 5 : result = error_mark_node;
7230 : }
7231 23778 : else if (code == NE_EXPR)
7232 : /* !(y == x) or !(x == y) */
7233 22536 : result = build1_loc (loc, TRUTH_NOT_EXPR,
7234 : boolean_type_node, result);
7235 : break;
7236 :
7237 : /* If a rewritten operator<=> candidate is selected by
7238 : overload resolution for an operator @, x @ y is
7239 : interpreted as 0 @ (y <=> x) if the selected candidate is
7240 : a synthesized candidate with reversed order of parameters,
7241 : or (x <=> y) @ 0 otherwise, using the selected rewritten
7242 : operator<=> candidate. */
7243 142 : case SPACESHIP_EXPR:
7244 142 : if (!cand->reversed ())
7245 : /* We're in the build_new_op call below for an outer
7246 : reversed call; we don't need to do anything more. */
7247 : break;
7248 30049 : gcc_fallthrough ();
7249 30049 : case LT_EXPR:
7250 30049 : case LE_EXPR:
7251 30049 : case GT_EXPR:
7252 30049 : case GE_EXPR:
7253 30049 : {
7254 30049 : tree lhs = result;
7255 30049 : tree rhs = integer_zero_node;
7256 30049 : if (cand->reversed ())
7257 341 : std::swap (lhs, rhs);
7258 30049 : warning_sentinel ws (warn_zero_as_null_pointer_constant);
7259 30049 : result = build_new_op (loc, code,
7260 : LOOKUP_NORMAL|LOOKUP_REWRITTEN,
7261 : lhs, rhs, NULL_TREE, lookups,
7262 : NULL, complain);
7263 30049 : }
7264 30049 : break;
7265 :
7266 0 : default:
7267 0 : gcc_unreachable ();
7268 : }
7269 : }
7270 :
7271 : /* In an expression of the form `a[]' where cand->fn
7272 : which is operator[] turns out to be a static member function,
7273 : `a' is none-the-less evaluated. */
7274 4525724 : if (code == ARRAY_REF)
7275 227808 : result = keep_unused_object_arg (result, arg1, cand->fn);
7276 : }
7277 : else
7278 : {
7279 : /* Give any warnings we noticed during overload resolution. */
7280 2210125 : if (cand->warnings && (complain & tf_warning))
7281 : {
7282 : struct candidate_warning *w;
7283 0 : for (w = cand->warnings; w; w = w->next)
7284 0 : joust (cand, w->loser, 1, complain);
7285 : }
7286 :
7287 : /* Check for comparison of different enum types. */
7288 2210125 : switch (code)
7289 : {
7290 1599996 : case GT_EXPR:
7291 1599996 : case LT_EXPR:
7292 1599996 : case GE_EXPR:
7293 1599996 : case LE_EXPR:
7294 1599996 : case EQ_EXPR:
7295 1599996 : case NE_EXPR:
7296 1599996 : if (TREE_CODE (arg1_type) == ENUMERAL_TYPE
7297 1528327 : && TREE_CODE (arg2_type) == ENUMERAL_TYPE
7298 1473438 : && (TYPE_MAIN_VARIANT (arg1_type)
7299 1473438 : != TYPE_MAIN_VARIANT (arg2_type))
7300 1600084 : && (complain & tf_warning))
7301 88 : warning_at (loc, OPT_Wenum_compare,
7302 : "comparison between %q#T and %q#T",
7303 : arg1_type, arg2_type);
7304 : break;
7305 : default:
7306 : break;
7307 : }
7308 :
7309 : /* "If a built-in candidate is selected by overload resolution, the
7310 : operands of class type are converted to the types of the
7311 : corresponding parameters of the selected operation function,
7312 : except that the second standard conversion sequence of a
7313 : user-defined conversion sequence (12.3.3.1.2) is not applied." */
7314 2210125 : conversion *conv = cand->convs[0];
7315 2210125 : if (conv->user_conv_p)
7316 : {
7317 15217 : conv = strip_standard_conversion (conv);
7318 15217 : arg1 = convert_like (conv, arg1, complain);
7319 : }
7320 :
7321 2210125 : if (arg2)
7322 : {
7323 1858097 : conv = cand->convs[1];
7324 1858097 : if (conv->user_conv_p)
7325 : {
7326 5551 : conv = strip_standard_conversion (conv);
7327 5551 : arg2 = convert_like (conv, arg2, complain);
7328 : }
7329 : }
7330 :
7331 2210125 : if (arg3)
7332 : {
7333 0 : conv = cand->convs[2];
7334 0 : if (conv->user_conv_p)
7335 : {
7336 0 : conv = strip_standard_conversion (conv);
7337 0 : arg3 = convert_like (conv, arg3, complain);
7338 : }
7339 : }
7340 : }
7341 : }
7342 :
7343 2210125 : user_defined_result_ready:
7344 :
7345 : /* Free all the conversions we allocated. */
7346 8031624 : obstack_free (&conversion_obstack, p);
7347 :
7348 8031624 : if (result || result_valid_p)
7349 : return result;
7350 :
7351 2210125 : builtin:
7352 100625275 : switch (code)
7353 : {
7354 2693556 : case MODIFY_EXPR:
7355 2693556 : return cp_build_modify_expr (loc, arg1, code2, arg2, complain);
7356 :
7357 8487574 : case INDIRECT_REF:
7358 8487574 : return cp_build_indirect_ref (loc, arg1, RO_UNARY_STAR, complain);
7359 :
7360 8307126 : case TRUTH_ANDIF_EXPR:
7361 8307126 : case TRUTH_ORIF_EXPR:
7362 8307126 : case TRUTH_AND_EXPR:
7363 8307126 : case TRUTH_OR_EXPR:
7364 8307126 : if ((complain & tf_warning) && !processing_template_decl)
7365 2884585 : warn_logical_operator (loc, code, boolean_type_node,
7366 : code_orig_arg1, arg1,
7367 : code_orig_arg2, arg2);
7368 : /* Fall through. */
7369 29887444 : case GT_EXPR:
7370 29887444 : case LT_EXPR:
7371 29887444 : case GE_EXPR:
7372 29887444 : case LE_EXPR:
7373 29887444 : case EQ_EXPR:
7374 29887444 : case NE_EXPR:
7375 29887444 : if ((complain & tf_warning)
7376 25768556 : && ((code_orig_arg1 == BOOLEAN_TYPE)
7377 25768556 : ^ (code_orig_arg2 == BOOLEAN_TYPE)))
7378 8286 : maybe_warn_bool_compare (loc, code, arg1, arg2);
7379 29887444 : if (complain & tf_warning && warn_tautological_compare)
7380 353513 : warn_tautological_cmp (loc, code, arg1, arg2);
7381 : /* Fall through. */
7382 68276138 : case SPACESHIP_EXPR:
7383 68276138 : case PLUS_EXPR:
7384 68276138 : case MINUS_EXPR:
7385 68276138 : case MULT_EXPR:
7386 68276138 : case TRUNC_DIV_EXPR:
7387 68276138 : case MAX_EXPR:
7388 68276138 : case MIN_EXPR:
7389 68276138 : case LSHIFT_EXPR:
7390 68276138 : case RSHIFT_EXPR:
7391 68276138 : case TRUNC_MOD_EXPR:
7392 68276138 : case BIT_AND_EXPR:
7393 68276138 : case BIT_IOR_EXPR:
7394 68276138 : case BIT_XOR_EXPR:
7395 68276138 : return cp_build_binary_op (loc, code, arg1, arg2, complain);
7396 :
7397 18364658 : case UNARY_PLUS_EXPR:
7398 18364658 : case NEGATE_EXPR:
7399 18364658 : case BIT_NOT_EXPR:
7400 18364658 : case TRUTH_NOT_EXPR:
7401 18364658 : case PREINCREMENT_EXPR:
7402 18364658 : case POSTINCREMENT_EXPR:
7403 18364658 : case PREDECREMENT_EXPR:
7404 18364658 : case POSTDECREMENT_EXPR:
7405 18364658 : case REALPART_EXPR:
7406 18364658 : case IMAGPART_EXPR:
7407 18364658 : case ABS_EXPR:
7408 18364658 : case CO_AWAIT_EXPR:
7409 18364658 : return cp_build_unary_op (code, arg1, false, complain);
7410 :
7411 1217356 : case ARRAY_REF:
7412 1217356 : return cp_build_array_ref (input_location, arg1, arg2, complain);
7413 :
7414 729 : case MEMBER_REF:
7415 729 : return build_m_component_ref (cp_build_indirect_ref (loc, arg1,
7416 : RO_ARROW_STAR,
7417 : complain),
7418 729 : arg2, complain);
7419 :
7420 : /* The caller will deal with these. */
7421 : case ADDR_EXPR:
7422 : case COMPONENT_REF:
7423 : case COMPOUND_EXPR:
7424 : return NULL_TREE;
7425 :
7426 0 : default:
7427 0 : gcc_unreachable ();
7428 : }
7429 : return NULL_TREE;
7430 106456011 : }
7431 :
7432 : /* Build a new call to operator[]. This may change ARGS. */
7433 :
7434 : tree
7435 67 : build_op_subscript (const op_location_t &loc, tree obj,
7436 : vec<tree, va_gc> **args, tree *overload,
7437 : tsubst_flags_t complain)
7438 : {
7439 67 : struct z_candidate *candidates = 0, *cand;
7440 67 : tree fns, first_mem_arg = NULL_TREE;
7441 67 : bool any_viable_p;
7442 67 : tree result = NULL_TREE;
7443 67 : void *p;
7444 :
7445 67 : auto_cond_timevar tv (TV_OVERLOAD);
7446 :
7447 67 : obj = mark_lvalue_use (obj);
7448 :
7449 67 : if (error_operand_p (obj))
7450 0 : return error_mark_node;
7451 :
7452 67 : tree type = TREE_TYPE (obj);
7453 :
7454 67 : obj = prep_operand (obj);
7455 :
7456 67 : if (TYPE_BINFO (type))
7457 : {
7458 67 : fns = lookup_fnfields (TYPE_BINFO (type), ovl_op_identifier (ARRAY_REF),
7459 : 1, complain);
7460 67 : if (fns == error_mark_node)
7461 : return error_mark_node;
7462 : }
7463 : else
7464 : fns = NULL_TREE;
7465 :
7466 67 : if (args != NULL && *args != NULL)
7467 : {
7468 67 : *args = resolve_args (*args, complain);
7469 67 : if (*args == NULL)
7470 0 : return error_mark_node;
7471 : }
7472 :
7473 : /* Get the high-water mark for the CONVERSION_OBSTACK. */
7474 67 : p = conversion_obstack_alloc (0);
7475 :
7476 67 : if (fns)
7477 : {
7478 65 : first_mem_arg = obj;
7479 :
7480 65 : add_candidates (BASELINK_FUNCTIONS (fns),
7481 : first_mem_arg, *args, NULL_TREE,
7482 : NULL_TREE, false,
7483 65 : BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
7484 : LOOKUP_NORMAL, &candidates, complain);
7485 : }
7486 :
7487 : /* Be strict here because if we choose a bad conversion candidate, the
7488 : errors we get won't mention the call context. */
7489 67 : candidates = splice_viable (candidates, true, &any_viable_p);
7490 67 : if (!any_viable_p)
7491 : {
7492 5 : if (complain & tf_error)
7493 : {
7494 1 : auto_diagnostic_group d;
7495 2 : error ("no match for call to %<%T::operator[] (%A)%>",
7496 1 : TREE_TYPE (obj), build_tree_list_vec (*args));
7497 1 : print_z_candidates (loc, candidates);
7498 1 : }
7499 5 : result = error_mark_node;
7500 : }
7501 : else
7502 : {
7503 62 : cand = tourney (candidates, complain);
7504 62 : if (cand == 0)
7505 : {
7506 0 : if (complain & tf_error)
7507 : {
7508 0 : auto_diagnostic_group d;
7509 0 : error ("call of %<%T::operator[] (%A)%> is ambiguous",
7510 0 : TREE_TYPE (obj), build_tree_list_vec (*args));
7511 0 : print_z_candidates (loc, candidates);
7512 0 : }
7513 0 : result = error_mark_node;
7514 : }
7515 62 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL
7516 62 : && DECL_OVERLOADED_OPERATOR_P (cand->fn)
7517 124 : && DECL_OVERLOADED_OPERATOR_IS (cand->fn, ARRAY_REF))
7518 : {
7519 62 : if (overload)
7520 62 : *overload = cand->fn;
7521 62 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
7522 124 : if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn))
7523 : /* There won't be a CALL_EXPR. */;
7524 62 : else if (result && result != error_mark_node)
7525 : {
7526 62 : tree call = extract_call_expr (result);
7527 62 : CALL_EXPR_OPERATOR_SYNTAX (call) = true;
7528 :
7529 : /* Specify evaluation order as per P0145R2. */
7530 62 : CALL_EXPR_ORDERED_ARGS (call) = op_is_ordered (ARRAY_REF) == 1;
7531 : }
7532 :
7533 : /* In an expression of the form `a[]' where cand->fn
7534 : which is operator[] turns out to be a static member function,
7535 : `a' is none-the-less evaluated. */
7536 62 : result = keep_unused_object_arg (result, obj, cand->fn);
7537 : }
7538 : else
7539 0 : gcc_unreachable ();
7540 : }
7541 :
7542 : /* Free all the conversions we allocated. */
7543 67 : obstack_free (&conversion_obstack, p);
7544 :
7545 : return result;
7546 67 : }
7547 :
7548 : /* CALL was returned by some call-building function; extract the actual
7549 : CALL_EXPR from any bits that have been tacked on, e.g. by
7550 : convert_from_reference. */
7551 :
7552 : tree
7553 32551756 : extract_call_expr (tree call)
7554 : {
7555 32551800 : while (TREE_CODE (call) == COMPOUND_EXPR)
7556 44 : call = TREE_OPERAND (call, 1);
7557 32551756 : if (REFERENCE_REF_P (call))
7558 3634398 : call = TREE_OPERAND (call, 0);
7559 32551756 : if (TREE_CODE (call) == TARGET_EXPR)
7560 258987 : call = TARGET_EXPR_INITIAL (call);
7561 32551756 : if (cxx_dialect >= cxx20)
7562 1955023 : switch (TREE_CODE (call))
7563 : {
7564 : /* C++20 rewritten comparison operators. */
7565 63064 : case TRUTH_NOT_EXPR:
7566 63064 : call = TREE_OPERAND (call, 0);
7567 63064 : break;
7568 194185 : case LT_EXPR:
7569 194185 : case LE_EXPR:
7570 194185 : case GT_EXPR:
7571 194185 : case GE_EXPR:
7572 194185 : case SPACESHIP_EXPR:
7573 194185 : {
7574 194185 : tree op0 = TREE_OPERAND (call, 0);
7575 194185 : if (integer_zerop (op0))
7576 163 : call = TREE_OPERAND (call, 1);
7577 : else
7578 : call = op0;
7579 : }
7580 : break;
7581 : default:;
7582 : }
7583 :
7584 32551756 : if (TREE_CODE (call) != CALL_EXPR
7585 32551756 : && TREE_CODE (call) != AGGR_INIT_EXPR
7586 20462315 : && call != error_mark_node)
7587 20461913 : return NULL_TREE;
7588 : return call;
7589 : }
7590 :
7591 : /* Returns true if FN has two parameters, of which the second has type
7592 : size_t. */
7593 :
7594 : static bool
7595 95919 : second_parm_is_size_t (tree fn)
7596 : {
7597 95919 : tree t = FUNCTION_ARG_CHAIN (fn);
7598 95919 : if (!t || !same_type_p (TREE_VALUE (t), size_type_node))
7599 95899 : return false;
7600 20 : t = TREE_CHAIN (t);
7601 20 : if (t == void_list_node)
7602 : return true;
7603 : return false;
7604 : }
7605 :
7606 : /* True if T, an allocation function, has std::align_val_t as its second
7607 : argument. */
7608 :
7609 : bool
7610 6 : aligned_allocation_fn_p (tree t)
7611 : {
7612 6 : if (!aligned_new_threshold)
7613 : return false;
7614 :
7615 0 : tree a = FUNCTION_ARG_CHAIN (t);
7616 0 : return (a && same_type_p (TREE_VALUE (a), align_type_node));
7617 : }
7618 :
7619 : /* True if T is std::destroying_delete_t. */
7620 :
7621 : static bool
7622 2554131 : std_destroying_delete_t_p (tree t)
7623 : {
7624 2554131 : return (TYPE_CONTEXT (t) == std_node
7625 2554131 : && id_equal (TYPE_IDENTIFIER (t), "destroying_delete_t"));
7626 : }
7627 :
7628 : /* A deallocation function with at least two parameters whose second parameter
7629 : type is of type std::destroying_delete_t is a destroying operator delete. A
7630 : destroying operator delete shall be a class member function named operator
7631 : delete. [ Note: Array deletion cannot use a destroying operator
7632 : delete. --end note ] */
7633 :
7634 : tree
7635 2554151 : destroying_delete_p (tree t)
7636 : {
7637 2554151 : tree a = TYPE_ARG_TYPES (TREE_TYPE (t));
7638 2554151 : if (!a || !TREE_CHAIN (a))
7639 : return NULL_TREE;
7640 2554131 : tree type = TREE_VALUE (TREE_CHAIN (a));
7641 2554131 : return std_destroying_delete_t_p (type) ? type : NULL_TREE;
7642 : }
7643 :
7644 : struct dealloc_info
7645 : {
7646 : bool sized;
7647 : bool aligned;
7648 : tree destroying;
7649 : };
7650 :
7651 : /* Returns true iff T, an element of an OVERLOAD chain, is a usual deallocation
7652 : function (3.7.4.2 [basic.stc.dynamic.deallocation]). If so, and DI is
7653 : non-null, also set *DI. */
7654 :
7655 : static bool
7656 1843569 : usual_deallocation_fn_p (tree t, dealloc_info *di)
7657 : {
7658 1843569 : if (di) *di = dealloc_info();
7659 :
7660 : /* A template instance is never a usual deallocation function,
7661 : regardless of its signature. */
7662 1843569 : if (TREE_CODE (t) == TEMPLATE_DECL
7663 1843569 : || primary_template_specialization_p (t))
7664 0 : return false;
7665 :
7666 : /* A usual deallocation function is a deallocation function whose parameters
7667 : after the first are
7668 : - optionally, a parameter of type std::destroying_delete_t, then
7669 : - optionally, a parameter of type std::size_t, then
7670 : - optionally, a parameter of type std::align_val_t. */
7671 1843569 : bool global = DECL_NAMESPACE_SCOPE_P (t);
7672 1843569 : tree chain = FUNCTION_ARG_CHAIN (t);
7673 1843569 : if (chain && destroying_delete_p (t))
7674 : {
7675 28 : if (di) di->destroying = TREE_VALUE (chain);
7676 28 : chain = TREE_CHAIN (chain);
7677 : }
7678 1843569 : if (chain
7679 1843565 : && (!global || flag_sized_deallocation)
7680 3674649 : && same_type_p (TREE_VALUE (chain), size_type_node))
7681 : {
7682 530254 : if (di) di->sized = true;
7683 530254 : chain = TREE_CHAIN (chain);
7684 : }
7685 1843565 : if (chain && aligned_new_threshold
7686 3663872 : && same_type_p (TREE_VALUE (chain), align_type_node))
7687 : {
7688 782024 : if (di) di->aligned = true;
7689 782024 : chain = TREE_CHAIN (chain);
7690 : }
7691 1843569 : return (chain == void_list_node);
7692 : }
7693 :
7694 : /* Just return whether FN is a usual deallocation function. */
7695 :
7696 : bool
7697 6251 : usual_deallocation_fn_p (tree fn)
7698 : {
7699 6239 : return usual_deallocation_fn_p (fn, NULL);
7700 : }
7701 :
7702 : /* Build a call to operator delete. This has to be handled very specially,
7703 : because the restrictions on what signatures match are different from all
7704 : other call instances. For a normal delete, only a delete taking (void *)
7705 : or (void *, size_t) is accepted. For a placement delete, only an exact
7706 : match with the placement new is accepted.
7707 :
7708 : CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
7709 : ADDR is the pointer to be deleted.
7710 : SIZE is the size of the memory block to be deleted.
7711 : GLOBAL_P is true if the delete-expression should not consider
7712 : class-specific delete operators.
7713 : PLACEMENT is the corresponding placement new call, or NULL_TREE.
7714 :
7715 : If this call to "operator delete" is being generated as part to
7716 : deallocate memory allocated via a new-expression (as per [expr.new]
7717 : which requires that if the initialization throws an exception then
7718 : we call a deallocation function), then ALLOC_FN is the allocation
7719 : function. */
7720 :
7721 : tree
7722 367367 : build_op_delete_call (enum tree_code code, tree addr, tree size,
7723 : bool global_p, tree placement,
7724 : tree alloc_fn, tsubst_flags_t complain)
7725 : {
7726 367367 : tree fn = NULL_TREE;
7727 367367 : tree fns, fnname, type, t;
7728 367367 : dealloc_info di_fn = { };
7729 :
7730 367367 : if (addr == error_mark_node)
7731 : return error_mark_node;
7732 :
7733 367367 : type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
7734 :
7735 367367 : fnname = ovl_op_identifier (false, code);
7736 :
7737 341795 : if (CLASS_TYPE_P (type)
7738 341764 : && COMPLETE_TYPE_P (complete_type (type))
7739 709107 : && !global_p)
7740 : /* In [class.free]
7741 :
7742 : If the result of the lookup is ambiguous or inaccessible, or if
7743 : the lookup selects a placement deallocation function, the
7744 : program is ill-formed.
7745 :
7746 : Therefore, we ask lookup_fnfields to complain about ambiguity. */
7747 : {
7748 263859 : fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1, complain);
7749 263859 : if (fns == error_mark_node)
7750 : return error_mark_node;
7751 : }
7752 : else
7753 : fns = NULL_TREE;
7754 :
7755 263855 : if (fns == NULL_TREE)
7756 366524 : fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE);
7757 :
7758 : /* Strip const and volatile from addr. */
7759 367363 : tree oaddr = addr;
7760 367363 : addr = cp_convert (ptr_type_node, addr, complain);
7761 :
7762 367363 : tree excluded_destroying = NULL_TREE;
7763 :
7764 367363 : if (placement)
7765 : {
7766 : /* "A declaration of a placement deallocation function matches the
7767 : declaration of a placement allocation function if it has the same
7768 : number of parameters and, after parameter transformations (8.3.5),
7769 : all parameter types except the first are identical."
7770 :
7771 : So we build up the function type we want and ask instantiate_type
7772 : to get it for us. */
7773 96669 : t = FUNCTION_ARG_CHAIN (alloc_fn);
7774 96669 : t = tree_cons (NULL_TREE, ptr_type_node, t);
7775 96669 : t = build_function_type (void_type_node, t);
7776 :
7777 96669 : fn = instantiate_type (t, fns, tf_none);
7778 96669 : if (fn == error_mark_node)
7779 : return NULL_TREE;
7780 :
7781 95919 : fn = MAYBE_BASELINK_FUNCTIONS (fn);
7782 :
7783 : /* "If the lookup finds the two-parameter form of a usual deallocation
7784 : function (3.7.4.2) and that function, considered as a placement
7785 : deallocation function, would have been selected as a match for the
7786 : allocation function, the program is ill-formed." */
7787 95919 : if (second_parm_is_size_t (fn))
7788 : {
7789 12 : const char *const msg1
7790 : = G_("exception cleanup for this placement new selects "
7791 : "non-placement %<operator delete%>");
7792 12 : const char *const msg2
7793 : = G_("%qD is a usual (non-placement) deallocation "
7794 : "function in C++14 (or with %<-fsized-deallocation%>)");
7795 :
7796 : /* But if the class has an operator delete (void *), then that is
7797 : the usual deallocation function, so we shouldn't complain
7798 : about using the operator delete (void *, size_t). */
7799 12 : if (DECL_CLASS_SCOPE_P (fn))
7800 24 : for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns)))
7801 : {
7802 12 : if (usual_deallocation_fn_p (elt)
7803 12 : && FUNCTION_ARG_CHAIN (elt) == void_list_node)
7804 4 : goto ok;
7805 : }
7806 : /* Before C++14 a two-parameter global deallocation function is
7807 : always a placement deallocation function, but warn if
7808 : -Wc++14-compat. */
7809 4 : else if (!flag_sized_deallocation)
7810 : {
7811 1 : if (complain & tf_warning)
7812 : {
7813 1 : auto_diagnostic_group d;
7814 1 : if (warning (OPT_Wc__14_compat, msg1))
7815 1 : inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
7816 1 : }
7817 1 : goto ok;
7818 : }
7819 :
7820 7 : if (complain & tf_warning_or_error)
7821 : {
7822 7 : auto_diagnostic_group d;
7823 7 : if (permerror (input_location, msg1))
7824 : {
7825 : /* Only mention C++14 for namespace-scope delete. */
7826 7 : if (DECL_NAMESPACE_SCOPE_P (fn))
7827 3 : inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
7828 : else
7829 4 : inform (DECL_SOURCE_LOCATION (fn),
7830 : "%qD is a usual (non-placement) deallocation "
7831 : "function", fn);
7832 : }
7833 7 : }
7834 : else
7835 0 : return error_mark_node;
7836 366613 : ok:;
7837 : }
7838 : }
7839 : else
7840 : /* "Any non-placement deallocation function matches a non-placement
7841 : allocation function. If the lookup finds a single matching
7842 : deallocation function, that function will be called; otherwise, no
7843 : deallocation function will be called." */
7844 2378706 : for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns)))
7845 : {
7846 1837318 : dealloc_info di_elt;
7847 1837318 : if (usual_deallocation_fn_p (elt, &di_elt))
7848 : {
7849 : /* If we're called for an EH cleanup in a new-expression, we can't
7850 : use a destroying delete; the exception was thrown before the
7851 : object was constructed. */
7852 1060436 : if (alloc_fn && di_elt.destroying)
7853 : {
7854 5 : excluded_destroying = elt;
7855 533016 : continue;
7856 : }
7857 :
7858 1060431 : if (!fn)
7859 : {
7860 270677 : fn = elt;
7861 270677 : di_fn = di_elt;
7862 270677 : continue;
7863 : }
7864 :
7865 : /* -- If any of the deallocation functions is a destroying
7866 : operator delete, all deallocation functions that are not
7867 : destroying operator deletes are eliminated from further
7868 : consideration. */
7869 789754 : if (di_elt.destroying != di_fn.destroying)
7870 : {
7871 4 : if (di_elt.destroying)
7872 : {
7873 2 : fn = elt;
7874 2 : di_fn = di_elt;
7875 : }
7876 4 : continue;
7877 : }
7878 :
7879 : /* -- If the type has new-extended alignment, a function with a
7880 : parameter of type std::align_val_t is preferred; otherwise a
7881 : function without such a parameter is preferred. If exactly one
7882 : preferred function is found, that function is selected and the
7883 : selection process terminates. If more than one preferred
7884 : function is found, all non-preferred functions are eliminated
7885 : from further consideration. */
7886 789750 : if (aligned_new_threshold)
7887 : {
7888 786974 : bool want_align = type_has_new_extended_alignment (type);
7889 786974 : if (di_elt.aligned != di_fn.aligned)
7890 : {
7891 262330 : if (want_align == di_elt.aligned)
7892 : {
7893 262312 : fn = elt;
7894 262312 : di_fn = di_elt;
7895 : }
7896 262330 : continue;
7897 : }
7898 : }
7899 :
7900 : /* -- If the deallocation functions have class scope, the one
7901 : without a parameter of type std::size_t is selected. */
7902 527420 : bool want_size;
7903 527420 : if (DECL_CLASS_SCOPE_P (fn))
7904 : want_size = false;
7905 :
7906 : /* -- If the type is complete and if, for the second alternative
7907 : (delete array) only, the operand is a pointer to a class type
7908 : with a non-trivial destructor or a (possibly multi-dimensional)
7909 : array thereof, the function with a parameter of type std::size_t
7910 : is selected.
7911 :
7912 : -- Otherwise, it is unspecified whether a deallocation function
7913 : with a parameter of type std::size_t is selected. */
7914 : else
7915 : {
7916 527291 : want_size = COMPLETE_TYPE_P (type);
7917 527291 : if (code == VEC_DELETE_EXPR
7918 527291 : && !TYPE_VEC_NEW_USES_COOKIE (type))
7919 : /* We need a cookie to determine the array size. */
7920 : want_size = false;
7921 : }
7922 527420 : gcc_assert (di_fn.sized != di_elt.sized);
7923 527420 : if (want_size == di_elt.sized)
7924 : {
7925 24310 : fn = elt;
7926 24310 : di_fn = di_elt;
7927 : }
7928 : }
7929 : }
7930 :
7931 : /* If we have a matching function, call it. */
7932 366613 : if (fn)
7933 : {
7934 366596 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7935 :
7936 : /* If the FN is a member function, make sure that it is
7937 : accessible. */
7938 366596 : if (BASELINK_P (fns))
7939 766 : perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn,
7940 : complain);
7941 :
7942 : /* Core issue 901: It's ok to new a type with deleted delete. */
7943 366596 : if (DECL_DELETED_FN (fn) && alloc_fn)
7944 : return NULL_TREE;
7945 :
7946 366590 : tree ret;
7947 366590 : if (placement)
7948 : {
7949 : /* The placement args might not be suitable for overload
7950 : resolution at this point, so build the call directly. */
7951 95919 : int nargs = call_expr_nargs (placement);
7952 95919 : tree *argarray = XALLOCAVEC (tree, nargs);
7953 95919 : int i;
7954 95919 : argarray[0] = addr;
7955 191886 : for (i = 1; i < nargs; i++)
7956 95967 : argarray[i] = CALL_EXPR_ARG (placement, i);
7957 95919 : if (!mark_used (fn, complain) && !(complain & tf_error))
7958 0 : return error_mark_node;
7959 95919 : ret = build_cxx_call (fn, nargs, argarray, complain);
7960 : }
7961 : else
7962 : {
7963 270671 : tree destroying = di_fn.destroying;
7964 270671 : if (destroying)
7965 : {
7966 : /* Strip const and volatile from addr but retain the type of the
7967 : object. */
7968 9 : tree rtype = TREE_TYPE (TREE_TYPE (oaddr));
7969 9 : rtype = cv_unqualified (rtype);
7970 9 : rtype = TYPE_POINTER_TO (rtype);
7971 9 : addr = cp_convert (rtype, oaddr, complain);
7972 9 : destroying = build_functional_cast (input_location,
7973 : destroying, NULL_TREE,
7974 : complain);
7975 : }
7976 :
7977 270671 : releasing_vec args;
7978 270671 : args->quick_push (addr);
7979 270671 : if (destroying)
7980 9 : args->quick_push (destroying);
7981 270671 : if (di_fn.sized)
7982 252806 : args->quick_push (size);
7983 270671 : if (di_fn.aligned)
7984 : {
7985 10 : tree al = build_int_cst (align_type_node, TYPE_ALIGN_UNIT (type));
7986 10 : args->quick_push (al);
7987 : }
7988 270671 : ret = cp_build_function_call_vec (fn, &args, complain);
7989 270671 : }
7990 :
7991 : /* Set this flag for all callers of this function. In addition to
7992 : delete-expressions, this is called for deallocating coroutine state;
7993 : treat that as an implicit delete-expression. This is also called for
7994 : the delete if the constructor throws in a new-expression, and for a
7995 : deleting destructor (which implements a delete-expression). */
7996 : /* But leave this flag off for destroying delete to avoid wrong
7997 : assumptions in the optimizers. */
7998 366590 : tree call = extract_call_expr (ret);
7999 366590 : if (TREE_CODE (call) == CALL_EXPR && !destroying_delete_p (fn))
8000 366575 : CALL_FROM_NEW_OR_DELETE_P (call) = 1;
8001 :
8002 366590 : return ret;
8003 : }
8004 :
8005 : /* If there's only a destroying delete that we can't use because the
8006 : object isn't constructed yet, and we used global new, use global
8007 : delete as well. */
8008 17 : if (excluded_destroying
8009 17 : && DECL_NAMESPACE_SCOPE_P (alloc_fn))
8010 3 : return build_op_delete_call (code, addr, size, true, placement,
8011 3 : alloc_fn, complain);
8012 :
8013 : /* [expr.new]
8014 :
8015 : If no unambiguous matching deallocation function can be found,
8016 : propagating the exception does not cause the object's memory to
8017 : be freed. */
8018 14 : if (alloc_fn)
8019 : {
8020 14 : if ((complain & tf_warning)
8021 14 : && !placement)
8022 : {
8023 14 : bool w = warning (0,
8024 : "no corresponding deallocation function for %qD",
8025 : alloc_fn);
8026 14 : if (w && excluded_destroying)
8027 1 : inform (DECL_SOURCE_LOCATION (excluded_destroying), "destroying "
8028 : "delete %qD cannot be used to release the allocated memory"
8029 : " if the initialization throws because the object is not "
8030 : "constructed yet", excluded_destroying);
8031 : }
8032 14 : return NULL_TREE;
8033 : }
8034 :
8035 0 : if (complain & tf_error)
8036 0 : error ("no suitable %<operator %s%> for %qT",
8037 0 : OVL_OP_INFO (false, code)->name, type);
8038 0 : return error_mark_node;
8039 : }
8040 :
8041 : /* Issue diagnostics about a disallowed access of DECL, using DIAG_DECL
8042 : in the diagnostics.
8043 :
8044 : If ISSUE_ERROR is true, then issue an error about the access, followed
8045 : by a note showing the declaration. Otherwise, just show the note.
8046 :
8047 : DIAG_DECL and DIAG_LOCATION will almost always be the same.
8048 : DIAG_LOCATION is just another DECL. NO_ACCESS_REASON is an optional
8049 : parameter used to specify why DECL wasn't accessible (e.g. ak_private
8050 : would be because DECL was private). If not using NO_ACCESS_REASON,
8051 : then it must be ak_none, and the access failure reason will be
8052 : figured out by looking at the protection of DECL. */
8053 :
8054 : void
8055 1330 : complain_about_access (tree decl, tree diag_decl, tree diag_location,
8056 : bool issue_error, access_kind no_access_reason)
8057 : {
8058 : /* If we have not already figured out why DECL is inaccessible... */
8059 1330 : if (no_access_reason == ak_none)
8060 : {
8061 : /* Examine the access of DECL to find out why. */
8062 1109 : if (TREE_PRIVATE (decl))
8063 : no_access_reason = ak_private;
8064 259 : else if (TREE_PROTECTED (decl))
8065 : no_access_reason = ak_protected;
8066 : }
8067 :
8068 : /* Now generate an error message depending on calculated access. */
8069 272 : if (no_access_reason == ak_private)
8070 : {
8071 1071 : if (issue_error)
8072 1064 : error ("%q#D is private within this context", diag_decl);
8073 1071 : inform (DECL_SOURCE_LOCATION (diag_location), "declared private here");
8074 : }
8075 259 : else if (no_access_reason == ak_protected)
8076 : {
8077 208 : if (issue_error)
8078 201 : error ("%q#D is protected within this context", diag_decl);
8079 208 : inform (DECL_SOURCE_LOCATION (diag_location), "declared protected here");
8080 : }
8081 : /* Couldn't figure out why DECL is inaccesible, so just say it's
8082 : inaccessible. */
8083 : else
8084 : {
8085 51 : if (issue_error)
8086 51 : error ("%q#D is inaccessible within this context", diag_decl);
8087 51 : inform (DECL_SOURCE_LOCATION (diag_decl), "declared here");
8088 : }
8089 1330 : }
8090 :
8091 : /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
8092 : bitwise or of LOOKUP_* values. If any errors are warnings are
8093 : generated, set *DIAGNOSTIC_FN to "error" or "warning",
8094 : respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
8095 : to NULL. */
8096 :
8097 : static tree
8098 4926691 : build_temp (tree expr, tree type, int flags,
8099 : diagnostic_t *diagnostic_kind, tsubst_flags_t complain)
8100 : {
8101 4926691 : int savew, savee;
8102 :
8103 4926691 : *diagnostic_kind = DK_UNSPECIFIED;
8104 :
8105 : /* If the source is a packed field, calling the copy constructor will require
8106 : binding the field to the reference parameter to the copy constructor, and
8107 : we'll end up with an infinite loop. If we can use a bitwise copy, then
8108 : do that now. */
8109 4926691 : if ((lvalue_kind (expr) & clk_packed)
8110 8 : && CLASS_TYPE_P (TREE_TYPE (expr))
8111 4926699 : && !type_has_nontrivial_copy_init (TREE_TYPE (expr)))
8112 4 : return get_target_expr (expr, complain);
8113 :
8114 : /* In decltype, we might have decided not to wrap this call in a TARGET_EXPR.
8115 : But it turns out to be a subexpression, so perform temporary
8116 : materialization now. */
8117 4926687 : if (TREE_CODE (expr) == CALL_EXPR
8118 10 : && CLASS_TYPE_P (type)
8119 4926697 : && same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
8120 10 : expr = build_cplus_new (type, expr, complain);
8121 :
8122 4926687 : savew = warningcount + werrorcount, savee = errorcount;
8123 4926687 : releasing_vec args (make_tree_vector_single (expr));
8124 4926687 : expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
8125 : &args, type, flags, complain);
8126 4926687 : if (warningcount + werrorcount > savew)
8127 0 : *diagnostic_kind = DK_WARNING;
8128 4926687 : else if (errorcount > savee)
8129 59 : *diagnostic_kind = DK_ERROR;
8130 4926687 : return expr;
8131 4926691 : }
8132 :
8133 : /* Get any location for EXPR, falling back to input_location.
8134 :
8135 : If the result is in a system header and is the virtual location for
8136 : a token coming from the expansion of a macro, unwind it to the
8137 : location of the expansion point of the macro (e.g. to avoid the
8138 : diagnostic being suppressed for expansions of NULL where "NULL" is
8139 : in a system header). */
8140 :
8141 : static location_t
8142 1098507 : get_location_for_expr_unwinding_for_system_header (tree expr)
8143 : {
8144 1098507 : location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
8145 1098507 : loc = expansion_point_location_if_in_system_header (loc);
8146 1098507 : return loc;
8147 : }
8148 :
8149 : /* Perform warnings about peculiar, but valid, conversions from/to NULL.
8150 : Also handle a subset of zero as null warnings.
8151 : EXPR is implicitly converted to type TOTYPE.
8152 : FN and ARGNUM are used for diagnostics. */
8153 :
8154 : static void
8155 231487335 : conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
8156 : {
8157 : /* Issue warnings about peculiar, but valid, uses of NULL. */
8158 231487335 : if (TREE_CODE (totype) != BOOLEAN_TYPE
8159 134999101 : && ARITHMETIC_TYPE_P (totype)
8160 306486953 : && null_node_p (expr))
8161 : {
8162 125 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8163 125 : if (fn)
8164 : {
8165 44 : auto_diagnostic_group d;
8166 44 : if (warning_at (loc, OPT_Wconversion_null,
8167 : "passing NULL to non-pointer argument %P of %qD",
8168 : argnum, fn))
8169 36 : inform (get_fndecl_argument_location (fn, argnum),
8170 : " declared here");
8171 44 : }
8172 : else
8173 81 : warning_at (loc, OPT_Wconversion_null,
8174 : "converting to non-pointer type %qT from NULL", totype);
8175 : }
8176 :
8177 : /* Issue warnings if "false" is converted to a NULL pointer */
8178 231487210 : else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
8179 231487210 : && TYPE_PTR_P (totype))
8180 : {
8181 8 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8182 8 : if (fn)
8183 : {
8184 4 : auto_diagnostic_group d;
8185 4 : if (warning_at (loc, OPT_Wconversion_null,
8186 : "converting %<false%> to pointer type for argument "
8187 : "%P of %qD", argnum, fn))
8188 4 : inform (get_fndecl_argument_location (fn, argnum),
8189 : " declared here");
8190 4 : }
8191 : else
8192 4 : warning_at (loc, OPT_Wconversion_null,
8193 : "converting %<false%> to pointer type %qT", totype);
8194 : }
8195 : /* Handle zero as null pointer warnings for cases other
8196 : than EQ_EXPR and NE_EXPR */
8197 212508691 : else if ((TYPE_PTR_OR_PTRMEM_P (totype) || NULLPTR_TYPE_P (totype))
8198 231672886 : && null_ptr_cst_p (expr))
8199 : {
8200 1098374 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8201 1098374 : maybe_warn_zero_as_null_pointer_constant (expr, loc);
8202 : }
8203 231487335 : }
8204 :
8205 : /* We gave a diagnostic during a conversion. If this was in the second
8206 : standard conversion sequence of a user-defined conversion sequence, say
8207 : which user-defined conversion. */
8208 :
8209 : static void
8210 5231 : maybe_print_user_conv_context (conversion *convs)
8211 : {
8212 5231 : if (convs->user_conv_p)
8213 145 : for (conversion *t = convs; t; t = next_conversion (t))
8214 126 : if (t->kind == ck_user)
8215 : {
8216 42 : print_z_candidate (0, N_(" after user-defined conversion:"),
8217 : t->cand);
8218 42 : break;
8219 : }
8220 5231 : }
8221 :
8222 : /* Locate the parameter with the given index within FNDECL.
8223 : ARGNUM is zero based, -1 indicates the `this' argument of a method.
8224 : Return the location of the FNDECL itself if there are problems. */
8225 :
8226 : location_t
8227 4390354 : get_fndecl_argument_location (tree fndecl, int argnum)
8228 : {
8229 : /* The locations of implicitly-declared functions are likely to be
8230 : more meaningful than those of their parameters. */
8231 4390354 : if (DECL_ARTIFICIAL (fndecl))
8232 304 : return DECL_SOURCE_LOCATION (fndecl);
8233 :
8234 4390050 : int i;
8235 4390050 : tree param;
8236 :
8237 : /* Locate param by index within DECL_ARGUMENTS (fndecl). */
8238 4390050 : for (i = 0, param = FUNCTION_FIRST_USER_PARM (fndecl);
8239 9996065 : i < argnum && param;
8240 5606015 : i++, param = TREE_CHAIN (param))
8241 : ;
8242 :
8243 : /* If something went wrong (e.g. if we have a builtin and thus no arguments),
8244 : return the location of FNDECL. */
8245 4390050 : if (param == NULL)
8246 34 : return DECL_SOURCE_LOCATION (fndecl);
8247 :
8248 4390016 : return DECL_SOURCE_LOCATION (param);
8249 : }
8250 :
8251 : /* If FNDECL is non-NULL, issue a note highlighting ARGNUM
8252 : within its declaration (or the fndecl itself if something went
8253 : wrong). */
8254 :
8255 : void
8256 6957 : maybe_inform_about_fndecl_for_bogus_argument_init (tree fn, int argnum)
8257 : {
8258 6957 : if (fn)
8259 1133 : inform (get_fndecl_argument_location (fn, argnum),
8260 : " initializing argument %P of %qD", argnum, fn);
8261 6957 : }
8262 :
8263 : /* Maybe warn about C++20 Conversions to arrays of unknown bound. C is
8264 : the conversion, EXPR is the expression we're converting. */
8265 :
8266 : static void
8267 15526151 : maybe_warn_array_conv (location_t loc, conversion *c, tree expr)
8268 : {
8269 15526151 : if (cxx_dialect >= cxx20)
8270 : return;
8271 :
8272 14460850 : tree type = TREE_TYPE (expr);
8273 14460850 : type = strip_pointer_operator (type);
8274 :
8275 14460850 : if (TREE_CODE (type) != ARRAY_TYPE
8276 14460850 : || TYPE_DOMAIN (type) == NULL_TREE)
8277 : return;
8278 :
8279 11099 : if (pedantic && conv_binds_to_array_of_unknown_bound (c))
8280 11 : pedwarn (loc, OPT_Wc__20_extensions,
8281 : "conversions to arrays of unknown bound "
8282 : "are only available with %<-std=c++20%> or %<-std=gnu++20%>");
8283 : }
8284 :
8285 : /* We call this recursively in convert_like_internal. */
8286 : static tree convert_like (conversion *, tree, tree, int, bool, bool, bool,
8287 : tsubst_flags_t);
8288 :
8289 : /* Perform the conversions in CONVS on the expression EXPR. FN and
8290 : ARGNUM are used for diagnostics. ARGNUM is zero based, -1
8291 : indicates the `this' argument of a method. INNER is nonzero when
8292 : being called to continue a conversion chain. It is negative when a
8293 : reference binding will be applied, positive otherwise. If
8294 : ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
8295 : conversions will be emitted if appropriate. If C_CAST_P is true,
8296 : this conversion is coming from a C-style cast; in that case,
8297 : conversions to inaccessible bases are permitted. */
8298 :
8299 : static tree
8300 458367806 : convert_like_internal (conversion *convs, tree expr, tree fn, int argnum,
8301 : bool issue_conversion_warnings, bool c_cast_p,
8302 : bool nested_p, tsubst_flags_t complain)
8303 : {
8304 458367806 : tree totype = convs->type;
8305 458367806 : diagnostic_t diag_kind;
8306 458367806 : int flags;
8307 458367806 : location_t loc = cp_expr_loc_or_input_loc (expr);
8308 :
8309 458367806 : if (convs->bad_p && !(complain & tf_error))
8310 29702 : return error_mark_node;
8311 :
8312 458338104 : if (convs->bad_p
8313 6977 : && convs->kind != ck_user
8314 6873 : && convs->kind != ck_list
8315 6867 : && convs->kind != ck_ambig
8316 6867 : && (convs->kind != ck_ref_bind
8317 5264 : || (convs->user_conv_p && next_conversion (convs)->bad_p))
8318 1634 : && (convs->kind != ck_rvalue
8319 23 : || SCALAR_TYPE_P (totype))
8320 458339721 : && convs->kind != ck_base)
8321 : {
8322 1617 : int complained = 0;
8323 1617 : conversion *t = convs;
8324 :
8325 : /* Give a helpful error if this is bad because of excess braces. */
8326 54 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8327 54 : && SCALAR_TYPE_P (totype)
8328 33 : && CONSTRUCTOR_NELTS (expr) > 0
8329 1650 : && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
8330 : {
8331 10 : complained = permerror (loc, "too many braces around initializer "
8332 : "for %qT", totype);
8333 30 : while (BRACE_ENCLOSED_INITIALIZER_P (expr)
8334 50 : && CONSTRUCTOR_NELTS (expr) == 1)
8335 20 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
8336 : }
8337 :
8338 : /* Give a helpful error if this is bad because a conversion to bool
8339 : from std::nullptr_t requires direct-initialization. */
8340 1617 : if (NULLPTR_TYPE_P (TREE_TYPE (expr))
8341 1617 : && TREE_CODE (totype) == BOOLEAN_TYPE)
8342 15 : complained = permerror (loc, "converting to %qH from %qI requires "
8343 : "direct-initialization",
8344 15 : totype, TREE_TYPE (expr));
8345 :
8346 1617 : if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (expr))
8347 40 : && SCALAR_FLOAT_TYPE_P (totype)
8348 1657 : && (extended_float_type_p (TREE_TYPE (expr))
8349 24 : || extended_float_type_p (totype)))
8350 40 : switch (cp_compare_floating_point_conversion_ranks (TREE_TYPE (expr),
8351 : totype))
8352 : {
8353 40 : case 2:
8354 40 : if (pedwarn (loc, 0, "converting to %qH from %qI with greater "
8355 40 : "conversion rank", totype, TREE_TYPE (expr)))
8356 : complained = 1;
8357 28 : else if (!complained)
8358 28 : complained = -1;
8359 : break;
8360 0 : case 3:
8361 0 : if (pedwarn (loc, 0, "converting to %qH from %qI with unordered "
8362 0 : "conversion ranks", totype, TREE_TYPE (expr)))
8363 : complained = 1;
8364 0 : else if (!complained)
8365 0 : complained = -1;
8366 : break;
8367 : default:
8368 : break;
8369 : }
8370 :
8371 3301 : for (; t ; t = next_conversion (t))
8372 : {
8373 3292 : if (t->kind == ck_user && t->cand->reason)
8374 : {
8375 51 : auto_diagnostic_group d;
8376 51 : complained = permerror (loc, "invalid user-defined conversion "
8377 51 : "from %qH to %qI", TREE_TYPE (expr),
8378 : totype);
8379 51 : if (complained)
8380 51 : print_z_candidate (loc, N_("candidate is:"), t->cand);
8381 51 : expr = convert_like (t, expr, fn, argnum,
8382 : /*issue_conversion_warnings=*/false,
8383 : /*c_cast_p=*/false, /*nested_p=*/true,
8384 : complain);
8385 51 : }
8386 3241 : else if (t->kind == ck_user || !t->bad_p)
8387 : {
8388 1608 : expr = convert_like (t, expr, fn, argnum,
8389 : /*issue_conversion_warnings=*/false,
8390 : /*c_cast_p=*/false, /*nested_p=*/true,
8391 : complain);
8392 1608 : if (t->bad_p)
8393 : complained = 1;
8394 : break;
8395 : }
8396 1633 : else if (t->kind == ck_ambig)
8397 0 : return convert_like (t, expr, fn, argnum,
8398 : /*issue_conversion_warnings=*/false,
8399 : /*c_cast_p=*/false, /*nested_p=*/true,
8400 0 : complain);
8401 1633 : else if (t->kind == ck_identity)
8402 : break;
8403 : }
8404 1605 : if (!complained && expr != error_mark_node)
8405 : {
8406 1489 : range_label_for_type_mismatch label (TREE_TYPE (expr), totype);
8407 1489 : gcc_rich_location richloc (loc, &label);
8408 1489 : complained = permerror (&richloc,
8409 : "invalid conversion from %qH to %qI",
8410 1489 : TREE_TYPE (expr), totype);
8411 1489 : }
8412 1617 : if (convs->kind == ck_ref_bind)
8413 31 : expr = convert_to_reference (totype, expr, CONV_IMPLICIT,
8414 : LOOKUP_NORMAL, NULL_TREE,
8415 : complain);
8416 : else
8417 1586 : expr = cp_convert (totype, expr, complain);
8418 1617 : if (complained == 1)
8419 1518 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
8420 1617 : return expr;
8421 : }
8422 :
8423 458336487 : if (issue_conversion_warnings && (complain & tf_warning))
8424 231487335 : conversion_null_warnings (totype, expr, fn, argnum);
8425 :
8426 458336487 : switch (convs->kind)
8427 : {
8428 2271478 : case ck_user:
8429 2271478 : {
8430 2271478 : struct z_candidate *cand = convs->cand;
8431 :
8432 2271478 : if (cand == NULL)
8433 : /* We chose the surrogate function from add_conv_candidate, now we
8434 : actually need to build the conversion. */
8435 57 : cand = build_user_type_conversion_1 (totype, expr,
8436 : LOOKUP_NO_CONVERSION, complain);
8437 :
8438 2271478 : tree convfn = cand->fn;
8439 :
8440 : /* When converting from an init list we consider explicit
8441 : constructors, but actually trying to call one is an error. */
8442 2316660 : if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
8443 267 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
8444 : /* Unless this is for direct-list-initialization. */
8445 263 : && (!CONSTRUCTOR_IS_DIRECT_INIT (expr) || convs->need_temporary_p)
8446 : /* And in C++98 a default constructor can't be explicit. */
8447 2271653 : && cxx_dialect >= cxx11)
8448 : {
8449 174 : if (!(complain & tf_error))
8450 17 : return error_mark_node;
8451 157 : location_t loc = location_of (expr);
8452 157 : if (CONSTRUCTOR_NELTS (expr) == 0
8453 157 : && FUNCTION_FIRST_USER_PARMTYPE (convfn) != void_list_node)
8454 : {
8455 9 : auto_diagnostic_group d;
8456 9 : if (pedwarn (loc, 0, "converting to %qT from initializer list "
8457 : "would use explicit constructor %qD",
8458 : totype, convfn))
8459 9 : inform (loc, "in C++11 and above a default constructor "
8460 : "can be explicit");
8461 9 : }
8462 : else
8463 148 : error ("converting to %qT from initializer list would use "
8464 : "explicit constructor %qD", totype, convfn);
8465 : }
8466 :
8467 : /* If we're initializing from {}, it's value-initialization. */
8468 337528 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8469 337528 : && CONSTRUCTOR_NELTS (expr) == 0
8470 64733 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype)
8471 2336164 : && !processing_template_decl)
8472 : {
8473 64703 : bool direct = CONSTRUCTOR_IS_DIRECT_INIT (expr);
8474 64703 : if (abstract_virtuals_error (NULL_TREE, totype, complain))
8475 15 : return error_mark_node;
8476 64688 : expr = build_value_init (totype, complain);
8477 64688 : expr = get_target_expr (expr, complain);
8478 64688 : if (expr != error_mark_node)
8479 : {
8480 64684 : TARGET_EXPR_LIST_INIT_P (expr) = true;
8481 64684 : TARGET_EXPR_DIRECT_INIT_P (expr) = direct;
8482 : }
8483 64688 : return expr;
8484 : }
8485 :
8486 : /* We don't know here whether EXPR is being used as an lvalue or
8487 : rvalue, but we know it's read. */
8488 2206758 : mark_exp_read (expr);
8489 :
8490 : /* Pass LOOKUP_NO_CONVERSION so rvalue/base handling knows not to allow
8491 : any more UDCs. */
8492 2206758 : expr = build_over_call (cand, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
8493 : complain);
8494 :
8495 : /* If this is a constructor or a function returning an aggr type,
8496 : we need to build up a TARGET_EXPR. */
8497 4413516 : if (DECL_CONSTRUCTOR_P (convfn))
8498 : {
8499 620651 : expr = build_cplus_new (totype, expr, complain);
8500 :
8501 : /* Remember that this was list-initialization. */
8502 620651 : if (convs->check_narrowing && expr != error_mark_node)
8503 273356 : TARGET_EXPR_LIST_INIT_P (expr) = true;
8504 : }
8505 :
8506 : return expr;
8507 : }
8508 337494405 : case ck_identity:
8509 337494405 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
8510 : {
8511 468211 : int nelts = CONSTRUCTOR_NELTS (expr);
8512 90808 : if (nelts == 0)
8513 377403 : expr = build_value_init (totype, complain);
8514 90808 : else if (nelts == 1)
8515 90808 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
8516 : else
8517 0 : gcc_unreachable ();
8518 : }
8519 337494405 : expr = mark_use (expr, /*rvalue_p=*/!convs->rvaluedness_matches_p,
8520 : /*read_p=*/true, UNKNOWN_LOCATION,
8521 : /*reject_builtin=*/true);
8522 :
8523 337494405 : if (type_unknown_p (expr))
8524 10463 : expr = instantiate_type (totype, expr, complain);
8525 337494405 : if (!nested_p && TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8526 5801 : expr = cp_convert (totype, TREE_OPERAND (expr, 0), complain);
8527 337494405 : if (expr == null_node
8528 337494405 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype))
8529 : /* If __null has been converted to an integer type, we do not want to
8530 : continue to warn about uses of EXPR as an integer, rather than as a
8531 : pointer. */
8532 56783 : expr = build_int_cst (totype, 0);
8533 : return expr;
8534 48 : case ck_ambig:
8535 : /* We leave bad_p off ck_ambig because overload resolution considers
8536 : it valid, it just fails when we try to perform it. So we need to
8537 : check complain here, too. */
8538 48 : if (complain & tf_error)
8539 : {
8540 : /* Call build_user_type_conversion again for the error. */
8541 96 : int flags = (convs->need_temporary_p
8542 48 : ? LOOKUP_IMPLICIT : LOOKUP_NORMAL);
8543 48 : build_user_type_conversion (totype, convs->u.expr, flags, complain);
8544 48 : gcc_assert (seen_error ());
8545 48 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
8546 : }
8547 48 : return error_mark_node;
8548 :
8549 2134 : case ck_list:
8550 2134 : {
8551 : /* Conversion to std::initializer_list<T>. */
8552 2134 : tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
8553 2134 : unsigned len = CONSTRUCTOR_NELTS (expr);
8554 2134 : tree array;
8555 :
8556 2134 : if (tree init = maybe_init_list_as_array (elttype, expr))
8557 : {
8558 109 : elttype = cp_build_qualified_type
8559 109 : (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
8560 109 : array = build_array_of_n_type (elttype, len);
8561 109 : array = build_vec_init_expr (array, init, complain);
8562 109 : array = get_target_expr (array);
8563 109 : array = cp_build_addr_expr (array, complain);
8564 : }
8565 2025 : else if (len)
8566 : {
8567 1962 : tree val; unsigned ix;
8568 :
8569 1962 : tree new_ctor = build_constructor (init_list_type_node, NULL);
8570 :
8571 : /* Convert all the elements. */
8572 8347 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
8573 : {
8574 6394 : tree sub = convert_like (convs->u.list[ix], val, fn,
8575 : argnum, false, false,
8576 : /*nested_p=*/true, complain);
8577 6394 : if (sub == error_mark_node)
8578 9 : return sub;
8579 1010 : if (!BRACE_ENCLOSED_INITIALIZER_P (val)
8580 6396 : && !check_narrowing (TREE_TYPE (sub), val, complain))
8581 0 : return error_mark_node;
8582 6385 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor),
8583 : NULL_TREE, sub);
8584 6385 : if (!TREE_CONSTANT (sub))
8585 1729 : TREE_CONSTANT (new_ctor) = false;
8586 : }
8587 : /* Build up the array. */
8588 1953 : elttype = cp_build_qualified_type
8589 1953 : (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
8590 1953 : array = build_array_of_n_type (elttype, len);
8591 1953 : array = finish_compound_literal (array, new_ctor, complain);
8592 : /* This is dubious now, should be blessed by P2752. */
8593 1953 : DECL_MERGEABLE (TARGET_EXPR_SLOT (array)) = true;
8594 : /* Take the address explicitly rather than via decay_conversion
8595 : to avoid the error about taking the address of a temporary. */
8596 1953 : array = cp_build_addr_expr (array, complain);
8597 : }
8598 : else
8599 63 : array = nullptr_node;
8600 :
8601 2125 : array = cp_convert (build_pointer_type (elttype), array, complain);
8602 2125 : if (array == error_mark_node)
8603 : return error_mark_node;
8604 :
8605 : /* Build up the initializer_list object. Note: fail gracefully
8606 : if the object cannot be completed because, for example, no
8607 : definition is provided (c++/80956). */
8608 2125 : totype = complete_type_or_maybe_complain (totype, NULL_TREE, complain);
8609 2125 : if (!totype)
8610 3 : return error_mark_node;
8611 2122 : tree field = next_aggregate_field (TYPE_FIELDS (totype));
8612 2122 : vec<constructor_elt, va_gc> *vec = NULL;
8613 2122 : CONSTRUCTOR_APPEND_ELT (vec, field, array);
8614 2122 : field = next_aggregate_field (DECL_CHAIN (field));
8615 2122 : CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
8616 2122 : tree new_ctor = build_constructor (totype, vec);
8617 2122 : return get_target_expr (new_ctor, complain);
8618 : }
8619 :
8620 668586 : case ck_aggr:
8621 668586 : if (TREE_CODE (totype) == COMPLEX_TYPE)
8622 : {
8623 20628 : tree real = CONSTRUCTOR_ELT (expr, 0)->value;
8624 20628 : tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
8625 20628 : real = perform_implicit_conversion (TREE_TYPE (totype),
8626 : real, complain);
8627 20628 : imag = perform_implicit_conversion (TREE_TYPE (totype),
8628 : imag, complain);
8629 20628 : expr = build2 (COMPLEX_EXPR, totype, real, imag);
8630 20628 : return expr;
8631 : }
8632 647958 : expr = reshape_init (totype, expr, complain);
8633 647958 : expr = get_target_expr (digest_init (totype, expr, complain),
8634 : complain);
8635 647958 : if (expr != error_mark_node)
8636 647949 : TARGET_EXPR_LIST_INIT_P (expr) = true;
8637 : return expr;
8638 :
8639 117899836 : default:
8640 117899836 : break;
8641 117899836 : };
8642 :
8643 117899836 : expr = convert_like (next_conversion (convs), expr, fn, argnum,
8644 : convs->kind == ck_ref_bind
8645 117899836 : ? issue_conversion_warnings : false,
8646 : c_cast_p, /*nested_p=*/true, complain & ~tf_no_cleanup);
8647 117899836 : if (expr == error_mark_node)
8648 : return error_mark_node;
8649 :
8650 117899642 : switch (convs->kind)
8651 : {
8652 62194650 : case ck_rvalue:
8653 62194650 : expr = decay_conversion (expr, complain);
8654 62194650 : if (expr == error_mark_node)
8655 : {
8656 19 : if (complain & tf_error)
8657 : {
8658 16 : auto_diagnostic_group d;
8659 16 : maybe_print_user_conv_context (convs);
8660 16 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
8661 16 : }
8662 19 : return error_mark_node;
8663 : }
8664 :
8665 66982135 : if (! MAYBE_CLASS_TYPE_P (totype))
8666 : return expr;
8667 :
8668 : /* Don't introduce copies when passing arguments along to the inherited
8669 : constructor. */
8670 4765512 : if (current_function_decl
8671 3375101 : && flag_new_inheriting_ctors
8672 11515216 : && DECL_INHERITED_CTOR (current_function_decl))
8673 : return expr;
8674 :
8675 4764625 : if (TREE_CODE (expr) == TARGET_EXPR
8676 4764625 : && TARGET_EXPR_LIST_INIT_P (expr))
8677 : /* Copy-list-initialization doesn't actually involve a copy. */
8678 : return expr;
8679 :
8680 : /* Fall through. */
8681 5618167 : case ck_base:
8682 5618167 : if (convs->kind == ck_base && !convs->need_temporary_p)
8683 : {
8684 : /* We are going to bind a reference directly to a base-class
8685 : subobject of EXPR. */
8686 : /* Build an expression for `*((base*) &expr)'. */
8687 691476 : expr = convert_to_base (expr, totype,
8688 691476 : !c_cast_p, /*nonnull=*/true, complain);
8689 691476 : return expr;
8690 : }
8691 :
8692 : /* Copy-initialization where the cv-unqualified version of the source
8693 : type is the same class as, or a derived class of, the class of the
8694 : destination [is treated as direct-initialization]. [dcl.init] */
8695 4926691 : flags = LOOKUP_NORMAL;
8696 : /* This conversion is being done in the context of a user-defined
8697 : conversion (i.e. the second step of copy-initialization), so
8698 : don't allow any more. */
8699 4926691 : if (convs->user_conv_p)
8700 67193 : flags |= LOOKUP_NO_CONVERSION;
8701 : /* We might be performing a conversion of the argument
8702 : to the user-defined conversion, i.e., not a conversion of the
8703 : result of the user-defined conversion. In which case we skip
8704 : explicit constructors. */
8705 4926691 : if (convs->copy_init_p)
8706 4863832 : flags |= LOOKUP_ONLYCONVERTING;
8707 4926691 : expr = build_temp (expr, totype, flags, &diag_kind, complain);
8708 4926691 : if (diag_kind && complain)
8709 : {
8710 59 : auto_diagnostic_group d;
8711 59 : maybe_print_user_conv_context (convs);
8712 59 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
8713 59 : }
8714 :
8715 4926691 : return build_cplus_new (totype, expr, complain);
8716 :
8717 16782843 : case ck_ref_bind:
8718 16782843 : {
8719 16782843 : tree ref_type = totype;
8720 :
8721 : /* direct_reference_binding might have inserted a ck_qual under
8722 : this ck_ref_bind for the benefit of conversion sequence ranking.
8723 : Ignore the conversion; we'll create our own below. */
8724 16782843 : if (next_conversion (convs)->kind == ck_qual
8725 16782843 : && !convs->need_temporary_p)
8726 : {
8727 2906 : gcc_assert (same_type_p (TREE_TYPE (expr),
8728 : next_conversion (convs)->type));
8729 : /* Strip the cast created by the ck_qual; cp_build_addr_expr
8730 : below expects an lvalue. */
8731 2906 : STRIP_NOPS (expr);
8732 : }
8733 :
8734 16782843 : if (convs->bad_p && !next_conversion (convs)->bad_p)
8735 : {
8736 5156 : tree extype = TREE_TYPE (expr);
8737 5156 : auto_diagnostic_group d;
8738 5156 : if (TYPE_REF_IS_RVALUE (ref_type)
8739 5156 : && lvalue_p (expr))
8740 1634 : error_at (loc, "cannot bind rvalue reference of type %qH to "
8741 : "lvalue of type %qI", totype, extype);
8742 6204 : else if (!TYPE_REF_IS_RVALUE (ref_type) && !lvalue_p (expr)
8743 4995 : && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
8744 : {
8745 1230 : conversion *next = next_conversion (convs);
8746 1230 : if (next->kind == ck_std)
8747 : {
8748 78 : next = next_conversion (next);
8749 78 : error_at (loc, "cannot bind non-const lvalue reference of "
8750 : "type %qH to a value of type %qI",
8751 : totype, next->type);
8752 : }
8753 1152 : else if (!CP_TYPE_CONST_P (TREE_TYPE (ref_type)))
8754 807 : error_at (loc, "cannot bind non-const lvalue reference of "
8755 : "type %qH to an rvalue of type %qI", totype, extype);
8756 : else // extype is volatile
8757 345 : error_at (loc, "cannot bind lvalue reference of type "
8758 : "%qH to an rvalue of type %qI", totype,
8759 : extype);
8760 : }
8761 2292 : else if (!reference_compatible_p (TREE_TYPE (totype), extype))
8762 : {
8763 : /* If we're converting from T[] to T[N], don't talk
8764 : about discarding qualifiers. (Converting from T[N] to
8765 : T[] is allowed by P0388R4.) */
8766 2292 : if (TREE_CODE (extype) == ARRAY_TYPE
8767 30 : && TYPE_DOMAIN (extype) == NULL_TREE
8768 14 : && TREE_CODE (TREE_TYPE (totype)) == ARRAY_TYPE
8769 2306 : && TYPE_DOMAIN (TREE_TYPE (totype)) != NULL_TREE)
8770 14 : error_at (loc, "cannot bind reference of type %qH to %qI "
8771 : "due to different array bounds", totype, extype);
8772 : else
8773 2278 : error_at (loc, "binding reference of type %qH to %qI "
8774 : "discards qualifiers", totype, extype);
8775 : }
8776 : else
8777 0 : gcc_unreachable ();
8778 5156 : maybe_print_user_conv_context (convs);
8779 5156 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
8780 :
8781 5156 : return error_mark_node;
8782 5156 : }
8783 16777687 : else if (complain & tf_warning)
8784 14946080 : maybe_warn_array_conv (loc, convs, expr);
8785 :
8786 : /* If necessary, create a temporary.
8787 :
8788 : VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
8789 : that need temporaries, even when their types are reference
8790 : compatible with the type of reference being bound, so the
8791 : upcoming call to cp_build_addr_expr doesn't fail. */
8792 16777687 : if (convs->need_temporary_p
8793 15535060 : || TREE_CODE (expr) == CONSTRUCTOR
8794 15535059 : || TREE_CODE (expr) == VA_ARG_EXPR)
8795 : {
8796 : /* Otherwise, a temporary of type "cv1 T1" is created and
8797 : initialized from the initializer expression using the rules
8798 : for a non-reference copy-initialization (8.5). */
8799 :
8800 1242628 : tree type = TREE_TYPE (ref_type);
8801 1242628 : cp_lvalue_kind lvalue = lvalue_kind (expr);
8802 :
8803 1242628 : gcc_assert (similar_type_p (type, next_conversion (convs)->type));
8804 1242628 : if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
8805 1757863 : && !TYPE_REF_IS_RVALUE (ref_type))
8806 : {
8807 : /* If the reference is volatile or non-const, we
8808 : cannot create a temporary. */
8809 111 : if (complain & tf_error)
8810 : {
8811 110 : if (lvalue & clk_bitfield)
8812 34 : error_at (loc, "cannot bind bit-field %qE to %qT",
8813 : expr, ref_type);
8814 76 : else if (lvalue & clk_packed)
8815 12 : error_at (loc, "cannot bind packed field %qE to %qT",
8816 : expr, ref_type);
8817 : else
8818 64 : error_at (loc, "cannot bind rvalue %qE to %qT",
8819 : expr, ref_type);
8820 : }
8821 111 : return error_mark_node;
8822 : }
8823 : /* If the source is a packed field, and we must use a copy
8824 : constructor, then building the target expr will require
8825 : binding the field to the reference parameter to the
8826 : copy constructor, and we'll end up with an infinite
8827 : loop. If we can use a bitwise copy, then we'll be
8828 : OK. */
8829 1242517 : if ((lvalue & clk_packed)
8830 24 : && CLASS_TYPE_P (type)
8831 1242537 : && type_has_nontrivial_copy_init (type))
8832 : {
8833 8 : error_at (loc, "cannot bind packed field %qE to %qT",
8834 : expr, ref_type);
8835 8 : return error_mark_node;
8836 : }
8837 1242509 : if (lvalue & clk_bitfield)
8838 : {
8839 31 : expr = convert_bitfield_to_declared_type (expr);
8840 31 : expr = fold_convert (type, expr);
8841 : }
8842 :
8843 : /* Creating &TARGET_EXPR<> in a template would break when
8844 : tsubsting the expression, so use an IMPLICIT_CONV_EXPR
8845 : instead. This can happen even when there's no class
8846 : involved, e.g., when converting an integer to a reference
8847 : type. */
8848 1242509 : if (processing_template_decl)
8849 219 : return build1 (IMPLICIT_CONV_EXPR, totype, expr);
8850 1242290 : expr = build_target_expr_with_type (expr, type, complain);
8851 : }
8852 :
8853 : /* Take the address of the thing to which we will bind the
8854 : reference. */
8855 16777349 : expr = cp_build_addr_expr (expr, complain);
8856 16777349 : if (expr == error_mark_node)
8857 : return error_mark_node;
8858 :
8859 : /* Convert it to a pointer to the type referred to by the
8860 : reference. This will adjust the pointer if a derived to
8861 : base conversion is being performed. */
8862 16777349 : expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
8863 : expr, complain);
8864 : /* Convert the pointer to the desired reference type. */
8865 16777349 : return build_nop (ref_type, expr);
8866 : }
8867 :
8868 1642518 : case ck_lvalue:
8869 1642518 : return decay_conversion (expr, complain);
8870 :
8871 232632 : case ck_fnptr:
8872 : /* ??? Should the address of a transaction-safe pointer point to the TM
8873 : clone, and this conversion look up the primary function? */
8874 232632 : return build_nop (totype, expr);
8875 :
8876 625022 : case ck_qual:
8877 : /* Warn about deprecated conversion if appropriate. */
8878 625022 : if (complain & tf_warning)
8879 : {
8880 580071 : string_conv_p (totype, expr, 1);
8881 580071 : maybe_warn_array_conv (loc, convs, expr);
8882 : }
8883 : break;
8884 :
8885 1383243 : case ck_ptr:
8886 1383243 : if (convs->base_p)
8887 113419 : expr = convert_to_base (expr, totype, !c_cast_p,
8888 : /*nonnull=*/false, complain);
8889 1383243 : return build_nop (totype, expr);
8890 :
8891 21053 : case ck_pmem:
8892 21053 : return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
8893 21053 : c_cast_p, complain);
8894 :
8895 : default:
8896 : break;
8897 : }
8898 :
8899 34786721 : if (convs->check_narrowing
8900 39433115 : && !check_narrowing (totype, expr, complain,
8901 4646394 : convs->check_narrowing_const_only))
8902 388 : return error_mark_node;
8903 :
8904 34786333 : warning_sentinel w (warn_zero_as_null_pointer_constant);
8905 34786333 : if (issue_conversion_warnings)
8906 19621197 : expr = cp_convert_and_check (totype, expr, complain);
8907 : else
8908 : {
8909 15165136 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8910 36 : expr = TREE_OPERAND (expr, 0);
8911 15165136 : expr = cp_convert (totype, expr, complain);
8912 : }
8913 :
8914 34786333 : return expr;
8915 : }
8916 :
8917 : /* Return true if converting FROM to TO is unsafe in a template. */
8918 :
8919 : static bool
8920 965975 : conv_unsafe_in_template_p (tree to, tree from)
8921 : {
8922 : /* Converting classes involves TARGET_EXPR. */
8923 965975 : if (CLASS_TYPE_P (to) || CLASS_TYPE_P (from))
8924 : return true;
8925 :
8926 : /* Converting real to integer produces FIX_TRUNC_EXPR which tsubst
8927 : doesn't handle. */
8928 745844 : if (SCALAR_FLOAT_TYPE_P (from) && INTEGRAL_OR_ENUMERATION_TYPE_P (to))
8929 : return true;
8930 :
8931 : /* Converting integer to real isn't a trivial conversion, either. */
8932 745840 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (from) && SCALAR_FLOAT_TYPE_P (to))
8933 4 : return true;
8934 :
8935 : return false;
8936 : }
8937 :
8938 : /* Wrapper for convert_like_internal that handles creating
8939 : IMPLICIT_CONV_EXPR. */
8940 :
8941 : static tree
8942 458587935 : convert_like (conversion *convs, tree expr, tree fn, int argnum,
8943 : bool issue_conversion_warnings, bool c_cast_p, bool nested_p,
8944 : tsubst_flags_t complain)
8945 : {
8946 : /* Creating &TARGET_EXPR<> in a template breaks when substituting,
8947 : and creating a CALL_EXPR in a template breaks in finish_call_expr
8948 : so use an IMPLICIT_CONV_EXPR for this conversion. We would have
8949 : created such codes e.g. when calling a user-defined conversion
8950 : function. */
8951 458587935 : tree conv_expr = NULL_TREE;
8952 458587935 : if (processing_template_decl
8953 53650792 : && convs->kind != ck_identity
8954 459553910 : && conv_unsafe_in_template_p (convs->type, TREE_TYPE (expr)))
8955 : {
8956 220139 : conv_expr = build1 (IMPLICIT_CONV_EXPR, convs->type, expr);
8957 220139 : if (convs->kind != ck_ref_bind)
8958 27842 : conv_expr = convert_from_reference (conv_expr);
8959 220139 : if (!convs->bad_p)
8960 : return conv_expr;
8961 : /* Do the normal processing to give the bad_p errors. But we still
8962 : need to return the IMPLICIT_CONV_EXPR, unless we're returning
8963 : error_mark_node. */
8964 : }
8965 458367806 : expr = convert_like_internal (convs, expr, fn, argnum,
8966 : issue_conversion_warnings, c_cast_p,
8967 : nested_p, complain);
8968 458367806 : if (expr == error_mark_node)
8969 : return error_mark_node;
8970 458331653 : return conv_expr ? conv_expr : expr;
8971 : }
8972 :
8973 : /* Convenience wrapper for convert_like. */
8974 :
8975 : static inline tree
8976 262862976 : convert_like (conversion *convs, tree expr, tsubst_flags_t complain)
8977 : {
8978 262862976 : return convert_like (convs, expr, NULL_TREE, 0,
8979 : /*issue_conversion_warnings=*/true,
8980 262862976 : /*c_cast_p=*/false, /*nested_p=*/false, complain);
8981 : }
8982 :
8983 : /* Convenience wrapper for convert_like. */
8984 :
8985 : static inline tree
8986 52432598 : convert_like_with_context (conversion *convs, tree expr, tree fn, int argnum,
8987 : tsubst_flags_t complain)
8988 : {
8989 0 : return convert_like (convs, expr, fn, argnum,
8990 : /*issue_conversion_warnings=*/true,
8991 : /*c_cast_p=*/false, /*nested_p=*/false, complain);
8992 : }
8993 :
8994 : /* ARG is being passed to a varargs function. Perform any conversions
8995 : required. Return the converted value. */
8996 :
8997 : tree
8998 1261318 : convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
8999 : {
9000 1261318 : tree arg_type = TREE_TYPE (arg);
9001 1261318 : location_t loc = cp_expr_loc_or_input_loc (arg);
9002 :
9003 : /* [expr.call]
9004 :
9005 : If the argument has integral or enumeration type that is subject
9006 : to the integral promotions (_conv.prom_), or a floating-point
9007 : type that is subject to the floating-point promotion
9008 : (_conv.fpprom_), the value of the argument is converted to the
9009 : promoted type before the call. */
9010 1261318 : if (SCALAR_FLOAT_TYPE_P (arg_type)
9011 66647 : && (TYPE_PRECISION (arg_type)
9012 66647 : < TYPE_PRECISION (double_type_node))
9013 18099 : && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type))
9014 1278680 : && !extended_float_type_p (arg_type))
9015 : {
9016 17362 : if ((complain & tf_warning)
9017 17362 : && warn_double_promotion && !c_inhibit_evaluation_warnings)
9018 4 : warning_at (loc, OPT_Wdouble_promotion,
9019 : "implicit conversion from %qH to %qI when passing "
9020 : "argument to function",
9021 : arg_type, double_type_node);
9022 17362 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
9023 4 : arg = TREE_OPERAND (arg, 0);
9024 17362 : arg = mark_rvalue_use (arg);
9025 17362 : arg = convert_to_real_nofold (double_type_node, arg);
9026 : }
9027 1243956 : else if (NULLPTR_TYPE_P (arg_type))
9028 : {
9029 238 : arg = mark_rvalue_use (arg);
9030 238 : if (TREE_SIDE_EFFECTS (arg))
9031 : {
9032 6 : warning_sentinel w(warn_unused_result);
9033 6 : arg = cp_build_compound_expr (arg, null_pointer_node, complain);
9034 6 : }
9035 : else
9036 232 : arg = null_pointer_node;
9037 : }
9038 1243718 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
9039 : {
9040 959834 : if (SCOPED_ENUM_P (arg_type))
9041 : {
9042 53 : tree prom = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg,
9043 : complain);
9044 53 : prom = cp_perform_integral_promotions (prom, complain);
9045 53 : if (abi_version_crosses (6)
9046 30 : && TYPE_MODE (TREE_TYPE (prom)) != TYPE_MODE (arg_type)
9047 83 : && (complain & tf_warning))
9048 60 : warning_at (loc, OPT_Wabi, "scoped enum %qT passed through %<...%>"
9049 : " as %qT before %<-fabi-version=6%>, %qT after",
9050 : arg_type,
9051 30 : TREE_TYPE (prom), ENUM_UNDERLYING_TYPE (arg_type));
9052 53 : if (!abi_version_at_least (6))
9053 1261318 : arg = prom;
9054 : }
9055 : else
9056 959781 : arg = cp_perform_integral_promotions (arg, complain);
9057 : }
9058 : else
9059 : /* [expr.call]
9060 :
9061 : The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
9062 : standard conversions are performed. */
9063 283884 : arg = decay_conversion (arg, complain);
9064 :
9065 1261318 : arg = require_complete_type (arg, complain);
9066 1261318 : arg_type = TREE_TYPE (arg);
9067 :
9068 1261318 : if (arg != error_mark_node
9069 : /* In a template (or ill-formed code), we can have an incomplete type
9070 : even after require_complete_type, in which case we don't know
9071 : whether it has trivial copy or not. */
9072 1261303 : && COMPLETE_TYPE_P (arg_type)
9073 2522621 : && !cp_unevaluated_operand)
9074 : {
9075 : /* [expr.call] 5.2.2/7:
9076 : Passing a potentially-evaluated argument of class type (Clause 9)
9077 : with a non-trivial copy constructor or a non-trivial destructor
9078 : with no corresponding parameter is conditionally-supported, with
9079 : implementation-defined semantics.
9080 :
9081 : We support it as pass-by-invisible-reference, just like a normal
9082 : value parameter.
9083 :
9084 : If the call appears in the context of a sizeof expression,
9085 : it is not potentially-evaluated. */
9086 459265 : if (type_has_nontrivial_copy_init (arg_type)
9087 459265 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type))
9088 : {
9089 42 : arg = force_rvalue (arg, complain);
9090 42 : if (complain & tf_warning)
9091 42 : warning (OPT_Wconditionally_supported,
9092 : "passing objects of non-trivially-copyable "
9093 : "type %q#T through %<...%> is conditionally supported",
9094 : arg_type);
9095 42 : return build1 (ADDR_EXPR, build_reference_type (arg_type), arg);
9096 : }
9097 : /* Build up a real lvalue-to-rvalue conversion in case the
9098 : copy constructor is trivial but not callable. */
9099 459223 : else if (CLASS_TYPE_P (arg_type))
9100 43924 : force_rvalue (arg, complain);
9101 :
9102 : }
9103 :
9104 : return arg;
9105 : }
9106 :
9107 : /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
9108 :
9109 : tree
9110 31085 : build_x_va_arg (location_t loc, tree expr, tree type)
9111 : {
9112 31085 : if (processing_template_decl)
9113 : {
9114 52 : tree r = build_min (VA_ARG_EXPR, type, expr);
9115 52 : SET_EXPR_LOCATION (r, loc);
9116 52 : return r;
9117 : }
9118 :
9119 31033 : type = complete_type_or_else (type, NULL_TREE);
9120 :
9121 31033 : if (expr == error_mark_node || !type)
9122 : return error_mark_node;
9123 :
9124 31012 : expr = mark_lvalue_use (expr);
9125 :
9126 31012 : if (TYPE_REF_P (type))
9127 : {
9128 8 : error ("cannot receive reference type %qT through %<...%>", type);
9129 8 : return error_mark_node;
9130 : }
9131 :
9132 31004 : if (type_has_nontrivial_copy_init (type)
9133 31004 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
9134 : {
9135 : /* conditionally-supported behavior [expr.call] 5.2.2/7. Let's treat
9136 : it as pass by invisible reference. */
9137 16 : warning_at (loc, OPT_Wconditionally_supported,
9138 : "receiving objects of non-trivially-copyable type %q#T "
9139 : "through %<...%> is conditionally-supported", type);
9140 :
9141 16 : tree ref = cp_build_reference_type (type, false);
9142 16 : expr = build_va_arg (loc, expr, ref);
9143 16 : return convert_from_reference (expr);
9144 : }
9145 :
9146 30988 : tree ret = build_va_arg (loc, expr, type);
9147 30988 : if (CLASS_TYPE_P (type))
9148 : /* Wrap the VA_ARG_EXPR in a TARGET_EXPR now so other code doesn't need to
9149 : know how to handle it. */
9150 12108 : ret = get_target_expr (ret);
9151 : return ret;
9152 : }
9153 :
9154 : /* TYPE has been given to va_arg. Apply the default conversions which
9155 : would have happened when passed via ellipsis. Return the promoted
9156 : type, or the passed type if there is no change. */
9157 :
9158 : tree
9159 1657579 : cxx_type_promotes_to (tree type)
9160 : {
9161 1657579 : tree promote;
9162 :
9163 : /* Perform the array-to-pointer and function-to-pointer
9164 : conversions. */
9165 1657579 : type = type_decays_to (type);
9166 :
9167 1657579 : promote = type_promotes_to (type);
9168 1657579 : if (same_type_p (type, promote))
9169 1657552 : promote = type;
9170 :
9171 1657579 : return promote;
9172 : }
9173 :
9174 : /* ARG is a default argument expression being passed to a parameter of
9175 : the indicated TYPE, which is a parameter to FN. PARMNUM is the
9176 : zero-based argument number. Do any required conversions. Return
9177 : the converted value. */
9178 :
9179 : static GTY(()) vec<tree, va_gc> *default_arg_context;
9180 : void
9181 3838374 : push_defarg_context (tree fn)
9182 3135618 : { vec_safe_push (default_arg_context, fn); }
9183 :
9184 : void
9185 3838374 : pop_defarg_context (void)
9186 3135618 : { default_arg_context->pop (); }
9187 :
9188 : tree
9189 702784 : convert_default_arg (tree type, tree arg, tree fn, int parmnum,
9190 : tsubst_flags_t complain)
9191 : {
9192 702784 : int i;
9193 702784 : tree t;
9194 :
9195 : /* See through clones. */
9196 702784 : fn = DECL_ORIGIN (fn);
9197 : /* And inheriting ctors. */
9198 702784 : if (flag_new_inheriting_ctors)
9199 702144 : fn = strip_inheriting_ctors (fn);
9200 :
9201 : /* Detect recursion. */
9202 703594 : FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t)
9203 818 : if (t == fn)
9204 : {
9205 8 : if (complain & tf_error)
9206 8 : error ("recursive evaluation of default argument for %q#D", fn);
9207 8 : return error_mark_node;
9208 : }
9209 :
9210 : /* If the ARG is an unparsed default argument expression, the
9211 : conversion cannot be performed. */
9212 702776 : if (TREE_CODE (arg) == DEFERRED_PARSE)
9213 : {
9214 20 : if (complain & tf_error)
9215 20 : error ("call to %qD uses the default argument for parameter %P, which "
9216 : "is not yet defined", fn, parmnum);
9217 20 : return error_mark_node;
9218 : }
9219 :
9220 702756 : push_defarg_context (fn);
9221 :
9222 702756 : if (fn && DECL_TEMPLATE_INFO (fn))
9223 529646 : arg = tsubst_default_argument (fn, parmnum, type, arg, complain);
9224 :
9225 : /* Due to:
9226 :
9227 : [dcl.fct.default]
9228 :
9229 : The names in the expression are bound, and the semantic
9230 : constraints are checked, at the point where the default
9231 : expressions appears.
9232 :
9233 : we must not perform access checks here. */
9234 702756 : push_deferring_access_checks (dk_no_check);
9235 : /* We must make a copy of ARG, in case subsequent processing
9236 : alters any part of it. */
9237 702756 : arg = break_out_target_exprs (arg, /*clear location*/true);
9238 :
9239 702756 : arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
9240 : ICR_DEFAULT_ARGUMENT, fn, parmnum,
9241 : complain);
9242 702756 : arg = convert_for_arg_passing (type, arg, complain);
9243 702756 : pop_deferring_access_checks();
9244 :
9245 702756 : pop_defarg_context ();
9246 :
9247 702756 : return arg;
9248 : }
9249 :
9250 : /* Returns the type which will really be used for passing an argument of
9251 : type TYPE. */
9252 :
9253 : tree
9254 358875290 : type_passed_as (tree type)
9255 : {
9256 : /* Pass classes with copy ctors by invisible reference. */
9257 358875290 : if (TREE_ADDRESSABLE (type))
9258 377436 : type = build_reference_type (type);
9259 358497854 : else if (targetm.calls.promote_prototypes (NULL_TREE)
9260 358497854 : && INTEGRAL_TYPE_P (type)
9261 72605557 : && COMPLETE_TYPE_P (type)
9262 431103410 : && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
9263 7907563 : type = integer_type_node;
9264 :
9265 358875290 : return type;
9266 : }
9267 :
9268 : /* Actually perform the appropriate conversion. */
9269 :
9270 : tree
9271 55658241 : convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
9272 : {
9273 55658241 : tree bitfield_type;
9274 :
9275 : /* If VAL is a bitfield, then -- since it has already been converted
9276 : to TYPE -- it cannot have a precision greater than TYPE.
9277 :
9278 : If it has a smaller precision, we must widen it here. For
9279 : example, passing "int f:3;" to a function expecting an "int" will
9280 : not result in any conversion before this point.
9281 :
9282 : If the precision is the same we must not risk widening. For
9283 : example, the COMPONENT_REF for a 32-bit "long long" bitfield will
9284 : often have type "int", even though the C++ type for the field is
9285 : "long long". If the value is being passed to a function
9286 : expecting an "int", then no conversions will be required. But,
9287 : if we call convert_bitfield_to_declared_type, the bitfield will
9288 : be converted to "long long". */
9289 55658241 : bitfield_type = is_bitfield_expr_with_lowered_type (val);
9290 55658241 : if (bitfield_type
9291 55658241 : && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
9292 0 : val = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type), val);
9293 :
9294 55658241 : if (val == error_mark_node)
9295 : ;
9296 : /* Pass classes with copy ctors by invisible reference. */
9297 55656561 : else if (TREE_ADDRESSABLE (type))
9298 138717 : val = build1 (ADDR_EXPR, build_reference_type (type), val);
9299 55517844 : else if (targetm.calls.promote_prototypes (NULL_TREE)
9300 55517844 : && INTEGRAL_TYPE_P (type)
9301 16436184 : && COMPLETE_TYPE_P (type)
9302 71954028 : && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
9303 1810677 : val = cp_perform_integral_promotions (val, complain);
9304 55658241 : if (complain & tf_warning)
9305 : {
9306 49917210 : if (warn_suggest_attribute_format)
9307 : {
9308 1854 : tree rhstype = TREE_TYPE (val);
9309 1854 : const enum tree_code coder = TREE_CODE (rhstype);
9310 1854 : const enum tree_code codel = TREE_CODE (type);
9311 1854 : if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
9312 735 : && coder == codel
9313 2589 : && check_missing_format_attribute (type, rhstype))
9314 8 : warning (OPT_Wsuggest_attribute_format,
9315 : "argument of function call might be a candidate "
9316 : "for a format attribute");
9317 : }
9318 61151463 : maybe_warn_parm_abi (type, cp_expr_loc_or_input_loc (val));
9319 : }
9320 :
9321 55658241 : if (complain & tf_warning)
9322 49917210 : warn_for_address_or_pointer_of_packed_member (type, val);
9323 :
9324 55658241 : return val;
9325 : }
9326 :
9327 : /* Returns non-zero iff FN is a function with magic varargs, i.e. ones for
9328 : which just decay_conversion or no conversions at all should be done.
9329 : This is true for some builtins which don't act like normal functions.
9330 : Return 2 if just decay_conversion and removal of excess precision should
9331 : be done, 1 if just decay_conversion. Return 3 for special treatment of
9332 : the 3rd argument for __builtin_*_overflow_p. */
9333 :
9334 : int
9335 62202950 : magic_varargs_p (tree fn)
9336 : {
9337 62202950 : if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
9338 3965318 : switch (DECL_FUNCTION_CODE (fn))
9339 : {
9340 : case BUILT_IN_CLASSIFY_TYPE:
9341 : case BUILT_IN_CONSTANT_P:
9342 : case BUILT_IN_NEXT_ARG:
9343 : case BUILT_IN_VA_START:
9344 : return 1;
9345 :
9346 21025 : case BUILT_IN_ADD_OVERFLOW_P:
9347 21025 : case BUILT_IN_SUB_OVERFLOW_P:
9348 21025 : case BUILT_IN_MUL_OVERFLOW_P:
9349 21025 : return 3;
9350 :
9351 109813 : case BUILT_IN_ISFINITE:
9352 109813 : case BUILT_IN_ISINF:
9353 109813 : case BUILT_IN_ISINF_SIGN:
9354 109813 : case BUILT_IN_ISNAN:
9355 109813 : case BUILT_IN_ISNORMAL:
9356 109813 : case BUILT_IN_FPCLASSIFY:
9357 109813 : return 2;
9358 :
9359 3783107 : default:
9360 3783107 : return lookup_attribute ("type generic",
9361 7566214 : TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
9362 : }
9363 :
9364 : return 0;
9365 : }
9366 :
9367 : /* Returns the decl of the dispatcher function if FN is a function version. */
9368 :
9369 : tree
9370 288 : get_function_version_dispatcher (tree fn)
9371 : {
9372 288 : tree dispatcher_decl = NULL;
9373 :
9374 288 : if (DECL_LOCAL_DECL_P (fn))
9375 32 : fn = DECL_LOCAL_DECL_ALIAS (fn);
9376 :
9377 288 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
9378 : && DECL_FUNCTION_VERSIONED (fn));
9379 :
9380 288 : gcc_assert (targetm.get_function_versions_dispatcher);
9381 288 : dispatcher_decl = targetm.get_function_versions_dispatcher (fn);
9382 :
9383 288 : if (dispatcher_decl == NULL)
9384 : {
9385 8 : error_at (input_location, "use of multiversioned function "
9386 : "without a default");
9387 8 : return NULL;
9388 : }
9389 :
9390 280 : retrofit_lang_decl (dispatcher_decl);
9391 280 : gcc_assert (dispatcher_decl != NULL);
9392 : return dispatcher_decl;
9393 : }
9394 :
9395 : /* fn is a function version dispatcher that is marked used. Mark all the
9396 : semantically identical function versions it will dispatch as used. */
9397 :
9398 : void
9399 184 : mark_versions_used (tree fn)
9400 : {
9401 184 : struct cgraph_node *node;
9402 184 : struct cgraph_function_version_info *node_v;
9403 184 : struct cgraph_function_version_info *it_v;
9404 :
9405 184 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9406 :
9407 184 : node = cgraph_node::get (fn);
9408 184 : if (node == NULL)
9409 : return;
9410 :
9411 184 : gcc_assert (node->dispatcher_function);
9412 :
9413 184 : node_v = node->function_version ();
9414 184 : if (node_v == NULL)
9415 : return;
9416 :
9417 : /* All semantically identical versions are chained. Traverse and mark each
9418 : one of them as used. */
9419 184 : it_v = node_v->next;
9420 1392 : while (it_v != NULL)
9421 : {
9422 1208 : mark_used (it_v->this_node->decl);
9423 1208 : it_v = it_v->next;
9424 : }
9425 : }
9426 :
9427 : /* Build a call to "the copy constructor" for the type of A, even if it
9428 : wouldn't be selected by normal overload resolution. Used for
9429 : diagnostics. */
9430 :
9431 : static tree
9432 4 : call_copy_ctor (tree a, tsubst_flags_t complain)
9433 : {
9434 4 : tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (a));
9435 4 : tree binfo = TYPE_BINFO (ctype);
9436 4 : tree copy = get_copy_ctor (ctype, complain);
9437 4 : copy = build_baselink (binfo, binfo, copy, NULL_TREE);
9438 4 : tree ob = build_dummy_object (ctype);
9439 4 : releasing_vec args (make_tree_vector_single (a));
9440 4 : tree r = build_new_method_call (ob, copy, &args, NULL_TREE,
9441 : LOOKUP_NORMAL, NULL, complain);
9442 4 : return r;
9443 4 : }
9444 :
9445 : /* Return the base constructor corresponding to COMPLETE_CTOR or NULL_TREE. */
9446 :
9447 : static tree
9448 34 : base_ctor_for (tree complete_ctor)
9449 : {
9450 34 : tree clone;
9451 34 : FOR_EACH_CLONE (clone, DECL_CLONED_FUNCTION (complete_ctor))
9452 34 : if (DECL_BASE_CONSTRUCTOR_P (clone))
9453 34 : return clone;
9454 : return NULL_TREE;
9455 : }
9456 :
9457 : /* Try to make EXP suitable to be used as the initializer for a base subobject,
9458 : and return whether we were successful. EXP must have already been cleared
9459 : by unsafe_copy_elision_p{,_opt}. */
9460 :
9461 : static bool
9462 168 : make_base_init_ok (tree exp)
9463 : {
9464 168 : if (TREE_CODE (exp) == TARGET_EXPR)
9465 168 : exp = TARGET_EXPR_INITIAL (exp);
9466 171 : while (TREE_CODE (exp) == COMPOUND_EXPR)
9467 3 : exp = TREE_OPERAND (exp, 1);
9468 168 : if (TREE_CODE (exp) == COND_EXPR)
9469 : {
9470 3 : bool ret = make_base_init_ok (TREE_OPERAND (exp, 2));
9471 3 : if (tree op1 = TREE_OPERAND (exp, 1))
9472 : {
9473 3 : bool r1 = make_base_init_ok (op1);
9474 : /* If unsafe_copy_elision_p was false, the arms should match. */
9475 3 : gcc_assert (r1 == ret);
9476 : }
9477 3 : return ret;
9478 : }
9479 165 : if (TREE_CODE (exp) != AGGR_INIT_EXPR)
9480 : /* A trivial copy is OK. */
9481 : return true;
9482 44 : if (!AGGR_INIT_VIA_CTOR_P (exp))
9483 : /* unsafe_copy_elision_p_opt must have said this is OK. */
9484 : return true;
9485 34 : tree fn = cp_get_callee_fndecl_nofold (exp);
9486 34 : if (DECL_BASE_CONSTRUCTOR_P (fn))
9487 : return true;
9488 34 : gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (fn));
9489 34 : fn = base_ctor_for (fn);
9490 34 : if (!fn || DECL_HAS_VTT_PARM_P (fn))
9491 : /* The base constructor has more parameters, so we can't just change the
9492 : call target. It would be possible to splice in the appropriate
9493 : arguments, but probably not worth the complexity. */
9494 : return false;
9495 24 : mark_used (fn);
9496 24 : AGGR_INIT_EXPR_FN (exp) = build_address (fn);
9497 24 : return true;
9498 : }
9499 :
9500 : /* Return 2 if T refers to a base, 1 if a potentially-overlapping field,
9501 : neither of which can be used for return by invisible reference. We avoid
9502 : doing C++17 mandatory copy elision for either of these cases.
9503 :
9504 : This returns non-zero even if the type of T has no tail padding that other
9505 : data could be allocated into, because that depends on the particular ABI.
9506 : unsafe_copy_elision_p_opt does consider whether there is padding. */
9507 :
9508 : int
9509 13879298 : unsafe_return_slot_p (tree t)
9510 : {
9511 : /* Check empty bases separately, they don't have fields. */
9512 13879298 : if (is_empty_base_ref (t))
9513 : return 2;
9514 :
9515 : /* A delegating constructor might be used to initialize a base. */
9516 13556806 : if (current_function_decl
9517 19535102 : && DECL_CONSTRUCTOR_P (current_function_decl)
9518 15617141 : && (t == current_class_ref
9519 2014060 : || tree_strip_nop_conversions (t) == current_class_ptr))
9520 47812 : return 2;
9521 :
9522 13508994 : STRIP_NOPS (t);
9523 13508994 : if (TREE_CODE (t) == ADDR_EXPR)
9524 27759 : t = TREE_OPERAND (t, 0);
9525 13508994 : if (TREE_CODE (t) == COMPONENT_REF)
9526 1206037 : t = TREE_OPERAND (t, 1);
9527 13508994 : if (TREE_CODE (t) != FIELD_DECL)
9528 : return false;
9529 1209581 : if (!CLASS_TYPE_P (TREE_TYPE (t)))
9530 : /* The middle-end will do the right thing for scalar types. */
9531 : return false;
9532 1146961 : if (DECL_FIELD_IS_BASE (t))
9533 : return 2;
9534 720459 : if (lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (t)))
9535 : return 1;
9536 : return 0;
9537 : }
9538 :
9539 : /* True IFF EXP is a prvalue that represents return by invisible reference. */
9540 :
9541 : static bool
9542 158340 : init_by_return_slot_p (tree exp)
9543 : {
9544 : /* Copy elision only happens with a TARGET_EXPR. */
9545 158343 : if (TREE_CODE (exp) != TARGET_EXPR)
9546 : return false;
9547 1491 : tree init = TARGET_EXPR_INITIAL (exp);
9548 : /* build_compound_expr pushes COMPOUND_EXPR inside TARGET_EXPR. */
9549 1498 : while (TREE_CODE (init) == COMPOUND_EXPR)
9550 7 : init = TREE_OPERAND (init, 1);
9551 1491 : if (TREE_CODE (init) == COND_EXPR)
9552 : {
9553 : /* We'll end up copying from each of the arms of the COND_EXPR directly
9554 : into the target, so look at them. */
9555 6 : if (tree op = TREE_OPERAND (init, 1))
9556 6 : if (init_by_return_slot_p (op))
9557 : return true;
9558 3 : return init_by_return_slot_p (TREE_OPERAND (init, 2));
9559 : }
9560 1485 : return (TREE_CODE (init) == AGGR_INIT_EXPR
9561 1485 : && !AGGR_INIT_VIA_CTOR_P (init));
9562 : }
9563 :
9564 : /* We can't elide a copy from a function returning by value to a
9565 : potentially-overlapping subobject, as the callee might clobber tail padding.
9566 : Return true iff this could be that case.
9567 :
9568 : Places that use this function (or _opt) to decide to elide a copy should
9569 : probably use make_safe_copy_elision instead. */
9570 :
9571 : bool
9572 825932 : unsafe_copy_elision_p (tree target, tree exp)
9573 : {
9574 825932 : return unsafe_return_slot_p (target) && init_by_return_slot_p (exp);
9575 : }
9576 :
9577 : /* As above, but for optimization allow more cases that are actually safe. */
9578 :
9579 : static bool
9580 2473876 : unsafe_copy_elision_p_opt (tree target, tree exp)
9581 : {
9582 2473876 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
9583 : /* It's safe to elide the copy for a class with no tail padding. */
9584 2473876 : if (!is_empty_class (type)
9585 2473876 : && tree_int_cst_equal (TYPE_SIZE (type), CLASSTYPE_SIZE (type)))
9586 : return false;
9587 822388 : return unsafe_copy_elision_p (target, exp);
9588 : }
9589 :
9590 : /* Try to make EXP suitable to be used as the initializer for TARGET,
9591 : and return whether we were successful. */
9592 :
9593 : bool
9594 12 : make_safe_copy_elision (tree target, tree exp)
9595 : {
9596 12 : int uns = unsafe_return_slot_p (target);
9597 12 : if (!uns)
9598 : return true;
9599 12 : if (init_by_return_slot_p (exp))
9600 : return false;
9601 12 : if (uns == 1)
9602 : return true;
9603 12 : return make_base_init_ok (exp);
9604 : }
9605 :
9606 : /* True IFF the result of the conversion C is a prvalue. */
9607 :
9608 : static bool
9609 504749955 : conv_is_prvalue (conversion *c)
9610 : {
9611 504749955 : if (c->kind == ck_rvalue)
9612 : return true;
9613 424304343 : if (c->kind == ck_base && c->need_temporary_p)
9614 : return true;
9615 423989056 : if (c->kind == ck_user && !TYPE_REF_P (c->type))
9616 : return true;
9617 419423164 : if (c->kind == ck_identity && c->u.expr
9618 307690292 : && TREE_CODE (c->u.expr) == TARGET_EXPR)
9619 41 : return true;
9620 :
9621 : return false;
9622 : }
9623 :
9624 : /* True iff C is a conversion that binds a reference to a prvalue. */
9625 :
9626 : static bool
9627 4988052 : conv_binds_ref_to_prvalue (conversion *c)
9628 : {
9629 4988052 : if (c->kind != ck_ref_bind)
9630 : return false;
9631 4988052 : if (c->need_temporary_p)
9632 : return true;
9633 :
9634 4987679 : return conv_is_prvalue (next_conversion (c));
9635 : }
9636 :
9637 : /* True iff EXPR represents a (subobject of a) temporary. */
9638 :
9639 : static bool
9640 1544 : expr_represents_temporary_p (tree expr)
9641 : {
9642 1628 : while (handled_component_p (expr))
9643 84 : expr = TREE_OPERAND (expr, 0);
9644 1544 : return TREE_CODE (expr) == TARGET_EXPR;
9645 : }
9646 :
9647 : /* True iff C is a conversion that binds a reference to a temporary.
9648 : This is a superset of conv_binds_ref_to_prvalue: here we're also
9649 : interested in xvalues. */
9650 :
9651 : static bool
9652 1424 : conv_binds_ref_to_temporary (conversion *c)
9653 : {
9654 1424 : if (conv_binds_ref_to_prvalue (c))
9655 : return true;
9656 1118 : if (c->kind != ck_ref_bind)
9657 : return false;
9658 1118 : c = next_conversion (c);
9659 : /* This is the case for
9660 : struct Base {};
9661 : struct Derived : Base {};
9662 : const Base& b(Derived{});
9663 : where we bind 'b' to the Base subobject of a temporary object of type
9664 : Derived. The subobject is an xvalue; the whole object is a prvalue.
9665 :
9666 : The ck_base doesn't have to be present for cases like X{}.m. */
9667 1118 : if (c->kind == ck_base)
9668 8 : c = next_conversion (c);
9669 1065 : if (c->kind == ck_identity && c->u.expr
9670 2183 : && expr_represents_temporary_p (c->u.expr))
9671 : return true;
9672 : return false;
9673 : }
9674 :
9675 : /* Return tristate::TS_TRUE if converting EXPR to a reference type TYPE binds
9676 : the reference to a temporary. Return tristate::TS_FALSE if converting
9677 : EXPR to a reference type TYPE doesn't bind the reference to a temporary. If
9678 : the conversion is invalid or bad, return tristate::TS_UNKNOWN. DIRECT_INIT_P
9679 : says whether the conversion should be done in direct- or copy-initialization
9680 : context. */
9681 :
9682 : tristate
9683 1873 : ref_conv_binds_to_temporary (tree type, tree expr, bool direct_init_p/*=false*/)
9684 : {
9685 1873 : gcc_assert (TYPE_REF_P (type));
9686 :
9687 : /* Get the high-water mark for the CONVERSION_OBSTACK. */
9688 1873 : void *p = conversion_obstack_alloc (0);
9689 :
9690 1873 : const int flags = direct_init_p ? LOOKUP_NORMAL : LOOKUP_IMPLICIT;
9691 1873 : conversion *conv = implicit_conversion (type, TREE_TYPE (expr), expr,
9692 : /*c_cast_p=*/false, flags, tf_none);
9693 1873 : tristate ret (tristate::TS_UNKNOWN);
9694 1873 : if (conv && !conv->bad_p)
9695 1424 : ret = tristate (conv_binds_ref_to_temporary (conv));
9696 :
9697 : /* Free all the conversions we allocated. */
9698 1873 : obstack_free (&conversion_obstack, p);
9699 :
9700 1873 : return ret;
9701 : }
9702 :
9703 : /* Call the trivial destructor for INSTANCE, which can be either an lvalue of
9704 : class type or a pointer to class type. If NO_PTR_DEREF is true and
9705 : INSTANCE has pointer type, clobber the pointer rather than what it points
9706 : to. */
9707 :
9708 : tree
9709 867755 : build_trivial_dtor_call (tree instance, bool no_ptr_deref)
9710 : {
9711 867755 : gcc_assert (!is_dummy_object (instance));
9712 :
9713 867755 : if (!flag_lifetime_dse)
9714 : {
9715 42 : no_clobber:
9716 69 : return fold_convert (void_type_node, instance);
9717 : }
9718 :
9719 916813 : if (INDIRECT_TYPE_P (TREE_TYPE (instance))
9720 867713 : && (!no_ptr_deref || TYPE_REF_P (TREE_TYPE (instance))))
9721 : {
9722 817296 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
9723 27 : goto no_clobber;
9724 817269 : instance = cp_build_fold_indirect_ref (instance);
9725 : }
9726 :
9727 : /* A trivial destructor should still clobber the object. */
9728 867686 : tree clobber = build_clobber (TREE_TYPE (instance));
9729 867686 : return build2 (MODIFY_EXPR, void_type_node,
9730 867686 : instance, clobber);
9731 : }
9732 :
9733 : /* Return true if in an immediate function context, or an unevaluated operand,
9734 : or a default argument/member initializer, or a subexpression of an immediate
9735 : invocation. */
9736 :
9737 : bool
9738 113301 : in_immediate_context ()
9739 : {
9740 113301 : return (cp_unevaluated_operand != 0
9741 111521 : || (current_function_decl != NULL_TREE
9742 220962 : && DECL_IMMEDIATE_FUNCTION_P (current_function_decl))
9743 : /* DR 2631: default args and DMI aren't immediately evaluated.
9744 : Return true here so immediate_invocation_p returns false. */
9745 110637 : || current_binding_level->kind == sk_function_parms
9746 110566 : || current_binding_level->kind == sk_template_parms
9747 110563 : || parsing_nsdmi ()
9748 223815 : || in_consteval_if_p);
9749 : }
9750 :
9751 : /* Return true if a call to FN with number of arguments NARGS
9752 : is an immediate invocation. */
9753 :
9754 : static bool
9755 135955828 : immediate_invocation_p (tree fn)
9756 : {
9757 135955828 : return (TREE_CODE (fn) == FUNCTION_DECL
9758 135955828 : && DECL_IMMEDIATE_FUNCTION_P (fn)
9759 135960761 : && !in_immediate_context ());
9760 : }
9761 :
9762 : /* Subroutine of the various build_*_call functions. Overload resolution
9763 : has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
9764 : ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
9765 : bitmask of various LOOKUP_* flags which apply to the call itself. */
9766 :
9767 : static tree
9768 90693826 : build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
9769 : {
9770 90693826 : tree fn = cand->fn;
9771 90693826 : const vec<tree, va_gc> *args = cand->args;
9772 90693826 : tree first_arg = cand->first_arg;
9773 90693826 : conversion **convs = cand->convs;
9774 90693826 : conversion *conv;
9775 90693826 : tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
9776 90693826 : int parmlen;
9777 90693826 : tree val;
9778 90693826 : int i = 0;
9779 90693826 : int j = 0;
9780 90693826 : unsigned int arg_index = 0;
9781 90693826 : int is_method = 0;
9782 90693826 : int nargs;
9783 90693826 : tree *argarray;
9784 90693826 : bool already_used = false;
9785 :
9786 : /* In a template, there is no need to perform all of the work that
9787 : is normally done. We are only interested in the type of the call
9788 : expression, i.e., the return type of the function. Any semantic
9789 : errors will be deferred until the template is instantiated. */
9790 90693826 : if (processing_template_decl)
9791 : {
9792 15173053 : if (undeduced_auto_decl (fn))
9793 66 : mark_used (fn, complain);
9794 : else
9795 : /* Otherwise set TREE_USED for the benefit of -Wunused-function.
9796 : See PR80598. */
9797 15172987 : TREE_USED (fn) = 1;
9798 :
9799 15173053 : tree return_type = TREE_TYPE (TREE_TYPE (fn));
9800 15173053 : tree callee;
9801 15173053 : if (first_arg == NULL_TREE)
9802 : {
9803 11973288 : callee = build_addr_func (fn, complain);
9804 11973288 : if (callee == error_mark_node)
9805 : return error_mark_node;
9806 : }
9807 : else
9808 : {
9809 3199765 : callee = build_baselink (cand->conversion_path, cand->access_path,
9810 : fn, NULL_TREE);
9811 3199765 : callee = build_min (COMPONENT_REF, TREE_TYPE (fn),
9812 : first_arg, callee, NULL_TREE);
9813 : }
9814 :
9815 15173053 : tree expr = build_call_vec (return_type, callee, args);
9816 15173053 : SET_EXPR_LOCATION (expr, input_location);
9817 15173053 : if (TREE_THIS_VOLATILE (fn) && cfun)
9818 2643308 : current_function_returns_abnormally = 1;
9819 15173053 : if (immediate_invocation_p (fn))
9820 : {
9821 226 : tree obj_arg = NULL_TREE, exprimm = expr;
9822 452 : if (DECL_CONSTRUCTOR_P (fn))
9823 0 : obj_arg = first_arg;
9824 0 : if (obj_arg
9825 0 : && is_dummy_object (obj_arg)
9826 0 : && !type_dependent_expression_p (obj_arg))
9827 : {
9828 0 : exprimm = build_cplus_new (DECL_CONTEXT (fn), expr, complain);
9829 0 : obj_arg = NULL_TREE;
9830 : }
9831 : /* Look through *(const T *)&obj. */
9832 226 : else if (obj_arg && INDIRECT_REF_P (obj_arg))
9833 : {
9834 0 : tree addr = TREE_OPERAND (obj_arg, 0);
9835 0 : STRIP_NOPS (addr);
9836 0 : if (TREE_CODE (addr) == ADDR_EXPR)
9837 : {
9838 0 : tree typeo = TREE_TYPE (obj_arg);
9839 0 : tree typei = TREE_TYPE (TREE_OPERAND (addr, 0));
9840 0 : if (same_type_ignoring_top_level_qualifiers_p (typeo, typei))
9841 0 : obj_arg = TREE_OPERAND (addr, 0);
9842 : }
9843 : }
9844 226 : fold_non_dependent_expr (exprimm, complain,
9845 : /*manifestly_const_eval=*/true,
9846 : obj_arg);
9847 : }
9848 15173053 : return convert_from_reference (expr);
9849 : }
9850 :
9851 : /* Give any warnings we noticed during overload resolution. */
9852 75520773 : if (cand->warnings && (complain & tf_warning))
9853 : {
9854 : struct candidate_warning *w;
9855 76 : for (w = cand->warnings; w; w = w->next)
9856 38 : joust (cand, w->loser, 1, complain);
9857 : }
9858 :
9859 : /* Core issue 2327: P0135 doesn't say how to handle the case where the
9860 : argument to the copy constructor ends up being a prvalue after
9861 : conversion. Let's do the normal processing, but pretend we aren't
9862 : actually using the copy constructor. */
9863 75520773 : bool force_elide = false;
9864 75520773 : if (cxx_dialect >= cxx17
9865 72975419 : && cand->num_convs == 1
9866 42867066 : && DECL_COMPLETE_CONSTRUCTOR_P (fn)
9867 12214346 : && (DECL_COPY_CONSTRUCTOR_P (fn)
9868 6343574 : || DECL_MOVE_CONSTRUCTOR_P (fn))
9869 4995679 : && !unsafe_return_slot_p (first_arg)
9870 80507358 : && conv_binds_ref_to_prvalue (convs[0]))
9871 : {
9872 35514 : force_elide = true;
9873 35514 : goto not_really_used;
9874 : }
9875 :
9876 : /* OK, we're actually calling this inherited constructor; set its deletedness
9877 : appropriately. We can get away with doing this here because calling is
9878 : the only way to refer to a constructor. */
9879 150970518 : if (DECL_INHERITED_CTOR (fn)
9880 11119310 : && !deduce_inheriting_ctor (fn))
9881 : {
9882 3 : if (complain & tf_error)
9883 3 : mark_used (fn);
9884 3 : return error_mark_node;
9885 : }
9886 :
9887 : /* Make =delete work with SFINAE. */
9888 75485256 : if (DECL_DELETED_FN (fn))
9889 : {
9890 407085 : if (complain & tf_error)
9891 2050 : mark_used (fn);
9892 407085 : return error_mark_node;
9893 : }
9894 :
9895 75078171 : if (DECL_FUNCTION_MEMBER_P (fn))
9896 : {
9897 45997969 : tree access_fn;
9898 : /* If FN is a template function, two cases must be considered.
9899 : For example:
9900 :
9901 : struct A {
9902 : protected:
9903 : template <class T> void f();
9904 : };
9905 : template <class T> struct B {
9906 : protected:
9907 : void g();
9908 : };
9909 : struct C : A, B<int> {
9910 : using A::f; // #1
9911 : using B<int>::g; // #2
9912 : };
9913 :
9914 : In case #1 where `A::f' is a member template, DECL_ACCESS is
9915 : recorded in the primary template but not in its specialization.
9916 : We check access of FN using its primary template.
9917 :
9918 : In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
9919 : because it is a member of class template B, DECL_ACCESS is
9920 : recorded in the specialization `B<int>::g'. We cannot use its
9921 : primary template because `B<T>::g' and `B<int>::g' may have
9922 : different access. */
9923 45997969 : if (DECL_TEMPLATE_INFO (fn)
9924 45997969 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
9925 5686382 : access_fn = DECL_TI_TEMPLATE (fn);
9926 : else
9927 : access_fn = fn;
9928 45997969 : if (!perform_or_defer_access_check (cand->access_path, access_fn,
9929 : fn, complain))
9930 15225 : return error_mark_node;
9931 : }
9932 :
9933 : /* If we're checking for implicit delete, don't bother with argument
9934 : conversions. */
9935 75062946 : if (flags & LOOKUP_SPECULATIVE)
9936 : {
9937 12893999 : if (cand->viable == 1)
9938 : return fn;
9939 136 : else if (!(complain & tf_error))
9940 : /* Reject bad conversions now. */
9941 121 : return error_mark_node;
9942 : /* else continue to get conversion error. */
9943 : }
9944 :
9945 62168947 : not_really_used:
9946 :
9947 : /* N3276 magic doesn't apply to nested calls. */
9948 62204476 : tsubst_flags_t decltype_flag = (complain & tf_decltype);
9949 62204476 : complain &= ~tf_decltype;
9950 : /* No-Cleanup doesn't apply to nested calls either. */
9951 62204476 : tsubst_flags_t no_cleanup_complain = complain;
9952 62204476 : complain &= ~tf_no_cleanup;
9953 :
9954 : /* Find maximum size of vector to hold converted arguments. */
9955 62204476 : parmlen = list_length (parm);
9956 115218524 : nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0);
9957 62204476 : if (parmlen > nargs)
9958 : nargs = parmlen;
9959 62204476 : argarray = XALLOCAVEC (tree, nargs);
9960 :
9961 62204476 : in_consteval_if_p_temp_override icip;
9962 : /* If the call is immediate function invocation, make sure
9963 : taking address of immediate functions is allowed in its arguments. */
9964 62204476 : if (immediate_invocation_p (STRIP_TEMPLATE (fn)))
9965 1639 : in_consteval_if_p = true;
9966 :
9967 : /* The implicit parameters to a constructor are not considered by overload
9968 : resolution, and must be of the proper type. */
9969 124408952 : if (DECL_CONSTRUCTOR_P (fn))
9970 : {
9971 6986979 : tree object_arg;
9972 6986979 : if (first_arg != NULL_TREE)
9973 : {
9974 : object_arg = first_arg;
9975 : first_arg = NULL_TREE;
9976 : }
9977 : else
9978 : {
9979 0 : object_arg = (*args)[arg_index];
9980 0 : ++arg_index;
9981 : }
9982 6986979 : argarray[j++] = build_this (object_arg);
9983 6986979 : parm = TREE_CHAIN (parm);
9984 : /* We should never try to call the abstract constructor. */
9985 6986979 : gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
9986 :
9987 6986979 : if (DECL_HAS_VTT_PARM_P (fn))
9988 : {
9989 15994 : argarray[j++] = (*args)[arg_index];
9990 15994 : ++arg_index;
9991 15994 : parm = TREE_CHAIN (parm);
9992 : }
9993 : }
9994 : /* Bypass access control for 'this' parameter. */
9995 55217497 : else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
9996 : {
9997 17489970 : tree arg = build_this (first_arg != NULL_TREE
9998 : ? first_arg
9999 0 : : (*args)[arg_index]);
10000 17489970 : tree argtype = TREE_TYPE (arg);
10001 :
10002 17489970 : if (arg == error_mark_node)
10003 : return error_mark_node;
10004 :
10005 17489967 : if (convs[i]->bad_p)
10006 : {
10007 373 : if (complain & tf_error)
10008 : {
10009 91 : auto_diagnostic_group d;
10010 91 : if (permerror (input_location, "passing %qT as %<this%> "
10011 : "argument discards qualifiers",
10012 91 : TREE_TYPE (argtype)))
10013 87 : inform (DECL_SOURCE_LOCATION (fn), " in call to %qD", fn);
10014 91 : }
10015 : else
10016 : return error_mark_node;
10017 : }
10018 :
10019 : /* The class where FN is defined. */
10020 17489685 : tree ctx = DECL_CONTEXT (fn);
10021 :
10022 : /* See if the function member or the whole class type is declared
10023 : final and the call can be devirtualized. */
10024 17489685 : if (DECL_FINAL_P (fn) || CLASSTYPE_FINAL (ctx))
10025 95397 : flags |= LOOKUP_NONVIRTUAL;
10026 :
10027 : /* [class.mfct.non-static]: If a non-static member function of a class
10028 : X is called for an object that is not of type X, or of a type
10029 : derived from X, the behavior is undefined.
10030 :
10031 : So we can assume that anything passed as 'this' is non-null, and
10032 : optimize accordingly. */
10033 : /* Check that the base class is accessible. */
10034 17489685 : if (!accessible_base_p (TREE_TYPE (argtype),
10035 17489685 : BINFO_TYPE (cand->conversion_path), true))
10036 : {
10037 39 : if (complain & tf_error)
10038 36 : error ("%qT is not an accessible base of %qT",
10039 36 : BINFO_TYPE (cand->conversion_path),
10040 36 : TREE_TYPE (argtype));
10041 : else
10042 3 : return error_mark_node;
10043 : }
10044 : /* If fn was found by a using declaration, the conversion path
10045 : will be to the derived class, not the base declaring fn. We
10046 : must convert to the base. */
10047 17489682 : tree base_binfo = cand->conversion_path;
10048 17489682 : if (BINFO_TYPE (base_binfo) != ctx)
10049 : {
10050 52804 : base_binfo = lookup_base (base_binfo, ctx, ba_unique, NULL, complain);
10051 52804 : if (base_binfo == error_mark_node)
10052 : return error_mark_node;
10053 : }
10054 :
10055 : /* If we know the dynamic type of the object, look up the final overrider
10056 : in the BINFO. */
10057 18052239 : if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
10058 17834086 : && resolves_to_fixed_type_p (arg))
10059 : {
10060 914 : tree ov = lookup_vfn_in_binfo (DECL_VINDEX (fn), base_binfo);
10061 :
10062 : /* And unwind base_binfo to match. If we don't find the type we're
10063 : looking for in BINFO_INHERITANCE_CHAIN, we're looking at diamond
10064 : inheritance; for now do a normal virtual call in that case. */
10065 914 : tree octx = DECL_CONTEXT (ov);
10066 914 : tree obinfo = base_binfo;
10067 1908 : while (obinfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (obinfo), octx))
10068 42 : obinfo = BINFO_INHERITANCE_CHAIN (obinfo);
10069 914 : if (obinfo)
10070 : {
10071 910 : fn = ov;
10072 910 : base_binfo = obinfo;
10073 910 : flags |= LOOKUP_NONVIRTUAL;
10074 : }
10075 : }
10076 :
10077 17489676 : tree converted_arg = build_base_path (PLUS_EXPR, arg,
10078 : base_binfo, 1, complain);
10079 :
10080 17489676 : argarray[j++] = converted_arg;
10081 17489676 : parm = TREE_CHAIN (parm);
10082 17489676 : if (first_arg != NULL_TREE)
10083 : first_arg = NULL_TREE;
10084 : else
10085 0 : ++arg_index;
10086 : ++i;
10087 : is_method = 1;
10088 : }
10089 :
10090 62204182 : gcc_assert (first_arg == NULL_TREE);
10091 114635664 : for (; arg_index < vec_safe_length (args) && parm;
10092 52431482 : parm = TREE_CHAIN (parm), ++arg_index, ++i)
10093 : {
10094 52432598 : tree type = TREE_VALUE (parm);
10095 52432598 : tree arg = (*args)[arg_index];
10096 52432598 : bool conversion_warning = true;
10097 :
10098 52432598 : conv = convs[i];
10099 :
10100 : /* If the argument is NULL and used to (implicitly) instantiate a
10101 : template function (and bind one of the template arguments to
10102 : the type of 'long int'), we don't want to warn about passing NULL
10103 : to non-pointer argument.
10104 : For example, if we have this template function:
10105 :
10106 : template<typename T> void func(T x) {}
10107 :
10108 : we want to warn (when -Wconversion is enabled) in this case:
10109 :
10110 : void foo() {
10111 : func<int>(NULL);
10112 : }
10113 :
10114 : but not in this case:
10115 :
10116 : void foo() {
10117 : func(NULL);
10118 : }
10119 : */
10120 52432598 : if (null_node_p (arg)
10121 33705 : && DECL_TEMPLATE_INFO (fn)
10122 75 : && cand->template_decl
10123 52432638 : && !cand->explicit_targs)
10124 : conversion_warning = false;
10125 :
10126 : /* Set user_conv_p on the argument conversions, so rvalue/base handling
10127 : knows not to allow any more UDCs. This needs to happen after we
10128 : process cand->warnings. */
10129 52432598 : if (flags & LOOKUP_NO_CONVERSION)
10130 931325 : conv->user_conv_p = true;
10131 :
10132 52432598 : tsubst_flags_t arg_complain = complain;
10133 52432598 : if (!conversion_warning)
10134 20 : arg_complain &= ~tf_warning;
10135 :
10136 52432598 : if (arg_complain & tf_warning)
10137 46799200 : maybe_warn_pessimizing_move (arg, type, /*return_p*/false);
10138 :
10139 52432598 : val = convert_like_with_context (conv, arg, fn, i - is_method,
10140 : arg_complain);
10141 52432598 : val = convert_for_arg_passing (type, val, arg_complain);
10142 :
10143 52432598 : if (val == error_mark_node)
10144 1116 : return error_mark_node;
10145 : else
10146 52431482 : argarray[j++] = val;
10147 : }
10148 :
10149 : /* Default arguments */
10150 62905737 : for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
10151 : {
10152 702791 : if (TREE_VALUE (parm) == error_mark_node)
10153 16 : return error_mark_node;
10154 1405550 : val = convert_default_arg (TREE_VALUE (parm),
10155 702775 : TREE_PURPOSE (parm),
10156 : fn, i - is_method,
10157 : complain);
10158 702775 : if (val == error_mark_node)
10159 104 : return error_mark_node;
10160 702671 : argarray[j++] = val;
10161 : }
10162 :
10163 : /* Ellipsis */
10164 62202946 : int magic = magic_varargs_p (fn);
10165 64044148 : for (; arg_index < vec_safe_length (args); ++arg_index)
10166 : {
10167 1841229 : tree a = (*args)[arg_index];
10168 1841229 : if (magic == 3 && arg_index == 2)
10169 : {
10170 : /* Do no conversions for certain magic varargs. */
10171 21013 : a = mark_type_use (a);
10172 21013 : if (TREE_CODE (a) == FUNCTION_DECL && reject_gcc_builtin (a))
10173 0 : return error_mark_node;
10174 : }
10175 1820216 : else if (magic != 0)
10176 : {
10177 : /* Don't truncate excess precision to the semantic type. */
10178 559324 : if (magic == 1 && TREE_CODE (a) == EXCESS_PRECISION_EXPR)
10179 112 : a = TREE_OPERAND (a, 0);
10180 : /* For other magic varargs only do decay_conversion. */
10181 559324 : a = decay_conversion (a, complain);
10182 : }
10183 1260892 : else if (DECL_CONSTRUCTOR_P (fn)
10184 1261168 : && same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (fn),
10185 276 : TREE_TYPE (a)))
10186 : {
10187 : /* Avoid infinite recursion trying to call A(...). */
10188 4 : if (complain & tf_error)
10189 : /* Try to call the actual copy constructor for a good error. */
10190 4 : call_copy_ctor (a, complain);
10191 4 : return error_mark_node;
10192 : }
10193 : else
10194 1260888 : a = convert_arg_to_ellipsis (a, complain);
10195 1841225 : if (a == error_mark_node)
10196 23 : return error_mark_node;
10197 1841202 : argarray[j++] = a;
10198 : }
10199 :
10200 62202919 : gcc_assert (j <= nargs);
10201 62202919 : nargs = j;
10202 62202919 : icip.reset ();
10203 :
10204 : /* Avoid performing argument transformation if warnings are disabled.
10205 : When tf_warning is set and at least one of the warnings is active
10206 : the check_function_arguments function might warn about something. */
10207 :
10208 62202919 : bool warned_p = false;
10209 62202919 : if ((complain & tf_warning)
10210 49568870 : && (warn_nonnull
10211 48012704 : || warn_format
10212 48012700 : || warn_suggest_attribute_format
10213 48012580 : || warn_restrict))
10214 : {
10215 1559286 : tree *fargs = (!nargs ? argarray
10216 1293153 : : (tree *) alloca (nargs * sizeof (tree)));
10217 4887688 : for (j = 0; j < nargs; j++)
10218 : {
10219 : /* For -Wformat undo the implicit passing by hidden reference
10220 : done by convert_arg_to_ellipsis. */
10221 3328402 : if (TREE_CODE (argarray[j]) == ADDR_EXPR
10222 3328402 : && TYPE_REF_P (TREE_TYPE (argarray[j])))
10223 1500 : fargs[j] = TREE_OPERAND (argarray[j], 0);
10224 : else
10225 3326902 : fargs[j] = argarray[j];
10226 : }
10227 :
10228 1559286 : warned_p = check_function_arguments (input_location, fn, TREE_TYPE (fn),
10229 : nargs, fargs, NULL);
10230 : }
10231 :
10232 124405838 : if (DECL_INHERITED_CTOR (fn))
10233 : {
10234 : /* Check for passing ellipsis arguments to an inherited constructor. We
10235 : could handle this by open-coding the inherited constructor rather than
10236 : defining it, but let's not bother now. */
10237 30677 : if (!cp_unevaluated_operand
10238 30607 : && cand->num_convs
10239 23659 : && cand->convs[cand->num_convs-1]->ellipsis_p)
10240 : {
10241 15 : if (complain & tf_error)
10242 : {
10243 15 : sorry ("passing arguments to ellipsis of inherited constructor "
10244 : "%qD", cand->fn);
10245 15 : inform (DECL_SOURCE_LOCATION (cand->fn), "declared here");
10246 : }
10247 15 : return error_mark_node;
10248 : }
10249 :
10250 : /* A base constructor inheriting from a virtual base doesn't get the
10251 : inherited arguments, just this and __vtt. */
10252 30662 : if (ctor_omit_inherited_parms (fn))
10253 62202904 : nargs = 2;
10254 : }
10255 :
10256 : /* Avoid actually calling copy constructors and copy assignment operators,
10257 : if possible. */
10258 :
10259 62202904 : if (! flag_elide_constructors && !force_elide)
10260 : /* Do things the hard way. */;
10261 62202720 : else if (cand->num_convs == 1
10262 65982338 : && (DECL_COPY_CONSTRUCTOR_P (fn)
10263 63331250 : || DECL_MOVE_CONSTRUCTOR_P (fn))
10264 : /* It's unsafe to elide the constructor when handling
10265 : a noexcept-expression, it may evaluate to the wrong
10266 : value (c++/53025). */
10267 64678800 : && (force_elide || cp_noexcept_operand == 0))
10268 : {
10269 2473876 : tree targ;
10270 2473876 : tree arg = argarray[num_artificial_parms_for (fn)];
10271 2473876 : tree fa = argarray[0];
10272 2473876 : bool trivial = trivial_fn_p (fn);
10273 :
10274 : /* Pull out the real argument, disregarding const-correctness. */
10275 2473876 : targ = arg;
10276 : /* Strip the reference binding for the constructor parameter. */
10277 0 : if (CONVERT_EXPR_P (targ)
10278 2473876 : && TYPE_REF_P (TREE_TYPE (targ)))
10279 2473876 : targ = TREE_OPERAND (targ, 0);
10280 : /* But don't strip any other reference bindings; binding a temporary to a
10281 : reference prevents copy elision. */
10282 4149267 : while ((CONVERT_EXPR_P (targ)
10283 3482493 : && !TYPE_REF_P (TREE_TYPE (targ)))
10284 8973460 : || TREE_CODE (targ) == NON_LVALUE_EXPR)
10285 3242268 : targ = TREE_OPERAND (targ, 0);
10286 2473876 : if (TREE_CODE (targ) == ADDR_EXPR)
10287 : {
10288 730005 : targ = TREE_OPERAND (targ, 0);
10289 730005 : if (!same_type_ignoring_top_level_qualifiers_p
10290 730005 : (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
10291 : targ = NULL_TREE;
10292 : }
10293 : else
10294 : targ = NULL_TREE;
10295 :
10296 729573 : if (targ)
10297 : arg = targ;
10298 : else
10299 1744303 : arg = cp_build_fold_indirect_ref (arg);
10300 :
10301 : /* In C++17 we shouldn't be copying a TARGET_EXPR except into a
10302 : potentially-overlapping subobject. */
10303 2473876 : if (CHECKING_P && cxx_dialect >= cxx17)
10304 2373427 : gcc_assert (TREE_CODE (arg) != TARGET_EXPR
10305 : || force_elide
10306 : /* It's from binding the ref parm to a packed field. */
10307 : || convs[0]->need_temporary_p
10308 : || seen_error ()
10309 : /* See unsafe_copy_elision_p. */
10310 : || unsafe_return_slot_p (fa));
10311 :
10312 2473876 : bool unsafe = unsafe_copy_elision_p_opt (fa, arg);
10313 2473876 : bool eliding_temp = (TREE_CODE (arg) == TARGET_EXPR && !unsafe);
10314 :
10315 : /* [class.copy]: the copy constructor is implicitly defined even if the
10316 : implementation elided its use. But don't warn about deprecation when
10317 : eliding a temporary, as then no copy is actually performed. */
10318 2473876 : warning_sentinel s (warn_deprecated_copy, eliding_temp);
10319 2473876 : if (force_elide)
10320 : /* The language says this isn't called. */;
10321 2438394 : else if (!trivial)
10322 : {
10323 771886 : if (!mark_used (fn, complain) && !(complain & tf_error))
10324 0 : return error_mark_node;
10325 : already_used = true;
10326 : }
10327 : else
10328 1666508 : cp_handle_deprecated_or_unavailable (fn, complain);
10329 :
10330 82194 : if (eliding_temp && DECL_BASE_CONSTRUCTOR_P (fn)
10331 2474026 : && !make_base_init_ok (arg))
10332 : unsafe = true;
10333 :
10334 : /* If we're creating a temp and we already have one, don't create a
10335 : new one. If we're not creating a temp but we get one, use
10336 : INIT_EXPR to collapse the temp into our target. Otherwise, if the
10337 : ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
10338 : temp or an INIT_EXPR otherwise. */
10339 2473876 : if (is_dummy_object (fa))
10340 : {
10341 1707832 : if (TREE_CODE (arg) == TARGET_EXPR)
10342 : return arg;
10343 1627916 : else if (trivial)
10344 1275417 : return force_target_expr (DECL_CONTEXT (fn), arg, complain);
10345 : }
10346 766044 : else if ((trivial || TREE_CODE (arg) == TARGET_EXPR)
10347 356341 : && !unsafe)
10348 : {
10349 356271 : tree to = cp_build_fold_indirect_ref (fa);
10350 356271 : val = cp_build_init_expr (to, arg);
10351 356271 : return val;
10352 : }
10353 2473876 : }
10354 59728844 : else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
10355 688837 : && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR)
10356 60210086 : && trivial_fn_p (fn))
10357 : {
10358 : /* Don't use cp_build_fold_indirect_ref, op= returns an lvalue even if
10359 : the object argument isn't one. */
10360 271434 : tree to = cp_build_indirect_ref (input_location, argarray[0],
10361 : RO_ARROW, complain);
10362 271434 : tree type = TREE_TYPE (to);
10363 271434 : tree as_base = CLASSTYPE_AS_BASE (type);
10364 271434 : tree arg = argarray[1];
10365 271434 : location_t loc = cp_expr_loc_or_input_loc (arg);
10366 :
10367 271434 : if (is_really_empty_class (type, /*ignore_vptr*/true))
10368 : {
10369 : /* Avoid copying empty classes. */
10370 126324 : val = build2 (COMPOUND_EXPR, type, arg, to);
10371 126324 : suppress_warning (val, OPT_Wunused);
10372 : }
10373 145110 : else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
10374 : {
10375 129248 : if (is_std_init_list (type)
10376 129248 : && conv_binds_ref_to_prvalue (convs[1]))
10377 3 : warning_at (loc, OPT_Winit_list_lifetime,
10378 : "assignment from temporary %<initializer_list%> does "
10379 : "not extend the lifetime of the underlying array");
10380 129248 : arg = cp_build_fold_indirect_ref (arg);
10381 129248 : val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
10382 : }
10383 : else
10384 : {
10385 : /* We must only copy the non-tail padding parts. */
10386 15862 : tree arg0, arg2, t;
10387 15862 : tree array_type, alias_set;
10388 :
10389 15862 : arg2 = TYPE_SIZE_UNIT (as_base);
10390 15862 : to = cp_stabilize_reference (to);
10391 15862 : arg0 = cp_build_addr_expr (to, complain);
10392 :
10393 15862 : array_type = build_array_type (unsigned_char_type_node,
10394 : build_index_type
10395 15862 : (size_binop (MINUS_EXPR,
10396 : arg2, size_int (1))));
10397 15862 : alias_set = build_int_cst (build_pointer_type (type), 0);
10398 15862 : t = build2 (MODIFY_EXPR, void_type_node,
10399 : build2 (MEM_REF, array_type, arg0, alias_set),
10400 : build2 (MEM_REF, array_type, arg, alias_set));
10401 15862 : val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
10402 15862 : suppress_warning (val, OPT_Wunused);
10403 : }
10404 :
10405 271434 : cp_handle_deprecated_or_unavailable (fn, complain);
10406 :
10407 271434 : return val;
10408 : }
10409 59457410 : else if (trivial_fn_p (fn))
10410 : {
10411 2597848 : if (DECL_DESTRUCTOR_P (fn))
10412 815739 : return build_trivial_dtor_call (argarray[0]);
10413 483185 : else if (default_ctor_p (fn))
10414 : {
10415 481593 : if (is_dummy_object (argarray[0]))
10416 142052 : return force_target_expr (DECL_CONTEXT (fn), void_node,
10417 142052 : no_cleanup_complain);
10418 : else
10419 339541 : return cp_build_fold_indirect_ref (argarray[0]);
10420 : }
10421 : }
10422 :
10423 58922350 : gcc_assert (!force_elide);
10424 :
10425 58922534 : if (!already_used
10426 58922534 : && !mark_used (fn, complain))
10427 252 : return error_mark_node;
10428 :
10429 : /* Warn if the built-in writes to an object of a non-trivial type. */
10430 58922278 : if (warn_class_memaccess
10431 1488344 : && vec_safe_length (args) >= 2
10432 59745094 : && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
10433 63879 : maybe_warn_class_memaccess (input_location, fn, args);
10434 :
10435 58922278 : if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
10436 : {
10437 343500 : tree t;
10438 343500 : tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
10439 343500 : DECL_CONTEXT (fn),
10440 : ba_any, NULL, complain);
10441 343500 : gcc_assert (binfo && binfo != error_mark_node);
10442 :
10443 343500 : argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
10444 : complain);
10445 343500 : if (TREE_SIDE_EFFECTS (argarray[0]))
10446 55909 : argarray[0] = save_expr (argarray[0]);
10447 343500 : t = build_pointer_type (TREE_TYPE (fn));
10448 343500 : fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
10449 343500 : TREE_TYPE (fn) = t;
10450 : }
10451 : else
10452 : {
10453 : /* If FN is marked deprecated or unavailable, then we've already
10454 : issued a diagnostic from mark_used above, so avoid redundantly
10455 : issuing another one from build_addr_func. */
10456 58578778 : auto w = make_temp_override (deprecated_state,
10457 58578778 : UNAVAILABLE_DEPRECATED_SUPPRESS);
10458 :
10459 58578778 : fn = build_addr_func (fn, complain);
10460 58578778 : if (fn == error_mark_node)
10461 0 : return error_mark_node;
10462 58578778 : }
10463 :
10464 58922278 : tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag);
10465 58922278 : if (call == error_mark_node)
10466 : return call;
10467 58921799 : if (cand->flags & LOOKUP_LIST_INIT_CTOR)
10468 : {
10469 542010 : tree c = extract_call_expr (call);
10470 : /* build_new_op will clear this when appropriate. */
10471 542010 : CALL_EXPR_ORDERED_ARGS (c) = true;
10472 : }
10473 58921799 : if (warned_p)
10474 : {
10475 305 : tree c = extract_call_expr (call);
10476 305 : if (TREE_CODE (c) == CALL_EXPR)
10477 305 : suppress_warning (c /* Suppress all warnings. */);
10478 : }
10479 58921799 : if (TREE_CODE (fn) == ADDR_EXPR)
10480 : {
10481 58578299 : tree fndecl = STRIP_TEMPLATE (TREE_OPERAND (fn, 0));
10482 58578299 : if (immediate_invocation_p (fndecl))
10483 : {
10484 1631 : tree obj_arg = NULL_TREE;
10485 : /* Undo convert_from_reference called by build_cxx_call. */
10486 1631 : if (REFERENCE_REF_P (call))
10487 1 : call = TREE_OPERAND (call, 0);
10488 3262 : if (DECL_CONSTRUCTOR_P (fndecl))
10489 459 : obj_arg = cand->first_arg ? cand->first_arg : (*args)[0];
10490 459 : if (obj_arg && is_dummy_object (obj_arg))
10491 : {
10492 449 : call = build_cplus_new (DECL_CONTEXT (fndecl), call, complain);
10493 449 : obj_arg = NULL_TREE;
10494 : }
10495 : /* Look through *(const T *)&obj. */
10496 1182 : else if (obj_arg && INDIRECT_REF_P (obj_arg))
10497 : {
10498 5 : tree addr = TREE_OPERAND (obj_arg, 0);
10499 5 : STRIP_NOPS (addr);
10500 5 : if (TREE_CODE (addr) == ADDR_EXPR)
10501 : {
10502 5 : tree typeo = TREE_TYPE (obj_arg);
10503 5 : tree typei = TREE_TYPE (TREE_OPERAND (addr, 0));
10504 5 : if (same_type_ignoring_top_level_qualifiers_p (typeo, typei))
10505 5 : obj_arg = TREE_OPERAND (addr, 0);
10506 : }
10507 : }
10508 1631 : call = cxx_constant_value (call, obj_arg, complain);
10509 1631 : if (obj_arg && !error_operand_p (call))
10510 10 : call = cp_build_init_expr (obj_arg, call);
10511 1631 : call = convert_from_reference (call);
10512 : }
10513 : }
10514 : return call;
10515 : }
10516 :
10517 : namespace
10518 : {
10519 :
10520 : /* Return the DECL of the first non-static subobject of class TYPE
10521 : that satisfies the predicate PRED or null if none can be found. */
10522 :
10523 : template <class Predicate>
10524 : tree
10525 4514 : first_non_static_field (tree type, Predicate pred)
10526 : {
10527 4514 : if (!type || !CLASS_TYPE_P (type))
10528 : return NULL_TREE;
10529 :
10530 75755 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
10531 : {
10532 72029 : if (TREE_CODE (field) != FIELD_DECL)
10533 55620 : continue;
10534 16409 : if (TREE_STATIC (field))
10535 0 : continue;
10536 67137 : if (pred (field))
10537 788 : return field;
10538 : }
10539 :
10540 3726 : int i = 0;
10541 :
10542 3894 : for (tree base_binfo, binfo = TYPE_BINFO (type);
10543 3894 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
10544 : {
10545 180 : tree base = TREE_TYPE (base_binfo);
10546 180 : if (pred (base))
10547 12 : return base;
10548 168 : if (tree field = first_non_static_field (base, pred))
10549 0 : return field;
10550 : }
10551 :
10552 : return NULL_TREE;
10553 : }
10554 :
10555 : struct NonPublicField
10556 : {
10557 15377 : bool operator() (const_tree t) const
10558 : {
10559 15377 : return DECL_P (t) && (TREE_PRIVATE (t) || TREE_PROTECTED (t));
10560 : }
10561 : };
10562 :
10563 : /* Return the DECL of the first non-public subobject of class TYPE
10564 : or null if none can be found. */
10565 :
10566 : static inline tree
10567 4114 : first_non_public_field (tree type)
10568 : {
10569 4114 : return first_non_static_field (type, NonPublicField ());
10570 : }
10571 :
10572 : struct NonTrivialField
10573 : {
10574 1212 : bool operator() (const_tree t) const
10575 : {
10576 1212 : return !trivial_type_p (DECL_P (t) ? TREE_TYPE (t) : t);
10577 : }
10578 : };
10579 :
10580 : /* Return the DECL of the first non-trivial subobject of class TYPE
10581 : or null if none can be found. */
10582 :
10583 : static inline tree
10584 232 : first_non_trivial_field (tree type)
10585 : {
10586 232 : return first_non_static_field (type, NonTrivialField ());
10587 : }
10588 :
10589 : } /* unnamed namespace */
10590 :
10591 : /* Return true if all copy and move assignment operator overloads for
10592 : class TYPE are trivial and at least one of them is not deleted and,
10593 : when ACCESS is set, accessible. Return false otherwise. Set
10594 : HASASSIGN to true when the TYPE has a (not necessarily trivial)
10595 : copy or move assignment. */
10596 :
10597 : static bool
10598 6240 : has_trivial_copy_assign_p (tree type, bool access, bool *hasassign)
10599 : {
10600 6240 : tree fns = get_class_binding (type, assign_op_identifier);
10601 6240 : bool all_trivial = true;
10602 :
10603 : /* Iterate over overloads of the assignment operator, checking
10604 : accessible copy assignments for triviality. */
10605 :
10606 26264 : for (tree f : ovl_range (fns))
10607 : {
10608 : /* Skip operators that aren't copy assignments. */
10609 10807 : if (!copy_fn_p (f))
10610 3895 : continue;
10611 :
10612 6912 : bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
10613 7296 : || accessible_p (TYPE_BINFO (type), f, true));
10614 :
10615 : /* Skip template assignment operators and deleted functions. */
10616 6912 : if (TREE_CODE (f) != FUNCTION_DECL || DECL_DELETED_FN (f))
10617 957 : continue;
10618 :
10619 5955 : if (accessible)
10620 5619 : *hasassign = true;
10621 :
10622 5619 : if (!accessible || !trivial_fn_p (f))
10623 : all_trivial = false;
10624 :
10625 : /* Break early when both properties have been determined. */
10626 5955 : if (*hasassign && !all_trivial)
10627 : break;
10628 : }
10629 :
10630 : /* Return true if they're all trivial and one of the expressions
10631 : TYPE() = TYPE() or TYPE() = (TYPE&)() is valid. */
10632 6240 : tree ref = cp_build_reference_type (type, false);
10633 6240 : return (all_trivial
10634 6240 : && (is_trivially_xible (MODIFY_EXPR, type, type)
10635 484 : || is_trivially_xible (MODIFY_EXPR, type, ref)));
10636 : }
10637 :
10638 : /* Return true if all copy and move ctor overloads for class TYPE are
10639 : trivial and at least one of them is not deleted and, when ACCESS is
10640 : set, accessible. Return false otherwise. Set each element of HASCTOR[]
10641 : to true when the TYPE has a (not necessarily trivial) default and copy
10642 : (or move) ctor, respectively. */
10643 :
10644 : static bool
10645 6240 : has_trivial_copy_p (tree type, bool access, bool hasctor[2])
10646 : {
10647 6240 : tree fns = get_class_binding (type, complete_ctor_identifier);
10648 6240 : bool all_trivial = true;
10649 :
10650 37162 : for (tree f : ovl_range (fns))
10651 : {
10652 : /* Skip template constructors. */
10653 15685 : if (TREE_CODE (f) != FUNCTION_DECL)
10654 140 : continue;
10655 :
10656 15545 : bool cpy_or_move_ctor_p = copy_fn_p (f);
10657 :
10658 : /* Skip ctors other than default, copy, and move. */
10659 15545 : if (!cpy_or_move_ctor_p && !default_ctor_p (f))
10660 3465 : continue;
10661 :
10662 12080 : if (DECL_DELETED_FN (f))
10663 457 : continue;
10664 :
10665 11623 : bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
10666 11879 : || accessible_p (TYPE_BINFO (type), f, true));
10667 :
10668 11463 : if (accessible)
10669 11463 : hasctor[cpy_or_move_ctor_p] = true;
10670 :
10671 11623 : if (cpy_or_move_ctor_p && (!accessible || !trivial_fn_p (f)))
10672 : all_trivial = false;
10673 :
10674 : /* Break early when both properties have been determined. */
10675 11623 : if (hasctor[0] && hasctor[1] && !all_trivial)
10676 : break;
10677 : }
10678 :
10679 6240 : return all_trivial;
10680 : }
10681 :
10682 : /* Issue a warning on a call to the built-in function FNDECL if it is
10683 : a raw memory write whose destination is not an object of (something
10684 : like) trivial or standard layout type with a non-deleted assignment
10685 : and copy ctor. Detects const correctness violations, corrupting
10686 : references, virtual table pointers, and bypassing non-trivial
10687 : assignments. */
10688 :
10689 : static void
10690 63879 : maybe_warn_class_memaccess (location_t loc, tree fndecl,
10691 : const vec<tree, va_gc> *args)
10692 : {
10693 : /* Except for bcopy where it's second, the destination pointer is
10694 : the first argument for all functions handled here. Compute
10695 : the index of the destination and source arguments. */
10696 63879 : unsigned dstidx = DECL_FUNCTION_CODE (fndecl) == BUILT_IN_BCOPY;
10697 63879 : unsigned srcidx = !dstidx;
10698 :
10699 63879 : tree dest = (*args)[dstidx];
10700 63879 : if (!TREE_TYPE (dest)
10701 63879 : || (TREE_CODE (TREE_TYPE (dest)) != ARRAY_TYPE
10702 62775 : && !INDIRECT_TYPE_P (TREE_TYPE (dest))))
10703 59331 : return;
10704 :
10705 22232 : tree srctype = NULL_TREE;
10706 :
10707 : /* Determine the type of the pointed-to object and whether it's
10708 : a complete class type. */
10709 22232 : tree desttype = TREE_TYPE (TREE_TYPE (dest));
10710 :
10711 22232 : if (!desttype || !COMPLETE_TYPE_P (desttype) || !CLASS_TYPE_P (desttype))
10712 : return;
10713 :
10714 : /* Check to see if the raw memory call is made by a non-static member
10715 : function with THIS as the destination argument for the destination
10716 : type. If so, and if the class has no non-trivial bases or members,
10717 : be more permissive. */
10718 6376 : if (current_function_decl
10719 6376 : && DECL_NONSTATIC_MEMBER_FUNCTION_P (current_function_decl)
10720 6711 : && is_this_parameter (tree_strip_nop_conversions (dest)))
10721 : {
10722 272 : tree ctx = DECL_CONTEXT (current_function_decl);
10723 272 : bool special = same_type_ignoring_top_level_qualifiers_p (ctx, desttype);
10724 272 : tree binfo = TYPE_BINFO (ctx);
10725 :
10726 272 : if (special
10727 272 : && !BINFO_VTABLE (binfo)
10728 504 : && !first_non_trivial_field (desttype))
10729 : return;
10730 : }
10731 :
10732 : /* True if the class is trivial. */
10733 6240 : bool trivial = trivial_type_p (desttype);
10734 :
10735 : /* Set to true if DESTYPE has an accessible copy assignment. */
10736 6240 : bool hasassign = false;
10737 : /* True if all of the class' overloaded copy assignment operators
10738 : are all trivial (and not deleted) and at least one of them is
10739 : accessible. */
10740 6240 : bool trivassign = has_trivial_copy_assign_p (desttype, true, &hasassign);
10741 :
10742 : /* Set to true if DESTTYPE has an accessible default and copy ctor,
10743 : respectively. */
10744 6240 : bool hasctors[2] = { false, false };
10745 :
10746 : /* True if all of the class' overloaded copy constructors are all
10747 : trivial (and not deleted) and at least one of them is accessible. */
10748 6240 : bool trivcopy = has_trivial_copy_p (desttype, true, hasctors);
10749 :
10750 : /* Set FLD to the first private/protected member of the class. */
10751 6240 : tree fld = trivial ? first_non_public_field (desttype) : NULL_TREE;
10752 :
10753 : /* The warning format string. */
10754 6240 : const char *warnfmt = NULL;
10755 : /* A suggested alternative to offer instead of the raw memory call.
10756 : Empty string when none can be come up with. */
10757 6240 : const char *suggest = "";
10758 6240 : bool warned = false;
10759 :
10760 6240 : switch (DECL_FUNCTION_CODE (fndecl))
10761 : {
10762 756 : case BUILT_IN_MEMSET:
10763 756 : if (!integer_zerop (maybe_constant_value ((*args)[1])))
10764 : {
10765 : /* Diagnose setting non-copy-assignable or non-trivial types,
10766 : or types with a private member, to (potentially) non-zero
10767 : bytes. Since the value of the bytes being written is unknown,
10768 : suggest using assignment instead (if one exists). Also warn
10769 : for writes into objects for which zero-initialization doesn't
10770 : mean all bits clear (pointer-to-member data, where null is all
10771 : bits set). Since the value being written is (most likely)
10772 : non-zero, simply suggest assignment (but not copy assignment). */
10773 252 : suggest = "; use assignment instead";
10774 252 : if (!trivassign)
10775 : warnfmt = G_("%qD writing to an object of type %#qT with "
10776 : "no trivial copy-assignment");
10777 158 : else if (!trivial)
10778 : warnfmt = G_("%qD writing to an object of non-trivial type %#qT%s");
10779 104 : else if (fld)
10780 : {
10781 32 : const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
10782 32 : warned = warning_at (loc, OPT_Wclass_memaccess,
10783 : "%qD writing to an object of type %#qT with "
10784 : "%qs member %qD",
10785 : fndecl, desttype, access, fld);
10786 : }
10787 72 : else if (!zero_init_p (desttype))
10788 16 : warnfmt = G_("%qD writing to an object of type %#qT containing "
10789 : "a pointer to data member%s");
10790 :
10791 : break;
10792 : }
10793 : /* Fall through. */
10794 :
10795 660 : case BUILT_IN_BZERO:
10796 : /* Similarly to the above, diagnose clearing non-trivial or non-
10797 : standard layout objects, or objects of types with no assignmenmt.
10798 : Since the value being written is known to be zero, suggest either
10799 : copy assignment, copy ctor, or default ctor as an alternative,
10800 : depending on what's available. */
10801 :
10802 660 : if (hasassign && hasctors[0])
10803 : suggest = G_("; use assignment or value-initialization instead");
10804 68 : else if (hasassign)
10805 : suggest = G_("; use assignment instead");
10806 44 : else if (hasctors[0])
10807 22 : suggest = G_("; use value-initialization instead");
10808 :
10809 660 : if (!trivassign)
10810 : warnfmt = G_("%qD clearing an object of type %#qT with "
10811 : "no trivial copy-assignment%s");
10812 518 : else if (!trivial)
10813 : warnfmt = G_("%qD clearing an object of non-trivial type %#qT%s");
10814 424 : else if (!zero_init_p (desttype))
10815 16 : warnfmt = G_("%qD clearing an object of type %#qT containing "
10816 : "a pointer-to-member%s");
10817 : break;
10818 :
10819 3288 : case BUILT_IN_BCOPY:
10820 3288 : case BUILT_IN_MEMCPY:
10821 3288 : case BUILT_IN_MEMMOVE:
10822 3288 : case BUILT_IN_MEMPCPY:
10823 : /* Determine the type of the source object. */
10824 3288 : srctype = TREE_TYPE ((*args)[srcidx]);
10825 3288 : if (!srctype || !INDIRECT_TYPE_P (srctype))
10826 0 : srctype = void_type_node;
10827 : else
10828 3288 : srctype = TREE_TYPE (srctype);
10829 :
10830 : /* Since it's impossible to determine wheter the byte copy is
10831 : being used in place of assignment to an existing object or
10832 : as a substitute for initialization, assume it's the former.
10833 : Determine the best alternative to use instead depending on
10834 : what's not deleted. */
10835 3288 : if (hasassign && hasctors[1])
10836 : suggest = G_("; use copy-assignment or copy-initialization instead");
10837 688 : else if (hasassign)
10838 : suggest = G_("; use copy-assignment instead");
10839 478 : else if (hasctors[1])
10840 403 : suggest = G_("; use copy-initialization instead");
10841 :
10842 3288 : if (!trivassign)
10843 : warnfmt = G_("%qD writing to an object of type %#qT with no trivial "
10844 : "copy-assignment%s");
10845 2232 : else if (!trivially_copyable_p (desttype))
10846 : warnfmt = G_("%qD writing to an object of non-trivially copyable "
10847 : "type %#qT%s");
10848 1793 : else if (!trivcopy)
10849 : warnfmt = G_("%qD writing to an object with a deleted copy constructor");
10850 :
10851 1793 : else if (!trivial
10852 263 : && !VOID_TYPE_P (srctype)
10853 195 : && !is_byte_access_type (srctype)
10854 1905 : && !same_type_ignoring_top_level_qualifiers_p (desttype,
10855 : srctype))
10856 : {
10857 : /* Warn when copying into a non-trivial object from an object
10858 : of a different type other than void or char. */
10859 72 : warned = warning_at (loc, OPT_Wclass_memaccess,
10860 : "%qD copying an object of non-trivial type "
10861 : "%#qT from an array of %#qT",
10862 : fndecl, desttype, srctype);
10863 : }
10864 1721 : else if (fld
10865 576 : && !VOID_TYPE_P (srctype)
10866 512 : && !is_byte_access_type (srctype)
10867 2105 : && !same_type_ignoring_top_level_qualifiers_p (desttype,
10868 : srctype))
10869 : {
10870 288 : const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
10871 288 : warned = warning_at (loc, OPT_Wclass_memaccess,
10872 : "%qD copying an object of type %#qT with "
10873 : "%qs member %qD from an array of %#qT; use "
10874 : "assignment or copy-initialization instead",
10875 : fndecl, desttype, access, fld, srctype);
10876 : }
10877 1433 : else if (!trivial && vec_safe_length (args) > 2)
10878 : {
10879 191 : tree sz = maybe_constant_value ((*args)[2]);
10880 191 : if (!tree_fits_uhwi_p (sz))
10881 : break;
10882 :
10883 : /* Finally, warn on partial copies. */
10884 131 : unsigned HOST_WIDE_INT typesize
10885 131 : = tree_to_uhwi (TYPE_SIZE_UNIT (desttype));
10886 131 : if (typesize == 0)
10887 : break;
10888 128 : if (unsigned HOST_WIDE_INT partial = tree_to_uhwi (sz) % typesize)
10889 16 : warned = warning_at (loc, OPT_Wclass_memaccess,
10890 : (typesize - partial > 1
10891 : ? G_("%qD writing to an object of "
10892 : "a non-trivial type %#qT leaves %wu "
10893 : "bytes unchanged")
10894 : : G_("%qD writing to an object of "
10895 : "a non-trivial type %#qT leaves %wu "
10896 : "byte unchanged")),
10897 : fndecl, desttype, typesize - partial);
10898 : }
10899 : break;
10900 :
10901 348 : case BUILT_IN_REALLOC:
10902 :
10903 348 : if (!trivially_copyable_p (desttype))
10904 : warnfmt = G_("%qD moving an object of non-trivially copyable type "
10905 : "%#qT; use %<new%> and %<delete%> instead");
10906 186 : else if (!trivcopy)
10907 : warnfmt = G_("%qD moving an object of type %#qT with deleted copy "
10908 : "constructor; use %<new%> and %<delete%> instead");
10909 182 : else if (!get_dtor (desttype, tf_none))
10910 : warnfmt = G_("%qD moving an object of type %#qT with deleted "
10911 : "destructor");
10912 170 : else if (!trivial)
10913 : {
10914 28 : tree sz = maybe_constant_value ((*args)[1]);
10915 28 : if (TREE_CODE (sz) == INTEGER_CST
10916 28 : && tree_int_cst_lt (sz, TYPE_SIZE_UNIT (desttype)))
10917 : /* Finally, warn on reallocation into insufficient space. */
10918 4 : warned = warning_at (loc, OPT_Wclass_memaccess,
10919 : "%qD moving an object of non-trivial type "
10920 : "%#qT and size %E into a region of size %E",
10921 4 : fndecl, desttype, TYPE_SIZE_UNIT (desttype),
10922 : sz);
10923 : }
10924 : break;
10925 :
10926 : default:
10927 : return;
10928 : }
10929 :
10930 4548 : if (warnfmt)
10931 : {
10932 2089 : if (suggest)
10933 2089 : warned = warning_at (loc, OPT_Wclass_memaccess,
10934 : warnfmt, fndecl, desttype, suggest);
10935 : else
10936 : warned = warning_at (loc, OPT_Wclass_memaccess,
10937 : warnfmt, fndecl, desttype);
10938 : }
10939 :
10940 4548 : if (warned)
10941 2497 : inform (location_of (desttype), "%#qT declared here", desttype);
10942 : }
10943 :
10944 : /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
10945 : If FN is the result of resolving an overloaded target built-in,
10946 : ORIG_FNDECL is the original function decl, otherwise it is null.
10947 : This function performs no overload resolution, conversion, or other
10948 : high-level operations. */
10949 :
10950 : tree
10951 60640270 : build_cxx_call (tree fn, int nargs, tree *argarray,
10952 : tsubst_flags_t complain, tree orig_fndecl)
10953 : {
10954 60640270 : tree fndecl;
10955 :
10956 : /* Remember roughly where this call is. */
10957 60640270 : location_t loc = cp_expr_loc_or_input_loc (fn);
10958 60640270 : fn = build_call_a (fn, nargs, argarray);
10959 60640270 : SET_EXPR_LOCATION (fn, loc);
10960 :
10961 60640270 : fndecl = get_callee_fndecl (fn);
10962 60640270 : if (!orig_fndecl)
10963 60640270 : orig_fndecl = fndecl;
10964 :
10965 : /* Check that arguments to builtin functions match the expectations. */
10966 60640270 : if (fndecl
10967 59735699 : && !processing_template_decl
10968 120336354 : && fndecl_built_in_p (fndecl))
10969 : {
10970 : int i;
10971 :
10972 : /* We need to take care that values to BUILT_IN_NORMAL
10973 : are reduced. */
10974 14239018 : for (i = 0; i < nargs; i++)
10975 9145130 : argarray[i] = maybe_constant_value (argarray[i]);
10976 :
10977 5093888 : if (!check_builtin_function_arguments (EXPR_LOCATION (fn), vNULL, fndecl,
10978 : orig_fndecl, nargs, argarray))
10979 420 : return error_mark_node;
10980 5093468 : else if (fndecl_built_in_p (fndecl, BUILT_IN_CLEAR_PADDING))
10981 : {
10982 363 : tree arg0 = argarray[0];
10983 363 : STRIP_NOPS (arg0);
10984 363 : if (TREE_CODE (arg0) == ADDR_EXPR
10985 282 : && DECL_P (TREE_OPERAND (arg0, 0))
10986 624 : && same_type_ignoring_top_level_qualifiers_p
10987 261 : (TREE_TYPE (TREE_TYPE (argarray[0])),
10988 261 : TREE_TYPE (TREE_TYPE (arg0))))
10989 : /* For __builtin_clear_padding (&var) we know the type
10990 : is for a complete object, so there is no risk in clearing
10991 : padding that is reused in some derived class member. */;
10992 116 : else if (!trivially_copyable_p (TREE_TYPE (TREE_TYPE (argarray[0]))))
10993 : {
10994 24 : error_at (EXPR_LOC_OR_LOC (argarray[0], input_location),
10995 : "argument %u in call to function %qE "
10996 : "has pointer to a non-trivially-copyable type (%qT)",
10997 24 : 1, fndecl, TREE_TYPE (argarray[0]));
10998 24 : return error_mark_node;
10999 : }
11000 : }
11001 : }
11002 :
11003 60639826 : if (VOID_TYPE_P (TREE_TYPE (fn)))
11004 : return fn;
11005 :
11006 : /* 5.2.2/11: If a function call is a prvalue of object type: if the
11007 : function call is either the operand of a decltype-specifier or the
11008 : right operand of a comma operator that is the operand of a
11009 : decltype-specifier, a temporary object is not introduced for the
11010 : prvalue. The type of the prvalue may be incomplete. */
11011 43739308 : if (!(complain & tf_decltype))
11012 : {
11013 40154142 : fn = require_complete_type (fn, complain);
11014 40154142 : if (fn == error_mark_node)
11015 : return error_mark_node;
11016 :
11017 43671237 : if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
11018 : {
11019 3517000 : fn = build_cplus_new (TREE_TYPE (fn), fn, complain);
11020 3517000 : maybe_warn_parm_abi (TREE_TYPE (fn), loc);
11021 : }
11022 : }
11023 43739276 : return convert_from_reference (fn);
11024 : }
11025 :
11026 : /* Returns the value to use for the in-charge parameter when making a
11027 : call to a function with the indicated NAME.
11028 :
11029 : FIXME:Can't we find a neater way to do this mapping? */
11030 :
11031 : tree
11032 979443 : in_charge_arg_for_name (tree name)
11033 : {
11034 979443 : if (IDENTIFIER_CTOR_P (name))
11035 : {
11036 14654 : if (name == complete_ctor_identifier)
11037 7327 : return integer_one_node;
11038 7327 : gcc_checking_assert (name == base_ctor_identifier);
11039 : }
11040 : else
11041 : {
11042 964789 : if (name == complete_dtor_identifier)
11043 5637 : return integer_two_node;
11044 959152 : else if (name == deleting_dtor_identifier)
11045 0 : return integer_three_node;
11046 959152 : gcc_checking_assert (name == base_dtor_identifier);
11047 : }
11048 :
11049 966479 : return integer_zero_node;
11050 : }
11051 :
11052 : /* We've built up a constructor call RET. Complain if it delegates to the
11053 : constructor we're currently compiling. */
11054 :
11055 : static void
11056 188964 : check_self_delegation (tree ret)
11057 : {
11058 188964 : if (TREE_CODE (ret) == TARGET_EXPR)
11059 0 : ret = TARGET_EXPR_INITIAL (ret);
11060 188964 : tree fn = cp_get_callee_fndecl_nofold (ret);
11061 188964 : if (fn && DECL_ABSTRACT_ORIGIN (fn) == current_function_decl)
11062 46 : error ("constructor delegates to itself");
11063 188964 : }
11064 :
11065 : /* Build a call to a constructor, destructor, or an assignment
11066 : operator for INSTANCE, an expression with class type. NAME
11067 : indicates the special member function to call; *ARGS are the
11068 : arguments. ARGS may be NULL. This may change ARGS. BINFO
11069 : indicates the base of INSTANCE that is to be passed as the `this'
11070 : parameter to the member function called.
11071 :
11072 : FLAGS are the LOOKUP_* flags to use when processing the call.
11073 :
11074 : If NAME indicates a complete object constructor, INSTANCE may be
11075 : NULL_TREE. In this case, the caller will call build_cplus_new to
11076 : store the newly constructed object into a VAR_DECL. */
11077 :
11078 : tree
11079 15835507 : build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
11080 : tree binfo, int flags, tsubst_flags_t complain)
11081 : {
11082 15835507 : tree fns;
11083 : /* The type of the subobject to be constructed or destroyed. */
11084 15835507 : tree class_type;
11085 15835507 : vec<tree, va_gc> *allocated = NULL;
11086 15835507 : tree ret;
11087 :
11088 15835507 : gcc_assert (IDENTIFIER_CDTOR_P (name) || name == assign_op_identifier);
11089 :
11090 15835507 : if (error_operand_p (instance))
11091 0 : return error_mark_node;
11092 :
11093 15835507 : if (IDENTIFIER_DTOR_P (name))
11094 : {
11095 5453231 : gcc_assert (args == NULL || vec_safe_is_empty (*args));
11096 5453231 : if (!type_build_dtor_call (TREE_TYPE (instance)))
11097 : /* Shortcut to avoid lazy destructor declaration. */
11098 40147 : return build_trivial_dtor_call (instance);
11099 : }
11100 :
11101 15795360 : if (TYPE_P (binfo))
11102 : {
11103 : /* Resolve the name. */
11104 11663713 : if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
11105 7 : return error_mark_node;
11106 :
11107 11663706 : binfo = TYPE_BINFO (binfo);
11108 : }
11109 :
11110 11663706 : gcc_assert (binfo != NULL_TREE);
11111 :
11112 15795353 : class_type = BINFO_TYPE (binfo);
11113 :
11114 : /* Handle the special case where INSTANCE is NULL_TREE. */
11115 15795353 : if (name == complete_ctor_identifier && !instance)
11116 6864635 : instance = build_dummy_object (class_type);
11117 : else
11118 : {
11119 : /* Convert to the base class, if necessary. */
11120 8930718 : if (!same_type_ignoring_top_level_qualifiers_p
11121 8930718 : (TREE_TYPE (instance), BINFO_TYPE (binfo)))
11122 : {
11123 1197864 : if (IDENTIFIER_CDTOR_P (name))
11124 : /* For constructors and destructors, either the base is
11125 : non-virtual, or it is virtual but we are doing the
11126 : conversion from a constructor or destructor for the
11127 : complete object. In either case, we can convert
11128 : statically. */
11129 1183336 : instance = convert_to_base_statically (instance, binfo);
11130 : else
11131 : {
11132 : /* However, for assignment operators, we must convert
11133 : dynamically if the base is virtual. */
11134 14528 : gcc_checking_assert (name == assign_op_identifier);
11135 14528 : instance = build_base_path (PLUS_EXPR, instance,
11136 : binfo, /*nonnull=*/1, complain);
11137 : }
11138 : }
11139 : }
11140 :
11141 15795353 : gcc_assert (instance != NULL_TREE);
11142 :
11143 : /* In C++17, "If the initializer expression is a prvalue and the
11144 : cv-unqualified version of the source type is the same class as the class
11145 : of the destination, the initializer expression is used to initialize the
11146 : destination object." Handle that here to avoid doing overload
11147 : resolution. */
11148 15795353 : if (cxx_dialect >= cxx17
11149 15498654 : && args && vec_safe_length (*args) == 1
11150 23401931 : && !unsafe_return_slot_p (instance))
11151 : {
11152 6953075 : tree arg = (**args)[0];
11153 :
11154 312400 : if (BRACE_ENCLOSED_INITIALIZER_P (arg)
11155 312393 : && !TYPE_HAS_LIST_CTOR (class_type)
11156 278098 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (arg)
11157 7231167 : && CONSTRUCTOR_NELTS (arg) == 1)
11158 140568 : arg = CONSTRUCTOR_ELT (arg, 0)->value;
11159 :
11160 6953075 : if ((TREE_CODE (arg) == TARGET_EXPR
11161 6953075 : || TREE_CODE (arg) == CONSTRUCTOR)
11162 11235832 : && (same_type_ignoring_top_level_qualifiers_p
11163 4282757 : (class_type, TREE_TYPE (arg))))
11164 : {
11165 3884529 : if (is_dummy_object (instance))
11166 : return arg;
11167 106114 : else if (TREE_CODE (arg) == TARGET_EXPR)
11168 106114 : TARGET_EXPR_DIRECT_INIT_P (arg) = true;
11169 :
11170 106114 : if ((complain & tf_error)
11171 106112 : && (flags & LOOKUP_DELEGATING_CONS))
11172 0 : check_self_delegation (arg);
11173 : /* Avoid change of behavior on Wunused-var-2.C. */
11174 106114 : instance = mark_lvalue_use (instance);
11175 106114 : return cp_build_init_expr (instance, arg);
11176 : }
11177 : }
11178 :
11179 11910824 : fns = lookup_fnfields (binfo, name, 1, complain);
11180 :
11181 : /* When making a call to a constructor or destructor for a subobject
11182 : that uses virtual base classes, pass down a pointer to a VTT for
11183 : the subobject. */
11184 11910824 : if ((name == base_ctor_identifier
11185 10518424 : || name == base_dtor_identifier)
11186 13094202 : && CLASSTYPE_VBASECLASSES (class_type))
11187 : {
11188 43422 : tree vtt;
11189 43422 : tree sub_vtt;
11190 :
11191 : /* If the current function is a complete object constructor
11192 : or destructor, then we fetch the VTT directly.
11193 : Otherwise, we look it up using the VTT we were given. */
11194 43422 : vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
11195 43422 : vtt = decay_conversion (vtt, complain);
11196 43422 : if (vtt == error_mark_node)
11197 0 : return error_mark_node;
11198 43422 : vtt = build_if_in_charge (vtt, current_vtt_parm);
11199 43422 : if (BINFO_SUBVTT_INDEX (binfo))
11200 43318 : sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
11201 : else
11202 104 : sub_vtt = vtt;
11203 :
11204 43422 : if (args == NULL)
11205 : {
11206 27424 : allocated = make_tree_vector ();
11207 27424 : args = &allocated;
11208 : }
11209 :
11210 43422 : vec_safe_insert (*args, 0, sub_vtt);
11211 : }
11212 :
11213 23821648 : ret = build_new_method_call (instance, fns, args,
11214 11910824 : TYPE_BINFO (BINFO_TYPE (binfo)),
11215 : flags, /*fn=*/NULL,
11216 : complain);
11217 :
11218 11910824 : if (allocated != NULL)
11219 27424 : release_tree_vector (allocated);
11220 :
11221 11910824 : if ((complain & tf_error)
11222 11125982 : && (flags & LOOKUP_DELEGATING_CONS)
11223 189026 : && name == complete_ctor_identifier)
11224 188964 : check_self_delegation (ret);
11225 :
11226 : return ret;
11227 : }
11228 :
11229 : /* Return the NAME, as a C string. The NAME indicates a function that
11230 : is a member of TYPE. *FREE_P is set to true if the caller must
11231 : free the memory returned.
11232 :
11233 : Rather than go through all of this, we should simply set the names
11234 : of constructors and destructors appropriately, and dispense with
11235 : ctor_identifier, dtor_identifier, etc. */
11236 :
11237 : static char *
11238 77 : name_as_c_string (tree name, tree type, bool *free_p)
11239 : {
11240 77 : const char *pretty_name;
11241 :
11242 : /* Assume that we will not allocate memory. */
11243 77 : *free_p = false;
11244 : /* Constructors and destructors are special. */
11245 77 : if (IDENTIFIER_CDTOR_P (name))
11246 : {
11247 49 : pretty_name
11248 49 : = identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type)));
11249 : /* For a destructor, add the '~'. */
11250 49 : if (IDENTIFIER_DTOR_P (name))
11251 : {
11252 0 : pretty_name = concat ("~", pretty_name, NULL);
11253 : /* Remember that we need to free the memory allocated. */
11254 0 : *free_p = true;
11255 : }
11256 : }
11257 28 : else if (IDENTIFIER_CONV_OP_P (name))
11258 : {
11259 0 : pretty_name = concat ("operator ",
11260 0 : type_as_string_translate (TREE_TYPE (name),
11261 : TFF_PLAIN_IDENTIFIER),
11262 : NULL);
11263 : /* Remember that we need to free the memory allocated. */
11264 0 : *free_p = true;
11265 : }
11266 : else
11267 28 : pretty_name = identifier_to_locale (IDENTIFIER_POINTER (name));
11268 :
11269 77 : return CONST_CAST (char *, pretty_name);
11270 : }
11271 :
11272 : /* If CANDIDATES contains exactly one candidate, return it, otherwise
11273 : return NULL. */
11274 :
11275 : static z_candidate *
11276 491 : single_z_candidate (z_candidate *candidates)
11277 : {
11278 0 : if (candidates == NULL)
11279 : return NULL;
11280 :
11281 487 : if (candidates->next)
11282 0 : return NULL;
11283 :
11284 : return candidates;
11285 : }
11286 :
11287 : /* If CANDIDATE is invalid due to a bad argument type, return the
11288 : pertinent conversion_info.
11289 :
11290 : Otherwise, return NULL. */
11291 :
11292 : static const conversion_info *
11293 243 : maybe_get_bad_conversion_for_unmatched_call (const z_candidate *candidate)
11294 : {
11295 : /* Must be an rr_arg_conversion or rr_bad_arg_conversion. */
11296 243 : rejection_reason *r = candidate->reason;
11297 :
11298 243 : if (r == NULL)
11299 : return NULL;
11300 :
11301 243 : switch (r->code)
11302 : {
11303 : default:
11304 : return NULL;
11305 :
11306 56 : case rr_arg_conversion:
11307 56 : return &r->u.conversion;
11308 :
11309 8 : case rr_bad_arg_conversion:
11310 8 : return &r->u.bad_conversion;
11311 : }
11312 : }
11313 :
11314 : /* Issue an error and note complaining about a bad argument type at a
11315 : callsite with a single candidate FNDECL.
11316 :
11317 : ARG_LOC is the location of the argument (or UNKNOWN_LOCATION, in which
11318 : case input_location is used).
11319 : FROM_TYPE is the type of the actual argument; TO_TYPE is the type of
11320 : the formal parameter. */
11321 :
11322 : void
11323 160 : complain_about_bad_argument (location_t arg_loc,
11324 : tree from_type, tree to_type,
11325 : tree fndecl, int parmnum)
11326 : {
11327 160 : auto_diagnostic_group d;
11328 160 : range_label_for_type_mismatch rhs_label (from_type, to_type);
11329 160 : range_label *label = &rhs_label;
11330 160 : if (arg_loc == UNKNOWN_LOCATION)
11331 : {
11332 4 : arg_loc = input_location;
11333 4 : label = NULL;
11334 : }
11335 160 : gcc_rich_location richloc (arg_loc, label);
11336 160 : error_at (&richloc,
11337 : "cannot convert %qH to %qI",
11338 : from_type, to_type);
11339 160 : maybe_inform_about_fndecl_for_bogus_argument_init (fndecl,
11340 : parmnum);
11341 160 : }
11342 :
11343 : /* Subroutine of build_new_method_call_1, for where there are no viable
11344 : candidates for the call. */
11345 :
11346 : static void
11347 499 : complain_about_no_candidates_for_method_call (tree instance,
11348 : z_candidate *candidates,
11349 : tree explicit_targs,
11350 : tree basetype,
11351 : tree optype, tree name,
11352 : bool skip_first_for_error,
11353 : vec<tree, va_gc> *user_args)
11354 : {
11355 499 : auto_diagnostic_group d;
11356 499 : if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
11357 0 : cxx_incomplete_type_error (instance, basetype);
11358 499 : else if (optype)
11359 8 : error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
11360 : basetype, optype, build_tree_list_vec (user_args),
11361 8 : TREE_TYPE (instance));
11362 : else
11363 : {
11364 : /* Special-case for when there's a single candidate that's failing
11365 : due to a bad argument type. */
11366 491 : if (z_candidate *candidate = single_z_candidate (candidates))
11367 243 : if (const conversion_info *conv
11368 243 : = maybe_get_bad_conversion_for_unmatched_call (candidate))
11369 : {
11370 64 : tree from_type = conv->from;
11371 64 : if (!TYPE_P (conv->from))
11372 8 : from_type = lvalue_type (conv->from);
11373 64 : complain_about_bad_argument (conv->loc,
11374 64 : from_type, conv->to_type,
11375 64 : candidate->fn, conv->n_arg);
11376 64 : return;
11377 : }
11378 :
11379 427 : tree arglist = build_tree_list_vec (user_args);
11380 427 : tree errname = name;
11381 427 : bool twiddle = false;
11382 427 : if (IDENTIFIER_CDTOR_P (errname))
11383 : {
11384 249 : twiddle = IDENTIFIER_DTOR_P (errname);
11385 249 : errname = constructor_name (basetype);
11386 : }
11387 427 : if (explicit_targs)
11388 23 : errname = lookup_template_function (errname, explicit_targs);
11389 427 : if (skip_first_for_error)
11390 4 : arglist = TREE_CHAIN (arglist);
11391 854 : error ("no matching function for call to %<%T::%s%E(%A)%#V%>",
11392 427 : basetype, &"~"[!twiddle], errname, arglist,
11393 427 : TREE_TYPE (instance));
11394 : }
11395 435 : print_z_candidates (location_of (name), candidates);
11396 499 : }
11397 :
11398 : /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
11399 : be set, upon return, to the function called. ARGS may be NULL.
11400 : This may change ARGS. */
11401 :
11402 : tree
11403 49294798 : build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args,
11404 : tree conversion_path, int flags,
11405 : tree *fn_p, tsubst_flags_t complain)
11406 : {
11407 49294798 : struct z_candidate *candidates = 0, *cand;
11408 49294798 : tree explicit_targs = NULL_TREE;
11409 49294798 : tree basetype = NULL_TREE;
11410 49294798 : tree access_binfo;
11411 49294798 : tree optype;
11412 49294798 : tree first_mem_arg = NULL_TREE;
11413 49294798 : tree name;
11414 49294798 : bool skip_first_for_error;
11415 49294798 : vec<tree, va_gc> *user_args;
11416 49294798 : tree call;
11417 49294798 : tree fn;
11418 49294798 : int template_only = 0;
11419 49294798 : bool any_viable_p;
11420 49294798 : tree orig_instance;
11421 49294798 : tree orig_fns;
11422 49294798 : vec<tree, va_gc> *orig_args = NULL;
11423 49294798 : void *p;
11424 :
11425 49294798 : auto_cond_timevar tv (TV_OVERLOAD);
11426 :
11427 49294798 : gcc_assert (instance != NULL_TREE);
11428 :
11429 : /* We don't know what function we're going to call, yet. */
11430 49294798 : if (fn_p)
11431 14278393 : *fn_p = NULL_TREE;
11432 :
11433 49294798 : if (error_operand_p (instance)
11434 49294798 : || !fns || error_operand_p (fns))
11435 953805 : return error_mark_node;
11436 :
11437 48340993 : if (!BASELINK_P (fns))
11438 : {
11439 0 : if (complain & tf_error)
11440 0 : error ("call to non-function %qD", fns);
11441 0 : return error_mark_node;
11442 : }
11443 :
11444 48340993 : orig_instance = instance;
11445 48340993 : orig_fns = fns;
11446 :
11447 : /* Dismantle the baselink to collect all the information we need. */
11448 48340993 : if (!conversion_path)
11449 23116095 : conversion_path = BASELINK_BINFO (fns);
11450 48340993 : access_binfo = BASELINK_ACCESS_BINFO (fns);
11451 48340993 : optype = BASELINK_OPTYPE (fns);
11452 48340993 : fns = BASELINK_FUNCTIONS (fns);
11453 48340993 : if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
11454 : {
11455 6098372 : explicit_targs = TREE_OPERAND (fns, 1);
11456 6098372 : fns = TREE_OPERAND (fns, 0);
11457 6098372 : template_only = 1;
11458 : }
11459 48340993 : gcc_assert (OVL_P (fns));
11460 48340993 : fn = OVL_FIRST (fns);
11461 48340993 : name = DECL_NAME (fn);
11462 :
11463 48340993 : basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
11464 48340993 : gcc_assert (CLASS_TYPE_P (basetype));
11465 :
11466 48340993 : user_args = args == NULL ? NULL : *args;
11467 : /* Under DR 147 A::A() is an invalid constructor call,
11468 : not a functional cast. */
11469 48340993 : if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
11470 : {
11471 83 : if (! (complain & tf_error))
11472 12 : return error_mark_node;
11473 :
11474 71 : basetype = DECL_CONTEXT (fn);
11475 71 : name = constructor_name (basetype);
11476 71 : auto_diagnostic_group d;
11477 71 : if (permerror (input_location,
11478 : "cannot call constructor %<%T::%D%> directly",
11479 : basetype, name))
11480 71 : inform (input_location, "for a function-style cast, remove the "
11481 : "redundant %<::%D%>", name);
11482 71 : call = build_functional_cast (input_location, basetype,
11483 : build_tree_list_vec (user_args),
11484 : complain);
11485 71 : return call;
11486 71 : }
11487 :
11488 48340910 : if (processing_template_decl)
11489 : {
11490 5611713 : orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
11491 5611713 : instance = build_non_dependent_expr (instance);
11492 5611713 : if (args != NULL)
11493 5549919 : make_args_non_dependent (*args);
11494 : }
11495 :
11496 : /* Process the argument list. */
11497 48340910 : if (args != NULL && *args != NULL)
11498 : {
11499 41049686 : *args = resolve_args (*args, complain);
11500 41049686 : if (*args == NULL)
11501 113 : return error_mark_node;
11502 : user_args = *args;
11503 : }
11504 :
11505 : /* Consider the object argument to be used even if we end up selecting a
11506 : static member function. */
11507 48340797 : instance = mark_type_use (instance);
11508 :
11509 : /* Figure out whether to skip the first argument for the error
11510 : message we will display to users if an error occurs. We don't
11511 : want to display any compiler-generated arguments. The "this"
11512 : pointer hasn't been added yet. However, we must remove the VTT
11513 : pointer if this is a call to a base-class constructor or
11514 : destructor. */
11515 48340797 : skip_first_for_error = false;
11516 48340797 : if (IDENTIFIER_CDTOR_P (name))
11517 : {
11518 : /* Callers should explicitly indicate whether they want to ctor
11519 : the complete object or just the part without virtual bases. */
11520 23931757 : gcc_assert (name != ctor_identifier);
11521 :
11522 : /* Remove the VTT pointer, if present. */
11523 22539364 : if ((name == base_ctor_identifier || name == base_dtor_identifier)
11524 25115135 : && CLASSTYPE_VBASECLASSES (basetype))
11525 : skip_first_for_error = true;
11526 :
11527 : /* It's OK to call destructors and constructors on cv-qualified
11528 : objects. Therefore, convert the INSTANCE to the unqualified
11529 : type, if necessary. */
11530 23931757 : if (!same_type_p (basetype, TREE_TYPE (instance)))
11531 : {
11532 219615 : instance = build_this (instance);
11533 219615 : instance = build_nop (build_pointer_type (basetype), instance);
11534 219615 : instance = build_fold_indirect_ref (instance);
11535 : }
11536 : }
11537 : else
11538 48818080 : gcc_assert (!DECL_DESTRUCTOR_P (fn) && !DECL_CONSTRUCTOR_P (fn));
11539 :
11540 : /* For the overload resolution we need to find the actual `this`
11541 : that would be captured if the call turns out to be to a
11542 : non-static member function. Do not actually capture it at this
11543 : point. */
11544 96681594 : if (DECL_CONSTRUCTOR_P (fn))
11545 : /* Constructors don't use the enclosing 'this'. */
11546 : first_mem_arg = instance;
11547 : else
11548 37689221 : first_mem_arg = maybe_resolve_dummy (instance, false);
11549 :
11550 : /* Get the high-water mark for the CONVERSION_OBSTACK. */
11551 48340797 : p = conversion_obstack_alloc (0);
11552 :
11553 : /* The number of arguments artificial parms in ARGS; we subtract one because
11554 : there's no 'this' in ARGS. */
11555 48340797 : unsigned skip = num_artificial_parms_for (fn) - 1;
11556 :
11557 : /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
11558 : initializer, not T({ }). */
11559 48340797 : if (DECL_CONSTRUCTOR_P (fn)
11560 8758923 : && vec_safe_length (user_args) > skip
11561 55838560 : && DIRECT_LIST_INIT_P ((*user_args)[skip]))
11562 : {
11563 319422 : tree init_list = (*user_args)[skip];
11564 319422 : tree init = NULL_TREE;
11565 :
11566 319422 : gcc_assert (user_args->length () == skip + 1
11567 : && !(flags & LOOKUP_ONLYCONVERTING));
11568 :
11569 : /* If the initializer list has no elements and T is a class type with
11570 : a default constructor, the object is value-initialized. Handle
11571 : this here so we don't need to handle it wherever we use
11572 : build_special_member_call. */
11573 319422 : if (CONSTRUCTOR_NELTS (init_list) == 0
11574 50379 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
11575 : /* For a user-provided default constructor, use the normal
11576 : mechanisms so that protected access works. */
11577 50379 : && type_has_non_user_provided_default_constructor (basetype)
11578 315971 : && !processing_template_decl)
11579 46925 : init = build_value_init (basetype, complain);
11580 :
11581 : /* If BASETYPE is an aggregate, we need to do aggregate
11582 : initialization. */
11583 272497 : else if (CP_AGGREGATE_TYPE_P (basetype))
11584 : {
11585 34 : init = reshape_init (basetype, init_list, complain);
11586 34 : init = digest_init (basetype, init, complain);
11587 : }
11588 :
11589 46959 : if (init)
11590 : {
11591 46959 : if (is_dummy_object (instance))
11592 13729 : return get_target_expr (init, complain);
11593 33230 : return cp_build_init_expr (instance, init);
11594 : }
11595 :
11596 : /* Otherwise go ahead with overload resolution. */
11597 272463 : add_list_candidates (fns, first_mem_arg, user_args,
11598 : basetype, explicit_targs, template_only,
11599 : conversion_path, access_binfo, flags,
11600 : &candidates, complain);
11601 : }
11602 : else
11603 48021375 : add_candidates (fns, first_mem_arg, user_args, optype,
11604 : explicit_targs, template_only, conversion_path,
11605 : access_binfo, flags, &candidates, complain);
11606 :
11607 48293838 : any_viable_p = false;
11608 48293838 : candidates = splice_viable (candidates, false, &any_viable_p);
11609 :
11610 48293838 : if (!any_viable_p)
11611 : {
11612 : /* [dcl.init], 17.6.2.2:
11613 :
11614 : Otherwise, if no constructor is viable, the destination type is
11615 : a (possibly cv-qualified) aggregate class A, and the initializer
11616 : is a parenthesized expression-list, the object is initialized as
11617 : follows...
11618 :
11619 : We achieve this by building up a CONSTRUCTOR, as for list-init,
11620 : and setting CONSTRUCTOR_IS_PAREN_INIT to distinguish between
11621 : the two. */
11622 6540 : if (DECL_CONSTRUCTOR_P (fn)
11623 3553 : && !(flags & LOOKUP_ONLYCONVERTING)
11624 3533 : && cxx_dialect >= cxx20
11625 1065 : && CP_AGGREGATE_TYPE_P (basetype)
11626 6540 : && !vec_safe_is_empty (user_args))
11627 : {
11628 : /* Create a CONSTRUCTOR from ARGS, e.g. {1, 2} from <1, 2>. */
11629 324 : tree ctor = build_constructor_from_vec (init_list_type_node,
11630 : user_args);
11631 324 : CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true;
11632 324 : CONSTRUCTOR_IS_PAREN_INIT (ctor) = true;
11633 324 : if (is_dummy_object (instance))
11634 : return ctor;
11635 : else
11636 : {
11637 265 : ctor = digest_init (basetype, ctor, complain);
11638 265 : if (ctor == error_mark_node)
11639 : return error_mark_node;
11640 228 : return cp_build_init_expr (instance, ctor);
11641 : }
11642 : }
11643 6216 : if (complain & tf_error)
11644 499 : complain_about_no_candidates_for_method_call (instance, candidates,
11645 : explicit_targs, basetype,
11646 : optype, name,
11647 : skip_first_for_error,
11648 : user_args);
11649 6216 : call = error_mark_node;
11650 : }
11651 : else
11652 : {
11653 48287298 : cand = tourney (candidates, complain);
11654 48287298 : if (cand == 0)
11655 : {
11656 153 : char *pretty_name;
11657 153 : bool free_p;
11658 153 : tree arglist;
11659 :
11660 153 : if (complain & tf_error)
11661 : {
11662 77 : pretty_name = name_as_c_string (name, basetype, &free_p);
11663 77 : arglist = build_tree_list_vec (user_args);
11664 77 : if (skip_first_for_error)
11665 0 : arglist = TREE_CHAIN (arglist);
11666 77 : auto_diagnostic_group d;
11667 154 : if (!any_strictly_viable (candidates))
11668 18 : error ("no matching function for call to %<%s(%A)%>",
11669 : pretty_name, arglist);
11670 : else
11671 59 : error ("call of overloaded %<%s(%A)%> is ambiguous",
11672 : pretty_name, arglist);
11673 77 : print_z_candidates (location_of (name), candidates);
11674 77 : if (free_p)
11675 0 : free (pretty_name);
11676 77 : }
11677 153 : call = error_mark_node;
11678 153 : if (fn_p)
11679 67 : *fn_p = error_mark_node;
11680 : }
11681 : else
11682 : {
11683 48287145 : fn = cand->fn;
11684 48287145 : call = NULL_TREE;
11685 :
11686 48287145 : if (!(flags & LOOKUP_NONVIRTUAL)
11687 32401896 : && DECL_PURE_VIRTUAL_P (fn)
11688 130611 : && instance == current_class_ref
11689 48385170 : && (complain & tf_warning))
11690 : {
11691 : /* This is not an error, it is runtime undefined
11692 : behavior. */
11693 98025 : if (!current_function_decl)
11694 3 : warning (0, "pure virtual %q#D called from "
11695 : "non-static data member initializer", fn);
11696 98022 : else if (DECL_CONSTRUCTOR_P (current_function_decl)
11697 98022 : || DECL_DESTRUCTOR_P (current_function_decl))
11698 12 : warning (0, (DECL_CONSTRUCTOR_P (current_function_decl)
11699 : ? G_("pure virtual %q#D called from constructor")
11700 : : G_("pure virtual %q#D called from destructor")),
11701 : fn);
11702 : }
11703 :
11704 48287145 : if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
11705 73365432 : && !DECL_CONSTRUCTOR_P (fn)
11706 74368921 : && is_dummy_object (instance))
11707 : {
11708 516 : instance = maybe_resolve_dummy (instance, true);
11709 516 : if (instance == error_mark_node)
11710 : call = error_mark_node;
11711 516 : else if (!is_dummy_object (instance))
11712 : {
11713 : /* We captured 'this' in the current lambda now that
11714 : we know we really need it. */
11715 425 : cand->first_arg = instance;
11716 : }
11717 91 : else if (current_class_ptr && any_dependent_bases_p ())
11718 : /* We can't tell until instantiation time whether we can use
11719 : *this as the implicit object argument. */;
11720 : else
11721 : {
11722 55 : if (complain & tf_error)
11723 39 : error ("cannot call member function %qD without object",
11724 : fn);
11725 55 : call = error_mark_node;
11726 : }
11727 : }
11728 :
11729 48287145 : if (call != error_mark_node)
11730 : {
11731 : /* Now we know what function is being called. */
11732 48287090 : if (fn_p)
11733 13323415 : *fn_p = fn;
11734 : /* Build the actual CALL_EXPR. */
11735 48287090 : call = build_over_call (cand, flags, complain);
11736 :
11737 : /* Suppress warnings for if (my_struct.operator= (x)) where
11738 : my_struct is implicitly converted to bool. */
11739 48287090 : if (TREE_CODE (call) == MODIFY_EXPR)
11740 815770 : suppress_warning (call, OPT_Wparentheses);
11741 :
11742 : /* In an expression of the form `a->f()' where `f' turns
11743 : out to be a static member function, `a' is
11744 : none-the-less evaluated. */
11745 48287090 : if (!is_dummy_object (instance))
11746 34484374 : call = keep_unused_object_arg (call, instance, fn);
11747 48287090 : if (call != error_mark_node
11748 95738148 : && DECL_DESTRUCTOR_P (cand->fn)
11749 61566829 : && !VOID_TYPE_P (TREE_TYPE (call)))
11750 : /* An explicit call of the form "x->~X()" has type
11751 : "void". However, on platforms where destructors
11752 : return "this" (i.e., those where
11753 : targetm.cxx.cdtor_returns_this is true), such calls
11754 : will appear to have a return value of pointer type
11755 : to the low-level call machinery. We do not want to
11756 : change the low-level machinery, since we want to be
11757 : able to optimize "delete f()" on such platforms as
11758 : "operator delete(~X(f()))" (rather than generating
11759 : "t = f(), ~X(t), operator delete (t)"). */
11760 7783305 : call = build_nop (void_type_node, call);
11761 : }
11762 : }
11763 : }
11764 :
11765 48293514 : if (processing_template_decl && call != error_mark_node)
11766 : {
11767 5611098 : bool cast_to_void = false;
11768 :
11769 5611098 : if (TREE_CODE (call) == COMPOUND_EXPR)
11770 7 : call = TREE_OPERAND (call, 1);
11771 5611091 : else if (TREE_CODE (call) == NOP_EXPR)
11772 : {
11773 0 : cast_to_void = true;
11774 0 : call = TREE_OPERAND (call, 0);
11775 : }
11776 5611098 : if (INDIRECT_REF_P (call))
11777 268312 : call = TREE_OPERAND (call, 0);
11778 :
11779 : /* Prune all but the selected function from the original overload
11780 : set so that we can avoid some duplicate work at instantiation time. */
11781 5611098 : if (really_overloaded_fn (fns))
11782 : {
11783 2977698 : if (DECL_TEMPLATE_INFO (fn)
11784 2977698 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
11785 : {
11786 : /* Use the selected template, not the specialization, so that
11787 : this looks like an actual lookup result for sake of
11788 : filter_memfn_lookup. */
11789 :
11790 2061769 : if (OVL_SINGLE_P (fns))
11791 : /* If the original overload set consists of a single function
11792 : template, this isn't beneficial. */
11793 2029317 : goto skip_prune;
11794 :
11795 32452 : fn = ovl_make (DECL_TI_TEMPLATE (fn));
11796 32452 : if (template_only)
11797 32403 : fn = lookup_template_function (fn, explicit_targs);
11798 : }
11799 948381 : orig_fns = copy_node (orig_fns);
11800 948381 : BASELINK_FUNCTIONS (orig_fns) = fn;
11801 948381 : BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (orig_fns) = true;
11802 : }
11803 :
11804 2633400 : skip_prune:
11805 5611098 : call = (build_min_non_dep_call_vec
11806 5611098 : (call,
11807 5611098 : build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
11808 : orig_instance, orig_fns, NULL_TREE),
11809 : orig_args));
11810 5611098 : SET_EXPR_LOCATION (call, input_location);
11811 5611098 : call = convert_from_reference (call);
11812 5611098 : if (cast_to_void)
11813 0 : call = build_nop (void_type_node, call);
11814 : }
11815 :
11816 : /* Free all the conversions we allocated. */
11817 48293514 : obstack_free (&conversion_obstack, p);
11818 :
11819 48293514 : if (orig_args != NULL)
11820 5549913 : release_tree_vector (orig_args);
11821 :
11822 : return call;
11823 49294798 : }
11824 :
11825 : /* Returns true iff standard conversion sequence ICS1 is a proper
11826 : subsequence of ICS2. */
11827 :
11828 : static bool
11829 27054624 : is_subseq (conversion *ics1, conversion *ics2)
11830 : {
11831 : /* We can assume that a conversion of the same code
11832 : between the same types indicates a subsequence since we only get
11833 : here if the types we are converting from are the same. */
11834 :
11835 27054624 : while (ics1->kind == ck_rvalue
11836 31468539 : || ics1->kind == ck_lvalue)
11837 4413915 : ics1 = next_conversion (ics1);
11838 :
11839 : while (1)
11840 : {
11841 38637618 : while (ics2->kind == ck_rvalue
11842 38637618 : || ics2->kind == ck_lvalue)
11843 4413915 : ics2 = next_conversion (ics2);
11844 :
11845 34223703 : if (ics2->kind == ck_user
11846 34223703 : || !has_next (ics2->kind))
11847 : /* At this point, ICS1 cannot be a proper subsequence of
11848 : ICS2. We can get a USER_CONV when we are comparing the
11849 : second standard conversion sequence of two user conversion
11850 : sequences. */
11851 : return false;
11852 :
11853 7174481 : ics2 = next_conversion (ics2);
11854 :
11855 7174481 : while (ics2->kind == ck_rvalue
11856 11227036 : || ics2->kind == ck_lvalue)
11857 4052555 : ics2 = next_conversion (ics2);
11858 :
11859 7174481 : if (ics2->kind == ics1->kind
11860 5447 : && same_type_p (ics2->type, ics1->type)
11861 7179883 : && (ics1->kind == ck_identity
11862 5402 : || same_type_p (next_conversion (ics2)->type,
11863 : next_conversion (ics1)->type)))
11864 5402 : return true;
11865 : }
11866 : }
11867 :
11868 : /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
11869 : be any _TYPE nodes. */
11870 :
11871 : bool
11872 153164796 : is_properly_derived_from (tree derived, tree base)
11873 : {
11874 153164796 : if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
11875 : return false;
11876 :
11877 : /* We only allow proper derivation here. The DERIVED_FROM_P macro
11878 : considers every class derived from itself. */
11879 146024175 : return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
11880 146024175 : && DERIVED_FROM_P (base, derived));
11881 : }
11882 :
11883 : /* We build the ICS for an implicit object parameter as a pointer
11884 : conversion sequence. However, such a sequence should be compared
11885 : as if it were a reference conversion sequence. If ICS is the
11886 : implicit conversion sequence for an implicit object parameter,
11887 : modify it accordingly. */
11888 :
11889 : static void
11890 75864146 : maybe_handle_implicit_object (conversion **ics)
11891 : {
11892 75864146 : if ((*ics)->this_p)
11893 : {
11894 : /* [over.match.funcs]
11895 :
11896 : For non-static member functions, the type of the
11897 : implicit object parameter is "reference to cv X"
11898 : where X is the class of which the function is a
11899 : member and cv is the cv-qualification on the member
11900 : function declaration. */
11901 5181338 : conversion *t = *ics;
11902 5181338 : tree reference_type;
11903 :
11904 : /* The `this' parameter is a pointer to a class type. Make the
11905 : implicit conversion talk about a reference to that same class
11906 : type. */
11907 5181338 : reference_type = TREE_TYPE (t->type);
11908 5181338 : reference_type = build_reference_type (reference_type);
11909 :
11910 5181338 : if (t->kind == ck_qual)
11911 1612358 : t = next_conversion (t);
11912 5181338 : if (t->kind == ck_ptr)
11913 205645 : t = next_conversion (t);
11914 5181338 : t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
11915 5181338 : t = direct_reference_binding (reference_type, t);
11916 5181338 : t->this_p = 1;
11917 5181338 : t->rvaluedness_matches_p = 0;
11918 5181338 : *ics = t;
11919 : }
11920 75864146 : }
11921 :
11922 : /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
11923 : and return the initial reference binding conversion. Otherwise,
11924 : leave *ICS unchanged and return NULL. */
11925 :
11926 : static conversion *
11927 75864146 : maybe_handle_ref_bind (conversion **ics)
11928 : {
11929 75864146 : if ((*ics)->kind == ck_ref_bind)
11930 : {
11931 19292115 : conversion *old_ics = *ics;
11932 19292115 : *ics = next_conversion (old_ics);
11933 19292115 : (*ics)->user_conv_p = old_ics->user_conv_p;
11934 19292115 : return old_ics;
11935 : }
11936 :
11937 : return NULL;
11938 : }
11939 :
11940 : /* Get the expression at the beginning of the conversion chain C. */
11941 :
11942 : static tree
11943 14 : conv_get_original_expr (conversion *c)
11944 : {
11945 14 : for (; c; c = next_conversion (c))
11946 14 : if (c->kind == ck_identity || c->kind == ck_ambig || c->kind == ck_aggr)
11947 14 : return c->u.expr;
11948 : return NULL_TREE;
11949 : }
11950 :
11951 : /* Return a tree representing the number of elements initialized by the
11952 : list-initialization C. The caller must check that C converts to an
11953 : array type. */
11954 :
11955 : static tree
11956 58 : nelts_initialized_by_list_init (conversion *c)
11957 : {
11958 : /* If the array we're converting to has a dimension, we'll use that. */
11959 58 : if (TYPE_DOMAIN (c->type))
11960 44 : return array_type_nelts_top (c->type);
11961 : else
11962 : {
11963 : /* Otherwise, we look at how many elements the constructor we're
11964 : initializing from has. */
11965 14 : tree ctor = conv_get_original_expr (c);
11966 24 : return size_int (CONSTRUCTOR_NELTS (ctor));
11967 : }
11968 : }
11969 :
11970 : /* True iff C is a conversion that binds a reference or a pointer to
11971 : an array of unknown bound. */
11972 :
11973 : static inline bool
11974 8189203 : conv_binds_to_array_of_unknown_bound (conversion *c)
11975 : {
11976 : /* ck_ref_bind won't have the reference stripped. */
11977 8189203 : tree type = non_reference (c->type);
11978 : /* ck_qual won't have the pointer stripped. */
11979 8189203 : type = strip_pointer_operator (type);
11980 8189203 : return (TREE_CODE (type) == ARRAY_TYPE
11981 8189203 : && TYPE_DOMAIN (type) == NULL_TREE);
11982 : }
11983 :
11984 : /* Compare two implicit conversion sequences according to the rules set out in
11985 : [over.ics.rank]. Return values:
11986 :
11987 : 1: ics1 is better than ics2
11988 : -1: ics2 is better than ics1
11989 : 0: ics1 and ics2 are indistinguishable */
11990 :
11991 : static int
11992 37932109 : compare_ics (conversion *ics1, conversion *ics2)
11993 : {
11994 37932109 : tree from_type1;
11995 37932109 : tree from_type2;
11996 37932109 : tree to_type1;
11997 37932109 : tree to_type2;
11998 37932109 : tree deref_from_type1 = NULL_TREE;
11999 37932109 : tree deref_from_type2 = NULL_TREE;
12000 37932109 : tree deref_to_type1 = NULL_TREE;
12001 37932109 : tree deref_to_type2 = NULL_TREE;
12002 37932109 : conversion_rank rank1, rank2;
12003 :
12004 : /* REF_BINDING is nonzero if the result of the conversion sequence
12005 : is a reference type. In that case REF_CONV is the reference
12006 : binding conversion. */
12007 37932109 : conversion *ref_conv1;
12008 37932109 : conversion *ref_conv2;
12009 :
12010 : /* Compare badness before stripping the reference conversion. */
12011 37932109 : if (ics1->bad_p > ics2->bad_p)
12012 : return -1;
12013 37932096 : else if (ics1->bad_p < ics2->bad_p)
12014 : return 1;
12015 :
12016 : /* Handle implicit object parameters. */
12017 37932073 : maybe_handle_implicit_object (&ics1);
12018 37932073 : maybe_handle_implicit_object (&ics2);
12019 :
12020 : /* Handle reference parameters. */
12021 37932073 : ref_conv1 = maybe_handle_ref_bind (&ics1);
12022 37932073 : ref_conv2 = maybe_handle_ref_bind (&ics2);
12023 :
12024 : /* List-initialization sequence L1 is a better conversion sequence than
12025 : list-initialization sequence L2 if L1 converts to
12026 : std::initializer_list<X> for some X and L2 does not. */
12027 37932073 : if (ics1->kind == ck_list && ics2->kind != ck_list)
12028 : return 1;
12029 37931675 : if (ics2->kind == ck_list && ics1->kind != ck_list)
12030 : return -1;
12031 :
12032 : /* [over.ics.rank]
12033 :
12034 : When comparing the basic forms of implicit conversion sequences (as
12035 : defined in _over.best.ics_)
12036 :
12037 : --a standard conversion sequence (_over.ics.scs_) is a better
12038 : conversion sequence than a user-defined conversion sequence
12039 : or an ellipsis conversion sequence, and
12040 :
12041 : --a user-defined conversion sequence (_over.ics.user_) is a
12042 : better conversion sequence than an ellipsis conversion sequence
12043 : (_over.ics.ellipsis_). */
12044 : /* Use BAD_CONVERSION_RANK because we already checked for a badness
12045 : mismatch. If both ICS are bad, we try to make a decision based on
12046 : what would have happened if they'd been good. This is not an
12047 : extension, we'll still give an error when we build up the call; this
12048 : just helps us give a more helpful error message. */
12049 37931406 : rank1 = BAD_CONVERSION_RANK (ics1);
12050 37931406 : rank2 = BAD_CONVERSION_RANK (ics2);
12051 :
12052 37931406 : if (rank1 > rank2)
12053 : return -1;
12054 30993081 : else if (rank1 < rank2)
12055 : return 1;
12056 :
12057 13712878 : if (ics1->ellipsis_p)
12058 : /* Both conversions are ellipsis conversions. */
12059 : return 0;
12060 :
12061 : /* User-defined conversion sequence U1 is a better conversion sequence
12062 : than another user-defined conversion sequence U2 if they contain the
12063 : same user-defined conversion operator or constructor and if the sec-
12064 : ond standard conversion sequence of U1 is better than the second
12065 : standard conversion sequence of U2. */
12066 :
12067 : /* Handle list-conversion with the same code even though it isn't always
12068 : ranked as a user-defined conversion and it doesn't have a second
12069 : standard conversion sequence; it will still have the desired effect.
12070 : Specifically, we need to do the reference binding comparison at the
12071 : end of this function. */
12072 :
12073 13712852 : if (ics1->user_conv_p || ics1->kind == ck_list
12074 13501007 : || ics1->kind == ck_aggr || ics2->kind == ck_aggr)
12075 : {
12076 211880 : conversion *t1 = strip_standard_conversion (ics1);
12077 211880 : conversion *t2 = strip_standard_conversion (ics2);
12078 :
12079 211880 : if (!t1 || !t2 || t1->kind != t2->kind)
12080 : return 0;
12081 211859 : else if (t1->kind == ck_user)
12082 : {
12083 211231 : tree f1 = t1->cand ? t1->cand->fn : t1->type;
12084 211231 : tree f2 = t2->cand ? t2->cand->fn : t2->type;
12085 211231 : if (f1 != f2)
12086 : return 0;
12087 : }
12088 : /* List-initialization sequence L1 is a better conversion sequence than
12089 : list-initialization sequence L2 if
12090 :
12091 : -- L1 and L2 convert to arrays of the same element type, and either
12092 : the number of elements n1 initialized by L1 is less than the number
12093 : of elements n2 initialized by L2, or n1=n2 and L2 converts to an array
12094 : of unknown bound and L1 does not. (Added in CWG 1307 and extended by
12095 : P0388R4.) */
12096 628 : else if (t1->kind == ck_aggr
12097 339 : && TREE_CODE (t1->type) == ARRAY_TYPE
12098 32 : && TREE_CODE (t2->type) == ARRAY_TYPE
12099 660 : && same_type_p (TREE_TYPE (t1->type), TREE_TYPE (t2->type)))
12100 : {
12101 29 : tree n1 = nelts_initialized_by_list_init (t1);
12102 29 : tree n2 = nelts_initialized_by_list_init (t2);
12103 29 : if (tree_int_cst_lt (n1, n2))
12104 : return 1;
12105 8 : else if (tree_int_cst_lt (n2, n1))
12106 : return -1;
12107 : /* The n1 == n2 case. */
12108 8 : bool c1 = conv_binds_to_array_of_unknown_bound (t1);
12109 8 : bool c2 = conv_binds_to_array_of_unknown_bound (t2);
12110 8 : if (c1 && !c2)
12111 : return -1;
12112 2 : else if (!c1 && c2)
12113 : return 1;
12114 : else
12115 : return 0;
12116 : }
12117 : else
12118 : {
12119 : /* For ambiguous or aggregate conversions, use the target type as
12120 : a proxy for the conversion function. */
12121 599 : if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
12122 : return 0;
12123 : }
12124 :
12125 : /* We can just fall through here, after setting up
12126 : FROM_TYPE1 and FROM_TYPE2. */
12127 209998 : from_type1 = t1->type;
12128 209998 : from_type2 = t2->type;
12129 209998 : }
12130 : else
12131 : {
12132 : conversion *t1;
12133 : conversion *t2;
12134 :
12135 : /* We're dealing with two standard conversion sequences.
12136 :
12137 : [over.ics.rank]
12138 :
12139 : Standard conversion sequence S1 is a better conversion
12140 : sequence than standard conversion sequence S2 if
12141 :
12142 : --S1 is a proper subsequence of S2 (comparing the conversion
12143 : sequences in the canonical form defined by _over.ics.scs_,
12144 : excluding any Lvalue Transformation; the identity
12145 : conversion sequence is considered to be a subsequence of
12146 : any non-identity conversion sequence */
12147 :
12148 : t1 = ics1;
12149 21340260 : while (t1->kind != ck_identity)
12150 7839288 : t1 = next_conversion (t1);
12151 13500972 : from_type1 = t1->type;
12152 :
12153 13500972 : t2 = ics2;
12154 21325451 : while (t2->kind != ck_identity)
12155 7824479 : t2 = next_conversion (t2);
12156 13500972 : from_type2 = t2->type;
12157 : }
12158 :
12159 : /* One sequence can only be a subsequence of the other if they start with
12160 : the same type. They can start with different types when comparing the
12161 : second standard conversion sequence in two user-defined conversion
12162 : sequences. */
12163 13710970 : if (same_type_p (from_type1, from_type2))
12164 : {
12165 13529982 : if (is_subseq (ics1, ics2))
12166 : return 1;
12167 13524642 : if (is_subseq (ics2, ics1))
12168 : return -1;
12169 : }
12170 :
12171 : /* [over.ics.rank]
12172 :
12173 : Or, if not that,
12174 :
12175 : --the rank of S1 is better than the rank of S2 (by the rules
12176 : defined below):
12177 :
12178 : Standard conversion sequences are ordered by their ranks: an Exact
12179 : Match is a better conversion than a Promotion, which is a better
12180 : conversion than a Conversion.
12181 :
12182 : Two conversion sequences with the same rank are indistinguishable
12183 : unless one of the following rules applies:
12184 :
12185 : --A conversion that does not a convert a pointer, pointer to member,
12186 : or std::nullptr_t to bool is better than one that does.
12187 :
12188 : The ICS_STD_RANK automatically handles the pointer-to-bool rule,
12189 : so that we do not have to check it explicitly. */
12190 13705568 : if (ics1->rank < ics2->rank)
12191 : return 1;
12192 13705482 : else if (ics2->rank < ics1->rank)
12193 : return -1;
12194 :
12195 13705482 : to_type1 = ics1->type;
12196 13705482 : to_type2 = ics2->type;
12197 :
12198 : /* A conversion from scalar arithmetic type to complex is worse than a
12199 : conversion between scalar arithmetic types. */
12200 13705482 : if (same_type_p (from_type1, from_type2)
12201 13524494 : && ARITHMETIC_TYPE_P (from_type1)
12202 3963932 : && ARITHMETIC_TYPE_P (to_type1)
12203 3963770 : && ARITHMETIC_TYPE_P (to_type2)
12204 13705482 : && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
12205 3963755 : != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
12206 : {
12207 77 : if (TREE_CODE (to_type1) == COMPLEX_TYPE)
12208 : return -1;
12209 : else
12210 : return 1;
12211 : }
12212 :
12213 13705405 : {
12214 : /* A conversion in either direction between floating-point type FP1 and
12215 : floating-point type FP2 is better than a conversion in the same
12216 : direction between FP1 and arithmetic type T3 if
12217 : - the floating-point conversion rank of FP1 is equal to the rank of
12218 : FP2, and
12219 : - T3 is not a floating-point type, or T3 is a floating-point type
12220 : whose rank is not equal to the rank of FP1, or the floating-point
12221 : conversion subrank of FP2 is greater than the subrank of T3. */
12222 13705405 : tree fp1 = from_type1;
12223 13705405 : tree fp2 = to_type1;
12224 13705405 : tree fp3 = from_type2;
12225 13705405 : tree t3 = to_type2;
12226 13705405 : int ret = 1;
12227 13705405 : if (TYPE_MAIN_VARIANT (fp2) == TYPE_MAIN_VARIANT (t3))
12228 : {
12229 10688728 : std::swap (fp1, fp2);
12230 10688728 : std::swap (fp3, t3);
12231 : }
12232 13705405 : if (TYPE_MAIN_VARIANT (fp1) == TYPE_MAIN_VARIANT (fp3)
12233 13705357 : && SCALAR_FLOAT_TYPE_P (fp1)
12234 : /* Only apply this rule if at least one of the 3 types is
12235 : extended floating-point type, otherwise keep them as
12236 : before for compatibility reasons with types like __float128.
12237 : float, double and long double alone have different conversion
12238 : ranks and so when just those 3 types are involved, this
12239 : rule doesn't trigger. */
12240 16642835 : && (extended_float_type_p (fp1)
12241 2932977 : || (SCALAR_FLOAT_TYPE_P (fp2) && extended_float_type_p (fp2))
12242 2931410 : || (SCALAR_FLOAT_TYPE_P (t3) && extended_float_type_p (t3))))
12243 : {
12244 6020 : if (TREE_CODE (fp2) != REAL_TYPE)
12245 : {
12246 951 : ret = -ret;
12247 951 : std::swap (fp2, t3);
12248 : }
12249 6020 : if (SCALAR_FLOAT_TYPE_P (fp2))
12250 : {
12251 : /* cp_compare_floating_point_conversion_ranks returns -1, 0 or 1
12252 : if the conversion rank is equal (-1 or 1 if the subrank is
12253 : different). */
12254 5484 : if (IN_RANGE (cp_compare_floating_point_conversion_ranks (fp1,
12255 : fp2),
12256 : -1, 1))
12257 : {
12258 : /* Conversion ranks of FP1 and FP2 are equal. */
12259 1477 : if (TREE_CODE (t3) != REAL_TYPE
12260 1477 : || !IN_RANGE (cp_compare_floating_point_conversion_ranks
12261 : (fp1, t3),
12262 : -1, 1))
12263 : /* FP1 <-> FP2 conversion is better. */
12264 1337 : return ret;
12265 140 : int c = cp_compare_floating_point_conversion_ranks (fp2, t3);
12266 140 : gcc_assert (IN_RANGE (c, -1, 1));
12267 140 : if (c == 1)
12268 : /* Conversion subrank of FP2 is greater than subrank of T3.
12269 : FP1 <-> FP2 conversion is better. */
12270 : return ret;
12271 140 : else if (c == -1)
12272 : /* Conversion subrank of FP2 is less than subrank of T3.
12273 : FP1 <-> T3 conversion is better. */
12274 0 : return -ret;
12275 : }
12276 4007 : else if (SCALAR_FLOAT_TYPE_P (t3)
12277 4007 : && IN_RANGE (cp_compare_floating_point_conversion_ranks
12278 : (fp1, t3),
12279 : -1, 1))
12280 : /* Conversion ranks of FP1 and FP2 are not equal, conversion
12281 : ranks of FP1 and T3 are equal.
12282 : FP1 <-> T3 conversion is better. */
12283 369 : return -ret;
12284 : }
12285 : }
12286 : }
12287 :
12288 13703699 : if (TYPE_PTR_P (from_type1)
12289 721431 : && TYPE_PTR_P (from_type2)
12290 721411 : && TYPE_PTR_P (to_type1)
12291 721389 : && TYPE_PTR_P (to_type2))
12292 : {
12293 721389 : deref_from_type1 = TREE_TYPE (from_type1);
12294 721389 : deref_from_type2 = TREE_TYPE (from_type2);
12295 721389 : deref_to_type1 = TREE_TYPE (to_type1);
12296 721389 : deref_to_type2 = TREE_TYPE (to_type2);
12297 : }
12298 : /* The rules for pointers to members A::* are just like the rules
12299 : for pointers A*, except opposite: if B is derived from A then
12300 : A::* converts to B::*, not vice versa. For that reason, we
12301 : switch the from_ and to_ variables here. */
12302 52 : else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2)
12303 52 : && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2))
12304 12982310 : || (TYPE_PTRMEMFUNC_P (from_type1)
12305 365 : && TYPE_PTRMEMFUNC_P (from_type2)
12306 365 : && TYPE_PTRMEMFUNC_P (to_type1)
12307 365 : && TYPE_PTRMEMFUNC_P (to_type2)))
12308 : {
12309 417 : deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
12310 417 : deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
12311 417 : deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
12312 417 : deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
12313 : }
12314 :
12315 13703699 : if (deref_from_type1 != NULL_TREE
12316 721806 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
12317 96406 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
12318 : {
12319 : /* This was one of the pointer or pointer-like conversions.
12320 :
12321 : [over.ics.rank]
12322 :
12323 : --If class B is derived directly or indirectly from class A,
12324 : conversion of B* to A* is better than conversion of B* to
12325 : void*, and conversion of A* to void* is better than
12326 : conversion of B* to void*. */
12327 96406 : if (VOID_TYPE_P (deref_to_type1)
12328 49 : && VOID_TYPE_P (deref_to_type2))
12329 : {
12330 6 : if (is_properly_derived_from (deref_from_type1,
12331 : deref_from_type2))
12332 : return -1;
12333 6 : else if (is_properly_derived_from (deref_from_type2,
12334 : deref_from_type1))
12335 : return 1;
12336 : }
12337 96400 : else if (VOID_TYPE_P (deref_to_type1)
12338 96357 : || VOID_TYPE_P (deref_to_type2))
12339 : {
12340 47 : if (same_type_p (deref_from_type1, deref_from_type2))
12341 : {
12342 47 : if (VOID_TYPE_P (deref_to_type2))
12343 : {
12344 4 : if (is_properly_derived_from (deref_from_type1,
12345 : deref_to_type1))
12346 : return 1;
12347 : }
12348 : /* We know that DEREF_TO_TYPE1 is `void' here. */
12349 43 : else if (is_properly_derived_from (deref_from_type1,
12350 : deref_to_type2))
12351 : return -1;
12352 : }
12353 : }
12354 96353 : else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
12355 96353 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
12356 : {
12357 : /* [over.ics.rank]
12358 :
12359 : --If class B is derived directly or indirectly from class A
12360 : and class C is derived directly or indirectly from B,
12361 :
12362 : --conversion of C* to B* is better than conversion of C* to
12363 : A*,
12364 :
12365 : --conversion of B* to A* is better than conversion of C* to
12366 : A* */
12367 96353 : if (same_type_p (deref_from_type1, deref_from_type2))
12368 : {
12369 96345 : if (is_properly_derived_from (deref_to_type1,
12370 : deref_to_type2))
12371 : return 1;
12372 96345 : else if (is_properly_derived_from (deref_to_type2,
12373 : deref_to_type1))
12374 : return -1;
12375 : }
12376 8 : else if (same_type_p (deref_to_type1, deref_to_type2))
12377 : {
12378 8 : if (is_properly_derived_from (deref_from_type2,
12379 : deref_from_type1))
12380 : return 1;
12381 0 : else if (is_properly_derived_from (deref_from_type1,
12382 : deref_from_type2))
12383 : return -1;
12384 : }
12385 : }
12386 : }
12387 27214586 : else if (CLASS_TYPE_P (non_reference (from_type1))
12388 21787606 : && same_type_p (from_type1, from_type2))
12389 : {
12390 8000532 : tree from = non_reference (from_type1);
12391 :
12392 : /* [over.ics.rank]
12393 :
12394 : --binding of an expression of type C to a reference of type
12395 : B& is better than binding an expression of type C to a
12396 : reference of type A&
12397 :
12398 : --conversion of C to B is better than conversion of C to A, */
12399 8000532 : if (is_properly_derived_from (from, to_type1)
12400 8000532 : && is_properly_derived_from (from, to_type2))
12401 : {
12402 475344 : if (is_properly_derived_from (to_type1, to_type2))
12403 : return 1;
12404 474525 : else if (is_properly_derived_from (to_type2, to_type1))
12405 : return -1;
12406 : }
12407 : }
12408 11213522 : else if (CLASS_TYPE_P (non_reference (to_type1))
12409 5786542 : && same_type_p (to_type1, to_type2))
12410 : {
12411 6 : tree to = non_reference (to_type1);
12412 :
12413 : /* [over.ics.rank]
12414 :
12415 : --binding of an expression of type B to a reference of type
12416 : A& is better than binding an expression of type C to a
12417 : reference of type A&,
12418 :
12419 : --conversion of B to A is better than conversion of C to A */
12420 6 : if (is_properly_derived_from (from_type1, to)
12421 6 : && is_properly_derived_from (from_type2, to))
12422 : {
12423 4 : if (is_properly_derived_from (from_type2, from_type1))
12424 : return 1;
12425 4 : else if (is_properly_derived_from (from_type1, from_type2))
12426 : return -1;
12427 : }
12428 : }
12429 :
12430 : /* [over.ics.rank]
12431 :
12432 : --S1 and S2 differ only in their qualification conversion and yield
12433 : similar types T1 and T2 (_conv.qual_), respectively, and the cv-
12434 : qualification signature of type T1 is a proper subset of the cv-
12435 : qualification signature of type T2 */
12436 13682501 : if (ics1->kind == ck_qual
12437 305 : && ics2->kind == ck_qual
12438 13682806 : && same_type_p (from_type1, from_type2))
12439 : {
12440 305 : int result = comp_cv_qual_signature (to_type1, to_type2);
12441 305 : if (result != 0)
12442 : return result;
12443 : }
12444 :
12445 : /* [over.ics.rank]
12446 :
12447 : --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
12448 : to an implicit object parameter of a non-static member function
12449 : declared without a ref-qualifier, and either S1 binds an lvalue
12450 : reference to an lvalue and S2 binds an rvalue reference or S1 binds an
12451 : rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x
12452 : draft standard, 13.3.3.2)
12453 :
12454 : --S1 and S2 are reference bindings (_dcl.init.ref_), and the
12455 : types to which the references refer are the same type except for
12456 : top-level cv-qualifiers, and the type to which the reference
12457 : initialized by S2 refers is more cv-qualified than the type to
12458 : which the reference initialized by S1 refers.
12459 :
12460 : DR 1328 [over.match.best]: the context is an initialization by
12461 : conversion function for direct reference binding (13.3.1.6) of a
12462 : reference to function type, the return type of F1 is the same kind of
12463 : reference (i.e. lvalue or rvalue) as the reference being initialized,
12464 : and the return type of F2 is not. */
12465 :
12466 13682451 : if (ref_conv1 && ref_conv2)
12467 : {
12468 5288102 : if (!ref_conv1->this_p && !ref_conv2->this_p
12469 5265136 : && (ref_conv1->rvaluedness_matches_p
12470 5265136 : != ref_conv2->rvaluedness_matches_p)
12471 11651648 : && (same_type_p (ref_conv1->type, ref_conv2->type)
12472 3778816 : || (TYPE_REF_IS_RVALUE (ref_conv1->type)
12473 3778816 : != TYPE_REF_IS_RVALUE (ref_conv2->type))))
12474 : {
12475 3776775 : if (ref_conv1->bad_p
12476 3776775 : && !same_type_p (TREE_TYPE (ref_conv1->type),
12477 : TREE_TYPE (ref_conv2->type)))
12478 : /* Don't prefer a bad conversion that drops cv-quals to a bad
12479 : conversion with the wrong rvalueness. */
12480 : return 0;
12481 3775329 : return (ref_conv1->rvaluedness_matches_p
12482 3775329 : - ref_conv2->rvaluedness_matches_p);
12483 : }
12484 :
12485 4096054 : if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
12486 : {
12487 : /* Per P0388R4:
12488 :
12489 : void f (int(&)[]), // (1)
12490 : f (int(&)[1]), // (2)
12491 : f (int*); // (3)
12492 :
12493 : (2) is better than (1), but (3) should be equal to (1) and to
12494 : (2). For that reason we don't use ck_qual for (1) which would
12495 : give it the cr_exact rank while (3) remains ck_identity.
12496 : Therefore we compare (1) and (2) here. For (1) we'll have
12497 :
12498 : ck_ref_bind <- ck_identity
12499 : int[] & int[1]
12500 :
12501 : so to handle this we must look at ref_conv. */
12502 4094010 : bool c1 = conv_binds_to_array_of_unknown_bound (ref_conv1);
12503 4094010 : bool c2 = conv_binds_to_array_of_unknown_bound (ref_conv2);
12504 4094010 : if (c1 && !c2)
12505 : return -1;
12506 4094008 : else if (!c1 && c2)
12507 : return 1;
12508 :
12509 4094008 : int q1 = cp_type_quals (TREE_TYPE (ref_conv1->type));
12510 4094008 : int q2 = cp_type_quals (TREE_TYPE (ref_conv2->type));
12511 4094008 : if (ref_conv1->bad_p)
12512 : {
12513 : /* Prefer the one that drops fewer cv-quals. */
12514 1592 : tree ftype = next_conversion (ref_conv1)->type;
12515 1592 : int fquals = cp_type_quals (ftype);
12516 1592 : q1 ^= fquals;
12517 1592 : q2 ^= fquals;
12518 : }
12519 4094008 : return comp_cv_qualification (q2, q1);
12520 : }
12521 : }
12522 :
12523 : /* [over.ics.rank]
12524 :
12525 : Per CWG 1601:
12526 : -- A conversion that promotes an enumeration whose underlying type
12527 : is fixed to its underlying type is better than one that promotes to
12528 : the promoted underlying type, if the two are different. */
12529 5811666 : if (ics1->rank == cr_promotion
12530 129 : && ics2->rank == cr_promotion
12531 129 : && UNSCOPED_ENUM_P (from_type1)
12532 20 : && ENUM_FIXED_UNDERLYING_TYPE_P (from_type1)
12533 5811686 : && same_type_p (from_type1, from_type2))
12534 : {
12535 20 : tree utype = ENUM_UNDERLYING_TYPE (from_type1);
12536 20 : tree prom = type_promotes_to (from_type1);
12537 20 : if (!same_type_p (utype, prom))
12538 : {
12539 12 : if (same_type_p (to_type1, utype)
12540 12 : && same_type_p (to_type2, prom))
12541 : return 1;
12542 6 : else if (same_type_p (to_type2, utype)
12543 6 : && same_type_p (to_type1, prom))
12544 : return -1;
12545 : }
12546 : }
12547 :
12548 : /* Neither conversion sequence is better than the other. */
12549 : return 0;
12550 : }
12551 :
12552 : /* The source type for this standard conversion sequence. */
12553 :
12554 : static tree
12555 11 : source_type (conversion *t)
12556 : {
12557 11 : return strip_standard_conversion (t)->type;
12558 : }
12559 :
12560 : /* Note a warning about preferring WINNER to LOSER. We do this by storing
12561 : a pointer to LOSER and re-running joust to produce the warning if WINNER
12562 : is actually used. */
12563 :
12564 : static void
12565 185 : add_warning (struct z_candidate *winner, struct z_candidate *loser)
12566 : {
12567 185 : candidate_warning *cw = (candidate_warning *)
12568 0 : conversion_obstack_alloc (sizeof (candidate_warning));
12569 185 : cw->loser = loser;
12570 185 : cw->next = winner->warnings;
12571 185 : winner->warnings = cw;
12572 185 : }
12573 :
12574 : /* CAND is a constructor candidate in joust in C++17 and up. If it copies a
12575 : prvalue returned from a conversion function, return true. Otherwise, return
12576 : false. */
12577 :
12578 : static bool
12579 185342 : joust_maybe_elide_copy (z_candidate *cand)
12580 : {
12581 185342 : tree fn = cand->fn;
12582 465976 : if (!DECL_COPY_CONSTRUCTOR_P (fn) && !DECL_MOVE_CONSTRUCTOR_P (fn))
12583 95226 : return false;
12584 90116 : conversion *conv = cand->convs[0];
12585 90116 : if (conv->kind == ck_ambig)
12586 : return false;
12587 90114 : gcc_checking_assert (conv->kind == ck_ref_bind);
12588 90114 : conv = next_conversion (conv);
12589 90114 : if (conv->kind == ck_user && !TYPE_REF_P (conv->type))
12590 : {
12591 98 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
12592 : (conv->type, DECL_CONTEXT (fn)));
12593 98 : z_candidate *uc = conv->cand;
12594 98 : if (DECL_CONV_FN_P (uc->fn))
12595 : return true;
12596 : }
12597 : return false;
12598 : }
12599 :
12600 : /* True if the defining declarations of the two candidates have equivalent
12601 : parameters. */
12602 :
12603 : static bool
12604 81797 : cand_parms_match (z_candidate *c1, z_candidate *c2)
12605 : {
12606 81797 : tree fn1 = c1->fn;
12607 81797 : tree fn2 = c2->fn;
12608 81797 : if (fn1 == fn2)
12609 : return true;
12610 76747 : if (identifier_p (fn1) || identifier_p (fn2))
12611 : return false;
12612 : /* We don't look at c1->template_decl because that's only set for primary
12613 : templates, not e.g. non-template member functions of class templates. */
12614 76743 : tree t1 = most_general_template (fn1);
12615 76743 : tree t2 = most_general_template (fn2);
12616 76743 : if (t1 || t2)
12617 : {
12618 2591 : if (!t1 || !t2)
12619 : return false;
12620 2589 : if (t1 == t2)
12621 : return true;
12622 2585 : fn1 = DECL_TEMPLATE_RESULT (t1);
12623 2585 : fn2 = DECL_TEMPLATE_RESULT (t2);
12624 : }
12625 76737 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn1));
12626 76737 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (fn2));
12627 151078 : if (DECL_FUNCTION_MEMBER_P (fn1)
12628 2397 : && DECL_FUNCTION_MEMBER_P (fn2)
12629 79115 : && (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn1)
12630 2378 : != DECL_NONSTATIC_MEMBER_FUNCTION_P (fn2)))
12631 : {
12632 : /* Ignore 'this' when comparing the parameters of a static member
12633 : function with those of a non-static one. */
12634 2 : parms1 = skip_artificial_parms_for (fn1, parms1);
12635 2 : parms2 = skip_artificial_parms_for (fn2, parms2);
12636 : }
12637 76737 : return compparms (parms1, parms2);
12638 : }
12639 :
12640 : /* True iff FN is a copy or move constructor or assignment operator. */
12641 :
12642 : static bool
12643 9145294 : sfk_copy_or_move (tree fn)
12644 : {
12645 9145294 : if (TREE_CODE (fn) != FUNCTION_DECL)
12646 : return false;
12647 9145216 : special_function_kind sfk = special_function_p (fn);
12648 9145216 : return sfk >= sfk_copy_constructor && sfk <= sfk_move_assignment;
12649 : }
12650 :
12651 : /* Compare two candidates for overloading as described in
12652 : [over.match.best]. Return values:
12653 :
12654 : 1: cand1 is better than cand2
12655 : -1: cand2 is better than cand1
12656 : 0: cand1 and cand2 are indistinguishable */
12657 :
12658 : static int
12659 31230697 : joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
12660 : tsubst_flags_t complain)
12661 : {
12662 31230697 : int winner = 0;
12663 31230697 : int off1 = 0, off2 = 0;
12664 31230697 : size_t i;
12665 31230697 : size_t len;
12666 :
12667 : /* Candidates that involve bad conversions are always worse than those
12668 : that don't. */
12669 31230697 : if (cand1->viable > cand2->viable)
12670 : return 1;
12671 31230697 : if (cand1->viable < cand2->viable)
12672 : return -1;
12673 :
12674 : /* If we have two pseudo-candidates for conversions to the same type,
12675 : or two candidates for the same function, arbitrarily pick one. */
12676 31230697 : if (cand1->fn == cand2->fn
12677 1113099 : && cand1->reversed () == cand2->reversed ()
12678 32319377 : && (IS_TYPE_OR_DECL_P (cand1->fn)))
12679 : return 1;
12680 :
12681 : /* Prefer a non-deleted function over an implicitly deleted move
12682 : constructor or assignment operator. This differs slightly from the
12683 : wording for issue 1402 (which says the move op is ignored by overload
12684 : resolution), but this way produces better error messages. */
12685 31230664 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
12686 28189867 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
12687 59420327 : && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn))
12688 : {
12689 955197 : if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn)
12690 834198 : && move_fn_p (cand1->fn))
12691 : return -1;
12692 1545544 : if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn)
12693 1322986 : && move_fn_p (cand2->fn))
12694 : return 1;
12695 : }
12696 :
12697 : /* a viable function F1
12698 : is defined to be a better function than another viable function F2 if
12699 : for all arguments i, ICSi(F1) is not a worse conversion sequence than
12700 : ICSi(F2), and then */
12701 :
12702 : /* for some argument j, ICSj(F1) is a better conversion sequence than
12703 : ICSj(F2) */
12704 :
12705 : /* For comparing static and non-static member functions, we ignore
12706 : the implicit object parameter of the non-static function. The
12707 : standard says to pretend that the static function has an object
12708 : parm, but that won't work with operator overloading. */
12709 31222922 : len = cand1->num_convs;
12710 31222922 : if (len != cand2->num_convs)
12711 : {
12712 55 : int static_1 = (TREE_CODE (cand1->fn) == FUNCTION_DECL
12713 55 : && DECL_STATIC_FUNCTION_P (cand1->fn));
12714 55 : int static_2 = (TREE_CODE (cand2->fn) == FUNCTION_DECL
12715 55 : && DECL_STATIC_FUNCTION_P (cand2->fn));
12716 :
12717 55 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
12718 39 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
12719 39 : && DECL_CONSTRUCTOR_P (cand1->fn)
12720 61 : && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
12721 : /* We're comparing a near-match list constructor and a near-match
12722 : non-list constructor. Just treat them as unordered. */
12723 : return 0;
12724 :
12725 49 : gcc_assert (static_1 != static_2);
12726 :
12727 49 : if (static_1)
12728 : {
12729 : /* C++23 [over.best.ics.general] says:
12730 : When the parameter is the implicit object parameter of a static
12731 : member function, the implicit conversion sequence is a standard
12732 : conversion sequence that is neither better nor worse than any
12733 : other standard conversion sequence. */
12734 0 : if (CONVERSION_RANK (cand2->convs[0]) >= cr_user)
12735 0 : winner = 1;
12736 : off2 = 1;
12737 : }
12738 : else
12739 : {
12740 49 : if (CONVERSION_RANK (cand1->convs[0]) >= cr_user)
12741 16 : winner = -1;
12742 49 : off1 = 1;
12743 49 : --len;
12744 : }
12745 : }
12746 :
12747 69109162 : for (i = 0; i < len; ++i)
12748 : {
12749 37902802 : conversion *t1 = cand1->convs[i + off1];
12750 37902802 : conversion *t2 = cand2->convs[i + off2];
12751 37902802 : int comp = compare_ics (t1, t2);
12752 :
12753 37902802 : if (comp != 0)
12754 : {
12755 29887960 : if ((complain & tf_warning)
12756 25608107 : && warn_sign_promo
12757 88 : && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
12758 : == cr_std + cr_promotion)
12759 44 : && t1->kind == ck_std
12760 30 : && t2->kind == ck_std
12761 30 : && TREE_CODE (t1->type) == INTEGER_TYPE
12762 30 : && TREE_CODE (t2->type) == INTEGER_TYPE
12763 30 : && (TYPE_PRECISION (t1->type)
12764 30 : == TYPE_PRECISION (t2->type))
12765 29887990 : && (TYPE_UNSIGNED (next_conversion (t1)->type)
12766 0 : || (TREE_CODE (next_conversion (t1)->type)
12767 : == ENUMERAL_TYPE)))
12768 : {
12769 30 : tree type = next_conversion (t1)->type;
12770 30 : tree type1, type2;
12771 30 : struct z_candidate *w, *l;
12772 30 : if (comp > 0)
12773 : type1 = t1->type, type2 = t2->type,
12774 : w = cand1, l = cand2;
12775 : else
12776 11 : type1 = t2->type, type2 = t1->type,
12777 11 : w = cand2, l = cand1;
12778 :
12779 30 : if (warn)
12780 : {
12781 8 : warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
12782 : type, type1, type2);
12783 8 : warning (OPT_Wsign_promo, " in call to %qD", w->fn);
12784 : }
12785 : else
12786 22 : add_warning (w, l);
12787 : }
12788 :
12789 29887960 : if (winner && comp != winner)
12790 : {
12791 : /* Ambiguity between normal and reversed comparison operators
12792 : with the same parameter types. P2468 decided not to go with
12793 : this approach to resolving the ambiguity, so pedwarn. */
12794 16556 : if ((complain & tf_warning_or_error)
12795 16498 : && (cand1->reversed () != cand2->reversed ())
12796 16578 : && cand_parms_match (cand1, cand2))
12797 : {
12798 17 : struct z_candidate *w, *l;
12799 17 : if (cand2->reversed ())
12800 : winner = 1, w = cand1, l = cand2;
12801 : else
12802 9 : winner = -1, w = cand2, l = cand1;
12803 17 : if (warn)
12804 : {
12805 8 : auto_diagnostic_group d;
12806 8 : if (pedwarn (input_location, 0,
12807 : "C++20 says that these are ambiguous, "
12808 : "even though the second is reversed:"))
12809 : {
12810 8 : print_z_candidate (input_location,
12811 : N_("candidate 1:"), w);
12812 8 : print_z_candidate (input_location,
12813 : N_("candidate 2:"), l);
12814 8 : if (w->fn == l->fn
12815 6 : && DECL_NONSTATIC_MEMBER_FUNCTION_P (w->fn)
12816 13 : && (type_memfn_quals (TREE_TYPE (w->fn))
12817 5 : & TYPE_QUAL_CONST) == 0)
12818 : {
12819 : /* Suggest adding const to
12820 : struct A { bool operator==(const A&); }; */
12821 4 : tree parmtype
12822 4 : = FUNCTION_FIRST_USER_PARMTYPE (w->fn);
12823 4 : parmtype = TREE_VALUE (parmtype);
12824 4 : if (TYPE_REF_P (parmtype)
12825 4 : && TYPE_READONLY (TREE_TYPE (parmtype))
12826 8 : && (same_type_ignoring_top_level_qualifiers_p
12827 4 : (TREE_TYPE (parmtype),
12828 4 : DECL_CONTEXT (w->fn))))
12829 4 : inform (DECL_SOURCE_LOCATION (w->fn),
12830 : "try making the operator a %<const%> "
12831 : "member function");
12832 : }
12833 : }
12834 8 : }
12835 : else
12836 9 : add_warning (w, l);
12837 17 : return winner;
12838 : }
12839 :
12840 16539 : winner = 0;
12841 16539 : goto tweak;
12842 : }
12843 : winner = comp;
12844 : }
12845 : }
12846 :
12847 : /* warn about confusing overload resolution for user-defined conversions,
12848 : either between a constructor and a conversion op, or between two
12849 : conversion ops. */
12850 31206360 : if ((complain & tf_warning)
12851 : /* In C++17, the constructor might have been elided, which means that
12852 : an originally null ->second_conv could become non-null. */
12853 27066989 : && winner && warn_conversion && cand1->second_conv && cand2->second_conv
12854 66 : && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
12855 31206393 : && winner != compare_ics (cand1->second_conv, cand2->second_conv))
12856 : {
12857 33 : struct z_candidate *w, *l;
12858 33 : bool give_warning = false;
12859 :
12860 33 : if (winner == 1)
12861 : w = cand1, l = cand2;
12862 : else
12863 8 : w = cand2, l = cand1;
12864 :
12865 : /* We don't want to complain about `X::operator T1 ()'
12866 : beating `X::operator T2 () const', when T2 is a no less
12867 : cv-qualified version of T1. */
12868 33 : if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
12869 49 : && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
12870 : {
12871 8 : tree t = TREE_TYPE (TREE_TYPE (l->fn));
12872 8 : tree f = TREE_TYPE (TREE_TYPE (w->fn));
12873 :
12874 8 : if (TREE_CODE (t) == TREE_CODE (f) && INDIRECT_TYPE_P (t))
12875 : {
12876 8 : t = TREE_TYPE (t);
12877 8 : f = TREE_TYPE (f);
12878 : }
12879 8 : if (!comp_ptr_ttypes (t, f))
12880 : give_warning = true;
12881 : }
12882 : else
12883 : give_warning = true;
12884 :
12885 : if (!give_warning)
12886 : /*NOP*/;
12887 25 : else if (warn)
12888 : {
12889 11 : tree source = source_type (w->convs[0]);
12890 11 : if (INDIRECT_TYPE_P (source))
12891 8 : source = TREE_TYPE (source);
12892 11 : auto_diagnostic_group d;
12893 11 : if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
12894 22 : && warning (OPT_Wconversion, " for conversion from %qH to %qI",
12895 11 : source, w->second_conv->type))
12896 : {
12897 11 : inform (input_location, " because conversion sequence "
12898 : "for the argument is better");
12899 : }
12900 11 : }
12901 : else
12902 14 : add_warning (w, l);
12903 : }
12904 :
12905 31206360 : if (winner)
12906 : return winner;
12907 :
12908 : /* DR 495 moved this tiebreaker above the template ones. */
12909 : /* or, if not that,
12910 : the context is an initialization by user-defined conversion (see
12911 : _dcl.init_ and _over.match.user_) and the standard conversion
12912 : sequence from the return type of F1 to the destination type (i.e.,
12913 : the type of the entity being initialized) is a better conversion
12914 : sequence than the standard conversion sequence from the return type
12915 : of F2 to the destination type. */
12916 :
12917 4572765 : if (cand1->second_conv)
12918 : {
12919 29274 : winner = compare_ics (cand1->second_conv, cand2->second_conv);
12920 29274 : if (winner)
12921 : return winner;
12922 : }
12923 :
12924 : /* CWG2735 (PR109247): A copy/move ctor/op= for which its operand uses an
12925 : explicit conversion (due to list-initialization) is worse. */
12926 4572647 : {
12927 4572647 : z_candidate *sp = nullptr;
12928 4572647 : if (sfk_copy_or_move (cand1->fn))
12929 385 : sp = cand1;
12930 4572647 : if (sfk_copy_or_move (cand2->fn))
12931 235151 : sp = sp ? nullptr : cand2;
12932 4572599 : if (sp)
12933 : {
12934 470880 : conversion *conv = sp->convs[!DECL_CONSTRUCTOR_P (sp->fn)];
12935 235440 : if (conv->user_conv_p)
12936 1058 : for (; conv; conv = next_conversion (conv))
12937 935 : if (conv->kind == ck_user
12938 406 : && DECL_P (conv->cand->fn)
12939 1341 : && DECL_NONCONVERTING_P (conv->cand->fn))
12940 315 : return (sp == cand1) ? -1 : 1;
12941 : }
12942 : }
12943 :
12944 : /* DR2327: C++17 copy elision in [over.match.ctor] (direct-init) context.
12945 : The standard currently says that only constructors are candidates, but if
12946 : one copies a prvalue returned by a conversion function we prefer that.
12947 :
12948 : Clang does something similar, as discussed at
12949 : http://lists.isocpp.org/core/2017/10/3166.php
12950 : http://lists.isocpp.org/core/2019/03/5721.php */
12951 4222149 : if (len == 1 && cxx_dialect >= cxx17
12952 4202420 : && DECL_P (cand1->fn)
12953 4202420 : && DECL_COMPLETE_CONSTRUCTOR_P (cand1->fn)
12954 4755415 : && !(cand1->flags & LOOKUP_ONLYCONVERTING))
12955 : {
12956 92671 : bool elided1 = joust_maybe_elide_copy (cand1);
12957 92671 : bool elided2 = joust_maybe_elide_copy (cand2);
12958 92671 : winner = elided1 - elided2;
12959 92671 : if (winner)
12960 : return winner;
12961 : }
12962 :
12963 : /* or, if not that,
12964 : F1 is a non-template function and F2 is a template function
12965 : specialization. */
12966 :
12967 4572362 : if (!cand1->template_decl && cand2->template_decl)
12968 : return 1;
12969 4570826 : else if (cand1->template_decl && !cand2->template_decl)
12970 : return -1;
12971 :
12972 : /* or, if not that,
12973 : F1 and F2 are template functions and the function template for F1 is
12974 : more specialized than the template for F2 according to the partial
12975 : ordering rules. */
12976 :
12977 4316705 : if (cand1->template_decl && cand2->template_decl)
12978 : {
12979 1429036 : winner = more_specialized_fn
12980 1429036 : (TI_TEMPLATE (cand1->template_decl),
12981 1429036 : TI_TEMPLATE (cand2->template_decl),
12982 : /* [temp.func.order]: The presence of unused ellipsis and default
12983 : arguments has no effect on the partial ordering of function
12984 : templates. add_function_candidate() will not have
12985 : counted the "this" argument for constructors. */
12986 2858072 : cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
12987 1429036 : if (winner)
12988 : return winner;
12989 : }
12990 :
12991 : /* Concepts: F1 and F2 are non-template functions with the same
12992 : parameter-type-lists, and F1 is more constrained than F2 according to the
12993 : partial ordering of constraints described in 13.5.4. */
12994 :
12995 105775 : if (flag_concepts && DECL_P (cand1->fn) && DECL_P (cand2->fn)
12996 105760 : && !cand1->template_decl && !cand2->template_decl
12997 2997061 : && cand_parms_match (cand1, cand2))
12998 : {
12999 14007 : winner = more_constrained (cand1->fn, cand2->fn);
13000 14007 : if (winner)
13001 : return winner;
13002 : }
13003 :
13004 : /* F2 is a rewritten candidate (12.4.1.2) and F1 is not, or F1 and F2 are
13005 : rewritten candidates, and F2 is a synthesized candidate with reversed
13006 : order of parameters and F1 is not. */
13007 2913219 : if (cand1->rewritten ())
13008 : {
13009 38573 : if (!cand2->rewritten ())
13010 : return -1;
13011 21004 : if (!cand1->reversed () && cand2->reversed ())
13012 : return 1;
13013 21002 : if (cand1->reversed () && !cand2->reversed ())
13014 : return -1;
13015 : }
13016 2874646 : else if (cand2->rewritten ())
13017 : return 1;
13018 :
13019 : /* F1 is generated from a deduction-guide (13.3.1.8) and F2 is not */
13020 2868103 : if (deduction_guide_p (cand1->fn))
13021 : {
13022 873 : gcc_assert (deduction_guide_p (cand2->fn));
13023 : /* We distinguish between candidates from an explicit deduction guide and
13024 : candidates built from a constructor based on DECL_ARTIFICIAL. */
13025 873 : int art1 = DECL_ARTIFICIAL (cand1->fn);
13026 873 : int art2 = DECL_ARTIFICIAL (cand2->fn);
13027 873 : if (art1 != art2)
13028 540 : return art2 - art1;
13029 :
13030 333 : if (art1)
13031 : {
13032 : /* Prefer the special copy guide over a declared copy/move
13033 : constructor. */
13034 333 : if (copy_guide_p (cand1->fn))
13035 : return 1;
13036 256 : if (copy_guide_p (cand2->fn))
13037 : return -1;
13038 :
13039 : /* Prefer a candidate generated from a non-template constructor. */
13040 16 : int tg1 = template_guide_p (cand1->fn);
13041 16 : int tg2 = template_guide_p (cand2->fn);
13042 16 : if (tg1 != tg2)
13043 4 : return tg2 - tg1;
13044 : }
13045 : }
13046 :
13047 : /* F1 is a member of a class D, F2 is a member of a base class B of D, and
13048 : for all arguments the corresponding parameters of F1 and F2 have the same
13049 : type (CWG 2273/2277). */
13050 2867204 : if (DECL_P (cand1->fn) && DECL_CLASS_SCOPE_P (cand1->fn)
13051 35391 : && !DECL_CONV_FN_P (cand1->fn)
13052 35200 : && DECL_P (cand2->fn) && DECL_CLASS_SCOPE_P (cand2->fn)
13053 2902442 : && !DECL_CONV_FN_P (cand2->fn))
13054 : {
13055 35200 : tree base1 = DECL_CONTEXT (strip_inheriting_ctors (cand1->fn));
13056 35200 : tree base2 = DECL_CONTEXT (strip_inheriting_ctors (cand2->fn));
13057 :
13058 35200 : bool used1 = false;
13059 35200 : bool used2 = false;
13060 35200 : if (base1 == base2)
13061 : /* No difference. */;
13062 92 : else if (DERIVED_FROM_P (base1, base2))
13063 : used1 = true;
13064 26 : else if (DERIVED_FROM_P (base2, base1))
13065 35200 : used2 = true;
13066 :
13067 35200 : if (int diff = used2 - used1)
13068 : {
13069 117 : for (i = 0; i < len; ++i)
13070 : {
13071 52 : conversion *t1 = cand1->convs[i + off1];
13072 52 : conversion *t2 = cand2->convs[i + off2];
13073 52 : if (!same_type_p (t1->type, t2->type))
13074 : break;
13075 : }
13076 78 : if (i == len)
13077 : return diff;
13078 : }
13079 : }
13080 :
13081 : /* Check whether we can discard a builtin candidate, either because we
13082 : have two identical ones or matching builtin and non-builtin candidates.
13083 :
13084 : (Pedantically in the latter case the builtin which matched the user
13085 : function should not be added to the overload set, but we spot it here.
13086 :
13087 : [over.match.oper]
13088 : ... the builtin candidates include ...
13089 : - do not have the same parameter type list as any non-template
13090 : non-member candidate. */
13091 :
13092 2867177 : if (identifier_p (cand1->fn) || identifier_p (cand2->fn))
13093 : {
13094 57 : for (i = 0; i < len; ++i)
13095 47 : if (!same_type_p (cand1->convs[i]->type,
13096 : cand2->convs[i]->type))
13097 : break;
13098 35 : if (i == cand1->num_convs)
13099 : {
13100 10 : if (cand1->fn == cand2->fn)
13101 : /* Two built-in candidates; arbitrarily pick one. */
13102 : return 1;
13103 309984 : else if (identifier_p (cand1->fn))
13104 : /* cand1 is built-in; prefer cand2. */
13105 : return -1;
13106 : else
13107 : /* cand2 is built-in; prefer cand1. */
13108 : return 1;
13109 : }
13110 : }
13111 :
13112 : /* For candidates of a multi-versioned function, make the version with
13113 : the highest priority win. This version will be checked for dispatching
13114 : first. If this version can be inlined into the caller, the front-end
13115 : will simply make a direct call to this function. */
13116 :
13117 2867167 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13118 2867139 : && DECL_FUNCTION_VERSIONED (cand1->fn)
13119 1071 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13120 2868238 : && DECL_FUNCTION_VERSIONED (cand2->fn))
13121 : {
13122 1071 : tree f1 = TREE_TYPE (cand1->fn);
13123 1071 : tree f2 = TREE_TYPE (cand2->fn);
13124 1071 : tree p1 = TYPE_ARG_TYPES (f1);
13125 1071 : tree p2 = TYPE_ARG_TYPES (f2);
13126 :
13127 : /* Check if cand1->fn and cand2->fn are versions of the same function. It
13128 : is possible that cand1->fn and cand2->fn are function versions but of
13129 : different functions. Check types to see if they are versions of the same
13130 : function. */
13131 1071 : if (compparms (p1, p2)
13132 1071 : && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
13133 : {
13134 : /* Always make the version with the higher priority, more
13135 : specialized, win. */
13136 1071 : gcc_assert (targetm.compare_version_priority);
13137 1071 : if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
13138 : return 1;
13139 : else
13140 : return -1;
13141 : }
13142 : }
13143 :
13144 : /* If the two function declarations represent the same function (this can
13145 : happen with declarations in multiple scopes and arg-dependent lookup),
13146 : arbitrarily choose one. But first make sure the default args we're
13147 : using match. */
13148 2866068 : if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
13149 5732164 : && equal_functions (cand1->fn, cand2->fn))
13150 : {
13151 38 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
13152 38 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
13153 :
13154 76 : gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
13155 :
13156 52 : for (i = 0; i < len; ++i)
13157 : {
13158 : /* Don't crash if the fn is variadic. */
13159 14 : if (!parms1)
13160 : break;
13161 14 : parms1 = TREE_CHAIN (parms1);
13162 14 : parms2 = TREE_CHAIN (parms2);
13163 : }
13164 :
13165 38 : if (off1)
13166 0 : parms1 = TREE_CHAIN (parms1);
13167 38 : else if (off2)
13168 0 : parms2 = TREE_CHAIN (parms2);
13169 :
13170 72 : for (; parms1; ++i)
13171 : {
13172 84 : if (!cp_tree_equal (TREE_PURPOSE (parms1),
13173 42 : TREE_PURPOSE (parms2)))
13174 : {
13175 8 : if (warn)
13176 : {
13177 4 : if (complain & tf_error)
13178 : {
13179 4 : auto_diagnostic_group d;
13180 4 : if (permerror (input_location,
13181 : "default argument mismatch in "
13182 : "overload resolution"))
13183 : {
13184 4 : inform (DECL_SOURCE_LOCATION (cand1->fn),
13185 : " candidate 1: %q#F", cand1->fn);
13186 4 : inform (DECL_SOURCE_LOCATION (cand2->fn),
13187 : " candidate 2: %q#F", cand2->fn);
13188 : }
13189 4 : }
13190 : else
13191 : return 0;
13192 : }
13193 : else
13194 4 : add_warning (cand1, cand2);
13195 : break;
13196 : }
13197 34 : parms1 = TREE_CHAIN (parms1);
13198 34 : parms2 = TREE_CHAIN (parms2);
13199 : }
13200 :
13201 38 : return 1;
13202 : }
13203 :
13204 2882597 : tweak:
13205 :
13206 : /* Extension: If the worst conversion for one candidate is better than the
13207 : worst conversion for the other, take the first. */
13208 2882597 : if (!pedantic && (complain & tf_warning_or_error))
13209 : {
13210 : conversion_rank rank1 = cr_identity, rank2 = cr_identity;
13211 5958746 : struct z_candidate *w = 0, *l = 0;
13212 :
13213 5958746 : for (i = 0; i < len; ++i)
13214 : {
13215 3098788 : if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
13216 2841393 : rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
13217 3098788 : if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
13218 2841411 : rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
13219 : }
13220 2859958 : if (rank1 < rank2)
13221 38 : winner = 1, w = cand1, l = cand2;
13222 2859958 : if (rank1 > rank2)
13223 105 : winner = -1, w = cand2, l = cand1;
13224 2859958 : if (winner)
13225 : {
13226 : /* Don't choose a deleted function over ambiguity. */
13227 143 : if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
13228 : return 0;
13229 143 : if (warn)
13230 : {
13231 7 : auto_diagnostic_group d;
13232 7 : if (pedwarn (input_location, 0,
13233 : "ISO C++ says that these are ambiguous, even "
13234 : "though the worst conversion for the first is "
13235 : "better than the worst conversion for the second:"))
13236 : {
13237 4 : print_z_candidate (input_location, N_("candidate 1:"), w);
13238 4 : print_z_candidate (input_location, N_("candidate 2:"), l);
13239 : }
13240 7 : }
13241 : else
13242 136 : add_warning (w, l);
13243 143 : return winner;
13244 : }
13245 : }
13246 :
13247 : gcc_assert (!winner);
13248 : return 0;
13249 : }
13250 :
13251 : /* Given a list of candidates for overloading, find the best one, if any.
13252 : This algorithm has a worst case of O(2n) (winner is last), and a best
13253 : case of O(n/2) (totally ambiguous); much better than a sorting
13254 : algorithm. */
13255 :
13256 : static struct z_candidate *
13257 95347210 : tourney (struct z_candidate *candidates, tsubst_flags_t complain)
13258 : {
13259 95347210 : struct z_candidate *champ = candidates, *challenger;
13260 95347210 : int fate;
13261 95347210 : struct z_candidate *champ_compared_to_predecessor = nullptr;
13262 :
13263 : /* Walk through the list once, comparing each current champ to the next
13264 : candidate, knocking out a candidate or two with each comparison. */
13265 :
13266 120081474 : for (challenger = champ->next; challenger; )
13267 : {
13268 24738949 : fate = joust (champ, challenger, 0, complain);
13269 24738949 : if (fate == 1)
13270 13801632 : challenger = challenger->next;
13271 : else
13272 : {
13273 10937317 : if (fate == 0)
13274 : {
13275 2880776 : champ = challenger->next;
13276 2880776 : if (champ == 0)
13277 : return NULL;
13278 : champ_compared_to_predecessor = nullptr;
13279 : }
13280 : else
13281 : {
13282 : champ_compared_to_predecessor = champ;
13283 : champ = challenger;
13284 : }
13285 :
13286 10932632 : challenger = champ->next;
13287 : }
13288 : }
13289 :
13290 : /* Make sure the champ is better than all the candidates it hasn't yet
13291 : been compared to. */
13292 :
13293 6489840 : for (challenger = candidates;
13294 101832365 : challenger != champ
13295 101832365 : && challenger != champ_compared_to_predecessor;
13296 6489840 : challenger = challenger->next)
13297 : {
13298 6491710 : fate = joust (champ, challenger, 0, complain);
13299 6491710 : if (fate != 1)
13300 : return NULL;
13301 : }
13302 :
13303 : return champ;
13304 : }
13305 :
13306 : /* Returns nonzero if things of type FROM can be converted to TO. */
13307 :
13308 : bool
13309 1569202 : can_convert (tree to, tree from, tsubst_flags_t complain)
13310 : {
13311 1569202 : tree arg = NULL_TREE;
13312 : /* implicit_conversion only considers user-defined conversions
13313 : if it has an expression for the call argument list. */
13314 1569202 : if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to))
13315 41 : arg = build_stub_object (from);
13316 1569202 : return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain);
13317 : }
13318 :
13319 : /* Returns nonzero if things of type FROM can be converted to TO with a
13320 : standard conversion. */
13321 :
13322 : bool
13323 493 : can_convert_standard (tree to, tree from, tsubst_flags_t complain)
13324 : {
13325 493 : return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
13326 : }
13327 :
13328 : /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
13329 :
13330 : bool
13331 2018019 : can_convert_arg (tree to, tree from, tree arg, int flags,
13332 : tsubst_flags_t complain)
13333 : {
13334 2018019 : conversion *t;
13335 2018019 : void *p;
13336 2018019 : bool ok_p;
13337 :
13338 : /* Get the high-water mark for the CONVERSION_OBSTACK. */
13339 2018019 : p = conversion_obstack_alloc (0);
13340 : /* We want to discard any access checks done for this test,
13341 : as we might not be in the appropriate access context and
13342 : we'll do the check again when we actually perform the
13343 : conversion. */
13344 2018019 : push_deferring_access_checks (dk_deferred);
13345 :
13346 2018019 : t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
13347 : flags, complain);
13348 2018019 : ok_p = (t && !t->bad_p);
13349 :
13350 : /* Discard the access checks now. */
13351 2018019 : pop_deferring_access_checks ();
13352 : /* Free all the conversions we allocated. */
13353 2018019 : obstack_free (&conversion_obstack, p);
13354 :
13355 2018019 : return ok_p;
13356 : }
13357 :
13358 : /* Like can_convert_arg, but allows dubious conversions as well. */
13359 :
13360 : bool
13361 57568173 : can_convert_arg_bad (tree to, tree from, tree arg, int flags,
13362 : tsubst_flags_t complain)
13363 : {
13364 57568173 : conversion *t;
13365 57568173 : void *p;
13366 :
13367 : /* Get the high-water mark for the CONVERSION_OBSTACK. */
13368 57568173 : p = conversion_obstack_alloc (0);
13369 : /* Try to perform the conversion. */
13370 57568173 : t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
13371 : flags, complain);
13372 : /* Free all the conversions we allocated. */
13373 57568173 : obstack_free (&conversion_obstack, p);
13374 :
13375 57568173 : return t != NULL;
13376 : }
13377 :
13378 : /* Return an IMPLICIT_CONV_EXPR from EXPR to TYPE with bits set from overload
13379 : resolution FLAGS. */
13380 :
13381 : tree
13382 5925950 : build_implicit_conv_flags (tree type, tree expr, int flags)
13383 : {
13384 : /* In a template, we are only concerned about determining the
13385 : type of non-dependent expressions, so we do not have to
13386 : perform the actual conversion. But for initializers, we
13387 : need to be able to perform it at instantiation
13388 : (or instantiate_non_dependent_expr) time. */
13389 5925950 : expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
13390 5925950 : if (!(flags & LOOKUP_ONLYCONVERTING))
13391 2062480 : IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
13392 5925950 : if (flags & LOOKUP_NO_NARROWING)
13393 1560 : IMPLICIT_CONV_EXPR_BRACED_INIT (expr) = true;
13394 5925950 : return expr;
13395 : }
13396 :
13397 : /* Convert EXPR to TYPE. Return the converted expression.
13398 :
13399 : Note that we allow bad conversions here because by the time we get to
13400 : this point we are committed to doing the conversion. If we end up
13401 : doing a bad conversion, convert_like will complain. */
13402 :
13403 : tree
13404 108144035 : perform_implicit_conversion_flags (tree type, tree expr,
13405 : tsubst_flags_t complain, int flags)
13406 : {
13407 108144035 : conversion *conv;
13408 108144035 : void *p;
13409 108144035 : location_t loc = cp_expr_loc_or_input_loc (expr);
13410 :
13411 108144035 : if (TYPE_REF_P (type))
13412 228863 : expr = mark_lvalue_use (expr);
13413 : else
13414 107915172 : expr = mark_rvalue_use (expr);
13415 :
13416 108144035 : if (error_operand_p (expr))
13417 455 : return error_mark_node;
13418 :
13419 : /* Get the high-water mark for the CONVERSION_OBSTACK. */
13420 108143580 : p = conversion_obstack_alloc (0);
13421 :
13422 108143580 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
13423 : /*c_cast_p=*/false,
13424 : flags, complain);
13425 :
13426 108143580 : if (!conv)
13427 : {
13428 88219 : if (complain & tf_error)
13429 425 : implicit_conversion_error (loc, type, expr);
13430 88219 : expr = error_mark_node;
13431 : }
13432 108055361 : else if (processing_template_decl && conv->kind != ck_identity)
13433 5925717 : expr = build_implicit_conv_flags (type, expr, flags);
13434 : else
13435 : {
13436 : /* Give a conversion call the same location as expr. */
13437 102129644 : iloc_sentinel il (loc);
13438 102129644 : expr = convert_like (conv, expr, complain);
13439 102129644 : }
13440 :
13441 : /* Free all the conversions we allocated. */
13442 108143580 : obstack_free (&conversion_obstack, p);
13443 :
13444 : return expr;
13445 : }
13446 :
13447 : tree
13448 17576563 : perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
13449 : {
13450 17181691 : return perform_implicit_conversion_flags (type, expr, complain,
13451 16782563 : LOOKUP_IMPLICIT);
13452 : }
13453 :
13454 : /* Convert EXPR to TYPE (as a direct-initialization) if that is
13455 : permitted. If the conversion is valid, the converted expression is
13456 : returned. Otherwise, NULL_TREE is returned, except in the case
13457 : that TYPE is a class type; in that case, an error is issued. If
13458 : C_CAST_P is true, then this direct-initialization is taking
13459 : place as part of a static_cast being attempted as part of a C-style
13460 : cast. */
13461 :
13462 : tree
13463 29469582 : perform_direct_initialization_if_possible (tree type,
13464 : tree expr,
13465 : bool c_cast_p,
13466 : tsubst_flags_t complain)
13467 : {
13468 29469582 : conversion *conv;
13469 29469582 : void *p;
13470 :
13471 29469582 : if (type == error_mark_node || error_operand_p (expr))
13472 : return error_mark_node;
13473 : /* [dcl.init]
13474 :
13475 : If the destination type is a (possibly cv-qualified) class type:
13476 :
13477 : -- If the initialization is direct-initialization ...,
13478 : constructors are considered.
13479 :
13480 : -- If overload resolution is successful, the selected constructor
13481 : is called to initialize the object, with the initializer expression
13482 : or expression-list as its argument(s).
13483 :
13484 : -- Otherwise, if no constructor is viable, the destination type is
13485 : a (possibly cv-qualified) aggregate class A, and the initializer is
13486 : a parenthesized expression-list, the object is initialized as
13487 : follows... */
13488 29469582 : if (CLASS_TYPE_P (type))
13489 : {
13490 610199 : releasing_vec args (make_tree_vector_single (expr));
13491 610199 : expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
13492 : &args, type, LOOKUP_NORMAL, complain);
13493 610199 : return build_cplus_new (type, expr, complain);
13494 610199 : }
13495 :
13496 : /* Get the high-water mark for the CONVERSION_OBSTACK. */
13497 28859383 : p = conversion_obstack_alloc (0);
13498 :
13499 28859383 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
13500 : c_cast_p,
13501 : LOOKUP_NORMAL, complain);
13502 28859383 : if (!conv || conv->bad_p)
13503 : expr = NULL_TREE;
13504 25825053 : else if (processing_template_decl && conv->kind != ck_identity)
13505 : {
13506 : /* In a template, we are only concerned about determining the
13507 : type of non-dependent expressions, so we do not have to
13508 : perform the actual conversion. But for initializers, we
13509 : need to be able to perform it at instantiation
13510 : (or instantiate_non_dependent_expr) time. */
13511 440581 : expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
13512 440581 : IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
13513 : }
13514 : else
13515 25384472 : expr = convert_like (conv, expr, NULL_TREE, 0,
13516 : /*issue_conversion_warnings=*/false,
13517 : c_cast_p, /*nested_p=*/false, complain);
13518 :
13519 : /* Free all the conversions we allocated. */
13520 28859383 : obstack_free (&conversion_obstack, p);
13521 :
13522 : return expr;
13523 : }
13524 :
13525 : /* When initializing a reference that lasts longer than a full-expression,
13526 : this special rule applies:
13527 :
13528 : [class.temporary]
13529 :
13530 : The temporary to which the reference is bound or the temporary
13531 : that is the complete object to which the reference is bound
13532 : persists for the lifetime of the reference.
13533 :
13534 : The temporaries created during the evaluation of the expression
13535 : initializing the reference, except the temporary to which the
13536 : reference is bound, are destroyed at the end of the
13537 : full-expression in which they are created.
13538 :
13539 : In that case, we store the converted expression into a new
13540 : VAR_DECL in a new scope.
13541 :
13542 : However, we want to be careful not to create temporaries when
13543 : they are not required. For example, given:
13544 :
13545 : struct B {};
13546 : struct D : public B {};
13547 : D f();
13548 : const B& b = f();
13549 :
13550 : there is no need to copy the return value from "f"; we can just
13551 : extend its lifetime. Similarly, given:
13552 :
13553 : struct S {};
13554 : struct T { operator S(); };
13555 : T t;
13556 : const S& s = t;
13557 :
13558 : we can extend the lifetime of the return value of the conversion
13559 : operator.
13560 :
13561 : The next several functions are involved in this lifetime extension. */
13562 :
13563 : /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
13564 : reference is being bound to a temporary. Create and return a new
13565 : VAR_DECL with the indicated TYPE; this variable will store the value to
13566 : which the reference is bound. */
13567 :
13568 : tree
13569 4556 : make_temporary_var_for_ref_to_temp (tree decl, tree type)
13570 : {
13571 4556 : tree var = create_temporary_var (type);
13572 :
13573 : /* Register the variable. */
13574 4556 : if (VAR_P (decl)
13575 4556 : && (TREE_STATIC (decl) || CP_DECL_THREAD_LOCAL_P (decl)))
13576 : {
13577 : /* Namespace-scope or local static; give it a mangled name. */
13578 :
13579 : /* If an initializer is visible to multiple translation units, those
13580 : translation units must agree on the addresses of the
13581 : temporaries. Therefore the temporaries must be given a consistent name
13582 : and vague linkage. The mangled name of a temporary is the name of the
13583 : non-temporary object in whose initializer they appear, prefixed with
13584 : GR and suffixed with a sequence number mangled using the usual rules
13585 : for a seq-id. Temporaries are numbered with a pre-order, depth-first,
13586 : left-to-right walk of the complete initializer. */
13587 507 : copy_linkage (var, decl);
13588 :
13589 507 : tree name = mangle_ref_init_variable (decl);
13590 507 : DECL_NAME (var) = name;
13591 507 : SET_DECL_ASSEMBLER_NAME (var, name);
13592 : }
13593 : else
13594 : /* Create a new cleanup level if necessary. */
13595 4049 : maybe_push_cleanup_level (type);
13596 :
13597 4556 : return pushdecl (var);
13598 : }
13599 :
13600 : /* EXPR is the initializer for a variable DECL of reference or
13601 : std::initializer_list type. Create, push and return a new VAR_DECL
13602 : for the initializer so that it will live as long as DECL. Any
13603 : cleanup for the new variable is returned through CLEANUP, and the
13604 : code to initialize the new variable is returned through INITP. */
13605 :
13606 : static tree
13607 4556 : set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
13608 : tree *initp, tree *cond_guard)
13609 : {
13610 4556 : tree init;
13611 4556 : tree type;
13612 4556 : tree var;
13613 :
13614 : /* Create the temporary variable. */
13615 4556 : type = TREE_TYPE (expr);
13616 4556 : var = make_temporary_var_for_ref_to_temp (decl, type);
13617 4556 : layout_decl (var, 0);
13618 : /* If the rvalue is the result of a function call it will be
13619 : a TARGET_EXPR. If it is some other construct (such as a
13620 : member access expression where the underlying object is
13621 : itself the result of a function call), turn it into a
13622 : TARGET_EXPR here. It is important that EXPR be a
13623 : TARGET_EXPR below since otherwise the INIT_EXPR will
13624 : attempt to make a bitwise copy of EXPR to initialize
13625 : VAR. */
13626 4556 : if (TREE_CODE (expr) != TARGET_EXPR)
13627 0 : expr = get_target_expr (expr);
13628 : else
13629 : {
13630 4556 : if (TREE_ADDRESSABLE (expr))
13631 4502 : TREE_ADDRESSABLE (var) = 1;
13632 4556 : if (DECL_MERGEABLE (TARGET_EXPR_SLOT (expr)))
13633 395 : DECL_MERGEABLE (var) = true;
13634 : }
13635 :
13636 4556 : if (TREE_CODE (decl) == FIELD_DECL
13637 4556 : && extra_warnings && !warning_suppressed_p (decl))
13638 : {
13639 4 : warning (OPT_Wextra, "a temporary bound to %qD only persists "
13640 : "until the constructor exits", decl);
13641 4 : suppress_warning (decl);
13642 : }
13643 :
13644 : /* Recursively extend temps in this initializer. */
13645 4556 : TARGET_EXPR_INITIAL (expr)
13646 4556 : = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups,
13647 : cond_guard);
13648 :
13649 : /* Any reference temp has a non-trivial initializer. */
13650 4556 : DECL_NONTRIVIALLY_INITIALIZED_P (var) = true;
13651 :
13652 : /* If the initializer is constant, put it in DECL_INITIAL so we get
13653 : static initialization and use in constant expressions. */
13654 4556 : init = maybe_constant_init (expr, var, /*manifestly_const_eval=*/true);
13655 : /* As in store_init_value. */
13656 4556 : init = cp_fully_fold (init);
13657 4556 : if (TREE_CONSTANT (init))
13658 : {
13659 709 : if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
13660 : {
13661 : /* 5.19 says that a constant expression can include an
13662 : lvalue-rvalue conversion applied to "a glvalue of literal type
13663 : that refers to a non-volatile temporary object initialized
13664 : with a constant expression". Rather than try to communicate
13665 : that this VAR_DECL is a temporary, just mark it constexpr. */
13666 473 : DECL_DECLARED_CONSTEXPR_P (var) = true;
13667 473 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
13668 473 : TREE_CONSTANT (var) = true;
13669 473 : TREE_READONLY (var) = true;
13670 : }
13671 709 : DECL_INITIAL (var) = init;
13672 709 : init = NULL_TREE;
13673 : }
13674 : else
13675 : /* Create the INIT_EXPR that will initialize the temporary
13676 : variable. */
13677 3847 : init = split_nonconstant_init (var, expr);
13678 4556 : if (at_function_scope_p ())
13679 : {
13680 4101 : add_decl_expr (var);
13681 :
13682 4101 : if (TREE_STATIC (var))
13683 52 : init = add_stmt_to_compound (init, register_dtor_fn (var));
13684 : else
13685 : {
13686 4049 : tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
13687 4049 : if (cleanup)
13688 : {
13689 666 : if (cond_guard && cleanup != error_mark_node)
13690 : {
13691 21 : if (*cond_guard == NULL_TREE)
13692 : {
13693 21 : *cond_guard = build_local_temp (boolean_type_node);
13694 21 : add_decl_expr (*cond_guard);
13695 21 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION,
13696 : *cond_guard, NOP_EXPR,
13697 : boolean_false_node,
13698 : tf_warning_or_error);
13699 21 : finish_expr_stmt (set);
13700 : }
13701 21 : cleanup = build3 (COND_EXPR, void_type_node,
13702 : *cond_guard, cleanup, NULL_TREE);
13703 : }
13704 666 : vec_safe_push (*cleanups, cleanup);
13705 : }
13706 : }
13707 :
13708 : /* We must be careful to destroy the temporary only
13709 : after its initialization has taken place. If the
13710 : initialization throws an exception, then the
13711 : destructor should not be run. We cannot simply
13712 : transform INIT into something like:
13713 :
13714 : (INIT, ({ CLEANUP_STMT; }))
13715 :
13716 : because emit_local_var always treats the
13717 : initializer as a full-expression. Thus, the
13718 : destructor would run too early; it would run at the
13719 : end of initializing the reference variable, rather
13720 : than at the end of the block enclosing the
13721 : reference variable.
13722 :
13723 : The solution is to pass back a cleanup expression
13724 : which the caller is responsible for attaching to
13725 : the statement tree. */
13726 : }
13727 : else
13728 : {
13729 455 : rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
13730 455 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
13731 : {
13732 14 : if (CP_DECL_THREAD_LOCAL_P (var))
13733 0 : tls_aggregates = tree_cons (NULL_TREE, var,
13734 : tls_aggregates);
13735 : else
13736 14 : static_aggregates = tree_cons (NULL_TREE, var,
13737 : static_aggregates);
13738 : }
13739 : else
13740 : /* Check whether the dtor is callable. */
13741 441 : cxx_maybe_build_cleanup (var, tf_warning_or_error);
13742 : }
13743 : /* Avoid -Wunused-variable warning (c++/38958). */
13744 4556 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
13745 4556 : && VAR_P (decl))
13746 592 : TREE_USED (decl) = DECL_READ_P (decl) = true;
13747 :
13748 4556 : *initp = init;
13749 4556 : return var;
13750 : }
13751 :
13752 : /* Convert EXPR to the indicated reference TYPE, in a way suitable for
13753 : initializing a variable of that TYPE. */
13754 :
13755 : tree
13756 3178752 : initialize_reference (tree type, tree expr,
13757 : int flags, tsubst_flags_t complain)
13758 : {
13759 3178752 : conversion *conv;
13760 3178752 : void *p;
13761 3178752 : location_t loc = cp_expr_loc_or_input_loc (expr);
13762 :
13763 3178752 : if (type == error_mark_node || error_operand_p (expr))
13764 : return error_mark_node;
13765 :
13766 : /* Get the high-water mark for the CONVERSION_OBSTACK. */
13767 3178687 : p = conversion_obstack_alloc (0);
13768 :
13769 3178687 : conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
13770 : flags, complain);
13771 : /* If this conversion failed, we're in C++20, and we have something like
13772 : A& a(b) where A is an aggregate, try again, this time as A& a{b}. */
13773 3178687 : if ((!conv || conv->bad_p)
13774 430 : && (flags & LOOKUP_AGGREGATE_PAREN_INIT))
13775 : {
13776 2 : tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
13777 2 : CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
13778 2 : CONSTRUCTOR_IS_PAREN_INIT (e) = true;
13779 2 : conversion *c = reference_binding (type, TREE_TYPE (e), e,
13780 : /*c_cast_p=*/false, flags, complain);
13781 : /* If this worked, use it. */
13782 2 : if (c && !c->bad_p)
13783 : expr = e, conv = c;
13784 : }
13785 3179070 : if (!conv || conv->bad_p)
13786 : {
13787 429 : if (complain & tf_error)
13788 : {
13789 406 : if (conv)
13790 360 : convert_like (conv, expr, complain);
13791 46 : else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
13792 15 : && !TYPE_REF_IS_RVALUE (type)
13793 61 : && !lvalue_p (expr))
13794 6 : error_at (loc, "invalid initialization of non-const reference of "
13795 : "type %qH from an rvalue of type %qI",
13796 6 : type, TREE_TYPE (expr));
13797 : else
13798 40 : error_at (loc, "invalid initialization of reference of type "
13799 : "%qH from expression of type %qI", type,
13800 40 : TREE_TYPE (expr));
13801 : }
13802 429 : return error_mark_node;
13803 : }
13804 :
13805 3178258 : if (conv->kind == ck_ref_bind)
13806 : /* Perform the conversion. */
13807 3178254 : expr = convert_like (conv, expr, complain);
13808 4 : else if (conv->kind == ck_ambig)
13809 : /* We gave an error in build_user_type_conversion_1. */
13810 4 : expr = error_mark_node;
13811 : else
13812 0 : gcc_unreachable ();
13813 :
13814 : /* Free all the conversions we allocated. */
13815 3178258 : obstack_free (&conversion_obstack, p);
13816 :
13817 : return expr;
13818 : }
13819 :
13820 : /* Return true if T is std::pair<const T&, const T&>. */
13821 :
13822 : static bool
13823 466415 : std_pair_ref_ref_p (tree t)
13824 : {
13825 : /* First, check if we have std::pair. */
13826 36762 : if (!NON_UNION_CLASS_TYPE_P (t)
13827 502682 : || !CLASSTYPE_TEMPLATE_INSTANTIATION (t))
13828 : return false;
13829 4326 : tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (t));
13830 4326 : if (!decl_in_std_namespace_p (tdecl))
13831 : return false;
13832 2524 : tree name = DECL_NAME (tdecl);
13833 2524 : if (!name || !id_equal (name, "pair"))
13834 : return false;
13835 :
13836 : /* Now see if the template arguments are both const T&. */
13837 252 : tree args = CLASSTYPE_TI_ARGS (t);
13838 252 : if (TREE_VEC_LENGTH (args) != 2)
13839 : return false;
13840 292 : for (int i = 0; i < 2; i++)
13841 312 : if (!TYPE_REF_OBJ_P (TREE_VEC_ELT (args, i))
13842 312 : || !CP_TYPE_CONST_P (TREE_TYPE (TREE_VEC_ELT (args, i))))
13843 232 : return false;
13844 :
13845 : return true;
13846 : }
13847 :
13848 : /* Return true if a class CTYPE is either std::reference_wrapper or
13849 : std::ref_view, or a reference wrapper class. We consider a class
13850 : a reference wrapper class if it has a reference member. We no
13851 : longer check that it has a constructor taking the same reference type
13852 : since that approach still generated too many false positives. */
13853 :
13854 : static bool
13855 169 : class_has_reference_member_p (tree t)
13856 : {
13857 169 : for (tree fields = TYPE_FIELDS (t);
13858 2745 : fields;
13859 2576 : fields = DECL_CHAIN (fields))
13860 2639 : if (TREE_CODE (fields) == FIELD_DECL
13861 139 : && !DECL_ARTIFICIAL (fields)
13862 2747 : && TYPE_REF_P (TREE_TYPE (fields)))
13863 : return true;
13864 : return false;
13865 : }
13866 :
13867 : /* A wrapper for the above suitable as a callback for dfs_walk_once. */
13868 :
13869 : static tree
13870 169 : class_has_reference_member_p_r (tree binfo, void *)
13871 : {
13872 169 : return (class_has_reference_member_p (BINFO_TYPE (binfo))
13873 169 : ? integer_one_node : NULL_TREE);
13874 : }
13875 :
13876 : static bool
13877 267 : reference_like_class_p (tree ctype)
13878 : {
13879 267 : if (!CLASS_TYPE_P (ctype))
13880 : return false;
13881 :
13882 : /* Also accept a std::pair<const T&, const T&>. */
13883 162 : if (std_pair_ref_ref_p (ctype))
13884 : return true;
13885 :
13886 152 : tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (ctype));
13887 152 : if (decl_in_std_namespace_p (tdecl))
13888 : {
13889 21 : tree name = DECL_NAME (tdecl);
13890 21 : if (name
13891 21 : && (id_equal (name, "reference_wrapper")
13892 9 : || id_equal (name, "span")
13893 8 : || id_equal (name, "ref_view")))
13894 : return true;
13895 : }
13896 :
13897 : /* Some classes, such as std::tuple, have the reference member in its
13898 : (non-direct) base class. */
13899 139 : if (dfs_walk_once (TYPE_BINFO (ctype), class_has_reference_member_p_r,
13900 : nullptr, nullptr))
13901 : return true;
13902 :
13903 : return false;
13904 : }
13905 :
13906 : /* Helper for maybe_warn_dangling_reference to find a problematic CALL_EXPR
13907 : that initializes the LHS (and at least one of its arguments represents
13908 : a temporary, as outlined in maybe_warn_dangling_reference), or NULL_TREE
13909 : if none found. For instance:
13910 :
13911 : const S& s = S().self(); // S::self (&TARGET_EXPR <...>)
13912 : const int& r = (42, f(1)); // f(1)
13913 : const int& t = b ? f(1) : f(2); // f(1)
13914 : const int& u = b ? f(1) : f(g); // f(1)
13915 : const int& v = b ? f(g) : f(2); // f(2)
13916 : const int& w = b ? f(g) : f(g); // NULL_TREE
13917 : const int& y = (f(1), 42); // NULL_TREE
13918 : const int& z = f(f(1)); // f(f(1))
13919 :
13920 : EXPR is the initializer. If ARG_P is true, we're processing an argument
13921 : to a function; the point is to distinguish between, for example,
13922 :
13923 : Ref::inner (&TARGET_EXPR <D.2839, F::foo (fm)>)
13924 :
13925 : where we shouldn't warn, and
13926 :
13927 : Ref::inner (&TARGET_EXPR <D.2908, F::foo (&TARGET_EXPR <...>)>)
13928 :
13929 : where we should warn (Ref is a reference_like_class_p so we see through
13930 : it. */
13931 :
13932 : static tree
13933 2850 : do_warn_dangling_reference (tree expr, bool arg_p)
13934 : {
13935 2850 : STRIP_NOPS (expr);
13936 :
13937 2850 : if (arg_p && expr_represents_temporary_p (expr))
13938 : {
13939 : /* An attempt to reduce the number of -Wdangling-reference
13940 : false positives concerning reference wrappers (c++/107532).
13941 : When we encounter a reference_like_class_p, we don't warn
13942 : just yet; instead, we keep recursing to see if there were
13943 : any temporaries behind the reference-wrapper class. */
13944 : tree e = expr;
13945 222 : while (handled_component_p (e))
13946 15 : e = TREE_OPERAND (e, 0);
13947 207 : if (!reference_like_class_p (TREE_TYPE (e)))
13948 : return expr;
13949 : }
13950 :
13951 2689 : switch (TREE_CODE (expr))
13952 : {
13953 1486 : case CALL_EXPR:
13954 1486 : {
13955 1486 : tree fndecl = cp_get_callee_fndecl_nofold (expr);
13956 1486 : if (!fndecl
13957 1486 : || warning_suppressed_p (fndecl, OPT_Wdangling_reference)
13958 1473 : || !warning_enabled_at (DECL_SOURCE_LOCATION (fndecl),
13959 : OPT_Wdangling_reference)
13960 : /* Don't emit a false positive for:
13961 : std::vector<int> v = ...;
13962 : std::vector<int>::const_iterator it = v.begin();
13963 : const int &r = *it++;
13964 : because R refers to one of the int elements of V, not to
13965 : a temporary object. Member operator* may return a reference
13966 : but probably not to one of its arguments. */
13967 2012 : || (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
13968 310 : && DECL_OVERLOADED_OPERATOR_P (fndecl)
13969 102 : && DECL_OVERLOADED_OPERATOR_IS (fndecl, INDIRECT_REF)))
13970 1023 : return NULL_TREE;
13971 :
13972 463 : tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
13973 : /* If the function doesn't return a reference, don't warn. This
13974 : can be e.g.
13975 : const int& z = std::min({1, 2, 3, 4, 5, 6, 7});
13976 : which doesn't dangle: std::min here returns an int.
13977 :
13978 : If the function returns a std::pair<const T&, const T&>, we
13979 : warn, to detect e.g.
13980 : std::pair<const int&, const int&> v = std::minmax(1, 2);
13981 : which also creates a dangling reference, because std::minmax
13982 : returns std::pair<const T&, const T&>(b, a). */
13983 463 : if (!(TYPE_REF_OBJ_P (rettype) || reference_like_class_p (rettype)))
13984 : return NULL_TREE;
13985 :
13986 : /* Here we're looking to see if any of the arguments is a temporary
13987 : initializing a reference parameter. */
13988 512 : for (int i = 0; i < call_expr_nargs (expr); ++i)
13989 : {
13990 445 : tree arg = CALL_EXPR_ARG (expr, i);
13991 : /* Check that this argument initializes a reference, except for
13992 : the argument initializing the object of a member function. */
13993 445 : if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
13994 445 : && !TYPE_REF_P (TREE_TYPE (arg)))
13995 18 : continue;
13996 427 : STRIP_NOPS (arg);
13997 427 : if (TREE_CODE (arg) == ADDR_EXPR)
13998 332 : arg = TREE_OPERAND (arg, 0);
13999 : /* Recurse to see if the argument is a temporary. It could also
14000 : be another call taking a temporary and returning it and
14001 : initializing this reference parameter. */
14002 427 : if (do_warn_dangling_reference (arg, /*arg_p=*/true))
14003 : return expr;
14004 : /* Don't warn about member function like:
14005 : std::any a(...);
14006 : S& s = a.emplace<S>({0}, 0);
14007 : which constructs a new object and returns a reference to it, but
14008 : we still want to detect:
14009 : struct S { const S& self () { return *this; } };
14010 : const S& s = S().self();
14011 : where 's' dangles. If we've gotten here, the object this function
14012 : is invoked on is not a temporary. */
14013 227 : if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl))
14014 : break;
14015 : }
14016 : return NULL_TREE;
14017 : }
14018 21 : case COMPOUND_EXPR:
14019 21 : return do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p);
14020 31 : case COND_EXPR:
14021 31 : if (tree t = do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p))
14022 : return t;
14023 16 : return do_warn_dangling_reference (TREE_OPERAND (expr, 2), arg_p);
14024 0 : case PAREN_EXPR:
14025 0 : return do_warn_dangling_reference (TREE_OPERAND (expr, 0), arg_p);
14026 56 : case TARGET_EXPR:
14027 56 : return do_warn_dangling_reference (TARGET_EXPR_INITIAL (expr), arg_p);
14028 : default:
14029 : return NULL_TREE;
14030 : }
14031 : }
14032 :
14033 : /* Implement -Wdangling-reference, to detect cases like
14034 :
14035 : int n = 1;
14036 : const int& r = std::max(n - 1, n + 1); // r is dangling
14037 :
14038 : This creates temporaries from the arguments, returns a reference to
14039 : one of the temporaries, but both temporaries are destroyed at the end
14040 : of the full expression.
14041 :
14042 : This works by checking if a reference is initialized with a function
14043 : that returns a reference, and at least one parameter of the function
14044 : is a reference that is bound to a temporary. It assumes that such a
14045 : function actually returns one of its arguments.
14046 :
14047 : DECL is the reference being initialized, INIT is the initializer. */
14048 :
14049 : static void
14050 22574372 : maybe_warn_dangling_reference (const_tree decl, tree init)
14051 : {
14052 22574372 : if (!warn_dangling_reference)
14053 : return;
14054 468542 : tree type = TREE_TYPE (decl);
14055 : /* Only warn if what we're initializing has type T&& or const T&, or
14056 : std::pair<const T&, const T&>. (A non-const lvalue reference can't
14057 : bind to a temporary.) */
14058 934795 : if (!((TYPE_REF_OBJ_P (type)
14059 5103 : && (TYPE_REF_IS_RVALUE (type)
14060 4861 : || CP_TYPE_CONST_P (TREE_TYPE (type))))
14061 466253 : || std_pair_ref_ref_p (type)))
14062 : return;
14063 : /* Don't suppress the diagnostic just because the call comes from
14064 : a system header. If the DECL is not in a system header, or if
14065 : -Wsystem-headers was provided, warn. */
14066 2299 : auto wsh
14067 2299 : = make_temp_override (global_dc->dc_warn_system_headers,
14068 2299 : (!in_system_header_at (DECL_SOURCE_LOCATION (decl))
14069 2299 : || global_dc->dc_warn_system_headers));
14070 2299 : if (tree call = do_warn_dangling_reference (init, /*arg_p=*/false))
14071 : {
14072 161 : auto_diagnostic_group d;
14073 161 : if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wdangling_reference,
14074 : "possibly dangling reference to a temporary"))
14075 161 : inform (EXPR_LOCATION (call), "the temporary was destroyed at "
14076 : "the end of the full expression %qE", call);
14077 161 : }
14078 2299 : }
14079 :
14080 : /* If *P is an xvalue expression, prevent temporary lifetime extension if it
14081 : gets used to initialize a reference. */
14082 :
14083 : static tree
14084 107 : prevent_lifetime_extension (tree t)
14085 : {
14086 107 : tree *p = &t;
14087 107 : while (TREE_CODE (*p) == COMPOUND_EXPR)
14088 0 : p = &TREE_OPERAND (*p, 1);
14089 113 : while (handled_component_p (*p))
14090 6 : p = &TREE_OPERAND (*p, 0);
14091 : /* Change a TARGET_EXPR from prvalue to xvalue. */
14092 107 : if (TREE_CODE (*p) == TARGET_EXPR)
14093 3 : *p = build2 (COMPOUND_EXPR, TREE_TYPE (*p), *p,
14094 3 : move (TARGET_EXPR_SLOT (*p)));
14095 107 : return t;
14096 : }
14097 :
14098 : /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
14099 : which is bound either to a reference or a std::initializer_list. */
14100 :
14101 : static tree
14102 273822 : extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups,
14103 : tree *cond_guard)
14104 : {
14105 : /* CWG1299 (C++20): The temporary object to which the reference is bound or
14106 : the temporary object that is the complete object of a subobject to which
14107 : the reference is bound persists for the lifetime of the reference if the
14108 : glvalue to which the reference is bound was obtained through one of the
14109 : following:
14110 : - a temporary materialization conversion ([conv.rval]),
14111 : - ( expression ), where expression is one of these expressions,
14112 : - subscripting ([expr.sub]) of an array operand, where that operand is one
14113 : of these expressions,
14114 : - a class member access ([expr.ref]) using the . operator where the left
14115 : operand is one of these expressions and the right operand designates a
14116 : non-static data member of non-reference type,
14117 : - a pointer-to-member operation ([expr.mptr.oper]) using the .* operator
14118 : where the left operand is one of these expressions and the right operand
14119 : is a pointer to data member of non-reference type,
14120 : - a const_cast ([expr.const.cast]), static_cast ([expr.static.cast]),
14121 : dynamic_cast ([expr.dynamic.cast]), or reinterpret_cast
14122 : ([expr.reinterpret.cast]) converting, without a user-defined conversion,
14123 : a glvalue operand that is one of these expressions to a glvalue that
14124 : refers to the object designated by the operand, or to its complete
14125 : object or a subobject thereof,
14126 : - a conditional expression ([expr.cond]) that is a glvalue where the
14127 : second or third operand is one of these expressions, or
14128 : - a comma expression ([expr.comma]) that is a glvalue where the right
14129 : operand is one of these expressions. */
14130 :
14131 : /* FIXME several cases are still handled wrong (101572, 81420). */
14132 :
14133 273822 : tree sub = init;
14134 273822 : tree *p;
14135 273822 : STRIP_NOPS (sub);
14136 273822 : if (TREE_CODE (sub) == COMPOUND_EXPR)
14137 : {
14138 256 : TREE_OPERAND (sub, 1)
14139 256 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups,
14140 : cond_guard);
14141 256 : return init;
14142 : }
14143 273566 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14144 273566 : && TYPE_PTRDATAMEM_P (TREE_TYPE (tree_strip_nop_conversions
14145 : (TREE_OPERAND (sub, 1)))))
14146 : {
14147 : /* A pointer-to-member operation. */
14148 6 : TREE_OPERAND (sub, 0)
14149 6 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 0), cleanups,
14150 : cond_guard);
14151 6 : return init;
14152 : }
14153 273560 : if (TREE_CODE (sub) == COND_EXPR)
14154 : {
14155 16469 : tree cur_cond_guard = NULL_TREE;
14156 16469 : if (TREE_OPERAND (sub, 1))
14157 16469 : TREE_OPERAND (sub, 1)
14158 32938 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups,
14159 : &cur_cond_guard);
14160 16469 : if (cur_cond_guard)
14161 : {
14162 9 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard,
14163 : NOP_EXPR, boolean_true_node,
14164 : tf_warning_or_error);
14165 9 : TREE_OPERAND (sub, 1)
14166 18 : = cp_build_compound_expr (set, TREE_OPERAND (sub, 1),
14167 : tf_warning_or_error);
14168 : }
14169 16469 : cur_cond_guard = NULL_TREE;
14170 16469 : if (TREE_OPERAND (sub, 2))
14171 16469 : TREE_OPERAND (sub, 2)
14172 32938 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 2), cleanups,
14173 : &cur_cond_guard);
14174 16469 : if (cur_cond_guard)
14175 : {
14176 12 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard,
14177 : NOP_EXPR, boolean_true_node,
14178 : tf_warning_or_error);
14179 12 : TREE_OPERAND (sub, 2)
14180 24 : = cp_build_compound_expr (set, TREE_OPERAND (sub, 2),
14181 : tf_warning_or_error);
14182 : }
14183 16469 : return init;
14184 : }
14185 257091 : if (TREE_CODE (sub) != ADDR_EXPR)
14186 : return init;
14187 : /* Deal with binding to a subobject. */
14188 24145 : for (p = &TREE_OPERAND (sub, 0);
14189 29800 : TREE_CODE (*p) == COMPONENT_REF || TREE_CODE (*p) == ARRAY_REF; )
14190 5655 : p = &TREE_OPERAND (*p, 0);
14191 24145 : if (TREE_CODE (*p) == TARGET_EXPR)
14192 : {
14193 4556 : tree subinit = NULL_TREE;
14194 4556 : *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit, cond_guard);
14195 4556 : recompute_tree_invariant_for_addr_expr (sub);
14196 4556 : if (init != sub)
14197 4538 : init = fold_convert (TREE_TYPE (init), sub);
14198 4556 : if (subinit)
14199 3847 : init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
14200 : }
14201 : return init;
14202 : }
14203 :
14204 : /* INIT is part of the initializer for DECL. If there are any
14205 : reference or initializer lists being initialized, extend their
14206 : lifetime to match that of DECL. */
14207 :
14208 : tree
14209 23719747 : extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups,
14210 : tree *cond_guard)
14211 : {
14212 23719747 : tree type = TREE_TYPE (init);
14213 23719747 : if (processing_template_decl)
14214 : return init;
14215 :
14216 22574372 : maybe_warn_dangling_reference (decl, init);
14217 :
14218 22574372 : if (TYPE_REF_P (type))
14219 240137 : init = extend_ref_init_temps_1 (decl, init, cleanups, cond_guard);
14220 : else
14221 : {
14222 22334235 : tree ctor = init;
14223 22334235 : if (TREE_CODE (ctor) == TARGET_EXPR)
14224 362061 : ctor = TARGET_EXPR_INITIAL (ctor);
14225 22334235 : if (TREE_CODE (ctor) == CONSTRUCTOR)
14226 : {
14227 : /* [dcl.init] When initializing an aggregate from a parenthesized list
14228 : of values... a temporary object bound to a reference does not have
14229 : its lifetime extended. */
14230 1491274 : if (CONSTRUCTOR_IS_PAREN_INIT (ctor))
14231 : return init;
14232 :
14233 1491175 : if (is_std_init_list (type))
14234 : {
14235 : /* The temporary array underlying a std::initializer_list
14236 : is handled like a reference temporary. */
14237 485 : tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
14238 485 : array = extend_ref_init_temps_1 (decl, array, cleanups,
14239 : cond_guard);
14240 485 : CONSTRUCTOR_ELT (ctor, 0)->value = array;
14241 : }
14242 : else
14243 : {
14244 1490690 : unsigned i;
14245 1490690 : constructor_elt *p;
14246 1490690 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
14247 5412605 : FOR_EACH_VEC_SAFE_ELT (elts, i, p)
14248 3921915 : p->value = extend_ref_init_temps (decl, p->value, cleanups,
14249 : cond_guard);
14250 : }
14251 1491175 : recompute_constructor_flags (ctor);
14252 1491175 : if (decl_maybe_constant_var_p (decl) && TREE_CONSTANT (ctor))
14253 312286 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
14254 : }
14255 : }
14256 :
14257 : return init;
14258 : }
14259 :
14260 : /* Returns true iff an initializer for TYPE could contain temporaries that
14261 : need to be extended because they are bound to references or
14262 : std::initializer_list. */
14263 :
14264 : bool
14265 4336 : type_has_extended_temps (tree type)
14266 : {
14267 4336 : type = strip_array_types (type);
14268 4336 : if (TYPE_REF_P (type))
14269 : return true;
14270 4327 : if (CLASS_TYPE_P (type))
14271 : {
14272 2267 : if (is_std_init_list (type))
14273 : return true;
14274 2264 : for (tree f = next_aggregate_field (TYPE_FIELDS (type));
14275 5616 : f; f = next_aggregate_field (DECL_CHAIN (f)))
14276 3361 : if (type_has_extended_temps (TREE_TYPE (f)))
14277 : return true;
14278 : }
14279 : return false;
14280 : }
14281 :
14282 : /* Returns true iff TYPE is some variant of std::initializer_list. */
14283 :
14284 : bool
14285 88330679 : is_std_init_list (tree type)
14286 : {
14287 88330679 : if (!TYPE_P (type))
14288 : return false;
14289 88330651 : if (cxx_dialect == cxx98)
14290 : return false;
14291 : /* Look through typedefs. */
14292 88049342 : type = TYPE_MAIN_VARIANT (type);
14293 68545786 : return (CLASS_TYPE_P (type)
14294 68444374 : && CP_TYPE_CONTEXT (type) == std_node
14295 133487611 : && init_list_identifier == DECL_NAME (TYPE_NAME (type)));
14296 : }
14297 :
14298 : /* Returns true iff DECL is a list constructor: i.e. a constructor which
14299 : will accept an argument list of a single std::initializer_list<T>. */
14300 :
14301 : bool
14302 77547486 : is_list_ctor (tree decl)
14303 : {
14304 77547486 : tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
14305 77547486 : tree arg;
14306 :
14307 77547486 : if (!args || args == void_list_node)
14308 : return false;
14309 :
14310 63144887 : arg = non_reference (TREE_VALUE (args));
14311 63144887 : if (!is_std_init_list (arg))
14312 : return false;
14313 :
14314 1350734 : args = TREE_CHAIN (args);
14315 :
14316 2316070 : if (args && args != void_list_node && !TREE_PURPOSE (args))
14317 : /* There are more non-defaulted parms. */
14318 : return false;
14319 :
14320 : return true;
14321 : }
14322 :
14323 : #include "gt-cp-call.h"
|