Branch data Line data Source code
1 : : /* Functions related to invoking -*- C++ -*- methods and overloaded functions.
2 : : Copyright (C) 1987-2024 Free Software Foundation, Inc.
3 : : Contributed by Michael Tiemann (tiemann@cygnus.com) and
4 : : modified by Brendan Kehoe (brendan@cygnus.com).
5 : :
6 : : This file is part of GCC.
7 : :
8 : : GCC is free software; you can redistribute it and/or modify
9 : : it under the terms of the GNU General Public License as published by
10 : : the Free Software Foundation; either version 3, or (at your option)
11 : : any later version.
12 : :
13 : : GCC is distributed in the hope that it will be useful,
14 : : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : : GNU General Public License for more details.
17 : :
18 : : You should have received a copy of the GNU General Public License
19 : : along with GCC; see the file COPYING3. If not see
20 : : <http://www.gnu.org/licenses/>. */
21 : :
22 : :
23 : : /* High-level class interface. */
24 : :
25 : : #include "config.h"
26 : : #include "system.h"
27 : : #include "coretypes.h"
28 : : #include "target.h"
29 : : #include "cp-tree.h"
30 : : #include "timevar.h"
31 : : #include "stringpool.h"
32 : : #include "cgraph.h"
33 : : #include "stor-layout.h"
34 : : #include "trans-mem.h"
35 : : #include "flags.h"
36 : : #include "toplev.h"
37 : : #include "intl.h"
38 : : #include "convert.h"
39 : : #include "langhooks.h"
40 : : #include "c-family/c-objc.h"
41 : : #include "internal-fn.h"
42 : : #include "stringpool.h"
43 : : #include "attribs.h"
44 : : #include "decl.h"
45 : : #include "c-family/c-type-mismatch.h"
46 : : #include "tristate.h"
47 : : #include "tree-pretty-print-markup.h"
48 : :
49 : : /* The various kinds of conversion. */
50 : :
51 : : enum conversion_kind {
52 : : ck_identity,
53 : : ck_lvalue,
54 : : ck_fnptr,
55 : : ck_qual,
56 : : ck_std,
57 : : ck_ptr,
58 : : ck_pmem,
59 : : ck_base,
60 : : ck_ref_bind,
61 : : ck_user,
62 : : ck_ambig,
63 : : ck_list,
64 : : ck_aggr,
65 : : ck_rvalue,
66 : : /* When LOOKUP_SHORTCUT_BAD_CONVS is set, we may return a conversion of
67 : : this kind whenever we know the true conversion is either bad or outright
68 : : invalid, but we don't want to attempt to compute the bad conversion (for
69 : : sake of avoiding unnecessary instantiation). bad_p should always be set
70 : : for these. */
71 : : ck_deferred_bad,
72 : : };
73 : :
74 : : /* The rank of the conversion. Order of the enumerals matters; better
75 : : conversions should come earlier in the list. */
76 : :
77 : : enum conversion_rank {
78 : : cr_identity,
79 : : cr_exact,
80 : : cr_promotion,
81 : : cr_std,
82 : : cr_pbool,
83 : : cr_user,
84 : : cr_ellipsis,
85 : : cr_bad
86 : : };
87 : :
88 : : /* An implicit conversion sequence, in the sense of [over.best.ics].
89 : : The first conversion to be performed is at the end of the chain.
90 : : That conversion is always a cr_identity conversion. */
91 : :
92 : : struct conversion {
93 : : /* The kind of conversion represented by this step. */
94 : : conversion_kind kind;
95 : : /* The rank of this conversion. */
96 : : conversion_rank rank;
97 : : BOOL_BITFIELD user_conv_p : 1;
98 : : BOOL_BITFIELD ellipsis_p : 1;
99 : : BOOL_BITFIELD this_p : 1;
100 : : /* True if this conversion would be permitted with a bending of
101 : : language standards, e.g. disregarding pointer qualifiers or
102 : : converting integers to pointers. */
103 : : BOOL_BITFIELD bad_p : 1;
104 : : /* If KIND is ck_ref_bind or ck_base, true to indicate that a
105 : : temporary should be created to hold the result of the
106 : : conversion. If KIND is ck_ambig or ck_user, true means force
107 : : copy-initialization. */
108 : : BOOL_BITFIELD need_temporary_p : 1;
109 : : /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion
110 : : from a pointer-to-derived to pointer-to-base is being performed. */
111 : : BOOL_BITFIELD base_p : 1;
112 : : /* If KIND is ck_ref_bind, true when either an lvalue reference is
113 : : being bound to an lvalue expression or an rvalue reference is
114 : : being bound to an rvalue expression. If KIND is ck_rvalue or ck_base,
115 : : true when we are treating an lvalue as an rvalue (12.8p33). If
116 : : ck_identity, we will be binding a reference directly or decaying to
117 : : a pointer. */
118 : : BOOL_BITFIELD rvaluedness_matches_p: 1;
119 : : BOOL_BITFIELD check_narrowing: 1;
120 : : /* Whether check_narrowing should only check TREE_CONSTANTs; used
121 : : in build_converted_constant_expr. */
122 : : BOOL_BITFIELD check_narrowing_const_only: 1;
123 : : /* True if this conversion is taking place in a copy-initialization context
124 : : and we should only consider converting constructors. Only set in
125 : : ck_base and ck_rvalue. */
126 : : BOOL_BITFIELD copy_init_p : 1;
127 : : /* The type of the expression resulting from the conversion. */
128 : : tree type;
129 : : union {
130 : : /* The next conversion in the chain. Since the conversions are
131 : : arranged from outermost to innermost, the NEXT conversion will
132 : : actually be performed before this conversion. This variant is
133 : : used only when KIND is neither ck_identity, ck_aggr, ck_ambig nor
134 : : ck_list. Please use the next_conversion function instead
135 : : of using this field directly. */
136 : : conversion *next;
137 : : /* The expression at the beginning of the conversion chain. This
138 : : variant is used only if KIND is ck_identity, ck_aggr, or ck_ambig.
139 : : You can use conv_get_original_expr to get this expression. */
140 : : tree expr;
141 : : /* The array of conversions for an initializer_list, so this
142 : : variant is used only when KIN D is ck_list. */
143 : : conversion **list;
144 : : } u;
145 : : /* The function candidate corresponding to this conversion
146 : : sequence. This field is only used if KIND is ck_user. */
147 : : struct z_candidate *cand;
148 : : };
149 : :
150 : : #define CONVERSION_RANK(NODE) \
151 : : ((NODE)->bad_p ? cr_bad \
152 : : : (NODE)->ellipsis_p ? cr_ellipsis \
153 : : : (NODE)->user_conv_p ? cr_user \
154 : : : (NODE)->rank)
155 : :
156 : : #define BAD_CONVERSION_RANK(NODE) \
157 : : ((NODE)->ellipsis_p ? cr_ellipsis \
158 : : : (NODE)->user_conv_p ? cr_user \
159 : : : (NODE)->rank)
160 : :
161 : : static struct obstack conversion_obstack;
162 : : static bool conversion_obstack_initialized;
163 : : struct rejection_reason;
164 : :
165 : : static struct z_candidate * tourney (struct z_candidate *, tsubst_flags_t);
166 : : static int equal_functions (tree, tree);
167 : : static int joust (struct z_candidate *, struct z_candidate *, bool,
168 : : tsubst_flags_t);
169 : : static int compare_ics (conversion *, conversion *);
170 : : static void maybe_warn_class_memaccess (location_t, tree,
171 : : const vec<tree, va_gc> *);
172 : : static tree build_over_call (struct z_candidate *, int, tsubst_flags_t);
173 : : static tree convert_like (conversion *, tree, tsubst_flags_t);
174 : : static tree convert_like_with_context (conversion *, tree, tree, int,
175 : : tsubst_flags_t);
176 : : static void op_error (const op_location_t &, enum tree_code, enum tree_code,
177 : : tree, tree, tree, bool);
178 : : static struct z_candidate *build_user_type_conversion_1 (tree, tree, int,
179 : : tsubst_flags_t);
180 : : static void print_z_candidate (location_t, const char *, struct z_candidate *);
181 : : static void print_z_candidates (location_t, struct z_candidate *,
182 : : tristate = tristate::unknown ());
183 : : static tree build_this (tree);
184 : : static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
185 : : static bool any_strictly_viable (struct z_candidate *);
186 : : static struct z_candidate *add_template_candidate
187 : : (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
188 : : tree, tree, tree, int, unification_kind_t, bool, tsubst_flags_t);
189 : : static struct z_candidate *add_template_candidate_real
190 : : (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
191 : : tree, tree, tree, int, tree, unification_kind_t, bool, tsubst_flags_t);
192 : : static bool is_complete (tree);
193 : : static struct z_candidate *add_conv_candidate
194 : : (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, tree,
195 : : tree, tsubst_flags_t);
196 : : static struct z_candidate *add_function_candidate
197 : : (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *, tree,
198 : : tree, int, conversion**, bool, tsubst_flags_t);
199 : : static conversion *implicit_conversion (tree, tree, tree, bool, int,
200 : : tsubst_flags_t);
201 : : static conversion *reference_binding (tree, tree, tree, bool, int,
202 : : tsubst_flags_t);
203 : : static conversion *build_conv (conversion_kind, tree, conversion *);
204 : : static conversion *build_list_conv (tree, tree, int, tsubst_flags_t);
205 : : static conversion *next_conversion (conversion *);
206 : : static bool is_subseq (conversion *, conversion *);
207 : : static conversion *maybe_handle_ref_bind (conversion **);
208 : : static void maybe_handle_implicit_object (conversion **);
209 : : static struct z_candidate *add_candidate
210 : : (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, size_t,
211 : : conversion **, tree, tree, int, struct rejection_reason *, int);
212 : : static tree source_type (conversion *);
213 : : static void add_warning (struct z_candidate *, struct z_candidate *);
214 : : static conversion *direct_reference_binding (tree, conversion *);
215 : : static bool promoted_arithmetic_type_p (tree);
216 : : static conversion *conditional_conversion (tree, tree, tsubst_flags_t);
217 : : static char *name_as_c_string (tree, tree, bool *);
218 : : static tree prep_operand (tree);
219 : : static void add_candidates (tree, tree, const vec<tree, va_gc> *, tree, tree,
220 : : bool, tree, tree, int, struct z_candidate **,
221 : : tsubst_flags_t);
222 : : static conversion *merge_conversion_sequences (conversion *, conversion *);
223 : : static tree build_temp (tree, tree, int, diagnostic_t *, tsubst_flags_t);
224 : : static conversion *build_identity_conv (tree, tree);
225 : : static inline bool conv_binds_to_array_of_unknown_bound (conversion *);
226 : : static bool conv_is_prvalue (conversion *);
227 : : static tree prevent_lifetime_extension (tree);
228 : :
229 : : /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
230 : : NAME can take many forms... */
231 : :
232 : : bool
233 : 2813373 : check_dtor_name (tree basetype, tree name)
234 : : {
235 : : /* Just accept something we've already complained about. */
236 : 2813373 : if (name == error_mark_node)
237 : : return true;
238 : :
239 : 2813373 : if (TREE_CODE (name) == TYPE_DECL)
240 : 84 : name = TREE_TYPE (name);
241 : 2813289 : else if (TYPE_P (name))
242 : : /* OK */;
243 : 26 : else if (identifier_p (name))
244 : : {
245 : 26 : if ((MAYBE_CLASS_TYPE_P (basetype)
246 : 0 : || TREE_CODE (basetype) == ENUMERAL_TYPE)
247 : 26 : && name == constructor_name (basetype))
248 : : return true;
249 : :
250 : : /* Otherwise lookup the name, it could be an unrelated typedef
251 : : of the correct type. */
252 : 6 : name = lookup_name (name, LOOK_want::TYPE);
253 : 6 : if (!name)
254 : : return false;
255 : 3 : name = TREE_TYPE (name);
256 : 3 : if (name == error_mark_node)
257 : : return false;
258 : : }
259 : : else
260 : : {
261 : : /* In the case of:
262 : :
263 : : template <class T> struct S { ~S(); };
264 : : int i;
265 : : i.~S();
266 : :
267 : : NAME will be a class template. */
268 : 0 : gcc_assert (DECL_CLASS_TEMPLATE_P (name));
269 : : return false;
270 : : }
271 : :
272 : 2813347 : return same_type_p (TYPE_MAIN_VARIANT (basetype), TYPE_MAIN_VARIANT (name));
273 : : }
274 : :
275 : : /* We want the address of a function or method. We avoid creating a
276 : : pointer-to-member function. */
277 : :
278 : : tree
279 : 226620478 : build_addr_func (tree function, tsubst_flags_t complain)
280 : : {
281 : 226620478 : tree type = TREE_TYPE (function);
282 : :
283 : : /* We have to do these by hand to avoid real pointer to member
284 : : functions. */
285 : 226620478 : if (TREE_CODE (type) == METHOD_TYPE)
286 : : {
287 : 37675711 : if (TREE_CODE (function) == OFFSET_REF)
288 : : {
289 : 88 : tree object = build_address (TREE_OPERAND (function, 0));
290 : 88 : return get_member_function_from_ptrfunc (&object,
291 : 88 : TREE_OPERAND (function, 1),
292 : : complain);
293 : : }
294 : 37675623 : function = build_address (function);
295 : : }
296 : 188944767 : else if (TREE_CODE (function) == FUNCTION_DECL
297 : 272333018 : && DECL_IMMEDIATE_FUNCTION_P (function))
298 : 230683 : function = build_address (function);
299 : : else
300 : 188714084 : function = decay_conversion (function, complain, /*reject_builtin=*/false);
301 : :
302 : : return function;
303 : : }
304 : :
305 : : /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
306 : : POINTER_TYPE to those. Note, pointer to member function types
307 : : (TYPE_PTRMEMFUNC_P) must be handled by our callers. There are
308 : : two variants. build_call_a is the primitive taking an array of
309 : : arguments, while build_call_n is a wrapper that handles varargs. */
310 : :
311 : : tree
312 : 148759 : build_call_n (tree function, int n, ...)
313 : : {
314 : 148759 : if (n == 0)
315 : 0 : return build_call_a (function, 0, NULL);
316 : : else
317 : : {
318 : 148759 : tree *argarray = XALLOCAVEC (tree, n);
319 : 148759 : va_list ap;
320 : 148759 : int i;
321 : :
322 : 148759 : va_start (ap, n);
323 : 297518 : for (i = 0; i < n; i++)
324 : 148759 : argarray[i] = va_arg (ap, tree);
325 : 148759 : va_end (ap);
326 : 148759 : return build_call_a (function, n, argarray);
327 : : }
328 : : }
329 : :
330 : : /* Update various flags in cfun and the call itself based on what is being
331 : : called. Split out of build_call_a so that bot_manip can use it too. */
332 : :
333 : : void
334 : 106391715 : set_flags_from_callee (tree call)
335 : : {
336 : : /* Handle both CALL_EXPRs and AGGR_INIT_EXPRs. */
337 : 106391715 : tree decl = cp_get_callee_fndecl_nofold (call);
338 : :
339 : : /* We check both the decl and the type; a function may be known not to
340 : : throw without being declared throw(). */
341 : 106391715 : bool nothrow = decl && TREE_NOTHROW (decl);
342 : 106391715 : tree callee = cp_get_callee (call);
343 : 106391715 : if (callee)
344 : 106391707 : nothrow |= TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (callee)));
345 : 8 : else if (TREE_CODE (call) == CALL_EXPR
346 : 8 : && internal_fn_flags (CALL_EXPR_IFN (call)) & ECF_NOTHROW)
347 : : nothrow = true;
348 : :
349 : 106391715 : if (cfun && cp_function_chain && !cp_unevaluated_operand)
350 : : {
351 : 74222982 : if (!nothrow && at_function_scope_p ())
352 : 19087805 : cp_function_chain->can_throw = 1;
353 : :
354 : 74222982 : if (decl && TREE_THIS_VOLATILE (decl))
355 : 2483353 : current_function_returns_abnormally = 1;
356 : : }
357 : :
358 : 106391715 : TREE_NOTHROW (call) = nothrow;
359 : 106391715 : }
360 : :
361 : : tree
362 : 104766841 : build_call_a (tree function, int n, tree *argarray)
363 : : {
364 : 104766841 : tree decl;
365 : 104766841 : tree result_type;
366 : 104766841 : tree fntype;
367 : 104766841 : int i;
368 : :
369 : 104766841 : function = build_addr_func (function, tf_warning_or_error);
370 : :
371 : 104766841 : gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
372 : 104766841 : fntype = TREE_TYPE (TREE_TYPE (function));
373 : 104766841 : gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype));
374 : 104766841 : result_type = TREE_TYPE (fntype);
375 : : /* An rvalue has no cv-qualifiers. */
376 : 104766841 : if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
377 : 71350183 : result_type = cv_unqualified (result_type);
378 : :
379 : 104766841 : function = build_call_array_loc (input_location,
380 : : result_type, function, n, argarray);
381 : 104766841 : set_flags_from_callee (function);
382 : :
383 : 104766841 : decl = get_callee_fndecl (function);
384 : :
385 : 104766841 : if (decl && !TREE_USED (decl))
386 : : {
387 : : /* We invoke build_call directly for several library
388 : : functions. These may have been declared normally if
389 : : we're building libgcc, so we can't just check
390 : : DECL_ARTIFICIAL. */
391 : 117853 : gcc_assert (DECL_ARTIFICIAL (decl)
392 : : || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
393 : : "__", 2));
394 : 117853 : mark_used (decl);
395 : : }
396 : :
397 : 104766841 : require_complete_eh_spec_types (fntype, decl);
398 : :
399 : 304017707 : TREE_HAS_CONSTRUCTOR (function) = (decl && DECL_CONSTRUCTOR_P (decl));
400 : :
401 : : /* Don't pass empty class objects by value. This is useful
402 : : for tags in STL, which are used to control overload resolution.
403 : : We don't need to handle other cases of copying empty classes. */
404 : 104766841 : if (!decl || !fndecl_built_in_p (decl))
405 : 216202277 : for (i = 0; i < n; i++)
406 : : {
407 : 118140513 : tree arg = CALL_EXPR_ARG (function, i);
408 : 118140513 : if (is_empty_class (TREE_TYPE (arg))
409 : 118140513 : && simple_empty_class_p (TREE_TYPE (arg), arg, INIT_EXPR))
410 : : {
411 : 4757742 : while (TREE_CODE (arg) == TARGET_EXPR)
412 : : /* We're disconnecting the initializer from its target,
413 : : don't create a temporary. */
414 : 2378120 : arg = TARGET_EXPR_INITIAL (arg);
415 : 2379622 : tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg));
416 : 2379622 : arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t);
417 : 2379622 : CALL_EXPR_ARG (function, i) = arg;
418 : : }
419 : : }
420 : :
421 : 104766841 : return function;
422 : : }
423 : :
424 : : /* New overloading code. */
425 : :
426 : : struct z_candidate;
427 : :
428 : : struct candidate_warning {
429 : : z_candidate *loser;
430 : : candidate_warning *next;
431 : : };
432 : :
433 : : /* Information for providing diagnostics about why overloading failed. */
434 : :
435 : : enum rejection_reason_code {
436 : : rr_none,
437 : : rr_arity,
438 : : rr_explicit_conversion,
439 : : rr_template_conversion,
440 : : rr_arg_conversion,
441 : : rr_bad_arg_conversion,
442 : : rr_template_unification,
443 : : rr_invalid_copy,
444 : : rr_inherited_ctor,
445 : : rr_constraint_failure,
446 : : rr_ignored,
447 : : };
448 : :
449 : : struct conversion_info {
450 : : /* The index of the argument, 0-based. */
451 : : int n_arg;
452 : : /* The actual argument or its type. */
453 : : tree from;
454 : : /* The type of the parameter. */
455 : : tree to_type;
456 : : /* The location of the argument. */
457 : : location_t loc;
458 : : };
459 : :
460 : : struct rejection_reason {
461 : : enum rejection_reason_code code;
462 : : union {
463 : : /* Information about an arity mismatch. */
464 : : struct {
465 : : /* The expected number of arguments. */
466 : : int expected;
467 : : /* The actual number of arguments in the call. */
468 : : int actual;
469 : : /* Whether EXPECTED should be treated as a lower bound. */
470 : : bool least_p;
471 : : } arity;
472 : : /* Information about an argument conversion mismatch. */
473 : : struct conversion_info conversion;
474 : : /* Same, but for bad argument conversions. */
475 : : struct conversion_info bad_conversion;
476 : : /* Information about template unification failures. These are the
477 : : parameters passed to fn_type_unification. */
478 : : struct {
479 : : tree tmpl;
480 : : tree explicit_targs;
481 : : int num_targs;
482 : : const tree *args;
483 : : unsigned int nargs;
484 : : tree return_type;
485 : : unification_kind_t strict;
486 : : int flags;
487 : : } template_unification;
488 : : /* Information about template instantiation failures. These are the
489 : : parameters passed to instantiate_template. */
490 : : struct {
491 : : tree tmpl;
492 : : tree targs;
493 : : } template_instantiation;
494 : : } u;
495 : : };
496 : :
497 : : struct z_candidate {
498 : : /* The FUNCTION_DECL that will be called if this candidate is
499 : : selected by overload resolution. */
500 : : tree fn;
501 : : /* If not NULL_TREE, the first argument to use when calling this
502 : : function. */
503 : : tree first_arg;
504 : : /* The rest of the arguments to use when calling this function. If
505 : : there are no further arguments this may be NULL or it may be an
506 : : empty vector. */
507 : : const vec<tree, va_gc> *args;
508 : : /* The implicit conversion sequences for each of the arguments to
509 : : FN. */
510 : : conversion **convs;
511 : : /* The number of implicit conversion sequences. */
512 : : size_t num_convs;
513 : : /* If FN is a user-defined conversion, the standard conversion
514 : : sequence from the type returned by FN to the desired destination
515 : : type. */
516 : : conversion *second_conv;
517 : : struct rejection_reason *reason;
518 : : /* If FN is a member function, the binfo indicating the path used to
519 : : qualify the name of FN at the call site. This path is used to
520 : : determine whether or not FN is accessible if it is selected by
521 : : overload resolution. The DECL_CONTEXT of FN will always be a
522 : : (possibly improper) base of this binfo. */
523 : : tree access_path;
524 : : /* If FN is a non-static member function, the binfo indicating the
525 : : subobject to which the `this' pointer should be converted if FN
526 : : is selected by overload resolution. The type pointed to by
527 : : the `this' pointer must correspond to the most derived class
528 : : indicated by the CONVERSION_PATH. */
529 : : tree conversion_path;
530 : : tree template_decl;
531 : : tree explicit_targs;
532 : : candidate_warning *warnings;
533 : : z_candidate *next;
534 : : int viable;
535 : :
536 : : /* The flags active in add_candidate. */
537 : : int flags;
538 : :
539 : 30399124 : bool rewritten () const { return (flags & LOOKUP_REWRITTEN); }
540 : 684141357 : bool reversed () const { return (flags & LOOKUP_REVERSED); }
541 : : };
542 : :
543 : : /* Returns true iff T is a null pointer constant in the sense of
544 : : [conv.ptr]. */
545 : :
546 : : bool
547 : 87903300 : null_ptr_cst_p (tree t)
548 : : {
549 : 87903300 : tree type = TREE_TYPE (t);
550 : :
551 : : /* [conv.ptr]
552 : :
553 : : A null pointer constant is an integer literal ([lex.icon]) with value
554 : : zero or a prvalue of type std::nullptr_t. */
555 : 87903300 : if (NULLPTR_TYPE_P (type))
556 : : return true;
557 : :
558 : 81379667 : if (cxx_dialect >= cxx11)
559 : : {
560 : 81054625 : STRIP_ANY_LOCATION_WRAPPER (t);
561 : :
562 : : /* Core issue 903 says only literal 0 is a null pointer constant. */
563 : 81054625 : if (TREE_CODE (t) == INTEGER_CST
564 : 9287176 : && !TREE_OVERFLOW (t)
565 : 9287176 : && TREE_CODE (type) == INTEGER_TYPE
566 : 8713719 : && integer_zerop (t)
567 : 86580553 : && !char_type_p (type))
568 : : return true;
569 : : }
570 : 325042 : else if (CP_INTEGRAL_TYPE_P (type))
571 : : {
572 : 62076 : t = fold_non_dependent_expr (t, tf_none);
573 : 62076 : STRIP_NOPS (t);
574 : 62076 : if (integer_zerop (t) && !TREE_OVERFLOW (t))
575 : : return true;
576 : : }
577 : :
578 : : return false;
579 : : }
580 : :
581 : : /* Returns true iff T is a null member pointer value (4.11). */
582 : :
583 : : bool
584 : 49221311 : null_member_pointer_value_p (tree t)
585 : : {
586 : 49221311 : tree type = TREE_TYPE (t);
587 : 49221311 : if (!type)
588 : : return false;
589 : 49022619 : else if (TYPE_PTRMEMFUNC_P (type))
590 : 431 : return (TREE_CODE (t) == CONSTRUCTOR
591 : 227 : && CONSTRUCTOR_NELTS (t)
592 : 655 : && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value));
593 : 49022188 : else if (TYPE_PTRDATAMEM_P (type))
594 : 5561 : return integer_all_onesp (t);
595 : : else
596 : : return false;
597 : : }
598 : :
599 : : /* Returns nonzero if PARMLIST consists of only default parms,
600 : : ellipsis, and/or undeduced parameter packs. */
601 : :
602 : : bool
603 : 496985199 : sufficient_parms_p (const_tree parmlist)
604 : : {
605 : 505133809 : for (; parmlist && parmlist != void_list_node;
606 : 8148610 : parmlist = TREE_CHAIN (parmlist))
607 : 166354785 : if (!TREE_PURPOSE (parmlist)
608 : 166354785 : && !PACK_EXPANSION_P (TREE_VALUE (parmlist)))
609 : : return false;
610 : : return true;
611 : : }
612 : :
613 : : /* Allocate N bytes of memory from the conversion obstack. The memory
614 : : is zeroed before being returned. */
615 : :
616 : : static void *
617 : 5128558617 : conversion_obstack_alloc (size_t n)
618 : : {
619 : 5128558617 : void *p;
620 : 5128558617 : if (!conversion_obstack_initialized)
621 : : {
622 : 79430 : gcc_obstack_init (&conversion_obstack);
623 : 79430 : conversion_obstack_initialized = true;
624 : : }
625 : 5128558617 : p = obstack_alloc (&conversion_obstack, n);
626 : 5128558617 : memset (p, 0, n);
627 : 5128558617 : return p;
628 : : }
629 : :
630 : : /* RAII class to discard anything added to conversion_obstack. */
631 : :
632 : : struct conversion_obstack_sentinel
633 : : {
634 : : void *p;
635 : 1746052420 : conversion_obstack_sentinel (): p (conversion_obstack_alloc (0)) {}
636 : 873012656 : ~conversion_obstack_sentinel () { obstack_free (&conversion_obstack, p); }
637 : : };
638 : :
639 : : /* Allocate rejection reasons. */
640 : :
641 : : static struct rejection_reason *
642 : 491746081 : alloc_rejection (enum rejection_reason_code code)
643 : : {
644 : 491746081 : struct rejection_reason *p;
645 : 0 : p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
646 : 491746081 : p->code = code;
647 : 491746081 : return p;
648 : : }
649 : :
650 : : static struct rejection_reason *
651 : 137010862 : arity_rejection (tree first_arg, int expected, int actual, bool least_p = false)
652 : : {
653 : 107874759 : struct rejection_reason *r = alloc_rejection (rr_arity);
654 : 137010862 : int adjust = first_arg != NULL_TREE;
655 : 137010862 : r->u.arity.expected = expected - adjust;
656 : 137010862 : r->u.arity.actual = actual - adjust;
657 : 137010862 : r->u.arity.least_p = least_p;
658 : 137010862 : return r;
659 : : }
660 : :
661 : : static struct rejection_reason *
662 : 95050857 : arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to,
663 : : location_t loc)
664 : : {
665 : 3634213 : struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
666 : 95050857 : int adjust = first_arg != NULL_TREE;
667 : 95050857 : r->u.conversion.n_arg = n_arg - adjust;
668 : 95050857 : r->u.conversion.from = from;
669 : 95050857 : r->u.conversion.to_type = to;
670 : 95050857 : r->u.conversion.loc = loc;
671 : 95050857 : return r;
672 : : }
673 : :
674 : : static struct rejection_reason *
675 : 14858770 : bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to,
676 : : location_t loc)
677 : : {
678 : 1221 : struct rejection_reason *r = alloc_rejection (rr_bad_arg_conversion);
679 : 14858770 : int adjust = first_arg != NULL_TREE;
680 : 14858770 : r->u.bad_conversion.n_arg = n_arg - adjust;
681 : 14858770 : r->u.bad_conversion.from = from;
682 : 14858770 : r->u.bad_conversion.to_type = to;
683 : 14858770 : r->u.bad_conversion.loc = loc;
684 : 14858770 : return r;
685 : : }
686 : :
687 : : static struct rejection_reason *
688 : 1998 : explicit_conversion_rejection (tree from, tree to)
689 : : {
690 : 1998 : struct rejection_reason *r = alloc_rejection (rr_explicit_conversion);
691 : 1998 : r->u.conversion.n_arg = 0;
692 : 1998 : r->u.conversion.from = from;
693 : 1998 : r->u.conversion.to_type = to;
694 : 1998 : r->u.conversion.loc = UNKNOWN_LOCATION;
695 : 1998 : return r;
696 : : }
697 : :
698 : : static struct rejection_reason *
699 : 21 : template_conversion_rejection (tree from, tree to)
700 : : {
701 : 21 : struct rejection_reason *r = alloc_rejection (rr_template_conversion);
702 : 21 : r->u.conversion.n_arg = 0;
703 : 21 : r->u.conversion.from = from;
704 : 21 : r->u.conversion.to_type = to;
705 : 21 : r->u.conversion.loc = UNKNOWN_LOCATION;
706 : 21 : return r;
707 : : }
708 : :
709 : : static struct rejection_reason *
710 : 243995087 : template_unification_rejection (tree tmpl, tree explicit_targs, tree targs,
711 : : const tree *args, unsigned int nargs,
712 : : tree return_type, unification_kind_t strict,
713 : : int flags)
714 : : {
715 : 243995087 : size_t args_n_bytes = sizeof (*args) * nargs;
716 : 243995087 : tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes);
717 : 243995087 : struct rejection_reason *r = alloc_rejection (rr_template_unification);
718 : 243995087 : r->u.template_unification.tmpl = tmpl;
719 : 243995087 : r->u.template_unification.explicit_targs = explicit_targs;
720 : 243995087 : r->u.template_unification.num_targs = TREE_VEC_LENGTH (targs);
721 : : /* Copy args to our own storage. */
722 : 243995087 : memcpy (args1, args, args_n_bytes);
723 : 243995087 : r->u.template_unification.args = args1;
724 : 243995087 : r->u.template_unification.nargs = nargs;
725 : 243995087 : r->u.template_unification.return_type = return_type;
726 : 243995087 : r->u.template_unification.strict = strict;
727 : 243995087 : r->u.template_unification.flags = flags;
728 : 243995087 : return r;
729 : : }
730 : :
731 : : static struct rejection_reason *
732 : 112 : template_unification_error_rejection (void)
733 : : {
734 : 0 : return alloc_rejection (rr_template_unification);
735 : : }
736 : :
737 : : static struct rejection_reason *
738 : 400752 : invalid_copy_with_fn_template_rejection (void)
739 : : {
740 : 0 : struct rejection_reason *r = alloc_rejection (rr_invalid_copy);
741 : 400752 : return r;
742 : : }
743 : :
744 : : static struct rejection_reason *
745 : 373418 : inherited_ctor_rejection (void)
746 : : {
747 : 0 : struct rejection_reason *r = alloc_rejection (rr_inherited_ctor);
748 : 373418 : return r;
749 : : }
750 : :
751 : : /* Build a constraint failure record. */
752 : :
753 : : static struct rejection_reason *
754 : 54204 : constraint_failure (void)
755 : : {
756 : 0 : struct rejection_reason *r = alloc_rejection (rr_constraint_failure);
757 : 54204 : return r;
758 : : }
759 : :
760 : : /* Dynamically allocate a conversion. */
761 : :
762 : : static conversion *
763 : 1761955554 : alloc_conversion (conversion_kind kind)
764 : : {
765 : 1761955554 : conversion *c;
766 : 0 : c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
767 : 1761955554 : c->kind = kind;
768 : 1761955554 : return c;
769 : : }
770 : :
771 : : /* Make sure that all memory on the conversion obstack has been
772 : : freed. */
773 : :
774 : : void
775 : 91117 : validate_conversion_obstack (void)
776 : : {
777 : 91117 : if (conversion_obstack_initialized)
778 : 79237 : gcc_assert ((obstack_next_free (&conversion_obstack)
779 : : == obstack_base (&conversion_obstack)));
780 : 91117 : }
781 : :
782 : : /* Dynamically allocate an array of N conversions. */
783 : :
784 : : static conversion **
785 : 669763854 : alloc_conversions (size_t n)
786 : : {
787 : 0 : return (conversion **) conversion_obstack_alloc (n * sizeof (conversion *));
788 : : }
789 : :
790 : : /* True iff the active member of conversion::u for code CODE is NEXT. */
791 : :
792 : : static inline bool
793 : 997210183 : has_next (conversion_kind code)
794 : : {
795 : 927728936 : return !(code == ck_identity
796 : 997210183 : || code == ck_ambig
797 : : || code == ck_list
798 : 927778845 : || code == ck_aggr
799 : 997210183 : || code == ck_deferred_bad);
800 : : }
801 : :
802 : : static conversion *
803 : 508868055 : build_conv (conversion_kind code, tree type, conversion *from)
804 : : {
805 : 508868055 : conversion *t;
806 : 508868055 : conversion_rank rank = CONVERSION_RANK (from);
807 : :
808 : : /* Only call this function for conversions that use u.next. */
809 : 508868055 : gcc_assert (from == NULL || has_next (code));
810 : :
811 : : /* Note that the caller is responsible for filling in t->cand for
812 : : user-defined conversions. */
813 : 508868055 : t = alloc_conversion (code);
814 : 508868055 : t->type = type;
815 : 508868055 : t->u.next = from;
816 : :
817 : 508868055 : switch (code)
818 : : {
819 : 136347953 : case ck_ptr:
820 : 136347953 : case ck_pmem:
821 : 136347953 : case ck_base:
822 : 136347953 : case ck_std:
823 : 136347953 : if (rank < cr_std)
824 : 508868055 : rank = cr_std;
825 : : break;
826 : :
827 : 32482274 : case ck_qual:
828 : 32482274 : case ck_fnptr:
829 : 32482274 : if (rank < cr_exact)
830 : 508868055 : rank = cr_exact;
831 : : break;
832 : :
833 : : default:
834 : : break;
835 : : }
836 : 508868055 : t->rank = rank;
837 : 508868055 : t->user_conv_p = (code == ck_user || from->user_conv_p);
838 : 508868055 : t->bad_p = from->bad_p;
839 : 508868055 : t->base_p = false;
840 : 508868055 : return t;
841 : : }
842 : :
843 : : /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
844 : : specialization of std::initializer_list<T>, if such a conversion is
845 : : possible. */
846 : :
847 : : static conversion *
848 : 60774 : build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
849 : : {
850 : 60774 : tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
851 : 60774 : unsigned len = CONSTRUCTOR_NELTS (ctor);
852 : 60774 : conversion **subconvs = alloc_conversions (len);
853 : 60774 : conversion *t;
854 : 60774 : unsigned i;
855 : 60774 : tree val;
856 : :
857 : : /* Within a list-initialization we can have more user-defined
858 : : conversions. */
859 : 60774 : flags &= ~LOOKUP_NO_CONVERSION;
860 : : /* But no narrowing conversions. */
861 : 60774 : flags |= LOOKUP_NO_NARROWING;
862 : :
863 : : /* Can't make an array of these types. */
864 : 60774 : if (TYPE_REF_P (elttype)
865 : 60768 : || TREE_CODE (elttype) == FUNCTION_TYPE
866 : 60768 : || VOID_TYPE_P (elttype))
867 : : return NULL;
868 : :
869 : 185567 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
870 : : {
871 : 134855 : conversion *sub
872 : 134855 : = implicit_conversion (elttype, TREE_TYPE (val), val,
873 : : false, flags, complain);
874 : 134855 : if (sub == NULL)
875 : : return NULL;
876 : :
877 : 124799 : subconvs[i] = sub;
878 : : }
879 : :
880 : 50712 : t = alloc_conversion (ck_list);
881 : 50712 : t->type = type;
882 : 50712 : t->u.list = subconvs;
883 : 50712 : t->rank = cr_exact;
884 : :
885 : 156787 : for (i = 0; i < len; ++i)
886 : : {
887 : 106075 : conversion *sub = subconvs[i];
888 : 106075 : if (sub->rank > t->rank)
889 : 46913 : t->rank = sub->rank;
890 : 106075 : if (sub->user_conv_p)
891 : 3110 : t->user_conv_p = true;
892 : 106075 : if (sub->bad_p)
893 : 46894 : t->bad_p = true;
894 : : }
895 : :
896 : : return t;
897 : : }
898 : :
899 : : /* Return the next conversion of the conversion chain (if applicable),
900 : : or NULL otherwise. Please use this function instead of directly
901 : : accessing fields of struct conversion. */
902 : :
903 : : static conversion *
904 : 413863507 : next_conversion (conversion *conv)
905 : : {
906 : 413863507 : if (conv == NULL
907 : 413863507 : || !has_next (conv->kind))
908 : : return NULL;
909 : 407123677 : return conv->u.next;
910 : : }
911 : :
912 : : /* Strip to the first ck_user, ck_ambig, ck_list, ck_aggr or ck_identity
913 : : encountered. */
914 : :
915 : : static conversion *
916 : 728323 : strip_standard_conversion (conversion *conv)
917 : : {
918 : 728323 : while (conv
919 : 730598 : && conv->kind != ck_user
920 : 743481 : && has_next (conv->kind))
921 : 2275 : conv = next_conversion (conv);
922 : 728323 : return conv;
923 : : }
924 : :
925 : : /* Subroutine of build_aggr_conv: check whether FROM is a valid aggregate
926 : : initializer for array type ATYPE. */
927 : :
928 : : static bool
929 : 9830 : can_convert_array (tree atype, tree from, int flags, tsubst_flags_t complain)
930 : : {
931 : 9830 : tree elttype = TREE_TYPE (atype);
932 : 9830 : unsigned i;
933 : :
934 : 9830 : if (TREE_CODE (from) == CONSTRUCTOR)
935 : : {
936 : 11018 : for (i = 0; i < CONSTRUCTOR_NELTS (from); ++i)
937 : : {
938 : 1233 : tree val = CONSTRUCTOR_ELT (from, i)->value;
939 : 1233 : bool ok;
940 : 1233 : if (TREE_CODE (elttype) == ARRAY_TYPE)
941 : 18 : ok = can_convert_array (elttype, val, flags, complain);
942 : : else
943 : 1215 : ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags,
944 : : complain);
945 : 1233 : if (!ok)
946 : : return false;
947 : : }
948 : : return true;
949 : : }
950 : :
951 : 45 : if (char_type_p (TYPE_MAIN_VARIANT (elttype))
952 : 45 : && TREE_CODE (tree_strip_any_location_wrapper (from)) == STRING_CST)
953 : 45 : return array_string_literal_compatible_p (atype, from);
954 : :
955 : : /* No other valid way to aggregate initialize an array. */
956 : : return false;
957 : : }
958 : :
959 : : /* Helper for build_aggr_conv. Return true if FIELD is in PSET, or if
960 : : FIELD has ANON_AGGR_TYPE_P and any initializable field in there recursively
961 : : is in PSET. */
962 : :
963 : : static bool
964 : 646860 : field_in_pset (hash_set<tree, true> &pset, tree field)
965 : : {
966 : 646860 : if (pset.contains (field))
967 : : return true;
968 : 84 : if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
969 : 0 : for (field = TYPE_FIELDS (TREE_TYPE (field));
970 : 0 : field; field = DECL_CHAIN (field))
971 : : {
972 : 0 : field = next_aggregate_field (field);
973 : 0 : if (field == NULL_TREE)
974 : : break;
975 : 0 : if (field_in_pset (pset, field))
976 : : return true;
977 : : }
978 : : return false;
979 : : }
980 : :
981 : : /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
982 : : aggregate class, if such a conversion is possible. */
983 : :
984 : : static conversion *
985 : 1002346 : build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
986 : : {
987 : 1002346 : unsigned HOST_WIDE_INT i = 0;
988 : 1002346 : conversion *c;
989 : 1002346 : tree field = next_aggregate_field (TYPE_FIELDS (type));
990 : 1002346 : tree empty_ctor = NULL_TREE;
991 : 1002346 : hash_set<tree, true> pset;
992 : :
993 : : /* We already called reshape_init in implicit_conversion, but it might not
994 : : have done anything in the case of parenthesized aggr init. */
995 : :
996 : : /* The conversions within the init-list aren't affected by the enclosing
997 : : context; they're always simple copy-initialization. */
998 : 1002346 : flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
999 : :
1000 : : /* For designated initializers, verify that each initializer is convertible
1001 : : to corresponding TREE_TYPE (ce->index) and mark those FIELD_DECLs as
1002 : : visited. In the following loop then ignore already visited
1003 : : FIELD_DECLs. */
1004 : 1002346 : tree idx, val;
1005 : 1649122 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, val)
1006 : : {
1007 : 646815 : if (!idx)
1008 : : break;
1009 : :
1010 : 646808 : gcc_checking_assert (TREE_CODE (idx) == FIELD_DECL);
1011 : :
1012 : 646808 : tree ftype = TREE_TYPE (idx);
1013 : 646808 : bool ok;
1014 : :
1015 : 646808 : if (TREE_CODE (ftype) == ARRAY_TYPE)
1016 : 811 : ok = can_convert_array (ftype, val, flags, complain);
1017 : : else
1018 : 645997 : ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
1019 : : complain);
1020 : :
1021 : 646808 : if (!ok)
1022 : : return NULL;
1023 : :
1024 : : /* For unions, there should be just one initializer. */
1025 : 646799 : if (TREE_CODE (type) == UNION_TYPE)
1026 : : {
1027 : : field = NULL_TREE;
1028 : : i = 1;
1029 : : break;
1030 : : }
1031 : 646776 : pset.add (idx);
1032 : : }
1033 : :
1034 : 1664270 : for (; field; field = next_aggregate_field (DECL_CHAIN (field)))
1035 : : {
1036 : 662179 : tree ftype = TREE_TYPE (field);
1037 : 662179 : bool ok;
1038 : :
1039 : 662179 : if (!pset.is_empty () && field_in_pset (pset, field))
1040 : 646776 : continue;
1041 : 15403 : if (i < CONSTRUCTOR_NELTS (ctor))
1042 : : {
1043 : 9 : constructor_elt *ce = CONSTRUCTOR_ELT (ctor, i);
1044 : 9 : gcc_checking_assert (!ce->index);
1045 : 9 : val = ce->value;
1046 : 9 : ++i;
1047 : : }
1048 : 15394 : else if (DECL_INITIAL (field))
1049 : 289 : val = get_nsdmi (field, /*ctor*/false, complain);
1050 : 15105 : else if (TYPE_REF_P (ftype))
1051 : : /* Value-initialization of reference is ill-formed. */
1052 : : return NULL;
1053 : : else
1054 : : {
1055 : 15099 : if (empty_ctor == NULL_TREE)
1056 : 12481 : empty_ctor = build_constructor (init_list_type_node, NULL);
1057 : : val = empty_ctor;
1058 : : }
1059 : :
1060 : 15397 : if (TREE_CODE (ftype) == ARRAY_TYPE)
1061 : 9001 : ok = can_convert_array (ftype, val, flags, complain);
1062 : : else
1063 : 6396 : ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
1064 : : complain);
1065 : :
1066 : 15397 : if (!ok)
1067 : : return NULL;
1068 : :
1069 : 15389 : if (TREE_CODE (type) == UNION_TYPE)
1070 : : break;
1071 : : }
1072 : :
1073 : 1002323 : if (i < CONSTRUCTOR_NELTS (ctor))
1074 : : return NULL;
1075 : :
1076 : 1002322 : c = alloc_conversion (ck_aggr);
1077 : 1002322 : c->type = type;
1078 : 1002322 : c->rank = cr_exact;
1079 : 1002322 : c->user_conv_p = true;
1080 : 1002322 : c->check_narrowing = true;
1081 : 1002322 : c->u.expr = ctor;
1082 : 1002322 : return c;
1083 : 1002346 : }
1084 : :
1085 : : /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
1086 : : array type, if such a conversion is possible. */
1087 : :
1088 : : static conversion *
1089 : 582 : build_array_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
1090 : : {
1091 : 582 : conversion *c;
1092 : 582 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1093 : 582 : tree elttype = TREE_TYPE (type);
1094 : 582 : bool bad = false;
1095 : 582 : bool user = false;
1096 : 582 : enum conversion_rank rank = cr_exact;
1097 : :
1098 : : /* We might need to propagate the size from the element to the array. */
1099 : 582 : complete_type (type);
1100 : :
1101 : 582 : if (TYPE_DOMAIN (type)
1102 : 582 : && !variably_modified_type_p (TYPE_DOMAIN (type), NULL_TREE))
1103 : : {
1104 : 512 : unsigned HOST_WIDE_INT alen = tree_to_uhwi (array_type_nelts_top (type));
1105 : 512 : if (alen < len)
1106 : : return NULL;
1107 : : }
1108 : :
1109 : 567 : flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1110 : :
1111 : 2420 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
1112 : : {
1113 : 847 : conversion *sub
1114 : 847 : = implicit_conversion (elttype, TREE_TYPE (e.value), e.value,
1115 : : false, flags, complain);
1116 : 847 : if (sub == NULL)
1117 : : return NULL;
1118 : :
1119 : 815 : if (sub->rank > rank)
1120 : 189 : rank = sub->rank;
1121 : 815 : if (sub->user_conv_p)
1122 : 23 : user = true;
1123 : 815 : if (sub->bad_p)
1124 : 120 : bad = true;
1125 : : }
1126 : :
1127 : 535 : c = alloc_conversion (ck_aggr);
1128 : 535 : c->type = type;
1129 : 535 : c->rank = rank;
1130 : 535 : c->user_conv_p = user;
1131 : 535 : c->bad_p = bad;
1132 : 535 : c->u.expr = ctor;
1133 : 535 : return c;
1134 : : }
1135 : :
1136 : : /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
1137 : : complex type, if such a conversion is possible. */
1138 : :
1139 : : static conversion *
1140 : 56632 : build_complex_conv (tree type, tree ctor, int flags,
1141 : : tsubst_flags_t complain)
1142 : : {
1143 : 56632 : conversion *c;
1144 : 56632 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1145 : 56632 : tree elttype = TREE_TYPE (type);
1146 : 56632 : bool bad = false;
1147 : 56632 : bool user = false;
1148 : 56632 : enum conversion_rank rank = cr_exact;
1149 : :
1150 : 56632 : if (len != 2)
1151 : : return NULL;
1152 : :
1153 : 56592 : flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1154 : :
1155 : 282960 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
1156 : : {
1157 : 113184 : conversion *sub
1158 : 113184 : = implicit_conversion (elttype, TREE_TYPE (e.value), e.value,
1159 : : false, flags, complain);
1160 : 113184 : if (sub == NULL)
1161 : : return NULL;
1162 : :
1163 : 113184 : if (sub->rank > rank)
1164 : 36 : rank = sub->rank;
1165 : 113184 : if (sub->user_conv_p)
1166 : 0 : user = true;
1167 : 113184 : if (sub->bad_p)
1168 : 0 : bad = true;
1169 : : }
1170 : :
1171 : 56592 : c = alloc_conversion (ck_aggr);
1172 : 56592 : c->type = type;
1173 : 56592 : c->rank = rank;
1174 : 56592 : c->user_conv_p = user;
1175 : 56592 : c->bad_p = bad;
1176 : 56592 : c->u.expr = ctor;
1177 : 56592 : return c;
1178 : : }
1179 : :
1180 : : /* Build a representation of the identity conversion from EXPR to
1181 : : itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
1182 : :
1183 : : static conversion *
1184 : 1247634134 : build_identity_conv (tree type, tree expr)
1185 : : {
1186 : 1247634134 : conversion *c;
1187 : :
1188 : 0 : c = alloc_conversion (ck_identity);
1189 : 1247634134 : c->type = type;
1190 : 1247634134 : c->u.expr = expr;
1191 : :
1192 : 1247634134 : return c;
1193 : : }
1194 : :
1195 : : /* Converting from EXPR to TYPE was ambiguous in the sense that there
1196 : : were multiple user-defined conversions to accomplish the job.
1197 : : Build a conversion that indicates that ambiguity. */
1198 : :
1199 : : static conversion *
1200 : 1335 : build_ambiguous_conv (tree type, tree expr)
1201 : : {
1202 : 1335 : conversion *c;
1203 : :
1204 : 0 : c = alloc_conversion (ck_ambig);
1205 : 1335 : c->type = type;
1206 : 1335 : c->u.expr = expr;
1207 : :
1208 : 1335 : return c;
1209 : : }
1210 : :
1211 : : tree
1212 : 2279800263 : strip_top_quals (tree t)
1213 : : {
1214 : 2279800263 : if (TREE_CODE (t) == ARRAY_TYPE)
1215 : : return t;
1216 : 2275163136 : return cp_build_qualified_type (t, 0);
1217 : : }
1218 : :
1219 : : /* Returns the standard conversion path (see [conv]) from type FROM to type
1220 : : TO, if any. For proper handling of null pointer constants, you must
1221 : : also pass the expression EXPR to convert from. If C_CAST_P is true,
1222 : : this conversion is coming from a C-style cast. */
1223 : :
1224 : : static conversion *
1225 : 1014615887 : standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
1226 : : int flags, tsubst_flags_t complain)
1227 : : {
1228 : 1014615887 : enum tree_code fcode, tcode;
1229 : 1014615887 : conversion *conv;
1230 : 1014615887 : bool fromref = false;
1231 : 1014615887 : tree qualified_to;
1232 : :
1233 : 1014615887 : to = non_reference (to);
1234 : 1014615887 : if (TYPE_REF_P (from))
1235 : : {
1236 : 68545 : fromref = true;
1237 : 68545 : from = TREE_TYPE (from);
1238 : : }
1239 : 1014615887 : qualified_to = to;
1240 : 1014615887 : to = strip_top_quals (to);
1241 : 1014615887 : from = strip_top_quals (from);
1242 : :
1243 : 1014615887 : if (expr && type_unknown_p (expr))
1244 : : {
1245 : 205318 : if (TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
1246 : : {
1247 : 28072 : tsubst_flags_t tflags = tf_conv;
1248 : 28072 : expr = instantiate_type (to, expr, tflags);
1249 : 28072 : if (expr == error_mark_node)
1250 : : return NULL;
1251 : 18419 : from = TREE_TYPE (expr);
1252 : : }
1253 : 177246 : else if (TREE_CODE (to) == BOOLEAN_TYPE)
1254 : : {
1255 : : /* Necessary for eg, TEMPLATE_ID_EXPRs (c++/50961). */
1256 : 4626 : expr = resolve_nondeduced_context (expr, complain);
1257 : 4626 : from = TREE_TYPE (expr);
1258 : : }
1259 : : }
1260 : :
1261 : 1014606234 : fcode = TREE_CODE (from);
1262 : 1014606234 : tcode = TREE_CODE (to);
1263 : :
1264 : 1014606234 : conv = build_identity_conv (from, expr);
1265 : 1014606234 : if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
1266 : : {
1267 : 4669242 : from = type_decays_to (from);
1268 : 4669242 : fcode = TREE_CODE (from);
1269 : : /* Tell convert_like that we're using the address. */
1270 : 4669242 : conv->rvaluedness_matches_p = true;
1271 : 4669242 : conv = build_conv (ck_lvalue, from, conv);
1272 : : }
1273 : : /* Wrapping a ck_rvalue around a class prvalue (as a result of using
1274 : : obvalue_p) seems odd, since it's already a prvalue, but that's how we
1275 : : express the copy constructor call required by copy-initialization. */
1276 : 1009936992 : else if (fromref || (expr && obvalue_p (expr)))
1277 : : {
1278 : 247742615 : if (expr)
1279 : : {
1280 : 247674135 : tree bitfield_type;
1281 : 247674135 : bitfield_type = is_bitfield_expr_with_lowered_type (expr);
1282 : 247674135 : if (bitfield_type)
1283 : : {
1284 : 3790384 : from = strip_top_quals (bitfield_type);
1285 : 3790384 : fcode = TREE_CODE (from);
1286 : : }
1287 : : }
1288 : 247742615 : conv = build_conv (ck_rvalue, from, conv);
1289 : : /* If we're performing copy-initialization, remember to skip
1290 : : explicit constructors. */
1291 : 247742615 : if (flags & LOOKUP_ONLYCONVERTING)
1292 : 214732012 : conv->copy_init_p = true;
1293 : : }
1294 : :
1295 : : /* Allow conversion between `__complex__' data types. */
1296 : 1014606234 : if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
1297 : : {
1298 : : /* The standard conversion sequence to convert FROM to TO is
1299 : : the standard conversion sequence to perform componentwise
1300 : : conversion. */
1301 : 2428752 : conversion *part_conv = standard_conversion
1302 : 2428752 : (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags,
1303 : : complain);
1304 : :
1305 : 2428752 : if (!part_conv)
1306 : : conv = NULL;
1307 : 2428752 : else if (part_conv->kind == ck_identity)
1308 : : /* Leave conv alone. */;
1309 : : else
1310 : : {
1311 : 312366 : conv = build_conv (part_conv->kind, to, conv);
1312 : 312366 : conv->rank = part_conv->rank;
1313 : : }
1314 : :
1315 : 2428752 : return conv;
1316 : : }
1317 : :
1318 : 1012177482 : if (same_type_p (from, to))
1319 : : {
1320 : 665500396 : if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue)
1321 : 11845780 : conv->type = qualified_to;
1322 : 665500396 : return conv;
1323 : : }
1324 : :
1325 : : /* [conv.ptr]
1326 : : A null pointer constant can be converted to a pointer type; ... A
1327 : : null pointer constant of integral type can be converted to an
1328 : : rvalue of type std::nullptr_t. */
1329 : 220592972 : if ((tcode == POINTER_TYPE || TYPE_PTRMEM_P (to)
1330 : 220505201 : || NULLPTR_TYPE_P (to))
1331 : 349341711 : && ((expr && null_ptr_cst_p (expr))
1332 : 125216882 : || NULLPTR_TYPE_P (from)))
1333 : 3531861 : conv = build_conv (ck_std, to, conv);
1334 : 343145225 : else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
1335 : 342735876 : || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
1336 : : {
1337 : : /* For backwards brain damage compatibility, allow interconversion of
1338 : : pointers and integers with a pedwarn. */
1339 : 1553072 : conv = build_conv (ck_std, to, conv);
1340 : 1553072 : conv->bad_p = true;
1341 : : }
1342 : 341592153 : else if (UNSCOPED_ENUM_P (to) && fcode == INTEGER_TYPE)
1343 : : {
1344 : : /* For backwards brain damage compatibility, allow interconversion of
1345 : : enums and integers with a pedwarn. */
1346 : 390094 : conv = build_conv (ck_std, to, conv);
1347 : 390094 : conv->bad_p = true;
1348 : : }
1349 : 341202059 : else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
1350 : 222736640 : || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from)))
1351 : : {
1352 : 623 : tree to_pointee;
1353 : 623 : tree from_pointee;
1354 : :
1355 : 623 : if (tcode == POINTER_TYPE)
1356 : : {
1357 : 118465419 : to_pointee = TREE_TYPE (to);
1358 : 118465419 : from_pointee = TREE_TYPE (from);
1359 : :
1360 : : /* Since this is the target of a pointer, it can't have function
1361 : : qualifiers, so any TYPE_QUALS must be for attributes const or
1362 : : noreturn. Strip them. */
1363 : 118465419 : if (TREE_CODE (to_pointee) == FUNCTION_TYPE
1364 : 118465419 : && TYPE_QUALS (to_pointee))
1365 : 9 : to_pointee = build_qualified_type (to_pointee, TYPE_UNQUALIFIED);
1366 : 118465419 : if (TREE_CODE (from_pointee) == FUNCTION_TYPE
1367 : 118465419 : && TYPE_QUALS (from_pointee))
1368 : 18 : from_pointee = build_qualified_type (from_pointee, TYPE_UNQUALIFIED);
1369 : : }
1370 : : else
1371 : : {
1372 : 623 : to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
1373 : 623 : from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
1374 : : }
1375 : :
1376 : 118466042 : if (tcode == POINTER_TYPE
1377 : 118466042 : && same_type_ignoring_top_level_qualifiers_p (from_pointee,
1378 : : to_pointee))
1379 : : ;
1380 : 83023983 : else if (VOID_TYPE_P (to_pointee)
1381 : 3276168 : && !TYPE_PTRDATAMEM_P (from)
1382 : 3276168 : && TREE_CODE (from_pointee) != FUNCTION_TYPE)
1383 : : {
1384 : 3275885 : tree nfrom = TREE_TYPE (from);
1385 : : /* Don't try to apply restrict to void. */
1386 : 3275885 : int quals = cp_type_quals (nfrom) & ~TYPE_QUAL_RESTRICT;
1387 : 3275885 : from_pointee = cp_build_qualified_type (void_type_node, quals);
1388 : 3275885 : from = build_pointer_type (from_pointee);
1389 : 3275885 : conv = build_conv (ck_ptr, from, conv);
1390 : 3275885 : }
1391 : 79748098 : else if (TYPE_PTRDATAMEM_P (from))
1392 : : {
1393 : 623 : tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
1394 : 623 : tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
1395 : :
1396 : 623 : if (same_type_p (fbase, tbase))
1397 : : /* No base conversion needed. */;
1398 : 538 : else if (DERIVED_FROM_P (fbase, tbase)
1399 : 936 : && (same_type_ignoring_top_level_qualifiers_p
1400 : 398 : (from_pointee, to_pointee)))
1401 : : {
1402 : 374 : from = build_ptrmem_type (tbase, from_pointee);
1403 : 374 : conv = build_conv (ck_pmem, from, conv);
1404 : : }
1405 : : else
1406 : 164 : return NULL;
1407 : : }
1408 : 33472272 : else if (CLASS_TYPE_P (from_pointee)
1409 : 33470334 : && CLASS_TYPE_P (to_pointee)
1410 : : /* [conv.ptr]
1411 : :
1412 : : An rvalue of type "pointer to cv D," where D is a
1413 : : class type, can be converted to an rvalue of type
1414 : : "pointer to cv B," where B is a base class (clause
1415 : : _class.derived_) of D. If B is an inaccessible
1416 : : (clause _class.access_) or ambiguous
1417 : : (_class.member.lookup_) base class of D, a program
1418 : : that necessitates this conversion is ill-formed.
1419 : : Therefore, we use DERIVED_FROM_P, and do not check
1420 : : access or uniqueness. */
1421 : 112939564 : && DERIVED_FROM_P (to_pointee, from_pointee))
1422 : : {
1423 : 4660137 : from_pointee
1424 : 4660137 : = cp_build_qualified_type (to_pointee,
1425 : : cp_type_quals (from_pointee));
1426 : 4660137 : from = build_pointer_type (from_pointee);
1427 : 4660137 : conv = build_conv (ck_ptr, from, conv);
1428 : 4660137 : conv->base_p = true;
1429 : : }
1430 : :
1431 : 118465878 : if (same_type_p (from, to))
1432 : : /* OK */;
1433 : 112311005 : else if (c_cast_p && comp_ptr_ttypes_const (to, from, bounds_either))
1434 : : /* In a C-style cast, we ignore CV-qualification because we
1435 : : are allowed to perform a static_cast followed by a
1436 : : const_cast. */
1437 : 614 : conv = build_conv (ck_qual, to, conv);
1438 : 112310391 : else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
1439 : 32245572 : conv = build_conv (ck_qual, to, conv);
1440 : 80064819 : else if (expr && string_conv_p (to, expr, 0))
1441 : : /* converting from string constant to char *. */
1442 : 204 : conv = build_conv (ck_qual, to, conv);
1443 : 80064615 : else if (fnptr_conv_p (to, from))
1444 : 210276 : conv = build_conv (ck_fnptr, to, conv);
1445 : : /* Allow conversions among compatible ObjC pointer types (base
1446 : : conversions have been already handled above). */
1447 : 79854339 : else if (c_dialect_objc ()
1448 : 79854339 : && objc_compare_types (to, from, -4, NULL_TREE))
1449 : 0 : conv = build_conv (ck_ptr, to, conv);
1450 : 79854339 : else if (ptr_reasonably_similar (to_pointee, from_pointee))
1451 : : {
1452 : 5964125 : conv = build_conv (ck_ptr, to, conv);
1453 : 5964125 : conv->bad_p = true;
1454 : : }
1455 : : else
1456 : : return NULL;
1457 : :
1458 : 156341727 : from = to;
1459 : : }
1460 : 222736017 : else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
1461 : : {
1462 : 85755 : tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
1463 : 85755 : tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
1464 : 85755 : tree fbase = class_of_this_parm (fromfn);
1465 : 85755 : tree tbase = class_of_this_parm (tofn);
1466 : :
1467 : : /* If FBASE and TBASE are equivalent but incomplete, DERIVED_FROM_P
1468 : : yields false. But a pointer to member of incomplete class is OK. */
1469 : 85755 : if (!same_type_p (fbase, tbase) && !DERIVED_FROM_P (fbase, tbase))
1470 : : return NULL;
1471 : :
1472 : 85594 : tree fstat = static_fn_type (fromfn);
1473 : 85594 : tree tstat = static_fn_type (tofn);
1474 : 85594 : if (same_type_p (tstat, fstat)
1475 : 85594 : || fnptr_conv_p (tstat, fstat))
1476 : : /* OK */;
1477 : : else
1478 : : return NULL;
1479 : :
1480 : 85326 : if (!same_type_p (fbase, tbase))
1481 : : {
1482 : 85259 : from = build_memfn_type (fstat,
1483 : : tbase,
1484 : : cp_type_quals (tbase),
1485 : : type_memfn_rqual (tofn));
1486 : 85259 : from = build_ptrmemfunc_type (build_pointer_type (from));
1487 : 85259 : conv = build_conv (ck_pmem, from, conv);
1488 : 85259 : conv->base_p = true;
1489 : : }
1490 : 85326 : if (fnptr_conv_p (tstat, fstat))
1491 : 67 : conv = build_conv (ck_fnptr, to, conv);
1492 : : }
1493 : 222650262 : else if (tcode == BOOLEAN_TYPE)
1494 : : {
1495 : : /* [conv.bool]
1496 : :
1497 : : A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
1498 : : to member type can be converted to a prvalue of type bool. ...
1499 : : For direct-initialization (8.5 [dcl.init]), a prvalue of type
1500 : : std::nullptr_t can be converted to a prvalue of type bool; */
1501 : 5457173 : if (ARITHMETIC_TYPE_P (from)
1502 : 5376235 : || UNSCOPED_ENUM_P (from)
1503 : 3195091 : || fcode == POINTER_TYPE
1504 : 2268318 : || TYPE_PTRMEM_P (from)
1505 : 12307317 : || NULLPTR_TYPE_P (from))
1506 : : {
1507 : 7771186 : conv = build_conv (ck_std, to, conv);
1508 : 7771186 : if (fcode == POINTER_TYPE
1509 : 6844413 : || TYPE_PTRDATAMEM_P (from)
1510 : 6844275 : || (TYPE_PTRMEMFUNC_P (from)
1511 : 77 : && conv->rank < cr_pbool)
1512 : 14615384 : || NULLPTR_TYPE_P (from))
1513 : 927063 : conv->rank = cr_pbool;
1514 : 7771186 : if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING))
1515 : 36 : conv->bad_p = true;
1516 : 7771186 : if (flags & LOOKUP_NO_NARROWING)
1517 : 31689 : conv->check_narrowing = true;
1518 : 7771186 : return conv;
1519 : : }
1520 : :
1521 : : return NULL;
1522 : : }
1523 : : /* We don't check for ENUMERAL_TYPE here because there are no standard
1524 : : conversions to enum type. */
1525 : : /* As an extension, allow conversion to complex type. */
1526 : 212611048 : else if (ARITHMETIC_TYPE_P (to))
1527 : : {
1528 : 28210691 : if (! (INTEGRAL_CODE_P (fcode)
1529 : 25360499 : || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL)))
1530 : 134956909 : || SCOPED_ENUM_P (from))
1531 : : return NULL;
1532 : :
1533 : : /* If we're parsing an enum with no fixed underlying type, we're
1534 : : dealing with an incomplete type, which renders the conversion
1535 : : ill-formed. */
1536 : 105753260 : if (!COMPLETE_TYPE_P (from))
1537 : : return NULL;
1538 : :
1539 : 105753254 : conv = build_conv (ck_std, to, conv);
1540 : :
1541 : 105753254 : tree underlying_type = NULL_TREE;
1542 : 105753254 : if (TREE_CODE (from) == ENUMERAL_TYPE
1543 : 105753254 : && ENUM_FIXED_UNDERLYING_TYPE_P (from))
1544 : 2676405 : underlying_type = ENUM_UNDERLYING_TYPE (from);
1545 : :
1546 : : /* Give this a better rank if it's a promotion.
1547 : :
1548 : : To handle CWG 1601, also bump the rank if we are converting
1549 : : an enumeration with a fixed underlying type to the underlying
1550 : : type. */
1551 : 105753254 : if ((same_type_p (to, type_promotes_to (from))
1552 : 92651673 : || (underlying_type && same_type_p (to, underlying_type)))
1553 : 105753273 : && next_conversion (conv)->rank <= cr_promotion)
1554 : 13101600 : conv->rank = cr_promotion;
1555 : :
1556 : : /* A prvalue of floating-point type can be converted to a prvalue of
1557 : : another floating-point type with a greater or equal conversion
1558 : : rank ([conv.rank]). A prvalue of standard floating-point type can
1559 : : be converted to a prvalue of another standard floating-point type.
1560 : : For backwards compatibility with handling __float128 and other
1561 : : non-standard floating point types, allow all implicit floating
1562 : : point conversions if neither type is extended floating-point
1563 : : type and if at least one of them is, fail if they have unordered
1564 : : conversion rank or from has higher conversion rank. */
1565 : 105753254 : if (fcode == REAL_TYPE
1566 : 105753254 : && tcode == REAL_TYPE
1567 : 17427770 : && (extended_float_type_p (from)
1568 : 16586462 : || extended_float_type_p (to))
1569 : 110276680 : && cp_compare_floating_point_conversion_ranks (from, to) >= 2)
1570 : 2171472 : conv->bad_p = true;
1571 : : }
1572 : 103014638 : else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
1573 : 103014638 : && vector_types_convertible_p (from, to, false))
1574 : 4206 : return build_conv (ck_std, to, conv);
1575 : 103010432 : else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
1576 : 34130663 : && is_properly_derived_from (from, to))
1577 : : {
1578 : 452456 : if (conv->kind == ck_rvalue)
1579 : 452405 : conv = next_conversion (conv);
1580 : 452456 : conv = build_conv (ck_base, to, conv);
1581 : : /* The derived-to-base conversion indicates the initialization
1582 : : of a parameter with base type from an object of a derived
1583 : : type. A temporary object is created to hold the result of
1584 : : the conversion unless we're binding directly to a reference. */
1585 : 452456 : conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
1586 : : /* If we're performing copy-initialization, remember to skip
1587 : : explicit constructors. */
1588 : 452456 : if (flags & LOOKUP_ONLYCONVERTING)
1589 : 452389 : conv->copy_init_p = true;
1590 : : }
1591 : : else
1592 : 102557976 : return NULL;
1593 : :
1594 : 156341727 : if (flags & LOOKUP_NO_NARROWING)
1595 : 16233106 : conv->check_narrowing = true;
1596 : :
1597 : : return conv;
1598 : : }
1599 : :
1600 : : /* Returns nonzero if T1 is reference-related to T2.
1601 : :
1602 : : This is considered when a reference to T1 is initialized by a T2. */
1603 : :
1604 : : bool
1605 : 161440706 : reference_related_p (tree t1, tree t2)
1606 : : {
1607 : 161440706 : if (t1 == error_mark_node || t2 == error_mark_node)
1608 : : return false;
1609 : :
1610 : 161440702 : t1 = TYPE_MAIN_VARIANT (t1);
1611 : 161440702 : t2 = TYPE_MAIN_VARIANT (t2);
1612 : :
1613 : : /* [dcl.init.ref]
1614 : :
1615 : : Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
1616 : : to "cv2 T2" if T1 is similar to T2, or T1 is a base class of T2. */
1617 : 161440702 : return (similar_type_p (t1, t2)
1618 : 161440702 : || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1619 : 34047769 : && DERIVED_FROM_P (t1, t2)));
1620 : : }
1621 : :
1622 : : /* Returns nonzero if T1 is reference-compatible with T2. */
1623 : :
1624 : : bool
1625 : 140452239 : reference_compatible_p (tree t1, tree t2)
1626 : : {
1627 : : /* [dcl.init.ref]
1628 : :
1629 : : "cv1 T1" is reference compatible with "cv2 T2" if
1630 : : a prvalue of type "pointer to cv2 T2" can be converted to the type
1631 : : "pointer to cv1 T1" via a standard conversion sequence. */
1632 : 140452239 : tree ptype1 = build_pointer_type (t1);
1633 : 140452239 : tree ptype2 = build_pointer_type (t2);
1634 : 140452239 : conversion *conv = standard_conversion (ptype1, ptype2, NULL_TREE,
1635 : : /*c_cast_p=*/false, 0, tf_none);
1636 : 140452239 : if (!conv || conv->bad_p)
1637 : 78097579 : return false;
1638 : : return true;
1639 : : }
1640 : :
1641 : : /* Return true if converting FROM to TO would involve a qualification
1642 : : conversion. */
1643 : :
1644 : : static bool
1645 : 69119377 : involves_qualification_conversion_p (tree to, tree from)
1646 : : {
1647 : : /* If we're not convering a pointer to another one, we won't get
1648 : : a qualification conversion. */
1649 : 69119377 : if (!((TYPE_PTR_P (to) && TYPE_PTR_P (from))
1650 : 2638 : || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from))))
1651 : : return false;
1652 : :
1653 : 2651785 : conversion *conv = standard_conversion (to, from, NULL_TREE,
1654 : : /*c_cast_p=*/false, 0, tf_none);
1655 : 5278043 : for (conversion *t = conv; t; t = next_conversion (t))
1656 : 2651799 : if (t->kind == ck_qual)
1657 : : return true;
1658 : :
1659 : : return false;
1660 : : }
1661 : :
1662 : : /* A reference of the indicated TYPE is being bound directly to the
1663 : : expression represented by the implicit conversion sequence CONV.
1664 : : Return a conversion sequence for this binding. */
1665 : :
1666 : : static conversion *
1667 : 71713049 : direct_reference_binding (tree type, conversion *conv)
1668 : : {
1669 : 71713049 : tree t;
1670 : :
1671 : 71713049 : gcc_assert (TYPE_REF_P (type));
1672 : 71713049 : gcc_assert (!TYPE_REF_P (conv->type));
1673 : :
1674 : 71713049 : t = TREE_TYPE (type);
1675 : :
1676 : 71713049 : if (conv->kind == ck_identity)
1677 : : /* Mark the identity conv as to not decay to rvalue. */
1678 : 71713049 : conv->rvaluedness_matches_p = true;
1679 : :
1680 : : /* [over.ics.rank]
1681 : :
1682 : : When a parameter of reference type binds directly
1683 : : (_dcl.init.ref_) to an argument expression, the implicit
1684 : : conversion sequence is the identity conversion, unless the
1685 : : argument expression has a type that is a derived class of the
1686 : : parameter type, in which case the implicit conversion sequence is
1687 : : a derived-to-base Conversion.
1688 : :
1689 : : If the parameter binds directly to the result of applying a
1690 : : conversion function to the argument expression, the implicit
1691 : : conversion sequence is a user-defined conversion sequence
1692 : : (_over.ics.user_), with the second standard conversion sequence
1693 : : either an identity conversion or, if the conversion function
1694 : : returns an entity of a type that is a derived class of the
1695 : : parameter type, a derived-to-base conversion. */
1696 : 71713049 : if (is_properly_derived_from (conv->type, t))
1697 : : {
1698 : : /* Represent the derived-to-base conversion. */
1699 : 2593672 : conv = build_conv (ck_base, t, conv);
1700 : : /* We will actually be binding to the base-class subobject in
1701 : : the derived class, so we mark this conversion appropriately.
1702 : : That way, convert_like knows not to generate a temporary. */
1703 : 2593672 : conv->need_temporary_p = false;
1704 : : }
1705 : 69119377 : else if (involves_qualification_conversion_p (t, conv->type))
1706 : : /* Represent the qualification conversion. After DR 2352
1707 : : #1 and #2 were indistinguishable conversion sequences:
1708 : :
1709 : : void f(int*); // #1
1710 : : void f(const int* const &); // #2
1711 : : void g(int* p) { f(p); }
1712 : :
1713 : : because the types "int *" and "const int *const" are
1714 : : reference-related and we were binding both directly and they
1715 : : had the same rank. To break it up, we add a ck_qual under the
1716 : : ck_ref_bind so that conversion sequence ranking chooses #1.
1717 : :
1718 : : We strip_top_quals here which is also what standard_conversion
1719 : : does. Failure to do so would confuse comp_cv_qual_signature
1720 : : into thinking that in
1721 : :
1722 : : void f(const int * const &); // #1
1723 : : void f(const int *); // #2
1724 : : int *x;
1725 : : f(x);
1726 : :
1727 : : #2 is a better match than #1 even though they're ambiguous (97296). */
1728 : 25541 : conv = build_conv (ck_qual, strip_top_quals (t), conv);
1729 : :
1730 : 71713049 : return build_conv (ck_ref_bind, type, conv);
1731 : : }
1732 : :
1733 : : /* Returns the conversion path from type FROM to reference type TO for
1734 : : purposes of reference binding. For lvalue binding, either pass a
1735 : : reference type to FROM or an lvalue expression to EXPR. If the
1736 : : reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1737 : : the conversion returned. If C_CAST_P is true, this
1738 : : conversion is coming from a C-style cast. */
1739 : :
1740 : : static conversion *
1741 : 140075533 : reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags,
1742 : : tsubst_flags_t complain)
1743 : : {
1744 : 140075533 : conversion *conv = NULL;
1745 : 140075533 : conversion *bad_direct_conv = nullptr;
1746 : 140075533 : tree to = TREE_TYPE (rto);
1747 : 140075533 : tree from = rfrom;
1748 : 140075533 : tree tfrom;
1749 : 140075533 : bool related_p;
1750 : 140075533 : bool compatible_p;
1751 : 140075533 : cp_lvalue_kind gl_kind;
1752 : 140075533 : bool is_lvalue;
1753 : :
1754 : 140075533 : if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1755 : : {
1756 : 48 : expr = instantiate_type (to, expr, tf_none);
1757 : 48 : if (expr == error_mark_node)
1758 : : return NULL;
1759 : 48 : from = TREE_TYPE (expr);
1760 : : }
1761 : :
1762 : 140075533 : bool copy_list_init = false;
1763 : 140075533 : bool single_list_conv = false;
1764 : 140075533 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1765 : : {
1766 : 158360 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
1767 : : /* DR 1288: Otherwise, if the initializer list has a single element
1768 : : of type E and ... [T's] referenced type is reference-related to E,
1769 : : the object or reference is initialized from that element...
1770 : :
1771 : : ??? With P0388R4, we should bind 't' directly to U{}:
1772 : : using U = A[2];
1773 : : A (&&t)[] = {U{}};
1774 : : because A[] and A[2] are reference-related. But we don't do it
1775 : : because grok_reference_init has deduced the array size (to 1), and
1776 : : A[1] and A[2] aren't reference-related. */
1777 : 158360 : if (CONSTRUCTOR_NELTS (expr) == 1
1778 : 38254 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
1779 : : {
1780 : 1402 : tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1781 : 1402 : if (error_operand_p (elt))
1782 : : return NULL;
1783 : 1397 : tree etype = TREE_TYPE (elt);
1784 : 1397 : if (reference_related_p (to, etype))
1785 : : {
1786 : 317 : expr = elt;
1787 : 317 : from = etype;
1788 : 317 : goto skip;
1789 : : }
1790 : 1080 : else if (CLASS_TYPE_P (etype) && TYPE_HAS_CONVERSION (etype))
1791 : : /* CWG1996: jason's proposed drafting adds "or initializing T from E
1792 : : would bind directly". We check that in the direct binding with
1793 : : conversion code below. */
1794 : : single_list_conv = true;
1795 : : }
1796 : : /* Otherwise, if T is a reference type, a prvalue temporary of the type
1797 : : referenced by T is copy-list-initialized, and the reference is bound
1798 : : to that temporary. */
1799 : : copy_list_init = true;
1800 : 140075528 : skip:;
1801 : : }
1802 : :
1803 : 140075528 : if (TYPE_REF_P (from))
1804 : : {
1805 : 49277 : from = TREE_TYPE (from);
1806 : 49277 : if (!TYPE_REF_IS_RVALUE (rfrom)
1807 : 49277 : || TREE_CODE (from) == FUNCTION_TYPE)
1808 : : gl_kind = clk_ordinary;
1809 : : else
1810 : : gl_kind = clk_rvalueref;
1811 : : }
1812 : 140026251 : else if (expr)
1813 : 138110785 : gl_kind = lvalue_kind (expr);
1814 : 1807291 : else if (CLASS_TYPE_P (from)
1815 : 1915466 : || TREE_CODE (from) == ARRAY_TYPE)
1816 : : gl_kind = clk_class;
1817 : : else
1818 : : gl_kind = clk_none;
1819 : :
1820 : : /* Don't allow a class prvalue when LOOKUP_NO_TEMP_BIND. */
1821 : 140075528 : if ((flags & LOOKUP_NO_TEMP_BIND)
1822 : 1884138 : && (gl_kind & clk_class))
1823 : : gl_kind = clk_none;
1824 : :
1825 : : /* Same mask as real_lvalue_p. */
1826 : 138538977 : is_lvalue = gl_kind && !(gl_kind & (clk_rvalueref|clk_class));
1827 : :
1828 : 118897587 : tfrom = from;
1829 : 118897587 : if ((gl_kind & clk_bitfield) != 0)
1830 : 2546736 : tfrom = unlowered_expr_type (expr);
1831 : :
1832 : : /* Figure out whether or not the types are reference-related and
1833 : : reference compatible. We have to do this after stripping
1834 : : references from FROM. */
1835 : 140075528 : related_p = reference_related_p (to, tfrom);
1836 : : /* If this is a C cast, first convert to an appropriately qualified
1837 : : type, so that we can later do a const_cast to the desired type. */
1838 : 140075528 : if (related_p && c_cast_p
1839 : 140075528 : && !at_least_as_qualified_p (to, tfrom))
1840 : 192 : to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1841 : 140075528 : compatible_p = reference_compatible_p (to, tfrom);
1842 : :
1843 : : /* Directly bind reference when target expression's type is compatible with
1844 : : the reference and expression is an lvalue. In DR391, the wording in
1845 : : [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1846 : : const and rvalue references to rvalues of compatible class type.
1847 : : We should also do direct bindings for non-class xvalues. */
1848 : 140075528 : if ((related_p || compatible_p) && gl_kind)
1849 : : {
1850 : : /* [dcl.init.ref]
1851 : :
1852 : : If the initializer expression
1853 : :
1854 : : -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1855 : : is reference-compatible with "cv2 T2,"
1856 : :
1857 : : the reference is bound directly to the initializer expression
1858 : : lvalue.
1859 : :
1860 : : [...]
1861 : : If the initializer expression is an rvalue, with T2 a class type,
1862 : : and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1863 : : is bound to the object represented by the rvalue or to a sub-object
1864 : : within that object. */
1865 : :
1866 : 62769354 : conv = build_identity_conv (tfrom, expr);
1867 : 62769354 : conv = direct_reference_binding (rto, conv);
1868 : :
1869 : 62769354 : if (TYPE_REF_P (rfrom))
1870 : : /* Handle rvalue reference to function properly. */
1871 : 11099 : conv->rvaluedness_matches_p
1872 : 11099 : = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom));
1873 : : else
1874 : 62758255 : conv->rvaluedness_matches_p
1875 : 62758255 : = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
1876 : :
1877 : 62769354 : if ((gl_kind & clk_bitfield) != 0
1878 : 62769354 : || ((gl_kind & clk_packed) != 0 && !TYPE_PACKED (to)))
1879 : : /* For the purposes of overload resolution, we ignore the fact
1880 : : this expression is a bitfield or packed field. (In particular,
1881 : : [over.ics.ref] says specifically that a function with a
1882 : : non-const reference parameter is viable even if the
1883 : : argument is a bitfield.)
1884 : :
1885 : : However, when we actually call the function we must create
1886 : : a temporary to which to bind the reference. If the
1887 : : reference is volatile, or isn't const, then we cannot make
1888 : : a temporary, so we just issue an error when the conversion
1889 : : actually occurs. */
1890 : 109 : conv->need_temporary_p = true;
1891 : :
1892 : : /* Don't allow binding of lvalues (other than function lvalues) to
1893 : : rvalue references. */
1894 : 46233924 : if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
1895 : 69011015 : && TREE_CODE (to) != FUNCTION_TYPE)
1896 : 6239655 : conv->bad_p = true;
1897 : :
1898 : : /* Nor the reverse. */
1899 : 16535430 : if (!is_lvalue && !TYPE_REF_IS_RVALUE (rto)
1900 : : /* Unless it's really a C++20 lvalue being treated as an xvalue.
1901 : : But in C++23, such an expression is just an xvalue, not a special
1902 : : lvalue, so the binding is once again ill-formed. */
1903 : 9402210 : && !(cxx_dialect <= cxx20
1904 : 6508240 : && (gl_kind & clk_implicit_rval))
1905 : 8823037 : && (!CP_TYPE_CONST_NON_VOLATILE_P (to)
1906 : 8769961 : || (flags & LOOKUP_NO_RVAL_BIND))
1907 : 62822430 : && TREE_CODE (to) != FUNCTION_TYPE)
1908 : 53076 : conv->bad_p = true;
1909 : :
1910 : 62769354 : if (!compatible_p)
1911 : 4118494 : conv->bad_p = true;
1912 : :
1913 : 62769354 : return conv;
1914 : : }
1915 : : /* [class.conv.fct] A conversion function is never used to convert a
1916 : : (possibly cv-qualified) object to the (possibly cv-qualified) same
1917 : : object type (or a reference to it), to a (possibly cv-qualified) base
1918 : : class of that type (or a reference to it).... */
1919 : 3329443 : else if (!related_p
1920 : 73976731 : && !(flags & LOOKUP_NO_CONVERSION)
1921 : 29068680 : && (CLASS_TYPE_P (from) || single_list_conv))
1922 : : {
1923 : 8106203 : tree rexpr = expr;
1924 : 8106203 : if (single_list_conv)
1925 : 22 : rexpr = CONSTRUCTOR_ELT (expr, 0)->value;
1926 : :
1927 : : /* [dcl.init.ref]
1928 : :
1929 : : If the initializer expression
1930 : :
1931 : : -- has a class type (i.e., T2 is a class type) can be
1932 : : implicitly converted to an lvalue of type "cv3 T3," where
1933 : : "cv1 T1" is reference-compatible with "cv3 T3". (this
1934 : : conversion is selected by enumerating the applicable
1935 : : conversion functions (_over.match.ref_) and choosing the
1936 : : best one through overload resolution. (_over.match_).
1937 : :
1938 : : the reference is bound to the lvalue result of the conversion
1939 : : in the second case. */
1940 : 8106203 : z_candidate *cand = build_user_type_conversion_1 (rto, rexpr, flags,
1941 : : complain);
1942 : 8106203 : if (cand)
1943 : : {
1944 : 10568 : if (!cand->second_conv->bad_p)
1945 : : return cand->second_conv;
1946 : :
1947 : : /* Direct reference binding wasn't successful and yielded a bad
1948 : : conversion. Proceed with trying to go through a temporary
1949 : : instead, and if that also fails then we'll return this bad
1950 : : conversion rather than no conversion for sake of better
1951 : : diagnostics. */
1952 : : bad_direct_conv = cand->second_conv;
1953 : : }
1954 : : }
1955 : :
1956 : : /* From this point on, we conceptually need temporaries, even if we
1957 : : elide them. Only the cases above are "direct bindings". */
1958 : 77295700 : if (flags & LOOKUP_NO_TEMP_BIND)
1959 : : return bad_direct_conv ? bad_direct_conv : nullptr;
1960 : :
1961 : : /* [over.ics.rank]
1962 : :
1963 : : When a parameter of reference type is not bound directly to an
1964 : : argument expression, the conversion sequence is the one required
1965 : : to convert the argument expression to the underlying type of the
1966 : : reference according to _over.best.ics_. Conceptually, this
1967 : : conversion sequence corresponds to copy-initializing a temporary
1968 : : of the underlying type with the argument expression. Any
1969 : : difference in top-level cv-qualification is subsumed by the
1970 : : initialization itself and does not constitute a conversion. */
1971 : :
1972 : 75502498 : bool maybe_valid_p = true;
1973 : :
1974 : : /* [dcl.init.ref]
1975 : :
1976 : : Otherwise, the reference shall be an lvalue reference to a
1977 : : non-volatile const type, or the reference shall be an rvalue
1978 : : reference. */
1979 : 98110350 : if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto))
1980 : : maybe_valid_p = false;
1981 : :
1982 : : /* [dcl.init.ref]
1983 : :
1984 : : Otherwise, a temporary of type "cv1 T1" is created and
1985 : : initialized from the initializer expression using the rules for a
1986 : : non-reference copy initialization. If T1 is reference-related to
1987 : : T2, cv1 must be the same cv-qualification as, or greater
1988 : : cv-qualification than, cv2; otherwise, the program is ill-formed. */
1989 : 75502498 : if (related_p && !at_least_as_qualified_p (to, from))
1990 : : maybe_valid_p = false;
1991 : :
1992 : : /* We try below to treat an invalid reference binding as a bad conversion
1993 : : to improve diagnostics, but doing so may cause otherwise unnecessary
1994 : : instantiations that can lead to a hard error. So during the first pass
1995 : : of overload resolution wherein we shortcut bad conversions, instead just
1996 : : produce a special conversion indicating a second pass is necessary if
1997 : : there's no strictly viable candidate. */
1998 : 75502498 : if (!maybe_valid_p && (flags & LOOKUP_SHORTCUT_BAD_CONVS))
1999 : : {
2000 : 4341920 : if (bad_direct_conv)
2001 : : return bad_direct_conv;
2002 : :
2003 : 4341869 : conv = alloc_conversion (ck_deferred_bad);
2004 : 4341869 : conv->bad_p = true;
2005 : 4341869 : return conv;
2006 : : }
2007 : :
2008 : : /* We're generating a temporary now, but don't bind any more in the
2009 : : conversion (specifically, don't slice the temporary returned by a
2010 : : conversion operator). */
2011 : 71160578 : flags |= LOOKUP_NO_TEMP_BIND;
2012 : :
2013 : : /* Core issue 899: When [copy-]initializing a temporary to be bound
2014 : : to the first parameter of a copy constructor (12.8) called with
2015 : : a single argument in the context of direct-initialization,
2016 : : explicit conversion functions are also considered.
2017 : :
2018 : : So don't set LOOKUP_ONLYCONVERTING in that case. */
2019 : 71160578 : if (!(flags & LOOKUP_COPY_PARM))
2020 : 61787374 : flags |= LOOKUP_ONLYCONVERTING;
2021 : :
2022 : 71160578 : if (!conv)
2023 : 71160578 : conv = implicit_conversion (to, from, expr, c_cast_p,
2024 : : flags, complain);
2025 : 71160578 : if (!conv)
2026 : : return bad_direct_conv ? bad_direct_conv : nullptr;
2027 : :
2028 : 7461974 : if (conv->user_conv_p)
2029 : : {
2030 : 4412945 : if (copy_list_init)
2031 : : /* Remember this was copy-list-initialization. */
2032 : 91165 : conv->need_temporary_p = true;
2033 : :
2034 : : /* If initializing the temporary used a conversion function,
2035 : : recalculate the second conversion sequence. */
2036 : 12600168 : for (conversion *t = conv; t; t = next_conversion (t))
2037 : 8486910 : if (t->kind == ck_user
2038 : 4373335 : && c_cast_p && !maybe_valid_p)
2039 : : {
2040 : 12 : if (complain & tf_warning)
2041 : 12 : warning (OPT_Wcast_user_defined,
2042 : : "casting %qT to %qT does not use %qD",
2043 : 12 : from, rto, t->cand->fn);
2044 : : /* Don't let recalculation try to make this valid. */
2045 : : break;
2046 : : }
2047 : 8486898 : else if (t->kind == ck_user
2048 : 8486898 : && DECL_CONV_FN_P (t->cand->fn))
2049 : : {
2050 : 299675 : tree ftype = TREE_TYPE (TREE_TYPE (t->cand->fn));
2051 : : /* A prvalue of non-class type is cv-unqualified. */
2052 : 299675 : if (!TYPE_REF_P (ftype) && !CLASS_TYPE_P (ftype))
2053 : 352 : ftype = cv_unqualified (ftype);
2054 : 299675 : int sflags = (flags|LOOKUP_NO_CONVERSION)&~LOOKUP_NO_TEMP_BIND;
2055 : 299675 : conversion *new_second
2056 : 299675 : = reference_binding (rto, ftype, NULL_TREE, c_cast_p,
2057 : : sflags, complain);
2058 : 299675 : if (!new_second)
2059 : : return bad_direct_conv ? bad_direct_conv : nullptr;
2060 : 299675 : conv = merge_conversion_sequences (t, new_second);
2061 : 299675 : gcc_assert (maybe_valid_p || conv->bad_p);
2062 : : return conv;
2063 : : }
2064 : : }
2065 : :
2066 : 7162299 : conv = build_conv (ck_ref_bind, rto, conv);
2067 : : /* This reference binding, unlike those above, requires the
2068 : : creation of a temporary. */
2069 : 7162299 : conv->need_temporary_p = true;
2070 : 7162299 : conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
2071 : 7162299 : conv->bad_p |= !maybe_valid_p;
2072 : :
2073 : 7162299 : return conv;
2074 : : }
2075 : :
2076 : : /* Returns the implicit conversion sequence (see [over.ics]) from type
2077 : : FROM to type TO. The optional expression EXPR may affect the
2078 : : conversion. FLAGS are the usual overloading flags. If C_CAST_P is
2079 : : true, this conversion is coming from a C-style cast. */
2080 : :
2081 : : static conversion *
2082 : 1003906920 : implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
2083 : : int flags, tsubst_flags_t complain)
2084 : : {
2085 : 1003906920 : conversion *conv;
2086 : :
2087 : 1003906920 : if (from == error_mark_node || to == error_mark_node
2088 : 1003906189 : || expr == error_mark_node)
2089 : : return NULL;
2090 : :
2091 : : /* Other flags only apply to the primary function in overload
2092 : : resolution, or after we've chosen one. */
2093 : 1003906189 : flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM
2094 : : |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_NO_NARROWING
2095 : : |LOOKUP_PROTECT|LOOKUP_NO_NON_INTEGRAL|LOOKUP_SHORTCUT_BAD_CONVS);
2096 : :
2097 : : /* FIXME: actually we don't want warnings either, but we can't just
2098 : : have 'complain &= ~(tf_warning|tf_error)' because it would cause
2099 : : the regression of, eg, g++.old-deja/g++.benjamin/16077.C.
2100 : : We really ought not to issue that warning until we've committed
2101 : : to that conversion. */
2102 : 1003906189 : complain &= ~tf_error;
2103 : :
2104 : : /* Call reshape_init early to remove redundant braces. */
2105 : 1003906189 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr) && CLASS_TYPE_P (to))
2106 : : {
2107 : 1657717 : to = complete_type (to);
2108 : 1657717 : if (!COMPLETE_TYPE_P (to))
2109 : : return nullptr;
2110 : 1657693 : if (!CLASSTYPE_NON_AGGREGATE (to))
2111 : : {
2112 : 1002556 : expr = reshape_init (to, expr, complain);
2113 : 1002556 : if (expr == error_mark_node)
2114 : : return nullptr;
2115 : 1002388 : from = TREE_TYPE (expr);
2116 : : }
2117 : : }
2118 : :
2119 : 1003905997 : if (TYPE_REF_P (to))
2120 : 134822886 : conv = reference_binding (to, from, expr, c_cast_p, flags, complain);
2121 : : else
2122 : 869083111 : conv = standard_conversion (to, from, expr, c_cast_p, flags, complain);
2123 : :
2124 : 1003905997 : if (conv)
2125 : : return conv;
2126 : :
2127 : 174347580 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
2128 : : {
2129 : 3361760 : if (is_std_init_list (to) && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2130 : 60774 : return build_list_conv (to, expr, flags, complain);
2131 : :
2132 : : /* As an extension, allow list-initialization of _Complex. */
2133 : 3240203 : if (TREE_CODE (to) == COMPLEX_TYPE
2134 : 3296844 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2135 : : {
2136 : 56632 : conv = build_complex_conv (to, expr, flags, complain);
2137 : 56632 : if (conv)
2138 : : return conv;
2139 : : }
2140 : :
2141 : : /* Allow conversion from an initializer-list with one element to a
2142 : : scalar type. */
2143 : 3183611 : if (SCALAR_TYPE_P (to))
2144 : : {
2145 : 1580645 : int nelts = CONSTRUCTOR_NELTS (expr);
2146 : 281744 : tree elt;
2147 : :
2148 : 281744 : if (nelts == 0)
2149 : 1298901 : elt = build_value_init (to, tf_none);
2150 : 281744 : else if (nelts == 1 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2151 : 281295 : elt = CONSTRUCTOR_ELT (expr, 0)->value;
2152 : : else
2153 : 449 : elt = error_mark_node;
2154 : :
2155 : 1580645 : conv = implicit_conversion (to, TREE_TYPE (elt), elt,
2156 : : c_cast_p, flags, complain);
2157 : 1580645 : if (conv)
2158 : : {
2159 : 1579613 : conv->check_narrowing = true;
2160 : 1579613 : if (BRACE_ENCLOSED_INITIALIZER_P (elt))
2161 : : /* Too many levels of braces, i.e. '{{1}}'. */
2162 : 14 : conv->bad_p = true;
2163 : 1579613 : return conv;
2164 : : }
2165 : : }
2166 : 1602966 : else if (TREE_CODE (to) == ARRAY_TYPE)
2167 : 582 : return build_array_conv (to, expr, flags, complain);
2168 : : }
2169 : :
2170 : 172650019 : if (expr != NULL_TREE
2171 : 169015296 : && (MAYBE_CLASS_TYPE_P (from)
2172 : 103301546 : || MAYBE_CLASS_TYPE_P (to))
2173 : 287289008 : && (flags & LOOKUP_NO_CONVERSION) == 0)
2174 : : {
2175 : 44169176 : struct z_candidate *cand;
2176 : :
2177 : 32198211 : if (CLASS_TYPE_P (to)
2178 : 32198165 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
2179 : 45765560 : && !CLASSTYPE_NON_AGGREGATE (complete_type (to)))
2180 : 1002346 : return build_aggr_conv (to, expr, flags, complain);
2181 : :
2182 : 43166830 : cand = build_user_type_conversion_1 (to, expr, flags, complain);
2183 : 43166830 : if (cand)
2184 : : {
2185 : 590738 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
2186 : 588407 : && CONSTRUCTOR_NELTS (expr) == 1
2187 : 17575 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr)
2188 : 8394287 : && !is_list_ctor (cand->fn))
2189 : : {
2190 : : /* "If C is not an initializer-list constructor and the
2191 : : initializer list has a single element of type cv U, where U is
2192 : : X or a class derived from X, the implicit conversion sequence
2193 : : has Exact Match rank if U is X, or Conversion rank if U is
2194 : : derived from X." */
2195 : 17241 : tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
2196 : 17241 : tree elttype = TREE_TYPE (elt);
2197 : 17241 : if (reference_related_p (to, elttype))
2198 : 67 : return implicit_conversion (to, elttype, elt,
2199 : 67 : c_cast_p, flags, complain);
2200 : : }
2201 : 8376645 : conv = cand->second_conv;
2202 : : }
2203 : :
2204 : : /* We used to try to bind a reference to a temporary here, but that
2205 : : is now handled after the recursive call to this function at the end
2206 : : of reference_binding. */
2207 : 43166763 : return conv;
2208 : : }
2209 : :
2210 : : return NULL;
2211 : : }
2212 : :
2213 : : /* Like implicit_conversion, but return NULL if the conversion is bad.
2214 : :
2215 : : This is not static so that check_non_deducible_conversion can call it within
2216 : : add_template_candidate_real as part of overload resolution; it should not be
2217 : : called outside of overload resolution. */
2218 : :
2219 : : conversion *
2220 : 4511557 : good_conversion (tree to, tree from, tree expr,
2221 : : int flags, tsubst_flags_t complain)
2222 : : {
2223 : 4511557 : conversion *c = implicit_conversion (to, from, expr, /*cast*/false,
2224 : : flags, complain);
2225 : 4511557 : if (c && c->bad_p)
2226 : 2020705 : c = NULL;
2227 : 4511557 : return c;
2228 : : }
2229 : :
2230 : : /* Add a new entry to the list of candidates. Used by the add_*_candidate
2231 : : functions. ARGS will not be changed until a single candidate is
2232 : : selected. */
2233 : :
2234 : : static struct z_candidate *
2235 : 699225792 : add_candidate (struct z_candidate **candidates,
2236 : : tree fn, tree first_arg, const vec<tree, va_gc> *args,
2237 : : size_t num_convs, conversion **convs,
2238 : : tree access_path, tree conversion_path,
2239 : : int viable, struct rejection_reason *reason,
2240 : : int flags)
2241 : : {
2242 : 699225792 : struct z_candidate *cand = (struct z_candidate *)
2243 : 699225792 : conversion_obstack_alloc (sizeof (struct z_candidate));
2244 : :
2245 : 699225792 : cand->fn = fn;
2246 : 699225792 : cand->first_arg = first_arg;
2247 : 699225792 : cand->args = args;
2248 : 699225792 : cand->convs = convs;
2249 : 699225792 : cand->num_convs = num_convs;
2250 : 699225792 : cand->access_path = access_path;
2251 : 699225792 : cand->conversion_path = conversion_path;
2252 : 699225792 : cand->viable = viable;
2253 : 699225792 : cand->reason = reason;
2254 : 699225792 : cand->next = *candidates;
2255 : 699225792 : cand->flags = flags;
2256 : 699225792 : *candidates = cand;
2257 : :
2258 : 699225792 : if (convs && cand->reversed ())
2259 : : /* Swap the conversions for comparison in joust; we'll swap them back
2260 : : before build_over_call. */
2261 : 35582917 : std::swap (convs[0], convs[1]);
2262 : :
2263 : 699225792 : return cand;
2264 : : }
2265 : :
2266 : : /* FN is a function from the overload set that we outright didn't even
2267 : : consider (for some reason); add it to the list as an non-viable "ignored"
2268 : : candidate. */
2269 : :
2270 : : static z_candidate *
2271 : 388845842 : add_ignored_candidate (z_candidate **candidates, tree fn)
2272 : : {
2273 : : /* No need to dynamically allocate these. */
2274 : 388845842 : static const rejection_reason reason_ignored = { rr_ignored, {} };
2275 : :
2276 : 388845842 : struct z_candidate *cand = (struct z_candidate *)
2277 : 388845531 : conversion_obstack_alloc (sizeof (struct z_candidate));
2278 : :
2279 : 388845842 : cand->fn = fn;
2280 : 388845842 : cand->reason = const_cast<rejection_reason *> (&reason_ignored);
2281 : 388845842 : cand->next = *candidates;
2282 : 388845842 : *candidates = cand;
2283 : :
2284 : 388845842 : return cand;
2285 : : }
2286 : :
2287 : : /* True iff CAND is a candidate added by add_ignored_candidate. */
2288 : :
2289 : : static bool
2290 : 365769059 : ignored_candidate_p (const z_candidate *cand)
2291 : : {
2292 : 365763064 : return cand->reason && cand->reason->code == rr_ignored;
2293 : : }
2294 : :
2295 : : /* Return the number of remaining arguments in the parameter list
2296 : : beginning with ARG. */
2297 : :
2298 : : int
2299 : 107877002 : remaining_arguments (tree arg)
2300 : : {
2301 : 107877002 : int n;
2302 : :
2303 : 191630861 : for (n = 0; arg != NULL_TREE && arg != void_list_node;
2304 : 83753859 : arg = TREE_CHAIN (arg))
2305 : 83753859 : n++;
2306 : :
2307 : 107877002 : return n;
2308 : : }
2309 : :
2310 : : /* [over.match.copy]: When initializing a temporary object (12.2) to be bound
2311 : : to the first parameter of a constructor where the parameter is of type
2312 : : "reference to possibly cv-qualified T" and the constructor is called with a
2313 : : single argument in the context of direct-initialization of an object of type
2314 : : "cv2 T", explicit conversion functions are also considered.
2315 : :
2316 : : So set LOOKUP_COPY_PARM to let reference_binding know that
2317 : : it's being called in that context. */
2318 : :
2319 : : int
2320 : 294797341 : conv_flags (int i, int nargs, tree fn, tree arg, int flags)
2321 : : {
2322 : 294797341 : int lflags = flags;
2323 : 294797341 : tree t;
2324 : 294510596 : if (i == 0 && nargs == 1 && DECL_CONSTRUCTOR_P (fn)
2325 : 84362769 : && (t = FUNCTION_FIRST_USER_PARMTYPE (fn))
2326 : 379160110 : && (same_type_ignoring_top_level_qualifiers_p
2327 : 84362769 : (non_reference (TREE_VALUE (t)), DECL_CONTEXT (fn))))
2328 : : {
2329 : 65651980 : if (!(flags & LOOKUP_ONLYCONVERTING))
2330 : 20159576 : lflags |= LOOKUP_COPY_PARM;
2331 : 65651980 : if ((flags & LOOKUP_LIST_INIT_CTOR)
2332 : 65651980 : && BRACE_ENCLOSED_INITIALIZER_P (arg))
2333 : 401 : lflags |= LOOKUP_NO_CONVERSION;
2334 : : }
2335 : : else
2336 : 229145361 : lflags |= LOOKUP_ONLYCONVERTING;
2337 : :
2338 : 294797341 : return lflags;
2339 : : }
2340 : :
2341 : : /* Build an appropriate 'this' conversion for the method FN and class
2342 : : type CTYPE from the value ARG (having type ARGTYPE) to the type PARMTYPE.
2343 : : This function modifies PARMTYPE, ARGTYPE and ARG. */
2344 : :
2345 : : static conversion *
2346 : 64950857 : build_this_conversion (tree fn, tree ctype,
2347 : : tree& parmtype, tree& argtype, tree& arg,
2348 : : int flags, tsubst_flags_t complain)
2349 : : {
2350 : 129901714 : gcc_assert (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
2351 : : && !DECL_CONSTRUCTOR_P (fn));
2352 : :
2353 : : /* The type of the implicit object parameter ('this') for
2354 : : overload resolution is not always the same as for the
2355 : : function itself; conversion functions are considered to
2356 : : be members of the class being converted, and functions
2357 : : introduced by a using-declaration are considered to be
2358 : : members of the class that uses them.
2359 : :
2360 : : Since build_over_call ignores the ICS for the `this'
2361 : : parameter, we can just change the parm type. */
2362 : 64950857 : parmtype = cp_build_qualified_type (ctype,
2363 : 64950857 : cp_type_quals (TREE_TYPE (parmtype)));
2364 : 64950857 : bool this_p = true;
2365 : 64950857 : if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn)))
2366 : : {
2367 : : /* If the function has a ref-qualifier, the implicit
2368 : : object parameter has reference type. */
2369 : 63467 : bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn));
2370 : 63467 : parmtype = cp_build_reference_type (parmtype, rv);
2371 : : /* The special handling of 'this' conversions in compare_ics
2372 : : does not apply if there is a ref-qualifier. */
2373 : 63467 : this_p = false;
2374 : : }
2375 : : else
2376 : : {
2377 : 64887390 : parmtype = build_pointer_type (parmtype);
2378 : : /* We don't use build_this here because we don't want to
2379 : : capture the object argument until we've chosen a
2380 : : non-static member function. */
2381 : 64887390 : arg = build_address (arg);
2382 : 64887390 : argtype = lvalue_type (arg);
2383 : : }
2384 : 64950857 : flags |= LOOKUP_ONLYCONVERTING;
2385 : 64950857 : conversion *t = implicit_conversion (parmtype, argtype, arg,
2386 : : /*c_cast_p=*/false, flags, complain);
2387 : 64950857 : t->this_p = this_p;
2388 : 64950857 : return t;
2389 : : }
2390 : :
2391 : : /* Create an overload candidate for the function or method FN called
2392 : : with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
2393 : : FLAGS is passed on to implicit_conversion.
2394 : :
2395 : : This does not change ARGS.
2396 : :
2397 : : CTYPE, if non-NULL, is the type we want to pretend this function
2398 : : comes from for purposes of overload resolution.
2399 : :
2400 : : SHORTCUT_BAD_CONVS controls how we handle "bad" argument conversions.
2401 : : If true, we stop computing conversions upon seeing the first bad
2402 : : conversion. This is used by add_candidates to avoid computing
2403 : : more conversions than necessary in the presence of a strictly viable
2404 : : candidate, while preserving the defacto behavior of overload resolution
2405 : : when it turns out there are only non-strictly viable candidates. */
2406 : :
2407 : : static struct z_candidate *
2408 : 417709850 : add_function_candidate (struct z_candidate **candidates,
2409 : : tree fn, tree ctype, tree first_arg,
2410 : : const vec<tree, va_gc> *args, tree access_path,
2411 : : tree conversion_path, int flags,
2412 : : conversion **convs,
2413 : : bool shortcut_bad_convs,
2414 : : tsubst_flags_t complain)
2415 : : {
2416 : 417709850 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
2417 : 417709850 : int i, len;
2418 : 417709850 : tree parmnode;
2419 : 417709850 : tree orig_first_arg = first_arg;
2420 : 417709850 : int skip;
2421 : 417709850 : int viable = 1;
2422 : 417709850 : struct rejection_reason *reason = NULL;
2423 : :
2424 : : /* The `this', `in_chrg' and VTT arguments to constructors are not
2425 : : considered in overload resolution. */
2426 : 835419700 : if (DECL_CONSTRUCTOR_P (fn))
2427 : : {
2428 : 179966209 : if (ctor_omit_inherited_parms (fn))
2429 : : /* Bring back parameters omitted from an inherited ctor. */
2430 : 150 : parmlist = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
2431 : : else
2432 : 179966134 : parmlist = skip_artificial_parms_for (fn, parmlist);
2433 : 179966209 : skip = num_artificial_parms_for (fn);
2434 : 179966209 : if (skip > 0 && first_arg != NULL_TREE)
2435 : : {
2436 : 179966209 : --skip;
2437 : 179966209 : first_arg = NULL_TREE;
2438 : : }
2439 : : }
2440 : : else
2441 : : skip = 0;
2442 : :
2443 : 806893155 : len = vec_safe_length (args) - skip + (first_arg != NULL_TREE ? 1 : 0);
2444 : 417709850 : if (!convs)
2445 : 366342843 : convs = alloc_conversions (len);
2446 : :
2447 : : /* 13.3.2 - Viable functions [over.match.viable]
2448 : : First, to be a viable function, a candidate function shall have enough
2449 : : parameters to agree in number with the arguments in the list.
2450 : :
2451 : : We need to check this first; otherwise, checking the ICSes might cause
2452 : : us to produce an ill-formed template instantiation. */
2453 : :
2454 : 417709850 : parmnode = parmlist;
2455 : 864475471 : for (i = 0; i < len; ++i)
2456 : : {
2457 : 498128670 : if (parmnode == NULL_TREE || parmnode == void_list_node)
2458 : : break;
2459 : 446765621 : parmnode = TREE_CHAIN (parmnode);
2460 : : }
2461 : :
2462 : 417709850 : if ((i < len && parmnode)
2463 : 417709850 : || !sufficient_parms_p (parmnode))
2464 : : {
2465 : 107874759 : int remaining = remaining_arguments (parmnode);
2466 : 107874759 : viable = 0;
2467 : 107874759 : reason = arity_rejection (first_arg, i + remaining, len);
2468 : : }
2469 : :
2470 : : /* An inherited constructor (12.6.3 [class.inhctor.init]) that has a first
2471 : : parameter of type "reference to cv C" (including such a constructor
2472 : : instantiated from a template) is excluded from the set of candidate
2473 : : functions when used to construct an object of type D with an argument list
2474 : : containing a single argument if C is reference-related to D. */
2475 : 374040516 : if (viable && len == 1 && parmlist && DECL_CONSTRUCTOR_P (fn)
2476 : 85648254 : && flag_new_inheriting_ctors
2477 : 503349419 : && DECL_INHERITED_CTOR (fn))
2478 : : {
2479 : 550513 : tree ptype = non_reference (TREE_VALUE (parmlist));
2480 : 550513 : tree dtype = DECL_CONTEXT (fn);
2481 : 1101026 : tree btype = DECL_INHERITED_CTOR_BASE (fn);
2482 : 550513 : if (reference_related_p (ptype, dtype)
2483 : 550513 : && reference_related_p (btype, ptype))
2484 : : {
2485 : 373418 : viable = false;
2486 : 373418 : reason = inherited_ctor_rejection ();
2487 : : }
2488 : : }
2489 : :
2490 : : /* Second, for a function to be viable, its constraints must be
2491 : : satisfied. */
2492 : 417709850 : if (flag_concepts && viable && !constraints_satisfied_p (fn))
2493 : : {
2494 : 54195 : reason = constraint_failure ();
2495 : 54195 : viable = false;
2496 : : }
2497 : :
2498 : : /* When looking for a function from a subobject from an implicit
2499 : : copy/move constructor/operator=, don't consider anything that takes (a
2500 : : reference to) an unrelated type. See c++/44909 and core 1092. */
2501 : 417709850 : if (viable && parmlist && (flags & LOOKUP_DEFAULTED))
2502 : : {
2503 : 57593914 : if (DECL_CONSTRUCTOR_P (fn))
2504 : : i = 1;
2505 : 15056018 : else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2506 : 15056018 : && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
2507 : : i = 2;
2508 : : else
2509 : : i = 0;
2510 : 28796957 : if (i && len == i)
2511 : : {
2512 : 15291507 : parmnode = chain_index (i-1, parmlist);
2513 : 15291507 : if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
2514 : : ctype))
2515 : 3070479 : viable = 0;
2516 : : }
2517 : :
2518 : : /* This only applies at the top level. */
2519 : 25726478 : flags &= ~LOOKUP_DEFAULTED;
2520 : : }
2521 : :
2522 : 417709850 : if (! viable)
2523 : 111372851 : goto out;
2524 : :
2525 : 306336999 : if (shortcut_bad_convs)
2526 : 306270714 : flags |= LOOKUP_SHORTCUT_BAD_CONVS;
2527 : : else
2528 : 66285 : flags &= ~LOOKUP_SHORTCUT_BAD_CONVS;
2529 : :
2530 : : /* Third, for F to be a viable function, there shall exist for each
2531 : : argument an implicit conversion sequence that converts that argument
2532 : : to the corresponding parameter of F. */
2533 : :
2534 : 306336999 : parmnode = parmlist;
2535 : :
2536 : 544310837 : for (i = 0; i < len; ++i)
2537 : : {
2538 : 343684192 : tree argtype, to_type;
2539 : 343684192 : tree arg;
2540 : :
2541 : 343684192 : if (parmnode == void_list_node)
2542 : : break;
2543 : :
2544 : 343684192 : if (convs[i])
2545 : : {
2546 : : /* Already set during deduction. */
2547 : 4296285 : parmnode = TREE_CHAIN (parmnode);
2548 : 4296285 : continue;
2549 : : }
2550 : :
2551 : 339387907 : if (i == 0 && first_arg != NULL_TREE)
2552 : 62466616 : arg = first_arg;
2553 : : else
2554 : 529368921 : arg = CONST_CAST_TREE (
2555 : : (*args)[i + skip - (first_arg != NULL_TREE ? 1 : 0)]);
2556 : 339387907 : argtype = lvalue_type (arg);
2557 : :
2558 : 339387907 : conversion *t;
2559 : 339387907 : if (parmnode)
2560 : : {
2561 : 334822552 : tree parmtype = TREE_VALUE (parmnode);
2562 : 334822552 : if (i == 0
2563 : 264384093 : && DECL_IOBJ_MEMBER_FUNCTION_P (fn)
2564 : 629346160 : && !DECL_CONSTRUCTOR_P (fn))
2565 : 62459047 : t = build_this_conversion (fn, ctype, parmtype, argtype, arg,
2566 : : flags, complain);
2567 : : else
2568 : : {
2569 : 272363505 : int lflags = conv_flags (i, len-skip, fn, arg, flags);
2570 : 272363505 : t = implicit_conversion (parmtype, argtype, arg,
2571 : : /*c_cast_p=*/false, lflags, complain);
2572 : : }
2573 : 334822552 : to_type = parmtype;
2574 : 334822552 : parmnode = TREE_CHAIN (parmnode);
2575 : : }
2576 : : else
2577 : : {
2578 : 4565355 : t = build_identity_conv (argtype, arg);
2579 : 4565355 : t->ellipsis_p = true;
2580 : 4565355 : to_type = argtype;
2581 : : }
2582 : :
2583 : 339387907 : convs[i] = t;
2584 : 339387907 : if (! t)
2585 : : {
2586 : 90921355 : viable = 0;
2587 : 90921355 : reason = arg_conversion_rejection (first_arg, i, argtype, to_type,
2588 : 90921355 : EXPR_LOCATION (arg));
2589 : 90921355 : break;
2590 : : }
2591 : :
2592 : 248466552 : if (t->bad_p)
2593 : : {
2594 : 14828552 : viable = -1;
2595 : 14828552 : reason = bad_arg_conversion_rejection (first_arg, i, arg, to_type,
2596 : 14828552 : EXPR_LOCATION (arg));
2597 : 14828552 : if (shortcut_bad_convs)
2598 : : break;
2599 : : }
2600 : : }
2601 : :
2602 : 200626645 : out:
2603 : 417709850 : return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
2604 : 417709850 : access_path, conversion_path, viable, reason, flags);
2605 : : }
2606 : :
2607 : : /* Create an overload candidate for the conversion function FN which will
2608 : : be invoked for expression OBJ, producing a pointer-to-function which
2609 : : will in turn be called with the argument list FIRST_ARG/ARGLIST,
2610 : : and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
2611 : : passed on to implicit_conversion.
2612 : :
2613 : : Actually, we don't really care about FN; we care about the type it
2614 : : converts to. There may be multiple conversion functions that will
2615 : : convert to that type, and we rely on build_user_type_conversion_1 to
2616 : : choose the best one; so when we create our candidate, we record the type
2617 : : instead of the function. */
2618 : :
2619 : : static struct z_candidate *
2620 : 18489 : add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
2621 : : const vec<tree, va_gc> *arglist,
2622 : : tree access_path, tree conversion_path,
2623 : : tsubst_flags_t complain)
2624 : : {
2625 : 18489 : tree totype = TREE_TYPE (TREE_TYPE (fn));
2626 : 18489 : int i, len, viable, flags;
2627 : 18489 : tree parmlist, parmnode;
2628 : 18489 : conversion **convs;
2629 : 18489 : struct rejection_reason *reason;
2630 : :
2631 : 37033 : for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
2632 : 18544 : parmlist = TREE_TYPE (parmlist);
2633 : 18489 : parmlist = TYPE_ARG_TYPES (parmlist);
2634 : :
2635 : 18489 : len = vec_safe_length (arglist) + 1;
2636 : 18489 : convs = alloc_conversions (len);
2637 : 18489 : parmnode = parmlist;
2638 : 18489 : viable = 1;
2639 : 18489 : flags = LOOKUP_IMPLICIT;
2640 : 18489 : reason = NULL;
2641 : :
2642 : : /* Don't bother looking up the same type twice. */
2643 : 18489 : if (*candidates && (*candidates)->fn == totype)
2644 : : return NULL;
2645 : :
2646 : 18474 : if (!constraints_satisfied_p (fn))
2647 : : {
2648 : 9 : reason = constraint_failure ();
2649 : 9 : viable = 0;
2650 : 9 : return add_candidate (candidates, fn, obj, arglist, len, convs,
2651 : 9 : access_path, conversion_path, viable, reason, flags);
2652 : : }
2653 : :
2654 : 62486 : for (i = 0; i < len; ++i)
2655 : : {
2656 : 44097 : tree arg, argtype, convert_type = NULL_TREE;
2657 : 44097 : conversion *t;
2658 : :
2659 : 44097 : if (i == 0)
2660 : : arg = obj;
2661 : : else
2662 : 25632 : arg = (*arglist)[i - 1];
2663 : 44097 : argtype = lvalue_type (arg);
2664 : :
2665 : 44097 : if (i == 0)
2666 : : {
2667 : 18465 : t = build_identity_conv (argtype, NULL_TREE);
2668 : 18465 : t = build_conv (ck_user, totype, t);
2669 : : /* Leave the 'cand' field null; we'll figure out the conversion in
2670 : : convert_like if this candidate is chosen. */
2671 : 18465 : convert_type = totype;
2672 : : }
2673 : 25632 : else if (parmnode == void_list_node)
2674 : : break;
2675 : 25600 : else if (parmnode)
2676 : : {
2677 : 25591 : t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
2678 : : /*c_cast_p=*/false, flags, complain);
2679 : 25591 : convert_type = TREE_VALUE (parmnode);
2680 : : }
2681 : : else
2682 : : {
2683 : 9 : t = build_identity_conv (argtype, arg);
2684 : 9 : t->ellipsis_p = true;
2685 : 9 : convert_type = argtype;
2686 : : }
2687 : :
2688 : 44065 : convs[i] = t;
2689 : 44065 : if (! t)
2690 : : break;
2691 : :
2692 : 44021 : if (t->bad_p)
2693 : : {
2694 : 20 : viable = -1;
2695 : 60 : reason = bad_arg_conversion_rejection (NULL_TREE, i, arg, convert_type,
2696 : 20 : EXPR_LOCATION (arg));
2697 : : }
2698 : :
2699 : 44021 : if (i == 0)
2700 : 18465 : continue;
2701 : :
2702 : 25556 : if (parmnode)
2703 : 25547 : parmnode = TREE_CHAIN (parmnode);
2704 : : }
2705 : :
2706 : 18465 : if (i < len
2707 : 18465 : || ! sufficient_parms_p (parmnode))
2708 : : {
2709 : 104 : int remaining = remaining_arguments (parmnode);
2710 : 104 : viable = 0;
2711 : 104 : reason = arity_rejection (NULL_TREE, i + remaining, len);
2712 : : }
2713 : :
2714 : 18465 : return add_candidate (candidates, totype, obj, arglist, len, convs,
2715 : 18465 : access_path, conversion_path, viable, reason, flags);
2716 : : }
2717 : :
2718 : : static void
2719 : 7936628 : build_builtin_candidate (struct z_candidate **candidates, tree fnname,
2720 : : tree type1, tree type2, const vec<tree,va_gc> &args,
2721 : : tree *argtypes, int flags, tsubst_flags_t complain)
2722 : : {
2723 : 7936628 : conversion *t;
2724 : 7936628 : conversion **convs;
2725 : 7936628 : size_t num_convs;
2726 : 7936628 : int viable = 1;
2727 : 7936628 : tree types[2];
2728 : 7936628 : struct rejection_reason *reason = NULL;
2729 : :
2730 : 7936628 : types[0] = type1;
2731 : 7936628 : types[1] = type2;
2732 : :
2733 : 7936628 : num_convs = args.length ();
2734 : 7936628 : convs = alloc_conversions (num_convs);
2735 : :
2736 : : /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2737 : : conversion ops are allowed. We handle that here by just checking for
2738 : : boolean_type_node because other operators don't ask for it. COND_EXPR
2739 : : also does contextual conversion to bool for the first operand, but we
2740 : : handle that in build_conditional_expr, and type1 here is operand 2. */
2741 : 7936628 : if (type1 != boolean_type_node)
2742 : 7400176 : flags |= LOOKUP_ONLYCONVERTING;
2743 : :
2744 : 23205508 : for (unsigned i = 0; i < 2 && i < num_convs; ++i)
2745 : : {
2746 : 15268880 : t = implicit_conversion (types[i], argtypes[i], args[i],
2747 : : /*c_cast_p=*/false, flags, complain);
2748 : 15268880 : if (! t)
2749 : : {
2750 : 495289 : viable = 0;
2751 : : /* We need something for printing the candidate. */
2752 : 495289 : t = build_identity_conv (types[i], NULL_TREE);
2753 : 495289 : reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2754 : 495289 : types[i], EXPR_LOCATION (args[i]));
2755 : : }
2756 : 14773591 : else if (t->bad_p)
2757 : : {
2758 : 107 : viable = 0;
2759 : 107 : reason = bad_arg_conversion_rejection (NULL_TREE, i, args[i],
2760 : : types[i],
2761 : 107 : EXPR_LOCATION (args[i]));
2762 : : }
2763 : 15268880 : convs[i] = t;
2764 : : }
2765 : :
2766 : : /* For COND_EXPR we rearranged the arguments; undo that now. */
2767 : 7936628 : if (num_convs == 3)
2768 : : {
2769 : 29 : convs[2] = convs[1];
2770 : 29 : convs[1] = convs[0];
2771 : 29 : t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
2772 : : /*c_cast_p=*/false, flags,
2773 : : complain);
2774 : 29 : if (t)
2775 : 29 : convs[0] = t;
2776 : : else
2777 : : {
2778 : 0 : viable = 0;
2779 : 0 : reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2],
2780 : : boolean_type_node,
2781 : 0 : EXPR_LOCATION (args[2]));
2782 : : }
2783 : : }
2784 : :
2785 : 7936628 : add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
2786 : : num_convs, convs,
2787 : : /*access_path=*/NULL_TREE,
2788 : : /*conversion_path=*/NULL_TREE,
2789 : : viable, reason, flags);
2790 : 7936628 : }
2791 : :
2792 : : static bool
2793 : 6 : is_complete (tree t)
2794 : : {
2795 : 6 : return COMPLETE_TYPE_P (complete_type (t));
2796 : : }
2797 : :
2798 : : /* Returns nonzero if TYPE is a promoted arithmetic type. */
2799 : :
2800 : : static bool
2801 : 2399 : promoted_arithmetic_type_p (tree type)
2802 : : {
2803 : : /* [over.built]
2804 : :
2805 : : In this section, the term promoted integral type is used to refer
2806 : : to those integral types which are preserved by integral promotion
2807 : : (including e.g. int and long but excluding e.g. char).
2808 : : Similarly, the term promoted arithmetic type refers to promoted
2809 : : integral types plus floating types. */
2810 : 2399 : return ((CP_INTEGRAL_TYPE_P (type)
2811 : 105 : && same_type_p (type_promotes_to (type), type))
2812 : 2399 : || SCALAR_FLOAT_TYPE_P (type));
2813 : : }
2814 : :
2815 : : /* Create any builtin operator overload candidates for the operator in
2816 : : question given the converted operand types TYPE1 and TYPE2. The other
2817 : : args are passed through from add_builtin_candidates to
2818 : : build_builtin_candidate.
2819 : :
2820 : : TYPE1 and TYPE2 may not be permissible, and we must filter them.
2821 : : If CODE is requires candidates operands of the same type of the kind
2822 : : of which TYPE1 and TYPE2 are, we add both candidates
2823 : : CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2824 : :
2825 : : static void
2826 : 11257387 : add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
2827 : : enum tree_code code2, tree fnname, tree type1,
2828 : : tree type2, vec<tree,va_gc> &args, tree *argtypes,
2829 : : int flags, tsubst_flags_t complain)
2830 : : {
2831 : 11257387 : switch (code)
2832 : : {
2833 : 21 : case POSTINCREMENT_EXPR:
2834 : 21 : case POSTDECREMENT_EXPR:
2835 : 21 : args[1] = integer_zero_node;
2836 : 21 : type2 = integer_type_node;
2837 : 21 : break;
2838 : : default:
2839 : : break;
2840 : : }
2841 : :
2842 : 11257387 : switch (code)
2843 : : {
2844 : :
2845 : : /* 4 For every pair (T, VQ), where T is an arithmetic type other than bool,
2846 : : and VQ is either volatile or empty, there exist candidate operator
2847 : : functions of the form
2848 : : VQ T& operator++(VQ T&);
2849 : : T operator++(VQ T&, int);
2850 : : 5 For every pair (T, VQ), where T is an arithmetic type other than bool,
2851 : : and VQ is either volatile or empty, there exist candidate operator
2852 : : functions of the form
2853 : : VQ T& operator--(VQ T&);
2854 : : T operator--(VQ T&, int);
2855 : : 6 For every pair (T, VQ), where T is a cv-qualified or cv-unqualified object
2856 : : type, and VQ is either volatile or empty, there exist candidate operator
2857 : : functions of the form
2858 : : T*VQ& operator++(T*VQ&);
2859 : : T*VQ& operator--(T*VQ&);
2860 : : T* operator++(T*VQ&, int);
2861 : : T* operator--(T*VQ&, int); */
2862 : :
2863 : 18 : case POSTDECREMENT_EXPR:
2864 : 18 : case PREDECREMENT_EXPR:
2865 : 18 : if (TREE_CODE (type1) == BOOLEAN_TYPE)
2866 : : return;
2867 : : /* FALLTHRU */
2868 : 39 : case POSTINCREMENT_EXPR:
2869 : 39 : case PREINCREMENT_EXPR:
2870 : : /* P0002R1, Remove deprecated operator++(bool) added "other than bool"
2871 : : to p4. */
2872 : 39 : if (TREE_CODE (type1) == BOOLEAN_TYPE && cxx_dialect >= cxx17)
2873 : : return;
2874 : 33 : if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
2875 : : {
2876 : 24 : type1 = build_reference_type (type1);
2877 : 24 : break;
2878 : : }
2879 : : return;
2880 : :
2881 : : /* 7 For every cv-qualified or cv-unqualified object type T, there
2882 : : exist candidate operator functions of the form
2883 : :
2884 : : T& operator*(T*);
2885 : :
2886 : :
2887 : : 8 For every function type T that does not have cv-qualifiers or
2888 : : a ref-qualifier, there exist candidate operator functions of the form
2889 : : T& operator*(T*); */
2890 : :
2891 : 88345 : case INDIRECT_REF:
2892 : 88345 : if (TYPE_PTR_P (type1)
2893 : 88345 : && (TYPE_PTROB_P (type1)
2894 : 13 : || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
2895 : : break;
2896 : : return;
2897 : :
2898 : : /* 9 For every type T, there exist candidate operator functions of the form
2899 : : T* operator+(T*);
2900 : :
2901 : : 10 For every floating-point or promoted integral type T, there exist
2902 : : candidate operator functions of the form
2903 : : T operator+(T);
2904 : : T operator-(T); */
2905 : :
2906 : 201 : case UNARY_PLUS_EXPR: /* unary + */
2907 : 201 : if (TYPE_PTR_P (type1))
2908 : : break;
2909 : : /* FALLTHRU */
2910 : 43575 : case NEGATE_EXPR:
2911 : 43575 : if (ARITHMETIC_TYPE_P (type1))
2912 : : break;
2913 : : return;
2914 : :
2915 : : /* 11 For every promoted integral type T, there exist candidate operator
2916 : : functions of the form
2917 : : T operator~(T); */
2918 : :
2919 : 114504 : case BIT_NOT_EXPR:
2920 : 114504 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
2921 : : break;
2922 : : return;
2923 : :
2924 : : /* 12 For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, C1
2925 : : is the same type as C2 or is a derived class of C2, and T is an object
2926 : : type or a function type there exist candidate operator functions of the
2927 : : form
2928 : : CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
2929 : : where CV12 is the union of CV1 and CV2. */
2930 : :
2931 : 22 : case MEMBER_REF:
2932 : 22 : if (TYPE_PTR_P (type1) && TYPE_PTRMEM_P (type2))
2933 : : {
2934 : 22 : tree c1 = TREE_TYPE (type1);
2935 : 22 : tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
2936 : :
2937 : 19 : if (CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
2938 : 38 : && (TYPE_PTRMEMFUNC_P (type2)
2939 : 6 : || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
2940 : : break;
2941 : : }
2942 : : return;
2943 : :
2944 : : /* 13 For every pair of types L and R, where each of L and R is a floating-point
2945 : : or promoted integral type, there exist candidate operator functions of the
2946 : : form
2947 : : LR operator*(L, R);
2948 : : LR operator/(L, R);
2949 : : LR operator+(L, R);
2950 : : LR operator-(L, R);
2951 : : bool operator<(L, R);
2952 : : bool operator>(L, R);
2953 : : bool operator<=(L, R);
2954 : : bool operator>=(L, R);
2955 : : bool operator==(L, R);
2956 : : bool operator!=(L, R);
2957 : : where LR is the result of the usual arithmetic conversions between
2958 : : types L and R.
2959 : :
2960 : : 14 For every integral type T there exists a candidate operator function of
2961 : : the form
2962 : :
2963 : : std::strong_ordering operator<=>(T, T);
2964 : :
2965 : : 15 For every pair of floating-point types L and R, there exists a candidate
2966 : : operator function of the form
2967 : :
2968 : : std::partial_ordering operator<=>(L, R);
2969 : :
2970 : : 16 For every cv-qualified or cv-unqualified object type T there exist
2971 : : candidate operator functions of the form
2972 : : T* operator+(T*, std::ptrdiff_t);
2973 : : T& operator[](T*, std::ptrdiff_t);
2974 : : T* operator-(T*, std::ptrdiff_t);
2975 : : T* operator+(std::ptrdiff_t, T*);
2976 : : T& operator[](std::ptrdiff_t, T*);
2977 : :
2978 : : 17 For every T, where T is a pointer to object type, there exist candidate
2979 : : operator functions of the form
2980 : : std::ptrdiff_t operator-(T, T);
2981 : :
2982 : : 18 For every T, where T is an enumeration type or a pointer type, there
2983 : : exist candidate operator functions of the form
2984 : : bool operator<(T, T);
2985 : : bool operator>(T, T);
2986 : : bool operator<=(T, T);
2987 : : bool operator>=(T, T);
2988 : : bool operator==(T, T);
2989 : : bool operator!=(T, T);
2990 : : R operator<=>(T, T);
2991 : :
2992 : : where R is the result type specified in [expr.spaceship].
2993 : :
2994 : : 19 For every T, where T is a pointer-to-member type or std::nullptr_t,
2995 : : there exist candidate operator functions of the form
2996 : : bool operator==(T, T);
2997 : : bool operator!=(T, T); */
2998 : :
2999 : 140885 : case MINUS_EXPR:
3000 : 140885 : if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
3001 : : break;
3002 : 17 : if (TYPE_PTROB_P (type1)
3003 : 140876 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3004 : : {
3005 : 7 : type2 = ptrdiff_type_node;
3006 : 7 : break;
3007 : : }
3008 : : /* FALLTHRU */
3009 : 286238 : case MULT_EXPR:
3010 : 286238 : case TRUNC_DIV_EXPR:
3011 : 286238 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3012 : : break;
3013 : : return;
3014 : :
3015 : : /* This isn't exactly what's specified above for operator<=>, but it's
3016 : : close enough. In particular, we don't care about the return type
3017 : : specified above; it doesn't participate in overload resolution and it
3018 : : doesn't affect the semantics of the built-in operator. */
3019 : 6949371 : case SPACESHIP_EXPR:
3020 : 6949371 : case EQ_EXPR:
3021 : 6949371 : case NE_EXPR:
3022 : 86656 : if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
3023 : 7036027 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)))
3024 : : break;
3025 : 6949348 : if (NULLPTR_TYPE_P (type1) && NULLPTR_TYPE_P (type2))
3026 : : break;
3027 : 6949342 : if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (args[1]))
3028 : : {
3029 : : type2 = type1;
3030 : : break;
3031 : : }
3032 : 6949339 : if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (args[0]))
3033 : : {
3034 : : type1 = type2;
3035 : : break;
3036 : : }
3037 : : /* Fall through. */
3038 : 7301186 : case LT_EXPR:
3039 : 7301186 : case GT_EXPR:
3040 : 7301186 : case LE_EXPR:
3041 : 7301186 : case GE_EXPR:
3042 : 7301186 : case MAX_EXPR:
3043 : 7301186 : case MIN_EXPR:
3044 : 7301186 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3045 : : break;
3046 : 5520894 : if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3047 : : break;
3048 : 5520580 : if (TREE_CODE (type1) == ENUMERAL_TYPE
3049 : 3740966 : && TREE_CODE (type2) == ENUMERAL_TYPE)
3050 : : break;
3051 : 3266286 : if (TYPE_PTR_P (type1)
3052 : 3266286 : && null_ptr_cst_p (args[1]))
3053 : : {
3054 : : type2 = type1;
3055 : : break;
3056 : : }
3057 : 3266267 : if (null_ptr_cst_p (args[0])
3058 : 3266267 : && TYPE_PTR_P (type2))
3059 : : {
3060 : : type1 = type2;
3061 : : break;
3062 : : }
3063 : : return;
3064 : :
3065 : 175539 : case PLUS_EXPR:
3066 : 175539 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3067 : : break;
3068 : : /* FALLTHRU */
3069 : 136949 : case ARRAY_REF:
3070 : 136949 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
3071 : : {
3072 : 5 : type1 = ptrdiff_type_node;
3073 : 5 : break;
3074 : : }
3075 : 136944 : if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3076 : : {
3077 : 35335 : type2 = ptrdiff_type_node;
3078 : 35335 : break;
3079 : : }
3080 : : return;
3081 : :
3082 : : /* 18For every pair of promoted integral types L and R, there exist candi-
3083 : : date operator functions of the form
3084 : : LR operator%(L, R);
3085 : : LR operator&(L, R);
3086 : : LR operator^(L, R);
3087 : : LR operator|(L, R);
3088 : : L operator<<(L, R);
3089 : : L operator>>(L, R);
3090 : : where LR is the result of the usual arithmetic conversions between
3091 : : types L and R. */
3092 : :
3093 : 2232953 : case TRUNC_MOD_EXPR:
3094 : 2232953 : case BIT_AND_EXPR:
3095 : 2232953 : case BIT_IOR_EXPR:
3096 : 2232953 : case BIT_XOR_EXPR:
3097 : 2232953 : case LSHIFT_EXPR:
3098 : 2232953 : case RSHIFT_EXPR:
3099 : 2232953 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3100 : : break;
3101 : : return;
3102 : :
3103 : : /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
3104 : : type, VQ is either volatile or empty, and R is a promoted arithmetic
3105 : : type, there exist candidate operator functions of the form
3106 : : VQ L& operator=(VQ L&, R);
3107 : : VQ L& operator*=(VQ L&, R);
3108 : : VQ L& operator/=(VQ L&, R);
3109 : : VQ L& operator+=(VQ L&, R);
3110 : : VQ L& operator-=(VQ L&, R);
3111 : :
3112 : : 20For every pair T, VQ), where T is any type and VQ is either volatile
3113 : : or empty, there exist candidate operator functions of the form
3114 : : T*VQ& operator=(T*VQ&, T*);
3115 : :
3116 : : 21For every pair T, VQ), where T is a pointer to member type and VQ is
3117 : : either volatile or empty, there exist candidate operator functions of
3118 : : the form
3119 : : VQ T& operator=(VQ T&, T);
3120 : :
3121 : : 22For every triple T, VQ, I), where T is a cv-qualified or cv-
3122 : : unqualified complete object type, VQ is either volatile or empty, and
3123 : : I is a promoted integral type, there exist candidate operator func-
3124 : : tions of the form
3125 : : T*VQ& operator+=(T*VQ&, I);
3126 : : T*VQ& operator-=(T*VQ&, I);
3127 : :
3128 : : 23For every triple L, VQ, R), where L is an integral or enumeration
3129 : : type, VQ is either volatile or empty, and R is a promoted integral
3130 : : type, there exist candidate operator functions of the form
3131 : :
3132 : : VQ L& operator%=(VQ L&, R);
3133 : : VQ L& operator<<=(VQ L&, R);
3134 : : VQ L& operator>>=(VQ L&, R);
3135 : : VQ L& operator&=(VQ L&, R);
3136 : : VQ L& operator^=(VQ L&, R);
3137 : : VQ L& operator|=(VQ L&, R); */
3138 : :
3139 : 985340 : case MODIFY_EXPR:
3140 : 985340 : switch (code2)
3141 : : {
3142 : 210 : case PLUS_EXPR:
3143 : 210 : case MINUS_EXPR:
3144 : 210 : if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3145 : : {
3146 : 0 : type2 = ptrdiff_type_node;
3147 : 0 : break;
3148 : : }
3149 : : /* FALLTHRU */
3150 : 215 : case MULT_EXPR:
3151 : 215 : case TRUNC_DIV_EXPR:
3152 : 215 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3153 : : break;
3154 : : return;
3155 : :
3156 : 985125 : case TRUNC_MOD_EXPR:
3157 : 985125 : case BIT_AND_EXPR:
3158 : 985125 : case BIT_IOR_EXPR:
3159 : 985125 : case BIT_XOR_EXPR:
3160 : 985125 : case LSHIFT_EXPR:
3161 : 985125 : case RSHIFT_EXPR:
3162 : 985125 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3163 : : break;
3164 : : return;
3165 : :
3166 : 0 : case NOP_EXPR:
3167 : 0 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3168 : : break;
3169 : 0 : if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
3170 : 0 : || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3171 : 0 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
3172 : 0 : || ((TYPE_PTRMEMFUNC_P (type1)
3173 : 0 : || TYPE_PTR_P (type1))
3174 : 0 : && null_ptr_cst_p (args[1])))
3175 : : {
3176 : : type2 = type1;
3177 : : break;
3178 : : }
3179 : : return;
3180 : :
3181 : 0 : default:
3182 : 0 : gcc_unreachable ();
3183 : : }
3184 : 985273 : type1 = build_reference_type (type1);
3185 : 985273 : break;
3186 : :
3187 : 2308 : case COND_EXPR:
3188 : : /* [over.built]
3189 : :
3190 : : For every pair of promoted arithmetic types L and R, there
3191 : : exist candidate operator functions of the form
3192 : :
3193 : : LR operator?(bool, L, R);
3194 : :
3195 : : where LR is the result of the usual arithmetic conversions
3196 : : between types L and R.
3197 : :
3198 : : For every type T, where T is a pointer or pointer-to-member
3199 : : type, there exist candidate operator functions of the form T
3200 : : operator?(bool, T, T); */
3201 : :
3202 : 2308 : if (promoted_arithmetic_type_p (type1)
3203 : 2308 : && promoted_arithmetic_type_p (type2))
3204 : : /* That's OK. */
3205 : : break;
3206 : :
3207 : : /* Otherwise, the types should be pointers. */
3208 : 2294 : if (!TYPE_PTR_OR_PTRMEM_P (type1) || !TYPE_PTR_OR_PTRMEM_P (type2))
3209 : : return;
3210 : :
3211 : : /* We don't check that the two types are the same; the logic
3212 : : below will actually create two candidates; one in which both
3213 : : parameter types are TYPE1, and one in which both parameter
3214 : : types are TYPE2. */
3215 : : break;
3216 : :
3217 : 3 : case REALPART_EXPR:
3218 : 3 : case IMAGPART_EXPR:
3219 : 3 : if (ARITHMETIC_TYPE_P (type1))
3220 : : break;
3221 : : return;
3222 : :
3223 : 0 : default:
3224 : 0 : gcc_unreachable ();
3225 : : }
3226 : :
3227 : : /* Make sure we don't create builtin candidates with dependent types. */
3228 : 7400049 : bool u1 = uses_template_parms (type1);
3229 : 7400049 : bool u2 = type2 ? uses_template_parms (type2) : false;
3230 : 7400035 : if (u1 || u2)
3231 : : {
3232 : : /* Try to recover if one of the types is non-dependent. But if
3233 : : there's only one type, there's nothing we can do. */
3234 : 20 : if (!type2)
3235 : : return;
3236 : : /* And we lose if both are dependent. */
3237 : 17 : if (u1 && u2)
3238 : : return;
3239 : : /* Or if they have different forms. */
3240 : 9 : if (TREE_CODE (type1) != TREE_CODE (type2))
3241 : : return;
3242 : :
3243 : 9 : if (u1 && !u2)
3244 : : type1 = type2;
3245 : 6 : else if (u2 && !u1)
3246 : 7400038 : type2 = type1;
3247 : : }
3248 : :
3249 : : /* If we're dealing with two pointer types or two enumeral types,
3250 : : we need candidates for both of them. */
3251 : 7268860 : if (type2 && !same_type_p (type1, type2)
3252 : 1296938 : && TREE_CODE (type1) == TREE_CODE (type2)
3253 : 7676112 : && (TYPE_REF_P (type1)
3254 : 276074 : || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3255 : 275983 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
3256 : 275971 : || TYPE_PTRMEMFUNC_P (type1)
3257 : 275971 : || MAYBE_CLASS_TYPE_P (type1)
3258 : 275971 : || TREE_CODE (type1) == ENUMERAL_TYPE))
3259 : : {
3260 : 197 : if (TYPE_PTR_OR_PTRMEM_P (type1))
3261 : : {
3262 : 103 : tree cptype = composite_pointer_type (input_location,
3263 : : type1, type2,
3264 : : error_mark_node,
3265 : : error_mark_node,
3266 : : CPO_CONVERSION,
3267 : : tf_none);
3268 : 103 : if (cptype != error_mark_node)
3269 : : {
3270 : 59 : build_builtin_candidate
3271 : 59 : (candidates, fnname, cptype, cptype, args, argtypes,
3272 : : flags, complain);
3273 : 59 : return;
3274 : : }
3275 : : }
3276 : :
3277 : 138 : build_builtin_candidate
3278 : 138 : (candidates, fnname, type1, type1, args, argtypes, flags, complain);
3279 : 138 : build_builtin_candidate
3280 : 138 : (candidates, fnname, type2, type2, args, argtypes, flags, complain);
3281 : 138 : return;
3282 : : }
3283 : :
3284 : 7399841 : build_builtin_candidate
3285 : 7399841 : (candidates, fnname, type1, type2, args, argtypes, flags, complain);
3286 : : }
3287 : :
3288 : : tree
3289 : 644136565 : type_decays_to (tree type)
3290 : : {
3291 : 644136565 : if (TREE_CODE (type) == ARRAY_TYPE)
3292 : 4695464 : return build_pointer_type (TREE_TYPE (type));
3293 : 639441101 : if (TREE_CODE (type) == FUNCTION_TYPE)
3294 : 37464 : return build_pointer_type (type);
3295 : : return type;
3296 : : }
3297 : :
3298 : : /* There are three conditions of builtin candidates:
3299 : :
3300 : : 1) bool-taking candidates. These are the same regardless of the input.
3301 : : 2) pointer-pair taking candidates. These are generated for each type
3302 : : one of the input types converts to.
3303 : : 3) arithmetic candidates. According to the standard, we should generate
3304 : : all of these, but I'm trying not to...
3305 : :
3306 : : Here we generate a superset of the possible candidates for this particular
3307 : : case. That is a subset of the full set the standard defines, plus some
3308 : : other cases which the standard disallows. add_builtin_candidate will
3309 : : filter out the invalid set. */
3310 : :
3311 : : static void
3312 : 14520687 : add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
3313 : : enum tree_code code2, tree fnname,
3314 : : vec<tree, va_gc> *argv,
3315 : : int flags, tsubst_flags_t complain)
3316 : : {
3317 : 14520687 : int ref1;
3318 : 14520687 : int enum_p = 0;
3319 : 14520687 : tree type, argtypes[3], t;
3320 : : /* TYPES[i] is the set of possible builtin-operator parameter types
3321 : : we will consider for the Ith argument. */
3322 : 14520687 : vec<tree, va_gc> *types[2];
3323 : 14520687 : unsigned ix;
3324 : 14520687 : vec<tree, va_gc> &args = *argv;
3325 : 14520687 : unsigned len = args.length ();
3326 : :
3327 : 40076965 : for (unsigned i = 0; i < len; ++i)
3328 : : {
3329 : 25556278 : if (args[i])
3330 : 25556278 : argtypes[i] = unlowered_expr_type (args[i]);
3331 : : else
3332 : 0 : argtypes[i] = NULL_TREE;
3333 : : }
3334 : :
3335 : 14520687 : switch (code)
3336 : : {
3337 : : /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
3338 : : and VQ is either volatile or empty, there exist candidate operator
3339 : : functions of the form
3340 : : VQ T& operator++(VQ T&); */
3341 : :
3342 : : case POSTINCREMENT_EXPR:
3343 : : case PREINCREMENT_EXPR:
3344 : : case POSTDECREMENT_EXPR:
3345 : : case PREDECREMENT_EXPR:
3346 : : case MODIFY_EXPR:
3347 : : ref1 = 1;
3348 : : break;
3349 : :
3350 : : /* 24There also exist candidate operator functions of the form
3351 : : bool operator!(bool);
3352 : : bool operator&&(bool, bool);
3353 : : bool operator||(bool, bool); */
3354 : :
3355 : 473198 : case TRUTH_NOT_EXPR:
3356 : 473198 : build_builtin_candidate
3357 : 473198 : (candidates, fnname, boolean_type_node,
3358 : : NULL_TREE, args, argtypes, flags, complain);
3359 : 8005842 : return;
3360 : :
3361 : 63254 : case TRUTH_ORIF_EXPR:
3362 : 63254 : case TRUTH_ANDIF_EXPR:
3363 : 63254 : build_builtin_candidate
3364 : 63254 : (candidates, fnname, boolean_type_node,
3365 : : boolean_type_node, args, argtypes, flags, complain);
3366 : 63254 : return;
3367 : :
3368 : : case ADDR_EXPR:
3369 : : case COMPOUND_EXPR:
3370 : : case COMPONENT_REF:
3371 : : case CO_AWAIT_EXPR:
3372 : : return;
3373 : :
3374 : 3852026 : case COND_EXPR:
3375 : 3852026 : case EQ_EXPR:
3376 : 3852026 : case NE_EXPR:
3377 : 3852026 : case LT_EXPR:
3378 : 3852026 : case LE_EXPR:
3379 : 3852026 : case GT_EXPR:
3380 : 3852026 : case GE_EXPR:
3381 : 3852026 : case SPACESHIP_EXPR:
3382 : 3852026 : enum_p = 1;
3383 : : /* Fall through. */
3384 : :
3385 : : default:
3386 : : ref1 = 0;
3387 : : }
3388 : :
3389 : 12199817 : types[0] = make_tree_vector ();
3390 : 12199817 : types[1] = make_tree_vector ();
3391 : :
3392 : 12199817 : if (len == 3)
3393 : 737 : len = 2;
3394 : 26148336 : for (unsigned i = 0; i < len; ++i)
3395 : : {
3396 : 19160293 : if (MAYBE_CLASS_TYPE_P (argtypes[i]))
3397 : : {
3398 : 6848118 : tree convs;
3399 : :
3400 : 6848118 : if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
3401 : : return;
3402 : :
3403 : 4996606 : convs = lookup_conversions (argtypes[i]);
3404 : :
3405 : 4996606 : if (code == COND_EXPR)
3406 : : {
3407 : 1284 : if (lvalue_p (args[i]))
3408 : 882 : vec_safe_push (types[i], build_reference_type (argtypes[i]));
3409 : :
3410 : 1284 : vec_safe_push (types[i], TYPE_MAIN_VARIANT (argtypes[i]));
3411 : : }
3412 : :
3413 : 4995322 : else if (! convs)
3414 : : return;
3415 : :
3416 : 3715694 : for (; convs; convs = TREE_CHAIN (convs))
3417 : : {
3418 : 2079350 : type = TREE_TYPE (convs);
3419 : :
3420 : 2622703 : if (i == 0 && ref1
3421 : 2079350 : && (!TYPE_REF_P (type)
3422 : 39 : || CP_TYPE_CONST_P (TREE_TYPE (type))))
3423 : 543353 : continue;
3424 : :
3425 : 1535997 : if (code == COND_EXPR && TYPE_REF_P (type))
3426 : 0 : vec_safe_push (types[i], type);
3427 : :
3428 : 1535997 : type = non_reference (type);
3429 : 1535997 : if (i != 0 || ! ref1)
3430 : : {
3431 : 1535958 : type = cv_unqualified (type_decays_to (type));
3432 : 1535958 : if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
3433 : 0 : vec_safe_push (types[i], type);
3434 : 1535958 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3435 : 799720 : type = type_promotes_to (type);
3436 : : }
3437 : :
3438 : 1535997 : if (! vec_member (type, types[i]))
3439 : 1534183 : vec_safe_push (types[i], type);
3440 : : }
3441 : : }
3442 : : else
3443 : : {
3444 : 12312175 : if (code == COND_EXPR && lvalue_p (args[i]))
3445 : 118 : vec_safe_push (types[i], build_reference_type (argtypes[i]));
3446 : 12312175 : type = non_reference (argtypes[i]);
3447 : 12312175 : if (i != 0 || ! ref1)
3448 : : {
3449 : 11326853 : type = cv_unqualified (type_decays_to (type));
3450 : 11326853 : if (enum_p && UNSCOPED_ENUM_P (type))
3451 : 3021213 : vec_safe_push (types[i], type);
3452 : 11326853 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3453 : 8980719 : type = type_promotes_to (type);
3454 : : }
3455 : 12312175 : vec_safe_push (types[i], type);
3456 : : }
3457 : : }
3458 : :
3459 : : /* Run through the possible parameter types of both arguments,
3460 : : creating candidates with those parameter types. */
3461 : 15209150 : FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t)
3462 : : {
3463 : 8221107 : unsigned jx;
3464 : 8221107 : tree u;
3465 : :
3466 : 8221107 : if (!types[1]->is_empty ())
3467 : 19231991 : FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u)
3468 : 11010884 : add_builtin_candidate
3469 : 11010884 : (candidates, code, code2, fnname, t,
3470 : : u, args, argtypes, flags, complain);
3471 : : else
3472 : 246503 : add_builtin_candidate
3473 : 246503 : (candidates, code, code2, fnname, t,
3474 : : NULL_TREE, args, argtypes, flags, complain);
3475 : : }
3476 : :
3477 : 6988043 : release_tree_vector (types[0]);
3478 : 6988043 : release_tree_vector (types[1]);
3479 : : }
3480 : :
3481 : :
3482 : : /* If TMPL can be successfully instantiated as indicated by
3483 : : EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
3484 : :
3485 : : TMPL is the template. EXPLICIT_TARGS are any explicit template
3486 : : arguments. ARGLIST is the arguments provided at the call-site.
3487 : : This does not change ARGLIST. The RETURN_TYPE is the desired type
3488 : : for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
3489 : : as for add_function_candidate. If an OBJ is supplied, FLAGS and
3490 : : CTYPE are ignored, and OBJ is as for add_conv_candidate.
3491 : :
3492 : : SHORTCUT_BAD_CONVS is as in add_function_candidate. */
3493 : :
3494 : : static struct z_candidate*
3495 : 324941718 : add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
3496 : : tree ctype, tree explicit_targs, tree first_arg,
3497 : : const vec<tree, va_gc> *arglist, tree return_type,
3498 : : tree access_path, tree conversion_path,
3499 : : int flags, tree obj, unification_kind_t strict,
3500 : : bool shortcut_bad_convs, tsubst_flags_t complain)
3501 : : {
3502 : 324941718 : int ntparms = DECL_NTPARMS (tmpl);
3503 : 324941718 : tree targs = make_tree_vec (ntparms);
3504 : 324941718 : unsigned int len = vec_safe_length (arglist);
3505 : 324941718 : unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
3506 : 324941718 : unsigned int skip_without_in_chrg = 0;
3507 : 324941718 : tree first_arg_without_in_chrg = first_arg;
3508 : 324941718 : tree *args_without_in_chrg;
3509 : 324941718 : unsigned int nargs_without_in_chrg;
3510 : 324941718 : unsigned int ia, ix;
3511 : 324941718 : tree arg;
3512 : 324941718 : struct z_candidate *cand;
3513 : 324941718 : tree fn;
3514 : 324941718 : struct rejection_reason *reason = NULL;
3515 : 324941718 : int errs;
3516 : 324941718 : conversion **convs = NULL;
3517 : :
3518 : : /* We don't do deduction on the in-charge parameter, the VTT
3519 : : parameter or 'this'. */
3520 : 324941718 : if (DECL_IOBJ_MEMBER_FUNCTION_P (tmpl))
3521 : : {
3522 : 38979086 : if (first_arg_without_in_chrg != NULL_TREE)
3523 : : first_arg_without_in_chrg = NULL_TREE;
3524 : 12 : else if (return_type && strict == DEDUCE_CALL)
3525 : : /* We're deducing for a call to the result of a template conversion
3526 : : function, so the args don't contain 'this'; leave them alone. */;
3527 : : else
3528 : 324941718 : ++skip_without_in_chrg;
3529 : : }
3530 : :
3531 : 324941718 : if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
3532 : 324941718 : || DECL_BASE_CONSTRUCTOR_P (tmpl))
3533 : 325841693 : && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
3534 : : {
3535 : 138 : if (first_arg_without_in_chrg != NULL_TREE)
3536 : : first_arg_without_in_chrg = NULL_TREE;
3537 : : else
3538 : 138 : ++skip_without_in_chrg;
3539 : : }
3540 : :
3541 : 324941718 : if (len < skip_without_in_chrg)
3542 : 0 : return add_ignored_candidate (candidates, tmpl);
3543 : :
3544 : 361178868 : if (DECL_CONSTRUCTOR_P (tmpl) && nargs == 2
3545 : 353361695 : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (first_arg),
3546 : 28419977 : TREE_TYPE ((*arglist)[0])))
3547 : : {
3548 : : /* 12.8/6 says, "A declaration of a constructor for a class X is
3549 : : ill-formed if its first parameter is of type (optionally cv-qualified)
3550 : : X and either there are no other parameters or else all other
3551 : : parameters have default arguments. A member function template is never
3552 : : instantiated to produce such a constructor signature."
3553 : :
3554 : : So if we're trying to copy an object of the containing class, don't
3555 : : consider a template constructor that has a first parameter type that
3556 : : is just a template parameter, as we would deduce a signature that we
3557 : : would then reject in the code below. */
3558 : 1124673 : if (tree firstparm = FUNCTION_FIRST_USER_PARMTYPE (tmpl))
3559 : : {
3560 : 1124673 : firstparm = TREE_VALUE (firstparm);
3561 : 1124673 : if (PACK_EXPANSION_P (firstparm))
3562 : 952 : firstparm = PACK_EXPANSION_PATTERN (firstparm);
3563 : 1124673 : if (TREE_CODE (firstparm) == TEMPLATE_TYPE_PARM)
3564 : : {
3565 : 400587 : gcc_assert (!explicit_targs);
3566 : 400587 : reason = invalid_copy_with_fn_template_rejection ();
3567 : 400587 : goto fail;
3568 : : }
3569 : : }
3570 : : }
3571 : :
3572 : 324541131 : nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
3573 : 324541131 : + (len - skip_without_in_chrg));
3574 : 324541131 : args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
3575 : 324541131 : ia = 0;
3576 : 324541131 : if (first_arg_without_in_chrg != NULL_TREE)
3577 : : {
3578 : 3503 : args_without_in_chrg[ia] = first_arg_without_in_chrg;
3579 : 3503 : ++ia;
3580 : : }
3581 : 549265361 : for (ix = skip_without_in_chrg;
3582 : 873806492 : vec_safe_iterate (arglist, ix, &arg);
3583 : : ++ix)
3584 : : {
3585 : 549265361 : args_without_in_chrg[ia] = arg;
3586 : 549265361 : ++ia;
3587 : : }
3588 : 324541131 : gcc_assert (ia == nargs_without_in_chrg);
3589 : :
3590 : 324541131 : if (!obj)
3591 : : {
3592 : : /* Check that there's no obvious arity mismatch before proceeding with
3593 : : deduction. This avoids substituting explicit template arguments
3594 : : into the template or e.g. derived-to-base parm/arg unification
3595 : : (which could result in an error outside the immediate context) when
3596 : : the resulting candidate would be unviable anyway. */
3597 : 324541119 : int min_arity = 0, max_arity = 0;
3598 : 324541119 : tree parms = TYPE_ARG_TYPES (TREE_TYPE (tmpl));
3599 : 324541119 : parms = skip_artificial_parms_for (tmpl, parms);
3600 : 1230295025 : for (; parms != void_list_node; parms = TREE_CHAIN (parms))
3601 : : {
3602 : 1168188812 : if (!parms || PACK_EXPANSION_P (TREE_VALUE (parms)))
3603 : : {
3604 : : max_arity = -1;
3605 : : break;
3606 : : }
3607 : 581212787 : if (TREE_PURPOSE (parms))
3608 : : /* A parameter with a default argument. */
3609 : 7255680 : ++max_arity;
3610 : : else
3611 : 573957107 : ++min_arity, ++max_arity;
3612 : : }
3613 : 324541119 : if (ia < (unsigned)min_arity)
3614 : : {
3615 : : /* Too few arguments. */
3616 : 25257593 : reason = arity_rejection (NULL_TREE, min_arity, ia,
3617 : : /*least_p=*/(max_arity == -1));
3618 : 25257593 : goto fail;
3619 : : }
3620 : 299283526 : else if (max_arity != -1 && ia > (unsigned)max_arity)
3621 : : {
3622 : : /* Too many arguments. */
3623 : 3878406 : reason = arity_rejection (NULL_TREE, max_arity, ia);
3624 : 3878406 : goto fail;
3625 : : }
3626 : :
3627 : 295405120 : convs = alloc_conversions (nargs);
3628 : :
3629 : 295405120 : if (shortcut_bad_convs
3630 : 295402500 : && DECL_IOBJ_MEMBER_FUNCTION_P (tmpl)
3631 : 346222564 : && !DECL_CONSTRUCTOR_P (tmpl))
3632 : : {
3633 : : /* Check the 'this' conversion before proceeding with deduction.
3634 : : This is effectively an extension of the DR 1391 resolution
3635 : : that we perform in check_non_deducible_conversions, though it's
3636 : : convenient to do this extra check here instead of there. */
3637 : 2491810 : tree parmtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (tmpl)));
3638 : 2491810 : tree argtype = lvalue_type (first_arg);
3639 : 2491810 : tree arg = first_arg;
3640 : 2491810 : conversion *t = build_this_conversion (tmpl, ctype,
3641 : : parmtype, argtype, arg,
3642 : : flags, complain);
3643 : 2491810 : convs[0] = t;
3644 : 2491810 : if (t->bad_p)
3645 : : {
3646 : 28890 : reason = bad_arg_conversion_rejection (first_arg, 0,
3647 : : arg, parmtype,
3648 : 28890 : EXPR_LOCATION (arg));
3649 : 28890 : goto fail;
3650 : : }
3651 : : }
3652 : : }
3653 : :
3654 : 295376242 : errs = errorcount+sorrycount;
3655 : 590738933 : fn = fn_type_unification (tmpl, explicit_targs, targs,
3656 : : args_without_in_chrg,
3657 : : nargs_without_in_chrg,
3658 : : return_type, strict, flags, convs,
3659 : 295376242 : false, complain & tf_decltype);
3660 : :
3661 : 295362691 : if (fn == error_mark_node)
3662 : : {
3663 : : /* Don't repeat unification later if it already resulted in errors. */
3664 : 243995199 : if (errorcount+sorrycount == errs)
3665 : 243995087 : reason = template_unification_rejection (tmpl, explicit_targs,
3666 : : targs, args_without_in_chrg,
3667 : : nargs_without_in_chrg,
3668 : : return_type, strict, flags);
3669 : : else
3670 : 112 : reason = template_unification_error_rejection ();
3671 : 243995199 : goto fail;
3672 : : }
3673 : :
3674 : : /* Now the explicit specifier might have been deduced; check if this
3675 : : declaration is explicit. If it is and we're ignoring non-converting
3676 : : constructors, don't add this function to the set of candidates. */
3677 : 51367492 : if (((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
3678 : : == LOOKUP_ONLYCONVERTING)
3679 : 51367492 : && DECL_NONCONVERTING_P (fn))
3680 : 311 : return add_ignored_candidate (candidates, fn);
3681 : :
3682 : 102734362 : if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
3683 : : {
3684 : 2624613 : tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
3685 : 5249199 : if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
3686 : : ctype))
3687 : : {
3688 : : /* We're trying to produce a constructor with a prohibited signature,
3689 : : as discussed above; handle here any cases we didn't catch then,
3690 : : such as X(X<T>). */
3691 : 165 : reason = invalid_copy_with_fn_template_rejection ();
3692 : 165 : goto fail;
3693 : : }
3694 : : }
3695 : :
3696 : 51367016 : if (obj != NULL_TREE)
3697 : : /* Aha, this is a conversion function. */
3698 : 9 : cand = add_conv_candidate (candidates, fn, obj, arglist,
3699 : : access_path, conversion_path, complain);
3700 : : else
3701 : 51367007 : cand = add_function_candidate (candidates, fn, ctype,
3702 : : first_arg, arglist, access_path,
3703 : : conversion_path, flags, convs,
3704 : : shortcut_bad_convs, complain);
3705 : 51367016 : if (DECL_TI_TEMPLATE (fn) != tmpl)
3706 : : /* This situation can occur if a member template of a template
3707 : : class is specialized. Then, instantiate_template might return
3708 : : an instantiation of the specialization, in which case the
3709 : : DECL_TI_TEMPLATE field will point at the original
3710 : : specialization. For example:
3711 : :
3712 : : template <class T> struct S { template <class U> void f(U);
3713 : : template <> void f(int) {}; };
3714 : : S<double> sd;
3715 : : sd.f(3);
3716 : :
3717 : : Here, TMPL will be template <class U> S<double>::f(U).
3718 : : And, instantiate template will give us the specialization
3719 : : template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
3720 : : for this will point at template <class T> template <> S<T>::f(int),
3721 : : so that we can find the definition. For the purposes of
3722 : : overload resolution, however, we want the original TMPL. */
3723 : 3413096 : cand->template_decl = build_template_info (tmpl, targs);
3724 : : else
3725 : 47953920 : cand->template_decl = DECL_TEMPLATE_INFO (fn);
3726 : 51367016 : cand->explicit_targs = explicit_targs;
3727 : :
3728 : 51367016 : return cand;
3729 : 273560840 : fail:
3730 : 273560840 : int viable = (reason->code == rr_bad_arg_conversion ? -1 : 0);
3731 : 273560840 : return add_candidate (candidates, tmpl, first_arg, arglist, nargs, convs,
3732 : 273560840 : access_path, conversion_path, viable, reason, flags);
3733 : : }
3734 : :
3735 : :
3736 : : static struct z_candidate *
3737 : 324941706 : add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
3738 : : tree explicit_targs, tree first_arg,
3739 : : const vec<tree, va_gc> *arglist, tree return_type,
3740 : : tree access_path, tree conversion_path, int flags,
3741 : : unification_kind_t strict, bool shortcut_bad_convs,
3742 : : tsubst_flags_t complain)
3743 : : {
3744 : 324941706 : return
3745 : 0 : add_template_candidate_real (candidates, tmpl, ctype,
3746 : : explicit_targs, first_arg, arglist,
3747 : : return_type, access_path, conversion_path,
3748 : : flags, NULL_TREE, strict, shortcut_bad_convs,
3749 : 324928155 : complain);
3750 : : }
3751 : :
3752 : : /* Create an overload candidate for the conversion function template TMPL,
3753 : : returning RETURN_TYPE, which will be invoked for expression OBJ to produce a
3754 : : pointer-to-function which will in turn be called with the argument list
3755 : : ARGLIST, and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
3756 : : passed on to implicit_conversion. */
3757 : :
3758 : : static struct z_candidate *
3759 : 12 : add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
3760 : : tree obj,
3761 : : const vec<tree, va_gc> *arglist,
3762 : : tree return_type, tree access_path,
3763 : : tree conversion_path, tsubst_flags_t complain)
3764 : : {
3765 : 12 : return
3766 : 12 : add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
3767 : : NULL_TREE, arglist, return_type, access_path,
3768 : : conversion_path, 0, obj, DEDUCE_CALL,
3769 : 12 : /*shortcut_bad_convs=*/false, complain);
3770 : : }
3771 : :
3772 : : /* The CANDS are the set of candidates that were considered for
3773 : : overload resolution. Sort CANDS so that the strictly viable
3774 : : candidates appear first, followed by non-strictly viable candidates,
3775 : : followed by non-viable candidates. Returns the first candidate
3776 : : in this sorted list. If any of the candidates were viable, set
3777 : : *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
3778 : : considered viable only if it is strictly viable when setting
3779 : : *ANY_VIABLE_P. */
3780 : :
3781 : : static struct z_candidate*
3782 : 203632299 : splice_viable (struct z_candidate *cands,
3783 : : bool strict_p,
3784 : : bool *any_viable_p)
3785 : : {
3786 : 203632299 : z_candidate *strictly_viable = nullptr;
3787 : 203632299 : z_candidate **strictly_viable_tail = &strictly_viable;
3788 : :
3789 : 203632299 : z_candidate *non_strictly_viable = nullptr;
3790 : 203632299 : z_candidate **non_strictly_viable_tail = &non_strictly_viable;
3791 : :
3792 : 203632299 : z_candidate *non_viable = nullptr;
3793 : 203632299 : z_candidate **non_viable_tail = &non_viable;
3794 : :
3795 : 203632299 : z_candidate *non_viable_ignored = nullptr;
3796 : 203632299 : z_candidate **non_viable_ignored_tail = &non_viable_ignored;
3797 : :
3798 : : /* Be strict inside templates, since build_over_call won't actually
3799 : : do the conversions to get pedwarns. */
3800 : 203632299 : if (processing_template_decl)
3801 : 36560544 : strict_p = true;
3802 : :
3803 : 785198627 : for (z_candidate *cand = cands; cand; cand = cand->next)
3804 : : {
3805 : 581566328 : if (!strict_p
3806 : 200555669 : && (cand->viable == 1 || TREE_CODE (cand->fn) == TEMPLATE_DECL))
3807 : : /* Be strict in the presence of a viable candidate. Also if
3808 : : there are template candidates, so that we get deduction errors
3809 : : for them instead of silently preferring a bad conversion. */
3810 : 523496151 : strict_p = true;
3811 : :
3812 : : /* Move this candidate to the appropriate list according to
3813 : : its viability. */
3814 : 581566328 : auto& tail = (cand->viable == 1 ? strictly_viable_tail
3815 : : : cand->viable == -1 ? non_strictly_viable_tail
3816 : 322813619 : : ignored_candidate_p (cand) ? non_viable_ignored_tail
3817 : 581566328 : : non_viable_tail);
3818 : 581566328 : *tail = cand;
3819 : 581566328 : tail = &cand->next;
3820 : : }
3821 : :
3822 : 407264598 : *any_viable_p = (strictly_viable != nullptr
3823 : 203632299 : || (!strict_p && non_strictly_viable != nullptr));
3824 : :
3825 : : /* Combine the lists. */
3826 : 203632299 : *non_viable_ignored_tail = nullptr;
3827 : 203632299 : *non_viable_tail = non_viable_ignored;
3828 : 203632299 : *non_strictly_viable_tail = non_viable;
3829 : 203632299 : *strictly_viable_tail = non_strictly_viable;
3830 : :
3831 : 203632299 : return strictly_viable;
3832 : : }
3833 : :
3834 : : static bool
3835 : 194950053 : any_strictly_viable (struct z_candidate *cands)
3836 : : {
3837 : 247918993 : for (; cands; cands = cands->next)
3838 : 55839664 : if (cands->viable == 1)
3839 : : return true;
3840 : : return false;
3841 : : }
3842 : :
3843 : : /* OBJ is being used in an expression like "OBJ.f (...)". In other
3844 : : words, it is about to become the "this" pointer for a member
3845 : : function call. Take the address of the object. */
3846 : :
3847 : : static tree
3848 : 45070828 : build_this (tree obj)
3849 : : {
3850 : : /* In a template, we are only concerned about the type of the
3851 : : expression, so we can take a shortcut. */
3852 : 45070828 : if (processing_template_decl)
3853 : 61413 : return build_address (obj);
3854 : :
3855 : 45009415 : return cp_build_addr_expr (obj, tf_warning_or_error);
3856 : : }
3857 : :
3858 : : /* Returns true iff functions are equivalent. Equivalent functions are
3859 : : not '==' only if one is a function-local extern function or if
3860 : : both are extern "C". */
3861 : :
3862 : : static inline int
3863 : 4899614 : equal_functions (tree fn1, tree fn2)
3864 : : {
3865 : 4899614 : if (TREE_CODE (fn1) != TREE_CODE (fn2))
3866 : : return 0;
3867 : 4875551 : if (TREE_CODE (fn1) == TEMPLATE_DECL)
3868 : 49206 : return fn1 == fn2;
3869 : 9652672 : if (DECL_LOCAL_DECL_P (fn1) || DECL_LOCAL_DECL_P (fn2)
3870 : 9652669 : || DECL_EXTERN_C_FUNCTION_P (fn1))
3871 : 3016284 : return decls_match (fn1, fn2);
3872 : 1810061 : return fn1 == fn2;
3873 : : }
3874 : :
3875 : : /* Print information about a candidate FN being rejected due to INFO. */
3876 : :
3877 : : static void
3878 : 5451 : print_conversion_rejection (location_t loc, struct conversion_info *info,
3879 : : tree fn)
3880 : : {
3881 : 5451 : tree from = info->from;
3882 : 5451 : if (!TYPE_P (from))
3883 : 4436 : from = lvalue_type (from);
3884 : 5451 : if (info->n_arg == -1)
3885 : : {
3886 : : /* Conversion of implicit `this' argument failed. */
3887 : 36 : if (!TYPE_P (info->from))
3888 : : /* A bad conversion for 'this' must be discarding cv-quals. */
3889 : 36 : inform (loc, "passing %qT as %<this%> "
3890 : : "argument discards qualifiers",
3891 : : from);
3892 : : else
3893 : 0 : inform (loc, "no known conversion for implicit "
3894 : : "%<this%> parameter from %qH to %qI",
3895 : : from, info->to_type);
3896 : : }
3897 : 5415 : else if (!TYPE_P (info->from))
3898 : : {
3899 : 4400 : if (info->n_arg >= 0)
3900 : 4400 : inform (loc, "conversion of argument %d would be ill-formed:",
3901 : : info->n_arg + 1);
3902 : 4400 : iloc_sentinel ils = loc;
3903 : 4400 : perform_implicit_conversion (info->to_type, info->from,
3904 : : tf_warning_or_error);
3905 : 4400 : }
3906 : 1015 : else if (info->n_arg == -2)
3907 : : /* Conversion of conversion function return value failed. */
3908 : 34 : inform (loc, "no known conversion from %qH to %qI",
3909 : : from, info->to_type);
3910 : : else
3911 : : {
3912 : 981 : if (TREE_CODE (fn) == FUNCTION_DECL)
3913 : 824 : loc = get_fndecl_argument_location (fn, info->n_arg);
3914 : 981 : inform (loc, "no known conversion for argument %d from %qH to %qI",
3915 : 981 : info->n_arg + 1, from, info->to_type);
3916 : : }
3917 : 5451 : }
3918 : :
3919 : : /* Print information about a candidate with WANT parameters and we found
3920 : : HAVE. */
3921 : :
3922 : : static void
3923 : 1307 : print_arity_information (location_t loc, unsigned int have, unsigned int want,
3924 : : bool least_p)
3925 : : {
3926 : 1307 : if (least_p)
3927 : 41 : inform_n (loc, want,
3928 : : "candidate expects at least %d argument, %d provided",
3929 : : "candidate expects at least %d arguments, %d provided",
3930 : : want, have);
3931 : : else
3932 : 1266 : inform_n (loc, want,
3933 : : "candidate expects %d argument, %d provided",
3934 : : "candidate expects %d arguments, %d provided",
3935 : : want, have);
3936 : 1307 : }
3937 : :
3938 : : /* Print information about one overload candidate CANDIDATE. MSGSTR
3939 : : is the text to print before the candidate itself.
3940 : :
3941 : : NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
3942 : : to have been run through gettext by the caller. This wart makes
3943 : : life simpler in print_z_candidates and for the translators. */
3944 : :
3945 : : static void
3946 : 13419 : print_z_candidate (location_t loc, const char *msgstr,
3947 : : struct z_candidate *candidate)
3948 : : {
3949 : 13419 : const char *msg = (msgstr == NULL
3950 : 13419 : ? ""
3951 : 13419 : : ACONCAT ((_(msgstr), " ", NULL)));
3952 : 13419 : tree fn = candidate->fn;
3953 : 13419 : if (flag_new_inheriting_ctors)
3954 : 13392 : fn = strip_inheriting_ctors (fn);
3955 : 13419 : location_t cloc = location_of (fn);
3956 : :
3957 : 13419 : if (identifier_p (fn))
3958 : : {
3959 : 213 : cloc = loc;
3960 : 213 : if (candidate->num_convs == 3)
3961 : 0 : inform (cloc, "%s%<%D(%T, %T, %T)%> (built-in)", msg, fn,
3962 : 0 : candidate->convs[0]->type,
3963 : 0 : candidate->convs[1]->type,
3964 : 0 : candidate->convs[2]->type);
3965 : 213 : else if (candidate->num_convs == 2)
3966 : 188 : inform (cloc, "%s%<%D(%T, %T)%> (built-in)", msg, fn,
3967 : 188 : candidate->convs[0]->type,
3968 : 188 : candidate->convs[1]->type);
3969 : : else
3970 : 25 : inform (cloc, "%s%<%D(%T)%> (built-in)", msg, fn,
3971 : 25 : candidate->convs[0]->type);
3972 : : }
3973 : 13206 : else if (TYPE_P (fn))
3974 : 9 : inform (cloc, "%s%qT (conversion)", msg, fn);
3975 : 13197 : else if (candidate->viable == -1)
3976 : 4449 : inform (cloc, "%s%#qD (near match)", msg, fn);
3977 : 8748 : else if (ignored_candidate_p (candidate))
3978 : 6 : inform (cloc, "%s%#qD (ignored)", msg, fn);
3979 : 8742 : else if (DECL_DELETED_FN (fn))
3980 : 49 : inform (cloc, "%s%#qD (deleted)", msg, fn);
3981 : 8693 : else if (candidate->reversed ())
3982 : 88 : inform (cloc, "%s%#qD (reversed)", msg, fn);
3983 : 8605 : else if (candidate->rewritten ())
3984 : 0 : inform (cloc, "%s%#qD (rewritten)", msg, fn);
3985 : : else
3986 : 8605 : inform (cloc, "%s%#qD", msg, fn);
3987 : 13419 : if (fn != candidate->fn)
3988 : : {
3989 : 51 : cloc = location_of (candidate->fn);
3990 : 51 : inform (cloc, "inherited here");
3991 : : }
3992 : : /* Give the user some information about why this candidate failed. */
3993 : 13419 : if (candidate->reason != NULL)
3994 : : {
3995 : 10364 : auto_diagnostic_nesting_level sentinel;
3996 : 10364 : struct rejection_reason *r = candidate->reason;
3997 : :
3998 : 10364 : switch (r->code)
3999 : : {
4000 : 1307 : case rr_arity:
4001 : 1307 : print_arity_information (cloc, r->u.arity.actual,
4002 : 1307 : r->u.arity.expected,
4003 : 1307 : r->u.arity.least_p);
4004 : 1307 : break;
4005 : 987 : case rr_arg_conversion:
4006 : 987 : print_conversion_rejection (cloc, &r->u.conversion, fn);
4007 : 987 : break;
4008 : 4464 : case rr_bad_arg_conversion:
4009 : 4464 : print_conversion_rejection (cloc, &r->u.bad_conversion, fn);
4010 : 4464 : break;
4011 : 10 : case rr_explicit_conversion:
4012 : 10 : inform (cloc, "return type %qT of explicit conversion function "
4013 : : "cannot be converted to %qT with a qualification "
4014 : : "conversion", r->u.conversion.from,
4015 : : r->u.conversion.to_type);
4016 : 10 : break;
4017 : 6 : case rr_template_conversion:
4018 : 6 : inform (cloc, "conversion from return type %qT of template "
4019 : : "conversion function specialization to %qT is not an "
4020 : : "exact match", r->u.conversion.from,
4021 : : r->u.conversion.to_type);
4022 : 6 : break;
4023 : 3484 : case rr_template_unification:
4024 : : /* We use template_unification_error_rejection if unification caused
4025 : : actual non-SFINAE errors, in which case we don't need to repeat
4026 : : them here. */
4027 : 3484 : if (r->u.template_unification.tmpl == NULL_TREE)
4028 : : {
4029 : 45 : inform (cloc, "substitution of deduced template arguments "
4030 : : "resulted in errors seen above");
4031 : 45 : break;
4032 : : }
4033 : : /* Re-run template unification with diagnostics. */
4034 : 3439 : inform (cloc, "template argument deduction/substitution failed:");
4035 : 3439 : {
4036 : 3439 : auto_diagnostic_nesting_level sentinel;
4037 : 3439 : fn_type_unification (r->u.template_unification.tmpl,
4038 : : r->u.template_unification.explicit_targs,
4039 : : (make_tree_vec
4040 : : (r->u.template_unification.num_targs)),
4041 : : r->u.template_unification.args,
4042 : : r->u.template_unification.nargs,
4043 : : r->u.template_unification.return_type,
4044 : : r->u.template_unification.strict,
4045 : : r->u.template_unification.flags,
4046 : : NULL, true, false);
4047 : 3439 : }
4048 : 3439 : break;
4049 : 2 : case rr_invalid_copy:
4050 : 2 : inform (cloc,
4051 : : "a constructor taking a single argument of its own "
4052 : : "class type is invalid");
4053 : 2 : break;
4054 : 70 : case rr_constraint_failure:
4055 : 70 : {
4056 : 70 : auto_diagnostic_nesting_level sentinel;
4057 : 70 : diagnose_constraints (cloc, fn, NULL_TREE);
4058 : 70 : }
4059 : 70 : break;
4060 : 28 : case rr_inherited_ctor:
4061 : 28 : inform (cloc, "an inherited constructor is not a candidate for "
4062 : : "initialization from an expression of the same or derived "
4063 : : "type");
4064 : 28 : break;
4065 : : case rr_ignored:
4066 : : break;
4067 : 0 : case rr_none:
4068 : 0 : default:
4069 : : /* This candidate didn't have any issues or we failed to
4070 : : handle a particular code. Either way... */
4071 : 0 : gcc_unreachable ();
4072 : : }
4073 : 10364 : }
4074 : 13419 : }
4075 : :
4076 : : /* Print information about each overload candidate in CANDIDATES,
4077 : : which is assumed to have gone through splice_viable and tourney
4078 : : (if splice_viable succeeded). */
4079 : :
4080 : : static void
4081 : 5209 : print_z_candidates (location_t loc, struct z_candidate *candidates,
4082 : : tristate only_viable_p /* = tristate::unknown () */)
4083 : : {
4084 : 5209 : struct z_candidate *cand1;
4085 : 5209 : struct z_candidate **cand2;
4086 : :
4087 : 5209 : if (!candidates)
4088 : 987 : return;
4089 : :
4090 : : /* Remove non-viable deleted candidates. */
4091 : 4222 : cand1 = candidates;
4092 : 19985 : for (cand2 = &cand1; *cand2; )
4093 : : {
4094 : 15763 : if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
4095 : 11321 : && !(*cand2)->viable
4096 : 19393 : && DECL_DELETED_FN ((*cand2)->fn))
4097 : 82 : *cand2 = (*cand2)->next;
4098 : : else
4099 : 15681 : cand2 = &(*cand2)->next;
4100 : : }
4101 : : /* ...if there are any non-deleted ones. */
4102 : 4222 : if (cand1)
4103 : 4216 : candidates = cand1;
4104 : :
4105 : : /* There may be duplicates in the set of candidates. We put off
4106 : : checking this condition as long as possible, since we have no way
4107 : : to eliminate duplicates from a set of functions in less than n^2
4108 : : time. Now we are about to emit an error message, so it is more
4109 : : permissible to go slowly. */
4110 : 19815 : for (cand1 = candidates; cand1; cand1 = cand1->next)
4111 : : {
4112 : 15593 : tree fn = cand1->fn;
4113 : : /* Skip builtin candidates and conversion functions. */
4114 : 15593 : if (!DECL_P (fn))
4115 : 282 : continue;
4116 : 15311 : cand2 = &cand1->next;
4117 : 166602 : while (*cand2)
4118 : : {
4119 : 151291 : if (DECL_P ((*cand2)->fn)
4120 : 151291 : && equal_functions (fn, (*cand2)->fn))
4121 : 94 : *cand2 = (*cand2)->next;
4122 : : else
4123 : 151197 : cand2 = &(*cand2)->next;
4124 : : }
4125 : : }
4126 : :
4127 : : /* Unless otherwise specified, if there's a (strictly) viable candidate
4128 : : then we assume we're being called as part of diagnosing ambiguity, in
4129 : : which case we want to print only viable candidates since non-viable
4130 : : candidates couldn't have contributed to the ambiguity. */
4131 : 4222 : if (only_viable_p.is_unknown ())
4132 : 4213 : only_viable_p = candidates->viable == 1;
4133 : :
4134 : 4222 : auto_diagnostic_nesting_level sentinel;
4135 : :
4136 : 4222 : int num_candidates = 0;
4137 : 19815 : for (auto iter = candidates; iter; iter = iter->next)
4138 : 15593 : ++num_candidates;
4139 : :
4140 : 4222 : inform_n (loc,
4141 : : num_candidates, "there is %i candidate", "there are %i candidates",
4142 : : num_candidates);
4143 : 4222 : auto_diagnostic_nesting_level sentinel2;
4144 : :
4145 : 4222 : int candidate_idx = 0;
4146 : 21724 : for (; candidates; candidates = candidates->next)
4147 : : {
4148 : 13647 : if (only_viable_p.is_true () && candidates->viable != 1)
4149 : : break;
4150 : 13338 : if (ignored_candidate_p (candidates) && !flag_diagnostics_all_candidates)
4151 : : {
4152 : 26 : inform (loc, "some candidates omitted; "
4153 : : "use %<-fdiagnostics-all-candidates%> to display them");
4154 : 26 : break;
4155 : : }
4156 : 13280 : pretty_printer pp;
4157 : 13280 : pp_printf (&pp, N_("candidate %i:"), candidate_idx + 1);
4158 : 13280 : const char *const msgstr = pp_formatted_text (&pp);
4159 : 13280 : print_z_candidate (loc, msgstr, candidates);
4160 : 13280 : ++candidate_idx;
4161 : 13280 : }
4162 : 4222 : }
4163 : :
4164 : : /* USER_SEQ is a user-defined conversion sequence, beginning with a
4165 : : USER_CONV. STD_SEQ is the standard conversion sequence applied to
4166 : : the result of the conversion function to convert it to the final
4167 : : desired type. Merge the two sequences into a single sequence,
4168 : : and return the merged sequence. */
4169 : :
4170 : : static conversion *
4171 : 8700243 : merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
4172 : : {
4173 : 8700243 : conversion **t;
4174 : 8700243 : bool bad = user_seq->bad_p;
4175 : :
4176 : 8700243 : gcc_assert (user_seq->kind == ck_user);
4177 : :
4178 : : /* Find the end of the second conversion sequence. */
4179 : 9235566 : for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
4180 : : {
4181 : : /* The entire sequence is a user-conversion sequence. */
4182 : 535323 : (*t)->user_conv_p = true;
4183 : 535323 : if (bad)
4184 : 2056 : (*t)->bad_p = true;
4185 : : }
4186 : :
4187 : 8700243 : if ((*t)->rvaluedness_matches_p)
4188 : : /* We're binding a reference directly to the result of the conversion.
4189 : : build_user_type_conversion_1 stripped the REFERENCE_TYPE from the return
4190 : : type, but we want it back. */
4191 : 309901 : user_seq->type = TREE_TYPE (TREE_TYPE (user_seq->cand->fn));
4192 : :
4193 : : /* Replace the identity conversion with the user conversion
4194 : : sequence. */
4195 : 8700243 : *t = user_seq;
4196 : :
4197 : 8700243 : return std_seq;
4198 : : }
4199 : :
4200 : : /* Handle overload resolution for initializing an object of class type from
4201 : : an initializer list. First we look for a suitable constructor that
4202 : : takes a std::initializer_list; if we don't find one, we then look for a
4203 : : non-list constructor.
4204 : :
4205 : : Parameters are as for add_candidates, except that the arguments are in
4206 : : the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
4207 : : the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
4208 : :
4209 : : static void
4210 : 1209580 : add_list_candidates (tree fns, tree first_arg,
4211 : : const vec<tree, va_gc> *args, tree totype,
4212 : : tree explicit_targs, bool template_only,
4213 : : tree conversion_path, tree access_path,
4214 : : int flags,
4215 : : struct z_candidate **candidates,
4216 : : tsubst_flags_t complain)
4217 : : {
4218 : 1209580 : gcc_assert (*candidates == NULL);
4219 : :
4220 : : /* We're looking for a ctor for list-initialization. */
4221 : 1209580 : flags |= LOOKUP_LIST_INIT_CTOR;
4222 : : /* And we don't allow narrowing conversions. We also use this flag to
4223 : : avoid the copy constructor call for copy-list-initialization. */
4224 : 1209580 : flags |= LOOKUP_NO_NARROWING;
4225 : :
4226 : 2419160 : unsigned nart = num_artificial_parms_for (OVL_FIRST (fns)) - 1;
4227 : 1209580 : tree init_list = (*args)[nart];
4228 : :
4229 : : /* Always use the default constructor if the list is empty (DR 990). */
4230 : 1209580 : if (CONSTRUCTOR_NELTS (init_list) == 0
4231 : 1209580 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
4232 : : ;
4233 : 1079737 : else if (CONSTRUCTOR_IS_DESIGNATED_INIT (init_list)
4234 : 1079737 : && !CP_AGGREGATE_TYPE_P (totype))
4235 : : {
4236 : 48 : if (complain & tf_error)
4237 : 12 : error ("designated initializers cannot be used with a "
4238 : : "non-aggregate type %qT", totype);
4239 : 1993 : return;
4240 : : }
4241 : : /* If the class has a list ctor, try passing the list as a single
4242 : : argument first, but only consider list ctors. */
4243 : 1079689 : else if (TYPE_HAS_LIST_CTOR (totype))
4244 : : {
4245 : 58544 : flags |= LOOKUP_LIST_ONLY;
4246 : 58544 : add_candidates (fns, first_arg, args, NULL_TREE,
4247 : : explicit_targs, template_only, conversion_path,
4248 : : access_path, flags, candidates, complain);
4249 : 117088 : if (any_strictly_viable (*candidates))
4250 : : return;
4251 : : }
4252 : :
4253 : : /* Expand the CONSTRUCTOR into a new argument vec. */
4254 : 1207587 : vec<tree, va_gc> *new_args;
4255 : 2284981 : vec_alloc (new_args, nart + CONSTRUCTOR_NELTS (init_list));
4256 : 1207590 : for (unsigned i = 0; i < nart; ++i)
4257 : 3 : new_args->quick_push ((*args)[i]);
4258 : 3007750 : for (unsigned i = 0; i < CONSTRUCTOR_NELTS (init_list); ++i)
4259 : 1800163 : new_args->quick_push (CONSTRUCTOR_ELT (init_list, i)->value);
4260 : :
4261 : : /* We aren't looking for list-ctors anymore. */
4262 : 1207587 : flags &= ~LOOKUP_LIST_ONLY;
4263 : : /* We allow more user-defined conversions within an init-list. */
4264 : 1207587 : flags &= ~LOOKUP_NO_CONVERSION;
4265 : :
4266 : 1207587 : add_candidates (fns, first_arg, new_args, NULL_TREE,
4267 : : explicit_targs, template_only, conversion_path,
4268 : : access_path, flags, candidates, complain);
4269 : : }
4270 : :
4271 : : /* Given C(std::initializer_list<A>), return A. */
4272 : :
4273 : : static tree
4274 : 1156 : list_ctor_element_type (tree fn)
4275 : : {
4276 : 1156 : gcc_checking_assert (is_list_ctor (fn));
4277 : :
4278 : 1156 : tree parm = FUNCTION_FIRST_USER_PARMTYPE (fn);
4279 : 1156 : parm = non_reference (TREE_VALUE (parm));
4280 : 1156 : return TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
4281 : : }
4282 : :
4283 : : /* If EXPR is a braced-init-list where the elements all decay to the same type,
4284 : : return that type. */
4285 : :
4286 : : static tree
4287 : 1038 : braced_init_element_type (tree expr)
4288 : : {
4289 : 1038 : if (TREE_CODE (expr) == CONSTRUCTOR
4290 : 1038 : && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
4291 : 0 : return TREE_TYPE (TREE_TYPE (expr));
4292 : 1038 : if (!BRACE_ENCLOSED_INITIALIZER_P (expr))
4293 : : return NULL_TREE;
4294 : :
4295 : 1038 : tree elttype = NULL_TREE;
4296 : 6239 : for (constructor_elt &e: CONSTRUCTOR_ELTS (expr))
4297 : : {
4298 : 3253 : tree type = TREE_TYPE (e.value);
4299 : 3253 : type = type_decays_to (type);
4300 : 3253 : if (!elttype)
4301 : : elttype = type;
4302 : 2225 : else if (!same_type_p (type, elttype))
4303 : : return NULL_TREE;
4304 : : }
4305 : : return elttype;
4306 : : }
4307 : :
4308 : : /* True iff EXPR contains any temporaries with non-trivial destruction.
4309 : :
4310 : : ??? Also ignore classes with non-trivial but no-op destruction other than
4311 : : std::allocator? */
4312 : :
4313 : : static bool
4314 : 166 : has_non_trivial_temporaries (tree expr)
4315 : : {
4316 : 166 : auto_vec<tree*> temps;
4317 : 166 : cp_walk_tree_without_duplicates (&expr, find_temps_r, &temps);
4318 : 325 : for (tree *p : temps)
4319 : : {
4320 : 53 : tree t = TREE_TYPE (*p);
4321 : 53 : if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (t)
4322 : 53 : && !is_std_allocator (t))
4323 : : return true;
4324 : : }
4325 : : return false;
4326 : 166 : }
4327 : :
4328 : : /* We're initializing an array of ELTTYPE from INIT. If it seems useful,
4329 : : return INIT as an array (of its own type) so the caller can initialize the
4330 : : target array in a loop. */
4331 : :
4332 : : static tree
4333 : 4006 : maybe_init_list_as_array (tree elttype, tree init)
4334 : : {
4335 : : /* Only do this if the array can go in rodata but not once converted. */
4336 : 4006 : if (!TYPE_NON_AGGREGATE_CLASS (elttype))
4337 : : return NULL_TREE;
4338 : 1038 : tree init_elttype = braced_init_element_type (init);
4339 : 1038 : if (!init_elttype || !SCALAR_TYPE_P (init_elttype) || !TREE_CONSTANT (init))
4340 : : return NULL_TREE;
4341 : :
4342 : : /* Check with a stub expression to weed out special cases, and check whether
4343 : : we call the same function for direct-init as copy-list-init. */
4344 : 229 : conversion_obstack_sentinel cos;
4345 : 229 : init_elttype = cp_build_qualified_type (init_elttype, TYPE_QUAL_CONST);
4346 : 229 : tree arg = build_stub_object (init_elttype);
4347 : 229 : conversion *c = implicit_conversion (elttype, init_elttype, arg, false,
4348 : : LOOKUP_NORMAL, tf_none);
4349 : 229 : if (c && c->kind == ck_rvalue)
4350 : 0 : c = next_conversion (c);
4351 : 223 : if (!c || c->kind != ck_user)
4352 : : return NULL_TREE;
4353 : : /* Check that we actually can perform the conversion. */
4354 : 220 : if (convert_like (c, arg, tf_none) == error_mark_node)
4355 : : /* Let the normal code give the error. */
4356 : : return NULL_TREE;
4357 : :
4358 : 214 : tree first = CONSTRUCTOR_ELT (init, 0)->value;
4359 : 214 : conversion *fc = implicit_conversion (elttype, init_elttype, first, false,
4360 : : LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING,
4361 : : tf_none);
4362 : 214 : if (fc && fc->kind == ck_rvalue)
4363 : 40 : fc = next_conversion (fc);
4364 : 214 : if (!fc || fc->kind != ck_user || fc->cand->fn != c->cand->fn)
4365 : : return NULL_TREE;
4366 : 172 : first = convert_like (fc, first, tf_none);
4367 : 172 : if (first == error_mark_node)
4368 : : /* Let the normal code give the error. */
4369 : : return NULL_TREE;
4370 : :
4371 : : /* Don't do this if the conversion would be constant. */
4372 : 169 : first = maybe_constant_init (first);
4373 : 169 : if (TREE_CONSTANT (first))
4374 : : return NULL_TREE;
4375 : :
4376 : : /* We can't do this if the conversion creates temporaries that need
4377 : : to live until the whole array is initialized. */
4378 : 166 : if (has_non_trivial_temporaries (first))
4379 : : return NULL_TREE;
4380 : :
4381 : : /* We can't do this if copying from the initializer_list would be
4382 : : ill-formed. */
4383 : 166 : tree copy_argtypes = make_tree_vec (1);
4384 : 166 : TREE_VEC_ELT (copy_argtypes, 0)
4385 : 166 : = cp_build_qualified_type (elttype, TYPE_QUAL_CONST);
4386 : 166 : if (!is_xible (INIT_EXPR, elttype, copy_argtypes))
4387 : : return NULL_TREE;
4388 : :
4389 : 320 : tree arr = build_array_of_n_type (init_elttype, CONSTRUCTOR_NELTS (init));
4390 : 160 : arr = finish_compound_literal (arr, init, tf_none);
4391 : 160 : DECL_MERGEABLE (TARGET_EXPR_SLOT (arr)) = true;
4392 : 160 : return arr;
4393 : 229 : }
4394 : :
4395 : : /* If we were going to call e.g. vector(initializer_list<string>) starting
4396 : : with a list of string-literals (which is inefficient, see PR105838),
4397 : : instead build an array of const char* and pass it to the range constructor.
4398 : : But only do this for standard library types, where we can assume the
4399 : : transformation makes sense.
4400 : :
4401 : : Really the container classes should have initializer_list<U> constructors to
4402 : : get the same effect more simply; this is working around that lack. */
4403 : :
4404 : : static tree
4405 : 8400568 : maybe_init_list_as_range (tree fn, tree expr)
4406 : : {
4407 : 8400568 : if (!processing_template_decl
4408 : 8198300 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
4409 : 583989 : && is_list_ctor (fn)
4410 : 8401860 : && decl_in_std_namespace_p (fn))
4411 : : {
4412 : 1156 : tree to = list_ctor_element_type (fn);
4413 : 1156 : if (tree init = maybe_init_list_as_array (to, expr))
4414 : : {
4415 : 42 : tree begin = decay_conversion (TARGET_EXPR_SLOT (init), tf_none);
4416 : 42 : tree nelts = array_type_nelts_top (TREE_TYPE (init));
4417 : 42 : tree end = cp_build_binary_op (input_location, PLUS_EXPR, begin,
4418 : : nelts, tf_none);
4419 : 42 : begin = cp_build_compound_expr (init, begin, tf_none);
4420 : 42 : return build_constructor_va (init_list_type_node, 2,
4421 : 42 : NULL_TREE, begin, NULL_TREE, end);
4422 : : }
4423 : : }
4424 : :
4425 : : return NULL_TREE;
4426 : : }
4427 : :
4428 : : /* Returns the best overload candidate to perform the requested
4429 : : conversion. This function is used for three the overloading situations
4430 : : described in [over.match.copy], [over.match.conv], and [over.match.ref].
4431 : : If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
4432 : : per [dcl.init.ref], so we ignore temporary bindings. */
4433 : :
4434 : : static struct z_candidate *
4435 : 51287835 : build_user_type_conversion_1 (tree totype, tree expr, int flags,
4436 : : tsubst_flags_t complain)
4437 : : {
4438 : 51287835 : struct z_candidate *candidates, *cand;
4439 : 51287835 : tree fromtype;
4440 : 51287835 : tree ctors = NULL_TREE;
4441 : 51287835 : tree conv_fns = NULL_TREE;
4442 : 51287835 : conversion *conv = NULL;
4443 : 51287835 : tree first_arg = NULL_TREE;
4444 : 51287835 : vec<tree, va_gc> *args = NULL;
4445 : 51287835 : bool any_viable_p;
4446 : 51287835 : int convflags;
4447 : :
4448 : 51287835 : if (!expr)
4449 : : return NULL;
4450 : :
4451 : 51287829 : fromtype = TREE_TYPE (expr);
4452 : :
4453 : : /* We represent conversion within a hierarchy using RVALUE_CONV and
4454 : : BASE_CONV, as specified by [over.best.ics]; these become plain
4455 : : constructor calls, as specified in [dcl.init]. */
4456 : 51287829 : gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
4457 : : || !DERIVED_FROM_P (totype, fromtype));
4458 : :
4459 : 51287829 : if (CLASS_TYPE_P (totype))
4460 : : /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
4461 : : creating a garbage BASELINK; constructors can't be inherited. */
4462 : 31210352 : ctors = get_class_binding (totype, complete_ctor_identifier);
4463 : :
4464 : 51287829 : tree to_nonref = non_reference (totype);
4465 : 51287829 : if (MAYBE_CLASS_TYPE_P (fromtype))
4466 : : {
4467 : 31845806 : if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
4468 : 31817416 : (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
4469 : 25463432 : && DERIVED_FROM_P (to_nonref, fromtype)))
4470 : : {
4471 : : /* [class.conv.fct] A conversion function is never used to
4472 : : convert a (possibly cv-qualified) object to the (possibly
4473 : : cv-qualified) same object type (or a reference to it), to a
4474 : : (possibly cv-qualified) base class of that type (or a
4475 : : reference to it)... */
4476 : : }
4477 : : else
4478 : 31817413 : conv_fns = lookup_conversions (fromtype);
4479 : : }
4480 : :
4481 : 51287829 : candidates = 0;
4482 : 51287829 : flags |= LOOKUP_NO_CONVERSION;
4483 : 51287829 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4484 : 594080 : flags |= LOOKUP_NO_NARROWING;
4485 : : /* Prevent add_candidates from treating a non-strictly viable candidate
4486 : : as unviable. */
4487 : 51287829 : complain |= tf_conv;
4488 : :
4489 : : /* It's OK to bind a temporary for converting constructor arguments, but
4490 : : not in converting the return value of a conversion operator. */
4491 : 51287829 : convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION
4492 : 51287829 : | (flags & LOOKUP_NO_NARROWING));
4493 : 51287829 : flags &= ~LOOKUP_NO_TEMP_BIND;
4494 : :
4495 : 51287829 : if (ctors)
4496 : : {
4497 : 31066731 : int ctorflags = flags;
4498 : :
4499 : 31066731 : first_arg = build_dummy_object (totype);
4500 : :
4501 : : /* We should never try to call the abstract or base constructor
4502 : : from here. */
4503 : 218348259 : gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_FIRST (ctors))
4504 : : && !DECL_HAS_VTT_PARM_P (OVL_FIRST (ctors)));
4505 : :
4506 : 31066731 : args = make_tree_vector_single (expr);
4507 : 31066731 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4508 : : {
4509 : : /* List-initialization. */
4510 : 594080 : add_list_candidates (ctors, first_arg, args, totype, NULL_TREE,
4511 : 594080 : false, TYPE_BINFO (totype), TYPE_BINFO (totype),
4512 : : ctorflags, &candidates, complain);
4513 : : }
4514 : : else
4515 : : {
4516 : 30472651 : add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
4517 : 30472651 : TYPE_BINFO (totype), TYPE_BINFO (totype),
4518 : : ctorflags, &candidates, complain);
4519 : : }
4520 : :
4521 : 178873500 : for (cand = candidates; cand; cand = cand->next)
4522 : : {
4523 : 147806769 : cand->second_conv = build_identity_conv (totype, NULL_TREE);
4524 : :
4525 : : /* If totype isn't a reference, and LOOKUP_ONLYCONVERTING is
4526 : : set, then this is copy-initialization. In that case, "The
4527 : : result of the call is then used to direct-initialize the
4528 : : object that is the destination of the copy-initialization."
4529 : : [dcl.init]
4530 : :
4531 : : We represent this in the conversion sequence with an
4532 : : rvalue conversion, which means a constructor call. */
4533 : 147806769 : if (!TYPE_REF_P (totype)
4534 : 147806769 : && cxx_dialect < cxx17
4535 : 384290 : && (flags & LOOKUP_ONLYCONVERTING)
4536 : 330671 : && !(convflags & LOOKUP_NO_TEMP_BIND))
4537 : 96834 : cand->second_conv
4538 : 96834 : = build_conv (ck_rvalue, totype, cand->second_conv);
4539 : : }
4540 : : }
4541 : :
4542 : 51287829 : if (conv_fns)
4543 : : {
4544 : 12322607 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4545 : 0 : first_arg = CONSTRUCTOR_ELT (expr, 0)->value;
4546 : : else
4547 : 51287829 : first_arg = expr;
4548 : : }
4549 : :
4550 : 64933845 : for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
4551 : : {
4552 : 13646016 : tree conversion_path = TREE_PURPOSE (conv_fns);
4553 : 13646016 : struct z_candidate *old_candidates;
4554 : :
4555 : : /* If LOOKUP_NO_CONVERSION, don't consider a conversion function that
4556 : : would need an addional user-defined conversion, i.e. if the return
4557 : : type differs in class-ness from the desired type. So we avoid
4558 : : considering operator bool when calling a copy constructor.
4559 : :
4560 : : This optimization avoids the failure in PR97600, and is allowed by
4561 : : [temp.inst]/9: "If the function selected by overload resolution can be
4562 : : determined without instantiating a class template definition, it is
4563 : : unspecified whether that instantiation actually takes place." */
4564 : 13646016 : tree convtype = non_reference (TREE_TYPE (conv_fns));
4565 : 18702688 : if ((flags & LOOKUP_NO_CONVERSION)
4566 : 13646016 : && !WILDCARD_TYPE_P (convtype)
4567 : 26541714 : && (CLASS_TYPE_P (to_nonref)
4568 : 13270857 : != CLASS_TYPE_P (convtype)))
4569 : 5056672 : continue;
4570 : :
4571 : : /* If we are called to convert to a reference type, we are trying to
4572 : : find a direct binding, so don't even consider temporaries. If
4573 : : we don't find a direct binding, the caller will try again to
4574 : : look for a temporary binding. */
4575 : 8589344 : if (TYPE_REF_P (totype))
4576 : 2168693 : convflags |= LOOKUP_NO_TEMP_BIND;
4577 : :
4578 : 8589344 : old_candidates = candidates;
4579 : 8589344 : add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
4580 : : NULL_TREE, false,
4581 : 8589344 : conversion_path, TYPE_BINFO (fromtype),
4582 : : flags, &candidates, complain);
4583 : :
4584 : 17178683 : for (cand = candidates; cand != old_candidates; cand = cand->next)
4585 : : {
4586 : 8589339 : if (cand->viable == 0)
4587 : : /* Already rejected, don't change to -1. */
4588 : 2000411 : continue;
4589 : :
4590 : 6588928 : tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
4591 : 6588928 : conversion *ics
4592 : 6588928 : = implicit_conversion (totype,
4593 : : rettype,
4594 : : 0,
4595 : : /*c_cast_p=*/false, convflags,
4596 : : complain);
4597 : :
4598 : : /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
4599 : : copy-initialization. In that case, "The result of the
4600 : : call is then used to direct-initialize the object that is
4601 : : the destination of the copy-initialization." [dcl.init]
4602 : :
4603 : : We represent this in the conversion sequence with an
4604 : : rvalue conversion, which means a constructor call. But
4605 : : don't add a second rvalue conversion if there's already
4606 : : one there. Which there really shouldn't be, but it's
4607 : : harmless since we'd add it here anyway. */
4608 : 2954715 : if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
4609 : 7094600 : && !(convflags & LOOKUP_NO_TEMP_BIND))
4610 : 206366 : ics = build_conv (ck_rvalue, totype, ics);
4611 : :
4612 : 6588928 : cand->second_conv = ics;
4613 : :
4614 : 6588928 : if (!ics)
4615 : : {
4616 : 3634213 : cand->viable = 0;
4617 : 7267399 : cand->reason = arg_conversion_rejection (NULL_TREE, -2,
4618 : : rettype, totype,
4619 : 3634213 : EXPR_LOCATION (expr));
4620 : : }
4621 : 10841 : else if (TYPE_REF_P (totype) && !ics->rvaluedness_matches_p
4622 : : /* Limit this to non-templates for now (PR90546). */
4623 : 222 : && !cand->template_decl
4624 : 2954932 : && TREE_CODE (TREE_TYPE (totype)) != FUNCTION_TYPE)
4625 : : {
4626 : : /* If we are called to convert to a reference type, we are trying
4627 : : to find a direct binding per [over.match.ref], so rvaluedness
4628 : : must match for non-functions. */
4629 : 211 : cand->viable = 0;
4630 : : }
4631 : 2954504 : else if (DECL_NONCONVERTING_P (cand->fn)
4632 : 2954504 : && ics->rank > cr_exact)
4633 : : {
4634 : : /* 13.3.1.5: For direct-initialization, those explicit
4635 : : conversion functions that are not hidden within S and
4636 : : yield type T or a type that can be converted to type T
4637 : : with a qualification conversion (4.4) are also candidate
4638 : : functions. */
4639 : : /* 13.3.1.6 doesn't have a parallel restriction, but it should;
4640 : : I've raised this issue with the committee. --jason 9/2011 */
4641 : 1998 : cand->viable = -1;
4642 : 1998 : cand->reason = explicit_conversion_rejection (rettype, totype);
4643 : : }
4644 : 2952506 : else if (cand->viable == 1 && ics->bad_p)
4645 : : {
4646 : 1201 : cand->viable = -1;
4647 : 1201 : cand->reason
4648 : 2402 : = bad_arg_conversion_rejection (NULL_TREE, -2,
4649 : : rettype, totype,
4650 : 1201 : EXPR_LOCATION (expr));
4651 : : }
4652 : 2951305 : else if (primary_template_specialization_p (cand->fn)
4653 : 2951305 : && ics->rank > cr_exact)
4654 : : {
4655 : : /* 13.3.3.1.2: If the user-defined conversion is specified by
4656 : : a specialization of a conversion function template, the
4657 : : second standard conversion sequence shall have exact match
4658 : : rank. */
4659 : 21 : cand->viable = -1;
4660 : 21 : cand->reason = template_conversion_rejection (rettype, totype);
4661 : : }
4662 : : }
4663 : : }
4664 : :
4665 : 51287829 : candidates = splice_viable (candidates, false, &any_viable_p);
4666 : 51287829 : if (!any_viable_p)
4667 : : {
4668 : 42885926 : if (args)
4669 : 25102469 : release_tree_vector (args);
4670 : 42885926 : return NULL;
4671 : : }
4672 : :
4673 : 8401903 : cand = tourney (candidates, complain);
4674 : 8401903 : if (cand == NULL)
4675 : : {
4676 : 1335 : if (complain & tf_error)
4677 : : {
4678 : 71 : auto_diagnostic_group d;
4679 : 74 : error_at (cp_expr_loc_or_input_loc (expr),
4680 : : "conversion from %qH to %qI is ambiguous",
4681 : : fromtype, totype);
4682 : 71 : print_z_candidates (location_of (expr), candidates);
4683 : 71 : }
4684 : :
4685 : 1335 : cand = candidates; /* any one will do */
4686 : 1335 : cand->second_conv = build_ambiguous_conv (totype, expr);
4687 : 1335 : cand->second_conv->user_conv_p = true;
4688 : 2670 : if (!any_strictly_viable (candidates))
4689 : 12 : cand->second_conv->bad_p = true;
4690 : 1335 : if (flags & LOOKUP_ONLYCONVERTING)
4691 : 1265 : cand->second_conv->need_temporary_p = true;
4692 : : /* If there are viable candidates, don't set ICS_BAD_FLAG; an
4693 : : ambiguous conversion is no worse than another user-defined
4694 : : conversion. */
4695 : :
4696 : 1335 : return cand;
4697 : : }
4698 : :
4699 : : /* Maybe pass { } as iterators instead of an initializer_list. */
4700 : 8400568 : if (tree iters = maybe_init_list_as_range (cand->fn, expr))
4701 : 84 : if (z_candidate *cand2
4702 : 42 : = build_user_type_conversion_1 (totype, iters, flags, tf_none))
4703 : 42 : if (cand2->viable == 1 && !is_list_ctor (cand2->fn))
4704 : : {
4705 : : cand = cand2;
4706 : : expr = iters;
4707 : : }
4708 : :
4709 : 8400568 : tree convtype;
4710 : 16801136 : if (!DECL_CONSTRUCTOR_P (cand->fn))
4711 : 2943173 : convtype = non_reference (TREE_TYPE (TREE_TYPE (cand->fn)));
4712 : 5457395 : else if (cand->second_conv->kind == ck_rvalue)
4713 : : /* DR 5: [in the first step of copy-initialization]...if the function
4714 : : is a constructor, the call initializes a temporary of the
4715 : : cv-unqualified version of the destination type. */
4716 : 5028 : convtype = cv_unqualified (totype);
4717 : : else
4718 : : convtype = totype;
4719 : : /* Build the user conversion sequence. */
4720 : 8400568 : conv = build_conv
4721 : 8400568 : (ck_user,
4722 : : convtype,
4723 : 8400568 : build_identity_conv (TREE_TYPE (expr), expr));
4724 : 8400568 : conv->cand = cand;
4725 : 8400568 : if (cand->viable == -1)
4726 : 20108 : conv->bad_p = true;
4727 : :
4728 : : /* Remember that this was a list-initialization. */
4729 : 8400568 : if (flags & LOOKUP_NO_NARROWING)
4730 : 1470138 : conv->check_narrowing = true;
4731 : :
4732 : : /* Combine it with the second conversion sequence. */
4733 : 8400568 : cand->second_conv = merge_conversion_sequences (conv,
4734 : : cand->second_conv);
4735 : :
4736 : 8400568 : return cand;
4737 : : }
4738 : :
4739 : : /* Wrapper for above. */
4740 : :
4741 : : tree
4742 : 14704 : build_user_type_conversion (tree totype, tree expr, int flags,
4743 : : tsubst_flags_t complain)
4744 : : {
4745 : 14704 : struct z_candidate *cand;
4746 : 14704 : tree ret;
4747 : :
4748 : 14704 : auto_cond_timevar tv (TV_OVERLOAD);
4749 : :
4750 : 14704 : conversion_obstack_sentinel cos;
4751 : :
4752 : 14704 : cand = build_user_type_conversion_1 (totype, expr, flags, complain);
4753 : :
4754 : 14704 : if (cand)
4755 : : {
4756 : 14525 : if (cand->second_conv->kind == ck_ambig)
4757 : 65 : ret = error_mark_node;
4758 : : else
4759 : : {
4760 : 14460 : expr = convert_like (cand->second_conv, expr, complain);
4761 : 14460 : ret = convert_from_reference (expr);
4762 : : }
4763 : : }
4764 : : else
4765 : : ret = NULL_TREE;
4766 : :
4767 : 29408 : return ret;
4768 : 14704 : }
4769 : :
4770 : : /* Give a helpful diagnostic when implicit_conversion fails. */
4771 : :
4772 : : static void
4773 : 598 : implicit_conversion_error (location_t loc, tree type, tree expr)
4774 : : {
4775 : 598 : tsubst_flags_t complain = tf_warning_or_error;
4776 : :
4777 : : /* If expr has unknown type, then it is an overloaded function.
4778 : : Call instantiate_type to get good error messages. */
4779 : 598 : if (TREE_TYPE (expr) == unknown_type_node)
4780 : 66 : instantiate_type (type, expr, complain);
4781 : 532 : else if (invalid_nonstatic_memfn_p (loc, expr, complain))
4782 : : /* We gave an error. */;
4783 : 62 : else if (BRACE_ENCLOSED_INITIALIZER_P (expr)
4784 : 59 : && CONSTRUCTOR_IS_DESIGNATED_INIT (expr)
4785 : 545 : && !CP_AGGREGATE_TYPE_P (type))
4786 : 18 : error_at (loc, "designated initializers cannot be used with a "
4787 : : "non-aggregate type %qT", type);
4788 : : else
4789 : : {
4790 : 509 : range_label_for_type_mismatch label (TREE_TYPE (expr), type);
4791 : 509 : gcc_rich_location rich_loc (loc, &label,
4792 : 509 : highlight_colors::percent_h);
4793 : 509 : error_at (&rich_loc, "could not convert %qE from %qH to %qI",
4794 : 509 : expr, TREE_TYPE (expr), type);
4795 : 509 : }
4796 : 598 : }
4797 : :
4798 : : /* Worker for build_converted_constant_expr. */
4799 : :
4800 : : static tree
4801 : 235303959 : build_converted_constant_expr_internal (tree type, tree expr,
4802 : : int flags, tsubst_flags_t complain)
4803 : : {
4804 : 235303959 : conversion *conv;
4805 : 235303959 : tree t;
4806 : 235303959 : location_t loc = cp_expr_loc_or_input_loc (expr);
4807 : :
4808 : 235303959 : if (error_operand_p (expr))
4809 : 49 : return error_mark_node;
4810 : :
4811 : 235303910 : conversion_obstack_sentinel cos;
4812 : :
4813 : 235303910 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
4814 : : /*c_cast_p=*/false, flags, complain);
4815 : :
4816 : : /* A converted constant expression of type T is an expression, implicitly
4817 : : converted to type T, where the converted expression is a constant
4818 : : expression and the implicit conversion sequence contains only
4819 : :
4820 : : * user-defined conversions,
4821 : : * lvalue-to-rvalue conversions (7.1),
4822 : : * array-to-pointer conversions (7.2),
4823 : : * function-to-pointer conversions (7.3),
4824 : : * qualification conversions (7.5),
4825 : : * integral promotions (7.6),
4826 : : * integral conversions (7.8) other than narrowing conversions (11.6.4),
4827 : : * null pointer conversions (7.11) from std::nullptr_t,
4828 : : * null member pointer conversions (7.12) from std::nullptr_t, and
4829 : : * function pointer conversions (7.13),
4830 : :
4831 : : and where the reference binding (if any) binds directly. */
4832 : :
4833 : 235303910 : for (conversion *c = conv;
4834 : 265873376 : c && c->kind != ck_identity;
4835 : 30569466 : c = next_conversion (c))
4836 : : {
4837 : 30569466 : switch (c->kind)
4838 : : {
4839 : : /* A conversion function is OK. If it isn't constexpr, we'll
4840 : : complain later that the argument isn't constant. */
4841 : : case ck_user:
4842 : : /* List-initialization is OK. */
4843 : : case ck_aggr:
4844 : : /* The lvalue-to-rvalue conversion is OK. */
4845 : : case ck_rvalue:
4846 : : /* Array-to-pointer and function-to-pointer. */
4847 : : case ck_lvalue:
4848 : : /* Function pointer conversions. */
4849 : : case ck_fnptr:
4850 : : /* Qualification conversions. */
4851 : : case ck_qual:
4852 : : break;
4853 : :
4854 : 340 : case ck_ref_bind:
4855 : 340 : if (c->need_temporary_p)
4856 : : {
4857 : 0 : if (complain & tf_error)
4858 : 0 : error_at (loc, "initializing %qH with %qI in converted "
4859 : : "constant expression does not bind directly",
4860 : 0 : type, next_conversion (c)->type);
4861 : : conv = NULL;
4862 : : }
4863 : : break;
4864 : :
4865 : 6985227 : case ck_base:
4866 : 6985227 : case ck_pmem:
4867 : 6985227 : case ck_ptr:
4868 : 6985227 : case ck_std:
4869 : 6985227 : t = next_conversion (c)->type;
4870 : 6985227 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (t)
4871 : 6985168 : && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4872 : : /* Integral promotion or conversion. */
4873 : : break;
4874 : 81 : if (NULLPTR_TYPE_P (t))
4875 : : /* Conversion from nullptr to pointer or pointer-to-member. */
4876 : : break;
4877 : :
4878 : 81 : if (complain & tf_error)
4879 : 61 : error_at (loc, "conversion from %qH to %qI in a "
4880 : : "converted constant expression", t, type);
4881 : : /* fall through. */
4882 : :
4883 : : default:
4884 : : conv = NULL;
4885 : : break;
4886 : : }
4887 : : }
4888 : :
4889 : : /* Avoid confusing convert_nontype_argument by introducing
4890 : : a redundant conversion to the same reference type. */
4891 : 235303707 : if (conv && conv->kind == ck_ref_bind
4892 : 235304250 : && REFERENCE_REF_P (expr))
4893 : : {
4894 : 129 : tree ref = TREE_OPERAND (expr, 0);
4895 : 129 : if (same_type_p (type, TREE_TYPE (ref)))
4896 : : return ref;
4897 : : }
4898 : :
4899 : 235303803 : if (conv)
4900 : : {
4901 : : /* Don't copy a class in a template. */
4902 : 1882 : if (CLASS_TYPE_P (type) && conv->kind == ck_rvalue
4903 : 235305195 : && processing_template_decl)
4904 : 51 : conv = next_conversion (conv);
4905 : :
4906 : : /* Issuing conversion warnings for value-dependent expressions is
4907 : : likely too noisy. */
4908 : 235303600 : warning_sentinel w (warn_conversion);
4909 : 235303600 : conv->check_narrowing = true;
4910 : 235303600 : conv->check_narrowing_const_only = true;
4911 : 235303600 : expr = convert_like (conv, expr, complain);
4912 : 235303600 : }
4913 : : else
4914 : : {
4915 : 203 : if (complain & tf_error)
4916 : 151 : implicit_conversion_error (loc, type, expr);
4917 : 203 : expr = error_mark_node;
4918 : : }
4919 : :
4920 : : return expr;
4921 : 235303910 : }
4922 : :
4923 : : /* Subroutine of convert_nontype_argument.
4924 : :
4925 : : EXPR is an expression used in a context that requires a converted
4926 : : constant-expression, such as a template non-type parameter. Do any
4927 : : necessary conversions (that are permitted for converted
4928 : : constant-expressions) to convert it to the desired type.
4929 : :
4930 : : This function doesn't consider explicit conversion functions. If
4931 : : you mean to use "a contextually converted constant expression of type
4932 : : bool", use build_converted_constant_bool_expr.
4933 : :
4934 : : If conversion is successful, returns the converted expression;
4935 : : otherwise, returns error_mark_node. */
4936 : :
4937 : : tree
4938 : 130169381 : build_converted_constant_expr (tree type, tree expr, tsubst_flags_t complain)
4939 : : {
4940 : 130169381 : return build_converted_constant_expr_internal (type, expr, LOOKUP_IMPLICIT,
4941 : 130169381 : complain);
4942 : : }
4943 : :
4944 : : /* Used to create "a contextually converted constant expression of type
4945 : : bool". This differs from build_converted_constant_expr in that it
4946 : : also considers explicit conversion functions. */
4947 : :
4948 : : tree
4949 : 105134578 : build_converted_constant_bool_expr (tree expr, tsubst_flags_t complain)
4950 : : {
4951 : 105134578 : return build_converted_constant_expr_internal (boolean_type_node, expr,
4952 : 105134578 : LOOKUP_NORMAL, complain);
4953 : : }
4954 : :
4955 : : /* Do any initial processing on the arguments to a function call. */
4956 : :
4957 : : vec<tree, va_gc> *
4958 : 136180425 : resolve_args (vec<tree, va_gc> *args, tsubst_flags_t complain)
4959 : : {
4960 : 136180425 : unsigned int ix;
4961 : 136180425 : tree arg;
4962 : :
4963 : 253294146 : FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
4964 : : {
4965 : 117118696 : if (error_operand_p (arg))
4966 : : return NULL;
4967 : 117113821 : else if (VOID_TYPE_P (TREE_TYPE (arg)))
4968 : : {
4969 : 73 : if (complain & tf_error)
4970 : 12 : error_at (cp_expr_loc_or_input_loc (arg),
4971 : : "invalid use of void expression");
4972 : 73 : return NULL;
4973 : : }
4974 : 117113748 : else if (invalid_nonstatic_memfn_p (EXPR_LOCATION (arg), arg, complain))
4975 : : return NULL;
4976 : :
4977 : : /* Force auto deduction now. Omit tf_warning to avoid redundant
4978 : : deprecated warning on deprecated-14.C. */
4979 : 117113721 : if (!mark_single_function (arg, complain & ~tf_warning))
4980 : : return NULL;
4981 : : }
4982 : : return args;
4983 : : }
4984 : :
4985 : : /* Perform overload resolution on FN, which is called with the ARGS.
4986 : :
4987 : : Return the candidate function selected by overload resolution, or
4988 : : NULL if the event that overload resolution failed. In the case
4989 : : that overload resolution fails, *CANDIDATES will be the set of
4990 : : candidates considered, and ANY_VIABLE_P will be set to true or
4991 : : false to indicate whether or not any of the candidates were
4992 : : viable.
4993 : :
4994 : : The ARGS should already have gone through RESOLVE_ARGS before this
4995 : : function is called. */
4996 : :
4997 : : static struct z_candidate *
4998 : 61699207 : perform_overload_resolution (tree fn,
4999 : : const vec<tree, va_gc> *args,
5000 : : struct z_candidate **candidates,
5001 : : bool *any_viable_p, tsubst_flags_t complain)
5002 : : {
5003 : 61699207 : struct z_candidate *cand;
5004 : 61699207 : tree explicit_targs;
5005 : 61699207 : int template_only;
5006 : :
5007 : 61699207 : auto_cond_timevar tv (TV_OVERLOAD);
5008 : :
5009 : 61699207 : explicit_targs = NULL_TREE;
5010 : 61699207 : template_only = 0;
5011 : :
5012 : 61699207 : *candidates = NULL;
5013 : 61699207 : *any_viable_p = true;
5014 : :
5015 : : /* Check FN. */
5016 : 61699207 : gcc_assert (OVL_P (fn) || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
5017 : :
5018 : 61699207 : if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
5019 : : {
5020 : 15018193 : explicit_targs = TREE_OPERAND (fn, 1);
5021 : 15018193 : fn = TREE_OPERAND (fn, 0);
5022 : 15018193 : template_only = 1;
5023 : : }
5024 : :
5025 : : /* Add the various candidate functions. */
5026 : 61699207 : add_candidates (fn, NULL_TREE, args, NULL_TREE,
5027 : : explicit_targs, template_only,
5028 : : /*conversion_path=*/NULL_TREE,
5029 : : /*access_path=*/NULL_TREE,
5030 : : LOOKUP_NORMAL,
5031 : : candidates, complain);
5032 : :
5033 : 61685689 : *candidates = splice_viable (*candidates, false, any_viable_p);
5034 : 61685689 : if (*any_viable_p)
5035 : 61666934 : cand = tourney (*candidates, complain);
5036 : : else
5037 : : cand = NULL;
5038 : :
5039 : 123371378 : return cand;
5040 : 61685689 : }
5041 : :
5042 : : /* Print an error message about being unable to build a call to FN with
5043 : : ARGS. ANY_VIABLE_P indicates whether any candidate functions could
5044 : : be located; CANDIDATES is a possibly empty list of such
5045 : : functions. */
5046 : :
5047 : : static void
5048 : 2784 : print_error_for_call_failure (tree fn, const vec<tree, va_gc> *args,
5049 : : struct z_candidate *candidates)
5050 : : {
5051 : 2784 : tree targs = NULL_TREE;
5052 : 2784 : if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
5053 : : {
5054 : 460 : targs = TREE_OPERAND (fn, 1);
5055 : 460 : fn = TREE_OPERAND (fn, 0);
5056 : : }
5057 : 5568 : tree name = OVL_NAME (fn);
5058 : 2784 : location_t loc = location_of (name);
5059 : 2784 : if (targs)
5060 : 460 : name = lookup_template_function (name, targs);
5061 : :
5062 : 2784 : auto_diagnostic_group d;
5063 : 5568 : if (!any_strictly_viable (candidates))
5064 : 2316 : error_at (loc, "no matching function for call to %<%D(%A)%>",
5065 : : name, build_tree_list_vec (args));
5066 : : else
5067 : 468 : error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous",
5068 : : name, build_tree_list_vec (args));
5069 : 2784 : if (candidates)
5070 : 2784 : print_z_candidates (loc, candidates);
5071 : 2784 : }
5072 : :
5073 : : /* Perform overload resolution on the set of deduction guides DGUIDES
5074 : : using ARGS. Returns the selected deduction guide, or error_mark_node
5075 : : if overload resolution fails. */
5076 : :
5077 : : tree
5078 : 12618 : perform_dguide_overload_resolution (tree dguides, const vec<tree, va_gc> *args,
5079 : : tsubst_flags_t complain)
5080 : : {
5081 : 12618 : z_candidate *candidates;
5082 : 12618 : bool any_viable_p;
5083 : 12618 : tree result;
5084 : :
5085 : 25236 : gcc_assert (deduction_guide_p (OVL_FIRST (dguides)));
5086 : :
5087 : 12618 : conversion_obstack_sentinel cos;
5088 : :
5089 : 12618 : z_candidate *cand = perform_overload_resolution (dguides, args, &candidates,
5090 : : &any_viable_p, complain);
5091 : 12618 : if (!cand)
5092 : : {
5093 : 813 : if (complain & tf_error)
5094 : 188 : print_error_for_call_failure (dguides, args, candidates);
5095 : 813 : result = error_mark_node;
5096 : : }
5097 : : else
5098 : 11805 : result = cand->fn;
5099 : :
5100 : 25236 : return result;
5101 : 12618 : }
5102 : :
5103 : : /* Return an expression for a call to FN (a namespace-scope function,
5104 : : or a static member function) with the ARGS. This may change
5105 : : ARGS. */
5106 : :
5107 : : tree
5108 : 61248050 : build_new_function_call (tree fn, vec<tree, va_gc> **args,
5109 : : tsubst_flags_t complain)
5110 : : {
5111 : 61248050 : struct z_candidate *candidates, *cand;
5112 : 61248050 : bool any_viable_p;
5113 : 61248050 : tree result;
5114 : :
5115 : 61248050 : if (args != NULL && *args != NULL)
5116 : : {
5117 : 61248050 : *args = resolve_args (*args, complain);
5118 : 61248050 : if (*args == NULL)
5119 : 299 : return error_mark_node;
5120 : : }
5121 : :
5122 : 61247751 : if (flag_tm)
5123 : 2074 : tm_malloc_replacement (fn);
5124 : :
5125 : 61247751 : conversion_obstack_sentinel cos;
5126 : :
5127 : 61247751 : cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
5128 : : complain);
5129 : :
5130 : 61234233 : if (!cand)
5131 : : {
5132 : 66374 : if (complain & tf_error)
5133 : : {
5134 : : // If there is a single (non-viable) function candidate,
5135 : : // let the error be diagnosed by cp_build_function_call_vec.
5136 : 2995 : if (!any_viable_p && candidates && ! candidates->next
5137 : 1241 : && TREE_CODE (candidates->fn) == FUNCTION_DECL
5138 : : /* A template-id callee consisting of a single (ignored)
5139 : : non-template candidate needs to be diagnosed the
5140 : : ordinary way. */
5141 : 416 : && (TREE_CODE (fn) != TEMPLATE_ID_EXPR
5142 : 29 : || candidates->template_decl))
5143 : 408 : return cp_build_function_call_vec (candidates->fn, args, complain);
5144 : :
5145 : : // Otherwise, emit notes for non-viable candidates.
5146 : 2587 : print_error_for_call_failure (fn, *args, candidates);
5147 : : }
5148 : 65966 : result = error_mark_node;
5149 : : }
5150 : : else
5151 : : {
5152 : 61167859 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
5153 : : }
5154 : :
5155 : 61233825 : if (flag_coroutines
5156 : 28444410 : && result
5157 : 28444410 : && TREE_CODE (result) == CALL_EXPR
5158 : 78138086 : && DECL_BUILT_IN_CLASS (TREE_OPERAND (CALL_EXPR_FN (result), 0))
5159 : 16904261 : == BUILT_IN_NORMAL)
5160 : 2442374 : result = coro_validate_builtin_call (result);
5161 : :
5162 : : return result;
5163 : 61234233 : }
5164 : :
5165 : : /* Build a call to a global operator new. FNNAME is the name of the
5166 : : operator (either "operator new" or "operator new[]") and ARGS are
5167 : : the arguments provided. This may change ARGS. *SIZE points to the
5168 : : total number of bytes required by the allocation, and is updated if
5169 : : that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
5170 : : be used. If this function determines that no cookie should be
5171 : : used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK
5172 : : is not NULL_TREE, it is evaluated before calculating the final
5173 : : array size, and if it fails, the array size is replaced with
5174 : : (size_t)-1 (usually triggering a std::bad_alloc exception). If FN
5175 : : is non-NULL, it will be set, upon return, to the allocation
5176 : : function called. */
5177 : :
5178 : : tree
5179 : 438829 : build_operator_new_call (tree fnname, vec<tree, va_gc> **args,
5180 : : tree *size, tree *cookie_size,
5181 : : tree align_arg, tree size_check,
5182 : : tree *fn, tsubst_flags_t complain)
5183 : : {
5184 : 438829 : tree original_size = *size;
5185 : 438829 : tree fns;
5186 : 438829 : struct z_candidate *candidates;
5187 : 438829 : struct z_candidate *cand = NULL;
5188 : 438829 : bool any_viable_p;
5189 : :
5190 : 438829 : if (fn)
5191 : 437616 : *fn = NULL_TREE;
5192 : : /* Set to (size_t)-1 if the size check fails. */
5193 : 438829 : if (size_check != NULL_TREE)
5194 : : {
5195 : 11163 : tree errval = TYPE_MAX_VALUE (sizetype);
5196 : 11163 : if (cxx_dialect >= cxx11 && flag_exceptions)
5197 : 10820 : errval = throw_bad_array_new_length ();
5198 : 11163 : *size = fold_build3 (COND_EXPR, sizetype, size_check,
5199 : : original_size, errval);
5200 : : }
5201 : 438829 : vec_safe_insert (*args, 0, *size);
5202 : 438829 : *args = resolve_args (*args, complain);
5203 : 438829 : if (*args == NULL)
5204 : 0 : return error_mark_node;
5205 : :
5206 : 438829 : conversion_obstack_sentinel cos;
5207 : :
5208 : : /* Based on:
5209 : :
5210 : : [expr.new]
5211 : :
5212 : : If this lookup fails to find the name, or if the allocated type
5213 : : is not a class type, the allocation function's name is looked
5214 : : up in the global scope.
5215 : :
5216 : : we disregard block-scope declarations of "operator new". */
5217 : 438829 : fns = lookup_qualified_name (global_namespace, fnname);
5218 : :
5219 : 438829 : if (align_arg)
5220 : : {
5221 : 44 : vec<tree, va_gc>* align_args
5222 : 44 : = vec_copy_and_insert (*args, align_arg, 1);
5223 : 44 : cand = perform_overload_resolution (fns, align_args, &candidates,
5224 : : &any_viable_p, tf_none);
5225 : 44 : if (cand)
5226 : 35 : *args = align_args;
5227 : : /* If no aligned allocation function matches, try again without the
5228 : : alignment. */
5229 : : }
5230 : :
5231 : : /* Figure out what function is being called. */
5232 : 35 : if (!cand)
5233 : 438794 : cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p,
5234 : : complain);
5235 : :
5236 : : /* If no suitable function could be found, issue an error message
5237 : : and give up. */
5238 : 438829 : if (!cand)
5239 : : {
5240 : 9 : if (complain & tf_error)
5241 : 9 : print_error_for_call_failure (fns, *args, candidates);
5242 : 9 : return error_mark_node;
5243 : : }
5244 : :
5245 : : /* If a cookie is required, add some extra space. Whether
5246 : : or not a cookie is required cannot be determined until
5247 : : after we know which function was called. */
5248 : 438820 : if (*cookie_size)
5249 : : {
5250 : 255 : bool use_cookie = true;
5251 : 255 : tree arg_types;
5252 : :
5253 : 255 : arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
5254 : : /* Skip the size_t parameter. */
5255 : 255 : arg_types = TREE_CHAIN (arg_types);
5256 : : /* Check the remaining parameters (if any). */
5257 : 255 : if (arg_types
5258 : 255 : && TREE_CHAIN (arg_types) == void_list_node
5259 : 313 : && same_type_p (TREE_VALUE (arg_types),
5260 : : ptr_type_node))
5261 : 43 : use_cookie = false;
5262 : : /* If we need a cookie, adjust the number of bytes allocated. */
5263 : 255 : if (use_cookie)
5264 : : {
5265 : : /* Update the total size. */
5266 : 212 : *size = size_binop (PLUS_EXPR, original_size, *cookie_size);
5267 : 212 : if (size_check)
5268 : : {
5269 : : /* Set to (size_t)-1 if the size check fails. */
5270 : 44 : gcc_assert (size_check != NULL_TREE);
5271 : 44 : *size = fold_build3 (COND_EXPR, sizetype, size_check,
5272 : : *size, TYPE_MAX_VALUE (sizetype));
5273 : : }
5274 : : /* Update the argument list to reflect the adjusted size. */
5275 : 212 : (**args)[0] = *size;
5276 : : }
5277 : : else
5278 : 43 : *cookie_size = NULL_TREE;
5279 : : }
5280 : :
5281 : : /* Tell our caller which function we decided to call. */
5282 : 438820 : if (fn)
5283 : 437607 : *fn = cand->fn;
5284 : :
5285 : : /* Build the CALL_EXPR. */
5286 : 438820 : tree ret = build_over_call (cand, LOOKUP_NORMAL, complain);
5287 : :
5288 : : /* Set this flag for all callers of this function. In addition to
5289 : : new-expressions, this is called for allocating coroutine state; treat
5290 : : that as an implicit new-expression. */
5291 : 438820 : tree call = extract_call_expr (ret);
5292 : 438820 : if (TREE_CODE (call) == CALL_EXPR)
5293 : 438820 : CALL_FROM_NEW_OR_DELETE_P (call) = 1;
5294 : :
5295 : : return ret;
5296 : 438829 : }
5297 : :
5298 : : /* Evaluate side-effects from OBJ before evaluating call
5299 : : to FN in RESULT expression.
5300 : : This is for expressions of the form `obj->fn(...)'
5301 : : where `fn' turns out to be a static member function and
5302 : : `obj' needs to be evaluated. `fn' could be also static operator[]
5303 : : or static operator(), in which cases the source expression
5304 : : would be `obj[...]' or `obj(...)'. */
5305 : :
5306 : : tree
5307 : 57194577 : keep_unused_object_arg (tree result, tree obj, tree fn)
5308 : : {
5309 : 57194577 : if (result == NULL_TREE
5310 : 57194577 : || result == error_mark_node
5311 : 56526361 : || DECL_OBJECT_MEMBER_FUNCTION_P (fn)
5312 : 58779030 : || !TREE_SIDE_EFFECTS (obj))
5313 : : return result;
5314 : :
5315 : : /* But avoid the implicit lvalue-rvalue conversion when `obj' is
5316 : : volatile. */
5317 : 57696 : tree a = obj;
5318 : 57696 : if (TREE_THIS_VOLATILE (a))
5319 : 57342 : a = build_this (a);
5320 : 57696 : if (TREE_SIDE_EFFECTS (a))
5321 : 363 : return cp_build_compound_expr (a, result, tf_error);
5322 : : return result;
5323 : : }
5324 : :
5325 : : /* Build a new call to operator(). This may change ARGS. */
5326 : :
5327 : : tree
5328 : 1465460 : build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
5329 : : {
5330 : 1465460 : struct z_candidate *candidates = 0, *cand;
5331 : 1465460 : tree fns, convs, first_mem_arg = NULL_TREE;
5332 : 1465460 : bool any_viable_p;
5333 : 1465460 : tree result = NULL_TREE;
5334 : :
5335 : 1465460 : auto_cond_timevar tv (TV_OVERLOAD);
5336 : :
5337 : 1465460 : obj = mark_lvalue_use (obj);
5338 : :
5339 : 1465460 : if (error_operand_p (obj))
5340 : 0 : return error_mark_node;
5341 : :
5342 : 1465460 : tree type = TREE_TYPE (obj);
5343 : :
5344 : 1465460 : obj = prep_operand (obj);
5345 : :
5346 : 1465460 : if (TYPE_PTRMEMFUNC_P (type))
5347 : : {
5348 : 0 : if (complain & tf_error)
5349 : : /* It's no good looking for an overloaded operator() on a
5350 : : pointer-to-member-function. */
5351 : 0 : error ("pointer-to-member function %qE cannot be called without "
5352 : : "an object; consider using %<.*%> or %<->*%>", obj);
5353 : 0 : return error_mark_node;
5354 : : }
5355 : :
5356 : 1465460 : if (TYPE_BINFO (type))
5357 : : {
5358 : 1465442 : fns = lookup_fnfields (TYPE_BINFO (type), call_op_identifier, 1, complain);
5359 : 1465442 : if (fns == error_mark_node)
5360 : : return error_mark_node;
5361 : : }
5362 : : else
5363 : : fns = NULL_TREE;
5364 : :
5365 : 1465450 : if (args != NULL && *args != NULL)
5366 : : {
5367 : 1465450 : *args = resolve_args (*args, complain);
5368 : 1465450 : if (*args == NULL)
5369 : 4542 : return error_mark_node;
5370 : : }
5371 : :
5372 : 1460908 : conversion_obstack_sentinel cos;
5373 : :
5374 : 1460908 : if (fns)
5375 : : {
5376 : 1459493 : first_mem_arg = obj;
5377 : :
5378 : 1459493 : add_candidates (BASELINK_FUNCTIONS (fns),
5379 : : first_mem_arg, *args, NULL_TREE,
5380 : : NULL_TREE, false,
5381 : 1459493 : BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
5382 : : LOOKUP_NORMAL, &candidates, complain);
5383 : : }
5384 : :
5385 : 1460908 : bool any_call_ops = candidates != nullptr;
5386 : :
5387 : 1460908 : convs = lookup_conversions (type);
5388 : :
5389 : 1504149 : for (; convs; convs = TREE_CHAIN (convs))
5390 : : {
5391 : 43241 : tree totype = TREE_TYPE (convs);
5392 : :
5393 : 31675 : if (TYPE_PTRFN_P (totype)
5394 : 11569 : || TYPE_REFFN_P (totype)
5395 : 54772 : || (TYPE_REF_P (totype)
5396 : 766 : && TYPE_PTRFN_P (TREE_TYPE (totype))))
5397 : 63530 : for (tree fn : ovl_range (TREE_VALUE (convs)))
5398 : : {
5399 : 31765 : if (DECL_NONCONVERTING_P (fn))
5400 : 3 : continue;
5401 : :
5402 : 31762 : if (TREE_CODE (fn) == TEMPLATE_DECL)
5403 : : {
5404 : : /* Making this work broke PR 71117 and 85118, so until the
5405 : : committee resolves core issue 2189, let's disable this
5406 : : candidate if there are any call operators. */
5407 : 13282 : if (any_call_ops)
5408 : 13270 : continue;
5409 : :
5410 : 12 : add_template_conv_candidate
5411 : 12 : (&candidates, fn, obj, *args, totype,
5412 : : /*access_path=*/NULL_TREE,
5413 : : /*conversion_path=*/NULL_TREE, complain);
5414 : : }
5415 : : else
5416 : 18480 : add_conv_candidate (&candidates, fn, obj,
5417 : : *args, /*conversion_path=*/NULL_TREE,
5418 : : /*access_path=*/NULL_TREE, complain);
5419 : : }
5420 : : }
5421 : :
5422 : : /* Be strict here because if we choose a bad conversion candidate, the
5423 : : errors we get won't mention the call context. */
5424 : 1460908 : candidates = splice_viable (candidates, true, &any_viable_p);
5425 : 1460908 : if (!any_viable_p)
5426 : : {
5427 : 5529 : if (complain & tf_error)
5428 : : {
5429 : 283 : auto_diagnostic_group d;
5430 : 283 : error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
5431 : : build_tree_list_vec (*args));
5432 : 283 : print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
5433 : 283 : }
5434 : 5529 : result = error_mark_node;
5435 : : }
5436 : : else
5437 : : {
5438 : 1455379 : cand = tourney (candidates, complain);
5439 : 1455379 : if (cand == 0)
5440 : : {
5441 : 12 : if (complain & tf_error)
5442 : : {
5443 : 6 : auto_diagnostic_group d;
5444 : 12 : error ("call of %<(%T) (%A)%> is ambiguous",
5445 : 6 : TREE_TYPE (obj), build_tree_list_vec (*args));
5446 : 6 : print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
5447 : 6 : }
5448 : 12 : result = error_mark_node;
5449 : : }
5450 : 1455367 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL
5451 : 1455308 : && DECL_OVERLOADED_OPERATOR_P (cand->fn)
5452 : 2910675 : && DECL_OVERLOADED_OPERATOR_IS (cand->fn, CALL_EXPR))
5453 : : {
5454 : 1455308 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
5455 : : /* In an expression of the form `a()' where cand->fn
5456 : : which is operator() turns out to be a static member function,
5457 : : `a' is none-the-less evaluated. */
5458 : 1455308 : result = keep_unused_object_arg (result, obj, cand->fn);
5459 : : }
5460 : : else
5461 : : {
5462 : 59 : if (TREE_CODE (cand->fn) == FUNCTION_DECL)
5463 : 0 : obj = convert_like_with_context (cand->convs[0], obj, cand->fn,
5464 : : -1, complain);
5465 : : else
5466 : : {
5467 : 59 : gcc_checking_assert (TYPE_P (cand->fn));
5468 : 59 : obj = convert_like (cand->convs[0], obj, complain);
5469 : : }
5470 : 59 : obj = convert_from_reference (obj);
5471 : 59 : result = cp_build_function_call_vec (obj, args, complain);
5472 : : }
5473 : : }
5474 : :
5475 : 1460908 : return result;
5476 : 1465460 : }
5477 : :
5478 : : /* Subroutine for preparing format strings suitable for the error
5479 : : function. It concatenates a prefix (controlled by MATCH), ERRMSG,
5480 : : and SUFFIX. */
5481 : :
5482 : : static const char *
5483 : 1440 : concat_op_error_string (bool match, const char *errmsg, const char *suffix)
5484 : : {
5485 : 1440 : return concat (match
5486 : : ? G_("ambiguous overload for ")
5487 : : : G_("no match for "),
5488 : 0 : errmsg, suffix, nullptr);
5489 : : }
5490 : :
5491 : : /* Called by op_error to prepare format strings suitable for the error
5492 : : function. It concatenates a prefix (controlled by MATCH), ERRMSG,
5493 : : and a suffix (controlled by NTYPES). */
5494 : :
5495 : : static const char *
5496 : 1419 : op_error_string (const char *errmsg, int ntypes, bool match)
5497 : : {
5498 : 1419 : const char *suffix;
5499 : 0 : if (ntypes == 3)
5500 : : suffix = G_(" (operand types are %qT, %qT, and %qT)");
5501 : 0 : else if (ntypes == 2)
5502 : : suffix = G_(" (operand types are %qT and %qT)");
5503 : : else
5504 : 0 : suffix = G_(" (operand type is %qT)");
5505 : 0 : return concat_op_error_string (match, errmsg, suffix);
5506 : : }
5507 : :
5508 : : /* Similar to op_error_string, but a special-case for binary ops that
5509 : : use %e for the args, rather than %qT. */
5510 : :
5511 : : static const char *
5512 : 21 : binop_error_string (const char *errmsg, bool match)
5513 : : {
5514 : 21 : return concat_op_error_string (match, errmsg,
5515 : 21 : G_(" (operand types are %e and %e)"));
5516 : : }
5517 : :
5518 : : static void
5519 : 1440 : op_error (const op_location_t &loc,
5520 : : enum tree_code code, enum tree_code code2,
5521 : : tree arg1, tree arg2, tree arg3, bool match)
5522 : : {
5523 : 1440 : bool assop = code == MODIFY_EXPR;
5524 : 1440 : const char *opname = OVL_OP_INFO (assop, assop ? code2 : code)->name;
5525 : :
5526 : 1440 : switch (code)
5527 : : {
5528 : 0 : case COND_EXPR:
5529 : 0 : if (flag_diagnostics_show_caret)
5530 : 0 : error_at (loc, op_error_string (G_("ternary %<operator?:%>"),
5531 : : 3, match),
5532 : 0 : TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
5533 : : else
5534 : 0 : error_at (loc, op_error_string (G_("ternary %<operator?:%> "
5535 : : "in %<%E ? %E : %E%>"), 3, match),
5536 : : arg1, arg2, arg3,
5537 : 0 : TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
5538 : : break;
5539 : :
5540 : 0 : case POSTINCREMENT_EXPR:
5541 : 0 : case POSTDECREMENT_EXPR:
5542 : 0 : if (flag_diagnostics_show_caret)
5543 : 0 : error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
5544 : 0 : opname, TREE_TYPE (arg1));
5545 : : else
5546 : 0 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
5547 : : 1, match),
5548 : 0 : opname, arg1, opname, TREE_TYPE (arg1));
5549 : : break;
5550 : :
5551 : 13 : case ARRAY_REF:
5552 : 13 : if (flag_diagnostics_show_caret)
5553 : 0 : error_at (loc, op_error_string (G_("%<operator[]%>"), 2, match),
5554 : 0 : TREE_TYPE (arg1), TREE_TYPE (arg2));
5555 : : else
5556 : 26 : error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
5557 : : 2, match),
5558 : 13 : arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2));
5559 : : break;
5560 : :
5561 : 6 : case REALPART_EXPR:
5562 : 6 : case IMAGPART_EXPR:
5563 : 6 : if (flag_diagnostics_show_caret)
5564 : 0 : error_at (loc, op_error_string (G_("%qs"), 1, match),
5565 : 0 : opname, TREE_TYPE (arg1));
5566 : : else
5567 : 12 : error_at (loc, op_error_string (G_("%qs in %<%s %E%>"), 1, match),
5568 : 6 : opname, opname, arg1, TREE_TYPE (arg1));
5569 : : break;
5570 : :
5571 : 0 : case CO_AWAIT_EXPR:
5572 : 0 : if (flag_diagnostics_show_caret)
5573 : 0 : error_at (loc, op_error_string (G_("%<operator %s%>"), 1, match),
5574 : 0 : opname, TREE_TYPE (arg1));
5575 : : else
5576 : 0 : error_at (loc, op_error_string (G_("%<operator %s%> in %<%s%E%>"),
5577 : : 1, match),
5578 : 0 : opname, opname, arg1, TREE_TYPE (arg1));
5579 : : break;
5580 : :
5581 : 1421 : default:
5582 : 1421 : if (arg2)
5583 : 1321 : if (flag_diagnostics_show_caret)
5584 : : {
5585 : 21 : binary_op_rich_location richloc (loc, arg1, arg2, true);
5586 : 21 : pp_markup::element_quoted_type element_0
5587 : 21 : (TREE_TYPE (arg1), highlight_colors::lhs);
5588 : 21 : pp_markup::element_quoted_type element_1
5589 : 21 : (TREE_TYPE (arg2), highlight_colors::rhs);
5590 : 21 : error_at (&richloc,
5591 : : binop_error_string (G_("%<operator%s%>"), match),
5592 : : opname, &element_0, &element_1);
5593 : 21 : }
5594 : : else
5595 : 2600 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
5596 : : 2, match),
5597 : : opname, arg1, opname, arg2,
5598 : 1300 : TREE_TYPE (arg1), TREE_TYPE (arg2));
5599 : : else
5600 : 100 : if (flag_diagnostics_show_caret)
5601 : 0 : error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
5602 : 0 : opname, TREE_TYPE (arg1));
5603 : : else
5604 : 200 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
5605 : : 1, match),
5606 : 100 : opname, opname, arg1, TREE_TYPE (arg1));
5607 : : break;
5608 : : }
5609 : 1440 : }
5610 : :
5611 : : /* Return the implicit conversion sequence that could be used to
5612 : : convert E1 to E2 in [expr.cond]. */
5613 : :
5614 : : static conversion *
5615 : 247934 : conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
5616 : : {
5617 : 247934 : tree t1 = non_reference (TREE_TYPE (e1));
5618 : 247934 : tree t2 = non_reference (TREE_TYPE (e2));
5619 : 247934 : conversion *conv;
5620 : 247934 : bool good_base;
5621 : :
5622 : : /* [expr.cond]
5623 : :
5624 : : If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
5625 : : implicitly converted (clause _conv_) to the type "lvalue reference to
5626 : : T2", subject to the constraint that in the conversion the
5627 : : reference must bind directly (_dcl.init.ref_) to an lvalue.
5628 : :
5629 : : If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
5630 : : implicitly converted to the type "rvalue reference to T2", subject to
5631 : : the constraint that the reference must bind directly. */
5632 : 247934 : if (glvalue_p (e2))
5633 : : {
5634 : 219234 : tree rtype = cp_build_reference_type (t2, !lvalue_p (e2));
5635 : 219234 : conv = implicit_conversion (rtype,
5636 : : t1,
5637 : : e1,
5638 : : /*c_cast_p=*/false,
5639 : : LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
5640 : : |LOOKUP_ONLYCONVERTING,
5641 : : complain);
5642 : 219234 : if (conv && !conv->bad_p)
5643 : : return conv;
5644 : : }
5645 : :
5646 : : /* If E2 is a prvalue or if neither of the conversions above can be done
5647 : : and at least one of the operands has (possibly cv-qualified) class
5648 : : type: */
5649 : 207844 : if (!CLASS_TYPE_P (t1) && !CLASS_TYPE_P (t2))
5650 : : return NULL;
5651 : :
5652 : : /* [expr.cond]
5653 : :
5654 : : If E1 and E2 have class type, and the underlying class types are
5655 : : the same or one is a base class of the other: E1 can be converted
5656 : : to match E2 if the class of T2 is the same type as, or a base
5657 : : class of, the class of T1, and the cv-qualification of T2 is the
5658 : : same cv-qualification as, or a greater cv-qualification than, the
5659 : : cv-qualification of T1. If the conversion is applied, E1 is
5660 : : changed to an rvalue of type T2 that still refers to the original
5661 : : source class object (or the appropriate subobject thereof). */
5662 : 114516 : if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
5663 : 229064 : && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
5664 : : {
5665 : 56931 : if (good_base && at_least_as_qualified_p (t2, t1))
5666 : : {
5667 : 28396 : conv = build_identity_conv (t1, e1);
5668 : 28396 : if (!same_type_p (TYPE_MAIN_VARIANT (t1),
5669 : : TYPE_MAIN_VARIANT (t2)))
5670 : 6 : conv = build_conv (ck_base, t2, conv);
5671 : : else
5672 : 28390 : conv = build_conv (ck_rvalue, t2, conv);
5673 : 28396 : return conv;
5674 : : }
5675 : : else
5676 : 28535 : return NULL;
5677 : : }
5678 : : else
5679 : : /* [expr.cond]
5680 : :
5681 : : Otherwise: E1 can be converted to match E2 if E1 can be implicitly
5682 : : converted to the type that expression E2 would have if E2 were
5683 : : converted to an rvalue (or the type it has, if E2 is an rvalue). */
5684 : 110997 : return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
5685 : 110997 : LOOKUP_IMPLICIT, complain);
5686 : : }
5687 : :
5688 : : /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
5689 : : arguments to the conditional expression. */
5690 : :
5691 : : tree
5692 : 4044229 : build_conditional_expr (const op_location_t &loc,
5693 : : tree arg1, tree arg2, tree arg3,
5694 : : tsubst_flags_t complain)
5695 : : {
5696 : 4044229 : tree arg2_type;
5697 : 4044229 : tree arg3_type;
5698 : 4044229 : tree result = NULL_TREE;
5699 : 4044229 : tree result_type = NULL_TREE;
5700 : 4044229 : tree semantic_result_type = NULL_TREE;
5701 : 4044229 : bool is_glvalue = true;
5702 : 4044229 : struct z_candidate *candidates = 0;
5703 : 4044229 : struct z_candidate *cand;
5704 : 4044229 : tree orig_arg2, orig_arg3;
5705 : :
5706 : 4044229 : auto_cond_timevar tv (TV_OVERLOAD);
5707 : :
5708 : : /* As a G++ extension, the second argument to the conditional can be
5709 : : omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
5710 : : c'.) If the second operand is omitted, make sure it is
5711 : : calculated only once. */
5712 : 4044229 : if (!arg2)
5713 : : {
5714 : 377 : if (complain & tf_error)
5715 : 371 : pedwarn (loc, OPT_Wpedantic,
5716 : : "ISO C++ forbids omitting the middle term of "
5717 : : "a %<?:%> expression");
5718 : :
5719 : 377 : if ((complain & tf_warning) && !truth_value_p (TREE_CODE (arg1)))
5720 : 329 : warn_for_omitted_condop (loc, arg1);
5721 : :
5722 : : /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
5723 : 377 : if (glvalue_p (arg1))
5724 : : {
5725 : 86 : arg1 = cp_stabilize_reference (arg1);
5726 : 86 : arg2 = arg1 = prevent_lifetime_extension (arg1);
5727 : : }
5728 : 291 : else if (TREE_CODE (arg1) == TARGET_EXPR)
5729 : : /* arg1 can't be a prvalue result of the conditional
5730 : : expression, since it needs to be materialized for the
5731 : : conversion to bool, so treat it as an xvalue in arg2. */
5732 : 6 : arg2 = move (TARGET_EXPR_SLOT (arg1));
5733 : 285 : else if (TREE_CODE (arg1) == EXCESS_PRECISION_EXPR)
5734 : 3 : arg2 = arg1 = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (arg1),
5735 : 3 : cp_save_expr (TREE_OPERAND (arg1, 0)));
5736 : : else
5737 : 282 : arg2 = arg1 = cp_save_expr (arg1);
5738 : : }
5739 : :
5740 : : /* If something has already gone wrong, just pass that fact up the
5741 : : tree. */
5742 : 4044229 : if (error_operand_p (arg1)
5743 : 4044147 : || error_operand_p (arg2)
5744 : 8088337 : || error_operand_p (arg3))
5745 : 147 : return error_mark_node;
5746 : :
5747 : 4044082 : conversion_obstack_sentinel cos;
5748 : :
5749 : 4044082 : orig_arg2 = arg2;
5750 : 4044082 : orig_arg3 = arg3;
5751 : :
5752 : 4044082 : if (gnu_vector_type_p (TREE_TYPE (arg1))
5753 : 4044082 : && VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1)))
5754 : : {
5755 : 1001 : tree arg1_type = TREE_TYPE (arg1);
5756 : :
5757 : : /* If arg1 is another cond_expr choosing between -1 and 0,
5758 : : then we can use its comparison. It may help to avoid
5759 : : additional comparison, produce more accurate diagnostics
5760 : : and enables folding. */
5761 : 1001 : if (TREE_CODE (arg1) == VEC_COND_EXPR
5762 : 912 : && integer_minus_onep (TREE_OPERAND (arg1, 1))
5763 : 1913 : && integer_zerop (TREE_OPERAND (arg1, 2)))
5764 : 912 : arg1 = TREE_OPERAND (arg1, 0);
5765 : :
5766 : 1001 : arg1 = force_rvalue (arg1, complain);
5767 : 1001 : arg2 = force_rvalue (arg2, complain);
5768 : 1001 : arg3 = force_rvalue (arg3, complain);
5769 : :
5770 : : /* force_rvalue can return error_mark on valid arguments. */
5771 : 1001 : if (error_operand_p (arg1)
5772 : 1001 : || error_operand_p (arg2)
5773 : 2002 : || error_operand_p (arg3))
5774 : 0 : return error_mark_node;
5775 : :
5776 : 1001 : arg2_type = TREE_TYPE (arg2);
5777 : 1001 : arg3_type = TREE_TYPE (arg3);
5778 : :
5779 : 1001 : if (!VECTOR_TYPE_P (arg2_type)
5780 : 75 : && !VECTOR_TYPE_P (arg3_type))
5781 : : {
5782 : : /* Rely on the error messages of the scalar version. */
5783 : 57 : tree scal = build_conditional_expr (loc, integer_one_node,
5784 : : orig_arg2, orig_arg3, complain);
5785 : 57 : if (scal == error_mark_node)
5786 : : return error_mark_node;
5787 : 57 : tree stype = TREE_TYPE (scal);
5788 : 57 : tree ctype = TREE_TYPE (arg1_type);
5789 : 57 : if (TYPE_SIZE (stype) != TYPE_SIZE (ctype)
5790 : 57 : || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype)))
5791 : : {
5792 : 9 : if (complain & tf_error)
5793 : 9 : error_at (loc, "inferred scalar type %qT is not an integer or "
5794 : : "floating-point type of the same size as %qT", stype,
5795 : 9 : COMPARISON_CLASS_P (arg1)
5796 : 9 : ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0)))
5797 : : : ctype);
5798 : 9 : return error_mark_node;
5799 : : }
5800 : :
5801 : 48 : tree vtype = build_opaque_vector_type (stype,
5802 : 48 : TYPE_VECTOR_SUBPARTS (arg1_type));
5803 : : /* We could pass complain & tf_warning to unsafe_conversion_p,
5804 : : but the warnings (like Wsign-conversion) have already been
5805 : : given by the scalar build_conditional_expr_1. We still check
5806 : : unsafe_conversion_p to forbid truncating long long -> float. */
5807 : 48 : if (unsafe_conversion_p (stype, arg2, NULL_TREE, false))
5808 : : {
5809 : 0 : if (complain & tf_error)
5810 : 0 : error_at (loc, "conversion of scalar %qH to vector %qI "
5811 : : "involves truncation", arg2_type, vtype);
5812 : 0 : return error_mark_node;
5813 : : }
5814 : 48 : if (unsafe_conversion_p (stype, arg3, NULL_TREE, false))
5815 : : {
5816 : 3 : if (complain & tf_error)
5817 : 3 : error_at (loc, "conversion of scalar %qH to vector %qI "
5818 : : "involves truncation", arg3_type, vtype);
5819 : 3 : return error_mark_node;
5820 : : }
5821 : :
5822 : 45 : arg2 = cp_convert (stype, arg2, complain);
5823 : 45 : arg2 = save_expr (arg2);
5824 : 45 : arg2 = build_vector_from_val (vtype, arg2);
5825 : 45 : arg2_type = vtype;
5826 : 45 : arg3 = cp_convert (stype, arg3, complain);
5827 : 45 : arg3 = save_expr (arg3);
5828 : 45 : arg3 = build_vector_from_val (vtype, arg3);
5829 : 45 : arg3_type = vtype;
5830 : : }
5831 : :
5832 : 1960 : if ((gnu_vector_type_p (arg2_type) && !VECTOR_TYPE_P (arg3_type))
5833 : 1882 : || (gnu_vector_type_p (arg3_type) && !VECTOR_TYPE_P (arg2_type)))
5834 : : {
5835 : 96 : enum stv_conv convert_flag =
5836 : 96 : scalar_to_vector (loc, VEC_COND_EXPR, arg2, arg3,
5837 : : complain & tf_error);
5838 : :
5839 : 96 : switch (convert_flag)
5840 : : {
5841 : 0 : case stv_error:
5842 : 0 : return error_mark_node;
5843 : 15 : case stv_firstarg:
5844 : 15 : {
5845 : 15 : arg2 = save_expr (arg2);
5846 : 15 : arg2 = convert (TREE_TYPE (arg3_type), arg2);
5847 : 15 : arg2 = build_vector_from_val (arg3_type, arg2);
5848 : 15 : arg2_type = TREE_TYPE (arg2);
5849 : 15 : break;
5850 : : }
5851 : 72 : case stv_secondarg:
5852 : 72 : {
5853 : 72 : arg3 = save_expr (arg3);
5854 : 72 : arg3 = convert (TREE_TYPE (arg2_type), arg3);
5855 : 72 : arg3 = build_vector_from_val (arg2_type, arg3);
5856 : 72 : arg3_type = TREE_TYPE (arg3);
5857 : 72 : break;
5858 : : }
5859 : : default:
5860 : : break;
5861 : : }
5862 : : }
5863 : :
5864 : 989 : if (!gnu_vector_type_p (arg2_type)
5865 : 986 : || !gnu_vector_type_p (arg3_type)
5866 : 980 : || !same_type_p (arg2_type, arg3_type)
5867 : 980 : || maybe_ne (TYPE_VECTOR_SUBPARTS (arg1_type),
5868 : 980 : TYPE_VECTOR_SUBPARTS (arg2_type))
5869 : 1969 : || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type))
5870 : : {
5871 : 9 : if (complain & tf_error)
5872 : 6 : error_at (loc,
5873 : : "incompatible vector types in conditional expression: "
5874 : 6 : "%qT, %qT and %qT", TREE_TYPE (arg1),
5875 : 6 : TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3));
5876 : 9 : return error_mark_node;
5877 : : }
5878 : :
5879 : 980 : if (!COMPARISON_CLASS_P (arg1))
5880 : : {
5881 : 86 : tree cmp_type = truth_type_for (arg1_type);
5882 : 86 : arg1 = build2 (NE_EXPR, cmp_type, arg1, build_zero_cst (arg1_type));
5883 : : }
5884 : 980 : return build3_loc (loc, VEC_COND_EXPR, arg2_type, arg1, arg2, arg3);
5885 : : }
5886 : :
5887 : : /* [expr.cond]
5888 : :
5889 : : The first expression is implicitly converted to bool (clause
5890 : : _conv_). */
5891 : 4043081 : arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
5892 : : LOOKUP_NORMAL);
5893 : 4043081 : if (error_operand_p (arg1))
5894 : 24 : return error_mark_node;
5895 : :
5896 : 4043057 : arg2_type = unlowered_expr_type (arg2);
5897 : 4043057 : arg3_type = unlowered_expr_type (arg3);
5898 : :
5899 : 4043057 : if ((TREE_CODE (arg2) == EXCESS_PRECISION_EXPR
5900 : 4043039 : || TREE_CODE (arg3) == EXCESS_PRECISION_EXPR)
5901 : 45 : && (TREE_CODE (arg2_type) == INTEGER_TYPE
5902 : : || SCALAR_FLOAT_TYPE_P (arg2_type)
5903 : : || TREE_CODE (arg2_type) == COMPLEX_TYPE)
5904 : 45 : && (TREE_CODE (arg3_type) == INTEGER_TYPE
5905 : : || SCALAR_FLOAT_TYPE_P (arg3_type)
5906 : : || TREE_CODE (arg3_type) == COMPLEX_TYPE))
5907 : : {
5908 : 45 : semantic_result_type
5909 : 45 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
5910 : 45 : if (semantic_result_type == error_mark_node)
5911 : : {
5912 : 0 : tree t1 = arg2_type;
5913 : 0 : tree t2 = arg3_type;
5914 : 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
5915 : 0 : t1 = TREE_TYPE (t1);
5916 : 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
5917 : 0 : t2 = TREE_TYPE (t2);
5918 : 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
5919 : : && SCALAR_FLOAT_TYPE_P (t2)
5920 : : && (extended_float_type_p (t1)
5921 : : || extended_float_type_p (t2))
5922 : : && cp_compare_floating_point_conversion_ranks
5923 : : (t1, t2) == 3);
5924 : 0 : if (complain & tf_error)
5925 : 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
5926 : : "have unordered conversion rank",
5927 : : arg2_type, arg3_type);
5928 : 0 : return error_mark_node;
5929 : : }
5930 : 45 : if (TREE_CODE (arg2) == EXCESS_PRECISION_EXPR)
5931 : : {
5932 : 18 : arg2 = TREE_OPERAND (arg2, 0);
5933 : 18 : arg2_type = TREE_TYPE (arg2);
5934 : : }
5935 : 45 : if (TREE_CODE (arg3) == EXCESS_PRECISION_EXPR)
5936 : : {
5937 : 27 : arg3 = TREE_OPERAND (arg3, 0);
5938 : 27 : arg3_type = TREE_TYPE (arg3);
5939 : : }
5940 : : }
5941 : :
5942 : : /* [expr.cond]
5943 : :
5944 : : If either the second or the third operand has type (possibly
5945 : : cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
5946 : : array-to-pointer (_conv.array_), and function-to-pointer
5947 : : (_conv.func_) standard conversions are performed on the second
5948 : : and third operands. */
5949 : 4043057 : if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
5950 : : {
5951 : : /* 'void' won't help in resolving an overloaded expression on the
5952 : : other side, so require it to resolve by itself. */
5953 : 6833 : if (arg2_type == unknown_type_node)
5954 : : {
5955 : 9 : arg2 = resolve_nondeduced_context_or_error (arg2, complain);
5956 : 9 : arg2_type = TREE_TYPE (arg2);
5957 : : }
5958 : 6833 : if (arg3_type == unknown_type_node)
5959 : : {
5960 : 0 : arg3 = resolve_nondeduced_context_or_error (arg3, complain);
5961 : 0 : arg3_type = TREE_TYPE (arg3);
5962 : : }
5963 : :
5964 : : /* [expr.cond]
5965 : :
5966 : : One of the following shall hold:
5967 : :
5968 : : --The second or the third operand (but not both) is a
5969 : : throw-expression (_except.throw_); the result is of the type
5970 : : and value category of the other.
5971 : :
5972 : : --Both the second and the third operands have type void; the
5973 : : result is of type void and is a prvalue. */
5974 : 6833 : if (TREE_CODE (arg2) == THROW_EXPR
5975 : 69 : && TREE_CODE (arg3) != THROW_EXPR)
5976 : : {
5977 : 57 : result_type = arg3_type;
5978 : 57 : is_glvalue = glvalue_p (arg3);
5979 : : }
5980 : 6776 : else if (TREE_CODE (arg2) != THROW_EXPR
5981 : 6764 : && TREE_CODE (arg3) == THROW_EXPR)
5982 : : {
5983 : 171 : result_type = arg2_type;
5984 : 171 : is_glvalue = glvalue_p (arg2);
5985 : : }
5986 : 6605 : else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
5987 : : {
5988 : 6573 : result_type = void_type_node;
5989 : 6573 : is_glvalue = false;
5990 : : }
5991 : : else
5992 : : {
5993 : 32 : if (complain & tf_error)
5994 : : {
5995 : 18 : if (VOID_TYPE_P (arg2_type))
5996 : 9 : error_at (cp_expr_loc_or_loc (arg3, loc),
5997 : : "second operand to the conditional operator "
5998 : : "is of type %<void%>, but the third operand is "
5999 : : "neither a throw-expression nor of type %<void%>");
6000 : : else
6001 : 9 : error_at (cp_expr_loc_or_loc (arg2, loc),
6002 : : "third operand to the conditional operator "
6003 : : "is of type %<void%>, but the second operand is "
6004 : : "neither a throw-expression nor of type %<void%>");
6005 : : }
6006 : 32 : return error_mark_node;
6007 : : }
6008 : :
6009 : 6801 : goto valid_operands;
6010 : : }
6011 : : /* [expr.cond]
6012 : :
6013 : : Otherwise, if the second and third operand have different types,
6014 : : and either has (possibly cv-qualified) class type, or if both are
6015 : : glvalues of the same value category and the same type except for
6016 : : cv-qualification, an attempt is made to convert each of those operands
6017 : : to the type of the other. */
6018 : 4036224 : else if (!same_type_p (arg2_type, arg3_type)
6019 : 4036224 : && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)
6020 : 1023135 : || (same_type_ignoring_top_level_qualifiers_p (arg2_type,
6021 : : arg3_type)
6022 : 271373 : && glvalue_p (arg2) && glvalue_p (arg3)
6023 : 40044 : && lvalue_p (arg2) == lvalue_p (arg3))))
6024 : : {
6025 : 123967 : conversion *conv2;
6026 : 123967 : conversion *conv3;
6027 : 123967 : bool converted = false;
6028 : :
6029 : 123967 : conv2 = conditional_conversion (arg2, arg3, complain);
6030 : 123967 : conv3 = conditional_conversion (arg3, arg2, complain);
6031 : :
6032 : : /* [expr.cond]
6033 : :
6034 : : If both can be converted, or one can be converted but the
6035 : : conversion is ambiguous, the program is ill-formed. If
6036 : : neither can be converted, the operands are left unchanged and
6037 : : further checking is performed as described below. If exactly
6038 : : one conversion is possible, that conversion is applied to the
6039 : : chosen operand and the converted operand is used in place of
6040 : : the original operand for the remainder of this section. */
6041 : 123967 : if ((conv2 && !conv2->bad_p
6042 : 58144 : && conv3 && !conv3->bad_p)
6043 : 58173 : || (conv2 && conv2->kind == ck_ambig)
6044 : 123901 : || (conv3 && conv3->kind == ck_ambig))
6045 : : {
6046 : 66 : if (complain & tf_error)
6047 : : {
6048 : 6 : error_at (loc, "operands to %<?:%> have different types "
6049 : : "%qT and %qT",
6050 : : arg2_type, arg3_type);
6051 : 6 : if (conv2 && !conv2->bad_p && conv3 && !conv3->bad_p)
6052 : 3 : inform (loc, " and each type can be converted to the other");
6053 : 3 : else if (conv2 && conv2->kind == ck_ambig)
6054 : 3 : convert_like (conv2, arg2, complain);
6055 : : else
6056 : 0 : convert_like (conv3, arg3, complain);
6057 : : }
6058 : 66 : result = error_mark_node;
6059 : : }
6060 : 123901 : else if (conv2 && !conv2->bad_p)
6061 : : {
6062 : 58078 : arg2 = convert_like (conv2, arg2, complain);
6063 : 58078 : arg2 = convert_from_reference (arg2);
6064 : 58078 : arg2_type = TREE_TYPE (arg2);
6065 : : /* Even if CONV2 is a valid conversion, the result of the
6066 : : conversion may be invalid. For example, if ARG3 has type
6067 : : "volatile X", and X does not have a copy constructor
6068 : : accepting a "volatile X&", then even if ARG2 can be
6069 : : converted to X, the conversion will fail. */
6070 : 58078 : if (error_operand_p (arg2))
6071 : 0 : result = error_mark_node;
6072 : 0 : converted = true;
6073 : : }
6074 : 65823 : else if (conv3 && !conv3->bad_p)
6075 : : {
6076 : 65086 : arg3 = convert_like (conv3, arg3, complain);
6077 : 65086 : arg3 = convert_from_reference (arg3);
6078 : 65086 : arg3_type = TREE_TYPE (arg3);
6079 : 65086 : if (error_operand_p (arg3))
6080 : 0 : result = error_mark_node;
6081 : 0 : converted = true;
6082 : : }
6083 : :
6084 : 66 : if (result)
6085 : : return result;
6086 : :
6087 : : /* If, after the conversion, both operands have class type,
6088 : : treat the cv-qualification of both operands as if it were the
6089 : : union of the cv-qualification of the operands.
6090 : :
6091 : : The standard is not clear about what to do in this
6092 : : circumstance. For example, if the first operand has type
6093 : : "const X" and the second operand has a user-defined
6094 : : conversion to "volatile X", what is the type of the second
6095 : : operand after this step? Making it be "const X" (matching
6096 : : the first operand) seems wrong, as that discards the
6097 : : qualification without actually performing a copy. Leaving it
6098 : : as "volatile X" seems wrong as that will result in the
6099 : : conditional expression failing altogether, even though,
6100 : : according to this step, the one operand could be converted to
6101 : : the type of the other. */
6102 : 123901 : if (converted
6103 : 123164 : && CLASS_TYPE_P (arg2_type)
6104 : 154251 : && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
6105 : 0 : arg2_type = arg3_type =
6106 : 0 : cp_build_qualified_type (arg2_type,
6107 : 0 : cp_type_quals (arg2_type)
6108 : 0 : | cp_type_quals (arg3_type));
6109 : : }
6110 : :
6111 : : /* [expr.cond]
6112 : :
6113 : : If the second and third operands are glvalues of the same value
6114 : : category and have the same type, the result is of that type and
6115 : : value category. */
6116 : 5180850 : if (((lvalue_p (arg2) && lvalue_p (arg3))
6117 : 3245900 : || (xvalue_p (arg2) && xvalue_p (arg3)))
6118 : 4853013 : && same_type_p (arg2_type, arg3_type))
6119 : : {
6120 : 770224 : result_type = arg2_type;
6121 : 770224 : goto valid_operands;
6122 : : }
6123 : :
6124 : : /* [expr.cond]
6125 : :
6126 : : Otherwise, the result is an rvalue. If the second and third
6127 : : operand do not have the same type, and either has (possibly
6128 : : cv-qualified) class type, overload resolution is used to
6129 : : determine the conversions (if any) to be applied to the operands
6130 : : (_over.match.oper_, _over.built_). */
6131 : 3265934 : is_glvalue = false;
6132 : 3265934 : if (!same_type_p (arg2_type, arg3_type)
6133 : 3265934 : && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
6134 : : {
6135 : 737 : releasing_vec args;
6136 : 737 : conversion *conv;
6137 : 737 : bool any_viable_p;
6138 : :
6139 : : /* Rearrange the arguments so that add_builtin_candidate only has
6140 : : to know about two args. In build_builtin_candidate, the
6141 : : arguments are unscrambled. */
6142 : 737 : args->quick_push (arg2);
6143 : 737 : args->quick_push (arg3);
6144 : 737 : args->quick_push (arg1);
6145 : 737 : add_builtin_candidates (&candidates,
6146 : : COND_EXPR,
6147 : : NOP_EXPR,
6148 : : ovl_op_identifier (false, COND_EXPR),
6149 : : args,
6150 : : LOOKUP_NORMAL, complain);
6151 : :
6152 : : /* [expr.cond]
6153 : :
6154 : : If the overload resolution fails, the program is
6155 : : ill-formed. */
6156 : 737 : candidates = splice_viable (candidates, false, &any_viable_p);
6157 : 737 : if (!any_viable_p)
6158 : : {
6159 : 716 : if (complain & tf_error)
6160 : 15 : error_at (loc, "operands to %<?:%> have different types %qT and %qT",
6161 : : arg2_type, arg3_type);
6162 : 716 : return error_mark_node;
6163 : : }
6164 : 21 : cand = tourney (candidates, complain);
6165 : 21 : if (!cand)
6166 : : {
6167 : 0 : if (complain & tf_error)
6168 : : {
6169 : 0 : auto_diagnostic_group d;
6170 : 0 : op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, false);
6171 : 0 : print_z_candidates (loc, candidates);
6172 : 0 : }
6173 : 0 : return error_mark_node;
6174 : : }
6175 : :
6176 : : /* [expr.cond]
6177 : :
6178 : : Otherwise, the conversions thus determined are applied, and
6179 : : the converted operands are used in place of the original
6180 : : operands for the remainder of this section. */
6181 : 21 : conv = cand->convs[0];
6182 : 21 : arg1 = convert_like (conv, arg1, complain);
6183 : 21 : conv = cand->convs[1];
6184 : 21 : arg2 = convert_like (conv, arg2, complain);
6185 : 21 : arg2_type = TREE_TYPE (arg2);
6186 : 21 : conv = cand->convs[2];
6187 : 21 : arg3 = convert_like (conv, arg3, complain);
6188 : 21 : arg3_type = TREE_TYPE (arg3);
6189 : 737 : }
6190 : :
6191 : : /* [expr.cond]
6192 : :
6193 : : Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
6194 : : and function-to-pointer (_conv.func_) standard conversions are
6195 : : performed on the second and third operands.
6196 : :
6197 : : We need to force the lvalue-to-rvalue conversion here for class types,
6198 : : so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
6199 : : that isn't wrapped with a TARGET_EXPR plays havoc with exception
6200 : : regions. */
6201 : :
6202 : 3265218 : arg2 = force_rvalue (arg2, complain);
6203 : 3265218 : if (!CLASS_TYPE_P (arg2_type))
6204 : 3196324 : arg2_type = TREE_TYPE (arg2);
6205 : :
6206 : 3265218 : arg3 = force_rvalue (arg3, complain);
6207 : 3265218 : if (!CLASS_TYPE_P (arg3_type))
6208 : 3196324 : arg3_type = TREE_TYPE (arg3);
6209 : :
6210 : 3265218 : if (arg2 == error_mark_node || arg3 == error_mark_node)
6211 : : return error_mark_node;
6212 : :
6213 : : /* [expr.cond]
6214 : :
6215 : : After those conversions, one of the following shall hold:
6216 : :
6217 : : --The second and third operands have the same type; the result is of
6218 : : that type. */
6219 : 3265176 : if (same_type_p (arg2_type, arg3_type))
6220 : : result_type = arg2_type;
6221 : : /* [expr.cond]
6222 : :
6223 : : --The second and third operands have arithmetic or enumeration
6224 : : type; the usual arithmetic conversions are performed to bring
6225 : : them to a common type, and the result is of that type. */
6226 : 14428 : else if ((ARITHMETIC_TYPE_P (arg2_type)
6227 : 13729 : || UNSCOPED_ENUM_P (arg2_type))
6228 : 689734 : && (ARITHMETIC_TYPE_P (arg3_type)
6229 : 193 : || UNSCOPED_ENUM_P (arg3_type)))
6230 : : {
6231 : : /* A conditional expression between a floating-point
6232 : : type and an integer type should convert the integer type to
6233 : : the evaluation format of the floating-point type, with
6234 : : possible excess precision. */
6235 : 675182 : tree eptype2 = arg2_type;
6236 : 675182 : tree eptype3 = arg3_type;
6237 : 675182 : tree eptype;
6238 : 699 : if (ANY_INTEGRAL_TYPE_P (arg2_type)
6239 : 675185 : && (eptype = excess_precision_type (arg3_type)) != NULL_TREE)
6240 : : {
6241 : 3 : eptype3 = eptype;
6242 : 3 : if (!semantic_result_type)
6243 : 3 : semantic_result_type
6244 : 3 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
6245 : : }
6246 : 404 : else if (ANY_INTEGRAL_TYPE_P (arg3_type)
6247 : 675179 : && (eptype = excess_precision_type (arg2_type)) != NULL_TREE)
6248 : : {
6249 : 23 : eptype2 = eptype;
6250 : 23 : if (!semantic_result_type)
6251 : 23 : semantic_result_type
6252 : 23 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
6253 : : }
6254 : 675182 : result_type = type_after_usual_arithmetic_conversions (eptype2,
6255 : : eptype3);
6256 : 675182 : if (result_type == error_mark_node)
6257 : : {
6258 : 0 : tree t1 = eptype2;
6259 : 0 : tree t2 = eptype3;
6260 : 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6261 : 0 : t1 = TREE_TYPE (t1);
6262 : 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6263 : 0 : t2 = TREE_TYPE (t2);
6264 : 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
6265 : : && SCALAR_FLOAT_TYPE_P (t2)
6266 : : && (extended_float_type_p (t1)
6267 : : || extended_float_type_p (t2))
6268 : : && cp_compare_floating_point_conversion_ranks
6269 : : (t1, t2) == 3);
6270 : 0 : if (complain & tf_error)
6271 : 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
6272 : : "have unordered conversion rank",
6273 : : eptype2, eptype3);
6274 : 0 : return error_mark_node;
6275 : : }
6276 : 675182 : if (semantic_result_type == error_mark_node)
6277 : : {
6278 : 0 : tree t1 = arg2_type;
6279 : 0 : tree t2 = arg3_type;
6280 : 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6281 : 0 : t1 = TREE_TYPE (t1);
6282 : 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6283 : 0 : t2 = TREE_TYPE (t2);
6284 : 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
6285 : : && SCALAR_FLOAT_TYPE_P (t2)
6286 : : && (extended_float_type_p (t1)
6287 : : || extended_float_type_p (t2))
6288 : : && cp_compare_floating_point_conversion_ranks
6289 : : (t1, t2) == 3);
6290 : 0 : if (complain & tf_error)
6291 : 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
6292 : : "have unordered conversion rank",
6293 : : arg2_type, arg3_type);
6294 : 0 : return error_mark_node;
6295 : : }
6296 : :
6297 : 675182 : if (complain & tf_warning)
6298 : 645748 : do_warn_double_promotion (result_type, arg2_type, arg3_type,
6299 : : "implicit conversion from %qH to %qI to "
6300 : : "match other result of conditional",
6301 : : loc);
6302 : :
6303 : 675182 : if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
6304 : 95 : && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
6305 : : {
6306 : 34 : tree stripped_orig_arg2 = tree_strip_any_location_wrapper (orig_arg2);
6307 : 34 : tree stripped_orig_arg3 = tree_strip_any_location_wrapper (orig_arg3);
6308 : 34 : if (TREE_CODE (stripped_orig_arg2) == CONST_DECL
6309 : 23 : && TREE_CODE (stripped_orig_arg3) == CONST_DECL
6310 : 57 : && (DECL_CONTEXT (stripped_orig_arg2)
6311 : 23 : == DECL_CONTEXT (stripped_orig_arg3)))
6312 : : /* Two enumerators from the same enumeration can have different
6313 : : types when the enumeration is still being defined. */;
6314 : 28 : else if (complain & (cxx_dialect >= cxx26
6315 : 28 : ? tf_warning_or_error : tf_warning))
6316 : 43 : emit_diagnostic (cxx_dialect >= cxx26 ? DK_PEDWARN : DK_WARNING,
6317 : : loc, OPT_Wenum_compare, "enumerated mismatch "
6318 : : "in conditional expression: %qT vs %qT",
6319 : : arg2_type, arg3_type);
6320 : 3 : else if (cxx_dialect >= cxx26)
6321 : 1 : return error_mark_node;
6322 : : }
6323 : 675148 : else if ((((complain & (cxx_dialect >= cxx26
6324 : 675148 : ? tf_warning_or_error : tf_warning))
6325 : 651665 : && warn_deprecated_enum_float_conv)
6326 : 409283 : || (cxx_dialect >= cxx26
6327 : 4626 : && (complain & tf_warning_or_error) == 0))
6328 : 270410 : && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
6329 : 16 : && SCALAR_FLOAT_TYPE_P (arg3_type))
6330 : 270399 : || (SCALAR_FLOAT_TYPE_P (arg2_type)
6331 : 248 : && TREE_CODE (arg3_type) == ENUMERAL_TYPE)))
6332 : : {
6333 : 19 : if (cxx_dialect >= cxx26 && (complain & tf_warning_or_error) == 0)
6334 : 2 : return error_mark_node;
6335 : 17 : if (cxx_dialect >= cxx26 && TREE_CODE (arg2_type) == ENUMERAL_TYPE)
6336 : 4 : pedwarn (loc, OPT_Wdeprecated_enum_float_conversion,
6337 : : "conditional expression between enumeration type "
6338 : : "%qT and floating-point type %qT", arg2_type, arg3_type);
6339 : 13 : else if (cxx_dialect >= cxx26)
6340 : 3 : pedwarn (loc, OPT_Wdeprecated_enum_float_conversion,
6341 : : "conditional expression between floating-point type "
6342 : : "%qT and enumeration type %qT", arg2_type, arg3_type);
6343 : 10 : else if (TREE_CODE (arg2_type) == ENUMERAL_TYPE)
6344 : 6 : warning_at (loc, OPT_Wdeprecated_enum_float_conversion,
6345 : : "conditional expression between enumeration type "
6346 : : "%qT and floating-point type %qT is deprecated",
6347 : : arg2_type, arg3_type);
6348 : : else
6349 : 4 : warning_at (loc, OPT_Wdeprecated_enum_float_conversion,
6350 : : "conditional expression between floating-point "
6351 : : "type %qT and enumeration type %qT is deprecated",
6352 : : arg2_type, arg3_type);
6353 : : }
6354 : 666174 : else if ((extra_warnings || warn_enum_conversion)
6355 : 675134 : && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
6356 : 15 : && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
6357 : 8948 : || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
6358 : 8 : && !same_type_p (arg2_type,
6359 : : type_promotes_to (arg3_type)))))
6360 : : {
6361 : 17 : if (complain & tf_warning)
6362 : : {
6363 : 34 : enum opt_code opt = (warn_enum_conversion
6364 : 17 : ? OPT_Wenum_conversion
6365 : : : OPT_Wextra);
6366 : 17 : warning_at (loc, opt, "enumerated and "
6367 : : "non-enumerated type in conditional expression");
6368 : : }
6369 : : }
6370 : :
6371 : 675179 : arg2 = perform_implicit_conversion (result_type, arg2, complain);
6372 : 675179 : arg3 = perform_implicit_conversion (result_type, arg3, complain);
6373 : : }
6374 : : /* [expr.cond]
6375 : :
6376 : : --The second and third operands have pointer type, or one has
6377 : : pointer type and the other is a null pointer constant; pointer
6378 : : conversions (_conv.ptr_) and qualification conversions
6379 : : (_conv.qual_) are performed to bring them to their composite
6380 : : pointer type (_expr.rel_). The result is of the composite
6381 : : pointer type.
6382 : :
6383 : : --The second and third operands have pointer to member type, or
6384 : : one has pointer to member type and the other is a null pointer
6385 : : constant; pointer to member conversions (_conv.mem_) and
6386 : : qualification conversions (_conv.qual_) are performed to bring
6387 : : them to a common type, whose cv-qualification shall match the
6388 : : cv-qualification of either the second or the third operand.
6389 : : The result is of the common type. */
6390 : 13758 : else if ((null_ptr_cst_p (arg2)
6391 : 161 : && TYPE_PTR_OR_PTRMEM_P (arg3_type))
6392 : 13597 : || (null_ptr_cst_p (arg3)
6393 : 12299 : && TYPE_PTR_OR_PTRMEM_P (arg2_type))
6394 : 1298 : || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
6395 : 106 : || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type))
6396 : 13857 : || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
6397 : : {
6398 : 13668 : result_type = composite_pointer_type (loc,
6399 : : arg2_type, arg3_type, arg2,
6400 : : arg3, CPO_CONDITIONAL_EXPR,
6401 : : complain);
6402 : 13668 : if (result_type == error_mark_node)
6403 : : return error_mark_node;
6404 : 13646 : arg2 = perform_implicit_conversion (result_type, arg2, complain);
6405 : 13646 : arg3 = perform_implicit_conversion (result_type, arg3, complain);
6406 : : }
6407 : :
6408 : 3265061 : if (!result_type)
6409 : : {
6410 : 90 : if (complain & tf_error)
6411 : 3 : error_at (loc, "operands to %<?:%> have different types %qT and %qT",
6412 : : arg2_type, arg3_type);
6413 : 90 : return error_mark_node;
6414 : : }
6415 : :
6416 : 3265061 : if (arg2 == error_mark_node || arg3 == error_mark_node)
6417 : : return error_mark_node;
6418 : :
6419 : 3265058 : valid_operands:
6420 : 4042083 : if (processing_template_decl && is_glvalue)
6421 : : {
6422 : : /* Let lvalue_kind know this was a glvalue. */
6423 : 77278 : tree arg = (result_type == arg2_type ? arg2 : arg3);
6424 : 77278 : result_type = cp_build_reference_type (result_type, xvalue_p (arg));
6425 : : }
6426 : :
6427 : 4042083 : result = build3_loc (loc, COND_EXPR, result_type, arg1, arg2, arg3);
6428 : :
6429 : : /* If the ARG2 and ARG3 are the same and don't have side-effects,
6430 : : warn here, because the COND_EXPR will be turned into ARG2. */
6431 : 4042083 : if (warn_duplicated_branches
6432 : 144 : && (complain & tf_warning)
6433 : 4042227 : && (arg2 == arg3 || operand_equal_p (arg2, arg3,
6434 : : OEP_ADDRESS_OF_SAME_FIELD)))
6435 : 57 : warning_at (EXPR_LOCATION (result), OPT_Wduplicated_branches,
6436 : : "this condition has identical branches");
6437 : :
6438 : : /* We can't use result_type below, as fold might have returned a
6439 : : throw_expr. */
6440 : :
6441 : 4042083 : if (!is_glvalue)
6442 : : {
6443 : : /* Expand both sides into the same slot, hopefully the target of
6444 : : the ?: expression. We used to check for TARGET_EXPRs here,
6445 : : but now we sometimes wrap them in NOP_EXPRs so the test would
6446 : : fail. */
6447 : 3271799 : if (CLASS_TYPE_P (TREE_TYPE (result)))
6448 : : {
6449 : 68933 : result = get_target_expr (result, complain);
6450 : : /* Tell gimplify_modify_expr_rhs not to strip this in
6451 : : assignment context: we want both arms to initialize
6452 : : the same temporary. */
6453 : 68933 : TARGET_EXPR_NO_ELIDE (result) = true;
6454 : : }
6455 : : /* If this expression is an rvalue, but might be mistaken for an
6456 : : lvalue, we must add a NON_LVALUE_EXPR. */
6457 : 3271799 : result = rvalue (result);
6458 : 3271799 : if (semantic_result_type)
6459 : 71 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type,
6460 : : result);
6461 : : }
6462 : : else
6463 : : {
6464 : 770284 : result = force_paren_expr (result);
6465 : 770284 : gcc_assert (semantic_result_type == NULL_TREE);
6466 : : }
6467 : :
6468 : : return result;
6469 : 4044229 : }
6470 : :
6471 : : /* OPERAND is an operand to an expression. Perform necessary steps
6472 : : required before using it. If OPERAND is NULL_TREE, NULL_TREE is
6473 : : returned. */
6474 : :
6475 : : static tree
6476 : 480098907 : prep_operand (tree operand)
6477 : : {
6478 : 480098907 : if (operand)
6479 : : {
6480 : 553448832 : if (CLASS_TYPE_P (TREE_TYPE (operand))
6481 : 289945357 : && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
6482 : : /* Make sure the template type is instantiated now. */
6483 : 5551293 : instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
6484 : : }
6485 : :
6486 : 480098907 : return operand;
6487 : : }
6488 : :
6489 : : /* True iff CONV represents a conversion sequence which no other can be better
6490 : : than under [over.ics.rank]: in other words, a "conversion" to the exact same
6491 : : type (including binding to a reference to the same type). This is stronger
6492 : : than the standard's "identity" category, which also includes reference
6493 : : bindings that add cv-qualifiers or change rvalueness. */
6494 : :
6495 : : static bool
6496 : 164159940 : perfect_conversion_p (conversion *conv)
6497 : : {
6498 : 164159940 : if (CONVERSION_RANK (conv) != cr_identity)
6499 : : return false;
6500 : 114401914 : if (conv->kind == ck_ref_bind)
6501 : : {
6502 : 25620953 : if (!conv->rvaluedness_matches_p)
6503 : : return false;
6504 : 18470700 : if (!same_type_p (TREE_TYPE (conv->type),
6505 : : next_conversion (conv)->type))
6506 : : return false;
6507 : : }
6508 : 105082261 : if (conv->check_narrowing)
6509 : : /* Brace elision is imperfect. */
6510 : : return false;
6511 : : return true;
6512 : : }
6513 : :
6514 : : /* True if CAND represents a perfect match, i.e. all perfect conversions, so no
6515 : : other candidate can be a better match. Since the template/non-template
6516 : : tiebreaker comes immediately after the conversion comparison in
6517 : : [over.match.best], a perfect non-template candidate is better than all
6518 : : templates. */
6519 : :
6520 : : static bool
6521 : 366342843 : perfect_candidate_p (z_candidate *cand)
6522 : : {
6523 : 366342843 : if (cand->viable < 1)
6524 : : return false;
6525 : : /* CWG1402 makes an implicitly deleted move op worse than other
6526 : : candidates. */
6527 : 152759101 : if (DECL_DELETED_FN (cand->fn) && DECL_DEFAULTED_FN (cand->fn)
6528 : 151979972 : && move_fn_p (cand->fn))
6529 : : return false;
6530 : 150697633 : int len = cand->num_convs;
6531 : 255779851 : for (int i = 0; i < len; ++i)
6532 : 164159940 : if (!perfect_conversion_p (cand->convs[i]))
6533 : : return false;
6534 : 91619911 : if (conversion *conv = cand->second_conv)
6535 : 0 : if (!perfect_conversion_p (conv))
6536 : : return false;
6537 : : return true;
6538 : : }
6539 : :
6540 : : /* True iff one of CAND's argument conversions is missing. */
6541 : :
6542 : : static bool
6543 : 14817889 : missing_conversion_p (const z_candidate *cand)
6544 : : {
6545 : 28004100 : for (unsigned i = 0; i < cand->num_convs; ++i)
6546 : : {
6547 : 18481982 : conversion *conv = cand->convs[i];
6548 : 18481982 : if (!conv)
6549 : : return true;
6550 : 17527485 : if (conv->kind == ck_deferred_bad)
6551 : : {
6552 : : /* We don't know whether this conversion is outright invalid or
6553 : : just bad, so conservatively assume it's missing. */
6554 : 4341274 : gcc_checking_assert (conv->bad_p);
6555 : : return true;
6556 : : }
6557 : : }
6558 : : return false;
6559 : : }
6560 : :
6561 : : /* Add each of the viable functions in FNS (a FUNCTION_DECL or
6562 : : OVERLOAD) to the CANDIDATES, returning an updated list of
6563 : : CANDIDATES. The ARGS are the arguments provided to the call;
6564 : : if FIRST_ARG is non-null it is the implicit object argument,
6565 : : otherwise the first element of ARGS is used if needed. The
6566 : : EXPLICIT_TARGS are explicit template arguments provided.
6567 : : TEMPLATE_ONLY is true if only template functions should be
6568 : : considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
6569 : : add_function_candidate. */
6570 : :
6571 : : static void
6572 : 197093200 : add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args,
6573 : : tree return_type,
6574 : : tree explicit_targs, bool template_only,
6575 : : tree conversion_path, tree access_path,
6576 : : int flags,
6577 : : struct z_candidate **candidates,
6578 : : tsubst_flags_t complain)
6579 : : {
6580 : 197093200 : tree ctype;
6581 : 197093200 : const vec<tree, va_gc> *non_static_args;
6582 : 197093200 : bool check_list_ctor = false;
6583 : 197093200 : bool check_converting = false;
6584 : 197093200 : unification_kind_t strict;
6585 : 197093200 : tree ne_fns = NULL_TREE;
6586 : :
6587 : 197093200 : if (!fns)
6588 : 2205898 : return;
6589 : :
6590 : : /* Precalculate special handling of constructors and conversion ops. */
6591 : 194887302 : tree fn = OVL_FIRST (fns);
6592 : 194887302 : if (DECL_CONV_FN_P (fn))
6593 : : {
6594 : 8590771 : check_list_ctor = false;
6595 : 8590771 : check_converting = (flags & LOOKUP_ONLYCONVERTING) != 0;
6596 : 8590771 : if (flags & LOOKUP_NO_CONVERSION)
6597 : : /* We're doing return_type(x). */
6598 : : strict = DEDUCE_CONV;
6599 : : else
6600 : : /* We're doing x.operator return_type(). */
6601 : 1427 : strict = DEDUCE_EXACT;
6602 : : /* [over.match.funcs] For conversion functions, the function
6603 : : is considered to be a member of the class of the implicit
6604 : : object argument for the purpose of defining the type of
6605 : : the implicit object parameter. */
6606 : 8590771 : ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg));
6607 : : }
6608 : : else
6609 : : {
6610 : 372593062 : if (DECL_CONSTRUCTOR_P (fn))
6611 : : {
6612 : 48357442 : check_list_ctor = (flags & LOOKUP_LIST_ONLY) != 0;
6613 : : /* For list-initialization we consider explicit constructors
6614 : : and complain if one is chosen. */
6615 : 48357442 : check_converting
6616 : 48357442 : = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
6617 : : == LOOKUP_ONLYCONVERTING);
6618 : : }
6619 : 186296531 : strict = DEDUCE_CALL;
6620 : 297892957 : ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
6621 : : }
6622 : :
6623 : : /* P2468: Check if operator== is a rewrite target with first operand
6624 : : (*args)[0]; for now just do the lookups. */
6625 : 194887302 : if ((flags & (LOOKUP_REWRITTEN | LOOKUP_REVERSED))
6626 : 194887302 : && DECL_OVERLOADED_OPERATOR_IS (fn, EQ_EXPR))
6627 : : {
6628 : 1995254 : tree ne_name = ovl_op_identifier (false, NE_EXPR);
6629 : 1995254 : if (DECL_CLASS_SCOPE_P (fn))
6630 : : {
6631 : 54654 : ne_fns = lookup_fnfields (TREE_TYPE ((*args)[0]), ne_name,
6632 : : 1, tf_none);
6633 : 54654 : if (ne_fns == error_mark_node || ne_fns == NULL_TREE)
6634 : : ne_fns = NULL_TREE;
6635 : : else
6636 : 10932 : ne_fns = BASELINK_FUNCTIONS (ne_fns);
6637 : : }
6638 : : else
6639 : : {
6640 : 1940600 : tree context = decl_namespace_context (fn);
6641 : 1940600 : ne_fns = lookup_qualified_name (context, ne_name, LOOK_want::NORMAL,
6642 : : /*complain*/false);
6643 : 1940600 : if (ne_fns == error_mark_node
6644 : 740648 : || !is_overloaded_fn (ne_fns))
6645 : 194135722 : ne_fns = NULL_TREE;
6646 : : }
6647 : : }
6648 : :
6649 : 194887302 : if (first_arg)
6650 : : non_static_args = args;
6651 : : else
6652 : : /* Delay creating the implicit this parameter until it is needed. */
6653 : 78992127 : non_static_args = NULL;
6654 : :
6655 : 194887302 : bool seen_strictly_viable = any_strictly_viable (*candidates);
6656 : : /* If there's a non-template perfect match, we don't need to consider
6657 : : templates. So check non-templates first. This optimization is only
6658 : : really needed for the defaulted copy constructor of tuple and the like
6659 : : (96926), but it seems like we might as well enable it more generally. */
6660 : 194887302 : bool seen_perfect = false;
6661 : 194887302 : enum { templates, non_templates, either } which = either;
6662 : 194887302 : if (template_only)
6663 : : which = templates;
6664 : : else /*if (flags & LOOKUP_DEFAULTED)*/
6665 : 173355467 : which = non_templates;
6666 : :
6667 : : /* Template candidates that we'll potentially ignore if the
6668 : : perfect candidate optimization succeeds. */
6669 : 194887302 : z_candidate *ignored_template_cands = nullptr;
6670 : :
6671 : : /* During overload resolution, we first consider each function under the
6672 : : assumption that we'll eventually find a strictly viable candidate.
6673 : : This allows us to circumvent our defacto behavior when checking
6674 : : argument conversions and shortcut consideration of the candidate
6675 : : upon encountering the first bad conversion. If this assumption
6676 : : turns out to be false, and all candidates end up being non-strictly
6677 : : viable, then we reconsider such candidates under the defacto behavior.
6678 : : This trick is important for pruning member function overloads according
6679 : : to their const/ref-qualifiers (since all 'this' conversions are at
6680 : : worst bad) without breaking -fpermissive. */
6681 : 194887302 : z_candidate *bad_cands = nullptr;
6682 : 194887302 : bool shortcut_bad_convs = true;
6683 : :
6684 : 276664029 : again:
6685 : 1580533951 : for (tree fn : lkp_range (fns))
6686 : : {
6687 : 1303883473 : if (which == templates && TREE_CODE (fn) != TEMPLATE_DECL)
6688 : : {
6689 : 186367549 : if (template_only)
6690 : 457491 : add_ignored_candidate (candidates, fn);
6691 : 186367549 : continue;
6692 : : }
6693 : 1117515924 : if (which == non_templates && TREE_CODE (fn) == TEMPLATE_DECL)
6694 : : {
6695 : 375070634 : add_ignored_candidate (&ignored_template_cands, fn);
6696 : 375070634 : continue;
6697 : : }
6698 : 142711610 : if ((check_converting && DECL_NONCONVERTING_P (fn))
6699 : 872773115 : || (check_list_ctor && !is_list_ctor (fn)))
6700 : : {
6701 : 13317406 : add_ignored_candidate (candidates, fn);
6702 : 13317406 : continue;
6703 : : }
6704 : :
6705 : 729127884 : tree fn_first_arg = NULL_TREE;
6706 : 729127884 : const vec<tree, va_gc> *fn_args = args;
6707 : :
6708 : 729127884 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fn))
6709 : : {
6710 : : /* Figure out where the object arg comes from. If this
6711 : : function is a non-static member and we didn't get an
6712 : : implicit object argument, move it out of args. */
6713 : 282286275 : if (first_arg == NULL_TREE)
6714 : : {
6715 : 4292022 : unsigned int ix;
6716 : 4292022 : tree arg;
6717 : 4292022 : vec<tree, va_gc> *tempvec;
6718 : 4292022 : vec_alloc (tempvec, args->length () - 1);
6719 : 11553834 : for (ix = 1; args->iterate (ix, &arg); ++ix)
6720 : 2969790 : tempvec->quick_push (arg);
6721 : 4292022 : non_static_args = tempvec;
6722 : 4292022 : first_arg = (*args)[0];
6723 : : }
6724 : :
6725 : : fn_first_arg = first_arg;
6726 : : fn_args = non_static_args;
6727 : : }
6728 : :
6729 : : /* Don't bother reversing an operator with two identical parameters. */
6730 : 446841609 : else if (vec_safe_length (args) == 2 && (flags & LOOKUP_REVERSED))
6731 : : {
6732 : 72666920 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
6733 : 72666920 : if (same_type_p (TREE_VALUE (parmlist),
6734 : : TREE_VALUE (TREE_CHAIN (parmlist))))
6735 : 35112898 : continue;
6736 : : }
6737 : :
6738 : : /* When considering reversed operator==, if there's a corresponding
6739 : : operator!= in the same scope, it's not a rewrite target. */
6740 : 694014986 : if (ne_fns)
6741 : : {
6742 : 13744755 : bool found = false;
6743 : 130942485 : for (lkp_iterator ne (ne_fns); !found && ne; ++ne)
6744 : 119928167 : if (0 && !ne.using_p ()
6745 : : && DECL_NAMESPACE_SCOPE_P (fn)
6746 : : && DECL_CONTEXT (*ne) != DECL_CONTEXT (fn))
6747 : : /* ??? This kludge excludes inline namespace members for the H
6748 : : test in spaceship-eq15.C, but I don't see why we would want
6749 : : that behavior. Asked Core 2022-11-04. Disabling for now. */;
6750 : 119928167 : else if (fns_correspond (fn, *ne))
6751 : : {
6752 : : found = true;
6753 : : break;
6754 : : }
6755 : 13744755 : if (found)
6756 : 2730437 : continue;
6757 : : }
6758 : :
6759 : 691284549 : if (TREE_CODE (fn) == TEMPLATE_DECL)
6760 : 324941706 : add_template_candidate (candidates,
6761 : : fn,
6762 : : ctype,
6763 : : explicit_targs,
6764 : : fn_first_arg,
6765 : : fn_args,
6766 : : return_type,
6767 : : access_path,
6768 : : conversion_path,
6769 : : flags,
6770 : : strict,
6771 : : shortcut_bad_convs,
6772 : : complain);
6773 : : else
6774 : : {
6775 : 366342843 : add_function_candidate (candidates,
6776 : : fn,
6777 : : ctype,
6778 : : fn_first_arg,
6779 : : fn_args,
6780 : : access_path,
6781 : : conversion_path,
6782 : : flags,
6783 : : NULL,
6784 : : shortcut_bad_convs,
6785 : : complain);
6786 : 366342843 : if (perfect_candidate_p (*candidates))
6787 : 691270998 : seen_perfect = true;
6788 : : }
6789 : :
6790 : 691270998 : z_candidate *cand = *candidates;
6791 : 691270998 : if (cand->viable == 1)
6792 : 200626065 : seen_strictly_viable = true;
6793 : :
6794 : 691270998 : if (cand->viable == -1
6795 : 14818469 : && shortcut_bad_convs
6796 : 706088887 : && (missing_conversion_p (cand)
6797 : 9522118 : || TREE_CODE (cand->fn) == TEMPLATE_DECL))
6798 : : {
6799 : : /* This candidate has been tentatively marked non-strictly viable,
6800 : : and we didn't compute all argument conversions for it (having
6801 : : stopped at the first bad conversion). Move it to BAD_CANDS to
6802 : : to fully reconsider later if we don't find any strictly viable
6803 : : candidates. */
6804 : 5320143 : if (complain & (tf_error | tf_conv))
6805 : : {
6806 : 5165889 : *candidates = cand->next;
6807 : 5165889 : cand->next = bad_cands;
6808 : 5165889 : bad_cands = cand;
6809 : : }
6810 : : else
6811 : : /* But if we're in a SFINAE context, just mark this candidate as
6812 : : unviable outright and avoid potentially reconsidering it.
6813 : : This is safe to do because in a SFINAE context, performing a bad
6814 : : conversion is always an error (even with -fpermissive), so a
6815 : : non-strictly viable candidate is effectively unviable anyway. */
6816 : 154254 : cand->viable = 0;
6817 : : }
6818 : : }
6819 : 276650478 : if (which == non_templates && !seen_perfect)
6820 : : {
6821 : 81739374 : which = templates;
6822 : 81739374 : ignored_template_cands = nullptr;
6823 : 81739374 : goto again;
6824 : : }
6825 : 194911104 : else if (which == templates
6826 : 194911104 : && !seen_strictly_viable
6827 : : && shortcut_bad_convs
6828 : 32818682 : && bad_cands)
6829 : : {
6830 : : /* None of the candidates are strictly viable, so consider again those
6831 : : functions in BAD_CANDS, this time without shortcutting bad conversions
6832 : : so that all their argument conversions are computed. */
6833 : 106160 : which = either;
6834 : : fns = NULL_TREE;
6835 : 106160 : for (z_candidate *cand = bad_cands; cand; cand = cand->next)
6836 : : {
6837 : 68807 : tree fn = cand->fn;
6838 : 68807 : if (tree ti = cand->template_decl)
6839 : 72 : fn = TI_TEMPLATE (ti);
6840 : 68807 : fns = ovl_make (fn, fns);
6841 : : }
6842 : 37353 : shortcut_bad_convs = false;
6843 : 37353 : bad_cands = nullptr;
6844 : 37353 : goto again;
6845 : : }
6846 : :
6847 : 194873751 : if (complain & tf_error)
6848 : : {
6849 : : /* Remember any omitted candidates; we may want to print all candidates
6850 : : as part of overload resolution failure diagnostics. */
6851 : 330476802 : for (z_candidate *omitted_cands : { ignored_template_cands, bad_cands })
6852 : : {
6853 : 220317868 : z_candidate **omitted_cands_tail = &omitted_cands;
6854 : 262144845 : while (*omitted_cands_tail)
6855 : 41826977 : omitted_cands_tail = &(*omitted_cands_tail)->next;
6856 : 220317868 : *omitted_cands_tail = *candidates;
6857 : 220317868 : *candidates = omitted_cands;
6858 : : }
6859 : : }
6860 : : }
6861 : :
6862 : : /* Returns 1 if P0145R2 says that the LHS of operator CODE is evaluated first,
6863 : : -1 if the RHS is evaluated first, or 0 if the order is unspecified. */
6864 : :
6865 : : static int
6866 : 8625970 : op_is_ordered (tree_code code)
6867 : : {
6868 : 8625850 : switch (code)
6869 : : {
6870 : : // 5. b @= a
6871 : 2096185 : case MODIFY_EXPR:
6872 : 2096185 : return (flag_strong_eval_order > 1 ? -1 : 0);
6873 : :
6874 : : // 6. a[b]
6875 : 367479 : case ARRAY_REF:
6876 : 367359 : return (flag_strong_eval_order > 1 ? 1 : 0);
6877 : :
6878 : : // 1. a.b
6879 : : // Not overloadable (yet).
6880 : : // 2. a->b
6881 : : // Only one argument.
6882 : : // 3. a->*b
6883 : 41379 : case MEMBER_REF:
6884 : : // 7. a << b
6885 : 41379 : case LSHIFT_EXPR:
6886 : : // 8. a >> b
6887 : 41379 : case RSHIFT_EXPR:
6888 : : // a && b
6889 : : // Predates P0145R3.
6890 : 41379 : case TRUTH_ANDIF_EXPR:
6891 : : // a || b
6892 : : // Predates P0145R3.
6893 : 41379 : case TRUTH_ORIF_EXPR:
6894 : : // a , b
6895 : : // Predates P0145R3.
6896 : 41379 : case COMPOUND_EXPR:
6897 : 41379 : return (flag_strong_eval_order ? 1 : 0);
6898 : :
6899 : : default:
6900 : : return 0;
6901 : : }
6902 : : }
6903 : :
6904 : : /* Subroutine of build_new_op: Add to CANDIDATES all candidates for the
6905 : : operator indicated by CODE/CODE2. This function calls itself recursively to
6906 : : handle C++20 rewritten comparison operator candidates. Returns NULL_TREE
6907 : : upon success, and error_mark_node if something went wrong that prevented
6908 : : us from performing overload resolution (e.g. ambiguous member name lookup).
6909 : :
6910 : : LOOKUPS, if non-NULL, is the set of pertinent namespace-scope operator
6911 : : overloads to consider. This parameter is used when instantiating a
6912 : : dependent operator expression and has the same structure as
6913 : : DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS. */
6914 : :
6915 : : static tree
6916 : 17613230 : add_operator_candidates (z_candidate **candidates,
6917 : : tree_code code, tree_code code2,
6918 : : vec<tree, va_gc> *arglist, tree lookups,
6919 : : int flags, tsubst_flags_t complain)
6920 : : {
6921 : 17613230 : z_candidate *start_candidates = *candidates;
6922 : 17613230 : bool ismodop = code2 != ERROR_MARK;
6923 : 17613230 : tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
6924 : :
6925 : : /* LOOKUP_REWRITTEN is set when we're looking for the == or <=> operator to
6926 : : rewrite from, and also when we're looking for the e.g. < operator to use
6927 : : on the result of <=>. In the latter case, we don't want the flag set in
6928 : : the candidate, we just want to suppress looking for rewrites. */
6929 : 17613230 : bool rewritten = (flags & LOOKUP_REWRITTEN);
6930 : 17613230 : if (rewritten && code != EQ_EXPR && code != SPACESHIP_EXPR)
6931 : 321321 : flags &= ~LOOKUP_REWRITTEN;
6932 : :
6933 : 16789611 : bool memonly = false;
6934 : 16789611 : switch (code)
6935 : : {
6936 : : /* =, ->, [], () must be non-static member functions. */
6937 : 3379365 : case MODIFY_EXPR:
6938 : 3379365 : if (code2 != NOP_EXPR)
6939 : : break;
6940 : : /* FALLTHRU */
6941 : : case COMPONENT_REF:
6942 : : case ARRAY_REF:
6943 : : memonly = true;
6944 : : break;
6945 : :
6946 : : default:
6947 : : break;
6948 : : }
6949 : :
6950 : : /* Add namespace-scope operators to the list of functions to
6951 : : consider. */
6952 : : if (!memonly)
6953 : : {
6954 : 15206796 : tree fns;
6955 : 15206796 : if (!lookups)
6956 : 11509041 : fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE);
6957 : : /* If LOOKUPS is non-NULL, then we're instantiating a dependent operator
6958 : : expression, and LOOKUPS is the result of stage 1 name lookup. */
6959 : 3697755 : else if (tree found = purpose_member (fnname, lookups))
6960 : 1179950 : fns = TREE_VALUE (found);
6961 : : else
6962 : : fns = NULL_TREE;
6963 : 15206796 : fns = lookup_arg_dependent (fnname, fns, arglist);
6964 : 15206796 : add_candidates (fns, NULL_TREE, arglist, NULL_TREE,
6965 : : NULL_TREE, false, NULL_TREE, NULL_TREE,
6966 : : flags, candidates, complain);
6967 : : }
6968 : :
6969 : : /* Add class-member operators to the candidate set. */
6970 : 17613197 : tree arg1_type = TREE_TYPE ((*arglist)[0]);
6971 : 17613197 : unsigned nargs = arglist->length () > 1 ? 2 : 1;
6972 : 14127361 : tree arg2_type = nargs > 1 ? TREE_TYPE ((*arglist)[1]) : NULL_TREE;
6973 : 17613197 : if (CLASS_TYPE_P (arg1_type))
6974 : : {
6975 : 9591715 : tree fns = lookup_fnfields (arg1_type, fnname, 1, complain);
6976 : 9591715 : if (fns == error_mark_node)
6977 : : return error_mark_node;
6978 : 9591703 : if (fns)
6979 : : {
6980 : 4659385 : if (code == ARRAY_REF)
6981 : : {
6982 : 367363 : vec<tree,va_gc> *restlist = make_tree_vector ();
6983 : 734726 : for (unsigned i = 1; i < nargs; ++i)
6984 : 367363 : vec_safe_push (restlist, (*arglist)[i]);
6985 : 367363 : z_candidate *save_cand = *candidates;
6986 : 734726 : add_candidates (BASELINK_FUNCTIONS (fns),
6987 : 367363 : (*arglist)[0], restlist, NULL_TREE,
6988 : : NULL_TREE, false,
6989 : 367363 : BASELINK_BINFO (fns),
6990 : 367363 : BASELINK_ACCESS_BINFO (fns),
6991 : : flags, candidates, complain);
6992 : : /* Release the vec if we didn't add a candidate that uses it. */
6993 : 367844 : for (z_candidate *c = *candidates; c != save_cand; c = c->next)
6994 : 367844 : if (c->args == restlist)
6995 : : {
6996 : 367363 : restlist = NULL;
6997 : 367363 : break;
6998 : : }
6999 : 367363 : release_tree_vector (restlist);
7000 : : }
7001 : : else
7002 : 4292022 : add_candidates (BASELINK_FUNCTIONS (fns),
7003 : : NULL_TREE, arglist, NULL_TREE,
7004 : : NULL_TREE, false,
7005 : 4292022 : BASELINK_BINFO (fns),
7006 : 4292022 : BASELINK_ACCESS_BINFO (fns),
7007 : : flags, candidates, complain);
7008 : : }
7009 : : }
7010 : : /* Per [over.match.oper]3.2, if no operand has a class type, then
7011 : : only non-member functions that have type T1 or reference to
7012 : : cv-qualified-opt T1 for the first argument, if the first argument
7013 : : has an enumeration type, or T2 or reference to cv-qualified-opt
7014 : : T2 for the second argument, if the second argument has an
7015 : : enumeration type. Filter out those that don't match. */
7016 : 8021482 : else if (! arg2_type || ! CLASS_TYPE_P (arg2_type))
7017 : : {
7018 : : struct z_candidate **candp, **next;
7019 : :
7020 : 179108801 : for (candp = candidates; *candp != start_candidates; candp = next)
7021 : : {
7022 : 171283877 : unsigned i;
7023 : 171283877 : z_candidate *cand = *candp;
7024 : 171283877 : next = &cand->next;
7025 : :
7026 : 171283877 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
7027 : :
7028 : 507096754 : for (i = 0; i < nargs; ++i)
7029 : : {
7030 : 339003697 : tree parmtype = TREE_VALUE (parmlist);
7031 : 339003697 : tree argtype = unlowered_expr_type ((*arglist)[i]);
7032 : :
7033 : 339003697 : if (TYPE_REF_P (parmtype))
7034 : 266412235 : parmtype = TREE_TYPE (parmtype);
7035 : 339003697 : if (TREE_CODE (argtype) == ENUMERAL_TYPE
7036 : 666346338 : && (same_type_ignoring_top_level_qualifiers_p
7037 : 327342641 : (argtype, parmtype)))
7038 : : break;
7039 : :
7040 : 335812877 : parmlist = TREE_CHAIN (parmlist);
7041 : : }
7042 : :
7043 : : /* No argument has an appropriate type, so remove this
7044 : : candidate function from the list. */
7045 : 171283877 : if (i == nargs)
7046 : : {
7047 : 168093057 : *candp = cand->next;
7048 : 168093057 : next = candp;
7049 : : }
7050 : : }
7051 : : }
7052 : :
7053 : 17613185 : if (!rewritten)
7054 : : {
7055 : : /* The standard says to rewrite built-in candidates, too,
7056 : : but there's no point. */
7057 : 14519950 : add_builtin_candidates (candidates, code, code2, fnname, arglist,
7058 : : flags, complain);
7059 : :
7060 : : /* Maybe add C++20 rewritten comparison candidates. */
7061 : 14519950 : tree_code rewrite_code = ERROR_MARK;
7062 : 14519950 : if (cxx_dialect >= cxx20
7063 : 6848502 : && nargs == 2
7064 : 20021138 : && (OVERLOAD_TYPE_P (arg1_type) || OVERLOAD_TYPE_P (arg2_type)))
7065 : 5490022 : switch (code)
7066 : : {
7067 : 435306 : case LT_EXPR:
7068 : 435306 : case LE_EXPR:
7069 : 435306 : case GT_EXPR:
7070 : 435306 : case GE_EXPR:
7071 : 435306 : case SPACESHIP_EXPR:
7072 : 435306 : rewrite_code = SPACESHIP_EXPR;
7073 : 435306 : break;
7074 : :
7075 : : case NE_EXPR:
7076 : : case EQ_EXPR:
7077 : : rewrite_code = EQ_EXPR;
7078 : : break;
7079 : :
7080 : : default:;
7081 : : }
7082 : :
7083 : 435306 : if (rewrite_code)
7084 : : {
7085 : 1942398 : tree r;
7086 : 1942398 : flags |= LOOKUP_REWRITTEN;
7087 : 1942398 : if (rewrite_code != code)
7088 : : {
7089 : : /* Add rewritten candidates in same order. */
7090 : 829352 : r = add_operator_candidates (candidates, rewrite_code, ERROR_MARK,
7091 : : arglist, lookups, flags, complain);
7092 : 829352 : if (r == error_mark_node)
7093 : : return error_mark_node;
7094 : : }
7095 : :
7096 : 1942392 : z_candidate *save_cand = *candidates;
7097 : :
7098 : : /* Add rewritten candidates in reverse order. */
7099 : 1942392 : flags |= LOOKUP_REVERSED;
7100 : 1942392 : vec<tree,va_gc> *revlist = make_tree_vector ();
7101 : 1942392 : revlist->quick_push ((*arglist)[1]);
7102 : 1942392 : revlist->quick_push ((*arglist)[0]);
7103 : 1942392 : r = add_operator_candidates (candidates, rewrite_code, ERROR_MARK,
7104 : : revlist, lookups, flags, complain);
7105 : 1942392 : if (r == error_mark_node)
7106 : : return error_mark_node;
7107 : :
7108 : : /* Release the vec if we didn't add a candidate that uses it. */
7109 : 1979566 : for (z_candidate *c = *candidates; c != save_cand; c = c->next)
7110 : 890508 : if (c->args == revlist)
7111 : : {
7112 : : revlist = NULL;
7113 : : break;
7114 : : }
7115 : 1942392 : release_tree_vector (revlist);
7116 : : }
7117 : : }
7118 : :
7119 : : return NULL_TREE;
7120 : : }
7121 : :
7122 : : tree
7123 : 159556335 : build_new_op (const op_location_t &loc, enum tree_code code, int flags,
7124 : : tree arg1, tree arg2, tree arg3, tree lookups,
7125 : : tree *overload, tsubst_flags_t complain)
7126 : : {
7127 : 159556335 : struct z_candidate *candidates = 0, *cand;
7128 : 159556335 : releasing_vec arglist;
7129 : 159556335 : tree result = NULL_TREE;
7130 : 159556335 : bool result_valid_p = false;
7131 : 159556335 : enum tree_code code2 = ERROR_MARK;
7132 : 159556335 : enum tree_code code_orig_arg1 = ERROR_MARK;
7133 : 159556335 : enum tree_code code_orig_arg2 = ERROR_MARK;
7134 : 159556335 : bool strict_p;
7135 : 159556335 : bool any_viable_p;
7136 : :
7137 : 159556335 : auto_cond_timevar tv (TV_OVERLOAD);
7138 : :
7139 : 159556335 : if (error_operand_p (arg1)
7140 : 159553114 : || error_operand_p (arg2)
7141 : 319100767 : || error_operand_p (arg3))
7142 : 11903 : return error_mark_node;
7143 : :
7144 : 159544432 : conversion_obstack_sentinel cos;
7145 : :
7146 : 159544432 : bool ismodop = code == MODIFY_EXPR;
7147 : 159544432 : if (ismodop)
7148 : : {
7149 : 7241671 : code2 = TREE_CODE (arg3);
7150 : 7241671 : arg3 = NULL_TREE;
7151 : : }
7152 : :
7153 : 159544432 : tree arg1_type = unlowered_expr_type (arg1);
7154 : 159544432 : tree arg2_type = arg2 ? unlowered_expr_type (arg2) : NULL_TREE;
7155 : :
7156 : 159544432 : arg1 = prep_operand (arg1);
7157 : :
7158 : 159544432 : switch (code)
7159 : : {
7160 : 0 : case NEW_EXPR:
7161 : 0 : case VEC_NEW_EXPR:
7162 : 0 : case VEC_DELETE_EXPR:
7163 : 0 : case DELETE_EXPR:
7164 : : /* Use build_operator_new_call and build_op_delete_call instead. */
7165 : 0 : gcc_unreachable ();
7166 : :
7167 : 0 : case CALL_EXPR:
7168 : : /* Use build_op_call instead. */
7169 : 0 : gcc_unreachable ();
7170 : :
7171 : 12789899 : case TRUTH_ORIF_EXPR:
7172 : 12789899 : case TRUTH_ANDIF_EXPR:
7173 : 12789899 : case TRUTH_AND_EXPR:
7174 : 12789899 : case TRUTH_OR_EXPR:
7175 : : /* These are saved for the sake of warn_logical_operator. */
7176 : 12789899 : code_orig_arg1 = TREE_CODE (arg1);
7177 : 12789899 : code_orig_arg2 = TREE_CODE (arg2);
7178 : 12789899 : break;
7179 : 34623188 : case GT_EXPR:
7180 : 34623188 : case LT_EXPR:
7181 : 34623188 : case GE_EXPR:
7182 : 34623188 : case LE_EXPR:
7183 : 34623188 : case EQ_EXPR:
7184 : 34623188 : case NE_EXPR:
7185 : : /* These are saved for the sake of maybe_warn_bool_compare. */
7186 : 34623188 : code_orig_arg1 = TREE_CODE (arg1_type);
7187 : 34623188 : code_orig_arg2 = TREE_CODE (arg2_type);
7188 : 34623188 : break;
7189 : :
7190 : : default:
7191 : : break;
7192 : : }
7193 : :
7194 : 159544432 : arg2 = prep_operand (arg2);
7195 : 159544432 : arg3 = prep_operand (arg3);
7196 : :
7197 : 159544432 : if (code == COND_EXPR)
7198 : : /* Use build_conditional_expr instead. */
7199 : 0 : gcc_unreachable ();
7200 : 159544432 : else if (! OVERLOAD_TYPE_P (arg1_type)
7201 : 304523258 : && (! arg2 || ! OVERLOAD_TYPE_P (arg2_type)))
7202 : 144702946 : goto builtin;
7203 : :
7204 : 14841486 : if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
7205 : : {
7206 : 144515 : arg2 = integer_zero_node;
7207 : 144515 : arg2_type = integer_type_node;
7208 : : }
7209 : :
7210 : 14841486 : arglist->quick_push (arg1);
7211 : 14841486 : if (arg2 != NULL_TREE)
7212 : 11355650 : arglist->quick_push (arg2);
7213 : 14841486 : if (arg3 != NULL_TREE)
7214 : 0 : arglist->quick_push (arg3);
7215 : :
7216 : 14841486 : result = add_operator_candidates (&candidates, code, code2, arglist,
7217 : : lookups, flags, complain);
7218 : 14841453 : if (result == error_mark_node)
7219 : : return error_mark_node;
7220 : :
7221 : 14841441 : switch (code)
7222 : : {
7223 : : case COMPOUND_EXPR:
7224 : : case ADDR_EXPR:
7225 : : /* For these, the built-in candidates set is empty
7226 : : [over.match.oper]/3. We don't want non-strict matches
7227 : : because exact matches are always possible with built-in
7228 : : operators. The built-in candidate set for COMPONENT_REF
7229 : : would be empty too, but since there are no such built-in
7230 : : operators, we accept non-strict matches for them. */
7231 : : strict_p = true;
7232 : : break;
7233 : :
7234 : 13245629 : default:
7235 : 13245629 : strict_p = false;
7236 : 13245629 : break;
7237 : : }
7238 : :
7239 : 14841441 : candidates = splice_viable (candidates, strict_p, &any_viable_p);
7240 : 14841441 : if (!any_viable_p)
7241 : : {
7242 : 1610646 : switch (code)
7243 : : {
7244 : 88 : case POSTINCREMENT_EXPR:
7245 : 88 : case POSTDECREMENT_EXPR:
7246 : : /* Don't try anything fancy if we're not allowed to produce
7247 : : errors. */
7248 : 88 : if (!(complain & tf_error))
7249 : : return error_mark_node;
7250 : :
7251 : : /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
7252 : : distinguish between prefix and postfix ++ and
7253 : : operator++() was used for both, so we allow this with
7254 : : -fpermissive. */
7255 : : else
7256 : : {
7257 : 43 : tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
7258 : 86 : const char *msg = (flag_permissive)
7259 : 43 : ? G_("no %<%D(int)%> declared for postfix %qs,"
7260 : : " trying prefix operator instead")
7261 : : : G_("no %<%D(int)%> declared for postfix %qs");
7262 : 43 : permerror (loc, msg, fnname, OVL_OP_INFO (false, code)->name);
7263 : : }
7264 : :
7265 : 43 : if (!flag_permissive)
7266 : 31 : return error_mark_node;
7267 : :
7268 : 12 : if (code == POSTINCREMENT_EXPR)
7269 : : code = PREINCREMENT_EXPR;
7270 : : else
7271 : 0 : code = PREDECREMENT_EXPR;
7272 : 12 : result = build_new_op (loc, code, flags, arg1, NULL_TREE,
7273 : : NULL_TREE, lookups, overload, complain);
7274 : 12 : break;
7275 : :
7276 : : /* The caller will deal with these. */
7277 : : case ADDR_EXPR:
7278 : : case COMPOUND_EXPR:
7279 : : case COMPONENT_REF:
7280 : : case CO_AWAIT_EXPR:
7281 : : result = NULL_TREE;
7282 : : result_valid_p = true;
7283 : : break;
7284 : :
7285 : 11492 : default:
7286 : 11492 : if (complain & tf_error)
7287 : : {
7288 : : /* If one of the arguments of the operator represents
7289 : : an invalid use of member function pointer, try to report
7290 : : a meaningful error ... */
7291 : 1322 : if (invalid_nonstatic_memfn_p (loc, arg1, tf_error)
7292 : 1319 : || invalid_nonstatic_memfn_p (loc, arg2, tf_error)
7293 : 2635 : || invalid_nonstatic_memfn_p (loc, arg3, tf_error))
7294 : : /* We displayed the error message. */;
7295 : : else
7296 : : {
7297 : : /* ... Otherwise, report the more generic
7298 : : "no matching operator found" error */
7299 : 1313 : auto_diagnostic_group d;
7300 : 1313 : op_error (loc, code, code2, arg1, arg2, arg3, false);
7301 : 1313 : print_z_candidates (loc, candidates);
7302 : 1313 : }
7303 : : }
7304 : 11492 : result = error_mark_node;
7305 : 11492 : break;
7306 : : }
7307 : : }
7308 : : else
7309 : : {
7310 : 13230795 : cand = tourney (candidates, complain);
7311 : 13230795 : if (cand == 0)
7312 : : {
7313 : 168 : if (complain & tf_error)
7314 : : {
7315 : 127 : auto_diagnostic_group d;
7316 : 127 : op_error (loc, code, code2, arg1, arg2, arg3, true);
7317 : 127 : print_z_candidates (loc, candidates);
7318 : 127 : }
7319 : 168 : result = error_mark_node;
7320 : 168 : if (overload)
7321 : 149 : *overload = error_mark_node;
7322 : : }
7323 : 13230627 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
7324 : : {
7325 : 9906515 : if (overload)
7326 : 7734879 : *overload = cand->fn;
7327 : :
7328 : 9906515 : if (resolve_args (arglist, complain) == NULL)
7329 : 2 : result = error_mark_node;
7330 : : else
7331 : : {
7332 : 9906513 : tsubst_flags_t ocomplain = complain;
7333 : 9906513 : if (cand->rewritten ())
7334 : : /* We'll wrap this call in another one. */
7335 : 531650 : ocomplain &= ~tf_decltype;
7336 : 9906513 : if (cand->reversed ())
7337 : : {
7338 : : /* We swapped these in add_candidate, swap them back now. */
7339 : 8440 : std::swap (cand->convs[0], cand->convs[1]);
7340 : 8440 : if (cand->fn == current_function_decl)
7341 : 1 : warning_at (loc, 0, "in C++20 this comparison calls the "
7342 : : "current function recursively with reversed "
7343 : : "arguments");
7344 : : }
7345 : 9906513 : result = build_over_call (cand, LOOKUP_NORMAL, ocomplain);
7346 : : }
7347 : :
7348 : 18533898 : if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn))
7349 : : /* There won't be a CALL_EXPR. */;
7350 : 8627371 : else if (result && result != error_mark_node)
7351 : : {
7352 : 8625850 : tree call = extract_call_expr (result);
7353 : 8625850 : CALL_EXPR_OPERATOR_SYNTAX (call) = true;
7354 : :
7355 : : /* Specify evaluation order as per P0145R2. */
7356 : 8625850 : CALL_EXPR_ORDERED_ARGS (call) = false;
7357 : 8625850 : switch (op_is_ordered (code))
7358 : : {
7359 : 2072552 : case -1:
7360 : 2072552 : CALL_EXPR_REVERSE_ARGS (call) = true;
7361 : 2072552 : break;
7362 : :
7363 : 407695 : case 1:
7364 : 407695 : CALL_EXPR_ORDERED_ARGS (call) = true;
7365 : 407695 : break;
7366 : :
7367 : : default:
7368 : : break;
7369 : : }
7370 : : }
7371 : :
7372 : : /* If this was a C++20 rewritten comparison, adjust the result. */
7373 : 9906512 : if (cand->rewritten ())
7374 : : {
7375 : : /* FIXME build_min_non_dep_op_overload can't handle rewrites. */
7376 : 531650 : if (overload)
7377 : 531474 : *overload = NULL_TREE;
7378 : 531650 : switch (code)
7379 : : {
7380 : 4217 : case EQ_EXPR:
7381 : 4217 : gcc_checking_assert (cand->reversed ());
7382 : 209809 : gcc_fallthrough ();
7383 : 209809 : case NE_EXPR:
7384 : 209809 : if (result == error_mark_node)
7385 : : ;
7386 : : /* If a rewritten operator== candidate is selected by
7387 : : overload resolution for an operator @, its return type
7388 : : shall be cv bool.... */
7389 : 209807 : else if (TREE_CODE (TREE_TYPE (result)) != BOOLEAN_TYPE)
7390 : : {
7391 : 46 : if (complain & tf_error)
7392 : : {
7393 : 6 : auto_diagnostic_group d;
7394 : 6 : error_at (loc, "return type of %qD is not %qs",
7395 : : cand->fn, "bool");
7396 : 6 : inform (loc, "used as rewritten candidate for "
7397 : : "comparison of %qT and %qT",
7398 : : arg1_type, arg2_type);
7399 : 6 : }
7400 : 46 : result = error_mark_node;
7401 : : }
7402 : 209761 : else if (code == NE_EXPR)
7403 : : /* !(y == x) or !(x == y) */
7404 : 205569 : result = build1_loc (loc, TRUTH_NOT_EXPR,
7405 : : boolean_type_node, result);
7406 : : break;
7407 : :
7408 : : /* If a rewritten operator<=> candidate is selected by
7409 : : overload resolution for an operator @, x @ y is
7410 : : interpreted as 0 @ (y <=> x) if the selected candidate is
7411 : : a synthesized candidate with reversed order of parameters,
7412 : : or (x <=> y) @ 0 otherwise, using the selected rewritten
7413 : : operator<=> candidate. */
7414 : 352 : case SPACESHIP_EXPR:
7415 : 352 : if (!cand->reversed ())
7416 : : /* We're in the build_new_op call below for an outer
7417 : : reversed call; we don't need to do anything more. */
7418 : : break;
7419 : 321665 : gcc_fallthrough ();
7420 : 321665 : case LT_EXPR:
7421 : 321665 : case LE_EXPR:
7422 : 321665 : case GT_EXPR:
7423 : 321665 : case GE_EXPR:
7424 : 321665 : {
7425 : 321665 : tree lhs = result;
7426 : 321665 : tree rhs = integer_zero_node;
7427 : 321665 : if (cand->reversed ())
7428 : 649 : std::swap (lhs, rhs);
7429 : 321665 : warning_sentinel ws (warn_zero_as_null_pointer_constant);
7430 : 321665 : result = build_new_op (loc, code,
7431 : : LOOKUP_NORMAL|LOOKUP_REWRITTEN,
7432 : : lhs, rhs, NULL_TREE, lookups,
7433 : : NULL, complain);
7434 : 321665 : }
7435 : 321665 : break;
7436 : :
7437 : 0 : default:
7438 : 0 : gcc_unreachable ();
7439 : : }
7440 : : }
7441 : :
7442 : : /* In an expression of the form `a[]' where cand->fn
7443 : : which is operator[] turns out to be a static member function,
7444 : : `a' is none-the-less evaluated. */
7445 : 9906512 : if (code == ARRAY_REF)
7446 : 367359 : result = keep_unused_object_arg (result, arg1, cand->fn);
7447 : : }
7448 : : else
7449 : : {
7450 : : /* Give any warnings we noticed during overload resolution. */
7451 : 3324112 : if (cand->warnings && (complain & tf_warning))
7452 : : {
7453 : : struct candidate_warning *w;
7454 : 0 : for (w = cand->warnings; w; w = w->next)
7455 : 0 : joust (cand, w->loser, 1, complain);
7456 : : }
7457 : :
7458 : : /* Check for comparison of different enum types. */
7459 : 3324112 : switch (code)
7460 : : {
7461 : 2461407 : case GT_EXPR:
7462 : 2461407 : case LT_EXPR:
7463 : 2461407 : case GE_EXPR:
7464 : 2461407 : case LE_EXPR:
7465 : 2461407 : case EQ_EXPR:
7466 : 2461407 : case NE_EXPR:
7467 : 2461407 : if (TREE_CODE (arg1_type) == ENUMERAL_TYPE
7468 : 2332717 : && TREE_CODE (arg2_type) == ENUMERAL_TYPE
7469 : 4715655 : && (TYPE_MAIN_VARIANT (arg1_type)
7470 : 2254248 : != TYPE_MAIN_VARIANT (arg2_type)))
7471 : : {
7472 : 79 : if (cxx_dialect >= cxx26
7473 : 24 : && (complain & tf_warning_or_error) == 0)
7474 : 1 : result = error_mark_node;
7475 : 78 : else if (cxx_dialect >= cxx26 || (complain & tf_warning))
7476 : 129 : emit_diagnostic (cxx_dialect >= cxx26
7477 : : ? DK_PEDWARN : DK_WARNING,
7478 : : loc, OPT_Wenum_compare,
7479 : : "comparison between %q#T and %q#T",
7480 : : arg1_type, arg2_type);
7481 : : }
7482 : : break;
7483 : : default:
7484 : : break;
7485 : : }
7486 : :
7487 : : /* "If a built-in candidate is selected by overload resolution, the
7488 : : operands of class type are converted to the types of the
7489 : : corresponding parameters of the selected operation function,
7490 : : except that the second standard conversion sequence of a
7491 : : user-defined conversion sequence (12.3.3.1.2) is not applied." */
7492 : 3324112 : conversion *conv = cand->convs[0];
7493 : 3324112 : if (conv->user_conv_p)
7494 : : {
7495 : 26601 : conv = strip_standard_conversion (conv);
7496 : 26601 : arg1 = convert_like (conv, arg1, complain);
7497 : : }
7498 : :
7499 : 3324112 : if (arg2)
7500 : : {
7501 : 2851732 : conv = cand->convs[1];
7502 : 2851732 : if (conv->user_conv_p)
7503 : : {
7504 : 8277 : conv = strip_standard_conversion (conv);
7505 : 8277 : arg2 = convert_like (conv, arg2, complain);
7506 : : }
7507 : : }
7508 : :
7509 : 3324112 : if (arg3)
7510 : : {
7511 : 0 : conv = cand->convs[2];
7512 : 0 : if (conv->user_conv_p)
7513 : : {
7514 : 0 : conv = strip_standard_conversion (conv);
7515 : 0 : arg3 = convert_like (conv, arg3, complain);
7516 : : }
7517 : : }
7518 : : }
7519 : : }
7520 : :
7521 : 14841362 : if (result || result_valid_p)
7522 : : return result;
7523 : :
7524 : 3324111 : builtin:
7525 : 148027057 : switch (code)
7526 : : {
7527 : 3862733 : case MODIFY_EXPR:
7528 : 3862733 : return cp_build_modify_expr (loc, arg1, code2, arg2, complain);
7529 : :
7530 : 14225831 : case INDIRECT_REF:
7531 : 14225831 : return cp_build_indirect_ref (loc, arg1, RO_UNARY_STAR, complain);
7532 : :
7533 : 12789334 : case TRUTH_ANDIF_EXPR:
7534 : 12789334 : case TRUTH_ORIF_EXPR:
7535 : 12789334 : case TRUTH_AND_EXPR:
7536 : 12789334 : case TRUTH_OR_EXPR:
7537 : 12789334 : if ((complain & tf_warning) && !processing_template_decl)
7538 : 5046111 : warn_logical_operator (loc, code, boolean_type_node,
7539 : : code_orig_arg1, arg1,
7540 : : code_orig_arg2, arg2);
7541 : : /* Fall through. */
7542 : 43017653 : case GT_EXPR:
7543 : 43017653 : case LT_EXPR:
7544 : 43017653 : case GE_EXPR:
7545 : 43017653 : case LE_EXPR:
7546 : 43017653 : case EQ_EXPR:
7547 : 43017653 : case NE_EXPR:
7548 : 43017653 : if ((complain & tf_warning)
7549 : 40959844 : && ((code_orig_arg1 == BOOLEAN_TYPE)
7550 : 40959844 : ^ (code_orig_arg2 == BOOLEAN_TYPE)))
7551 : 11187 : maybe_warn_bool_compare (loc, code, arg1, arg2);
7552 : 40959844 : if (complain & tf_warning && warn_tautological_compare)
7553 : 389679 : warn_tautological_cmp (loc, code, arg1, arg2);
7554 : : /* Fall through. */
7555 : 101549215 : case SPACESHIP_EXPR:
7556 : 101549215 : case PLUS_EXPR:
7557 : 101549215 : case MINUS_EXPR:
7558 : 101549215 : case MULT_EXPR:
7559 : 101549215 : case TRUNC_DIV_EXPR:
7560 : 101549215 : case MAX_EXPR:
7561 : 101549215 : case MIN_EXPR:
7562 : 101549215 : case LSHIFT_EXPR:
7563 : 101549215 : case RSHIFT_EXPR:
7564 : 101549215 : case TRUNC_MOD_EXPR:
7565 : 101549215 : case BIT_AND_EXPR:
7566 : 101549215 : case BIT_IOR_EXPR:
7567 : 101549215 : case BIT_XOR_EXPR:
7568 : 101549215 : return cp_build_binary_op (loc, code, arg1, arg2, complain);
7569 : :
7570 : 24649389 : case UNARY_PLUS_EXPR:
7571 : 24649389 : case NEGATE_EXPR:
7572 : 24649389 : case BIT_NOT_EXPR:
7573 : 24649389 : case TRUTH_NOT_EXPR:
7574 : 24649389 : case PREINCREMENT_EXPR:
7575 : 24649389 : case POSTINCREMENT_EXPR:
7576 : 24649389 : case PREDECREMENT_EXPR:
7577 : 24649389 : case POSTDECREMENT_EXPR:
7578 : 24649389 : case REALPART_EXPR:
7579 : 24649389 : case IMAGPART_EXPR:
7580 : 24649389 : case ABS_EXPR:
7581 : 24649389 : case CO_AWAIT_EXPR:
7582 : 24649389 : return cp_build_unary_op (code, arg1, false, complain);
7583 : :
7584 : 1587361 : case ARRAY_REF:
7585 : 1587361 : return cp_build_array_ref (input_location, arg1, arg2, complain);
7586 : :
7587 : 739 : case MEMBER_REF:
7588 : 739 : return build_m_component_ref (cp_build_indirect_ref (loc, arg1,
7589 : : RO_ARROW_STAR,
7590 : : complain),
7591 : 739 : arg2, complain);
7592 : :
7593 : : /* The caller will deal with these. */
7594 : : case ADDR_EXPR:
7595 : : case COMPONENT_REF:
7596 : : case COMPOUND_EXPR:
7597 : : return NULL_TREE;
7598 : :
7599 : 0 : default:
7600 : 0 : gcc_unreachable ();
7601 : : }
7602 : : return NULL_TREE;
7603 : 159556299 : }
7604 : :
7605 : : /* Build a new call to operator[]. This may change ARGS. */
7606 : :
7607 : : tree
7608 : 151 : build_op_subscript (const op_location_t &loc, tree obj,
7609 : : vec<tree, va_gc> **args, tree *overload,
7610 : : tsubst_flags_t complain)
7611 : : {
7612 : 151 : struct z_candidate *candidates = 0, *cand;
7613 : 151 : tree fns, first_mem_arg = NULL_TREE;
7614 : 151 : bool any_viable_p;
7615 : 151 : tree result = NULL_TREE;
7616 : :
7617 : 151 : auto_cond_timevar tv (TV_OVERLOAD);
7618 : :
7619 : 151 : obj = mark_lvalue_use (obj);
7620 : :
7621 : 151 : if (error_operand_p (obj))
7622 : 0 : return error_mark_node;
7623 : :
7624 : 151 : tree type = TREE_TYPE (obj);
7625 : :
7626 : 151 : obj = prep_operand (obj);
7627 : :
7628 : 151 : if (TYPE_BINFO (type))
7629 : : {
7630 : 151 : fns = lookup_fnfields (TYPE_BINFO (type), ovl_op_identifier (ARRAY_REF),
7631 : : 1, complain);
7632 : 151 : if (fns == error_mark_node)
7633 : : return error_mark_node;
7634 : : }
7635 : : else
7636 : : fns = NULL_TREE;
7637 : :
7638 : 151 : if (args != NULL && *args != NULL)
7639 : : {
7640 : 151 : *args = resolve_args (*args, complain);
7641 : 151 : if (*args == NULL)
7642 : 0 : return error_mark_node;
7643 : : }
7644 : :
7645 : 151 : conversion_obstack_sentinel cos;
7646 : :
7647 : 151 : if (fns)
7648 : : {
7649 : 149 : first_mem_arg = obj;
7650 : :
7651 : 149 : add_candidates (BASELINK_FUNCTIONS (fns),
7652 : : first_mem_arg, *args, NULL_TREE,
7653 : : NULL_TREE, false,
7654 : 149 : BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
7655 : : LOOKUP_NORMAL, &candidates, complain);
7656 : : }
7657 : :
7658 : : /* Be strict here because if we choose a bad conversion candidate, the
7659 : : errors we get won't mention the call context. */
7660 : 151 : candidates = splice_viable (candidates, true, &any_viable_p);
7661 : 151 : if (!any_viable_p)
7662 : : {
7663 : 31 : if (complain & tf_error)
7664 : : {
7665 : 1 : auto_diagnostic_group d;
7666 : 2 : error ("no match for call to %<%T::operator[] (%A)%>",
7667 : 1 : TREE_TYPE (obj), build_tree_list_vec (*args));
7668 : 1 : print_z_candidates (loc, candidates);
7669 : 1 : }
7670 : 31 : result = error_mark_node;
7671 : : }
7672 : : else
7673 : : {
7674 : 120 : cand = tourney (candidates, complain);
7675 : 120 : if (cand == 0)
7676 : : {
7677 : 0 : if (complain & tf_error)
7678 : : {
7679 : 0 : auto_diagnostic_group d;
7680 : 0 : error ("call of %<%T::operator[] (%A)%> is ambiguous",
7681 : 0 : TREE_TYPE (obj), build_tree_list_vec (*args));
7682 : 0 : print_z_candidates (loc, candidates);
7683 : 0 : }
7684 : 0 : result = error_mark_node;
7685 : : }
7686 : 120 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL
7687 : 120 : && DECL_OVERLOADED_OPERATOR_P (cand->fn)
7688 : 240 : && DECL_OVERLOADED_OPERATOR_IS (cand->fn, ARRAY_REF))
7689 : : {
7690 : 120 : if (overload)
7691 : 120 : *overload = cand->fn;
7692 : 120 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
7693 : 240 : if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn))
7694 : : /* There won't be a CALL_EXPR. */;
7695 : 120 : else if (result && result != error_mark_node)
7696 : : {
7697 : 120 : tree call = extract_call_expr (result);
7698 : 120 : CALL_EXPR_OPERATOR_SYNTAX (call) = true;
7699 : :
7700 : : /* Specify evaluation order as per P0145R2. */
7701 : 120 : CALL_EXPR_ORDERED_ARGS (call) = op_is_ordered (ARRAY_REF) == 1;
7702 : : }
7703 : :
7704 : : /* In an expression of the form `a[]' where cand->fn
7705 : : which is operator[] turns out to be a static member function,
7706 : : `a' is none-the-less evaluated. */
7707 : 120 : result = keep_unused_object_arg (result, obj, cand->fn);
7708 : : }
7709 : : else
7710 : 0 : gcc_unreachable ();
7711 : : }
7712 : :
7713 : 151 : return result;
7714 : 151 : }
7715 : :
7716 : : /* CALL was returned by some call-building function; extract the actual
7717 : : CALL_EXPR from any bits that have been tacked on, e.g. by
7718 : : convert_from_reference. */
7719 : :
7720 : : tree
7721 : 19280802 : extract_call_expr (tree call)
7722 : : {
7723 : 19280963 : while (TREE_CODE (call) == COMPOUND_EXPR)
7724 : 161 : call = TREE_OPERAND (call, 1);
7725 : 19280802 : if (REFERENCE_REF_P (call))
7726 : 6508641 : call = TREE_OPERAND (call, 0);
7727 : 19280802 : if (TREE_CODE (call) == TARGET_EXPR)
7728 : 1059270 : call = TARGET_EXPR_INITIAL (call);
7729 : 19280802 : if (cxx_dialect >= cxx20)
7730 : 8084362 : switch (TREE_CODE (call))
7731 : : {
7732 : : /* C++20 rewritten comparison operators. */
7733 : 4229 : case TRUTH_NOT_EXPR:
7734 : 4229 : call = TREE_OPERAND (call, 0);
7735 : 4229 : break;
7736 : 14544 : case LT_EXPR:
7737 : 14544 : case LE_EXPR:
7738 : 14544 : case GT_EXPR:
7739 : 14544 : case GE_EXPR:
7740 : 14544 : case SPACESHIP_EXPR:
7741 : 14544 : {
7742 : 14544 : tree op0 = TREE_OPERAND (call, 0);
7743 : 14544 : if (integer_zerop (op0))
7744 : 335 : call = TREE_OPERAND (call, 1);
7745 : : else
7746 : : call = op0;
7747 : : }
7748 : : break;
7749 : : default:;
7750 : : }
7751 : :
7752 : 19280802 : if (TREE_CODE (call) != CALL_EXPR
7753 : 686958 : && TREE_CODE (call) != AGGR_INIT_EXPR
7754 : 600234 : && call != error_mark_node)
7755 : 600216 : return NULL_TREE;
7756 : : return call;
7757 : : }
7758 : :
7759 : : /* Returns true if FN has two parameters, of which the second has type
7760 : : size_t. */
7761 : :
7762 : : static bool
7763 : 233534 : second_parm_is_size_t (tree fn)
7764 : : {
7765 : 233534 : tree t = FUNCTION_ARG_CHAIN (fn);
7766 : 233534 : if (!t || !same_type_p (TREE_VALUE (t), size_type_node))
7767 : 233519 : return false;
7768 : 15 : t = TREE_CHAIN (t);
7769 : 15 : if (t == void_list_node)
7770 : : return true;
7771 : : return false;
7772 : : }
7773 : :
7774 : : /* True if T, an allocation function, has std::align_val_t as its second
7775 : : argument. */
7776 : :
7777 : : bool
7778 : 302303 : aligned_allocation_fn_p (tree t)
7779 : : {
7780 : 302303 : if (!aligned_new_threshold)
7781 : : return false;
7782 : :
7783 : 293524 : tree a = FUNCTION_ARG_CHAIN (t);
7784 : 293524 : return (a && same_type_p (TREE_VALUE (a), align_type_node));
7785 : : }
7786 : :
7787 : : /* True if T is std::destroying_delete_t. */
7788 : :
7789 : : static bool
7790 : 3885297 : std_destroying_delete_t_p (tree t)
7791 : : {
7792 : 3885297 : return (TYPE_CONTEXT (t) == std_node
7793 : 3885297 : && id_equal (TYPE_IDENTIFIER (t), "destroying_delete_t"));
7794 : : }
7795 : :
7796 : : /* A deallocation function with at least two parameters whose second parameter
7797 : : type is of type std::destroying_delete_t is a destroying operator delete. A
7798 : : destroying operator delete shall be a class member function named operator
7799 : : delete. [ Note: Array deletion cannot use a destroying operator
7800 : : delete. --end note ] */
7801 : :
7802 : : tree
7803 : 3885312 : destroying_delete_p (tree t)
7804 : : {
7805 : 3885312 : tree a = TYPE_ARG_TYPES (TREE_TYPE (t));
7806 : 3885312 : if (!a || !TREE_CHAIN (a))
7807 : : return NULL_TREE;
7808 : 3885297 : tree type = TREE_VALUE (TREE_CHAIN (a));
7809 : 3885297 : return std_destroying_delete_t_p (type) ? type : NULL_TREE;
7810 : : }
7811 : :
7812 : : struct dealloc_info
7813 : : {
7814 : : bool sized;
7815 : : bool aligned;
7816 : : tree destroying;
7817 : : };
7818 : :
7819 : : /* Returns true iff T, an element of an OVERLOAD chain, is a usual deallocation
7820 : : function (3.7.4.2 [basic.stc.dynamic.deallocation]). If so, and DI is
7821 : : non-null, also set *DI. */
7822 : :
7823 : : static bool
7824 : 2776364 : usual_deallocation_fn_p (tree t, dealloc_info *di)
7825 : : {
7826 : 2776364 : if (di) *di = dealloc_info();
7827 : :
7828 : : /* A template instance is never a usual deallocation function,
7829 : : regardless of its signature. */
7830 : 2776364 : if (TREE_CODE (t) == TEMPLATE_DECL
7831 : 2776364 : || primary_template_specialization_p (t))
7832 : 0 : return false;
7833 : :
7834 : : /* A usual deallocation function is a deallocation function whose parameters
7835 : : after the first are
7836 : : - optionally, a parameter of type std::destroying_delete_t, then
7837 : : - optionally, a parameter of type std::size_t, then
7838 : : - optionally, a parameter of type std::align_val_t. */
7839 : 2776364 : bool global = DECL_NAMESPACE_SCOPE_P (t);
7840 : 2776364 : tree chain = FUNCTION_ARG_CHAIN (t);
7841 : 2776364 : if (chain && destroying_delete_p (t))
7842 : : {
7843 : 81 : if (di) di->destroying = TREE_VALUE (chain);
7844 : 81 : chain = TREE_CHAIN (chain);
7845 : : }
7846 : 2776364 : if (chain
7847 : 2776361 : && (!global || flag_sized_deallocation)
7848 : 5539173 : && same_type_p (TREE_VALUE (chain), size_type_node))
7849 : : {
7850 : 797163 : if (di) di->sized = true;
7851 : 797163 : chain = TREE_CHAIN (chain);
7852 : : }
7853 : 2776361 : if (chain && aligned_new_threshold
7854 : 5538247 : && same_type_p (TREE_VALUE (chain), align_type_node))
7855 : : {
7856 : 1185984 : if (di) di->aligned = true;
7857 : 1185984 : chain = TREE_CHAIN (chain);
7858 : : }
7859 : 2776364 : return (chain == void_list_node);
7860 : : }
7861 : :
7862 : : /* Just return whether FN is a usual deallocation function. */
7863 : :
7864 : : bool
7865 : 8433 : usual_deallocation_fn_p (tree fn)
7866 : : {
7867 : 8433 : return usual_deallocation_fn_p (fn, NULL);
7868 : : }
7869 : :
7870 : : /* Build a call to operator delete. This has to be handled very specially,
7871 : : because the restrictions on what signatures match are different from all
7872 : : other call instances. For a normal delete, only a delete taking (void *)
7873 : : or (void *, size_t) is accepted. For a placement delete, only an exact
7874 : : match with the placement new is accepted.
7875 : :
7876 : : CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
7877 : : ADDR is the pointer to be deleted.
7878 : : SIZE is the size of the memory block to be deleted.
7879 : : GLOBAL_P is true if the delete-expression should not consider
7880 : : class-specific delete operators.
7881 : : CORO_P is true if the allocation is for a coroutine, where the two argument
7882 : : usual deallocation should be chosen in preference to the single argument
7883 : : version in a class context.
7884 : : PLACEMENT is the corresponding placement new call, or NULL_TREE.
7885 : :
7886 : : If this call to "operator delete" is being generated as part to
7887 : : deallocate memory allocated via a new-expression (as per [expr.new]
7888 : : which requires that if the initialization throws an exception then
7889 : : we call a deallocation function), then ALLOC_FN is the allocation
7890 : : function. */
7891 : :
7892 : : static tree
7893 : 637293 : build_op_delete_call_1 (enum tree_code code, tree addr, tree size,
7894 : : bool global_p, bool coro_p, tree placement,
7895 : : tree alloc_fn, tsubst_flags_t complain)
7896 : : {
7897 : 637293 : tree fn = NULL_TREE;
7898 : 637293 : tree fns, fnname, type, t;
7899 : 637293 : dealloc_info di_fn = { };
7900 : :
7901 : 637293 : if (addr == error_mark_node)
7902 : : return error_mark_node;
7903 : :
7904 : 637293 : type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
7905 : :
7906 : 637293 : fnname = ovl_op_identifier (false, code);
7907 : :
7908 : 556226 : if (CLASS_TYPE_P (type)
7909 : 556193 : && COMPLETE_TYPE_P (complete_type (type))
7910 : 1193459 : && !global_p)
7911 : : /* In [class.free]
7912 : :
7913 : : If the result of the lookup is ambiguous or inaccessible, or if
7914 : : the lookup selects a placement deallocation function, the
7915 : : program is ill-formed.
7916 : :
7917 : : Therefore, we ask lookup_fnfields to complain about ambiguity. */
7918 : : {
7919 : 376859 : fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1, complain);
7920 : 376859 : if (fns == error_mark_node)
7921 : : return error_mark_node;
7922 : : }
7923 : : else
7924 : : fns = NULL_TREE;
7925 : :
7926 : 376856 : if (fns == NULL_TREE)
7927 : 636350 : fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE);
7928 : :
7929 : : /* Strip const and volatile from addr. */
7930 : 637290 : tree oaddr = addr;
7931 : 637290 : addr = cp_convert (ptr_type_node, addr, complain);
7932 : :
7933 : 637290 : tree excluded_destroying = NULL_TREE;
7934 : :
7935 : 637290 : if (placement)
7936 : : {
7937 : : /* "A declaration of a placement deallocation function matches the
7938 : : declaration of a placement allocation function if it has the same
7939 : : number of parameters and, after parameter transformations (8.3.5),
7940 : : all parameter types except the first are identical."
7941 : :
7942 : : So we build up the function type we want and ask instantiate_type
7943 : : to get it for us. */
7944 : 234322 : t = FUNCTION_ARG_CHAIN (alloc_fn);
7945 : 234322 : t = tree_cons (NULL_TREE, ptr_type_node, t);
7946 : 234322 : t = build_function_type (void_type_node, t);
7947 : :
7948 : 234322 : fn = instantiate_type (t, fns, tf_none);
7949 : 234322 : if (fn == error_mark_node)
7950 : : return NULL_TREE;
7951 : :
7952 : 233534 : fn = MAYBE_BASELINK_FUNCTIONS (fn);
7953 : :
7954 : : /* "If the lookup finds the two-parameter form of a usual deallocation
7955 : : function (3.7.4.2) and that function, considered as a placement
7956 : : deallocation function, would have been selected as a match for the
7957 : : allocation function, the program is ill-formed." */
7958 : 233534 : if (second_parm_is_size_t (fn))
7959 : : {
7960 : 9 : const char *const msg1
7961 : : = G_("exception cleanup for this placement new selects "
7962 : : "non-placement %<operator delete%>");
7963 : 9 : const char *const msg2
7964 : : = G_("%qD is a usual (non-placement) deallocation "
7965 : : "function in C++14 (or with %<-fsized-deallocation%>)");
7966 : :
7967 : : /* But if the class has an operator delete (void *), then that is
7968 : : the usual deallocation function, so we shouldn't complain
7969 : : about using the operator delete (void *, size_t). */
7970 : 9 : if (DECL_CLASS_SCOPE_P (fn))
7971 : 12 : for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns)))
7972 : : {
7973 : 9 : if (usual_deallocation_fn_p (elt)
7974 : 9 : && FUNCTION_ARG_CHAIN (elt) == void_list_node)
7975 : 3 : goto ok;
7976 : : }
7977 : : /* Before C++14 a two-parameter global deallocation function is
7978 : : always a placement deallocation function, but warn if
7979 : : -Wc++14-compat. */
7980 : 3 : else if (!flag_sized_deallocation)
7981 : : {
7982 : 1 : if (complain & tf_warning)
7983 : : {
7984 : 1 : auto_diagnostic_group d;
7985 : 1 : if (warning (OPT_Wc__14_compat, msg1))
7986 : 1 : inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
7987 : 1 : }
7988 : 1 : goto ok;
7989 : : }
7990 : :
7991 : 5 : if (complain & tf_warning_or_error)
7992 : : {
7993 : 5 : auto_diagnostic_group d;
7994 : 5 : if (permerror (input_location, msg1))
7995 : : {
7996 : : /* Only mention C++14 for namespace-scope delete. */
7997 : 5 : if (DECL_NAMESPACE_SCOPE_P (fn))
7998 : 2 : inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
7999 : : else
8000 : 3 : inform (DECL_SOURCE_LOCATION (fn),
8001 : : "%qD is a usual (non-placement) deallocation "
8002 : : "function", fn);
8003 : : }
8004 : 5 : }
8005 : : else
8006 : 0 : return error_mark_node;
8007 : 636502 : ok:;
8008 : : }
8009 : : }
8010 : : else
8011 : : /* "Any non-placement deallocation function matches a non-placement
8012 : : allocation function. If the lookup finds a single matching
8013 : : deallocation function, that function will be called; otherwise, no
8014 : : deallocation function will be called." */
8015 : 3170899 : for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns)))
8016 : : {
8017 : 2767931 : dealloc_info di_elt;
8018 : 2767931 : if (usual_deallocation_fn_p (elt, &di_elt))
8019 : : {
8020 : : /* If we're called for an EH cleanup in a new-expression, we can't
8021 : : use a destroying delete; the exception was thrown before the
8022 : : object was constructed. */
8023 : 1592562 : if (alloc_fn && di_elt.destroying)
8024 : : {
8025 : 14 : excluded_destroying = elt;
8026 : 799429 : continue;
8027 : : }
8028 : :
8029 : 1592548 : if (!fn)
8030 : : {
8031 : 402944 : fn = elt;
8032 : 402944 : di_fn = di_elt;
8033 : 402944 : continue;
8034 : : }
8035 : :
8036 : : /* -- If any of the deallocation functions is a destroying
8037 : : operator delete, all deallocation functions that are not
8038 : : destroying operator deletes are eliminated from further
8039 : : consideration. */
8040 : 1189604 : if (di_elt.destroying != di_fn.destroying)
8041 : : {
8042 : 12 : if (di_elt.destroying)
8043 : : {
8044 : 6 : fn = elt;
8045 : 6 : di_fn = di_elt;
8046 : : }
8047 : 12 : continue;
8048 : : }
8049 : :
8050 : : /* -- If the type has new-extended alignment, a function with a
8051 : : parameter of type std::align_val_t is preferred; otherwise a
8052 : : function without such a parameter is preferred. If exactly one
8053 : : preferred function is found, that function is selected and the
8054 : : selection process terminates. If more than one preferred
8055 : : function is found, all non-preferred functions are eliminated
8056 : : from further consideration. */
8057 : 1189592 : if (aligned_new_threshold)
8058 : : {
8059 : 1189351 : bool want_align = type_has_new_extended_alignment (type);
8060 : 1189351 : if (di_elt.aligned != di_fn.aligned)
8061 : : {
8062 : 396459 : if (want_align == di_elt.aligned)
8063 : : {
8064 : 396428 : fn = elt;
8065 : 396428 : di_fn = di_elt;
8066 : : }
8067 : 396459 : continue;
8068 : : }
8069 : : }
8070 : :
8071 : : /* -- If the deallocation functions have class scope, the one
8072 : : without a parameter of type std::size_t is selected. */
8073 : 793133 : bool want_size;
8074 : 793133 : if (DECL_CLASS_SCOPE_P (fn) && !coro_p)
8075 : : want_size = false;
8076 : :
8077 : : /* -- If the type is complete and if, for the second alternative
8078 : : (delete array) only, the operand is a pointer to a class type
8079 : : with a non-trivial destructor or a (possibly multi-dimensional)
8080 : : array thereof, the function with a parameter of type std::size_t
8081 : : is selected.
8082 : :
8083 : : -- Otherwise, it is unspecified whether a deallocation function
8084 : : with a parameter of type std::size_t is selected. */
8085 : : else
8086 : : {
8087 : 793025 : want_size = COMPLETE_TYPE_P (type);
8088 : 793025 : if (code == VEC_DELETE_EXPR
8089 : 793025 : && !TYPE_VEC_NEW_USES_COOKIE (type))
8090 : : /* We need a cookie to determine the array size. */
8091 : : want_size = false;
8092 : : }
8093 : 793133 : gcc_assert (di_fn.sized != di_elt.sized);
8094 : 793133 : if (want_size == di_elt.sized)
8095 : : {
8096 : 26265 : fn = elt;
8097 : 26265 : di_fn = di_elt;
8098 : : }
8099 : : }
8100 : : }
8101 : :
8102 : : /* If we have a matching function, call it. */
8103 : 636502 : if (fn)
8104 : : {
8105 : 636478 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8106 : :
8107 : : /* If the FN is a member function, make sure that it is
8108 : : accessible. */
8109 : 636478 : if (BASELINK_P (fns))
8110 : 874 : perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn,
8111 : : complain);
8112 : :
8113 : : /* Core issue 901: It's ok to new a type with deleted delete. */
8114 : 636478 : if (DECL_DELETED_FN (fn) && alloc_fn)
8115 : : return NULL_TREE;
8116 : :
8117 : 636472 : tree ret;
8118 : 636472 : if (placement)
8119 : : {
8120 : : /* The placement args might not be suitable for overload
8121 : : resolution at this point, so build the call directly. */
8122 : 233534 : int nargs = call_expr_nargs (placement);
8123 : 233534 : tree *argarray = XALLOCAVEC (tree, nargs);
8124 : 233534 : int i;
8125 : 233534 : argarray[0] = addr;
8126 : 467107 : for (i = 1; i < nargs; i++)
8127 : 233573 : argarray[i] = CALL_EXPR_ARG (placement, i);
8128 : 233534 : if (!mark_used (fn, complain) && !(complain & tf_error))
8129 : 0 : return error_mark_node;
8130 : 233534 : ret = build_cxx_call (fn, nargs, argarray, complain);
8131 : : }
8132 : : else
8133 : : {
8134 : 402938 : tree destroying = di_fn.destroying;
8135 : 402938 : if (destroying)
8136 : : {
8137 : : /* Strip const and volatile from addr but retain the type of the
8138 : : object. */
8139 : 26 : tree rtype = TREE_TYPE (TREE_TYPE (oaddr));
8140 : 26 : rtype = cv_unqualified (rtype);
8141 : 26 : rtype = TYPE_POINTER_TO (rtype);
8142 : 26 : addr = cp_convert (rtype, oaddr, complain);
8143 : 26 : destroying = build_functional_cast (input_location,
8144 : : destroying, NULL_TREE,
8145 : : complain);
8146 : : }
8147 : :
8148 : 402938 : releasing_vec args;
8149 : 402938 : args->quick_push (addr);
8150 : 402938 : if (destroying)
8151 : 26 : args->quick_push (destroying);
8152 : 402938 : if (di_fn.sized)
8153 : 383752 : args->quick_push (size);
8154 : 402938 : if (di_fn.aligned)
8155 : : {
8156 : 19 : tree al = build_int_cst (align_type_node, TYPE_ALIGN_UNIT (type));
8157 : 19 : args->quick_push (al);
8158 : : }
8159 : 402938 : ret = cp_build_function_call_vec (fn, &args, complain);
8160 : 402938 : }
8161 : :
8162 : : /* Set this flag for all callers of this function. In addition to
8163 : : delete-expressions, this is called for deallocating coroutine state;
8164 : : treat that as an implicit delete-expression. This is also called for
8165 : : the delete if the constructor throws in a new-expression, and for a
8166 : : deleting destructor (which implements a delete-expression). */
8167 : : /* But leave this flag off for destroying delete to avoid wrong
8168 : : assumptions in the optimizers. */
8169 : 636472 : tree call = extract_call_expr (ret);
8170 : 636472 : if (TREE_CODE (call) == CALL_EXPR && !destroying_delete_p (fn))
8171 : 636440 : CALL_FROM_NEW_OR_DELETE_P (call) = 1;
8172 : :
8173 : 636472 : return ret;
8174 : : }
8175 : :
8176 : : /* If there's only a destroying delete that we can't use because the
8177 : : object isn't constructed yet, and we used global new, use global
8178 : : delete as well. */
8179 : 24 : if (excluded_destroying
8180 : 24 : && DECL_NAMESPACE_SCOPE_P (alloc_fn))
8181 : 8 : return build_op_delete_call (code, addr, size, true, placement,
8182 : 8 : alloc_fn, complain);
8183 : :
8184 : : /* [expr.new]
8185 : :
8186 : : If no unambiguous matching deallocation function can be found,
8187 : : propagating the exception does not cause the object's memory to
8188 : : be freed. */
8189 : 16 : if (alloc_fn)
8190 : : {
8191 : 15 : if ((complain & tf_warning)
8192 : 15 : && !placement)
8193 : : {
8194 : 15 : bool w = warning (0,
8195 : : "no corresponding deallocation function for %qD",
8196 : : alloc_fn);
8197 : 15 : if (w && excluded_destroying)
8198 : 3 : inform (DECL_SOURCE_LOCATION (excluded_destroying), "destroying "
8199 : : "delete %qD cannot be used to release the allocated memory"
8200 : : " if the initialization throws because the object is not "
8201 : : "constructed yet", excluded_destroying);
8202 : : }
8203 : 15 : return NULL_TREE;
8204 : : }
8205 : :
8206 : 1 : if (complain & tf_error)
8207 : 1 : error ("no suitable %<operator %s%> for %qT",
8208 : 1 : OVL_OP_INFO (false, code)->name, type);
8209 : 1 : return error_mark_node;
8210 : : }
8211 : :
8212 : : /* Arguments as per build_op_delete_call_1 (). */
8213 : :
8214 : : tree
8215 : 634611 : build_op_delete_call (enum tree_code code, tree addr, tree size, bool global_p,
8216 : : tree placement, tree alloc_fn, tsubst_flags_t complain)
8217 : : {
8218 : 634611 : return build_op_delete_call_1 (code, addr, size, global_p, /*coro_p*/false,
8219 : 634611 : placement, alloc_fn, complain);
8220 : : }
8221 : :
8222 : : /* Arguments as per build_op_delete_call_1 (). */
8223 : :
8224 : : tree
8225 : 2682 : build_coroutine_op_delete_call (enum tree_code code, tree addr, tree size,
8226 : : bool global_p, tree placement, tree alloc_fn,
8227 : : tsubst_flags_t complain)
8228 : : {
8229 : 2682 : return build_op_delete_call_1 (code, addr, size, global_p, /*coro_p*/true,
8230 : 2682 : placement, alloc_fn, complain);
8231 : : }
8232 : :
8233 : : /* Issue diagnostics about a disallowed access of DECL, using DIAG_DECL
8234 : : in the diagnostics.
8235 : :
8236 : : If ISSUE_ERROR is true, then issue an error about the access, followed
8237 : : by a note showing the declaration. Otherwise, just show the note.
8238 : :
8239 : : DIAG_DECL and DIAG_LOCATION will almost always be the same.
8240 : : DIAG_LOCATION is just another DECL. NO_ACCESS_REASON is an optional
8241 : : parameter used to specify why DECL wasn't accessible (e.g. ak_private
8242 : : would be because DECL was private). If not using NO_ACCESS_REASON,
8243 : : then it must be ak_none, and the access failure reason will be
8244 : : figured out by looking at the protection of DECL. */
8245 : :
8246 : : void
8247 : 1134 : complain_about_access (tree decl, tree diag_decl, tree diag_location,
8248 : : bool issue_error, access_kind no_access_reason)
8249 : : {
8250 : : /* If we have not already figured out why DECL is inaccessible... */
8251 : 1134 : if (no_access_reason == ak_none)
8252 : : {
8253 : : /* Examine the access of DECL to find out why. */
8254 : 952 : if (TREE_PRIVATE (decl))
8255 : : no_access_reason = ak_private;
8256 : 225 : else if (TREE_PROTECTED (decl))
8257 : : no_access_reason = ak_protected;
8258 : : }
8259 : :
8260 : : /* Now generate an error message depending on calculated access. */
8261 : 227 : if (no_access_reason == ak_private)
8262 : : {
8263 : 909 : if (issue_error)
8264 : 895 : error ("%q#D is private within this context", diag_decl);
8265 : 909 : inform (DECL_SOURCE_LOCATION (diag_location), "declared private here");
8266 : : }
8267 : 225 : else if (no_access_reason == ak_protected)
8268 : : {
8269 : 180 : if (issue_error)
8270 : 166 : error ("%q#D is protected within this context", diag_decl);
8271 : 180 : inform (DECL_SOURCE_LOCATION (diag_location), "declared protected here");
8272 : : }
8273 : : /* Couldn't figure out why DECL is inaccesible, so just say it's
8274 : : inaccessible. */
8275 : : else
8276 : : {
8277 : 45 : if (issue_error)
8278 : 45 : error ("%q#D is inaccessible within this context", diag_decl);
8279 : 45 : inform (DECL_SOURCE_LOCATION (diag_decl), "declared here");
8280 : : }
8281 : 1134 : }
8282 : :
8283 : : /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
8284 : : bitwise or of LOOKUP_* values. If any errors are warnings are
8285 : : generated, set *DIAGNOSTIC_FN to "error" or "warning",
8286 : : respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
8287 : : to NULL. */
8288 : :
8289 : : static tree
8290 : 9126822 : build_temp (tree expr, tree type, int flags,
8291 : : diagnostic_t *diagnostic_kind, tsubst_flags_t complain)
8292 : : {
8293 : 9126822 : int savew, savee;
8294 : :
8295 : 9126822 : *diagnostic_kind = DK_UNSPECIFIED;
8296 : :
8297 : : /* If the source is a packed field, calling the copy constructor will require
8298 : : binding the field to the reference parameter to the copy constructor, and
8299 : : we'll end up with an infinite loop. If we can use a bitwise copy, then
8300 : : do that now. */
8301 : 9126822 : if ((lvalue_kind (expr) & clk_packed)
8302 : 6 : && CLASS_TYPE_P (TREE_TYPE (expr))
8303 : 9126828 : && !type_has_nontrivial_copy_init (TREE_TYPE (expr)))
8304 : 3 : return get_target_expr (expr, complain);
8305 : :
8306 : : /* In decltype, we might have decided not to wrap this call in a TARGET_EXPR.
8307 : : But it turns out to be a subexpression, so perform temporary
8308 : : materialization now. */
8309 : 9126819 : if (TREE_CODE (expr) == CALL_EXPR
8310 : 6 : && CLASS_TYPE_P (type)
8311 : 9126825 : && same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
8312 : 6 : expr = build_cplus_new (type, expr, complain);
8313 : :
8314 : 9126819 : savew = warningcount + werrorcount, savee = errorcount;
8315 : 9126819 : releasing_vec args (make_tree_vector_single (expr));
8316 : 9126819 : expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
8317 : : &args, type, flags, complain);
8318 : 9126819 : if (warningcount + werrorcount > savew)
8319 : 0 : *diagnostic_kind = DK_WARNING;
8320 : 9126819 : else if (errorcount > savee)
8321 : 62 : *diagnostic_kind = DK_ERROR;
8322 : 9126819 : return expr;
8323 : 9126819 : }
8324 : :
8325 : : /* Get any location for EXPR, falling back to input_location.
8326 : :
8327 : : If the result is in a system header and is the virtual location for
8328 : : a token coming from the expansion of a macro, unwind it to the
8329 : : location of the expansion point of the macro (e.g. to avoid the
8330 : : diagnostic being suppressed for expansions of NULL where "NULL" is
8331 : : in a system header). */
8332 : :
8333 : : static location_t
8334 : 1663873 : get_location_for_expr_unwinding_for_system_header (tree expr)
8335 : : {
8336 : 1663873 : location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
8337 : 1663873 : loc = expansion_point_location_if_in_system_header (loc);
8338 : 1663873 : return loc;
8339 : : }
8340 : :
8341 : : /* Perform warnings about peculiar, but valid, conversions from/to NULL.
8342 : : Also handle a subset of zero as null warnings.
8343 : : EXPR is implicitly converted to type TOTYPE.
8344 : : FN and ARGNUM are used for diagnostics. */
8345 : :
8346 : : static void
8347 : 375096056 : conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
8348 : : {
8349 : : /* Issue warnings about peculiar, but valid, uses of NULL. */
8350 : 375096056 : if (TREE_CODE (totype) != BOOLEAN_TYPE
8351 : 222437911 : && ARITHMETIC_TYPE_P (totype)
8352 : 497348790 : && null_node_p (expr))
8353 : : {
8354 : 94 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8355 : 94 : if (fn)
8356 : : {
8357 : 33 : auto_diagnostic_group d;
8358 : 33 : if (warning_at (loc, OPT_Wconversion_null,
8359 : : "passing NULL to non-pointer argument %P of %qD",
8360 : : argnum, fn))
8361 : 27 : inform (get_fndecl_argument_location (fn, argnum),
8362 : : "declared here");
8363 : 33 : }
8364 : : else
8365 : 61 : warning_at (loc, OPT_Wconversion_null,
8366 : : "converting to non-pointer type %qT from NULL", totype);
8367 : : }
8368 : :
8369 : : /* Issue warnings if "false" is converted to a NULL pointer */
8370 : 375095962 : else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
8371 : 375095962 : && TYPE_PTR_P (totype))
8372 : : {
8373 : 7 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8374 : 7 : if (fn)
8375 : : {
8376 : 3 : auto_diagnostic_group d;
8377 : 3 : if (warning_at (loc, OPT_Wconversion_null,
8378 : : "converting %<false%> to pointer type for argument "
8379 : : "%P of %qD", argnum, fn))
8380 : 3 : inform (get_fndecl_argument_location (fn, argnum),
8381 : : "declared here");
8382 : 3 : }
8383 : : else
8384 : 4 : warning_at (loc, OPT_Wconversion_null,
8385 : : "converting %<false%> to pointer type %qT", totype);
8386 : : }
8387 : : /* Handle zero as null pointer warnings for cases other
8388 : : than EQ_EXPR and NE_EXPR */
8389 : 343716080 : else if ((TYPE_PTR_OR_PTRMEM_P (totype) || NULLPTR_TYPE_P (totype))
8390 : 375363998 : && null_ptr_cst_p (expr))
8391 : : {
8392 : 1663772 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8393 : 1663772 : maybe_warn_zero_as_null_pointer_constant (expr, loc);
8394 : : }
8395 : 375096056 : }
8396 : :
8397 : : /* We gave a diagnostic during a conversion. If this was in the second
8398 : : standard conversion sequence of a user-defined conversion sequence, say
8399 : : which user-defined conversion. */
8400 : :
8401 : : static void
8402 : 5190 : maybe_print_user_conv_context (conversion *convs)
8403 : : {
8404 : 5190 : if (convs->user_conv_p)
8405 : 160 : for (conversion *t = convs; t; t = next_conversion (t))
8406 : 136 : if (t->kind == ck_user)
8407 : : {
8408 : 41 : print_z_candidate (0, N_(" after user-defined conversion:"),
8409 : : t->cand);
8410 : 41 : break;
8411 : : }
8412 : 5190 : }
8413 : :
8414 : : /* Locate the parameter with the given index within FNDECL.
8415 : : ARGNUM is zero based, -1 indicates the `this' argument of a method.
8416 : : Return the location of the FNDECL itself if there are problems. */
8417 : :
8418 : : location_t
8419 : 6635621 : get_fndecl_argument_location (tree fndecl, int argnum)
8420 : : {
8421 : : /* The locations of implicitly-declared functions are likely to be
8422 : : more meaningful than those of their parameters. */
8423 : 6635621 : if (DECL_ARTIFICIAL (fndecl))
8424 : 309 : return DECL_SOURCE_LOCATION (fndecl);
8425 : :
8426 : 6635312 : int i;
8427 : 6635312 : tree param;
8428 : :
8429 : : /* Locate param by index within DECL_ARGUMENTS (fndecl). */
8430 : 6635312 : for (i = 0, param = FUNCTION_FIRST_USER_PARM (fndecl);
8431 : 15542971 : i < argnum && param;
8432 : 8907659 : i++, param = TREE_CHAIN (param))
8433 : : ;
8434 : :
8435 : : /* If something went wrong (e.g. if we have a builtin and thus no arguments),
8436 : : return the location of FNDECL. */
8437 : 6635312 : if (param == NULL)
8438 : 29 : return DECL_SOURCE_LOCATION (fndecl);
8439 : :
8440 : 6635283 : return DECL_SOURCE_LOCATION (param);
8441 : : }
8442 : :
8443 : : /* If FNDECL is non-NULL, issue a note highlighting ARGNUM
8444 : : within its declaration (or the fndecl itself if something went
8445 : : wrong). */
8446 : :
8447 : : void
8448 : 6932 : maybe_inform_about_fndecl_for_bogus_argument_init (tree fn, int argnum,
8449 : : const char *highlight_color)
8450 : : {
8451 : 6932 : if (fn)
8452 : : {
8453 : 1074 : gcc_rich_location richloc (get_fndecl_argument_location (fn, argnum));
8454 : 1074 : richloc.set_highlight_color (highlight_color);
8455 : 1074 : inform (&richloc,
8456 : : "initializing argument %P of %qD", argnum, fn);
8457 : 1074 : }
8458 : 6932 : }
8459 : :
8460 : : /* Maybe warn about C++20 Conversions to arrays of unknown bound. C is
8461 : : the conversion, EXPR is the expression we're converting. */
8462 : :
8463 : : static void
8464 : 26682359 : maybe_warn_array_conv (location_t loc, conversion *c, tree expr)
8465 : : {
8466 : 26682359 : if (cxx_dialect >= cxx20)
8467 : : return;
8468 : :
8469 : 15590696 : tree type = TREE_TYPE (expr);
8470 : 15590696 : type = strip_pointer_operator (type);
8471 : :
8472 : 15590696 : if (TREE_CODE (type) != ARRAY_TYPE
8473 : 15590696 : || TYPE_DOMAIN (type) == NULL_TREE)
8474 : : return;
8475 : :
8476 : 12262 : if (pedantic && conv_binds_to_array_of_unknown_bound (c))
8477 : 10 : pedwarn (loc, OPT_Wc__20_extensions,
8478 : : "conversions to arrays of unknown bound "
8479 : : "are only available with %<-std=c++20%> or %<-std=gnu++20%>");
8480 : : }
8481 : :
8482 : : /* We call this recursively in convert_like_internal. */
8483 : : static tree convert_like (conversion *, tree, tree, int, bool, bool, bool,
8484 : : tsubst_flags_t);
8485 : :
8486 : : /* Perform the conversions in CONVS on the expression EXPR. FN and
8487 : : ARGNUM are used for diagnostics. ARGNUM is zero based, -1
8488 : : indicates the `this' argument of a method. INNER is nonzero when
8489 : : being called to continue a conversion chain. It is negative when a
8490 : : reference binding will be applied, positive otherwise. If
8491 : : ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
8492 : : conversions will be emitted if appropriate. If C_CAST_P is true,
8493 : : this conversion is coming from a C-style cast; in that case,
8494 : : conversions to inaccessible bases are permitted. */
8495 : :
8496 : : static tree
8497 : 730758531 : convert_like_internal (conversion *convs, tree expr, tree fn, int argnum,
8498 : : bool issue_conversion_warnings, bool c_cast_p,
8499 : : bool nested_p, tsubst_flags_t complain)
8500 : : {
8501 : 730758531 : tree totype = convs->type;
8502 : 730758531 : diagnostic_t diag_kind;
8503 : 730758531 : int flags;
8504 : 730758531 : location_t loc = cp_expr_loc_or_input_loc (expr);
8505 : :
8506 : 730758531 : if (convs->bad_p && !(complain & tf_error))
8507 : 22251 : return error_mark_node;
8508 : :
8509 : 730736280 : if (convs->bad_p
8510 : 6995 : && convs->kind != ck_user
8511 : 6893 : && convs->kind != ck_list
8512 : 6887 : && convs->kind != ck_ambig
8513 : 6887 : && (convs->kind != ck_ref_bind
8514 : 5277 : || (convs->user_conv_p && next_conversion (convs)->bad_p))
8515 : 1631 : && (convs->kind != ck_rvalue
8516 : 15 : || SCALAR_TYPE_P (totype))
8517 : 730737902 : && convs->kind != ck_base)
8518 : : {
8519 : 1622 : int complained = 0;
8520 : 1622 : conversion *t = convs;
8521 : :
8522 : : /* Give a helpful error if this is bad because of excess braces. */
8523 : 46 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8524 : 46 : && SCALAR_TYPE_P (totype)
8525 : 34 : && CONSTRUCTOR_NELTS (expr) > 0
8526 : 1656 : && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
8527 : : {
8528 : 10 : complained = permerror (loc, "too many braces around initializer "
8529 : : "for %qT", totype);
8530 : 30 : while (BRACE_ENCLOSED_INITIALIZER_P (expr)
8531 : 50 : && CONSTRUCTOR_NELTS (expr) == 1)
8532 : 20 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
8533 : : }
8534 : :
8535 : : /* Give a helpful error if this is bad because a conversion to bool
8536 : : from std::nullptr_t requires direct-initialization. */
8537 : 1622 : if (NULLPTR_TYPE_P (TREE_TYPE (expr))
8538 : 1622 : && TREE_CODE (totype) == BOOLEAN_TYPE)
8539 : 15 : complained = permerror (loc, "converting to %qH from %qI requires "
8540 : : "direct-initialization",
8541 : 15 : totype, TREE_TYPE (expr));
8542 : :
8543 : 1622 : if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (expr))
8544 : 69 : && SCALAR_FLOAT_TYPE_P (totype)
8545 : 1691 : && (extended_float_type_p (TREE_TYPE (expr))
8546 : 18 : || extended_float_type_p (totype)))
8547 : 69 : switch (cp_compare_floating_point_conversion_ranks (TREE_TYPE (expr),
8548 : : totype))
8549 : : {
8550 : 69 : case 2:
8551 : 69 : if (pedwarn (loc, OPT_Wnarrowing, "ISO C++ does not allow "
8552 : : "converting to %qH from %qI with greater "
8553 : 69 : "conversion rank", totype, TREE_TYPE (expr)))
8554 : : complained = 1;
8555 : 15 : else if (!complained)
8556 : 15 : complained = -1;
8557 : : break;
8558 : 0 : case 3:
8559 : 0 : if (pedwarn (loc, OPT_Wnarrowing, "ISO C++ does not allow "
8560 : : "converting to %qH from %qI with unordered "
8561 : 0 : "conversion rank", totype, TREE_TYPE (expr)))
8562 : : complained = 1;
8563 : 0 : else if (!complained)
8564 : 0 : complained = -1;
8565 : : break;
8566 : : default:
8567 : : break;
8568 : : }
8569 : :
8570 : 3312 : for (; t ; t = next_conversion (t))
8571 : : {
8572 : 3303 : if (t->kind == ck_user && t->cand->reason)
8573 : : {
8574 : 52 : auto_diagnostic_group d;
8575 : 52 : complained = permerror (loc, "invalid user-defined conversion "
8576 : 52 : "from %qH to %qI", TREE_TYPE (expr),
8577 : : totype);
8578 : 52 : if (complained)
8579 : 52 : print_z_candidate (loc, N_("candidate is:"), t->cand);
8580 : 52 : expr = convert_like (t, expr, fn, argnum,
8581 : : /*issue_conversion_warnings=*/false,
8582 : : /*c_cast_p=*/false, /*nested_p=*/true,
8583 : : complain);
8584 : 52 : }
8585 : 3251 : else if (t->kind == ck_user || !t->bad_p)
8586 : : {
8587 : 1613 : expr = convert_like (t, expr, fn, argnum,
8588 : : /*issue_conversion_warnings=*/false,
8589 : : /*c_cast_p=*/false, /*nested_p=*/true,
8590 : : complain);
8591 : 1613 : if (t->bad_p)
8592 : : complained = 1;
8593 : : break;
8594 : : }
8595 : 1638 : else if (t->kind == ck_ambig)
8596 : 0 : return convert_like (t, expr, fn, argnum,
8597 : : /*issue_conversion_warnings=*/false,
8598 : : /*c_cast_p=*/false, /*nested_p=*/true,
8599 : 0 : complain);
8600 : 1638 : else if (t->kind == ck_identity)
8601 : : break;
8602 : : }
8603 : 1622 : if (!complained && expr != error_mark_node)
8604 : : {
8605 : 1476 : range_label_for_type_mismatch label (TREE_TYPE (expr), totype);
8606 : 1476 : gcc_rich_location richloc (loc, &label, highlight_colors::percent_h);
8607 : 1476 : complained = permerror (&richloc,
8608 : : "invalid conversion from %qH to %qI",
8609 : 1476 : TREE_TYPE (expr), totype);
8610 : 1476 : if (complained)
8611 : 1421 : maybe_emit_indirection_note (loc, expr, totype);
8612 : 1476 : }
8613 : 1622 : if (convs->kind == ck_ref_bind)
8614 : 21 : expr = convert_to_reference (totype, expr, CONV_IMPLICIT,
8615 : : LOOKUP_NORMAL, NULL_TREE,
8616 : : complain);
8617 : : else
8618 : 1601 : expr = cp_convert (totype, expr, complain);
8619 : 1622 : if (complained == 1)
8620 : 1552 : maybe_inform_about_fndecl_for_bogus_argument_init
8621 : 1552 : (fn, argnum, highlight_colors::percent_i);
8622 : 1622 : return expr;
8623 : : }
8624 : :
8625 : 730734658 : if (issue_conversion_warnings && (complain & tf_warning))
8626 : 375096056 : conversion_null_warnings (totype, expr, fn, argnum);
8627 : :
8628 : 730734658 : switch (convs->kind)
8629 : : {
8630 : 4057059 : case ck_user:
8631 : 4057059 : {
8632 : 4057059 : struct z_candidate *cand = convs->cand;
8633 : :
8634 : 4057059 : if (cand == NULL)
8635 : : /* We chose the surrogate function from add_conv_candidate, now we
8636 : : actually need to build the conversion. */
8637 : 56 : cand = build_user_type_conversion_1 (totype, expr,
8638 : : LOOKUP_NO_CONVERSION, complain);
8639 : :
8640 : 4057059 : tree convfn = cand->fn;
8641 : :
8642 : : /* When converting from an init list we consider explicit
8643 : : constructors, but actually trying to call one is an error. */
8644 : 4231733 : if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
8645 : 2287 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
8646 : : /* Unless this is for direct-list-initialization. */
8647 : 2284 : && (!CONSTRUCTOR_IS_DIRECT_INIT (expr) || convs->need_temporary_p)
8648 : : /* And in C++98 a default constructor can't be explicit. */
8649 : 4057272 : && cxx_dialect >= cxx11)
8650 : : {
8651 : 212 : if (!(complain & tf_error))
8652 : 36 : return error_mark_node;
8653 : 176 : location_t loc = location_of (expr);
8654 : 176 : if (CONSTRUCTOR_NELTS (expr) == 0
8655 : 176 : && FUNCTION_FIRST_USER_PARMTYPE (convfn) != void_list_node)
8656 : : {
8657 : 7 : auto_diagnostic_group d;
8658 : 7 : if (pedwarn (loc, 0, "converting to %qT from initializer list "
8659 : : "would use explicit constructor %qD",
8660 : : totype, convfn))
8661 : : {
8662 : 7 : inform (DECL_SOURCE_LOCATION (convfn), "%qD declared here",
8663 : : convfn);
8664 : 7 : inform (loc, "in C++11 and above a default constructor "
8665 : : "can be explicit");
8666 : : }
8667 : 7 : }
8668 : : else
8669 : : {
8670 : 169 : auto_diagnostic_group d;
8671 : 169 : error ("converting to %qT from initializer list would use "
8672 : : "explicit constructor %qD", totype, convfn);
8673 : 169 : inform (DECL_SOURCE_LOCATION (convfn), "%qD declared here",
8674 : : convfn);
8675 : 169 : }
8676 : : }
8677 : :
8678 : : /* If we're initializing from {}, it's value-initialization. */
8679 : 577403 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8680 : 577403 : && CONSTRUCTOR_NELTS (expr) == 0
8681 : 100695 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype)
8682 : 4157691 : && !processing_template_decl)
8683 : : {
8684 : 100668 : if (abstract_virtuals_error (NULL_TREE, totype, complain))
8685 : 15 : return error_mark_node;
8686 : 100653 : expr = build_value_init (totype, complain);
8687 : 100653 : expr = get_target_expr (expr, complain);
8688 : 100653 : if (expr != error_mark_node)
8689 : 100532 : TARGET_EXPR_LIST_INIT_P (expr) = true;
8690 : 100653 : return expr;
8691 : : }
8692 : :
8693 : : /* We don't know here whether EXPR is being used as an lvalue or
8694 : : rvalue, but we know it's read. */
8695 : 3956355 : mark_exp_read (expr);
8696 : :
8697 : : /* Give the conversion call the location of EXPR rather than the
8698 : : location of the context that caused the conversion. */
8699 : 3956355 : iloc_sentinel ils (loc);
8700 : :
8701 : : /* Pass LOOKUP_NO_CONVERSION so rvalue/base handling knows not to allow
8702 : : any more UDCs. */
8703 : 3956355 : expr = build_over_call (cand, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
8704 : : complain);
8705 : :
8706 : : /* If this is a constructor or a function returning an aggr type,
8707 : : we need to build up a TARGET_EXPR. */
8708 : 7912710 : if (DECL_CONSTRUCTOR_P (convfn))
8709 : : {
8710 : 1405623 : expr = build_cplus_new (totype, expr, complain);
8711 : :
8712 : : /* Remember that this was list-initialization. */
8713 : 1405623 : if (convs->check_narrowing && expr != error_mark_node)
8714 : 477552 : TARGET_EXPR_LIST_INIT_P (expr) = true;
8715 : : }
8716 : :
8717 : 3956355 : return expr;
8718 : 3956355 : }
8719 : 530931867 : case ck_identity:
8720 : 530931867 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
8721 : : {
8722 : 818505 : int nelts = CONSTRUCTOR_NELTS (expr);
8723 : 140347 : if (nelts == 0)
8724 : 678158 : expr = build_value_init (totype, complain);
8725 : 140347 : else if (nelts == 1)
8726 : 140347 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
8727 : : else
8728 : 0 : gcc_unreachable ();
8729 : : }
8730 : 530931867 : expr = mark_use (expr, /*rvalue_p=*/!convs->rvaluedness_matches_p,
8731 : : /*read_p=*/true, UNKNOWN_LOCATION,
8732 : : /*reject_builtin=*/true);
8733 : :
8734 : 530931867 : if (type_unknown_p (expr))
8735 : 16566 : expr = instantiate_type (totype, expr, complain);
8736 : 530931867 : if (!nested_p && TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8737 : 54272 : expr = cp_convert (totype, TREE_OPERAND (expr, 0), complain);
8738 : 530931867 : if (expr == null_node
8739 : 530931867 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype))
8740 : : /* If __null has been converted to an integer type, we do not want to
8741 : : continue to warn about uses of EXPR as an integer, rather than as a
8742 : : pointer. */
8743 : 71543 : expr = build_int_cst (totype, 0);
8744 : : return expr;
8745 : 47 : case ck_ambig:
8746 : : /* We leave bad_p off ck_ambig because overload resolution considers
8747 : : it valid, it just fails when we try to perform it. So we need to
8748 : : check complain here, too. */
8749 : 47 : if (complain & tf_error)
8750 : : {
8751 : : /* Call build_user_type_conversion again for the error. */
8752 : 94 : int flags = (convs->need_temporary_p
8753 : 47 : ? LOOKUP_IMPLICIT : LOOKUP_NORMAL);
8754 : 47 : build_user_type_conversion (totype, convs->u.expr, flags, complain);
8755 : 47 : gcc_assert (seen_error ());
8756 : 47 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
8757 : : }
8758 : 47 : return error_mark_node;
8759 : :
8760 : 2850 : case ck_list:
8761 : 2850 : {
8762 : : /* Conversion to std::initializer_list<T>. */
8763 : 2850 : tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
8764 : 2850 : unsigned len = CONSTRUCTOR_NELTS (expr);
8765 : 2850 : tree array;
8766 : :
8767 : 2850 : if (tree init = maybe_init_list_as_array (elttype, expr))
8768 : : {
8769 : 118 : elttype = cp_build_qualified_type
8770 : 118 : (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
8771 : 118 : array = build_array_of_n_type (elttype, len);
8772 : 118 : array = build_vec_init_expr (array, init, complain);
8773 : 118 : array = get_target_expr (array);
8774 : 118 : array = cp_build_addr_expr (array, complain);
8775 : : }
8776 : 2732 : else if (len)
8777 : : {
8778 : 2670 : tree val; unsigned ix;
8779 : :
8780 : 2670 : tree new_ctor = build_constructor (init_list_type_node, NULL);
8781 : :
8782 : : /* Convert all the elements. */
8783 : 11404 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
8784 : : {
8785 : 8743 : tree sub = convert_like (convs->u.list[ix], val, fn,
8786 : : argnum, false, false,
8787 : : /*nested_p=*/true, complain);
8788 : 8743 : if (sub == error_mark_node)
8789 : : return sub;
8790 : 1428 : if (!BRACE_ENCLOSED_INITIALIZER_P (val)
8791 : 8752 : && !check_narrowing (TREE_TYPE (sub), val, complain))
8792 : 0 : return error_mark_node;
8793 : 8734 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor),
8794 : : NULL_TREE, sub);
8795 : 8734 : if (!TREE_CONSTANT (sub))
8796 : 2338 : TREE_CONSTANT (new_ctor) = false;
8797 : : }
8798 : : /* Build up the array. */
8799 : 2661 : elttype = cp_build_qualified_type
8800 : 2661 : (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
8801 : 2661 : array = build_array_of_n_type (elttype, len);
8802 : 2661 : array = finish_compound_literal (array, new_ctor, complain);
8803 : : /* This is dubious now, should be blessed by P2752. */
8804 : 2661 : DECL_MERGEABLE (TARGET_EXPR_SLOT (array)) = true;
8805 : 2661 : array = cp_build_addr_expr (array, complain);
8806 : : }
8807 : : else
8808 : 62 : array = nullptr_node;
8809 : :
8810 : 2841 : array = cp_convert (build_pointer_type (elttype), array, complain);
8811 : 2841 : if (array == error_mark_node)
8812 : : return error_mark_node;
8813 : :
8814 : : /* Build up the initializer_list object. Note: fail gracefully
8815 : : if the object cannot be completed because, for example, no
8816 : : definition is provided (c++/80956). */
8817 : 2841 : totype = complete_type_or_maybe_complain (totype, NULL_TREE, complain);
8818 : 2841 : if (!totype)
8819 : 0 : return error_mark_node;
8820 : 2841 : tree field = next_aggregate_field (TYPE_FIELDS (totype));
8821 : 2841 : vec<constructor_elt, va_gc> *vec = NULL;
8822 : 2841 : CONSTRUCTOR_APPEND_ELT (vec, field, array);
8823 : 2841 : field = next_aggregate_field (DECL_CHAIN (field));
8824 : 2841 : CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
8825 : 2841 : tree new_ctor = build_constructor (totype, vec);
8826 : 2841 : return get_target_expr (new_ctor, complain);
8827 : : }
8828 : :
8829 : 988940 : case ck_aggr:
8830 : 988940 : if (TREE_CODE (totype) == COMPLEX_TYPE)
8831 : : {
8832 : 28296 : tree real = CONSTRUCTOR_ELT (expr, 0)->value;
8833 : 28296 : tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
8834 : 28296 : real = perform_implicit_conversion (TREE_TYPE (totype),
8835 : : real, complain);
8836 : 28296 : imag = perform_implicit_conversion (TREE_TYPE (totype),
8837 : : imag, complain);
8838 : 28296 : expr = build2 (COMPLEX_EXPR, totype, real, imag);
8839 : 28296 : return expr;
8840 : : }
8841 : 960644 : expr = reshape_init (totype, expr, complain);
8842 : 960644 : expr = get_target_expr (digest_init (totype, expr, complain),
8843 : : complain);
8844 : 960644 : if (expr != error_mark_node)
8845 : 960631 : TARGET_EXPR_LIST_INIT_P (expr) = true;
8846 : : return expr;
8847 : :
8848 : 194753895 : default:
8849 : 194753895 : break;
8850 : 194753895 : };
8851 : :
8852 : 194753895 : conversion *nc = next_conversion (convs);
8853 : 194753895 : if (convs->kind == ck_ref_bind && nc->kind == ck_qual
8854 : 11263 : && !convs->need_temporary_p)
8855 : : /* direct_reference_binding might have inserted a ck_qual under
8856 : : this ck_ref_bind for the benefit of conversion sequence ranking.
8857 : : Don't actually perform that conversion. */
8858 : 8493 : nc = next_conversion (nc);
8859 : :
8860 : 194753895 : expr = convert_like (nc, expr, fn, argnum,
8861 : : convs->kind == ck_ref_bind
8862 : 194753895 : ? issue_conversion_warnings : false,
8863 : : c_cast_p, /*nested_p=*/true, complain & ~tf_no_cleanup);
8864 : 194753895 : if (expr == error_mark_node)
8865 : : return error_mark_node;
8866 : :
8867 : 194753684 : switch (convs->kind)
8868 : : {
8869 : 95471205 : case ck_rvalue:
8870 : 95471205 : expr = decay_conversion (expr, complain);
8871 : 95471205 : if (expr == error_mark_node)
8872 : : {
8873 : 15 : if (complain & tf_error)
8874 : : {
8875 : 12 : auto_diagnostic_group d;
8876 : 12 : maybe_print_user_conv_context (convs);
8877 : 12 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
8878 : 12 : }
8879 : 15 : return error_mark_node;
8880 : : }
8881 : :
8882 : 95471190 : if (! MAYBE_CLASS_TYPE_P (totype))
8883 : : return expr;
8884 : :
8885 : : /* Don't introduce copies when passing arguments along to the inherited
8886 : : constructor. */
8887 : 8915940 : if (current_function_decl
8888 : 6806194 : && flag_new_inheriting_ctors
8889 : 22527848 : && DECL_INHERITED_CTOR (current_function_decl))
8890 : : return expr;
8891 : :
8892 : 8910143 : if (TREE_CODE (expr) == TARGET_EXPR
8893 : 8910143 : && TARGET_EXPR_LIST_INIT_P (expr))
8894 : : /* Copy-list-initialization doesn't actually involve a copy. */
8895 : : return expr;
8896 : :
8897 : : /* Fall through. */
8898 : 10256445 : case ck_base:
8899 : 10256445 : if (convs->kind == ck_base && !convs->need_temporary_p)
8900 : : {
8901 : : /* We are going to bind a reference directly to a base-class
8902 : : subobject of EXPR. */
8903 : : /* Build an expression for `*((base*) &expr)'. */
8904 : 1129623 : expr = convert_to_base (expr, totype,
8905 : 1129623 : !c_cast_p, /*nonnull=*/true, complain);
8906 : 1129623 : return expr;
8907 : : }
8908 : :
8909 : : /* Copy-initialization where the cv-unqualified version of the source
8910 : : type is the same class as, or a derived class of, the class of the
8911 : : destination [is treated as direct-initialization]. [dcl.init] */
8912 : 9126822 : flags = LOOKUP_NORMAL;
8913 : : /* This conversion is being done in the context of a user-defined
8914 : : conversion (i.e. the second step of copy-initialization), so
8915 : : don't allow any more. */
8916 : 9126822 : if (convs->user_conv_p)
8917 : 208548 : flags |= LOOKUP_NO_CONVERSION;
8918 : : /* We might be performing a conversion of the argument
8919 : : to the user-defined conversion, i.e., not a conversion of the
8920 : : result of the user-defined conversion. In which case we skip
8921 : : explicit constructors. */
8922 : 9126822 : if (convs->copy_init_p)
8923 : 8922930 : flags |= LOOKUP_ONLYCONVERTING;
8924 : 9126822 : expr = build_temp (expr, totype, flags, &diag_kind, complain);
8925 : 9126822 : if (diag_kind && complain)
8926 : : {
8927 : 62 : auto_diagnostic_group d;
8928 : 62 : maybe_print_user_conv_context (convs);
8929 : 62 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
8930 : 62 : }
8931 : :
8932 : 9126822 : return build_cplus_new (totype, expr, complain);
8933 : :
8934 : 35172543 : case ck_ref_bind:
8935 : 35172543 : {
8936 : 35172543 : tree ref_type = totype;
8937 : :
8938 : 35172543 : if (convs->bad_p && !next_conversion (convs)->bad_p)
8939 : : {
8940 : 5116 : tree extype = TREE_TYPE (expr);
8941 : 5116 : auto_diagnostic_group d;
8942 : 5116 : if (TYPE_REF_IS_RVALUE (ref_type)
8943 : 5116 : && lvalue_p (expr))
8944 : 1633 : error_at (loc, "cannot bind rvalue reference of type %qH to "
8945 : : "lvalue of type %qI", totype, extype);
8946 : 6120 : else if (!TYPE_REF_IS_RVALUE (ref_type) && !lvalue_p (expr)
8947 : 4947 : && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
8948 : : {
8949 : 1215 : conversion *next = next_conversion (convs);
8950 : 1215 : if (next->kind == ck_std)
8951 : : {
8952 : 61 : next = next_conversion (next);
8953 : 61 : error_at (loc, "cannot bind non-const lvalue reference of "
8954 : : "type %qH to a value of type %qI",
8955 : : totype, next->type);
8956 : : }
8957 : 1154 : else if (!CP_TYPE_CONST_P (TREE_TYPE (ref_type)))
8958 : 813 : error_at (loc, "cannot bind non-const lvalue reference of "
8959 : : "type %qH to an rvalue of type %qI", totype, extype);
8960 : : else // extype is volatile
8961 : 341 : error_at (loc, "cannot bind lvalue reference of type "
8962 : : "%qH to an rvalue of type %qI", totype,
8963 : : extype);
8964 : : }
8965 : 2268 : else if (!reference_compatible_p (TREE_TYPE (totype), extype))
8966 : : {
8967 : : /* If we're converting from T[] to T[N], don't talk
8968 : : about discarding qualifiers. (Converting from T[N] to
8969 : : T[] is allowed by P0388R4.) */
8970 : 2268 : if (TREE_CODE (extype) == ARRAY_TYPE
8971 : 27 : && TYPE_DOMAIN (extype) == NULL_TREE
8972 : 15 : && TREE_CODE (TREE_TYPE (totype)) == ARRAY_TYPE
8973 : 2283 : && TYPE_DOMAIN (TREE_TYPE (totype)) != NULL_TREE)
8974 : 15 : error_at (loc, "cannot bind reference of type %qH to %qI "
8975 : : "due to different array bounds", totype, extype);
8976 : : else
8977 : 2253 : error_at (loc, "binding reference of type %qH to %qI "
8978 : : "discards qualifiers", totype, extype);
8979 : : }
8980 : : else
8981 : 0 : gcc_unreachable ();
8982 : 5116 : maybe_print_user_conv_context (convs);
8983 : 5116 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
8984 : :
8985 : 5116 : return error_mark_node;
8986 : 5116 : }
8987 : 35167427 : else if (complain & tf_warning)
8988 : 25713927 : maybe_warn_array_conv (loc, convs, expr);
8989 : :
8990 : : /* If necessary, create a temporary.
8991 : :
8992 : : VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
8993 : : that need temporaries, even when their types are reference
8994 : : compatible with the type of reference being bound, so the
8995 : : upcoming call to cp_build_addr_expr doesn't fail. */
8996 : 35167427 : if (convs->need_temporary_p
8997 : 33254880 : || TREE_CODE (expr) == CONSTRUCTOR
8998 : 33254879 : || TREE_CODE (expr) == VA_ARG_EXPR)
8999 : : {
9000 : : /* Otherwise, a temporary of type "cv1 T1" is created and
9001 : : initialized from the initializer expression using the rules
9002 : : for a non-reference copy-initialization (8.5). */
9003 : :
9004 : 1912548 : tree type = TREE_TYPE (ref_type);
9005 : 1912548 : cp_lvalue_kind lvalue = lvalue_kind (expr);
9006 : :
9007 : 1912548 : gcc_assert (similar_type_p (type, next_conversion (convs)->type));
9008 : 1912548 : if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
9009 : 2720600 : && !TYPE_REF_IS_RVALUE (ref_type))
9010 : : {
9011 : : /* If the reference is volatile or non-const, we
9012 : : cannot create a temporary. */
9013 : 168 : if (complain & tf_error)
9014 : : {
9015 : 167 : if (lvalue & clk_bitfield)
9016 : 30 : error_at (loc, "cannot bind bit-field %qE to %qT",
9017 : : expr, ref_type);
9018 : 137 : else if (lvalue & clk_packed)
9019 : 9 : error_at (loc, "cannot bind packed field %qE to %qT",
9020 : : expr, ref_type);
9021 : : else
9022 : 128 : error_at (loc, "cannot bind rvalue %qE to %qT",
9023 : : expr, ref_type);
9024 : : }
9025 : 168 : return error_mark_node;
9026 : : }
9027 : : /* If the source is a packed field, and we must use a copy
9028 : : constructor, then building the target expr will require
9029 : : binding the field to the reference parameter to the
9030 : : copy constructor, and we'll end up with an infinite
9031 : : loop. If we can use a bitwise copy, then we'll be
9032 : : OK. */
9033 : 1912380 : if ((lvalue & clk_packed)
9034 : 20 : && CLASS_TYPE_P (type)
9035 : 1912397 : && type_has_nontrivial_copy_init (type))
9036 : : {
9037 : 6 : error_at (loc, "cannot bind packed field %qE to %qT",
9038 : : expr, ref_type);
9039 : 6 : return error_mark_node;
9040 : : }
9041 : 1912374 : if (lvalue & clk_bitfield)
9042 : : {
9043 : 24 : expr = convert_bitfield_to_declared_type (expr);
9044 : 24 : expr = fold_convert (type, expr);
9045 : : }
9046 : :
9047 : : /* Creating &TARGET_EXPR<> in a template would break when
9048 : : tsubsting the expression, so use an IMPLICIT_CONV_EXPR
9049 : : instead. This can happen even when there's no class
9050 : : involved, e.g., when converting an integer to a reference
9051 : : type. */
9052 : 1912374 : if (processing_template_decl)
9053 : 2128 : return build1 (IMPLICIT_CONV_EXPR, totype, expr);
9054 : 1910246 : expr = build_target_expr_with_type (expr, type, complain);
9055 : : }
9056 : :
9057 : : /* Take the address of the thing to which we will bind the
9058 : : reference. */
9059 : 35165125 : expr = cp_build_addr_expr (expr, complain);
9060 : 35165125 : if (expr == error_mark_node)
9061 : : return error_mark_node;
9062 : :
9063 : : /* Convert it to a pointer to the type referred to by the
9064 : : reference. This will adjust the pointer if a derived to
9065 : : base conversion is being performed. */
9066 : 35165125 : expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
9067 : : expr, complain);
9068 : : /* Convert the pointer to the desired reference type. */
9069 : 35165125 : return build_nop (ref_type, expr);
9070 : : }
9071 : :
9072 : 2201870 : case ck_lvalue:
9073 : 2201870 : return decay_conversion (expr, complain);
9074 : :
9075 : 210208 : case ck_fnptr:
9076 : : /* ??? Should the address of a transaction-safe pointer point to the TM
9077 : : clone, and this conversion look up the primary function? */
9078 : 210208 : return build_nop (totype, expr);
9079 : :
9080 : 1021106 : case ck_qual:
9081 : : /* Warn about deprecated conversion if appropriate. */
9082 : 1021106 : if (complain & tf_warning)
9083 : : {
9084 : 968432 : string_conv_p (totype, expr, 1);
9085 : 968432 : maybe_warn_array_conv (loc, convs, expr);
9086 : : }
9087 : : break;
9088 : :
9089 : 2049052 : case ck_ptr:
9090 : 2049052 : if (convs->base_p)
9091 : 131089 : expr = convert_to_base (expr, totype, !c_cast_p,
9092 : : /*nonnull=*/false, complain);
9093 : 2049052 : return build_nop (totype, expr);
9094 : :
9095 : 28605 : case ck_pmem:
9096 : 28605 : return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
9097 : 28605 : c_cast_p, complain);
9098 : :
9099 : : default:
9100 : : break;
9101 : : }
9102 : :
9103 : 58271767 : if (convs->check_narrowing
9104 : 72855161 : && !check_narrowing (totype, expr, complain,
9105 : 14583394 : convs->check_narrowing_const_only))
9106 : 456 : return error_mark_node;
9107 : :
9108 : 116542622 : warning_sentinel w (warn_zero_as_null_pointer_constant);
9109 : 58271311 : if (issue_conversion_warnings)
9110 : 37523094 : expr = cp_convert_and_check (totype, expr, complain);
9111 : : else
9112 : : {
9113 : 20748217 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
9114 : 36 : expr = TREE_OPERAND (expr, 0);
9115 : 20748217 : expr = cp_convert (totype, expr, complain);
9116 : : }
9117 : :
9118 : 58271311 : return expr;
9119 : : }
9120 : :
9121 : : /* Return true if converting FROM to TO is unsafe in a template. */
9122 : :
9123 : : static bool
9124 : 1403226 : conv_unsafe_in_template_p (tree to, tree from)
9125 : : {
9126 : : /* Converting classes involves TARGET_EXPR. */
9127 : 1403226 : if (CLASS_TYPE_P (to) || CLASS_TYPE_P (from))
9128 : : return true;
9129 : :
9130 : : /* Converting real to integer produces FIX_TRUNC_EXPR which tsubst
9131 : : doesn't handle. */
9132 : 1060328 : if (SCALAR_FLOAT_TYPE_P (from) && INTEGRAL_OR_ENUMERATION_TYPE_P (to))
9133 : : return true;
9134 : :
9135 : : /* Converting integer to real isn't a trivial conversion, either. */
9136 : 1060325 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (from) && SCALAR_FLOAT_TYPE_P (to))
9137 : 3 : return true;
9138 : :
9139 : : return false;
9140 : : }
9141 : :
9142 : : /* Wrapper for convert_like_internal that handles creating
9143 : : IMPLICIT_CONV_EXPR. */
9144 : :
9145 : : static tree
9146 : 731101426 : convert_like (conversion *convs, tree expr, tree fn, int argnum,
9147 : : bool issue_conversion_warnings, bool c_cast_p, bool nested_p,
9148 : : tsubst_flags_t complain)
9149 : : {
9150 : : /* Creating &TARGET_EXPR<> in a template breaks when substituting,
9151 : : and creating a CALL_EXPR in a template breaks in finish_call_expr
9152 : : so use an IMPLICIT_CONV_EXPR for this conversion. We would have
9153 : : created such codes e.g. when calling a user-defined conversion
9154 : : function. */
9155 : 731101426 : tree conv_expr = NULL_TREE;
9156 : 731101426 : if (processing_template_decl
9157 : 85957793 : && convs->kind != ck_identity
9158 : 732504652 : && conv_unsafe_in_template_p (convs->type, TREE_TYPE (expr)))
9159 : : {
9160 : 342904 : conv_expr = build1 (IMPLICIT_CONV_EXPR, convs->type, expr);
9161 : 342904 : if (convs->kind != ck_ref_bind)
9162 : 39820 : conv_expr = convert_from_reference (conv_expr);
9163 : 342904 : if (!convs->bad_p)
9164 : : return conv_expr;
9165 : : /* Do the normal processing to give the bad_p errors. But we still
9166 : : need to return the IMPLICIT_CONV_EXPR, unless we're returning
9167 : : error_mark_node. */
9168 : : }
9169 : 730758531 : expr = convert_like_internal (convs, expr, fn, argnum,
9170 : : issue_conversion_warnings, c_cast_p,
9171 : : nested_p, complain);
9172 : 730758531 : if (expr == error_mark_node)
9173 : : return error_mark_node;
9174 : 730729525 : return conv_expr ? conv_expr : expr;
9175 : : }
9176 : :
9177 : : /* Convenience wrapper for convert_like. */
9178 : :
9179 : : static inline tree
9180 : 413912013 : convert_like (conversion *convs, tree expr, tsubst_flags_t complain)
9181 : : {
9182 : 413912013 : return convert_like (convs, expr, NULL_TREE, 0,
9183 : : /*issue_conversion_warnings=*/true,
9184 : 413912013 : /*c_cast_p=*/false, /*nested_p=*/false, complain);
9185 : : }
9186 : :
9187 : : /* Convenience wrapper for convert_like. */
9188 : :
9189 : : static inline tree
9190 : 89273716 : convert_like_with_context (conversion *convs, tree expr, tree fn, int argnum,
9191 : : tsubst_flags_t complain)
9192 : : {
9193 : 0 : return convert_like (convs, expr, fn, argnum,
9194 : : /*issue_conversion_warnings=*/true,
9195 : : /*c_cast_p=*/false, /*nested_p=*/false, complain);
9196 : : }
9197 : :
9198 : : /* ARG is being passed to a varargs function. Perform any conversions
9199 : : required. Return the converted value. */
9200 : :
9201 : : tree
9202 : 1292674 : convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
9203 : : {
9204 : 1292674 : tree arg_type = TREE_TYPE (arg);
9205 : 1292674 : location_t loc = cp_expr_loc_or_input_loc (arg);
9206 : :
9207 : : /* [expr.call]
9208 : :
9209 : : If the argument has integral or enumeration type that is subject
9210 : : to the integral promotions (_conv.prom_), or a floating-point
9211 : : type that is subject to the floating-point promotion
9212 : : (_conv.fpprom_), the value of the argument is converted to the
9213 : : promoted type before the call. */
9214 : 1292674 : if (SCALAR_FLOAT_TYPE_P (arg_type)
9215 : 45592 : && (TYPE_PRECISION (arg_type)
9216 : 45592 : < TYPE_PRECISION (double_type_node))
9217 : 11095 : && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type))
9218 : 1303091 : && !extended_float_type_p (arg_type))
9219 : : {
9220 : 10413 : if ((complain & tf_warning)
9221 : 10410 : && warn_double_promotion && !c_inhibit_evaluation_warnings)
9222 : 3 : warning_at (loc, OPT_Wdouble_promotion,
9223 : : "implicit conversion from %qH to %qI when passing "
9224 : : "argument to function",
9225 : : arg_type, double_type_node);
9226 : 10413 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
9227 : 3 : arg = TREE_OPERAND (arg, 0);
9228 : 10413 : arg = mark_rvalue_use (arg);
9229 : 10413 : arg = convert_to_real_nofold (double_type_node, arg);
9230 : : }
9231 : 1282261 : else if (NULLPTR_TYPE_P (arg_type))
9232 : : {
9233 : 2191 : arg = mark_rvalue_use (arg);
9234 : 2191 : if (TREE_SIDE_EFFECTS (arg))
9235 : : {
9236 : 6 : warning_sentinel w(warn_unused_result);
9237 : 6 : arg = cp_build_compound_expr (arg, null_pointer_node, complain);
9238 : 6 : }
9239 : : else
9240 : 2185 : arg = null_pointer_node;
9241 : : }
9242 : 1280070 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
9243 : : {
9244 : 919499 : if (SCOPED_ENUM_P (arg_type))
9245 : : {
9246 : 63 : tree prom = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg,
9247 : : complain);
9248 : 63 : prom = cp_perform_integral_promotions (prom, complain);
9249 : 123 : if (abi_version_crosses (6)
9250 : 30 : && TYPE_MODE (TREE_TYPE (prom)) != TYPE_MODE (arg_type)
9251 : 93 : && (complain & tf_warning))
9252 : 60 : warning_at (loc, OPT_Wabi, "scoped enum %qT passed through %<...%>"
9253 : : " as %qT before %<-fabi-version=6%>, %qT after",
9254 : : arg_type,
9255 : 30 : TREE_TYPE (prom), ENUM_UNDERLYING_TYPE (arg_type));
9256 : 63 : if (!abi_version_at_least (6))
9257 : 1292674 : arg = prom;
9258 : : }
9259 : : else
9260 : 919436 : arg = cp_perform_integral_promotions (arg, complain);
9261 : : }
9262 : : else
9263 : : /* [expr.call]
9264 : :
9265 : : The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
9266 : : standard conversions are performed. */
9267 : 360571 : arg = decay_conversion (arg, complain);
9268 : :
9269 : 1292674 : arg = require_complete_type (arg, complain);
9270 : 1292674 : arg_type = TREE_TYPE (arg);
9271 : :
9272 : 1292674 : if (arg != error_mark_node
9273 : : /* In a template (or ill-formed code), we can have an incomplete type
9274 : : even after require_complete_type, in which case we don't know
9275 : : whether it has trivial copy or not. */
9276 : 1292662 : && COMPLETE_TYPE_P (arg_type)
9277 : 2585336 : && !cp_unevaluated_operand)
9278 : : {
9279 : : /* [expr.call] 5.2.2/7:
9280 : : Passing a potentially-evaluated argument of class type (Clause 9)
9281 : : with a non-trivial copy constructor or a non-trivial destructor
9282 : : with no corresponding parameter is conditionally-supported, with
9283 : : implementation-defined semantics.
9284 : :
9285 : : We support it as pass-by-invisible-reference, just like a normal
9286 : : value parameter.
9287 : :
9288 : : If the call appears in the context of a sizeof expression,
9289 : : it is not potentially-evaluated. */
9290 : 526910 : if (type_has_nontrivial_copy_init (arg_type)
9291 : 526910 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type))
9292 : : {
9293 : 33 : arg = force_rvalue (arg, complain);
9294 : 33 : if (complain & tf_warning)
9295 : 33 : warning (OPT_Wconditionally_supported,
9296 : : "passing objects of non-trivially-copyable "
9297 : : "type %q#T through %<...%> is conditionally supported",
9298 : : arg_type);
9299 : 33 : return build1 (ADDR_EXPR, build_reference_type (arg_type), arg);
9300 : : }
9301 : : /* Build up a real lvalue-to-rvalue conversion in case the
9302 : : copy constructor is trivial but not callable. */
9303 : 526877 : else if (CLASS_TYPE_P (arg_type))
9304 : 44007 : force_rvalue (arg, complain);
9305 : :
9306 : : }
9307 : :
9308 : : return arg;
9309 : : }
9310 : :
9311 : : /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
9312 : :
9313 : : tree
9314 : 30978 : build_x_va_arg (location_t loc, tree expr, tree type)
9315 : : {
9316 : 30978 : if (processing_template_decl)
9317 : : {
9318 : 39 : tree r = build_min (VA_ARG_EXPR, type, expr);
9319 : 39 : SET_EXPR_LOCATION (r, loc);
9320 : 39 : return r;
9321 : : }
9322 : :
9323 : 30939 : type = complete_type_or_else (type, NULL_TREE);
9324 : :
9325 : 30939 : if (expr == error_mark_node || !type)
9326 : : return error_mark_node;
9327 : :
9328 : 30918 : expr = mark_lvalue_use (expr);
9329 : :
9330 : 30918 : if (TYPE_REF_P (type))
9331 : : {
9332 : 6 : error ("cannot receive reference type %qT through %<...%>", type);
9333 : 6 : return error_mark_node;
9334 : : }
9335 : :
9336 : 30912 : if (type_has_nontrivial_copy_init (type)
9337 : 30912 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
9338 : : {
9339 : : /* conditionally-supported behavior [expr.call] 5.2.2/7. Let's treat
9340 : : it as pass by invisible reference. */
9341 : 12 : warning_at (loc, OPT_Wconditionally_supported,
9342 : : "receiving objects of non-trivially-copyable type %q#T "
9343 : : "through %<...%> is conditionally-supported", type);
9344 : :
9345 : 12 : tree ref = cp_build_reference_type (type, false);
9346 : 12 : expr = build_va_arg (loc, expr, ref);
9347 : 12 : return convert_from_reference (expr);
9348 : : }
9349 : :
9350 : 30900 : tree ret = build_va_arg (loc, expr, type);
9351 : 30900 : if (CLASS_TYPE_P (type))
9352 : : /* Wrap the VA_ARG_EXPR in a TARGET_EXPR now so other code doesn't need to
9353 : : know how to handle it. */
9354 : 12094 : ret = get_target_expr (ret);
9355 : : return ret;
9356 : : }
9357 : :
9358 : : /* TYPE has been given to va_arg. Apply the default conversions which
9359 : : would have happened when passed via ellipsis. Return the promoted
9360 : : type, or the passed type if there is no change. */
9361 : :
9362 : : tree
9363 : 2268298 : cxx_type_promotes_to (tree type)
9364 : : {
9365 : 2268298 : tree promote;
9366 : :
9367 : : /* Perform the array-to-pointer and function-to-pointer
9368 : : conversions. */
9369 : 2268298 : type = type_decays_to (type);
9370 : :
9371 : 2268298 : promote = type_promotes_to (type);
9372 : 2268298 : if (same_type_p (type, promote))
9373 : 2268274 : promote = type;
9374 : :
9375 : 2268298 : return promote;
9376 : : }
9377 : :
9378 : : /* ARG is a default argument expression being passed to a parameter of
9379 : : the indicated TYPE, which is a parameter to FN. PARMNUM is the
9380 : : zero-based argument number. Do any required conversions. Return
9381 : : the converted value. */
9382 : :
9383 : : static GTY(()) vec<tree, va_gc> *default_arg_context;
9384 : : void
9385 : 5785320 : push_defarg_context (tree fn)
9386 : 5785320 : { vec_safe_push (default_arg_context, fn); }
9387 : :
9388 : : void
9389 : 5785320 : pop_defarg_context (void)
9390 : 5785320 : { default_arg_context->pop (); }
9391 : :
9392 : : tree
9393 : 992509 : convert_default_arg (tree type, tree arg, tree fn, int parmnum,
9394 : : tsubst_flags_t complain)
9395 : : {
9396 : 992509 : int i;
9397 : 992509 : tree t;
9398 : :
9399 : : /* See through clones. */
9400 : 992509 : fn = DECL_ORIGIN (fn);
9401 : : /* And inheriting ctors. */
9402 : 992509 : if (flag_new_inheriting_ctors)
9403 : 991869 : fn = strip_inheriting_ctors (fn);
9404 : :
9405 : : /* Detect recursion. */
9406 : 993360 : FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t)
9407 : 857 : if (t == fn)
9408 : : {
9409 : 6 : if (complain & tf_error)
9410 : 6 : error ("recursive evaluation of default argument for %q#D", fn);
9411 : 6 : return error_mark_node;
9412 : : }
9413 : :
9414 : : /* If the ARG is an unparsed default argument expression, the
9415 : : conversion cannot be performed. */
9416 : 992503 : if (TREE_CODE (arg) == DEFERRED_PARSE)
9417 : : {
9418 : 15 : if (complain & tf_error)
9419 : 15 : error ("call to %qD uses the default argument for parameter %P, which "
9420 : : "is not yet defined", fn, parmnum);
9421 : 15 : return error_mark_node;
9422 : : }
9423 : :
9424 : 992488 : push_defarg_context (fn);
9425 : :
9426 : 992488 : if (fn && DECL_TEMPLATE_INFO (fn))
9427 : 752014 : arg = tsubst_default_argument (fn, parmnum, type, arg, complain);
9428 : :
9429 : : /* Due to:
9430 : :
9431 : : [dcl.fct.default]
9432 : :
9433 : : The names in the expression are bound, and the semantic
9434 : : constraints are checked, at the point where the default
9435 : : expressions appears.
9436 : :
9437 : : we must not perform access checks here. */
9438 : 992488 : push_deferring_access_checks (dk_no_check);
9439 : : /* We must make a copy of ARG, in case subsequent processing
9440 : : alters any part of it. */
9441 : 992488 : arg = break_out_target_exprs (arg, /*clear location*/true);
9442 : :
9443 : 992488 : arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
9444 : : ICR_DEFAULT_ARGUMENT, fn, parmnum,
9445 : : complain);
9446 : 992488 : arg = convert_for_arg_passing (type, arg, complain);
9447 : 992488 : pop_deferring_access_checks();
9448 : :
9449 : 992488 : pop_defarg_context ();
9450 : :
9451 : 992488 : return arg;
9452 : : }
9453 : :
9454 : : /* Returns the type which will really be used for passing an argument of
9455 : : type TYPE. */
9456 : :
9457 : : tree
9458 : 515477292 : type_passed_as (tree type)
9459 : : {
9460 : : /* Pass classes with copy ctors by invisible reference. */
9461 : 515477292 : if (TREE_ADDRESSABLE (type))
9462 : 530703 : type = build_reference_type (type);
9463 : 514946589 : else if (targetm.calls.promote_prototypes (NULL_TREE)
9464 : 514946589 : && INTEGRAL_TYPE_P (type)
9465 : 94735723 : && COMPLETE_TYPE_P (type)
9466 : 609682312 : && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
9467 : 11109613 : type = integer_type_node;
9468 : :
9469 : 515477292 : return type;
9470 : : }
9471 : :
9472 : : /* Actually perform the appropriate conversion. */
9473 : :
9474 : : tree
9475 : 93898835 : convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
9476 : : {
9477 : 93898835 : tree bitfield_type;
9478 : :
9479 : : /* If VAL is a bitfield, then -- since it has already been converted
9480 : : to TYPE -- it cannot have a precision greater than TYPE.
9481 : :
9482 : : If it has a smaller precision, we must widen it here. For
9483 : : example, passing "int f:3;" to a function expecting an "int" will
9484 : : not result in any conversion before this point.
9485 : :
9486 : : If the precision is the same we must not risk widening. For
9487 : : example, the COMPONENT_REF for a 32-bit "long long" bitfield will
9488 : : often have type "int", even though the C++ type for the field is
9489 : : "long long". If the value is being passed to a function
9490 : : expecting an "int", then no conversions will be required. But,
9491 : : if we call convert_bitfield_to_declared_type, the bitfield will
9492 : : be converted to "long long". */
9493 : 93898835 : bitfield_type = is_bitfield_expr_with_lowered_type (val);
9494 : 93898835 : if (bitfield_type
9495 : 93898835 : && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
9496 : 0 : val = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type), val);
9497 : :
9498 : 93898835 : if (val == error_mark_node)
9499 : : ;
9500 : : /* Pass classes with copy ctors by invisible reference. */
9501 : 93896586 : else if (TREE_ADDRESSABLE (type))
9502 : 190580 : val = build1 (ADDR_EXPR, build_reference_type (type), val);
9503 : 93706006 : else if (targetm.calls.promote_prototypes (NULL_TREE)
9504 : 93706006 : && INTEGRAL_TYPE_P (type)
9505 : 25674015 : && COMPLETE_TYPE_P (type)
9506 : 119380021 : && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
9507 : 2719764 : val = cp_perform_integral_promotions (val, complain);
9508 : 93898835 : if (complain & tf_warning)
9509 : : {
9510 : 78742282 : if (warn_suggest_attribute_format)
9511 : : {
9512 : 1960 : tree rhstype = TREE_TYPE (val);
9513 : 1960 : const enum tree_code coder = TREE_CODE (rhstype);
9514 : 1960 : const enum tree_code codel = TREE_CODE (type);
9515 : 1960 : if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
9516 : 753 : && coder == codel
9517 : 2713 : && check_missing_format_attribute (type, rhstype))
9518 : 6 : warning (OPT_Wsuggest_attribute_format,
9519 : : "argument of function call might be a candidate "
9520 : : "for a format attribute");
9521 : : }
9522 : 96441009 : maybe_warn_parm_abi (type, cp_expr_loc_or_input_loc (val));
9523 : : }
9524 : :
9525 : 78742282 : if (complain & tf_warning)
9526 : 78742282 : warn_for_address_of_packed_member (type, val);
9527 : :
9528 : : /* gimplify_arg elides TARGET_EXPRs that initialize a function argument,
9529 : : unless the initializer is a CONSTRUCTOR. In that case, we fail to
9530 : : elide the copy anyway. See that function for more information. */
9531 : 5013324 : if (SIMPLE_TARGET_EXPR_P (val)
9532 : 98353666 : && TREE_CODE (TARGET_EXPR_INITIAL (val)) != CONSTRUCTOR)
9533 : 2289969 : set_target_expr_eliding (val);
9534 : :
9535 : 93898835 : return val;
9536 : : }
9537 : :
9538 : : /* Returns non-zero iff FN is a function with magic varargs, i.e. ones for
9539 : : which just decay_conversion or no conversions at all should be done.
9540 : : This is true for some builtins which don't act like normal functions.
9541 : : Return 2 if just decay_conversion and removal of excess precision should
9542 : : be done, 1 if just decay_conversion. Return 3 for special treatment of
9543 : : the 3rd argument for __builtin_*_overflow_p. Return 4 for special
9544 : : treatment of the 1st argument for
9545 : : __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g. */
9546 : :
9547 : : int
9548 : 108172431 : magic_varargs_p (tree fn)
9549 : : {
9550 : 108172431 : if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
9551 : 5219290 : switch (DECL_FUNCTION_CODE (fn))
9552 : : {
9553 : : case BUILT_IN_CLASSIFY_TYPE:
9554 : : case BUILT_IN_CONSTANT_P:
9555 : : case BUILT_IN_NEXT_ARG:
9556 : : case BUILT_IN_VA_START:
9557 : : return 1;
9558 : :
9559 : 20842 : case BUILT_IN_ADD_OVERFLOW_P:
9560 : 20842 : case BUILT_IN_SUB_OVERFLOW_P:
9561 : 20842 : case BUILT_IN_MUL_OVERFLOW_P:
9562 : 20842 : return 3;
9563 : :
9564 : 229061 : case BUILT_IN_ISFINITE:
9565 : 229061 : case BUILT_IN_ISINF:
9566 : 229061 : case BUILT_IN_ISINF_SIGN:
9567 : 229061 : case BUILT_IN_ISNAN:
9568 : 229061 : case BUILT_IN_ISNORMAL:
9569 : 229061 : case BUILT_IN_FPCLASSIFY:
9570 : 229061 : return 2;
9571 : :
9572 : 624 : case BUILT_IN_CLZG:
9573 : 624 : case BUILT_IN_CTZG:
9574 : 624 : case BUILT_IN_CLRSBG:
9575 : 624 : case BUILT_IN_FFSG:
9576 : 624 : case BUILT_IN_PARITYG:
9577 : 624 : case BUILT_IN_POPCOUNTG:
9578 : 624 : return 4;
9579 : :
9580 : 4877981 : default:
9581 : 4877981 : return lookup_attribute ("type generic",
9582 : 9755962 : TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
9583 : : }
9584 : :
9585 : : return 0;
9586 : : }
9587 : :
9588 : : /* Returns the decl of the dispatcher function if FN is a function version. */
9589 : :
9590 : : tree
9591 : 222 : get_function_version_dispatcher (tree fn)
9592 : : {
9593 : 222 : tree dispatcher_decl = NULL;
9594 : :
9595 : 222 : if (DECL_LOCAL_DECL_P (fn))
9596 : 9 : fn = DECL_LOCAL_DECL_ALIAS (fn);
9597 : :
9598 : 222 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
9599 : : && DECL_FUNCTION_VERSIONED (fn));
9600 : :
9601 : 222 : gcc_assert (targetm.get_function_versions_dispatcher);
9602 : 222 : dispatcher_decl = targetm.get_function_versions_dispatcher (fn);
9603 : :
9604 : 222 : if (dispatcher_decl == NULL)
9605 : : {
9606 : 9 : error_at (input_location, "use of multiversioned function "
9607 : : "without a default");
9608 : 9 : return NULL;
9609 : : }
9610 : :
9611 : 213 : retrofit_lang_decl (dispatcher_decl);
9612 : 213 : gcc_assert (dispatcher_decl != NULL);
9613 : : return dispatcher_decl;
9614 : : }
9615 : :
9616 : : /* fn is a function version dispatcher that is marked used. Mark all the
9617 : : semantically identical function versions it will dispatch as used. */
9618 : :
9619 : : void
9620 : 141 : mark_versions_used (tree fn)
9621 : : {
9622 : 141 : struct cgraph_node *node;
9623 : 141 : struct cgraph_function_version_info *node_v;
9624 : 141 : struct cgraph_function_version_info *it_v;
9625 : :
9626 : 141 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9627 : :
9628 : 141 : node = cgraph_node::get (fn);
9629 : 141 : if (node == NULL)
9630 : : return;
9631 : :
9632 : 141 : gcc_assert (node->dispatcher_function);
9633 : :
9634 : 141 : node_v = node->function_version ();
9635 : 141 : if (node_v == NULL)
9636 : : return;
9637 : :
9638 : : /* All semantically identical versions are chained. Traverse and mark each
9639 : : one of them as used. */
9640 : 141 : it_v = node_v->next;
9641 : 1059 : while (it_v != NULL)
9642 : : {
9643 : 918 : mark_used (it_v->this_node->decl);
9644 : 918 : it_v = it_v->next;
9645 : : }
9646 : : }
9647 : :
9648 : : /* Build a call to "the copy constructor" for the type of A, even if it
9649 : : wouldn't be selected by normal overload resolution. Used for
9650 : : diagnostics. */
9651 : :
9652 : : static tree
9653 : 3 : call_copy_ctor (tree a, tsubst_flags_t complain)
9654 : : {
9655 : 3 : tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (a));
9656 : 3 : tree binfo = TYPE_BINFO (ctype);
9657 : 3 : tree copy = get_copy_ctor (ctype, complain);
9658 : 3 : copy = build_baselink (binfo, binfo, copy, NULL_TREE);
9659 : 3 : tree ob = build_dummy_object (ctype);
9660 : 3 : releasing_vec args (make_tree_vector_single (a));
9661 : 3 : tree r = build_new_method_call (ob, copy, &args, NULL_TREE,
9662 : : LOOKUP_NORMAL, NULL, complain);
9663 : 3 : return r;
9664 : 3 : }
9665 : :
9666 : : /* Return the base constructor corresponding to COMPLETE_CTOR or NULL_TREE. */
9667 : :
9668 : : static tree
9669 : 42 : base_ctor_for (tree complete_ctor)
9670 : : {
9671 : 42 : tree clone;
9672 : 42 : FOR_EACH_CLONE (clone, DECL_CLONED_FUNCTION (complete_ctor))
9673 : 42 : if (DECL_BASE_CONSTRUCTOR_P (clone))
9674 : : return clone;
9675 : : return NULL_TREE;
9676 : : }
9677 : :
9678 : : /* Try to make EXP suitable to be used as the initializer for a base subobject,
9679 : : and return whether we were successful. EXP must have already been cleared
9680 : : by unsafe_copy_elision_p{,_opt}. */
9681 : :
9682 : : static bool
9683 : 190 : make_base_init_ok (tree exp)
9684 : : {
9685 : 190 : if (TREE_CODE (exp) == TARGET_EXPR)
9686 : 190 : exp = TARGET_EXPR_INITIAL (exp);
9687 : 193 : while (TREE_CODE (exp) == COMPOUND_EXPR)
9688 : 3 : exp = TREE_OPERAND (exp, 1);
9689 : 190 : if (TREE_CODE (exp) == COND_EXPR)
9690 : : {
9691 : 3 : bool ret = make_base_init_ok (TREE_OPERAND (exp, 2));
9692 : 3 : if (tree op1 = TREE_OPERAND (exp, 1))
9693 : : {
9694 : 3 : bool r1 = make_base_init_ok (op1);
9695 : : /* If unsafe_copy_elision_p was false, the arms should match. */
9696 : 3 : gcc_assert (r1 == ret);
9697 : : }
9698 : 3 : return ret;
9699 : : }
9700 : 187 : if (TREE_CODE (exp) != AGGR_INIT_EXPR)
9701 : : /* A trivial copy is OK. */
9702 : : return true;
9703 : 56 : if (!AGGR_INIT_VIA_CTOR_P (exp))
9704 : : /* unsafe_copy_elision_p_opt must have said this is OK. */
9705 : : return true;
9706 : 42 : tree fn = cp_get_callee_fndecl_nofold (exp);
9707 : 42 : if (DECL_BASE_CONSTRUCTOR_P (fn))
9708 : : return true;
9709 : 42 : gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (fn));
9710 : 42 : fn = base_ctor_for (fn);
9711 : 42 : if (!fn || DECL_HAS_VTT_PARM_P (fn))
9712 : : /* The base constructor has more parameters, so we can't just change the
9713 : : call target. It would be possible to splice in the appropriate
9714 : : arguments, but probably not worth the complexity. */
9715 : : return false;
9716 : 33 : mark_used (fn);
9717 : 33 : AGGR_INIT_EXPR_FN (exp) = build_address (fn);
9718 : 33 : return true;
9719 : : }
9720 : :
9721 : : /* Return 2 if T refers to a base, 1 if a potentially-overlapping field,
9722 : : neither of which can be used for return by invisible reference. We avoid
9723 : : doing C++17 mandatory copy elision for either of these cases.
9724 : :
9725 : : This returns non-zero even if the type of T has no tail padding that other
9726 : : data could be allocated into, because that depends on the particular ABI.
9727 : : unsafe_copy_elision_p_opt does consider whether there is padding. */
9728 : :
9729 : : int
9730 : 25495885 : unsafe_return_slot_p (tree t)
9731 : : {
9732 : : /* Check empty bases separately, they don't have fields. */
9733 : 25495885 : if (is_empty_base_ref (t))
9734 : : return 2;
9735 : :
9736 : : /* A delegating constructor might be used to initialize a base. */
9737 : 25055783 : if (current_function_decl
9738 : 33687708 : && DECL_CONSTRUCTOR_P (current_function_decl)
9739 : 27618621 : && (t == current_class_ref
9740 : 2480297 : || tree_strip_nop_conversions (t) == current_class_ptr))
9741 : 93400 : return 2;
9742 : :
9743 : 24962383 : STRIP_NOPS (t);
9744 : 24962383 : if (TREE_CODE (t) == ADDR_EXPR)
9745 : 39835 : t = TREE_OPERAND (t, 0);
9746 : 24962383 : if (TREE_CODE (t) == COMPONENT_REF)
9747 : 2330774 : t = TREE_OPERAND (t, 1);
9748 : 24962383 : if (TREE_CODE (t) != FIELD_DECL)
9749 : : return false;
9750 : 2364942 : if (!CLASS_TYPE_P (TREE_TYPE (t)))
9751 : : /* The middle-end will do the right thing for scalar types. */
9752 : : return false;
9753 : 1925762 : if (DECL_FIELD_IS_BASE (t))
9754 : : return 2;
9755 : 1275496 : if (lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (t)))
9756 : : return 1;
9757 : : return 0;
9758 : : }
9759 : :
9760 : : /* True IFF EXP is a prvalue that represents return by invisible reference. */
9761 : :
9762 : : static bool
9763 : 235466 : init_by_return_slot_p (tree exp)
9764 : : {
9765 : : /* Copy elision only happens with a TARGET_EXPR. */
9766 : 235469 : if (TREE_CODE (exp) != TARGET_EXPR)
9767 : : return false;
9768 : 19922 : tree init = TARGET_EXPR_INITIAL (exp);
9769 : : /* build_compound_expr pushes COMPOUND_EXPR inside TARGET_EXPR. */
9770 : 19928 : while (TREE_CODE (init) == COMPOUND_EXPR)
9771 : 6 : init = TREE_OPERAND (init, 1);
9772 : 19922 : if (TREE_CODE (init) == COND_EXPR)
9773 : : {
9774 : : /* We'll end up copying from each of the arms of the COND_EXPR directly
9775 : : into the target, so look at them. */
9776 : 6 : if (tree op = TREE_OPERAND (init, 1))
9777 : 6 : if (init_by_return_slot_p (op))
9778 : : return true;
9779 : 3 : return init_by_return_slot_p (TREE_OPERAND (init, 2));
9780 : : }
9781 : 19916 : return (TREE_CODE (init) == AGGR_INIT_EXPR
9782 : 19916 : && !AGGR_INIT_VIA_CTOR_P (init));
9783 : : }
9784 : :
9785 : : /* We can't elide a copy from a function returning by value to a
9786 : : potentially-overlapping subobject, as the callee might clobber tail padding.
9787 : : Return true iff this could be that case.
9788 : :
9789 : : Places that use this function (or _opt) to decide to elide a copy should
9790 : : probably use make_safe_copy_elision instead. */
9791 : :
9792 : : bool
9793 : 1231578 : unsafe_copy_elision_p (tree target, tree exp)
9794 : : {
9795 : 1231578 : return unsafe_return_slot_p (target) && init_by_return_slot_p (exp);
9796 : : }
9797 : :
9798 : : /* As above, but for optimization allow more cases that are actually safe. */
9799 : :
9800 : : static bool
9801 : 4546304 : unsafe_copy_elision_p_opt (tree target, tree exp)
9802 : : {
9803 : 4546304 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
9804 : : /* It's safe to elide the copy for a class with no tail padding. */
9805 : 4546304 : if (!is_empty_class (type)
9806 : 4546304 : && tree_int_cst_equal (TYPE_SIZE (type), CLASSTYPE_SIZE (type)))
9807 : : return false;
9808 : 1197410 : return unsafe_copy_elision_p (target, exp);
9809 : : }
9810 : :
9811 : : /* Try to make EXP suitable to be used as the initializer for TARGET,
9812 : : and return whether we were successful. */
9813 : :
9814 : : bool
9815 : 709 : make_safe_copy_elision (tree target, tree exp)
9816 : : {
9817 : 709 : int uns = unsafe_return_slot_p (target);
9818 : 709 : if (!uns)
9819 : : return true;
9820 : 709 : if (init_by_return_slot_p (exp))
9821 : : return false;
9822 : 709 : if (uns == 1)
9823 : : return true;
9824 : 19 : return make_base_init_ok (exp);
9825 : : }
9826 : :
9827 : : /* True IFF the result of the conversion C is a prvalue. */
9828 : :
9829 : : static bool
9830 : 8647030 : conv_is_prvalue (conversion *c)
9831 : : {
9832 : 8647030 : if (c->kind == ck_rvalue)
9833 : : return true;
9834 : 8647030 : if (c->kind == ck_base && c->need_temporary_p)
9835 : : return true;
9836 : 8647030 : if (c->kind == ck_user && !TYPE_REF_P (c->type))
9837 : : return true;
9838 : 8577827 : if (c->kind == ck_identity && c->u.expr
9839 : 8361651 : && TREE_CODE (c->u.expr) == TARGET_EXPR)
9840 : 43 : return true;
9841 : :
9842 : : return false;
9843 : : }
9844 : :
9845 : : /* True iff C is a conversion that binds a reference to a prvalue. */
9846 : :
9847 : : static bool
9848 : 8647523 : conv_binds_ref_to_prvalue (conversion *c)
9849 : : {
9850 : 8647523 : if (c->kind != ck_ref_bind)
9851 : : return false;
9852 : 8647523 : if (c->need_temporary_p)
9853 : : return true;
9854 : :
9855 : 8647030 : return conv_is_prvalue (next_conversion (c));
9856 : : }
9857 : :
9858 : : /* True iff EXPR represents a (subobject of a) temporary. */
9859 : :
9860 : : static bool
9861 : 3176 : expr_represents_temporary_p (tree expr)
9862 : : {
9863 : 3280 : while (handled_component_p (expr))
9864 : 104 : expr = TREE_OPERAND (expr, 0);
9865 : 3176 : return TREE_CODE (expr) == TARGET_EXPR;
9866 : : }
9867 : :
9868 : : /* True iff C is a conversion that binds a reference to a temporary.
9869 : : This is a superset of conv_binds_ref_to_prvalue: here we're also
9870 : : interested in xvalues. */
9871 : :
9872 : : static bool
9873 : 2530 : conv_binds_ref_to_temporary (conversion *c)
9874 : : {
9875 : 2530 : if (conv_binds_ref_to_prvalue (c))
9876 : : return true;
9877 : 2136 : if (c->kind != ck_ref_bind)
9878 : : return false;
9879 : 2136 : c = next_conversion (c);
9880 : : /* This is the case for
9881 : : struct Base {};
9882 : : struct Derived : Base {};
9883 : : const Base& b(Derived{});
9884 : : where we bind 'b' to the Base subobject of a temporary object of type
9885 : : Derived. The subobject is an xvalue; the whole object is a prvalue.
9886 : :
9887 : : The ck_base doesn't have to be present for cases like X{}.m. */
9888 : 2136 : if (c->kind == ck_base)
9889 : 8 : c = next_conversion (c);
9890 : 2081 : if (c->kind == ck_identity && c->u.expr
9891 : 4217 : && expr_represents_temporary_p (c->u.expr))
9892 : : return true;
9893 : : return false;
9894 : : }
9895 : :
9896 : : /* Return tristate::TS_TRUE if converting EXPR to a reference type TYPE binds
9897 : : the reference to a temporary. Return tristate::TS_FALSE if converting
9898 : : EXPR to a reference type TYPE doesn't bind the reference to a temporary. If
9899 : : the conversion is invalid or bad, return tristate::TS_UNKNOWN. DIRECT_INIT_P
9900 : : says whether the conversion should be done in direct- or copy-initialization
9901 : : context. */
9902 : :
9903 : : tristate
9904 : 3009 : ref_conv_binds_to_temporary (tree type, tree expr, bool direct_init_p/*=false*/)
9905 : : {
9906 : 3009 : gcc_assert (TYPE_REF_P (type));
9907 : :
9908 : 3009 : conversion_obstack_sentinel cos;
9909 : :
9910 : 3009 : const int flags = direct_init_p ? LOOKUP_NORMAL : LOOKUP_IMPLICIT;
9911 : 3009 : conversion *conv = implicit_conversion (type, TREE_TYPE (expr), expr,
9912 : : /*c_cast_p=*/false, flags, tf_none);
9913 : 3009 : tristate ret (tristate::TS_UNKNOWN);
9914 : 3009 : if (conv && !conv->bad_p)
9915 : 2530 : ret = tristate (conv_binds_ref_to_temporary (conv));
9916 : :
9917 : 6018 : return ret;
9918 : 3009 : }
9919 : :
9920 : : /* Call the trivial destructor for INSTANCE, which can be either an lvalue of
9921 : : class type or a pointer to class type. If NO_PTR_DEREF is true and
9922 : : INSTANCE has pointer type, clobber the pointer rather than what it points
9923 : : to. */
9924 : :
9925 : : tree
9926 : 1847723 : build_trivial_dtor_call (tree instance, bool no_ptr_deref)
9927 : : {
9928 : 1847723 : gcc_assert (!is_dummy_object (instance));
9929 : :
9930 : 1847723 : if (!flag_lifetime_dse)
9931 : : {
9932 : 228 : no_clobber:
9933 : 249 : return fold_convert (void_type_node, instance);
9934 : : }
9935 : :
9936 : 1909559 : if (INDIRECT_TYPE_P (TREE_TYPE (instance))
9937 : 1847495 : && (!no_ptr_deref || TYPE_REF_P (TREE_TYPE (instance))))
9938 : : {
9939 : 1772470 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
9940 : 21 : goto no_clobber;
9941 : 1772449 : instance = cp_build_fold_indirect_ref (instance);
9942 : : }
9943 : :
9944 : : /* A trivial destructor should still clobber the object. */
9945 : 1847474 : tree clobber = build_clobber (TREE_TYPE (instance), CLOBBER_OBJECT_END);
9946 : 1847474 : return build2 (MODIFY_EXPR, void_type_node,
9947 : 1847474 : instance, clobber);
9948 : : }
9949 : :
9950 : : /* Return true if in an immediate function context, or an unevaluated operand,
9951 : : or a default argument/member initializer, or a subexpression of an immediate
9952 : : invocation. */
9953 : :
9954 : : bool
9955 : 2909809 : in_immediate_context ()
9956 : : {
9957 : 2909809 : return (cp_unevaluated_operand != 0
9958 : 2563522 : || (current_function_decl != NULL_TREE
9959 : 4492844 : && DECL_IMMEDIATE_FUNCTION_P (current_function_decl))
9960 : : /* DR 2631: default args and DMI aren't immediately evaluated.
9961 : : Return true here so immediate_invocation_p returns false. */
9962 : 2549497 : || current_binding_level->kind == sk_function_parms
9963 : 2549383 : || current_binding_level->kind == sk_template_parms
9964 : 2549368 : || parsing_nsdmi ()
9965 : 5458627 : || in_consteval_if_p);
9966 : : }
9967 : :
9968 : : /* Return true if a call to FN with number of arguments NARGS
9969 : : is an immediate invocation. */
9970 : :
9971 : : bool
9972 : 138637002 : immediate_invocation_p (tree fn)
9973 : : {
9974 : 138637002 : return (TREE_CODE (fn) == FUNCTION_DECL
9975 : 138637002 : && DECL_IMMEDIATE_FUNCTION_P (fn)
9976 : 139225066 : && !in_immediate_context ());
9977 : : }
9978 : :
9979 : : /* Subroutine of the various build_*_call functions. Overload resolution
9980 : : has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
9981 : : ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
9982 : : bitmask of various LOOKUP_* flags which apply to the call itself. */
9983 : :
9984 : : static tree
9985 : 151267062 : build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
9986 : : {
9987 : 151267062 : tree fn = cand->fn;
9988 : 151267062 : const vec<tree, va_gc> *args = cand->args;
9989 : 151267062 : tree first_arg = cand->first_arg;
9990 : 151267062 : conversion **convs = cand->convs;
9991 : 151267062 : tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
9992 : 151267062 : int parmlen;
9993 : 151267062 : tree val;
9994 : 151267062 : int nargs;
9995 : 151267062 : tree *argarray;
9996 : 151267062 : bool already_used = false;
9997 : :
9998 : : /* In a template, there is no need to perform all of the work that
9999 : : is normally done. We are only interested in the type of the call
10000 : : expression, i.e., the return type of the function. Any semantic
10001 : : errors will be deferred until the template is instantiated. */
10002 : 151267062 : if (processing_template_decl)
10003 : : {
10004 : 23409248 : if (undeduced_auto_decl (fn))
10005 : 1297 : mark_used (fn, complain);
10006 : : else
10007 : : /* Otherwise set TREE_USED for the benefit of -Wunused-function.
10008 : : See PR80598. */
10009 : 23407951 : TREE_USED (fn) = 1;
10010 : :
10011 : 23409248 : tree return_type = TREE_TYPE (TREE_TYPE (fn));
10012 : 23409248 : tree callee;
10013 : 23409248 : if (first_arg == NULL_TREE)
10014 : : {
10015 : 18230855 : callee = build_addr_func (fn, complain);
10016 : 18230855 : if (callee == error_mark_node)
10017 : : return error_mark_node;
10018 : : }
10019 : : else
10020 : : {
10021 : 5178393 : callee = build_baselink (cand->conversion_path, cand->access_path,
10022 : : fn, NULL_TREE);
10023 : 5178393 : callee = build_min (COMPONENT_REF, TREE_TYPE (fn),
10024 : : first_arg, callee, NULL_TREE);
10025 : : }
10026 : :
10027 : 23409248 : tree expr = build_call_vec (return_type, callee, args);
10028 : 23409248 : SET_EXPR_LOCATION (expr, input_location);
10029 : 23409248 : if (TREE_THIS_VOLATILE (fn) && cfun)
10030 : 1928801 : current_function_returns_abnormally = 1;
10031 : 23409248 : if (TREE_DEPRECATED (fn)
10032 : 23409248 : && warning_suppressed_at (input_location,
10033 : : OPT_Wdeprecated_declarations))
10034 : : /* Make the expr consistent with the location. */
10035 : 19467 : TREE_NO_WARNING (expr) = true;
10036 : 23409248 : if (immediate_invocation_p (fn))
10037 : : {
10038 : 22281 : tree obj_arg = NULL_TREE, exprimm = expr;
10039 : 44562 : if (DECL_CONSTRUCTOR_P (fn))
10040 : 0 : obj_arg = first_arg;
10041 : 0 : if (obj_arg
10042 : 0 : && is_dummy_object (obj_arg)
10043 : 0 : && !type_dependent_expression_p (obj_arg))
10044 : : {
10045 : 0 : exprimm = build_cplus_new (DECL_CONTEXT (fn), expr, complain);
10046 : 0 : obj_arg = NULL_TREE;
10047 : : }
10048 : : /* Look through *(const T *)&obj. */
10049 : 22281 : else if (obj_arg && INDIRECT_REF_P (obj_arg))
10050 : : {
10051 : 0 : tree addr = TREE_OPERAND (obj_arg, 0);
10052 : 0 : STRIP_NOPS (addr);
10053 : 0 : if (TREE_CODE (addr) == ADDR_EXPR)
10054 : : {
10055 : 0 : tree typeo = TREE_TYPE (obj_arg);
10056 : 0 : tree typei = TREE_TYPE (TREE_OPERAND (addr, 0));
10057 : 0 : if (same_type_ignoring_top_level_qualifiers_p (typeo, typei))
10058 : 0 : obj_arg = TREE_OPERAND (addr, 0);
10059 : : }
10060 : : }
10061 : 22281 : fold_non_dependent_expr (exprimm, complain,
10062 : : /*manifestly_const_eval=*/true,
10063 : : obj_arg);
10064 : : }
10065 : 23409248 : return convert_from_reference (expr);
10066 : : }
10067 : :
10068 : : /* Give any warnings we noticed during overload resolution. */
10069 : 127857814 : if (cand->warnings && (complain & tf_warning))
10070 : : {
10071 : : struct candidate_warning *w;
10072 : 94 : for (w = cand->warnings; w; w = w->next)
10073 : 47 : joust (cand, w->loser, 1, complain);
10074 : : }
10075 : :
10076 : : /* Core issue 2327: P0135 doesn't say how to handle the case where the
10077 : : argument to the copy constructor ends up being a prvalue after
10078 : : conversion. Let's do the normal processing, but pretend we aren't
10079 : : actually using the copy constructor. */
10080 : 127857814 : bool force_elide = false;
10081 : 127857814 : if (cxx_dialect >= cxx17
10082 : 125936949 : && cand->num_convs == 1
10083 : 73967983 : && DECL_COMPLETE_CONSTRUCTOR_P (fn)
10084 : 22219246 : && (DECL_COPY_CONSTRUCTOR_P (fn)
10085 : 11508428 : || DECL_MOVE_CONSTRUCTOR_P (fn))
10086 : 8666409 : && !unsafe_return_slot_p (first_arg)
10087 : 136502760 : && conv_binds_ref_to_prvalue (convs[0]))
10088 : : {
10089 : 69342 : force_elide = true;
10090 : 69342 : goto not_really_used;
10091 : : }
10092 : :
10093 : : /* OK, we're actually calling this inherited constructor; set its deletedness
10094 : : appropriately. We can get away with doing this here because calling is
10095 : : the only way to refer to a constructor. */
10096 : 255576944 : if (DECL_INHERITED_CTOR (fn)
10097 : 18449040 : && !deduce_inheriting_ctor (fn))
10098 : : {
10099 : 2 : if (complain & tf_error)
10100 : 2 : mark_used (fn);
10101 : 2 : return error_mark_node;
10102 : : }
10103 : :
10104 : : /* Make =delete work with SFINAE. */
10105 : 127788470 : if (DECL_DELETED_FN (fn))
10106 : : {
10107 : 671874 : if (complain & tf_error)
10108 : : {
10109 : 2149 : mark_used (fn);
10110 : 2149 : if (cand->next)
10111 : : {
10112 : 2004 : if (flag_diagnostics_all_candidates)
10113 : 9 : print_z_candidates (input_location, cand, /*only_viable_p=*/false);
10114 : : else
10115 : 1995 : inform (input_location,
10116 : : "use %<-fdiagnostics-all-candidates%> to display "
10117 : : "considered candidates");
10118 : : }
10119 : : }
10120 : 671874 : return error_mark_node;
10121 : : }
10122 : :
10123 : 127116596 : if (DECL_FUNCTION_MEMBER_P (fn))
10124 : : {
10125 : 74828250 : tree access_fn;
10126 : : /* If FN is a template function, two cases must be considered.
10127 : : For example:
10128 : :
10129 : : struct A {
10130 : : protected:
10131 : : template <class T> void f();
10132 : : };
10133 : : template <class T> struct B {
10134 : : protected:
10135 : : void g();
10136 : : };
10137 : : struct C : A, B<int> {
10138 : : using A::f; // #1
10139 : : using B<int>::g; // #2
10140 : : };
10141 : :
10142 : : In case #1 where `A::f' is a member template, DECL_ACCESS is
10143 : : recorded in the primary template but not in its specialization.
10144 : : We check access of FN using its primary template.
10145 : :
10146 : : In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
10147 : : because it is a member of class template B, DECL_ACCESS is
10148 : : recorded in the specialization `B<int>::g'. We cannot use its
10149 : : primary template because `B<T>::g' and `B<int>::g' may have
10150 : : different access. */
10151 : 74828250 : if (DECL_TEMPLATE_INFO (fn)
10152 : 74828250 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
10153 : 7800577 : access_fn = DECL_TI_TEMPLATE (fn);
10154 : : else
10155 : : access_fn = fn;
10156 : 74828250 : if (!perform_or_defer_access_check (cand->access_path, access_fn,
10157 : : fn, complain))
10158 : 20565 : return error_mark_node;
10159 : : }
10160 : :
10161 : : /* If we're checking for implicit delete, don't bother with argument
10162 : : conversions. */
10163 : 127096031 : if (flags & LOOKUP_SPECULATIVE)
10164 : : {
10165 : 18991016 : if (cand->viable == 1)
10166 : : return fn;
10167 : 110 : else if (!(complain & tf_error))
10168 : : /* Reject bad conversions now. */
10169 : 95 : return error_mark_node;
10170 : : /* else continue to get conversion error. */
10171 : : }
10172 : :
10173 : 108105015 : not_really_used:
10174 : :
10175 : : /* N3276 magic doesn't apply to nested calls. */
10176 : 108174372 : tsubst_flags_t decltype_flag = (complain & tf_decltype);
10177 : 108174372 : complain &= ~tf_decltype;
10178 : : /* No-Cleanup doesn't apply to nested calls either. */
10179 : 108174372 : tsubst_flags_t no_cleanup_complain = complain;
10180 : 108174372 : complain &= ~tf_no_cleanup;
10181 : :
10182 : : /* Find maximum size of vector to hold converted arguments. */
10183 : 108174372 : parmlen = list_length (parm);
10184 : 201506422 : nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0);
10185 : 108174372 : if (parmlen > nargs)
10186 : : nargs = parmlen;
10187 : 108174372 : argarray = XALLOCAVEC (tree, nargs);
10188 : :
10189 : 216348741 : in_consteval_if_p_temp_override icip;
10190 : : /* If the call is immediate function invocation, make sure
10191 : : taking address of immediate functions is allowed in its arguments. */
10192 : 108174372 : if (immediate_invocation_p (STRIP_TEMPLATE (fn)))
10193 : 195229 : in_consteval_if_p = true;
10194 : :
10195 : 108174372 : int argarray_size = 0;
10196 : 108174372 : unsigned int arg_index = 0;
10197 : 108174372 : int conv_index = 0;
10198 : 108174372 : int param_index = 0;
10199 : :
10200 : 152834089 : auto consume_object_arg = [&arg_index, &first_arg, args]()
10201 : : {
10202 : 44659717 : if (!first_arg)
10203 : 0 : return (*args)[arg_index++];
10204 : 44659717 : tree object_arg = first_arg;
10205 : 44659717 : first_arg = NULL_TREE;
10206 : 44659717 : return object_arg;
10207 : 108174372 : };
10208 : :
10209 : : /* The implicit parameters to a constructor are not considered by overload
10210 : : resolution, and must be of the proper type. */
10211 : 216348744 : if (DECL_CONSTRUCTOR_P (fn))
10212 : : {
10213 : 12342539 : tree object_arg = consume_object_arg ();
10214 : 12342539 : argarray[argarray_size++] = build_this (object_arg);
10215 : 12342539 : parm = TREE_CHAIN (parm);
10216 : : /* We should never try to call the abstract constructor. */
10217 : 12342539 : gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
10218 : :
10219 : 12342539 : if (DECL_HAS_VTT_PARM_P (fn))
10220 : : {
10221 : 17331 : argarray[argarray_size++] = (*args)[arg_index];
10222 : 17331 : ++arg_index;
10223 : 17331 : parm = TREE_CHAIN (parm);
10224 : : }
10225 : : }
10226 : : /* Bypass access control for 'this' parameter. */
10227 : 95831833 : else if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
10228 : : {
10229 : 32312458 : tree arg = build_this (consume_object_arg ());
10230 : 32312458 : tree argtype = TREE_TYPE (arg);
10231 : :
10232 : 32312458 : if (arg == error_mark_node)
10233 : : return error_mark_node;
10234 : 32312455 : if (convs[conv_index++]->bad_p)
10235 : : {
10236 : 721 : if (complain & tf_error)
10237 : : {
10238 : 86 : auto_diagnostic_group d;
10239 : 86 : if (permerror (input_location, "passing %qT as %<this%> "
10240 : : "argument discards qualifiers",
10241 : 86 : TREE_TYPE (argtype)))
10242 : 83 : inform (DECL_SOURCE_LOCATION (fn), " in call to %qD", fn);
10243 : 86 : }
10244 : : else
10245 : : return error_mark_node;
10246 : : }
10247 : :
10248 : : /* The class where FN is defined. */
10249 : 32311820 : tree ctx = DECL_CONTEXT (fn);
10250 : :
10251 : : /* See if the function member or the whole class type is declared
10252 : : final and the call can be devirtualized. */
10253 : 32311820 : if (DECL_FINAL_P (fn) || CLASSTYPE_FINAL (ctx))
10254 : 270140 : flags |= LOOKUP_NONVIRTUAL;
10255 : :
10256 : : /* [class.mfct.non-static]: If a non-static member function of a class
10257 : : X is called for an object that is not of type X, or of a type
10258 : : derived from X, the behavior is undefined.
10259 : :
10260 : : So we can assume that anything passed as 'this' is non-null, and
10261 : : optimize accordingly. */
10262 : : /* Check that the base class is accessible. */
10263 : 32311820 : if (!accessible_base_p (TREE_TYPE (argtype),
10264 : 32311820 : BINFO_TYPE (cand->conversion_path), true))
10265 : : {
10266 : 33 : if (complain & tf_error)
10267 : 30 : error ("%qT is not an accessible base of %qT",
10268 : 30 : BINFO_TYPE (cand->conversion_path),
10269 : 30 : TREE_TYPE (argtype));
10270 : : else
10271 : 3 : return error_mark_node;
10272 : : }
10273 : : /* If fn was found by a using declaration, the conversion path
10274 : : will be to the derived class, not the base declaring fn. We
10275 : : must convert to the base. */
10276 : 32311817 : tree base_binfo = cand->conversion_path;
10277 : 32311817 : if (BINFO_TYPE (base_binfo) != ctx)
10278 : : {
10279 : 78166 : base_binfo = lookup_base (base_binfo, ctx, ba_unique, NULL, complain);
10280 : 78166 : if (base_binfo == error_mark_node)
10281 : : return error_mark_node;
10282 : : }
10283 : :
10284 : : /* If we know the dynamic type of the object, look up the final overrider
10285 : : in the BINFO. */
10286 : 33147418 : if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
10287 : 32831562 : && resolves_to_fixed_type_p (arg))
10288 : : {
10289 : 813 : tree ov = lookup_vfn_in_binfo (DECL_VINDEX (fn), base_binfo);
10290 : :
10291 : : /* And unwind base_binfo to match. If we don't find the type we're
10292 : : looking for in BINFO_INHERITANCE_CHAIN, we're looking at diamond
10293 : : inheritance; for now do a normal virtual call in that case. */
10294 : 813 : tree octx = DECL_CONTEXT (ov);
10295 : 813 : tree obinfo = base_binfo;
10296 : 1689 : while (obinfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (obinfo), octx))
10297 : 33 : obinfo = BINFO_INHERITANCE_CHAIN (obinfo);
10298 : 813 : if (obinfo)
10299 : : {
10300 : 810 : fn = ov;
10301 : 810 : base_binfo = obinfo;
10302 : 810 : flags |= LOOKUP_NONVIRTUAL;
10303 : : }
10304 : : }
10305 : :
10306 : 32311811 : tree converted_arg = build_base_path (PLUS_EXPR, arg,
10307 : : base_binfo, 1, complain);
10308 : :
10309 : 32311811 : argarray[argarray_size++] = converted_arg;
10310 : 32311811 : parm = TREE_CHAIN (parm);
10311 : : }
10312 : :
10313 : 197447441 : auto handle_arg = [fn, flags](tree type,
10314 : : tree arg,
10315 : : int const param_index,
10316 : : conversion *conv,
10317 : : tsubst_flags_t const arg_complain)
10318 : : {
10319 : : /* Set user_conv_p on the argument conversions, so rvalue/base handling
10320 : : knows not to allow any more UDCs. This needs to happen after we
10321 : : process cand->warnings. */
10322 : 89273716 : if (flags & LOOKUP_NO_CONVERSION)
10323 : 1930361 : conv->user_conv_p = true;
10324 : :
10325 : 89273716 : if (arg_complain & tf_warning)
10326 : 74281251 : maybe_warn_pessimizing_move (arg, type, /*return_p=*/false);
10327 : :
10328 : 89273716 : tree val = convert_like_with_context (conv, arg, fn,
10329 : : param_index, arg_complain);
10330 : 89273716 : val = convert_for_arg_passing (type, val, arg_complain);
10331 : 89273716 : return val;
10332 : 108173725 : };
10333 : :
10334 : 108173725 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
10335 : : {
10336 : 4720 : gcc_assert (cand->num_convs > 0);
10337 : 4720 : tree object_arg = consume_object_arg ();
10338 : 4720 : val = handle_arg (TREE_VALUE (parm),
10339 : : object_arg,
10340 : : param_index++,
10341 : 4720 : convs[conv_index++],
10342 : : complain);
10343 : :
10344 : 4720 : if (val == error_mark_node)
10345 : : return error_mark_node;
10346 : : else
10347 : 4608 : argarray[argarray_size++] = val;
10348 : 4608 : parm = TREE_CHAIN (parm);
10349 : : }
10350 : :
10351 : 108173613 : gcc_assert (first_arg == NULL_TREE);
10352 : 197441500 : for (; arg_index < vec_safe_length (args) && parm;
10353 : 89267887 : parm = TREE_CHAIN (parm), ++arg_index, ++param_index, ++conv_index)
10354 : : {
10355 : 89268996 : tree current_arg = (*args)[arg_index];
10356 : :
10357 : : /* If the argument is NULL and used to (implicitly) instantiate a
10358 : : template function (and bind one of the template arguments to
10359 : : the type of 'long int'), we don't want to warn about passing NULL
10360 : : to non-pointer argument.
10361 : : For example, if we have this template function:
10362 : :
10363 : : template<typename T> void func(T x) {}
10364 : :
10365 : : we want to warn (when -Wconversion is enabled) in this case:
10366 : :
10367 : : void foo() {
10368 : : func<int>(NULL);
10369 : : }
10370 : :
10371 : : but not in this case:
10372 : :
10373 : : void foo() {
10374 : : func(NULL);
10375 : : }
10376 : : */
10377 : 89268996 : bool const conversion_warning = !(null_node_p (current_arg)
10378 : 45633 : && DECL_TEMPLATE_INFO (fn)
10379 : 61 : && cand->template_decl
10380 : 30 : && !cand->explicit_targs);
10381 : :
10382 : 89269011 : tsubst_flags_t const arg_complain
10383 : 15 : = conversion_warning ? complain : complain & ~tf_warning;
10384 : :
10385 : 89268996 : val = handle_arg (TREE_VALUE (parm),
10386 : : current_arg,
10387 : : param_index,
10388 : 89268996 : convs[conv_index],
10389 : : arg_complain);
10390 : :
10391 : 89268996 : if (val == error_mark_node)
10392 : : return error_mark_node;
10393 : : else
10394 : 89267887 : argarray[argarray_size++] = val;
10395 : : }
10396 : :
10397 : : /* Default arguments */
10398 : 109164923 : for (; parm && parm != void_list_node;
10399 : 992419 : parm = TREE_CHAIN (parm), param_index++)
10400 : : {
10401 : 992513 : if (TREE_VALUE (parm) == error_mark_node)
10402 : : return error_mark_node;
10403 : 1985002 : val = convert_default_arg (TREE_VALUE (parm),
10404 : 992501 : TREE_PURPOSE (parm),
10405 : : fn, param_index,
10406 : : complain);
10407 : 992501 : if (val == error_mark_node)
10408 : : return error_mark_node;
10409 : 992419 : argarray[argarray_size++] = val;
10410 : : }
10411 : :
10412 : : /* Ellipsis */
10413 : 108172410 : int magic = magic_varargs_p (fn);
10414 : 110455992 : for (; arg_index < vec_safe_length (args); ++arg_index)
10415 : : {
10416 : 2283603 : tree a = (*args)[arg_index];
10417 : 2283603 : if ((magic == 3 && arg_index == 2) || (magic == 4 && arg_index == 0))
10418 : : {
10419 : : /* Do no conversions for certain magic varargs. */
10420 : 21430 : a = mark_type_use (a);
10421 : 21430 : if (TREE_CODE (a) == FUNCTION_DECL && reject_gcc_builtin (a))
10422 : 0 : return error_mark_node;
10423 : : }
10424 : 2262173 : else if (magic != 0)
10425 : : {
10426 : : /* Don't truncate excess precision to the semantic type. */
10427 : 969942 : if (magic == 1 && TREE_CODE (a) == EXCESS_PRECISION_EXPR)
10428 : 84 : a = TREE_OPERAND (a, 0);
10429 : : /* For other magic varargs only do decay_conversion. */
10430 : 969942 : a = decay_conversion (a, complain);
10431 : : }
10432 : 1292231 : else if (DECL_CONSTRUCTOR_P (fn)
10433 : 286 : && vec_safe_length (args) == 1
10434 : 1292351 : && same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (fn),
10435 : 120 : TREE_TYPE (a)))
10436 : : {
10437 : : /* Avoid infinite recursion trying to call A(...). */
10438 : 3 : if (complain & tf_error)
10439 : : /* Try to call the actual copy constructor for a good error. */
10440 : 3 : call_copy_ctor (a, complain);
10441 : 3 : return error_mark_node;
10442 : : }
10443 : : else
10444 : 1292228 : a = convert_arg_to_ellipsis (a, complain);
10445 : 2283600 : if (a == error_mark_node)
10446 : : return error_mark_node;
10447 : 2283582 : argarray[argarray_size++] = a;
10448 : : }
10449 : :
10450 : 108172389 : gcc_assert (argarray_size <= nargs);
10451 : 108172389 : nargs = argarray_size;
10452 : 108172389 : icip.reset ();
10453 : :
10454 : : /* Avoid performing argument transformation if warnings are disabled.
10455 : : When tf_warning is set and at least one of the warnings is active
10456 : : the check_function_arguments function might warn about something. */
10457 : :
10458 : 108172389 : bool warned_p = false;
10459 : 108172389 : if ((complain & tf_warning)
10460 : 81220414 : && (warn_nonnull
10461 : 79352540 : || warn_format
10462 : 79352537 : || warn_suggest_attribute_format
10463 : 79352441 : || warn_restrict))
10464 : : {
10465 : 1870220 : tree *fargs = (!nargs ? argarray
10466 : 1543035 : : (tree *) alloca (nargs * sizeof (tree)));
10467 : 5827062 : for (int j = 0; j < nargs; j++)
10468 : : {
10469 : : /* For -Wformat undo the implicit passing by hidden reference
10470 : : done by convert_arg_to_ellipsis. */
10471 : 3956842 : if (TREE_CODE (argarray[j]) == ADDR_EXPR
10472 : 3956842 : && TYPE_REF_P (TREE_TYPE (argarray[j])))
10473 : 1545 : fargs[j] = TREE_OPERAND (argarray[j], 0);
10474 : : else
10475 : 3955297 : fargs[j] = argarray[j];
10476 : : }
10477 : :
10478 : 1870220 : warned_p = check_function_arguments (input_location, fn, TREE_TYPE (fn),
10479 : : nargs, fargs, NULL,
10480 : : cp_comp_parm_types);
10481 : : }
10482 : :
10483 : 216344778 : if (DECL_INHERITED_CTOR (fn))
10484 : : {
10485 : : /* Check for passing ellipsis arguments to an inherited constructor. We
10486 : : could handle this by open-coding the inherited constructor rather than
10487 : : defining it, but let's not bother now. */
10488 : 41051 : if (!cp_unevaluated_operand
10489 : 40931 : && cand->num_convs
10490 : 40931 : && cand->convs[cand->num_convs-1]->ellipsis_p)
10491 : : {
10492 : 15 : if (complain & tf_error)
10493 : : {
10494 : 15 : sorry ("passing arguments to ellipsis of inherited constructor "
10495 : : "%qD", cand->fn);
10496 : 15 : inform (DECL_SOURCE_LOCATION (cand->fn), "declared here");
10497 : : }
10498 : 15 : return error_mark_node;
10499 : : }
10500 : :
10501 : : /* A base constructor inheriting from a virtual base doesn't get the
10502 : : inherited arguments, just this and __vtt. */
10503 : 41036 : if (ctor_omit_inherited_parms (fn))
10504 : 108172374 : nargs = 2;
10505 : : }
10506 : :
10507 : : /* Avoid actually calling copy constructors and copy assignment operators,
10508 : : if possible. */
10509 : :
10510 : 108172374 : if (!force_elide
10511 : 108103064 : && (!flag_elide_constructors
10512 : : /* It's unsafe to elide the operation when handling
10513 : : a noexcept-expression, it may evaluate to the wrong
10514 : : value (c++/53025, c++/96090). */
10515 : 108102890 : || cp_noexcept_operand != 0))
10516 : : /* Do things the hard way. */;
10517 : 106785362 : else if (cand->num_convs == 1
10518 : 164815095 : && (DECL_COPY_CONSTRUCTOR_P (fn)
10519 : 110370876 : || DECL_MOVE_CONSTRUCTOR_P (fn)))
10520 : : {
10521 : 4546304 : tree targ;
10522 : 4546304 : tree arg = argarray[num_artificial_parms_for (fn)];
10523 : 4546304 : tree fa = argarray[0];
10524 : 4546304 : bool trivial = trivial_fn_p (fn);
10525 : :
10526 : : /* Pull out the real argument, disregarding const-correctness. */
10527 : 4546304 : targ = arg;
10528 : : /* Strip the reference binding for the constructor parameter. */
10529 : 0 : if (CONVERT_EXPR_P (targ)
10530 : 4546304 : && TYPE_REF_P (TREE_TYPE (targ)))
10531 : 4546304 : targ = TREE_OPERAND (targ, 0);
10532 : : /* But don't strip any other reference bindings; binding a temporary to a
10533 : : reference prevents copy elision. */
10534 : 7519421 : while ((CONVERT_EXPR_P (targ)
10535 : 6371225 : && !TYPE_REF_P (TREE_TYPE (targ)))
10536 : 16173535 : || TREE_CODE (targ) == NON_LVALUE_EXPR)
10537 : 5903498 : targ = TREE_OPERAND (targ, 0);
10538 : 4546304 : if (TREE_CODE (targ) == ADDR_EXPR)
10539 : : {
10540 : 1440692 : targ = TREE_OPERAND (targ, 0);
10541 : 1440692 : if (!same_type_ignoring_top_level_qualifiers_p
10542 : 1440692 : (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
10543 : : targ = NULL_TREE;
10544 : : }
10545 : : else
10546 : : targ = NULL_TREE;
10547 : :
10548 : 1440254 : if (targ)
10549 : : arg = targ;
10550 : : else
10551 : 3106050 : arg = cp_build_fold_indirect_ref (arg);
10552 : :
10553 : : /* In C++17 we shouldn't be copying a TARGET_EXPR except into a
10554 : : potentially-overlapping subobject. */
10555 : 4546304 : if (CHECKING_P && cxx_dialect >= cxx17)
10556 : 4469827 : gcc_assert (TREE_CODE (arg) != TARGET_EXPR
10557 : : || force_elide
10558 : : /* It's from binding the ref parm to a packed field. */
10559 : : || convs[0]->need_temporary_p
10560 : : || seen_error ()
10561 : : /* See unsafe_copy_elision_p. */
10562 : : || unsafe_return_slot_p (fa));
10563 : :
10564 : 4546304 : bool unsafe = unsafe_copy_elision_p_opt (fa, arg);
10565 : 4546304 : bool eliding_temp = (TREE_CODE (arg) == TARGET_EXPR && !unsafe);
10566 : :
10567 : : /* [class.copy]: the copy constructor is implicitly defined even if the
10568 : : implementation elided its use. But don't warn about deprecation when
10569 : : eliding a temporary, as then no copy is actually performed. */
10570 : 4546304 : warning_sentinel s (warn_deprecated_copy, eliding_temp);
10571 : 4546304 : if (force_elide)
10572 : : /* The language says this isn't called. */;
10573 : 4476994 : else if (!trivial)
10574 : : {
10575 : 1070710 : if (!mark_used (fn, complain) && !(complain & tf_error))
10576 : 0 : return error_mark_node;
10577 : : already_used = true;
10578 : : }
10579 : : else
10580 : 3406284 : cp_handle_deprecated_or_unavailable (fn, complain);
10581 : :
10582 : 109101 : if (eliding_temp && DECL_BASE_CONSTRUCTOR_P (fn)
10583 : 4546469 : && !make_base_init_ok (arg))
10584 : : unsafe = true;
10585 : :
10586 : : /* If we're creating a temp and we already have one, don't create a
10587 : : new one. If we're not creating a temp but we get one, use
10588 : : INIT_EXPR to collapse the temp into our target. Otherwise, if the
10589 : : ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
10590 : : temp or an INIT_EXPR otherwise. */
10591 : 4546304 : if (is_dummy_object (fa))
10592 : : {
10593 : 3353042 : if (TREE_CODE (arg) == TARGET_EXPR)
10594 : : return arg;
10595 : 3252179 : else if (trivial)
10596 : 2760960 : return force_target_expr (DECL_CONTEXT (fn), arg, complain);
10597 : : }
10598 : 1193262 : else if ((trivial || TREE_CODE (arg) == TARGET_EXPR)
10599 : 619597 : && !unsafe)
10600 : : {
10601 : 619517 : tree to = cp_build_fold_indirect_ref (fa);
10602 : 619517 : val = cp_build_init_expr (to, arg);
10603 : 619517 : return val;
10604 : : }
10605 : 4546304 : }
10606 : 102239058 : else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
10607 : 1881480 : && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR)
10608 : 103696011 : && trivial_fn_p (fn))
10609 : : {
10610 : 983636 : tree to = cp_build_fold_indirect_ref (argarray[0]);
10611 : 983636 : tree type = TREE_TYPE (to);
10612 : 983636 : tree as_base = CLASSTYPE_AS_BASE (type);
10613 : 983636 : tree arg = argarray[1];
10614 : 983636 : location_t loc = cp_expr_loc_or_input_loc (arg);
10615 : :
10616 : 983636 : if (is_really_empty_class (type, /*ignore_vptr*/true))
10617 : : {
10618 : : /* Avoid copying empty classes, but ensure op= returns an lvalue even
10619 : : if the object argument isn't one. This isn't needed in other cases
10620 : : since MODIFY_EXPR is always considered an lvalue. */
10621 : 164943 : to = cp_build_addr_expr (to, tf_none);
10622 : 164943 : to = cp_build_indirect_ref (input_location, to, RO_ARROW, complain);
10623 : 164943 : val = build2 (COMPOUND_EXPR, type, arg, to);
10624 : 164943 : suppress_warning (val, OPT_Wunused);
10625 : : }
10626 : 818693 : else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
10627 : : {
10628 : 757866 : if (is_std_init_list (type)
10629 : 757866 : && conv_binds_ref_to_prvalue (convs[1]))
10630 : 3 : warning_at (loc, OPT_Winit_list_lifetime,
10631 : : "assignment from temporary %<initializer_list%> does "
10632 : : "not extend the lifetime of the underlying array");
10633 : 757866 : arg = cp_build_fold_indirect_ref (arg);
10634 : 757866 : val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
10635 : : }
10636 : : else
10637 : : {
10638 : : /* We must only copy the non-tail padding parts. */
10639 : 60827 : tree arg0, arg2, t;
10640 : 60827 : tree array_type, alias_set;
10641 : :
10642 : 60827 : arg2 = TYPE_SIZE_UNIT (as_base);
10643 : 60827 : to = cp_stabilize_reference (to);
10644 : 60827 : arg0 = cp_build_addr_expr (to, complain);
10645 : :
10646 : 60827 : array_type = build_array_type (unsigned_char_type_node,
10647 : : build_index_type
10648 : 60827 : (size_binop (MINUS_EXPR,
10649 : : arg2, size_int (1))));
10650 : 60827 : alias_set = build_int_cst (build_pointer_type (type), 0);
10651 : 60827 : t = build2 (MODIFY_EXPR, void_type_node,
10652 : : build2 (MEM_REF, array_type, arg0, alias_set),
10653 : : build2 (MEM_REF, array_type, arg, alias_set));
10654 : 60827 : val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
10655 : 60827 : suppress_warning (val, OPT_Wunused);
10656 : : }
10657 : :
10658 : 983636 : cp_handle_deprecated_or_unavailable (fn, complain);
10659 : :
10660 : 983636 : return val;
10661 : : }
10662 : 101255422 : else if (trivial_fn_p (fn))
10663 : : {
10664 : 4677822 : if (DECL_DESTRUCTOR_P (fn))
10665 : 1768747 : return build_trivial_dtor_call (argarray[0]);
10666 : 570164 : else if (default_ctor_p (fn))
10667 : : {
10668 : 570164 : if (is_dummy_object (argarray[0]))
10669 : 217493 : return force_target_expr (DECL_CONTEXT (fn), void_node,
10670 : 217493 : no_cleanup_complain);
10671 : : else
10672 : 352671 : return cp_build_fold_indirect_ref (argarray[0]);
10673 : : }
10674 : : }
10675 : :
10676 : 101368487 : gcc_assert (!force_elide);
10677 : :
10678 : 101368487 : if (!already_used
10679 : 101368487 : && !mark_used (fn, complain))
10680 : 362 : return error_mark_node;
10681 : :
10682 : : /* Warn if the built-in writes to an object of a non-trivial type. */
10683 : 101368122 : if (warn_class_memaccess
10684 : 1827866 : && vec_safe_length (args) >= 2
10685 : 102317264 : && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
10686 : 65171 : maybe_warn_class_memaccess (input_location, fn, args);
10687 : :
10688 : 101368122 : if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
10689 : : {
10690 : 518941 : tree t;
10691 : 518941 : tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
10692 : 518941 : DECL_CONTEXT (fn),
10693 : : ba_any, NULL, complain);
10694 : 518941 : gcc_assert (binfo && binfo != error_mark_node);
10695 : :
10696 : 518941 : argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
10697 : : complain);
10698 : 518941 : if (TREE_SIDE_EFFECTS (argarray[0]))
10699 : 70247 : argarray[0] = save_expr (argarray[0]);
10700 : 518941 : t = build_pointer_type (TREE_TYPE (fn));
10701 : 518941 : fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
10702 : 518941 : TREE_TYPE (fn) = t;
10703 : : }
10704 : : else
10705 : : {
10706 : : /* If FN is marked deprecated or unavailable, then we've already
10707 : : issued a diagnostic from mark_used above, so avoid redundantly
10708 : : issuing another one from build_addr_func. */
10709 : 100849181 : auto w = make_temp_override (deprecated_state,
10710 : 100849181 : UNAVAILABLE_DEPRECATED_SUPPRESS);
10711 : :
10712 : 100849181 : fn = build_addr_func (fn, complain);
10713 : 100849181 : if (fn == error_mark_node)
10714 : 0 : return error_mark_node;
10715 : :
10716 : : /* We're actually invoking the function. (Immediate functions get an
10717 : : & when invoking it even though the user didn't use &.) */
10718 : 100849181 : ADDR_EXPR_DENOTES_CALL_P (fn) = true;
10719 : 100849181 : }
10720 : :
10721 : 101368122 : tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag);
10722 : 101368122 : if (call == error_mark_node)
10723 : : return call;
10724 : 101367276 : if (cand->flags & LOOKUP_LIST_INIT_CTOR)
10725 : : {
10726 : 1028403 : tree c = extract_call_expr (call);
10727 : : /* build_new_op will clear this when appropriate. */
10728 : 1028403 : CALL_EXPR_ORDERED_ARGS (c) = true;
10729 : : }
10730 : 101367276 : if (warned_p)
10731 : : {
10732 : 265 : tree c = extract_call_expr (call);
10733 : 265 : if (TREE_CODE (c) == CALL_EXPR)
10734 : 265 : suppress_warning (c /* Suppress all warnings. */);
10735 : : }
10736 : 101367011 : else if (TREE_DEPRECATED (fn)
10737 : 101367011 : && warning_suppressed_at (input_location,
10738 : : OPT_Wdeprecated_declarations))
10739 : : {
10740 : 0 : tree c = extract_call_expr (call);
10741 : 0 : if (TREE_CODE (c) == CALL_EXPR)
10742 : 0 : TREE_NO_WARNING (c) = true;
10743 : : }
10744 : :
10745 : : return call;
10746 : : }
10747 : :
10748 : : namespace
10749 : : {
10750 : :
10751 : : /* Return the DECL of the first non-static subobject of class TYPE
10752 : : that satisfies the predicate PRED or null if none can be found. */
10753 : :
10754 : : template <class Predicate>
10755 : : tree
10756 : 3538 : first_non_static_field (tree type, Predicate pred)
10757 : : {
10758 : 3538 : if (!type || !CLASS_TYPE_P (type))
10759 : : return NULL_TREE;
10760 : :
10761 : 68980 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
10762 : : {
10763 : 66036 : if (TREE_CODE (field) != FIELD_DECL)
10764 : 43114 : continue;
10765 : 22922 : if (TREE_STATIC (field))
10766 : 0 : continue;
10767 : 62584 : if (pred (field))
10768 : : return field;
10769 : : }
10770 : :
10771 : 2944 : int i = 0;
10772 : :
10773 : 3064 : for (tree base_binfo, binfo = TYPE_BINFO (type);
10774 : 3064 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
10775 : : {
10776 : 126 : tree base = TREE_TYPE (base_binfo);
10777 : 3370 : if (pred (base))
10778 : : return base;
10779 : 120 : if (tree field = first_non_static_field (base, pred))
10780 : : return field;
10781 : : }
10782 : :
10783 : : return NULL_TREE;
10784 : : }
10785 : :
10786 : : struct NonPublicField
10787 : : {
10788 : 22172 : bool operator() (const_tree t) const
10789 : : {
10790 : 22172 : return DECL_P (t) && (TREE_PRIVATE (t) || TREE_PROTECTED (t));
10791 : : }
10792 : : };
10793 : :
10794 : : /* Return the DECL of the first non-public subobject of class TYPE
10795 : : or null if none can be found. */
10796 : :
10797 : : static inline tree
10798 : 3244 : first_non_public_field (tree type)
10799 : : {
10800 : 3244 : return first_non_static_field (type, NonPublicField ());
10801 : : }
10802 : :
10803 : : struct NonTrivialField
10804 : : {
10805 : 876 : bool operator() (const_tree t) const
10806 : : {
10807 : 876 : return !trivial_type_p (DECL_P (t) ? TREE_TYPE (t) : t);
10808 : : }
10809 : : };
10810 : :
10811 : : /* Return the DECL of the first non-trivial subobject of class TYPE
10812 : : or null if none can be found. */
10813 : :
10814 : : static inline tree
10815 : 174 : first_non_trivial_field (tree type)
10816 : : {
10817 : 174 : return first_non_static_field (type, NonTrivialField ());
10818 : : }
10819 : :
10820 : : } /* unnamed namespace */
10821 : :
10822 : : /* Return true if all copy and move assignment operator overloads for
10823 : : class TYPE are trivial and at least one of them is not deleted and,
10824 : : when ACCESS is set, accessible. Return false otherwise. Set
10825 : : HASASSIGN to true when the TYPE has a (not necessarily trivial)
10826 : : copy or move assignment. */
10827 : :
10828 : : static bool
10829 : 5003 : has_trivial_copy_assign_p (tree type, bool access, bool *hasassign)
10830 : : {
10831 : 5003 : tree fns = get_class_binding (type, assign_op_identifier);
10832 : 5003 : bool all_trivial = true;
10833 : :
10834 : : /* Iterate over overloads of the assignment operator, checking
10835 : : accessible copy assignments for triviality. */
10836 : :
10837 : 16443 : for (tree f : ovl_range (fns))
10838 : : {
10839 : : /* Skip operators that aren't copy assignments. */
10840 : 8535 : if (!copy_fn_p (f))
10841 : 3028 : continue;
10842 : :
10843 : 5507 : bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
10844 : 5795 : || accessible_p (TYPE_BINFO (type), f, true));
10845 : :
10846 : : /* Skip template assignment operators and deleted functions. */
10847 : 5507 : if (TREE_CODE (f) != FUNCTION_DECL || DECL_DELETED_FN (f))
10848 : 694 : continue;
10849 : :
10850 : 4813 : if (accessible)
10851 : 4561 : *hasassign = true;
10852 : :
10853 : 4561 : if (!accessible || !trivial_fn_p (f))
10854 : : all_trivial = false;
10855 : :
10856 : : /* Break early when both properties have been determined. */
10857 : 4813 : if (*hasassign && !all_trivial)
10858 : : break;
10859 : : }
10860 : :
10861 : : /* Return true if they're all trivial and one of the expressions
10862 : : TYPE() = TYPE() or TYPE() = (TYPE&)() is valid. */
10863 : 5003 : tree ref = cp_build_reference_type (type, false);
10864 : 5003 : return (all_trivial
10865 : 5003 : && (is_trivially_xible (MODIFY_EXPR, type, type)
10866 : 333 : || is_trivially_xible (MODIFY_EXPR, type, ref)));
10867 : : }
10868 : :
10869 : : /* Return true if all copy and move ctor overloads for class TYPE are
10870 : : trivial and at least one of them is not deleted and, when ACCESS is
10871 : : set, accessible. Return false otherwise. Set each element of HASCTOR[]
10872 : : to true when the TYPE has a (not necessarily trivial) default and copy
10873 : : (or move) ctor, respectively. */
10874 : :
10875 : : static bool
10876 : 5003 : has_trivial_copy_p (tree type, bool access, bool hasctor[2])
10877 : : {
10878 : 5003 : tree fns = get_class_binding (type, complete_ctor_identifier);
10879 : 5003 : bool all_trivial = true;
10880 : :
10881 : 25033 : for (tree f : ovl_range (fns))
10882 : : {
10883 : : /* Skip template constructors. */
10884 : 12548 : if (TREE_CODE (f) != FUNCTION_DECL)
10885 : 105 : continue;
10886 : :
10887 : 12443 : bool cpy_or_move_ctor_p = copy_fn_p (f);
10888 : :
10889 : : /* Skip ctors other than default, copy, and move. */
10890 : 12443 : if (!cpy_or_move_ctor_p && !default_ctor_p (f))
10891 : 2739 : continue;
10892 : :
10893 : 9704 : if (DECL_DELETED_FN (f))
10894 : 306 : continue;
10895 : :
10896 : 9398 : bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
10897 : 9590 : || accessible_p (TYPE_BINFO (type), f, true));
10898 : :
10899 : 9278 : if (accessible)
10900 : 9278 : hasctor[cpy_or_move_ctor_p] = true;
10901 : :
10902 : 9398 : if (cpy_or_move_ctor_p && (!accessible || !trivial_fn_p (f)))
10903 : : all_trivial = false;
10904 : :
10905 : : /* Break early when both properties have been determined. */
10906 : 9398 : if (hasctor[0] && hasctor[1] && !all_trivial)
10907 : : break;
10908 : : }
10909 : :
10910 : 5003 : return all_trivial;
10911 : : }
10912 : :
10913 : : /* Issue a warning on a call to the built-in function FNDECL if it is
10914 : : a raw memory write whose destination is not an object of (something
10915 : : like) trivial or standard layout type with a non-deleted assignment
10916 : : and copy ctor. Detects const correctness violations, corrupting
10917 : : references, virtual table pointers, and bypassing non-trivial
10918 : : assignments. */
10919 : :
10920 : : static void
10921 : 65171 : maybe_warn_class_memaccess (location_t loc, tree fndecl,
10922 : : const vec<tree, va_gc> *args)
10923 : : {
10924 : : /* Except for bcopy where it's second, the destination pointer is
10925 : : the first argument for all functions handled here. Compute
10926 : : the index of the destination and source arguments. */
10927 : 65171 : unsigned dstidx = DECL_FUNCTION_CODE (fndecl) == BUILT_IN_BCOPY;
10928 : 65171 : unsigned srcidx = !dstidx;
10929 : :
10930 : 65171 : tree dest = (*args)[dstidx];
10931 : 65171 : if (!TREE_TYPE (dest)
10932 : 65171 : || (TREE_CODE (TREE_TYPE (dest)) != ARRAY_TYPE
10933 : 64035 : && !INDIRECT_TYPE_P (TREE_TYPE (dest))))
10934 : 61796 : return;
10935 : :
10936 : 20779 : tree srctype = NULL_TREE;
10937 : :
10938 : : /* Determine the type of the pointed-to object and whether it's
10939 : : a complete class type. */
10940 : 20779 : tree desttype = TREE_TYPE (TREE_TYPE (dest));
10941 : :
10942 : 20779 : if (!desttype || !COMPLETE_TYPE_P (desttype) || !CLASS_TYPE_P (desttype))
10943 : : return;
10944 : :
10945 : : /* Check to see if the raw memory call is made by a non-static member
10946 : : function with THIS as the destination argument for the destination
10947 : : type. If so, and if the class has no non-trivial bases or members,
10948 : : be more permissive. */
10949 : 5105 : if (current_function_decl
10950 : 5105 : && DECL_OBJECT_MEMBER_FUNCTION_P (current_function_decl)
10951 : 5372 : && is_object_parameter (tree_strip_nop_conversions (dest)))
10952 : : {
10953 : 204 : tree ctx = DECL_CONTEXT (current_function_decl);
10954 : 204 : bool special = same_type_ignoring_top_level_qualifiers_p (ctx, desttype);
10955 : 204 : tree binfo = TYPE_BINFO (ctx);
10956 : :
10957 : 204 : if (special
10958 : 204 : && !BINFO_VTABLE (binfo)
10959 : 378 : && !first_non_trivial_field (desttype))
10960 : : return;
10961 : : }
10962 : :
10963 : : /* True if the class is trivial. */
10964 : 5003 : bool trivial = trivial_type_p (desttype);
10965 : :
10966 : : /* Set to true if DESTYPE has an accessible copy assignment. */
10967 : 5003 : bool hasassign = false;
10968 : : /* True if all of the class' overloaded copy assignment operators
10969 : : are all trivial (and not deleted) and at least one of them is
10970 : : accessible. */
10971 : 5003 : bool trivassign = has_trivial_copy_assign_p (desttype, true, &hasassign);
10972 : :
10973 : : /* Set to true if DESTTYPE has an accessible default and copy ctor,
10974 : : respectively. */
10975 : 5003 : bool hasctors[2] = { false, false };
10976 : :
10977 : : /* True if all of the class' overloaded copy constructors are all
10978 : : trivial (and not deleted) and at least one of them is accessible. */
10979 : 5003 : bool trivcopy = has_trivial_copy_p (desttype, true, hasctors);
10980 : :
10981 : : /* Set FLD to the first private/protected member of the class. */
10982 : 5003 : tree fld = trivial ? first_non_public_field (desttype) : NULL_TREE;
10983 : :
10984 : : /* The warning format string. */
10985 : 5003 : const char *warnfmt = NULL;
10986 : : /* A suggested alternative to offer instead of the raw memory call.
10987 : : Empty string when none can be come up with. */
10988 : 5003 : const char *suggest = "";
10989 : 5003 : bool warned = false;
10990 : :
10991 : 5003 : switch (DECL_FUNCTION_CODE (fndecl))
10992 : : {
10993 : 560 : case BUILT_IN_MEMSET:
10994 : 560 : if (!integer_zerop (maybe_constant_value ((*args)[1])))
10995 : : {
10996 : : /* Diagnose setting non-copy-assignable or non-trivial types,
10997 : : or types with a private member, to (potentially) non-zero
10998 : : bytes. Since the value of the bytes being written is unknown,
10999 : : suggest using assignment instead (if one exists). Also warn
11000 : : for writes into objects for which zero-initialization doesn't
11001 : : mean all bits clear (pointer-to-member data, where null is all
11002 : : bits set). Since the value being written is (most likely)
11003 : : non-zero, simply suggest assignment (but not copy assignment). */
11004 : 196 : suggest = "; use assignment instead";
11005 : 196 : if (!trivassign)
11006 : : warnfmt = G_("%qD writing to an object of type %#qT with "
11007 : : "no trivial copy-assignment");
11008 : 125 : else if (!trivial)
11009 : : warnfmt = G_("%qD writing to an object of non-trivial type %#qT%s");
11010 : 85 : else if (fld)
11011 : : {
11012 : 24 : const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
11013 : 24 : warned = warning_at (loc, OPT_Wclass_memaccess,
11014 : : "%qD writing to an object of type %#qT with "
11015 : : "%qs member %qD",
11016 : : fndecl, desttype, access, fld);
11017 : : }
11018 : 61 : else if (!zero_init_p (desttype))
11019 : : warnfmt = G_("%qD writing to an object of type %#qT containing "
11020 : : "a pointer to data member%s");
11021 : :
11022 : : break;
11023 : : }
11024 : : /* Fall through. */
11025 : :
11026 : 481 : case BUILT_IN_BZERO:
11027 : : /* Similarly to the above, diagnose clearing non-trivial or non-
11028 : : standard layout objects, or objects of types with no assignmenmt.
11029 : : Since the value being written is known to be zero, suggest either
11030 : : copy assignment, copy ctor, or default ctor as an alternative,
11031 : : depending on what's available. */
11032 : :
11033 : 481 : if (hasassign && hasctors[0])
11034 : : suggest = G_("; use assignment or value-initialization instead");
11035 : 49 : else if (hasassign)
11036 : : suggest = G_("; use assignment instead");
11037 : 31 : else if (hasctors[0])
11038 : 16 : suggest = G_("; use value-initialization instead");
11039 : :
11040 : 481 : if (!trivassign)
11041 : : warnfmt = G_("%qD clearing an object of type %#qT with "
11042 : : "no trivial copy-assignment%s");
11043 : 374 : else if (!trivial)
11044 : : warnfmt = G_("%qD clearing an object of non-trivial type %#qT%s");
11045 : 304 : else if (!zero_init_p (desttype))
11046 : : warnfmt = G_("%qD clearing an object of type %#qT containing "
11047 : : "a pointer-to-member%s");
11048 : : break;
11049 : :
11050 : 2437 : case BUILT_IN_BCOPY:
11051 : 2437 : case BUILT_IN_MEMCPY:
11052 : 2437 : case BUILT_IN_MEMMOVE:
11053 : 2437 : case BUILT_IN_MEMPCPY:
11054 : : /* Determine the type of the source object. */
11055 : 2437 : srctype = TREE_TYPE ((*args)[srcidx]);
11056 : 2437 : if (!srctype || !INDIRECT_TYPE_P (srctype))
11057 : 0 : srctype = void_type_node;
11058 : : else
11059 : 2437 : srctype = TREE_TYPE (srctype);
11060 : :
11061 : : /* Since it's impossible to determine wheter the byte copy is
11062 : : being used in place of assignment to an existing object or
11063 : : as a substitute for initialization, assume it's the former.
11064 : : Determine the best alternative to use instead depending on
11065 : : what's not deleted. */
11066 : 2437 : if (hasassign && hasctors[1])
11067 : : suggest = G_("; use copy-assignment or copy-initialization instead");
11068 : 488 : else if (hasassign)
11069 : : suggest = G_("; use copy-assignment instead");
11070 : 341 : else if (hasctors[1])
11071 : 290 : suggest = G_("; use copy-initialization instead");
11072 : :
11073 : 2437 : if (!trivassign)
11074 : : warnfmt = G_("%qD writing to an object of type %#qT with no trivial "
11075 : : "copy-assignment%s");
11076 : 1639 : else if (!trivially_copyable_p (desttype))
11077 : : warnfmt = G_("%qD writing to an object of non-trivially copyable "
11078 : : "type %#qT%s");
11079 : 1315 : else if (!trivcopy)
11080 : : warnfmt = G_("%qD writing to an object with a deleted copy constructor");
11081 : :
11082 : 1315 : else if (!trivial
11083 : 211 : && !VOID_TYPE_P (srctype)
11084 : 160 : && !is_byte_access_type (srctype)
11085 : 1412 : && !same_type_ignoring_top_level_qualifiers_p (desttype,
11086 : : srctype))
11087 : : {
11088 : : /* Warn when copying into a non-trivial object from an object
11089 : : of a different type other than void or char. */
11090 : 54 : warned = warning_at (loc, OPT_Wclass_memaccess,
11091 : : "%qD copying an object of non-trivial type "
11092 : : "%#qT from an array of %#qT",
11093 : : fndecl, desttype, srctype);
11094 : : }
11095 : 1261 : else if (fld
11096 : 432 : && !VOID_TYPE_P (srctype)
11097 : 384 : && !is_byte_access_type (srctype)
11098 : 1549 : && !same_type_ignoring_top_level_qualifiers_p (desttype,
11099 : : srctype))
11100 : : {
11101 : 216 : const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
11102 : 216 : warned = warning_at (loc, OPT_Wclass_memaccess,
11103 : : "%qD copying an object of type %#qT with "
11104 : : "%qs member %qD from an array of %#qT; use "
11105 : : "assignment or copy-initialization instead",
11106 : : fndecl, desttype, access, fld, srctype);
11107 : : }
11108 : 1045 : else if (!trivial && vec_safe_length (args) > 2)
11109 : : {
11110 : 157 : tree sz = maybe_constant_value ((*args)[2]);
11111 : 157 : if (!tree_fits_uhwi_p (sz))
11112 : : break;
11113 : :
11114 : : /* Finally, warn on partial copies. */
11115 : 99 : unsigned HOST_WIDE_INT typesize
11116 : 99 : = tree_to_uhwi (TYPE_SIZE_UNIT (desttype));
11117 : 99 : if (typesize == 0)
11118 : : break;
11119 : 96 : if (unsigned HOST_WIDE_INT partial = tree_to_uhwi (sz) % typesize)
11120 : 12 : warned = warning_at (loc, OPT_Wclass_memaccess,
11121 : : (typesize - partial > 1
11122 : : ? G_("%qD writing to an object of "
11123 : : "a non-trivial type %#qT leaves %wu "
11124 : : "bytes unchanged")
11125 : : : G_("%qD writing to an object of "
11126 : : "a non-trivial type %#qT leaves %wu "
11127 : : "byte unchanged")),
11128 : : fndecl, desttype, typesize - partial);
11129 : : }
11130 : : break;
11131 : :
11132 : 261 : case BUILT_IN_REALLOC:
11133 : :
11134 : 261 : if (!trivially_copyable_p (desttype))
11135 : : warnfmt = G_("%qD moving an object of non-trivially copyable type "
11136 : : "%#qT; use %<new%> and %<delete%> instead");
11137 : 138 : else if (!trivcopy)
11138 : : warnfmt = G_("%qD moving an object of type %#qT with deleted copy "
11139 : : "constructor; use %<new%> and %<delete%> instead");
11140 : 135 : else if (!get_dtor (desttype, tf_none))
11141 : : warnfmt = G_("%qD moving an object of type %#qT with deleted "
11142 : : "destructor");
11143 : 126 : else if (!trivial)
11144 : : {
11145 : 33 : tree sz = maybe_constant_value ((*args)[1]);
11146 : 33 : if (TREE_CODE (sz) == INTEGER_CST
11147 : 33 : && tree_int_cst_lt (sz, TYPE_SIZE_UNIT (desttype)))
11148 : : /* Finally, warn on reallocation into insufficient space. */
11149 : 7 : warned = warning_at (loc, OPT_Wclass_memaccess,
11150 : : "%qD moving an object of non-trivial type "
11151 : : "%#qT and size %E into a region of size %E",
11152 : 7 : fndecl, desttype, TYPE_SIZE_UNIT (desttype),
11153 : : sz);
11154 : : }
11155 : : break;
11156 : :
11157 : : default:
11158 : : return;
11159 : : }
11160 : :
11161 : 310 : if (warnfmt)
11162 : : {
11163 : 1569 : if (suggest)
11164 : 1569 : warned = warning_at (loc, OPT_Wclass_memaccess,
11165 : : warnfmt, fndecl, desttype, suggest);
11166 : : else
11167 : : warned = warning_at (loc, OPT_Wclass_memaccess,
11168 : : warnfmt, fndecl, desttype);
11169 : : }
11170 : :
11171 : 3375 : if (warned)
11172 : 1879 : inform (location_of (desttype), "%#qT declared here", desttype);
11173 : : }
11174 : :
11175 : : /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
11176 : : If FN is the result of resolving an overloaded target built-in,
11177 : : ORIG_FNDECL is the original function decl, otherwise it is null.
11178 : : This function performs no overload resolution, conversion, or other
11179 : : high-level operations. */
11180 : :
11181 : : tree
11182 : 104583659 : build_cxx_call (tree fn, int nargs, tree *argarray,
11183 : : tsubst_flags_t complain, tree orig_fndecl)
11184 : : {
11185 : 104583659 : tree fndecl;
11186 : :
11187 : : /* Remember roughly where this call is. */
11188 : 104583659 : location_t loc = cp_expr_loc_or_input_loc (fn);
11189 : 104583659 : fn = build_call_a (fn, nargs, argarray);
11190 : 104583659 : SET_EXPR_LOCATION (fn, loc);
11191 : :
11192 : 104583659 : fndecl = get_callee_fndecl (fn);
11193 : 104583659 : if (!orig_fndecl)
11194 : 104583659 : orig_fndecl = fndecl;
11195 : :
11196 : : /* Check that arguments to builtin functions match the expectations. */
11197 : 104583659 : if (fndecl
11198 : 102592895 : && !processing_template_decl
11199 : 207120014 : && fndecl_built_in_p (fndecl))
11200 : : {
11201 : : int i;
11202 : :
11203 : : /* We need to take care that values to BUILT_IN_NORMAL
11204 : : are reduced. */
11205 : 18786281 : for (i = 0; i < nargs; i++)
11206 : 12229247 : argarray[i] = maybe_constant_value (argarray[i]);
11207 : :
11208 : 6557034 : if (!check_builtin_function_arguments (EXPR_LOCATION (fn), vNULL, fndecl,
11209 : : orig_fndecl, nargs, argarray,
11210 : : complain & tf_error))
11211 : 801 : return error_mark_node;
11212 : 6556233 : else if (fndecl_built_in_p (fndecl, BUILT_IN_CLEAR_PADDING))
11213 : : {
11214 : 8241 : tree arg0 = argarray[0];
11215 : 8241 : STRIP_NOPS (arg0);
11216 : 8241 : if (TREE_CODE (arg0) == ADDR_EXPR
11217 : 265 : && DECL_P (TREE_OPERAND (arg0, 0))
11218 : 8485 : && same_type_ignoring_top_level_qualifiers_p
11219 : 244 : (TREE_TYPE (TREE_TYPE (argarray[0])),
11220 : 244 : TREE_TYPE (TREE_TYPE (arg0))))
11221 : : /* For __builtin_clear_padding (&var) we know the type
11222 : : is for a complete object, so there is no risk in clearing
11223 : : padding that is reused in some derived class member. */;
11224 : 8004 : else if (!trivially_copyable_p (TREE_TYPE (TREE_TYPE (argarray[0]))))
11225 : : {
11226 : 18 : error_at (EXPR_LOC_OR_LOC (argarray[0], input_location),
11227 : : "argument %u in call to function %qE "
11228 : : "has pointer to a non-trivially-copyable type (%qT)",
11229 : 18 : 1, fndecl, TREE_TYPE (argarray[0]));
11230 : 18 : return error_mark_node;
11231 : : }
11232 : : }
11233 : : }
11234 : :
11235 : 104582840 : if (VOID_TYPE_P (TREE_TYPE (fn)))
11236 : : return fn;
11237 : :
11238 : : /* 5.2.2/11: If a function call is a prvalue of object type: if the
11239 : : function call is either the operand of a decltype-specifier or the
11240 : : right operand of a comma operator that is the operand of a
11241 : : decltype-specifier, a temporary object is not introduced for the
11242 : : prvalue. The type of the prvalue may be incomplete. */
11243 : 77413492 : if (!(complain & tf_decltype))
11244 : : {
11245 : 67347864 : fn = require_complete_type (fn, complain);
11246 : 67347864 : if (fn == error_mark_node)
11247 : : return error_mark_node;
11248 : :
11249 : 67347840 : if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
11250 : : {
11251 : 7493837 : fn = build_cplus_new (TREE_TYPE (fn), fn, complain);
11252 : 7493837 : maybe_warn_parm_abi (TREE_TYPE (fn), loc);
11253 : : }
11254 : : }
11255 : 77413468 : return convert_from_reference (fn);
11256 : : }
11257 : :
11258 : : /* Returns the value to use for the in-charge parameter when making a
11259 : : call to a function with the indicated NAME.
11260 : :
11261 : : FIXME:Can't we find a neater way to do this mapping? */
11262 : :
11263 : : tree
11264 : 36798 : in_charge_arg_for_name (tree name)
11265 : : {
11266 : 36798 : if (IDENTIFIER_CTOR_P (name))
11267 : : {
11268 : 20624 : if (name == complete_ctor_identifier)
11269 : 10312 : return integer_one_node;
11270 : 10312 : gcc_checking_assert (name == base_ctor_identifier);
11271 : : }
11272 : : else
11273 : : {
11274 : 16174 : if (name == complete_dtor_identifier)
11275 : 8087 : return integer_two_node;
11276 : 8087 : else if (name == deleting_dtor_identifier)
11277 : : /* The deleting dtor should now be handled by
11278 : : build_delete_destructor_body. */
11279 : 0 : gcc_unreachable ();
11280 : 8087 : gcc_checking_assert (name == base_dtor_identifier);
11281 : : }
11282 : :
11283 : 18399 : return integer_zero_node;
11284 : : }
11285 : :
11286 : : /* We've built up a constructor call RET. Complain if it delegates to the
11287 : : constructor we're currently compiling. */
11288 : :
11289 : : static void
11290 : 275079 : check_self_delegation (tree ret)
11291 : : {
11292 : 275079 : if (TREE_CODE (ret) == TARGET_EXPR)
11293 : 0 : ret = TARGET_EXPR_INITIAL (ret);
11294 : 275079 : tree fn = cp_get_callee_fndecl_nofold (ret);
11295 : 275079 : if (fn && DECL_ABSTRACT_ORIGIN (fn) == current_function_decl)
11296 : 46 : error ("constructor delegates to itself");
11297 : 275079 : }
11298 : :
11299 : : /* Build a call to a constructor, destructor, or an assignment
11300 : : operator for INSTANCE, an expression with class type. NAME
11301 : : indicates the special member function to call; *ARGS are the
11302 : : arguments. ARGS may be NULL. This may change ARGS. BINFO
11303 : : indicates the base of INSTANCE that is to be passed as the `this'
11304 : : parameter to the member function called.
11305 : :
11306 : : FLAGS are the LOOKUP_* flags to use when processing the call.
11307 : :
11308 : : If NAME indicates a complete object constructor, INSTANCE may be
11309 : : NULL_TREE. In this case, the caller will call build_cplus_new to
11310 : : store the newly constructed object into a VAR_DECL. */
11311 : :
11312 : : tree
11313 : 26730719 : build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
11314 : : tree binfo, int flags, tsubst_flags_t complain)
11315 : : {
11316 : 26730719 : tree fns;
11317 : : /* The type of the subobject to be constructed or destroyed. */
11318 : 26730719 : tree class_type;
11319 : 26730719 : vec<tree, va_gc> *allocated = NULL;
11320 : 26730719 : tree ret;
11321 : :
11322 : 26730719 : gcc_assert (IDENTIFIER_CDTOR_P (name) || name == assign_op_identifier);
11323 : :
11324 : 26730719 : if (error_operand_p (instance))
11325 : 0 : return error_mark_node;
11326 : :
11327 : 26730719 : if (IDENTIFIER_DTOR_P (name))
11328 : : {
11329 : 8365389 : gcc_assert (args == NULL || vec_safe_is_empty (*args));
11330 : 8365389 : if (!type_build_dtor_call (TREE_TYPE (instance)))
11331 : : /* Shortcut to avoid lazy destructor declaration. */
11332 : 37583 : return build_trivial_dtor_call (instance);
11333 : : }
11334 : :
11335 : 26693136 : if (TYPE_P (binfo))
11336 : : {
11337 : : /* Resolve the name. */
11338 : 20454290 : if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
11339 : 24 : return error_mark_node;
11340 : :
11341 : 20454266 : binfo = TYPE_BINFO (binfo);
11342 : : }
11343 : :
11344 : 20454266 : gcc_assert (binfo != NULL_TREE);
11345 : :
11346 : 26693112 : class_type = BINFO_TYPE (binfo);
11347 : :
11348 : : /* Handle the special case where INSTANCE is NULL_TREE. */
11349 : 26693112 : if (name == complete_ctor_identifier && !instance)
11350 : 12943162 : instance = build_dummy_object (class_type);
11351 : : else
11352 : : {
11353 : : /* Convert to the base class, if necessary. */
11354 : 13749950 : if (!same_type_ignoring_top_level_qualifiers_p
11355 : 13749950 : (TREE_TYPE (instance), BINFO_TYPE (binfo)))
11356 : : {
11357 : 1586535 : if (IDENTIFIER_CDTOR_P (name))
11358 : : /* For constructors and destructors, either the base is
11359 : : non-virtual, or it is virtual but we are doing the
11360 : : conversion from a constructor or destructor for the
11361 : : complete object. In either case, we can convert
11362 : : statically. */
11363 : 1566869 : instance = convert_to_base_statically (instance, binfo);
11364 : : else
11365 : : {
11366 : : /* However, for assignment operators, we must convert
11367 : : dynamically if the base is virtual. */
11368 : 19666 : gcc_checking_assert (name == assign_op_identifier);
11369 : 19666 : instance = build_base_path (PLUS_EXPR, instance,
11370 : : binfo, /*nonnull=*/1, complain);
11371 : : }
11372 : : }
11373 : : }
11374 : :
11375 : 26693112 : gcc_assert (instance != NULL_TREE);
11376 : :
11377 : : /* In C++17, "If the initializer expression is a prvalue and the
11378 : : cv-unqualified version of the source type is the same class as the class
11379 : : of the destination, the initializer expression is used to initialize the
11380 : : destination object." Handle that here to avoid doing overload
11381 : : resolution. */
11382 : 26693112 : if (cxx_dialect >= cxx17
11383 : 26503829 : && args && vec_safe_length (*args) == 1
11384 : 40964685 : && !unsafe_return_slot_p (instance))
11385 : : {
11386 : 13276680 : tree arg = (**args)[0];
11387 : :
11388 : 689735 : if (BRACE_ENCLOSED_INITIALIZER_P (arg)
11389 : 689718 : && !TYPE_HAS_LIST_CTOR (class_type)
11390 : 641859 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (arg)
11391 : 13918525 : && CONSTRUCTOR_NELTS (arg) == 1)
11392 : 397083 : arg = CONSTRUCTOR_ELT (arg, 0)->value;
11393 : :
11394 : 13276680 : if ((TREE_CODE (arg) == TARGET_EXPR
11395 : 5700036 : || TREE_CODE (arg) == CONSTRUCTOR)
11396 : 21146056 : && (same_type_ignoring_top_level_qualifiers_p
11397 : 7869376 : (class_type, TREE_TYPE (arg))))
11398 : : {
11399 : 7214391 : if (is_dummy_object (instance))
11400 : : return arg;
11401 : 152228 : else if (TREE_CODE (arg) == TARGET_EXPR)
11402 : 152228 : TARGET_EXPR_DIRECT_INIT_P (arg) = true;
11403 : :
11404 : 152228 : if ((complain & tf_error)
11405 : 152226 : && (flags & LOOKUP_DELEGATING_CONS))
11406 : 0 : check_self_delegation (arg);
11407 : : /* Avoid change of behavior on Wunused-var-2.C. */
11408 : 152228 : instance = mark_lvalue_use (instance);
11409 : 152228 : return cp_build_init_expr (instance, arg);
11410 : : }
11411 : : }
11412 : :
11413 : 19478721 : fns = lookup_fnfields (binfo, name, 1, complain);
11414 : :
11415 : : /* When making a call to a constructor or destructor for a subobject
11416 : : that uses virtual base classes, pass down a pointer to a VTT for
11417 : : the subobject. */
11418 : 19478721 : if ((name == base_ctor_identifier
11419 : 17500242 : || name == base_dtor_identifier)
11420 : 21045662 : && CLASSTYPE_VBASECLASSES (class_type))
11421 : : {
11422 : 48174 : tree vtt;
11423 : 48174 : tree sub_vtt;
11424 : :
11425 : : /* If the current function is a complete object constructor
11426 : : or destructor, then we fetch the VTT directly.
11427 : : Otherwise, we look it up using the VTT we were given. */
11428 : 48174 : vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
11429 : 48174 : vtt = decay_conversion (vtt, complain);
11430 : 48174 : if (vtt == error_mark_node)
11431 : 0 : return error_mark_node;
11432 : 48174 : vtt = build_if_in_charge (vtt, current_vtt_parm);
11433 : 48174 : if (BINFO_SUBVTT_INDEX (binfo))
11434 : 48010 : sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
11435 : : else
11436 : 164 : sub_vtt = vtt;
11437 : :
11438 : 48174 : if (args == NULL)
11439 : : {
11440 : 30839 : allocated = make_tree_vector ();
11441 : 30839 : args = &allocated;
11442 : : }
11443 : :
11444 : 48174 : vec_safe_insert (*args, 0, sub_vtt);
11445 : : }
11446 : :
11447 : 38957442 : ret = build_new_method_call (instance, fns, args,
11448 : 19478721 : TYPE_BINFO (BINFO_TYPE (binfo)),
11449 : : flags, /*fn=*/NULL,
11450 : : complain);
11451 : :
11452 : 19478721 : if (allocated != NULL)
11453 : 30839 : release_tree_vector (allocated);
11454 : :
11455 : 19478721 : if ((complain & tf_error)
11456 : 18031792 : && (flags & LOOKUP_DELEGATING_CONS)
11457 : 275171 : && name == complete_ctor_identifier)
11458 : 275079 : check_self_delegation (ret);
11459 : :
11460 : : return ret;
11461 : : }
11462 : :
11463 : : /* Return the NAME, as a C string. The NAME indicates a function that
11464 : : is a member of TYPE. *FREE_P is set to true if the caller must
11465 : : free the memory returned.
11466 : :
11467 : : Rather than go through all of this, we should simply set the names
11468 : : of constructors and destructors appropriately, and dispense with
11469 : : ctor_identifier, dtor_identifier, etc. */
11470 : :
11471 : : static char *
11472 : 88 : name_as_c_string (tree name, tree type, bool *free_p)
11473 : : {
11474 : 88 : const char *pretty_name;
11475 : :
11476 : : /* Assume that we will not allocate memory. */
11477 : 88 : *free_p = false;
11478 : : /* Constructors and destructors are special. */
11479 : 88 : if (IDENTIFIER_CDTOR_P (name))
11480 : : {
11481 : 42 : pretty_name
11482 : 42 : = identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type)));
11483 : : /* For a destructor, add the '~'. */
11484 : 42 : if (IDENTIFIER_DTOR_P (name))
11485 : : {
11486 : 0 : pretty_name = concat ("~", pretty_name, NULL);
11487 : : /* Remember that we need to free the memory allocated. */
11488 : 0 : *free_p = true;
11489 : : }
11490 : : }
11491 : 46 : else if (IDENTIFIER_CONV_OP_P (name))
11492 : : {
11493 : 0 : pretty_name = concat ("operator ",
11494 : 0 : type_as_string_translate (TREE_TYPE (name),
11495 : : TFF_PLAIN_IDENTIFIER),
11496 : : NULL);
11497 : : /* Remember that we need to free the memory allocated. */
11498 : 0 : *free_p = true;
11499 : : }
11500 : : else
11501 : 46 : pretty_name = identifier_to_locale (IDENTIFIER_POINTER (name));
11502 : :
11503 : 88 : return CONST_CAST (char *, pretty_name);
11504 : : }
11505 : :
11506 : : /* If CANDIDATES contains exactly one candidate, return it, otherwise
11507 : : return NULL. */
11508 : :
11509 : : static z_candidate *
11510 : 575 : single_z_candidate (z_candidate *candidates)
11511 : : {
11512 : 0 : if (candidates == NULL)
11513 : : return NULL;
11514 : :
11515 : 563 : if (candidates->next)
11516 : 0 : return NULL;
11517 : :
11518 : : return candidates;
11519 : : }
11520 : :
11521 : : /* If CANDIDATE is invalid due to a bad argument type, return the
11522 : : pertinent conversion_info.
11523 : :
11524 : : Otherwise, return NULL. */
11525 : :
11526 : : static const conversion_info *
11527 : 268 : maybe_get_bad_conversion_for_unmatched_call (const z_candidate *candidate)
11528 : : {
11529 : : /* Must be an rr_arg_conversion or rr_bad_arg_conversion. */
11530 : 268 : rejection_reason *r = candidate->reason;
11531 : :
11532 : 268 : if (r == NULL)
11533 : : return NULL;
11534 : :
11535 : 268 : switch (r->code)
11536 : : {
11537 : : default:
11538 : : return NULL;
11539 : :
11540 : 48 : case rr_arg_conversion:
11541 : 48 : return &r->u.conversion;
11542 : :
11543 : 6 : case rr_bad_arg_conversion:
11544 : 6 : return &r->u.bad_conversion;
11545 : : }
11546 : : }
11547 : :
11548 : : /* Issue an error and note complaining about a bad argument type at a
11549 : : callsite with a single candidate FNDECL.
11550 : :
11551 : : ARG_LOC is the location of the argument (or UNKNOWN_LOCATION, in which
11552 : : case input_location is used).
11553 : : FROM_TYPE is the type of the actual argument; TO_TYPE is the type of
11554 : : the formal parameter. */
11555 : :
11556 : : void
11557 : 143 : complain_about_bad_argument (location_t arg_loc,
11558 : : tree from_type, tree to_type,
11559 : : tree fndecl, int parmnum)
11560 : : {
11561 : 143 : auto_diagnostic_group d;
11562 : 143 : range_label_for_type_mismatch rhs_label (from_type, to_type);
11563 : 143 : range_label *label = &rhs_label;
11564 : 143 : if (arg_loc == UNKNOWN_LOCATION)
11565 : : {
11566 : 4 : arg_loc = input_location;
11567 : 4 : label = NULL;
11568 : : }
11569 : 143 : gcc_rich_location richloc (arg_loc, label, highlight_colors::percent_h);
11570 : 143 : error_at (&richloc,
11571 : : "cannot convert %qH to %qI",
11572 : : from_type, to_type);
11573 : 143 : maybe_inform_about_fndecl_for_bogus_argument_init
11574 : 143 : (fndecl,
11575 : : parmnum,
11576 : : highlight_colors::percent_i);
11577 : 143 : }
11578 : :
11579 : : /* Subroutine of build_new_method_call_1, for where there are no viable
11580 : : candidates for the call. */
11581 : :
11582 : : static void
11583 : 581 : complain_about_no_candidates_for_method_call (tree instance,
11584 : : z_candidate *candidates,
11585 : : tree explicit_targs,
11586 : : tree basetype,
11587 : : tree optype, tree name,
11588 : : bool skip_first_for_error,
11589 : : vec<tree, va_gc> *user_args)
11590 : : {
11591 : 581 : auto_diagnostic_group d;
11592 : 581 : if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
11593 : 0 : cxx_incomplete_type_error (instance, basetype);
11594 : 581 : else if (optype)
11595 : 6 : error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
11596 : : basetype, optype, build_tree_list_vec (user_args),
11597 : 6 : TREE_TYPE (instance));
11598 : : else
11599 : : {
11600 : : /* Special-case for when there's a single candidate that's failing
11601 : : due to a bad argument type. */
11602 : 575 : if (z_candidate *candidate = single_z_candidate (candidates))
11603 : 268 : if (const conversion_info *conv
11604 : 268 : = maybe_get_bad_conversion_for_unmatched_call (candidate))
11605 : : {
11606 : 54 : tree from_type = conv->from;
11607 : 54 : if (!TYPE_P (conv->from))
11608 : 6 : from_type = lvalue_type (conv->from);
11609 : 54 : complain_about_bad_argument (conv->loc,
11610 : 54 : from_type, conv->to_type,
11611 : 54 : candidate->fn, conv->n_arg);
11612 : 54 : return;
11613 : : }
11614 : :
11615 : 521 : tree arglist = build_tree_list_vec (user_args);
11616 : 521 : tree errname = name;
11617 : 521 : bool twiddle = false;
11618 : 521 : if (IDENTIFIER_CDTOR_P (errname))
11619 : : {
11620 : 285 : twiddle = IDENTIFIER_DTOR_P (errname);
11621 : 285 : errname = constructor_name (basetype);
11622 : : }
11623 : 521 : if (explicit_targs)
11624 : 37 : errname = lookup_template_function (errname, explicit_targs);
11625 : 521 : if (skip_first_for_error)
11626 : 3 : arglist = TREE_CHAIN (arglist);
11627 : 1042 : error ("no matching function for call to %<%T::%s%E(%A)%#V%>",
11628 : 521 : basetype, &"~"[!twiddle], errname, arglist,
11629 : 521 : TREE_TYPE (instance));
11630 : : }
11631 : 527 : print_z_candidates (location_of (name), candidates);
11632 : 581 : }
11633 : :
11634 : : /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
11635 : : be set, upon return, to the function called. ARGS may be NULL.
11636 : : This may change ARGS. */
11637 : :
11638 : : tree
11639 : 75772763 : build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args,
11640 : : tree conversion_path, int flags,
11641 : : tree *fn_p, tsubst_flags_t complain)
11642 : : {
11643 : 75772763 : struct z_candidate *candidates = 0, *cand;
11644 : 75772763 : tree explicit_targs = NULL_TREE;
11645 : 75772763 : tree basetype = NULL_TREE;
11646 : 75772763 : tree access_binfo;
11647 : 75772763 : tree optype;
11648 : 75772763 : tree first_mem_arg = NULL_TREE;
11649 : 75772763 : tree name;
11650 : 75772763 : bool skip_first_for_error;
11651 : 75772763 : vec<tree, va_gc> *user_args;
11652 : 75772763 : tree call;
11653 : 75772763 : tree fn;
11654 : 75772763 : int template_only = 0;
11655 : 75772763 : bool any_viable_p;
11656 : 75772763 : tree orig_instance;
11657 : 75772763 : tree orig_fns;
11658 : 75772763 : vec<tree, va_gc> *orig_args = NULL;
11659 : :
11660 : 75772763 : auto_cond_timevar tv (TV_OVERLOAD);
11661 : :
11662 : 75772763 : gcc_assert (instance != NULL_TREE);
11663 : :
11664 : : /* We don't know what function we're going to call, yet. */
11665 : 75772763 : if (fn_p)
11666 : 21018396 : *fn_p = NULL_TREE;
11667 : :
11668 : 75772763 : if (error_operand_p (instance)
11669 : 75772763 : || !fns || error_operand_p (fns))
11670 : 1339774 : return error_mark_node;
11671 : :
11672 : 74432989 : if (!BASELINK_P (fns))
11673 : : {
11674 : 0 : if (complain & tf_error)
11675 : 0 : error ("call to non-function %qD", fns);
11676 : 0 : return error_mark_node;
11677 : : }
11678 : :
11679 : 74432989 : orig_instance = instance;
11680 : 74432989 : orig_fns = fns;
11681 : :
11682 : : /* Dismantle the baselink to collect all the information we need. */
11683 : 74432989 : if (!conversion_path)
11684 : 35287556 : conversion_path = BASELINK_BINFO (fns);
11685 : 74432989 : access_binfo = BASELINK_ACCESS_BINFO (fns);
11686 : 74432989 : optype = BASELINK_OPTYPE (fns);
11687 : 74432989 : fns = BASELINK_FUNCTIONS (fns);
11688 : 74432989 : if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
11689 : : {
11690 : 6513658 : explicit_targs = TREE_OPERAND (fns, 1);
11691 : 6513658 : fns = TREE_OPERAND (fns, 0);
11692 : 6513658 : template_only = 1;
11693 : : }
11694 : 74432989 : gcc_assert (OVL_P (fns));
11695 : 74432989 : fn = OVL_FIRST (fns);
11696 : 74432989 : name = DECL_NAME (fn);
11697 : :
11698 : 74432989 : basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
11699 : 74432989 : gcc_assert (CLASS_TYPE_P (basetype));
11700 : :
11701 : 74432989 : user_args = args == NULL ? NULL : *args;
11702 : : /* Under DR 147 A::A() is an invalid constructor call,
11703 : : not a functional cast. */
11704 : 74432989 : if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
11705 : : {
11706 : 54 : if (! (complain & tf_error))
11707 : 0 : return error_mark_node;
11708 : :
11709 : 54 : basetype = DECL_CONTEXT (fn);
11710 : 54 : name = constructor_name (basetype);
11711 : 54 : auto_diagnostic_group d;
11712 : 54 : if (permerror (input_location,
11713 : : "cannot call constructor %<%T::%D%> directly",
11714 : : basetype, name))
11715 : 54 : inform (input_location, "for a function-style cast, remove the "
11716 : : "redundant %<::%D%>", name);
11717 : 54 : call = build_functional_cast (input_location, basetype,
11718 : : build_tree_list_vec (user_args),
11719 : : complain);
11720 : 54 : return call;
11721 : 54 : }
11722 : :
11723 : 74432935 : if (processing_template_decl)
11724 : 7484861 : orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
11725 : :
11726 : : /* Process the argument list. */
11727 : 74336083 : if (args != NULL && *args != NULL)
11728 : : {
11729 : 63105452 : *args = resolve_args (*args, complain);
11730 : 63105452 : if (*args == NULL)
11731 : 120 : return error_mark_node;
11732 : : user_args = *args;
11733 : : }
11734 : :
11735 : : /* Consider the object argument to be used even if we end up selecting a
11736 : : static member function. */
11737 : 74432815 : instance = mark_type_use (instance);
11738 : :
11739 : : /* Figure out whether to skip the first argument for the error
11740 : : message we will display to users if an error occurs. We don't
11741 : : want to display any compiler-generated arguments. The "this"
11742 : : pointer hasn't been added yet. However, we must remove the VTT
11743 : : pointer if this is a call to a base-class constructor or
11744 : : destructor. */
11745 : 74432815 : skip_first_for_error = false;
11746 : 74432815 : if (IDENTIFIER_CDTOR_P (name))
11747 : : {
11748 : : /* Callers should explicitly indicate whether they want to ctor
11749 : : the complete object or just the part without virtual bases. */
11750 : 37291163 : gcc_assert (name != ctor_identifier);
11751 : :
11752 : : /* Remove the VTT pointer, if present. */
11753 : 35312690 : if ((name == base_ctor_identifier || name == base_dtor_identifier)
11754 : 38858104 : && CLASSTYPE_VBASECLASSES (basetype))
11755 : : skip_first_for_error = true;
11756 : :
11757 : : /* It's OK to call destructors and constructors on cv-qualified
11758 : : objects. Therefore, convert the INSTANCE to the unqualified
11759 : : type, if necessary. */
11760 : 37291163 : if (!same_type_p (basetype, TREE_TYPE (instance)))
11761 : : {
11762 : 358489 : instance = build_this (instance);
11763 : 358489 : instance = build_nop (build_pointer_type (basetype), instance);
11764 : 358489 : instance = build_fold_indirect_ref (instance);
11765 : : }
11766 : : }
11767 : : else
11768 : 74283304 : gcc_assert (!DECL_DESTRUCTOR_P (fn) && !DECL_CONSTRUCTOR_P (fn));
11769 : :
11770 : : /* For the overload resolution we need to find the actual `this`
11771 : : that would be captured if the call turns out to be to a
11772 : : non-static member function. Do not actually capture it at this
11773 : : point. */
11774 : 148865630 : if (DECL_CONSTRUCTOR_P (fn))
11775 : : /* Constructors don't use the enclosing 'this'. */
11776 : : first_mem_arg = instance;
11777 : : else
11778 : 57121384 : first_mem_arg = maybe_resolve_dummy (instance, false);
11779 : :
11780 : 74432815 : conversion_obstack_sentinel cos;
11781 : :
11782 : : /* The number of arguments artificial parms in ARGS; we subtract one because
11783 : : there's no 'this' in ARGS. */
11784 : 74432815 : unsigned skip = num_artificial_parms_for (fn) - 1;
11785 : :
11786 : : /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
11787 : : initializer, not T({ }). */
11788 : 74432815 : if (DECL_CONSTRUCTOR_P (fn)
11789 : 14296228 : && vec_safe_length (user_args) > skip
11790 : 86952203 : && DIRECT_LIST_INIT_P ((*user_args)[skip]))
11791 : : {
11792 : 692771 : tree init_list = (*user_args)[skip];
11793 : 692771 : tree init = NULL_TREE;
11794 : :
11795 : 692771 : gcc_assert (user_args->length () == skip + 1
11796 : : && !(flags & LOOKUP_ONLYCONVERTING));
11797 : :
11798 : : /* If the initializer list has no elements and T is a class type with
11799 : : a default constructor, the object is value-initialized. Handle
11800 : : this here so we don't need to handle it wherever we use
11801 : : build_special_member_call. */
11802 : 692771 : if (CONSTRUCTOR_NELTS (init_list) == 0
11803 : 101146 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
11804 : : /* For a user-provided default constructor, use the normal
11805 : : mechanisms so that protected access works. */
11806 : 101146 : && type_has_non_user_provided_default_constructor (basetype)
11807 : 668857 : && !processing_template_decl)
11808 : 77229 : init = build_value_init (basetype, complain);
11809 : :
11810 : : /* If BASETYPE is an aggregate, we need to do aggregate
11811 : : initialization. */
11812 : 615542 : else if (CP_AGGREGATE_TYPE_P (basetype))
11813 : : {
11814 : 42 : init = reshape_init (basetype, init_list, complain);
11815 : 42 : init = digest_init (basetype, init, complain);
11816 : : }
11817 : :
11818 : 77271 : if (init)
11819 : : {
11820 : 77271 : if (is_dummy_object (instance))
11821 : 21118 : return get_target_expr (init, complain);
11822 : 56153 : return cp_build_init_expr (instance, init);
11823 : : }
11824 : :
11825 : : /* Otherwise go ahead with overload resolution. */
11826 : 615500 : add_list_candidates (fns, first_mem_arg, user_args,
11827 : : basetype, explicit_targs, template_only,
11828 : : conversion_path, access_binfo, flags,
11829 : : &candidates, complain);
11830 : : }
11831 : : else
11832 : 73740044 : add_candidates (fns, first_mem_arg, user_args, optype,
11833 : : explicit_targs, template_only, conversion_path,
11834 : : access_binfo, flags, &candidates, complain);
11835 : :
11836 : 74355544 : any_viable_p = false;
11837 : 74355544 : candidates = splice_viable (candidates, false, &any_viable_p);
11838 : :
11839 : 74355544 : if (!any_viable_p)
11840 : : {
11841 : : /* [dcl.init], 17.6.2.2:
11842 : :
11843 : : Otherwise, if no constructor is viable, the destination type is
11844 : : a (possibly cv-qualified) aggregate class A, and the initializer
11845 : : is a parenthesized expression-list, the object is initialized as
11846 : : follows...
11847 : :
11848 : : We achieve this by building up a CONSTRUCTOR, as for list-init,
11849 : : and setting CONSTRUCTOR_IS_PAREN_INIT to distinguish between
11850 : : the two. */
11851 : 13188 : if (DECL_CONSTRUCTOR_P (fn)
11852 : 9538 : && !(flags & LOOKUP_ONLYCONVERTING)
11853 : 9520 : && cxx_dialect >= cxx20
11854 : 7116 : && CP_AGGREGATE_TYPE_P (basetype)
11855 : 13188 : && !vec_safe_is_empty (user_args))
11856 : : {
11857 : : /* Create a CONSTRUCTOR from ARGS, e.g. {1, 2} from <1, 2>. */
11858 : 827 : tree ctor = build_constructor_from_vec (init_list_type_node,
11859 : : user_args);
11860 : 827 : CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true;
11861 : 827 : CONSTRUCTOR_IS_PAREN_INIT (ctor) = true;
11862 : 827 : if (is_dummy_object (instance))
11863 : : return ctor;
11864 : : else
11865 : : {
11866 : 665 : ctor = digest_init (basetype, ctor, complain);
11867 : 665 : if (ctor == error_mark_node)
11868 : : return error_mark_node;
11869 : 543 : return cp_build_init_expr (instance, ctor);
11870 : : }
11871 : : }
11872 : 12361 : if (complain & tf_error)
11873 : 581 : complain_about_no_candidates_for_method_call (instance, candidates,
11874 : : explicit_targs, basetype,
11875 : : optype, name,
11876 : : skip_first_for_error,
11877 : : user_args);
11878 : 12361 : call = error_mark_node;
11879 : : }
11880 : : else
11881 : : {
11882 : 74342356 : cand = tourney (candidates, complain);
11883 : 74342356 : if (cand == 0)
11884 : : {
11885 : 184 : char *pretty_name;
11886 : 184 : bool free_p;
11887 : 184 : tree arglist;
11888 : :
11889 : 184 : if (complain & tf_error)
11890 : : {
11891 : 88 : pretty_name = name_as_c_string (name, basetype, &free_p);
11892 : 88 : arglist = build_tree_list_vec (user_args);
11893 : 88 : if (skip_first_for_error)
11894 : 0 : arglist = TREE_CHAIN (arglist);
11895 : 88 : auto_diagnostic_group d;
11896 : 176 : if (!any_strictly_viable (candidates))
11897 : 13 : error ("no matching function for call to %<%s(%A)%>",
11898 : : pretty_name, arglist);
11899 : : else
11900 : 75 : error ("call of overloaded %<%s(%A)%> is ambiguous",
11901 : : pretty_name, arglist);
11902 : 88 : print_z_candidates (location_of (name), candidates);
11903 : 88 : if (free_p)
11904 : 0 : free (pretty_name);
11905 : 88 : }
11906 : 184 : call = error_mark_node;
11907 : 184 : if (fn_p)
11908 : 84 : *fn_p = error_mark_node;
11909 : : }
11910 : : else
11911 : : {
11912 : 74342172 : fn = cand->fn;
11913 : 74342172 : call = NULL_TREE;
11914 : :
11915 : 74342172 : if (!(flags & LOOKUP_NONVIRTUAL)
11916 : 53012132 : && DECL_PURE_VIRTUAL_P (fn)
11917 : 195904 : && instance == current_class_ref
11918 : 74495016 : && (complain & tf_warning))
11919 : : {
11920 : : /* This is not an error, it is runtime undefined
11921 : : behavior. */
11922 : 152844 : if (!current_function_decl)
11923 : 3 : warning (0, "pure virtual %q#D called from "
11924 : : "non-static data member initializer", fn);
11925 : 152841 : else if (DECL_CONSTRUCTOR_P (current_function_decl)
11926 : 152841 : || DECL_DESTRUCTOR_P (current_function_decl))
11927 : 9 : warning (0, (DECL_CONSTRUCTOR_P (current_function_decl)
11928 : : ? G_("pure virtual %q#D called from constructor")
11929 : : : G_("pure virtual %q#D called from destructor")),
11930 : : fn);
11931 : : }
11932 : :
11933 : 89038580 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fn)
11934 : 119293260 : && !DECL_CONSTRUCTOR_P (fn)
11935 : 116764316 : && is_dummy_object (instance))
11936 : : {
11937 : 6850 : instance = maybe_resolve_dummy (instance, true);
11938 : 6850 : if (instance == error_mark_node)
11939 : : call = error_mark_node;
11940 : 6850 : else if (!is_dummy_object (instance))
11941 : : {
11942 : : /* We captured 'this' in the current lambda now that
11943 : : we know we really need it. */
11944 : 6730 : cand->first_arg = instance;
11945 : : }
11946 : 120 : else if (current_class_ptr && any_dependent_bases_p ())
11947 : : /* We can't tell until instantiation time whether we can use
11948 : : *this as the implicit object argument. */;
11949 : : else
11950 : : {
11951 : 85 : if (complain & tf_error)
11952 : 58 : error ("cannot call member function %qD without object",
11953 : : fn);
11954 : 85 : call = error_mark_node;
11955 : : }
11956 : : }
11957 : :
11958 : 74342172 : if (call != error_mark_node)
11959 : : {
11960 : : /* Now we know what function is being called. */
11961 : 74342087 : if (fn_p)
11962 : 19672607 : *fn_p = fn;
11963 : : /* Build the actual CALL_EXPR. */
11964 : 74342087 : call = build_over_call (cand, flags, complain);
11965 : :
11966 : : /* Suppress warnings for if (my_struct.operator= (x)) where
11967 : : my_struct is implicitly converted to bool. */
11968 : 74342087 : if (TREE_CODE (call) == MODIFY_EXPR)
11969 : 1787491 : suppress_warning (call, OPT_Wparentheses);
11970 : :
11971 : : /* In an expression of the form `a->f()' where `f' turns
11972 : : out to be a static member function, `a' is
11973 : : none-the-less evaluated. */
11974 : 74342087 : if (!is_dummy_object (instance))
11975 : 55371775 : call = keep_unused_object_arg (call, instance, fn);
11976 : 74342087 : if (call != error_mark_node
11977 : 147343236 : && DECL_DESTRUCTOR_P (cand->fn)
11978 : 94321326 : && !VOID_TYPE_P (TREE_TYPE (call)))
11979 : : /* An explicit call of the form "x->~X()" has type
11980 : : "void". However, on platforms where destructors
11981 : : return "this" (i.e., those where
11982 : : targetm.cxx.cdtor_returns_this is true), such calls
11983 : : will appear to have a return value of pointer type
11984 : : to the low-level call machinery. We do not want to
11985 : : change the low-level machinery, since we want to be
11986 : : able to optimize "delete f()" on such platforms as
11987 : : "operator delete(~X(f()))" (rather than generating
11988 : : "t = f(), ~X(t), operator delete (t)"). */
11989 : 11503475 : call = build_nop (void_type_node, call);
11990 : : }
11991 : : }
11992 : : }
11993 : :
11994 : 74354717 : if (processing_template_decl && call != error_mark_node)
11995 : : {
11996 : 7484811 : bool cast_to_void = false;
11997 : :
11998 : 7484811 : if (TREE_CODE (call) == COMPOUND_EXPR)
11999 : 6 : call = TREE_OPERAND (call, 1);
12000 : 7484805 : else if (TREE_CODE (call) == NOP_EXPR)
12001 : : {
12002 : 0 : cast_to_void = true;
12003 : 0 : call = TREE_OPERAND (call, 0);
12004 : : }
12005 : 7484811 : if (INDIRECT_REF_P (call))
12006 : 431310 : call = TREE_OPERAND (call, 0);
12007 : :
12008 : : /* Prune all but the selected function from the original overload
12009 : : set so that we can avoid some duplicate work at instantiation time. */
12010 : 7484811 : if (really_overloaded_fn (fns))
12011 : : {
12012 : 3502314 : if (DECL_TEMPLATE_INFO (fn)
12013 : 3502314 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
12014 : : {
12015 : : /* Use the selected template, not the specialization, so that
12016 : : this looks like an actual lookup result for sake of
12017 : : filter_memfn_lookup. */
12018 : :
12019 : 2169920 : if (OVL_SINGLE_P (fns))
12020 : : /* If the original overload set consists of a single function
12021 : : template, this isn't beneficial. */
12022 : 2136497 : goto skip_prune;
12023 : :
12024 : 33423 : fn = ovl_make (DECL_TI_TEMPLATE (fn));
12025 : 33423 : if (template_only)
12026 : 29124 : fn = lookup_template_function (fn, explicit_targs);
12027 : : }
12028 : 1365817 : orig_fns = copy_node (orig_fns);
12029 : 1365817 : BASELINK_FUNCTIONS (orig_fns) = fn;
12030 : 1365817 : BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (orig_fns) = true;
12031 : : }
12032 : :
12033 : 3982497 : skip_prune:
12034 : 7484811 : call = (build_min_non_dep_call_vec
12035 : 7484811 : (call,
12036 : 7484811 : build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
12037 : : orig_instance, orig_fns, NULL_TREE),
12038 : : orig_args));
12039 : 7484811 : SET_EXPR_LOCATION (call, input_location);
12040 : 7484811 : call = convert_from_reference (call);
12041 : 7484811 : if (cast_to_void)
12042 : 0 : call = build_nop (void_type_node, call);
12043 : : }
12044 : :
12045 : 74354717 : if (orig_args != NULL)
12046 : 7388003 : release_tree_vector (orig_args);
12047 : :
12048 : : return call;
12049 : 75772763 : }
12050 : :
12051 : : /* Returns true iff standard conversion sequence ICS1 is a proper
12052 : : subsequence of ICS2. */
12053 : :
12054 : : static bool
12055 : 62909731 : is_subseq (conversion *ics1, conversion *ics2)
12056 : : {
12057 : : /* We can assume that a conversion of the same code
12058 : : between the same types indicates a subsequence since we only get
12059 : : here if the types we are converting from are the same. */
12060 : :
12061 : 62909731 : while (ics1->kind == ck_rvalue
12062 : 79709185 : || ics1->kind == ck_lvalue)
12063 : 16799454 : ics1 = next_conversion (ics1);
12064 : :
12065 : : while (1)
12066 : : {
12067 : 91441245 : while (ics2->kind == ck_rvalue
12068 : 91441245 : || ics2->kind == ck_lvalue)
12069 : 16799454 : ics2 = next_conversion (ics2);
12070 : :
12071 : 74641791 : if (ics2->kind == ck_user
12072 : 74641791 : || !has_next (ics2->kind))
12073 : : /* At this point, ICS1 cannot be a proper subsequence of
12074 : : ICS2. We can get a USER_CONV when we are comparing the
12075 : : second standard conversion sequence of two user conversion
12076 : : sequences. */
12077 : : return false;
12078 : :
12079 : 11734929 : ics2 = next_conversion (ics2);
12080 : :
12081 : 11734929 : while (ics2->kind == ck_rvalue
12082 : 18992851 : || ics2->kind == ck_lvalue)
12083 : 7257922 : ics2 = next_conversion (ics2);
12084 : :
12085 : 11734929 : if (ics2->kind == ics1->kind
12086 : 2914 : && same_type_p (ics2->type, ics1->type)
12087 : 11737798 : && (ics1->kind == ck_identity
12088 : 2869 : || same_type_p (next_conversion (ics2)->type,
12089 : : next_conversion (ics1)->type)))
12090 : 2869 : return true;
12091 : : }
12092 : : }
12093 : :
12094 : : /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
12095 : : be any _TYPE nodes. */
12096 : :
12097 : : bool
12098 : 255825608 : is_properly_derived_from (tree derived, tree base)
12099 : : {
12100 : 255825608 : if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
12101 : : return false;
12102 : :
12103 : : /* We only allow proper derivation here. The DERIVED_FROM_P macro
12104 : : considers every class derived from itself. */
12105 : 243638752 : return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
12106 : 243638752 : && DERIVED_FROM_P (base, derived));
12107 : : }
12108 : :
12109 : : /* We build the ICS for an implicit object parameter as a pointer
12110 : : conversion sequence. However, such a sequence should be compared
12111 : : as if it were a reference conversion sequence. If ICS is the
12112 : : implicit conversion sequence for an implicit object parameter,
12113 : : modify it accordingly. */
12114 : :
12115 : : static void
12116 : 141093360 : maybe_handle_implicit_object (conversion **ics)
12117 : : {
12118 : 141093360 : if ((*ics)->this_p)
12119 : : {
12120 : : /* [over.match.funcs]
12121 : :
12122 : : For non-static member functions, the type of the
12123 : : implicit object parameter is "reference to cv X"
12124 : : where X is the class of which the function is a
12125 : : member and cv is the cv-qualification on the member
12126 : : function declaration. */
12127 : 8943695 : conversion *t = *ics;
12128 : 8943695 : tree reference_type;
12129 : :
12130 : : /* The `this' parameter is a pointer to a class type. Make the
12131 : : implicit conversion talk about a reference to that same class
12132 : : type. */
12133 : 8943695 : reference_type = TREE_TYPE (t->type);
12134 : 8943695 : reference_type = build_reference_type (reference_type);
12135 : :
12136 : 8943695 : if (t->kind == ck_qual)
12137 : 2403642 : t = next_conversion (t);
12138 : 8943695 : if (t->kind == ck_ptr)
12139 : 380994 : t = next_conversion (t);
12140 : 8943695 : t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
12141 : 8943695 : t = direct_reference_binding (reference_type, t);
12142 : 8943695 : t->this_p = 1;
12143 : 8943695 : t->rvaluedness_matches_p = 0;
12144 : 8943695 : *ics = t;
12145 : : }
12146 : 141093360 : }
12147 : :
12148 : : /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
12149 : : and return the initial reference binding conversion. Otherwise,
12150 : : leave *ICS unchanged and return NULL. */
12151 : :
12152 : : static conversion *
12153 : 141093360 : maybe_handle_ref_bind (conversion **ics)
12154 : : {
12155 : 141093360 : if ((*ics)->kind == ck_ref_bind)
12156 : : {
12157 : 36694770 : conversion *old_ics = *ics;
12158 : 36694770 : *ics = next_conversion (old_ics);
12159 : 36694770 : (*ics)->user_conv_p = old_ics->user_conv_p;
12160 : 36694770 : return old_ics;
12161 : : }
12162 : :
12163 : : return NULL;
12164 : : }
12165 : :
12166 : : /* Get the expression at the beginning of the conversion chain C. */
12167 : :
12168 : : static tree
12169 : 51 : conv_get_original_expr (conversion *c)
12170 : : {
12171 : 60 : for (; c; c = next_conversion (c))
12172 : 60 : if (c->kind == ck_identity || c->kind == ck_ambig || c->kind == ck_aggr)
12173 : 51 : return c->u.expr;
12174 : : return NULL_TREE;
12175 : : }
12176 : :
12177 : : /* Return a tree representing the number of elements initialized by the
12178 : : list-initialization C. The caller must check that C converts to an
12179 : : array type. */
12180 : :
12181 : : static tree
12182 : 126 : nelts_initialized_by_list_init (conversion *c)
12183 : : {
12184 : : /* If the array we're converting to has a dimension, we'll use that. */
12185 : 126 : if (TYPE_DOMAIN (c->type))
12186 : 84 : return array_type_nelts_top (c->type);
12187 : : else
12188 : : {
12189 : : /* Otherwise, we look at how many elements the constructor we're
12190 : : initializing from has. */
12191 : 42 : tree ctor = conv_get_original_expr (c);
12192 : 72 : return size_int (CONSTRUCTOR_NELTS (ctor));
12193 : : }
12194 : : }
12195 : :
12196 : : /* True iff C is a conversion that binds a reference or a pointer to
12197 : : an array of unknown bound. */
12198 : :
12199 : : static inline bool
12200 : 17988390 : conv_binds_to_array_of_unknown_bound (conversion *c)
12201 : : {
12202 : : /* ck_ref_bind won't have the reference stripped. */
12203 : 17988390 : tree type = non_reference (c->type);
12204 : : /* ck_qual won't have the pointer stripped. */
12205 : 17988390 : type = strip_pointer_operator (type);
12206 : 17988390 : return (TREE_CODE (type) == ARRAY_TYPE
12207 : 17988390 : && TYPE_DOMAIN (type) == NULL_TREE);
12208 : : }
12209 : :
12210 : : /* Compare two implicit conversion sequences according to the rules set out in
12211 : : [over.ics.rank]. Return values:
12212 : :
12213 : : 1: ics1 is better than ics2
12214 : : -1: ics2 is better than ics1
12215 : : 0: ics1 and ics2 are indistinguishable */
12216 : :
12217 : : static int
12218 : 70546726 : compare_ics (conversion *ics1, conversion *ics2)
12219 : : {
12220 : 70546726 : tree from_type1;
12221 : 70546726 : tree from_type2;
12222 : 70546726 : tree to_type1;
12223 : 70546726 : tree to_type2;
12224 : 70546726 : tree deref_from_type1 = NULL_TREE;
12225 : 70546726 : tree deref_from_type2 = NULL_TREE;
12226 : 70546726 : tree deref_to_type1 = NULL_TREE;
12227 : 70546726 : tree deref_to_type2 = NULL_TREE;
12228 : 70546726 : conversion_rank rank1, rank2;
12229 : :
12230 : : /* REF_BINDING is nonzero if the result of the conversion sequence
12231 : : is a reference type. In that case REF_CONV is the reference
12232 : : binding conversion. */
12233 : 70546726 : conversion *ref_conv1;
12234 : 70546726 : conversion *ref_conv2;
12235 : :
12236 : : /* Compare badness before stripping the reference conversion. */
12237 : 70546726 : if (ics1->bad_p > ics2->bad_p)
12238 : : return -1;
12239 : 70546709 : else if (ics1->bad_p < ics2->bad_p)
12240 : : return 1;
12241 : :
12242 : : /* Handle implicit object parameters. */
12243 : 70546680 : maybe_handle_implicit_object (&ics1);
12244 : 70546680 : maybe_handle_implicit_object (&ics2);
12245 : :
12246 : : /* Handle reference parameters. */
12247 : 70546680 : ref_conv1 = maybe_handle_ref_bind (&ics1);
12248 : 70546680 : ref_conv2 = maybe_handle_ref_bind (&ics2);
12249 : :
12250 : : /* List-initialization sequence L1 is a better conversion sequence than
12251 : : list-initialization sequence L2 if L1 converts to
12252 : : std::initializer_list<X> for some X and L2 does not. */
12253 : 70546680 : if (ics1->kind == ck_list && ics2->kind != ck_list)
12254 : : return 1;
12255 : 70546217 : if (ics2->kind == ck_list && ics1->kind != ck_list)
12256 : : return -1;
12257 : :
12258 : : /* [over.ics.rank]
12259 : :
12260 : : When comparing the basic forms of implicit conversion sequences (as
12261 : : defined in _over.best.ics_)
12262 : :
12263 : : --a standard conversion sequence (_over.ics.scs_) is a better
12264 : : conversion sequence than a user-defined conversion sequence
12265 : : or an ellipsis conversion sequence, and
12266 : :
12267 : : --a user-defined conversion sequence (_over.ics.user_) is a
12268 : : better conversion sequence than an ellipsis conversion sequence
12269 : : (_over.ics.ellipsis_). */
12270 : : /* Use BAD_CONVERSION_RANK because we already checked for a badness
12271 : : mismatch. If both ICS are bad, we try to make a decision based on
12272 : : what would have happened if they'd been good. This is not an
12273 : : extension, we'll still give an error when we build up the call; this
12274 : : just helps us give a more helpful error message. */
12275 : 70545974 : rank1 = BAD_CONVERSION_RANK (ics1);
12276 : 70545974 : rank2 = BAD_CONVERSION_RANK (ics2);
12277 : :
12278 : 70545974 : if (rank1 > rank2)
12279 : : return -1;
12280 : 60584526 : else if (rank1 < rank2)
12281 : : return 1;
12282 : :
12283 : 31753304 : if (ics1->ellipsis_p)
12284 : : /* Both conversions are ellipsis conversions. */
12285 : : return 0;
12286 : :
12287 : : /* User-defined conversion sequence U1 is a better conversion sequence
12288 : : than another user-defined conversion sequence U2 if they contain the
12289 : : same user-defined conversion operator or constructor and if the sec-
12290 : : ond standard conversion sequence of U1 is better than the second
12291 : : standard conversion sequence of U2. */
12292 : :
12293 : : /* Handle list-conversion with the same code even though it isn't always
12294 : : ranked as a user-defined conversion and it doesn't have a second
12295 : : standard conversion sequence; it will still have the desired effect.
12296 : : Specifically, we need to do the reference binding comparison at the
12297 : : end of this function. */
12298 : :
12299 : 31753268 : if (ics1->user_conv_p || ics1->kind == ck_list
12300 : 31406619 : || ics1->kind == ck_aggr || ics2->kind == ck_aggr)
12301 : : {
12302 : 346718 : conversion *t1 = strip_standard_conversion (ics1);
12303 : 346718 : conversion *t2 = strip_standard_conversion (ics2);
12304 : :
12305 : 346718 : if (!t1 || !t2 || t1->kind != t2->kind)
12306 : : return 0;
12307 : 346699 : else if (t1->kind == ck_user)
12308 : : {
12309 : 341415 : tree f1 = t1->cand ? t1->cand->fn : t1->type;
12310 : 341415 : tree f2 = t2->cand ? t2->cand->fn : t2->type;
12311 : 341415 : if (f1 != f2)
12312 : : return 0;
12313 : : }
12314 : : /* List-initialization sequence L1 is a better conversion sequence than
12315 : : list-initialization sequence L2 if
12316 : :
12317 : : -- L1 and L2 convert to arrays of the same element type, and either
12318 : : the number of elements n1 initialized by L1 is less than the number
12319 : : of elements n2 initialized by L2, or n1=n2 and L2 converts to an array
12320 : : of unknown bound and L1 does not. (Added in CWG 1307 and extended by
12321 : : P0388R4.) */
12322 : 5284 : else if (t1->kind == ck_aggr
12323 : 4995 : && TREE_CODE (t1->type) == ARRAY_TYPE
12324 : 66 : && TREE_CODE (t2->type) == ARRAY_TYPE
12325 : 5350 : && same_type_p (TREE_TYPE (t1->type), TREE_TYPE (t2->type)))
12326 : : {
12327 : 63 : tree n1 = nelts_initialized_by_list_init (t1);
12328 : 63 : tree n2 = nelts_initialized_by_list_init (t2);
12329 : 63 : if (tree_int_cst_lt (n1, n2))
12330 : : return 1;
12331 : 24 : else if (tree_int_cst_lt (n2, n1))
12332 : : return -1;
12333 : : /* The n1 == n2 case. */
12334 : 24 : bool c1 = conv_binds_to_array_of_unknown_bound (t1);
12335 : 24 : bool c2 = conv_binds_to_array_of_unknown_bound (t2);
12336 : 24 : if (c1 && !c2)
12337 : : return -1;
12338 : 6 : else if (!c1 && c2)
12339 : : return 1;
12340 : : else
12341 : : return 0;
12342 : : }
12343 : : else
12344 : : {
12345 : : /* For ambiguous or aggregate conversions, use the target type as
12346 : : a proxy for the conversion function. */
12347 : 5221 : if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
12348 : : return 0;
12349 : : }
12350 : :
12351 : : /* We can just fall through here, after setting up
12352 : : FROM_TYPE1 and FROM_TYPE2. */
12353 : 344623 : from_type1 = t1->type;
12354 : 344623 : from_type2 = t2->type;
12355 : 344623 : }
12356 : : else
12357 : : {
12358 : : conversion *t1;
12359 : : conversion *t2;
12360 : :
12361 : : /* We're dealing with two standard conversion sequences.
12362 : :
12363 : : [over.ics.rank]
12364 : :
12365 : : Standard conversion sequence S1 is a better conversion
12366 : : sequence than standard conversion sequence S2 if
12367 : :
12368 : : --S1 is a proper subsequence of S2 (comparing the conversion
12369 : : sequences in the canonical form defined by _over.ics.scs_,
12370 : : excluding any Lvalue Transformation; the identity
12371 : : conversion sequence is considered to be a subsequence of
12372 : : any non-identity conversion sequence */
12373 : :
12374 : : t1 = ics1;
12375 : 49349321 : while (t1->kind != ck_identity)
12376 : 17942771 : t1 = next_conversion (t1);
12377 : 31406550 : from_type1 = t1->type;
12378 : :
12379 : 31406550 : t2 = ics2;
12380 : 49304085 : while (t2->kind != ck_identity)
12381 : 17897535 : t2 = next_conversion (t2);
12382 : 31406550 : from_type2 = t2->type;
12383 : : }
12384 : :
12385 : : /* One sequence can only be a subsequence of the other if they start with
12386 : : the same type. They can start with different types when comparing the
12387 : : second standard conversion sequence in two user-defined conversion
12388 : : sequences. */
12389 : 31751173 : if (same_type_p (from_type1, from_type2))
12390 : : {
12391 : 31456270 : if (is_subseq (ics1, ics2))
12392 : : return 1;
12393 : 31453461 : if (is_subseq (ics2, ics1))
12394 : : return -1;
12395 : : }
12396 : :
12397 : : /* [over.ics.rank]
12398 : :
12399 : : Or, if not that,
12400 : :
12401 : : --the rank of S1 is better than the rank of S2 (by the rules
12402 : : defined below):
12403 : :
12404 : : Standard conversion sequences are ordered by their ranks: an Exact
12405 : : Match is a better conversion than a Promotion, which is a better
12406 : : conversion than a Conversion.
12407 : :
12408 : : Two conversion sequences with the same rank are indistinguishable
12409 : : unless one of the following rules applies:
12410 : :
12411 : : --A conversion that does not a convert a pointer, pointer to member,
12412 : : or std::nullptr_t to bool is better than one that does.
12413 : :
12414 : : The ICS_STD_RANK automatically handles the pointer-to-bool rule,
12415 : : so that we do not have to check it explicitly. */
12416 : 31748304 : if (ics1->rank < ics2->rank)
12417 : : return 1;
12418 : 31748224 : else if (ics2->rank < ics1->rank)
12419 : : return -1;
12420 : :
12421 : 31748224 : to_type1 = ics1->type;
12422 : 31748224 : to_type2 = ics2->type;
12423 : :
12424 : : /* A conversion from scalar arithmetic type to complex is worse than a
12425 : : conversion between scalar arithmetic types. */
12426 : 31748224 : if (same_type_p (from_type1, from_type2)
12427 : 31453321 : && ARITHMETIC_TYPE_P (from_type1)
12428 : 8419669 : && ARITHMETIC_TYPE_P (to_type1)
12429 : 8419538 : && ARITHMETIC_TYPE_P (to_type2)
12430 : 31748224 : && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
12431 : 8419522 : != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
12432 : : {
12433 : 76 : if (TREE_CODE (to_type1) == COMPLEX_TYPE)
12434 : : return -1;
12435 : : else
12436 : : return 1;
12437 : : }
12438 : :
12439 : 31748148 : {
12440 : : /* A conversion in either direction between floating-point type FP1 and
12441 : : floating-point type FP2 is better than a conversion in the same
12442 : : direction between FP1 and arithmetic type T3 if
12443 : : - the floating-point conversion rank of FP1 is equal to the rank of
12444 : : FP2, and
12445 : : - T3 is not a floating-point type, or T3 is a floating-point type
12446 : : whose rank is not equal to the rank of FP1, or the floating-point
12447 : : conversion subrank of FP2 is greater than the subrank of T3. */
12448 : 31748148 : tree fp1 = from_type1;
12449 : 31748148 : tree fp2 = to_type1;
12450 : 31748148 : tree fp3 = from_type2;
12451 : 31748148 : tree t3 = to_type2;
12452 : 31748148 : int ret = 1;
12453 : 31748148 : if (TYPE_MAIN_VARIANT (fp2) == TYPE_MAIN_VARIANT (t3))
12454 : : {
12455 : 26755846 : std::swap (fp1, fp2);
12456 : 26755846 : std::swap (fp3, t3);
12457 : : }
12458 : 31748148 : if (TYPE_MAIN_VARIANT (fp1) == TYPE_MAIN_VARIANT (fp3)
12459 : 31748047 : && SCALAR_FLOAT_TYPE_P (fp1)
12460 : : /* Only apply this rule if at least one of the 3 types is
12461 : : extended floating-point type, otherwise keep them as
12462 : : before for compatibility reasons with types like __float128.
12463 : : float, double and long double alone have different conversion
12464 : : ranks and so when just those 3 types are involved, this
12465 : : rule doesn't trigger. */
12466 : 36023070 : && (extended_float_type_p (fp1)
12467 : 4194362 : || (SCALAR_FLOAT_TYPE_P (fp2) && extended_float_type_p (fp2))
12468 : 4144201 : || (SCALAR_FLOAT_TYPE_P (t3) && extended_float_type_p (t3))))
12469 : : {
12470 : 130721 : if (TREE_CODE (fp2) != REAL_TYPE)
12471 : : {
12472 : 39871 : ret = -ret;
12473 : 39871 : std::swap (fp2, t3);
12474 : : }
12475 : 130721 : if (SCALAR_FLOAT_TYPE_P (fp2))
12476 : : {
12477 : : /* cp_compare_floating_point_conversion_ranks returns -1, 0 or 1
12478 : : if the conversion rank is equal (-1 or 1 if the subrank is
12479 : : different). */
12480 : 108939 : if (IN_RANGE (cp_compare_floating_point_conversion_ranks (fp1,
12481 : : fp2),
12482 : : -1, 1))
12483 : : {
12484 : : /* Conversion ranks of FP1 and FP2 are equal. */
12485 : 41660 : if (TREE_CODE (t3) != REAL_TYPE
12486 : 41660 : || !IN_RANGE (cp_compare_floating_point_conversion_ranks
12487 : : (fp1, t3),
12488 : : -1, 1))
12489 : : /* FP1 <-> FP2 conversion is better. */
12490 : 41380 : return ret;
12491 : 280 : int c = cp_compare_floating_point_conversion_ranks (fp2, t3);
12492 : 280 : gcc_assert (IN_RANGE (c, -1, 1));
12493 : 280 : if (c == 1)
12494 : : /* Conversion subrank of FP2 is greater than subrank of T3.
12495 : : FP1 <-> FP2 conversion is better. */
12496 : : return ret;
12497 : 280 : else if (c == -1)
12498 : : /* Conversion subrank of FP2 is less than subrank of T3.
12499 : : FP1 <-> T3 conversion is better. */
12500 : 0 : return -ret;
12501 : : }
12502 : 67279 : else if (SCALAR_FLOAT_TYPE_P (t3)
12503 : 67279 : && IN_RANGE (cp_compare_floating_point_conversion_ranks
12504 : : (fp1, t3),
12505 : : -1, 1))
12506 : : /* Conversion ranks of FP1 and FP2 are not equal, conversion
12507 : : ranks of FP1 and T3 are equal.
12508 : : FP1 <-> T3 conversion is better. */
12509 : 11423 : return -ret;
12510 : : }
12511 : : }
12512 : : }
12513 : :
12514 : 31695345 : if (TYPE_PTR_P (from_type1)
12515 : 6286200 : && TYPE_PTR_P (from_type2)
12516 : 6286184 : && TYPE_PTR_P (to_type1)
12517 : 6286160 : && TYPE_PTR_P (to_type2))
12518 : : {
12519 : 6286160 : deref_from_type1 = TREE_TYPE (from_type1);
12520 : 6286160 : deref_from_type2 = TREE_TYPE (from_type2);
12521 : 6286160 : deref_to_type1 = TREE_TYPE (to_type1);
12522 : 6286160 : deref_to_type2 = TREE_TYPE (to_type2);
12523 : : }
12524 : : /* The rules for pointers to members A::* are just like the rules
12525 : : for pointers A*, except opposite: if B is derived from A then
12526 : : A::* converts to B::*, not vice versa. For that reason, we
12527 : : switch the from_ and to_ variables here. */
12528 : 52 : else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2)
12529 : 52 : && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2))
12530 : 25409185 : || (TYPE_PTRMEMFUNC_P (from_type1)
12531 : 403 : && TYPE_PTRMEMFUNC_P (from_type2)
12532 : 403 : && TYPE_PTRMEMFUNC_P (to_type1)
12533 : 403 : && TYPE_PTRMEMFUNC_P (to_type2)))
12534 : : {
12535 : 455 : deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
12536 : 455 : deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
12537 : 455 : deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
12538 : 455 : deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
12539 : : }
12540 : :
12541 : 6286615 : if (deref_from_type1 != NULL_TREE
12542 : 6286615 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
12543 : 142035 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
12544 : : {
12545 : : /* This was one of the pointer or pointer-like conversions.
12546 : :
12547 : : [over.ics.rank]
12548 : :
12549 : : --If class B is derived directly or indirectly from class A,
12550 : : conversion of B* to A* is better than conversion of B* to
12551 : : void*, and conversion of A* to void* is better than
12552 : : conversion of B* to void*. */
12553 : 142035 : if (VOID_TYPE_P (deref_to_type1)
12554 : 51 : && VOID_TYPE_P (deref_to_type2))
12555 : : {
12556 : 8 : if (is_properly_derived_from (deref_from_type1,
12557 : : deref_from_type2))
12558 : : return -1;
12559 : 8 : else if (is_properly_derived_from (deref_from_type2,
12560 : : deref_from_type1))
12561 : : return 1;
12562 : : }
12563 : 142027 : else if (VOID_TYPE_P (deref_to_type1)
12564 : 141984 : || VOID_TYPE_P (deref_to_type2))
12565 : : {
12566 : 47 : if (same_type_p (deref_from_type1, deref_from_type2))
12567 : : {
12568 : 47 : if (VOID_TYPE_P (deref_to_type2))
12569 : : {
12570 : 4 : if (is_properly_derived_from (deref_from_type1,
12571 : : deref_to_type1))
12572 : : return 1;
12573 : : }
12574 : : /* We know that DEREF_TO_TYPE1 is `void' here. */
12575 : 43 : else if (is_properly_derived_from (deref_from_type1,
12576 : : deref_to_type2))
12577 : : return -1;
12578 : : }
12579 : : }
12580 : 141980 : else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
12581 : 141980 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
12582 : : {
12583 : : /* [over.ics.rank]
12584 : :
12585 : : --If class B is derived directly or indirectly from class A
12586 : : and class C is derived directly or indirectly from B,
12587 : :
12588 : : --conversion of C* to B* is better than conversion of C* to
12589 : : A*,
12590 : :
12591 : : --conversion of B* to A* is better than conversion of C* to
12592 : : A* */
12593 : 141980 : if (same_type_p (deref_from_type1, deref_from_type2))
12594 : : {
12595 : 141974 : if (is_properly_derived_from (deref_to_type1,
12596 : : deref_to_type2))
12597 : : return 1;
12598 : 141974 : else if (is_properly_derived_from (deref_to_type2,
12599 : : deref_to_type1))
12600 : : return -1;
12601 : : }
12602 : 6 : else if (same_type_p (deref_to_type1, deref_to_type2))
12603 : : {
12604 : 6 : if (is_properly_derived_from (deref_from_type2,
12605 : : deref_from_type1))
12606 : : return 1;
12607 : 0 : else if (is_properly_derived_from (deref_from_type1,
12608 : : deref_from_type2))
12609 : : return -1;
12610 : : }
12611 : : }
12612 : : }
12613 : 63106620 : else if (CLASS_TYPE_P (non_reference (from_type1))
12614 : 46929814 : && same_type_p (from_type1, from_type2))
12615 : : {
12616 : 15116979 : tree from = non_reference (from_type1);
12617 : :
12618 : : /* [over.ics.rank]
12619 : :
12620 : : --binding of an expression of type C to a reference of type
12621 : : B& is better than binding an expression of type C to a
12622 : : reference of type A&
12623 : :
12624 : : --conversion of C to B is better than conversion of C to A, */
12625 : 15116979 : if (is_properly_derived_from (from, to_type1)
12626 : 15116979 : && is_properly_derived_from (from, to_type2))
12627 : : {
12628 : 721522 : if (is_properly_derived_from (to_type1, to_type2))
12629 : : return 1;
12630 : 719959 : else if (is_properly_derived_from (to_type2, to_type1))
12631 : : return -1;
12632 : : }
12633 : : }
12634 : 32872662 : else if (CLASS_TYPE_P (non_reference (to_type1))
12635 : 16695856 : && same_type_p (to_type1, to_type2))
12636 : : {
12637 : 5 : tree to = non_reference (to_type1);
12638 : :
12639 : : /* [over.ics.rank]
12640 : :
12641 : : --binding of an expression of type B to a reference of type
12642 : : A& is better than binding an expression of type C to a
12643 : : reference of type A&,
12644 : :
12645 : : --conversion of B to A is better than conversion of C to A */
12646 : 5 : if (is_properly_derived_from (from_type1, to)
12647 : 5 : && is_properly_derived_from (from_type2, to))
12648 : : {
12649 : 3 : if (is_properly_derived_from (from_type2, from_type1))
12650 : : return 1;
12651 : 3 : else if (is_properly_derived_from (from_type1, from_type2))
12652 : : return -1;
12653 : : }
12654 : : }
12655 : :
12656 : : /* [over.ics.rank]
12657 : :
12658 : : --S1 and S2 differ only in their qualification conversion and yield
12659 : : similar types T1 and T2 (_conv.qual_), respectively, and the cv-
12660 : : qualification signature of type T1 is a proper subset of the cv-
12661 : : qualification signature of type T2 */
12662 : 31651000 : if (ics1->kind == ck_qual
12663 : 311 : && ics2->kind == ck_qual
12664 : 31651311 : && same_type_p (from_type1, from_type2))
12665 : : {
12666 : 311 : int result = comp_cv_qual_signature (to_type1, to_type2);
12667 : 311 : if (result != 0)
12668 : : return result;
12669 : : }
12670 : :
12671 : : /* [over.ics.rank]
12672 : :
12673 : : --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
12674 : : to an implicit object parameter of a non-static member function
12675 : : declared without a ref-qualifier, and either S1 binds an lvalue
12676 : : reference to an lvalue and S2 binds an rvalue reference or S1 binds an
12677 : : rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x
12678 : : draft standard, 13.3.3.2)
12679 : :
12680 : : --S1 and S2 are reference bindings (_dcl.init.ref_), and the
12681 : : types to which the references refer are the same type except for
12682 : : top-level cv-qualifiers, and the type to which the reference
12683 : : initialized by S2 refers is more cv-qualified than the type to
12684 : : which the reference initialized by S1 refers.
12685 : :
12686 : : DR 1328 [over.match.best]: the context is an initialization by
12687 : : conversion function for direct reference binding (13.3.1.6) of a
12688 : : reference to function type, the return type of F1 is the same kind of
12689 : : reference (i.e. lvalue or rvalue) as the reference being initialized,
12690 : : and the return type of F2 is not. */
12691 : :
12692 : 31650938 : if (ref_conv1 && ref_conv2)
12693 : : {
12694 : 10686013 : if (!ref_conv1->this_p && !ref_conv2->this_p
12695 : 10589551 : && (ref_conv1->rvaluedness_matches_p
12696 : 10589551 : != ref_conv2->rvaluedness_matches_p)
12697 : 21279591 : && (same_type_p (ref_conv1->type, ref_conv2->type)
12698 : 6142866 : || (TYPE_REF_IS_RVALUE (ref_conv1->type)
12699 : 6142866 : != TYPE_REF_IS_RVALUE (ref_conv2->type))))
12700 : : {
12701 : 6140714 : if (ref_conv1->bad_p
12702 : 6140714 : && !same_type_p (TREE_TYPE (ref_conv1->type),
12703 : : TREE_TYPE (ref_conv2->type)))
12704 : : /* Don't prefer a bad conversion that drops cv-quals to a bad
12705 : : conversion with the wrong rvalueness. */
12706 : : return 0;
12707 : 6139255 : return (ref_conv1->rvaluedness_matches_p
12708 : 6139255 : - ref_conv2->rvaluedness_matches_p);
12709 : : }
12710 : :
12711 : 8996008 : if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
12712 : : {
12713 : : /* Per P0388R4:
12714 : :
12715 : : void f (int(&)[]), // (1)
12716 : : f (int(&)[1]), // (2)
12717 : : f (int*); // (3)
12718 : :
12719 : : (2) is better than (1), but (3) should be equal to (1) and to
12720 : : (2). For that reason we don't use ck_qual for (1) which would
12721 : : give it the cr_exact rank while (3) remains ck_identity.
12722 : : Therefore we compare (1) and (2) here. For (1) we'll have
12723 : :
12724 : : ck_ref_bind <- ck_identity
12725 : : int[] & int[1]
12726 : :
12727 : : so to handle this we must look at ref_conv. */
12728 : 8993849 : bool c1 = conv_binds_to_array_of_unknown_bound (ref_conv1);
12729 : 8993849 : bool c2 = conv_binds_to_array_of_unknown_bound (ref_conv2);
12730 : 8993849 : if (c1 && !c2)
12731 : : return -1;
12732 : 8993843 : else if (!c1 && c2)
12733 : : return 1;
12734 : :
12735 : 8993843 : int q1 = cp_type_quals (TREE_TYPE (ref_conv1->type));
12736 : 8993843 : int q2 = cp_type_quals (TREE_TYPE (ref_conv2->type));
12737 : 8993843 : if (ref_conv1->bad_p)
12738 : : {
12739 : : /* Prefer the one that drops fewer cv-quals. */
12740 : 1606 : tree ftype = next_conversion (ref_conv1)->type;
12741 : 1606 : int fquals = cp_type_quals (ftype);
12742 : 1606 : q1 ^= fquals;
12743 : 1606 : q2 ^= fquals;
12744 : : }
12745 : 8993843 : return comp_cv_qualification (q2, q1);
12746 : : }
12747 : : }
12748 : :
12749 : : /* [over.ics.rank]
12750 : :
12751 : : Per CWG 1601:
12752 : : -- A conversion that promotes an enumeration whose underlying type
12753 : : is fixed to its underlying type is better than one that promotes to
12754 : : the promoted underlying type, if the two are different. */
12755 : 16516375 : if (ics1->rank == cr_promotion
12756 : 136 : && ics2->rank == cr_promotion
12757 : 136 : && UNSCOPED_ENUM_P (from_type1)
12758 : 23 : && ENUM_FIXED_UNDERLYING_TYPE_P (from_type1)
12759 : 16516395 : && same_type_p (from_type1, from_type2))
12760 : : {
12761 : 20 : tree utype = ENUM_UNDERLYING_TYPE (from_type1);
12762 : 20 : tree prom = type_promotes_to (from_type1);
12763 : 20 : if (!same_type_p (utype, prom))
12764 : : {
12765 : 12 : if (same_type_p (to_type1, utype)
12766 : 12 : && same_type_p (to_type2, prom))
12767 : : return 1;
12768 : 6 : else if (same_type_p (to_type2, utype)
12769 : 6 : && same_type_p (to_type1, prom))
12770 : : return -1;
12771 : : }
12772 : : }
12773 : :
12774 : : /* Neither conversion sequence is better than the other. */
12775 : : return 0;
12776 : : }
12777 : :
12778 : : /* The source type for this standard conversion sequence. */
12779 : :
12780 : : static tree
12781 : 9 : source_type (conversion *t)
12782 : : {
12783 : 9 : return strip_standard_conversion (t)->type;
12784 : : }
12785 : :
12786 : : /* Note a warning about preferring WINNER to LOSER. We do this by storing
12787 : : a pointer to LOSER and re-running joust to produce the warning if WINNER
12788 : : is actually used. */
12789 : :
12790 : : static void
12791 : 197 : add_warning (struct z_candidate *winner, struct z_candidate *loser)
12792 : : {
12793 : 197 : candidate_warning *cw = (candidate_warning *)
12794 : 0 : conversion_obstack_alloc (sizeof (candidate_warning));
12795 : 197 : cw->loser = loser;
12796 : 197 : cw->next = winner->warnings;
12797 : 197 : winner->warnings = cw;
12798 : 197 : }
12799 : :
12800 : : /* CAND is a constructor candidate in joust in C++17 and up. If it copies a
12801 : : prvalue returned from a conversion function, return true. Otherwise, return
12802 : : false. */
12803 : :
12804 : : static bool
12805 : 440388 : joust_maybe_elide_copy (z_candidate *cand)
12806 : : {
12807 : 440388 : tree fn = cand->fn;
12808 : 1105333 : if (!DECL_COPY_CONSTRUCTOR_P (fn) && !DECL_MOVE_CONSTRUCTOR_P (fn))
12809 : 224481 : return false;
12810 : 215907 : conversion *conv = cand->convs[0];
12811 : 215907 : if (conv->kind == ck_ambig)
12812 : : return false;
12813 : 215905 : gcc_checking_assert (conv->kind == ck_ref_bind);
12814 : 215905 : conv = next_conversion (conv);
12815 : 215905 : if (conv->kind == ck_user && !TYPE_REF_P (conv->type))
12816 : : {
12817 : 99 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
12818 : : (conv->type, DECL_CONTEXT (fn)));
12819 : 99 : z_candidate *uc = conv->cand;
12820 : 99 : if (DECL_CONV_FN_P (uc->fn))
12821 : : return true;
12822 : : }
12823 : : return false;
12824 : : }
12825 : :
12826 : : /* Return the class that CAND's implicit object parameter refers to. */
12827 : :
12828 : : static tree
12829 : 68897 : class_of_implicit_object (z_candidate *cand)
12830 : : {
12831 : 68897 : if (!DECL_IOBJ_MEMBER_FUNCTION_P (cand->fn))
12832 : : return NULL_TREE;
12833 : :
12834 : : /* "For conversion functions that are implicit object member functions,
12835 : : the function is considered to be a member of the class of the implied
12836 : : object argument for the purpose of defining the type of the implicit
12837 : : object parameter." */
12838 : 68897 : if (DECL_CONV_FN_P (cand->fn))
12839 : 0 : return TYPE_MAIN_VARIANT (TREE_TYPE (cand->first_arg));
12840 : :
12841 : : /* "For non-conversion functions that are implicit object member
12842 : : functions nominated by a using-declaration in a derived class, the
12843 : : function is considered to be a member of the derived class for the
12844 : : purpose of defining the type of the implicit object parameter."
12845 : :
12846 : : That derived class is reflected in the conversion_path binfo. */
12847 : 68897 : return BINFO_TYPE (cand->conversion_path);
12848 : : }
12849 : :
12850 : : /* Return whether the first parameter of C1 matches the second parameter
12851 : : of C2. */
12852 : :
12853 : : static bool
12854 : 131081 : reversed_match (z_candidate *c1, z_candidate *c2)
12855 : : {
12856 : 131081 : tree fn1 = c1->fn;
12857 : 131081 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (c2->fn));
12858 : 131081 : tree parm2 = TREE_VALUE (TREE_CHAIN (parms2));
12859 : 131081 : if (DECL_IOBJ_MEMBER_FUNCTION_P (fn1))
12860 : : {
12861 : 68897 : tree ctx = class_of_implicit_object (c1);
12862 : 68897 : return iobj_parm_corresponds_to (fn1, parm2, ctx);
12863 : : }
12864 : : else
12865 : : {
12866 : 62184 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn1));
12867 : 62184 : tree parm1 = TREE_VALUE (parms1);
12868 : 62184 : return same_type_p (parm1, parm2);
12869 : : }
12870 : : }
12871 : :
12872 : : /* True if the defining declarations of the two candidates have equivalent
12873 : : parameters. MATCH_KIND controls whether we're trying to compare the
12874 : : original declarations (for a warning) or the actual candidates. */
12875 : :
12876 : : enum class pmatch { original, current };
12877 : :
12878 : : static bool
12879 : 1642046 : cand_parms_match (z_candidate *c1, z_candidate *c2, pmatch match_kind)
12880 : : {
12881 : 1642046 : tree fn1 = c1->fn;
12882 : 1642046 : tree fn2 = c2->fn;
12883 : 1642046 : bool reversed = (match_kind == pmatch::current
12884 : 1642046 : && c1->reversed () != c2->reversed ());
12885 : 1642046 : if (fn1 == fn2 && !reversed)
12886 : : return true;
12887 : 1642013 : if (identifier_p (fn1) || identifier_p (fn2))
12888 : : return false;
12889 : 1642009 : if (match_kind == pmatch::original)
12890 : : {
12891 : : /* We don't look at c1->template_decl because that's only set for
12892 : : primary templates, not e.g. non-template member functions of
12893 : : class templates. */
12894 : 13 : tree t1 = most_general_template (fn1);
12895 : 13 : tree t2 = most_general_template (fn2);
12896 : 13 : if (t1 || t2)
12897 : : {
12898 : 8 : if (!t1 || !t2)
12899 : : return false;
12900 : 8 : if (t1 == t2)
12901 : : return true;
12902 : 0 : fn1 = DECL_TEMPLATE_RESULT (t1);
12903 : 0 : fn2 = DECL_TEMPLATE_RESULT (t2);
12904 : : }
12905 : : }
12906 : :
12907 : 1642001 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn1));
12908 : 1642001 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (fn2));
12909 : :
12910 : 3236765 : if (DECL_FUNCTION_MEMBER_P (fn1)
12911 : 1642397 : && DECL_FUNCTION_MEMBER_P (fn2))
12912 : : {
12913 : 47590 : tree base1 = DECL_CONTEXT (strip_inheriting_ctors (fn1));
12914 : 47590 : tree base2 = DECL_CONTEXT (strip_inheriting_ctors (fn2));
12915 : 47590 : if (base1 != base2)
12916 : 35026 : return false;
12917 : :
12918 : 47476 : if (reversed)
12919 : 34910 : return (reversed_match (c1, c2)
12920 : 34910 : && reversed_match (c2, c1));
12921 : :
12922 : : /* Use object_parms_correspond to simplify comparing iobj/xobj/static
12923 : : member functions. */
12924 : 12566 : if (!object_parms_correspond (fn1, fn2, base1))
12925 : : return false;
12926 : :
12927 : : /* We just compared the object parameters, if they don't correspond
12928 : : we already returned false. */
12929 : 37692 : auto skip_parms = [] (tree fn, tree parms)
12930 : : {
12931 : 25128 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
12932 : 680 : return TREE_CHAIN (parms);
12933 : : else
12934 : 24448 : return skip_artificial_parms_for (fn, parms);
12935 : : };
12936 : 12564 : parms1 = skip_parms (fn1, parms1);
12937 : 12564 : parms2 = skip_parms (fn2, parms2);
12938 : : }
12939 : 1594411 : else if (reversed)
12940 : 31074 : return (reversed_match (c1, c2)
12941 : 31074 : && reversed_match (c2, c1));
12942 : 1575901 : return compparms (parms1, parms2);
12943 : : }
12944 : :
12945 : : /* True iff FN is a copy or move constructor or assignment operator. */
12946 : :
12947 : : static bool
12948 : 16589454 : sfk_copy_or_move (tree fn)
12949 : : {
12950 : 16589454 : if (TREE_CODE (fn) != FUNCTION_DECL)
12951 : : return false;
12952 : 16589373 : special_function_kind sfk = special_function_p (fn);
12953 : 16589373 : return sfk >= sfk_copy_constructor && sfk <= sfk_move_assignment;
12954 : : }
12955 : :
12956 : : /* Compare two candidates for overloading as described in
12957 : : [over.match.best]. Return values:
12958 : :
12959 : : 1: cand1 is better than cand2
12960 : : -1: cand2 is better than cand1
12961 : : 0: cand1 and cand2 are indistinguishable */
12962 : :
12963 : : static int
12964 : 62575923 : joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
12965 : : tsubst_flags_t complain)
12966 : : {
12967 : 62575923 : int winner = 0;
12968 : 62575923 : int off1 = 0, off2 = 0;
12969 : 62575923 : size_t i;
12970 : 62575923 : size_t len;
12971 : :
12972 : : /* Candidates that involve bad conversions are always worse than those
12973 : : that don't. */
12974 : 62575923 : if (cand1->viable > cand2->viable)
12975 : : return 1;
12976 : 51582609 : if (cand1->viable < cand2->viable)
12977 : : return -1;
12978 : :
12979 : : /* If we have two pseudo-candidates for conversions to the same type,
12980 : : or two candidates for the same function, arbitrarily pick one. */
12981 : 51582609 : if (cand1->fn == cand2->fn
12982 : 1869539 : && cand1->reversed () == cand2->reversed ()
12983 : 52990809 : && (IS_TYPE_OR_DECL_P (cand1->fn)))
12984 : : return 1;
12985 : :
12986 : : /* Prefer a non-deleted function over an implicitly deleted move
12987 : : constructor or assignment operator. This differs slightly from the
12988 : : wording for issue 1402 (which says the move op is ignored by overload
12989 : : resolution), but this way produces better error messages. */
12990 : 51582573 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
12991 : 47233501 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
12992 : 98810948 : && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn))
12993 : : {
12994 : 1689197 : if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn)
12995 : 1465408 : && move_fn_p (cand1->fn))
12996 : : return -1;
12997 : 2703574 : if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn)
12998 : 2214740 : && move_fn_p (cand2->fn))
12999 : : return 1;
13000 : : }
13001 : :
13002 : : /* a viable function F1
13003 : : is defined to be a better function than another viable function F2 if
13004 : : for all arguments i, ICSi(F1) is not a worse conversion sequence than
13005 : : ICSi(F2), and then */
13006 : :
13007 : : /* for some argument j, ICSj(F1) is a better conversion sequence than
13008 : : ICSj(F2) */
13009 : :
13010 : : /* For comparing static and non-static member functions, we ignore
13011 : : the implicit object parameter of the non-static function. The
13012 : : standard says to pretend that the static function has an object
13013 : : parm, but that won't work with operator overloading. */
13014 : 51562500 : len = cand1->num_convs;
13015 : 51562500 : if (len != cand2->num_convs)
13016 : : {
13017 : 141 : int static_1 = (TREE_CODE (cand1->fn) == FUNCTION_DECL
13018 : 141 : && DECL_STATIC_FUNCTION_P (cand1->fn));
13019 : 141 : int static_2 = (TREE_CODE (cand2->fn) == FUNCTION_DECL
13020 : 141 : && DECL_STATIC_FUNCTION_P (cand2->fn));
13021 : :
13022 : 141 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13023 : 125 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13024 : 125 : && DECL_CONSTRUCTOR_P (cand1->fn)
13025 : 147 : && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
13026 : : /* We're comparing a near-match list constructor and a near-match
13027 : : non-list constructor. Just treat them as unordered. */
13028 : : return 0;
13029 : :
13030 : 135 : gcc_assert (static_1 != static_2);
13031 : :
13032 : 135 : if (static_1)
13033 : : {
13034 : : /* C++23 [over.best.ics.general] says:
13035 : : When the parameter is the implicit object parameter of a static
13036 : : member function, the implicit conversion sequence is a standard
13037 : : conversion sequence that is neither better nor worse than any
13038 : : other standard conversion sequence. */
13039 : 40 : if (CONVERSION_RANK (cand2->convs[0]) >= cr_user)
13040 : 0 : winner = 1;
13041 : : off2 = 1;
13042 : : }
13043 : : else
13044 : : {
13045 : 95 : if (CONVERSION_RANK (cand1->convs[0]) >= cr_user)
13046 : 16 : winner = -1;
13047 : 95 : off1 = 1;
13048 : 95 : --len;
13049 : : }
13050 : : }
13051 : :
13052 : 122077001 : for (i = 0; i < len; ++i)
13053 : : {
13054 : 70515050 : conversion *t1 = cand1->convs[i + off1];
13055 : 70515050 : conversion *t2 = cand2->convs[i + off2];
13056 : 70515050 : int comp = compare_ics (t1, t2);
13057 : :
13058 : 70515050 : if (comp != 0)
13059 : : {
13060 : 47781006 : if ((complain & tf_warning)
13061 : 41964781 : && warn_sign_promo
13062 : 82 : && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
13063 : : == cr_std + cr_promotion)
13064 : 50 : && t1->kind == ck_std
13065 : 31 : && t2->kind == ck_std
13066 : 31 : && TREE_CODE (t1->type) == INTEGER_TYPE
13067 : 31 : && TREE_CODE (t2->type) == INTEGER_TYPE
13068 : 31 : && (TYPE_PRECISION (t1->type)
13069 : 31 : == TYPE_PRECISION (t2->type))
13070 : 47781037 : && (TYPE_UNSIGNED (next_conversion (t1)->type)
13071 : 0 : || (TREE_CODE (next_conversion (t1)->type)
13072 : : == ENUMERAL_TYPE)))
13073 : : {
13074 : 31 : tree type = next_conversion (t1)->type;
13075 : 31 : tree type1, type2;
13076 : 31 : struct z_candidate *w, *l;
13077 : 31 : if (comp > 0)
13078 : : type1 = t1->type, type2 = t2->type,
13079 : : w = cand1, l = cand2;
13080 : : else
13081 : 8 : type1 = t2->type, type2 = t1->type,
13082 : 8 : w = cand2, l = cand1;
13083 : :
13084 : 31 : if (warn)
13085 : : {
13086 : 9 : warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
13087 : : type, type1, type2);
13088 : 9 : warning (OPT_Wsign_promo, " in call to %qD", w->fn);
13089 : : }
13090 : : else
13091 : 22 : add_warning (w, l);
13092 : : }
13093 : :
13094 : 47781006 : if (winner && comp != winner)
13095 : : {
13096 : : /* Ambiguity between normal and reversed comparison operators
13097 : : with the same parameter types. P2468 decided not to go with
13098 : : this approach to resolving the ambiguity, so pedwarn. */
13099 : 543 : if ((complain & tf_warning_or_error)
13100 : 430 : && (cand1->reversed () != cand2->reversed ())
13101 : 593 : && cand_parms_match (cand1, cand2, pmatch::original))
13102 : : {
13103 : 41 : struct z_candidate *w, *l;
13104 : 41 : if (cand2->reversed ())
13105 : : winner = 1, w = cand1, l = cand2;
13106 : : else
13107 : 21 : winner = -1, w = cand2, l = cand1;
13108 : 41 : if (warn)
13109 : : {
13110 : 20 : auto_diagnostic_group d;
13111 : 20 : if (pedwarn (input_location, 0,
13112 : : "C++20 says that these are ambiguous, "
13113 : : "even though the second is reversed:"))
13114 : : {
13115 : 20 : print_z_candidate (input_location,
13116 : : N_("candidate 1:"), w);
13117 : 20 : print_z_candidate (input_location,
13118 : : N_("candidate 2:"), l);
13119 : 20 : if (w->fn == l->fn
13120 : 16 : && DECL_IOBJ_MEMBER_FUNCTION_P (w->fn)
13121 : 35 : && (type_memfn_quals (TREE_TYPE (w->fn))
13122 : 15 : & TYPE_QUAL_CONST) == 0)
13123 : : {
13124 : : /* Suggest adding const to
13125 : : struct A { bool operator==(const A&); }; */
13126 : 12 : tree parmtype
13127 : 12 : = FUNCTION_FIRST_USER_PARMTYPE (w->fn);
13128 : 12 : parmtype = TREE_VALUE (parmtype);
13129 : 12 : if (TYPE_REF_P (parmtype)
13130 : 12 : && TYPE_READONLY (TREE_TYPE (parmtype))
13131 : 24 : && (same_type_ignoring_top_level_qualifiers_p
13132 : 12 : (TREE_TYPE (parmtype),
13133 : 12 : DECL_CONTEXT (w->fn))))
13134 : 12 : inform (DECL_SOURCE_LOCATION (w->fn),
13135 : : "try making the operator a %<const%> "
13136 : : "member function");
13137 : : }
13138 : : }
13139 : 20 : }
13140 : : else
13141 : 21 : add_warning (w, l);
13142 : 41 : return winner;
13143 : : }
13144 : :
13145 : 502 : winner = 0;
13146 : 502 : goto tweak;
13147 : : }
13148 : : winner = comp;
13149 : : }
13150 : : }
13151 : :
13152 : : /* warn about confusing overload resolution for user-defined conversions,
13153 : : either between a constructor and a conversion op, or between two
13154 : : conversion ops. */
13155 : 51561951 : if ((complain & tf_warning)
13156 : : /* In C++17, the constructor might have been elided, which means that
13157 : : an originally null ->second_conv could become non-null. */
13158 : 44573507 : && winner && warn_conversion && cand1->second_conv && cand2->second_conv
13159 : 54 : && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
13160 : 51561978 : && winner != compare_ics (cand1->second_conv, cand2->second_conv))
13161 : : {
13162 : 27 : struct z_candidate *w, *l;
13163 : 27 : bool give_warning = false;
13164 : :
13165 : 27 : if (winner == 1)
13166 : : w = cand1, l = cand2;
13167 : : else
13168 : 6 : w = cand2, l = cand1;
13169 : :
13170 : : /* We don't want to complain about `X::operator T1 ()'
13171 : : beating `X::operator T2 () const', when T2 is a no less
13172 : : cv-qualified version of T1. */
13173 : 27 : if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
13174 : 39 : && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
13175 : : {
13176 : 6 : tree t = TREE_TYPE (TREE_TYPE (l->fn));
13177 : 6 : tree f = TREE_TYPE (TREE_TYPE (w->fn));
13178 : :
13179 : 6 : if (TREE_CODE (t) == TREE_CODE (f) && INDIRECT_TYPE_P (t))
13180 : : {
13181 : 6 : t = TREE_TYPE (t);
13182 : 6 : f = TREE_TYPE (f);
13183 : : }
13184 : 6 : if (!comp_ptr_ttypes (t, f))
13185 : : give_warning = true;
13186 : : }
13187 : : else
13188 : : give_warning = true;
13189 : :
13190 : : if (!give_warning)
13191 : : /*NOP*/;
13192 : 21 : else if (warn)
13193 : : {
13194 : 9 : tree source = source_type (w->convs[0]);
13195 : 9 : if (INDIRECT_TYPE_P (source))
13196 : 6 : source = TREE_TYPE (source);
13197 : 9 : auto_diagnostic_group d;
13198 : 9 : if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
13199 : 18 : && warning (OPT_Wconversion, " for conversion from %qH to %qI",
13200 : 9 : source, w->second_conv->type))
13201 : : {
13202 : 9 : inform (input_location, " because conversion sequence "
13203 : : "for the argument is better");
13204 : : }
13205 : 9 : }
13206 : : else
13207 : 12 : add_warning (w, l);
13208 : : }
13209 : :
13210 : 51561951 : if (winner)
13211 : : return winner;
13212 : :
13213 : : /* DR 495 moved this tiebreaker above the template ones. */
13214 : : /* or, if not that,
13215 : : the context is an initialization by user-defined conversion (see
13216 : : _dcl.init_ and _over.match.user_) and the standard conversion
13217 : : sequence from the return type of F1 to the destination type (i.e.,
13218 : : the type of the entity being initialized) is a better conversion
13219 : : sequence than the standard conversion sequence from the return type
13220 : : of F2 to the destination type. */
13221 : :
13222 : 8294849 : if (cand1->second_conv)
13223 : : {
13224 : 31649 : winner = compare_ics (cand1->second_conv, cand2->second_conv);
13225 : 31649 : if (winner)
13226 : : return winner;
13227 : : }
13228 : :
13229 : : /* CWG2735 (PR109247): A copy/move ctor/op= for which its operand uses an
13230 : : explicit conversion (due to list-initialization) is worse. */
13231 : 8294727 : {
13232 : 8294727 : z_candidate *sp = nullptr;
13233 : 8294727 : if (sfk_copy_or_move (cand1->fn))
13234 : 474 : sp = cand1;
13235 : 8294727 : if (sfk_copy_or_move (cand2->fn))
13236 : 506713 : sp = sp ? nullptr : cand2;
13237 : 8294678 : if (sp)
13238 : : {
13239 : 1014178 : conversion *conv = sp->convs[!DECL_CONSTRUCTOR_P (sp->fn)];
13240 : 507089 : if (conv->user_conv_p)
13241 : 1284 : for (; conv; conv = next_conversion (conv))
13242 : 1160 : if (conv->kind == ck_user
13243 : 518 : && DECL_P (conv->cand->fn)
13244 : 1678 : && DECL_NONCONVERTING_P (conv->cand->fn))
13245 : 452 : return (sp == cand1) ? -1 : 1;
13246 : : }
13247 : : }
13248 : :
13249 : : /* DR2327: C++17 copy elision in [over.match.ctor] (direct-init) context.
13250 : : The standard currently says that only constructors are candidates, but if
13251 : : one copies a prvalue returned by a conversion function we prefer that.
13252 : :
13253 : : Clang does something similar, as discussed at
13254 : : http://lists.isocpp.org/core/2017/10/3166.php
13255 : : http://lists.isocpp.org/core/2019/03/5721.php */
13256 : 6200081 : if (len == 1 && cxx_dialect >= cxx17
13257 : 6185811 : && DECL_P (cand1->fn)
13258 : 6185811 : && DECL_COMPLETE_CONSTRUCTOR_P (cand1->fn)
13259 : 8725691 : && !(cand1->flags & LOOKUP_ONLYCONVERTING))
13260 : : {
13261 : 220194 : bool elided1 = joust_maybe_elide_copy (cand1);
13262 : 220194 : bool elided2 = joust_maybe_elide_copy (cand2);
13263 : 220194 : winner = elided1 - elided2;
13264 : 220194 : if (winner)
13265 : : return winner;
13266 : : }
13267 : :
13268 : : /* or, if not that,
13269 : : F1 is a non-template function and F2 is a template function
13270 : : specialization. */
13271 : :
13272 : 8294332 : if (!cand1->template_decl && cand2->template_decl)
13273 : : return 1;
13274 : 8288106 : else if (cand1->template_decl && !cand2->template_decl)
13275 : : return -1;
13276 : :
13277 : : /* or, if not that,
13278 : : F1 and F2 are template functions and the function template for F1 is
13279 : : more specialized than the template for F2 according to the partial
13280 : : ordering rules. */
13281 : :
13282 : 7763366 : if (cand1->template_decl && cand2->template_decl)
13283 : : {
13284 : 2953894 : winner = more_specialized_fn
13285 : 2953894 : (TI_TEMPLATE (cand1->template_decl),
13286 : 2953894 : TI_TEMPLATE (cand2->template_decl),
13287 : : /* [temp.func.order]: The presence of unused ellipsis and default
13288 : : arguments has no effect on the partial ordering of function
13289 : : templates. add_function_candidate() will not have
13290 : : counted the "this" argument for constructors. */
13291 : 5907788 : cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
13292 : 2953894 : if (winner)
13293 : : return winner;
13294 : : }
13295 : :
13296 : : /* F1 and F2 are non-template functions and
13297 : : - they have the same non-object-parameter-type-lists ([dcl.fct]), and
13298 : : - if they are member functions, both are direct members of the same
13299 : : class, and
13300 : : - if both are non-static member functions, they have the same types for
13301 : : their object parameters, and
13302 : : - F1 is more constrained than F2 according to the partial ordering of
13303 : : constraints described in [temp.constr.order]. */
13304 : 2119225 : if (flag_concepts && DECL_P (cand1->fn) && DECL_P (cand2->fn)
13305 : 2119198 : && !cand1->template_decl && !cand2->template_decl
13306 : 6935306 : && cand_parms_match (cand1, cand2, pmatch::current))
13307 : : {
13308 : 96038 : winner = more_constrained (cand1->fn, cand2->fn);
13309 : 96038 : if (winner)
13310 : : return winner;
13311 : : }
13312 : :
13313 : : /* F2 is a rewritten candidate (12.4.1.2) and F1 is not, or F1 and F2 are
13314 : : rewritten candidates, and F2 is a synthesized candidate with reversed
13315 : : order of parameters and F1 is not. */
13316 : 5288747 : if (cand1->rewritten ())
13317 : : {
13318 : 517093 : if (!cand2->rewritten ())
13319 : : return -1;
13320 : 349354 : if (!cand1->reversed () && cand2->reversed ())
13321 : : return 1;
13322 : 349354 : if (cand1->reversed () && !cand2->reversed ())
13323 : : return -1;
13324 : : }
13325 : 4771654 : else if (cand2->rewritten ())
13326 : : return 1;
13327 : :
13328 : 4755970 : if (deduction_guide_p (cand1->fn))
13329 : : {
13330 : 5611 : gcc_assert (deduction_guide_p (cand2->fn));
13331 : :
13332 : : /* F1 and F2 are generated from class template argument deduction for a
13333 : : class D, and F2 is generated from inheriting constructors from a base
13334 : : class of D while F1 is not, and for each explicit function argument,
13335 : : the corresponding parameters of F1 and F2 are either both ellipses or
13336 : : have the same type. */
13337 : 5611 : bool inherited1 = inherited_guide_p (cand1->fn);
13338 : 5611 : bool inherited2 = inherited_guide_p (cand2->fn);
13339 : 5611 : if (int diff = inherited2 - inherited1)
13340 : : {
13341 : 42 : for (i = 0; i < len; ++i)
13342 : : {
13343 : 10 : conversion *t1 = cand1->convs[i + off1];
13344 : 10 : conversion *t2 = cand2->convs[i + off2];
13345 : : /* ??? It seems the ellipses part of this tiebreaker isn't
13346 : : needed since a mismatch should have broken the tie earlier
13347 : : during ICS comparison. */
13348 : 10 : gcc_checking_assert (t1->ellipsis_p == t2->ellipsis_p);
13349 : 10 : if (!same_type_p (t1->type, t2->type))
13350 : : break;
13351 : : }
13352 : 34 : if (i == len)
13353 : : return diff;
13354 : : }
13355 : :
13356 : : /* F1 is generated from a deduction-guide (13.3.1.8) and F2 is not */
13357 : : /* We distinguish between candidates from an explicit deduction guide and
13358 : : candidates built from a constructor based on DECL_ARTIFICIAL. */
13359 : 5579 : int art1 = DECL_ARTIFICIAL (cand1->fn);
13360 : 5579 : int art2 = DECL_ARTIFICIAL (cand2->fn);
13361 : 5579 : if (art1 != art2)
13362 : 944 : return art2 - art1;
13363 : :
13364 : 4635 : if (art1)
13365 : : {
13366 : : /* Prefer the special copy guide over a declared copy/move
13367 : : constructor. */
13368 : 4632 : if (copy_guide_p (cand1->fn))
13369 : : return 1;
13370 : 4538 : if (copy_guide_p (cand2->fn))
13371 : : return -1;
13372 : :
13373 : : /* Prefer a candidate generated from a non-template constructor. */
13374 : 26 : int tg1 = template_guide_p (cand1->fn);
13375 : 26 : int tg2 = template_guide_p (cand2->fn);
13376 : 26 : if (tg1 != tg2)
13377 : 6 : return tg2 - tg1;
13378 : : }
13379 : : }
13380 : :
13381 : : /* F1 is a constructor for a class D, F2 is a constructor for a base class B
13382 : : of D, and for all arguments the corresponding parameters of F1 and F2 have
13383 : : the same type (CWG 2273/2277). */
13384 : 14250987 : if (DECL_INHERITED_CTOR (cand1->fn) || DECL_INHERITED_CTOR (cand2->fn))
13385 : : {
13386 : 92 : tree base1 = DECL_CONTEXT (strip_inheriting_ctors (cand1->fn));
13387 : 92 : tree base2 = DECL_CONTEXT (strip_inheriting_ctors (cand2->fn));
13388 : :
13389 : 92 : bool used1 = false;
13390 : 92 : bool used2 = false;
13391 : 92 : if (base1 == base2)
13392 : : /* No difference. */;
13393 : 92 : else if (DERIVED_FROM_P (base1, base2))
13394 : : used1 = true;
13395 : 18 : else if (DERIVED_FROM_P (base2, base1))
13396 : : used2 = true;
13397 : :
13398 : 83 : if (int diff = used2 - used1)
13399 : : {
13400 : 110 : for (i = 0; i < len; ++i)
13401 : : {
13402 : 36 : conversion *t1 = cand1->convs[i + off1];
13403 : 36 : conversion *t2 = cand2->convs[i + off2];
13404 : 36 : if (!same_type_p (t1->type, t2->type))
13405 : : break;
13406 : : }
13407 : 83 : if (i == len)
13408 : : return diff;
13409 : : }
13410 : : }
13411 : :
13412 : : /* Check whether we can discard a builtin candidate, either because we
13413 : : have two identical ones or matching builtin and non-builtin candidates.
13414 : :
13415 : : (Pedantically in the latter case the builtin which matched the user
13416 : : function should not be added to the overload set, but we spot it here.
13417 : :
13418 : : [over.match.oper]
13419 : : ... the builtin candidates include ...
13420 : : - do not have the same parameter type list as any non-template
13421 : : non-member candidate. */
13422 : :
13423 : 4750308 : if (identifier_p (cand1->fn) || identifier_p (cand2->fn))
13424 : : {
13425 : 65 : for (i = 0; i < len; ++i)
13426 : 52 : if (!same_type_p (cand1->convs[i]->type,
13427 : : cand2->convs[i]->type))
13428 : : break;
13429 : 37 : if (i == cand1->num_convs)
13430 : : {
13431 : 13 : if (cand1->fn == cand2->fn)
13432 : : /* Two built-in candidates; arbitrarily pick one. */
13433 : : return 1;
13434 : 12082676 : else if (identifier_p (cand1->fn))
13435 : : /* cand1 is built-in; prefer cand2. */
13436 : : return -1;
13437 : : else
13438 : : /* cand2 is built-in; prefer cand1. */
13439 : : return 1;
13440 : : }
13441 : : }
13442 : :
13443 : : /* For candidates of a multi-versioned function, make the version with
13444 : : the highest priority win. This version will be checked for dispatching
13445 : : first. If this version can be inlined into the caller, the front-end
13446 : : will simply make a direct call to this function. */
13447 : :
13448 : 4750295 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13449 : 4750268 : && DECL_FUNCTION_VERSIONED (cand1->fn)
13450 : 860 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13451 : 4751155 : && DECL_FUNCTION_VERSIONED (cand2->fn))
13452 : : {
13453 : 860 : tree f1 = TREE_TYPE (cand1->fn);
13454 : 860 : tree f2 = TREE_TYPE (cand2->fn);
13455 : 860 : tree p1 = TYPE_ARG_TYPES (f1);
13456 : 860 : tree p2 = TYPE_ARG_TYPES (f2);
13457 : :
13458 : : /* Check if cand1->fn and cand2->fn are versions of the same function. It
13459 : : is possible that cand1->fn and cand2->fn are function versions but of
13460 : : different functions. Check types to see if they are versions of the same
13461 : : function. */
13462 : 860 : if (compparms (p1, p2)
13463 : 860 : && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
13464 : : {
13465 : : /* Always make the version with the higher priority, more
13466 : : specialized, win. */
13467 : 860 : gcc_assert (targetm.compare_version_priority);
13468 : 860 : if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
13469 : : return 1;
13470 : : else
13471 : : return -1;
13472 : : }
13473 : : }
13474 : :
13475 : : /* If the two function declarations represent the same function (this can
13476 : : happen with declarations in multiple scopes and arg-dependent lookup),
13477 : : arbitrarily choose one. But first make sure the default args we're
13478 : : using match. */
13479 : 4749408 : if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
13480 : 9498843 : && equal_functions (cand1->fn, cand2->fn))
13481 : : {
13482 : 31 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
13483 : 31 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
13484 : :
13485 : 62 : gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
13486 : :
13487 : 44 : for (i = 0; i < len; ++i)
13488 : : {
13489 : : /* Don't crash if the fn is variadic. */
13490 : 13 : if (!parms1)
13491 : : break;
13492 : 13 : parms1 = TREE_CHAIN (parms1);
13493 : 13 : parms2 = TREE_CHAIN (parms2);
13494 : : }
13495 : :
13496 : 31 : if (off1)
13497 : 0 : parms1 = TREE_CHAIN (parms1);
13498 : 31 : else if (off2)
13499 : 0 : parms2 = TREE_CHAIN (parms2);
13500 : :
13501 : 59 : for (; parms1; ++i)
13502 : : {
13503 : 68 : if (!cp_tree_equal (TREE_PURPOSE (parms1),
13504 : 34 : TREE_PURPOSE (parms2)))
13505 : : {
13506 : 6 : if (warn)
13507 : : {
13508 : 3 : if (complain & tf_error)
13509 : : {
13510 : 3 : auto_diagnostic_group d;
13511 : 3 : if (permerror (input_location,
13512 : : "default argument mismatch in "
13513 : : "overload resolution"))
13514 : : {
13515 : 3 : inform (DECL_SOURCE_LOCATION (cand1->fn),
13516 : : " candidate 1: %q#F", cand1->fn);
13517 : 3 : inform (DECL_SOURCE_LOCATION (cand2->fn),
13518 : : " candidate 2: %q#F", cand2->fn);
13519 : : }
13520 : 3 : }
13521 : : else
13522 : : return 0;
13523 : : }
13524 : : else
13525 : 3 : add_warning (cand1, cand2);
13526 : : break;
13527 : : }
13528 : 28 : parms1 = TREE_CHAIN (parms1);
13529 : 28 : parms2 = TREE_CHAIN (parms2);
13530 : : }
13531 : :
13532 : 31 : return 1;
13533 : : }
13534 : :
13535 : 4749906 : tweak:
13536 : :
13537 : : /* Extension: If the worst conversion for one candidate is better than the
13538 : : worst conversion for the other, take the first. */
13539 : 4749906 : if (!pedantic && (complain & tf_warning_or_error))
13540 : : {
13541 : : conversion_rank rank1 = cr_identity, rank2 = cr_identity;
13542 : 11101559 : struct z_candidate *w = 0, *l = 0;
13543 : :
13544 : 11101559 : for (i = 0; i < len; ++i)
13545 : : {
13546 : 6476653 : if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
13547 : 4615023 : rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
13548 : 6476653 : if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
13549 : 4615039 : rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
13550 : : }
13551 : 4624906 : if (rank1 < rank2)
13552 : 36 : winner = 1, w = cand1, l = cand2;
13553 : 4624906 : if (rank1 > rank2)
13554 : : winner = -1, w = cand2, l = cand1;
13555 : 4624797 : if (winner)
13556 : : {
13557 : : /* Don't choose a deleted function over ambiguity. */
13558 : 145 : if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
13559 : : return 0;
13560 : 145 : if (warn)
13561 : : {
13562 : 6 : auto_diagnostic_group d;
13563 : 6 : if (pedwarn (input_location, 0,
13564 : : "ISO C++ says that these are ambiguous, even "
13565 : : "though the worst conversion for the first is "
13566 : : "better than the worst conversion for the second:"))
13567 : : {
13568 : 3 : print_z_candidate (input_location, N_("candidate 1:"), w);
13569 : 3 : print_z_candidate (input_location, N_("candidate 2:"), l);
13570 : : }
13571 : 6 : }
13572 : : else
13573 : 139 : add_warning (w, l);
13574 : 145 : return winner;
13575 : : }
13576 : : }
13577 : :
13578 : : gcc_assert (!winner);
13579 : : return 0;
13580 : : }
13581 : :
13582 : : /* Given a list of candidates for overloading, find the best one, if any.
13583 : : This algorithm has a worst case of O(2n) (winner is last), and a best
13584 : : case of O(n/2) (totally ambiguous); much better than a sorting
13585 : : algorithm. The candidates list is assumed to be sorted according
13586 : : to viability (via splice_viable). */
13587 : :
13588 : : static struct z_candidate *
13589 : 159097508 : tourney (struct z_candidate *candidates, tsubst_flags_t complain)
13590 : : {
13591 : 159097508 : struct z_candidate **champ = &candidates, **challenger;
13592 : 159097508 : int fate;
13593 : 159097508 : struct z_candidate *previous_worse_champ = nullptr;
13594 : :
13595 : : /* Walk through the list once, comparing each current champ to the next
13596 : : candidate, knocking out a candidate or two with each comparison. */
13597 : :
13598 : 210866799 : for (challenger = &candidates->next; *challenger && (*challenger)->viable; )
13599 : : {
13600 : 51817539 : fate = joust (*champ, *challenger, 0, complain);
13601 : 51817539 : if (fate == 1)
13602 : 33811674 : challenger = &(*challenger)->next;
13603 : 18005865 : else if (fate == -1)
13604 : : {
13605 : 13257804 : previous_worse_champ = *champ;
13606 : 13257804 : champ = challenger;
13607 : 13257804 : challenger = &(*challenger)->next;
13608 : : }
13609 : : else
13610 : : {
13611 : 4748061 : previous_worse_champ = nullptr;
13612 : 4748061 : champ = &(*challenger)->next;
13613 : 4748061 : if (!*champ || !(*champ)->viable
13614 : 4699907 : || (*champ)->viable < (*challenger)->viable)
13615 : : {
13616 : : champ = nullptr;
13617 : : break;
13618 : : }
13619 : 4699813 : challenger = &(*champ)->next;
13620 : : }
13621 : : }
13622 : :
13623 : : /* Make sure the champ is better than all the candidates it hasn't yet
13624 : : been compared to. */
13625 : :
13626 : 159097508 : if (champ)
13627 : 22839359 : for (challenger = &candidates;
13628 : 181888619 : challenger != champ;
13629 : 22839359 : challenger = &(*challenger)->next)
13630 : : {
13631 : 22841260 : if (*challenger == previous_worse_champ)
13632 : : /* We already know this candidate is worse than the champ. */
13633 : 12082923 : continue;
13634 : 10758337 : fate = joust (*champ, *challenger, 0, complain);
13635 : 10758337 : if (fate != 1)
13636 : : {
13637 : : champ = nullptr;
13638 : : break;
13639 : : }
13640 : : }
13641 : :
13642 : 159049260 : if (!champ)
13643 : 50149 : return nullptr;
13644 : :
13645 : : /* Move the champ to the front of the candidate list. */
13646 : :
13647 : 159047359 : if (champ != &candidates)
13648 : : {
13649 : 13648322 : z_candidate *saved_champ = *champ;
13650 : 13648322 : *champ = saved_champ->next;
13651 : 13648322 : saved_champ->next = candidates;
13652 : 13648322 : candidates = saved_champ;
13653 : : }
13654 : :
13655 : 159047359 : return candidates;
13656 : : }
13657 : :
13658 : : /* Returns nonzero if things of type FROM can be converted to TO. */
13659 : :
13660 : : bool
13661 : 2807825 : can_convert (tree to, tree from, tsubst_flags_t complain)
13662 : : {
13663 : 2807825 : tree arg = NULL_TREE;
13664 : : /* implicit_conversion only considers user-defined conversions
13665 : : if it has an expression for the call argument list. */
13666 : 2807825 : if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to))
13667 : 109 : arg = build_stub_object (from);
13668 : 2807825 : return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain);
13669 : : }
13670 : :
13671 : : /* Returns nonzero if things of type FROM can be converted to TO with a
13672 : : standard conversion. */
13673 : :
13674 : : bool
13675 : 244 : can_convert_standard (tree to, tree from, tsubst_flags_t complain)
13676 : : {
13677 : 244 : return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
13678 : : }
13679 : :
13680 : : /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
13681 : :
13682 : : bool
13683 : 3506753 : can_convert_arg (tree to, tree from, tree arg, int flags,
13684 : : tsubst_flags_t complain)
13685 : : {
13686 : 3506753 : conversion *t;
13687 : 3506753 : bool ok_p;
13688 : :
13689 : 3506753 : conversion_obstack_sentinel cos;
13690 : : /* We want to discard any access checks done for this test,
13691 : : as we might not be in the appropriate access context and
13692 : : we'll do the check again when we actually perform the
13693 : : conversion. */
13694 : 3506753 : push_deferring_access_checks (dk_deferred);
13695 : :
13696 : 3506753 : t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
13697 : : flags, complain);
13698 : 3506753 : ok_p = (t && !t->bad_p);
13699 : :
13700 : : /* Discard the access checks now. */
13701 : 3506753 : pop_deferring_access_checks ();
13702 : :
13703 : 7013506 : return ok_p;
13704 : 3506753 : }
13705 : :
13706 : : /* Like can_convert_arg, but allows dubious conversions as well. */
13707 : :
13708 : : bool
13709 : 105686047 : can_convert_arg_bad (tree to, tree from, tree arg, int flags,
13710 : : tsubst_flags_t complain)
13711 : : {
13712 : 105686047 : conversion *t;
13713 : :
13714 : 105686047 : conversion_obstack_sentinel cos;
13715 : : /* Try to perform the conversion. */
13716 : 105686047 : t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
13717 : : flags, complain);
13718 : :
13719 : 211372094 : return t != NULL;
13720 : 105686047 : }
13721 : :
13722 : : /* Return an IMPLICIT_CONV_EXPR from EXPR to TYPE with bits set from overload
13723 : : resolution FLAGS. */
13724 : :
13725 : : tree
13726 : 11193829 : build_implicit_conv_flags (tree type, tree expr, int flags)
13727 : : {
13728 : : /* In a template, we are only concerned about determining the
13729 : : type of non-dependent expressions, so we do not have to
13730 : : perform the actual conversion. But for initializers, we
13731 : : need to be able to perform it at instantiation
13732 : : (or instantiate_non_dependent_expr) time. */
13733 : 11193829 : expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
13734 : 11193829 : if (!(flags & LOOKUP_ONLYCONVERTING))
13735 : 3094044 : IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
13736 : 11193829 : if (flags & LOOKUP_NO_NARROWING)
13737 : 5221 : IMPLICIT_CONV_EXPR_BRACED_INIT (expr) = true;
13738 : 11193829 : return expr;
13739 : : }
13740 : :
13741 : : /* Convert EXPR to TYPE. Return the converted expression.
13742 : :
13743 : : Note that we allow bad conversions here because by the time we get to
13744 : : this point we are committed to doing the conversion. If we end up
13745 : : doing a bad conversion, convert_like will complain. */
13746 : :
13747 : : tree
13748 : 184821431 : perform_implicit_conversion_flags (tree type, tree expr,
13749 : : tsubst_flags_t complain, int flags)
13750 : : {
13751 : 184821431 : conversion *conv;
13752 : 184821431 : location_t loc = cp_expr_loc_or_input_loc (expr);
13753 : :
13754 : 184821431 : if (TYPE_REF_P (type))
13755 : 393537 : expr = mark_lvalue_use (expr);
13756 : : else
13757 : 184427894 : expr = mark_rvalue_use (expr);
13758 : :
13759 : 184821431 : if (error_operand_p (expr))
13760 : 440 : return error_mark_node;
13761 : :
13762 : 184820991 : conversion_obstack_sentinel cos;
13763 : :
13764 : 184820991 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
13765 : : /*c_cast_p=*/false,
13766 : : flags, complain);
13767 : :
13768 : 184820991 : if (!conv)
13769 : : {
13770 : 148701 : if (complain & tf_error)
13771 : 447 : implicit_conversion_error (loc, type, expr);
13772 : 148701 : expr = error_mark_node;
13773 : : }
13774 : 184672290 : else if (processing_template_decl && conv->kind != ck_identity)
13775 : 11189698 : expr = build_implicit_conv_flags (type, expr, flags);
13776 : : else
13777 : : {
13778 : : /* Give a conversion call the same location as expr. */
13779 : 173482592 : iloc_sentinel il (loc);
13780 : 173482592 : expr = convert_like (conv, expr, complain);
13781 : 173482592 : }
13782 : :
13783 : 184820991 : return expr;
13784 : 184820991 : }
13785 : :
13786 : : tree
13787 : 23968658 : perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
13788 : : {
13789 : 23968658 : return perform_implicit_conversion_flags (type, expr, complain,
13790 : 23968658 : LOOKUP_IMPLICIT);
13791 : : }
13792 : :
13793 : : /* Convert EXPR to TYPE (as a direct-initialization) if that is
13794 : : permitted. If the conversion is valid, the converted expression is
13795 : : returned. Otherwise, NULL_TREE is returned, except in the case
13796 : : that TYPE is a class type; in that case, an error is issued. If
13797 : : C_CAST_P is true, then this direct-initialization is taking
13798 : : place as part of a static_cast being attempted as part of a C-style
13799 : : cast. */
13800 : :
13801 : : tree
13802 : 38942125 : perform_direct_initialization_if_possible (tree type,
13803 : : tree expr,
13804 : : bool c_cast_p,
13805 : : tsubst_flags_t complain)
13806 : : {
13807 : 38942125 : conversion *conv;
13808 : :
13809 : 38942125 : if (type == error_mark_node || error_operand_p (expr))
13810 : : return error_mark_node;
13811 : : /* [dcl.init]
13812 : :
13813 : : If the destination type is a (possibly cv-qualified) class type:
13814 : :
13815 : : -- If the initialization is direct-initialization ...,
13816 : : constructors are considered.
13817 : :
13818 : : -- If overload resolution is successful, the selected constructor
13819 : : is called to initialize the object, with the initializer expression
13820 : : or expression-list as its argument(s).
13821 : :
13822 : : -- Otherwise, if no constructor is viable, the destination type is
13823 : : a (possibly cv-qualified) aggregate class A, and the initializer is
13824 : : a parenthesized expression-list, the object is initialized as
13825 : : follows... */
13826 : 38942125 : if (CLASS_TYPE_P (type))
13827 : : {
13828 : 1387378 : releasing_vec args (make_tree_vector_single (expr));
13829 : 1387378 : expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
13830 : : &args, type, LOOKUP_NORMAL, complain);
13831 : 1387378 : return build_cplus_new (type, expr, complain);
13832 : 1387378 : }
13833 : :
13834 : 37554747 : conversion_obstack_sentinel cos;
13835 : :
13836 : 37554747 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
13837 : : c_cast_p,
13838 : : LOOKUP_NORMAL, complain);
13839 : 37554747 : if (!conv || conv->bad_p)
13840 : : expr = NULL_TREE;
13841 : 33761482 : else if (processing_template_decl && conv->kind != ck_identity)
13842 : : {
13843 : : /* In a template, we are only concerned about determining the
13844 : : type of non-dependent expressions, so we do not have to
13845 : : perform the actual conversion. But for initializers, we
13846 : : need to be able to perform it at instantiation
13847 : : (or instantiate_non_dependent_expr) time. */
13848 : 610088 : expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
13849 : 610088 : IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
13850 : : }
13851 : : else
13852 : 33151394 : expr = convert_like (conv, expr, NULL_TREE, 0,
13853 : : /*issue_conversion_warnings=*/false,
13854 : : c_cast_p, /*nested_p=*/false, complain);
13855 : :
13856 : 37554747 : return expr;
13857 : 37554747 : }
13858 : :
13859 : : /* When initializing a reference that lasts longer than a full-expression,
13860 : : this special rule applies:
13861 : :
13862 : : [class.temporary]
13863 : :
13864 : : The temporary to which the reference is bound or the temporary
13865 : : that is the complete object to which the reference is bound
13866 : : persists for the lifetime of the reference.
13867 : :
13868 : : The temporaries created during the evaluation of the expression
13869 : : initializing the reference, except the temporary to which the
13870 : : reference is bound, are destroyed at the end of the
13871 : : full-expression in which they are created.
13872 : :
13873 : : In that case, we store the converted expression into a new
13874 : : VAR_DECL in a new scope.
13875 : :
13876 : : However, we want to be careful not to create temporaries when
13877 : : they are not required. For example, given:
13878 : :
13879 : : struct B {};
13880 : : struct D : public B {};
13881 : : D f();
13882 : : const B& b = f();
13883 : :
13884 : : there is no need to copy the return value from "f"; we can just
13885 : : extend its lifetime. Similarly, given:
13886 : :
13887 : : struct S {};
13888 : : struct T { operator S(); };
13889 : : T t;
13890 : : const S& s = t;
13891 : :
13892 : : we can extend the lifetime of the return value of the conversion
13893 : : operator.
13894 : :
13895 : : The next several functions are involved in this lifetime extension. */
13896 : :
13897 : : /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
13898 : : reference is being bound to a temporary. Create and return a new
13899 : : VAR_DECL with the indicated TYPE; this variable will store the value to
13900 : : which the reference is bound. */
13901 : :
13902 : : tree
13903 : 8255 : make_temporary_var_for_ref_to_temp (tree decl, tree type)
13904 : : {
13905 : 8255 : tree var = create_temporary_var (type);
13906 : :
13907 : : /* Register the variable. */
13908 : 8255 : if (VAR_P (decl)
13909 : 8255 : && (TREE_STATIC (decl) || CP_DECL_THREAD_LOCAL_P (decl)))
13910 : : {
13911 : : /* Namespace-scope or local static; give it a mangled name. */
13912 : :
13913 : : /* If an initializer is visible to multiple translation units, those
13914 : : translation units must agree on the addresses of the
13915 : : temporaries. Therefore the temporaries must be given a consistent name
13916 : : and vague linkage. The mangled name of a temporary is the name of the
13917 : : non-temporary object in whose initializer they appear, prefixed with
13918 : : GR and suffixed with a sequence number mangled using the usual rules
13919 : : for a seq-id. Temporaries are numbered with a pre-order, depth-first,
13920 : : left-to-right walk of the complete initializer. */
13921 : 596 : copy_linkage (var, decl);
13922 : :
13923 : 596 : tree name = mangle_ref_init_variable (decl);
13924 : 596 : DECL_NAME (var) = name;
13925 : 596 : SET_DECL_ASSEMBLER_NAME (var, name);
13926 : :
13927 : : /* Set the context to make the variable mergeable in modules. */
13928 : 596 : DECL_CONTEXT (var) = current_scope ();
13929 : : }
13930 : : else
13931 : : /* Create a new cleanup level if necessary. */
13932 : 7659 : maybe_push_cleanup_level (type);
13933 : :
13934 : 8255 : return pushdecl (var);
13935 : : }
13936 : :
13937 : : /* EXPR is the initializer for a variable DECL of reference or
13938 : : std::initializer_list type. Create, push and return a new VAR_DECL
13939 : : for the initializer so that it will live as long as DECL. Any
13940 : : cleanup for the new variable is returned through CLEANUP, and the
13941 : : code to initialize the new variable is returned through INITP. */
13942 : :
13943 : : static tree
13944 : 8255 : set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
13945 : : tree *initp, tree *cond_guard)
13946 : : {
13947 : 8255 : tree init;
13948 : 8255 : tree type;
13949 : 8255 : tree var;
13950 : :
13951 : : /* Create the temporary variable. */
13952 : 8255 : type = TREE_TYPE (expr);
13953 : 8255 : var = make_temporary_var_for_ref_to_temp (decl, type);
13954 : 8255 : layout_decl (var, 0);
13955 : : /* If the rvalue is the result of a function call it will be
13956 : : a TARGET_EXPR. If it is some other construct (such as a
13957 : : member access expression where the underlying object is
13958 : : itself the result of a function call), turn it into a
13959 : : TARGET_EXPR here. It is important that EXPR be a
13960 : : TARGET_EXPR below since otherwise the INIT_EXPR will
13961 : : attempt to make a bitwise copy of EXPR to initialize
13962 : : VAR. */
13963 : 8255 : if (TREE_CODE (expr) != TARGET_EXPR)
13964 : 0 : expr = get_target_expr (expr);
13965 : : else
13966 : : {
13967 : 8255 : if (TREE_ADDRESSABLE (expr))
13968 : 8201 : TREE_ADDRESSABLE (var) = 1;
13969 : 8255 : if (DECL_MERGEABLE (TARGET_EXPR_SLOT (expr)))
13970 : 343 : DECL_MERGEABLE (var) = true;
13971 : : }
13972 : :
13973 : 8255 : if (TREE_CODE (decl) == FIELD_DECL
13974 : 8255 : && extra_warnings && !warning_suppressed_p (decl))
13975 : : {
13976 : 3 : warning (OPT_Wextra, "a temporary bound to %qD only persists "
13977 : : "until the constructor exits", decl);
13978 : 3 : suppress_warning (decl);
13979 : : }
13980 : :
13981 : : /* Recursively extend temps in this initializer. */
13982 : 8255 : TARGET_EXPR_INITIAL (expr)
13983 : 8255 : = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups,
13984 : : cond_guard);
13985 : :
13986 : : /* Any reference temp has a non-trivial initializer. */
13987 : 8255 : DECL_NONTRIVIALLY_INITIALIZED_P (var) = true;
13988 : :
13989 : : /* If the initializer is constant, put it in DECL_INITIAL so we get
13990 : : static initialization and use in constant expressions. */
13991 : 8255 : init = maybe_constant_init (expr, var, /*manifestly_const_eval=*/true);
13992 : : /* As in store_init_value. */
13993 : 8255 : init = cp_fully_fold (init);
13994 : 8255 : if (TREE_CONSTANT (init))
13995 : : {
13996 : 725 : if (literal_type_p (type)
13997 : 701 : && CP_TYPE_CONST_NON_VOLATILE_P (type)
13998 : 1191 : && !TYPE_HAS_MUTABLE_P (type))
13999 : : {
14000 : : /* 5.19 says that a constant expression can include an
14001 : : lvalue-rvalue conversion applied to "a glvalue of literal type
14002 : : that refers to a non-volatile temporary object initialized
14003 : : with a constant expression". Rather than try to communicate
14004 : : that this VAR_DECL is a temporary, just mark it constexpr. */
14005 : 463 : DECL_DECLARED_CONSTEXPR_P (var) = true;
14006 : 463 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
14007 : 463 : TREE_CONSTANT (var) = true;
14008 : 463 : TREE_READONLY (var) = true;
14009 : : }
14010 : 725 : DECL_INITIAL (var) = init;
14011 : 725 : init = NULL_TREE;
14012 : : }
14013 : : else
14014 : : /* Create the INIT_EXPR that will initialize the temporary
14015 : : variable. */
14016 : 7530 : init = split_nonconstant_init (var, expr);
14017 : 8255 : if (at_function_scope_p ())
14018 : : {
14019 : 7718 : add_decl_expr (var);
14020 : :
14021 : 7718 : if (TREE_STATIC (var))
14022 : 59 : init = add_stmt_to_compound (init, register_dtor_fn (var));
14023 : : else
14024 : : {
14025 : 7659 : tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
14026 : 7659 : if (cleanup)
14027 : : {
14028 : 5176 : if (cond_guard && cleanup != error_mark_node)
14029 : : {
14030 : 21 : if (*cond_guard == NULL_TREE)
14031 : : {
14032 : 21 : *cond_guard = build_local_temp (boolean_type_node);
14033 : 21 : add_decl_expr (*cond_guard);
14034 : 21 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION,
14035 : : *cond_guard, NOP_EXPR,
14036 : : boolean_false_node,
14037 : : tf_warning_or_error);
14038 : 21 : finish_expr_stmt (set);
14039 : : }
14040 : 21 : cleanup = build3 (COND_EXPR, void_type_node,
14041 : : *cond_guard, cleanup, NULL_TREE);
14042 : : }
14043 : 5176 : vec_safe_push (*cleanups, cleanup);
14044 : : }
14045 : : }
14046 : :
14047 : : /* We must be careful to destroy the temporary only
14048 : : after its initialization has taken place. If the
14049 : : initialization throws an exception, then the
14050 : : destructor should not be run. We cannot simply
14051 : : transform INIT into something like:
14052 : :
14053 : : (INIT, ({ CLEANUP_STMT; }))
14054 : :
14055 : : because emit_local_var always treats the
14056 : : initializer as a full-expression. Thus, the
14057 : : destructor would run too early; it would run at the
14058 : : end of initializing the reference variable, rather
14059 : : than at the end of the block enclosing the
14060 : : reference variable.
14061 : :
14062 : : The solution is to pass back a cleanup expression
14063 : : which the caller is responsible for attaching to
14064 : : the statement tree. */
14065 : : }
14066 : : else
14067 : : {
14068 : 537 : rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
14069 : 537 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
14070 : : {
14071 : 16 : if (CP_DECL_THREAD_LOCAL_P (var))
14072 : 0 : tls_aggregates = tree_cons (NULL_TREE, var,
14073 : : tls_aggregates);
14074 : : else
14075 : 16 : static_aggregates = tree_cons (NULL_TREE, var,
14076 : : static_aggregates);
14077 : : }
14078 : : else
14079 : : /* Check whether the dtor is callable. */
14080 : 521 : cxx_maybe_build_cleanup (var, tf_warning_or_error);
14081 : : }
14082 : : /* Avoid -Wunused-variable warning (c++/38958). */
14083 : 8255 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
14084 : 8255 : && VAR_P (decl))
14085 : 5105 : TREE_USED (decl) = DECL_READ_P (decl) = true;
14086 : :
14087 : 8255 : *initp = init;
14088 : 8255 : return var;
14089 : : }
14090 : :
14091 : : /* Convert EXPR to the indicated reference TYPE, in a way suitable for
14092 : : initializing a variable of that TYPE. */
14093 : :
14094 : : tree
14095 : 4953030 : initialize_reference (tree type, tree expr,
14096 : : int flags, tsubst_flags_t complain)
14097 : : {
14098 : 4953030 : conversion *conv;
14099 : 4953030 : location_t loc = cp_expr_loc_or_input_loc (expr);
14100 : :
14101 : 4953030 : if (type == error_mark_node || error_operand_p (expr))
14102 : : return error_mark_node;
14103 : :
14104 : 4952968 : conversion_obstack_sentinel cos;
14105 : :
14106 : 4952968 : conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
14107 : : flags, complain);
14108 : : /* If this conversion failed, we're in C++20, and we have something like
14109 : : A& a(b) where A is an aggregate, try again, this time as A& a{b}. */
14110 : 4952968 : if ((!conv || conv->bad_p)
14111 : 469 : && (flags & LOOKUP_AGGREGATE_PAREN_INIT))
14112 : : {
14113 : 4 : tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
14114 : 4 : CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
14115 : 4 : CONSTRUCTOR_IS_PAREN_INIT (e) = true;
14116 : 4 : conversion *c = reference_binding (type, TREE_TYPE (e), e,
14117 : : /*c_cast_p=*/false, flags, complain);
14118 : : /* If this worked, use it. */
14119 : 4 : if (c && !c->bad_p)
14120 : : expr = e, conv = c;
14121 : : }
14122 : 4953375 : if (!conv || conv->bad_p)
14123 : : {
14124 : 466 : if (complain & tf_error)
14125 : : {
14126 : 348 : if (conv)
14127 : 303 : convert_like (conv, expr, complain);
14128 : 45 : else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
14129 : 13 : && !TYPE_REF_IS_RVALUE (type)
14130 : 58 : && !lvalue_p (expr))
14131 : 6 : error_at (loc, "invalid initialization of non-const reference of "
14132 : : "type %qH from an rvalue of type %qI",
14133 : 6 : type, TREE_TYPE (expr));
14134 : : else
14135 : 39 : error_at (loc, "invalid initialization of reference of type "
14136 : : "%qH from expression of type %qI", type,
14137 : 39 : TREE_TYPE (expr));
14138 : : }
14139 : 466 : return error_mark_node;
14140 : : }
14141 : :
14142 : 4952502 : if (conv->kind == ck_ref_bind)
14143 : : /* Perform the conversion. */
14144 : 4952499 : expr = convert_like (conv, expr, complain);
14145 : 3 : else if (conv->kind == ck_ambig)
14146 : : /* We gave an error in build_user_type_conversion_1. */
14147 : 3 : expr = error_mark_node;
14148 : : else
14149 : 0 : gcc_unreachable ();
14150 : :
14151 : : return expr;
14152 : 4952968 : }
14153 : :
14154 : : /* Return true if T is std::pair<const T&, const T&>. */
14155 : :
14156 : : static bool
14157 : 441371 : std_pair_ref_ref_p (tree t)
14158 : : {
14159 : : /* First, check if we have std::pair. */
14160 : 43147 : if (!NON_UNION_CLASS_TYPE_P (t)
14161 : 484058 : || !CLASSTYPE_TEMPLATE_INSTANTIATION (t))
14162 : : return false;
14163 : 12662 : tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (t));
14164 : 12662 : if (!decl_in_std_namespace_p (tdecl))
14165 : : return false;
14166 : 9105 : tree name = DECL_NAME (tdecl);
14167 : 9105 : if (!name || !id_equal (name, "pair"))
14168 : : return false;
14169 : :
14170 : : /* Now see if the template arguments are both const T&. */
14171 : 309 : tree args = CLASSTYPE_TI_ARGS (t);
14172 : 309 : if (TREE_VEC_LENGTH (args) != 2)
14173 : : return false;
14174 : 369 : for (int i = 0; i < 2; i++)
14175 : 399 : if (!TYPE_REF_OBJ_P (TREE_VEC_ELT (args, i))
14176 : 399 : || !CP_TYPE_CONST_P (TREE_TYPE (TREE_VEC_ELT (args, i))))
14177 : 279 : return false;
14178 : :
14179 : : return true;
14180 : : }
14181 : :
14182 : : /* Return true if a class T has a reference member. */
14183 : :
14184 : : static bool
14185 : 216 : class_has_reference_member_p (tree t)
14186 : : {
14187 : 216 : for (tree fields = TYPE_FIELDS (t);
14188 : 3361 : fields;
14189 : 3145 : fields = DECL_CHAIN (fields))
14190 : 3208 : if (TREE_CODE (fields) == FIELD_DECL
14191 : 196 : && !DECL_ARTIFICIAL (fields)
14192 : 3375 : && TYPE_REF_P (TREE_TYPE (fields)))
14193 : : return true;
14194 : : return false;
14195 : : }
14196 : :
14197 : : /* A wrapper for the above suitable as a callback for dfs_walk_once. */
14198 : :
14199 : : static tree
14200 : 216 : class_has_reference_member_p_r (tree binfo, void *)
14201 : : {
14202 : 216 : return (class_has_reference_member_p (BINFO_TYPE (binfo))
14203 : 216 : ? integer_one_node : NULL_TREE);
14204 : : }
14205 : :
14206 : :
14207 : : /* Return true if T (either a class or a function) has been marked as
14208 : : not-dangling. */
14209 : :
14210 : : static bool
14211 : 1408 : no_dangling_p (tree t)
14212 : : {
14213 : 1408 : t = lookup_attribute ("no_dangling", TYPE_ATTRIBUTES (t));
14214 : 1408 : if (!t)
14215 : : return false;
14216 : :
14217 : 75 : t = TREE_VALUE (t);
14218 : 75 : if (!t)
14219 : : return true;
14220 : :
14221 : 51 : t = build_converted_constant_bool_expr (TREE_VALUE (t), tf_warning_or_error);
14222 : 51 : t = cxx_constant_value (t);
14223 : 51 : return t == boolean_true_node;
14224 : : }
14225 : :
14226 : : /* Return true if a class CTYPE is either std::reference_wrapper or
14227 : : std::ref_view, or a reference wrapper class. We consider a class
14228 : : a reference wrapper class if it has a reference member. We no
14229 : : longer check that it has a constructor taking the same reference type
14230 : : since that approach still generated too many false positives. */
14231 : :
14232 : : static bool
14233 : 432 : reference_like_class_p (tree ctype)
14234 : : {
14235 : 432 : if (!CLASS_TYPE_P (ctype))
14236 : : return false;
14237 : :
14238 : 300 : if (no_dangling_p (ctype))
14239 : : return true;
14240 : :
14241 : : /* Also accept a std::pair<const T&, const T&>. */
14242 : 276 : if (std_pair_ref_ref_p (ctype))
14243 : : return true;
14244 : :
14245 : 261 : tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (ctype));
14246 : 261 : if (decl_in_std_namespace_p (tdecl))
14247 : : {
14248 : 38 : tree name = DECL_NAME (tdecl);
14249 : 38 : if (name
14250 : 38 : && (id_equal (name, "reference_wrapper")
14251 : 26 : || id_equal (name, "span")
14252 : 11 : || id_equal (name, "ref_view")))
14253 : : return true;
14254 : : }
14255 : :
14256 : : /* Avoid warning if CTYPE looks like std::span: it has a T* member and
14257 : : a trivial destructor. For example,
14258 : :
14259 : : template<typename T>
14260 : : struct Span {
14261 : : T* data_;
14262 : : std::size len_;
14263 : : };
14264 : :
14265 : : is considered std::span-like. */
14266 : 234 : if (NON_UNION_CLASS_TYPE_P (ctype) && TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
14267 : 214 : for (tree field = next_aggregate_field (TYPE_FIELDS (ctype));
14268 : 353 : field; field = next_aggregate_field (DECL_CHAIN (field)))
14269 : 185 : if (TYPE_PTR_P (TREE_TYPE (field)))
14270 : : return true;
14271 : :
14272 : : /* Some classes, such as std::tuple, have the reference member in its
14273 : : (non-direct) base class. */
14274 : 188 : if (dfs_walk_once (TYPE_BINFO (ctype), class_has_reference_member_p_r,
14275 : : nullptr, nullptr))
14276 : : return true;
14277 : :
14278 : : return false;
14279 : : }
14280 : :
14281 : : /* Helper for maybe_warn_dangling_reference to find a problematic temporary
14282 : : in EXPR (as outlined in maybe_warn_dangling_reference), or NULL_TREE
14283 : : if none found. For instance:
14284 : :
14285 : : const S& s = S().self(); // S()
14286 : : const int& r = (42, f(1)); // temporary for passing 1 to f
14287 : : const int& t = b ? f(1) : f(2); // temporary for 1
14288 : : const int& u = b ? f(1) : f(g); // temporary for 1
14289 : : const int& v = b ? f(g) : f(2); // temporary for 2
14290 : : const int& w = b ? f(g) : f(g); // NULL_TREE
14291 : : const int& y = (f(1), 42); // NULL_TREE
14292 : : const int& z = f(f(1)); // temporary for 1
14293 : :
14294 : : EXPR is the initializer. If ARG_P is true, we're processing an argument
14295 : : to a function; the point is to distinguish between, for example,
14296 : :
14297 : : Ref::inner (&TARGET_EXPR <D.2839, F::foo (fm)>)
14298 : :
14299 : : where we shouldn't warn, and
14300 : :
14301 : : Ref::inner (&TARGET_EXPR <D.2908, F::foo (&TARGET_EXPR <...>)>)
14302 : :
14303 : : where we should warn (Ref is a reference_like_class_p so we see through
14304 : : it. */
14305 : :
14306 : : static tree
14307 : 3239 : do_warn_dangling_reference (tree expr, bool arg_p)
14308 : : {
14309 : 3239 : STRIP_NOPS (expr);
14310 : :
14311 : 3239 : if (arg_p && expr_represents_temporary_p (expr))
14312 : : {
14313 : : /* An attempt to reduce the number of -Wdangling-reference
14314 : : false positives concerning reference wrappers (c++/107532).
14315 : : When we encounter a reference_like_class_p, we don't warn
14316 : : just yet; instead, we keep recursing to see if there were
14317 : : any temporaries behind the reference-wrapper class. */
14318 : : tree e = expr;
14319 : 352 : while (handled_component_p (e))
14320 : 15 : e = TREE_OPERAND (e, 0);
14321 : 337 : tree type = TREE_TYPE (e);
14322 : : /* If the temporary represents a lambda, we don't really know
14323 : : what's going on here. */
14324 : 459 : if (!reference_like_class_p (type) && !LAMBDA_TYPE_P (type))
14325 : : return expr;
14326 : : }
14327 : :
14328 : 3007 : switch (TREE_CODE (expr))
14329 : : {
14330 : 1646 : case CALL_EXPR:
14331 : 1646 : {
14332 : 1646 : tree fndecl = cp_get_callee_fndecl_nofold (expr);
14333 : 1646 : if (!fndecl
14334 : 1646 : || warning_suppressed_p (fndecl, OPT_Wdangling_reference)
14335 : 1635 : || !warning_enabled_at (DECL_SOURCE_LOCATION (fndecl),
14336 : : OPT_Wdangling_reference)
14337 : : /* Don't emit a false positive for:
14338 : : std::vector<int> v = ...;
14339 : : std::vector<int>::const_iterator it = v.begin();
14340 : : const int &r = *it++;
14341 : : because R refers to one of the int elements of V, not to
14342 : : a temporary object. Member operator* may return a reference
14343 : : but probably not to one of its arguments. */
14344 : 1315 : || (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl)
14345 : 825 : && DECL_OVERLOADED_OPERATOR_P (fndecl)
14346 : 316 : && DECL_OVERLOADED_OPERATOR_IS (fndecl, INDIRECT_REF))
14347 : 2754 : || no_dangling_p (TREE_TYPE (fndecl)))
14348 : 562 : return NULL_TREE;
14349 : :
14350 : 1084 : tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
14351 : : /* If the function doesn't return a reference, don't warn. This
14352 : : can be e.g.
14353 : : const int& z = std::min({1, 2, 3, 4, 5, 6, 7});
14354 : : which doesn't dangle: std::min here returns an int.
14355 : :
14356 : : If the function returns a std::pair<const T&, const T&>, we
14357 : : warn, to detect e.g.
14358 : : std::pair<const int&, const int&> v = std::minmax(1, 2);
14359 : : which also creates a dangling reference, because std::minmax
14360 : : returns std::pair<const T&, const T&>(b, a). */
14361 : 1084 : if (!(TYPE_REF_OBJ_P (rettype) || reference_like_class_p (rettype)))
14362 : : return NULL_TREE;
14363 : :
14364 : : /* Here we're looking to see if any of the arguments is a temporary
14365 : : initializing a reference parameter. */
14366 : 1388 : for (int i = 0; i < call_expr_nargs (expr); ++i)
14367 : : {
14368 : 1090 : tree arg = CALL_EXPR_ARG (expr, i);
14369 : : /* Check that this argument initializes a reference, except for
14370 : : the argument initializing the object of a member function. */
14371 : 1090 : if (!DECL_IOBJ_MEMBER_FUNCTION_P (fndecl)
14372 : 1090 : && !TYPE_REF_P (TREE_TYPE (arg)))
14373 : 106 : continue;
14374 : 984 : STRIP_NOPS (arg);
14375 : 984 : if (TREE_CODE (arg) == ADDR_EXPR)
14376 : 613 : arg = TREE_OPERAND (arg, 0);
14377 : : /* Recurse to see if the argument is a temporary. It could also
14378 : : be another call taking a temporary and returning it and
14379 : : initializing this reference parameter. */
14380 : 984 : if ((arg = do_warn_dangling_reference (arg, /*arg_p=*/true)))
14381 : : {
14382 : : /* If we know the temporary could not bind to the return type,
14383 : : don't warn. This is for scalars and empty classes only
14384 : : because for other classes we can't be sure we are not
14385 : : returning its sub-object. */
14386 : 277 : if ((SCALAR_TYPE_P (TREE_TYPE (arg))
14387 : 152 : || is_empty_class (TREE_TYPE (arg)))
14388 : 152 : && TYPE_REF_P (rettype)
14389 : 137 : && !reference_related_p (TREE_TYPE (rettype),
14390 : 137 : TREE_TYPE (arg)))
14391 : 21 : continue;
14392 : 256 : return arg;
14393 : : }
14394 : : /* Don't warn about member functions like:
14395 : : std::any a(...);
14396 : : S& s = a.emplace<S>({0}, 0);
14397 : : which construct a new object and return a reference to it, but
14398 : : we still want to detect:
14399 : : struct S { const S& self () { return *this; } };
14400 : : const S& s = S().self();
14401 : : where 's' dangles. If we've gotten here, the object this function
14402 : : is invoked on is not a temporary. */
14403 : 707 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl))
14404 : : break;
14405 : : }
14406 : : return NULL_TREE;
14407 : : }
14408 : 28 : case COMPOUND_EXPR:
14409 : 28 : return do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p);
14410 : 31 : case COND_EXPR:
14411 : 31 : if (tree t = do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p))
14412 : : return t;
14413 : 16 : return do_warn_dangling_reference (TREE_OPERAND (expr, 2), arg_p);
14414 : 0 : case PAREN_EXPR:
14415 : 0 : return do_warn_dangling_reference (TREE_OPERAND (expr, 0), arg_p);
14416 : 120 : case TARGET_EXPR:
14417 : 120 : return do_warn_dangling_reference (TARGET_EXPR_INITIAL (expr), arg_p);
14418 : : default:
14419 : : return NULL_TREE;
14420 : : }
14421 : : }
14422 : :
14423 : : /* Implement -Wdangling-reference, to detect cases like
14424 : :
14425 : : int n = 1;
14426 : : const int& r = std::max(n - 1, n + 1); // r is dangling
14427 : :
14428 : : This creates temporaries from the arguments, returns a reference to
14429 : : one of the temporaries, but both temporaries are destroyed at the end
14430 : : of the full expression.
14431 : :
14432 : : This works by checking if a reference is initialized with a function
14433 : : that returns a reference, and at least one parameter of the function
14434 : : is a reference that is bound to a temporary. It assumes that such a
14435 : : function actually returns one of its arguments.
14436 : :
14437 : : DECL is the reference being initialized, INIT is the initializer. */
14438 : :
14439 : : static void
14440 : 49114933 : maybe_warn_dangling_reference (const_tree decl, tree init)
14441 : : {
14442 : 49114933 : if (!warn_dangling_reference)
14443 : : return;
14444 : 443140 : tree type = TREE_TYPE (decl);
14445 : : /* Only warn if what we're initializing has type T&& or const T&, or
14446 : : std::pair<const T&, const T&>. (A non-const lvalue reference can't
14447 : : bind to a temporary.) */
14448 : 884235 : if (!((TYPE_REF_OBJ_P (type)
14449 : 3778 : && (TYPE_REF_IS_RVALUE (type)
14450 : 3598 : || CP_TYPE_CONST_P (TREE_TYPE (type))))
14451 : 441095 : || std_pair_ref_ref_p (type)))
14452 : : return;
14453 : : /* Don't suppress the diagnostic just because the call comes from
14454 : : a system header. If the DECL is not in a system header, or if
14455 : : -Wsystem-headers was provided, warn. */
14456 : 2060 : auto wsh
14457 : 2060 : = make_temp_override (global_dc->m_warn_system_headers,
14458 : 2060 : (!in_system_header_at (DECL_SOURCE_LOCATION (decl))
14459 : 2060 : || global_dc->m_warn_system_headers));
14460 : 2060 : if (tree call = do_warn_dangling_reference (init, /*arg_p=*/false))
14461 : : {
14462 : 211 : auto_diagnostic_group d;
14463 : 211 : if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wdangling_reference,
14464 : : "possibly dangling reference to a temporary"))
14465 : 211 : inform (EXPR_LOCATION (call), "%qT temporary created here",
14466 : 211 : TREE_TYPE (call));
14467 : 211 : }
14468 : 2060 : }
14469 : :
14470 : : /* If *P is an xvalue expression, prevent temporary lifetime extension if it
14471 : : gets used to initialize a reference. */
14472 : :
14473 : : static tree
14474 : 86 : prevent_lifetime_extension (tree t)
14475 : : {
14476 : 86 : tree *p = &t;
14477 : 86 : while (TREE_CODE (*p) == COMPOUND_EXPR)
14478 : 0 : p = &TREE_OPERAND (*p, 1);
14479 : 95 : while (handled_component_p (*p))
14480 : 9 : p = &TREE_OPERAND (*p, 0);
14481 : : /* Change a TARGET_EXPR from prvalue to xvalue. */
14482 : 86 : if (TREE_CODE (*p) == TARGET_EXPR)
14483 : 3 : *p = build2 (COMPOUND_EXPR, TREE_TYPE (*p), *p,
14484 : 3 : move (TARGET_EXPR_SLOT (*p)));
14485 : 86 : return t;
14486 : : }
14487 : :
14488 : : /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
14489 : : which is bound either to a reference or a std::initializer_list. */
14490 : :
14491 : : static tree
14492 : 611632 : extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups,
14493 : : tree *cond_guard)
14494 : : {
14495 : : /* CWG1299 (C++20): The temporary object to which the reference is bound or
14496 : : the temporary object that is the complete object of a subobject to which
14497 : : the reference is bound persists for the lifetime of the reference if the
14498 : : glvalue to which the reference is bound was obtained through one of the
14499 : : following:
14500 : : - a temporary materialization conversion ([conv.rval]),
14501 : : - ( expression ), where expression is one of these expressions,
14502 : : - subscripting ([expr.sub]) of an array operand, where that operand is one
14503 : : of these expressions,
14504 : : - a class member access ([expr.ref]) using the . operator where the left
14505 : : operand is one of these expressions and the right operand designates a
14506 : : non-static data member of non-reference type,
14507 : : - a pointer-to-member operation ([expr.mptr.oper]) using the .* operator
14508 : : where the left operand is one of these expressions and the right operand
14509 : : is a pointer to data member of non-reference type,
14510 : : - a const_cast ([expr.const.cast]), static_cast ([expr.static.cast]),
14511 : : dynamic_cast ([expr.dynamic.cast]), or reinterpret_cast
14512 : : ([expr.reinterpret.cast]) converting, without a user-defined conversion,
14513 : : a glvalue operand that is one of these expressions to a glvalue that
14514 : : refers to the object designated by the operand, or to its complete
14515 : : object or a subobject thereof,
14516 : : - a conditional expression ([expr.cond]) that is a glvalue where the
14517 : : second or third operand is one of these expressions, or
14518 : : - a comma expression ([expr.comma]) that is a glvalue where the right
14519 : : operand is one of these expressions. */
14520 : :
14521 : : /* FIXME several cases are still handled wrong (101572, 81420). */
14522 : :
14523 : 611632 : tree sub = init;
14524 : 611632 : tree *p;
14525 : 611632 : STRIP_NOPS (sub);
14526 : 611632 : if (TREE_CODE (sub) == COMPOUND_EXPR)
14527 : : {
14528 : 272 : TREE_OPERAND (sub, 1)
14529 : 272 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups,
14530 : : cond_guard);
14531 : 272 : return init;
14532 : : }
14533 : 611360 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14534 : 611360 : && TYPE_PTRDATAMEM_P (TREE_TYPE (tree_strip_nop_conversions
14535 : : (TREE_OPERAND (sub, 1)))))
14536 : : {
14537 : : /* A pointer-to-member operation. */
14538 : 6 : TREE_OPERAND (sub, 0)
14539 : 6 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 0), cleanups,
14540 : : cond_guard);
14541 : 6 : return init;
14542 : : }
14543 : 611354 : if (TREE_CODE (sub) == COND_EXPR)
14544 : : {
14545 : 21979 : tree cur_cond_guard = NULL_TREE;
14546 : 21979 : if (TREE_OPERAND (sub, 1))
14547 : 21979 : TREE_OPERAND (sub, 1)
14548 : 43958 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups,
14549 : : &cur_cond_guard);
14550 : 21979 : if (cur_cond_guard)
14551 : : {
14552 : 9 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard,
14553 : : NOP_EXPR, boolean_true_node,
14554 : : tf_warning_or_error);
14555 : 9 : TREE_OPERAND (sub, 1)
14556 : 18 : = cp_build_compound_expr (set, TREE_OPERAND (sub, 1),
14557 : : tf_warning_or_error);
14558 : : }
14559 : 21979 : cur_cond_guard = NULL_TREE;
14560 : 21979 : if (TREE_OPERAND (sub, 2))
14561 : 21979 : TREE_OPERAND (sub, 2)
14562 : 43958 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 2), cleanups,
14563 : : &cur_cond_guard);
14564 : 21979 : if (cur_cond_guard)
14565 : : {
14566 : 12 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard,
14567 : : NOP_EXPR, boolean_true_node,
14568 : : tf_warning_or_error);
14569 : 12 : TREE_OPERAND (sub, 2)
14570 : 24 : = cp_build_compound_expr (set, TREE_OPERAND (sub, 2),
14571 : : tf_warning_or_error);
14572 : : }
14573 : 21979 : return init;
14574 : : }
14575 : 589375 : if (TREE_CODE (sub) != ADDR_EXPR)
14576 : : return init;
14577 : : /* Deal with binding to a subobject. */
14578 : 245075 : for (p = &TREE_OPERAND (sub, 0);
14579 : 282684 : TREE_CODE (*p) == COMPONENT_REF || TREE_CODE (*p) == ARRAY_REF; )
14580 : 37609 : p = &TREE_OPERAND (*p, 0);
14581 : 245075 : if (TREE_CODE (*p) == TARGET_EXPR)
14582 : : {
14583 : 8255 : tree subinit = NULL_TREE;
14584 : 8255 : *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit, cond_guard);
14585 : 8255 : recompute_tree_invariant_for_addr_expr (sub);
14586 : 8255 : if (init != sub)
14587 : 8237 : init = fold_convert (TREE_TYPE (init), sub);
14588 : 8255 : if (subinit)
14589 : 7530 : init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
14590 : : }
14591 : : return init;
14592 : : }
14593 : :
14594 : : /* INIT is part of the initializer for DECL. If there are any
14595 : : reference or initializer lists being initialized, extend their
14596 : : lifetime to match that of DECL. */
14597 : :
14598 : : tree
14599 : 50911094 : extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups,
14600 : : tree *cond_guard)
14601 : : {
14602 : 50911094 : tree type = TREE_TYPE (init);
14603 : 50911094 : if (processing_template_decl)
14604 : : return init;
14605 : :
14606 : : /* P2718R0 - ignore temporaries in C++23 for-range-initializer, those
14607 : : have all extended lifetime. */
14608 : 49166985 : if (DECL_NAME (decl) == for_range__identifier
14609 : 49166985 : && flag_range_for_ext_temps)
14610 : : return init;
14611 : :
14612 : 49114933 : maybe_warn_dangling_reference (decl, init);
14613 : :
14614 : 49114933 : if (TYPE_REF_P (type))
14615 : 566963 : init = extend_ref_init_temps_1 (decl, init, cleanups, cond_guard);
14616 : : else
14617 : : {
14618 : 48547970 : tree ctor = init;
14619 : 48547970 : if (TREE_CODE (ctor) == TARGET_EXPR)
14620 : 848167 : ctor = TARGET_EXPR_INITIAL (ctor);
14621 : 48547970 : if (TREE_CODE (ctor) == CONSTRUCTOR)
14622 : : {
14623 : : /* [dcl.init] When initializing an aggregate from a parenthesized list
14624 : : of values... a temporary object bound to a reference does not have
14625 : : its lifetime extended. */
14626 : 3983167 : if (CONSTRUCTOR_IS_PAREN_INIT (ctor))
14627 : : return init;
14628 : :
14629 : 3982887 : if (is_std_init_list (type))
14630 : : {
14631 : : /* The temporary array underlying a std::initializer_list
14632 : : is handled like a reference temporary. */
14633 : 433 : tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
14634 : 433 : array = extend_ref_init_temps_1 (decl, array, cleanups,
14635 : : cond_guard);
14636 : 433 : CONSTRUCTOR_ELT (ctor, 0)->value = array;
14637 : : }
14638 : : else
14639 : : {
14640 : 3982454 : unsigned i;
14641 : 3982454 : constructor_elt *p;
14642 : 3982454 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
14643 : 21239143 : FOR_EACH_VEC_SAFE_ELT (elts, i, p)
14644 : 17256689 : p->value = extend_ref_init_temps (decl, p->value, cleanups,
14645 : : cond_guard);
14646 : : }
14647 : 3982887 : recompute_constructor_flags (ctor);
14648 : 3982887 : if (decl_maybe_constant_var_p (decl) && TREE_CONSTANT (ctor))
14649 : 1765501 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
14650 : : }
14651 : : }
14652 : :
14653 : : return init;
14654 : : }
14655 : :
14656 : : /* Returns true iff an initializer for TYPE could contain temporaries that
14657 : : need to be extended because they are bound to references or
14658 : : std::initializer_list. */
14659 : :
14660 : : bool
14661 : 4964 : type_has_extended_temps (tree type)
14662 : : {
14663 : 4964 : type = strip_array_types (type);
14664 : 4964 : if (TYPE_REF_P (type))
14665 : : return true;
14666 : 4931 : if (CLASS_TYPE_P (type))
14667 : : {
14668 : 2558 : if (is_std_init_list (type))
14669 : : return true;
14670 : 2555 : for (tree f = next_aggregate_field (TYPE_FIELDS (type));
14671 : 6322 : f; f = next_aggregate_field (DECL_CHAIN (f)))
14672 : 3800 : if (type_has_extended_temps (TREE_TYPE (f)))
14673 : : return true;
14674 : : }
14675 : : return false;
14676 : : }
14677 : :
14678 : : /* Returns true iff TYPE is some variant of std::initializer_list. */
14679 : :
14680 : : bool
14681 : 132254398 : is_std_init_list (tree type)
14682 : : {
14683 : 132254398 : if (!TYPE_P (type))
14684 : : return false;
14685 : 132254375 : if (cxx_dialect == cxx98)
14686 : : return false;
14687 : : /* Look through typedefs. */
14688 : 131905989 : type = TYPE_MAIN_VARIANT (type);
14689 : 103864188 : return (CLASS_TYPE_P (type)
14690 : 103733513 : && CP_TYPE_CONTEXT (type) == std_node
14691 : 196741670 : && init_list_identifier == DECL_NAME (TYPE_NAME (type)));
14692 : : }
14693 : :
14694 : : /* Returns true iff DECL is a list constructor: i.e. a constructor which
14695 : : will accept an argument list of a single std::initializer_list<T>. */
14696 : :
14697 : : bool
14698 : 115001761 : is_list_ctor (tree decl)
14699 : : {
14700 : 115001761 : tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
14701 : 115001761 : tree arg;
14702 : :
14703 : 115001761 : if (!args || args == void_list_node)
14704 : : return false;
14705 : :
14706 : 93160630 : arg = non_reference (TREE_VALUE (args));
14707 : 93160630 : if (!is_std_init_list (arg))
14708 : : return false;
14709 : :
14710 : 1855953 : args = TREE_CHAIN (args);
14711 : :
14712 : 3134559 : if (args && args != void_list_node && !TREE_PURPOSE (args))
14713 : : /* There are more non-defaulted parms. */
14714 : : return false;
14715 : :
14716 : : return true;
14717 : : }
14718 : :
14719 : : /* We know that can_convert_arg_bad already said "no" when trying to convert
14720 : : FROM to TO with ARG and FLAGS. Try to figure out if it was because
14721 : : an explicit conversion function was skipped when looking for a way to
14722 : : perform the conversion. At this point we've already printed an error. */
14723 : :
14724 : : void
14725 : 1249 : maybe_show_nonconverting_candidate (tree to, tree from, tree arg, int flags)
14726 : : {
14727 : 1249 : if (!(flags & LOOKUP_ONLYCONVERTING))
14728 : 154 : return;
14729 : :
14730 : 1095 : conversion_obstack_sentinel cos;
14731 : 1095 : conversion *c = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
14732 : : flags & ~LOOKUP_ONLYCONVERTING, tf_none);
14733 : 1095 : if (c && !c->bad_p && c->user_conv_p)
14734 : : /* Ay, the conversion would have worked in direct-init context. */
14735 : 258 : for (; c; c = next_conversion (c))
14736 : 172 : if (c->kind == ck_user
14737 : 86 : && DECL_P (c->cand->fn)
14738 : 258 : && DECL_NONCONVERTING_P (c->cand->fn))
14739 : 86 : inform (DECL_SOURCE_LOCATION (c->cand->fn), "explicit conversion "
14740 : : "function was not considered");
14741 : 1095 : }
14742 : :
14743 : : /* We're converting EXPR to TYPE. If that conversion involves a conversion
14744 : : function and we're binding EXPR to a reference parameter of that function,
14745 : : return true. */
14746 : :
14747 : : bool
14748 : 171 : conv_binds_to_reference_parm_p (tree type, tree expr)
14749 : : {
14750 : 171 : conversion_obstack_sentinel cos;
14751 : 171 : conversion *c = implicit_conversion (type, TREE_TYPE (expr), expr,
14752 : : /*c_cast_p=*/false, LOOKUP_NORMAL,
14753 : : tf_none);
14754 : 171 : if (c && !c->bad_p && c->user_conv_p)
14755 : 117 : for (; c; c = next_conversion (c))
14756 : 81 : if (c->kind == ck_user)
14757 : 244 : for (z_candidate *cand = c->cand; cand; cand = cand->next)
14758 : 208 : if (cand->viable == 1)
14759 : 81 : for (size_t i = 0; i < cand->num_convs; ++i)
14760 : 45 : if (cand->convs[i]->kind == ck_ref_bind
14761 : 45 : && conv_get_original_expr (cand->convs[i]) == expr)
14762 : : return true;
14763 : :
14764 : : return false;
14765 : 171 : }
14766 : :
14767 : : #include "gt-cp-call.h"
|