Branch data Line data Source code
1 : : /* Functions related to invoking -*- C++ -*- methods and overloaded functions.
2 : : Copyright (C) 1987-2025 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, enum diagnostics::kind *,
224 : : tsubst_flags_t);
225 : : static conversion *build_identity_conv (tree, tree);
226 : : static inline bool conv_binds_to_array_of_unknown_bound (conversion *);
227 : : static bool conv_is_prvalue (conversion *);
228 : : static tree prevent_lifetime_extension (tree);
229 : :
230 : : /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
231 : : NAME can take many forms... */
232 : :
233 : : bool
234 : 3020923 : check_dtor_name (tree basetype, tree name)
235 : : {
236 : : /* Just accept something we've already complained about. */
237 : 3020923 : if (name == error_mark_node)
238 : : return true;
239 : :
240 : 3020923 : if (TREE_CODE (name) == TYPE_DECL)
241 : 84 : name = TREE_TYPE (name);
242 : 3020839 : else if (TYPE_P (name))
243 : : /* OK */;
244 : 26 : else if (identifier_p (name))
245 : : {
246 : 26 : if ((MAYBE_CLASS_TYPE_P (basetype)
247 : 0 : || TREE_CODE (basetype) == ENUMERAL_TYPE)
248 : 26 : && name == constructor_name (basetype))
249 : : return true;
250 : :
251 : : /* Otherwise lookup the name, it could be an unrelated typedef
252 : : of the correct type. */
253 : 6 : name = lookup_name (name, LOOK_want::TYPE);
254 : 6 : if (!name)
255 : : return false;
256 : 3 : name = TREE_TYPE (name);
257 : 3 : if (name == error_mark_node)
258 : : return false;
259 : : }
260 : : else
261 : : {
262 : : /* In the case of:
263 : :
264 : : template <class T> struct S { ~S(); };
265 : : int i;
266 : : i.~S();
267 : :
268 : : NAME will be a class template. */
269 : 0 : gcc_assert (DECL_CLASS_TEMPLATE_P (name));
270 : : return false;
271 : : }
272 : :
273 : 3020897 : return same_type_p (TYPE_MAIN_VARIANT (basetype), TYPE_MAIN_VARIANT (name));
274 : : }
275 : :
276 : : /* We want the address of a function or method. We avoid creating a
277 : : pointer-to-member function. */
278 : :
279 : : tree
280 : 269690185 : build_addr_func (tree function, tsubst_flags_t complain)
281 : : {
282 : 269690185 : tree type = TREE_TYPE (function);
283 : :
284 : : /* We have to do these by hand to avoid real pointer to member
285 : : functions. */
286 : 269690185 : if (TREE_CODE (type) == METHOD_TYPE)
287 : : {
288 : 43350476 : if (TREE_CODE (function) == OFFSET_REF)
289 : : {
290 : 88 : tree object = build_address (TREE_OPERAND (function, 0));
291 : 88 : return get_member_function_from_ptrfunc (&object,
292 : 88 : TREE_OPERAND (function, 1),
293 : : complain);
294 : : }
295 : 43350388 : function = build_address (function);
296 : : }
297 : 226339709 : else if (TREE_CODE (function) == FUNCTION_DECL
298 : 325995214 : && DECL_IMMEDIATE_FUNCTION_P (function))
299 : 565667 : function = build_address (function);
300 : : else
301 : 225774042 : function = decay_conversion (function, complain, /*reject_builtin=*/false);
302 : :
303 : : return function;
304 : : }
305 : :
306 : : /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
307 : : POINTER_TYPE to those. Note, pointer to member function types
308 : : (TYPE_PTRMEMFUNC_P) must be handled by our callers. There are
309 : : two variants. build_call_a is the primitive taking an array of
310 : : arguments, while build_call_n is a wrapper that handles varargs. */
311 : :
312 : : tree
313 : 172229 : build_call_n (tree function, int n, ...)
314 : : {
315 : 172229 : if (n == 0)
316 : 0 : return build_call_a (function, 0, NULL);
317 : : else
318 : : {
319 : 172229 : tree *argarray = XALLOCAVEC (tree, n);
320 : 172229 : va_list ap;
321 : 172229 : int i;
322 : :
323 : 172229 : va_start (ap, n);
324 : 344458 : for (i = 0; i < n; i++)
325 : 172229 : argarray[i] = va_arg (ap, tree);
326 : 172229 : va_end (ap);
327 : 172229 : return build_call_a (function, n, argarray);
328 : : }
329 : : }
330 : :
331 : : /* Update various flags in cfun and the call itself based on what is being
332 : : called. Split out of build_call_a so that bot_manip can use it too. */
333 : :
334 : : void
335 : 127505449 : set_flags_from_callee (tree call)
336 : : {
337 : : /* Handle both CALL_EXPRs and AGGR_INIT_EXPRs. */
338 : 127505449 : tree decl = cp_get_callee_fndecl_nofold (call);
339 : :
340 : : /* We check both the decl and the type; a function may be known not to
341 : : throw without being declared throw(). */
342 : 127505449 : bool nothrow = decl && TREE_NOTHROW (decl);
343 : 127505449 : tree callee = cp_get_callee (call);
344 : 127505449 : if (callee)
345 : 127505441 : nothrow |= TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (callee)));
346 : 8 : else if (TREE_CODE (call) == CALL_EXPR
347 : 8 : && internal_fn_flags (CALL_EXPR_IFN (call)) & ECF_NOTHROW)
348 : : nothrow = true;
349 : :
350 : 127505449 : if (cfun && cp_function_chain && !cp_unevaluated_operand)
351 : : {
352 : 82689760 : if (!nothrow && at_function_scope_p ())
353 : 21975409 : cp_function_chain->can_throw = 1;
354 : :
355 : 82689760 : if (decl && TREE_THIS_VOLATILE (decl))
356 : 2710618 : current_function_returns_abnormally = 1;
357 : : }
358 : :
359 : 127505449 : TREE_NOTHROW (call) = nothrow;
360 : 127505449 : }
361 : :
362 : : tree
363 : 125567479 : build_call_a (tree function, int n, tree *argarray)
364 : : {
365 : 125567479 : tree decl;
366 : 125567479 : tree result_type;
367 : 125567479 : tree fntype;
368 : 125567479 : int i;
369 : :
370 : 125567479 : function = build_addr_func (function, tf_warning_or_error);
371 : :
372 : 125567479 : gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
373 : 125567479 : fntype = TREE_TYPE (TREE_TYPE (function));
374 : 125567479 : gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype));
375 : 125567479 : result_type = TREE_TYPE (fntype);
376 : : /* An rvalue has no cv-qualifiers. */
377 : 125567479 : if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
378 : 82046413 : result_type = cv_unqualified (result_type);
379 : :
380 : 125567479 : function = build_call_array_loc (input_location,
381 : : result_type, function, n, argarray);
382 : 125567479 : set_flags_from_callee (function);
383 : :
384 : 125567479 : decl = get_callee_fndecl (function);
385 : :
386 : 125567479 : if (decl && !TREE_USED (decl))
387 : : {
388 : : /* We invoke build_call directly for several library
389 : : functions. These may have been declared normally if
390 : : we're building libgcc, so we can't just check
391 : : DECL_ARTIFICIAL. */
392 : 148381 : gcc_assert (DECL_ARTIFICIAL (decl)
393 : : || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
394 : : "__", 2));
395 : 148381 : mark_used (decl);
396 : : }
397 : :
398 : 125567479 : require_complete_eh_spec_types (fntype, decl);
399 : :
400 : 364799260 : TREE_HAS_CONSTRUCTOR (function) = (decl && DECL_CONSTRUCTOR_P (decl));
401 : :
402 : : /* Don't pass empty class objects by value. This is useful
403 : : for tags in STL, which are used to control overload resolution.
404 : : We don't need to handle other cases of copying empty classes. */
405 : 125567479 : if (!decl || !fndecl_built_in_p (decl))
406 : 256044171 : for (i = 0; i < n; i++)
407 : : {
408 : 137677521 : tree arg = CALL_EXPR_ARG (function, i);
409 : 137677521 : if (is_empty_class (TREE_TYPE (arg))
410 : 137677521 : && simple_empty_class_p (TREE_TYPE (arg), arg, INIT_EXPR))
411 : : {
412 : 5309657 : while (TREE_CODE (arg) == TARGET_EXPR)
413 : : /* We're disconnecting the initializer from its target,
414 : : don't create a temporary. */
415 : 2654040 : arg = TARGET_EXPR_INITIAL (arg);
416 : 2655617 : suppress_warning (arg, OPT_Wunused_result);
417 : 2655617 : tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg));
418 : 2655617 : arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t);
419 : 2655617 : CALL_EXPR_ARG (function, i) = arg;
420 : : }
421 : : }
422 : :
423 : 125567479 : return function;
424 : : }
425 : :
426 : : /* New overloading code. */
427 : :
428 : : struct z_candidate;
429 : :
430 : : struct candidate_warning {
431 : : z_candidate *loser;
432 : : candidate_warning *next;
433 : : };
434 : :
435 : : /* Information for providing diagnostics about why overloading failed. */
436 : :
437 : : enum rejection_reason_code {
438 : : rr_none,
439 : : rr_arity,
440 : : rr_explicit_conversion,
441 : : rr_template_conversion,
442 : : rr_arg_conversion,
443 : : rr_bad_arg_conversion,
444 : : rr_template_unification,
445 : : rr_invalid_copy,
446 : : rr_inherited_ctor,
447 : : rr_constraint_failure,
448 : : rr_ignored,
449 : : };
450 : :
451 : : struct conversion_info {
452 : : /* The index of the argument, 0-based. */
453 : : int n_arg;
454 : : /* The actual argument or its type. */
455 : : tree from;
456 : : /* The type of the parameter. */
457 : : tree to_type;
458 : : /* The location of the argument. */
459 : : location_t loc;
460 : : };
461 : :
462 : : struct rejection_reason {
463 : : enum rejection_reason_code code;
464 : : union {
465 : : /* Information about an arity mismatch. */
466 : : struct {
467 : : /* The expected number of arguments. */
468 : : int expected;
469 : : /* The actual number of arguments in the call. */
470 : : int actual;
471 : : /* Whether EXPECTED should be treated as a lower bound. */
472 : : bool least_p;
473 : : } arity;
474 : : /* Information about an argument conversion mismatch. */
475 : : struct conversion_info conversion;
476 : : /* Same, but for bad argument conversions. */
477 : : struct conversion_info bad_conversion;
478 : : /* Information about template unification failures. These are the
479 : : parameters passed to fn_type_unification. */
480 : : struct {
481 : : tree tmpl;
482 : : tree explicit_targs;
483 : : int num_targs;
484 : : const tree *args;
485 : : unsigned int nargs;
486 : : tree return_type;
487 : : unification_kind_t strict;
488 : : int flags;
489 : : } template_unification;
490 : : /* Information about template instantiation failures. These are the
491 : : parameters passed to instantiate_template. */
492 : : struct {
493 : : tree tmpl;
494 : : tree targs;
495 : : } template_instantiation;
496 : : } u;
497 : : };
498 : :
499 : : struct z_candidate {
500 : : /* The FUNCTION_DECL that will be called if this candidate is
501 : : selected by overload resolution. */
502 : : tree fn;
503 : : /* If not NULL_TREE, the first argument to use when calling this
504 : : function. */
505 : : tree first_arg;
506 : : /* The rest of the arguments to use when calling this function. If
507 : : there are no further arguments this may be NULL or it may be an
508 : : empty vector. */
509 : : const vec<tree, va_gc> *args;
510 : : /* The implicit conversion sequences for each of the arguments to
511 : : FN. */
512 : : conversion **convs;
513 : : /* The number of implicit conversion sequences. */
514 : : size_t num_convs;
515 : : /* If FN is a user-defined conversion, the standard conversion
516 : : sequence from the type returned by FN to the desired destination
517 : : type. */
518 : : conversion *second_conv;
519 : : struct rejection_reason *reason;
520 : : /* If FN is a member function, the binfo indicating the path used to
521 : : qualify the name of FN at the call site. This path is used to
522 : : determine whether or not FN is accessible if it is selected by
523 : : overload resolution. The DECL_CONTEXT of FN will always be a
524 : : (possibly improper) base of this binfo. */
525 : : tree access_path;
526 : : /* If FN is a non-static member function, the binfo indicating the
527 : : subobject to which the `this' pointer should be converted if FN
528 : : is selected by overload resolution. The type pointed to by
529 : : the `this' pointer must correspond to the most derived class
530 : : indicated by the CONVERSION_PATH. */
531 : : tree conversion_path;
532 : : tree template_decl;
533 : : tree explicit_targs;
534 : : candidate_warning *warnings;
535 : : z_candidate *next;
536 : : int viable;
537 : :
538 : : /* The flags active in add_candidate. */
539 : : int flags;
540 : :
541 : 44600805 : bool rewritten () const { return (flags & LOOKUP_REWRITTEN); }
542 : 766400777 : bool reversed () const { return (flags & LOOKUP_REVERSED); }
543 : : };
544 : :
545 : : /* Returns true iff T is a null pointer constant in the sense of
546 : : [conv.ptr]. */
547 : :
548 : : bool
549 : 98755468 : null_ptr_cst_p (tree t)
550 : : {
551 : 98755468 : tree type = TREE_TYPE (t);
552 : :
553 : : /* [conv.ptr]
554 : :
555 : : A null pointer constant is an integer literal ([lex.icon]) with value
556 : : zero or a prvalue of type std::nullptr_t. */
557 : 98755468 : if (NULLPTR_TYPE_P (type))
558 : : return true;
559 : :
560 : 91663740 : if (cxx_dialect >= cxx11)
561 : : {
562 : 91280530 : STRIP_ANY_LOCATION_WRAPPER (t);
563 : :
564 : : /* Core issue 903 says only literal 0 is a null pointer constant. */
565 : 91280530 : if (TREE_CODE (t) == INTEGER_CST
566 : 10112226 : && !TREE_OVERFLOW (t)
567 : 10112226 : && TREE_CODE (type) == INTEGER_TYPE
568 : 9495462 : && integer_zerop (t)
569 : 97291142 : && !char_type_p (type))
570 : : return true;
571 : : }
572 : 383210 : else if (CP_INTEGRAL_TYPE_P (type))
573 : : {
574 : 75405 : t = fold_non_dependent_expr (t, tf_none);
575 : 75405 : STRIP_NOPS (t);
576 : 75405 : if (integer_zerop (t) && !TREE_OVERFLOW (t))
577 : : return true;
578 : : }
579 : :
580 : : return false;
581 : : }
582 : :
583 : : /* Returns true iff T is a null member pointer value (4.11). */
584 : :
585 : : bool
586 : 79802258 : null_member_pointer_value_p (tree t)
587 : : {
588 : 79802258 : tree type = TREE_TYPE (t);
589 : 79802258 : if (!type)
590 : : return false;
591 : 79622911 : else if (TYPE_PTRMEMFUNC_P (type))
592 : 551 : return (TREE_CODE (t) == CONSTRUCTOR
593 : 279 : && CONSTRUCTOR_NELTS (t)
594 : 827 : && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value));
595 : 79622360 : else if (TYPE_PTRDATAMEM_P (type))
596 : 5464 : return integer_all_onesp (t);
597 : : else
598 : : return false;
599 : : }
600 : :
601 : : /* Returns nonzero if PARMLIST consists of only default parms,
602 : : ellipsis, and/or undeduced parameter packs. */
603 : :
604 : : bool
605 : 563563668 : sufficient_parms_p (const_tree parmlist)
606 : : {
607 : 572432530 : for (; parmlist && parmlist != void_list_node;
608 : 8868862 : parmlist = TREE_CHAIN (parmlist))
609 : 183931210 : if (!TREE_PURPOSE (parmlist)
610 : 183931210 : && !PACK_EXPANSION_P (TREE_VALUE (parmlist)))
611 : : return false;
612 : : return true;
613 : : }
614 : :
615 : : /* Allocate N bytes of memory from the conversion obstack. The memory
616 : : is zeroed before being returned. */
617 : :
618 : : static void *
619 : 5757232257 : conversion_obstack_alloc (size_t n)
620 : : {
621 : 5757232257 : void *p;
622 : 5757232257 : if (!conversion_obstack_initialized)
623 : : {
624 : 82771 : gcc_obstack_init (&conversion_obstack);
625 : 82771 : conversion_obstack_initialized = true;
626 : : }
627 : 5757232257 : p = obstack_alloc (&conversion_obstack, n);
628 : 5757232257 : memset (p, 0, n);
629 : 5757232257 : return p;
630 : : }
631 : :
632 : : /* RAII class to discard anything added to conversion_obstack. */
633 : :
634 : : struct conversion_obstack_sentinel
635 : : {
636 : : void *p;
637 : 1947156954 : conversion_obstack_sentinel (): p (conversion_obstack_alloc (0)) {}
638 : 973564923 : ~conversion_obstack_sentinel () { obstack_free (&conversion_obstack, p); }
639 : : };
640 : :
641 : : /* Allocate rejection reasons. */
642 : :
643 : : static struct rejection_reason *
644 : 545559224 : alloc_rejection (enum rejection_reason_code code)
645 : : {
646 : 545559224 : struct rejection_reason *p;
647 : 0 : p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
648 : 545559224 : p->code = code;
649 : 545559224 : return p;
650 : : }
651 : :
652 : : static struct rejection_reason *
653 : 154448000 : arity_rejection (tree first_arg, int expected, int actual, bool least_p = false)
654 : : {
655 : 121032387 : struct rejection_reason *r = alloc_rejection (rr_arity);
656 : 154448000 : int adjust = first_arg != NULL_TREE;
657 : 154448000 : r->u.arity.expected = expected - adjust;
658 : 154448000 : r->u.arity.actual = actual - adjust;
659 : 154448000 : r->u.arity.least_p = least_p;
660 : 154448000 : return r;
661 : : }
662 : :
663 : : static struct rejection_reason *
664 : 108178220 : arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to,
665 : : location_t loc)
666 : : {
667 : 4155374 : struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
668 : 108178220 : int adjust = first_arg != NULL_TREE;
669 : 108178220 : r->u.conversion.n_arg = n_arg - adjust;
670 : 108178220 : r->u.conversion.from = from;
671 : 108178220 : r->u.conversion.to_type = to;
672 : 108178220 : r->u.conversion.loc = loc;
673 : 108178220 : return r;
674 : : }
675 : :
676 : : static struct rejection_reason *
677 : 17151153 : bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to,
678 : : location_t loc)
679 : : {
680 : 1438 : struct rejection_reason *r = alloc_rejection (rr_bad_arg_conversion);
681 : 17151153 : int adjust = first_arg != NULL_TREE;
682 : 17151153 : r->u.bad_conversion.n_arg = n_arg - adjust;
683 : 17151153 : r->u.bad_conversion.from = from;
684 : 17151153 : r->u.bad_conversion.to_type = to;
685 : 17151153 : r->u.bad_conversion.loc = loc;
686 : 17151153 : return r;
687 : : }
688 : :
689 : : static struct rejection_reason *
690 : 2457 : explicit_conversion_rejection (tree from, tree to)
691 : : {
692 : 2457 : struct rejection_reason *r = alloc_rejection (rr_explicit_conversion);
693 : 2457 : r->u.conversion.n_arg = 0;
694 : 2457 : r->u.conversion.from = from;
695 : 2457 : r->u.conversion.to_type = to;
696 : 2457 : r->u.conversion.loc = UNKNOWN_LOCATION;
697 : 2457 : return r;
698 : : }
699 : :
700 : : static struct rejection_reason *
701 : 21 : template_conversion_rejection (tree from, tree to)
702 : : {
703 : 21 : struct rejection_reason *r = alloc_rejection (rr_template_conversion);
704 : 21 : r->u.conversion.n_arg = 0;
705 : 21 : r->u.conversion.from = from;
706 : 21 : r->u.conversion.to_type = to;
707 : 21 : r->u.conversion.loc = UNKNOWN_LOCATION;
708 : 21 : return r;
709 : : }
710 : :
711 : : static struct rejection_reason *
712 : 264639362 : template_unification_rejection (tree tmpl, tree explicit_targs, tree targs,
713 : : const tree *args, unsigned int nargs,
714 : : tree return_type, unification_kind_t strict,
715 : : int flags)
716 : : {
717 : 264639362 : size_t args_n_bytes = sizeof (*args) * nargs;
718 : 264639362 : tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes);
719 : 264639362 : struct rejection_reason *r = alloc_rejection (rr_template_unification);
720 : 264639362 : r->u.template_unification.tmpl = tmpl;
721 : 264639362 : r->u.template_unification.explicit_targs = explicit_targs;
722 : 264639362 : r->u.template_unification.num_targs = TREE_VEC_LENGTH (targs);
723 : : /* Copy args to our own storage. */
724 : 264639362 : memcpy (args1, args, args_n_bytes);
725 : 264639362 : r->u.template_unification.args = args1;
726 : 264639362 : r->u.template_unification.nargs = nargs;
727 : 264639362 : r->u.template_unification.return_type = return_type;
728 : 264639362 : r->u.template_unification.strict = strict;
729 : 264639362 : r->u.template_unification.flags = flags;
730 : 264639362 : return r;
731 : : }
732 : :
733 : : static struct rejection_reason *
734 : 109 : template_unification_error_rejection (void)
735 : : {
736 : 0 : return alloc_rejection (rr_template_unification);
737 : : }
738 : :
739 : : static struct rejection_reason *
740 : 578671 : invalid_copy_with_fn_template_rejection (void)
741 : : {
742 : 0 : struct rejection_reason *r = alloc_rejection (rr_invalid_copy);
743 : 578671 : return r;
744 : : }
745 : :
746 : : static struct rejection_reason *
747 : 418431 : inherited_ctor_rejection (void)
748 : : {
749 : 0 : struct rejection_reason *r = alloc_rejection (rr_inherited_ctor);
750 : 418431 : return r;
751 : : }
752 : :
753 : : /* Build a constraint failure record. */
754 : :
755 : : static struct rejection_reason *
756 : 142800 : constraint_failure (void)
757 : : {
758 : 0 : struct rejection_reason *r = alloc_rejection (rr_constraint_failure);
759 : 142800 : return r;
760 : : }
761 : :
762 : : /* Dynamically allocate a conversion. */
763 : :
764 : : static conversion *
765 : 1988700978 : alloc_conversion (conversion_kind kind)
766 : : {
767 : 1988700978 : conversion *c;
768 : 0 : c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
769 : 1988700978 : c->kind = kind;
770 : 1988700978 : return c;
771 : : }
772 : :
773 : : /* Make sure that all memory on the conversion obstack has been
774 : : freed. */
775 : :
776 : : void
777 : 94553 : validate_conversion_obstack (void)
778 : : {
779 : 94553 : if (conversion_obstack_initialized)
780 : 82442 : gcc_assert ((obstack_next_free (&conversion_obstack)
781 : : == obstack_base (&conversion_obstack)));
782 : 94553 : }
783 : :
784 : : /* Dynamically allocate an array of N conversions. */
785 : :
786 : : static conversion **
787 : 749860429 : alloc_conversions (size_t n)
788 : : {
789 : 0 : return (conversion **) conversion_obstack_alloc (n * sizeof (conversion *));
790 : : }
791 : :
792 : : /* True iff the active member of conversion::u for code CODE is NEXT. */
793 : :
794 : : static inline bool
795 : 1136809307 : has_next (conversion_kind code)
796 : : {
797 : 1057413821 : return !(code == ck_identity
798 : 1136809307 : || code == ck_ambig
799 : : || code == ck_list
800 : 1057487189 : || code == ck_aggr
801 : 1136809307 : || code == ck_deferred_bad);
802 : : }
803 : :
804 : : static conversion *
805 : 579700969 : build_conv (conversion_kind code, tree type, conversion *from)
806 : : {
807 : 579700969 : conversion *t;
808 : 579700969 : conversion_rank rank = CONVERSION_RANK (from);
809 : :
810 : : /* Only call this function for conversions that use u.next. */
811 : 579700969 : gcc_assert (from == NULL || has_next (code));
812 : :
813 : : /* Note that the caller is responsible for filling in t->cand for
814 : : user-defined conversions. */
815 : 579700969 : t = alloc_conversion (code);
816 : 579700969 : t->type = type;
817 : 579700969 : t->u.next = from;
818 : :
819 : 579700969 : switch (code)
820 : : {
821 : 155251877 : case ck_ptr:
822 : 155251877 : case ck_pmem:
823 : 155251877 : case ck_base:
824 : 155251877 : case ck_std:
825 : 155251877 : if (rank < cr_std)
826 : 579700969 : rank = cr_std;
827 : : break;
828 : :
829 : 37976307 : case ck_qual:
830 : 37976307 : case ck_fnptr:
831 : 37976307 : if (rank < cr_exact)
832 : 579700969 : rank = cr_exact;
833 : : break;
834 : :
835 : : default:
836 : : break;
837 : : }
838 : 579700969 : t->rank = rank;
839 : 579700969 : t->user_conv_p = (code == ck_user || from->user_conv_p);
840 : 579700969 : t->bad_p = from->bad_p;
841 : 579700969 : t->base_p = false;
842 : 579700969 : return t;
843 : : }
844 : :
845 : : /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
846 : : specialization of std::initializer_list<T>, if such a conversion is
847 : : possible. */
848 : :
849 : : static conversion *
850 : 67778 : build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
851 : : {
852 : 67778 : tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
853 : 67778 : unsigned len = CONSTRUCTOR_NELTS (ctor);
854 : 67778 : conversion **subconvs = alloc_conversions (len);
855 : 67778 : conversion *t;
856 : 67778 : unsigned i;
857 : 67778 : tree val;
858 : :
859 : : /* Within a list-initialization we can have more user-defined
860 : : conversions. */
861 : 67778 : flags &= ~LOOKUP_NO_CONVERSION;
862 : : /* But no narrowing conversions. */
863 : 67778 : flags |= LOOKUP_NO_NARROWING;
864 : :
865 : : /* Can't make an array of these types. */
866 : 67778 : if (TYPE_REF_P (elttype)
867 : 67772 : || TREE_CODE (elttype) == FUNCTION_TYPE
868 : 67772 : || VOID_TYPE_P (elttype))
869 : : return NULL;
870 : :
871 : 213933 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
872 : : {
873 : 156719 : if (TREE_CODE (val) == RAW_DATA_CST)
874 : : {
875 : 51 : tree elt
876 : 51 : = build_int_cst (TREE_TYPE (val), RAW_DATA_UCHAR_ELT (val, 0));
877 : 51 : conversion *sub
878 : 51 : = implicit_conversion (elttype, TREE_TYPE (val), elt,
879 : : false, flags, complain);
880 : 51 : conversion *next;
881 : 51 : if (sub == NULL)
882 : : return NULL;
883 : : /* For conversion to initializer_list<unsigned char> or
884 : : initializer_list<char> or initializer_list<signed char>
885 : : we can optimize and keep RAW_DATA_CST with adjusted
886 : : type if we report narrowing errors if needed.
887 : : Use just one subconversion for that case. */
888 : 69 : if (sub->kind == ck_std
889 : 24 : && sub->type
890 : 24 : && (TREE_CODE (sub->type) == INTEGER_TYPE
891 : 6 : || is_byte_access_type (sub->type))
892 : 18 : && TYPE_PRECISION (sub->type) == CHAR_BIT
893 : 18 : && (next = next_conversion (sub))
894 : 69 : && next->kind == ck_identity)
895 : : {
896 : 18 : subconvs[i] = sub;
897 : 18 : continue;
898 : : }
899 : : /* Otherwise. build separate subconv for each RAW_DATA_CST
900 : : byte. Wrap those into an artificial ck_list which convert_like
901 : : will then handle. */
902 : 33 : conversion **subsubconvs = alloc_conversions (RAW_DATA_LENGTH (val));
903 : 33 : unsigned int j;
904 : 33 : subsubconvs[0] = sub;
905 : 15849 : for (j = 1; j < (unsigned) RAW_DATA_LENGTH (val); ++j)
906 : : {
907 : 15816 : elt = build_int_cst (TREE_TYPE (val),
908 : 15816 : RAW_DATA_UCHAR_ELT (val, j));
909 : 15816 : sub = implicit_conversion (elttype, TREE_TYPE (val), elt,
910 : : false, flags, complain);
911 : 15816 : if (sub == NULL)
912 : : return NULL;
913 : 15816 : subsubconvs[j] = sub;
914 : : }
915 : :
916 : 33 : t = alloc_conversion (ck_list);
917 : 33 : t->type = type;
918 : 33 : t->u.list = subsubconvs;
919 : 33 : t->rank = cr_exact;
920 : 15882 : for (j = 0; j < (unsigned) RAW_DATA_LENGTH (val); ++j)
921 : : {
922 : 15849 : sub = subsubconvs[j];
923 : 15849 : if (sub->rank > t->rank)
924 : 6 : t->rank = sub->rank;
925 : 15849 : if (sub->user_conv_p)
926 : 12063 : t->user_conv_p = true;
927 : 15849 : if (sub->bad_p)
928 : 0 : t->bad_p = true;
929 : : }
930 : 33 : subconvs[i] = t;
931 : 33 : continue;
932 : 33 : }
933 : :
934 : 156668 : conversion *sub
935 : 156668 : = implicit_conversion (elttype, TREE_TYPE (val), val,
936 : : false, flags, complain);
937 : 156668 : if (sub == NULL)
938 : : return NULL;
939 : :
940 : 146110 : subconvs[i] = sub;
941 : : }
942 : :
943 : 57214 : t = alloc_conversion (ck_list);
944 : 57214 : t->type = type;
945 : 57214 : t->u.list = subconvs;
946 : 57214 : t->rank = cr_exact;
947 : :
948 : 183813 : for (i = 0; i < len; ++i)
949 : : {
950 : 126599 : conversion *sub = subconvs[i];
951 : 126599 : if (sub->rank > t->rank)
952 : 51548 : t->rank = sub->rank;
953 : 126599 : if (sub->user_conv_p)
954 : 10675 : t->user_conv_p = true;
955 : 126599 : if (sub->bad_p)
956 : 52242 : t->bad_p = true;
957 : : }
958 : :
959 : : return t;
960 : : }
961 : :
962 : : /* Return the next conversion of the conversion chain (if applicable),
963 : : or NULL otherwise. Please use this function instead of directly
964 : : accessing fields of struct conversion. */
965 : :
966 : : static conversion *
967 : 472298021 : next_conversion (conversion *conv)
968 : : {
969 : 472298021 : if (conv == NULL
970 : 472298021 : || !has_next (conv->kind))
971 : : return NULL;
972 : 464775400 : return conv->u.next;
973 : : }
974 : :
975 : : /* Strip to the first ck_user, ck_ambig, ck_list, ck_aggr or ck_identity
976 : : encountered. */
977 : :
978 : : static conversion *
979 : 836915 : strip_standard_conversion (conversion *conv)
980 : : {
981 : 836915 : while (conv
982 : 839654 : && conv->kind != ck_user
983 : 864635 : && has_next (conv->kind))
984 : 2739 : conv = next_conversion (conv);
985 : 836915 : return conv;
986 : : }
987 : :
988 : : /* Subroutine of build_aggr_conv: check whether FROM is a valid aggregate
989 : : initializer for array type ATYPE. */
990 : :
991 : : static bool
992 : 21840 : can_convert_array (tree atype, tree from, int flags, tsubst_flags_t complain)
993 : : {
994 : 21840 : tree elttype = TREE_TYPE (atype);
995 : 21840 : unsigned i;
996 : :
997 : 21840 : if (TREE_CODE (from) == CONSTRUCTOR)
998 : : {
999 : 26021 : for (i = 0; i < CONSTRUCTOR_NELTS (from); ++i)
1000 : : {
1001 : 4226 : tree val = CONSTRUCTOR_ELT (from, i)->value;
1002 : 4226 : bool ok;
1003 : 4226 : if (TREE_CODE (elttype) == ARRAY_TYPE)
1004 : 18 : ok = can_convert_array (elttype, val, flags, complain);
1005 : : else
1006 : 4208 : ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags,
1007 : : complain);
1008 : 4226 : if (!ok)
1009 : : return false;
1010 : : }
1011 : : return true;
1012 : : }
1013 : :
1014 : 45 : if (char_type_p (TYPE_MAIN_VARIANT (elttype))
1015 : 45 : && TREE_CODE (tree_strip_any_location_wrapper (from)) == STRING_CST)
1016 : 45 : return array_string_literal_compatible_p (atype, from);
1017 : :
1018 : : /* No other valid way to aggregate initialize an array. */
1019 : : return false;
1020 : : }
1021 : :
1022 : : /* Helper for build_aggr_conv. Return true if FIELD is in PSET, or if
1023 : : FIELD has ANON_AGGR_TYPE_P and any initializable field in there recursively
1024 : : is in PSET. */
1025 : :
1026 : : static bool
1027 : 686631 : field_in_pset (hash_set<tree, true> &pset, tree field)
1028 : : {
1029 : 686631 : if (pset.contains (field))
1030 : : return true;
1031 : 112 : if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1032 : 0 : for (field = TYPE_FIELDS (TREE_TYPE (field));
1033 : 0 : field; field = DECL_CHAIN (field))
1034 : : {
1035 : 0 : field = next_aggregate_field (field);
1036 : 0 : if (field == NULL_TREE)
1037 : : break;
1038 : 0 : if (field_in_pset (pset, field))
1039 : : return true;
1040 : : }
1041 : : return false;
1042 : : }
1043 : :
1044 : : /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
1045 : : aggregate class, if such a conversion is possible. */
1046 : :
1047 : : static conversion *
1048 : 1156978 : build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
1049 : : {
1050 : 1156978 : unsigned HOST_WIDE_INT i = 0;
1051 : 1156978 : conversion *c;
1052 : 1156978 : tree field = next_aggregate_field (TYPE_FIELDS (type));
1053 : 1156978 : tree empty_ctor = NULL_TREE;
1054 : 1156978 : hash_set<tree, true> pset;
1055 : :
1056 : : /* We already called reshape_init in implicit_conversion, but it might not
1057 : : have done anything in the case of parenthesized aggr init. */
1058 : :
1059 : : /* The conversions within the init-list aren't affected by the enclosing
1060 : : context; they're always simple copy-initialization. */
1061 : 1156978 : flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1062 : :
1063 : : /* For designated initializers, verify that each initializer is convertible
1064 : : to corresponding TREE_TYPE (ce->index) and mark those FIELD_DECLs as
1065 : : visited. In the following loop then ignore already visited
1066 : : FIELD_DECLs. */
1067 : 1156978 : tree idx, val;
1068 : 1843497 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, val)
1069 : : {
1070 : 686560 : if (!idx)
1071 : : break;
1072 : :
1073 : 686553 : gcc_checking_assert (TREE_CODE (idx) == FIELD_DECL);
1074 : :
1075 : 686553 : tree ftype = TREE_TYPE (idx);
1076 : 686553 : bool ok;
1077 : :
1078 : 686553 : if (TREE_CODE (ftype) == ARRAY_TYPE)
1079 : 1640 : ok = can_convert_array (ftype, val, flags, complain);
1080 : : else
1081 : 684913 : ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
1082 : : complain);
1083 : :
1084 : 686553 : if (!ok)
1085 : : return NULL;
1086 : :
1087 : : /* For unions, there should be just one initializer. */
1088 : 686542 : if (TREE_CODE (type) == UNION_TYPE)
1089 : : {
1090 : : field = NULL_TREE;
1091 : : i = 1;
1092 : : break;
1093 : : }
1094 : 686519 : pset.add (idx);
1095 : : }
1096 : :
1097 : 1927114 : for (; field; field = next_aggregate_field (DECL_CHAIN (field)))
1098 : : {
1099 : 770399 : tree ftype = TREE_TYPE (field);
1100 : 770399 : bool ok;
1101 : :
1102 : 770399 : if (!pset.is_empty () && field_in_pset (pset, field))
1103 : 686519 : continue;
1104 : 83880 : if (i < CONSTRUCTOR_NELTS (ctor))
1105 : : {
1106 : 9 : constructor_elt *ce = CONSTRUCTOR_ELT (ctor, i);
1107 : 9 : gcc_checking_assert (!ce->index);
1108 : 9 : val = ce->value;
1109 : 9 : ++i;
1110 : : }
1111 : 83871 : else if (DECL_INITIAL (field))
1112 : 347 : val = get_nsdmi (field, /*ctor*/false, complain);
1113 : 83524 : else if (TYPE_REF_P (ftype))
1114 : : /* Value-initialization of reference is ill-formed. */
1115 : : return NULL;
1116 : : else
1117 : : {
1118 : 83518 : if (empty_ctor == NULL_TREE)
1119 : 38108 : empty_ctor = build_constructor (init_list_type_node, NULL);
1120 : : val = empty_ctor;
1121 : : }
1122 : :
1123 : 83874 : if (TREE_CODE (ftype) == ARRAY_TYPE)
1124 : 20182 : ok = can_convert_array (ftype, val, flags, complain);
1125 : : else
1126 : 63692 : ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
1127 : : complain);
1128 : :
1129 : 83874 : if (!ok)
1130 : : return NULL;
1131 : :
1132 : 83866 : if (TREE_CODE (type) == UNION_TYPE)
1133 : : break;
1134 : : }
1135 : :
1136 : 1156953 : if (i < CONSTRUCTOR_NELTS (ctor))
1137 : : return NULL;
1138 : :
1139 : 1156952 : c = alloc_conversion (ck_aggr);
1140 : 1156952 : c->type = type;
1141 : 1156952 : c->rank = cr_exact;
1142 : 1156952 : c->user_conv_p = true;
1143 : 1156952 : c->check_narrowing = true;
1144 : 1156952 : c->u.expr = ctor;
1145 : 1156952 : return c;
1146 : 1156978 : }
1147 : :
1148 : : /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
1149 : : array type, if such a conversion is possible. */
1150 : :
1151 : : static conversion *
1152 : 1052 : build_array_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
1153 : : {
1154 : 1052 : conversion *c;
1155 : 1052 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1156 : 1052 : tree elttype = TREE_TYPE (type);
1157 : 1052 : bool bad = false;
1158 : 1052 : bool user = false;
1159 : 1052 : enum conversion_rank rank = cr_exact;
1160 : :
1161 : : /* We might need to propagate the size from the element to the array. */
1162 : 1052 : complete_type (type);
1163 : :
1164 : 1052 : if (TYPE_DOMAIN (type)
1165 : 1052 : && !variably_modified_type_p (TYPE_DOMAIN (type), NULL_TREE))
1166 : : {
1167 : 982 : unsigned HOST_WIDE_INT alen = tree_to_uhwi (array_type_nelts_top (type));
1168 : 982 : if (alen < len)
1169 : : return NULL;
1170 : : }
1171 : :
1172 : 1037 : flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1173 : :
1174 : 5186 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
1175 : : {
1176 : 2217 : conversion *sub
1177 : 2217 : = implicit_conversion (elttype, TREE_TYPE (e.value), e.value,
1178 : : false, flags, complain);
1179 : 2217 : if (sub == NULL)
1180 : : return NULL;
1181 : :
1182 : 2171 : if (sub->rank > rank)
1183 : 198 : rank = sub->rank;
1184 : 2171 : if (sub->user_conv_p)
1185 : 23 : user = true;
1186 : 2171 : if (sub->bad_p)
1187 : 129 : bad = true;
1188 : : }
1189 : :
1190 : 991 : c = alloc_conversion (ck_aggr);
1191 : 991 : c->type = type;
1192 : 991 : c->rank = rank;
1193 : 991 : c->user_conv_p = user;
1194 : 991 : c->bad_p = bad;
1195 : 991 : c->u.expr = ctor;
1196 : 991 : return c;
1197 : : }
1198 : :
1199 : : /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
1200 : : complex type, if such a conversion is possible. */
1201 : :
1202 : : static conversion *
1203 : 59110 : build_complex_conv (tree type, tree ctor, int flags,
1204 : : tsubst_flags_t complain)
1205 : : {
1206 : 59110 : conversion *c;
1207 : 59110 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1208 : 59110 : tree elttype = TREE_TYPE (type);
1209 : 59110 : bool bad = false;
1210 : 59110 : bool user = false;
1211 : 59110 : enum conversion_rank rank = cr_exact;
1212 : :
1213 : 59110 : if (len != 2)
1214 : : return NULL;
1215 : :
1216 : 59070 : flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1217 : :
1218 : 295350 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
1219 : : {
1220 : 118140 : conversion *sub
1221 : 118140 : = implicit_conversion (elttype, TREE_TYPE (e.value), e.value,
1222 : : false, flags, complain);
1223 : 118140 : if (sub == NULL)
1224 : : return NULL;
1225 : :
1226 : 118140 : if (sub->rank > rank)
1227 : 44 : rank = sub->rank;
1228 : 118140 : if (sub->user_conv_p)
1229 : 0 : user = true;
1230 : 118140 : if (sub->bad_p)
1231 : 0 : bad = true;
1232 : : }
1233 : :
1234 : 59070 : c = alloc_conversion (ck_aggr);
1235 : 59070 : c->type = type;
1236 : 59070 : c->rank = rank;
1237 : 59070 : c->user_conv_p = user;
1238 : 59070 : c->bad_p = bad;
1239 : 59070 : c->u.expr = ctor;
1240 : 59070 : return c;
1241 : : }
1242 : :
1243 : : /* Build a representation of the identity conversion from EXPR to
1244 : : itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
1245 : :
1246 : : static conversion *
1247 : 1402845817 : build_identity_conv (tree type, tree expr)
1248 : : {
1249 : 1402845817 : conversion *c;
1250 : :
1251 : 0 : c = alloc_conversion (ck_identity);
1252 : 1402845817 : c->type = type;
1253 : 1402845817 : c->u.expr = expr;
1254 : :
1255 : 1402845817 : return c;
1256 : : }
1257 : :
1258 : : /* Converting from EXPR to TYPE was ambiguous in the sense that there
1259 : : were multiple user-defined conversions to accomplish the job.
1260 : : Build a conversion that indicates that ambiguity. */
1261 : :
1262 : : static conversion *
1263 : 1335 : build_ambiguous_conv (tree type, tree expr)
1264 : : {
1265 : 1335 : conversion *c;
1266 : :
1267 : 0 : c = alloc_conversion (ck_ambig);
1268 : 1335 : c->type = type;
1269 : 1335 : c->u.expr = expr;
1270 : :
1271 : 1335 : return c;
1272 : : }
1273 : :
1274 : : tree
1275 : 2551450962 : strip_top_quals (tree t)
1276 : : {
1277 : 2551450962 : if (TREE_CODE (t) == ARRAY_TYPE)
1278 : : return t;
1279 : 2545998579 : return cp_build_qualified_type (t, 0);
1280 : : }
1281 : :
1282 : : /* Returns the standard conversion path (see [conv]) from type FROM to type
1283 : : TO, if any. For proper handling of null pointer constants, you must
1284 : : also pass the expression EXPR to convert from. If C_CAST_P is true,
1285 : : this conversion is coming from a C-style cast. */
1286 : :
1287 : : static conversion *
1288 : 1136050001 : standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
1289 : : int flags, tsubst_flags_t complain)
1290 : : {
1291 : 1136050001 : enum tree_code fcode, tcode;
1292 : 1136050001 : conversion *conv;
1293 : 1136050001 : bool fromref = false;
1294 : 1136050001 : tree qualified_to;
1295 : :
1296 : 1136050001 : to = non_reference (to);
1297 : 1136050001 : if (TYPE_REF_P (from))
1298 : : {
1299 : 70348 : fromref = true;
1300 : 70348 : from = TREE_TYPE (from);
1301 : : }
1302 : 1136050001 : qualified_to = to;
1303 : 1136050001 : to = strip_top_quals (to);
1304 : 1136050001 : from = strip_top_quals (from);
1305 : :
1306 : 1136050001 : if (expr && type_unknown_p (expr))
1307 : : {
1308 : 286548 : if (TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
1309 : : {
1310 : 37294 : tsubst_flags_t tflags = tf_conv;
1311 : 37294 : expr = instantiate_type (to, expr, tflags);
1312 : 37294 : if (expr == error_mark_node)
1313 : : return NULL;
1314 : 22889 : from = TREE_TYPE (expr);
1315 : : }
1316 : 249254 : else if (TREE_CODE (to) == BOOLEAN_TYPE)
1317 : : {
1318 : : /* Necessary for eg, TEMPLATE_ID_EXPRs (c++/50961). */
1319 : 7002 : expr = resolve_nondeduced_context (expr, complain);
1320 : 7002 : from = TREE_TYPE (expr);
1321 : : }
1322 : : }
1323 : :
1324 : 1136035596 : fcode = TREE_CODE (from);
1325 : 1136035596 : tcode = TREE_CODE (to);
1326 : :
1327 : 1136035596 : conv = build_identity_conv (from, expr);
1328 : 1136035596 : if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
1329 : : {
1330 : 5482127 : from = type_decays_to (from);
1331 : 5482127 : fcode = TREE_CODE (from);
1332 : : /* Tell convert_like that we're using the address. */
1333 : 5482127 : conv->rvaluedness_matches_p = true;
1334 : 5482127 : conv = build_conv (ck_lvalue, from, conv);
1335 : : }
1336 : : /* Wrapping a ck_rvalue around a class prvalue (as a result of using
1337 : : obvalue_p) seems odd, since it's already a prvalue, but that's how we
1338 : : express the copy constructor call required by copy-initialization. */
1339 : 1130553469 : else if (fromref || (expr && obvalue_p (expr)))
1340 : : {
1341 : 277326321 : if (expr)
1342 : : {
1343 : 277255985 : tree bitfield_type;
1344 : 277255985 : bitfield_type = is_bitfield_expr_with_lowered_type (expr);
1345 : 277255985 : if (bitfield_type)
1346 : : {
1347 : 4733793 : from = strip_top_quals (bitfield_type);
1348 : 4733793 : fcode = TREE_CODE (from);
1349 : : }
1350 : : }
1351 : 277326321 : conv = build_conv (ck_rvalue, from, conv);
1352 : : /* If we're performing copy-initialization, remember to skip
1353 : : explicit constructors. */
1354 : 277326321 : if (flags & LOOKUP_ONLYCONVERTING)
1355 : 240948361 : conv->copy_init_p = true;
1356 : : }
1357 : :
1358 : : /* Allow conversion between `__complex__' data types. */
1359 : 1136035596 : if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
1360 : : {
1361 : : /* The standard conversion sequence to convert FROM to TO is
1362 : : the standard conversion sequence to perform componentwise
1363 : : conversion. */
1364 : 2593799 : conversion *part_conv = standard_conversion
1365 : 2593799 : (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags,
1366 : : complain);
1367 : :
1368 : 2593799 : if (!part_conv)
1369 : : conv = NULL;
1370 : 2593799 : else if (part_conv->kind == ck_identity)
1371 : : /* Leave conv alone. */;
1372 : : else
1373 : : {
1374 : 361046 : conv = build_conv (part_conv->kind, to, conv);
1375 : 361046 : conv->rank = part_conv->rank;
1376 : : }
1377 : :
1378 : 2593799 : return conv;
1379 : : }
1380 : :
1381 : 1133441797 : if (same_type_p (from, to))
1382 : : {
1383 : 739608346 : if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue)
1384 : 13884799 : conv->type = qualified_to;
1385 : 725723547 : else if (from != to)
1386 : : /* Use TO in order to not lose TO in diagnostics. */
1387 : 60178646 : conv->type = to;
1388 : 739608346 : return conv;
1389 : : }
1390 : :
1391 : : /* [conv.ptr]
1392 : : A null pointer constant can be converted to a pointer type; ... A
1393 : : null pointer constant of integral type can be converted to an
1394 : : rvalue of type std::nullptr_t. */
1395 : 250854252 : if ((tcode == POINTER_TYPE || TYPE_PTRMEM_P (to)
1396 : 250762651 : || NULLPTR_TYPE_P (to))
1397 : 397425428 : && ((expr && null_ptr_cst_p (expr))
1398 : 142645433 : || NULLPTR_TYPE_P (from)))
1399 : 3925747 : conv = build_conv (ck_std, to, conv);
1400 : 389907704 : else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
1401 : 389416610 : || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
1402 : : {
1403 : : /* For backwards brain damage compatibility, allow interconversion of
1404 : : pointers and integers with a pedwarn. */
1405 : 1734559 : conv = build_conv (ck_std, to, conv);
1406 : 1734559 : conv->bad_p = true;
1407 : : }
1408 : 388173145 : else if (UNSCOPED_ENUM_P (to) && fcode == INTEGER_TYPE)
1409 : : {
1410 : : /* For backwards brain damage compatibility, allow interconversion of
1411 : : enums and integers with a pedwarn. */
1412 : 401354 : conv = build_conv (ck_std, to, conv);
1413 : 401354 : conv->bad_p = true;
1414 : : }
1415 : 387771791 : else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
1416 : 253568489 : || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from)))
1417 : : {
1418 : 635 : tree to_pointee;
1419 : 635 : tree from_pointee;
1420 : :
1421 : 635 : if (tcode == POINTER_TYPE)
1422 : : {
1423 : 134203302 : to_pointee = TREE_TYPE (to);
1424 : 134203302 : from_pointee = TREE_TYPE (from);
1425 : :
1426 : : /* Since this is the target of a pointer, it can't have function
1427 : : qualifiers, so any TYPE_QUALS must be for attributes const or
1428 : : noreturn. Strip them. */
1429 : 134203302 : if (TREE_CODE (to_pointee) == FUNCTION_TYPE
1430 : 134203302 : && TYPE_QUALS (to_pointee))
1431 : 9 : to_pointee = build_qualified_type (to_pointee, TYPE_UNQUALIFIED);
1432 : 134203302 : if (TREE_CODE (from_pointee) == FUNCTION_TYPE
1433 : 134203302 : && TYPE_QUALS (from_pointee))
1434 : 18 : from_pointee = build_qualified_type (from_pointee, TYPE_UNQUALIFIED);
1435 : : }
1436 : : else
1437 : : {
1438 : 635 : to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
1439 : 635 : from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
1440 : : }
1441 : :
1442 : 134203937 : if (tcode == POINTER_TYPE
1443 : 134203937 : && same_type_ignoring_top_level_qualifiers_p (from_pointee,
1444 : : to_pointee))
1445 : : ;
1446 : 92762355 : else if (VOID_TYPE_P (to_pointee)
1447 : 3598326 : && !TYPE_PTRDATAMEM_P (from)
1448 : 3598326 : && TREE_CODE (from_pointee) != FUNCTION_TYPE)
1449 : : {
1450 : 3598043 : tree nfrom = TREE_TYPE (from);
1451 : : /* Don't try to apply restrict to void. */
1452 : 3598043 : int quals = cp_type_quals (nfrom) & ~TYPE_QUAL_RESTRICT;
1453 : 3598043 : from_pointee = cp_build_qualified_type (void_type_node, quals);
1454 : 3598043 : from = build_pointer_type (from_pointee);
1455 : 3598043 : conv = build_conv (ck_ptr, from, conv);
1456 : 3598043 : }
1457 : 89164312 : else if (TYPE_PTRDATAMEM_P (from))
1458 : : {
1459 : 635 : tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
1460 : 635 : tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
1461 : :
1462 : 635 : if (same_type_p (fbase, tbase))
1463 : : /* No base conversion needed. */;
1464 : 550 : else if (DERIVED_FROM_P (fbase, tbase)
1465 : 948 : && (same_type_ignoring_top_level_qualifiers_p
1466 : 398 : (from_pointee, to_pointee)))
1467 : : {
1468 : 374 : from = build_ptrmem_type (tbase, from_pointee);
1469 : 374 : conv = build_conv (ck_pmem, from, conv);
1470 : : }
1471 : : else
1472 : 176 : return NULL;
1473 : : }
1474 : 38758930 : else if (CLASS_TYPE_P (from_pointee)
1475 : 38756915 : && CLASS_TYPE_P (to_pointee)
1476 : : /* [conv.ptr]
1477 : :
1478 : : An rvalue of type "pointer to cv D," where D is a
1479 : : class type, can be converted to an rvalue of type
1480 : : "pointer to cv B," where B is a base class (clause
1481 : : _class.derived_) of D. If B is an inaccessible
1482 : : (clause _class.access_) or ambiguous
1483 : : (_class.member.lookup_) base class of D, a program
1484 : : that necessitates this conversion is ill-formed.
1485 : : Therefore, we use DERIVED_FROM_P, and do not check
1486 : : access or uniqueness. */
1487 : 127489283 : && DERIVED_FROM_P (to_pointee, from_pointee))
1488 : : {
1489 : 5270551 : from_pointee
1490 : 5270551 : = cp_build_qualified_type (to_pointee,
1491 : : cp_type_quals (from_pointee));
1492 : 5270551 : from = build_pointer_type (from_pointee);
1493 : 5270551 : conv = build_conv (ck_ptr, from, conv);
1494 : 5270551 : conv->base_p = true;
1495 : : }
1496 : :
1497 : 134203761 : if (same_type_p (from, to))
1498 : : /* OK */;
1499 : 127311804 : else if (c_cast_p && comp_ptr_ttypes_const (to, from, bounds_either))
1500 : : /* In a C-style cast, we ignore CV-qualification because we
1501 : : are allowed to perform a static_cast followed by a
1502 : : const_cast. */
1503 : 622 : conv = build_conv (ck_qual, to, conv);
1504 : 127311182 : else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
1505 : 37712617 : conv = build_conv (ck_qual, to, conv);
1506 : 89598565 : else if (expr && string_conv_p (to, expr, 0))
1507 : : /* converting from string constant to char *. */
1508 : 204 : conv = build_conv (ck_qual, to, conv);
1509 : 89598361 : else if (fnptr_conv_p (to, from))
1510 : 233753 : conv = build_conv (ck_fnptr, to, conv);
1511 : : /* Allow conversions among compatible ObjC pointer types (base
1512 : : conversions have been already handled above). */
1513 : 89364608 : else if (c_dialect_objc ()
1514 : 89364608 : && objc_compare_types (to, from, -4, NULL_TREE))
1515 : 0 : conv = build_conv (ck_ptr, to, conv);
1516 : 89364608 : else if (ptr_reasonably_similar (to_pointee, from_pointee))
1517 : : {
1518 : 6775315 : conv = build_conv (ck_ptr, to, conv);
1519 : 6775315 : conv->bad_p = true;
1520 : : }
1521 : : else
1522 : : return NULL;
1523 : :
1524 : 179599039 : from = to;
1525 : : }
1526 : 253567854 : else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
1527 : : {
1528 : 89535 : tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
1529 : 89535 : tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
1530 : 89535 : tree fbase = class_of_this_parm (fromfn);
1531 : 89535 : tree tbase = class_of_this_parm (tofn);
1532 : :
1533 : : /* If FBASE and TBASE are equivalent but incomplete, DERIVED_FROM_P
1534 : : yields false. But a pointer to member of incomplete class is OK. */
1535 : 89535 : if (!same_type_p (fbase, tbase) && !DERIVED_FROM_P (fbase, tbase))
1536 : : return NULL;
1537 : :
1538 : 89374 : tree fstat = static_fn_type (fromfn);
1539 : 89374 : tree tstat = static_fn_type (tofn);
1540 : 89374 : if (same_type_p (tstat, fstat)
1541 : 89374 : || fnptr_conv_p (tstat, fstat))
1542 : : /* OK */;
1543 : : else
1544 : : return NULL;
1545 : :
1546 : 89106 : if (!same_type_p (fbase, tbase))
1547 : : {
1548 : 89039 : from = build_memfn_type (fstat,
1549 : : tbase,
1550 : : cp_type_quals (tbase),
1551 : : type_memfn_rqual (tofn));
1552 : 89039 : from = build_ptrmemfunc_type (build_pointer_type (from));
1553 : 89039 : conv = build_conv (ck_pmem, from, conv);
1554 : 89039 : conv->base_p = true;
1555 : : }
1556 : 89106 : if (fnptr_conv_p (tstat, fstat))
1557 : 67 : conv = build_conv (ck_fnptr, to, conv);
1558 : : }
1559 : 253478319 : else if (tcode == BOOLEAN_TYPE)
1560 : : {
1561 : : /* [conv.bool]
1562 : :
1563 : : A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
1564 : : to member type can be converted to a prvalue of type bool. ...
1565 : : For direct-initialization (8.5 [dcl.init]), a prvalue of type
1566 : : std::nullptr_t can be converted to a prvalue of type bool; */
1567 : 5955959 : if (ARITHMETIC_TYPE_P (from)
1568 : 5865979 : || UNSCOPED_ENUM_P (from)
1569 : 3573904 : || fcode == POINTER_TYPE
1570 : 2567623 : || TYPE_PTRMEM_P (from)
1571 : 13468520 : || NULLPTR_TYPE_P (from))
1572 : : {
1573 : 8333787 : conv = build_conv (ck_std, to, conv);
1574 : 8333787 : if (fcode == POINTER_TYPE
1575 : 7327506 : || TYPE_PTRDATAMEM_P (from)
1576 : 7327364 : || (TYPE_PTRMEMFUNC_P (from)
1577 : 77 : && conv->rank < cr_pbool)
1578 : 15661074 : || NULLPTR_TYPE_P (from))
1579 : 1006575 : conv->rank = cr_pbool;
1580 : 8333787 : if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING))
1581 : 36 : conv->bad_p = true;
1582 : 8333787 : if (flags & LOOKUP_NO_NARROWING)
1583 : 32443 : conv->check_narrowing = true;
1584 : 8333787 : return conv;
1585 : : }
1586 : :
1587 : : return NULL;
1588 : : }
1589 : : /* We don't check for ENUMERAL_TYPE here because there are no standard
1590 : : conversions to enum type. */
1591 : : /* As an extension, allow conversion to complex type. */
1592 : 242577203 : else if (ARITHMETIC_TYPE_P (to))
1593 : : {
1594 : 31072394 : if (! (INTEGRAL_CODE_P (fcode)
1595 : 27460989 : || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL)))
1596 : 153540731 : || SCOPED_ENUM_P (from))
1597 : : return NULL;
1598 : :
1599 : : /* If we're parsing an enum with no fixed underlying type, we're
1600 : : dealing with an incomplete type, which renders the conversion
1601 : : ill-formed. */
1602 : 121354571 : if (!COMPLETE_TYPE_P (from))
1603 : : return NULL;
1604 : :
1605 : 121354565 : conv = build_conv (ck_std, to, conv);
1606 : :
1607 : 121354565 : tree underlying_type = NULL_TREE;
1608 : 121354565 : if (TREE_CODE (from) == ENUMERAL_TYPE
1609 : 121354565 : && ENUM_FIXED_UNDERLYING_TYPE_P (from))
1610 : 2699495 : underlying_type = ENUM_UNDERLYING_TYPE (from);
1611 : :
1612 : : /* Give this a better rank if it's a promotion.
1613 : :
1614 : : To handle CWG 1601, also bump the rank if we are converting
1615 : : an enumeration with a fixed underlying type to the underlying
1616 : : type. */
1617 : 121354565 : if ((same_type_p (to, type_promotes_to (from))
1618 : 109066254 : || (underlying_type && same_type_p (to, underlying_type)))
1619 : 121354584 : && next_conversion (conv)->rank <= cr_promotion)
1620 : 12288330 : conv->rank = cr_promotion;
1621 : :
1622 : : /* A prvalue of floating-point type can be converted to a prvalue of
1623 : : another floating-point type with a greater or equal conversion
1624 : : rank ([conv.rank]). A prvalue of standard floating-point type can
1625 : : be converted to a prvalue of another standard floating-point type.
1626 : : For backwards compatibility with handling __float128 and other
1627 : : non-standard floating point types, allow all implicit floating
1628 : : point conversions if neither type is extended floating-point
1629 : : type and if at least one of them is, fail if they have unordered
1630 : : conversion rank or from has higher conversion rank. */
1631 : 121354565 : if (fcode == REAL_TYPE
1632 : 121354565 : && tcode == REAL_TYPE
1633 : 19080625 : && (extended_float_type_p (from)
1634 : 18107400 : || extended_float_type_p (to))
1635 : 126873627 : && cp_compare_floating_point_conversion_ranks (from, to) >= 2)
1636 : 2600014 : conv->bad_p = true;
1637 : : }
1638 : 116497461 : else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
1639 : 116497461 : && vector_types_convertible_p (from, to, false))
1640 : 4244 : return build_conv (ck_std, to, conv);
1641 : 116493217 : else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
1642 : 39348533 : && is_properly_derived_from (from, to))
1643 : : {
1644 : 479240 : if (conv->kind == ck_rvalue)
1645 : 479189 : conv = next_conversion (conv);
1646 : 479240 : conv = build_conv (ck_base, to, conv);
1647 : : /* The derived-to-base conversion indicates the initialization
1648 : : of a parameter with base type from an object of a derived
1649 : : type. A temporary object is created to hold the result of
1650 : : the conversion unless we're binding directly to a reference. */
1651 : 479240 : conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
1652 : : /* If we're performing copy-initialization, remember to skip
1653 : : explicit constructors. */
1654 : 479240 : if (flags & LOOKUP_ONLYCONVERTING)
1655 : 479173 : conv->copy_init_p = true;
1656 : : }
1657 : : else
1658 : 116013977 : return NULL;
1659 : :
1660 : 179599039 : if (flags & LOOKUP_NO_NARROWING)
1661 : 25816438 : conv->check_narrowing = true;
1662 : :
1663 : : return conv;
1664 : : }
1665 : :
1666 : : /* Returns nonzero if T1 is reference-related to T2.
1667 : :
1668 : : This is considered when a reference to T1 is initialized by a T2. */
1669 : :
1670 : : bool
1671 : 183752431 : reference_related_p (tree t1, tree t2)
1672 : : {
1673 : 183752431 : if (t1 == error_mark_node || t2 == error_mark_node)
1674 : : return false;
1675 : :
1676 : 183752427 : t1 = TYPE_MAIN_VARIANT (t1);
1677 : 183752427 : t2 = TYPE_MAIN_VARIANT (t2);
1678 : :
1679 : : /* [dcl.init.ref]
1680 : :
1681 : : Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
1682 : : to "cv2 T2" if T1 is similar to T2, or T1 is a base class of T2. */
1683 : 183752427 : return (similar_type_p (t1, t2)
1684 : 183752427 : || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1685 : 38813311 : && DERIVED_FROM_P (t1, t2)));
1686 : : }
1687 : :
1688 : : /* Returns nonzero if T1 is reference-compatible with T2. */
1689 : :
1690 : : bool
1691 : 162409752 : reference_compatible_p (tree t1, tree t2)
1692 : : {
1693 : : /* [dcl.init.ref]
1694 : :
1695 : : "cv1 T1" is reference compatible with "cv2 T2" if
1696 : : a prvalue of type "pointer to cv2 T2" can be converted to the type
1697 : : "pointer to cv1 T1" via a standard conversion sequence. */
1698 : 162409752 : tree ptype1 = build_pointer_type (t1);
1699 : 162409752 : tree ptype2 = build_pointer_type (t2);
1700 : 162409752 : conversion *conv = standard_conversion (ptype1, ptype2, NULL_TREE,
1701 : : /*c_cast_p=*/false, 0, tf_none);
1702 : 162409752 : if (!conv || conv->bad_p)
1703 : 87508665 : return false;
1704 : : return true;
1705 : : }
1706 : :
1707 : : /* Return true if converting FROM to TO would involve a qualification
1708 : : conversion. */
1709 : :
1710 : : static bool
1711 : 83182286 : involves_qualification_conversion_p (tree to, tree from)
1712 : : {
1713 : : /* If we're not convering a pointer to another one, we won't get
1714 : : a qualification conversion. */
1715 : 83182286 : if (!((TYPE_PTR_P (to) && TYPE_PTR_P (from))
1716 : 2675 : || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from))))
1717 : : return false;
1718 : :
1719 : 3036921 : conversion *conv = standard_conversion (to, from, NULL_TREE,
1720 : : /*c_cast_p=*/false, 0, tf_none);
1721 : 6044816 : for (conversion *t = conv; t; t = next_conversion (t))
1722 : 3036939 : if (t->kind == ck_qual)
1723 : : return true;
1724 : :
1725 : : return false;
1726 : : }
1727 : :
1728 : : /* Return true if HANDLER is a match for exception object with EXCEPT_TYPE as
1729 : : per [except.handle]/3. */
1730 : :
1731 : : bool
1732 : 417 : handler_match_for_exception_type (tree handler, tree except_type)
1733 : : {
1734 : 417 : tree handler_type = HANDLER_TYPE (handler);
1735 : 417 : if (handler_type == NULL_TREE)
1736 : : return true; /* ... */
1737 : 385 : if (same_type_ignoring_top_level_qualifiers_p (handler_type, except_type))
1738 : : return true;
1739 : 283 : if (CLASS_TYPE_P (except_type) && CLASS_TYPE_P (handler_type))
1740 : : {
1741 : 71 : base_kind b_kind;
1742 : 71 : tree binfo = lookup_base (except_type, handler_type, ba_check, &b_kind,
1743 : : tf_none);
1744 : 71 : if (binfo && binfo != error_mark_node)
1745 : 27 : return true;
1746 : : }
1747 : 256 : if (TYPE_PTR_P (handler_type) || TYPE_PTRDATAMEM_P (handler_type))
1748 : : {
1749 : 60 : if (TREE_CODE (except_type) == NULLPTR_TYPE)
1750 : : return true;
1751 : 54 : if ((TYPE_PTR_P (handler_type) && TYPE_PTR_P (except_type))
1752 : 9 : || (TYPE_PTRDATAMEM_P (handler_type)
1753 : 0 : && TYPE_PTRDATAMEM_P (except_type)))
1754 : : {
1755 : 45 : conversion *conv
1756 : 45 : = standard_conversion (handler_type, except_type, NULL_TREE,
1757 : : /*c_cast_p=*/false, 0, tf_none);
1758 : 45 : if (conv && !conv->bad_p)
1759 : : {
1760 : 33 : for (conversion *t = conv; t; t = next_conversion (t))
1761 : 22 : switch (t->kind)
1762 : : {
1763 : 22 : case ck_ptr:
1764 : 22 : case ck_fnptr:
1765 : 22 : case ck_qual:
1766 : 22 : case ck_identity:
1767 : 22 : break;
1768 : : default:
1769 : : return false;
1770 : : }
1771 : : return true;
1772 : : }
1773 : : }
1774 : : }
1775 : : return false;
1776 : : }
1777 : :
1778 : : /* A reference of the indicated TYPE is being bound directly to the
1779 : : expression represented by the implicit conversion sequence CONV.
1780 : : Return a conversion sequence for this binding. */
1781 : :
1782 : : static conversion *
1783 : 86106293 : direct_reference_binding (tree type, conversion *conv)
1784 : : {
1785 : 86106293 : tree t;
1786 : :
1787 : 86106293 : gcc_assert (TYPE_REF_P (type));
1788 : 86106293 : gcc_assert (!TYPE_REF_P (conv->type));
1789 : :
1790 : 86106293 : t = TREE_TYPE (type);
1791 : :
1792 : 86106293 : if (conv->kind == ck_identity)
1793 : : /* Mark the identity conv as to not decay to rvalue. */
1794 : 86106293 : conv->rvaluedness_matches_p = true;
1795 : :
1796 : : /* [over.ics.rank]
1797 : :
1798 : : When a parameter of reference type binds directly
1799 : : (_dcl.init.ref_) to an argument expression, the implicit
1800 : : conversion sequence is the identity conversion, unless the
1801 : : argument expression has a type that is a derived class of the
1802 : : parameter type, in which case the implicit conversion sequence is
1803 : : a derived-to-base Conversion.
1804 : :
1805 : : If the parameter binds directly to the result of applying a
1806 : : conversion function to the argument expression, the implicit
1807 : : conversion sequence is a user-defined conversion sequence
1808 : : (_over.ics.user_), with the second standard conversion sequence
1809 : : either an identity conversion or, if the conversion function
1810 : : returns an entity of a type that is a derived class of the
1811 : : parameter type, a derived-to-base conversion. */
1812 : 86106293 : if (is_properly_derived_from (conv->type, t))
1813 : : {
1814 : : /* Represent the derived-to-base conversion. */
1815 : 2924007 : conv = build_conv (ck_base, t, conv);
1816 : : /* We will actually be binding to the base-class subobject in
1817 : : the derived class, so we mark this conversion appropriately.
1818 : : That way, convert_like knows not to generate a temporary. */
1819 : 2924007 : conv->need_temporary_p = false;
1820 : : }
1821 : 83182286 : else if (involves_qualification_conversion_p (t, conv->type))
1822 : : /* Represent the qualification conversion. After DR 2352
1823 : : #1 and #2 were indistinguishable conversion sequences:
1824 : :
1825 : : void f(int*); // #1
1826 : : void f(const int* const &); // #2
1827 : : void g(int* p) { f(p); }
1828 : :
1829 : : because the types "int *" and "const int *const" are
1830 : : reference-related and we were binding both directly and they
1831 : : had the same rank. To break it up, we add a ck_qual under the
1832 : : ck_ref_bind so that conversion sequence ranking chooses #1.
1833 : :
1834 : : We strip_top_quals here which is also what standard_conversion
1835 : : does. Failure to do so would confuse comp_cv_qual_signature
1836 : : into thinking that in
1837 : :
1838 : : void f(const int * const &); // #1
1839 : : void f(const int *); // #2
1840 : : int *x;
1841 : : f(x);
1842 : :
1843 : : #2 is a better match than #1 even though they're ambiguous (97296). */
1844 : 29044 : conv = build_conv (ck_qual, strip_top_quals (t), conv);
1845 : :
1846 : 86106293 : return build_conv (ck_ref_bind, type, conv);
1847 : : }
1848 : :
1849 : : /* Returns the conversion path from type FROM to reference type TO for
1850 : : purposes of reference binding. For lvalue binding, either pass a
1851 : : reference type to FROM or an lvalue expression to EXPR. If the
1852 : : reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1853 : : the conversion returned. If C_CAST_P is true, this
1854 : : conversion is coming from a C-style cast. */
1855 : :
1856 : : static conversion *
1857 : 161989213 : reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags,
1858 : : tsubst_flags_t complain)
1859 : : {
1860 : 161989213 : conversion *conv = NULL;
1861 : 161989213 : conversion *bad_direct_conv = nullptr;
1862 : 161989213 : tree to = TREE_TYPE (rto);
1863 : 161989213 : tree from = rfrom;
1864 : 161989213 : tree tfrom;
1865 : 161989213 : bool related_p;
1866 : 161989213 : bool compatible_p;
1867 : 161989213 : cp_lvalue_kind gl_kind;
1868 : 161989213 : bool is_lvalue;
1869 : :
1870 : 161989213 : if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1871 : : {
1872 : 48 : expr = instantiate_type (to, expr, tf_none);
1873 : 48 : if (expr == error_mark_node)
1874 : : return NULL;
1875 : 48 : from = TREE_TYPE (expr);
1876 : : }
1877 : :
1878 : 161989213 : bool copy_list_init = false;
1879 : 161989213 : bool single_list_conv = false;
1880 : 161989213 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1881 : : {
1882 : 187391 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
1883 : : /* DR 1288: Otherwise, if the initializer list has a single element
1884 : : of type E and ... [T's] referenced type is reference-related to E,
1885 : : the object or reference is initialized from that element...
1886 : :
1887 : : ??? With P0388R4, we should bind 't' directly to U{}:
1888 : : using U = A[2];
1889 : : A (&&t)[] = {U{}};
1890 : : because A[] and A[2] are reference-related. But we don't do it
1891 : : because grok_reference_init has deduced the array size (to 1), and
1892 : : A[1] and A[2] aren't reference-related. */
1893 : 187391 : if (CONSTRUCTOR_NELTS (expr) == 1
1894 : 50578 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
1895 : : {
1896 : 1609 : tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1897 : 1609 : if (error_operand_p (elt))
1898 : : return NULL;
1899 : 1604 : tree etype = TREE_TYPE (elt);
1900 : 1604 : if (reference_related_p (to, etype))
1901 : : {
1902 : 372 : expr = elt;
1903 : 372 : from = etype;
1904 : 372 : goto skip;
1905 : : }
1906 : 1232 : else if (CLASS_TYPE_P (etype) && TYPE_HAS_CONVERSION (etype))
1907 : : /* CWG1996: jason's proposed drafting adds "or initializing T from E
1908 : : would bind directly". We check that in the direct binding with
1909 : : conversion code below. */
1910 : : single_list_conv = true;
1911 : : }
1912 : : /* Otherwise, if T is a reference type, a prvalue temporary of the type
1913 : : referenced by T is copy-list-initialized, and the reference is bound
1914 : : to that temporary. */
1915 : : copy_list_init = true;
1916 : 161989208 : skip:;
1917 : : }
1918 : :
1919 : 161989208 : if (TYPE_REF_P (from))
1920 : : {
1921 : 51454 : from = TREE_TYPE (from);
1922 : 51454 : if (!TYPE_REF_IS_RVALUE (rfrom)
1923 : 51454 : || TREE_CODE (from) == FUNCTION_TYPE)
1924 : : gl_kind = clk_ordinary;
1925 : : else
1926 : : gl_kind = clk_rvalueref;
1927 : : }
1928 : 161937754 : else if (expr)
1929 : 159782778 : gl_kind = lvalue_kind (expr);
1930 : 2032115 : else if (CLASS_TYPE_P (from)
1931 : 2154976 : || TREE_CODE (from) == ARRAY_TYPE)
1932 : : gl_kind = clk_class;
1933 : : else
1934 : : gl_kind = clk_none;
1935 : :
1936 : : /* Don't allow a class prvalue when LOOKUP_NO_TEMP_BIND. */
1937 : 161989208 : if ((flags & LOOKUP_NO_TEMP_BIND)
1938 : 2138503 : && (gl_kind & clk_class))
1939 : : gl_kind = clk_none;
1940 : :
1941 : : /* Same mask as real_lvalue_p. */
1942 : 160247456 : is_lvalue = gl_kind && !(gl_kind & (clk_rvalueref|clk_class));
1943 : :
1944 : 138929323 : tfrom = from;
1945 : 138929323 : if ((gl_kind & clk_bitfield) != 0)
1946 : 3032348 : tfrom = unlowered_expr_type (expr);
1947 : :
1948 : : /* Figure out whether or not the types are reference-related and
1949 : : reference compatible. We have to do this after stripping
1950 : : references from FROM. */
1951 : 161989208 : related_p = reference_related_p (to, tfrom);
1952 : : /* If this is a C cast, first convert to an appropriately qualified
1953 : : type, so that we can later do a const_cast to the desired type. */
1954 : 161989208 : if (related_p && c_cast_p
1955 : 161989208 : && !at_least_as_qualified_p (to, tfrom))
1956 : 194 : to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1957 : 161989208 : compatible_p = reference_compatible_p (to, tfrom);
1958 : :
1959 : : /* Directly bind reference when target expression's type is compatible with
1960 : : the reference and expression is an lvalue. In DR391, the wording in
1961 : : [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1962 : : const and rvalue references to rvalues of compatible class type.
1963 : : We should also do direct bindings for non-class xvalues. */
1964 : 161989208 : if ((related_p || compatible_p) && gl_kind)
1965 : : {
1966 : : /* [dcl.init.ref]
1967 : :
1968 : : If the initializer expression
1969 : :
1970 : : -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1971 : : is reference-compatible with "cv2 T2,"
1972 : :
1973 : : the reference is bound directly to the initializer expression
1974 : : lvalue.
1975 : :
1976 : : [...]
1977 : : If the initializer expression is an rvalue, with T2 a class type,
1978 : : and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1979 : : is bound to the object represented by the rvalue or to a sub-object
1980 : : within that object. */
1981 : :
1982 : 75812295 : conv = build_identity_conv (tfrom, expr);
1983 : 75812295 : conv = direct_reference_binding (rto, conv);
1984 : :
1985 : 75812295 : if (TYPE_REF_P (rfrom))
1986 : : /* Handle rvalue reference to function properly. */
1987 : 11442 : conv->rvaluedness_matches_p
1988 : 11442 : = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom));
1989 : : else
1990 : 75800853 : conv->rvaluedness_matches_p
1991 : 75800853 : = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
1992 : :
1993 : 75812295 : if ((gl_kind & clk_bitfield) != 0
1994 : 75812295 : || ((gl_kind & clk_packed) != 0 && !TYPE_PACKED (to)))
1995 : : /* For the purposes of overload resolution, we ignore the fact
1996 : : this expression is a bitfield or packed field. (In particular,
1997 : : [over.ics.ref] says specifically that a function with a
1998 : : non-const reference parameter is viable even if the
1999 : : argument is a bitfield.)
2000 : :
2001 : : However, when we actually call the function we must create
2002 : : a temporary to which to bind the reference. If the
2003 : : reference is volatile, or isn't const, then we cannot make
2004 : : a temporary, so we just issue an error when the conversion
2005 : : actually occurs. */
2006 : 109 : conv->need_temporary_p = true;
2007 : :
2008 : : /* Don't allow binding of lvalues (other than function lvalues) to
2009 : : rvalue references. */
2010 : 56613407 : if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
2011 : 83158457 : && TREE_CODE (to) != FUNCTION_TYPE)
2012 : 7343894 : conv->bad_p = true;
2013 : :
2014 : : /* Nor the reverse. */
2015 : 19198888 : if (!is_lvalue && !TYPE_REF_IS_RVALUE (rto)
2016 : : /* Unless it's really a C++20 lvalue being treated as an xvalue.
2017 : : But in C++23, such an expression is just an xvalue, not a special
2018 : : lvalue, so the binding is once again ill-formed. */
2019 : 11017940 : && !(cxx_dialect <= cxx20
2020 : 6813998 : && (gl_kind & clk_implicit_rval))
2021 : 10408564 : && (!CP_TYPE_CONST_NON_VOLATILE_P (to)
2022 : 10345002 : || (flags & LOOKUP_NO_RVAL_BIND))
2023 : 75875857 : && TREE_CODE (to) != FUNCTION_TYPE)
2024 : 63562 : conv->bad_p = true;
2025 : :
2026 : 75812295 : if (!compatible_p)
2027 : 4817625 : conv->bad_p = true;
2028 : :
2029 : 75812295 : return conv;
2030 : : }
2031 : : /* [class.conv.fct] A conversion function is never used to convert a
2032 : : (possibly cv-qualified) object to the (possibly cv-qualified) same
2033 : : object type (or a reference to it), to a (possibly cv-qualified) base
2034 : : class of that type (or a reference to it).... */
2035 : 3488231 : else if (!related_p
2036 : 82688682 : && !(flags & LOOKUP_NO_CONVERSION)
2037 : 32125606 : && (CLASS_TYPE_P (from) || single_list_conv))
2038 : : {
2039 : 9631387 : tree rexpr = expr;
2040 : 9631387 : if (single_list_conv)
2041 : 22 : rexpr = CONSTRUCTOR_ELT (expr, 0)->value;
2042 : :
2043 : : /* [dcl.init.ref]
2044 : :
2045 : : If the initializer expression
2046 : :
2047 : : -- has a class type (i.e., T2 is a class type) can be
2048 : : implicitly converted to an lvalue of type "cv3 T3," where
2049 : : "cv1 T1" is reference-compatible with "cv3 T3". (this
2050 : : conversion is selected by enumerating the applicable
2051 : : conversion functions (_over.match.ref_) and choosing the
2052 : : best one through overload resolution. (_over.match_).
2053 : :
2054 : : the reference is bound to the lvalue result of the conversion
2055 : : in the second case. */
2056 : 9631387 : z_candidate *cand = build_user_type_conversion_1 (rto, rexpr, flags,
2057 : : complain);
2058 : 9631387 : if (cand)
2059 : : {
2060 : 11041 : if (!cand->second_conv->bad_p)
2061 : : return cand->second_conv;
2062 : :
2063 : : /* Direct reference binding wasn't successful and yielded a bad
2064 : : conversion. Proceed with trying to go through a temporary
2065 : : instead, and if that also fails then we'll return this bad
2066 : : conversion rather than no conversion for sake of better
2067 : : diagnostics. */
2068 : : bad_direct_conv = cand->second_conv;
2069 : : }
2070 : : }
2071 : :
2072 : : /* From this point on, we conceptually need temporaries, even if we
2073 : : elide them. Only the cases above are "direct bindings". */
2074 : 86165966 : if (flags & LOOKUP_NO_TEMP_BIND)
2075 : : return bad_direct_conv ? bad_direct_conv : nullptr;
2076 : :
2077 : : /* [over.ics.rank]
2078 : :
2079 : : When a parameter of reference type is not bound directly to an
2080 : : argument expression, the conversion sequence is the one required
2081 : : to convert the argument expression to the underlying type of the
2082 : : reference according to _over.best.ics_. Conceptually, this
2083 : : conversion sequence corresponds to copy-initializing a temporary
2084 : : of the underlying type with the argument expression. Any
2085 : : difference in top-level cv-qualification is subsumed by the
2086 : : initialization itself and does not constitute a conversion. */
2087 : :
2088 : 84134920 : bool maybe_valid_p = true;
2089 : :
2090 : : /* [dcl.init.ref]
2091 : :
2092 : : Otherwise, the reference shall be an lvalue reference to a
2093 : : non-volatile const type, or the reference shall be an rvalue
2094 : : reference. */
2095 : 109476293 : if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto))
2096 : : maybe_valid_p = false;
2097 : :
2098 : : /* [dcl.init.ref]
2099 : :
2100 : : Otherwise, a temporary of type "cv1 T1" is created and
2101 : : initialized from the initializer expression using the rules for a
2102 : : non-reference copy initialization. If T1 is reference-related to
2103 : : T2, cv1 must be the same cv-qualification as, or greater
2104 : : cv-qualification than, cv2; otherwise, the program is ill-formed. */
2105 : 84134920 : if (related_p && !at_least_as_qualified_p (to, from))
2106 : : maybe_valid_p = false;
2107 : :
2108 : : /* We try below to treat an invalid reference binding as a bad conversion
2109 : : to improve diagnostics, but doing so may cause otherwise unnecessary
2110 : : instantiations that can lead to a hard error. So during the first pass
2111 : : of overload resolution wherein we shortcut bad conversions, instead just
2112 : : produce a special conversion indicating a second pass is necessary if
2113 : : there's no strictly viable candidate. */
2114 : 84134920 : if (!maybe_valid_p && (flags & LOOKUP_SHORTCUT_BAD_CONVS))
2115 : : {
2116 : 4878648 : if (bad_direct_conv)
2117 : : return bad_direct_conv;
2118 : :
2119 : 4878597 : conv = alloc_conversion (ck_deferred_bad);
2120 : 4878597 : conv->bad_p = true;
2121 : 4878597 : return conv;
2122 : : }
2123 : :
2124 : : /* We're generating a temporary now, but don't bind any more in the
2125 : : conversion (specifically, don't slice the temporary returned by a
2126 : : conversion operator). */
2127 : 79256272 : flags |= LOOKUP_NO_TEMP_BIND;
2128 : :
2129 : : /* Core issue 899: When [copy-]initializing a temporary to be bound
2130 : : to the first parameter of a copy constructor (12.8) called with
2131 : : a single argument in the context of direct-initialization,
2132 : : explicit conversion functions are also considered.
2133 : :
2134 : : So don't set LOOKUP_ONLYCONVERTING in that case. */
2135 : 79256272 : if (!(flags & LOOKUP_COPY_PARM))
2136 : 68895252 : flags |= LOOKUP_ONLYCONVERTING;
2137 : :
2138 : 79256272 : if (!conv)
2139 : 79256272 : conv = implicit_conversion (to, from, expr, c_cast_p,
2140 : : flags, complain);
2141 : 79256272 : if (!conv)
2142 : : return bad_direct_conv ? bad_direct_conv : nullptr;
2143 : :
2144 : 8003551 : if (conv->user_conv_p)
2145 : : {
2146 : 4834916 : if (copy_list_init)
2147 : : /* Remember this was copy-list-initialization. */
2148 : 112624 : conv->need_temporary_p = true;
2149 : :
2150 : : /* If initializing the temporary used a conversion function,
2151 : : recalculate the second conversion sequence. */
2152 : 13812599 : for (conversion *t = conv; t; t = next_conversion (t))
2153 : 9298233 : if (t->kind == ck_user
2154 : 4783550 : && c_cast_p && !maybe_valid_p)
2155 : : {
2156 : 12 : if (complain & tf_warning)
2157 : 12 : warning (OPT_Wcast_user_defined,
2158 : : "casting %qT to %qT does not use %qD",
2159 : 12 : from, rto, t->cand->fn);
2160 : : /* Don't let recalculation try to make this valid. */
2161 : : break;
2162 : : }
2163 : 9298221 : else if (t->kind == ck_user
2164 : 9298221 : && DECL_CONV_FN_P (t->cand->fn))
2165 : : {
2166 : 320538 : tree ftype = TREE_TYPE (TREE_TYPE (t->cand->fn));
2167 : : /* A prvalue of non-class type is cv-unqualified. */
2168 : 320538 : if (!TYPE_REF_P (ftype) && !CLASS_TYPE_P (ftype))
2169 : 344 : ftype = cv_unqualified (ftype);
2170 : 320538 : int sflags = (flags|LOOKUP_NO_CONVERSION)&~LOOKUP_NO_TEMP_BIND;
2171 : 320538 : conversion *new_second
2172 : 320538 : = reference_binding (rto, ftype, NULL_TREE, c_cast_p,
2173 : : sflags, complain);
2174 : 320538 : if (!new_second)
2175 : : return bad_direct_conv ? bad_direct_conv : nullptr;
2176 : 320538 : conv = merge_conversion_sequences (t, new_second);
2177 : 320538 : gcc_assert (maybe_valid_p || conv->bad_p);
2178 : : return conv;
2179 : : }
2180 : : }
2181 : :
2182 : 7683013 : conv = build_conv (ck_ref_bind, rto, conv);
2183 : : /* This reference binding, unlike those above, requires the
2184 : : creation of a temporary. */
2185 : 7683013 : conv->need_temporary_p = true;
2186 : 7683013 : conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
2187 : 7683013 : conv->bad_p |= !maybe_valid_p;
2188 : :
2189 : 7683013 : return conv;
2190 : : }
2191 : :
2192 : : /* Returns the implicit conversion sequence (see [over.ics]) from type
2193 : : FROM to type TO. The optional expression EXPR may affect the
2194 : : conversion. FLAGS are the usual overloading flags. If C_CAST_P is
2195 : : true, this conversion is coming from a C-style cast. */
2196 : :
2197 : : static conversion *
2198 : 1124267077 : implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
2199 : : int flags, tsubst_flags_t complain)
2200 : : {
2201 : 1124267077 : conversion *conv;
2202 : :
2203 : 1124267077 : if (from == error_mark_node || to == error_mark_node
2204 : 1124266027 : || expr == error_mark_node)
2205 : : return NULL;
2206 : :
2207 : : /* Other flags only apply to the primary function in overload
2208 : : resolution, or after we've chosen one. */
2209 : 1124266027 : flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM
2210 : : |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_NO_NARROWING
2211 : : |LOOKUP_PROTECT|LOOKUP_NO_NON_INTEGRAL|LOOKUP_SHORTCUT_BAD_CONVS);
2212 : :
2213 : : /* FIXME: actually we don't want warnings either, but we can't just
2214 : : have 'complain &= ~(tf_warning|tf_error)' because it would cause
2215 : : the regression of, eg, g++.old-deja/g++.benjamin/16077.C.
2216 : : We really ought not to issue that warning until we've committed
2217 : : to that conversion. */
2218 : 1124266027 : complain &= ~tf_error;
2219 : :
2220 : : /* Call reshape_init early to remove redundant braces. */
2221 : 1124266027 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr) && CLASS_TYPE_P (to))
2222 : : {
2223 : 1890171 : to = complete_type (to);
2224 : 1890171 : if (!COMPLETE_TYPE_P (to))
2225 : : return nullptr;
2226 : 1890147 : if (!CLASSTYPE_NON_AGGREGATE (to))
2227 : : {
2228 : 1157222 : expr = reshape_init (to, expr, complain);
2229 : 1157222 : if (expr == error_mark_node)
2230 : : return nullptr;
2231 : 1157029 : from = TREE_TYPE (expr);
2232 : : }
2233 : : }
2234 : :
2235 : : /* An argument should have gone through convert_from_reference. */
2236 : 1124265810 : gcc_checking_assert (!expr || !TYPE_REF_P (from));
2237 : :
2238 : 1124265810 : if (TYPE_REF_P (to))
2239 : 156256326 : conv = reference_binding (to, from, expr, c_cast_p, flags, complain);
2240 : : else
2241 : 968009484 : conv = standard_conversion (to, from, expr, c_cast_p, flags, complain);
2242 : :
2243 : 1124265810 : if (conv)
2244 : : return conv;
2245 : :
2246 : 196784028 : if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
2247 : : {
2248 : 3788655 : if (is_std_init_list (to) && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2249 : 67778 : return build_list_conv (to, expr, flags, complain);
2250 : :
2251 : : /* As an extension, allow list-initialization of _Complex. */
2252 : 3653090 : if (TREE_CODE (to) == COMPLEX_TYPE
2253 : 3712209 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2254 : : {
2255 : 59110 : conv = build_complex_conv (to, expr, flags, complain);
2256 : 59110 : if (conv)
2257 : : return conv;
2258 : : }
2259 : :
2260 : : /* Allow conversion from an initializer-list with one element to a
2261 : : scalar type. */
2262 : 3594020 : if (SCALAR_TYPE_P (to))
2263 : : {
2264 : 1759386 : int nelts = CONSTRUCTOR_NELTS (expr);
2265 : 302986 : tree elt;
2266 : :
2267 : 302986 : if (nelts == 0)
2268 : 1456400 : elt = build_value_init (to, tf_none);
2269 : 302986 : else if (nelts == 1 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2270 : 302426 : elt = CONSTRUCTOR_ELT (expr, 0)->value;
2271 : : else
2272 : 560 : elt = error_mark_node;
2273 : :
2274 : 1759386 : conv = implicit_conversion (to, TREE_TYPE (elt), elt,
2275 : : c_cast_p, flags, complain);
2276 : 1759386 : if (conv)
2277 : : {
2278 : 1758337 : conv->check_narrowing = true;
2279 : 1758337 : if (BRACE_ENCLOSED_INITIALIZER_P (elt))
2280 : : /* Too many levels of braces, i.e. '{{1}}'. */
2281 : 14 : conv->bad_p = true;
2282 : 1758337 : return conv;
2283 : : }
2284 : : }
2285 : 1834634 : else if (TREE_CODE (to) == ARRAY_TYPE)
2286 : 1052 : return build_array_conv (to, expr, flags, complain);
2287 : : }
2288 : :
2289 : 194897791 : if (expr != NULL_TREE
2290 : 190741907 : && (MAYBE_CLASS_TYPE_P (from)
2291 : 114047927 : || MAYBE_CLASS_TYPE_P (to))
2292 : 325232217 : && (flags & LOOKUP_NO_CONVERSION) == 0)
2293 : : {
2294 : 50461930 : struct z_candidate *cand;
2295 : :
2296 : 36019312 : if (CLASS_TYPE_P (to)
2297 : 36019266 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
2298 : 52283728 : && !CLASSTYPE_NON_AGGREGATE (complete_type (to)))
2299 : 1156978 : return build_aggr_conv (to, expr, flags, complain);
2300 : :
2301 : 49304952 : cand = build_user_type_conversion_1 (to, expr, flags, complain);
2302 : 49304952 : if (cand)
2303 : : {
2304 : 655885 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
2305 : 653258 : && CONSTRUCTOR_NELTS (expr) == 1
2306 : 24298 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr)
2307 : 9454301 : && !is_list_ctor (cand->fn))
2308 : : {
2309 : : /* "If C is not an initializer-list constructor and the
2310 : : initializer list has a single element of type cv U, where U is
2311 : : X or a class derived from X, the implicit conversion sequence
2312 : : has Exact Match rank if U is X, or Conversion rank if U is
2313 : : derived from X." */
2314 : 23942 : tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
2315 : 23942 : tree elttype = TREE_TYPE (elt);
2316 : 23942 : if (reference_related_p (to, elttype))
2317 : 67 : return implicit_conversion (to, elttype, elt,
2318 : 67 : c_cast_p, flags, complain);
2319 : : }
2320 : 9429936 : conv = cand->second_conv;
2321 : : }
2322 : :
2323 : : /* We used to try to bind a reference to a temporary here, but that
2324 : : is now handled after the recursive call to this function at the end
2325 : : of reference_binding. */
2326 : 49304885 : return conv;
2327 : : }
2328 : :
2329 : : return NULL;
2330 : : }
2331 : :
2332 : : /* Like implicit_conversion, but return NULL if the conversion is bad.
2333 : :
2334 : : This is not static so that check_non_deducible_conversion can call it within
2335 : : add_template_candidate_real as part of overload resolution; it should not be
2336 : : called outside of overload resolution. */
2337 : :
2338 : : conversion *
2339 : 5569667 : good_conversion (tree to, tree from, tree expr,
2340 : : int flags, tsubst_flags_t complain)
2341 : : {
2342 : 5569667 : conversion *c = implicit_conversion (to, from, expr, /*cast*/false,
2343 : : flags, complain);
2344 : 5569667 : if (c && c->bad_p)
2345 : 2245743 : c = NULL;
2346 : 5569667 : return c;
2347 : : }
2348 : :
2349 : : /* Add a new entry to the list of candidates. Used by the add_*_candidate
2350 : : functions. ARGS will not be changed until a single candidate is
2351 : : selected. */
2352 : :
2353 : : static struct z_candidate *
2354 : 783772040 : add_candidate (struct z_candidate **candidates,
2355 : : tree fn, tree first_arg, const vec<tree, va_gc> *args,
2356 : : size_t num_convs, conversion **convs,
2357 : : tree access_path, tree conversion_path,
2358 : : int viable, struct rejection_reason *reason,
2359 : : int flags)
2360 : : {
2361 : 783772040 : struct z_candidate *cand = (struct z_candidate *)
2362 : 783772040 : conversion_obstack_alloc (sizeof (struct z_candidate));
2363 : :
2364 : 783772040 : cand->fn = fn;
2365 : 783772040 : cand->first_arg = first_arg;
2366 : 783772040 : cand->args = args;
2367 : 783772040 : cand->convs = convs;
2368 : 783772040 : cand->num_convs = num_convs;
2369 : 783772040 : cand->access_path = access_path;
2370 : 783772040 : cand->conversion_path = conversion_path;
2371 : 783772040 : cand->viable = viable;
2372 : 783772040 : cand->reason = reason;
2373 : 783772040 : cand->next = *candidates;
2374 : 783772040 : cand->flags = flags;
2375 : 783772040 : *candidates = cand;
2376 : :
2377 : 783772040 : if (convs && cand->reversed ())
2378 : : /* Swap the conversions for comparison in joust; we'll swap them back
2379 : : before build_over_call. */
2380 : 36268734 : std::swap (convs[0], convs[1]);
2381 : :
2382 : 783772040 : return cand;
2383 : : }
2384 : :
2385 : : /* FN is a function from the overload set that we outright didn't even
2386 : : consider (for some reason); add it to the list as an non-viable "ignored"
2387 : : candidate. */
2388 : :
2389 : : static z_candidate *
2390 : 451121546 : add_ignored_candidate (z_candidate **candidates, tree fn)
2391 : : {
2392 : : /* No need to dynamically allocate these. */
2393 : 451121546 : static const rejection_reason reason_ignored = { rr_ignored, {} };
2394 : :
2395 : 451121546 : struct z_candidate *cand = (struct z_candidate *)
2396 : 451120609 : conversion_obstack_alloc (sizeof (struct z_candidate));
2397 : :
2398 : 451121546 : cand->fn = fn;
2399 : 451121546 : cand->reason = const_cast<rejection_reason *> (&reason_ignored);
2400 : 451121546 : cand->next = *candidates;
2401 : 451121546 : *candidates = cand;
2402 : :
2403 : 451121546 : return cand;
2404 : : }
2405 : :
2406 : : /* True iff CAND is a candidate added by add_ignored_candidate. */
2407 : :
2408 : : static bool
2409 : 414730010 : ignored_candidate_p (const z_candidate *cand)
2410 : : {
2411 : 414724011 : return cand->reason && cand->reason->code == rr_ignored;
2412 : : }
2413 : :
2414 : : /* Return the number of remaining arguments in the parameter list
2415 : : beginning with ARG. */
2416 : :
2417 : : int
2418 : 121035885 : remaining_arguments (tree arg)
2419 : : {
2420 : 121035885 : int n;
2421 : :
2422 : 213629065 : for (n = 0; arg != NULL_TREE && arg != void_list_node;
2423 : 92593180 : arg = TREE_CHAIN (arg))
2424 : 92593180 : n++;
2425 : :
2426 : 121035885 : return n;
2427 : : }
2428 : :
2429 : : /* [over.match.copy]: When initializing a temporary object (12.2) to be bound
2430 : : to the first parameter of a constructor where the parameter is of type
2431 : : "reference to possibly cv-qualified T" and the constructor is called with a
2432 : : single argument in the context of direct-initialization of an object of type
2433 : : "cv2 T", explicit conversion functions are also considered.
2434 : :
2435 : : So set LOOKUP_COPY_PARM to let reference_binding know that
2436 : : it's being called in that context. */
2437 : :
2438 : : int
2439 : 329757560 : conv_flags (int i, int nargs, tree fn, tree arg, int flags)
2440 : : {
2441 : 329757560 : int lflags = flags;
2442 : 329757560 : tree t;
2443 : 331159282 : if (i == 0 && nargs == 1 && DECL_CONSTRUCTOR_P (fn)
2444 : 95740948 : && (t = FUNCTION_FIRST_USER_PARMTYPE (fn))
2445 : 425498508 : && (same_type_ignoring_top_level_qualifiers_p
2446 : 95740948 : (non_reference (TREE_VALUE (t)), DECL_CONTEXT (fn))))
2447 : : {
2448 : 74622635 : if (!(flags & LOOKUP_ONLYCONVERTING))
2449 : 22872499 : lflags |= LOOKUP_COPY_PARM;
2450 : 74622635 : if ((flags & LOOKUP_LIST_INIT_CTOR)
2451 : 74622635 : && BRACE_ENCLOSED_INITIALIZER_P (arg))
2452 : 850 : lflags |= LOOKUP_NO_CONVERSION;
2453 : : }
2454 : : else
2455 : 255134925 : lflags |= LOOKUP_ONLYCONVERTING;
2456 : :
2457 : 329757560 : return lflags;
2458 : : }
2459 : :
2460 : : /* Build an appropriate 'this' conversion for the method FN and class
2461 : : type CTYPE from the value ARG (having type ARGTYPE) to the type PARMTYPE.
2462 : : This function modifies PARMTYPE, ARGTYPE and ARG. */
2463 : :
2464 : : static conversion *
2465 : 73937164 : build_this_conversion (tree fn, tree ctype,
2466 : : tree& parmtype, tree& argtype, tree& arg,
2467 : : int flags, tsubst_flags_t complain)
2468 : : {
2469 : 147874328 : gcc_assert (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
2470 : : && !DECL_CONSTRUCTOR_P (fn));
2471 : :
2472 : : /* The type of the implicit object parameter ('this') for
2473 : : overload resolution is not always the same as for the
2474 : : function itself; conversion functions are considered to
2475 : : be members of the class being converted, and functions
2476 : : introduced by a using-declaration are considered to be
2477 : : members of the class that uses them.
2478 : :
2479 : : Since build_over_call ignores the ICS for the `this'
2480 : : parameter, we can just change the parm type. */
2481 : 73937164 : parmtype = cp_build_qualified_type (ctype,
2482 : 73937164 : cp_type_quals (TREE_TYPE (parmtype)));
2483 : 73937164 : bool this_p = true;
2484 : 73937164 : if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn)))
2485 : : {
2486 : : /* If the function has a ref-qualifier, the implicit
2487 : : object parameter has reference type. */
2488 : 86827 : bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn));
2489 : 86827 : parmtype = cp_build_reference_type (parmtype, rv);
2490 : : /* The special handling of 'this' conversions in compare_ics
2491 : : does not apply if there is a ref-qualifier. */
2492 : 86827 : this_p = false;
2493 : : }
2494 : : else
2495 : : {
2496 : 73850337 : parmtype = build_pointer_type (parmtype);
2497 : : /* We don't use build_this here because we don't want to
2498 : : capture the object argument until we've chosen a
2499 : : non-static member function. */
2500 : 73850337 : arg = build_address (arg);
2501 : 73850337 : argtype = lvalue_type (arg);
2502 : : }
2503 : 73937164 : flags |= LOOKUP_ONLYCONVERTING;
2504 : 73937164 : conversion *t = implicit_conversion (parmtype, argtype, arg,
2505 : : /*c_cast_p=*/false, flags, complain);
2506 : 73937164 : t->this_p = this_p;
2507 : 73937164 : return t;
2508 : : }
2509 : :
2510 : : /* Create an overload candidate for the function or method FN called
2511 : : with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
2512 : : FLAGS is passed on to implicit_conversion.
2513 : :
2514 : : This does not change ARGS.
2515 : :
2516 : : CTYPE, if non-NULL, is the type we want to pretend this function
2517 : : comes from for purposes of overload resolution.
2518 : :
2519 : : SHORTCUT_BAD_CONVS controls how we handle "bad" argument conversions.
2520 : : If true, we stop computing conversions upon seeing the first bad
2521 : : conversion. This is used by add_candidates to avoid computing
2522 : : more conversions than necessary in the presence of a strictly viable
2523 : : candidate, while preserving the defacto behavior of overload resolution
2524 : : when it turns out there are only non-strictly viable candidates. */
2525 : :
2526 : : static struct z_candidate *
2527 : 476969167 : add_function_candidate (struct z_candidate **candidates,
2528 : : tree fn, tree ctype, tree first_arg,
2529 : : const vec<tree, va_gc> *args, tree access_path,
2530 : : tree conversion_path, int flags,
2531 : : conversion **convs,
2532 : : bool shortcut_bad_convs,
2533 : : tsubst_flags_t complain)
2534 : : {
2535 : 476969167 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
2536 : 476969167 : int i, len;
2537 : 476969167 : tree parmnode;
2538 : 476969167 : tree orig_first_arg = first_arg;
2539 : 476969167 : int skip;
2540 : 476969167 : int viable = 1;
2541 : 476969167 : struct rejection_reason *reason = NULL;
2542 : :
2543 : : /* The `this', `in_chrg' and VTT arguments to constructors are not
2544 : : considered in overload resolution. */
2545 : 953938334 : if (DECL_CONSTRUCTOR_P (fn))
2546 : : {
2547 : 204053424 : if (ctor_omit_inherited_parms (fn))
2548 : : /* Bring back parameters omitted from an inherited ctor. */
2549 : 150 : parmlist = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
2550 : : else
2551 : 204053349 : parmlist = skip_artificial_parms_for (fn, parmlist);
2552 : 204053424 : skip = num_artificial_parms_for (fn);
2553 : 204053424 : if (skip > 0 && first_arg != NULL_TREE)
2554 : : {
2555 : 204053424 : --skip;
2556 : 204053424 : first_arg = NULL_TREE;
2557 : : }
2558 : : }
2559 : : else
2560 : : skip = 0;
2561 : :
2562 : 921947270 : len = vec_safe_length (args) - skip + (first_arg != NULL_TREE ? 1 : 0);
2563 : 476969167 : if (!convs)
2564 : 411918706 : convs = alloc_conversions (len);
2565 : :
2566 : : /* 13.3.2 - Viable functions [over.match.viable]
2567 : : First, to be a viable function, a candidate function shall have enough
2568 : : parameters to agree in number with the arguments in the list.
2569 : :
2570 : : We need to check this first; otherwise, checking the ICSes might cause
2571 : : us to produce an ill-formed template instantiation. */
2572 : :
2573 : 476969167 : parmnode = parmlist;
2574 : 985716012 : for (i = 0; i < len; ++i)
2575 : : {
2576 : 567043139 : if (parmnode == NULL_TREE || parmnode == void_list_node)
2577 : : break;
2578 : 508746845 : parmnode = TREE_CHAIN (parmnode);
2579 : : }
2580 : :
2581 : 476969167 : if ((i < len && parmnode)
2582 : 476969167 : || !sufficient_parms_p (parmnode))
2583 : : {
2584 : 121032387 : int remaining = remaining_arguments (parmnode);
2585 : 121032387 : viable = 0;
2586 : 121032387 : reason = arity_rejection (first_arg, i + remaining, len);
2587 : : }
2588 : :
2589 : : /* An inherited constructor (12.6.3 [class.inhctor.init]) that has a first
2590 : : parameter of type "reference to cv C" (including such a constructor
2591 : : instantiated from a template) is excluded from the set of candidate
2592 : : functions when used to construct an object of type D with an argument list
2593 : : containing a single argument if C is reference-related to D. */
2594 : 426603094 : if (viable && len == 1 && parmlist && DECL_CONSTRUCTOR_P (fn)
2595 : 97948466 : && flag_new_inheriting_ctors
2596 : 574908948 : && DECL_INHERITED_CTOR (fn))
2597 : : {
2598 : 617316 : tree ptype = non_reference (TREE_VALUE (parmlist));
2599 : 617316 : tree dtype = DECL_CONTEXT (fn);
2600 : 1234632 : tree btype = DECL_INHERITED_CTOR_BASE (fn);
2601 : 617316 : if (reference_related_p (ptype, dtype)
2602 : 617316 : && reference_related_p (btype, ptype))
2603 : : {
2604 : 418431 : viable = false;
2605 : 418431 : reason = inherited_ctor_rejection ();
2606 : : }
2607 : : }
2608 : :
2609 : : /* Second, for a function to be viable, its constraints must be
2610 : : satisfied. */
2611 : 476969167 : if (flag_concepts && viable && !constraints_satisfied_p (fn))
2612 : : {
2613 : 142791 : reason = constraint_failure ();
2614 : 142791 : viable = false;
2615 : : }
2616 : :
2617 : : /* When looking for a function from a subobject from an implicit
2618 : : copy/move constructor/operator=, don't consider anything that takes (a
2619 : : reference to) an unrelated type. See c++/44909 and core 1092. */
2620 : 476969167 : if (viable && parmlist && (flags & LOOKUP_DEFAULTED))
2621 : : {
2622 : 64024638 : if (DECL_CONSTRUCTOR_P (fn))
2623 : : i = 1;
2624 : 16709823 : else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2625 : 16709823 : && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
2626 : : i = 2;
2627 : : else
2628 : : i = 0;
2629 : 32012319 : if (i && len == i)
2630 : : {
2631 : 17134061 : parmnode = chain_index (i-1, parmlist);
2632 : 17134061 : if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
2633 : : ctype))
2634 : 3469922 : viable = 0;
2635 : : }
2636 : :
2637 : : /* This only applies at the top level. */
2638 : 28542397 : flags &= ~LOOKUP_DEFAULTED;
2639 : : }
2640 : :
2641 : 476969167 : if (! viable)
2642 : 125063531 : goto out;
2643 : :
2644 : 351905636 : if (shortcut_bad_convs)
2645 : 351835697 : flags |= LOOKUP_SHORTCUT_BAD_CONVS;
2646 : : else
2647 : 69939 : flags &= ~LOOKUP_SHORTCUT_BAD_CONVS;
2648 : :
2649 : : /* Third, for F to be a viable function, there shall exist for each
2650 : : argument an implicit conversion sequence that converts that argument
2651 : : to the corresponding parameter of F. */
2652 : :
2653 : 351905636 : parmnode = parmlist;
2654 : :
2655 : 623188921 : for (i = 0; i < len; ++i)
2656 : : {
2657 : 391825100 : tree argtype, to_type;
2658 : 391825100 : tree arg;
2659 : :
2660 : 391825100 : if (parmnode == void_list_node)
2661 : : break;
2662 : :
2663 : 391825100 : if (convs[i])
2664 : : {
2665 : : /* Already set during deduction. */
2666 : 5536303 : parmnode = TREE_CHAIN (parmnode);
2667 : 5536303 : continue;
2668 : : }
2669 : :
2670 : 386288797 : if (i == 0 && first_arg != NULL_TREE)
2671 : 70887816 : arg = first_arg;
2672 : : else
2673 : 602155850 : arg = CONST_CAST_TREE (
2674 : : (*args)[i + skip - (first_arg != NULL_TREE ? 1 : 0)]);
2675 : 386288797 : argtype = lvalue_type (arg);
2676 : :
2677 : 386288797 : conversion *t;
2678 : 386288797 : if (parmnode)
2679 : : {
2680 : 381284165 : tree parmtype = TREE_VALUE (parmnode);
2681 : 381284165 : if (i == 0
2682 : 300978420 : && DECL_IOBJ_MEMBER_FUNCTION_P (fn)
2683 : 716964319 : && !DECL_CONSTRUCTOR_P (fn))
2684 : 70879750 : t = build_this_conversion (fn, ctype, parmtype, argtype, arg,
2685 : : flags, complain);
2686 : : else
2687 : : {
2688 : 310404415 : int lflags = conv_flags (i, len-skip, fn, arg, flags);
2689 : 310404415 : t = implicit_conversion (parmtype, argtype, arg,
2690 : : /*c_cast_p=*/false, lflags, complain);
2691 : : }
2692 : 381284165 : to_type = parmtype;
2693 : 381284165 : parmnode = TREE_CHAIN (parmnode);
2694 : : }
2695 : : else
2696 : : {
2697 : 5004632 : t = build_identity_conv (argtype, arg);
2698 : 5004632 : t->ellipsis_p = true;
2699 : 5004632 : to_type = argtype;
2700 : : }
2701 : :
2702 : 386288797 : convs[i] = t;
2703 : 386288797 : if (! t)
2704 : : {
2705 : 103466235 : viable = 0;
2706 : 103466235 : reason = arg_conversion_rejection (first_arg, i, argtype, to_type,
2707 : 103466235 : EXPR_LOCATION (arg));
2708 : 103466235 : break;
2709 : : }
2710 : :
2711 : 282822562 : if (t->bad_p)
2712 : : {
2713 : 17116483 : viable = -1;
2714 : 17116483 : reason = bad_arg_conversion_rejection (first_arg, i, arg, to_type,
2715 : 17116483 : EXPR_LOCATION (arg));
2716 : 17116483 : if (shortcut_bad_convs)
2717 : : break;
2718 : : }
2719 : : }
2720 : :
2721 : 231363821 : out:
2722 : 476969167 : return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
2723 : 476969167 : access_path, conversion_path, viable, reason, flags);
2724 : : }
2725 : :
2726 : : /* Create an overload candidate for the conversion function FN which will
2727 : : be invoked for expression OBJ, producing a pointer-to-function which
2728 : : will in turn be called with the argument list FIRST_ARG/ARGLIST,
2729 : : and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
2730 : : passed on to implicit_conversion.
2731 : :
2732 : : Actually, we don't really care about FN; we care about the type it
2733 : : converts to. There may be multiple conversion functions that will
2734 : : convert to that type, and we rely on build_user_type_conversion_1 to
2735 : : choose the best one; so when we create our candidate, we record the type
2736 : : instead of the function. */
2737 : :
2738 : : static struct z_candidate *
2739 : 46918 : add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
2740 : : const vec<tree, va_gc> *arglist,
2741 : : tree access_path, tree conversion_path,
2742 : : tsubst_flags_t complain)
2743 : : {
2744 : 46918 : tree totype = TREE_TYPE (TREE_TYPE (fn));
2745 : 46918 : int i, len, viable, flags;
2746 : 46918 : tree parmlist, parmnode;
2747 : 46918 : conversion **convs;
2748 : 46918 : struct rejection_reason *reason;
2749 : :
2750 : 93901 : for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
2751 : 46983 : parmlist = TREE_TYPE (parmlist);
2752 : 46918 : parmlist = TYPE_ARG_TYPES (parmlist);
2753 : :
2754 : 46918 : len = vec_safe_length (arglist) + 1;
2755 : 46918 : convs = alloc_conversions (len);
2756 : 46918 : parmnode = parmlist;
2757 : 46918 : viable = 1;
2758 : 46918 : flags = LOOKUP_IMPLICIT;
2759 : 46918 : reason = NULL;
2760 : :
2761 : : /* Don't bother looking up the same type twice. */
2762 : 46918 : if (*candidates && (*candidates)->fn == totype)
2763 : : return NULL;
2764 : :
2765 : 46903 : if (!constraints_satisfied_p (fn))
2766 : : {
2767 : 9 : reason = constraint_failure ();
2768 : 9 : viable = 0;
2769 : 9 : return add_candidate (candidates, fn, obj, arglist, len, convs,
2770 : 9 : access_path, conversion_path, viable, reason, flags);
2771 : : }
2772 : :
2773 : 155377 : for (i = 0; i < len; ++i)
2774 : : {
2775 : 108574 : tree arg, argtype, convert_type = NULL_TREE;
2776 : 108574 : conversion *t;
2777 : :
2778 : 108574 : if (i == 0)
2779 : : arg = obj;
2780 : : else
2781 : 61680 : arg = (*arglist)[i - 1];
2782 : 108574 : argtype = lvalue_type (arg);
2783 : :
2784 : 108574 : if (i == 0)
2785 : : {
2786 : 46894 : t = build_identity_conv (argtype, NULL_TREE);
2787 : 46894 : t = build_conv (ck_user, totype, t);
2788 : : /* Leave the 'cand' field null; we'll figure out the conversion in
2789 : : convert_like if this candidate is chosen. */
2790 : 46894 : convert_type = totype;
2791 : : }
2792 : 61680 : else if (parmnode == void_list_node)
2793 : : break;
2794 : 61648 : else if (parmnode)
2795 : : {
2796 : 61639 : t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
2797 : : /*c_cast_p=*/false, flags, complain);
2798 : 61639 : convert_type = TREE_VALUE (parmnode);
2799 : : }
2800 : : else
2801 : : {
2802 : 9 : t = build_identity_conv (argtype, arg);
2803 : 9 : t->ellipsis_p = true;
2804 : 9 : convert_type = argtype;
2805 : : }
2806 : :
2807 : 108542 : convs[i] = t;
2808 : 108542 : if (! t)
2809 : : break;
2810 : :
2811 : 108483 : if (t->bad_p)
2812 : : {
2813 : 20 : viable = -1;
2814 : 60 : reason = bad_arg_conversion_rejection (NULL_TREE, i, arg, convert_type,
2815 : 20 : EXPR_LOCATION (arg));
2816 : : }
2817 : :
2818 : 108483 : if (i == 0)
2819 : 46894 : continue;
2820 : :
2821 : 61589 : if (parmnode)
2822 : 61580 : parmnode = TREE_CHAIN (parmnode);
2823 : : }
2824 : :
2825 : 46894 : if (i < len
2826 : 46894 : || ! sufficient_parms_p (parmnode))
2827 : : {
2828 : 117 : int remaining = remaining_arguments (parmnode);
2829 : 117 : viable = 0;
2830 : 117 : reason = arity_rejection (NULL_TREE, i + remaining, len);
2831 : : }
2832 : :
2833 : 46894 : return add_candidate (candidates, totype, obj, arglist, len, convs,
2834 : 46894 : access_path, conversion_path, viable, reason, flags);
2835 : : }
2836 : :
2837 : : static void
2838 : 8089245 : build_builtin_candidate (struct z_candidate **candidates, tree fnname,
2839 : : tree type1, tree type2, const vec<tree,va_gc> &args,
2840 : : tree *argtypes, int flags, tsubst_flags_t complain)
2841 : : {
2842 : 8089245 : conversion *t;
2843 : 8089245 : conversion **convs;
2844 : 8089245 : size_t num_convs;
2845 : 8089245 : int viable = 1;
2846 : 8089245 : tree types[2];
2847 : 8089245 : struct rejection_reason *reason = NULL;
2848 : :
2849 : 8089245 : types[0] = type1;
2850 : 8089245 : types[1] = type2;
2851 : :
2852 : 8089245 : num_convs = args.length ();
2853 : 8089245 : convs = alloc_conversions (num_convs);
2854 : :
2855 : : /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2856 : : conversion ops are allowed. We handle that here by just checking for
2857 : : boolean_type_node because other operators don't ask for it. COND_EXPR
2858 : : also does contextual conversion to bool for the first operand, but we
2859 : : handle that in build_conditional_expr, and type1 here is operand 2. */
2860 : 8089245 : if (type1 != boolean_type_node)
2861 : 7534124 : flags |= LOOKUP_ONLYCONVERTING;
2862 : :
2863 : 23628630 : for (unsigned i = 0; i < 2 && i < num_convs; ++i)
2864 : : {
2865 : 15539385 : t = implicit_conversion (types[i], argtypes[i], args[i],
2866 : : /*c_cast_p=*/false, flags, complain);
2867 : 15539385 : if (! t)
2868 : : {
2869 : 556611 : viable = 0;
2870 : : /* We need something for printing the candidate. */
2871 : 556611 : t = build_identity_conv (types[i], NULL_TREE);
2872 : 556611 : reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2873 : 556611 : types[i], EXPR_LOCATION (args[i]));
2874 : : }
2875 : 14982774 : else if (t->bad_p)
2876 : : {
2877 : 145 : viable = 0;
2878 : 145 : reason = bad_arg_conversion_rejection (NULL_TREE, i, args[i],
2879 : : types[i],
2880 : 145 : EXPR_LOCATION (args[i]));
2881 : : }
2882 : 15539385 : convs[i] = t;
2883 : : }
2884 : :
2885 : : /* For COND_EXPR we rearranged the arguments; undo that now. */
2886 : 8089245 : if (num_convs == 3)
2887 : : {
2888 : 65 : convs[2] = convs[1];
2889 : 65 : convs[1] = convs[0];
2890 : 65 : t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
2891 : : /*c_cast_p=*/false, flags,
2892 : : complain);
2893 : 65 : if (t)
2894 : 65 : convs[0] = t;
2895 : : else
2896 : : {
2897 : 0 : viable = 0;
2898 : 0 : reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2],
2899 : : boolean_type_node,
2900 : 0 : EXPR_LOCATION (args[2]));
2901 : : }
2902 : : }
2903 : :
2904 : 8089245 : add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
2905 : : num_convs, convs,
2906 : : /*access_path=*/NULL_TREE,
2907 : : /*conversion_path=*/NULL_TREE,
2908 : : viable, reason, flags);
2909 : 8089245 : }
2910 : :
2911 : : static bool
2912 : 6 : is_complete (tree t)
2913 : : {
2914 : 6 : return COMPLETE_TYPE_P (complete_type (t));
2915 : : }
2916 : :
2917 : : /* Returns nonzero if TYPE is a promoted arithmetic type. */
2918 : :
2919 : : static bool
2920 : 2898 : promoted_arithmetic_type_p (tree type)
2921 : : {
2922 : : /* [over.built]
2923 : :
2924 : : In this section, the term promoted integral type is used to refer
2925 : : to those integral types which are preserved by integral promotion
2926 : : (including e.g. int and long but excluding e.g. char).
2927 : : Similarly, the term promoted arithmetic type refers to promoted
2928 : : integral types plus floating types. */
2929 : 2898 : return ((CP_INTEGRAL_TYPE_P (type)
2930 : 180 : && same_type_p (type_promotes_to (type), type))
2931 : 2898 : || SCALAR_FLOAT_TYPE_P (type));
2932 : : }
2933 : :
2934 : : /* Create any builtin operator overload candidates for the operator in
2935 : : question given the converted operand types TYPE1 and TYPE2. The other
2936 : : args are passed through from add_builtin_candidates to
2937 : : build_builtin_candidate.
2938 : :
2939 : : TYPE1 and TYPE2 may not be permissible, and we must filter them.
2940 : : If CODE is requires candidates operands of the same type of the kind
2941 : : of which TYPE1 and TYPE2 are, we add both candidates
2942 : : CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2943 : :
2944 : : static void
2945 : 11276116 : add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
2946 : : enum tree_code code2, tree fnname, tree type1,
2947 : : tree type2, vec<tree,va_gc> &args, tree *argtypes,
2948 : : int flags, tsubst_flags_t complain)
2949 : : {
2950 : 11276116 : switch (code)
2951 : : {
2952 : 21 : case POSTINCREMENT_EXPR:
2953 : 21 : case POSTDECREMENT_EXPR:
2954 : 21 : args[1] = integer_zero_node;
2955 : 21 : type2 = integer_type_node;
2956 : 21 : break;
2957 : : default:
2958 : : break;
2959 : : }
2960 : :
2961 : 11276116 : switch (code)
2962 : : {
2963 : :
2964 : : /* 4 For every pair (T, VQ), where T is an arithmetic type other than bool,
2965 : : and VQ is either volatile or empty, there exist candidate operator
2966 : : functions of the form
2967 : : VQ T& operator++(VQ T&);
2968 : : T operator++(VQ T&, int);
2969 : : 5 For every pair (T, VQ), where T is an arithmetic type other than bool,
2970 : : and VQ is either volatile or empty, there exist candidate operator
2971 : : functions of the form
2972 : : VQ T& operator--(VQ T&);
2973 : : T operator--(VQ T&, int);
2974 : : 6 For every pair (T, VQ), where T is a cv-qualified or cv-unqualified object
2975 : : type, and VQ is either volatile or empty, there exist candidate operator
2976 : : functions of the form
2977 : : T*VQ& operator++(T*VQ&);
2978 : : T*VQ& operator--(T*VQ&);
2979 : : T* operator++(T*VQ&, int);
2980 : : T* operator--(T*VQ&, int); */
2981 : :
2982 : 18 : case POSTDECREMENT_EXPR:
2983 : 18 : case PREDECREMENT_EXPR:
2984 : 18 : if (TREE_CODE (type1) == BOOLEAN_TYPE)
2985 : : return;
2986 : : /* FALLTHRU */
2987 : 39 : case POSTINCREMENT_EXPR:
2988 : 39 : case PREINCREMENT_EXPR:
2989 : : /* P0002R1, Remove deprecated operator++(bool) added "other than bool"
2990 : : to p4. */
2991 : 39 : if (TREE_CODE (type1) == BOOLEAN_TYPE && cxx_dialect >= cxx17)
2992 : : return;
2993 : 33 : if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
2994 : : {
2995 : 24 : type1 = build_reference_type (type1);
2996 : 24 : break;
2997 : : }
2998 : : return;
2999 : :
3000 : : /* 7 For every cv-qualified or cv-unqualified object type T, there
3001 : : exist candidate operator functions of the form
3002 : :
3003 : : T& operator*(T*);
3004 : :
3005 : :
3006 : : 8 For every function type T that does not have cv-qualifiers or
3007 : : a ref-qualifier, there exist candidate operator functions of the form
3008 : : T& operator*(T*); */
3009 : :
3010 : 93057 : case INDIRECT_REF:
3011 : 93057 : if (TYPE_PTR_P (type1)
3012 : 93057 : && (TYPE_PTROB_P (type1)
3013 : 13 : || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
3014 : : break;
3015 : : return;
3016 : :
3017 : : /* 9 For every type T, there exist candidate operator functions of the form
3018 : : T* operator+(T*);
3019 : :
3020 : : 10 For every floating-point or promoted integral type T, there exist
3021 : : candidate operator functions of the form
3022 : : T operator+(T);
3023 : : T operator-(T); */
3024 : :
3025 : 238 : case UNARY_PLUS_EXPR: /* unary + */
3026 : 238 : if (TYPE_PTR_P (type1))
3027 : : break;
3028 : : /* FALLTHRU */
3029 : 49411 : case NEGATE_EXPR:
3030 : 49411 : if (ARITHMETIC_TYPE_P (type1))
3031 : : break;
3032 : : return;
3033 : :
3034 : : /* 11 For every promoted integral type T, there exist candidate operator
3035 : : functions of the form
3036 : : T operator~(T); */
3037 : :
3038 : 122552 : case BIT_NOT_EXPR:
3039 : 122552 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
3040 : : break;
3041 : : return;
3042 : :
3043 : : /* 12 For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, C1
3044 : : is the same type as C2 or is a derived class of C2, and T is an object
3045 : : type or a function type there exist candidate operator functions of the
3046 : : form
3047 : : CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
3048 : : where CV12 is the union of CV1 and CV2. */
3049 : :
3050 : 22 : case MEMBER_REF:
3051 : 22 : if (TYPE_PTR_P (type1) && TYPE_PTRMEM_P (type2))
3052 : : {
3053 : 22 : tree c1 = TREE_TYPE (type1);
3054 : 22 : tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
3055 : :
3056 : 19 : if (CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
3057 : 38 : && (TYPE_PTRMEMFUNC_P (type2)
3058 : 6 : || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
3059 : : break;
3060 : : }
3061 : : return;
3062 : :
3063 : : /* 13 For every pair of types L and R, where each of L and R is a floating-point
3064 : : or promoted integral type, there exist candidate operator functions of the
3065 : : form
3066 : : LR operator*(L, R);
3067 : : LR operator/(L, R);
3068 : : LR operator+(L, R);
3069 : : LR operator-(L, R);
3070 : : bool operator<(L, R);
3071 : : bool operator>(L, R);
3072 : : bool operator<=(L, R);
3073 : : bool operator>=(L, R);
3074 : : bool operator==(L, R);
3075 : : bool operator!=(L, R);
3076 : : where LR is the result of the usual arithmetic conversions between
3077 : : types L and R.
3078 : :
3079 : : 14 For every integral type T there exists a candidate operator function of
3080 : : the form
3081 : :
3082 : : std::strong_ordering operator<=>(T, T);
3083 : :
3084 : : 15 For every pair of floating-point types L and R, there exists a candidate
3085 : : operator function of the form
3086 : :
3087 : : std::partial_ordering operator<=>(L, R);
3088 : :
3089 : : 16 For every cv-qualified or cv-unqualified object type T there exist
3090 : : candidate operator functions of the form
3091 : : T* operator+(T*, std::ptrdiff_t);
3092 : : T& operator[](T*, std::ptrdiff_t);
3093 : : T* operator-(T*, std::ptrdiff_t);
3094 : : T* operator+(std::ptrdiff_t, T*);
3095 : : T& operator[](std::ptrdiff_t, T*);
3096 : :
3097 : : 17 For every T, where T is a pointer to object type, there exist candidate
3098 : : operator functions of the form
3099 : : std::ptrdiff_t operator-(T, T);
3100 : :
3101 : : 18 For every T, where T is an enumeration type or a pointer type, there
3102 : : exist candidate operator functions of the form
3103 : : bool operator<(T, T);
3104 : : bool operator>(T, T);
3105 : : bool operator<=(T, T);
3106 : : bool operator>=(T, T);
3107 : : bool operator==(T, T);
3108 : : bool operator!=(T, T);
3109 : : R operator<=>(T, T);
3110 : :
3111 : : where R is the result type specified in [expr.spaceship].
3112 : :
3113 : : 19 For every T, where T is a pointer-to-member type or std::nullptr_t,
3114 : : there exist candidate operator functions of the form
3115 : : bool operator==(T, T);
3116 : : bool operator!=(T, T); */
3117 : :
3118 : 141444 : case MINUS_EXPR:
3119 : 141444 : if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
3120 : : break;
3121 : 18 : if (TYPE_PTROB_P (type1)
3122 : 141436 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3123 : : {
3124 : 8 : type2 = ptrdiff_type_node;
3125 : 8 : break;
3126 : : }
3127 : : /* FALLTHRU */
3128 : 304531 : case MULT_EXPR:
3129 : 304531 : case TRUNC_DIV_EXPR:
3130 : 304531 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3131 : : break;
3132 : : return;
3133 : :
3134 : : /* This isn't exactly what's specified above for operator<=>, but it's
3135 : : close enough. In particular, we don't care about the return type
3136 : : specified above; it doesn't participate in overload resolution and it
3137 : : doesn't affect the semantics of the built-in operator. */
3138 : 6579312 : case SPACESHIP_EXPR:
3139 : 6579312 : case EQ_EXPR:
3140 : 6579312 : case NE_EXPR:
3141 : 109541 : if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
3142 : 6688853 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)))
3143 : : break;
3144 : 6579278 : if (NULLPTR_TYPE_P (type1) && NULLPTR_TYPE_P (type2))
3145 : : break;
3146 : 6579272 : if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (args[1]))
3147 : : {
3148 : : type2 = type1;
3149 : : break;
3150 : : }
3151 : 6579269 : if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (args[0]))
3152 : : {
3153 : : type1 = type2;
3154 : : break;
3155 : : }
3156 : : /* Fall through. */
3157 : 6953062 : case LT_EXPR:
3158 : 6953062 : case GT_EXPR:
3159 : 6953062 : case LE_EXPR:
3160 : 6953062 : case GE_EXPR:
3161 : 6953062 : case MAX_EXPR:
3162 : 6953062 : case MIN_EXPR:
3163 : 6953062 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3164 : : break;
3165 : 5408402 : if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3166 : : break;
3167 : 5407982 : if (TREE_CODE (type1) == ENUMERAL_TYPE
3168 : 3850742 : && TREE_CODE (type2) == ENUMERAL_TYPE)
3169 : : break;
3170 : 2840162 : if (TYPE_PTR_P (type1)
3171 : 2840162 : && null_ptr_cst_p (args[1]))
3172 : : {
3173 : : type2 = type1;
3174 : : break;
3175 : : }
3176 : 2840143 : if (null_ptr_cst_p (args[0])
3177 : 2840143 : && TYPE_PTR_P (type2))
3178 : : {
3179 : : type1 = type2;
3180 : : break;
3181 : : }
3182 : : return;
3183 : :
3184 : 188966 : case PLUS_EXPR:
3185 : 188966 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3186 : : break;
3187 : : /* FALLTHRU */
3188 : 144997 : case ARRAY_REF:
3189 : 144997 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
3190 : : {
3191 : 5 : type1 = ptrdiff_type_node;
3192 : 5 : break;
3193 : : }
3194 : 144992 : if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3195 : : {
3196 : 37730 : type2 = ptrdiff_type_node;
3197 : 37730 : break;
3198 : : }
3199 : : return;
3200 : :
3201 : : /* 18For every pair of promoted integral types L and R, there exist candi-
3202 : : date operator functions of the form
3203 : : LR operator%(L, R);
3204 : : LR operator&(L, R);
3205 : : LR operator^(L, R);
3206 : : LR operator|(L, R);
3207 : : L operator<<(L, R);
3208 : : L operator>>(L, R);
3209 : : where LR is the result of the usual arithmetic conversions between
3210 : : types L and R. */
3211 : :
3212 : 2440497 : case TRUNC_MOD_EXPR:
3213 : 2440497 : case BIT_AND_EXPR:
3214 : 2440497 : case BIT_IOR_EXPR:
3215 : 2440497 : case BIT_XOR_EXPR:
3216 : 2440497 : case LSHIFT_EXPR:
3217 : 2440497 : case RSHIFT_EXPR:
3218 : 2440497 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3219 : : break;
3220 : : return;
3221 : :
3222 : : /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
3223 : : type, VQ is either volatile or empty, and R is a promoted arithmetic
3224 : : type, there exist candidate operator functions of the form
3225 : : VQ L& operator=(VQ L&, R);
3226 : : VQ L& operator*=(VQ L&, R);
3227 : : VQ L& operator/=(VQ L&, R);
3228 : : VQ L& operator+=(VQ L&, R);
3229 : : VQ L& operator-=(VQ L&, R);
3230 : :
3231 : : 20For every pair T, VQ), where T is any type and VQ is either volatile
3232 : : or empty, there exist candidate operator functions of the form
3233 : : T*VQ& operator=(T*VQ&, T*);
3234 : :
3235 : : 21For every pair T, VQ), where T is a pointer to member type and VQ is
3236 : : either volatile or empty, there exist candidate operator functions of
3237 : : the form
3238 : : VQ T& operator=(VQ T&, T);
3239 : :
3240 : : 22For every triple T, VQ, I), where T is a cv-qualified or cv-
3241 : : unqualified complete object type, VQ is either volatile or empty, and
3242 : : I is a promoted integral type, there exist candidate operator func-
3243 : : tions of the form
3244 : : T*VQ& operator+=(T*VQ&, I);
3245 : : T*VQ& operator-=(T*VQ&, I);
3246 : :
3247 : : 23For every triple L, VQ, R), where L is an integral or enumeration
3248 : : type, VQ is either volatile or empty, and R is a promoted integral
3249 : : type, there exist candidate operator functions of the form
3250 : :
3251 : : VQ L& operator%=(VQ L&, R);
3252 : : VQ L& operator<<=(VQ L&, R);
3253 : : VQ L& operator>>=(VQ L&, R);
3254 : : VQ L& operator&=(VQ L&, R);
3255 : : VQ L& operator^=(VQ L&, R);
3256 : : VQ L& operator|=(VQ L&, R); */
3257 : :
3258 : 1093628 : case MODIFY_EXPR:
3259 : 1093628 : switch (code2)
3260 : : {
3261 : 25958 : case PLUS_EXPR:
3262 : 25958 : case MINUS_EXPR:
3263 : 25958 : if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3264 : : {
3265 : 0 : type2 = ptrdiff_type_node;
3266 : 0 : break;
3267 : : }
3268 : : /* FALLTHRU */
3269 : 25963 : case MULT_EXPR:
3270 : 25963 : case TRUNC_DIV_EXPR:
3271 : 25963 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3272 : : break;
3273 : : return;
3274 : :
3275 : 1067665 : case TRUNC_MOD_EXPR:
3276 : 1067665 : case BIT_AND_EXPR:
3277 : 1067665 : case BIT_IOR_EXPR:
3278 : 1067665 : case BIT_XOR_EXPR:
3279 : 1067665 : case LSHIFT_EXPR:
3280 : 1067665 : case RSHIFT_EXPR:
3281 : 1067665 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3282 : : break;
3283 : : return;
3284 : :
3285 : 0 : case NOP_EXPR:
3286 : 0 : if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3287 : : break;
3288 : 0 : if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
3289 : 0 : || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3290 : 0 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
3291 : 0 : || ((TYPE_PTRMEMFUNC_P (type1)
3292 : 0 : || TYPE_PTR_P (type1))
3293 : 0 : && null_ptr_cst_p (args[1])))
3294 : : {
3295 : : type2 = type1;
3296 : : break;
3297 : : }
3298 : : return;
3299 : :
3300 : 0 : default:
3301 : 0 : gcc_unreachable ();
3302 : : }
3303 : 1004066 : type1 = build_reference_type (type1);
3304 : 1004066 : break;
3305 : :
3306 : 2732 : case COND_EXPR:
3307 : : /* [over.built]
3308 : :
3309 : : For every pair of promoted arithmetic types L and R, there
3310 : : exist candidate operator functions of the form
3311 : :
3312 : : LR operator?(bool, L, R);
3313 : :
3314 : : where LR is the result of the usual arithmetic conversions
3315 : : between types L and R.
3316 : :
3317 : : For every type T, where T is a pointer or pointer-to-member
3318 : : type, there exist candidate operator functions of the form T
3319 : : operator?(bool, T, T); */
3320 : :
3321 : 2732 : if (promoted_arithmetic_type_p (type1)
3322 : 2732 : && promoted_arithmetic_type_p (type2))
3323 : : /* That's OK. */
3324 : : break;
3325 : :
3326 : : /* Otherwise, the types should be pointers. */
3327 : 2718 : if (!((TYPE_PTR_OR_PTRMEM_P (type1) || null_ptr_cst_p (args[0]))
3328 : 160 : && (TYPE_PTR_OR_PTRMEM_P (type2) || null_ptr_cst_p (args[1]))))
3329 : 2667 : return;
3330 : :
3331 : : /* We don't check that the two types are the same; the logic
3332 : : below will actually create two candidates; one in which both
3333 : : parameter types are TYPE1, and one in which both parameter
3334 : : types are TYPE2. */
3335 : : break;
3336 : :
3337 : 3 : case REALPART_EXPR:
3338 : 3 : case IMAGPART_EXPR:
3339 : 3 : if (ARITHMETIC_TYPE_P (type1))
3340 : : break;
3341 : : return;
3342 : :
3343 : 0 : default:
3344 : 0 : gcc_unreachable ();
3345 : : }
3346 : :
3347 : : /* Make sure we don't create builtin candidates with dependent types. */
3348 : 7533991 : bool u1 = uses_template_parms (type1);
3349 : 7533991 : bool u2 = type2 ? uses_template_parms (type2) : false;
3350 : 7533977 : if (u1 || u2)
3351 : : {
3352 : : /* Try to recover if one of the types is non-dependent. But if
3353 : : there's only one type, there's nothing we can do. */
3354 : 20 : if (!type2)
3355 : : return;
3356 : : /* And we lose if both are dependent. */
3357 : 17 : if (u1 && u2)
3358 : : return;
3359 : : /* Or if they have different forms. */
3360 : 9 : if (TREE_CODE (type1) != TREE_CODE (type2))
3361 : : return;
3362 : :
3363 : 9 : if (u1 && !u2)
3364 : : type1 = type2;
3365 : 6 : else if (u2 && !u1)
3366 : 7533980 : type2 = type1;
3367 : : }
3368 : :
3369 : : /* If we're dealing with two pointer types or two enumeral types,
3370 : : we need candidates for both of them. */
3371 : 7392496 : if (type2 && !same_type_p (type1, type2)
3372 : 1335246 : && TREE_CODE (type1) == TREE_CODE (type2)
3373 : 7827137 : && (TYPE_REF_P (type1)
3374 : 293157 : || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3375 : 293040 : || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
3376 : 293022 : || TYPE_PTRMEMFUNC_P (type1)
3377 : 293022 : || MAYBE_CLASS_TYPE_P (type1)
3378 : 293022 : || TREE_CODE (type1) == ENUMERAL_TYPE))
3379 : : {
3380 : 229 : if (TYPE_PTR_OR_PTRMEM_P (type1))
3381 : : {
3382 : 135 : tree cptype = composite_pointer_type (input_location,
3383 : : type1, type2,
3384 : : error_mark_node,
3385 : : error_mark_node,
3386 : : CPO_CONVERSION,
3387 : : tf_none);
3388 : 135 : if (cptype != error_mark_node)
3389 : : {
3390 : 85 : build_builtin_candidate
3391 : 85 : (candidates, fnname, cptype, cptype, args, argtypes,
3392 : : flags, complain);
3393 : 85 : return;
3394 : : }
3395 : : }
3396 : :
3397 : 144 : build_builtin_candidate
3398 : 144 : (candidates, fnname, type1, type1, args, argtypes, flags, complain);
3399 : 144 : build_builtin_candidate
3400 : 144 : (candidates, fnname, type2, type2, args, argtypes, flags, complain);
3401 : 144 : return;
3402 : : }
3403 : :
3404 : 7533751 : build_builtin_candidate
3405 : 7533751 : (candidates, fnname, type1, type2, args, argtypes, flags, complain);
3406 : : }
3407 : :
3408 : : tree
3409 : 707465557 : type_decays_to (tree type)
3410 : : {
3411 : 707465557 : if (TREE_CODE (type) == ARRAY_TYPE)
3412 : 5516188 : return build_pointer_type (TREE_TYPE (type));
3413 : 701949369 : if (TREE_CODE (type) == FUNCTION_TYPE)
3414 : 39833 : return build_pointer_type (type);
3415 : : return type;
3416 : : }
3417 : :
3418 : : /* There are three conditions of builtin candidates:
3419 : :
3420 : : 1) bool-taking candidates. These are the same regardless of the input.
3421 : : 2) pointer-pair taking candidates. These are generated for each type
3422 : : one of the input types converts to.
3423 : : 3) arithmetic candidates. According to the standard, we should generate
3424 : : all of these, but I'm trying not to...
3425 : :
3426 : : Here we generate a superset of the possible candidates for this particular
3427 : : case. That is a subset of the full set the standard defines, plus some
3428 : : other cases which the standard disallows. add_builtin_candidate will
3429 : : filter out the invalid set. */
3430 : :
3431 : : static void
3432 : 16883101 : add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
3433 : : enum tree_code code2, tree fnname,
3434 : : vec<tree, va_gc> *argv,
3435 : : int flags, tsubst_flags_t complain)
3436 : : {
3437 : 16883101 : int ref1;
3438 : 16883101 : int enum_p = 0;
3439 : 16883101 : tree type, argtypes[3], t;
3440 : : /* TYPES[i] is the set of possible builtin-operator parameter types
3441 : : we will consider for the Ith argument. */
3442 : 16883101 : vec<tree, va_gc> *types[2];
3443 : 16883101 : unsigned ix;
3444 : 16883101 : vec<tree, va_gc> &args = *argv;
3445 : 16883101 : unsigned len = args.length ();
3446 : :
3447 : 46669040 : for (unsigned i = 0; i < len; ++i)
3448 : : {
3449 : 29785939 : if (args[i])
3450 : 29785939 : argtypes[i] = unlowered_expr_type (args[i]);
3451 : : else
3452 : 0 : argtypes[i] = NULL_TREE;
3453 : : }
3454 : :
3455 : 16883101 : switch (code)
3456 : : {
3457 : : /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
3458 : : and VQ is either volatile or empty, there exist candidate operator
3459 : : functions of the form
3460 : : VQ T& operator++(VQ T&); */
3461 : :
3462 : : case POSTINCREMENT_EXPR:
3463 : : case PREINCREMENT_EXPR:
3464 : : case POSTDECREMENT_EXPR:
3465 : : case PREDECREMENT_EXPR:
3466 : : case MODIFY_EXPR:
3467 : : ref1 = 1;
3468 : : break;
3469 : :
3470 : : /* 24There also exist candidate operator functions of the form
3471 : : bool operator!(bool);
3472 : : bool operator&&(bool, bool);
3473 : : bool operator||(bool, bool); */
3474 : :
3475 : 497621 : case TRUTH_NOT_EXPR:
3476 : 497621 : build_builtin_candidate
3477 : 497621 : (candidates, fnname, boolean_type_node,
3478 : : NULL_TREE, args, argtypes, flags, complain);
3479 : 9584839 : return;
3480 : :
3481 : 57500 : case TRUTH_ORIF_EXPR:
3482 : 57500 : case TRUTH_ANDIF_EXPR:
3483 : 57500 : build_builtin_candidate
3484 : 57500 : (candidates, fnname, boolean_type_node,
3485 : : boolean_type_node, args, argtypes, flags, complain);
3486 : 57500 : return;
3487 : :
3488 : : case ADDR_EXPR:
3489 : : case COMPOUND_EXPR:
3490 : : case COMPONENT_REF:
3491 : : case CO_AWAIT_EXPR:
3492 : : return;
3493 : :
3494 : 4504862 : case COND_EXPR:
3495 : 4504862 : case EQ_EXPR:
3496 : 4504862 : case NE_EXPR:
3497 : 4504862 : case LT_EXPR:
3498 : 4504862 : case LE_EXPR:
3499 : 4504862 : case GT_EXPR:
3500 : 4504862 : case GE_EXPR:
3501 : 4504862 : case SPACESHIP_EXPR:
3502 : 4504862 : enum_p = 1;
3503 : : /* Fall through. */
3504 : :
3505 : : default:
3506 : : ref1 = 0;
3507 : : }
3508 : :
3509 : 14484894 : types[0] = make_tree_vector ();
3510 : 14484894 : types[1] = make_tree_vector ();
3511 : :
3512 : 14484894 : if (len == 3)
3513 : 862 : len = 2;
3514 : 30064706 : for (unsigned i = 0; i < len; ++i)
3515 : : {
3516 : 22268823 : if (MAYBE_CLASS_TYPE_P (argtypes[i]))
3517 : : {
3518 : 8501579 : tree convs;
3519 : :
3520 : 8501579 : if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
3521 : : return;
3522 : :
3523 : 6135406 : convs = lookup_conversions (argtypes[i]);
3524 : :
3525 : 6135406 : if (code == COND_EXPR)
3526 : : {
3527 : 1491 : if (lvalue_p (args[i]))
3528 : 1053 : vec_safe_push (types[i], build_reference_type (argtypes[i]));
3529 : :
3530 : 1491 : vec_safe_push (types[i], TYPE_MAIN_VARIANT (argtypes[i]));
3531 : : }
3532 : :
3533 : 6133915 : else if (! convs)
3534 : : return;
3535 : :
3536 : 4130404 : for (; convs; convs = TREE_CHAIN (convs))
3537 : : {
3538 : 2317836 : type = TREE_TYPE (convs);
3539 : :
3540 : 2910387 : if (i == 0 && ref1
3541 : 2317836 : && (!TYPE_REF_P (type)
3542 : 39 : || CP_TYPE_CONST_P (TREE_TYPE (type))))
3543 : 592551 : continue;
3544 : :
3545 : 1725285 : if (code == COND_EXPR && TYPE_REF_P (type))
3546 : 0 : vec_safe_push (types[i], type);
3547 : :
3548 : 1725285 : type = non_reference (type);
3549 : 1725285 : if (i != 0 || ! ref1)
3550 : : {
3551 : 1725246 : type = cv_unqualified (type_decays_to (type));
3552 : 1725246 : if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
3553 : 0 : vec_safe_push (types[i], type);
3554 : 1725246 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3555 : 895534 : type = type_promotes_to (type);
3556 : : }
3557 : :
3558 : 1725285 : if (! vec_member (type, types[i]))
3559 : 1723000 : vec_safe_push (types[i], type);
3560 : : }
3561 : : }
3562 : : else
3563 : : {
3564 : 13767244 : if (code == COND_EXPR && lvalue_p (args[i]))
3565 : 125 : vec_safe_push (types[i], build_reference_type (argtypes[i]));
3566 : 13767244 : type = non_reference (argtypes[i]);
3567 : 13767244 : if (i != 0 || ! ref1)
3568 : : {
3569 : 12673732 : type = cv_unqualified (type_decays_to (type));
3570 : 12673732 : if (enum_p && UNSCOPED_ENUM_P (type))
3571 : 2497733 : vec_safe_push (types[i], type);
3572 : 12673732 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3573 : 8621087 : type = type_promotes_to (type);
3574 : : }
3575 : 13767244 : vec_safe_push (types[i], type);
3576 : : }
3577 : : }
3578 : :
3579 : : /* Run through the possible parameter types of both arguments,
3580 : : creating candidates with those parameter types. */
3581 : 16546528 : FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t)
3582 : : {
3583 : 8750645 : unsigned jx;
3584 : 8750645 : tree u;
3585 : :
3586 : 8750645 : if (!types[1]->is_empty ())
3587 : 19761661 : FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u)
3588 : 11011016 : add_builtin_candidate
3589 : 11011016 : (candidates, code, code2, fnname, t,
3590 : : u, args, argtypes, flags, complain);
3591 : : else
3592 : 265100 : add_builtin_candidate
3593 : 265100 : (candidates, code, code2, fnname, t,
3594 : : NULL_TREE, args, argtypes, flags, complain);
3595 : : }
3596 : :
3597 : 7795883 : release_tree_vector (types[0]);
3598 : 7795883 : release_tree_vector (types[1]);
3599 : : }
3600 : :
3601 : :
3602 : : /* If TMPL can be successfully instantiated as indicated by
3603 : : EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
3604 : :
3605 : : TMPL is the template. EXPLICIT_TARGS are any explicit template
3606 : : arguments. ARGLIST is the arguments provided at the call-site.
3607 : : This does not change ARGLIST. The RETURN_TYPE is the desired type
3608 : : for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
3609 : : as for add_function_candidate. If an OBJ is supplied, FLAGS and
3610 : : CTYPE are ignored, and OBJ is as for add_conv_candidate.
3611 : :
3612 : : SHORTCUT_BAD_CONVS is as in add_function_candidate. */
3613 : :
3614 : : static struct z_candidate*
3615 : 363731683 : add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
3616 : : tree ctype, tree explicit_targs, tree first_arg,
3617 : : const vec<tree, va_gc> *arglist, tree return_type,
3618 : : tree access_path, tree conversion_path,
3619 : : int flags, tree obj, unification_kind_t strict,
3620 : : bool shortcut_bad_convs, tsubst_flags_t complain)
3621 : : {
3622 : 363731683 : int ntparms = DECL_NTPARMS (tmpl);
3623 : 363731683 : tree targs = make_tree_vec (ntparms);
3624 : 363731683 : unsigned int len = vec_safe_length (arglist);
3625 : 363731683 : unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
3626 : 363731683 : unsigned int skip_without_in_chrg = 0;
3627 : 363731683 : tree first_arg_without_in_chrg = first_arg;
3628 : 363731683 : tree *args_without_in_chrg;
3629 : 363731683 : unsigned int nargs_without_in_chrg;
3630 : 363731683 : unsigned int ia, ix;
3631 : 363731683 : tree arg;
3632 : 363731683 : struct z_candidate *cand;
3633 : 363731683 : tree fn;
3634 : 363731683 : struct rejection_reason *reason = NULL;
3635 : 363731683 : int errs;
3636 : 363731683 : conversion **convs = NULL;
3637 : :
3638 : : /* We don't do deduction on the in-charge parameter, the VTT
3639 : : parameter or 'this'. */
3640 : 363731683 : if (DECL_IOBJ_MEMBER_FUNCTION_P (tmpl))
3641 : : {
3642 : 44191179 : if (first_arg_without_in_chrg != NULL_TREE)
3643 : : first_arg_without_in_chrg = NULL_TREE;
3644 : 12 : else if (return_type && strict == DEDUCE_CALL)
3645 : : /* We're deducing for a call to the result of a template conversion
3646 : : function, so the args don't contain 'this'; leave them alone. */;
3647 : : else
3648 : 363731683 : ++skip_without_in_chrg;
3649 : : }
3650 : :
3651 : 363731683 : if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
3652 : 363731683 : || DECL_BASE_CONSTRUCTOR_P (tmpl))
3653 : 364688164 : && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
3654 : : {
3655 : 489 : if (first_arg_without_in_chrg != NULL_TREE)
3656 : : first_arg_without_in_chrg = NULL_TREE;
3657 : : else
3658 : 489 : ++skip_without_in_chrg;
3659 : : }
3660 : :
3661 : 363731683 : if (len < skip_without_in_chrg)
3662 : 0 : return add_ignored_candidate (candidates, tmpl);
3663 : :
3664 : 404460602 : if (DECL_CONSTRUCTOR_P (tmpl) && nargs == 2
3665 : 395837335 : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (first_arg),
3666 : 32105652 : TREE_TYPE ((*arglist)[0])))
3667 : : {
3668 : : /* 12.8/6 says, "A declaration of a constructor for a class X is
3669 : : ill-formed if its first parameter is of type (optionally cv-qualified)
3670 : : X and either there are no other parameters or else all other
3671 : : parameters have default arguments. A member function template is never
3672 : : instantiated to produce such a constructor signature."
3673 : :
3674 : : So if we're trying to copy an object of the containing class, don't
3675 : : consider a template constructor that has a first parameter type that
3676 : : is just a template parameter, as we would deduce a signature that we
3677 : : would then reject in the code below. */
3678 : 1666152 : if (tree firstparm = FUNCTION_FIRST_USER_PARMTYPE (tmpl))
3679 : : {
3680 : 1666152 : firstparm = TREE_VALUE (firstparm);
3681 : 1666152 : if (PACK_EXPANSION_P (firstparm))
3682 : 1583 : firstparm = PACK_EXPANSION_PATTERN (firstparm);
3683 : 1666152 : if (TREE_CODE (firstparm) == TEMPLATE_TYPE_PARM)
3684 : : {
3685 : 578426 : gcc_assert (!explicit_targs);
3686 : 578426 : reason = invalid_copy_with_fn_template_rejection ();
3687 : 578426 : goto fail;
3688 : : }
3689 : : }
3690 : : }
3691 : :
3692 : 363153257 : nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
3693 : 363153257 : + (len - skip_without_in_chrg));
3694 : 363153257 : args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
3695 : 363153257 : ia = 0;
3696 : 363153257 : if (first_arg_without_in_chrg != NULL_TREE)
3697 : : {
3698 : 4014 : args_without_in_chrg[ia] = first_arg_without_in_chrg;
3699 : 4014 : ++ia;
3700 : : }
3701 : 601797106 : for (ix = skip_without_in_chrg;
3702 : 964950363 : vec_safe_iterate (arglist, ix, &arg);
3703 : : ++ix)
3704 : : {
3705 : 601797106 : args_without_in_chrg[ia] = arg;
3706 : 601797106 : ++ia;
3707 : : }
3708 : 363153257 : gcc_assert (ia == nargs_without_in_chrg);
3709 : :
3710 : 363153257 : if (!obj)
3711 : : {
3712 : : /* Check that there's no obvious arity mismatch before proceeding with
3713 : : deduction. This avoids substituting explicit template arguments
3714 : : into the template or e.g. derived-to-base parm/arg unification
3715 : : (which could result in an error outside the immediate context) when
3716 : : the resulting candidate would be unviable anyway. */
3717 : 363153245 : int min_arity = 0, max_arity = 0;
3718 : 363153245 : tree parms = TYPE_ARG_TYPES (TREE_TYPE (tmpl));
3719 : 363153245 : parms = skip_artificial_parms_for (tmpl, parms);
3720 : 1364472613 : for (; parms != void_list_node; parms = TREE_CHAIN (parms))
3721 : : {
3722 : 1282568210 : if (!parms || PACK_EXPANSION_P (TREE_VALUE (parms)))
3723 : : {
3724 : : max_arity = -1;
3725 : : break;
3726 : : }
3727 : 638166123 : if (TREE_PURPOSE (parms))
3728 : : /* A parameter with a default argument. */
3729 : 8189685 : ++max_arity;
3730 : : else
3731 : 629976438 : ++min_arity, ++max_arity;
3732 : : }
3733 : 363153245 : if (ia < (unsigned)min_arity)
3734 : : {
3735 : : /* Too few arguments. */
3736 : 28944156 : reason = arity_rejection (NULL_TREE, min_arity, ia,
3737 : : /*least_p=*/(max_arity == -1));
3738 : 28944156 : goto fail;
3739 : : }
3740 : 334209089 : else if (max_arity != -1 && ia > (unsigned)max_arity)
3741 : : {
3742 : : /* Too many arguments. */
3743 : 4471340 : reason = arity_rejection (NULL_TREE, max_arity, ia);
3744 : 4471340 : goto fail;
3745 : : }
3746 : :
3747 : 329737749 : convs = alloc_conversions (nargs);
3748 : :
3749 : 329737749 : if (shortcut_bad_convs
3750 : 329733765 : && DECL_IOBJ_MEMBER_FUNCTION_P (tmpl)
3751 : 386883109 : && !DECL_CONSTRUCTOR_P (tmpl))
3752 : : {
3753 : : /* Check the 'this' conversion before proceeding with deduction.
3754 : : This is effectively an extension of the DR 1391 resolution
3755 : : that we perform in check_non_deducible_conversions, though it's
3756 : : convenient to do this extra check here instead of there. */
3757 : 3057414 : tree parmtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (tmpl)));
3758 : 3057414 : tree argtype = lvalue_type (first_arg);
3759 : 3057414 : tree arg = first_arg;
3760 : 3057414 : conversion *t = build_this_conversion (tmpl, ctype,
3761 : : parmtype, argtype, arg,
3762 : : flags, complain);
3763 : 3057414 : convs[0] = t;
3764 : 3057414 : if (t->bad_p)
3765 : : {
3766 : 33087 : reason = bad_arg_conversion_rejection (first_arg, 0,
3767 : : arg, parmtype,
3768 : 33087 : EXPR_LOCATION (arg));
3769 : 33087 : goto fail;
3770 : : }
3771 : : }
3772 : : }
3773 : :
3774 : 329704674 : errs = errorcount+sorrycount;
3775 : 659395797 : fn = fn_type_unification (tmpl, explicit_targs, targs,
3776 : : args_without_in_chrg,
3777 : : nargs_without_in_chrg,
3778 : : return_type, strict, flags, convs,
3779 : 329704674 : false, complain & tf_decltype);
3780 : :
3781 : 329691123 : if (fn == error_mark_node)
3782 : : {
3783 : : /* Don't repeat unification later if it already resulted in errors. */
3784 : 264639471 : if (errorcount+sorrycount == errs)
3785 : 264639362 : reason = template_unification_rejection (tmpl, explicit_targs,
3786 : : targs, args_without_in_chrg,
3787 : : nargs_without_in_chrg,
3788 : : return_type, strict, flags);
3789 : : else
3790 : 109 : reason = template_unification_error_rejection ();
3791 : 264639471 : goto fail;
3792 : : }
3793 : :
3794 : : /* Now the explicit specifier might have been deduced; check if this
3795 : : declaration is explicit. If it is and we're ignoring non-converting
3796 : : constructors, don't add this function to the set of candidates. */
3797 : 65051652 : if (((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
3798 : : == LOOKUP_ONLYCONVERTING)
3799 : 65051652 : && DECL_NONCONVERTING_P (fn))
3800 : 937 : return add_ignored_candidate (candidates, fn);
3801 : :
3802 : 130101430 : if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
3803 : : {
3804 : 3046748 : tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
3805 : 6093469 : if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
3806 : : ctype))
3807 : : {
3808 : : /* We're trying to produce a constructor with a prohibited signature,
3809 : : as discussed above; handle here any cases we didn't catch then,
3810 : : such as X(X<T>). */
3811 : 245 : reason = invalid_copy_with_fn_template_rejection ();
3812 : 245 : goto fail;
3813 : : }
3814 : : }
3815 : :
3816 : 65050470 : if (obj != NULL_TREE)
3817 : : /* Aha, this is a conversion function. */
3818 : 9 : cand = add_conv_candidate (candidates, fn, obj, arglist,
3819 : : access_path, conversion_path, complain);
3820 : : else
3821 : 65050461 : cand = add_function_candidate (candidates, fn, ctype,
3822 : : first_arg, arglist, access_path,
3823 : : conversion_path, flags, convs,
3824 : : shortcut_bad_convs, complain);
3825 : 65050470 : if (DECL_TI_TEMPLATE (fn) != tmpl)
3826 : : /* This situation can occur if a member template of a template
3827 : : class is specialized. Then, instantiate_template might return
3828 : : an instantiation of the specialization, in which case the
3829 : : DECL_TI_TEMPLATE field will point at the original
3830 : : specialization. For example:
3831 : :
3832 : : template <class T> struct S { template <class U> void f(U);
3833 : : template <> void f(int) {}; };
3834 : : S<double> sd;
3835 : : sd.f(3);
3836 : :
3837 : : Here, TMPL will be template <class U> S<double>::f(U).
3838 : : And, instantiate template will give us the specialization
3839 : : template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
3840 : : for this will point at template <class T> template <> S<T>::f(int),
3841 : : so that we can find the definition. For the purposes of
3842 : : overload resolution, however, we want the original TMPL. */
3843 : 3944777 : cand->template_decl = build_template_info (tmpl, targs);
3844 : : else
3845 : 61105693 : cand->template_decl = DECL_TEMPLATE_INFO (fn);
3846 : 65050470 : cand->explicit_targs = explicit_targs;
3847 : :
3848 : 65050470 : return cand;
3849 : 298666725 : fail:
3850 : 298666725 : int viable = (reason->code == rr_bad_arg_conversion ? -1 : 0);
3851 : 298666725 : return add_candidate (candidates, tmpl, first_arg, arglist, nargs, convs,
3852 : 298666725 : access_path, conversion_path, viable, reason, flags);
3853 : : }
3854 : :
3855 : :
3856 : : static struct z_candidate *
3857 : 363731671 : add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
3858 : : tree explicit_targs, tree first_arg,
3859 : : const vec<tree, va_gc> *arglist, tree return_type,
3860 : : tree access_path, tree conversion_path, int flags,
3861 : : unification_kind_t strict, bool shortcut_bad_convs,
3862 : : tsubst_flags_t complain)
3863 : : {
3864 : 363731671 : return
3865 : 0 : add_template_candidate_real (candidates, tmpl, ctype,
3866 : : explicit_targs, first_arg, arglist,
3867 : : return_type, access_path, conversion_path,
3868 : : flags, NULL_TREE, strict, shortcut_bad_convs,
3869 : 363718120 : complain);
3870 : : }
3871 : :
3872 : : /* Create an overload candidate for the conversion function template TMPL,
3873 : : returning RETURN_TYPE, which will be invoked for expression OBJ to produce a
3874 : : pointer-to-function which will in turn be called with the argument list
3875 : : ARGLIST, and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
3876 : : passed on to implicit_conversion. */
3877 : :
3878 : : static struct z_candidate *
3879 : 12 : add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
3880 : : tree obj,
3881 : : const vec<tree, va_gc> *arglist,
3882 : : tree return_type, tree access_path,
3883 : : tree conversion_path, tsubst_flags_t complain)
3884 : : {
3885 : 12 : return
3886 : 12 : add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
3887 : : NULL_TREE, arglist, return_type, access_path,
3888 : : conversion_path, 0, obj, DEDUCE_CALL,
3889 : 12 : /*shortcut_bad_convs=*/false, complain);
3890 : : }
3891 : :
3892 : : /* The CANDS are the set of candidates that were considered for
3893 : : overload resolution. Sort CANDS so that the strictly viable
3894 : : candidates appear first, followed by non-strictly viable candidates,
3895 : : followed by non-viable candidates. Returns the first candidate
3896 : : in this sorted list. If any of the candidates were viable, set
3897 : : *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
3898 : : considered viable only if it is strictly viable when setting
3899 : : *ANY_VIABLE_P. */
3900 : :
3901 : : static struct z_candidate*
3902 : 236908841 : splice_viable (struct z_candidate *cands,
3903 : : bool strict_p,
3904 : : bool *any_viable_p)
3905 : : {
3906 : 236908841 : z_candidate *strictly_viable = nullptr;
3907 : 236908841 : z_candidate **strictly_viable_tail = &strictly_viable;
3908 : :
3909 : 236908841 : z_candidate *non_strictly_viable = nullptr;
3910 : 236908841 : z_candidate **non_strictly_viable_tail = &non_strictly_viable;
3911 : :
3912 : 236908841 : z_candidate *non_viable = nullptr;
3913 : 236908841 : z_candidate **non_viable_tail = &non_viable;
3914 : :
3915 : 236908841 : z_candidate *non_viable_ignored = nullptr;
3916 : 236908841 : z_candidate **non_viable_ignored_tail = &non_viable_ignored;
3917 : :
3918 : : /* Be strict inside templates, since build_over_call won't actually
3919 : : do the conversions to get pedwarns. */
3920 : 236908841 : if (processing_template_decl)
3921 : 39866323 : strict_p = true;
3922 : :
3923 : 899694661 : for (z_candidate *cand = cands; cand; cand = cand->next)
3924 : : {
3925 : 662785820 : if (!strict_p
3926 : 234504767 : && (cand->viable == 1 || TREE_CODE (cand->fn) == TEMPLATE_DECL))
3927 : : /* Be strict in the presence of a viable candidate. Also if
3928 : : there are template candidates, so that we get deduction errors
3929 : : for them instead of silently preferring a bad conversion. */
3930 : 595831473 : strict_p = true;
3931 : :
3932 : : /* Move this candidate to the appropriate list according to
3933 : : its viability. */
3934 : 662785820 : auto& tail = (cand->viable == 1 ? strictly_viable_tail
3935 : : : cand->viable == -1 ? non_strictly_viable_tail
3936 : 1029357312 : : ignored_candidate_p (cand) ? non_viable_ignored_tail
3937 : 1029357312 : : non_viable_tail);
3938 : 662785820 : *tail = cand;
3939 : 662785820 : tail = &cand->next;
3940 : : }
3941 : :
3942 : 473817682 : *any_viable_p = (strictly_viable != nullptr
3943 : 236908841 : || (!strict_p && non_strictly_viable != nullptr));
3944 : :
3945 : : /* Combine the lists. */
3946 : 236908841 : *non_viable_ignored_tail = nullptr;
3947 : 236908841 : *non_viable_tail = non_viable_ignored;
3948 : 236908841 : *non_strictly_viable_tail = non_viable;
3949 : 236908841 : *strictly_viable_tail = non_strictly_viable;
3950 : :
3951 : 236908841 : return strictly_viable;
3952 : : }
3953 : :
3954 : : static bool
3955 : 226369315 : any_strictly_viable (struct z_candidate *cands)
3956 : : {
3957 : 287269344 : for (; cands; cands = cands->next)
3958 : 64486910 : if (cands->viable == 1)
3959 : : return true;
3960 : : return false;
3961 : : }
3962 : :
3963 : : /* OBJ is being used in an expression like "OBJ.f (...)". In other
3964 : : words, it is about to become the "this" pointer for a member
3965 : : function call. Take the address of the object. */
3966 : :
3967 : : static tree
3968 : 52150757 : build_this (tree obj)
3969 : : {
3970 : : /* In a template, we are only concerned about the type of the
3971 : : expression, so we can take a shortcut. */
3972 : 52150757 : if (processing_template_decl)
3973 : 64493 : return build_address (obj);
3974 : :
3975 : 52086264 : return cp_build_addr_expr (obj, tf_warning_or_error);
3976 : : }
3977 : :
3978 : : /* Returns true iff functions are equivalent. Equivalent functions are
3979 : : not '==' only if one is a function-local extern function or if
3980 : : both are extern "C". */
3981 : :
3982 : : static inline int
3983 : 5310877 : equal_functions (tree fn1, tree fn2)
3984 : : {
3985 : 5310877 : if (TREE_CODE (fn1) != TREE_CODE (fn2))
3986 : : return 0;
3987 : 5286705 : if (TREE_CODE (fn1) == TEMPLATE_DECL)
3988 : 49210 : return fn1 == fn2;
3989 : 10474972 : if (DECL_LOCAL_DECL_P (fn1) || DECL_LOCAL_DECL_P (fn2)
3990 : 10474969 : || DECL_EXTERN_C_FUNCTION_P (fn1))
3991 : 3153050 : return decls_match (fn1, fn2);
3992 : 2084445 : return fn1 == fn2;
3993 : : }
3994 : :
3995 : : /* Print information about a candidate FN being rejected due to INFO. */
3996 : :
3997 : : static void
3998 : 5483 : print_conversion_rejection (location_t loc, struct conversion_info *info,
3999 : : tree fn)
4000 : : {
4001 : 5483 : tree from = info->from;
4002 : 5483 : if (!TYPE_P (from))
4003 : 4440 : from = lvalue_type (from);
4004 : 5483 : if (info->n_arg == -1)
4005 : : {
4006 : : /* Conversion of implicit `this' argument failed. */
4007 : 44 : if (!TYPE_P (info->from))
4008 : : /* A bad conversion for 'this' must be discarding cv-quals. */
4009 : 44 : inform (loc, "passing %qT as %<this%> "
4010 : : "argument discards qualifiers",
4011 : : from);
4012 : : else
4013 : 0 : inform (loc, "no known conversion for implicit "
4014 : : "%<this%> parameter from %qH to %qI",
4015 : : from, info->to_type);
4016 : : }
4017 : 5439 : else if (!TYPE_P (info->from))
4018 : : {
4019 : 4396 : if (info->n_arg >= 0)
4020 : 4396 : inform (loc, "conversion of argument %d would be ill-formed:",
4021 : : info->n_arg + 1);
4022 : 4396 : iloc_sentinel ils = loc;
4023 : 4396 : perform_implicit_conversion (info->to_type, info->from,
4024 : : tf_warning_or_error);
4025 : 4396 : }
4026 : 1043 : else if (info->n_arg == -2)
4027 : : /* Conversion of conversion function return value failed. */
4028 : 34 : inform (loc, "no known conversion from %qH to %qI",
4029 : : from, info->to_type);
4030 : : else
4031 : : {
4032 : 1009 : if (TREE_CODE (fn) == FUNCTION_DECL)
4033 : 852 : loc = get_fndecl_argument_location (fn, info->n_arg);
4034 : 1009 : inform (loc, "no known conversion for argument %d from %qH to %qI",
4035 : 1009 : info->n_arg + 1, from, info->to_type);
4036 : : }
4037 : 5483 : }
4038 : :
4039 : : /* Print information about a candidate with WANT parameters and we found
4040 : : HAVE. */
4041 : :
4042 : : static void
4043 : 1463 : print_arity_information (location_t loc, unsigned int have, unsigned int want,
4044 : : bool least_p)
4045 : : {
4046 : 1463 : if (least_p)
4047 : 41 : inform_n (loc, want,
4048 : : "candidate expects at least %d argument, %d provided",
4049 : : "candidate expects at least %d arguments, %d provided",
4050 : : want, have);
4051 : : else
4052 : 1422 : inform_n (loc, want,
4053 : : "candidate expects %d argument, %d provided",
4054 : : "candidate expects %d arguments, %d provided",
4055 : : want, have);
4056 : 1463 : }
4057 : :
4058 : : /* Print information about one overload candidate CANDIDATE. MSGSTR
4059 : : is the text to print before the candidate itself.
4060 : :
4061 : : NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
4062 : : to have been run through gettext by the caller. This wart makes
4063 : : life simpler in print_z_candidates and for the translators. */
4064 : :
4065 : : static void
4066 : 13675 : print_z_candidate (location_t loc, const char *msgstr,
4067 : : struct z_candidate *candidate)
4068 : : {
4069 : 13675 : const char *msg = (msgstr == NULL
4070 : 13675 : ? ""
4071 : 13675 : : ACONCAT ((_(msgstr), " ", NULL)));
4072 : 13675 : tree fn = candidate->fn;
4073 : 13675 : if (flag_new_inheriting_ctors)
4074 : 13648 : fn = strip_inheriting_ctors (fn);
4075 : 13675 : location_t cloc = location_of (fn);
4076 : :
4077 : 13675 : if (identifier_p (fn))
4078 : : {
4079 : 213 : cloc = loc;
4080 : 213 : if (candidate->num_convs == 3)
4081 : 0 : inform (cloc, "%s%<%D(%T, %T, %T)%> (built-in)", msg, fn,
4082 : 0 : candidate->convs[0]->type,
4083 : 0 : candidate->convs[1]->type,
4084 : 0 : candidate->convs[2]->type);
4085 : 213 : else if (candidate->num_convs == 2)
4086 : 188 : inform (cloc, "%s%<%D(%T, %T)%> (built-in)", msg, fn,
4087 : 188 : candidate->convs[0]->type,
4088 : 188 : candidate->convs[1]->type);
4089 : : else
4090 : 25 : inform (cloc, "%s%<%D(%T)%> (built-in)", msg, fn,
4091 : 25 : candidate->convs[0]->type);
4092 : : }
4093 : 13462 : else if (TYPE_P (fn))
4094 : 9 : inform (cloc, "%s%qT (conversion)", msg, fn);
4095 : 13453 : else if (candidate->viable == -1)
4096 : 4453 : inform (cloc, "%s%#qD (near match)", msg, fn);
4097 : 9000 : else if (ignored_candidate_p (candidate))
4098 : 6 : inform (cloc, "%s%#qD (ignored)", msg, fn);
4099 : 8994 : else if (DECL_DELETED_FN (fn))
4100 : 49 : inform (cloc, "%s%#qD (deleted)", msg, fn);
4101 : 8945 : else if (candidate->reversed ())
4102 : 88 : inform (cloc, "%s%#qD (reversed)", msg, fn);
4103 : 8857 : else if (candidate->rewritten ())
4104 : 0 : inform (cloc, "%s%#qD (rewritten)", msg, fn);
4105 : : else
4106 : 8857 : inform (cloc, "%s%#qD", msg, fn);
4107 : 13675 : if (fn != candidate->fn)
4108 : : {
4109 : 51 : cloc = location_of (candidate->fn);
4110 : 51 : inform (cloc, "inherited here");
4111 : : }
4112 : : /* Give the user some information about why this candidate failed. */
4113 : 13675 : if (candidate->reason != NULL)
4114 : : {
4115 : 10618 : auto_diagnostic_nesting_level sentinel;
4116 : 10618 : struct rejection_reason *r = candidate->reason;
4117 : :
4118 : 10618 : switch (r->code)
4119 : : {
4120 : 1463 : case rr_arity:
4121 : 1463 : print_arity_information (cloc, r->u.arity.actual,
4122 : 1463 : r->u.arity.expected,
4123 : 1463 : r->u.arity.least_p);
4124 : 1463 : break;
4125 : 1015 : case rr_arg_conversion:
4126 : 1015 : print_conversion_rejection (cloc, &r->u.conversion, fn);
4127 : 1015 : break;
4128 : 4468 : case rr_bad_arg_conversion:
4129 : 4468 : print_conversion_rejection (cloc, &r->u.bad_conversion, fn);
4130 : 4468 : break;
4131 : 10 : case rr_explicit_conversion:
4132 : 10 : inform (cloc, "return type %qT of explicit conversion function "
4133 : : "cannot be converted to %qT with a qualification "
4134 : : "conversion", r->u.conversion.from,
4135 : : r->u.conversion.to_type);
4136 : 10 : break;
4137 : 6 : case rr_template_conversion:
4138 : 6 : inform (cloc, "conversion from return type %qT of template "
4139 : : "conversion function specialization to %qT is not an "
4140 : : "exact match", r->u.conversion.from,
4141 : : r->u.conversion.to_type);
4142 : 6 : break;
4143 : 3524 : case rr_template_unification:
4144 : : /* We use template_unification_error_rejection if unification caused
4145 : : actual non-SFINAE errors, in which case we don't need to repeat
4146 : : them here. */
4147 : 3524 : if (r->u.template_unification.tmpl == NULL_TREE)
4148 : : {
4149 : 45 : inform (cloc, "substitution of deduced template arguments "
4150 : : "resulted in errors seen above");
4151 : 45 : break;
4152 : : }
4153 : : /* Re-run template unification with diagnostics. */
4154 : 3479 : inform (cloc, "template argument deduction/substitution failed:");
4155 : 3479 : {
4156 : 3479 : auto_diagnostic_nesting_level sentinel;
4157 : 3479 : fn_type_unification (r->u.template_unification.tmpl,
4158 : : r->u.template_unification.explicit_targs,
4159 : : (make_tree_vec
4160 : : (r->u.template_unification.num_targs)),
4161 : : r->u.template_unification.args,
4162 : : r->u.template_unification.nargs,
4163 : : r->u.template_unification.return_type,
4164 : : r->u.template_unification.strict,
4165 : : r->u.template_unification.flags,
4166 : : NULL, true, false);
4167 : 3479 : }
4168 : 3479 : break;
4169 : 2 : case rr_invalid_copy:
4170 : 2 : inform (cloc,
4171 : : "a constructor taking a single argument of its own "
4172 : : "class type is invalid");
4173 : 2 : break;
4174 : 96 : case rr_constraint_failure:
4175 : 96 : {
4176 : 96 : auto_diagnostic_nesting_level sentinel;
4177 : 96 : diagnose_constraints (cloc, fn, NULL_TREE);
4178 : 96 : }
4179 : 96 : break;
4180 : 28 : case rr_inherited_ctor:
4181 : 28 : inform (cloc, "an inherited constructor is not a candidate for "
4182 : : "initialization from an expression of the same or derived "
4183 : : "type");
4184 : 28 : break;
4185 : : case rr_ignored:
4186 : : break;
4187 : 0 : case rr_none:
4188 : 0 : default:
4189 : : /* This candidate didn't have any issues or we failed to
4190 : : handle a particular code. Either way... */
4191 : 0 : gcc_unreachable ();
4192 : : }
4193 : 10618 : }
4194 : 13675 : }
4195 : :
4196 : : /* Print information about each overload candidate in CANDIDATES,
4197 : : which is assumed to have gone through splice_viable and tourney
4198 : : (if splice_viable succeeded). */
4199 : :
4200 : : static void
4201 : 5372 : print_z_candidates (location_t loc, struct z_candidate *candidates,
4202 : : tristate only_viable_p /* = tristate::unknown () */)
4203 : : {
4204 : 5372 : struct z_candidate *cand1;
4205 : 5372 : struct z_candidate **cand2;
4206 : :
4207 : 5372 : if (!candidates)
4208 : 998 : return;
4209 : :
4210 : : /* Remove non-viable deleted candidates. */
4211 : 4374 : cand1 = candidates;
4212 : 20411 : for (cand2 = &cand1; *cand2; )
4213 : : {
4214 : 16037 : if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
4215 : 11547 : && !(*cand2)->viable
4216 : 19887 : && DECL_DELETED_FN ((*cand2)->fn))
4217 : 100 : *cand2 = (*cand2)->next;
4218 : : else
4219 : 15937 : cand2 = &(*cand2)->next;
4220 : : }
4221 : : /* ...if there are any non-deleted ones. */
4222 : 4374 : if (cand1)
4223 : 4368 : candidates = cand1;
4224 : :
4225 : : /* There may be duplicates in the set of candidates. We put off
4226 : : checking this condition as long as possible, since we have no way
4227 : : to eliminate duplicates from a set of functions in less than n^2
4228 : : time. Now we are about to emit an error message, so it is more
4229 : : permissible to go slowly. */
4230 : 20223 : for (cand1 = candidates; cand1; cand1 = cand1->next)
4231 : : {
4232 : 15849 : tree fn = cand1->fn;
4233 : : /* Skip builtin candidates and conversion functions. */
4234 : 15849 : if (!DECL_P (fn))
4235 : 282 : continue;
4236 : 15567 : cand2 = &cand1->next;
4237 : 167275 : while (*cand2)
4238 : : {
4239 : 151708 : if (DECL_P ((*cand2)->fn)
4240 : 151708 : && equal_functions (fn, (*cand2)->fn))
4241 : 94 : *cand2 = (*cand2)->next;
4242 : : else
4243 : 151614 : cand2 = &(*cand2)->next;
4244 : : }
4245 : : }
4246 : :
4247 : : /* Unless otherwise specified, if there's a (strictly) viable candidate
4248 : : then we assume we're being called as part of diagnosing ambiguity, in
4249 : : which case we want to print only viable candidates since non-viable
4250 : : candidates couldn't have contributed to the ambiguity. */
4251 : 4374 : if (only_viable_p.is_unknown ())
4252 : 4365 : only_viable_p = candidates->viable == 1;
4253 : :
4254 : 4374 : auto_diagnostic_nesting_level sentinel;
4255 : :
4256 : 4374 : int num_candidates = 0;
4257 : 20223 : for (auto iter = candidates; iter; iter = iter->next)
4258 : 15849 : ++num_candidates;
4259 : :
4260 : 4374 : inform_n (loc,
4261 : : num_candidates, "there is %i candidate", "there are %i candidates",
4262 : : num_candidates);
4263 : 4374 : auto_diagnostic_nesting_level sentinel2;
4264 : :
4265 : 4374 : int candidate_idx = 0;
4266 : 22284 : for (; candidates; candidates = candidates->next)
4267 : : {
4268 : 13903 : if (only_viable_p.is_true () && candidates->viable != 1)
4269 : : break;
4270 : 13594 : if (ignored_candidate_p (candidates) && !flag_diagnostics_all_candidates)
4271 : : {
4272 : 26 : inform (loc, "some candidates omitted; "
4273 : : "use %<-fdiagnostics-all-candidates%> to display them");
4274 : 26 : break;
4275 : : }
4276 : 13536 : pretty_printer pp;
4277 : 13536 : pp_printf (&pp, N_("candidate %i:"), candidate_idx + 1);
4278 : 13536 : const char *const msgstr = pp_formatted_text (&pp);
4279 : 13536 : print_z_candidate (loc, msgstr, candidates);
4280 : 13536 : ++candidate_idx;
4281 : 13536 : }
4282 : 4374 : }
4283 : :
4284 : : /* USER_SEQ is a user-defined conversion sequence, beginning with a
4285 : : USER_CONV. STD_SEQ is the standard conversion sequence applied to
4286 : : the result of the conversion function to convert it to the final
4287 : : desired type. Merge the two sequences into a single sequence,
4288 : : and return the merged sequence. */
4289 : :
4290 : : static conversion *
4291 : 9782795 : merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
4292 : : {
4293 : 9782795 : conversion **t;
4294 : 9782795 : bool bad = user_seq->bad_p;
4295 : :
4296 : 9782795 : gcc_assert (user_seq->kind == ck_user);
4297 : :
4298 : : /* Find the end of the second conversion sequence. */
4299 : 10362406 : for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
4300 : : {
4301 : : /* The entire sequence is a user-conversion sequence. */
4302 : 579611 : (*t)->user_conv_p = true;
4303 : 579611 : if (bad)
4304 : 2229 : (*t)->bad_p = true;
4305 : : }
4306 : :
4307 : 9782795 : if ((*t)->rvaluedness_matches_p)
4308 : : /* We're binding a reference directly to the result of the conversion.
4309 : : build_user_type_conversion_1 stripped the REFERENCE_TYPE from the return
4310 : : type, but we want it back. */
4311 : 331245 : user_seq->type = TREE_TYPE (TREE_TYPE (user_seq->cand->fn));
4312 : :
4313 : : /* Replace the identity conversion with the user conversion
4314 : : sequence. */
4315 : 9782795 : *t = user_seq;
4316 : :
4317 : 9782795 : return std_seq;
4318 : : }
4319 : :
4320 : : /* Handle overload resolution for initializing an object of class type from
4321 : : an initializer list. First we look for a suitable constructor that
4322 : : takes a std::initializer_list; if we don't find one, we then look for a
4323 : : non-list constructor.
4324 : :
4325 : : Parameters are as for add_candidates, except that the arguments are in
4326 : : the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
4327 : : the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
4328 : :
4329 : : static void
4330 : 1374540 : add_list_candidates (tree fns, tree first_arg,
4331 : : const vec<tree, va_gc> *args, tree totype,
4332 : : tree explicit_targs, bool template_only,
4333 : : tree conversion_path, tree access_path,
4334 : : int flags,
4335 : : struct z_candidate **candidates,
4336 : : tsubst_flags_t complain)
4337 : : {
4338 : 1374540 : gcc_assert (*candidates == NULL);
4339 : :
4340 : : /* We're looking for a ctor for list-initialization. */
4341 : 1374540 : flags |= LOOKUP_LIST_INIT_CTOR;
4342 : : /* And we don't allow narrowing conversions. We also use this flag to
4343 : : avoid the copy constructor call for copy-list-initialization. */
4344 : 1374540 : flags |= LOOKUP_NO_NARROWING;
4345 : :
4346 : 2749080 : unsigned nart = num_artificial_parms_for (OVL_FIRST (fns)) - 1;
4347 : 1374540 : tree init_list = (*args)[nart];
4348 : :
4349 : : /* Always use the default constructor if the list is empty (DR 990). */
4350 : 1374540 : if (CONSTRUCTOR_NELTS (init_list) == 0
4351 : 1374540 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
4352 : : ;
4353 : 1218465 : else if (CONSTRUCTOR_IS_DESIGNATED_INIT (init_list)
4354 : 1218465 : && !CP_AGGREGATE_TYPE_P (totype))
4355 : : {
4356 : 48 : if (complain & tf_error)
4357 : 12 : error ("designated initializers cannot be used with a "
4358 : : "non-aggregate type %qT", totype);
4359 : 2625 : return;
4360 : : }
4361 : : /* If the class has a list ctor, try passing the list as a single
4362 : : argument first, but only consider list ctors. */
4363 : 1218417 : else if (TYPE_HAS_LIST_CTOR (totype))
4364 : : {
4365 : 76831 : flags |= LOOKUP_LIST_ONLY;
4366 : 76831 : add_candidates (fns, first_arg, args, NULL_TREE,
4367 : : explicit_targs, template_only, conversion_path,
4368 : : access_path, flags, candidates, complain);
4369 : 153662 : if (any_strictly_viable (*candidates))
4370 : : return;
4371 : : }
4372 : :
4373 : : /* Expand the CONSTRUCTOR into a new argument vec. */
4374 : 1371915 : vec<tree, va_gc> *new_args;
4375 : 2587204 : vec_alloc (new_args, nart + CONSTRUCTOR_NELTS (init_list));
4376 : 1371918 : for (unsigned i = 0; i < nart; ++i)
4377 : 3 : new_args->quick_push ((*args)[i]);
4378 : 1371915 : new_args = append_ctor_to_tree_vector (new_args, init_list);
4379 : :
4380 : : /* We aren't looking for list-ctors anymore. */
4381 : 1371915 : flags &= ~LOOKUP_LIST_ONLY;
4382 : : /* We allow more user-defined conversions within an init-list. */
4383 : 1371915 : flags &= ~LOOKUP_NO_CONVERSION;
4384 : :
4385 : 1371915 : add_candidates (fns, first_arg, new_args, NULL_TREE,
4386 : : explicit_targs, template_only, conversion_path,
4387 : : access_path, flags, candidates, complain);
4388 : : }
4389 : :
4390 : : /* Given C(std::initializer_list<A>), return A. */
4391 : :
4392 : : static tree
4393 : 1383 : list_ctor_element_type (tree fn)
4394 : : {
4395 : 1383 : gcc_checking_assert (is_list_ctor (fn));
4396 : :
4397 : 1383 : tree parm = FUNCTION_FIRST_USER_PARMTYPE (fn);
4398 : 1383 : parm = non_reference (TREE_VALUE (parm));
4399 : 1383 : return TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
4400 : : }
4401 : :
4402 : : /* If EXPR is a braced-init-list where the elements all decay to the same type,
4403 : : return that type. */
4404 : :
4405 : : static tree
4406 : 1273 : braced_init_element_type (tree expr)
4407 : : {
4408 : 1273 : if (TREE_CODE (expr) == CONSTRUCTOR
4409 : 1273 : && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
4410 : 0 : return TREE_TYPE (TREE_TYPE (expr));
4411 : 1273 : if (!BRACE_ENCLOSED_INITIALIZER_P (expr))
4412 : : return NULL_TREE;
4413 : :
4414 : 1273 : tree elttype = NULL_TREE;
4415 : 13713 : for (constructor_elt &e: CONSTRUCTOR_ELTS (expr))
4416 : : {
4417 : 10070 : tree type = TREE_TYPE (e.value);
4418 : 10070 : type = type_decays_to (type);
4419 : 10070 : if (!elttype)
4420 : : elttype = type;
4421 : 8816 : else if (!same_type_p (type, elttype))
4422 : : return NULL_TREE;
4423 : : }
4424 : : return elttype;
4425 : : }
4426 : :
4427 : : /* True iff EXPR contains any temporaries with non-trivial destruction.
4428 : :
4429 : : ??? Also ignore classes with non-trivial but no-op destruction other than
4430 : : std::allocator? */
4431 : :
4432 : : static bool
4433 : 196 : has_non_trivial_temporaries (tree expr)
4434 : : {
4435 : 196 : auto_vec<tree*> temps;
4436 : 196 : cp_walk_tree_without_duplicates (&expr, find_temps_r, &temps);
4437 : 400 : for (tree *p : temps)
4438 : : {
4439 : 68 : tree t = TREE_TYPE (*p);
4440 : 68 : if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (t)
4441 : 68 : && !is_std_allocator (t))
4442 : : return true;
4443 : : }
4444 : : return false;
4445 : 196 : }
4446 : :
4447 : : /* Return number of initialized elements in CTOR. */
4448 : :
4449 : : unsigned HOST_WIDE_INT
4450 : 278 : count_ctor_elements (tree ctor)
4451 : : {
4452 : 278 : unsigned HOST_WIDE_INT len = 0;
4453 : 2223 : for (constructor_elt &e: CONSTRUCTOR_ELTS (ctor))
4454 : 1389 : if (TREE_CODE (e.value) == RAW_DATA_CST)
4455 : 9 : len += RAW_DATA_LENGTH (e.value);
4456 : : else
4457 : 1380 : ++len;
4458 : 278 : return len;
4459 : : }
4460 : :
4461 : : /* We're initializing an array of ELTTYPE from INIT. If it seems useful,
4462 : : return INIT as an array (of its own type) so the caller can initialize the
4463 : : target array in a loop. */
4464 : :
4465 : : static tree
4466 : 5062 : maybe_init_list_as_array (tree elttype, tree init)
4467 : : {
4468 : : /* Only do this if the array can go in rodata but not once converted. */
4469 : 5062 : if (!TYPE_NON_AGGREGATE_CLASS (elttype))
4470 : : return NULL_TREE;
4471 : 1273 : tree init_elttype = braced_init_element_type (init);
4472 : 1273 : if (!init_elttype || !SCALAR_TYPE_P (init_elttype) || !TREE_CONSTANT (init))
4473 : : return NULL_TREE;
4474 : :
4475 : : /* Check with a stub expression to weed out special cases, and check whether
4476 : : we call the same function for direct-init as copy-list-init. */
4477 : 356 : conversion_obstack_sentinel cos;
4478 : 356 : init_elttype = cp_build_qualified_type (init_elttype, TYPE_QUAL_CONST);
4479 : 356 : tree arg = build_stub_object (init_elttype);
4480 : 356 : conversion *c = implicit_conversion (elttype, init_elttype, arg, false,
4481 : : LOOKUP_NORMAL, tf_none);
4482 : 356 : if (c && c->kind == ck_rvalue)
4483 : 0 : c = next_conversion (c);
4484 : 350 : if (!c || c->kind != ck_user)
4485 : : return NULL_TREE;
4486 : : /* Check that we actually can perform the conversion. */
4487 : 347 : if (convert_like (c, arg, tf_none) == error_mark_node)
4488 : : /* Let the normal code give the error. */
4489 : : return NULL_TREE;
4490 : :
4491 : : /* A glvalue initializer might be significant to a reference constructor
4492 : : or conversion operator. */
4493 : 341 : if (!DECL_CONSTRUCTOR_P (c->cand->fn)
4494 : 341 : || (TYPE_REF_P (TREE_VALUE
4495 : : (FUNCTION_FIRST_USER_PARMTYPE (c->cand->fn)))))
4496 : 240 : for (auto &ce : CONSTRUCTOR_ELTS (init))
4497 : 108 : if (non_mergeable_glvalue_p (ce.value))
4498 : : return NULL_TREE;
4499 : :
4500 : 338 : tree first = CONSTRUCTOR_ELT (init, 0)->value;
4501 : 338 : conversion *fc = implicit_conversion (elttype, init_elttype, first, false,
4502 : : LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING,
4503 : : tf_none);
4504 : 338 : if (fc && fc->kind == ck_rvalue)
4505 : 48 : fc = next_conversion (fc);
4506 : 338 : if (!fc || fc->kind != ck_user || fc->cand->fn != c->cand->fn)
4507 : : return NULL_TREE;
4508 : 296 : first = convert_like (fc, first, tf_none);
4509 : 296 : if (first == error_mark_node)
4510 : : /* Let the normal code give the error. */
4511 : : return NULL_TREE;
4512 : :
4513 : : /* Don't do this if the conversion would be constant. */
4514 : 293 : first = maybe_constant_init (first);
4515 : 293 : if (TREE_CONSTANT (first))
4516 : : return NULL_TREE;
4517 : :
4518 : : /* We can't do this if the conversion creates temporaries that need
4519 : : to live until the whole array is initialized. */
4520 : 196 : if (has_non_trivial_temporaries (first))
4521 : : return NULL_TREE;
4522 : :
4523 : : /* We can't do this if copying from the initializer_list would be
4524 : : ill-formed. */
4525 : 196 : tree copy_argtypes = make_tree_vec (1);
4526 : 196 : TREE_VEC_ELT (copy_argtypes, 0)
4527 : 196 : = cp_build_qualified_type (elttype, TYPE_QUAL_CONST);
4528 : 196 : if (!is_xible (INIT_EXPR, elttype, copy_argtypes))
4529 : : return NULL_TREE;
4530 : :
4531 : 190 : unsigned HOST_WIDE_INT len = count_ctor_elements (init);
4532 : 190 : tree arr = build_array_of_n_type (init_elttype, len);
4533 : 190 : arr = finish_compound_literal (arr, init, tf_none);
4534 : 190 : DECL_MERGEABLE (TARGET_EXPR_SLOT (arr)) = true;
4535 : 190 : return arr;
4536 : 356 : }
4537 : :
4538 : : /* If we were going to call e.g. vector(initializer_list<string>) starting
4539 : : with a list of string-literals (which is inefficient, see PR105838),
4540 : : instead build an array of const char* and pass it to the range constructor.
4541 : : But only do this for standard library types, where we can assume the
4542 : : transformation makes sense.
4543 : :
4544 : : Really the container classes should have initializer_list<U> constructors to
4545 : : get the same effect more simply; this is working around that lack. */
4546 : :
4547 : : static tree
4548 : 9462257 : maybe_init_list_as_range (tree fn, tree expr)
4549 : : {
4550 : 9462257 : if (!processing_template_decl
4551 : 9215406 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
4552 : 648286 : && is_list_ctor (fn)
4553 : 9463791 : && decl_in_std_namespace_p (fn))
4554 : : {
4555 : 1383 : tree to = list_ctor_element_type (fn);
4556 : 1383 : if (tree init = maybe_init_list_as_array (to, expr))
4557 : : {
4558 : 58 : tree begin = decay_conversion (TARGET_EXPR_SLOT (init), tf_none);
4559 : 58 : tree nelts = array_type_nelts_top (TREE_TYPE (init));
4560 : 58 : tree end = cp_build_binary_op (input_location, PLUS_EXPR, begin,
4561 : : nelts, tf_none);
4562 : 58 : begin = cp_build_compound_expr (init, begin, tf_none);
4563 : 58 : return build_constructor_va (init_list_type_node, 2,
4564 : 58 : NULL_TREE, begin, NULL_TREE, end);
4565 : : }
4566 : : }
4567 : :
4568 : : return NULL_TREE;
4569 : : }
4570 : :
4571 : : /* Returns the best overload candidate to perform the requested
4572 : : conversion. This function is used for three the overloading situations
4573 : : described in [over.match.copy], [over.match.conv], and [over.match.ref].
4574 : : If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
4575 : : per [dcl.init.ref], so we ignore temporary bindings. */
4576 : :
4577 : : static struct z_candidate *
4578 : 58959075 : build_user_type_conversion_1 (tree totype, tree expr, int flags,
4579 : : tsubst_flags_t complain)
4580 : : {
4581 : 58959075 : struct z_candidate *candidates, *cand;
4582 : 58959075 : tree fromtype;
4583 : 58959075 : tree ctors = NULL_TREE;
4584 : 58959075 : tree conv_fns = NULL_TREE;
4585 : 58959075 : conversion *conv = NULL;
4586 : 58959075 : tree first_arg = NULL_TREE;
4587 : 58959075 : vec<tree, va_gc> *args = NULL;
4588 : 58959075 : bool any_viable_p;
4589 : 58959075 : int convflags;
4590 : :
4591 : 58959075 : if (!expr)
4592 : : return NULL;
4593 : :
4594 : 58959069 : fromtype = TREE_TYPE (expr);
4595 : :
4596 : : /* We represent conversion within a hierarchy using RVALUE_CONV and
4597 : : BASE_CONV, as specified by [over.best.ics]; these become plain
4598 : : constructor calls, as specified in [dcl.init]. */
4599 : 58959069 : gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
4600 : : || !DERIVED_FROM_P (totype, fromtype));
4601 : :
4602 : 58959069 : if (CLASS_TYPE_P (totype))
4603 : : /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
4604 : : creating a garbage BASELINK; constructors can't be inherited. */
4605 : 34884751 : ctors = get_class_binding (totype, complete_ctor_identifier);
4606 : :
4607 : 58959069 : tree to_nonref = non_reference (totype);
4608 : 58959069 : if (MAYBE_CLASS_TYPE_P (fromtype))
4609 : : {
4610 : 37712655 : if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
4611 : 37683017 : (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
4612 : 29674525 : && DERIVED_FROM_P (to_nonref, fromtype)))
4613 : : {
4614 : : /* [class.conv.fct] A conversion function is never used to
4615 : : convert a (possibly cv-qualified) object to the (possibly
4616 : : cv-qualified) same object type (or a reference to it), to a
4617 : : (possibly cv-qualified) base class of that type (or a
4618 : : reference to it)... */
4619 : : }
4620 : : else
4621 : 37683014 : conv_fns = lookup_conversions (fromtype);
4622 : : }
4623 : :
4624 : 58959069 : candidates = 0;
4625 : 58959069 : flags |= LOOKUP_NO_CONVERSION;
4626 : 58959069 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4627 : 664878 : flags |= LOOKUP_NO_NARROWING;
4628 : : /* Prevent add_candidates from treating a non-strictly viable candidate
4629 : : as unviable. */
4630 : 58959069 : complain |= tf_conv;
4631 : :
4632 : : /* It's OK to bind a temporary for converting constructor arguments, but
4633 : : not in converting the return value of a conversion operator. */
4634 : 58959069 : convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION
4635 : 58959069 : | (flags & LOOKUP_NO_NARROWING));
4636 : 58959069 : flags &= ~LOOKUP_NO_TEMP_BIND;
4637 : :
4638 : 58959069 : if (ctors)
4639 : : {
4640 : 34732835 : int ctorflags = flags;
4641 : :
4642 : 34732835 : first_arg = build_dummy_object (totype);
4643 : :
4644 : : /* We should never try to call the abstract or base constructor
4645 : : from here. */
4646 : 244084337 : gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_FIRST (ctors))
4647 : : && !DECL_HAS_VTT_PARM_P (OVL_FIRST (ctors)));
4648 : :
4649 : 34732835 : args = make_tree_vector_single (expr);
4650 : 34732835 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4651 : : {
4652 : : /* List-initialization. */
4653 : 664878 : add_list_candidates (ctors, first_arg, args, totype, NULL_TREE,
4654 : 664878 : false, TYPE_BINFO (totype), TYPE_BINFO (totype),
4655 : : ctorflags, &candidates, complain);
4656 : : }
4657 : : else
4658 : : {
4659 : 34067957 : add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
4660 : 34067957 : TYPE_BINFO (totype), TYPE_BINFO (totype),
4661 : : ctorflags, &candidates, complain);
4662 : : }
4663 : :
4664 : 200336716 : for (cand = candidates; cand; cand = cand->next)
4665 : : {
4666 : 165603881 : cand->second_conv = build_identity_conv (totype, NULL_TREE);
4667 : :
4668 : : /* If totype isn't a reference, and LOOKUP_ONLYCONVERTING is
4669 : : set, then this is copy-initialization. In that case, "The
4670 : : result of the call is then used to direct-initialize the
4671 : : object that is the destination of the copy-initialization."
4672 : : [dcl.init]
4673 : :
4674 : : We represent this in the conversion sequence with an
4675 : : rvalue conversion, which means a constructor call. */
4676 : 165603881 : if (!TYPE_REF_P (totype)
4677 : 165603881 : && cxx_dialect < cxx17
4678 : 421437 : && (flags & LOOKUP_ONLYCONVERTING)
4679 : 365205 : && !(convflags & LOOKUP_NO_TEMP_BIND))
4680 : 114509 : cand->second_conv
4681 : 114509 : = build_conv (ck_rvalue, totype, cand->second_conv);
4682 : : }
4683 : : }
4684 : :
4685 : 58959069 : if (conv_fns)
4686 : : {
4687 : 13899033 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4688 : 0 : first_arg = CONSTRUCTOR_ELT (expr, 0)->value;
4689 : : else
4690 : 58959069 : first_arg = expr;
4691 : : }
4692 : :
4693 : 74295000 : for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
4694 : : {
4695 : 15335931 : tree conversion_path = TREE_PURPOSE (conv_fns);
4696 : 15335931 : struct z_candidate *old_candidates;
4697 : :
4698 : : /* If LOOKUP_NO_CONVERSION, don't consider a conversion function that
4699 : : would need an addional user-defined conversion, i.e. if the return
4700 : : type differs in class-ness from the desired type. So we avoid
4701 : : considering operator bool when calling a copy constructor.
4702 : :
4703 : : This optimization avoids the failure in PR97600, and is allowed by
4704 : : [temp.inst]/9: "If the function selected by overload resolution can be
4705 : : determined without instantiating a class template definition, it is
4706 : : unspecified whether that instantiation actually takes place." */
4707 : 15335931 : tree convtype = non_reference (TREE_TYPE (conv_fns));
4708 : 21001049 : if ((flags & LOOKUP_NO_CONVERSION)
4709 : 15335931 : && !WILDCARD_TYPE_P (convtype)
4710 : 29810364 : && (CLASS_TYPE_P (to_nonref)
4711 : 14905182 : != CLASS_TYPE_P (convtype)))
4712 : 5665118 : continue;
4713 : :
4714 : : /* If we are called to convert to a reference type, we are trying to
4715 : : find a direct binding, so don't even consider temporaries. If
4716 : : we don't find a direct binding, the caller will try again to
4717 : : look for a temporary binding. */
4718 : 9670813 : if (TYPE_REF_P (totype))
4719 : 2401014 : convflags |= LOOKUP_NO_TEMP_BIND;
4720 : :
4721 : 9670813 : old_candidates = candidates;
4722 : 9670813 : add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
4723 : : NULL_TREE, false,
4724 : 9670813 : conversion_path, TYPE_BINFO (fromtype),
4725 : : flags, &candidates, complain);
4726 : :
4727 : 19341621 : for (cand = candidates; cand != old_candidates; cand = cand->next)
4728 : : {
4729 : 9670808 : if (cand->viable == 0)
4730 : : /* Already rejected, don't change to -1. */
4731 : 2195044 : continue;
4732 : :
4733 : 7475764 : tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
4734 : 7475764 : conversion *ics
4735 : 7475764 : = implicit_conversion (totype,
4736 : : rettype,
4737 : : 0,
4738 : : /*c_cast_p=*/false, convflags,
4739 : : complain);
4740 : :
4741 : : /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
4742 : : copy-initialization. In that case, "The result of the
4743 : : call is then used to direct-initialize the object that is
4744 : : the destination of the copy-initialization." [dcl.init]
4745 : :
4746 : : We represent this in the conversion sequence with an
4747 : : rvalue conversion, which means a constructor call. But
4748 : : don't add a second rvalue conversion if there's already
4749 : : one there. Which there really shouldn't be, but it's
4750 : : harmless since we'd add it here anyway. */
4751 : 3320390 : if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
4752 : 8017674 : && !(convflags & LOOKUP_NO_TEMP_BIND))
4753 : 221733 : ics = build_conv (ck_rvalue, totype, ics);
4754 : :
4755 : 7475764 : cand->second_conv = ics;
4756 : :
4757 : 7475764 : if (!ics)
4758 : : {
4759 : 4155374 : cand->viable = 0;
4760 : 8309233 : cand->reason = arg_conversion_rejection (NULL_TREE, -2,
4761 : : rettype, totype,
4762 : 4155374 : EXPR_LOCATION (expr));
4763 : : }
4764 : 11314 : else if (TYPE_REF_P (totype) && !ics->rvaluedness_matches_p
4765 : : /* Limit this to non-templates for now (PR90546). */
4766 : 222 : && !cand->template_decl
4767 : 3320607 : && TREE_CODE (TREE_TYPE (totype)) != FUNCTION_TYPE)
4768 : : {
4769 : : /* If we are called to convert to a reference type, we are trying
4770 : : to find a direct binding per [over.match.ref], so rvaluedness
4771 : : must match for non-functions. */
4772 : 211 : cand->viable = 0;
4773 : : }
4774 : 3320179 : else if (DECL_NONCONVERTING_P (cand->fn)
4775 : 3320179 : && ics->rank > cr_exact)
4776 : : {
4777 : : /* 13.3.1.5: For direct-initialization, those explicit
4778 : : conversion functions that are not hidden within S and
4779 : : yield type T or a type that can be converted to type T
4780 : : with a qualification conversion (4.4) are also candidate
4781 : : functions. */
4782 : : /* 13.3.1.6 doesn't have a parallel restriction, but it should;
4783 : : I've raised this issue with the committee. --jason 9/2011 */
4784 : 2457 : cand->viable = -1;
4785 : 2457 : cand->reason = explicit_conversion_rejection (rettype, totype);
4786 : : }
4787 : 3317722 : else if (cand->viable == 1 && ics->bad_p)
4788 : : {
4789 : 1418 : cand->viable = -1;
4790 : 1418 : cand->reason
4791 : 2836 : = bad_arg_conversion_rejection (NULL_TREE, -2,
4792 : : rettype, totype,
4793 : 1418 : EXPR_LOCATION (expr));
4794 : : }
4795 : 3316304 : else if (primary_template_specialization_p (cand->fn)
4796 : 3316304 : && ics->rank > cr_exact)
4797 : : {
4798 : : /* 13.3.3.1.2: If the user-defined conversion is specified by
4799 : : a specialization of a conversion function template, the
4800 : : second standard conversion sequence shall have exact match
4801 : : rank. */
4802 : 21 : cand->viable = -1;
4803 : 21 : cand->reason = template_conversion_rejection (rettype, totype);
4804 : : }
4805 : : }
4806 : : }
4807 : :
4808 : 58959069 : candidates = splice_viable (candidates, false, &any_viable_p);
4809 : 58959069 : if (!any_viable_p)
4810 : : {
4811 : 49495477 : if (args)
4812 : 28035618 : release_tree_vector (args);
4813 : 49495477 : return NULL;
4814 : : }
4815 : :
4816 : 9463592 : cand = tourney (candidates, complain);
4817 : 9463592 : if (cand == NULL)
4818 : : {
4819 : 1335 : if (complain & tf_error)
4820 : : {
4821 : 71 : auto_diagnostic_group d;
4822 : 74 : error_at (cp_expr_loc_or_input_loc (expr),
4823 : : "conversion from %qH to %qI is ambiguous",
4824 : : fromtype, totype);
4825 : 71 : print_z_candidates (location_of (expr), candidates);
4826 : 71 : }
4827 : :
4828 : 1335 : cand = candidates; /* any one will do */
4829 : 1335 : cand->second_conv = build_ambiguous_conv (totype, expr);
4830 : 1335 : cand->second_conv->user_conv_p = true;
4831 : 2670 : if (!any_strictly_viable (candidates))
4832 : 12 : cand->second_conv->bad_p = true;
4833 : 1335 : if (flags & LOOKUP_ONLYCONVERTING)
4834 : 1265 : cand->second_conv->need_temporary_p = true;
4835 : : /* If there are viable candidates, don't set ICS_BAD_FLAG; an
4836 : : ambiguous conversion is no worse than another user-defined
4837 : : conversion. */
4838 : :
4839 : 1335 : return cand;
4840 : : }
4841 : :
4842 : : /* Maybe pass { } as iterators instead of an initializer_list. */
4843 : 9462257 : if (tree iters = maybe_init_list_as_range (cand->fn, expr))
4844 : 116 : if (z_candidate *cand2
4845 : 58 : = build_user_type_conversion_1 (totype, iters, flags, tf_none))
4846 : 55 : if (cand2->viable == 1 && !is_list_ctor (cand2->fn))
4847 : : {
4848 : : cand = cand2;
4849 : : expr = iters;
4850 : : }
4851 : :
4852 : 9462257 : tree convtype;
4853 : 18924514 : if (!DECL_CONSTRUCTOR_P (cand->fn))
4854 : 3308099 : convtype = non_reference (TREE_TYPE (TREE_TYPE (cand->fn)));
4855 : 6154158 : else if (cand->second_conv->kind == ck_rvalue)
4856 : : /* DR 5: [in the first step of copy-initialization]...if the function
4857 : : is a constructor, the call initializes a temporary of the
4858 : : cv-unqualified version of the destination type. */
4859 : 11729 : convtype = cv_unqualified (totype);
4860 : : else
4861 : : convtype = totype;
4862 : : /* Build the user conversion sequence. */
4863 : 9462257 : conv = build_conv
4864 : 9462257 : (ck_user,
4865 : : convtype,
4866 : 9462257 : build_identity_conv (TREE_TYPE (expr), expr));
4867 : 9462257 : conv->cand = cand;
4868 : 9462257 : if (cand->viable == -1)
4869 : 20992 : conv->bad_p = true;
4870 : :
4871 : : /* Remember that this was a list-initialization. */
4872 : 9462257 : if (flags & LOOKUP_NO_NARROWING)
4873 : 1648051 : conv->check_narrowing = true;
4874 : :
4875 : : /* Combine it with the second conversion sequence. */
4876 : 9462257 : cand->second_conv = merge_conversion_sequences (conv,
4877 : : cand->second_conv);
4878 : :
4879 : 9462257 : return cand;
4880 : : }
4881 : :
4882 : : /* Wrapper for above. */
4883 : :
4884 : : tree
4885 : 22622 : build_user_type_conversion (tree totype, tree expr, int flags,
4886 : : tsubst_flags_t complain)
4887 : : {
4888 : 22622 : struct z_candidate *cand;
4889 : 22622 : tree ret;
4890 : :
4891 : 22622 : auto_cond_timevar tv (TV_OVERLOAD);
4892 : :
4893 : 22622 : conversion_obstack_sentinel cos;
4894 : :
4895 : 22622 : cand = build_user_type_conversion_1 (totype, expr, flags, complain);
4896 : :
4897 : 22622 : if (cand)
4898 : : {
4899 : 22437 : if (cand->second_conv->kind == ck_ambig)
4900 : 65 : ret = error_mark_node;
4901 : : else
4902 : : {
4903 : 22372 : expr = convert_like (cand->second_conv, expr, complain);
4904 : 22372 : ret = convert_from_reference (expr);
4905 : : }
4906 : : }
4907 : : else
4908 : : ret = NULL_TREE;
4909 : :
4910 : 45244 : return ret;
4911 : 22622 : }
4912 : :
4913 : : /* Give a helpful diagnostic when implicit_conversion fails. */
4914 : :
4915 : : static void
4916 : 654 : implicit_conversion_error (location_t loc, tree type, tree expr)
4917 : : {
4918 : 654 : tsubst_flags_t complain = tf_warning_or_error;
4919 : :
4920 : : /* If expr has unknown type, then it is an overloaded function.
4921 : : Call instantiate_type to get good error messages. */
4922 : 654 : if (TREE_TYPE (expr) == unknown_type_node)
4923 : 66 : instantiate_type (type, expr, complain);
4924 : 588 : else if (invalid_nonstatic_memfn_p (loc, expr, complain))
4925 : : /* We gave an error. */;
4926 : 62 : else if (BRACE_ENCLOSED_INITIALIZER_P (expr)
4927 : 59 : && CONSTRUCTOR_IS_DESIGNATED_INIT (expr)
4928 : 601 : && !CP_AGGREGATE_TYPE_P (type))
4929 : 18 : error_at (loc, "designated initializers cannot be used with a "
4930 : : "non-aggregate type %qT", type);
4931 : 565 : else if (is_stub_object (expr))
4932 : : /* The expression is generated by a trait check, we don't have
4933 : : a useful location to highlight the label. */
4934 : 13 : error_at (loc, "could not convert %qH to %qI",
4935 : 13 : TREE_TYPE (expr), type);
4936 : : else
4937 : : {
4938 : 552 : range_label_for_type_mismatch label (TREE_TYPE (expr), type);
4939 : 552 : gcc_rich_location rich_loc (loc, &label,
4940 : 552 : highlight_colors::percent_h);
4941 : 552 : error_at (&rich_loc, "could not convert %qE from %qH to %qI",
4942 : 552 : expr, TREE_TYPE (expr), type);
4943 : 552 : }
4944 : 654 : }
4945 : :
4946 : : /* Worker for build_converted_constant_expr. */
4947 : :
4948 : : static tree
4949 : 258900214 : build_converted_constant_expr_internal (tree type, tree expr,
4950 : : int flags, tsubst_flags_t complain)
4951 : : {
4952 : 258900214 : conversion *conv;
4953 : 258900214 : tree t;
4954 : 258900214 : location_t loc = cp_expr_loc_or_input_loc (expr);
4955 : :
4956 : 258900214 : if (error_operand_p (expr))
4957 : 49 : return error_mark_node;
4958 : :
4959 : 258900165 : conversion_obstack_sentinel cos;
4960 : :
4961 : 258900165 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
4962 : : /*c_cast_p=*/false, flags, complain);
4963 : :
4964 : : /* A converted constant expression of type T is an expression, implicitly
4965 : : converted to type T, where the converted expression is a constant
4966 : : expression and the implicit conversion sequence contains only
4967 : :
4968 : : * user-defined conversions,
4969 : : * lvalue-to-rvalue conversions (7.1),
4970 : : * array-to-pointer conversions (7.2),
4971 : : * function-to-pointer conversions (7.3),
4972 : : * qualification conversions (7.5),
4973 : : * integral promotions (7.6),
4974 : : * integral conversions (7.8) other than narrowing conversions (11.6.4),
4975 : : * null pointer conversions (7.11) from std::nullptr_t,
4976 : : * null member pointer conversions (7.12) from std::nullptr_t, and
4977 : : * function pointer conversions (7.13),
4978 : :
4979 : : and where the reference binding (if any) binds directly. */
4980 : :
4981 : 258900165 : for (conversion *c = conv;
4982 : 294255979 : c && c->kind != ck_identity;
4983 : 35355814 : c = next_conversion (c))
4984 : : {
4985 : 35355814 : switch (c->kind)
4986 : : {
4987 : : /* A conversion function is OK. If it isn't constexpr, we'll
4988 : : complain later that the argument isn't constant. */
4989 : : case ck_user:
4990 : : /* List-initialization is OK. */
4991 : : case ck_aggr:
4992 : : /* The lvalue-to-rvalue conversion is OK. */
4993 : : case ck_rvalue:
4994 : : /* Array-to-pointer and function-to-pointer. */
4995 : : case ck_lvalue:
4996 : : /* Function pointer conversions. */
4997 : : case ck_fnptr:
4998 : : /* Qualification conversions. */
4999 : : case ck_qual:
5000 : : break;
5001 : :
5002 : 361 : case ck_ref_bind:
5003 : 361 : if (c->need_temporary_p)
5004 : : {
5005 : 0 : if (complain & tf_error)
5006 : 0 : error_at (loc, "initializing %qH with %qI in converted "
5007 : : "constant expression does not bind directly",
5008 : 0 : type, next_conversion (c)->type);
5009 : : conv = NULL;
5010 : : }
5011 : : break;
5012 : :
5013 : 7136636 : case ck_base:
5014 : 7136636 : case ck_pmem:
5015 : 7136636 : case ck_ptr:
5016 : 7136636 : case ck_std:
5017 : 7136636 : t = next_conversion (c)->type;
5018 : 7136636 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (t)
5019 : 7136566 : && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5020 : : /* Integral promotion or conversion. */
5021 : : break;
5022 : 92 : if (NULLPTR_TYPE_P (t))
5023 : : /* Conversion from nullptr to pointer or pointer-to-member. */
5024 : : break;
5025 : :
5026 : 92 : if (complain & tf_error)
5027 : 72 : error_at (loc, "conversion from %qH to %qI in a "
5028 : : "converted constant expression", t, type);
5029 : : /* fall through. */
5030 : :
5031 : : default:
5032 : : conv = NULL;
5033 : : break;
5034 : : }
5035 : : }
5036 : :
5037 : : /* Avoid confusing convert_nontype_argument by introducing
5038 : : a redundant conversion to the same reference type. */
5039 : 258899929 : if (conv && conv->kind == ck_ref_bind
5040 : 258900526 : && REFERENCE_REF_P (expr))
5041 : : {
5042 : 137 : tree ref = TREE_OPERAND (expr, 0);
5043 : 137 : if (same_type_p (type, TREE_TYPE (ref)))
5044 : : return ref;
5045 : : }
5046 : :
5047 : 258900050 : if (conv)
5048 : : {
5049 : : /* Don't copy a class in a template. */
5050 : 4876 : if (CLASS_TYPE_P (type) && conv->kind == ck_rvalue
5051 : 258904328 : && processing_template_decl)
5052 : 51 : conv = next_conversion (conv);
5053 : :
5054 : : /* Issuing conversion warnings for value-dependent expressions is
5055 : : likely too noisy. */
5056 : 258899814 : warning_sentinel w (warn_conversion);
5057 : 258899814 : conv->check_narrowing = true;
5058 : 258899814 : conv->check_narrowing_const_only = true;
5059 : 258899814 : expr = convert_like (conv, expr, complain);
5060 : 258899814 : }
5061 : : else
5062 : : {
5063 : 236 : if (complain & tf_error)
5064 : 184 : implicit_conversion_error (loc, type, expr);
5065 : 236 : expr = error_mark_node;
5066 : : }
5067 : :
5068 : : return expr;
5069 : 258900165 : }
5070 : :
5071 : : /* Subroutine of convert_nontype_argument.
5072 : :
5073 : : EXPR is an expression used in a context that requires a converted
5074 : : constant-expression, such as a template non-type parameter. Do any
5075 : : necessary conversions (that are permitted for converted
5076 : : constant-expressions) to convert it to the desired type.
5077 : :
5078 : : This function doesn't consider explicit conversion functions. If
5079 : : you mean to use "a contextually converted constant expression of type
5080 : : bool", use build_converted_constant_bool_expr.
5081 : :
5082 : : If conversion is successful, returns the converted expression;
5083 : : otherwise, returns error_mark_node. */
5084 : :
5085 : : tree
5086 : 145884920 : build_converted_constant_expr (tree type, tree expr, tsubst_flags_t complain)
5087 : : {
5088 : 145884920 : return build_converted_constant_expr_internal (type, expr, LOOKUP_IMPLICIT,
5089 : 145884920 : complain);
5090 : : }
5091 : :
5092 : : /* Used to create "a contextually converted constant expression of type
5093 : : bool". This differs from build_converted_constant_expr in that it
5094 : : also considers explicit conversion functions. */
5095 : :
5096 : : tree
5097 : 113015294 : build_converted_constant_bool_expr (tree expr, tsubst_flags_t complain)
5098 : : {
5099 : 113015294 : return build_converted_constant_expr_internal (boolean_type_node, expr,
5100 : 113015294 : LOOKUP_NORMAL, complain);
5101 : : }
5102 : :
5103 : : /* Do any initial processing on the arguments to a function call. */
5104 : :
5105 : : vec<tree, va_gc> *
5106 : 160616895 : resolve_args (vec<tree, va_gc> *args, tsubst_flags_t complain)
5107 : : {
5108 : 160616895 : unsigned int ix;
5109 : 160616895 : tree arg;
5110 : :
5111 : 296363921 : FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
5112 : : {
5113 : 135747592 : if (error_operand_p (arg))
5114 : : return NULL;
5115 : 135747126 : else if (VOID_TYPE_P (TREE_TYPE (arg)))
5116 : : {
5117 : 73 : if (complain & tf_error)
5118 : 12 : error_at (cp_expr_loc_or_input_loc (arg),
5119 : : "invalid use of void expression");
5120 : 73 : return NULL;
5121 : : }
5122 : 135747053 : else if (invalid_nonstatic_memfn_p (EXPR_LOCATION (arg), arg, complain))
5123 : : return NULL;
5124 : :
5125 : : /* Force auto deduction now. Omit tf_warning to avoid redundant
5126 : : deprecated warning on deprecated-14.C. */
5127 : 135747026 : if (!mark_single_function (arg, complain & ~tf_warning))
5128 : : return NULL;
5129 : : }
5130 : : return args;
5131 : : }
5132 : :
5133 : : /* Perform overload resolution on FN, which is called with the ARGS.
5134 : :
5135 : : Return the candidate function selected by overload resolution, or
5136 : : NULL if the event that overload resolution failed. In the case
5137 : : that overload resolution fails, *CANDIDATES will be the set of
5138 : : candidates considered, and ANY_VIABLE_P will be set to true or
5139 : : false to indicate whether or not any of the candidates were
5140 : : viable.
5141 : :
5142 : : The ARGS should already have gone through RESOLVE_ARGS before this
5143 : : function is called. */
5144 : :
5145 : : static struct z_candidate *
5146 : 75961213 : perform_overload_resolution (tree fn,
5147 : : const vec<tree, va_gc> *args,
5148 : : struct z_candidate **candidates,
5149 : : bool *any_viable_p, tsubst_flags_t complain)
5150 : : {
5151 : 75961213 : struct z_candidate *cand;
5152 : 75961213 : tree explicit_targs;
5153 : 75961213 : int template_only;
5154 : :
5155 : 75961213 : auto_cond_timevar tv (TV_OVERLOAD);
5156 : :
5157 : 75961213 : explicit_targs = NULL_TREE;
5158 : 75961213 : template_only = 0;
5159 : :
5160 : 75961213 : *candidates = NULL;
5161 : 75961213 : *any_viable_p = true;
5162 : :
5163 : : /* Check FN. */
5164 : 75961213 : gcc_assert (OVL_P (fn) || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
5165 : :
5166 : 75961213 : if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
5167 : : {
5168 : 20812077 : explicit_targs = TREE_OPERAND (fn, 1);
5169 : 20812077 : fn = TREE_OPERAND (fn, 0);
5170 : 20812077 : template_only = 1;
5171 : : }
5172 : :
5173 : : /* Add the various candidate functions. */
5174 : 75961213 : add_candidates (fn, NULL_TREE, args, NULL_TREE,
5175 : : explicit_targs, template_only,
5176 : : /*conversion_path=*/NULL_TREE,
5177 : : /*access_path=*/NULL_TREE,
5178 : : LOOKUP_NORMAL,
5179 : : candidates, complain);
5180 : :
5181 : 75947695 : *candidates = splice_viable (*candidates, false, any_viable_p);
5182 : 75947695 : if (*any_viable_p)
5183 : 75868687 : cand = tourney (*candidates, complain);
5184 : : else
5185 : : cand = NULL;
5186 : :
5187 : 151895390 : return cand;
5188 : 75947695 : }
5189 : :
5190 : : /* Print an error message about being unable to build a call to FN with
5191 : : ARGS. ANY_VIABLE_P indicates whether any candidate functions could
5192 : : be located; CANDIDATES is a possibly empty list of such
5193 : : functions. */
5194 : :
5195 : : static void
5196 : 2816 : print_error_for_call_failure (tree fn, const vec<tree, va_gc> *args,
5197 : : struct z_candidate *candidates)
5198 : : {
5199 : 2816 : tree targs = NULL_TREE;
5200 : 2816 : if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
5201 : : {
5202 : 464 : targs = TREE_OPERAND (fn, 1);
5203 : 464 : fn = TREE_OPERAND (fn, 0);
5204 : : }
5205 : 5632 : tree name = OVL_NAME (fn);
5206 : 2816 : location_t loc = location_of (name);
5207 : 2816 : if (targs)
5208 : 464 : name = lookup_template_function (name, targs);
5209 : :
5210 : 2816 : auto_diagnostic_group d;
5211 : 5632 : if (!any_strictly_viable (candidates))
5212 : 2347 : error_at (loc, "no matching function for call to %<%D(%A)%>",
5213 : : name, build_tree_list_vec (args));
5214 : : else
5215 : 469 : error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous",
5216 : : name, build_tree_list_vec (args));
5217 : 2816 : if (candidates)
5218 : 2816 : print_z_candidates (loc, candidates);
5219 : 2816 : }
5220 : :
5221 : : /* Perform overload resolution on the set of deduction guides DGUIDES
5222 : : using ARGS. Returns the selected deduction guide, or error_mark_node
5223 : : if overload resolution fails. */
5224 : :
5225 : : tree
5226 : 17200 : perform_dguide_overload_resolution (tree dguides, const vec<tree, va_gc> *args,
5227 : : tsubst_flags_t complain)
5228 : : {
5229 : 17200 : z_candidate *candidates;
5230 : 17200 : bool any_viable_p;
5231 : 17200 : tree result;
5232 : :
5233 : 34400 : gcc_assert (deduction_guide_p (OVL_FIRST (dguides)));
5234 : :
5235 : 17200 : conversion_obstack_sentinel cos;
5236 : :
5237 : 17200 : z_candidate *cand = perform_overload_resolution (dguides, args, &candidates,
5238 : : &any_viable_p, complain);
5239 : 17200 : if (!cand)
5240 : : {
5241 : 868 : if (complain & tf_error)
5242 : 188 : print_error_for_call_failure (dguides, args, candidates);
5243 : 868 : result = error_mark_node;
5244 : : }
5245 : : else
5246 : 16332 : result = cand->fn;
5247 : :
5248 : 34400 : return result;
5249 : 17200 : }
5250 : :
5251 : : /* Return an expression for a call to FN (a namespace-scope function,
5252 : : or a static member function) with the ARGS. This may change
5253 : : ARGS. */
5254 : :
5255 : : tree
5256 : 75448963 : build_new_function_call (tree fn, vec<tree, va_gc> **args,
5257 : : tsubst_flags_t complain)
5258 : : {
5259 : 75448963 : struct z_candidate *candidates, *cand;
5260 : 75448963 : bool any_viable_p;
5261 : 75448963 : tree result;
5262 : :
5263 : 75448963 : if (args != NULL && *args != NULL)
5264 : : {
5265 : 75448963 : *args = resolve_args (*args, complain);
5266 : 75448963 : if (*args == NULL)
5267 : 300 : return error_mark_node;
5268 : : }
5269 : :
5270 : 75448663 : if (flag_tm)
5271 : 2175 : tm_malloc_replacement (fn);
5272 : :
5273 : 75448663 : conversion_obstack_sentinel cos;
5274 : :
5275 : 75448663 : cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
5276 : : complain);
5277 : :
5278 : 75435145 : if (!cand)
5279 : : {
5280 : 102981 : if (complain & tf_error)
5281 : : {
5282 : : // If there is a single (non-viable) function candidate,
5283 : : // let the error be diagnosed by cp_build_function_call_vec.
5284 : 3036 : if (!any_viable_p && candidates && ! candidates->next
5285 : 1281 : && TREE_CODE (candidates->fn) == FUNCTION_DECL
5286 : : /* A template-id callee consisting of a single (ignored)
5287 : : non-template candidate needs to be diagnosed the
5288 : : ordinary way. */
5289 : 425 : && (TREE_CODE (fn) != TEMPLATE_ID_EXPR
5290 : 29 : || candidates->template_decl))
5291 : 417 : return cp_build_function_call_vec (candidates->fn, args, complain);
5292 : :
5293 : : // Otherwise, emit notes for non-viable candidates.
5294 : 2619 : print_error_for_call_failure (fn, *args, candidates);
5295 : : }
5296 : 102564 : result = error_mark_node;
5297 : : }
5298 : : else
5299 : : {
5300 : 75332164 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
5301 : : }
5302 : :
5303 : 75434728 : if (flag_coroutines
5304 : 41845473 : && result
5305 : 41845473 : && TREE_CODE (result) == CALL_EXPR
5306 : 99259561 : && DECL_BUILT_IN_CLASS (TREE_OPERAND (CALL_EXPR_FN (result), 0))
5307 : 23824833 : == BUILT_IN_NORMAL)
5308 : 2932559 : result = coro_validate_builtin_call (result);
5309 : :
5310 : : return result;
5311 : 75435145 : }
5312 : :
5313 : : /* Build a call to a global operator new. FNNAME is the name of the
5314 : : operator (either "operator new" or "operator new[]") and ARGS are
5315 : : the arguments provided. This may change ARGS. *SIZE points to the
5316 : : total number of bytes required by the allocation, and is updated if
5317 : : that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
5318 : : be used. If this function determines that no cookie should be
5319 : : used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK
5320 : : is not NULL_TREE, it is evaluated before calculating the final
5321 : : array size, and if it fails, the array size is replaced with
5322 : : (size_t)-1 (usually triggering a std::bad_alloc exception). If FN
5323 : : is non-NULL, it will be set, upon return, to the allocation
5324 : : function called. */
5325 : :
5326 : : tree
5327 : 495335 : build_operator_new_call (tree fnname, vec<tree, va_gc> **args,
5328 : : tree *size, tree *cookie_size,
5329 : : tree align_arg, tree size_check,
5330 : : tree *fn, tsubst_flags_t complain)
5331 : : {
5332 : 495335 : tree original_size = *size;
5333 : 495335 : tree fns;
5334 : 495335 : struct z_candidate *candidates;
5335 : 495335 : struct z_candidate *cand = NULL;
5336 : 495335 : bool any_viable_p;
5337 : :
5338 : 495335 : if (fn)
5339 : 493923 : *fn = NULL_TREE;
5340 : : /* Set to (size_t)-1 if the size check fails. */
5341 : 495335 : if (size_check != NULL_TREE)
5342 : : {
5343 : 14192 : tree errval = TYPE_MAX_VALUE (sizetype);
5344 : 14192 : if (cxx_dialect >= cxx11 && flag_exceptions)
5345 : 13791 : errval = throw_bad_array_new_length ();
5346 : 14192 : *size = fold_build3 (COND_EXPR, sizetype, size_check,
5347 : : original_size, errval);
5348 : : }
5349 : 495335 : vec_safe_insert (*args, 0, *size);
5350 : 495335 : *args = resolve_args (*args, complain);
5351 : 495335 : if (*args == NULL)
5352 : 0 : return error_mark_node;
5353 : :
5354 : 495335 : conversion_obstack_sentinel cos;
5355 : :
5356 : : /* Based on:
5357 : :
5358 : : [expr.new]
5359 : :
5360 : : If this lookup fails to find the name, or if the allocated type
5361 : : is not a class type, the allocation function's name is looked
5362 : : up in the global scope.
5363 : :
5364 : : we disregard block-scope declarations of "operator new". */
5365 : 495335 : fns = lookup_qualified_name (global_namespace, fnname);
5366 : :
5367 : 495335 : if (align_arg)
5368 : : {
5369 : 2344 : vec<tree, va_gc>* align_args
5370 : 2344 : = vec_copy_and_insert (*args, align_arg, 1);
5371 : 2344 : cand = perform_overload_resolution (fns, align_args, &candidates,
5372 : : &any_viable_p, tf_none);
5373 : 2344 : if (cand)
5374 : 2329 : *args = align_args;
5375 : : /* If no aligned allocation function matches, try again without the
5376 : : alignment. */
5377 : : }
5378 : :
5379 : : /* Figure out what function is being called. */
5380 : 2329 : if (!cand)
5381 : 493006 : cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p,
5382 : : complain);
5383 : :
5384 : : /* If no suitable function could be found, issue an error message
5385 : : and give up. */
5386 : 495335 : if (!cand)
5387 : : {
5388 : 9 : if (complain & tf_error)
5389 : 9 : print_error_for_call_failure (fns, *args, candidates);
5390 : 9 : return error_mark_node;
5391 : : }
5392 : :
5393 : : /* If a cookie is required, add some extra space. Whether
5394 : : or not a cookie is required cannot be determined until
5395 : : after we know which function was called. */
5396 : 495326 : if (*cookie_size)
5397 : : {
5398 : 262 : bool use_cookie = true;
5399 : 262 : tree arg_types;
5400 : :
5401 : 262 : arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
5402 : : /* Skip the size_t parameter. */
5403 : 262 : arg_types = TREE_CHAIN (arg_types);
5404 : : /* Check the remaining parameters (if any). */
5405 : 262 : if (arg_types
5406 : 262 : && TREE_CHAIN (arg_types) == void_list_node
5407 : 322 : && same_type_p (TREE_VALUE (arg_types),
5408 : : ptr_type_node))
5409 : 45 : use_cookie = false;
5410 : : /* If we need a cookie, adjust the number of bytes allocated. */
5411 : 262 : if (use_cookie)
5412 : : {
5413 : : /* Update the total size. */
5414 : 217 : *size = size_binop (PLUS_EXPR, original_size, *cookie_size);
5415 : 217 : if (size_check)
5416 : : {
5417 : : /* Set to (size_t)-1 if the size check fails. */
5418 : 44 : gcc_assert (size_check != NULL_TREE);
5419 : 44 : *size = fold_build3 (COND_EXPR, sizetype, size_check,
5420 : : *size, TYPE_MAX_VALUE (sizetype));
5421 : : }
5422 : : /* Update the argument list to reflect the adjusted size. */
5423 : 217 : (**args)[0] = *size;
5424 : : }
5425 : : else
5426 : 45 : *cookie_size = NULL_TREE;
5427 : : }
5428 : :
5429 : : /* Tell our caller which function we decided to call. */
5430 : 495326 : if (fn)
5431 : 493914 : *fn = cand->fn;
5432 : :
5433 : : /* Build the CALL_EXPR. */
5434 : 495326 : tree ret = build_over_call (cand, LOOKUP_NORMAL, complain);
5435 : :
5436 : : /* Set this flag for all callers of this function. In addition to
5437 : : new-expressions, this is called for allocating coroutine state; treat
5438 : : that as an implicit new-expression. */
5439 : 495326 : tree call = extract_call_expr (ret);
5440 : 495326 : if (TREE_CODE (call) == CALL_EXPR)
5441 : 495326 : CALL_FROM_NEW_OR_DELETE_P (call) = 1;
5442 : :
5443 : : return ret;
5444 : 495335 : }
5445 : :
5446 : : /* Evaluate side-effects from OBJ before evaluating call
5447 : : to FN in RESULT expression.
5448 : : This is for expressions of the form `obj->fn(...)'
5449 : : where `fn' turns out to be a static member function and
5450 : : `obj' needs to be evaluated. `fn' could be also static operator[]
5451 : : or static operator(), in which cases the source expression
5452 : : would be `obj[...]' or `obj(...)'. */
5453 : :
5454 : : tree
5455 : 64191877 : keep_unused_object_arg (tree result, tree obj, tree fn)
5456 : : {
5457 : 64191877 : if (result == NULL_TREE
5458 : 64191877 : || result == error_mark_node
5459 : 63454424 : || DECL_OBJECT_MEMBER_FUNCTION_P (fn)
5460 : 65982030 : || !TREE_SIDE_EFFECTS (obj))
5461 : : return result;
5462 : :
5463 : : /* But avoid the implicit lvalue-rvalue conversion when `obj' is
5464 : : volatile. */
5465 : 60443 : tree a = obj;
5466 : 60443 : if (TREE_THIS_VOLATILE (a))
5467 : 59862 : a = build_this (a);
5468 : 60443 : if (TREE_SIDE_EFFECTS (a))
5469 : 590 : return cp_build_compound_expr (a, result, tf_error);
5470 : : return result;
5471 : : }
5472 : :
5473 : : /* Build a new call to operator(). This may change ARGS. */
5474 : :
5475 : : tree
5476 : 1822000 : build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
5477 : : {
5478 : 1822000 : struct z_candidate *candidates = 0, *cand;
5479 : 1822000 : tree fns, convs, first_mem_arg = NULL_TREE;
5480 : 1822000 : bool any_viable_p;
5481 : 1822000 : tree result = NULL_TREE;
5482 : :
5483 : 1822000 : auto_cond_timevar tv (TV_OVERLOAD);
5484 : :
5485 : 1822000 : obj = mark_lvalue_use (obj);
5486 : :
5487 : 1822000 : if (error_operand_p (obj))
5488 : 0 : return error_mark_node;
5489 : :
5490 : 1822000 : tree type = TREE_TYPE (obj);
5491 : :
5492 : 1822000 : obj = prep_operand (obj);
5493 : :
5494 : 1822000 : if (TYPE_PTRMEMFUNC_P (type))
5495 : : {
5496 : 0 : if (complain & tf_error)
5497 : : /* It's no good looking for an overloaded operator() on a
5498 : : pointer-to-member-function. */
5499 : 0 : error ("pointer-to-member function %qE cannot be called without "
5500 : : "an object; consider using %<.*%> or %<->*%>", obj);
5501 : 0 : return error_mark_node;
5502 : : }
5503 : :
5504 : 1822000 : if (TYPE_BINFO (type))
5505 : : {
5506 : 1821982 : fns = lookup_fnfields (TYPE_BINFO (type), call_op_identifier, 1, complain);
5507 : 1821982 : if (fns == error_mark_node)
5508 : : return error_mark_node;
5509 : : }
5510 : : else
5511 : : fns = NULL_TREE;
5512 : :
5513 : 1821990 : if (args != NULL && *args != NULL)
5514 : : {
5515 : 1821990 : *args = resolve_args (*args, complain);
5516 : 1821990 : if (*args == NULL)
5517 : 132 : return error_mark_node;
5518 : : }
5519 : :
5520 : 1821858 : conversion_obstack_sentinel cos;
5521 : :
5522 : 1821858 : if (fns)
5523 : : {
5524 : 1820396 : first_mem_arg = obj;
5525 : :
5526 : 1820396 : add_candidates (BASELINK_FUNCTIONS (fns),
5527 : : first_mem_arg, *args, NULL_TREE,
5528 : : NULL_TREE, false,
5529 : 1820396 : BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
5530 : : LOOKUP_NORMAL, &candidates, complain);
5531 : : }
5532 : :
5533 : 1821858 : bool any_call_ops = candidates != nullptr;
5534 : :
5535 : 1821858 : convs = lookup_conversions (type);
5536 : :
5537 : 1899102 : for (; convs; convs = TREE_CHAIN (convs))
5538 : : {
5539 : 77244 : tree totype = TREE_TYPE (convs);
5540 : :
5541 : 64210 : if (TYPE_PTRFN_P (totype)
5542 : 13037 : || TYPE_REFFN_P (totype)
5543 : 90237 : || (TYPE_REF_P (totype)
5544 : 914 : && TYPE_PTRFN_P (TREE_TYPE (totype))))
5545 : 128632 : for (tree fn : ovl_range (TREE_VALUE (convs)))
5546 : : {
5547 : 64316 : if (DECL_NONCONVERTING_P (fn))
5548 : 3 : continue;
5549 : :
5550 : 64313 : if (TREE_CODE (fn) == TEMPLATE_DECL)
5551 : : {
5552 : : /* Making this work broke PR 71117 and 85118, so until the
5553 : : committee resolves core issue 2189, let's disable this
5554 : : candidate if there are any call operators. */
5555 : 17404 : if (any_call_ops)
5556 : 17392 : continue;
5557 : :
5558 : 12 : add_template_conv_candidate
5559 : 12 : (&candidates, fn, obj, *args, totype,
5560 : : /*access_path=*/NULL_TREE,
5561 : : /*conversion_path=*/NULL_TREE, complain);
5562 : : }
5563 : : else
5564 : 46909 : add_conv_candidate (&candidates, fn, obj,
5565 : : *args, /*conversion_path=*/NULL_TREE,
5566 : : /*access_path=*/NULL_TREE, complain);
5567 : : }
5568 : : }
5569 : :
5570 : : /* Be strict here because if we choose a bad conversion candidate, the
5571 : : errors we get won't mention the call context. */
5572 : 1821858 : candidates = splice_viable (candidates, true, &any_viable_p);
5573 : 1821858 : if (!any_viable_p)
5574 : : {
5575 : 39617 : if (complain & tf_error)
5576 : : {
5577 : 288 : auto_diagnostic_group d;
5578 : 288 : error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
5579 : : build_tree_list_vec (*args));
5580 : 288 : print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
5581 : 288 : }
5582 : 39617 : result = error_mark_node;
5583 : : }
5584 : : else
5585 : : {
5586 : 1782241 : cand = tourney (candidates, complain);
5587 : 1782241 : if (cand == 0)
5588 : : {
5589 : 13 : if (complain & tf_error)
5590 : : {
5591 : 6 : auto_diagnostic_group d;
5592 : 12 : error ("call of %<(%T) (%A)%> is ambiguous",
5593 : 6 : TREE_TYPE (obj), build_tree_list_vec (*args));
5594 : 6 : print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
5595 : 6 : }
5596 : 13 : result = error_mark_node;
5597 : : }
5598 : 1782228 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL
5599 : 1782169 : && DECL_OVERLOADED_OPERATOR_P (cand->fn)
5600 : 3564397 : && DECL_OVERLOADED_OPERATOR_IS (cand->fn, CALL_EXPR))
5601 : : {
5602 : 1782169 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
5603 : : /* In an expression of the form `a()' where cand->fn
5604 : : which is operator() turns out to be a static member function,
5605 : : `a' is none-the-less evaluated. */
5606 : 1782169 : result = keep_unused_object_arg (result, obj, cand->fn);
5607 : : }
5608 : : else
5609 : : {
5610 : 59 : if (TREE_CODE (cand->fn) == FUNCTION_DECL)
5611 : 0 : obj = convert_like_with_context (cand->convs[0], obj, cand->fn,
5612 : : -1, complain);
5613 : : else
5614 : : {
5615 : 59 : gcc_checking_assert (TYPE_P (cand->fn));
5616 : 59 : obj = convert_like (cand->convs[0], obj, complain);
5617 : : }
5618 : 59 : obj = convert_from_reference (obj);
5619 : 59 : result = cp_build_function_call_vec (obj, args, complain);
5620 : : }
5621 : : }
5622 : :
5623 : 1821858 : return result;
5624 : 1822000 : }
5625 : :
5626 : : /* Subroutine for preparing format strings suitable for the error
5627 : : function. It concatenates a prefix (controlled by MATCH), ERRMSG,
5628 : : and SUFFIX. */
5629 : :
5630 : : static const char *
5631 : 1455 : concat_op_error_string (bool match, const char *errmsg, const char *suffix)
5632 : : {
5633 : 1455 : return concat (match
5634 : : ? G_("ambiguous overload for ")
5635 : : : G_("no match for "),
5636 : 0 : errmsg, suffix, nullptr);
5637 : : }
5638 : :
5639 : : /* Called by op_error to prepare format strings suitable for the error
5640 : : function. It concatenates a prefix (controlled by MATCH), ERRMSG,
5641 : : and a suffix (controlled by NTYPES). */
5642 : :
5643 : : static const char *
5644 : 1434 : op_error_string (const char *errmsg, int ntypes, bool match)
5645 : : {
5646 : 1434 : const char *suffix;
5647 : 0 : if (ntypes == 3)
5648 : : suffix = G_(" (operand types are %qT, %qT, and %qT)");
5649 : 0 : else if (ntypes == 2)
5650 : : suffix = G_(" (operand types are %qT and %qT)");
5651 : : else
5652 : 0 : suffix = G_(" (operand type is %qT)");
5653 : 0 : return concat_op_error_string (match, errmsg, suffix);
5654 : : }
5655 : :
5656 : : /* Similar to op_error_string, but a special-case for binary ops that
5657 : : use %e for the args, rather than %qT. */
5658 : :
5659 : : static const char *
5660 : 21 : binop_error_string (const char *errmsg, bool match)
5661 : : {
5662 : 21 : return concat_op_error_string (match, errmsg,
5663 : 21 : G_(" (operand types are %e and %e)"));
5664 : : }
5665 : :
5666 : : static void
5667 : 1455 : op_error (const op_location_t &loc,
5668 : : enum tree_code code, enum tree_code code2,
5669 : : tree arg1, tree arg2, tree arg3, bool match)
5670 : : {
5671 : 1455 : bool assop = code == MODIFY_EXPR;
5672 : 1455 : const char *opname = OVL_OP_INFO (assop, assop ? code2 : code)->name;
5673 : :
5674 : 1455 : switch (code)
5675 : : {
5676 : 0 : case COND_EXPR:
5677 : 0 : if (flag_diagnostics_show_caret)
5678 : 0 : error_at (loc, op_error_string (G_("ternary %<operator?:%>"),
5679 : : 3, match),
5680 : 0 : TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
5681 : : else
5682 : 0 : error_at (loc, op_error_string (G_("ternary %<operator?:%> "
5683 : : "in %<%E ? %E : %E%>"), 3, match),
5684 : : arg1, arg2, arg3,
5685 : 0 : TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
5686 : : break;
5687 : :
5688 : 0 : case POSTINCREMENT_EXPR:
5689 : 0 : case POSTDECREMENT_EXPR:
5690 : 0 : if (flag_diagnostics_show_caret)
5691 : 0 : error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
5692 : 0 : opname, TREE_TYPE (arg1));
5693 : : else
5694 : 0 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
5695 : : 1, match),
5696 : 0 : opname, arg1, opname, TREE_TYPE (arg1));
5697 : : break;
5698 : :
5699 : 13 : case ARRAY_REF:
5700 : 13 : if (flag_diagnostics_show_caret)
5701 : 0 : error_at (loc, op_error_string (G_("%<operator[]%>"), 2, match),
5702 : 0 : TREE_TYPE (arg1), TREE_TYPE (arg2));
5703 : : else
5704 : 26 : error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
5705 : : 2, match),
5706 : 13 : arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2));
5707 : : break;
5708 : :
5709 : 6 : case REALPART_EXPR:
5710 : 6 : case IMAGPART_EXPR:
5711 : 6 : if (flag_diagnostics_show_caret)
5712 : 0 : error_at (loc, op_error_string (G_("%qs"), 1, match),
5713 : 0 : opname, TREE_TYPE (arg1));
5714 : : else
5715 : 12 : error_at (loc, op_error_string (G_("%qs in %<%s %E%>"), 1, match),
5716 : 6 : opname, opname, arg1, TREE_TYPE (arg1));
5717 : : break;
5718 : :
5719 : 0 : case CO_AWAIT_EXPR:
5720 : 0 : if (flag_diagnostics_show_caret)
5721 : 0 : error_at (loc, op_error_string (G_("%<operator %s%>"), 1, match),
5722 : 0 : opname, TREE_TYPE (arg1));
5723 : : else
5724 : 0 : error_at (loc, op_error_string (G_("%<operator %s%> in %<%s%E%>"),
5725 : : 1, match),
5726 : 0 : opname, opname, arg1, TREE_TYPE (arg1));
5727 : : break;
5728 : :
5729 : 1436 : default:
5730 : 1436 : if (arg2)
5731 : 1336 : if (flag_diagnostics_show_caret)
5732 : : {
5733 : 21 : binary_op_rich_location richloc (loc, arg1, arg2, true);
5734 : 21 : pp_markup::element_quoted_type element_0
5735 : 21 : (TREE_TYPE (arg1), highlight_colors::lhs);
5736 : 21 : pp_markup::element_quoted_type element_1
5737 : 21 : (TREE_TYPE (arg2), highlight_colors::rhs);
5738 : 21 : error_at (&richloc,
5739 : : binop_error_string (G_("%<operator%s%>"), match),
5740 : : opname, &element_0, &element_1);
5741 : 21 : }
5742 : : else
5743 : 2630 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
5744 : : 2, match),
5745 : : opname, arg1, opname, arg2,
5746 : 1315 : TREE_TYPE (arg1), TREE_TYPE (arg2));
5747 : : else
5748 : 100 : if (flag_diagnostics_show_caret)
5749 : 0 : error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
5750 : 0 : opname, TREE_TYPE (arg1));
5751 : : else
5752 : 200 : error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
5753 : : 1, match),
5754 : 100 : opname, opname, arg1, TREE_TYPE (arg1));
5755 : : break;
5756 : : }
5757 : 1455 : }
5758 : :
5759 : : /* Return the implicit conversion sequence that could be used to
5760 : : convert E1 to E2 in [expr.cond]. */
5761 : :
5762 : : static conversion *
5763 : 282628 : conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
5764 : : {
5765 : 282628 : tree t1 = non_reference (TREE_TYPE (e1));
5766 : 282628 : tree t2 = non_reference (TREE_TYPE (e2));
5767 : 282628 : conversion *conv;
5768 : 282628 : bool good_base;
5769 : :
5770 : : /* [expr.cond]
5771 : :
5772 : : If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
5773 : : implicitly converted (clause _conv_) to the type "lvalue reference to
5774 : : T2", subject to the constraint that in the conversion the
5775 : : reference must bind directly (_dcl.init.ref_) to an lvalue.
5776 : :
5777 : : If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
5778 : : implicitly converted to the type "rvalue reference to T2", subject to
5779 : : the constraint that the reference must bind directly. */
5780 : 282628 : if (glvalue_p (e2))
5781 : : {
5782 : 252635 : tree rtype = cp_build_reference_type (t2, !lvalue_p (e2));
5783 : 252635 : conv = implicit_conversion (rtype,
5784 : : t1,
5785 : : e1,
5786 : : /*c_cast_p=*/false,
5787 : : LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
5788 : : |LOOKUP_ONLYCONVERTING,
5789 : : complain);
5790 : 252635 : if (conv && !conv->bad_p)
5791 : : return conv;
5792 : : }
5793 : :
5794 : : /* If E2 is a prvalue or if neither of the conversions above can be done
5795 : : and at least one of the operands has (possibly cv-qualified) class
5796 : : type: */
5797 : 234494 : if (!CLASS_TYPE_P (t1) && !CLASS_TYPE_P (t2))
5798 : : return NULL;
5799 : :
5800 : : /* [expr.cond]
5801 : :
5802 : : If E1 and E2 have class type, and the underlying class types are
5803 : : the same or one is a base class of the other: E1 can be converted
5804 : : to match E2 if the class of T2 is the same type as, or a base
5805 : : class of, the class of T1, and the cv-qualification of T2 is the
5806 : : same cv-qualification as, or a greater cv-qualification than, the
5807 : : cv-qualification of T1. If the conversion is applied, E1 is
5808 : : changed to an rvalue of type T2 that still refers to the original
5809 : : source class object (or the appropriate subobject thereof). */
5810 : 125804 : if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
5811 : 251664 : && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
5812 : : {
5813 : 59505 : if (good_base && at_least_as_qualified_p (t2, t1))
5814 : : {
5815 : 29644 : conv = build_identity_conv (t1, e1);
5816 : 29644 : if (!same_type_p (TYPE_MAIN_VARIANT (t1),
5817 : : TYPE_MAIN_VARIANT (t2)))
5818 : 6 : conv = build_conv (ck_base, t2, conv);
5819 : : else
5820 : 29638 : conv = build_conv (ck_rvalue, t2, conv);
5821 : 29644 : return conv;
5822 : : }
5823 : : else
5824 : 29861 : return NULL;
5825 : : }
5826 : : else
5827 : : /* [expr.cond]
5828 : :
5829 : : Otherwise: E1 can be converted to match E2 if E1 can be implicitly
5830 : : converted to the type that expression E2 would have if E2 were
5831 : : converted to an rvalue (or the type it has, if E2 is an rvalue). */
5832 : 127131 : return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
5833 : 127131 : LOOKUP_IMPLICIT, complain);
5834 : : }
5835 : :
5836 : : /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
5837 : : arguments to the conditional expression. */
5838 : :
5839 : : tree
5840 : 4786102 : build_conditional_expr (const op_location_t &loc,
5841 : : tree arg1, tree arg2, tree arg3,
5842 : : tsubst_flags_t complain)
5843 : : {
5844 : 4786102 : tree arg2_type;
5845 : 4786102 : tree arg3_type;
5846 : 4786102 : tree result = NULL_TREE;
5847 : 4786102 : tree result_type = NULL_TREE;
5848 : 4786102 : tree semantic_result_type = NULL_TREE;
5849 : 4786102 : bool is_glvalue = true;
5850 : 4786102 : struct z_candidate *candidates = 0;
5851 : 4786102 : struct z_candidate *cand;
5852 : 4786102 : tree orig_arg2, orig_arg3;
5853 : :
5854 : 4786102 : auto_cond_timevar tv (TV_OVERLOAD);
5855 : :
5856 : : /* As a G++ extension, the second argument to the conditional can be
5857 : : omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
5858 : : c'.) If the second operand is omitted, make sure it is
5859 : : calculated only once. */
5860 : 4786102 : if (!arg2)
5861 : : {
5862 : 377 : if (complain & tf_error)
5863 : 371 : pedwarn (loc, OPT_Wpedantic,
5864 : : "ISO C++ forbids omitting the middle term of "
5865 : : "a %<?:%> expression");
5866 : :
5867 : 377 : if ((complain & tf_warning) && !truth_value_p (TREE_CODE (arg1)))
5868 : 329 : warn_for_omitted_condop (loc, arg1);
5869 : :
5870 : : /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
5871 : 377 : if (glvalue_p (arg1))
5872 : : {
5873 : 86 : arg1 = cp_stabilize_reference (arg1);
5874 : 86 : arg2 = arg1 = prevent_lifetime_extension (arg1);
5875 : : }
5876 : 291 : else if (TREE_CODE (arg1) == TARGET_EXPR)
5877 : : /* arg1 can't be a prvalue result of the conditional
5878 : : expression, since it needs to be materialized for the
5879 : : conversion to bool, so treat it as an xvalue in arg2. */
5880 : 6 : arg2 = move (TARGET_EXPR_SLOT (arg1));
5881 : 285 : else if (TREE_CODE (arg1) == EXCESS_PRECISION_EXPR)
5882 : 3 : arg2 = arg1 = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (arg1),
5883 : 3 : cp_save_expr (TREE_OPERAND (arg1, 0)));
5884 : : else
5885 : 282 : arg2 = arg1 = cp_save_expr (arg1);
5886 : : }
5887 : :
5888 : : /* If something has already gone wrong, just pass that fact up the
5889 : : tree. */
5890 : 4786102 : if (error_operand_p (arg1)
5891 : 4786021 : || error_operand_p (arg2)
5892 : 9572083 : || error_operand_p (arg3))
5893 : 161 : return error_mark_node;
5894 : :
5895 : 4785941 : conversion_obstack_sentinel cos;
5896 : :
5897 : 4785941 : orig_arg2 = arg2;
5898 : 4785941 : orig_arg3 = arg3;
5899 : :
5900 : 4785941 : if (gnu_vector_type_p (TREE_TYPE (arg1))
5901 : 4785941 : && VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1)))
5902 : : {
5903 : 1002 : tree arg1_type = TREE_TYPE (arg1);
5904 : :
5905 : : /* If arg1 is another cond_expr choosing between -1 and 0,
5906 : : then we can use its comparison. It may help to avoid
5907 : : additional comparison, produce more accurate diagnostics
5908 : : and enables folding. */
5909 : 1002 : if (TREE_CODE (arg1) == VEC_COND_EXPR
5910 : 912 : && integer_minus_onep (TREE_OPERAND (arg1, 1))
5911 : 1914 : && integer_zerop (TREE_OPERAND (arg1, 2)))
5912 : 912 : arg1 = TREE_OPERAND (arg1, 0);
5913 : :
5914 : 1002 : arg1 = force_rvalue (arg1, complain);
5915 : 1002 : arg2 = force_rvalue (arg2, complain);
5916 : 1002 : arg3 = force_rvalue (arg3, complain);
5917 : :
5918 : : /* force_rvalue can return error_mark on valid arguments. */
5919 : 1002 : if (error_operand_p (arg1)
5920 : 1002 : || error_operand_p (arg2)
5921 : 2004 : || error_operand_p (arg3))
5922 : 0 : return error_mark_node;
5923 : :
5924 : 1002 : arg2_type = TREE_TYPE (arg2);
5925 : 1002 : arg3_type = TREE_TYPE (arg3);
5926 : :
5927 : 1002 : if (!VECTOR_TYPE_P (arg2_type)
5928 : 75 : && !VECTOR_TYPE_P (arg3_type))
5929 : : {
5930 : : /* Rely on the error messages of the scalar version. */
5931 : 57 : tree scal = build_conditional_expr (loc, integer_one_node,
5932 : : orig_arg2, orig_arg3, complain);
5933 : 57 : if (scal == error_mark_node)
5934 : : return error_mark_node;
5935 : 57 : tree stype = TREE_TYPE (scal);
5936 : 57 : tree ctype = TREE_TYPE (arg1_type);
5937 : 57 : if (TYPE_SIZE (stype) != TYPE_SIZE (ctype)
5938 : 57 : || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype)))
5939 : : {
5940 : 9 : if (complain & tf_error)
5941 : 9 : error_at (loc, "inferred scalar type %qT is not an integer or "
5942 : : "floating-point type of the same size as %qT", stype,
5943 : 9 : COMPARISON_CLASS_P (arg1)
5944 : 9 : ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0)))
5945 : : : ctype);
5946 : 9 : return error_mark_node;
5947 : : }
5948 : :
5949 : 48 : tree vtype = build_opaque_vector_type (stype,
5950 : 48 : TYPE_VECTOR_SUBPARTS (arg1_type));
5951 : : /* We could pass complain & tf_warning to unsafe_conversion_p,
5952 : : but the warnings (like Wsign-conversion) have already been
5953 : : given by the scalar build_conditional_expr_1. We still check
5954 : : unsafe_conversion_p to forbid truncating long long -> float. */
5955 : 48 : if (unsafe_conversion_p (stype, arg2, NULL_TREE, false))
5956 : : {
5957 : 0 : if (complain & tf_error)
5958 : 0 : error_at (loc, "conversion of scalar %qH to vector %qI "
5959 : : "involves truncation", arg2_type, vtype);
5960 : 0 : return error_mark_node;
5961 : : }
5962 : 48 : if (unsafe_conversion_p (stype, arg3, NULL_TREE, false))
5963 : : {
5964 : 3 : if (complain & tf_error)
5965 : 3 : error_at (loc, "conversion of scalar %qH to vector %qI "
5966 : : "involves truncation", arg3_type, vtype);
5967 : 3 : return error_mark_node;
5968 : : }
5969 : :
5970 : 45 : arg2 = cp_convert (stype, arg2, complain);
5971 : 45 : arg2 = save_expr (arg2);
5972 : 45 : arg2 = build_vector_from_val (vtype, arg2);
5973 : 45 : arg2_type = vtype;
5974 : 45 : arg3 = cp_convert (stype, arg3, complain);
5975 : 45 : arg3 = save_expr (arg3);
5976 : 45 : arg3 = build_vector_from_val (vtype, arg3);
5977 : 45 : arg3_type = vtype;
5978 : : }
5979 : :
5980 : 1962 : if ((gnu_vector_type_p (arg2_type) && !VECTOR_TYPE_P (arg3_type))
5981 : 1884 : || (gnu_vector_type_p (arg3_type) && !VECTOR_TYPE_P (arg2_type)))
5982 : : {
5983 : 96 : enum stv_conv convert_flag =
5984 : 96 : scalar_to_vector (loc, VEC_COND_EXPR, arg2, arg3,
5985 : : complain & tf_error);
5986 : :
5987 : 96 : switch (convert_flag)
5988 : : {
5989 : 0 : case stv_error:
5990 : 0 : return error_mark_node;
5991 : 15 : case stv_firstarg:
5992 : 15 : {
5993 : 15 : arg2 = save_expr (arg2);
5994 : 15 : arg2 = convert (TREE_TYPE (arg3_type), arg2);
5995 : 15 : arg2 = build_vector_from_val (arg3_type, arg2);
5996 : 15 : arg2_type = TREE_TYPE (arg2);
5997 : 15 : break;
5998 : : }
5999 : 72 : case stv_secondarg:
6000 : 72 : {
6001 : 72 : arg3 = save_expr (arg3);
6002 : 72 : arg3 = convert (TREE_TYPE (arg2_type), arg3);
6003 : 72 : arg3 = build_vector_from_val (arg2_type, arg3);
6004 : 72 : arg3_type = TREE_TYPE (arg3);
6005 : 72 : break;
6006 : : }
6007 : : default:
6008 : : break;
6009 : : }
6010 : : }
6011 : :
6012 : 990 : if (!gnu_vector_type_p (arg2_type)
6013 : 987 : || !gnu_vector_type_p (arg3_type)
6014 : 981 : || !same_type_p (arg2_type, arg3_type)
6015 : 981 : || maybe_ne (TYPE_VECTOR_SUBPARTS (arg1_type),
6016 : 981 : TYPE_VECTOR_SUBPARTS (arg2_type))
6017 : 1971 : || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type))
6018 : : {
6019 : 9 : if (complain & tf_error)
6020 : 6 : error_at (loc,
6021 : : "incompatible vector types in conditional expression: "
6022 : 6 : "%qT, %qT and %qT", TREE_TYPE (arg1),
6023 : 6 : TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3));
6024 : 9 : return error_mark_node;
6025 : : }
6026 : :
6027 : 981 : if (!COMPARISON_CLASS_P (arg1))
6028 : : {
6029 : 87 : tree cmp_type = truth_type_for (arg1_type);
6030 : 87 : arg1 = build2 (NE_EXPR, cmp_type, arg1, build_zero_cst (arg1_type));
6031 : : }
6032 : 981 : return build3_loc (loc, VEC_COND_EXPR, arg2_type, arg1, arg2, arg3);
6033 : : }
6034 : :
6035 : : /* [expr.cond]
6036 : :
6037 : : The first expression is implicitly converted to bool (clause
6038 : : _conv_). */
6039 : 4784939 : arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
6040 : : LOOKUP_NORMAL);
6041 : 4784939 : if (error_operand_p (arg1))
6042 : 25 : return error_mark_node;
6043 : :
6044 : 4784914 : arg2_type = unlowered_expr_type (arg2);
6045 : 4784914 : arg3_type = unlowered_expr_type (arg3);
6046 : :
6047 : 4784914 : if ((TREE_CODE (arg2) == EXCESS_PRECISION_EXPR
6048 : 4784896 : || TREE_CODE (arg3) == EXCESS_PRECISION_EXPR)
6049 : 45 : && (TREE_CODE (arg2_type) == INTEGER_TYPE
6050 : : || SCALAR_FLOAT_TYPE_P (arg2_type)
6051 : : || TREE_CODE (arg2_type) == COMPLEX_TYPE)
6052 : 45 : && (TREE_CODE (arg3_type) == INTEGER_TYPE
6053 : : || SCALAR_FLOAT_TYPE_P (arg3_type)
6054 : : || TREE_CODE (arg3_type) == COMPLEX_TYPE))
6055 : : {
6056 : 45 : semantic_result_type
6057 : 45 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
6058 : 45 : if (semantic_result_type == error_mark_node)
6059 : : {
6060 : 0 : tree t1 = arg2_type;
6061 : 0 : tree t2 = arg3_type;
6062 : 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6063 : 0 : t1 = TREE_TYPE (t1);
6064 : 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6065 : 0 : t2 = TREE_TYPE (t2);
6066 : 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
6067 : : && SCALAR_FLOAT_TYPE_P (t2)
6068 : : && (extended_float_type_p (t1)
6069 : : || extended_float_type_p (t2))
6070 : : && cp_compare_floating_point_conversion_ranks
6071 : : (t1, t2) == 3);
6072 : 0 : if (complain & tf_error)
6073 : 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
6074 : : "have unordered conversion rank",
6075 : : arg2_type, arg3_type);
6076 : 0 : return error_mark_node;
6077 : : }
6078 : 45 : if (TREE_CODE (arg2) == EXCESS_PRECISION_EXPR)
6079 : : {
6080 : 18 : arg2 = TREE_OPERAND (arg2, 0);
6081 : 18 : arg2_type = TREE_TYPE (arg2);
6082 : : }
6083 : 45 : if (TREE_CODE (arg3) == EXCESS_PRECISION_EXPR)
6084 : : {
6085 : 27 : arg3 = TREE_OPERAND (arg3, 0);
6086 : 27 : arg3_type = TREE_TYPE (arg3);
6087 : : }
6088 : : }
6089 : :
6090 : : /* [expr.cond]
6091 : :
6092 : : If either the second or the third operand has type (possibly
6093 : : cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
6094 : : array-to-pointer (_conv.array_), and function-to-pointer
6095 : : (_conv.func_) standard conversions are performed on the second
6096 : : and third operands. */
6097 : 4784914 : if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
6098 : : {
6099 : : /* 'void' won't help in resolving an overloaded expression on the
6100 : : other side, so require it to resolve by itself. */
6101 : 115324 : if (arg2_type == unknown_type_node)
6102 : : {
6103 : 9 : arg2 = resolve_nondeduced_context_or_error (arg2, complain);
6104 : 9 : arg2_type = TREE_TYPE (arg2);
6105 : : }
6106 : 115324 : if (arg3_type == unknown_type_node)
6107 : : {
6108 : 0 : arg3 = resolve_nondeduced_context_or_error (arg3, complain);
6109 : 0 : arg3_type = TREE_TYPE (arg3);
6110 : : }
6111 : :
6112 : : /* [expr.cond]
6113 : :
6114 : : One of the following shall hold:
6115 : :
6116 : : --The second or the third operand (but not both) is a
6117 : : throw-expression (_except.throw_); the result is of the type
6118 : : and value category of the other.
6119 : :
6120 : : --Both the second and the third operands have type void; the
6121 : : result is of type void and is a prvalue. */
6122 : 115324 : if (TREE_CODE (arg2) == THROW_EXPR
6123 : 76 : && TREE_CODE (arg3) != THROW_EXPR)
6124 : : {
6125 : 64 : result_type = arg3_type;
6126 : 64 : is_glvalue = glvalue_p (arg3);
6127 : : }
6128 : 115260 : else if (TREE_CODE (arg2) != THROW_EXPR
6129 : 115248 : && TREE_CODE (arg3) == THROW_EXPR)
6130 : : {
6131 : 171 : result_type = arg2_type;
6132 : 171 : is_glvalue = glvalue_p (arg2);
6133 : : }
6134 : 115089 : else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
6135 : : {
6136 : 115057 : result_type = void_type_node;
6137 : 115057 : is_glvalue = false;
6138 : : }
6139 : : else
6140 : : {
6141 : 32 : if (complain & tf_error)
6142 : : {
6143 : 18 : if (VOID_TYPE_P (arg2_type))
6144 : 9 : error_at (cp_expr_loc_or_loc (arg3, loc),
6145 : : "second operand to the conditional operator "
6146 : : "is of type %<void%>, but the third operand is "
6147 : : "neither a throw-expression nor of type %<void%>");
6148 : : else
6149 : 9 : error_at (cp_expr_loc_or_loc (arg2, loc),
6150 : : "third operand to the conditional operator "
6151 : : "is of type %<void%>, but the second operand is "
6152 : : "neither a throw-expression nor of type %<void%>");
6153 : : }
6154 : 32 : return error_mark_node;
6155 : : }
6156 : :
6157 : 115292 : goto valid_operands;
6158 : : }
6159 : : /* [expr.cond]
6160 : :
6161 : : Otherwise, if the second and third operand have different types,
6162 : : and either has (possibly cv-qualified) class type, or if both are
6163 : : glvalues of the same value category and the same type except for
6164 : : cv-qualification, an attempt is made to convert each of those operands
6165 : : to the type of the other. */
6166 : 4669590 : else if (!same_type_p (arg2_type, arg3_type)
6167 : 4669590 : && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)
6168 : 1121475 : || (same_type_ignoring_top_level_qualifiers_p (arg2_type,
6169 : : arg3_type)
6170 : 297951 : && glvalue_p (arg2) && glvalue_p (arg3)
6171 : 48082 : && lvalue_p (arg2) == lvalue_p (arg3))))
6172 : : {
6173 : 141314 : conversion *conv2;
6174 : 141314 : conversion *conv3;
6175 : 141314 : bool converted = false;
6176 : :
6177 : 141314 : conv2 = conditional_conversion (arg2, arg3, complain);
6178 : 141314 : conv3 = conditional_conversion (arg3, arg2, complain);
6179 : :
6180 : : /* [expr.cond]
6181 : :
6182 : : If both can be converted, or one can be converted but the
6183 : : conversion is ambiguous, the program is ill-formed. If
6184 : : neither can be converted, the operands are left unchanged and
6185 : : further checking is performed as described below. If exactly
6186 : : one conversion is possible, that conversion is applied to the
6187 : : chosen operand and the converted operand is used in place of
6188 : : the original operand for the remainder of this section. */
6189 : 141314 : if ((conv2 && !conv2->bad_p
6190 : 63014 : && conv3 && !conv3->bad_p)
6191 : 63019 : || (conv2 && conv2->kind == ck_ambig)
6192 : 141187 : || (conv3 && conv3->kind == ck_ambig))
6193 : : {
6194 : 127 : if (complain & tf_error)
6195 : : {
6196 : 6 : error_at (loc, "operands to %<?:%> have different types "
6197 : : "%qT and %qT",
6198 : : arg2_type, arg3_type);
6199 : 6 : if (conv2 && !conv2->bad_p && conv3 && !conv3->bad_p)
6200 : 3 : inform (loc, " and each type can be converted to the other");
6201 : 3 : else if (conv2 && conv2->kind == ck_ambig)
6202 : 3 : convert_like (conv2, arg2, complain);
6203 : : else
6204 : 0 : convert_like (conv3, arg3, complain);
6205 : : }
6206 : 127 : result = error_mark_node;
6207 : : }
6208 : 141187 : else if (conv2 && !conv2->bad_p)
6209 : : {
6210 : 62887 : arg2 = convert_like (conv2, arg2, complain);
6211 : 62887 : arg2 = convert_from_reference (arg2);
6212 : 62887 : arg2_type = TREE_TYPE (arg2);
6213 : : /* Even if CONV2 is a valid conversion, the result of the
6214 : : conversion may be invalid. For example, if ARG3 has type
6215 : : "volatile X", and X does not have a copy constructor
6216 : : accepting a "volatile X&", then even if ARG2 can be
6217 : : converted to X, the conversion will fail. */
6218 : 62887 : if (error_operand_p (arg2))
6219 : 0 : result = error_mark_node;
6220 : 0 : converted = true;
6221 : : }
6222 : 78300 : else if (conv3 && !conv3->bad_p)
6223 : : {
6224 : 77438 : arg3 = convert_like (conv3, arg3, complain);
6225 : 77438 : arg3 = convert_from_reference (arg3);
6226 : 77438 : arg3_type = TREE_TYPE (arg3);
6227 : 77438 : if (error_operand_p (arg3))
6228 : 0 : result = error_mark_node;
6229 : 0 : converted = true;
6230 : : }
6231 : :
6232 : 127 : if (result)
6233 : : return result;
6234 : :
6235 : : /* If, after the conversion, both operands have class type,
6236 : : treat the cv-qualification of both operands as if it were the
6237 : : union of the cv-qualification of the operands.
6238 : :
6239 : : The standard is not clear about what to do in this
6240 : : circumstance. For example, if the first operand has type
6241 : : "const X" and the second operand has a user-defined
6242 : : conversion to "volatile X", what is the type of the second
6243 : : operand after this step? Making it be "const X" (matching
6244 : : the first operand) seems wrong, as that discards the
6245 : : qualification without actually performing a copy. Leaving it
6246 : : as "volatile X" seems wrong as that will result in the
6247 : : conditional expression failing altogether, even though,
6248 : : according to this step, the one operand could be converted to
6249 : : the type of the other. */
6250 : 141187 : if (converted
6251 : 140325 : && CLASS_TYPE_P (arg2_type)
6252 : 173417 : && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
6253 : 0 : arg2_type = arg3_type =
6254 : 0 : cp_build_qualified_type (arg2_type,
6255 : 0 : cp_type_quals (arg2_type)
6256 : 0 : | cp_type_quals (arg3_type));
6257 : : }
6258 : :
6259 : : /* [expr.cond]
6260 : :
6261 : : If the second and third operands are glvalues of the same value
6262 : : category and have the same type, the result is of that type and
6263 : : value category. */
6264 : 6067017 : if (((lvalue_p (arg2) && lvalue_p (arg3))
6265 : 3658895 : || (xvalue_p (arg2) && xvalue_p (arg3)))
6266 : 5708573 : && same_type_p (arg2_type, arg3_type))
6267 : : {
6268 : 992409 : result_type = arg2_type;
6269 : 992409 : goto valid_operands;
6270 : : }
6271 : :
6272 : : /* [expr.cond]
6273 : :
6274 : : Otherwise, the result is an rvalue. If the second and third
6275 : : operand do not have the same type, and either has (possibly
6276 : : cv-qualified) class type, overload resolution is used to
6277 : : determine the conversions (if any) to be applied to the operands
6278 : : (_over.match.oper_, _over.built_). */
6279 : 3677054 : is_glvalue = false;
6280 : 3677054 : if (!same_type_p (arg2_type, arg3_type)
6281 : 3677054 : && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
6282 : : {
6283 : 862 : releasing_vec args;
6284 : 862 : conversion *conv;
6285 : 862 : bool any_viable_p;
6286 : :
6287 : : /* Rearrange the arguments so that add_builtin_candidate only has
6288 : : to know about two args. In build_builtin_candidate, the
6289 : : arguments are unscrambled. */
6290 : 862 : args->quick_push (arg2);
6291 : 862 : args->quick_push (arg3);
6292 : 862 : args->quick_push (arg1);
6293 : 862 : add_builtin_candidates (&candidates,
6294 : : COND_EXPR,
6295 : : NOP_EXPR,
6296 : : ovl_op_identifier (false, COND_EXPR),
6297 : : args,
6298 : : LOOKUP_NORMAL, complain);
6299 : :
6300 : : /* [expr.cond]
6301 : :
6302 : : If the overload resolution fails, the program is
6303 : : ill-formed. */
6304 : 862 : candidates = splice_viable (candidates, false, &any_viable_p);
6305 : 862 : if (!any_viable_p)
6306 : : {
6307 : 805 : if (complain & tf_error)
6308 : 22 : error_at (loc, "operands to %<?:%> have different types %qT and %qT",
6309 : : arg2_type, arg3_type);
6310 : 805 : return error_mark_node;
6311 : : }
6312 : 57 : cand = tourney (candidates, complain);
6313 : 57 : if (!cand)
6314 : : {
6315 : 0 : if (complain & tf_error)
6316 : : {
6317 : 0 : auto_diagnostic_group d;
6318 : 0 : op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, false);
6319 : 0 : print_z_candidates (loc, candidates);
6320 : 0 : }
6321 : 0 : return error_mark_node;
6322 : : }
6323 : :
6324 : : /* [expr.cond]
6325 : :
6326 : : Otherwise, the conversions thus determined are applied, and
6327 : : the converted operands are used in place of the original
6328 : : operands for the remainder of this section. */
6329 : 57 : conv = cand->convs[0];
6330 : 57 : arg1 = convert_like (conv, arg1, complain);
6331 : 57 : conv = cand->convs[1];
6332 : 57 : arg2 = convert_like (conv, arg2, complain);
6333 : 57 : arg2_type = TREE_TYPE (arg2);
6334 : 57 : conv = cand->convs[2];
6335 : 57 : arg3 = convert_like (conv, arg3, complain);
6336 : 57 : arg3_type = TREE_TYPE (arg3);
6337 : 862 : }
6338 : :
6339 : : /* [expr.cond]
6340 : :
6341 : : Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
6342 : : and function-to-pointer (_conv.func_) standard conversions are
6343 : : performed on the second and third operands.
6344 : :
6345 : : We need to force the lvalue-to-rvalue conversion here for class types,
6346 : : so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
6347 : : that isn't wrapped with a TARGET_EXPR plays havoc with exception
6348 : : regions. */
6349 : :
6350 : 3676249 : arg2 = force_rvalue (arg2, complain);
6351 : 3676249 : if (!CLASS_TYPE_P (arg2_type))
6352 : 3602591 : arg2_type = TREE_TYPE (arg2);
6353 : :
6354 : 3676249 : arg3 = force_rvalue (arg3, complain);
6355 : 3676249 : if (!CLASS_TYPE_P (arg3_type))
6356 : 3602591 : arg3_type = TREE_TYPE (arg3);
6357 : :
6358 : 3676249 : if (arg2 == error_mark_node || arg3 == error_mark_node)
6359 : : return error_mark_node;
6360 : :
6361 : : /* [expr.cond]
6362 : :
6363 : : After those conversions, one of the following shall hold:
6364 : :
6365 : : --The second and third operands have the same type; the result is of
6366 : : that type. */
6367 : 3676207 : if (same_type_p (arg2_type, arg3_type))
6368 : : result_type = arg2_type;
6369 : : /* [expr.cond]
6370 : :
6371 : : --The second and third operands have arithmetic or enumeration
6372 : : type; the usual arithmetic conversions are performed to bring
6373 : : them to a common type, and the result is of that type. */
6374 : 27176 : else if ((ARITHMETIC_TYPE_P (arg2_type)
6375 : 26337 : || UNSCOPED_ENUM_P (arg2_type))
6376 : 757530 : && (ARITHMETIC_TYPE_P (arg3_type)
6377 : 211 : || UNSCOPED_ENUM_P (arg3_type)))
6378 : : {
6379 : : /* A conditional expression between a floating-point
6380 : : type and an integer type should convert the integer type to
6381 : : the evaluation format of the floating-point type, with
6382 : : possible excess precision. */
6383 : 730212 : tree eptype2 = arg2_type;
6384 : 730212 : tree eptype3 = arg3_type;
6385 : 730212 : tree eptype;
6386 : 839 : if (ANY_INTEGRAL_TYPE_P (arg2_type)
6387 : 730215 : && (eptype = excess_precision_type (arg3_type)) != NULL_TREE)
6388 : : {
6389 : 3 : eptype3 = eptype;
6390 : 3 : if (!semantic_result_type)
6391 : 3 : semantic_result_type
6392 : 3 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
6393 : : }
6394 : 576 : else if (ANY_INTEGRAL_TYPE_P (arg3_type)
6395 : 730209 : && (eptype = excess_precision_type (arg2_type)) != NULL_TREE)
6396 : : {
6397 : 23 : eptype2 = eptype;
6398 : 23 : if (!semantic_result_type)
6399 : 23 : semantic_result_type
6400 : 23 : = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
6401 : : }
6402 : 730212 : result_type = type_after_usual_arithmetic_conversions (eptype2,
6403 : : eptype3);
6404 : 730212 : if (result_type == error_mark_node)
6405 : : {
6406 : 0 : tree t1 = eptype2;
6407 : 0 : tree t2 = eptype3;
6408 : 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6409 : 0 : t1 = TREE_TYPE (t1);
6410 : 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6411 : 0 : t2 = TREE_TYPE (t2);
6412 : 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
6413 : : && SCALAR_FLOAT_TYPE_P (t2)
6414 : : && (extended_float_type_p (t1)
6415 : : || extended_float_type_p (t2))
6416 : : && cp_compare_floating_point_conversion_ranks
6417 : : (t1, t2) == 3);
6418 : 0 : if (complain & tf_error)
6419 : 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
6420 : : "have unordered conversion rank",
6421 : : eptype2, eptype3);
6422 : 0 : return error_mark_node;
6423 : : }
6424 : 730212 : if (semantic_result_type == error_mark_node)
6425 : : {
6426 : 0 : tree t1 = arg2_type;
6427 : 0 : tree t2 = arg3_type;
6428 : 0 : if (TREE_CODE (t1) == COMPLEX_TYPE)
6429 : 0 : t1 = TREE_TYPE (t1);
6430 : 0 : if (TREE_CODE (t2) == COMPLEX_TYPE)
6431 : 0 : t2 = TREE_TYPE (t2);
6432 : 0 : gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1)
6433 : : && SCALAR_FLOAT_TYPE_P (t2)
6434 : : && (extended_float_type_p (t1)
6435 : : || extended_float_type_p (t2))
6436 : : && cp_compare_floating_point_conversion_ranks
6437 : : (t1, t2) == 3);
6438 : 0 : if (complain & tf_error)
6439 : 0 : error_at (loc, "operands to %<?:%> of types %qT and %qT "
6440 : : "have unordered conversion rank",
6441 : : arg2_type, arg3_type);
6442 : 0 : return error_mark_node;
6443 : : }
6444 : :
6445 : 730212 : if (complain & tf_warning)
6446 : 697431 : do_warn_double_promotion (result_type, arg2_type, arg3_type,
6447 : : "implicit conversion from %qH to %qI to "
6448 : : "match other result of conditional",
6449 : : loc);
6450 : :
6451 : 730212 : if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
6452 : 95 : && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
6453 : : {
6454 : 34 : tree stripped_orig_arg2 = tree_strip_any_location_wrapper (orig_arg2);
6455 : 34 : tree stripped_orig_arg3 = tree_strip_any_location_wrapper (orig_arg3);
6456 : 34 : if (TREE_CODE (stripped_orig_arg2) == CONST_DECL
6457 : 23 : && TREE_CODE (stripped_orig_arg3) == CONST_DECL
6458 : 57 : && (DECL_CONTEXT (stripped_orig_arg2)
6459 : 23 : == DECL_CONTEXT (stripped_orig_arg3)))
6460 : : /* Two enumerators from the same enumeration can have different
6461 : : types when the enumeration is still being defined. */;
6462 : 28 : else if (complain & (cxx_dialect >= cxx26
6463 : 28 : ? tf_warning_or_error : tf_warning))
6464 : 43 : emit_diagnostic ((cxx_dialect >= cxx26
6465 : : ? diagnostics::kind::pedwarn
6466 : : : diagnostics::kind::warning),
6467 : 25 : loc, OPT_Wenum_compare, "enumerated mismatch "
6468 : : "in conditional expression: %qT vs %qT",
6469 : : arg2_type, arg3_type);
6470 : 3 : else if (cxx_dialect >= cxx26)
6471 : 1 : return error_mark_node;
6472 : : }
6473 : 730178 : else if ((((complain & (cxx_dialect >= cxx26
6474 : 730178 : ? tf_warning_or_error : tf_warning))
6475 : 706785 : && warn_deprecated_enum_float_conv)
6476 : 423029 : || (cxx_dialect >= cxx26
6477 : 3190 : && (complain & tf_warning_or_error) == 0))
6478 : 310250 : && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
6479 : 16 : && SCALAR_FLOAT_TYPE_P (arg3_type))
6480 : 310239 : || (SCALAR_FLOAT_TYPE_P (arg2_type)
6481 : 350 : && TREE_CODE (arg3_type) == ENUMERAL_TYPE)))
6482 : : {
6483 : 19 : if (cxx_dialect >= cxx26 && (complain & tf_warning_or_error) == 0)
6484 : 2 : return error_mark_node;
6485 : 17 : if (cxx_dialect >= cxx26 && TREE_CODE (arg2_type) == ENUMERAL_TYPE)
6486 : 4 : pedwarn (loc, OPT_Wdeprecated_enum_float_conversion,
6487 : : "conditional expression between enumeration type "
6488 : : "%qT and floating-point type %qT", arg2_type, arg3_type);
6489 : 13 : else if (cxx_dialect >= cxx26)
6490 : 3 : pedwarn (loc, OPT_Wdeprecated_enum_float_conversion,
6491 : : "conditional expression between floating-point type "
6492 : : "%qT and enumeration type %qT", arg2_type, arg3_type);
6493 : 10 : else if (TREE_CODE (arg2_type) == ENUMERAL_TYPE)
6494 : 6 : warning_at (loc, OPT_Wdeprecated_enum_float_conversion,
6495 : : "conditional expression between enumeration type "
6496 : : "%qT and floating-point type %qT is deprecated",
6497 : : arg2_type, arg3_type);
6498 : : else
6499 : 4 : warning_at (loc, OPT_Wdeprecated_enum_float_conversion,
6500 : : "conditional expression between floating-point "
6501 : : "type %qT and enumeration type %qT is deprecated",
6502 : : arg2_type, arg3_type);
6503 : : }
6504 : 721161 : else if ((extra_warnings || warn_enum_conversion)
6505 : 730164 : && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
6506 : 15 : && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
6507 : 8991 : || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
6508 : 8 : && !same_type_p (arg2_type,
6509 : : type_promotes_to (arg3_type)))))
6510 : : {
6511 : 17 : if (complain & tf_warning)
6512 : : {
6513 : 12 : enum opt_code opt = (warn_enum_conversion
6514 : 17 : ? OPT_Wenum_conversion
6515 : : : OPT_Wextra);
6516 : 17 : warning_at (loc, opt, "enumerated and "
6517 : : "non-enumerated type in conditional expression");
6518 : : }
6519 : : }
6520 : :
6521 : 730209 : arg2 = perform_implicit_conversion (result_type, arg2, complain);
6522 : 730209 : arg3 = perform_implicit_conversion (result_type, arg3, complain);
6523 : : }
6524 : : /* [expr.cond]
6525 : :
6526 : : --The second and third operands have pointer type, or one has
6527 : : pointer type and the other is a null pointer constant; pointer
6528 : : conversions (_conv.ptr_) and qualification conversions
6529 : : (_conv.qual_) are performed to bring them to their composite
6530 : : pointer type (_expr.rel_). The result is of the composite
6531 : : pointer type.
6532 : :
6533 : : --The second and third operands have pointer to member type, or
6534 : : one has pointer to member type and the other is a null pointer
6535 : : constant; pointer to member conversions (_conv.mem_) and
6536 : : qualification conversions (_conv.qual_) are performed to bring
6537 : : them to a common type, whose cv-qualification shall match the
6538 : : cv-qualification of either the second or the third operand.
6539 : : The result is of the common type. */
6540 : 26384 : else if ((null_ptr_cst_p (arg2)
6541 : 179 : && TYPE_PTR_OR_PTRMEM_P (arg3_type))
6542 : 26205 : || (null_ptr_cst_p (arg3)
6543 : 24776 : && TYPE_PTR_OR_PTRMEM_P (arg2_type))
6544 : 1429 : || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
6545 : 106 : || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type))
6546 : 26483 : || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
6547 : : {
6548 : 26294 : result_type = composite_pointer_type (loc,
6549 : : arg2_type, arg3_type, arg2,
6550 : : arg3, CPO_CONDITIONAL_EXPR,
6551 : : complain);
6552 : 26294 : if (result_type == error_mark_node)
6553 : : return error_mark_node;
6554 : 26272 : arg2 = perform_implicit_conversion (result_type, arg2, complain);
6555 : 26272 : arg3 = perform_implicit_conversion (result_type, arg3, complain);
6556 : : }
6557 : :
6558 : 3676092 : if (!result_type)
6559 : : {
6560 : 90 : if (complain & tf_error)
6561 : 3 : error_at (loc, "operands to %<?:%> have different types %qT and %qT",
6562 : : arg2_type, arg3_type);
6563 : 90 : return error_mark_node;
6564 : : }
6565 : :
6566 : 3676092 : if (arg2 == error_mark_node || arg3 == error_mark_node)
6567 : : return error_mark_node;
6568 : :
6569 : 3676089 : valid_operands:
6570 : 4783790 : if (processing_template_decl && is_glvalue)
6571 : : {
6572 : : /* Let lvalue_kind know this was a glvalue. */
6573 : 82403 : tree arg = (result_type == arg2_type ? arg2 : arg3);
6574 : 82403 : result_type = cp_build_reference_type (result_type, xvalue_p (arg));
6575 : : }
6576 : :
6577 : 4783790 : result = build3_loc (loc, COND_EXPR, result_type, arg1, arg2, arg3);
6578 : :
6579 : : /* If the ARG2 and ARG3 are the same and don't have side-effects,
6580 : : warn here, because the COND_EXPR will be turned into ARG2. */
6581 : 4783790 : if (warn_duplicated_branches
6582 : 153 : && (complain & tf_warning)
6583 : 4783943 : && (arg2 == arg3 || operand_equal_p (arg2, arg3,
6584 : : OEP_ADDRESS_OF_SAME_FIELD)))
6585 : 57 : warning_at (EXPR_LOCATION (result), OPT_Wduplicated_branches,
6586 : : "this condition has identical branches");
6587 : :
6588 : : /* We can't use result_type below, as fold might have returned a
6589 : : throw_expr. */
6590 : :
6591 : 4783790 : if (!is_glvalue)
6592 : : {
6593 : : /* Expand both sides into the same slot, hopefully the target of
6594 : : the ?: expression. We used to check for TARGET_EXPRs here,
6595 : : but now we sometimes wrap them in NOP_EXPRs so the test would
6596 : : fail. */
6597 : 3791321 : if (CLASS_TYPE_P (TREE_TYPE (result)))
6598 : : {
6599 : 73697 : result = get_target_expr (result, complain);
6600 : : /* Tell gimplify_modify_expr_rhs not to strip this in
6601 : : assignment context: we want both arms to initialize
6602 : : the same temporary. */
6603 : 73697 : TARGET_EXPR_NO_ELIDE (result) = true;
6604 : : }
6605 : : /* If this expression is an rvalue, but might be mistaken for an
6606 : : lvalue, we must add a NON_LVALUE_EXPR. */
6607 : 3791321 : result = rvalue (result);
6608 : 3791321 : if (semantic_result_type)
6609 : 71 : result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type,
6610 : : result);
6611 : : }
6612 : : else
6613 : : {
6614 : 992469 : result = force_paren_expr (result);
6615 : 992469 : gcc_assert (semantic_result_type == NULL_TREE);
6616 : : }
6617 : :
6618 : : return result;
6619 : 4786102 : }
6620 : :
6621 : : /* OPERAND is an operand to an expression. Perform necessary steps
6622 : : required before using it. If OPERAND is NULL_TREE, NULL_TREE is
6623 : : returned. */
6624 : :
6625 : : static tree
6626 : 520737269 : prep_operand (tree operand)
6627 : : {
6628 : 520737269 : if (operand)
6629 : : {
6630 : 599731214 : if (CLASS_TYPE_P (TREE_TYPE (operand))
6631 : 315915149 : && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
6632 : : /* Make sure the template type is instantiated now. */
6633 : 7450480 : instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
6634 : : }
6635 : :
6636 : 520737269 : return operand;
6637 : : }
6638 : :
6639 : : /* True iff CONV represents a conversion sequence which no other can be better
6640 : : than under [over.ics.rank]: in other words, a "conversion" to the exact same
6641 : : type (including binding to a reference to the same type). This is stronger
6642 : : than the standard's "identity" category, which also includes reference
6643 : : bindings that add cv-qualifiers or change rvalueness. */
6644 : :
6645 : : static bool
6646 : 183662193 : perfect_conversion_p (conversion *conv)
6647 : : {
6648 : 183662193 : if (CONVERSION_RANK (conv) != cr_identity)
6649 : : return false;
6650 : 128125426 : if (conv->kind == ck_ref_bind)
6651 : : {
6652 : 30084649 : if (!conv->rvaluedness_matches_p)
6653 : : return false;
6654 : 21569363 : if (!same_type_p (TREE_TYPE (conv->type),
6655 : : next_conversion (conv)->type))
6656 : : return false;
6657 : : }
6658 : 116788280 : if (conv->check_narrowing)
6659 : : /* Brace elision is imperfect. */
6660 : : return false;
6661 : : return true;
6662 : : }
6663 : :
6664 : : /* True if CAND represents a perfect match, i.e. all perfect conversions, so no
6665 : : other candidate can be a better match. Since the template/non-template
6666 : : tiebreaker comes immediately after the conversion comparison in
6667 : : [over.match.best], a perfect non-template candidate is better than all
6668 : : templates. */
6669 : :
6670 : : static bool
6671 : 411918706 : perfect_candidate_p (z_candidate *cand)
6672 : : {
6673 : 411918706 : if (cand->viable < 1)
6674 : : return false;
6675 : : /* CWG1402 makes an implicitly deleted move op worse than other
6676 : : candidates. */
6677 : 170245741 : if (DECL_DELETED_FN (cand->fn) && DECL_DEFAULTED_FN (cand->fn)
6678 : 169372453 : && move_fn_p (cand->fn))
6679 : : return false;
6680 : 167961835 : int len = cand->num_convs;
6681 : 284750065 : for (int i = 0; i < len; ++i)
6682 : 183662193 : if (!perfect_conversion_p (cand->convs[i]))
6683 : : return false;
6684 : 101087872 : if (conversion *conv = cand->second_conv)
6685 : 0 : if (!perfect_conversion_p (conv))
6686 : : return false;
6687 : : return true;
6688 : : }
6689 : :
6690 : : /* True iff one of CAND's argument conversions is missing. */
6691 : :
6692 : : static bool
6693 : 17108667 : missing_conversion_p (const z_candidate *cand)
6694 : : {
6695 : 32355938 : for (unsigned i = 0; i < cand->num_convs; ++i)
6696 : : {
6697 : 21102330 : conversion *conv = cand->convs[i];
6698 : 21102330 : if (!conv)
6699 : : return true;
6700 : 20125201 : if (conv->kind == ck_deferred_bad)
6701 : : {
6702 : : /* We don't know whether this conversion is outright invalid or
6703 : : just bad, so conservatively assume it's missing. */
6704 : 4877930 : gcc_checking_assert (conv->bad_p);
6705 : : return true;
6706 : : }
6707 : : }
6708 : : return false;
6709 : : }
6710 : :
6711 : : /* Add each of the viable functions in FNS (a FUNCTION_DECL or
6712 : : OVERLOAD) to the CANDIDATES, returning an updated list of
6713 : : CANDIDATES. The ARGS are the arguments provided to the call;
6714 : : if FIRST_ARG is non-null it is the implicit object argument,
6715 : : otherwise the first element of ARGS is used if needed. The
6716 : : EXPLICIT_TARGS are explicit template arguments provided.
6717 : : TEMPLATE_ONLY is true if only template functions should be
6718 : : considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
6719 : : add_function_candidate. */
6720 : :
6721 : : static void
6722 : 228977851 : add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args,
6723 : : tree return_type,
6724 : : tree explicit_targs, bool template_only,
6725 : : tree conversion_path, tree access_path,
6726 : : int flags,
6727 : : struct z_candidate **candidates,
6728 : : tsubst_flags_t complain)
6729 : : {
6730 : 228977851 : tree ctype;
6731 : 228977851 : const vec<tree, va_gc> *non_static_args;
6732 : 228977851 : bool check_list_ctor = false;
6733 : 228977851 : bool check_converting = false;
6734 : 228977851 : unification_kind_t strict;
6735 : 228977851 : tree ne_context = NULL_TREE;
6736 : 228977851 : tree ne_fns = NULL_TREE;
6737 : :
6738 : 228977851 : if (!fns)
6739 : 2689606 : return;
6740 : :
6741 : : /* Precalculate special handling of constructors and conversion ops. */
6742 : 226288245 : tree fn = OVL_FIRST (fns);
6743 : 226288245 : if (DECL_CONV_FN_P (fn))
6744 : : {
6745 : 9673652 : check_list_ctor = false;
6746 : 9673652 : check_converting = (flags & LOOKUP_ONLYCONVERTING) != 0;
6747 : 9673652 : if (flags & LOOKUP_NO_CONVERSION)
6748 : : /* We're doing return_type(x). */
6749 : : strict = DEDUCE_CONV;
6750 : : else
6751 : : /* We're doing x.operator return_type(). */
6752 : 2839 : strict = DEDUCE_EXACT;
6753 : : /* [over.match.funcs] For conversion functions, the function
6754 : : is considered to be a member of the class of the implicit
6755 : : object argument for the purpose of defining the type of
6756 : : the implicit object parameter. */
6757 : 9673652 : ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg));
6758 : : }
6759 : : else
6760 : : {
6761 : 433229186 : if (DECL_CONSTRUCTOR_P (fn))
6762 : : {
6763 : 54695002 : check_list_ctor = (flags & LOOKUP_LIST_ONLY) != 0;
6764 : : /* For list-initialization we consider explicit constructors
6765 : : and complain if one is chosen. */
6766 : 54695002 : check_converting
6767 : 54695002 : = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
6768 : : == LOOKUP_ONLYCONVERTING);
6769 : : }
6770 : 216614593 : strict = DEDUCE_CALL;
6771 : 342109169 : ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
6772 : : }
6773 : :
6774 : : /* P2468: Check if operator== is a rewrite target with first operand
6775 : : (*args)[0]; for now just do the lookups. */
6776 : 226288245 : if ((flags & (LOOKUP_REWRITTEN | LOOKUP_REVERSED))
6777 : 226288245 : && DECL_OVERLOADED_OPERATOR_IS (fn, EQ_EXPR))
6778 : : {
6779 : 2784780 : tree ne_name = ovl_op_identifier (false, NE_EXPR);
6780 : 2784780 : if (DECL_CLASS_SCOPE_P (fn))
6781 : : {
6782 : 69221 : ne_context = DECL_CONTEXT (fn);
6783 : 69221 : ne_fns = lookup_fnfields (TREE_TYPE ((*args)[0]), ne_name,
6784 : : 1, tf_none);
6785 : 69221 : if (ne_fns == error_mark_node || ne_fns == NULL_TREE)
6786 : : ne_fns = NULL_TREE;
6787 : : else
6788 : 17250 : ne_fns = BASELINK_FUNCTIONS (ne_fns);
6789 : : }
6790 : : else
6791 : : {
6792 : 2715559 : ne_context = decl_namespace_context (fn);
6793 : 2715559 : ne_fns = lookup_qualified_name (ne_context, ne_name,
6794 : : LOOK_want::NORMAL,
6795 : : /*complain*/false);
6796 : 2715559 : if (ne_fns == error_mark_node
6797 : 790494 : || !is_overloaded_fn (ne_fns))
6798 : 1925065 : ne_fns = NULL_TREE;
6799 : : }
6800 : : }
6801 : :
6802 : 226288245 : if (first_arg)
6803 : : non_static_args = args;
6804 : : else
6805 : : /* Delay creating the implicit this parameter until it is needed. */
6806 : 96568377 : non_static_args = NULL;
6807 : :
6808 : 226288245 : bool seen_strictly_viable = any_strictly_viable (*candidates);
6809 : : /* If there's a non-template perfect match, we don't need to consider
6810 : : templates. So check non-templates first. This optimization is only
6811 : : really needed for the defaulted copy constructor of tuple and the like
6812 : : (96926), but it seems like we might as well enable it more generally. */
6813 : 226288245 : bool seen_perfect = false;
6814 : 226288245 : enum { templates, non_templates, either } which = either;
6815 : 226288245 : if (template_only)
6816 : : which = templates;
6817 : : else /*if (flags & LOOKUP_DEFAULTED)*/
6818 : 198667304 : which = non_templates;
6819 : :
6820 : : /* Template candidates that we'll potentially ignore if the
6821 : : perfect candidate optimization succeeds. */
6822 : 226288245 : z_candidate *ignored_template_cands = nullptr;
6823 : :
6824 : : /* During overload resolution, we first consider each function under the
6825 : : assumption that we'll eventually find a strictly viable candidate.
6826 : : This allows us to circumvent our defacto behavior when checking
6827 : : argument conversions and shortcut consideration of the candidate
6828 : : upon encountering the first bad conversion. If this assumption
6829 : : turns out to be false, and all candidates end up being non-strictly
6830 : : viable, then we reconsider such candidates under the defacto behavior.
6831 : : This trick is important for pruning member function overloads according
6832 : : to their const/ref-qualifiers (since all 'this' conversions are at
6833 : : worst bad) without breaking -fpermissive. */
6834 : 226288245 : z_candidate *bad_cands = nullptr;
6835 : 226288245 : bool shortcut_bad_convs = true;
6836 : :
6837 : 323914327 : again:
6838 : 1819351192 : for (tree fn : lkp_range (fns))
6839 : : {
6840 : 1495450416 : if (which == templates && TREE_CODE (fn) != TEMPLATE_DECL)
6841 : : {
6842 : 213012703 : if (template_only)
6843 : 465358 : add_ignored_candidate (candidates, fn);
6844 : 213012703 : continue;
6845 : : }
6846 : 1282437713 : if (which == non_templates && TREE_CODE (fn) == TEMPLATE_DECL)
6847 : : {
6848 : 435416604 : add_ignored_candidate (&ignored_template_cands, fn);
6849 : 435416604 : continue;
6850 : : }
6851 : 162381880 : if ((check_converting && DECL_NONCONVERTING_P (fn))
6852 : 995316370 : || (check_list_ctor && !is_list_ctor (fn)))
6853 : : {
6854 : 15238647 : add_ignored_candidate (candidates, fn);
6855 : 15238647 : continue;
6856 : : }
6857 : :
6858 : 831782462 : tree fn_first_arg = NULL_TREE;
6859 : 831782462 : const vec<tree, va_gc> *fn_args = args;
6860 : :
6861 : 831782462 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fn))
6862 : : {
6863 : : /* Figure out where the object arg comes from. If this
6864 : : function is a non-static member and we didn't get an
6865 : : implicit object argument, move it out of args. */
6866 : 320016535 : if (first_arg == NULL_TREE)
6867 : : {
6868 : 5448360 : unsigned int ix;
6869 : 5448360 : tree arg;
6870 : 5448360 : vec<tree, va_gc> *tempvec;
6871 : 5448360 : vec_alloc (tempvec, args->length () - 1);
6872 : 14604987 : for (ix = 1; args->iterate (ix, &arg); ++ix)
6873 : 3708267 : tempvec->quick_push (arg);
6874 : 5448360 : non_static_args = tempvec;
6875 : 5448360 : first_arg = (*args)[0];
6876 : : }
6877 : :
6878 : : fn_first_arg = first_arg;
6879 : : fn_args = non_static_args;
6880 : : }
6881 : :
6882 : : /* Don't bother reversing an operator with two identical parameters. */
6883 : 511765927 : else if (vec_safe_length (args) == 2 && (flags & LOOKUP_REVERSED))
6884 : : {
6885 : 88587790 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
6886 : 88587790 : if (same_type_p (TREE_VALUE (parmlist),
6887 : : TREE_VALUE (TREE_CHAIN (parmlist))))
6888 : 41533681 : continue;
6889 : : }
6890 : :
6891 : : /* When considering reversed operator==, if there's a corresponding
6892 : : operator!= in the same scope, it's not a rewrite target. */
6893 : 790248781 : if (ne_context)
6894 : : {
6895 : 59662726 : if (TREE_CODE (ne_context) == NAMESPACE_DECL)
6896 : : {
6897 : : /* With argument-dependent lookup, fns can span multiple
6898 : : namespaces; make sure we look in the fn's namespace for a
6899 : : corresponding operator!=. */
6900 : 59583211 : tree fn_ns = decl_namespace_context (fn);
6901 : 59583211 : if (fn_ns != ne_context)
6902 : : {
6903 : 3679177 : ne_context = fn_ns;
6904 : 3679177 : tree ne_name = ovl_op_identifier (false, NE_EXPR);
6905 : 3679177 : ne_fns = lookup_qualified_name (ne_context, ne_name,
6906 : : LOOK_want::NORMAL,
6907 : : /*complain*/false);
6908 : 3679177 : if (ne_fns == error_mark_node
6909 : 2285556 : || !is_overloaded_fn (ne_fns))
6910 : 1393621 : ne_fns = NULL_TREE;
6911 : : }
6912 : : }
6913 : 59662726 : bool found = false;
6914 : 677446524 : for (lkp_iterator ne (ne_fns); !found && ne; ++ne)
6915 : 632382202 : if (0 && !ne.using_p ()
6916 : : && DECL_NAMESPACE_SCOPE_P (fn)
6917 : : && DECL_CONTEXT (*ne) != DECL_CONTEXT (fn))
6918 : : /* ??? This kludge excludes inline namespace members for the H
6919 : : test in spaceship-eq15.C, but I don't see why we would want
6920 : : that behavior. Asked Core 2022-11-04. Disabling for now. */;
6921 : 632382202 : else if (fns_correspond (fn, *ne))
6922 : : {
6923 : : found = true;
6924 : : break;
6925 : : }
6926 : 59662726 : if (found)
6927 : 14598404 : continue;
6928 : : }
6929 : :
6930 : 775650377 : if (TREE_CODE (fn) == TEMPLATE_DECL)
6931 : 363731671 : add_template_candidate (candidates,
6932 : : fn,
6933 : : ctype,
6934 : : explicit_targs,
6935 : : fn_first_arg,
6936 : : fn_args,
6937 : : return_type,
6938 : : access_path,
6939 : : conversion_path,
6940 : : flags,
6941 : : strict,
6942 : : shortcut_bad_convs,
6943 : : complain);
6944 : : else
6945 : : {
6946 : 411918706 : add_function_candidate (candidates,
6947 : : fn,
6948 : : ctype,
6949 : : fn_first_arg,
6950 : : fn_args,
6951 : : access_path,
6952 : : conversion_path,
6953 : : flags,
6954 : : NULL,
6955 : : shortcut_bad_convs,
6956 : : complain);
6957 : 411918706 : if (perfect_candidate_p (*candidates))
6958 : 775636826 : seen_perfect = true;
6959 : : }
6960 : :
6961 : 775636826 : z_candidate *cand = *candidates;
6962 : 775636826 : if (cand->viable == 1)
6963 : 231363218 : seen_strictly_viable = true;
6964 : :
6965 : 775636826 : if (cand->viable == -1
6966 : 17109270 : && shortcut_bad_convs
6967 : 792745493 : && (missing_conversion_p (cand)
6968 : 11253608 : || TREE_CODE (cand->fn) == TEMPLATE_DECL))
6969 : : {
6970 : : /* This candidate has been tentatively marked non-strictly viable,
6971 : : and we didn't compute all argument conversions for it (having
6972 : : stopped at the first bad conversion). Move it to BAD_CANDS to
6973 : : to fully reconsider later if we don't find any strictly viable
6974 : : candidates. */
6975 : 5881695 : if (complain & (tf_error | tf_conv))
6976 : : {
6977 : 5614126 : *candidates = cand->next;
6978 : 5614126 : cand->next = bad_cands;
6979 : 5614126 : bad_cands = cand;
6980 : : }
6981 : : else
6982 : : /* But if we're in a SFINAE context, just mark this candidate as
6983 : : unviable outright and avoid potentially reconsidering it.
6984 : : This is safe to do because in a SFINAE context, performing a bad
6985 : : conversion is always an error (even with -fpermissive), so a
6986 : : non-strictly viable candidate is effectively unviable anyway. */
6987 : 267569 : cand->viable = 0;
6988 : : }
6989 : : }
6990 : 323900776 : if (which == non_templates && !seen_perfect)
6991 : : {
6992 : 97584638 : which = templates;
6993 : 97584638 : ignored_template_cands = nullptr;
6994 : 97584638 : goto again;
6995 : : }
6996 : 226316138 : else if (which == templates
6997 : 226316138 : && !seen_strictly_viable
6998 : : && shortcut_bad_convs
6999 : 36776991 : && bad_cands)
7000 : : {
7001 : : /* None of the candidates are strictly viable, so consider again those
7002 : : functions in BAD_CANDS, this time without shortcutting bad conversions
7003 : : so that all their argument conversions are computed. */
7004 : 115243 : which = either;
7005 : : fns = NULL_TREE;
7006 : 115243 : for (z_candidate *cand = bad_cands; cand; cand = cand->next)
7007 : : {
7008 : 73799 : tree fn = cand->fn;
7009 : 73799 : if (tree ti = cand->template_decl)
7010 : 72 : fn = TI_TEMPLATE (ti);
7011 : 73799 : fns = ovl_make (fn, fns);
7012 : : }
7013 : 41444 : shortcut_bad_convs = false;
7014 : 41444 : bad_cands = nullptr;
7015 : 41444 : goto again;
7016 : : }
7017 : :
7018 : 226274694 : if (complain & tf_error)
7019 : : {
7020 : : /* Remember any omitted candidates; we may want to print all candidates
7021 : : as part of overload resolution failure diagnostics. */
7022 : 370581096 : for (z_candidate *omitted_cands : { ignored_template_cands, bad_cands })
7023 : : {
7024 : 247054064 : z_candidate **omitted_cands_tail = &omitted_cands;
7025 : 293264800 : while (*omitted_cands_tail)
7026 : 46210736 : omitted_cands_tail = &(*omitted_cands_tail)->next;
7027 : 247054064 : *omitted_cands_tail = *candidates;
7028 : 247054064 : *candidates = omitted_cands;
7029 : : }
7030 : : }
7031 : : }
7032 : :
7033 : : /* Returns 1 if P0145R2 says that the LHS of operator CODE is evaluated first,
7034 : : -1 if the RHS is evaluated first, or 0 if the order is unspecified. */
7035 : :
7036 : : static int
7037 : 10253670 : op_is_ordered (tree_code code)
7038 : : {
7039 : 10253474 : switch (code)
7040 : : {
7041 : : // 5. b @= a
7042 : 2338958 : case MODIFY_EXPR:
7043 : 2338958 : return (flag_strong_eval_order > 1 ? -1 : 0);
7044 : :
7045 : : // 6. a[b]
7046 : 500935 : case ARRAY_REF:
7047 : 500739 : return (flag_strong_eval_order > 1 ? 1 : 0);
7048 : :
7049 : : // 1. a.b
7050 : : // Not overloadable (yet).
7051 : : // 2. a->b
7052 : : // Only one argument.
7053 : : // 3. a->*b
7054 : 51247 : case MEMBER_REF:
7055 : : // 7. a << b
7056 : 51247 : case LSHIFT_EXPR:
7057 : : // 8. a >> b
7058 : 51247 : case RSHIFT_EXPR:
7059 : : // a && b
7060 : : // Predates P0145R3.
7061 : 51247 : case TRUTH_ANDIF_EXPR:
7062 : : // a || b
7063 : : // Predates P0145R3.
7064 : 51247 : case TRUTH_ORIF_EXPR:
7065 : : // a , b
7066 : : // Predates P0145R3.
7067 : 51247 : case COMPOUND_EXPR:
7068 : 51247 : return (flag_strong_eval_order ? 1 : 0);
7069 : :
7070 : : default:
7071 : : return 0;
7072 : : }
7073 : : }
7074 : :
7075 : : /* Subroutine of build_new_op: Add to CANDIDATES all candidates for the
7076 : : operator indicated by CODE/CODE2. This function calls itself recursively to
7077 : : handle C++20 rewritten comparison operator candidates. Returns NULL_TREE
7078 : : upon success, and error_mark_node if something went wrong that prevented
7079 : : us from performing overload resolution (e.g. ambiguous member name lookup).
7080 : :
7081 : : LOOKUPS, if non-NULL, is the set of pertinent namespace-scope operator
7082 : : overloads to consider. This parameter is used when instantiating a
7083 : : dependent operator expression and has the same structure as
7084 : : DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS. */
7085 : :
7086 : : static tree
7087 : 20916213 : add_operator_candidates (z_candidate **candidates,
7088 : : tree_code code, tree_code code2,
7089 : : vec<tree, va_gc> *arglist, tree lookups,
7090 : : int flags, tsubst_flags_t complain)
7091 : : {
7092 : 20916213 : z_candidate *start_candidates = *candidates;
7093 : 20916213 : bool ismodop = code2 != ERROR_MARK;
7094 : 20916213 : tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
7095 : :
7096 : : /* LOOKUP_REWRITTEN is set when we're looking for the == or <=> operator to
7097 : : rewrite from, and also when we're looking for the e.g. < operator to use
7098 : : on the result of <=>. In the latter case, we don't want the flag set in
7099 : : the candidate, we just want to suppress looking for rewrites. */
7100 : 20916213 : bool rewritten = (flags & LOOKUP_REWRITTEN);
7101 : 20916213 : if (rewritten && code != EQ_EXPR && code != SPACESHIP_EXPR)
7102 : 376032 : flags &= ~LOOKUP_REWRITTEN;
7103 : :
7104 : 19979365 : bool memonly = false;
7105 : 19979365 : switch (code)
7106 : : {
7107 : : /* =, ->, [], () must be non-static member functions. */
7108 : 4060605 : case MODIFY_EXPR:
7109 : 4060605 : if (code2 != NOP_EXPR)
7110 : : break;
7111 : : /* FALLTHRU */
7112 : : case COMPONENT_REF:
7113 : : case ARRAY_REF:
7114 : : memonly = true;
7115 : : break;
7116 : :
7117 : : default:
7118 : : break;
7119 : : }
7120 : :
7121 : : /* Add namespace-scope operators to the list of functions to
7122 : : consider. */
7123 : : if (!memonly)
7124 : : {
7125 : 17848410 : tree fns;
7126 : 17848410 : if (!lookups)
7127 : 12920358 : fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE);
7128 : : /* If LOOKUPS is non-NULL, then we're instantiating a dependent operator
7129 : : expression, and LOOKUPS is the result of stage 1 name lookup. */
7130 : 4928052 : else if (tree found = purpose_member (fnname, lookups))
7131 : 1530360 : fns = TREE_VALUE (found);
7132 : : else
7133 : : fns = NULL_TREE;
7134 : 17848410 : fns = lookup_arg_dependent (fnname, fns, arglist);
7135 : 17848410 : add_candidates (fns, NULL_TREE, arglist, NULL_TREE,
7136 : : NULL_TREE, false, NULL_TREE, NULL_TREE,
7137 : : flags, candidates, complain);
7138 : : }
7139 : :
7140 : : /* Add class-member operators to the candidate set. */
7141 : 20916180 : tree arg1_type = TREE_TYPE ((*arglist)[0]);
7142 : 20916180 : unsigned nargs = arglist->length () > 1 ? 2 : 1;
7143 : 16935052 : tree arg2_type = nargs > 1 ? TREE_TYPE ((*arglist)[1]) : NULL_TREE;
7144 : 20916180 : if (CLASS_TYPE_P (arg1_type))
7145 : : {
7146 : 11620662 : tree fns = lookup_fnfields (arg1_type, fnname, 1, complain);
7147 : 11620662 : if (fns == error_mark_node)
7148 : : return error_mark_node;
7149 : 11620650 : if (fns)
7150 : : {
7151 : 5949115 : if (code == ARRAY_REF)
7152 : : {
7153 : 500755 : vec<tree,va_gc> *restlist = make_tree_vector ();
7154 : 1001510 : for (unsigned i = 1; i < nargs; ++i)
7155 : 500755 : vec_safe_push (restlist, (*arglist)[i]);
7156 : 500755 : z_candidate *save_cand = *candidates;
7157 : 1001510 : add_candidates (BASELINK_FUNCTIONS (fns),
7158 : 500755 : (*arglist)[0], restlist, NULL_TREE,
7159 : : NULL_TREE, false,
7160 : 500755 : BASELINK_BINFO (fns),
7161 : 500755 : BASELINK_ACCESS_BINFO (fns),
7162 : : flags, candidates, complain);
7163 : : /* Release the vec if we didn't add a candidate that uses it. */
7164 : 501270 : for (z_candidate *c = *candidates; c != save_cand; c = c->next)
7165 : 501270 : if (c->args == restlist)
7166 : : {
7167 : 500755 : restlist = NULL;
7168 : 500755 : break;
7169 : : }
7170 : 500755 : release_tree_vector (restlist);
7171 : : }
7172 : : else
7173 : 5448360 : add_candidates (BASELINK_FUNCTIONS (fns),
7174 : : NULL_TREE, arglist, NULL_TREE,
7175 : : NULL_TREE, false,
7176 : 5448360 : BASELINK_BINFO (fns),
7177 : 5448360 : BASELINK_ACCESS_BINFO (fns),
7178 : : flags, candidates, complain);
7179 : : }
7180 : : }
7181 : : /* Per [over.match.oper]3.2, if no operand has a class type, then
7182 : : only non-member functions that have type T1 or reference to
7183 : : cv-qualified-opt T1 for the first argument, if the first argument
7184 : : has an enumeration type, or T2 or reference to cv-qualified-opt
7185 : : T2 for the second argument, if the second argument has an
7186 : : enumeration type. Filter out those that don't match. */
7187 : 9295518 : else if (! arg2_type || ! CLASS_TYPE_P (arg2_type))
7188 : : {
7189 : : struct z_candidate **candp, **next;
7190 : :
7191 : 189956539 : for (candp = candidates; *candp != start_candidates; candp = next)
7192 : : {
7193 : 180913584 : unsigned i;
7194 : 180913584 : z_candidate *cand = *candp;
7195 : 180913584 : next = &cand->next;
7196 : :
7197 : 180913584 : tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
7198 : :
7199 : 535121427 : for (i = 0; i < nargs; ++i)
7200 : : {
7201 : 357836358 : tree parmtype = TREE_VALUE (parmlist);
7202 : 357836358 : tree argtype = unlowered_expr_type ((*arglist)[i]);
7203 : :
7204 : 357836358 : if (TYPE_REF_P (parmtype))
7205 : 276768993 : parmtype = TREE_TYPE (parmtype);
7206 : 357836358 : if (TREE_CODE (argtype) == ENUMERAL_TYPE
7207 : 702529470 : && (same_type_ignoring_top_level_qualifiers_p
7208 : 344693112 : (argtype, parmtype)))
7209 : : break;
7210 : :
7211 : 354207843 : parmlist = TREE_CHAIN (parmlist);
7212 : : }
7213 : :
7214 : : /* No argument has an appropriate type, so remove this
7215 : : candidate function from the list. */
7216 : 180913584 : if (i == nargs)
7217 : : {
7218 : 177285069 : *candp = cand->next;
7219 : 177285069 : next = candp;
7220 : : }
7221 : : }
7222 : : }
7223 : :
7224 : 20916168 : if (!rewritten)
7225 : : {
7226 : : /* The standard says to rewrite built-in candidates, too,
7227 : : but there's no point. */
7228 : 16882239 : add_builtin_candidates (candidates, code, code2, fnname, arglist,
7229 : : flags, complain);
7230 : :
7231 : : /* Maybe add C++20 rewritten comparison candidates. */
7232 : 16882239 : tree_code rewrite_code = ERROR_MARK;
7233 : 16882239 : if (cxx_dialect >= cxx20
7234 : 8957883 : && nargs == 2
7235 : 24050577 : && (OVERLOAD_TYPE_P (arg1_type) || OVERLOAD_TYPE_P (arg2_type)))
7236 : 7168335 : switch (code)
7237 : : {
7238 : 489603 : case LT_EXPR:
7239 : 489603 : case LE_EXPR:
7240 : 489603 : case GT_EXPR:
7241 : 489603 : case GE_EXPR:
7242 : 489603 : case SPACESHIP_EXPR:
7243 : 489603 : rewrite_code = SPACESHIP_EXPR;
7244 : 489603 : break;
7245 : :
7246 : : case NE_EXPR:
7247 : : case EQ_EXPR:
7248 : : rewrite_code = EQ_EXPR;
7249 : : break;
7250 : :
7251 : : default:;
7252 : : }
7253 : :
7254 : 489603 : if (rewrite_code)
7255 : : {
7256 : 2521915 : tree r;
7257 : 2521915 : flags |= LOOKUP_REWRITTEN;
7258 : 2521915 : if (rewrite_code != code)
7259 : : {
7260 : : /* Add rewritten candidates in same order. */
7261 : 1135767 : r = add_operator_candidates (candidates, rewrite_code, ERROR_MARK,
7262 : : arglist, lookups, flags, complain);
7263 : 1135767 : if (r == error_mark_node)
7264 : : return error_mark_node;
7265 : : }
7266 : :
7267 : 2521909 : z_candidate *save_cand = *candidates;
7268 : :
7269 : : /* Add rewritten candidates in reverse order. */
7270 : 2521909 : flags |= LOOKUP_REVERSED;
7271 : 2521909 : vec<tree,va_gc> *revlist = make_tree_vector ();
7272 : 2521909 : revlist->quick_push ((*arglist)[1]);
7273 : 2521909 : revlist->quick_push ((*arglist)[0]);
7274 : 2521909 : r = add_operator_candidates (candidates, rewrite_code, ERROR_MARK,
7275 : : revlist, lookups, flags, complain);
7276 : 2521909 : if (r == error_mark_node)
7277 : : return error_mark_node;
7278 : :
7279 : : /* Release the vec if we didn't add a candidate that uses it. */
7280 : 2565746 : for (z_candidate *c = *candidates; c != save_cand; c = c->next)
7281 : 1138972 : if (c->args == revlist)
7282 : : {
7283 : : revlist = NULL;
7284 : : break;
7285 : : }
7286 : 2521909 : release_tree_vector (revlist);
7287 : : }
7288 : : }
7289 : :
7290 : : return NULL_TREE;
7291 : : }
7292 : :
7293 : : tree
7294 : 172981212 : build_new_op (const op_location_t &loc, enum tree_code code, int flags,
7295 : : tree arg1, tree arg2, tree arg3, tree lookups,
7296 : : tree *overload, tsubst_flags_t complain)
7297 : : {
7298 : 172981212 : struct z_candidate *candidates = 0, *cand;
7299 : 172981212 : releasing_vec arglist;
7300 : 172981212 : tree result = NULL_TREE;
7301 : 172981212 : bool result_valid_p = false;
7302 : 172981212 : enum tree_code code2 = ERROR_MARK;
7303 : 172981212 : enum tree_code code_orig_arg1 = ERROR_MARK;
7304 : 172981212 : enum tree_code code_orig_arg2 = ERROR_MARK;
7305 : 172981212 : bool strict_p;
7306 : 172981212 : bool any_viable_p;
7307 : :
7308 : 172981212 : auto_cond_timevar tv (TV_OVERLOAD);
7309 : :
7310 : 172981212 : if (error_operand_p (arg1)
7311 : 172977694 : || error_operand_p (arg2)
7312 : 345952892 : || error_operand_p (arg3))
7313 : 9532 : return error_mark_node;
7314 : :
7315 : 172971680 : conversion_obstack_sentinel cos;
7316 : :
7317 : 172971680 : bool ismodop = code == MODIFY_EXPR;
7318 : 172971680 : if (ismodop)
7319 : : {
7320 : 8172365 : code2 = TREE_CODE (arg3);
7321 : 8172365 : arg3 = NULL_TREE;
7322 : : }
7323 : :
7324 : 172971680 : tree arg1_type = unlowered_expr_type (arg1);
7325 : 172971680 : tree arg2_type = arg2 ? unlowered_expr_type (arg2) : NULL_TREE;
7326 : :
7327 : 172971680 : arg1 = prep_operand (arg1);
7328 : :
7329 : 172971680 : switch (code)
7330 : : {
7331 : 0 : case NEW_EXPR:
7332 : 0 : case VEC_NEW_EXPR:
7333 : 0 : case VEC_DELETE_EXPR:
7334 : 0 : case DELETE_EXPR:
7335 : : /* Use build_operator_new_call and build_op_delete_call instead. */
7336 : 0 : gcc_unreachable ();
7337 : :
7338 : 0 : case CALL_EXPR:
7339 : : /* Use build_op_call instead. */
7340 : 0 : gcc_unreachable ();
7341 : :
7342 : 14008256 : case TRUTH_ORIF_EXPR:
7343 : 14008256 : case TRUTH_ANDIF_EXPR:
7344 : 14008256 : case TRUTH_AND_EXPR:
7345 : 14008256 : case TRUTH_OR_EXPR:
7346 : : /* These are saved for the sake of warn_logical_operator. */
7347 : 14008256 : code_orig_arg1 = TREE_CODE (arg1);
7348 : 14008256 : code_orig_arg2 = TREE_CODE (arg2);
7349 : 14008256 : break;
7350 : 38008342 : case GT_EXPR:
7351 : 38008342 : case LT_EXPR:
7352 : 38008342 : case GE_EXPR:
7353 : 38008342 : case LE_EXPR:
7354 : 38008342 : case EQ_EXPR:
7355 : 38008342 : case NE_EXPR:
7356 : : /* These are saved for the sake of maybe_warn_bool_compare. */
7357 : 38008342 : code_orig_arg1 = TREE_CODE (arg1_type);
7358 : 38008342 : code_orig_arg2 = TREE_CODE (arg2_type);
7359 : 38008342 : break;
7360 : :
7361 : : default:
7362 : : break;
7363 : : }
7364 : :
7365 : 172971680 : arg2 = prep_operand (arg2);
7366 : 172971680 : arg3 = prep_operand (arg3);
7367 : :
7368 : 172971680 : if (code == COND_EXPR)
7369 : : /* Use build_conditional_expr instead. */
7370 : 0 : gcc_unreachable ();
7371 : 172971680 : else if (! OVERLOAD_TYPE_P (arg1_type)
7372 : 328986637 : && (! arg2 || ! OVERLOAD_TYPE_P (arg2_type)))
7373 : 155713143 : goto builtin;
7374 : :
7375 : 17258537 : if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
7376 : : {
7377 : 208582 : arg2 = integer_zero_node;
7378 : 208582 : arg2_type = integer_type_node;
7379 : : }
7380 : :
7381 : 17258537 : arglist->quick_push (arg1);
7382 : 17258537 : if (arg2 != NULL_TREE)
7383 : 13277409 : arglist->quick_push (arg2);
7384 : 17258537 : if (arg3 != NULL_TREE)
7385 : 0 : arglist->quick_push (arg3);
7386 : :
7387 : 17258537 : result = add_operator_candidates (&candidates, code, code2, arglist,
7388 : : lookups, flags, complain);
7389 : 17258504 : if (result == error_mark_node)
7390 : : return error_mark_node;
7391 : :
7392 : 17258492 : switch (code)
7393 : : {
7394 : : case COMPOUND_EXPR:
7395 : : case ADDR_EXPR:
7396 : : /* For these, the built-in candidates set is empty
7397 : : [over.match.oper]/3. We don't want non-strict matches
7398 : : because exact matches are always possible with built-in
7399 : : operators. The built-in candidate set for COMPONENT_REF
7400 : : would be empty too, but since there are no such built-in
7401 : : operators, we accept non-strict matches for them. */
7402 : : strict_p = true;
7403 : : break;
7404 : :
7405 : 15617928 : default:
7406 : 15617928 : strict_p = false;
7407 : 15617928 : break;
7408 : : }
7409 : :
7410 : 17258492 : candidates = splice_viable (candidates, strict_p, &any_viable_p);
7411 : 17258492 : if (!any_viable_p)
7412 : : {
7413 : 1659975 : switch (code)
7414 : : {
7415 : 88 : case POSTINCREMENT_EXPR:
7416 : 88 : case POSTDECREMENT_EXPR:
7417 : : /* Don't try anything fancy if we're not allowed to produce
7418 : : errors. */
7419 : 88 : if (!(complain & tf_error))
7420 : : return error_mark_node;
7421 : :
7422 : : /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
7423 : : distinguish between prefix and postfix ++ and
7424 : : operator++() was used for both, so we allow this with
7425 : : -fpermissive. */
7426 : : else
7427 : : {
7428 : 43 : tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
7429 : 31 : const char *msg = (flag_permissive)
7430 : 43 : ? G_("no %<%D(int)%> declared for postfix %qs,"
7431 : : " trying prefix operator instead")
7432 : : : G_("no %<%D(int)%> declared for postfix %qs");
7433 : 43 : permerror (loc, msg, fnname, OVL_OP_INFO (false, code)->name);
7434 : : }
7435 : :
7436 : 43 : if (!flag_permissive)
7437 : 31 : return error_mark_node;
7438 : :
7439 : 12 : if (code == POSTINCREMENT_EXPR)
7440 : : code = PREINCREMENT_EXPR;
7441 : : else
7442 : 0 : code = PREDECREMENT_EXPR;
7443 : 12 : result = build_new_op (loc, code, flags, arg1, NULL_TREE,
7444 : : NULL_TREE, lookups, overload, complain);
7445 : 12 : break;
7446 : :
7447 : : /* The caller will deal with these. */
7448 : : case ADDR_EXPR:
7449 : : case COMPOUND_EXPR:
7450 : : case COMPONENT_REF:
7451 : : case CO_AWAIT_EXPR:
7452 : : result = NULL_TREE;
7453 : : result_valid_p = true;
7454 : : break;
7455 : :
7456 : 15486 : default:
7457 : 15486 : if (complain & tf_error)
7458 : : {
7459 : : /* If one of the arguments of the operator represents
7460 : : an invalid use of member function pointer, try to report
7461 : : a meaningful error ... */
7462 : 1337 : if (invalid_nonstatic_memfn_p (loc, arg1, tf_error)
7463 : 1334 : || invalid_nonstatic_memfn_p (loc, arg2, tf_error)
7464 : 2665 : || invalid_nonstatic_memfn_p (loc, arg3, tf_error))
7465 : : /* We displayed the error message. */;
7466 : : else
7467 : : {
7468 : : /* ... Otherwise, report the more generic
7469 : : "no matching operator found" error */
7470 : 1328 : auto_diagnostic_group d;
7471 : 1328 : op_error (loc, code, code2, arg1, arg2, arg3, false);
7472 : 1328 : print_z_candidates (loc, candidates);
7473 : 1328 : }
7474 : : }
7475 : 15486 : result = error_mark_node;
7476 : 15486 : break;
7477 : : }
7478 : : }
7479 : : else
7480 : : {
7481 : 15598517 : cand = tourney (candidates, complain);
7482 : 15598517 : if (cand == 0)
7483 : : {
7484 : 172 : if (complain & tf_error)
7485 : : {
7486 : 127 : auto_diagnostic_group d;
7487 : 127 : op_error (loc, code, code2, arg1, arg2, arg3, true);
7488 : 127 : print_z_candidates (loc, candidates);
7489 : 127 : }
7490 : 172 : result = error_mark_node;
7491 : 172 : if (overload)
7492 : 153 : *overload = error_mark_node;
7493 : : }
7494 : 15598345 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
7495 : : {
7496 : 11971934 : if (overload)
7497 : : {
7498 : 9231513 : if (cand->rewritten ())
7499 : : /* build_min_non_dep_op_overload needs to know whether the
7500 : : candidate is rewritten/reversed. */
7501 : 735773 : *overload = build_tree_list (build_int_cst (integer_type_node,
7502 : 735773 : cand->flags),
7503 : : cand->fn);
7504 : : else
7505 : 8495740 : *overload = cand->fn;
7506 : : }
7507 : :
7508 : 11971934 : if (resolve_args (arglist, complain) == NULL)
7509 : 2 : result = error_mark_node;
7510 : : else
7511 : : {
7512 : 11971932 : tsubst_flags_t ocomplain = complain;
7513 : 11971932 : if (cand->rewritten ())
7514 : : /* We'll wrap this call in another one. */
7515 : 736000 : ocomplain &= ~tf_decltype;
7516 : 11971932 : if (cand->reversed ())
7517 : : {
7518 : : /* We swapped these in add_candidate, swap them back now. */
7519 : 16385 : std::swap (cand->convs[0], cand->convs[1]);
7520 : 16385 : if (cand->fn == current_function_decl)
7521 : 1 : warning_at (loc, 0, "in C++20 this comparison calls the "
7522 : : "current function recursively with reversed "
7523 : : "arguments");
7524 : : }
7525 : 11971932 : result = build_over_call (cand, LOOKUP_NORMAL, ocomplain);
7526 : : }
7527 : :
7528 : 22227070 : if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn))
7529 : : /* There won't be a CALL_EXPR. */;
7530 : 10255124 : else if (result && result != error_mark_node)
7531 : : {
7532 : 10253474 : tree call = extract_call_expr (result);
7533 : 10253474 : CALL_EXPR_OPERATOR_SYNTAX (call) = true;
7534 : :
7535 : : /* Specify evaluation order as per P0145R2. */
7536 : 10253474 : CALL_EXPR_ORDERED_ARGS (call) = false;
7537 : 10253474 : switch (op_is_ordered (code))
7538 : : {
7539 : 2311316 : case -1:
7540 : 2311316 : CALL_EXPR_REVERSE_ARGS (call) = true;
7541 : 2311316 : break;
7542 : :
7543 : 550945 : case 1:
7544 : 550945 : CALL_EXPR_ORDERED_ARGS (call) = true;
7545 : 550945 : break;
7546 : :
7547 : : default:
7548 : : break;
7549 : : }
7550 : : }
7551 : :
7552 : : /* If this was a C++20 rewritten comparison, adjust the result. */
7553 : 11971931 : if (cand->rewritten ())
7554 : : {
7555 : 736000 : switch (code)
7556 : : {
7557 : 8315 : case EQ_EXPR:
7558 : 8315 : gcc_checking_assert (cand->reversed ());
7559 : 359427 : gcc_fallthrough ();
7560 : 359427 : case NE_EXPR:
7561 : 359427 : if (result == error_mark_node)
7562 : : ;
7563 : : /* If a rewritten operator== candidate is selected by
7564 : : overload resolution for an operator @, its return type
7565 : : shall be cv bool.... */
7566 : 359423 : else if (TREE_CODE (TREE_TYPE (result)) != BOOLEAN_TYPE)
7567 : : {
7568 : 44 : if (complain & tf_error)
7569 : : {
7570 : 6 : auto_diagnostic_group d;
7571 : 6 : error_at (loc, "return type of %qD is not %qs",
7572 : : cand->fn, "bool");
7573 : 6 : inform (loc, "used as rewritten candidate for "
7574 : : "comparison of %qT and %qT",
7575 : : arg1_type, arg2_type);
7576 : 6 : }
7577 : 44 : result = error_mark_node;
7578 : : }
7579 : 359379 : else if (code == NE_EXPR)
7580 : : /* !(y == x) or !(x == y) */
7581 : 351089 : result = build1_loc (loc, TRUTH_NOT_EXPR,
7582 : : boolean_type_node, result);
7583 : : break;
7584 : :
7585 : : /* If a rewritten operator<=> candidate is selected by
7586 : : overload resolution for an operator @, x @ y is
7587 : : interpreted as 0 @ (y <=> x) if the selected candidate is
7588 : : a synthesized candidate with reversed order of parameters,
7589 : : or (x <=> y) @ 0 otherwise, using the selected rewritten
7590 : : operator<=> candidate. */
7591 : 454 : case SPACESHIP_EXPR:
7592 : 454 : if (!cand->reversed ())
7593 : : /* We're in the build_new_op call below for an outer
7594 : : reversed call; we don't need to do anything more. */
7595 : : break;
7596 : 376346 : gcc_fallthrough ();
7597 : 376346 : case LT_EXPR:
7598 : 376346 : case LE_EXPR:
7599 : 376346 : case GT_EXPR:
7600 : 376346 : case GE_EXPR:
7601 : 376346 : {
7602 : 376346 : tree lhs = result;
7603 : 376346 : tree rhs = integer_zero_node;
7604 : 376346 : if (cand->reversed ())
7605 : 943 : std::swap (lhs, rhs);
7606 : 376346 : warning_sentinel ws (warn_zero_as_null_pointer_constant);
7607 : 376346 : result = build_new_op (loc, code,
7608 : : LOOKUP_NORMAL|LOOKUP_REWRITTEN,
7609 : : lhs, rhs, NULL_TREE, lookups,
7610 : : NULL, complain);
7611 : 376346 : }
7612 : 376346 : break;
7613 : :
7614 : 0 : default:
7615 : 0 : gcc_unreachable ();
7616 : : }
7617 : : }
7618 : :
7619 : : /* In an expression of the form `a[]' where cand->fn
7620 : : which is operator[] turns out to be a static member function,
7621 : : `a' is none-the-less evaluated. */
7622 : 11971931 : if (code == ARRAY_REF)
7623 : 500739 : result = keep_unused_object_arg (result, arg1, cand->fn);
7624 : : }
7625 : : else
7626 : : {
7627 : : /* Give any warnings we noticed during overload resolution. */
7628 : 3626411 : if (cand->warnings && (complain & tf_warning))
7629 : : {
7630 : : struct candidate_warning *w;
7631 : 0 : for (w = cand->warnings; w; w = w->next)
7632 : 0 : joust (cand, w->loser, 1, complain);
7633 : : }
7634 : :
7635 : : /* Check for comparison of different enum types. */
7636 : 3626411 : switch (code)
7637 : : {
7638 : 2784330 : case GT_EXPR:
7639 : 2784330 : case LT_EXPR:
7640 : 2784330 : case GE_EXPR:
7641 : 2784330 : case LE_EXPR:
7642 : 2784330 : case EQ_EXPR:
7643 : 2784330 : case NE_EXPR:
7644 : 2784330 : if (TREE_CODE (arg1_type) == ENUMERAL_TYPE
7645 : 2648069 : && TREE_CODE (arg2_type) == ENUMERAL_TYPE
7646 : 5352104 : && (TYPE_MAIN_VARIANT (arg1_type)
7647 : 2567774 : != TYPE_MAIN_VARIANT (arg2_type)))
7648 : : {
7649 : 79 : if (cxx_dialect >= cxx26
7650 : 24 : && (complain & tf_warning_or_error) == 0)
7651 : 1 : result = error_mark_node;
7652 : 78 : else if (cxx_dialect >= cxx26 || (complain & tf_warning))
7653 : 129 : emit_diagnostic ((cxx_dialect >= cxx26
7654 : : ? diagnostics::kind::pedwarn
7655 : : : diagnostics::kind::warning),
7656 : 76 : loc, OPT_Wenum_compare,
7657 : : "comparison between %q#T and %q#T",
7658 : : arg1_type, arg2_type);
7659 : : }
7660 : : break;
7661 : : default:
7662 : : break;
7663 : : }
7664 : :
7665 : : /* "If a built-in candidate is selected by overload resolution, the
7666 : : operands of class type are converted to the types of the
7667 : : corresponding parameters of the selected operation function,
7668 : : except that the second standard conversion sequence of a
7669 : : user-defined conversion sequence (12.3.3.1.2) is not applied." */
7670 : 3626411 : conversion *conv = cand->convs[0];
7671 : 3626411 : if (conv->user_conv_p)
7672 : : {
7673 : 28453 : conv = strip_standard_conversion (conv);
7674 : 28453 : arg1 = convert_like (conv, arg1, complain);
7675 : : }
7676 : :
7677 : 3626411 : if (arg2)
7678 : : {
7679 : 3129455 : conv = cand->convs[1];
7680 : 3129455 : if (conv->user_conv_p)
7681 : : {
7682 : 8023 : conv = strip_standard_conversion (conv);
7683 : 8023 : arg2 = convert_like (conv, arg2, complain);
7684 : : }
7685 : : }
7686 : :
7687 : 3626411 : if (arg3)
7688 : : {
7689 : 0 : conv = cand->convs[2];
7690 : 0 : if (conv->user_conv_p)
7691 : : {
7692 : 0 : conv = strip_standard_conversion (conv);
7693 : 0 : arg3 = convert_like (conv, arg3, complain);
7694 : : }
7695 : : }
7696 : : }
7697 : : }
7698 : :
7699 : 17258413 : if (result || result_valid_p)
7700 : : return result;
7701 : :
7702 : 3626410 : builtin:
7703 : 159339553 : switch (code)
7704 : : {
7705 : 4112191 : case MODIFY_EXPR:
7706 : 4112191 : return cp_build_modify_expr (loc, arg1, code2, arg2, complain);
7707 : :
7708 : 16454312 : case INDIRECT_REF:
7709 : 16454312 : return cp_build_indirect_ref (loc, arg1, RO_UNARY_STAR, complain);
7710 : :
7711 : 14007685 : case TRUTH_ANDIF_EXPR:
7712 : 14007685 : case TRUTH_ORIF_EXPR:
7713 : 14007685 : case TRUTH_AND_EXPR:
7714 : 14007685 : case TRUTH_OR_EXPR:
7715 : 14007685 : if ((complain & tf_warning) && !processing_template_decl)
7716 : 5599300 : warn_logical_operator (loc, code, boolean_type_node,
7717 : : code_orig_arg1, arg1,
7718 : : code_orig_arg2, arg2);
7719 : : /* Fall through. */
7720 : 47096779 : case GT_EXPR:
7721 : 47096779 : case LT_EXPR:
7722 : 47096779 : case GE_EXPR:
7723 : 47096779 : case LE_EXPR:
7724 : 47096779 : case EQ_EXPR:
7725 : 47096779 : case NE_EXPR:
7726 : 47096779 : if ((complain & tf_warning)
7727 : 44586889 : && ((code_orig_arg1 == BOOLEAN_TYPE)
7728 : 44586889 : ^ (code_orig_arg2 == BOOLEAN_TYPE)))
7729 : 12067 : maybe_warn_bool_compare (loc, code, arg1, arg2);
7730 : 44586889 : if (complain & tf_warning && warn_tautological_compare)
7731 : 393720 : warn_tautological_cmp (loc, code, arg1, arg2);
7732 : : /* Fall through. */
7733 : 108815499 : case SPACESHIP_EXPR:
7734 : 108815499 : case PLUS_EXPR:
7735 : 108815499 : case MINUS_EXPR:
7736 : 108815499 : case MULT_EXPR:
7737 : 108815499 : case TRUNC_DIV_EXPR:
7738 : 108815499 : case MAX_EXPR:
7739 : 108815499 : case MIN_EXPR:
7740 : 108815499 : case LSHIFT_EXPR:
7741 : 108815499 : case RSHIFT_EXPR:
7742 : 108815499 : case TRUNC_MOD_EXPR:
7743 : 108815499 : case BIT_AND_EXPR:
7744 : 108815499 : case BIT_IOR_EXPR:
7745 : 108815499 : case BIT_XOR_EXPR:
7746 : 108815499 : return cp_build_binary_op (loc, code, arg1, arg2, complain);
7747 : :
7748 : 25837858 : case UNARY_PLUS_EXPR:
7749 : 25837858 : case NEGATE_EXPR:
7750 : 25837858 : case BIT_NOT_EXPR:
7751 : 25837858 : case TRUTH_NOT_EXPR:
7752 : 25837858 : case PREINCREMENT_EXPR:
7753 : 25837858 : case POSTINCREMENT_EXPR:
7754 : 25837858 : case PREDECREMENT_EXPR:
7755 : 25837858 : case POSTDECREMENT_EXPR:
7756 : 25837858 : case REALPART_EXPR:
7757 : 25837858 : case IMAGPART_EXPR:
7758 : 25837858 : case ABS_EXPR:
7759 : 25837858 : case CO_AWAIT_EXPR:
7760 : 25837858 : return cp_build_unary_op (code, arg1, false, complain);
7761 : :
7762 : 1720191 : case ARRAY_REF:
7763 : 1720191 : return cp_build_array_ref (input_location, arg1, arg2, complain);
7764 : :
7765 : 753 : case MEMBER_REF:
7766 : 753 : return build_m_component_ref (cp_build_indirect_ref (loc, arg1,
7767 : : RO_ARROW_STAR,
7768 : : complain),
7769 : 753 : arg2, complain);
7770 : :
7771 : : /* The caller will deal with these. */
7772 : : case ADDR_EXPR:
7773 : : case COMPONENT_REF:
7774 : : case COMPOUND_EXPR:
7775 : : return NULL_TREE;
7776 : :
7777 : 0 : default:
7778 : 0 : gcc_unreachable ();
7779 : : }
7780 : : return NULL_TREE;
7781 : 172981176 : }
7782 : :
7783 : : /* Build a new call to operator[]. This may change ARGS. */
7784 : :
7785 : : tree
7786 : 229 : build_op_subscript (const op_location_t &loc, tree obj,
7787 : : vec<tree, va_gc> **args, tree *overload,
7788 : : tsubst_flags_t complain)
7789 : : {
7790 : 229 : struct z_candidate *candidates = 0, *cand;
7791 : 229 : tree fns, first_mem_arg = NULL_TREE;
7792 : 229 : bool any_viable_p;
7793 : 229 : tree result = NULL_TREE;
7794 : :
7795 : 229 : auto_cond_timevar tv (TV_OVERLOAD);
7796 : :
7797 : 229 : obj = mark_lvalue_use (obj);
7798 : :
7799 : 229 : if (error_operand_p (obj))
7800 : 0 : return error_mark_node;
7801 : :
7802 : 229 : tree type = TREE_TYPE (obj);
7803 : :
7804 : 229 : obj = prep_operand (obj);
7805 : :
7806 : 229 : if (TYPE_BINFO (type))
7807 : : {
7808 : 229 : fns = lookup_fnfields (TYPE_BINFO (type), ovl_op_identifier (ARRAY_REF),
7809 : : 1, complain);
7810 : 229 : if (fns == error_mark_node)
7811 : : return error_mark_node;
7812 : : }
7813 : : else
7814 : : fns = NULL_TREE;
7815 : :
7816 : 229 : if (args != NULL && *args != NULL)
7817 : : {
7818 : 229 : *args = resolve_args (*args, complain);
7819 : 229 : if (*args == NULL)
7820 : 0 : return error_mark_node;
7821 : : }
7822 : :
7823 : 229 : conversion_obstack_sentinel cos;
7824 : :
7825 : 229 : if (fns)
7826 : : {
7827 : 227 : first_mem_arg = obj;
7828 : :
7829 : 227 : add_candidates (BASELINK_FUNCTIONS (fns),
7830 : : first_mem_arg, *args, NULL_TREE,
7831 : : NULL_TREE, false,
7832 : 227 : BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
7833 : : LOOKUP_NORMAL, &candidates, complain);
7834 : : }
7835 : :
7836 : : /* Be strict here because if we choose a bad conversion candidate, the
7837 : : errors we get won't mention the call context. */
7838 : 229 : candidates = splice_viable (candidates, true, &any_viable_p);
7839 : 229 : if (!any_viable_p)
7840 : : {
7841 : 33 : if (complain & tf_error)
7842 : : {
7843 : 1 : auto_diagnostic_group d;
7844 : 2 : error ("no match for call to %<%T::operator[] (%A)%>",
7845 : 1 : TREE_TYPE (obj), build_tree_list_vec (*args));
7846 : 1 : print_z_candidates (loc, candidates);
7847 : 1 : }
7848 : 33 : result = error_mark_node;
7849 : : }
7850 : : else
7851 : : {
7852 : 196 : cand = tourney (candidates, complain);
7853 : 196 : if (cand == 0)
7854 : : {
7855 : 0 : if (complain & tf_error)
7856 : : {
7857 : 0 : auto_diagnostic_group d;
7858 : 0 : error ("call of %<%T::operator[] (%A)%> is ambiguous",
7859 : 0 : TREE_TYPE (obj), build_tree_list_vec (*args));
7860 : 0 : print_z_candidates (loc, candidates);
7861 : 0 : }
7862 : 0 : result = error_mark_node;
7863 : : }
7864 : 196 : else if (TREE_CODE (cand->fn) == FUNCTION_DECL
7865 : 196 : && DECL_OVERLOADED_OPERATOR_P (cand->fn)
7866 : 392 : && DECL_OVERLOADED_OPERATOR_IS (cand->fn, ARRAY_REF))
7867 : : {
7868 : 196 : if (overload)
7869 : 196 : *overload = cand->fn;
7870 : 196 : result = build_over_call (cand, LOOKUP_NORMAL, complain);
7871 : 392 : if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn))
7872 : : /* There won't be a CALL_EXPR. */;
7873 : 196 : else if (result && result != error_mark_node)
7874 : : {
7875 : 196 : tree call = extract_call_expr (result);
7876 : 196 : CALL_EXPR_OPERATOR_SYNTAX (call) = true;
7877 : :
7878 : : /* Specify evaluation order as per P0145R2. */
7879 : 196 : CALL_EXPR_ORDERED_ARGS (call) = op_is_ordered (ARRAY_REF) == 1;
7880 : : }
7881 : :
7882 : : /* In an expression of the form `a[]' where cand->fn
7883 : : which is operator[] turns out to be a static member function,
7884 : : `a' is none-the-less evaluated. */
7885 : 196 : result = keep_unused_object_arg (result, obj, cand->fn);
7886 : : }
7887 : : else
7888 : 0 : gcc_unreachable ();
7889 : : }
7890 : :
7891 : 229 : return result;
7892 : 229 : }
7893 : :
7894 : : /* CALL was returned by some call-building function; extract the actual
7895 : : CALL_EXPR from any bits that have been tacked on, e.g. by
7896 : : convert_from_reference. */
7897 : :
7898 : : tree
7899 : 22372490 : extract_call_expr (tree call)
7900 : : {
7901 : 22372651 : while (TREE_CODE (call) == COMPOUND_EXPR)
7902 : 161 : call = TREE_OPERAND (call, 1);
7903 : 22372490 : if (REFERENCE_REF_P (call))
7904 : 7494949 : call = TREE_OPERAND (call, 0);
7905 : 22372490 : if (TREE_CODE (call) == TARGET_EXPR)
7906 : 1254010 : call = TARGET_EXPR_INITIAL (call);
7907 : 22372490 : if (cxx_dialect >= cxx20)
7908 : 10755451 : switch (TREE_CODE (call))
7909 : : {
7910 : : /* C++20 rewritten comparison operators. */
7911 : 5196 : case TRUTH_NOT_EXPR:
7912 : 5196 : call = TREE_OPERAND (call, 0);
7913 : 5196 : break;
7914 : 14841 : case LT_EXPR:
7915 : 14841 : case LE_EXPR:
7916 : 14841 : case GT_EXPR:
7917 : 14841 : case GE_EXPR:
7918 : 14841 : case SPACESHIP_EXPR:
7919 : 14841 : {
7920 : 14841 : tree op0 = TREE_OPERAND (call, 0);
7921 : 14841 : if (integer_zerop (op0))
7922 : 432 : call = TREE_OPERAND (call, 1);
7923 : : else
7924 : : call = op0;
7925 : : }
7926 : : break;
7927 : : default:;
7928 : : }
7929 : :
7930 : 22372490 : if (TREE_CODE (call) != CALL_EXPR
7931 : 719968 : && TREE_CODE (call) != AGGR_INIT_EXPR
7932 : 617390 : && call != error_mark_node)
7933 : 617372 : return NULL_TREE;
7934 : : return call;
7935 : : }
7936 : :
7937 : : /* Returns true if FN has two parameters, of which the second has type
7938 : : size_t. */
7939 : :
7940 : : static bool
7941 : 260820 : second_parm_is_size_t (tree fn)
7942 : : {
7943 : 260820 : tree t = FUNCTION_ARG_CHAIN (fn);
7944 : 260820 : if (!t || !same_type_p (TREE_VALUE (t), size_type_node))
7945 : 260805 : return false;
7946 : 15 : t = TREE_CHAIN (t);
7947 : 15 : if (t == void_list_node)
7948 : : return true;
7949 : : return false;
7950 : : }
7951 : :
7952 : : /* True if T, an allocation function, has std::align_val_t as its second
7953 : : argument. */
7954 : :
7955 : : bool
7956 : 326974 : aligned_allocation_fn_p (tree t)
7957 : : {
7958 : 326974 : if (!aligned_new_threshold)
7959 : : return false;
7960 : :
7961 : 317364 : tree a = FUNCTION_ARG_CHAIN (t);
7962 : 317364 : return (a && same_type_p (TREE_VALUE (a), align_type_node));
7963 : : }
7964 : :
7965 : : /* True if T is std::destroying_delete_t. */
7966 : :
7967 : : static bool
7968 : 4350059 : std_destroying_delete_t_p (tree t)
7969 : : {
7970 : 4350059 : return (TYPE_CONTEXT (t) == std_node
7971 : 4350059 : && id_equal (TYPE_IDENTIFIER (t), "destroying_delete_t"));
7972 : : }
7973 : :
7974 : : /* A deallocation function with at least two parameters whose second parameter
7975 : : type is of type std::destroying_delete_t is a destroying operator delete. A
7976 : : destroying operator delete shall be a class member function named operator
7977 : : delete. [ Note: Array deletion cannot use a destroying operator
7978 : : delete. --end note ] */
7979 : :
7980 : : tree
7981 : 4350074 : destroying_delete_p (tree t)
7982 : : {
7983 : 4350074 : tree a = TYPE_ARG_TYPES (TREE_TYPE (t));
7984 : 4350074 : if (!a || !TREE_CHAIN (a))
7985 : : return NULL_TREE;
7986 : 4350059 : tree type = TREE_VALUE (TREE_CHAIN (a));
7987 : 4350059 : return std_destroying_delete_t_p (type) ? type : NULL_TREE;
7988 : : }
7989 : :
7990 : : struct dealloc_info
7991 : : {
7992 : : bool sized;
7993 : : bool aligned;
7994 : : tree destroying;
7995 : : };
7996 : :
7997 : : /* Returns true iff T, an element of an OVERLOAD chain, is a usual deallocation
7998 : : function (3.7.4.2 [basic.stc.dynamic.deallocation]). If so, and DI is
7999 : : non-null, also set *DI. */
8000 : :
8001 : : static bool
8002 : 3118023 : usual_deallocation_fn_p (tree t, dealloc_info *di)
8003 : : {
8004 : 3118023 : if (di) *di = dealloc_info();
8005 : :
8006 : : /* A template instance is never a usual deallocation function,
8007 : : regardless of its signature. */
8008 : 3118023 : if (TREE_CODE (t) == TEMPLATE_DECL
8009 : 3118023 : || primary_template_specialization_p (t))
8010 : 12 : return false;
8011 : :
8012 : : /* A usual deallocation function is a deallocation function whose parameters
8013 : : after the first are
8014 : : - optionally, a parameter of type std::destroying_delete_t, then
8015 : : - optionally, a parameter of type std::size_t, then
8016 : : - optionally, a parameter of type std::align_val_t. */
8017 : 3118011 : bool global = DECL_NAMESPACE_SCOPE_P (t);
8018 : 3118011 : tree chain = FUNCTION_ARG_CHAIN (t);
8019 : 3118011 : if (chain && destroying_delete_p (t))
8020 : : {
8021 : 81 : if (di) di->destroying = TREE_VALUE (chain);
8022 : 81 : chain = TREE_CHAIN (chain);
8023 : : }
8024 : 3118011 : if (chain
8025 : 3118008 : && (!global || flag_sized_deallocation)
8026 : 6220929 : && same_type_p (TREE_VALUE (chain), size_type_node))
8027 : : {
8028 : 900152 : if (di) di->sized = true;
8029 : 900152 : chain = TREE_CHAIN (chain);
8030 : : }
8031 : 3118008 : if (chain && aligned_new_threshold
8032 : 6220003 : && same_type_p (TREE_VALUE (chain), align_type_node))
8033 : : {
8034 : 1333625 : if (di) di->aligned = true;
8035 : 1333625 : chain = TREE_CHAIN (chain);
8036 : : }
8037 : 3118011 : return (chain == void_list_node);
8038 : : }
8039 : :
8040 : : /* Just return whether FN is a usual deallocation function. */
8041 : :
8042 : : bool
8043 : 8541 : usual_deallocation_fn_p (tree fn)
8044 : : {
8045 : 8541 : return usual_deallocation_fn_p (fn, NULL);
8046 : : }
8047 : :
8048 : : /* Build a call to operator delete. This has to be handled very specially,
8049 : : because the restrictions on what signatures match are different from all
8050 : : other call instances. For a normal delete, only a delete taking (void *)
8051 : : or (void *, size_t) is accepted. For a placement delete, only an exact
8052 : : match with the placement new is accepted.
8053 : :
8054 : : CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
8055 : : ADDR is the pointer to be deleted.
8056 : : SIZE is the size of the memory block to be deleted.
8057 : : GLOBAL_P is true if the delete-expression should not consider
8058 : : class-specific delete operators.
8059 : : CORO_P is true if the allocation is for a coroutine, where the two argument
8060 : : usual deallocation should be chosen in preference to the single argument
8061 : : version in a class context.
8062 : : PLACEMENT is the corresponding placement new call, or NULL_TREE.
8063 : :
8064 : : If this call to "operator delete" is being generated as part to
8065 : : deallocate memory allocated via a new-expression (as per [expr.new]
8066 : : which requires that if the initialization throws an exception then
8067 : : we call a deallocation function), then ALLOC_FN is the allocation
8068 : : function. */
8069 : :
8070 : : static tree
8071 : 716638 : build_op_delete_call_1 (enum tree_code code, tree addr, tree size,
8072 : : bool global_p, bool coro_p, tree placement,
8073 : : tree alloc_fn, tsubst_flags_t complain)
8074 : : {
8075 : 716638 : tree fn = NULL_TREE;
8076 : 716638 : tree fns, fnname, type, t;
8077 : 716638 : dealloc_info di_fn = { };
8078 : :
8079 : 716638 : if (addr == error_mark_node)
8080 : : return error_mark_node;
8081 : :
8082 : 716638 : type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
8083 : :
8084 : 716638 : fnname = ovl_op_identifier (false, code);
8085 : :
8086 : 624600 : if (CLASS_TYPE_P (type)
8087 : 624567 : && COMPLETE_TYPE_P (complete_type (type))
8088 : 1341182 : && !global_p)
8089 : : /* In [class.free]
8090 : :
8091 : : If the result of the lookup is ambiguous or inaccessible, or if
8092 : : the lookup selects a placement deallocation function, the
8093 : : program is ill-formed.
8094 : :
8095 : : Therefore, we ask lookup_fnfields to complain about ambiguity. */
8096 : : {
8097 : 415622 : fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1, complain);
8098 : 415622 : if (fns == error_mark_node)
8099 : : return error_mark_node;
8100 : : }
8101 : : else
8102 : : fns = NULL_TREE;
8103 : :
8104 : 415619 : if (fns == NULL_TREE)
8105 : 715579 : fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE);
8106 : :
8107 : : /* Strip const and volatile from addr. */
8108 : 716635 : tree oaddr = addr;
8109 : 716635 : addr = cp_convert (ptr_type_node, addr, complain);
8110 : :
8111 : 716635 : tree excluded_destroying = NULL_TREE;
8112 : :
8113 : 716635 : if (placement)
8114 : : {
8115 : : /* "A declaration of a placement deallocation function matches the
8116 : : declaration of a placement allocation function if it has the same
8117 : : number of parameters and, after parameter transformations (8.3.5),
8118 : : all parameter types except the first are identical."
8119 : :
8120 : : So we build up the function type we want and ask instantiate_type
8121 : : to get it for us. */
8122 : 261612 : t = FUNCTION_ARG_CHAIN (alloc_fn);
8123 : 261612 : t = tree_cons (NULL_TREE, ptr_type_node, t);
8124 : 261612 : t = build_function_type (void_type_node, t);
8125 : :
8126 : 261612 : fn = instantiate_type (t, fns, tf_none);
8127 : 261612 : if (fn == error_mark_node)
8128 : : return NULL_TREE;
8129 : :
8130 : 260820 : fn = MAYBE_BASELINK_FUNCTIONS (fn);
8131 : :
8132 : : /* "If the lookup finds the two-parameter form of a usual deallocation
8133 : : function (3.7.4.2) and that function, considered as a placement
8134 : : deallocation function, would have been selected as a match for the
8135 : : allocation function, the program is ill-formed." */
8136 : 260820 : if (second_parm_is_size_t (fn))
8137 : : {
8138 : 9 : const char *const msg1
8139 : : = G_("exception cleanup for this placement new selects "
8140 : : "non-placement %<operator delete%>");
8141 : 9 : const char *const msg2
8142 : : = G_("%qD is a usual (non-placement) deallocation "
8143 : : "function in C++14 (or with %<-fsized-deallocation%>)");
8144 : :
8145 : : /* But if the class has an operator delete (void *), then that is
8146 : : the usual deallocation function, so we shouldn't complain
8147 : : about using the operator delete (void *, size_t). */
8148 : 9 : if (DECL_CLASS_SCOPE_P (fn))
8149 : 18 : for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns)))
8150 : : {
8151 : 9 : if (usual_deallocation_fn_p (elt)
8152 : 9 : && FUNCTION_ARG_CHAIN (elt) == void_list_node)
8153 : 3 : goto ok;
8154 : : }
8155 : : /* Before C++14 a two-parameter global deallocation function is
8156 : : always a placement deallocation function, but warn if
8157 : : -Wc++14-compat. */
8158 : 3 : else if (!flag_sized_deallocation)
8159 : : {
8160 : 1 : if (complain & tf_warning)
8161 : : {
8162 : 1 : auto_diagnostic_group d;
8163 : 1 : if (warning (OPT_Wc__14_compat, msg1))
8164 : 1 : inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
8165 : 1 : }
8166 : 1 : goto ok;
8167 : : }
8168 : :
8169 : 5 : if (complain & tf_warning_or_error)
8170 : : {
8171 : 5 : auto_diagnostic_group d;
8172 : 5 : if (permerror (input_location, msg1))
8173 : : {
8174 : : /* Only mention C++14 for namespace-scope delete. */
8175 : 5 : if (DECL_NAMESPACE_SCOPE_P (fn))
8176 : 2 : inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
8177 : : else
8178 : 3 : inform (DECL_SOURCE_LOCATION (fn),
8179 : : "%qD is a usual (non-placement) deallocation "
8180 : : "function", fn);
8181 : : }
8182 : 5 : }
8183 : : else
8184 : 0 : return error_mark_node;
8185 : 715843 : ok:;
8186 : : }
8187 : : }
8188 : : else
8189 : : /* "Any non-placement deallocation function matches a non-placement
8190 : : allocation function. If the lookup finds a single matching
8191 : : deallocation function, that function will be called; otherwise, no
8192 : : deallocation function will be called." */
8193 : 4019528 : for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns)))
8194 : : {
8195 : 3109482 : dealloc_info di_elt;
8196 : 3109482 : if (usual_deallocation_fn_p (elt, &di_elt))
8197 : : {
8198 : : /* If we're called for an EH cleanup in a new-expression, we can't
8199 : : use a destroying delete; the exception was thrown before the
8200 : : object was constructed. */
8201 : 1798908 : if (alloc_fn && di_elt.destroying)
8202 : : {
8203 : 14 : excluded_destroying = elt;
8204 : 905211 : continue;
8205 : : }
8206 : :
8207 : 1798894 : if (!fn)
8208 : : {
8209 : 454997 : fn = elt;
8210 : 454997 : di_fn = di_elt;
8211 : 454997 : continue;
8212 : : }
8213 : :
8214 : : /* -- If any of the deallocation functions is a destroying
8215 : : operator delete, all deallocation functions that are not
8216 : : destroying operator deletes are eliminated from further
8217 : : consideration. */
8218 : 1343897 : if (di_elt.destroying != di_fn.destroying)
8219 : : {
8220 : 12 : if (di_elt.destroying)
8221 : : {
8222 : 6 : fn = elt;
8223 : 6 : di_fn = di_elt;
8224 : : }
8225 : 12 : continue;
8226 : : }
8227 : :
8228 : : /* -- If the type has new-extended alignment, a function with a
8229 : : parameter of type std::align_val_t is preferred; otherwise a
8230 : : function without such a parameter is preferred. If exactly one
8231 : : preferred function is found, that function is selected and the
8232 : : selection process terminates. If more than one preferred
8233 : : function is found, all non-preferred functions are eliminated
8234 : : from further consideration. */
8235 : 1343885 : if (aligned_new_threshold)
8236 : : {
8237 : 1343645 : bool want_align = type_has_new_extended_alignment (type);
8238 : 1343645 : if (di_elt.aligned != di_fn.aligned)
8239 : : {
8240 : 450188 : if (want_align == di_elt.aligned)
8241 : : {
8242 : 445563 : fn = elt;
8243 : 445563 : di_fn = di_elt;
8244 : : }
8245 : 450188 : continue;
8246 : : }
8247 : : }
8248 : :
8249 : : /* -- If the deallocation functions have class scope, the one
8250 : : without a parameter of type std::size_t is selected. */
8251 : 893697 : bool want_size;
8252 : 893697 : if (DECL_CLASS_SCOPE_P (fn) && !coro_p)
8253 : : want_size = false;
8254 : :
8255 : : /* -- If the type is complete and if, for the second alternative
8256 : : (delete array) only, the operand is a pointer to a class type
8257 : : with a non-trivial destructor or a (possibly multi-dimensional)
8258 : : array thereof, the function with a parameter of type std::size_t
8259 : : is selected.
8260 : :
8261 : : -- Otherwise, it is unspecified whether a deallocation function
8262 : : with a parameter of type std::size_t is selected. */
8263 : : else
8264 : : {
8265 : 893589 : want_size = COMPLETE_TYPE_P (type);
8266 : 893589 : if (code == VEC_DELETE_EXPR
8267 : 893589 : && !TYPE_VEC_NEW_USES_COOKIE (type))
8268 : : /* We need a cookie to determine the array size. */
8269 : : want_size = false;
8270 : : }
8271 : 893697 : gcc_assert (di_fn.sized != di_elt.sized);
8272 : 893697 : if (want_size == di_elt.sized)
8273 : : {
8274 : 30139 : fn = elt;
8275 : 30139 : di_fn = di_elt;
8276 : : }
8277 : : }
8278 : : }
8279 : :
8280 : : /* If we have a matching function, call it. */
8281 : 715843 : if (fn)
8282 : : {
8283 : 715817 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8284 : :
8285 : : /* If the FN is a member function, make sure that it is
8286 : : accessible. */
8287 : 715817 : if (BASELINK_P (fns))
8288 : 988 : perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn,
8289 : : complain);
8290 : :
8291 : : /* Core issue 901: It's ok to new a type with deleted delete. */
8292 : 715817 : if (DECL_DELETED_FN (fn) && alloc_fn)
8293 : : return NULL_TREE;
8294 : :
8295 : 715811 : tree ret;
8296 : 715811 : if (placement)
8297 : : {
8298 : : /* The placement args might not be suitable for overload
8299 : : resolution at this point, so build the call directly. */
8300 : 260820 : int nargs = call_expr_nargs (placement);
8301 : 260820 : tree *argarray = XALLOCAVEC (tree, nargs);
8302 : 260820 : int i;
8303 : 260820 : argarray[0] = addr;
8304 : 521691 : for (i = 1; i < nargs; i++)
8305 : 260871 : argarray[i] = CALL_EXPR_ARG (placement, i);
8306 : 260820 : if (!mark_used (fn, complain) && !(complain & tf_error))
8307 : 0 : return error_mark_node;
8308 : 260820 : ret = build_cxx_call (fn, nargs, argarray, complain);
8309 : : }
8310 : : else
8311 : : {
8312 : 454991 : tree destroying = di_fn.destroying;
8313 : 454991 : if (destroying)
8314 : : {
8315 : : /* Strip const and volatile from addr but retain the type of the
8316 : : object. */
8317 : 26 : tree rtype = TREE_TYPE (TREE_TYPE (oaddr));
8318 : 26 : rtype = cv_unqualified (rtype);
8319 : 26 : rtype = TYPE_POINTER_TO (rtype);
8320 : 26 : addr = cp_convert (rtype, oaddr, complain);
8321 : 26 : destroying = build_functional_cast (input_location,
8322 : : destroying, NULL_TREE,
8323 : : complain);
8324 : : }
8325 : :
8326 : 454991 : releasing_vec args;
8327 : 454991 : args->quick_push (addr);
8328 : 454991 : if (destroying)
8329 : 26 : args->quick_push (destroying);
8330 : 454991 : if (di_fn.sized)
8331 : 432177 : args->quick_push (size);
8332 : 454991 : if (di_fn.aligned)
8333 : : {
8334 : 2316 : tree al = build_int_cst (align_type_node, TYPE_ALIGN_UNIT (type));
8335 : 2316 : args->quick_push (al);
8336 : : }
8337 : 454991 : ret = cp_build_function_call_vec (fn, &args, complain);
8338 : 454991 : }
8339 : :
8340 : : /* Set this flag for all callers of this function. In addition to
8341 : : delete-expressions, this is called for deallocating coroutine state;
8342 : : treat that as an implicit delete-expression. This is also called for
8343 : : the delete if the constructor throws in a new-expression, and for a
8344 : : deleting destructor (which implements a delete-expression). */
8345 : : /* But leave this flag off for destroying delete to avoid wrong
8346 : : assumptions in the optimizers. */
8347 : 715811 : tree call = extract_call_expr (ret);
8348 : 715811 : if (TREE_CODE (call) == CALL_EXPR && !destroying_delete_p (fn))
8349 : 715779 : CALL_FROM_NEW_OR_DELETE_P (call) = 1;
8350 : :
8351 : 715811 : return ret;
8352 : : }
8353 : :
8354 : : /* If there's only a destroying delete that we can't use because the
8355 : : object isn't constructed yet, and we used global new, use global
8356 : : delete as well. */
8357 : 26 : if (excluded_destroying
8358 : 26 : && DECL_NAMESPACE_SCOPE_P (alloc_fn))
8359 : 8 : return build_op_delete_call (code, addr, size, true, placement,
8360 : 8 : alloc_fn, complain);
8361 : :
8362 : : /* [expr.new]
8363 : :
8364 : : If no unambiguous matching deallocation function can be found,
8365 : : propagating the exception does not cause the object's memory to
8366 : : be freed. */
8367 : 18 : if (alloc_fn)
8368 : : {
8369 : 15 : if ((complain & tf_warning)
8370 : 15 : && !placement)
8371 : : {
8372 : 15 : bool w = warning (0,
8373 : : "no corresponding deallocation function for %qD",
8374 : : alloc_fn);
8375 : 15 : if (w && excluded_destroying)
8376 : 3 : inform (DECL_SOURCE_LOCATION (excluded_destroying), "destroying "
8377 : : "delete %qD cannot be used to release the allocated memory"
8378 : : " if the initialization throws because the object is not "
8379 : : "constructed yet", excluded_destroying);
8380 : : }
8381 : 15 : return NULL_TREE;
8382 : : }
8383 : :
8384 : 3 : if (complain & tf_error)
8385 : 3 : error ("no suitable %<operator %s%> for %qT",
8386 : 3 : OVL_OP_INFO (false, code)->name, type);
8387 : 3 : return error_mark_node;
8388 : : }
8389 : :
8390 : : /* Arguments as per build_op_delete_call_1 (). */
8391 : :
8392 : : tree
8393 : 713467 : build_op_delete_call (enum tree_code code, tree addr, tree size, bool global_p,
8394 : : tree placement, tree alloc_fn, tsubst_flags_t complain)
8395 : : {
8396 : 713467 : return build_op_delete_call_1 (code, addr, size, global_p, /*coro_p*/false,
8397 : 713467 : placement, alloc_fn, complain);
8398 : : }
8399 : :
8400 : : /* Arguments as per build_op_delete_call_1 (). */
8401 : :
8402 : : tree
8403 : 3171 : build_coroutine_op_delete_call (enum tree_code code, tree addr, tree size,
8404 : : bool global_p, tree placement, tree alloc_fn,
8405 : : tsubst_flags_t complain)
8406 : : {
8407 : 3171 : return build_op_delete_call_1 (code, addr, size, global_p, /*coro_p*/true,
8408 : 3171 : placement, alloc_fn, complain);
8409 : : }
8410 : :
8411 : : /* Issue diagnostics about a disallowed access of DECL, using DIAG_DECL
8412 : : in the diagnostics.
8413 : :
8414 : : If ISSUE_ERROR is true, then issue an error about the access, followed
8415 : : by a note showing the declaration. Otherwise, just show the note.
8416 : :
8417 : : DIAG_DECL and DIAG_LOCATION will almost always be the same.
8418 : : DIAG_LOCATION is just another DECL. NO_ACCESS_REASON is an optional
8419 : : parameter used to specify why DECL wasn't accessible (e.g. ak_private
8420 : : would be because DECL was private). If not using NO_ACCESS_REASON,
8421 : : then it must be ak_none, and the access failure reason will be
8422 : : figured out by looking at the protection of DECL. */
8423 : :
8424 : : void
8425 : 1157 : complain_about_access (tree decl, tree diag_decl, tree diag_location,
8426 : : bool issue_error, access_kind no_access_reason)
8427 : : {
8428 : : /* If we have not already figured out why DECL is inaccessible... */
8429 : 1157 : if (no_access_reason == ak_none)
8430 : : {
8431 : : /* Examine the access of DECL to find out why. */
8432 : 978 : if (TREE_PRIVATE (decl))
8433 : : no_access_reason = ak_private;
8434 : 225 : else if (TREE_PROTECTED (decl))
8435 : : no_access_reason = ak_protected;
8436 : : }
8437 : :
8438 : : /* Now generate an error message depending on calculated access. */
8439 : 224 : if (no_access_reason == ak_private)
8440 : : {
8441 : 932 : if (issue_error)
8442 : 918 : error ("%q#D is private within this context", diag_decl);
8443 : 932 : inform (DECL_SOURCE_LOCATION (diag_location), "declared private here");
8444 : : }
8445 : 225 : else if (no_access_reason == ak_protected)
8446 : : {
8447 : 180 : if (issue_error)
8448 : 166 : error ("%q#D is protected within this context", diag_decl);
8449 : 180 : inform (DECL_SOURCE_LOCATION (diag_location), "declared protected here");
8450 : : }
8451 : : /* Couldn't figure out why DECL is inaccesible, so just say it's
8452 : : inaccessible. */
8453 : : else
8454 : : {
8455 : 45 : if (issue_error)
8456 : 45 : error ("%q#D is inaccessible within this context", diag_decl);
8457 : 45 : inform (DECL_SOURCE_LOCATION (diag_decl), "declared here");
8458 : : }
8459 : 1157 : }
8460 : :
8461 : : /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
8462 : : bitwise or of LOOKUP_* values. If any errors are warnings are
8463 : : generated, set *DIAGNOSTIC_FN to "error" or "warning",
8464 : : respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
8465 : : to NULL. */
8466 : :
8467 : : static tree
8468 : 10803855 : build_temp (tree expr, tree type, int flags,
8469 : : enum diagnostics::kind *diagnostic_kind, tsubst_flags_t complain)
8470 : : {
8471 : 10803855 : int savew, savee;
8472 : :
8473 : 10803855 : *diagnostic_kind = diagnostics::kind::unspecified;
8474 : :
8475 : : /* If the source is a packed field, calling the copy constructor will require
8476 : : binding the field to the reference parameter to the copy constructor, and
8477 : : we'll end up with an infinite loop. If we can use a bitwise copy, then
8478 : : do that now. */
8479 : 10803855 : if ((lvalue_kind (expr) & clk_packed)
8480 : 6 : && CLASS_TYPE_P (TREE_TYPE (expr))
8481 : 10803861 : && !type_has_nontrivial_copy_init (TREE_TYPE (expr)))
8482 : 3 : return get_target_expr (expr, complain);
8483 : :
8484 : : /* In decltype, we might have decided not to wrap this call in a TARGET_EXPR.
8485 : : But it turns out to be a subexpression, so perform temporary
8486 : : materialization now. */
8487 : 10803852 : if (TREE_CODE (expr) == CALL_EXPR
8488 : 3 : && CLASS_TYPE_P (type)
8489 : 10803855 : && same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
8490 : 3 : expr = build_cplus_new (type, expr, complain);
8491 : :
8492 : 10803852 : savew = warningcount + werrorcount, savee = errorcount;
8493 : 10803852 : releasing_vec args (make_tree_vector_single (expr));
8494 : 10803852 : expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
8495 : : &args, type, flags, complain);
8496 : 10803852 : if (warningcount + werrorcount > savew)
8497 : 0 : *diagnostic_kind = diagnostics::kind::warning;
8498 : 10803852 : else if (errorcount > savee)
8499 : 74 : *diagnostic_kind = diagnostics::kind::error;
8500 : 10803852 : return expr;
8501 : 10803852 : }
8502 : :
8503 : : /* Get any location for EXPR, falling back to input_location.
8504 : :
8505 : : If the result is in a system header and is the virtual location for
8506 : : a token coming from the expansion of a macro, unwind it to the
8507 : : location of the expansion point of the macro (e.g. to avoid the
8508 : : diagnostic being suppressed for expansions of NULL where "NULL" is
8509 : : in a system header). */
8510 : :
8511 : : static location_t
8512 : 1798009 : get_location_for_expr_unwinding_for_system_header (tree expr)
8513 : : {
8514 : 1798009 : location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
8515 : 1798009 : loc = expansion_point_location_if_in_system_header (loc);
8516 : 1798009 : return loc;
8517 : : }
8518 : :
8519 : : /* Perform warnings about peculiar, but valid, conversions from/to NULL.
8520 : : Also handle a subset of zero as null warnings.
8521 : : EXPR is implicitly converted to type TOTYPE.
8522 : : FN and ARGNUM are used for diagnostics. */
8523 : :
8524 : : static void
8525 : 416525297 : conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
8526 : : {
8527 : : /* Issue warnings about peculiar, but valid, uses of NULL. */
8528 : 416525297 : if (TREE_CODE (totype) != BOOLEAN_TYPE
8529 : 248647962 : && ARITHMETIC_TYPE_P (totype)
8530 : 551642758 : && null_node_p (expr))
8531 : : {
8532 : 94 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8533 : 94 : if (fn)
8534 : : {
8535 : 33 : auto_diagnostic_group d;
8536 : 33 : if (warning_at (loc, OPT_Wconversion_null,
8537 : : "passing NULL to non-pointer argument %P of %qD",
8538 : : argnum, fn))
8539 : 27 : inform (get_fndecl_argument_location (fn, argnum),
8540 : : "declared here");
8541 : 33 : }
8542 : : else
8543 : 61 : warning_at (loc, OPT_Wconversion_null,
8544 : : "converting to non-pointer type %qT from NULL", totype);
8545 : : }
8546 : :
8547 : : /* Issue warnings if "false" is converted to a NULL pointer */
8548 : 416525203 : else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
8549 : 416525203 : && TYPE_PTR_P (totype))
8550 : : {
8551 : 7 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8552 : 7 : if (fn)
8553 : : {
8554 : 3 : auto_diagnostic_group d;
8555 : 3 : if (warning_at (loc, OPT_Wconversion_null,
8556 : : "converting %<false%> to pointer type for argument "
8557 : : "%P of %qD", argnum, fn))
8558 : 3 : inform (get_fndecl_argument_location (fn, argnum),
8559 : : "declared here");
8560 : 3 : }
8561 : : else
8562 : 4 : warning_at (loc, OPT_Wconversion_null,
8563 : : "converting %<false%> to pointer type %qT", totype);
8564 : : }
8565 : : /* Handle zero as null pointer warnings for cases other
8566 : : than EQ_EXPR and NE_EXPR */
8567 : 381501480 : else if ((TYPE_PTR_OR_PTRMEM_P (totype) || NULLPTR_TYPE_P (totype))
8568 : 416809547 : && null_ptr_cst_p (expr))
8569 : : {
8570 : 1797908 : location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
8571 : 1797908 : maybe_warn_zero_as_null_pointer_constant (expr, loc);
8572 : : }
8573 : 416525297 : }
8574 : :
8575 : : /* We gave a diagnostic during a conversion. If this was in the second
8576 : : standard conversion sequence of a user-defined conversion sequence, say
8577 : : which user-defined conversion. */
8578 : :
8579 : : static void
8580 : 5214 : maybe_print_user_conv_context (conversion *convs)
8581 : : {
8582 : 5214 : if (convs->user_conv_p)
8583 : 160 : for (conversion *t = convs; t; t = next_conversion (t))
8584 : 136 : if (t->kind == ck_user)
8585 : : {
8586 : 41 : print_z_candidate (0, N_(" after user-defined conversion:"),
8587 : : t->cand);
8588 : 41 : break;
8589 : : }
8590 : 5214 : }
8591 : :
8592 : : /* Locate the parameter with the given index within FNDECL.
8593 : : ARGNUM is zero based, -1 indicates the `this' argument of a method.
8594 : : Return the location of the FNDECL itself if there are problems. */
8595 : :
8596 : : location_t
8597 : 7140606 : get_fndecl_argument_location (tree fndecl, int argnum)
8598 : : {
8599 : : /* The locations of implicitly-declared functions are likely to be
8600 : : more meaningful than those of their parameters. */
8601 : 7140606 : if (DECL_ARTIFICIAL (fndecl))
8602 : 318 : return DECL_SOURCE_LOCATION (fndecl);
8603 : :
8604 : 7140288 : int i;
8605 : 7140288 : tree param;
8606 : :
8607 : : /* Locate param by index within DECL_ARGUMENTS (fndecl). */
8608 : 7140288 : for (i = 0, param = FUNCTION_FIRST_USER_PARM (fndecl);
8609 : 16807839 : i < argnum && param;
8610 : 9667551 : i++, param = TREE_CHAIN (param))
8611 : : ;
8612 : :
8613 : : /* If something went wrong (e.g. if we have a builtin and thus no arguments),
8614 : : return the location of FNDECL. */
8615 : 7140288 : if (param == NULL)
8616 : 29 : return DECL_SOURCE_LOCATION (fndecl);
8617 : :
8618 : 7140259 : return DECL_SOURCE_LOCATION (param);
8619 : : }
8620 : :
8621 : : /* If FNDECL is non-NULL, issue a note highlighting ARGNUM
8622 : : within its declaration (or the fndecl itself if something went
8623 : : wrong). */
8624 : :
8625 : : void
8626 : 6969 : maybe_inform_about_fndecl_for_bogus_argument_init (tree fn, int argnum,
8627 : : const char *highlight_color)
8628 : : {
8629 : 6969 : if (fn)
8630 : : {
8631 : 1094 : gcc_rich_location richloc (get_fndecl_argument_location (fn, argnum));
8632 : 1094 : richloc.set_highlight_color (highlight_color);
8633 : 1094 : inform (&richloc,
8634 : : "initializing argument %P of %qD", argnum, fn);
8635 : 1094 : }
8636 : 6969 : }
8637 : :
8638 : : /* Maybe warn about C++20 Conversions to arrays of unknown bound. C is
8639 : : the conversion, EXPR is the expression we're converting. */
8640 : :
8641 : : static void
8642 : 30142978 : maybe_warn_array_conv (location_t loc, conversion *c, tree expr)
8643 : : {
8644 : 30142978 : if (cxx_dialect >= cxx20)
8645 : : return;
8646 : :
8647 : 16127964 : tree type = TREE_TYPE (expr);
8648 : 16127964 : type = strip_pointer_operator (type);
8649 : :
8650 : 16127964 : if (TREE_CODE (type) != ARRAY_TYPE
8651 : 16127964 : || TYPE_DOMAIN (type) == NULL_TREE)
8652 : : return;
8653 : :
8654 : 13297 : if (pedantic && conv_binds_to_array_of_unknown_bound (c))
8655 : 10 : pedwarn (loc, OPT_Wc__20_extensions,
8656 : : "conversions to arrays of unknown bound "
8657 : : "are only available with %<-std=c++20%> or %<-std=gnu++20%>");
8658 : : }
8659 : :
8660 : : /* We call this recursively in convert_like_internal. */
8661 : : static tree convert_like (conversion *, tree, tree, int, bool, bool, bool,
8662 : : tsubst_flags_t);
8663 : :
8664 : : /* Adjust the result EXPR of a conversion to the expected type TOTYPE, which
8665 : : must be equivalent but might be a typedef. */
8666 : :
8667 : : static tree
8668 : 689938971 : maybe_adjust_type_name (tree type, tree expr, conversion_kind kind)
8669 : : {
8670 : 689938971 : if (expr == error_mark_node
8671 : 689938921 : || processing_template_decl)
8672 : : return expr;
8673 : :
8674 : 596784441 : tree etype = TREE_TYPE (expr);
8675 : 596784441 : if (etype == type)
8676 : : return expr;
8677 : :
8678 : 74725985 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p (etype, type)
8679 : : || is_bitfield_expr_with_lowered_type (expr)
8680 : : || seen_error ());
8681 : :
8682 : 72304944 : if (SCALAR_TYPE_P (type)
8683 : 142430951 : && (kind == ck_rvalue
8684 : : /* ??? We should be able to do this for ck_identity of more prvalue
8685 : : expressions, but checking !obvalue_p here breaks, so for now let's
8686 : : just handle NON_LVALUE_EXPR (such as the location wrapper for a
8687 : : literal). Maybe we want to express already-rvalue in the
8688 : : conversion somehow? */
8689 : 62795976 : || TREE_CODE (expr) == NON_LVALUE_EXPR))
8690 : 8782344 : expr = build_nop (type, expr);
8691 : :
8692 : : return expr;
8693 : : }
8694 : :
8695 : : /* Perform the conversions in CONVS on the expression EXPR. FN and
8696 : : ARGNUM are used for diagnostics. ARGNUM is zero based, -1
8697 : : indicates the `this' argument of a method. INNER is nonzero when
8698 : : being called to continue a conversion chain. It is negative when a
8699 : : reference binding will be applied, positive otherwise. If
8700 : : ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
8701 : : conversions will be emitted if appropriate. If C_CAST_P is true,
8702 : : this conversion is coming from a C-style cast; in that case,
8703 : : conversions to inaccessible bases are permitted. */
8704 : :
8705 : : static tree
8706 : 823552824 : convert_like_internal (conversion *convs, tree expr, tree fn, int argnum,
8707 : : bool issue_conversion_warnings, bool c_cast_p,
8708 : : bool nested_p, tsubst_flags_t complain)
8709 : : {
8710 : 823552824 : tree totype = convs->type;
8711 : 823552824 : enum diagnostics::kind diag_kind;
8712 : 823552824 : int flags;
8713 : 823552824 : location_t loc = cp_expr_loc_or_input_loc (expr);
8714 : 823552824 : const bool stub_object_p = is_stub_object (expr);
8715 : :
8716 : 823552824 : if (convs->bad_p && !(complain & tf_error))
8717 : 28680 : return error_mark_node;
8718 : :
8719 : 823524144 : gcc_checking_assert (!TYPE_REF_P (TREE_TYPE (expr)));
8720 : :
8721 : 823524144 : if (convs->bad_p
8722 : 7016 : && convs->kind != ck_user
8723 : 6914 : && convs->kind != ck_list
8724 : 6908 : && convs->kind != ck_ambig
8725 : 6908 : && (convs->kind != ck_ref_bind
8726 : 5289 : || (convs->user_conv_p && next_conversion (convs)->bad_p))
8727 : 1640 : && (convs->kind != ck_rvalue
8728 : 15 : || SCALAR_TYPE_P (totype))
8729 : 823525775 : && convs->kind != ck_base)
8730 : : {
8731 : 1631 : int complained = 0;
8732 : 1631 : conversion *t = convs;
8733 : :
8734 : : /* Give a helpful error if this is bad because of excess braces. */
8735 : 46 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8736 : 46 : && SCALAR_TYPE_P (totype)
8737 : 34 : && CONSTRUCTOR_NELTS (expr) > 0
8738 : 1665 : && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
8739 : : {
8740 : 10 : complained = permerror (loc, "too many braces around initializer "
8741 : : "for %qT", totype);
8742 : 30 : while (BRACE_ENCLOSED_INITIALIZER_P (expr)
8743 : 50 : && CONSTRUCTOR_NELTS (expr) == 1)
8744 : 20 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
8745 : : }
8746 : :
8747 : : /* Give a helpful error if this is bad because a conversion to bool
8748 : : from std::nullptr_t requires direct-initialization. */
8749 : 1631 : if (NULLPTR_TYPE_P (TREE_TYPE (expr))
8750 : 1631 : && TREE_CODE (totype) == BOOLEAN_TYPE)
8751 : 15 : complained = permerror (loc, "converting to %qH from %qI requires "
8752 : : "direct-initialization",
8753 : 15 : totype, TREE_TYPE (expr));
8754 : :
8755 : 1631 : if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (expr))
8756 : 69 : && SCALAR_FLOAT_TYPE_P (totype)
8757 : 1700 : && (extended_float_type_p (TREE_TYPE (expr))
8758 : 18 : || extended_float_type_p (totype)))
8759 : 69 : switch (cp_compare_floating_point_conversion_ranks (TREE_TYPE (expr),
8760 : : totype))
8761 : : {
8762 : 69 : case 2:
8763 : 69 : if (pedwarn (loc, OPT_Wnarrowing, "ISO C++ does not allow "
8764 : : "converting to %qH from %qI with greater "
8765 : 69 : "conversion rank", totype, TREE_TYPE (expr)))
8766 : 1631 : complained = 1;
8767 : 15 : else if (!complained)
8768 : 15 : complained = -1;
8769 : : break;
8770 : 0 : case 3:
8771 : 0 : if (pedwarn (loc, OPT_Wnarrowing, "ISO C++ does not allow "
8772 : : "converting to %qH from %qI with unordered "
8773 : 0 : "conversion rank", totype, TREE_TYPE (expr)))
8774 : : complained = 1;
8775 : 0 : else if (!complained)
8776 : 15 : complained = -1;
8777 : : break;
8778 : : default:
8779 : : break;
8780 : : }
8781 : :
8782 : 3278 : for (; t ; t = next_conversion (t))
8783 : : {
8784 : 3269 : if (t->kind == ck_user && t->cand->reason)
8785 : : {
8786 : 52 : auto_diagnostic_group d;
8787 : 52 : complained = permerror (loc, "invalid user-defined conversion "
8788 : 52 : "from %qH to %qI", TREE_TYPE (expr),
8789 : : totype);
8790 : 52 : if (complained)
8791 : : {
8792 : 52 : auto_diagnostic_nesting_level sentinel;
8793 : 52 : print_z_candidate (loc, N_("candidate is:"), t->cand);
8794 : 52 : }
8795 : 52 : expr = convert_like (t, expr, fn, argnum,
8796 : : /*issue_conversion_warnings=*/false,
8797 : : /*c_cast_p=*/false, /*nested_p=*/true,
8798 : : complain);
8799 : 52 : break;
8800 : 52 : }
8801 : 3217 : else if (t->kind == ck_user || !t->bad_p)
8802 : : {
8803 : 1570 : expr = convert_like (t, expr, fn, argnum,
8804 : : /*issue_conversion_warnings=*/false,
8805 : : /*c_cast_p=*/false, /*nested_p=*/true,
8806 : : complain);
8807 : 1570 : if (t->bad_p)
8808 : 0 : complained = 1;
8809 : : break;
8810 : : }
8811 : 1647 : else if (t->kind == ck_ambig)
8812 : 0 : return convert_like (t, expr, fn, argnum,
8813 : : /*issue_conversion_warnings=*/false,
8814 : : /*c_cast_p=*/false, /*nested_p=*/true,
8815 : 0 : complain);
8816 : 1647 : else if (t->kind == ck_identity)
8817 : : break;
8818 : : }
8819 : 1631 : if (!complained && stub_object_p)
8820 : : {
8821 : : /* An error diagnosed within a trait, don't give extra labels. */
8822 : 12 : error_at (loc, "invalid conversion from %qH to %qI",
8823 : 6 : TREE_TYPE (expr), totype);
8824 : 6 : complained = 1;
8825 : : }
8826 : 1625 : else if (!complained && expr != error_mark_node)
8827 : : {
8828 : 1478 : range_label_for_type_mismatch label (TREE_TYPE (expr), totype);
8829 : 1478 : gcc_rich_location richloc (loc, &label, highlight_colors::percent_h);
8830 : 1478 : complained = permerror (&richloc,
8831 : : "invalid conversion from %qH to %qI",
8832 : 1478 : TREE_TYPE (expr), totype);
8833 : 1478 : if (complained)
8834 : 1423 : maybe_emit_indirection_note (loc, expr, totype);
8835 : 1478 : }
8836 : 1631 : if (convs->kind == ck_ref_bind)
8837 : 21 : expr = convert_to_reference (totype, expr, CONV_IMPLICIT,
8838 : : LOOKUP_NORMAL, NULL_TREE,
8839 : : complain);
8840 : : else
8841 : 1610 : expr = cp_convert (totype, expr, complain);
8842 : 1631 : if (complained == 1)
8843 : 1560 : maybe_inform_about_fndecl_for_bogus_argument_init
8844 : 1560 : (fn, argnum, highlight_colors::percent_i);
8845 : 1631 : return expr;
8846 : : }
8847 : :
8848 : 823522513 : if (issue_conversion_warnings && (complain & tf_warning))
8849 : 416525297 : conversion_null_warnings (totype, expr, fn, argnum);
8850 : :
8851 : 823522513 : switch (convs->kind)
8852 : : {
8853 : 4659841 : case ck_user:
8854 : 4659841 : {
8855 : 4659841 : struct z_candidate *cand = convs->cand;
8856 : :
8857 : 4659841 : if (cand == NULL)
8858 : : /* We chose the surrogate function from add_conv_candidate, now we
8859 : : actually need to build the conversion. */
8860 : 56 : cand = build_user_type_conversion_1 (totype, expr,
8861 : : LOOKUP_NO_CONVERSION, complain);
8862 : :
8863 : 4659841 : tree convfn = cand->fn;
8864 : :
8865 : : /* When converting from an init list we consider explicit
8866 : : constructors, but actually trying to call one is an error. */
8867 : 4857928 : if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
8868 : 3347 : && BRACE_ENCLOSED_INITIALIZER_P (expr)
8869 : : /* Unless this is for direct-list-initialization. */
8870 : 3344 : && (!CONSTRUCTOR_IS_DIRECT_INIT (expr) || convs->need_temporary_p)
8871 : : /* And in C++98 a default constructor can't be explicit. */
8872 : 4660061 : && cxx_dialect >= cxx11)
8873 : : {
8874 : 219 : if (!(complain & tf_error))
8875 : 36 : return error_mark_node;
8876 : 183 : location_t loc = location_of (expr);
8877 : 183 : if (CONSTRUCTOR_NELTS (expr) == 0
8878 : 183 : && FUNCTION_FIRST_USER_PARMTYPE (convfn) != void_list_node)
8879 : : {
8880 : 7 : auto_diagnostic_group d;
8881 : 7 : if (pedwarn (loc, 0, "converting to %qT from initializer list "
8882 : : "would use explicit constructor %qD",
8883 : : totype, convfn))
8884 : : {
8885 : 7 : inform (DECL_SOURCE_LOCATION (convfn), "%qD declared here",
8886 : : convfn);
8887 : 7 : inform (loc, "in C++11 and above a default constructor "
8888 : : "can be explicit");
8889 : : }
8890 : 7 : }
8891 : : else
8892 : : {
8893 : 176 : auto_diagnostic_group d;
8894 : 176 : error ("converting to %qT from initializer list would use "
8895 : : "explicit constructor %qD", totype, convfn);
8896 : 176 : inform (DECL_SOURCE_LOCATION (convfn), "%qD declared here",
8897 : : convfn);
8898 : 176 : }
8899 : : }
8900 : :
8901 : : /* If we're initializing from {}, it's value-initialization. */
8902 : 632616 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8903 : 632616 : && CONSTRUCTOR_NELTS (expr) == 0
8904 : 114897 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype)
8905 : 4774675 : && !processing_template_decl)
8906 : : {
8907 : 114870 : if (abstract_virtuals_error (NULL_TREE, totype, complain))
8908 : 15 : return error_mark_node;
8909 : 114855 : expr = build_value_init (totype, complain);
8910 : 114855 : expr = get_target_expr (expr, complain);
8911 : 114855 : if (expr != error_mark_node)
8912 : 114692 : TARGET_EXPR_LIST_INIT_P (expr) = true;
8913 : 114855 : return expr;
8914 : : }
8915 : :
8916 : : /* We don't know here whether EXPR is being used as an lvalue or
8917 : : rvalue, but we know it's read. */
8918 : 4544935 : mark_exp_read (expr);
8919 : :
8920 : : /* Give the conversion call the location of EXPR rather than the
8921 : : location of the context that caused the conversion. */
8922 : 4544935 : iloc_sentinel ils (loc);
8923 : :
8924 : : /* Pass LOOKUP_NO_CONVERSION so rvalue/base handling knows not to allow
8925 : : any more UDCs. */
8926 : 4544935 : expr = build_over_call (cand, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
8927 : : complain);
8928 : :
8929 : : /* If this is a constructor or a function returning an aggr type,
8930 : : we need to build up a TARGET_EXPR. */
8931 : 9089870 : if (DECL_CONSTRUCTOR_P (convfn))
8932 : : {
8933 : 1680697 : expr = build_cplus_new (totype, expr, complain);
8934 : :
8935 : : /* Remember that this was list-initialization. */
8936 : 1680697 : if (convs->check_narrowing && expr != error_mark_node)
8937 : 536940 : TARGET_EXPR_LIST_INIT_P (expr) = true;
8938 : : }
8939 : :
8940 : 4544935 : return expr;
8941 : 4544935 : }
8942 : 592480057 : case ck_identity:
8943 : 592480057 : if (BRACE_ENCLOSED_INITIALIZER_P (expr))
8944 : : {
8945 : 880000 : int nelts = CONSTRUCTOR_NELTS (expr);
8946 : 150969 : if (nelts == 0)
8947 : 729031 : expr = build_value_init (totype, complain);
8948 : 150969 : else if (nelts == 1)
8949 : 150969 : expr = CONSTRUCTOR_ELT (expr, 0)->value;
8950 : : else
8951 : 0 : gcc_unreachable ();
8952 : : }
8953 : 592480057 : expr = mark_use (expr, /*rvalue_p=*/!convs->rvaluedness_matches_p,
8954 : : /*read_p=*/true, UNKNOWN_LOCATION,
8955 : : /*reject_builtin=*/true);
8956 : :
8957 : 592480057 : if (type_unknown_p (expr))
8958 : 20232 : expr = instantiate_type (totype, expr, complain);
8959 : 592480057 : if (!nested_p && TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8960 : 68760 : expr = cp_convert (totype, TREE_OPERAND (expr, 0), complain);
8961 : 592480057 : if (expr == null_node
8962 : 592480057 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype))
8963 : : /* If __null has been converted to an integer type, we do not want to
8964 : : continue to warn about uses of EXPR as an integer, rather than as a
8965 : : pointer. */
8966 : 156158 : expr = build_int_cst (totype, 0);
8967 : 592480057 : return maybe_adjust_type_name (totype, expr, convs->kind);
8968 : 47 : case ck_ambig:
8969 : : /* We leave bad_p off ck_ambig because overload resolution considers
8970 : : it valid, it just fails when we try to perform it. So we need to
8971 : : check complain here, too. */
8972 : 47 : if (complain & tf_error)
8973 : : {
8974 : : /* Call build_user_type_conversion again for the error. */
8975 : 3 : int flags = (convs->need_temporary_p
8976 : 47 : ? LOOKUP_IMPLICIT : LOOKUP_NORMAL);
8977 : 47 : build_user_type_conversion (totype, convs->u.expr, flags, complain);
8978 : 47 : gcc_assert (seen_error ());
8979 : 47 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
8980 : : }
8981 : 47 : return error_mark_node;
8982 : :
8983 : 3679 : case ck_list:
8984 : 3679 : {
8985 : : /* Conversion to std::initializer_list<T>. */
8986 : 3679 : tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
8987 : 3679 : unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (expr);
8988 : 3679 : tree array;
8989 : :
8990 : 3679 : if (tree init = maybe_init_list_as_array (elttype, expr))
8991 : : {
8992 : 132 : elttype
8993 : 132 : = cp_build_qualified_type (elttype, (cp_type_quals (elttype)
8994 : : | TYPE_QUAL_CONST));
8995 : 132 : tree index_type = TYPE_DOMAIN (TREE_TYPE (init));
8996 : 132 : array = build_cplus_array_type (elttype, index_type);
8997 : 132 : len = TREE_INT_CST_LOW (TYPE_MAX_VALUE (index_type)) + 1;
8998 : 132 : array = build_vec_init_expr (array, init, complain);
8999 : 132 : array = get_target_expr (array);
9000 : 132 : array = cp_build_addr_expr (array, complain);
9001 : : }
9002 : 3547 : else if (len)
9003 : : {
9004 : 3434 : tree val;
9005 : 3434 : unsigned ix;
9006 : 3434 : tree new_ctor = build_constructor (init_list_type_node, NULL);
9007 : :
9008 : : /* Convert all the elements. */
9009 : 20729 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
9010 : : {
9011 : 17310 : if (TREE_CODE (val) == RAW_DATA_CST)
9012 : : {
9013 : : /* For conversion to initializer_list<unsigned char> or
9014 : : initializer_list<char> or initializer_list<signed char>
9015 : : we can optimize and keep RAW_DATA_CST with adjusted
9016 : : type if we report narrowing errors if needed, for
9017 : : others this converts each element separately. */
9018 : 48 : if (convs->u.list[ix]->kind == ck_std)
9019 : : {
9020 : 18 : tree et = convs->u.list[ix]->type;
9021 : 18 : conversion *next = next_conversion (convs->u.list[ix]);
9022 : 18 : gcc_assert (et
9023 : : && (TREE_CODE (et) == INTEGER_TYPE
9024 : : || is_byte_access_type (et))
9025 : : && TYPE_PRECISION (et) == CHAR_BIT
9026 : : && next
9027 : : && next->kind == ck_identity);
9028 : 18 : if (!TYPE_UNSIGNED (et)
9029 : : /* For RAW_DATA_CST, TREE_TYPE (val) can be
9030 : : either integer_type_node (when it has been
9031 : : created by the lexer from CPP_EMBED) or
9032 : : after digestion/conversion some integral
9033 : : type with CHAR_BIT precision. For int with
9034 : : precision higher than CHAR_BIT or unsigned char
9035 : : diagnose narrowing conversions from
9036 : : that int/unsigned char to signed char if any
9037 : : byte has most significant bit set. */
9038 : 18 : && (TYPE_UNSIGNED (TREE_TYPE (val))
9039 : 12 : || (TYPE_PRECISION (TREE_TYPE (val))
9040 : : > CHAR_BIT)))
9041 : 2460 : for (int i = 0; i < RAW_DATA_LENGTH (val); ++i)
9042 : : {
9043 : 2454 : if (RAW_DATA_SCHAR_ELT (val, i) >= 0)
9044 : 2448 : continue;
9045 : 6 : else if (complain & tf_error)
9046 : : {
9047 : 6 : location_t loc
9048 : 6 : = cp_expr_loc_or_input_loc (val);
9049 : 6 : int savederrorcount = errorcount;
9050 : 18 : permerror_opt (loc, OPT_Wnarrowing,
9051 : : "narrowing conversion of "
9052 : : "%qd from %qH to %qI",
9053 : 6 : RAW_DATA_UCHAR_ELT (val, i),
9054 : 6 : TREE_TYPE (val), et);
9055 : 6 : if (errorcount != savederrorcount)
9056 : 6 : return error_mark_node;
9057 : : }
9058 : : else
9059 : 0 : return error_mark_node;
9060 : : }
9061 : 12 : tree sub = copy_node (val);
9062 : 12 : TREE_TYPE (sub) = et;
9063 : 12 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor),
9064 : : NULL_TREE, sub);
9065 : : }
9066 : : else
9067 : : {
9068 : 30 : conversion *conv = convs->u.list[ix];
9069 : 30 : gcc_assert (conv->kind == ck_list);
9070 : 15495 : for (int i = 0; i < RAW_DATA_LENGTH (val); ++i)
9071 : : {
9072 : 15465 : tree elt
9073 : 15465 : = build_int_cst (TREE_TYPE (val),
9074 : 15465 : RAW_DATA_UCHAR_ELT (val, i));
9075 : 15465 : tree sub
9076 : 15465 : = convert_like (conv->u.list[i], elt,
9077 : : fn, argnum, false, false,
9078 : : /*nested_p=*/true, complain);
9079 : 15465 : if (sub == error_mark_node)
9080 : : return sub;
9081 : 15465 : if (!check_narrowing (TREE_TYPE (sub), elt,
9082 : : complain))
9083 : 0 : return error_mark_node;
9084 : 15465 : tree nc = new_ctor;
9085 : 15465 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (nc),
9086 : : NULL_TREE, sub);
9087 : 15465 : if (!TREE_CONSTANT (sub))
9088 : 11679 : TREE_CONSTANT (new_ctor) = false;
9089 : : }
9090 : : }
9091 : 42 : len += RAW_DATA_LENGTH (val) - 1;
9092 : 42 : continue;
9093 : 42 : }
9094 : 17262 : tree sub = convert_like (convs->u.list[ix], val, fn,
9095 : : argnum, false, false,
9096 : : /*nested_p=*/true, complain);
9097 : 17262 : if (sub == error_mark_node)
9098 : : return sub;
9099 : 1539 : if (!BRACE_ENCLOSED_INITIALIZER_P (val)
9100 : 17273 : && !check_narrowing (TREE_TYPE (sub), val, complain))
9101 : 0 : return error_mark_node;
9102 : 17253 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor),
9103 : : NULL_TREE, sub);
9104 : 17253 : if (!TREE_CONSTANT (sub))
9105 : 9589 : TREE_CONSTANT (new_ctor) = false;
9106 : : }
9107 : : /* Build up the array. */
9108 : 3419 : elttype
9109 : 3419 : = cp_build_qualified_type (elttype, (cp_type_quals (elttype)
9110 : : | TYPE_QUAL_CONST));
9111 : 3419 : array = build_array_of_n_type (elttype, len);
9112 : 3419 : array = finish_compound_literal (array, new_ctor, complain);
9113 : : /* This is dubious now, should be blessed by P2752. */
9114 : 3419 : DECL_MERGEABLE (TARGET_EXPR_SLOT (array)) = true;
9115 : 3419 : array = cp_build_addr_expr (array, complain);
9116 : : }
9117 : : else
9118 : 113 : array = nullptr_node;
9119 : :
9120 : 3664 : array = cp_convert (build_pointer_type (elttype), array, complain);
9121 : 3664 : if (array == error_mark_node)
9122 : : return error_mark_node;
9123 : :
9124 : : /* Build up the initializer_list object. Note: fail gracefully
9125 : : if the object cannot be completed because, for example, no
9126 : : definition is provided (c++/80956). */
9127 : 3664 : totype = complete_type_or_maybe_complain (totype, NULL_TREE, complain);
9128 : 3664 : if (!totype)
9129 : 0 : return error_mark_node;
9130 : 3664 : tree field = next_aggregate_field (TYPE_FIELDS (totype));
9131 : 3664 : vec<constructor_elt, va_gc> *vec = NULL;
9132 : 3664 : CONSTRUCTOR_APPEND_ELT (vec, field, array);
9133 : 3664 : field = next_aggregate_field (DECL_CHAIN (field));
9134 : 3664 : CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
9135 : 3664 : tree new_ctor = build_constructor (totype, vec);
9136 : 3664 : return get_target_expr (new_ctor, complain);
9137 : : }
9138 : :
9139 : 1136532 : case ck_aggr:
9140 : 1136532 : if (TREE_CODE (totype) == COMPLEX_TYPE)
9141 : : {
9142 : 29535 : tree real = CONSTRUCTOR_ELT (expr, 0)->value;
9143 : 29535 : tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
9144 : 29535 : real = perform_implicit_conversion (TREE_TYPE (totype),
9145 : : real, complain);
9146 : 29535 : imag = perform_implicit_conversion (TREE_TYPE (totype),
9147 : : imag, complain);
9148 : 29535 : expr = build2 (COMPLEX_EXPR, totype, real, imag);
9149 : 29535 : return expr;
9150 : : }
9151 : 1106997 : expr = reshape_init (totype, expr, complain);
9152 : 1106997 : expr = get_target_expr (digest_init (totype, expr, complain),
9153 : : complain);
9154 : 1106997 : if (expr != error_mark_node)
9155 : 1106984 : TARGET_EXPR_LIST_INIT_P (expr) = true;
9156 : : return expr;
9157 : :
9158 : 225242357 : default:
9159 : 225242357 : break;
9160 : 225242357 : };
9161 : :
9162 : 225242357 : conversion *nc = next_conversion (convs);
9163 : 225242357 : if (convs->kind == ck_ref_bind && nc->kind == ck_qual
9164 : 12958 : && !convs->need_temporary_p)
9165 : : /* direct_reference_binding might have inserted a ck_qual under
9166 : : this ck_ref_bind for the benefit of conversion sequence ranking.
9167 : : Don't actually perform that conversion. */
9168 : 9644 : nc = next_conversion (nc);
9169 : :
9170 : 225242357 : expr = convert_like (nc, expr, fn, argnum,
9171 : : convs->kind == ck_ref_bind
9172 : 225242357 : ? issue_conversion_warnings : false,
9173 : : c_cast_p, /*nested_p=*/true, complain & ~tf_no_cleanup);
9174 : 225242357 : if (expr == error_mark_node)
9175 : : return error_mark_node;
9176 : :
9177 : 225242130 : switch (convs->kind)
9178 : : {
9179 : 108061224 : case ck_rvalue:
9180 : 108061224 : expr = decay_conversion (expr, complain);
9181 : 108061224 : if (expr == error_mark_node)
9182 : : {
9183 : 15 : if (complain & tf_error)
9184 : : {
9185 : 12 : auto_diagnostic_group d;
9186 : 12 : maybe_print_user_conv_context (convs);
9187 : 12 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9188 : 12 : }
9189 : 15 : return error_mark_node;
9190 : : }
9191 : :
9192 : 108061209 : if ((complain & tf_warning) && fn
9193 : 36922663 : && warn_suggest_attribute_format)
9194 : : {
9195 : 690 : tree rhstype = TREE_TYPE (expr);
9196 : 690 : const enum tree_code coder = TREE_CODE (rhstype);
9197 : 690 : const enum tree_code codel = TREE_CODE (totype);
9198 : 690 : if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
9199 : 250 : && coder == codel
9200 : 940 : && check_missing_format_attribute (totype, rhstype))
9201 : 6 : warning (OPT_Wsuggest_attribute_format,
9202 : : "argument of function call might be a candidate "
9203 : : "for a format attribute");
9204 : : }
9205 : :
9206 : 108061209 : if (! MAYBE_CLASS_TYPE_P (totype))
9207 : 97458914 : return maybe_adjust_type_name (totype, expr, convs->kind);
9208 : :
9209 : : /* Don't introduce copies when passing arguments along to the inherited
9210 : : constructor. */
9211 : 10602295 : if (current_function_decl
9212 : 8058878 : && flag_new_inheriting_ctors
9213 : 26719571 : && DECL_INHERITED_CTOR (current_function_decl))
9214 : : return expr;
9215 : :
9216 : 10595715 : if (TREE_CODE (expr) == TARGET_EXPR
9217 : 10595715 : && TARGET_EXPR_LIST_INIT_P (expr))
9218 : : /* Copy-list-initialization doesn't actually involve a copy. */
9219 : : return expr;
9220 : :
9221 : : /* Fall through. */
9222 : 12072058 : case ck_base:
9223 : 12072058 : if (convs->kind == ck_base && !convs->need_temporary_p)
9224 : : {
9225 : : /* We are going to bind a reference directly to a base-class
9226 : : subobject of EXPR. */
9227 : : /* Build an expression for `*((base*) &expr)'. */
9228 : 1268203 : expr = convert_to_base (expr, totype,
9229 : 1268203 : !c_cast_p, /*nonnull=*/true, complain);
9230 : 1268203 : return expr;
9231 : : }
9232 : :
9233 : : /* Copy-initialization where the cv-unqualified version of the source
9234 : : type is the same class as, or a derived class of, the class of the
9235 : : destination [is treated as direct-initialization]. [dcl.init] */
9236 : 10803855 : flags = LOOKUP_NORMAL;
9237 : : /* This conversion is being done in the context of a user-defined
9238 : : conversion (i.e. the second step of copy-initialization), so
9239 : : don't allow any more. */
9240 : 10803855 : if (convs->user_conv_p)
9241 : 223884 : flags |= LOOKUP_NO_CONVERSION;
9242 : : /* We might be performing a conversion of the argument
9243 : : to the user-defined conversion, i.e., not a conversion of the
9244 : : result of the user-defined conversion. In which case we skip
9245 : : explicit constructors. */
9246 : 10803855 : if (convs->copy_init_p)
9247 : 10585700 : flags |= LOOKUP_ONLYCONVERTING;
9248 : 10803855 : expr = build_temp (expr, totype, flags, &diag_kind, complain);
9249 : 10803855 : if (diag_kind != diagnostics::kind::unspecified && complain)
9250 : : {
9251 : 74 : auto_diagnostic_group d;
9252 : 74 : maybe_print_user_conv_context (convs);
9253 : 74 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9254 : 74 : }
9255 : :
9256 : 10803855 : return build_cplus_new (totype, expr, complain);
9257 : :
9258 : 44100404 : case ck_ref_bind:
9259 : 44100404 : {
9260 : 44100404 : tree ref_type = totype;
9261 : :
9262 : 44100404 : if (convs->bad_p && !next_conversion (convs)->bad_p)
9263 : : {
9264 : 5128 : tree extype = TREE_TYPE (expr);
9265 : 5128 : auto_diagnostic_group d;
9266 : 5128 : if (TYPE_REF_IS_RVALUE (ref_type)
9267 : 5128 : && lvalue_p (expr))
9268 : 1634 : error_at (loc, "cannot bind rvalue reference of type %qH to "
9269 : : "lvalue of type %qI", totype, extype);
9270 : 6142 : else if (!TYPE_REF_IS_RVALUE (ref_type) && !lvalue_p (expr)
9271 : 4965 : && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
9272 : : {
9273 : 1222 : conversion *next = next_conversion (convs);
9274 : 1222 : if (next->kind == ck_std)
9275 : : {
9276 : 64 : next = next_conversion (next);
9277 : 64 : error_at (loc, "cannot bind non-const lvalue reference of "
9278 : : "type %qH to a value of type %qI",
9279 : : totype, next->type);
9280 : : }
9281 : 1158 : else if (!CP_TYPE_CONST_P (TREE_TYPE (ref_type)))
9282 : 817 : error_at (loc, "cannot bind non-const lvalue reference of "
9283 : : "type %qH to an rvalue of type %qI", totype, extype);
9284 : : else // extype is volatile
9285 : 341 : error_at (loc, "cannot bind lvalue reference of type "
9286 : : "%qH to an rvalue of type %qI", totype,
9287 : : extype);
9288 : : }
9289 : 2272 : else if (!reference_compatible_p (TREE_TYPE (totype), extype))
9290 : : {
9291 : : /* If we're converting from T[] to T[N], don't talk
9292 : : about discarding qualifiers. (Converting from T[N] to
9293 : : T[] is allowed by P0388R4.) */
9294 : 2272 : if (TREE_CODE (extype) == ARRAY_TYPE
9295 : 27 : && TYPE_DOMAIN (extype) == NULL_TREE
9296 : 15 : && TREE_CODE (TREE_TYPE (totype)) == ARRAY_TYPE
9297 : 2287 : && TYPE_DOMAIN (TREE_TYPE (totype)) != NULL_TREE)
9298 : 15 : error_at (loc, "cannot bind reference of type %qH to %qI "
9299 : : "due to different array bounds", totype, extype);
9300 : : else
9301 : 2257 : error_at (loc, "binding reference of type %qH to %qI "
9302 : : "discards qualifiers", totype, extype);
9303 : : }
9304 : : else
9305 : 0 : gcc_unreachable ();
9306 : 5128 : maybe_print_user_conv_context (convs);
9307 : 5128 : maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
9308 : :
9309 : 5128 : return error_mark_node;
9310 : 5128 : }
9311 : 44095276 : else if (complain & tf_warning)
9312 : 29084157 : maybe_warn_array_conv (loc, convs, expr);
9313 : :
9314 : : /* If necessary, create a temporary.
9315 : :
9316 : : VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
9317 : : that need temporaries, even when their types are reference
9318 : : compatible with the type of reference being bound, so the
9319 : : upcoming call to cp_build_addr_expr doesn't fail. */
9320 : 44095276 : if (convs->need_temporary_p
9321 : 42098589 : || TREE_CODE (expr) == CONSTRUCTOR
9322 : 42098588 : || TREE_CODE (expr) == VA_ARG_EXPR)
9323 : : {
9324 : : /* Otherwise, a temporary of type "cv1 T1" is created and
9325 : : initialized from the initializer expression using the rules
9326 : : for a non-reference copy-initialization (8.5). */
9327 : :
9328 : 1996688 : tree type = TREE_TYPE (ref_type);
9329 : 1996688 : cp_lvalue_kind lvalue = lvalue_kind (expr);
9330 : :
9331 : 1996688 : gcc_assert (similar_type_p (type, next_conversion (convs)->type));
9332 : 1996688 : if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
9333 : 2803813 : && !TYPE_REF_IS_RVALUE (ref_type))
9334 : : {
9335 : : /* If the reference is volatile or non-const, we
9336 : : cannot create a temporary. */
9337 : 168 : if (complain & tf_error)
9338 : : {
9339 : 167 : if (lvalue & clk_bitfield)
9340 : 30 : error_at (loc, "cannot bind bit-field %qE to %qT",
9341 : : expr, ref_type);
9342 : 137 : else if (lvalue & clk_packed)
9343 : 9 : error_at (loc, "cannot bind packed field %qE to %qT",
9344 : : expr, ref_type);
9345 : : else
9346 : 128 : error_at (loc, "cannot bind rvalue %qE to %qT",
9347 : : expr, ref_type);
9348 : : }
9349 : 168 : return error_mark_node;
9350 : : }
9351 : : /* If the source is a packed field, and we must use a copy
9352 : : constructor, then building the target expr will require
9353 : : binding the field to the reference parameter to the
9354 : : copy constructor, and we'll end up with an infinite
9355 : : loop. If we can use a bitwise copy, then we'll be
9356 : : OK. */
9357 : 1996520 : if ((lvalue & clk_packed)
9358 : 20 : && CLASS_TYPE_P (type)
9359 : 1996537 : && type_has_nontrivial_copy_init (type))
9360 : : {
9361 : 6 : error_at (loc, "cannot bind packed field %qE to %qT",
9362 : : expr, ref_type);
9363 : 6 : return error_mark_node;
9364 : : }
9365 : 1996514 : if (lvalue & clk_bitfield)
9366 : : {
9367 : 24 : expr = convert_bitfield_to_declared_type (expr);
9368 : 24 : expr = fold_convert (type, expr);
9369 : : }
9370 : :
9371 : : /* Creating &TARGET_EXPR<> in a template would break when
9372 : : tsubsting the expression, so use an IMPLICIT_CONV_EXPR
9373 : : instead. This can happen even when there's no class
9374 : : involved, e.g., when converting an integer to a reference
9375 : : type. */
9376 : 1996514 : if (processing_template_decl)
9377 : 2411 : return build1 (IMPLICIT_CONV_EXPR, totype, expr);
9378 : 1994103 : expr = build_target_expr_with_type (expr, type, complain);
9379 : : }
9380 : :
9381 : : /* Take the address of the thing to which we will bind the
9382 : : reference. */
9383 : 44092691 : expr = cp_build_addr_expr (expr, complain);
9384 : 44092691 : if (expr == error_mark_node)
9385 : : return error_mark_node;
9386 : :
9387 : : /* Convert it to a pointer to the type referred to by the
9388 : : reference. This will adjust the pointer if a derived to
9389 : : base conversion is being performed. */
9390 : 44092691 : expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
9391 : : expr, complain);
9392 : : /* Convert the pointer to the desired reference type. */
9393 : 44092691 : return build_nop (ref_type, expr);
9394 : : }
9395 : :
9396 : 2489552 : case ck_lvalue:
9397 : 2489552 : return decay_conversion (expr, complain);
9398 : :
9399 : 229272 : case ck_fnptr:
9400 : : /* ??? Should the address of a transaction-safe pointer point to the TM
9401 : : clone, and this conversion look up the primary function? */
9402 : 229272 : return build_nop (totype, expr);
9403 : :
9404 : 1111944 : case ck_qual:
9405 : : /* Warn about deprecated conversion if appropriate. */
9406 : 1111944 : if (complain & tf_warning)
9407 : : {
9408 : 1058821 : string_conv_p (totype, expr, 1);
9409 : 1058821 : maybe_warn_array_conv (loc, convs, expr);
9410 : : }
9411 : : break;
9412 : :
9413 : 2266899 : case ck_ptr:
9414 : 2266899 : if (convs->base_p)
9415 : 141568 : expr = convert_to_base (expr, totype, !c_cast_p,
9416 : : /*nonnull=*/false, complain);
9417 : 2266899 : return build_nop (totype, expr);
9418 : :
9419 : 29865 : case ck_pmem:
9420 : 29865 : return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
9421 : 29865 : c_cast_p, complain);
9422 : :
9423 : : default:
9424 : : break;
9425 : : }
9426 : :
9427 : 66562352 : if (convs->check_narrowing
9428 : 86021939 : && !check_narrowing (totype, expr, complain,
9429 : 19459587 : convs->check_narrowing_const_only))
9430 : 458 : return error_mark_node;
9431 : :
9432 : 133123788 : warning_sentinel w (warn_zero_as_null_pointer_constant);
9433 : 66561894 : if (issue_conversion_warnings)
9434 : 44542411 : expr = cp_convert_and_check (totype, expr, complain);
9435 : : else
9436 : : {
9437 : 22019483 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
9438 : 36 : expr = TREE_OPERAND (expr, 0);
9439 : 22019483 : expr = cp_convert (totype, expr, complain);
9440 : : }
9441 : :
9442 : 66561894 : return expr;
9443 : : }
9444 : :
9445 : : /* Return true if converting FROM to TO is unsafe in a template. */
9446 : :
9447 : : static bool
9448 : 1510190 : conv_unsafe_in_template_p (tree to, tree from)
9449 : : {
9450 : : /* Converting classes involves TARGET_EXPR. */
9451 : 1510190 : if (CLASS_TYPE_P (to) || CLASS_TYPE_P (from))
9452 : : return true;
9453 : :
9454 : : /* Converting real to integer produces FIX_TRUNC_EXPR which tsubst
9455 : : doesn't handle. */
9456 : 1149434 : if (SCALAR_FLOAT_TYPE_P (from) && INTEGRAL_OR_ENUMERATION_TYPE_P (to))
9457 : : return true;
9458 : :
9459 : : /* Converting integer to real isn't a trivial conversion, either. */
9460 : 1149431 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (from) && SCALAR_FLOAT_TYPE_P (to))
9461 : 3 : return true;
9462 : :
9463 : : return false;
9464 : : }
9465 : :
9466 : : /* Wrapper for convert_like_internal that handles creating
9467 : : IMPLICIT_CONV_EXPR. */
9468 : :
9469 : : static tree
9470 : 823913577 : convert_like (conversion *convs, tree expr, tree fn, int argnum,
9471 : : bool issue_conversion_warnings, bool c_cast_p, bool nested_p,
9472 : : tsubst_flags_t complain)
9473 : : {
9474 : : /* Creating &TARGET_EXPR<> in a template breaks when substituting,
9475 : : and creating a CALL_EXPR in a template breaks in finish_call_expr
9476 : : so use an IMPLICIT_CONV_EXPR for this conversion. We would have
9477 : : created such codes e.g. when calling a user-defined conversion
9478 : : function. */
9479 : 823913577 : tree conv_expr = NULL_TREE;
9480 : 823913577 : if (processing_template_decl
9481 : 94060250 : && convs->kind != ck_identity
9482 : 825423767 : && conv_unsafe_in_template_p (convs->type, TREE_TYPE (expr)))
9483 : : {
9484 : 360762 : conv_expr = build1 (IMPLICIT_CONV_EXPR, convs->type, expr);
9485 : 360762 : if (convs->kind != ck_ref_bind)
9486 : 41757 : conv_expr = convert_from_reference (conv_expr);
9487 : 360762 : if (!convs->bad_p)
9488 : : return conv_expr;
9489 : : /* Do the normal processing to give the bad_p errors. But we still
9490 : : need to return the IMPLICIT_CONV_EXPR, unless we're returning
9491 : : error_mark_node. */
9492 : : }
9493 : 823552824 : expr = convert_like_internal (convs, expr, fn, argnum,
9494 : : issue_conversion_warnings, c_cast_p,
9495 : : nested_p, complain);
9496 : 823552824 : if (expr == error_mark_node)
9497 : : return error_mark_node;
9498 : 823517274 : return conv_expr ? conv_expr : expr;
9499 : : }
9500 : :
9501 : : /* Convenience wrapper for convert_like. */
9502 : :
9503 : : static inline tree
9504 : 458848683 : convert_like (conversion *convs, tree expr, tsubst_flags_t complain)
9505 : : {
9506 : 458848683 : return convert_like (convs, expr, NULL_TREE, 0,
9507 : : /*issue_conversion_warnings=*/true,
9508 : 458848683 : /*c_cast_p=*/false, /*nested_p=*/false, complain);
9509 : : }
9510 : :
9511 : : /* Convenience wrapper for convert_like. */
9512 : :
9513 : : static inline tree
9514 : 104247284 : convert_like_with_context (conversion *convs, tree expr, tree fn, int argnum,
9515 : : tsubst_flags_t complain)
9516 : : {
9517 : 0 : return convert_like (convs, expr, fn, argnum,
9518 : : /*issue_conversion_warnings=*/true,
9519 : : /*c_cast_p=*/false, /*nested_p=*/false, complain);
9520 : : }
9521 : :
9522 : : /* ARG is being passed to a varargs function. Perform any conversions
9523 : : required. Return the converted value. */
9524 : :
9525 : : tree
9526 : 1466767 : convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
9527 : : {
9528 : 1466767 : tree arg_type = TREE_TYPE (arg);
9529 : 1466767 : location_t loc = cp_expr_loc_or_input_loc (arg);
9530 : :
9531 : : /* [expr.call]
9532 : :
9533 : : If the argument has integral or enumeration type that is subject
9534 : : to the integral promotions (_conv.prom_), or a floating-point
9535 : : type that is subject to the floating-point promotion
9536 : : (_conv.fpprom_), the value of the argument is converted to the
9537 : : promoted type before the call. */
9538 : 1466767 : if (SCALAR_FLOAT_TYPE_P (arg_type)
9539 : 47287 : && (TYPE_PRECISION (arg_type)
9540 : 47287 : < TYPE_PRECISION (double_type_node))
9541 : 11660 : && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type))
9542 : 1477749 : && !extended_float_type_p (arg_type))
9543 : : {
9544 : 10978 : if ((complain & tf_warning)
9545 : 10975 : && warn_double_promotion && !c_inhibit_evaluation_warnings)
9546 : 3 : warning_at (loc, OPT_Wdouble_promotion,
9547 : : "implicit conversion from %qH to %qI when passing "
9548 : : "argument to function",
9549 : : arg_type, double_type_node);
9550 : 10978 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
9551 : 3 : arg = TREE_OPERAND (arg, 0);
9552 : 10978 : arg = mark_rvalue_use (arg);
9553 : 10978 : arg = convert_to_real_nofold (double_type_node, arg);
9554 : : }
9555 : 1455789 : else if (NULLPTR_TYPE_P (arg_type))
9556 : : {
9557 : 70 : arg = mark_rvalue_use (arg);
9558 : 70 : if (TREE_SIDE_EFFECTS (arg))
9559 : : {
9560 : 6 : warning_sentinel w(warn_unused_result);
9561 : 6 : arg = cp_build_compound_expr (arg, null_pointer_node, complain);
9562 : 6 : }
9563 : : else
9564 : 64 : arg = null_pointer_node;
9565 : : }
9566 : 1455719 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
9567 : : {
9568 : 1007419 : if (SCOPED_ENUM_P (arg_type))
9569 : : {
9570 : 63 : tree prom = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg,
9571 : : complain);
9572 : 63 : prom = cp_perform_integral_promotions (prom, complain);
9573 : 123 : if (abi_version_crosses (6)
9574 : 30 : && TYPE_MODE (TREE_TYPE (prom)) != TYPE_MODE (arg_type)
9575 : 93 : && (complain & tf_warning))
9576 : 60 : warning_at (loc, OPT_Wabi, "scoped enum %qT passed through %<...%>"
9577 : : " as %qT before %<-fabi-version=6%>, %qT after",
9578 : : arg_type,
9579 : 30 : TREE_TYPE (prom), ENUM_UNDERLYING_TYPE (arg_type));
9580 : 63 : if (!abi_version_at_least (6))
9581 : 1466767 : arg = prom;
9582 : : }
9583 : : else
9584 : 1007356 : arg = cp_perform_integral_promotions (arg, complain);
9585 : : }
9586 : : else
9587 : : /* [expr.call]
9588 : :
9589 : : The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
9590 : : standard conversions are performed. */
9591 : 448300 : arg = decay_conversion (arg, complain);
9592 : :
9593 : 1466767 : arg = require_complete_type (arg, complain);
9594 : 1466767 : arg_type = TREE_TYPE (arg);
9595 : :
9596 : 1466767 : if (arg != error_mark_node
9597 : : /* In a template (or ill-formed code), we can have an incomplete type
9598 : : even after require_complete_type, in which case we don't know
9599 : : whether it has trivial copy or not. */
9600 : 1466755 : && COMPLETE_TYPE_P (arg_type)
9601 : 2933522 : && !cp_unevaluated_operand)
9602 : : {
9603 : : /* [expr.call] 5.2.2/7:
9604 : : Passing a potentially-evaluated argument of class type (Clause 9)
9605 : : with a non-trivial copy constructor or a non-trivial destructor
9606 : : with no corresponding parameter is conditionally-supported, with
9607 : : implementation-defined semantics.
9608 : :
9609 : : We support it as pass-by-invisible-reference, just like a normal
9610 : : value parameter.
9611 : :
9612 : : If the call appears in the context of a sizeof expression,
9613 : : it is not potentially-evaluated. */
9614 : 638251 : if (type_has_nontrivial_copy_init (arg_type)
9615 : 638251 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type))
9616 : : {
9617 : 33 : arg = force_rvalue (arg, complain);
9618 : 33 : if (complain & tf_warning)
9619 : 33 : warning (OPT_Wconditionally_supported,
9620 : : "passing objects of non-trivially-copyable "
9621 : : "type %q#T through %<...%> is conditionally supported",
9622 : : arg_type);
9623 : 33 : return build1 (ADDR_EXPR, build_reference_type (arg_type), arg);
9624 : : }
9625 : : /* Build up a real lvalue-to-rvalue conversion in case the
9626 : : copy constructor is trivial but not callable. */
9627 : 638218 : else if (CLASS_TYPE_P (arg_type))
9628 : 44119 : force_rvalue (arg, complain);
9629 : :
9630 : : }
9631 : :
9632 : : return arg;
9633 : : }
9634 : :
9635 : : /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
9636 : :
9637 : : tree
9638 : 30978 : build_x_va_arg (location_t loc, tree expr, tree type)
9639 : : {
9640 : 30978 : if (processing_template_decl)
9641 : : {
9642 : 39 : tree r = build_min (VA_ARG_EXPR, type, expr);
9643 : 39 : SET_EXPR_LOCATION (r, loc);
9644 : 39 : return r;
9645 : : }
9646 : :
9647 : 30939 : type = complete_type_or_else (type, NULL_TREE);
9648 : :
9649 : 30939 : if (expr == error_mark_node || !type)
9650 : : return error_mark_node;
9651 : :
9652 : 30918 : expr = mark_lvalue_use (expr);
9653 : :
9654 : 30918 : if (TYPE_REF_P (type))
9655 : : {
9656 : 6 : error ("cannot receive reference type %qT through %<...%>", type);
9657 : 6 : return error_mark_node;
9658 : : }
9659 : :
9660 : 30912 : if (type_has_nontrivial_copy_init (type)
9661 : 30912 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
9662 : : {
9663 : : /* conditionally-supported behavior [expr.call] 5.2.2/7. Let's treat
9664 : : it as pass by invisible reference. */
9665 : 12 : warning_at (loc, OPT_Wconditionally_supported,
9666 : : "receiving objects of non-trivially-copyable type %q#T "
9667 : : "through %<...%> is conditionally-supported", type);
9668 : :
9669 : 12 : tree ref = cp_build_reference_type (type, false);
9670 : 12 : expr = build_va_arg (loc, expr, ref);
9671 : 12 : return convert_from_reference (expr);
9672 : : }
9673 : :
9674 : 30900 : tree ret = build_va_arg (loc, expr, type);
9675 : 30900 : if (CLASS_TYPE_P (type))
9676 : : /* Wrap the VA_ARG_EXPR in a TARGET_EXPR now so other code doesn't need to
9677 : : know how to handle it. */
9678 : 12094 : ret = get_target_expr (ret);
9679 : : return ret;
9680 : : }
9681 : :
9682 : : /* TYPE has been given to va_arg. Apply the default conversions which
9683 : : would have happened when passed via ellipsis. Return the promoted
9684 : : type, or the passed type if there is no change. */
9685 : :
9686 : : tree
9687 : 2462942 : cxx_type_promotes_to (tree type)
9688 : : {
9689 : 2462942 : tree promote;
9690 : :
9691 : : /* Perform the array-to-pointer and function-to-pointer
9692 : : conversions. */
9693 : 2462942 : type = type_decays_to (type);
9694 : :
9695 : 2462942 : promote = type_promotes_to (type);
9696 : 2462942 : if (same_type_p (type, promote))
9697 : 2462918 : promote = type;
9698 : :
9699 : 2462942 : return promote;
9700 : : }
9701 : :
9702 : : /* ARG is a default argument expression being passed to a parameter of
9703 : : the indicated TYPE, which is a parameter to FN. PARMNUM is the
9704 : : zero-based argument number. Do any required conversions. Return
9705 : : the converted value. */
9706 : :
9707 : : static GTY(()) vec<tree, va_gc> *default_arg_context;
9708 : : void
9709 : 6259007 : push_defarg_context (tree fn)
9710 : 6259007 : { vec_safe_push (default_arg_context, fn); }
9711 : :
9712 : : void
9713 : 6259007 : pop_defarg_context (void)
9714 : 6259007 : { default_arg_context->pop (); }
9715 : :
9716 : : tree
9717 : 1125156 : convert_default_arg (tree type, tree arg, tree fn, int parmnum,
9718 : : tsubst_flags_t complain)
9719 : : {
9720 : 1125156 : int i;
9721 : 1125156 : tree t;
9722 : :
9723 : : /* See through clones. */
9724 : 1125156 : fn = DECL_ORIGIN (fn);
9725 : : /* And inheriting ctors. */
9726 : 1125156 : if (flag_new_inheriting_ctors)
9727 : 1124516 : fn = strip_inheriting_ctors (fn);
9728 : :
9729 : : /* Detect recursion. */
9730 : 1126020 : FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t)
9731 : 870 : if (t == fn)
9732 : : {
9733 : 6 : if (complain & tf_error)
9734 : 6 : error ("recursive evaluation of default argument for %q#D", fn);
9735 : 6 : return error_mark_node;
9736 : : }
9737 : :
9738 : : /* If the ARG is an unparsed default argument expression, the
9739 : : conversion cannot be performed. */
9740 : 1125150 : if (TREE_CODE (arg) == DEFERRED_PARSE)
9741 : : {
9742 : 15 : if (complain & tf_error)
9743 : 15 : error ("call to %qD uses the default argument for parameter %P, which "
9744 : : "is not yet defined", fn, parmnum);
9745 : 15 : return error_mark_node;
9746 : : }
9747 : :
9748 : 1125135 : push_defarg_context (fn);
9749 : :
9750 : 1125135 : if (fn && DECL_TEMPLATE_INFO (fn))
9751 : 869900 : arg = tsubst_default_argument (fn, parmnum, type, arg, complain);
9752 : :
9753 : : /* Due to:
9754 : :
9755 : : [dcl.fct.default]
9756 : :
9757 : : The names in the expression are bound, and the semantic
9758 : : constraints are checked, at the point where the default
9759 : : expressions appears.
9760 : :
9761 : : we must not perform access checks here. */
9762 : 1125135 : push_deferring_access_checks (dk_no_check);
9763 : : /* We must make a copy of ARG, in case subsequent processing
9764 : : alters any part of it. */
9765 : 1125135 : arg = break_out_target_exprs (arg, /*clear location*/true);
9766 : :
9767 : 1125135 : arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
9768 : : ICR_DEFAULT_ARGUMENT, fn, parmnum,
9769 : : complain);
9770 : 1125135 : arg = convert_for_arg_passing (type, arg, complain);
9771 : 1125135 : pop_deferring_access_checks();
9772 : :
9773 : 1125135 : pop_defarg_context ();
9774 : :
9775 : 1125135 : return arg;
9776 : : }
9777 : :
9778 : : /* Returns the type which will really be used for passing an argument of
9779 : : type TYPE. */
9780 : :
9781 : : tree
9782 : 558561267 : type_passed_as (tree type)
9783 : : {
9784 : : /* Pass classes with copy ctors by invisible reference. */
9785 : 558561267 : if (TREE_ADDRESSABLE (type))
9786 : 593906 : type = build_reference_type (type);
9787 : :
9788 : 558561267 : return type;
9789 : : }
9790 : :
9791 : : /* Actually perform the appropriate conversion. */
9792 : :
9793 : : tree
9794 : 109336658 : convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
9795 : : {
9796 : 109336658 : tree bitfield_type;
9797 : :
9798 : : /* If VAL is a bitfield, then -- since it has already been converted
9799 : : to TYPE -- it cannot have a precision greater than TYPE.
9800 : :
9801 : : If it has a smaller precision, we must widen it here. For
9802 : : example, passing "int f:3;" to a function expecting an "int" will
9803 : : not result in any conversion before this point.
9804 : :
9805 : : If the precision is the same we must not risk widening. For
9806 : : example, the COMPONENT_REF for a 32-bit "long long" bitfield will
9807 : : often have type "int", even though the C++ type for the field is
9808 : : "long long". If the value is being passed to a function
9809 : : expecting an "int", then no conversions will be required. But,
9810 : : if we call convert_bitfield_to_declared_type, the bitfield will
9811 : : be converted to "long long". */
9812 : 109336658 : bitfield_type = is_bitfield_expr_with_lowered_type (val);
9813 : 109336658 : if (bitfield_type
9814 : 109336658 : && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
9815 : 0 : val = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type), val);
9816 : :
9817 : 109336658 : if (val == error_mark_node)
9818 : : ;
9819 : : /* Pass classes with copy ctors by invisible reference. */
9820 : 109334076 : else if (TREE_ADDRESSABLE (type))
9821 : 205962 : val = build1 (ADDR_EXPR, build_reference_type (type), val);
9822 : 109336658 : if (complain & tf_warning)
9823 : 108147719 : maybe_warn_parm_abi (type, cp_expr_loc_or_input_loc (val));
9824 : :
9825 : 87750631 : if (complain & tf_warning)
9826 : 87750631 : warn_for_address_of_packed_member (type, val);
9827 : :
9828 : : /* gimplify_arg elides TARGET_EXPRs that initialize a function argument,
9829 : : unless the initializer is a CONSTRUCTOR. In that case, we fail to
9830 : : elide the copy anyway. See that function for more information. */
9831 : 6134903 : if (SIMPLE_TARGET_EXPR_P (val)
9832 : 114772427 : && TREE_CODE (TARGET_EXPR_INITIAL (val)) != CONSTRUCTOR)
9833 : 3012665 : set_target_expr_eliding (val);
9834 : :
9835 : 109336658 : return val;
9836 : : }
9837 : :
9838 : : /* Returns non-zero iff FN is a function with magic varargs, i.e. ones for
9839 : : which just decay_conversion or no conversions at all should be done.
9840 : : This is true for some builtins which don't act like normal functions.
9841 : : Return 2 if just decay_conversion and removal of excess precision should
9842 : : be done, 1 if just decay_conversion. Return 3 for special treatment of
9843 : : the 3rd argument for __builtin_*_overflow_p. Return 4 for special
9844 : : treatment of the 1st argument for
9845 : : __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g. */
9846 : :
9847 : : int
9848 : 129683927 : magic_varargs_p (tree fn)
9849 : : {
9850 : 129683927 : if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
9851 : 5709871 : switch (DECL_FUNCTION_CODE (fn))
9852 : : {
9853 : : case BUILT_IN_CLASSIFY_TYPE:
9854 : : case BUILT_IN_CONSTANT_P:
9855 : : case BUILT_IN_NEXT_ARG:
9856 : : case BUILT_IN_VA_START:
9857 : : return 1;
9858 : :
9859 : 20842 : case BUILT_IN_ADD_OVERFLOW_P:
9860 : 20842 : case BUILT_IN_SUB_OVERFLOW_P:
9861 : 20842 : case BUILT_IN_MUL_OVERFLOW_P:
9862 : 20842 : return 3;
9863 : :
9864 : 248429 : case BUILT_IN_ISFINITE:
9865 : 248429 : case BUILT_IN_ISINF:
9866 : 248429 : case BUILT_IN_ISINF_SIGN:
9867 : 248429 : case BUILT_IN_ISNAN:
9868 : 248429 : case BUILT_IN_ISNORMAL:
9869 : 248429 : case BUILT_IN_FPCLASSIFY:
9870 : 248429 : return 2;
9871 : :
9872 : 56758 : case BUILT_IN_CLZG:
9873 : 56758 : case BUILT_IN_CTZG:
9874 : 56758 : case BUILT_IN_CLRSBG:
9875 : 56758 : case BUILT_IN_FFSG:
9876 : 56758 : case BUILT_IN_PARITYG:
9877 : 56758 : case BUILT_IN_POPCOUNTG:
9878 : 56758 : return 4;
9879 : :
9880 : 5279561 : default:
9881 : 5279561 : return lookup_attribute ("type generic",
9882 : 10559122 : TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
9883 : : }
9884 : :
9885 : : return 0;
9886 : : }
9887 : :
9888 : : /* Returns the decl of the dispatcher function if FN is a function version. */
9889 : :
9890 : : tree
9891 : 237 : get_function_version_dispatcher (tree fn)
9892 : : {
9893 : 237 : tree dispatcher_decl = NULL;
9894 : :
9895 : 237 : if (DECL_LOCAL_DECL_P (fn))
9896 : 9 : fn = DECL_LOCAL_DECL_ALIAS (fn);
9897 : :
9898 : 237 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
9899 : : && DECL_FUNCTION_VERSIONED (fn));
9900 : :
9901 : 237 : gcc_assert (targetm.get_function_versions_dispatcher);
9902 : 237 : dispatcher_decl = targetm.get_function_versions_dispatcher (fn);
9903 : :
9904 : 237 : if (dispatcher_decl == NULL)
9905 : : {
9906 : 9 : error_at (input_location, "use of multiversioned function "
9907 : : "without a default");
9908 : 9 : return NULL;
9909 : : }
9910 : :
9911 : 228 : retrofit_lang_decl (dispatcher_decl);
9912 : 228 : gcc_assert (dispatcher_decl != NULL);
9913 : : return dispatcher_decl;
9914 : : }
9915 : :
9916 : : /* fn is a function version dispatcher that is marked used. Mark all the
9917 : : semantically identical function versions it will dispatch as used. */
9918 : :
9919 : : void
9920 : 156 : mark_versions_used (tree fn)
9921 : : {
9922 : 156 : struct cgraph_node *node;
9923 : 156 : struct cgraph_function_version_info *node_v;
9924 : 156 : struct cgraph_function_version_info *it_v;
9925 : :
9926 : 156 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9927 : :
9928 : 156 : node = cgraph_node::get (fn);
9929 : 156 : if (node == NULL)
9930 : : return;
9931 : :
9932 : 156 : gcc_assert (node->dispatcher_function);
9933 : :
9934 : 156 : node_v = node->function_version ();
9935 : 156 : if (node_v == NULL)
9936 : : return;
9937 : :
9938 : : /* All semantically identical versions are chained. Traverse and mark each
9939 : : one of them as used. */
9940 : 156 : it_v = node_v->next;
9941 : 1119 : while (it_v != NULL)
9942 : : {
9943 : 963 : mark_used (it_v->this_node->decl);
9944 : 963 : it_v = it_v->next;
9945 : : }
9946 : : }
9947 : :
9948 : : /* Build a call to "the copy constructor" for the type of A, even if it
9949 : : wouldn't be selected by normal overload resolution. Used for
9950 : : diagnostics. */
9951 : :
9952 : : static tree
9953 : 3 : call_copy_ctor (tree a, tsubst_flags_t complain)
9954 : : {
9955 : 3 : tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (a));
9956 : 3 : tree binfo = TYPE_BINFO (ctype);
9957 : 3 : tree copy = get_copy_ctor (ctype, complain);
9958 : 3 : copy = build_baselink (binfo, binfo, copy, NULL_TREE);
9959 : 3 : tree ob = build_dummy_object (ctype);
9960 : 3 : releasing_vec args (make_tree_vector_single (a));
9961 : 3 : tree r = build_new_method_call (ob, copy, &args, NULL_TREE,
9962 : : LOOKUP_NORMAL, NULL, complain);
9963 : 3 : return r;
9964 : 3 : }
9965 : :
9966 : : /* Return the base constructor corresponding to COMPLETE_CTOR or NULL_TREE. */
9967 : :
9968 : : static tree
9969 : 42 : base_ctor_for (tree complete_ctor)
9970 : : {
9971 : 42 : tree clone;
9972 : 42 : FOR_EACH_CLONE (clone, DECL_CLONED_FUNCTION (complete_ctor))
9973 : 42 : if (DECL_BASE_CONSTRUCTOR_P (clone))
9974 : : return clone;
9975 : : return NULL_TREE;
9976 : : }
9977 : :
9978 : : /* Try to make EXP suitable to be used as the initializer for a base subobject,
9979 : : and return whether we were successful. EXP must have already been cleared
9980 : : by unsafe_copy_elision_p{,_opt}. */
9981 : :
9982 : : static bool
9983 : 206 : make_base_init_ok (tree exp)
9984 : : {
9985 : 206 : if (TREE_CODE (exp) == TARGET_EXPR)
9986 : 206 : exp = TARGET_EXPR_INITIAL (exp);
9987 : 209 : while (TREE_CODE (exp) == COMPOUND_EXPR)
9988 : 3 : exp = TREE_OPERAND (exp, 1);
9989 : 206 : if (TREE_CODE (exp) == COND_EXPR)
9990 : : {
9991 : 3 : bool ret = make_base_init_ok (TREE_OPERAND (exp, 2));
9992 : 3 : if (tree op1 = TREE_OPERAND (exp, 1))
9993 : : {
9994 : 3 : bool r1 = make_base_init_ok (op1);
9995 : : /* If unsafe_copy_elision_p was false, the arms should match. */
9996 : 3 : gcc_assert (r1 == ret);
9997 : : }
9998 : 3 : return ret;
9999 : : }
10000 : 203 : if (TREE_CODE (exp) != AGGR_INIT_EXPR)
10001 : : /* A trivial copy is OK. */
10002 : : return true;
10003 : 60 : if (!AGGR_INIT_VIA_CTOR_P (exp))
10004 : : /* unsafe_copy_elision_p_opt must have said this is OK. */
10005 : : return true;
10006 : 42 : tree fn = cp_get_callee_fndecl_nofold (exp);
10007 : 42 : if (DECL_BASE_CONSTRUCTOR_P (fn))
10008 : : return true;
10009 : 42 : gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (fn));
10010 : 42 : fn = base_ctor_for (fn);
10011 : 42 : if (!fn || DECL_HAS_VTT_PARM_P (fn))
10012 : : /* The base constructor has more parameters, so we can't just change the
10013 : : call target. It would be possible to splice in the appropriate
10014 : : arguments, but probably not worth the complexity. */
10015 : : return false;
10016 : 33 : mark_used (fn);
10017 : 33 : AGGR_INIT_EXPR_FN (exp) = build_address (fn);
10018 : 33 : return true;
10019 : : }
10020 : :
10021 : : /* Return 2 if T refers to a base, 1 if a potentially-overlapping field,
10022 : : neither of which can be used for return by invisible reference. We avoid
10023 : : doing C++17 mandatory copy elision for either of these cases.
10024 : :
10025 : : This returns non-zero even if the type of T has no tail padding that other
10026 : : data could be allocated into, because that depends on the particular ABI.
10027 : : unsafe_copy_elision_p_opt does consider whether there is padding. */
10028 : :
10029 : : int
10030 : 30516097 : unsafe_return_slot_p (tree t)
10031 : : {
10032 : : /* Check empty bases separately, they don't have fields. */
10033 : 30516097 : if (is_empty_base_ref (t))
10034 : : return 2;
10035 : :
10036 : : /* A delegating constructor might be used to initialize a base. */
10037 : 30046709 : if (current_function_decl
10038 : 39778908 : && DECL_CONSTRUCTOR_P (current_function_decl)
10039 : 32946384 : && (t == current_class_ref
10040 : 2810317 : || tree_strip_nop_conversions (t) == current_class_ptr))
10041 : 101687 : return 2;
10042 : :
10043 : 29945022 : STRIP_NOPS (t);
10044 : 29945022 : if (TREE_CODE (t) == ADDR_EXPR)
10045 : 48602 : t = TREE_OPERAND (t, 0);
10046 : 29945022 : if (TREE_CODE (t) == COMPONENT_REF)
10047 : 2592239 : t = TREE_OPERAND (t, 1);
10048 : 29945022 : if (TREE_CODE (t) != FIELD_DECL)
10049 : : return false;
10050 : 2627858 : if (!CLASS_TYPE_P (TREE_TYPE (t)))
10051 : : /* The middle-end will do the right thing for scalar types. */
10052 : : return false;
10053 : 2108995 : if (DECL_FIELD_IS_BASE (t))
10054 : : return 2;
10055 : 1421648 : if (lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (t)))
10056 : : return 1;
10057 : : return 0;
10058 : : }
10059 : :
10060 : : /* True IFF EXP is a prvalue that represents return by invisible reference. */
10061 : :
10062 : : static bool
10063 : 255813 : init_by_return_slot_p (tree exp)
10064 : : {
10065 : : /* Copy elision only happens with a TARGET_EXPR. */
10066 : 255816 : if (TREE_CODE (exp) != TARGET_EXPR)
10067 : : return false;
10068 : 24504 : tree init = TARGET_EXPR_INITIAL (exp);
10069 : : /* build_compound_expr pushes COMPOUND_EXPR inside TARGET_EXPR. */
10070 : 24510 : while (TREE_CODE (init) == COMPOUND_EXPR)
10071 : 6 : init = TREE_OPERAND (init, 1);
10072 : 24504 : if (TREE_CODE (init) == COND_EXPR)
10073 : : {
10074 : : /* We'll end up copying from each of the arms of the COND_EXPR directly
10075 : : into the target, so look at them. */
10076 : 6 : if (tree op = TREE_OPERAND (init, 1))
10077 : 6 : if (init_by_return_slot_p (op))
10078 : : return true;
10079 : 3 : return init_by_return_slot_p (TREE_OPERAND (init, 2));
10080 : : }
10081 : 24498 : return (TREE_CODE (init) == AGGR_INIT_EXPR
10082 : 24498 : && !AGGR_INIT_VIA_CTOR_P (init));
10083 : : }
10084 : :
10085 : : /* We can't elide a copy from a function returning by value to a
10086 : : potentially-overlapping subobject, as the callee might clobber tail padding.
10087 : : Return true iff this could be that case.
10088 : :
10089 : : Places that use this function (or _opt) to decide to elide a copy should
10090 : : probably use make_safe_copy_elision instead. */
10091 : :
10092 : : bool
10093 : 1705249 : unsafe_copy_elision_p (tree target, tree exp)
10094 : : {
10095 : 1705249 : return unsafe_return_slot_p (target) && init_by_return_slot_p (exp);
10096 : : }
10097 : :
10098 : : /* As above, but for optimization allow more cases that are actually safe. */
10099 : :
10100 : : static bool
10101 : 5682518 : unsafe_copy_elision_p_opt (tree target, tree exp)
10102 : : {
10103 : 5682518 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
10104 : : /* It's safe to elide the copy for a class with no tail padding. */
10105 : 5682518 : if (!is_empty_class (type)
10106 : 5682518 : && tree_int_cst_equal (TYPE_SIZE (type), CLASSTYPE_SIZE (type)))
10107 : : return false;
10108 : 1669630 : return unsafe_copy_elision_p (target, exp);
10109 : : }
10110 : :
10111 : : /* Try to make EXP suitable to be used as the initializer for TARGET,
10112 : : and return whether we were successful. */
10113 : :
10114 : : bool
10115 : 829 : make_safe_copy_elision (tree target, tree exp)
10116 : : {
10117 : 829 : int uns = unsafe_return_slot_p (target);
10118 : 829 : if (!uns)
10119 : : return true;
10120 : 829 : if (init_by_return_slot_p (exp))
10121 : : return false;
10122 : 829 : if (uns == 1)
10123 : : return true;
10124 : 19 : return make_base_init_ok (exp);
10125 : : }
10126 : :
10127 : : /* True IFF the result of the conversion C is a prvalue. */
10128 : :
10129 : : static bool
10130 : 10313880 : conv_is_prvalue (conversion *c)
10131 : : {
10132 : 10313880 : if (c->kind == ck_rvalue)
10133 : : return true;
10134 : 10313880 : if (c->kind == ck_base && c->need_temporary_p)
10135 : : return true;
10136 : 10313880 : if (c->kind == ck_user && !TYPE_REF_P (c->type))
10137 : : return true;
10138 : 10241310 : if (c->kind == ck_identity && c->u.expr
10139 : 10009311 : && TREE_CODE (c->u.expr) == TARGET_EXPR)
10140 : 43 : return true;
10141 : :
10142 : : return false;
10143 : : }
10144 : :
10145 : : /* True iff C is a conversion that binds a reference to a prvalue. */
10146 : :
10147 : : static bool
10148 : 10314593 : conv_binds_ref_to_prvalue (conversion *c)
10149 : : {
10150 : 10314593 : if (c->kind != ck_ref_bind)
10151 : : return false;
10152 : 10314593 : if (c->need_temporary_p)
10153 : : return true;
10154 : :
10155 : 10313880 : return conv_is_prvalue (next_conversion (c));
10156 : : }
10157 : :
10158 : : /* True iff EXPR represents a (subobject of a) temporary. */
10159 : :
10160 : : static bool
10161 : 13790 : expr_represents_temporary_p (tree expr)
10162 : : {
10163 : 13894 : while (handled_component_p (expr))
10164 : 104 : expr = TREE_OPERAND (expr, 0);
10165 : 13790 : return TREE_CODE (expr) == TARGET_EXPR;
10166 : : }
10167 : :
10168 : : /* True iff C is a conversion that binds a reference to a temporary.
10169 : : This is a superset of conv_binds_ref_to_prvalue: here we're also
10170 : : interested in xvalues. */
10171 : :
10172 : : static bool
10173 : 13115 : conv_binds_ref_to_temporary (conversion *c)
10174 : : {
10175 : 13115 : if (conv_binds_ref_to_prvalue (c))
10176 : : return true;
10177 : 12720 : if (c->kind != ck_ref_bind)
10178 : : return false;
10179 : 12720 : c = next_conversion (c);
10180 : : /* This is the case for
10181 : : struct Base {};
10182 : : struct Derived : Base {};
10183 : : const Base& b(Derived{});
10184 : : where we bind 'b' to the Base subobject of a temporary object of type
10185 : : Derived. The subobject is an xvalue; the whole object is a prvalue.
10186 : :
10187 : : The ck_base doesn't have to be present for cases like X{}.m. */
10188 : 12720 : if (c->kind == ck_base)
10189 : 8 : c = next_conversion (c);
10190 : 12665 : if (c->kind == ck_identity && c->u.expr
10191 : 25385 : && expr_represents_temporary_p (c->u.expr))
10192 : : return true;
10193 : : return false;
10194 : : }
10195 : :
10196 : : /* Return tristate::TS_TRUE if converting EXPR to a reference type TYPE binds
10197 : : the reference to a temporary. Return tristate::TS_FALSE if converting
10198 : : EXPR to a reference type TYPE doesn't bind the reference to a temporary. If
10199 : : the conversion is invalid or bad, return tristate::TS_UNKNOWN. DIRECT_INIT_P
10200 : : says whether the conversion should be done in direct- or copy-initialization
10201 : : context. */
10202 : :
10203 : : tristate
10204 : 13594 : ref_conv_binds_to_temporary (tree type, tree expr, bool direct_init_p/*=false*/)
10205 : : {
10206 : 13594 : gcc_assert (TYPE_REF_P (type));
10207 : :
10208 : 13594 : conversion_obstack_sentinel cos;
10209 : :
10210 : 13594 : const int flags = direct_init_p ? LOOKUP_NORMAL : LOOKUP_IMPLICIT;
10211 : 13594 : conversion *conv = implicit_conversion (type, TREE_TYPE (expr), expr,
10212 : : /*c_cast_p=*/false, flags, tf_none);
10213 : 13594 : tristate ret (tristate::TS_UNKNOWN);
10214 : 13594 : if (conv && !conv->bad_p)
10215 : 13115 : ret = tristate (conv_binds_ref_to_temporary (conv));
10216 : :
10217 : 27188 : return ret;
10218 : 13594 : }
10219 : :
10220 : : /* Call the trivial destructor for INSTANCE, which can be either an lvalue of
10221 : : class type or a pointer to class type. If NO_PTR_DEREF is true and
10222 : : INSTANCE has pointer type, clobber the pointer rather than what it points
10223 : : to. */
10224 : :
10225 : : tree
10226 : 1781216 : build_trivial_dtor_call (tree instance, bool no_ptr_deref)
10227 : : {
10228 : 1781216 : gcc_assert (!is_dummy_object (instance));
10229 : :
10230 : 1781216 : if (!flag_lifetime_dse)
10231 : : {
10232 : 88 : no_clobber:
10233 : 107 : return fold_convert (void_type_node, instance);
10234 : : }
10235 : :
10236 : 1836936 : if (INDIRECT_TYPE_P (TREE_TYPE (instance))
10237 : 1781128 : && (!no_ptr_deref || TYPE_REF_P (TREE_TYPE (instance))))
10238 : : {
10239 : 1710292 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
10240 : 19 : goto no_clobber;
10241 : 1710273 : instance = cp_build_fold_indirect_ref (instance);
10242 : : }
10243 : :
10244 : : /* A trivial destructor should still clobber the object. */
10245 : 1781109 : tree clobber = build_clobber (TREE_TYPE (instance), CLOBBER_OBJECT_END);
10246 : 1781109 : return build2 (MODIFY_EXPR, void_type_node,
10247 : 1781109 : instance, clobber);
10248 : : }
10249 : :
10250 : : /* Return true if in an immediate function context, or an unevaluated operand,
10251 : : or a default argument/member initializer, or a subexpression of an immediate
10252 : : invocation. */
10253 : :
10254 : : bool
10255 : 2730192 : in_immediate_context ()
10256 : : {
10257 : 2730192 : return (cp_unevaluated_operand != 0
10258 : 2299321 : || (current_function_decl != NULL_TREE
10259 : 3778758 : && DECL_IMMEDIATE_FUNCTION_P (current_function_decl))
10260 : : /* DR 2631: default args and DMI aren't immediately evaluated.
10261 : : Return true here so immediate_invocation_p returns false. */
10262 : 2155092 : || current_binding_level->kind == sk_function_parms
10263 : 2154959 : || current_binding_level->kind == sk_template_parms
10264 : 2154697 : || parsing_nsdmi ()
10265 : 4883227 : || in_consteval_if_p);
10266 : : }
10267 : :
10268 : : /* Return true if a call to FN with number of arguments NARGS
10269 : : is an immediate invocation. */
10270 : :
10271 : : bool
10272 : 163518645 : immediate_invocation_p (tree fn)
10273 : : {
10274 : 163518645 : return (TREE_CODE (fn) == FUNCTION_DECL
10275 : 163518645 : && DECL_IMMEDIATE_FUNCTION_P (fn)
10276 : 164511348 : && !in_immediate_context ());
10277 : : }
10278 : :
10279 : : /* Subroutine of the various build_*_call functions. Overload resolution
10280 : : has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
10281 : : ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
10282 : : bitmask of various LOOKUP_* flags which apply to the call itself. */
10283 : :
10284 : : static tree
10285 : 177030774 : build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
10286 : : {
10287 : 177030774 : tree fn = cand->fn;
10288 : 177030774 : const vec<tree, va_gc> *args = cand->args;
10289 : 177030774 : tree first_arg = cand->first_arg;
10290 : 177030774 : conversion **convs = cand->convs;
10291 : 177030774 : tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
10292 : 177030774 : int parmlen;
10293 : 177030774 : tree val;
10294 : 177030774 : int nargs;
10295 : 177030774 : tree *argarray;
10296 : 177030774 : bool already_used = false;
10297 : :
10298 : : /* In a template, there is no need to perform all of the work that
10299 : : is normally done. We are only interested in the type of the call
10300 : : expression, i.e., the return type of the function. Any semantic
10301 : : errors will be deferred until the template is instantiated. */
10302 : 177030774 : if (processing_template_decl)
10303 : : {
10304 : 25546169 : if (undeduced_auto_decl (fn))
10305 : 1667 : mark_used (fn, complain);
10306 : : else
10307 : : /* Otherwise set TREE_USED for the benefit of -Wunused-function.
10308 : : See PR80598. */
10309 : 25544502 : TREE_USED (fn) = 1;
10310 : :
10311 : 25546169 : tree return_type = TREE_TYPE (TREE_TYPE (fn));
10312 : 25546169 : tree callee;
10313 : 25546169 : if (first_arg == NULL_TREE)
10314 : : {
10315 : 19831902 : callee = build_addr_func (fn, complain);
10316 : 19831902 : if (callee == error_mark_node)
10317 : : return error_mark_node;
10318 : : }
10319 : : else
10320 : : {
10321 : 5714267 : callee = build_baselink (cand->conversion_path, cand->access_path,
10322 : : fn, NULL_TREE);
10323 : 5714267 : callee = build_min (COMPONENT_REF, TREE_TYPE (fn),
10324 : : first_arg, callee, NULL_TREE);
10325 : : }
10326 : :
10327 : 25546169 : tree expr = build_call_vec (return_type, callee, args);
10328 : 25546169 : SET_EXPR_LOCATION (expr, input_location);
10329 : 25546169 : if (TREE_THIS_VOLATILE (fn) && cfun)
10330 : 2181242 : current_function_returns_abnormally = 1;
10331 : 25546169 : if (TREE_DEPRECATED (fn)
10332 : 25546169 : && warning_suppressed_at (input_location,
10333 : : OPT_Wdeprecated_declarations))
10334 : : /* Make the expr consistent with the location. */
10335 : 20497 : TREE_NO_WARNING (expr) = true;
10336 : 25546169 : if (immediate_invocation_p (fn))
10337 : : {
10338 : 48112 : tree obj_arg = NULL_TREE, exprimm = expr;
10339 : 96224 : if (DECL_CONSTRUCTOR_P (fn))
10340 : 0 : obj_arg = first_arg;
10341 : 0 : if (obj_arg
10342 : 0 : && is_dummy_object (obj_arg)
10343 : 0 : && !type_dependent_expression_p (obj_arg))
10344 : : {
10345 : 0 : exprimm = build_cplus_new (DECL_CONTEXT (fn), expr, complain);
10346 : 0 : obj_arg = NULL_TREE;
10347 : : }
10348 : : /* Look through *(const T *)&obj. */
10349 : 48112 : else if (obj_arg && INDIRECT_REF_P (obj_arg))
10350 : : {
10351 : 0 : tree addr = TREE_OPERAND (obj_arg, 0);
10352 : 0 : STRIP_NOPS (addr);
10353 : 0 : if (TREE_CODE (addr) == ADDR_EXPR)
10354 : : {
10355 : 0 : tree typeo = TREE_TYPE (obj_arg);
10356 : 0 : tree typei = TREE_TYPE (TREE_OPERAND (addr, 0));
10357 : 0 : if (same_type_ignoring_top_level_qualifiers_p (typeo, typei))
10358 : 0 : obj_arg = TREE_OPERAND (addr, 0);
10359 : : }
10360 : : }
10361 : 48112 : fold_non_dependent_expr (exprimm, complain,
10362 : : /*manifestly_const_eval=*/true,
10363 : : obj_arg);
10364 : : }
10365 : 25546169 : return convert_from_reference (expr);
10366 : : }
10367 : :
10368 : : /* Give any warnings we noticed during overload resolution. */
10369 : 151484605 : if (cand->warnings && (complain & tf_warning))
10370 : : {
10371 : : struct candidate_warning *w;
10372 : 94 : for (w = cand->warnings; w; w = w->next)
10373 : 47 : joust (cand, w->loser, 1, complain);
10374 : : }
10375 : :
10376 : : /* Core issue 2327: P0135 doesn't say how to handle the case where the
10377 : : argument to the copy constructor ends up being a prvalue after
10378 : : conversion. Let's do the normal processing, but pretend we aren't
10379 : : actually using the copy constructor. */
10380 : 151484605 : bool force_elide = false;
10381 : 151484605 : if (cxx_dialect >= cxx17
10382 : 149564629 : && cand->num_convs == 1
10383 : 86689749 : && DECL_COMPLETE_CONSTRUCTOR_P (fn)
10384 : 26310196 : && (DECL_COPY_CONSTRUCTOR_P (fn)
10385 : 13244552 : || DECL_MOVE_CONSTRUCTOR_P (fn))
10386 : 10326460 : && !unsafe_return_slot_p (first_arg)
10387 : 161786019 : && conv_binds_ref_to_prvalue (convs[0]))
10388 : : {
10389 : 72928 : force_elide = true;
10390 : 72928 : goto not_really_used;
10391 : : }
10392 : :
10393 : : /* OK, we're actually calling this inherited constructor; set its deletedness
10394 : : appropriately. We can get away with doing this here because calling is
10395 : : the only way to refer to a constructor. */
10396 : 302823354 : if (DECL_INHERITED_CTOR (fn)
10397 : 21364669 : && !deduce_inheriting_ctor (fn))
10398 : : {
10399 : 2 : if (complain & tf_error)
10400 : 2 : mark_used (fn);
10401 : 2 : return error_mark_node;
10402 : : }
10403 : :
10404 : : /* Make =delete work with SFINAE. */
10405 : 151411675 : if (DECL_DELETED_FN (fn))
10406 : : {
10407 : 747776 : if (complain & tf_error)
10408 : : {
10409 : 2267 : mark_used (fn);
10410 : 2267 : if (cand->next)
10411 : : {
10412 : 2084 : if (flag_diagnostics_all_candidates)
10413 : 9 : print_z_candidates (input_location, cand, /*only_viable_p=*/false);
10414 : : else
10415 : 2075 : inform (input_location,
10416 : : "use %<-fdiagnostics-all-candidates%> to display "
10417 : : "considered candidates");
10418 : : }
10419 : : }
10420 : 747776 : return error_mark_node;
10421 : : }
10422 : :
10423 : 150663899 : if (DECL_FUNCTION_MEMBER_P (fn))
10424 : : {
10425 : 84773431 : tree access_fn;
10426 : : /* If FN is a template function, two cases must be considered.
10427 : : For example:
10428 : :
10429 : : struct A {
10430 : : protected:
10431 : : template <class T> void f();
10432 : : };
10433 : : template <class T> struct B {
10434 : : protected:
10435 : : void g();
10436 : : };
10437 : : struct C : A, B<int> {
10438 : : using A::f; // #1
10439 : : using B<int>::g; // #2
10440 : : };
10441 : :
10442 : : In case #1 where `A::f' is a member template, DECL_ACCESS is
10443 : : recorded in the primary template but not in its specialization.
10444 : : We check access of FN using its primary template.
10445 : :
10446 : : In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
10447 : : because it is a member of class template B, DECL_ACCESS is
10448 : : recorded in the specialization `B<int>::g'. We cannot use its
10449 : : primary template because `B<T>::g' and `B<int>::g' may have
10450 : : different access. */
10451 : 84773431 : if (DECL_TEMPLATE_INFO (fn)
10452 : 84773431 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
10453 : 8697312 : access_fn = DECL_TI_TEMPLATE (fn);
10454 : : else
10455 : : access_fn = fn;
10456 : 84773431 : if (!perform_or_defer_access_check (cand->access_path, access_fn,
10457 : : fn, complain))
10458 : 21744 : return error_mark_node;
10459 : : }
10460 : :
10461 : : /* If we're checking for implicit delete, don't bother with argument
10462 : : conversions. */
10463 : 150642155 : if (flags & LOOKUP_SPECULATIVE)
10464 : : {
10465 : 21028798 : if (cand->viable == 1)
10466 : : return fn;
10467 : 117 : else if (!(complain & tf_error))
10468 : : /* Reject bad conversions now. */
10469 : 102 : return error_mark_node;
10470 : : /* else continue to get conversion error. */
10471 : : }
10472 : :
10473 : 129613357 : not_really_used:
10474 : :
10475 : : /* N3276 magic doesn't apply to nested calls. */
10476 : 129686300 : tsubst_flags_t decltype_flag = (complain & tf_decltype);
10477 : 129686300 : complain &= ~tf_decltype;
10478 : : /* No-Cleanup doesn't apply to nested calls either. */
10479 : 129686300 : tsubst_flags_t no_cleanup_complain = complain;
10480 : 129686300 : complain &= ~tf_no_cleanup;
10481 : :
10482 : : /* Find maximum size of vector to hold converted arguments. */
10483 : 129686300 : parmlen = list_length (parm);
10484 : 242981895 : nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0);
10485 : 129686300 : if (parmlen > nargs)
10486 : : nargs = parmlen;
10487 : 129686300 : argarray = XALLOCAVEC (tree, nargs);
10488 : :
10489 : 259372597 : in_consteval_if_p_temp_override icip;
10490 : : /* If the call is immediate function invocation, make sure
10491 : : taking address of immediate functions is allowed in its arguments. */
10492 : 129686300 : if (immediate_invocation_p (STRIP_TEMPLATE (fn)))
10493 : 418096 : in_consteval_if_p = true;
10494 : :
10495 : 129686300 : int argarray_size = 0;
10496 : 129686300 : unsigned int arg_index = 0;
10497 : 129686300 : int conv_index = 0;
10498 : 129686300 : int param_index = 0;
10499 : :
10500 : 181386519 : auto consume_object_arg = [&arg_index, &first_arg, args]()
10501 : : {
10502 : 51700219 : if (!first_arg)
10503 : 0 : return (*args)[arg_index++];
10504 : 51700219 : tree object_arg = first_arg;
10505 : 51700219 : first_arg = NULL_TREE;
10506 : 51700219 : return object_arg;
10507 : 129686300 : };
10508 : :
10509 : : /* The implicit parameters to a constructor are not considered by overload
10510 : : resolution, and must be of the proper type. */
10511 : 259372600 : if (DECL_CONSTRUCTOR_P (fn))
10512 : : {
10513 : 14582543 : tree object_arg = consume_object_arg ();
10514 : 14582543 : argarray[argarray_size++] = build_this (object_arg);
10515 : 14582543 : parm = TREE_CHAIN (parm);
10516 : : /* We should never try to call the abstract constructor. */
10517 : 14582543 : gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
10518 : :
10519 : 14582543 : if (DECL_HAS_VTT_PARM_P (fn))
10520 : : {
10521 : 18119 : argarray[argarray_size++] = (*args)[arg_index];
10522 : 18119 : ++arg_index;
10523 : 18119 : parm = TREE_CHAIN (parm);
10524 : : }
10525 : : }
10526 : : /* Bypass access control for 'this' parameter. */
10527 : 115103757 : else if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
10528 : : {
10529 : 37112459 : tree arg = build_this (consume_object_arg ());
10530 : 37112459 : tree argtype = TREE_TYPE (arg);
10531 : :
10532 : 37112459 : if (arg == error_mark_node)
10533 : : return error_mark_node;
10534 : 37112456 : if (convs[conv_index++]->bad_p)
10535 : : {
10536 : 1128 : if (complain & tf_error)
10537 : : {
10538 : 104 : auto_diagnostic_group d;
10539 : 104 : if (permerror (input_location, "passing %qT as %<this%> "
10540 : : "argument discards qualifiers",
10541 : 104 : TREE_TYPE (argtype)))
10542 : 101 : inform (DECL_SOURCE_LOCATION (fn), " in call to %qD", fn);
10543 : 104 : }
10544 : : else
10545 : : return error_mark_node;
10546 : : }
10547 : :
10548 : : /* The class where FN is defined. */
10549 : 37111432 : tree ctx = DECL_CONTEXT (fn);
10550 : :
10551 : : /* See if the function member or the whole class type is declared
10552 : : final and the call can be devirtualized. */
10553 : 37111432 : if (DECL_FINAL_P (fn) || CLASSTYPE_FINAL (ctx))
10554 : 134287 : flags |= LOOKUP_NONVIRTUAL;
10555 : :
10556 : : /* [class.mfct.non-static]: If a non-static member function of a class
10557 : : X is called for an object that is not of type X, or of a type
10558 : : derived from X, the behavior is undefined.
10559 : :
10560 : : So we can assume that anything passed as 'this' is non-null, and
10561 : : optimize accordingly. */
10562 : : /* Check that the base class is accessible. */
10563 : 37111432 : if (!accessible_base_p (TREE_TYPE (argtype),
10564 : 37111432 : BINFO_TYPE (cand->conversion_path), true))
10565 : : {
10566 : 33 : if (complain & tf_error)
10567 : 30 : error ("%qT is not an accessible base of %qT",
10568 : 30 : BINFO_TYPE (cand->conversion_path),
10569 : 30 : TREE_TYPE (argtype));
10570 : : else
10571 : 3 : return error_mark_node;
10572 : : }
10573 : : /* If fn was found by a using declaration, the conversion path
10574 : : will be to the derived class, not the base declaring fn. We
10575 : : must convert to the base. */
10576 : 37111429 : tree base_binfo = cand->conversion_path;
10577 : 37111429 : if (BINFO_TYPE (base_binfo) != ctx)
10578 : : {
10579 : 92337 : base_binfo = lookup_base (base_binfo, ctx, ba_unique, NULL, complain);
10580 : 92337 : if (base_binfo == error_mark_node)
10581 : : return error_mark_node;
10582 : : }
10583 : :
10584 : : /* If we know the dynamic type of the object, look up the final overrider
10585 : : in the BINFO. */
10586 : 38029559 : if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
10587 : 37672845 : && resolves_to_fixed_type_p (arg))
10588 : : {
10589 : 953 : tree ov = lookup_vfn_in_binfo (DECL_VINDEX (fn), base_binfo);
10590 : :
10591 : : /* And unwind base_binfo to match. If we don't find the type we're
10592 : : looking for in BINFO_INHERITANCE_CHAIN, we're looking at diamond
10593 : : inheritance; for now do a normal virtual call in that case. */
10594 : 953 : tree octx = DECL_CONTEXT (ov);
10595 : 953 : tree obinfo = base_binfo;
10596 : 1969 : while (obinfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (obinfo), octx))
10597 : 33 : obinfo = BINFO_INHERITANCE_CHAIN (obinfo);
10598 : 953 : if (obinfo)
10599 : : {
10600 : 950 : fn = ov;
10601 : 950 : base_binfo = obinfo;
10602 : 950 : flags |= LOOKUP_NONVIRTUAL;
10603 : : }
10604 : : }
10605 : :
10606 : 37111423 : tree converted_arg = build_base_path (PLUS_EXPR, arg,
10607 : : base_binfo, 1, complain);
10608 : :
10609 : 37111423 : argarray[argarray_size++] = converted_arg;
10610 : 37111423 : parm = TREE_CHAIN (parm);
10611 : : }
10612 : :
10613 : 233932548 : auto handle_arg = [fn, flags](tree type,
10614 : : tree arg,
10615 : : int const param_index,
10616 : : conversion *conv,
10617 : : tsubst_flags_t const arg_complain)
10618 : : {
10619 : : /* Set user_conv_p on the argument conversions, so rvalue/base handling
10620 : : knows not to allow any more UDCs. This needs to happen after we
10621 : : process cand->warnings. */
10622 : 104247284 : if (flags & LOOKUP_NO_CONVERSION)
10623 : 2248307 : conv->user_conv_p = true;
10624 : :
10625 : 104247284 : if (arg_complain & tf_warning)
10626 : 82911507 : maybe_warn_pessimizing_move (arg, type, /*return_p=*/false);
10627 : :
10628 : 104247284 : tree val = convert_like_with_context (conv, arg, fn,
10629 : : param_index, arg_complain);
10630 : 104247284 : val = convert_for_arg_passing (type, val, arg_complain);
10631 : 104247284 : return val;
10632 : 129685264 : };
10633 : :
10634 : 129685264 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
10635 : : {
10636 : 5217 : gcc_assert (cand->num_convs > 0);
10637 : 5217 : tree object_arg = consume_object_arg ();
10638 : 5217 : val = handle_arg (TREE_VALUE (parm),
10639 : : object_arg,
10640 : : param_index++,
10641 : 5217 : convs[conv_index++],
10642 : : complain);
10643 : :
10644 : 5217 : if (val == error_mark_node)
10645 : : return error_mark_node;
10646 : : else
10647 : 5105 : argarray[argarray_size++] = val;
10648 : 5105 : parm = TREE_CHAIN (parm);
10649 : : }
10650 : :
10651 : 129685152 : gcc_assert (first_arg == NULL_TREE);
10652 : 233926086 : for (; arg_index < vec_safe_length (args) && parm;
10653 : 104240934 : parm = TREE_CHAIN (parm), ++arg_index, ++param_index, ++conv_index)
10654 : : {
10655 : 104242067 : tree current_arg = (*args)[arg_index];
10656 : :
10657 : : /* If the argument is NULL and used to (implicitly) instantiate a
10658 : : template function (and bind one of the template arguments to
10659 : : the type of 'long int'), we don't want to warn about passing NULL
10660 : : to non-pointer argument.
10661 : : For example, if we have this template function:
10662 : :
10663 : : template<typename T> void func(T x) {}
10664 : :
10665 : : we want to warn (when -Wconversion is enabled) in this case:
10666 : :
10667 : : void foo() {
10668 : : func<int>(NULL);
10669 : : }
10670 : :
10671 : : but not in this case:
10672 : :
10673 : : void foo() {
10674 : : func(NULL);
10675 : : }
10676 : : */
10677 : 104242067 : bool const conversion_warning = !(null_node_p (current_arg)
10678 : 48954 : && DECL_TEMPLATE_INFO (fn)
10679 : 60 : && cand->template_decl
10680 : 30 : && !cand->explicit_targs);
10681 : :
10682 : 15 : tsubst_flags_t const arg_complain
10683 : : = conversion_warning ? complain : complain & ~tf_warning;
10684 : :
10685 : 104242067 : val = handle_arg (TREE_VALUE (parm),
10686 : : current_arg,
10687 : : param_index,
10688 : 104242067 : convs[conv_index],
10689 : : arg_complain);
10690 : :
10691 : 104242067 : if (val == error_mark_node)
10692 : : return error_mark_node;
10693 : : else
10694 : 104240934 : argarray[argarray_size++] = val;
10695 : : }
10696 : :
10697 : : /* Default arguments */
10698 : 130809066 : for (; parm && parm != void_list_node;
10699 : 1125047 : parm = TREE_CHAIN (parm), param_index++)
10700 : : {
10701 : 1125160 : if (TREE_VALUE (parm) == error_mark_node)
10702 : : return error_mark_node;
10703 : 2250296 : val = convert_default_arg (TREE_VALUE (parm),
10704 : 1125148 : TREE_PURPOSE (parm),
10705 : : fn, param_index,
10706 : : complain);
10707 : 1125148 : if (val == error_mark_node)
10708 : : return error_mark_node;
10709 : 1125047 : argarray[argarray_size++] = val;
10710 : : }
10711 : :
10712 : : /* Ellipsis */
10713 : 129683906 : int magic = magic_varargs_p (fn);
10714 : 132327025 : for (; arg_index < vec_safe_length (args); ++arg_index)
10715 : : {
10716 : 2643140 : tree a = (*args)[arg_index];
10717 : 2643140 : if ((magic == 3 && arg_index == 2) || (magic == 4 && arg_index == 0))
10718 : : {
10719 : : /* Do no conversions for certain magic varargs. */
10720 : 77564 : a = mark_type_use (a);
10721 : 77564 : if (TREE_CODE (a) == FUNCTION_DECL && reject_gcc_builtin (a))
10722 : 0 : return error_mark_node;
10723 : : }
10724 : 2565576 : else if (magic != 0)
10725 : : {
10726 : : /* Don't truncate excess precision to the semantic type. */
10727 : 1099255 : if (magic == 1 && TREE_CODE (a) == EXCESS_PRECISION_EXPR)
10728 : 84 : a = TREE_OPERAND (a, 0);
10729 : : /* For other magic varargs only do decay_conversion. */
10730 : 1099255 : a = decay_conversion (a, complain);
10731 : : }
10732 : 1466321 : else if (DECL_CONSTRUCTOR_P (fn)
10733 : 289 : && vec_safe_length (args) == 1
10734 : 1466444 : && same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (fn),
10735 : 123 : TREE_TYPE (a)))
10736 : : {
10737 : : /* Avoid infinite recursion trying to call A(...). */
10738 : 3 : if (complain & tf_error)
10739 : : /* Try to call the actual copy constructor for a good error. */
10740 : 3 : call_copy_ctor (a, complain);
10741 : 3 : return error_mark_node;
10742 : : }
10743 : : else
10744 : 1466318 : a = convert_arg_to_ellipsis (a, complain);
10745 : 2643137 : if (a == error_mark_node)
10746 : : return error_mark_node;
10747 : 2643119 : argarray[argarray_size++] = a;
10748 : : }
10749 : :
10750 : 129683885 : gcc_assert (argarray_size <= nargs);
10751 : 129683885 : nargs = argarray_size;
10752 : 129683885 : icip.reset ();
10753 : :
10754 : : /* Avoid performing argument transformation if warnings are disabled.
10755 : : When tf_warning is set and at least one of the warnings is active
10756 : : the check_function_arguments function might warn about something. */
10757 : :
10758 : 129683885 : bool warned_p = false;
10759 : 129683885 : if ((complain & tf_warning)
10760 : 91385514 : && (warn_nonnull
10761 : 89538856 : || warn_format
10762 : 89538853 : || warn_suggest_attribute_format
10763 : 89538757 : || warn_restrict))
10764 : : {
10765 : 1849004 : tree *fargs = (!nargs ? argarray
10766 : 1530225 : : (tree *) alloca (nargs * sizeof (tree)));
10767 : 5689096 : for (int j = 0; j < nargs; j++)
10768 : : {
10769 : : /* For -Wformat undo the implicit passing by hidden reference
10770 : : done by convert_arg_to_ellipsis. */
10771 : 3840092 : if (TREE_CODE (argarray[j]) == ADDR_EXPR
10772 : 3840092 : && TYPE_REF_P (TREE_TYPE (argarray[j])))
10773 : 1596 : fargs[j] = TREE_OPERAND (argarray[j], 0);
10774 : : else
10775 : 3838496 : fargs[j] = argarray[j];
10776 : : }
10777 : :
10778 : 1849004 : warned_p = check_function_arguments (input_location, fn, TREE_TYPE (fn),
10779 : : nargs, fargs, NULL,
10780 : : cp_comp_parm_types);
10781 : : }
10782 : :
10783 : 259367770 : if (DECL_INHERITED_CTOR (fn))
10784 : : {
10785 : : /* Check for passing ellipsis arguments to an inherited constructor. We
10786 : : could handle this by open-coding the inherited constructor rather than
10787 : : defining it, but let's not bother now. */
10788 : 49926 : if (!cp_unevaluated_operand
10789 : 49668 : && cand->num_convs
10790 : 49668 : && cand->convs[cand->num_convs-1]->ellipsis_p)
10791 : : {
10792 : 15 : if (complain & tf_error)
10793 : : {
10794 : 15 : sorry ("passing arguments to ellipsis of inherited constructor "
10795 : : "%qD", cand->fn);
10796 : 15 : inform (DECL_SOURCE_LOCATION (cand->fn), "declared here");
10797 : : }
10798 : 15 : return error_mark_node;
10799 : : }
10800 : :
10801 : : /* A base constructor inheriting from a virtual base doesn't get the
10802 : : inherited arguments, just this and __vtt. */
10803 : 49911 : if (ctor_omit_inherited_parms (fn))
10804 : 129683870 : nargs = 2;
10805 : : }
10806 : :
10807 : : /* Avoid actually calling copy constructors and copy assignment operators,
10808 : : if possible. */
10809 : :
10810 : 129683870 : if (!force_elide
10811 : 129610974 : && (!flag_elide_constructors
10812 : : /* It's unsafe to elide the operation when handling
10813 : : a noexcept-expression, it may evaluate to the wrong
10814 : : value (c++/53025, c++/96090). */
10815 : 129610770 : || cp_noexcept_operand != 0))
10816 : : /* Do things the hard way. */;
10817 : 127990154 : else if (cand->num_convs == 1
10818 : 196935505 : && (DECL_COPY_CONSTRUCTOR_P (fn)
10819 : 130437722 : || DECL_MOVE_CONSTRUCTOR_P (fn)))
10820 : : {
10821 : 5682518 : tree targ;
10822 : 5682518 : tree arg = argarray[num_artificial_parms_for (fn)];
10823 : 5682518 : tree fa = argarray[0];
10824 : 5682518 : bool trivial = trivial_fn_p (fn);
10825 : :
10826 : : /* Pull out the real argument, disregarding const-correctness. */
10827 : 5682518 : targ = arg;
10828 : : /* Strip the reference binding for the constructor parameter. */
10829 : 0 : if (CONVERT_EXPR_P (targ)
10830 : 5682518 : && TYPE_REF_P (TREE_TYPE (targ)))
10831 : 5682518 : targ = TREE_OPERAND (targ, 0);
10832 : : /* But don't strip any other reference bindings; binding a temporary to a
10833 : : reference prevents copy elision. */
10834 : 9366993 : while ((CONVERT_EXPR_P (targ)
10835 : 8064302 : && !TYPE_REF_P (TREE_TYPE (targ)))
10836 : 19983338 : || TREE_CODE (targ) == NON_LVALUE_EXPR)
10837 : 7323871 : targ = TREE_OPERAND (targ, 0);
10838 : 5682518 : if (TREE_CODE (targ) == ADDR_EXPR)
10839 : : {
10840 : 1851456 : targ = TREE_OPERAND (targ, 0);
10841 : 1851456 : if (!same_type_ignoring_top_level_qualifiers_p
10842 : 1851456 : (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
10843 : : targ = NULL_TREE;
10844 : : }
10845 : : else
10846 : : targ = NULL_TREE;
10847 : :
10848 : 1850998 : if (targ)
10849 : : arg = targ;
10850 : : else
10851 : 3831520 : arg = cp_build_fold_indirect_ref (arg);
10852 : :
10853 : : /* In C++17 we shouldn't be copying a TARGET_EXPR except into a
10854 : : potentially-overlapping subobject. */
10855 : 5682518 : if (CHECKING_P && cxx_dialect >= cxx17)
10856 : 5601718 : gcc_assert (TREE_CODE (arg) != TARGET_EXPR
10857 : : || force_elide
10858 : : /* It's from binding the ref parm to a packed field. */
10859 : : || convs[0]->need_temporary_p
10860 : : || seen_error ()
10861 : : /* See unsafe_copy_elision_p. */
10862 : : || unsafe_return_slot_p (fa));
10863 : :
10864 : 5682518 : bool unsafe = unsafe_copy_elision_p_opt (fa, arg);
10865 : 5682518 : bool eliding_temp = (TREE_CODE (arg) == TARGET_EXPR && !unsafe);
10866 : :
10867 : : /* [class.copy]: the copy constructor is implicitly defined even if the
10868 : : implementation elided its use. But don't warn about deprecation when
10869 : : eliding a temporary, as then no copy is actually performed. */
10870 : 5682518 : warning_sentinel s (warn_deprecated_copy, eliding_temp);
10871 : 5682518 : if (force_elide)
10872 : : /* The language says this isn't called. */;
10873 : 5609622 : else if (!trivial)
10874 : : {
10875 : 1189571 : if (!mark_used (fn, complain) && !(complain & tf_error))
10876 : 0 : return error_mark_node;
10877 : : already_used = true;
10878 : : }
10879 : : else
10880 : 4420051 : cp_handle_deprecated_or_unavailable (fn, complain);
10881 : :
10882 : 115885 : if (eliding_temp && DECL_BASE_CONSTRUCTOR_P (fn)
10883 : 5682699 : && !make_base_init_ok (arg))
10884 : : unsafe = true;
10885 : :
10886 : : /* If we're creating a temp and we already have one, don't create a
10887 : : new one. If we're not creating a temp but we get one, use
10888 : : INIT_EXPR to collapse the temp into our target. Otherwise, if the
10889 : : ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
10890 : : temp or an INIT_EXPR otherwise. */
10891 : 5682518 : if (is_dummy_object (fa))
10892 : : {
10893 : 4211116 : if (TREE_CODE (arg) == TARGET_EXPR)
10894 : : return arg;
10895 : 4104788 : else if (trivial)
10896 : 3566123 : return force_target_expr (DECL_CONTEXT (fn), arg, complain);
10897 : : }
10898 : 1471402 : else if ((trivial || TREE_CODE (arg) == TARGET_EXPR)
10899 : 826813 : && !unsafe)
10900 : : {
10901 : 826731 : tree to = cp_build_fold_indirect_ref (fa);
10902 : 826731 : val = cp_build_init_expr (to, arg);
10903 : 826731 : return val;
10904 : : }
10905 : 5682518 : }
10906 : 122307636 : else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
10907 : 2369330 : && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR)
10908 : 124185531 : && trivial_fn_p (fn))
10909 : : {
10910 : 1334658 : tree to = cp_build_fold_indirect_ref (argarray[0]);
10911 : 1334658 : tree type = TREE_TYPE (to);
10912 : 1334658 : tree as_base = CLASSTYPE_AS_BASE (type);
10913 : 1334658 : tree arg = argarray[1];
10914 : 1334658 : location_t loc = cp_expr_loc_or_input_loc (arg);
10915 : :
10916 : 1334658 : if (is_really_empty_class (type, /*ignore_vptr*/true))
10917 : : {
10918 : : /* Avoid copying empty classes, but ensure op= returns an lvalue even
10919 : : if the object argument isn't one. */
10920 : 186020 : to = force_lvalue (to, complain);
10921 : 186020 : val = build2 (COMPOUND_EXPR, type, arg, to);
10922 : 186020 : suppress_warning (val, OPT_Wunused);
10923 : : }
10924 : 1148638 : else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
10925 : : {
10926 : 1024094 : if (is_std_init_list (type)
10927 : 1024094 : && conv_binds_ref_to_prvalue (convs[1]))
10928 : 3 : warning_at (loc, OPT_Winit_list_lifetime,
10929 : : "assignment from temporary %<initializer_list%> does "
10930 : : "not extend the lifetime of the underlying array");
10931 : 1024094 : arg = cp_build_fold_indirect_ref (arg);
10932 : 1024094 : val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
10933 : : }
10934 : : else
10935 : : {
10936 : : /* We must only copy the non-tail padding parts. */
10937 : 124544 : tree arg0, arg2, t;
10938 : 124544 : tree array_type, alias_set;
10939 : :
10940 : 124544 : arg2 = TYPE_SIZE_UNIT (as_base);
10941 : : /* Ensure op= returns an lvalue even if the object argument isn't
10942 : : one. */
10943 : 124544 : to = force_lvalue (to, complain);
10944 : 124544 : to = cp_stabilize_reference (to);
10945 : 124544 : arg0 = cp_build_addr_expr (to, complain);
10946 : :
10947 : 124544 : array_type = build_array_type (unsigned_char_type_node,
10948 : : build_index_type
10949 : : (size_binop (MINUS_EXPR,
10950 : : arg2, size_int (1))));
10951 : 124544 : alias_set = build_int_cst (build_pointer_type (type), 0);
10952 : 124544 : t = build2 (MODIFY_EXPR, void_type_node,
10953 : : build2 (MEM_REF, array_type, arg0, alias_set),
10954 : : build2 (MEM_REF, array_type, arg, alias_set));
10955 : 124544 : val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
10956 : 124544 : suppress_warning (val, OPT_Wunused);
10957 : : }
10958 : :
10959 : 1334658 : cp_handle_deprecated_or_unavailable (fn, complain);
10960 : :
10961 : 1334658 : return val;
10962 : : }
10963 : 120972978 : else if (trivial_fn_p (fn))
10964 : : {
10965 : 4670008 : if (DECL_DESTRUCTOR_P (fn))
10966 : 1701463 : return build_trivial_dtor_call (argarray[0]);
10967 : 633541 : else if (default_ctor_p (fn))
10968 : : {
10969 : 633541 : if (is_dummy_object (argarray[0]))
10970 : 243835 : return force_target_expr (DECL_CONTEXT (fn), void_node,
10971 : 243835 : no_cleanup_complain);
10972 : : else
10973 : 389706 : return cp_build_fold_indirect_ref (argarray[0]);
10974 : : }
10975 : : }
10976 : :
10977 : 121515026 : gcc_assert (!force_elide);
10978 : :
10979 : 121515026 : if (!already_used
10980 : 121515026 : && !mark_used (fn, complain))
10981 : 756 : return error_mark_node;
10982 : :
10983 : : /* Warn if the built-in writes to an object of a non-trivial type. */
10984 : 121514267 : if (warn_class_memaccess
10985 : 1832887 : && vec_safe_length (args) >= 2
10986 : 122440597 : && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
10987 : 65353 : maybe_warn_class_memaccess (input_location, fn, args);
10988 : :
10989 : 121514267 : if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
10990 : : {
10991 : 560472 : tree t;
10992 : 560472 : tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
10993 : 560472 : DECL_CONTEXT (fn),
10994 : : ba_any, NULL, complain);
10995 : 560472 : gcc_assert (binfo && binfo != error_mark_node);
10996 : :
10997 : 560472 : argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
10998 : : complain);
10999 : 560472 : if (TREE_SIDE_EFFECTS (argarray[0]))
11000 : 64759 : argarray[0] = save_expr (argarray[0]);
11001 : 560472 : t = build_pointer_type (TREE_TYPE (fn));
11002 : 560472 : fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
11003 : 560472 : TREE_TYPE (fn) = t;
11004 : : }
11005 : : else
11006 : : {
11007 : : /* If FN is marked deprecated or unavailable, then we've already
11008 : : issued a diagnostic from mark_used above, so avoid redundantly
11009 : : issuing another one from build_addr_func. */
11010 : 120953795 : auto w = make_temp_override (deprecated_state,
11011 : 120953795 : UNAVAILABLE_DEPRECATED_SUPPRESS);
11012 : :
11013 : 120953795 : fn = build_addr_func (fn, complain);
11014 : 120953795 : if (fn == error_mark_node)
11015 : 0 : return error_mark_node;
11016 : :
11017 : : /* We're actually invoking the function. (Immediate functions get an
11018 : : & when invoking it even though the user didn't use &.) */
11019 : 120953795 : ADDR_EXPR_DENOTES_CALL_P (fn) = true;
11020 : 120953795 : }
11021 : :
11022 : 121514267 : tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag);
11023 : 121514267 : if (call == error_mark_node)
11024 : : return call;
11025 : 121513418 : if (cand->flags & LOOKUP_LIST_INIT_CTOR)
11026 : : {
11027 : 1154555 : tree c = extract_call_expr (call);
11028 : : /* build_new_op will clear this when appropriate. */
11029 : 1154555 : CALL_EXPR_ORDERED_ARGS (c) = true;
11030 : : }
11031 : 121513418 : if (warned_p)
11032 : : {
11033 : 265 : tree c = extract_call_expr (call);
11034 : 265 : if (TREE_CODE (c) == CALL_EXPR)
11035 : 265 : suppress_warning (c /* Suppress all warnings. */);
11036 : : }
11037 : 121513153 : else if (TREE_DEPRECATED (fn)
11038 : 121513153 : && warning_suppressed_at (input_location,
11039 : : OPT_Wdeprecated_declarations))
11040 : : {
11041 : 0 : tree c = extract_call_expr (call);
11042 : 0 : if (TREE_CODE (c) == CALL_EXPR)
11043 : 0 : TREE_NO_WARNING (c) = true;
11044 : : }
11045 : :
11046 : : return call;
11047 : : }
11048 : :
11049 : : namespace
11050 : : {
11051 : :
11052 : : /* Return the DECL of the first non-static subobject of class TYPE
11053 : : that satisfies the predicate PRED or null if none can be found. */
11054 : :
11055 : : template <class Predicate>
11056 : : tree
11057 : 3539 : first_non_static_field (tree type, Predicate pred)
11058 : : {
11059 : 3539 : if (!type || !CLASS_TYPE_P (type))
11060 : : return NULL_TREE;
11061 : :
11062 : 69559 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
11063 : : {
11064 : 66614 : if (TREE_CODE (field) != FIELD_DECL)
11065 : 43126 : continue;
11066 : 23488 : if (TREE_STATIC (field))
11067 : 0 : continue;
11068 : 23488 : if (pred (field))
11069 : : return field;
11070 : : }
11071 : :
11072 : 2945 : int i = 0;
11073 : :
11074 : 3065 : for (tree base_binfo, binfo = TYPE_BINFO (type);
11075 : 3065 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11076 : : {
11077 : 126 : tree base = TREE_TYPE (base_binfo);
11078 : 126 : if (pred (base))
11079 : : return base;
11080 : 120 : if (tree field = first_non_static_field (base, pred))
11081 : : return field;
11082 : : }
11083 : :
11084 : : return NULL_TREE;
11085 : : }
11086 : :
11087 : : struct NonPublicField
11088 : : {
11089 : 22738 : bool operator() (const_tree t) const
11090 : : {
11091 : 22738 : return DECL_P (t) && (TREE_PRIVATE (t) || TREE_PROTECTED (t));
11092 : : }
11093 : : };
11094 : :
11095 : : /* Return the DECL of the first non-public subobject of class TYPE
11096 : : or null if none can be found. */
11097 : :
11098 : : static inline tree
11099 : 3245 : first_non_public_field (tree type)
11100 : : {
11101 : 3245 : return first_non_static_field (type, NonPublicField ());
11102 : : }
11103 : :
11104 : : struct NonTrivialField
11105 : : {
11106 : 876 : bool operator() (const_tree t) const
11107 : : {
11108 : 876 : return !trivial_type_p (DECL_P (t) ? TREE_TYPE (t) : t);
11109 : : }
11110 : : };
11111 : :
11112 : : /* Return the DECL of the first non-trivial subobject of class TYPE
11113 : : or null if none can be found. */
11114 : :
11115 : : static inline tree
11116 : 174 : first_non_trivial_field (tree type)
11117 : : {
11118 : 174 : return first_non_static_field (type, NonTrivialField ());
11119 : : }
11120 : :
11121 : : } /* unnamed namespace */
11122 : :
11123 : : /* Return true if all copy and move assignment operator overloads for
11124 : : class TYPE are trivial and at least one of them is not deleted and,
11125 : : when ACCESS is set, accessible. Return false otherwise. Set
11126 : : HASASSIGN to true when the TYPE has a (not necessarily trivial)
11127 : : copy or move assignment. */
11128 : :
11129 : : static bool
11130 : 5004 : has_trivial_copy_assign_p (tree type, bool access, bool *hasassign)
11131 : : {
11132 : 5004 : tree fns = get_class_binding (type, assign_op_identifier);
11133 : 5004 : bool all_trivial = true;
11134 : :
11135 : : /* Iterate over overloads of the assignment operator, checking
11136 : : accessible copy assignments for triviality. */
11137 : :
11138 : 16447 : for (tree f : ovl_range (fns))
11139 : : {
11140 : : /* Skip operators that aren't copy assignments. */
11141 : 8537 : if (!copy_fn_p (f))
11142 : 3029 : continue;
11143 : :
11144 : 5508 : bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
11145 : 5796 : || accessible_p (TYPE_BINFO (type), f, true));
11146 : :
11147 : : /* Skip template assignment operators and deleted functions. */
11148 : 5508 : if (TREE_CODE (f) != FUNCTION_DECL || DECL_DELETED_FN (f))
11149 : 694 : continue;
11150 : :
11151 : 4814 : if (accessible)
11152 : 4562 : *hasassign = true;
11153 : :
11154 : 4562 : if (!accessible || !trivial_fn_p (f))
11155 : : all_trivial = false;
11156 : :
11157 : : /* Break early when both properties have been determined. */
11158 : 4814 : if (*hasassign && !all_trivial)
11159 : : break;
11160 : : }
11161 : :
11162 : : /* Return true if they're all trivial and one of the expressions
11163 : : TYPE() = TYPE() or TYPE() = (TYPE&)() is valid. */
11164 : 5004 : tree ref = cp_build_reference_type (type, false);
11165 : 5004 : return (all_trivial
11166 : 5004 : && (is_trivially_xible (MODIFY_EXPR, type, type)
11167 : 333 : || is_trivially_xible (MODIFY_EXPR, type, ref)));
11168 : : }
11169 : :
11170 : : /* Return true if all copy and move ctor overloads for class TYPE are
11171 : : trivial and at least one of them is not deleted and, when ACCESS is
11172 : : set, accessible. Return false otherwise. Set each element of HASCTOR[]
11173 : : to true when the TYPE has a (not necessarily trivial) default and copy
11174 : : (or move) ctor, respectively. */
11175 : :
11176 : : static bool
11177 : 5004 : has_trivial_copy_p (tree type, bool access, bool hasctor[2])
11178 : : {
11179 : 5004 : tree fns = get_class_binding (type, complete_ctor_identifier);
11180 : 5004 : bool all_trivial = true;
11181 : :
11182 : 25039 : for (tree f : ovl_range (fns))
11183 : : {
11184 : : /* Skip template constructors. */
11185 : 12551 : if (TREE_CODE (f) != FUNCTION_DECL)
11186 : 105 : continue;
11187 : :
11188 : 12446 : bool cpy_or_move_ctor_p = copy_fn_p (f);
11189 : :
11190 : : /* Skip ctors other than default, copy, and move. */
11191 : 12446 : if (!cpy_or_move_ctor_p && !default_ctor_p (f))
11192 : 2740 : continue;
11193 : :
11194 : 9706 : if (DECL_DELETED_FN (f))
11195 : 306 : continue;
11196 : :
11197 : 9400 : bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
11198 : 9592 : || accessible_p (TYPE_BINFO (type), f, true));
11199 : :
11200 : 9280 : if (accessible)
11201 : 9280 : hasctor[cpy_or_move_ctor_p] = true;
11202 : :
11203 : 9400 : if (cpy_or_move_ctor_p && (!accessible || !trivial_fn_p (f)))
11204 : : all_trivial = false;
11205 : :
11206 : : /* Break early when both properties have been determined. */
11207 : 9400 : if (hasctor[0] && hasctor[1] && !all_trivial)
11208 : : break;
11209 : : }
11210 : :
11211 : 5004 : return all_trivial;
11212 : : }
11213 : :
11214 : : /* Issue a warning on a call to the built-in function FNDECL if it is
11215 : : a raw memory write whose destination is not an object of (something
11216 : : like) trivial or standard layout type with a non-deleted assignment
11217 : : and copy ctor. Detects const correctness violations, corrupting
11218 : : references, virtual table pointers, and bypassing non-trivial
11219 : : assignments. */
11220 : :
11221 : : static void
11222 : 65353 : maybe_warn_class_memaccess (location_t loc, tree fndecl,
11223 : : const vec<tree, va_gc> *args)
11224 : : {
11225 : : /* Except for bcopy where it's second, the destination pointer is
11226 : : the first argument for all functions handled here. Compute
11227 : : the index of the destination and source arguments. */
11228 : 65353 : unsigned dstidx = DECL_FUNCTION_CODE (fndecl) == BUILT_IN_BCOPY;
11229 : 65353 : unsigned srcidx = !dstidx;
11230 : :
11231 : 65353 : tree dest = (*args)[dstidx];
11232 : 65353 : if (!TREE_TYPE (dest)
11233 : 65353 : || (TREE_CODE (TREE_TYPE (dest)) != ARRAY_TYPE
11234 : 64203 : && !INDIRECT_TYPE_P (TREE_TYPE (dest))))
11235 : 61978 : return;
11236 : :
11237 : 20869 : tree srctype = NULL_TREE;
11238 : :
11239 : : /* Determine the type of the pointed-to object and whether it's
11240 : : a complete class type. */
11241 : 20869 : tree desttype = TREE_TYPE (TREE_TYPE (dest));
11242 : :
11243 : 20869 : if (!desttype || !COMPLETE_TYPE_P (desttype) || !CLASS_TYPE_P (desttype))
11244 : : return;
11245 : :
11246 : : /* Check to see if the raw memory call is made by a non-static member
11247 : : function with THIS as the destination argument for the destination
11248 : : type. If so, and if the class has no non-trivial bases or members,
11249 : : be more permissive. */
11250 : 5106 : if (current_function_decl
11251 : 5106 : && DECL_OBJECT_MEMBER_FUNCTION_P (current_function_decl)
11252 : 5373 : && is_object_parameter (tree_strip_nop_conversions (dest)))
11253 : : {
11254 : 204 : tree ctx = DECL_CONTEXT (current_function_decl);
11255 : 204 : bool special = same_type_ignoring_top_level_qualifiers_p (ctx, desttype);
11256 : 204 : tree binfo = TYPE_BINFO (ctx);
11257 : :
11258 : 204 : if (special
11259 : 204 : && !BINFO_VTABLE (binfo)
11260 : 378 : && !first_non_trivial_field (desttype))
11261 : : return;
11262 : : }
11263 : :
11264 : : /* True if the class is trivial. */
11265 : 5004 : bool trivial = trivial_type_p (desttype);
11266 : :
11267 : : /* Set to true if DESTYPE has an accessible copy assignment. */
11268 : 5004 : bool hasassign = false;
11269 : : /* True if all of the class' overloaded copy assignment operators
11270 : : are all trivial (and not deleted) and at least one of them is
11271 : : accessible. */
11272 : 5004 : bool trivassign = has_trivial_copy_assign_p (desttype, true, &hasassign);
11273 : :
11274 : : /* Set to true if DESTTYPE has an accessible default and copy ctor,
11275 : : respectively. */
11276 : 5004 : bool hasctors[2] = { false, false };
11277 : :
11278 : : /* True if all of the class' overloaded copy constructors are all
11279 : : trivial (and not deleted) and at least one of them is accessible. */
11280 : 5004 : bool trivcopy = has_trivial_copy_p (desttype, true, hasctors);
11281 : :
11282 : : /* Set FLD to the first private/protected member of the class. */
11283 : 5004 : tree fld = trivial ? first_non_public_field (desttype) : NULL_TREE;
11284 : :
11285 : : /* The warning format string. */
11286 : 5004 : const char *warnfmt = NULL;
11287 : : /* A suggested alternative to offer instead of the raw memory call.
11288 : : Empty string when none can be come up with. */
11289 : 5004 : const char *suggest = "";
11290 : 5004 : bool warned = false;
11291 : :
11292 : 5004 : switch (DECL_FUNCTION_CODE (fndecl))
11293 : : {
11294 : 560 : case BUILT_IN_MEMSET:
11295 : 560 : if (!integer_zerop (maybe_constant_value ((*args)[1])))
11296 : : {
11297 : : /* Diagnose setting non-copy-assignable or non-trivial types,
11298 : : or types with a private member, to (potentially) non-zero
11299 : : bytes. Since the value of the bytes being written is unknown,
11300 : : suggest using assignment instead (if one exists). Also warn
11301 : : for writes into objects for which zero-initialization doesn't
11302 : : mean all bits clear (pointer-to-member data, where null is all
11303 : : bits set). Since the value being written is (most likely)
11304 : : non-zero, simply suggest assignment (but not copy assignment). */
11305 : 196 : suggest = "; use assignment instead";
11306 : 196 : if (!trivassign)
11307 : : warnfmt = G_("%qD writing to an object of type %#qT with "
11308 : : "no trivial copy-assignment");
11309 : 125 : else if (!trivial)
11310 : : warnfmt = G_("%qD writing to an object of non-trivial type %#qT%s");
11311 : 85 : else if (fld)
11312 : : {
11313 : 24 : const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
11314 : 24 : warned = warning_at (loc, OPT_Wclass_memaccess,
11315 : : "%qD writing to an object of type %#qT with "
11316 : : "%qs member %qD",
11317 : : fndecl, desttype, access, fld);
11318 : : }
11319 : 61 : else if (!zero_init_p (desttype))
11320 : : warnfmt = G_("%qD writing to an object of type %#qT containing "
11321 : : "a pointer to data member%s");
11322 : :
11323 : : break;
11324 : : }
11325 : : /* Fall through. */
11326 : :
11327 : 481 : case BUILT_IN_BZERO:
11328 : : /* Similarly to the above, diagnose clearing non-trivial or non-
11329 : : standard layout objects, or objects of types with no assignmenmt.
11330 : : Since the value being written is known to be zero, suggest either
11331 : : copy assignment, copy ctor, or default ctor as an alternative,
11332 : : depending on what's available. */
11333 : :
11334 : 481 : if (hasassign && hasctors[0])
11335 : : suggest = G_("; use assignment or value-initialization instead");
11336 : 49 : else if (hasassign)
11337 : : suggest = G_("; use assignment instead");
11338 : 31 : else if (hasctors[0])
11339 : 16 : suggest = G_("; use value-initialization instead");
11340 : :
11341 : 481 : if (!trivassign)
11342 : : warnfmt = G_("%qD clearing an object of type %#qT with "
11343 : : "no trivial copy-assignment%s");
11344 : 374 : else if (!trivial)
11345 : : warnfmt = G_("%qD clearing an object of non-trivial type %#qT%s");
11346 : 304 : else if (!zero_init_p (desttype))
11347 : : warnfmt = G_("%qD clearing an object of type %#qT containing "
11348 : : "a pointer-to-member%s");
11349 : : break;
11350 : :
11351 : 2437 : case BUILT_IN_BCOPY:
11352 : 2437 : case BUILT_IN_MEMCPY:
11353 : 2437 : case BUILT_IN_MEMMOVE:
11354 : 2437 : case BUILT_IN_MEMPCPY:
11355 : : /* Determine the type of the source object. */
11356 : 2437 : srctype = TREE_TYPE ((*args)[srcidx]);
11357 : 2437 : if (!srctype || !INDIRECT_TYPE_P (srctype))
11358 : 0 : srctype = void_type_node;
11359 : : else
11360 : 2437 : srctype = TREE_TYPE (srctype);
11361 : :
11362 : : /* Since it's impossible to determine wheter the byte copy is
11363 : : being used in place of assignment to an existing object or
11364 : : as a substitute for initialization, assume it's the former.
11365 : : Determine the best alternative to use instead depending on
11366 : : what's not deleted. */
11367 : 2437 : if (hasassign && hasctors[1])
11368 : : suggest = G_("; use copy-assignment or copy-initialization instead");
11369 : 488 : else if (hasassign)
11370 : : suggest = G_("; use copy-assignment instead");
11371 : 341 : else if (hasctors[1])
11372 : 290 : suggest = G_("; use copy-initialization instead");
11373 : :
11374 : 2437 : if (!trivassign)
11375 : : warnfmt = G_("%qD writing to an object of type %#qT with no trivial "
11376 : : "copy-assignment%s");
11377 : 1639 : else if (!trivially_copyable_p (desttype))
11378 : : warnfmt = G_("%qD writing to an object of non-trivially copyable "
11379 : : "type %#qT%s");
11380 : 1315 : else if (!trivcopy)
11381 : : warnfmt = G_("%qD writing to an object with a deleted copy constructor");
11382 : :
11383 : 1315 : else if (!trivial
11384 : 211 : && !VOID_TYPE_P (srctype)
11385 : 160 : && !is_byte_access_type (srctype)
11386 : 1412 : && !same_type_ignoring_top_level_qualifiers_p (desttype,
11387 : : srctype))
11388 : : {
11389 : : /* Warn when copying into a non-trivial object from an object
11390 : : of a different type other than void or char. */
11391 : 54 : warned = warning_at (loc, OPT_Wclass_memaccess,
11392 : : "%qD copying an object of non-trivial type "
11393 : : "%#qT from an array of %#qT",
11394 : : fndecl, desttype, srctype);
11395 : : }
11396 : 1261 : else if (fld
11397 : 432 : && !VOID_TYPE_P (srctype)
11398 : 384 : && !is_byte_access_type (srctype)
11399 : 1549 : && !same_type_ignoring_top_level_qualifiers_p (desttype,
11400 : : srctype))
11401 : : {
11402 : 216 : const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
11403 : 216 : warned = warning_at (loc, OPT_Wclass_memaccess,
11404 : : "%qD copying an object of type %#qT with "
11405 : : "%qs member %qD from an array of %#qT; use "
11406 : : "assignment or copy-initialization instead",
11407 : : fndecl, desttype, access, fld, srctype);
11408 : : }
11409 : 1045 : else if (!trivial && vec_safe_length (args) > 2)
11410 : : {
11411 : 157 : tree sz = maybe_constant_value ((*args)[2]);
11412 : 157 : if (!tree_fits_uhwi_p (sz))
11413 : : break;
11414 : :
11415 : : /* Finally, warn on partial copies. */
11416 : 99 : unsigned HOST_WIDE_INT typesize
11417 : 99 : = tree_to_uhwi (TYPE_SIZE_UNIT (desttype));
11418 : 99 : if (typesize == 0)
11419 : : break;
11420 : 96 : if (unsigned HOST_WIDE_INT partial = tree_to_uhwi (sz) % typesize)
11421 : 12 : warned = warning_at (loc, OPT_Wclass_memaccess,
11422 : : (typesize - partial > 1
11423 : : ? G_("%qD writing to an object of "
11424 : : "a non-trivial type %#qT leaves %wu "
11425 : : "bytes unchanged")
11426 : : : G_("%qD writing to an object of "
11427 : : "a non-trivial type %#qT leaves %wu "
11428 : : "byte unchanged")),
11429 : : fndecl, desttype, typesize - partial);
11430 : : }
11431 : : break;
11432 : :
11433 : 261 : case BUILT_IN_REALLOC:
11434 : :
11435 : 261 : if (!trivially_copyable_p (desttype))
11436 : : warnfmt = G_("%qD moving an object of non-trivially copyable type "
11437 : : "%#qT; use %<new%> and %<delete%> instead");
11438 : 138 : else if (!trivcopy)
11439 : : warnfmt = G_("%qD moving an object of type %#qT with deleted copy "
11440 : : "constructor; use %<new%> and %<delete%> instead");
11441 : 135 : else if (!get_dtor (desttype, tf_none))
11442 : : warnfmt = G_("%qD moving an object of type %#qT with deleted "
11443 : : "destructor");
11444 : 126 : else if (!trivial)
11445 : : {
11446 : 33 : tree sz = maybe_constant_value ((*args)[1]);
11447 : 33 : if (TREE_CODE (sz) == INTEGER_CST
11448 : 33 : && tree_int_cst_lt (sz, TYPE_SIZE_UNIT (desttype)))
11449 : : /* Finally, warn on reallocation into insufficient space. */
11450 : 7 : warned = warning_at (loc, OPT_Wclass_memaccess,
11451 : : "%qD moving an object of non-trivial type "
11452 : : "%#qT and size %E into a region of size %E",
11453 : 7 : fndecl, desttype, TYPE_SIZE_UNIT (desttype),
11454 : : sz);
11455 : : }
11456 : : break;
11457 : :
11458 : : default:
11459 : : return;
11460 : : }
11461 : :
11462 : 310 : if (warnfmt)
11463 : : {
11464 : 1569 : if (suggest)
11465 : 1569 : warned = warning_at (loc, OPT_Wclass_memaccess,
11466 : : warnfmt, fndecl, desttype, suggest);
11467 : : else
11468 : : warned = warning_at (loc, OPT_Wclass_memaccess,
11469 : : warnfmt, fndecl, desttype);
11470 : : }
11471 : :
11472 : 3375 : if (warned)
11473 : 1879 : inform (location_of (desttype), "%#qT declared here", desttype);
11474 : : }
11475 : :
11476 : : /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
11477 : : If FN is the result of resolving an overloaded target built-in,
11478 : : ORIG_FNDECL is the original function decl, otherwise it is null.
11479 : : This function performs no overload resolution, conversion, or other
11480 : : high-level operations. */
11481 : :
11482 : : tree
11483 : 125347932 : build_cxx_call (tree fn, int nargs, tree *argarray,
11484 : : tsubst_flags_t complain, tree orig_fndecl)
11485 : : {
11486 : 125347932 : tree fndecl;
11487 : :
11488 : : /* Remember roughly where this call is. */
11489 : 125347932 : location_t loc = cp_expr_loc_or_input_loc (fn);
11490 : 125347932 : fn = build_call_a (fn, nargs, argarray);
11491 : 125347932 : SET_EXPR_LOCATION (fn, loc);
11492 : :
11493 : 125347932 : fndecl = get_callee_fndecl (fn);
11494 : 125347932 : if (!orig_fndecl)
11495 : 125347932 : orig_fndecl = fndecl;
11496 : :
11497 : : /* Check that arguments to builtin functions match the expectations. */
11498 : 125347932 : if (fndecl
11499 : 122895584 : && !processing_template_decl
11500 : 248187952 : && fndecl_built_in_p (fndecl))
11501 : : {
11502 : : int i;
11503 : :
11504 : : /* We need to take care that values to BUILT_IN_NORMAL
11505 : : are reduced. */
11506 : 19946054 : for (i = 0; i < nargs; i++)
11507 : 12909980 : argarray[i] = maybe_constant_value (argarray[i]);
11508 : :
11509 : 7036074 : if (!check_builtin_function_arguments (EXPR_LOCATION (fn), vNULL, fndecl,
11510 : : orig_fndecl, nargs, argarray,
11511 : : complain & tf_error))
11512 : 804 : return error_mark_node;
11513 : 7035270 : else if (fndecl_built_in_p (fndecl, BUILT_IN_CLEAR_PADDING))
11514 : : {
11515 : 9800 : tree arg0 = argarray[0];
11516 : 9800 : STRIP_NOPS (arg0);
11517 : 9800 : if (TREE_CODE (arg0) == ADDR_EXPR
11518 : 263 : && DECL_P (TREE_OPERAND (arg0, 0))
11519 : 10042 : && same_type_ignoring_top_level_qualifiers_p
11520 : 242 : (TREE_TYPE (TREE_TYPE (argarray[0])),
11521 : 242 : TREE_TYPE (TREE_TYPE (arg0))))
11522 : : /* For __builtin_clear_padding (&var) we know the type
11523 : : is for a complete object, so there is no risk in clearing
11524 : : padding that is reused in some derived class member. */;
11525 : 9565 : else if (!trivially_copyable_p (TREE_TYPE (TREE_TYPE (argarray[0]))))
11526 : : {
11527 : 18 : error_at (EXPR_LOC_OR_LOC (argarray[0], input_location),
11528 : : "argument %u in call to function %qE "
11529 : : "has pointer to a non-trivially-copyable type (%qT)",
11530 : 18 : 1, fndecl, TREE_TYPE (argarray[0]));
11531 : 18 : return error_mark_node;
11532 : : }
11533 : : }
11534 : : }
11535 : :
11536 : 125347110 : if (VOID_TYPE_P (TREE_TYPE (fn)))
11537 : : return fn;
11538 : :
11539 : : /* 5.2.2/11: If a function call is a prvalue of object type: if the
11540 : : function call is either the operand of a decltype-specifier or the
11541 : : right operand of a comma operator that is the operand of a
11542 : : decltype-specifier, a temporary object is not introduced for the
11543 : : prvalue. The type of the prvalue may be incomplete. */
11544 : 95049733 : if (!(complain & tf_decltype))
11545 : : {
11546 : 79884346 : fn = require_complete_type (fn, complain);
11547 : 79884346 : if (fn == error_mark_node)
11548 : : return error_mark_node;
11549 : :
11550 : 79884322 : if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
11551 : : {
11552 : 8977892 : fn = build_cplus_new (TREE_TYPE (fn), fn, complain);
11553 : 8977892 : maybe_warn_parm_abi (TREE_TYPE (fn), loc);
11554 : : }
11555 : : }
11556 : 95049709 : return convert_from_reference (fn);
11557 : : }
11558 : :
11559 : : /* Returns the value to use for the in-charge parameter when making a
11560 : : call to a function with the indicated NAME.
11561 : :
11562 : : FIXME:Can't we find a neater way to do this mapping? */
11563 : :
11564 : : tree
11565 : 41332 : in_charge_arg_for_name (tree name)
11566 : : {
11567 : 41332 : if (IDENTIFIER_CTOR_P (name))
11568 : : {
11569 : 23160 : if (name == complete_ctor_identifier)
11570 : 11580 : return integer_one_node;
11571 : 11580 : gcc_checking_assert (name == base_ctor_identifier);
11572 : : }
11573 : : else
11574 : : {
11575 : 18172 : if (name == complete_dtor_identifier)
11576 : 9086 : return integer_two_node;
11577 : 9086 : else if (name == deleting_dtor_identifier)
11578 : : /* The deleting dtor should now be handled by
11579 : : build_delete_destructor_body. */
11580 : 0 : gcc_unreachable ();
11581 : 9086 : gcc_checking_assert (name == base_dtor_identifier);
11582 : : }
11583 : :
11584 : 20666 : return integer_zero_node;
11585 : : }
11586 : :
11587 : : /* We've built up a constructor call RET. Complain if it delegates to the
11588 : : constructor we're currently compiling. */
11589 : :
11590 : : static void
11591 : 291719 : check_self_delegation (tree ret)
11592 : : {
11593 : 291719 : if (TREE_CODE (ret) == TARGET_EXPR)
11594 : 0 : ret = TARGET_EXPR_INITIAL (ret);
11595 : 291719 : tree fn = cp_get_callee_fndecl_nofold (ret);
11596 : 291719 : if (fn && DECL_ABSTRACT_ORIGIN (fn) == current_function_decl)
11597 : 46 : error ("constructor delegates to itself");
11598 : 291719 : }
11599 : :
11600 : : /* Build a call to a constructor, destructor, or an assignment
11601 : : operator for INSTANCE, an expression with class type. NAME
11602 : : indicates the special member function to call; *ARGS are the
11603 : : arguments. ARGS may be NULL. This may change ARGS. BINFO
11604 : : indicates the base of INSTANCE that is to be passed as the `this'
11605 : : parameter to the member function called.
11606 : :
11607 : : FLAGS are the LOOKUP_* flags to use when processing the call.
11608 : :
11609 : : If NAME indicates a complete object constructor, INSTANCE may be
11610 : : NULL_TREE. In this case, the caller will call build_cplus_new to
11611 : : store the newly constructed object into a VAR_DECL. */
11612 : :
11613 : : tree
11614 : 30352923 : build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
11615 : : tree binfo, int flags, tsubst_flags_t complain)
11616 : : {
11617 : 30352923 : tree fns;
11618 : : /* The type of the subobject to be constructed or destroyed. */
11619 : 30352923 : tree class_type;
11620 : 30352923 : vec<tree, va_gc> *allocated = NULL;
11621 : 30352923 : tree ret;
11622 : :
11623 : 30352923 : gcc_assert (IDENTIFIER_CDTOR_P (name) || name == assign_op_identifier);
11624 : :
11625 : 30352923 : if (error_operand_p (instance))
11626 : 0 : return error_mark_node;
11627 : :
11628 : 30352923 : if (IDENTIFIER_DTOR_P (name))
11629 : : {
11630 : 8808389 : gcc_assert (args == NULL || vec_safe_is_empty (*args));
11631 : 8808389 : if (!type_build_dtor_call (TREE_TYPE (instance)))
11632 : : /* Shortcut to avoid lazy destructor declaration. */
11633 : 38810 : return build_trivial_dtor_call (instance);
11634 : : }
11635 : :
11636 : 30314113 : if (TYPE_P (binfo))
11637 : : {
11638 : : /* Resolve the name. */
11639 : 23453650 : if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
11640 : 24 : return error_mark_node;
11641 : :
11642 : 23453626 : binfo = TYPE_BINFO (binfo);
11643 : : }
11644 : :
11645 : 23453626 : gcc_assert (binfo != NULL_TREE);
11646 : :
11647 : 30314089 : class_type = BINFO_TYPE (binfo);
11648 : :
11649 : : /* Handle the special case where INSTANCE is NULL_TREE. */
11650 : 30314089 : if (name == complete_ctor_identifier && !instance)
11651 : 15326914 : instance = build_dummy_object (class_type);
11652 : : else
11653 : : {
11654 : : /* Convert to the base class, if necessary. */
11655 : 14987175 : if (!same_type_ignoring_top_level_qualifiers_p
11656 : 14987175 : (TREE_TYPE (instance), BINFO_TYPE (binfo)))
11657 : : {
11658 : 1695030 : if (IDENTIFIER_CDTOR_P (name))
11659 : : /* For constructors and destructors, either the base is
11660 : : non-virtual, or it is virtual but we are doing the
11661 : : conversion from a constructor or destructor for the
11662 : : complete object. In either case, we can convert
11663 : : statically. */
11664 : 1674494 : instance = convert_to_base_statically (instance, binfo);
11665 : : else
11666 : : {
11667 : : /* However, for assignment operators, we must convert
11668 : : dynamically if the base is virtual. */
11669 : 20536 : gcc_checking_assert (name == assign_op_identifier);
11670 : 20536 : instance = build_base_path (PLUS_EXPR, instance,
11671 : : binfo, /*nonnull=*/1, complain);
11672 : : }
11673 : : }
11674 : : }
11675 : :
11676 : 30314089 : gcc_assert (instance != NULL_TREE);
11677 : :
11678 : : /* In C++17, "If the initializer expression is a prvalue and the
11679 : : cv-unqualified version of the source type is the same class as the class
11680 : : of the destination, the initializer expression is used to initialize the
11681 : : destination object." Handle that here to avoid doing overload
11682 : : resolution. */
11683 : 30314089 : if (cxx_dialect >= cxx17
11684 : 30113792 : && args && vec_safe_length (*args) == 1
11685 : 47210422 : && !unsafe_return_slot_p (instance))
11686 : : {
11687 : 15827489 : tree arg = (**args)[0];
11688 : :
11689 : 800779 : if (BRACE_ENCLOSED_INITIALIZER_P (arg)
11690 : 800762 : && !TYPE_HAS_LIST_CTOR (class_type)
11691 : 748892 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (arg)
11692 : 16576367 : && CONSTRUCTOR_NELTS (arg) == 1)
11693 : 439081 : arg = CONSTRUCTOR_ELT (arg, 0)->value;
11694 : :
11695 : 15827489 : if ((TREE_CODE (arg) == TARGET_EXPR
11696 : 7027856 : || TREE_CODE (arg) == CONSTRUCTOR)
11697 : 24988900 : && (same_type_ignoring_top_level_qualifiers_p
11698 : 9161411 : (class_type, TREE_TYPE (arg))))
11699 : : {
11700 : 8404103 : if (is_dummy_object (instance))
11701 : : return arg;
11702 : 161921 : else if (TREE_CODE (arg) == TARGET_EXPR)
11703 : 161921 : TARGET_EXPR_DIRECT_INIT_P (arg) = true;
11704 : :
11705 : 161921 : if ((complain & tf_error)
11706 : 161919 : && (flags & LOOKUP_DELEGATING_CONS))
11707 : 0 : check_self_delegation (arg);
11708 : : /* Avoid change of behavior on Wunused-var-2.C. */
11709 : 161921 : instance = mark_lvalue_use (instance);
11710 : 161921 : return cp_build_init_expr (instance, arg);
11711 : : }
11712 : : }
11713 : :
11714 : 21909986 : fns = lookup_fnfields (binfo, name, 1, complain);
11715 : :
11716 : : /* When making a call to a constructor or destructor for a subobject
11717 : : that uses virtual base classes, pass down a pointer to a VTT for
11718 : : the subobject. */
11719 : 21909986 : if ((name == base_ctor_identifier
11720 : 19779435 : || name == base_dtor_identifier)
11721 : 23584600 : && CLASSTYPE_VBASECLASSES (class_type))
11722 : : {
11723 : 50095 : tree vtt;
11724 : 50095 : tree sub_vtt;
11725 : :
11726 : : /* If the current function is a complete object constructor
11727 : : or destructor, then we fetch the VTT directly.
11728 : : Otherwise, we look it up using the VTT we were given. */
11729 : 50095 : vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
11730 : 50095 : vtt = decay_conversion (vtt, complain);
11731 : 50095 : if (vtt == error_mark_node)
11732 : 0 : return error_mark_node;
11733 : 50095 : vtt = build_if_in_charge (vtt, current_vtt_parm);
11734 : 50095 : if (BINFO_SUBVTT_INDEX (binfo))
11735 : 49835 : sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
11736 : : else
11737 : 260 : sub_vtt = vtt;
11738 : :
11739 : 50095 : if (args == NULL)
11740 : : {
11741 : 31972 : allocated = make_tree_vector ();
11742 : 31972 : args = &allocated;
11743 : : }
11744 : :
11745 : 50095 : vec_safe_insert (*args, 0, sub_vtt);
11746 : : }
11747 : :
11748 : 43819972 : ret = build_new_method_call (instance, fns, args,
11749 : 21909986 : TYPE_BINFO (BINFO_TYPE (binfo)),
11750 : : flags, /*fn=*/NULL,
11751 : : complain);
11752 : :
11753 : 21909986 : if (allocated != NULL)
11754 : 31972 : release_tree_vector (allocated);
11755 : :
11756 : 21909986 : if ((complain & tf_error)
11757 : 19827568 : && (flags & LOOKUP_DELEGATING_CONS)
11758 : 291859 : && name == complete_ctor_identifier)
11759 : 291719 : check_self_delegation (ret);
11760 : :
11761 : : return ret;
11762 : : }
11763 : :
11764 : : /* Return the NAME, as a C string. The NAME indicates a function that
11765 : : is a member of TYPE. *FREE_P is set to true if the caller must
11766 : : free the memory returned.
11767 : :
11768 : : Rather than go through all of this, we should simply set the names
11769 : : of constructors and destructors appropriately, and dispense with
11770 : : ctor_identifier, dtor_identifier, etc. */
11771 : :
11772 : : static char *
11773 : 88 : name_as_c_string (tree name, tree type, bool *free_p)
11774 : : {
11775 : 88 : const char *pretty_name;
11776 : :
11777 : : /* Assume that we will not allocate memory. */
11778 : 88 : *free_p = false;
11779 : : /* Constructors and destructors are special. */
11780 : 88 : if (IDENTIFIER_CDTOR_P (name))
11781 : : {
11782 : 42 : pretty_name
11783 : 42 : = identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type)));
11784 : : /* For a destructor, add the '~'. */
11785 : 42 : if (IDENTIFIER_DTOR_P (name))
11786 : : {
11787 : 0 : pretty_name = concat ("~", pretty_name, NULL);
11788 : : /* Remember that we need to free the memory allocated. */
11789 : 0 : *free_p = true;
11790 : : }
11791 : : }
11792 : 46 : else if (IDENTIFIER_CONV_OP_P (name))
11793 : : {
11794 : 0 : pretty_name = concat ("operator ",
11795 : 0 : type_as_string_translate (TREE_TYPE (name),
11796 : : TFF_PLAIN_IDENTIFIER),
11797 : : NULL);
11798 : : /* Remember that we need to free the memory allocated. */
11799 : 0 : *free_p = true;
11800 : : }
11801 : : else
11802 : 46 : pretty_name = identifier_to_locale (IDENTIFIER_POINTER (name));
11803 : :
11804 : 88 : return CONST_CAST (char *, pretty_name);
11805 : : }
11806 : :
11807 : : /* If CANDIDATES contains exactly one candidate, return it, otherwise
11808 : : return NULL. */
11809 : :
11810 : : static z_candidate *
11811 : 688 : single_z_candidate (z_candidate *candidates)
11812 : : {
11813 : 0 : if (candidates == NULL)
11814 : : return NULL;
11815 : :
11816 : 676 : if (candidates->next)
11817 : 0 : return NULL;
11818 : :
11819 : : return candidates;
11820 : : }
11821 : :
11822 : : /* If CANDIDATE is invalid due to a bad argument type, return the
11823 : : pertinent conversion_info.
11824 : :
11825 : : Otherwise, return NULL. */
11826 : :
11827 : : static const conversion_info *
11828 : 335 : maybe_get_bad_conversion_for_unmatched_call (const z_candidate *candidate)
11829 : : {
11830 : : /* Must be an rr_arg_conversion or rr_bad_arg_conversion. */
11831 : 335 : rejection_reason *r = candidate->reason;
11832 : :
11833 : 335 : if (r == NULL)
11834 : : return NULL;
11835 : :
11836 : 335 : switch (r->code)
11837 : : {
11838 : : default:
11839 : : return NULL;
11840 : :
11841 : 50 : case rr_arg_conversion:
11842 : 50 : return &r->u.conversion;
11843 : :
11844 : 6 : case rr_bad_arg_conversion:
11845 : 6 : return &r->u.bad_conversion;
11846 : : }
11847 : : }
11848 : :
11849 : : /* Issue an error and note complaining about a bad argument type at a
11850 : : callsite with a single candidate FNDECL.
11851 : :
11852 : : ARG_LOC is the location of the argument (or UNKNOWN_LOCATION, in which
11853 : : case input_location is used).
11854 : : FROM_TYPE is the type of the actual argument; TO_TYPE is the type of
11855 : : the formal parameter. */
11856 : :
11857 : : void
11858 : 148 : complain_about_bad_argument (location_t arg_loc,
11859 : : tree from_type, tree to_type,
11860 : : tree fndecl, int parmnum)
11861 : : {
11862 : 148 : auto_diagnostic_group d;
11863 : 148 : range_label_for_type_mismatch rhs_label (from_type, to_type);
11864 : 148 : range_label *label = &rhs_label;
11865 : 148 : if (arg_loc == UNKNOWN_LOCATION)
11866 : : {
11867 : 6 : arg_loc = input_location;
11868 : 6 : label = NULL;
11869 : : }
11870 : 148 : gcc_rich_location richloc (arg_loc, label, highlight_colors::percent_h);
11871 : 148 : error_at (&richloc,
11872 : : "cannot convert %qH to %qI",
11873 : : from_type, to_type);
11874 : 148 : maybe_inform_about_fndecl_for_bogus_argument_init
11875 : 148 : (fndecl,
11876 : : parmnum,
11877 : : highlight_colors::percent_i);
11878 : 148 : }
11879 : :
11880 : : /* Subroutine of build_new_method_call_1, for where there are no viable
11881 : : candidates for the call. */
11882 : :
11883 : : static void
11884 : 694 : complain_about_no_candidates_for_method_call (tree instance,
11885 : : z_candidate *candidates,
11886 : : tree explicit_targs,
11887 : : tree basetype,
11888 : : tree optype, tree name,
11889 : : bool skip_first_for_error,
11890 : : vec<tree, va_gc> *user_args)
11891 : : {
11892 : 694 : auto_diagnostic_group d;
11893 : 694 : if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
11894 : 0 : cxx_incomplete_type_error (instance, basetype);
11895 : 694 : else if (optype)
11896 : 6 : error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
11897 : : basetype, optype, build_tree_list_vec (user_args),
11898 : 6 : TREE_TYPE (instance));
11899 : : else
11900 : : {
11901 : : /* Special-case for when there's a single candidate that's failing
11902 : : due to a bad argument type. */
11903 : 688 : if (z_candidate *candidate = single_z_candidate (candidates))
11904 : 335 : if (const conversion_info *conv
11905 : 335 : = maybe_get_bad_conversion_for_unmatched_call (candidate))
11906 : : {
11907 : 56 : tree from_type = conv->from;
11908 : 56 : if (!TYPE_P (conv->from))
11909 : 6 : from_type = lvalue_type (conv->from);
11910 : 56 : complain_about_bad_argument (conv->loc,
11911 : 56 : from_type, conv->to_type,
11912 : 56 : candidate->fn, conv->n_arg);
11913 : 56 : return;
11914 : : }
11915 : :
11916 : 632 : tree arglist = build_tree_list_vec (user_args);
11917 : 632 : tree errname = name;
11918 : 632 : bool twiddle = false;
11919 : 632 : if (IDENTIFIER_CDTOR_P (errname))
11920 : : {
11921 : 331 : twiddle = IDENTIFIER_DTOR_P (errname);
11922 : 331 : errname = constructor_name (basetype);
11923 : : }
11924 : 632 : if (explicit_targs)
11925 : 46 : errname = lookup_template_function (errname, explicit_targs);
11926 : 632 : if (skip_first_for_error)
11927 : 3 : arglist = TREE_CHAIN (arglist);
11928 : 1264 : error ("no matching function for call to %<%T::%s%E(%A)%#V%>",
11929 : 632 : basetype, &"~"[!twiddle], errname, arglist,
11930 : 632 : TREE_TYPE (instance));
11931 : : }
11932 : 638 : print_z_candidates (location_of (name), candidates);
11933 : 694 : }
11934 : :
11935 : : /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
11936 : : be set, upon return, to the function called. ARGS may be NULL.
11937 : : This may change ARGS. */
11938 : :
11939 : : tree
11940 : 84427065 : build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args,
11941 : : tree conversion_path, int flags,
11942 : : tree *fn_p, tsubst_flags_t complain)
11943 : : {
11944 : 84427065 : struct z_candidate *candidates = 0, *cand;
11945 : 84427065 : tree explicit_targs = NULL_TREE;
11946 : 84427065 : tree basetype = NULL_TREE;
11947 : 84427065 : tree access_binfo;
11948 : 84427065 : tree optype;
11949 : 84427065 : tree first_mem_arg = NULL_TREE;
11950 : 84427065 : tree name;
11951 : 84427065 : bool skip_first_for_error;
11952 : 84427065 : vec<tree, va_gc> *user_args;
11953 : 84427065 : tree call;
11954 : 84427065 : tree fn;
11955 : 84427065 : int template_only = 0;
11956 : 84427065 : bool any_viable_p;
11957 : 84427065 : tree orig_instance;
11958 : 84427065 : tree orig_fns;
11959 : 84427065 : vec<tree, va_gc> *orig_args = NULL;
11960 : :
11961 : 84427065 : auto_cond_timevar tv (TV_OVERLOAD);
11962 : :
11963 : 84427065 : gcc_assert (instance != NULL_TREE);
11964 : :
11965 : : /* We don't know what function we're going to call, yet. */
11966 : 84427065 : if (fn_p)
11967 : 23202295 : *fn_p = NULL_TREE;
11968 : :
11969 : 84427065 : if (error_operand_p (instance)
11970 : 84427065 : || !fns || error_operand_p (fns))
11971 : 1414544 : return error_mark_node;
11972 : :
11973 : 83012521 : if (!BASELINK_P (fns))
11974 : : {
11975 : 0 : if (complain & tf_error)
11976 : 0 : error ("call to non-function %qD", fns);
11977 : 0 : return error_mark_node;
11978 : : }
11979 : :
11980 : 83012521 : orig_instance = instance;
11981 : 83012521 : orig_fns = fns;
11982 : :
11983 : : /* Dismantle the baselink to collect all the information we need. */
11984 : 83012521 : if (!conversion_path)
11985 : 39328562 : conversion_path = BASELINK_BINFO (fns);
11986 : 83012521 : access_binfo = BASELINK_ACCESS_BINFO (fns);
11987 : 83012521 : optype = BASELINK_OPTYPE (fns);
11988 : 83012521 : fns = BASELINK_FUNCTIONS (fns);
11989 : 83012521 : if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
11990 : : {
11991 : 6808880 : explicit_targs = TREE_OPERAND (fns, 1);
11992 : 6808880 : fns = TREE_OPERAND (fns, 0);
11993 : 6808880 : template_only = 1;
11994 : : }
11995 : 83012521 : gcc_assert (OVL_P (fns));
11996 : 83012521 : fn = OVL_FIRST (fns);
11997 : 83012521 : name = DECL_NAME (fn);
11998 : :
11999 : 83012521 : basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
12000 : 83012521 : gcc_assert (CLASS_TYPE_P (basetype));
12001 : :
12002 : 83012521 : user_args = args == NULL ? NULL : *args;
12003 : : /* Under DR 147 A::A() is an invalid constructor call,
12004 : : not a functional cast. */
12005 : 83012521 : if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
12006 : : {
12007 : 54 : if (! (complain & tf_error))
12008 : 0 : return error_mark_node;
12009 : :
12010 : 54 : basetype = DECL_CONTEXT (fn);
12011 : 54 : name = constructor_name (basetype);
12012 : 54 : auto_diagnostic_group d;
12013 : 54 : if (permerror (input_location,
12014 : : "cannot call constructor %<%T::%D%> directly",
12015 : : basetype, name))
12016 : 54 : inform (input_location, "for a function-style cast, remove the "
12017 : : "redundant %<::%D%>", name);
12018 : 54 : call = build_functional_cast (input_location, basetype,
12019 : : build_tree_list_vec (user_args),
12020 : : complain);
12021 : 54 : return call;
12022 : 54 : }
12023 : :
12024 : 83012467 : if (processing_template_decl)
12025 : 8063154 : orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
12026 : :
12027 : : /* Process the argument list. */
12028 : 82910123 : if (args != NULL && *args != NULL)
12029 : : {
12030 : 70856855 : *args = resolve_args (*args, complain);
12031 : 70856855 : if (*args == NULL)
12032 : 120 : return error_mark_node;
12033 : : user_args = *args;
12034 : : }
12035 : :
12036 : : /* Consider the object argument to be used even if we end up selecting a
12037 : : static member function. */
12038 : 83012347 : instance = mark_type_use (instance);
12039 : :
12040 : : /* Figure out whether to skip the first argument for the error
12041 : : message we will display to users if an error occurs. We don't
12042 : : want to display any compiler-generated arguments. The "this"
12043 : : pointer hasn't been added yet. However, we must remove the VTT
12044 : : pointer if this is a call to a base-class constructor or
12045 : : destructor. */
12046 : 83012347 : skip_first_for_error = false;
12047 : 83012347 : if (IDENTIFIER_CDTOR_P (name))
12048 : : {
12049 : : /* Callers should explicitly indicate whether they want to ctor
12050 : : the complete object or just the part without virtual bases. */
12051 : 41577818 : gcc_assert (name != ctor_identifier);
12052 : :
12053 : : /* Remove the VTT pointer, if present. */
12054 : 39447273 : if ((name == base_ctor_identifier || name == base_dtor_identifier)
12055 : 43252432 : && CLASSTYPE_VBASECLASSES (basetype))
12056 : : skip_first_for_error = true;
12057 : :
12058 : : /* It's OK to call destructors and constructors on cv-qualified
12059 : : objects. Therefore, convert the INSTANCE to the unqualified
12060 : : type, if necessary. */
12061 : 41577818 : if (!same_type_p (basetype, TREE_TYPE (instance)))
12062 : : {
12063 : 395893 : instance = build_this (instance);
12064 : 395893 : instance = build_nop (build_pointer_type (basetype), instance);
12065 : 395893 : instance = build_fold_indirect_ref (instance);
12066 : : }
12067 : : }
12068 : : else
12069 : 82869058 : gcc_assert (!DECL_DESTRUCTOR_P (fn) && !DECL_CONSTRUCTOR_P (fn));
12070 : :
12071 : : /* For the overload resolution we need to find the actual `this`
12072 : : that would be captured if the call turns out to be to a
12073 : : non-static member function. Do not actually capture it at this
12074 : : point. */
12075 : 166024694 : if (DECL_CONSTRUCTOR_P (fn))
12076 : : /* Constructors don't use the enclosing 'this'. */
12077 : : first_mem_arg = instance;
12078 : : else
12079 : 63032675 : first_mem_arg = maybe_resolve_dummy (instance, false);
12080 : :
12081 : 83012347 : conversion_obstack_sentinel cos;
12082 : :
12083 : : /* The number of arguments artificial parms in ARGS; we subtract one because
12084 : : there's no 'this' in ARGS. */
12085 : 83012347 : unsigned skip = num_artificial_parms_for (fn) - 1;
12086 : :
12087 : : /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
12088 : : initializer, not T({ }). */
12089 : 83012347 : if (DECL_CONSTRUCTOR_P (fn)
12090 : 16580183 : && vec_safe_length (user_args) > skip
12091 : 97642723 : && DIRECT_LIST_INIT_P ((*user_args)[skip]))
12092 : : {
12093 : 801373 : tree init_list = (*user_args)[skip];
12094 : 801373 : tree init = NULL_TREE;
12095 : :
12096 : 801373 : gcc_assert (user_args->length () == skip + 1
12097 : : && !(flags & LOOKUP_ONLYCONVERTING));
12098 : :
12099 : : /* If the initializer list has no elements and T is a class type with
12100 : : a default constructor, the object is value-initialized. Handle
12101 : : this here so we don't need to handle it wherever we use
12102 : : build_special_member_call. */
12103 : 801373 : if (CONSTRUCTOR_NELTS (init_list) == 0
12104 : 126935 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
12105 : : /* For a user-provided default constructor, use the normal
12106 : : mechanisms so that protected access works. */
12107 : 126935 : && type_has_non_user_provided_default_constructor (basetype)
12108 : 766110 : && !processing_template_decl)
12109 : 91669 : init = build_value_init (basetype, complain);
12110 : :
12111 : : /* If BASETYPE is an aggregate, we need to do aggregate
12112 : : initialization. */
12113 : 709704 : else if (CP_AGGREGATE_TYPE_P (basetype))
12114 : : {
12115 : 42 : init = reshape_init (basetype, init_list, complain);
12116 : 42 : init = digest_init (basetype, init, complain);
12117 : : }
12118 : :
12119 : 91711 : if (init)
12120 : : {
12121 : 91711 : if (is_dummy_object (instance))
12122 : 30257 : return get_target_expr (init, complain);
12123 : 61454 : return cp_build_init_expr (instance, init);
12124 : : }
12125 : :
12126 : : /* Otherwise go ahead with overload resolution. */
12127 : 709662 : add_list_candidates (fns, first_mem_arg, user_args,
12128 : : basetype, explicit_targs, template_only,
12129 : : conversion_path, access_binfo, flags,
12130 : : &candidates, complain);
12131 : : }
12132 : : else
12133 : 82210974 : add_candidates (fns, first_mem_arg, user_args, optype,
12134 : : explicit_targs, template_only, conversion_path,
12135 : : access_binfo, flags, &candidates, complain);
12136 : :
12137 : 82920636 : any_viable_p = false;
12138 : 82920636 : candidates = splice_viable (candidates, false, &any_viable_p);
12139 : :
12140 : 82920636 : if (!any_viable_p)
12141 : : {
12142 : : /* [dcl.init], 17.6.2.2:
12143 : :
12144 : : Otherwise, if no constructor is viable, the destination type is
12145 : : a (possibly cv-qualified) aggregate class A, and the initializer
12146 : : is a parenthesized expression-list, the object is initialized as
12147 : : follows...
12148 : :
12149 : : We achieve this by building up a CONSTRUCTOR, as for list-init,
12150 : : and setting CONSTRUCTOR_IS_PAREN_INIT to distinguish between
12151 : : the two. */
12152 : 16315 : if (DECL_CONSTRUCTOR_P (fn)
12153 : 11345 : && !(flags & LOOKUP_ONLYCONVERTING)
12154 : 11327 : && cxx_dialect >= cxx20
12155 : 8676 : && CP_AGGREGATE_TYPE_P (basetype)
12156 : 16315 : && !vec_safe_is_empty (user_args))
12157 : : {
12158 : : /* Create a CONSTRUCTOR from ARGS, e.g. {1, 2} from <1, 2>. */
12159 : 916 : tree ctor = build_constructor_from_vec (init_list_type_node,
12160 : : user_args);
12161 : 916 : CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true;
12162 : 916 : CONSTRUCTOR_IS_PAREN_INIT (ctor) = true;
12163 : 916 : if (is_dummy_object (instance))
12164 : : return ctor;
12165 : : else
12166 : : {
12167 : 687 : ctor = digest_init (basetype, ctor, complain);
12168 : 687 : if (ctor == error_mark_node)
12169 : : return error_mark_node;
12170 : 571 : return cp_build_init_expr (instance, ctor);
12171 : : }
12172 : : }
12173 : 15399 : if (complain & tf_error)
12174 : 694 : complain_about_no_candidates_for_method_call (instance, candidates,
12175 : : explicit_targs, basetype,
12176 : : optype, name,
12177 : : skip_first_for_error,
12178 : : user_args);
12179 : 15399 : call = error_mark_node;
12180 : : }
12181 : : else
12182 : : {
12183 : 82904321 : cand = tourney (candidates, complain);
12184 : 82904321 : if (cand == 0)
12185 : : {
12186 : 184 : char *pretty_name;
12187 : 184 : bool free_p;
12188 : 184 : tree arglist;
12189 : :
12190 : 184 : if (complain & tf_error)
12191 : : {
12192 : 88 : pretty_name = name_as_c_string (name, basetype, &free_p);
12193 : 88 : arglist = build_tree_list_vec (user_args);
12194 : 88 : if (skip_first_for_error)
12195 : 0 : arglist = TREE_CHAIN (arglist);
12196 : 88 : auto_diagnostic_group d;
12197 : 176 : if (!any_strictly_viable (candidates))
12198 : 13 : error ("no matching function for call to %<%s(%A)%>",
12199 : : pretty_name, arglist);
12200 : : else
12201 : 75 : error ("call of overloaded %<%s(%A)%> is ambiguous",
12202 : : pretty_name, arglist);
12203 : 88 : print_z_candidates (location_of (name), candidates);
12204 : 88 : if (free_p)
12205 : 0 : free (pretty_name);
12206 : 88 : }
12207 : 184 : call = error_mark_node;
12208 : 184 : if (fn_p)
12209 : 84 : *fn_p = error_mark_node;
12210 : : }
12211 : : else
12212 : : {
12213 : 82904137 : fn = cand->fn;
12214 : 82904137 : call = NULL_TREE;
12215 : :
12216 : 82904137 : if (!(flags & LOOKUP_NONVIRTUAL)
12217 : 60302382 : && DECL_PURE_VIRTUAL_P (fn)
12218 : 208212 : && instance == current_class_ref
12219 : 83066649 : && (complain & tf_warning))
12220 : : {
12221 : : /* This is not an error, it is runtime undefined
12222 : : behavior. */
12223 : 162512 : if (!current_function_decl)
12224 : 3 : warning (0, "pure virtual %q#D called from "
12225 : : "non-static data member initializer", fn);
12226 : 162509 : else if (DECL_CONSTRUCTOR_P (current_function_decl)
12227 : 162509 : || DECL_DESTRUCTOR_P (current_function_decl))
12228 : 9 : warning (0, (DECL_CONSTRUCTOR_P (current_function_decl)
12229 : : ? G_("pure virtual %q#D called from constructor")
12230 : : : G_("pure virtual %q#D called from destructor")),
12231 : : fn);
12232 : : }
12233 : :
12234 : 98636973 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fn)
12235 : 134344566 : && !DECL_CONSTRUCTOR_P (fn)
12236 : 130199940 : && is_dummy_object (instance))
12237 : : {
12238 : 22300 : instance = maybe_resolve_dummy (instance, true);
12239 : 22300 : if (instance == error_mark_node)
12240 : : call = error_mark_node;
12241 : 22300 : else if (!is_dummy_object (instance))
12242 : : {
12243 : : /* We captured 'this' in the current lambda now that
12244 : : we know we really need it. */
12245 : 22177 : cand->first_arg = instance;
12246 : : }
12247 : 123 : else if (current_class_ptr && any_dependent_bases_p ())
12248 : : /* We can't tell until instantiation time whether we can use
12249 : : *this as the implicit object argument. */;
12250 : : else
12251 : : {
12252 : 85 : if (complain & tf_error)
12253 : 60 : error ("cannot call member function %qD without object",
12254 : : fn);
12255 : 85 : call = error_mark_node;
12256 : : }
12257 : : }
12258 : :
12259 : 82904137 : if (call != error_mark_node)
12260 : : {
12261 : : /* Now we know what function is being called. */
12262 : 82904052 : if (fn_p)
12263 : 21780810 : *fn_p = fn;
12264 : : /* Build the actual CALL_EXPR. */
12265 : 82904052 : call = build_over_call (cand, flags, complain);
12266 : :
12267 : : /* Suppress warnings for if (my_struct.operator= (x)) where
12268 : : my_struct is implicitly converted to bool. */
12269 : 82904052 : if (TREE_CODE (call) == MODIFY_EXPR)
12270 : 1721297 : suppress_warning (call, OPT_Wparentheses);
12271 : :
12272 : : /* In an expression of the form `a->f()' where `f' turns
12273 : : out to be a static member function, `a' is
12274 : : none-the-less evaluated. */
12275 : 82904052 : if (!is_dummy_object (instance))
12276 : 61908758 : call = keep_unused_object_arg (call, instance, fn);
12277 : 82904052 : if (call != error_mark_node
12278 : 164328258 : && DECL_DESTRUCTOR_P (cand->fn)
12279 : 104501620 : && !VOID_TYPE_P (TREE_TYPE (call)))
12280 : : /* An explicit call of the form "x->~X()" has type
12281 : : "void". However, on platforms where destructors
12282 : : return "this" (i.e., those where
12283 : : targetm.cxx.cdtor_returns_this is true), such calls
12284 : : will appear to have a return value of pointer type
12285 : : to the low-level call machinery. We do not want to
12286 : : change the low-level machinery, since we want to be
12287 : : able to optimize "delete f()" on such platforms as
12288 : : "operator delete(~X(f()))" (rather than generating
12289 : : "t = f(), ~X(t), operator delete (t)"). */
12290 : 12667274 : call = build_nop (void_type_node, call);
12291 : : }
12292 : : }
12293 : : }
12294 : :
12295 : 82919720 : if (processing_template_decl && call != error_mark_node)
12296 : : {
12297 : 8063102 : bool cast_to_void = false;
12298 : :
12299 : 8063102 : if (TREE_CODE (call) == COMPOUND_EXPR)
12300 : 6 : call = TREE_OPERAND (call, 1);
12301 : 8063096 : else if (TREE_CODE (call) == NOP_EXPR)
12302 : : {
12303 : 0 : cast_to_void = true;
12304 : 0 : call = TREE_OPERAND (call, 0);
12305 : : }
12306 : 8063102 : if (INDIRECT_REF_P (call))
12307 : 524050 : call = TREE_OPERAND (call, 0);
12308 : :
12309 : : /* Prune all but the selected function from the original overload
12310 : : set so that we can avoid some duplicate work at instantiation time. */
12311 : 8063102 : if (really_overloaded_fn (fns))
12312 : : {
12313 : 3663359 : if (DECL_TEMPLATE_INFO (fn)
12314 : 3663359 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
12315 : : {
12316 : : /* Use the selected template, not the specialization, so that
12317 : : this looks like an actual lookup result for sake of
12318 : : filter_memfn_lookup. */
12319 : :
12320 : 2243208 : if (OVL_SINGLE_P (fns))
12321 : : /* If the original overload set consists of a single function
12322 : : template, this isn't beneficial. */
12323 : 2207959 : goto skip_prune;
12324 : :
12325 : 35249 : fn = ovl_make (DECL_TI_TEMPLATE (fn));
12326 : 35249 : if (template_only)
12327 : 30395 : fn = lookup_template_function (fn, explicit_targs);
12328 : : }
12329 : 1455400 : orig_fns = copy_node (orig_fns);
12330 : 1455400 : BASELINK_FUNCTIONS (orig_fns) = fn;
12331 : 1455400 : BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (orig_fns) = true;
12332 : : }
12333 : :
12334 : 4399743 : skip_prune:
12335 : 8063102 : call = (build_min_non_dep_call_vec
12336 : 8063102 : (call,
12337 : 8063102 : build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
12338 : : orig_instance, orig_fns, NULL_TREE),
12339 : : orig_args));
12340 : 8063102 : SET_EXPR_LOCATION (call, input_location);
12341 : 8063102 : call = convert_from_reference (call);
12342 : 8063102 : if (cast_to_void)
12343 : 0 : call = build_nop (void_type_node, call);
12344 : : }
12345 : :
12346 : 82919720 : if (orig_args != NULL)
12347 : 7960804 : release_tree_vector (orig_args);
12348 : :
12349 : : return call;
12350 : 84427065 : }
12351 : :
12352 : : /* Returns true iff standard conversion sequence ICS1 is a proper
12353 : : subsequence of ICS2. */
12354 : :
12355 : : static bool
12356 : 72081513 : is_subseq (conversion *ics1, conversion *ics2)
12357 : : {
12358 : : /* We can assume that a conversion of the same code
12359 : : between the same types indicates a subsequence since we only get
12360 : : here if the types we are converting from are the same. */
12361 : :
12362 : 72081513 : while (ics1->kind == ck_rvalue
12363 : 91626480 : || ics1->kind == ck_lvalue)
12364 : 19544967 : ics1 = next_conversion (ics1);
12365 : :
12366 : : while (1)
12367 : : {
12368 : 104557448 : while (ics2->kind == ck_rvalue
12369 : 104557448 : || ics2->kind == ck_lvalue)
12370 : 19544967 : ics2 = next_conversion (ics2);
12371 : :
12372 : 85012481 : if (ics2->kind == ck_user
12373 : 85012481 : || !has_next (ics2->kind))
12374 : : /* At this point, ICS1 cannot be a proper subsequence of
12375 : : ICS2. We can get a USER_CONV when we are comparing the
12376 : : second standard conversion sequence of two user conversion
12377 : : sequences. */
12378 : : return false;
12379 : :
12380 : 12934713 : ics2 = next_conversion (ics2);
12381 : :
12382 : 12934713 : while (ics2->kind == ck_rvalue
12383 : 21055597 : || ics2->kind == ck_lvalue)
12384 : 8120884 : ics2 = next_conversion (ics2);
12385 : :
12386 : 12934713 : if (ics2->kind == ics1->kind
12387 : 3790 : && same_type_p (ics2->type, ics1->type)
12388 : 12938458 : && (ics1->kind == ck_identity
12389 : 3745 : || same_type_p (next_conversion (ics2)->type,
12390 : : next_conversion (ics1)->type)))
12391 : 3745 : return true;
12392 : : }
12393 : : }
12394 : :
12395 : : /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
12396 : : be any _TYPE nodes. */
12397 : :
12398 : : bool
12399 : 288623315 : is_properly_derived_from (tree derived, tree base)
12400 : : {
12401 : 288623315 : if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
12402 : : return false;
12403 : :
12404 : : /* We only allow proper derivation here. The DERIVED_FROM_P macro
12405 : : considers every class derived from itself. */
12406 : 274927983 : return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
12407 : 274927983 : && DERIVED_FROM_P (base, derived));
12408 : : }
12409 : :
12410 : : /* We build the ICS for an implicit object parameter as a pointer
12411 : : conversion sequence. However, such a sequence should be compared
12412 : : as if it were a reference conversion sequence. If ICS is the
12413 : : implicit conversion sequence for an implicit object parameter,
12414 : : modify it accordingly. */
12415 : :
12416 : : static void
12417 : 154417672 : maybe_handle_implicit_object (conversion **ics)
12418 : : {
12419 : 154417672 : if ((*ics)->this_p)
12420 : : {
12421 : : /* [over.match.funcs]
12422 : :
12423 : : For non-static member functions, the type of the
12424 : : implicit object parameter is "reference to cv X"
12425 : : where X is the class of which the function is a
12426 : : member and cv is the cv-qualification on the member
12427 : : function declaration. */
12428 : 10293998 : conversion *t = *ics;
12429 : 10293998 : tree reference_type;
12430 : :
12431 : : /* The `this' parameter is a pointer to a class type. Make the
12432 : : implicit conversion talk about a reference to that same class
12433 : : type. */
12434 : 10293998 : reference_type = TREE_TYPE (t->type);
12435 : 10293998 : reference_type = build_reference_type (reference_type);
12436 : :
12437 : 10293998 : if (t->kind == ck_qual)
12438 : 2711972 : t = next_conversion (t);
12439 : 10293998 : if (t->kind == ck_ptr)
12440 : 430929 : t = next_conversion (t);
12441 : 10293998 : t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
12442 : 10293998 : t = direct_reference_binding (reference_type, t);
12443 : 10293998 : t->this_p = 1;
12444 : 10293998 : t->rvaluedness_matches_p = 0;
12445 : 10293998 : *ics = t;
12446 : : }
12447 : 154417672 : }
12448 : :
12449 : : /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
12450 : : and return the initial reference binding conversion. Otherwise,
12451 : : leave *ICS unchanged and return NULL. */
12452 : :
12453 : : static conversion *
12454 : 154417672 : maybe_handle_ref_bind (conversion **ics)
12455 : : {
12456 : 154417672 : if ((*ics)->kind == ck_ref_bind)
12457 : : {
12458 : 41654927 : conversion *old_ics = *ics;
12459 : 41654927 : *ics = next_conversion (old_ics);
12460 : 41654927 : (*ics)->user_conv_p = old_ics->user_conv_p;
12461 : 41654927 : return old_ics;
12462 : : }
12463 : :
12464 : : return NULL;
12465 : : }
12466 : :
12467 : : /* Get the expression at the beginning of the conversion chain C. */
12468 : :
12469 : : static tree
12470 : 51 : conv_get_original_expr (conversion *c)
12471 : : {
12472 : 60 : for (; c; c = next_conversion (c))
12473 : 60 : if (c->kind == ck_identity || c->kind == ck_ambig || c->kind == ck_aggr)
12474 : 51 : return c->u.expr;
12475 : : return NULL_TREE;
12476 : : }
12477 : :
12478 : : /* Return a tree representing the number of elements initialized by the
12479 : : list-initialization C. The caller must check that C converts to an
12480 : : array type. */
12481 : :
12482 : : static tree
12483 : 126 : nelts_initialized_by_list_init (conversion *c)
12484 : : {
12485 : : /* If the array we're converting to has a dimension, we'll use that. */
12486 : 126 : if (TYPE_DOMAIN (c->type))
12487 : 84 : return array_type_nelts_top (c->type);
12488 : : else
12489 : : {
12490 : : /* Otherwise, we look at how many elements the constructor we're
12491 : : initializing from has. */
12492 : 42 : tree ctor = conv_get_original_expr (c);
12493 : 72 : return size_int (CONSTRUCTOR_NELTS (ctor));
12494 : : }
12495 : : }
12496 : :
12497 : : /* True iff C is a conversion that binds a reference or a pointer to
12498 : : an array of unknown bound. */
12499 : :
12500 : : static inline bool
12501 : 20432706 : conv_binds_to_array_of_unknown_bound (conversion *c)
12502 : : {
12503 : : /* ck_ref_bind won't have the reference stripped. */
12504 : 20432706 : tree type = non_reference (c->type);
12505 : : /* ck_qual won't have the pointer stripped. */
12506 : 20432706 : type = strip_pointer_operator (type);
12507 : 20432706 : return (TREE_CODE (type) == ARRAY_TYPE
12508 : 20432706 : && TYPE_DOMAIN (type) == NULL_TREE);
12509 : : }
12510 : :
12511 : : /* Compare two implicit conversion sequences according to the rules set out in
12512 : : [over.ics.rank]. Return values:
12513 : :
12514 : : 1: ics1 is better than ics2
12515 : : -1: ics2 is better than ics1
12516 : : 0: ics1 and ics2 are indistinguishable */
12517 : :
12518 : : static int
12519 : 77208883 : compare_ics (conversion *ics1, conversion *ics2)
12520 : : {
12521 : 77208883 : tree from_type1;
12522 : 77208883 : tree from_type2;
12523 : 77208883 : tree to_type1;
12524 : 77208883 : tree to_type2;
12525 : 77208883 : tree deref_from_type1 = NULL_TREE;
12526 : 77208883 : tree deref_from_type2 = NULL_TREE;
12527 : 77208883 : tree deref_to_type1 = NULL_TREE;
12528 : 77208883 : tree deref_to_type2 = NULL_TREE;
12529 : 77208883 : conversion_rank rank1, rank2;
12530 : :
12531 : : /* REF_BINDING is nonzero if the result of the conversion sequence
12532 : : is a reference type. In that case REF_CONV is the reference
12533 : : binding conversion. */
12534 : 77208883 : conversion *ref_conv1;
12535 : 77208883 : conversion *ref_conv2;
12536 : :
12537 : : /* Compare badness before stripping the reference conversion. */
12538 : 77208883 : if (ics1->bad_p > ics2->bad_p)
12539 : : return -1;
12540 : 77208866 : else if (ics1->bad_p < ics2->bad_p)
12541 : : return 1;
12542 : :
12543 : : /* Handle implicit object parameters. */
12544 : 77208836 : maybe_handle_implicit_object (&ics1);
12545 : 77208836 : maybe_handle_implicit_object (&ics2);
12546 : :
12547 : : /* Handle reference parameters. */
12548 : 77208836 : ref_conv1 = maybe_handle_ref_bind (&ics1);
12549 : 77208836 : ref_conv2 = maybe_handle_ref_bind (&ics2);
12550 : :
12551 : : /* List-initialization sequence L1 is a better conversion sequence than
12552 : : list-initialization sequence L2 if L1 converts to
12553 : : std::initializer_list<X> for some X and L2 does not. */
12554 : 77208836 : if (ics1->kind == ck_list && ics2->kind != ck_list)
12555 : : return 1;
12556 : 77208272 : if (ics2->kind == ck_list && ics1->kind != ck_list)
12557 : : return -1;
12558 : :
12559 : : /* [over.ics.rank]
12560 : :
12561 : : When comparing the basic forms of implicit conversion sequences (as
12562 : : defined in _over.best.ics_)
12563 : :
12564 : : --a standard conversion sequence (_over.ics.scs_) is a better
12565 : : conversion sequence than a user-defined conversion sequence
12566 : : or an ellipsis conversion sequence, and
12567 : :
12568 : : --a user-defined conversion sequence (_over.ics.user_) is a
12569 : : better conversion sequence than an ellipsis conversion sequence
12570 : : (_over.ics.ellipsis_). */
12571 : : /* Use BAD_CONVERSION_RANK because we already checked for a badness
12572 : : mismatch. If both ICS are bad, we try to make a decision based on
12573 : : what would have happened if they'd been good. This is not an
12574 : : extension, we'll still give an error when we build up the call; this
12575 : : just helps us give a more helpful error message. */
12576 : 77207988 : rank1 = BAD_CONVERSION_RANK (ics1);
12577 : 77207988 : rank2 = BAD_CONVERSION_RANK (ics2);
12578 : :
12579 : 77207988 : if (rank1 > rank2)
12580 : : return -1;
12581 : 66783083 : else if (rank1 < rank2)
12582 : : return 1;
12583 : :
12584 : 36372338 : if (ics1->ellipsis_p)
12585 : : /* Both conversions are ellipsis conversions. */
12586 : : return 0;
12587 : :
12588 : : /* User-defined conversion sequence U1 is a better conversion sequence
12589 : : than another user-defined conversion sequence U2 if they contain the
12590 : : same user-defined conversion operator or constructor and if the sec-
12591 : : ond standard conversion sequence of U1 is better than the second
12592 : : standard conversion sequence of U2. */
12593 : :
12594 : : /* Handle list-conversion with the same code even though it isn't always
12595 : : ranked as a user-defined conversion and it doesn't have a second
12596 : : standard conversion sequence; it will still have the desired effect.
12597 : : Specifically, we need to do the reference binding comparison at the
12598 : : end of this function. */
12599 : :
12600 : 36372302 : if (ics1->user_conv_p || ics1->kind == ck_list
12601 : 35972156 : || ics1->kind == ck_aggr || ics2->kind == ck_aggr)
12602 : : {
12603 : 400215 : conversion *t1 = strip_standard_conversion (ics1);
12604 : 400215 : conversion *t2 = strip_standard_conversion (ics2);
12605 : :
12606 : 400215 : if (!t1 || !t2 || t1->kind != t2->kind)
12607 : : return 0;
12608 : 400196 : else if (t1->kind == ck_user)
12609 : : {
12610 : 389095 : tree f1 = t1->cand ? t1->cand->fn : t1->type;
12611 : 389095 : tree f2 = t2->cand ? t2->cand->fn : t2->type;
12612 : 389095 : if (f1 != f2)
12613 : : return 0;
12614 : : }
12615 : : /* List-initialization sequence L1 is a better conversion sequence than
12616 : : list-initialization sequence L2 if
12617 : :
12618 : : -- L1 and L2 convert to arrays of the same element type, and either
12619 : : the number of elements n1 initialized by L1 is less than the number
12620 : : of elements n2 initialized by L2, or n1=n2 and L2 converts to an array
12621 : : of unknown bound and L1 does not. (Added in CWG 1307 and extended by
12622 : : P0388R4.) */
12623 : 11101 : else if (t1->kind == ck_aggr
12624 : 10584 : && TREE_CODE (t1->type) == ARRAY_TYPE
12625 : 66 : && TREE_CODE (t2->type) == ARRAY_TYPE
12626 : 11167 : && same_type_p (TREE_TYPE (t1->type), TREE_TYPE (t2->type)))
12627 : : {
12628 : 63 : tree n1 = nelts_initialized_by_list_init (t1);
12629 : 63 : tree n2 = nelts_initialized_by_list_init (t2);
12630 : 63 : if (tree_int_cst_lt (n1, n2))
12631 : : return 1;
12632 : 24 : else if (tree_int_cst_lt (n2, n1))
12633 : : return -1;
12634 : : /* The n1 == n2 case. */
12635 : 24 : bool c1 = conv_binds_to_array_of_unknown_bound (t1);
12636 : 24 : bool c2 = conv_binds_to_array_of_unknown_bound (t2);
12637 : 24 : if (c1 && !c2)
12638 : : return -1;
12639 : 6 : else if (!c1 && c2)
12640 : : return 1;
12641 : : else
12642 : : return 0;
12643 : : }
12644 : : else
12645 : : {
12646 : : /* For ambiguous or aggregate conversions, use the target type as
12647 : : a proxy for the conversion function. */
12648 : 11038 : if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
12649 : : return 0;
12650 : : }
12651 : :
12652 : : /* We can just fall through here, after setting up
12653 : : FROM_TYPE1 and FROM_TYPE2. */
12654 : 397331 : from_type1 = t1->type;
12655 : 397331 : from_type2 = t2->type;
12656 : 397331 : }
12657 : : else
12658 : : {
12659 : : conversion *t1;
12660 : : conversion *t2;
12661 : :
12662 : : /* We're dealing with two standard conversion sequences.
12663 : :
12664 : : [over.ics.rank]
12665 : :
12666 : : Standard conversion sequence S1 is a better conversion
12667 : : sequence than standard conversion sequence S2 if
12668 : :
12669 : : --S1 is a proper subsequence of S2 (comparing the conversion
12670 : : sequences in the canonical form defined by _over.ics.scs_,
12671 : : excluding any Lvalue Transformation; the identity
12672 : : conversion sequence is considered to be a subsequence of
12673 : : any non-identity conversion sequence */
12674 : :
12675 : : t1 = ics1;
12676 : 56329561 : while (t1->kind != ck_identity)
12677 : 20357474 : t1 = next_conversion (t1);
12678 : 35972087 : from_type1 = t1->type;
12679 : :
12680 : 35972087 : t2 = ics2;
12681 : 56270517 : while (t2->kind != ck_identity)
12682 : 20298430 : t2 = next_conversion (t2);
12683 : 35972087 : from_type2 = t2->type;
12684 : : }
12685 : :
12686 : : /* One sequence can only be a subsequence of the other if they start with
12687 : : the same type. They can start with different types when comparing the
12688 : : second standard conversion sequence in two user-defined conversion
12689 : : sequences. */
12690 : 36369418 : if (same_type_p (from_type1, from_type2))
12691 : : {
12692 : 36042599 : if (is_subseq (ics1, ics2))
12693 : : return 1;
12694 : 36038914 : if (is_subseq (ics2, ics1))
12695 : : return -1;
12696 : : }
12697 : :
12698 : : /* [over.ics.rank]
12699 : :
12700 : : Or, if not that,
12701 : :
12702 : : --the rank of S1 is better than the rank of S2 (by the rules
12703 : : defined below):
12704 : :
12705 : : Standard conversion sequences are ordered by their ranks: an Exact
12706 : : Match is a better conversion than a Promotion, which is a better
12707 : : conversion than a Conversion.
12708 : :
12709 : : Two conversion sequences with the same rank are indistinguishable
12710 : : unless one of the following rules applies:
12711 : :
12712 : : --A conversion that does not a convert a pointer, pointer to member,
12713 : : or std::nullptr_t to bool is better than one that does.
12714 : :
12715 : : The ICS_STD_RANK automatically handles the pointer-to-bool rule,
12716 : : so that we do not have to check it explicitly. */
12717 : 36365673 : if (ics1->rank < ics2->rank)
12718 : : return 1;
12719 : 36365593 : else if (ics2->rank < ics1->rank)
12720 : : return -1;
12721 : :
12722 : 36365593 : to_type1 = ics1->type;
12723 : 36365593 : to_type2 = ics2->type;
12724 : :
12725 : : /* A conversion from scalar arithmetic type to complex is worse than a
12726 : : conversion between scalar arithmetic types. */
12727 : 36365593 : if (same_type_p (from_type1, from_type2)
12728 : 36038774 : && ARITHMETIC_TYPE_P (from_type1)
12729 : 9547249 : && ARITHMETIC_TYPE_P (to_type1)
12730 : 9547118 : && ARITHMETIC_TYPE_P (to_type2)
12731 : 36365593 : && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
12732 : 9547102 : != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
12733 : : {
12734 : 120 : if (TREE_CODE (to_type1) == COMPLEX_TYPE)
12735 : : return -1;
12736 : : else
12737 : : return 1;
12738 : : }
12739 : :
12740 : 36365473 : {
12741 : : /* A conversion in either direction between floating-point type FP1 and
12742 : : floating-point type FP2 is better than a conversion in the same
12743 : : direction between FP1 and arithmetic type T3 if
12744 : : - the floating-point conversion rank of FP1 is equal to the rank of
12745 : : FP2, and
12746 : : - T3 is not a floating-point type, or T3 is a floating-point type
12747 : : whose rank is not equal to the rank of FP1, or the floating-point
12748 : : conversion subrank of FP2 is greater than the subrank of T3. */
12749 : 36365473 : tree fp1 = from_type1;
12750 : 36365473 : tree fp2 = to_type1;
12751 : 36365473 : tree fp3 = from_type2;
12752 : 36365473 : tree t3 = to_type2;
12753 : 36365473 : int ret = 1;
12754 : 36365473 : if (TYPE_MAIN_VARIANT (fp2) == TYPE_MAIN_VARIANT (t3))
12755 : : {
12756 : 30881591 : std::swap (fp1, fp2);
12757 : 30881591 : std::swap (fp3, t3);
12758 : : }
12759 : 36365473 : if (TYPE_MAIN_VARIANT (fp1) == TYPE_MAIN_VARIANT (fp3)
12760 : 36365368 : && SCALAR_FLOAT_TYPE_P (fp1)
12761 : : /* Only apply this rule if at least one of the 3 types is
12762 : : extended floating-point type, otherwise keep them as
12763 : : before for compatibility reasons with types like __float128.
12764 : : float, double and long double alone have different conversion
12765 : : ranks and so when just those 3 types are involved, this
12766 : : rule doesn't trigger. */
12767 : 40992625 : && (extended_float_type_p (fp1)
12768 : 4520139 : || (SCALAR_FLOAT_TYPE_P (fp2) && extended_float_type_p (fp2))
12769 : 4339120 : || (SCALAR_FLOAT_TYPE_P (t3) && extended_float_type_p (t3))))
12770 : : {
12771 : 288032 : if (TREE_CODE (fp2) != REAL_TYPE)
12772 : : {
12773 : 48544 : ret = -ret;
12774 : 48544 : std::swap (fp2, t3);
12775 : : }
12776 : 288032 : if (SCALAR_FLOAT_TYPE_P (fp2))
12777 : : {
12778 : : /* cp_compare_floating_point_conversion_ranks returns -1, 0 or 1
12779 : : if the conversion rank is equal (-1 or 1 if the subrank is
12780 : : different). */
12781 : 261528 : if (IN_RANGE (cp_compare_floating_point_conversion_ranks (fp1,
12782 : : fp2),
12783 : : -1, 1))
12784 : : {
12785 : : /* Conversion ranks of FP1 and FP2 are equal. */
12786 : 177210 : if (TREE_CODE (t3) != REAL_TYPE
12787 : 177210 : || !IN_RANGE (cp_compare_floating_point_conversion_ranks
12788 : : (fp1, t3),
12789 : : -1, 1))
12790 : : /* FP1 <-> FP2 conversion is better. */
12791 : 176740 : return ret;
12792 : 470 : int c = cp_compare_floating_point_conversion_ranks (fp2, t3);
12793 : 470 : gcc_assert (IN_RANGE (c, -1, 1));
12794 : 470 : if (c == 1)
12795 : : /* Conversion subrank of FP2 is greater than subrank of T3.
12796 : : FP1 <-> FP2 conversion is better. */
12797 : : return ret;
12798 : 470 : else if (c == -1)
12799 : : /* Conversion subrank of FP2 is less than subrank of T3.
12800 : : FP1 <-> T3 conversion is better. */
12801 : 0 : return -ret;
12802 : : }
12803 : 84318 : else if (SCALAR_FLOAT_TYPE_P (t3)
12804 : 84318 : && IN_RANGE (cp_compare_floating_point_conversion_ranks
12805 : : (fp1, t3),
12806 : : -1, 1))
12807 : : /* Conversion ranks of FP1 and FP2 are not equal, conversion
12808 : : ranks of FP1 and T3 are equal.
12809 : : FP1 <-> T3 conversion is better. */
12810 : 14430 : return -ret;
12811 : : }
12812 : : }
12813 : : }
12814 : :
12815 : 36174303 : if (TYPE_PTR_P (from_type1)
12816 : 7587304 : && TYPE_PTR_P (from_type2)
12817 : 7587288 : && TYPE_PTR_P (to_type1)
12818 : 7587264 : && TYPE_PTR_P (to_type2))
12819 : : {
12820 : 7587264 : deref_from_type1 = TREE_TYPE (from_type1);
12821 : 7587264 : deref_from_type2 = TREE_TYPE (from_type2);
12822 : 7587264 : deref_to_type1 = TREE_TYPE (to_type1);
12823 : 7587264 : deref_to_type2 = TREE_TYPE (to_type2);
12824 : : }
12825 : : /* The rules for pointers to members A::* are just like the rules
12826 : : for pointers A*, except opposite: if B is derived from A then
12827 : : A::* converts to B::*, not vice versa. For that reason, we
12828 : : switch the from_ and to_ variables here. */
12829 : 52 : else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2)
12830 : 52 : && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2))
12831 : 28587039 : || (TYPE_PTRMEMFUNC_P (from_type1)
12832 : 403 : && TYPE_PTRMEMFUNC_P (from_type2)
12833 : 403 : && TYPE_PTRMEMFUNC_P (to_type1)
12834 : 403 : && TYPE_PTRMEMFUNC_P (to_type2)))
12835 : : {
12836 : 455 : deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
12837 : 455 : deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
12838 : 455 : deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
12839 : 455 : deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
12840 : : }
12841 : :
12842 : 7587719 : if (deref_from_type1 != NULL_TREE
12843 : 7587719 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
12844 : 195162 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
12845 : : {
12846 : : /* This was one of the pointer or pointer-like conversions.
12847 : :
12848 : : [over.ics.rank]
12849 : :
12850 : : --If class B is derived directly or indirectly from class A,
12851 : : conversion of B* to A* is better than conversion of B* to
12852 : : void*, and conversion of A* to void* is better than
12853 : : conversion of B* to void*. */
12854 : 195162 : if (VOID_TYPE_P (deref_to_type1)
12855 : 57 : && VOID_TYPE_P (deref_to_type2))
12856 : : {
12857 : 14 : if (is_properly_derived_from (deref_from_type1,
12858 : : deref_from_type2))
12859 : : return -1;
12860 : 14 : else if (is_properly_derived_from (deref_from_type2,
12861 : : deref_from_type1))
12862 : : return 1;
12863 : : }
12864 : 195148 : else if (VOID_TYPE_P (deref_to_type1)
12865 : 195105 : || VOID_TYPE_P (deref_to_type2))
12866 : : {
12867 : 47 : if (same_type_p (deref_from_type1, deref_from_type2))
12868 : : {
12869 : 47 : if (VOID_TYPE_P (deref_to_type2))
12870 : : {
12871 : 4 : if (is_properly_derived_from (deref_from_type1,
12872 : : deref_to_type1))
12873 : : return 1;
12874 : : }
12875 : : /* We know that DEREF_TO_TYPE1 is `void' here. */
12876 : 43 : else if (is_properly_derived_from (deref_from_type1,
12877 : : deref_to_type2))
12878 : : return -1;
12879 : : }
12880 : : }
12881 : 195101 : else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
12882 : 195101 : && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
12883 : : {
12884 : : /* [over.ics.rank]
12885 : :
12886 : : --If class B is derived directly or indirectly from class A
12887 : : and class C is derived directly or indirectly from B,
12888 : :
12889 : : --conversion of C* to B* is better than conversion of C* to
12890 : : A*,
12891 : :
12892 : : --conversion of B* to A* is better than conversion of C* to
12893 : : A* */
12894 : 195101 : if (same_type_p (deref_from_type1, deref_from_type2))
12895 : : {
12896 : 195095 : if (is_properly_derived_from (deref_to_type1,
12897 : : deref_to_type2))
12898 : : return 1;
12899 : 195095 : else if (is_properly_derived_from (deref_to_type2,
12900 : : deref_to_type1))
12901 : : return -1;
12902 : : }
12903 : 6 : else if (same_type_p (deref_to_type1, deref_to_type2))
12904 : : {
12905 : 6 : if (is_properly_derived_from (deref_from_type2,
12906 : : deref_from_type1))
12907 : : return 1;
12908 : 0 : else if (is_properly_derived_from (deref_from_type1,
12909 : : deref_from_type2))
12910 : : return -1;
12911 : : }
12912 : : }
12913 : : }
12914 : 71958282 : else if (CLASS_TYPE_P (non_reference (from_type1))
12915 : 53345113 : && same_type_p (from_type1, from_type2))
12916 : : {
12917 : 17079316 : tree from = non_reference (from_type1);
12918 : :
12919 : : /* [over.ics.rank]
12920 : :
12921 : : --binding of an expression of type C to a reference of type
12922 : : B& is better than binding an expression of type C to a
12923 : : reference of type A&
12924 : :
12925 : : --conversion of C to B is better than conversion of C to A, */
12926 : 17079316 : if (is_properly_derived_from (from, to_type1)
12927 : 17079316 : && is_properly_derived_from (from, to_type2))
12928 : : {
12929 : 799574 : if (is_properly_derived_from (to_type1, to_type2))
12930 : : return 1;
12931 : 797336 : else if (is_properly_derived_from (to_type2, to_type1))
12932 : : return -1;
12933 : : }
12934 : : }
12935 : 37799650 : else if (CLASS_TYPE_P (non_reference (to_type1))
12936 : 19186481 : && same_type_p (to_type1, to_type2))
12937 : : {
12938 : 5 : tree to = non_reference (to_type1);
12939 : :
12940 : : /* [over.ics.rank]
12941 : :
12942 : : --binding of an expression of type B to a reference of type
12943 : : A& is better than binding an expression of type C to a
12944 : : reference of type A&,
12945 : :
12946 : : --conversion of B to A is better than conversion of C to A */
12947 : 5 : if (is_properly_derived_from (from_type1, to)
12948 : 5 : && is_properly_derived_from (from_type2, to))
12949 : : {
12950 : 3 : if (is_properly_derived_from (from_type2, from_type1))
12951 : : return 1;
12952 : 3 : else if (is_properly_derived_from (from_type1, from_type2))
12953 : : return -1;
12954 : : }
12955 : : }
12956 : :
12957 : : /* [over.ics.rank]
12958 : :
12959 : : --S1 and S2 differ only in their qualification conversion and yield
12960 : : similar types T1 and T2 (_conv.qual_), respectively, and the cv-
12961 : : qualification signature of type T1 is a proper subset of the cv-
12962 : : qualification signature of type T2 */
12963 : 36117854 : if (ics1->kind == ck_qual
12964 : 377 : && ics2->kind == ck_qual
12965 : 36118231 : && same_type_p (from_type1, from_type2))
12966 : : {
12967 : 377 : int result = comp_cv_qual_signature (to_type1, to_type2);
12968 : 377 : if (result != 0)
12969 : : return result;
12970 : : }
12971 : :
12972 : : /* [over.ics.rank]
12973 : :
12974 : : --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
12975 : : to an implicit object parameter of a non-static member function
12976 : : declared without a ref-qualifier, and either S1 binds an lvalue
12977 : : reference to an lvalue and S2 binds an rvalue reference or S1 binds an
12978 : : rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x
12979 : : draft standard, 13.3.3.2)
12980 : :
12981 : : --S1 and S2 are reference bindings (_dcl.init.ref_), and the
12982 : : types to which the references refer are the same type except for
12983 : : top-level cv-qualifiers, and the type to which the reference
12984 : : initialized by S2 refers is more cv-qualified than the type to
12985 : : which the reference initialized by S1 refers.
12986 : :
12987 : : DR 1328 [over.match.best]: the context is an initialization by
12988 : : conversion function for direct reference binding (13.3.1.6) of a
12989 : : reference to function type, the return type of F1 is the same kind of
12990 : : reference (i.e. lvalue or rvalue) as the reference being initialized,
12991 : : and the return type of F2 is not. */
12992 : :
12993 : 36117756 : if (ref_conv1 && ref_conv2)
12994 : : {
12995 : 12138973 : if (!ref_conv1->this_p && !ref_conv2->this_p
12996 : 12023982 : && (ref_conv1->rvaluedness_matches_p
12997 : 12023982 : != ref_conv2->rvaluedness_matches_p)
12998 : 24281950 : && (same_type_p (ref_conv1->type, ref_conv2->type)
12999 : 7032909 : || (TYPE_REF_IS_RVALUE (ref_conv1->type)
13000 : 7032909 : != TYPE_REF_IS_RVALUE (ref_conv2->type))))
13001 : : {
13002 : 7030737 : if (ref_conv1->bad_p
13003 : 7030737 : && !same_type_p (TREE_TYPE (ref_conv1->type),
13004 : : TREE_TYPE (ref_conv2->type)))
13005 : : /* Don't prefer a bad conversion that drops cv-quals to a bad
13006 : : conversion with the wrong rvalueness. */
13007 : : return 0;
13008 : 7029278 : return (ref_conv1->rvaluedness_matches_p
13009 : 7029278 : - ref_conv2->rvaluedness_matches_p);
13010 : : }
13011 : :
13012 : 10218301 : if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
13013 : : {
13014 : : /* Per P0388R4:
13015 : :
13016 : : void f (int(&)[]), // (1)
13017 : : f (int(&)[1]), // (2)
13018 : : f (int*); // (3)
13019 : :
13020 : : (2) is better than (1), but (3) should be equal to (1) and to
13021 : : (2). For that reason we don't use ck_qual for (1) which would
13022 : : give it the cr_exact rank while (3) remains ck_identity.
13023 : : Therefore we compare (1) and (2) here. For (1) we'll have
13024 : :
13025 : : ck_ref_bind <- ck_identity
13026 : : int[] & int[1]
13027 : :
13028 : : so to handle this we must look at ref_conv. */
13029 : 10215970 : bool c1 = conv_binds_to_array_of_unknown_bound (ref_conv1);
13030 : 10215970 : bool c2 = conv_binds_to_array_of_unknown_bound (ref_conv2);
13031 : 10215970 : if (c1 && !c2)
13032 : : return -1;
13033 : 10215964 : else if (!c1 && c2)
13034 : : return 1;
13035 : :
13036 : 10215964 : int q1 = cp_type_quals (TREE_TYPE (ref_conv1->type));
13037 : 10215964 : int q2 = cp_type_quals (TREE_TYPE (ref_conv2->type));
13038 : 10215964 : if (ref_conv1->bad_p)
13039 : : {
13040 : : /* Prefer the one that drops fewer cv-quals. */
13041 : 1606 : tree ftype = next_conversion (ref_conv1)->type;
13042 : 1606 : int fquals = cp_type_quals (ftype);
13043 : 1606 : q1 ^= fquals;
13044 : 1606 : q2 ^= fquals;
13045 : : }
13046 : 10215964 : return comp_cv_qualification (q2, q1);
13047 : : }
13048 : : }
13049 : :
13050 : : /* [over.ics.rank]
13051 : :
13052 : : Per CWG 1601:
13053 : : -- A conversion that promotes an enumeration whose underlying type
13054 : : is fixed to its underlying type is better than one that promotes to
13055 : : the promoted underlying type, if the two are different. */
13056 : 18871049 : if (ics1->rank == cr_promotion
13057 : 140 : && ics2->rank == cr_promotion
13058 : 140 : && UNSCOPED_ENUM_P (from_type1)
13059 : 25 : && ENUM_FIXED_UNDERLYING_TYPE_P (from_type1)
13060 : 18871071 : && same_type_p (from_type1, from_type2))
13061 : : {
13062 : 22 : tree utype = ENUM_UNDERLYING_TYPE (from_type1);
13063 : 22 : tree prom = type_promotes_to (from_type1);
13064 : 22 : if (!same_type_p (utype, prom))
13065 : : {
13066 : 12 : if (same_type_p (to_type1, utype)
13067 : 12 : && same_type_p (to_type2, prom))
13068 : : return 1;
13069 : 6 : else if (same_type_p (to_type2, utype)
13070 : 6 : && same_type_p (to_type1, prom))
13071 : : return -1;
13072 : : }
13073 : : }
13074 : :
13075 : : /* Neither conversion sequence is better than the other. */
13076 : : return 0;
13077 : : }
13078 : :
13079 : : /* The source type for this standard conversion sequence. */
13080 : :
13081 : : static tree
13082 : 9 : source_type (conversion *t)
13083 : : {
13084 : 9 : return strip_standard_conversion (t)->type;
13085 : : }
13086 : :
13087 : : /* Note a warning about preferring WINNER to LOSER. We do this by storing
13088 : : a pointer to LOSER and re-running joust to produce the warning if WINNER
13089 : : is actually used. */
13090 : :
13091 : : static void
13092 : 201 : add_warning (struct z_candidate *winner, struct z_candidate *loser)
13093 : : {
13094 : 201 : candidate_warning *cw = (candidate_warning *)
13095 : 0 : conversion_obstack_alloc (sizeof (candidate_warning));
13096 : 201 : cw->loser = loser;
13097 : 201 : cw->next = winner->warnings;
13098 : 201 : winner->warnings = cw;
13099 : 201 : }
13100 : :
13101 : : /* CAND is a constructor candidate in joust in C++17 and up. If it copies a
13102 : : prvalue returned from a conversion function, return true. Otherwise, return
13103 : : false. */
13104 : :
13105 : : static bool
13106 : 588154 : joust_maybe_elide_copy (z_candidate *cand)
13107 : : {
13108 : 588154 : tree fn = cand->fn;
13109 : 1477447 : if (!DECL_COPY_CONSTRUCTOR_P (fn) && !DECL_MOVE_CONSTRUCTOR_P (fn))
13110 : 301061 : return false;
13111 : 287093 : conversion *conv = cand->convs[0];
13112 : 287093 : if (conv->kind == ck_ambig)
13113 : : return false;
13114 : 287091 : gcc_checking_assert (conv->kind == ck_ref_bind);
13115 : 287091 : conv = next_conversion (conv);
13116 : 287091 : if (conv->kind == ck_user && !TYPE_REF_P (conv->type))
13117 : : {
13118 : 99 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
13119 : : (conv->type, DECL_CONTEXT (fn)));
13120 : 99 : z_candidate *uc = conv->cand;
13121 : 99 : if (DECL_CONV_FN_P (uc->fn))
13122 : : return true;
13123 : : }
13124 : : return false;
13125 : : }
13126 : :
13127 : : /* Return the class that CAND's implicit object parameter refers to. */
13128 : :
13129 : : static tree
13130 : 76537 : class_of_implicit_object (z_candidate *cand)
13131 : : {
13132 : 76537 : if (!DECL_IOBJ_MEMBER_FUNCTION_P (cand->fn))
13133 : : return NULL_TREE;
13134 : :
13135 : : /* "For conversion functions that are implicit object member functions,
13136 : : the function is considered to be a member of the class of the implied
13137 : : object argument for the purpose of defining the type of the implicit
13138 : : object parameter." */
13139 : 76537 : if (DECL_CONV_FN_P (cand->fn))
13140 : 0 : return TYPE_MAIN_VARIANT (TREE_TYPE (cand->first_arg));
13141 : :
13142 : : /* "For non-conversion functions that are implicit object member
13143 : : functions nominated by a using-declaration in a derived class, the
13144 : : function is considered to be a member of the derived class for the
13145 : : purpose of defining the type of the implicit object parameter."
13146 : :
13147 : : That derived class is reflected in the conversion_path binfo. */
13148 : 76537 : return BINFO_TYPE (cand->conversion_path);
13149 : : }
13150 : :
13151 : : /* Return whether the first parameter of C1 matches the second parameter
13152 : : of C2. */
13153 : :
13154 : : static bool
13155 : 148981 : reversed_match (z_candidate *c1, z_candidate *c2)
13156 : : {
13157 : 148981 : tree fn1 = c1->fn;
13158 : 148981 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (c2->fn));
13159 : 148981 : tree parm2 = TREE_VALUE (TREE_CHAIN (parms2));
13160 : 148981 : if (DECL_IOBJ_MEMBER_FUNCTION_P (fn1))
13161 : : {
13162 : 76537 : tree ctx = class_of_implicit_object (c1);
13163 : 76537 : return iobj_parm_corresponds_to (fn1, parm2, ctx);
13164 : : }
13165 : : else
13166 : : {
13167 : 72444 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn1));
13168 : 72444 : tree parm1 = TREE_VALUE (parms1);
13169 : 72444 : return same_type_p (parm1, parm2);
13170 : : }
13171 : : }
13172 : :
13173 : : /* True if the defining declarations of the two candidates have equivalent
13174 : : parameters. MATCH_KIND controls whether we're trying to compare the
13175 : : original declarations (for a warning) or the actual candidates. */
13176 : :
13177 : : enum class pmatch { original, current };
13178 : :
13179 : : static bool
13180 : 1907442 : cand_parms_match (z_candidate *c1, z_candidate *c2, pmatch match_kind)
13181 : : {
13182 : 1907442 : tree fn1 = c1->fn;
13183 : 1907442 : tree fn2 = c2->fn;
13184 : 1907442 : bool reversed = (match_kind == pmatch::current
13185 : 1907442 : && c1->reversed () != c2->reversed ());
13186 : 1907442 : if (fn1 == fn2 && !reversed)
13187 : : return true;
13188 : 1907401 : if (identifier_p (fn1) || identifier_p (fn2))
13189 : : return false;
13190 : 1907397 : if (match_kind == pmatch::original)
13191 : : {
13192 : : /* We don't look at c1->template_decl because that's only set for
13193 : : primary templates, not e.g. non-template member functions of
13194 : : class templates. */
13195 : 13 : tree t1 = most_general_template (fn1);
13196 : 13 : tree t2 = most_general_template (fn2);
13197 : 13 : if (t1 || t2)
13198 : : {
13199 : 8 : if (!t1 || !t2)
13200 : : return false;
13201 : 8 : if (t1 == t2)
13202 : : return true;
13203 : 0 : fn1 = DECL_TEMPLATE_RESULT (t1);
13204 : 0 : fn2 = DECL_TEMPLATE_RESULT (t2);
13205 : : }
13206 : : }
13207 : :
13208 : 1907389 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn1));
13209 : 1907389 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (fn2));
13210 : :
13211 : 3760504 : if (DECL_FUNCTION_MEMBER_P (fn1)
13212 : 1907786 : && DECL_FUNCTION_MEMBER_P (fn2))
13213 : : {
13214 : 54643 : tree base1 = DECL_CONTEXT (strip_inheriting_ctors (fn1));
13215 : 54643 : tree base2 = DECL_CONTEXT (strip_inheriting_ctors (fn2));
13216 : 54643 : if (base1 != base2)
13217 : 38856 : return false;
13218 : :
13219 : 54529 : if (reversed)
13220 : 38740 : return (reversed_match (c1, c2)
13221 : 38740 : && reversed_match (c2, c1));
13222 : :
13223 : : /* Use object_parms_correspond to simplify comparing iobj/xobj/static
13224 : : member functions. */
13225 : 15789 : if (!object_parms_correspond (fn1, fn2, base1))
13226 : : return false;
13227 : :
13228 : : /* We just compared the object parameters, if they don't correspond
13229 : : we already returned false. */
13230 : 47361 : auto skip_parms = [] (tree fn, tree parms)
13231 : : {
13232 : 31574 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
13233 : 680 : return TREE_CHAIN (parms);
13234 : : else
13235 : 30894 : return skip_artificial_parms_for (fn, parms);
13236 : : };
13237 : 15787 : parms1 = skip_parms (fn1, parms1);
13238 : 15787 : parms2 = skip_parms (fn2, parms2);
13239 : : }
13240 : 1852746 : else if (reversed)
13241 : 36204 : return (reversed_match (c1, c2)
13242 : 36204 : && reversed_match (c2, c1));
13243 : 1832329 : return compparms (parms1, parms2);
13244 : : }
13245 : :
13246 : : /* True iff FN is a copy or move constructor or assignment operator. */
13247 : :
13248 : : static bool
13249 : 17993240 : sfk_copy_or_move (tree fn)
13250 : : {
13251 : 17993240 : if (TREE_CODE (fn) != FUNCTION_DECL)
13252 : : return false;
13253 : 17993159 : special_function_kind sfk = special_function_p (fn);
13254 : 17993159 : return sfk >= sfk_copy_constructor && sfk <= sfk_move_assignment;
13255 : : }
13256 : :
13257 : : /* Compare two candidates for overloading as described in
13258 : : [over.match.best]. Return values:
13259 : :
13260 : : 1: cand1 is better than cand2
13261 : : -1: cand2 is better than cand1
13262 : : 0: cand1 and cand2 are indistinguishable */
13263 : :
13264 : : static int
13265 : 68658479 : joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
13266 : : tsubst_flags_t complain)
13267 : : {
13268 : 68658479 : int winner = 0;
13269 : 68658479 : int off1 = 0, off2 = 0;
13270 : 68658479 : size_t i;
13271 : 68658479 : size_t len;
13272 : :
13273 : : /* Candidates that involve bad conversions are always worse than those
13274 : : that don't. */
13275 : 68658479 : if (cand1->viable > cand2->viable)
13276 : : return 1;
13277 : 55784070 : if (cand1->viable < cand2->viable)
13278 : : return -1;
13279 : :
13280 : : /* If we have two pseudo-candidates for conversions to the same type,
13281 : : or two candidates for the same function, arbitrarily pick one. */
13282 : 55784070 : if (cand1->fn == cand2->fn
13283 : 1605792 : && cand1->reversed () == cand2->reversed ()
13284 : 56926113 : && (IS_TYPE_OR_DECL_P (cand1->fn)))
13285 : : return 1;
13286 : :
13287 : : /* Prefer a non-deleted function over an implicitly deleted move
13288 : : constructor or assignment operator. This differs slightly from the
13289 : : wording for issue 1402 (which says the move op is ignored by overload
13290 : : resolution), but this way produces better error messages. */
13291 : 55784058 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13292 : 51592908 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13293 : 107370776 : && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn))
13294 : : {
13295 : 2100771 : if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn)
13296 : 1783720 : && move_fn_p (cand1->fn))
13297 : : return -1;
13298 : 3246438 : if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn)
13299 : 2595520 : && move_fn_p (cand2->fn))
13300 : : return 1;
13301 : : }
13302 : :
13303 : : /* a viable function F1
13304 : : is defined to be a better function than another viable function F2 if
13305 : : for all arguments i, ICSi(F1) is not a worse conversion sequence than
13306 : : ICSi(F2), and then */
13307 : :
13308 : : /* for some argument j, ICSj(F1) is a better conversion sequence than
13309 : : ICSj(F2) */
13310 : :
13311 : : /* For comparing static and non-static member functions, we ignore
13312 : : the implicit object parameter of the non-static function. The
13313 : : standard says to pretend that the static function has an object
13314 : : parm, but that won't work with operator overloading. */
13315 : 55761096 : len = cand1->num_convs;
13316 : 55761096 : if (len != cand2->num_convs)
13317 : : {
13318 : 141 : int static_1 = (TREE_CODE (cand1->fn) == FUNCTION_DECL
13319 : 141 : && DECL_STATIC_FUNCTION_P (cand1->fn));
13320 : 141 : int static_2 = (TREE_CODE (cand2->fn) == FUNCTION_DECL
13321 : 141 : && DECL_STATIC_FUNCTION_P (cand2->fn));
13322 : :
13323 : 141 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13324 : 125 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13325 : 125 : && DECL_CONSTRUCTOR_P (cand1->fn)
13326 : 147 : && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
13327 : : /* We're comparing a near-match list constructor and a near-match
13328 : : non-list constructor. Just treat them as unordered. */
13329 : : return 0;
13330 : :
13331 : 135 : gcc_assert (static_1 != static_2);
13332 : :
13333 : 135 : if (static_1)
13334 : : {
13335 : : /* C++23 [over.best.ics.general] says:
13336 : : When the parameter is the implicit object parameter of a static
13337 : : member function, the implicit conversion sequence is a standard
13338 : : conversion sequence that is neither better nor worse than any
13339 : : other standard conversion sequence. */
13340 : 40 : if (CONVERSION_RANK (cand2->convs[0]) >= cr_user)
13341 : 0 : winner = 1;
13342 : : off2 = 1;
13343 : : }
13344 : : else
13345 : : {
13346 : 95 : if (CONVERSION_RANK (cand1->convs[0]) >= cr_user)
13347 : 16 : winner = -1;
13348 : 95 : off1 = 1;
13349 : 95 : --len;
13350 : : }
13351 : : }
13352 : :
13353 : 132920087 : for (i = 0; i < len; ++i)
13354 : : {
13355 : 77159613 : conversion *t1 = cand1->convs[i + off1];
13356 : 77159613 : conversion *t2 = cand2->convs[i + off2];
13357 : 77159613 : int comp = compare_ics (t1, t2);
13358 : :
13359 : 77159613 : if (comp != 0)
13360 : : {
13361 : 51181468 : if ((complain & tf_warning)
13362 : 44351175 : && warn_sign_promo
13363 : 82 : && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
13364 : : == cr_std + cr_promotion)
13365 : 50 : && t1->kind == ck_std
13366 : 31 : && t2->kind == ck_std
13367 : 31 : && TREE_CODE (t1->type) == INTEGER_TYPE
13368 : 31 : && TREE_CODE (t2->type) == INTEGER_TYPE
13369 : 31 : && (TYPE_PRECISION (t1->type)
13370 : 31 : == TYPE_PRECISION (t2->type))
13371 : 51181499 : && (TYPE_UNSIGNED (next_conversion (t1)->type)
13372 : 0 : || (TREE_CODE (next_conversion (t1)->type)
13373 : : == ENUMERAL_TYPE)))
13374 : : {
13375 : 31 : tree type = next_conversion (t1)->type;
13376 : 31 : tree type1, type2;
13377 : 31 : struct z_candidate *w, *l;
13378 : 31 : if (comp > 0)
13379 : : type1 = t1->type, type2 = t2->type,
13380 : : w = cand1, l = cand2;
13381 : : else
13382 : 8 : type1 = t2->type, type2 = t1->type,
13383 : 8 : w = cand2, l = cand1;
13384 : :
13385 : 31 : if (warn)
13386 : : {
13387 : 9 : warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
13388 : : type, type1, type2);
13389 : 9 : warning (OPT_Wsign_promo, " in call to %qD", w->fn);
13390 : : }
13391 : : else
13392 : 22 : add_warning (w, l);
13393 : : }
13394 : :
13395 : 51181468 : if (winner && comp != winner)
13396 : : {
13397 : : /* Ambiguity between normal and reversed comparison operators
13398 : : with the same parameter types. P2468 decided not to go with
13399 : : this approach to resolving the ambiguity, so pedwarn. */
13400 : 616 : if ((complain & tf_warning_or_error)
13401 : 427 : && (cand1->reversed () != cand2->reversed ())
13402 : 674 : && cand_parms_match (cand1, cand2, pmatch::original))
13403 : : {
13404 : 49 : struct z_candidate *w, *l;
13405 : 49 : if (cand2->reversed ())
13406 : : winner = 1, w = cand1, l = cand2;
13407 : : else
13408 : 29 : winner = -1, w = cand2, l = cand1;
13409 : 49 : if (warn)
13410 : : {
13411 : 20 : auto_diagnostic_group d;
13412 : 20 : if (pedwarn (input_location, 0,
13413 : : "C++20 says that these are ambiguous, "
13414 : : "even though the second is reversed:"))
13415 : : {
13416 : 20 : print_z_candidate (input_location,
13417 : : N_("candidate 1:"), w);
13418 : 20 : print_z_candidate (input_location,
13419 : : N_("candidate 2:"), l);
13420 : 20 : if (w->fn == l->fn
13421 : 16 : && DECL_IOBJ_MEMBER_FUNCTION_P (w->fn)
13422 : 35 : && (type_memfn_quals (TREE_TYPE (w->fn))
13423 : 15 : & TYPE_QUAL_CONST) == 0)
13424 : : {
13425 : : /* Suggest adding const to
13426 : : struct A { bool operator==(const A&); }; */
13427 : 12 : tree parmtype
13428 : 12 : = FUNCTION_FIRST_USER_PARMTYPE (w->fn);
13429 : 12 : parmtype = TREE_VALUE (parmtype);
13430 : 12 : if (TYPE_REF_P (parmtype)
13431 : 12 : && TYPE_READONLY (TREE_TYPE (parmtype))
13432 : 24 : && (same_type_ignoring_top_level_qualifiers_p
13433 : 12 : (TREE_TYPE (parmtype),
13434 : 12 : DECL_CONTEXT (w->fn))))
13435 : 12 : inform (DECL_SOURCE_LOCATION (w->fn),
13436 : : "try making the operator a %<const%> "
13437 : : "member function");
13438 : : }
13439 : : }
13440 : 20 : }
13441 : : else
13442 : 29 : add_warning (w, l);
13443 : 49 : return winner;
13444 : : }
13445 : :
13446 : 567 : winner = 0;
13447 : 567 : goto tweak;
13448 : : }
13449 : : winner = comp;
13450 : : }
13451 : : }
13452 : :
13453 : : /* warn about confusing overload resolution for user-defined conversions,
13454 : : either between a constructor and a conversion op, or between two
13455 : : conversion ops. */
13456 : 55760474 : if ((complain & tf_warning)
13457 : : /* In C++17, the constructor might have been elided, which means that
13458 : : an originally null ->second_conv could become non-null. */
13459 : 47812138 : && winner && warn_conversion && cand1->second_conv && cand2->second_conv
13460 : 54 : && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
13461 : 55760501 : && winner != compare_ics (cand1->second_conv, cand2->second_conv))
13462 : : {
13463 : 27 : struct z_candidate *w, *l;
13464 : 27 : bool give_warning = false;
13465 : :
13466 : 27 : if (winner == 1)
13467 : : w = cand1, l = cand2;
13468 : : else
13469 : 6 : w = cand2, l = cand1;
13470 : :
13471 : : /* We don't want to complain about `X::operator T1 ()'
13472 : : beating `X::operator T2 () const', when T2 is a no less
13473 : : cv-qualified version of T1. */
13474 : 27 : if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
13475 : 39 : && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
13476 : : {
13477 : 6 : tree t = TREE_TYPE (TREE_TYPE (l->fn));
13478 : 6 : tree f = TREE_TYPE (TREE_TYPE (w->fn));
13479 : :
13480 : 6 : if (TREE_CODE (t) == TREE_CODE (f) && INDIRECT_TYPE_P (t))
13481 : : {
13482 : 6 : t = TREE_TYPE (t);
13483 : 6 : f = TREE_TYPE (f);
13484 : : }
13485 : 6 : if (!comp_ptr_ttypes (t, f))
13486 : : give_warning = true;
13487 : : }
13488 : : else
13489 : : give_warning = true;
13490 : :
13491 : : if (!give_warning)
13492 : : /*NOP*/;
13493 : 21 : else if (warn)
13494 : : {
13495 : 9 : tree source = source_type (w->convs[0]);
13496 : 9 : if (INDIRECT_TYPE_P (source))
13497 : 6 : source = TREE_TYPE (source);
13498 : 9 : auto_diagnostic_group d;
13499 : 9 : if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
13500 : 18 : && warning (OPT_Wconversion, " for conversion from %qH to %qI",
13501 : 9 : source, w->second_conv->type))
13502 : : {
13503 : 9 : inform (input_location, " because conversion sequence "
13504 : : "for the argument is better");
13505 : : }
13506 : 9 : }
13507 : : else
13508 : 12 : add_warning (w, l);
13509 : : }
13510 : :
13511 : 55760474 : if (winner)
13512 : : return winner;
13513 : :
13514 : : /* DR 495 moved this tiebreaker above the template ones. */
13515 : : /* or, if not that,
13516 : : the context is an initialization by user-defined conversion (see
13517 : : _dcl.init_ and _over.match.user_) and the standard conversion
13518 : : sequence from the return type of F1 to the destination type (i.e.,
13519 : : the type of the entity being initialized) is a better conversion
13520 : : sequence than the standard conversion sequence from the return type
13521 : : of F2 to the destination type. */
13522 : :
13523 : 8996756 : if (cand1->second_conv)
13524 : : {
13525 : 49243 : winner = compare_ics (cand1->second_conv, cand2->second_conv);
13526 : 49243 : if (winner)
13527 : : return winner;
13528 : : }
13529 : :
13530 : : /* CWG2735 (PR109247): A copy/move ctor/op= for which its operand uses an
13531 : : explicit conversion (due to list-initialization) is worse. */
13532 : 8996620 : {
13533 : 8996620 : z_candidate *sp = nullptr;
13534 : 8996620 : if (sfk_copy_or_move (cand1->fn))
13535 : 531 : sp = cand1;
13536 : 8996620 : if (sfk_copy_or_move (cand2->fn))
13537 : 720151 : sp = sp ? nullptr : cand2;
13538 : 8996571 : if (sp)
13539 : : {
13540 : 1441168 : conversion *conv = sp->convs[!DECL_CONSTRUCTOR_P (sp->fn)];
13541 : 720584 : if (conv->user_conv_p)
13542 : 1398 : for (; conv; conv = next_conversion (conv))
13543 : 1274 : if (conv->kind == ck_user
13544 : 575 : && DECL_P (conv->cand->fn)
13545 : 1849 : && DECL_NONCONVERTING_P (conv->cand->fn))
13546 : 509 : return (sp == cand1) ? -1 : 1;
13547 : : }
13548 : : }
13549 : :
13550 : : /* DR2327: C++17 copy elision in [over.match.ctor] (direct-init) context.
13551 : : The standard currently says that only constructors are candidates, but if
13552 : : one copies a prvalue returned by a conversion function we prefer that.
13553 : :
13554 : : Clang does something similar, as discussed at
13555 : : http://lists.isocpp.org/core/2017/10/3166.php
13556 : : http://lists.isocpp.org/core/2019/03/5721.php */
13557 : 6643818 : if (len == 1 && cxx_dialect >= cxx17
13558 : 6629435 : && DECL_P (cand1->fn)
13559 : 6629435 : && DECL_COMPLETE_CONSTRUCTOR_P (cand1->fn)
13560 : 9652153 : && !(cand1->flags & LOOKUP_ONLYCONVERTING))
13561 : : {
13562 : 294077 : bool elided1 = joust_maybe_elide_copy (cand1);
13563 : 294077 : bool elided2 = joust_maybe_elide_copy (cand2);
13564 : 294077 : winner = elided1 - elided2;
13565 : 294077 : if (winner)
13566 : : return winner;
13567 : : }
13568 : :
13569 : : /* or, if not that,
13570 : : F1 is a non-template function and F2 is a template function
13571 : : specialization. */
13572 : :
13573 : 8996168 : if (!cand1->template_decl && cand2->template_decl)
13574 : : return 1;
13575 : 8967346 : else if (cand1->template_decl && !cand2->template_decl)
13576 : : return -1;
13577 : :
13578 : : /* or, if not that,
13579 : : F1 and F2 are template functions and the function template for F1 is
13580 : : more specialized than the template for F2 according to the partial
13581 : : ordering rules. */
13582 : :
13583 : 8052585 : if (cand1->template_decl && cand2->template_decl)
13584 : : {
13585 : 2894355 : winner = more_specialized_fn
13586 : 2894355 : (TI_TEMPLATE (cand1->template_decl),
13587 : 2894355 : TI_TEMPLATE (cand2->template_decl),
13588 : : /* [temp.func.order]: The presence of unused ellipsis and default
13589 : : arguments has no effect on the partial ordering of function
13590 : : templates. add_function_candidate() will not have
13591 : : counted the "this" argument for constructors. */
13592 : 5788710 : cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
13593 : 2894355 : if (winner)
13594 : : return winner;
13595 : : }
13596 : :
13597 : : /* F1 and F2 are non-template functions and
13598 : : - they have the same non-object-parameter-type-lists ([dcl.fct]), and
13599 : : - if they are member functions, both are direct members of the same
13600 : : class, and
13601 : : - if both are non-static member functions, they have the same types for
13602 : : their object parameters, and
13603 : : - F1 is more constrained than F2 according to the partial ordering of
13604 : : constraints described in [temp.constr.order]. */
13605 : 2455353 : if (flag_concepts && DECL_P (cand1->fn) && DECL_P (cand2->fn)
13606 : 2455326 : && !cand1->template_decl && !cand2->template_decl
13607 : 7622627 : && cand_parms_match (cand1, cand2, pmatch::current))
13608 : : {
13609 : 111345 : winner = more_constrained (cand1->fn, cand2->fn);
13610 : 111345 : if (winner)
13611 : : return winner;
13612 : : }
13613 : :
13614 : : /* F2 is a rewritten candidate (12.4.1.2) and F1 is not, or F1 and F2 are
13615 : : rewritten candidates, and F2 is a synthesized candidate with reversed
13616 : : order of parameters and F1 is not. */
13617 : 5708286 : if (cand1->rewritten ())
13618 : : {
13619 : 522252 : if (!cand2->rewritten ())
13620 : : return -1;
13621 : 371509 : if (!cand1->reversed () && cand2->reversed ())
13622 : : return 1;
13623 : 371509 : if (cand1->reversed () && !cand2->reversed ())
13624 : : return -1;
13625 : : }
13626 : 5186034 : else if (cand2->rewritten ())
13627 : : return 1;
13628 : :
13629 : 5167755 : if (deduction_guide_p (cand1->fn))
13630 : : {
13631 : 6520 : gcc_assert (deduction_guide_p (cand2->fn));
13632 : :
13633 : : /* F1 and F2 are generated from class template argument deduction for a
13634 : : class D, and F2 is generated from inheriting constructors from a base
13635 : : class of D while F1 is not, and for each explicit function argument,
13636 : : the corresponding parameters of F1 and F2 are either both ellipses or
13637 : : have the same type. */
13638 : 6520 : bool inherited1 = inherited_guide_p (cand1->fn);
13639 : 6520 : bool inherited2 = inherited_guide_p (cand2->fn);
13640 : 6520 : if (int diff = inherited2 - inherited1)
13641 : : {
13642 : 52 : for (i = 0; i < len; ++i)
13643 : : {
13644 : 15 : conversion *t1 = cand1->convs[i + off1];
13645 : 15 : conversion *t2 = cand2->convs[i + off2];
13646 : : /* ??? It seems the ellipses part of this tiebreaker isn't
13647 : : needed since a mismatch should have broken the tie earlier
13648 : : during ICS comparison. */
13649 : 15 : gcc_checking_assert (t1->ellipsis_p == t2->ellipsis_p);
13650 : 15 : if (!same_type_p (t1->type, t2->type))
13651 : : break;
13652 : : }
13653 : 39 : if (i == len)
13654 : : return diff;
13655 : : }
13656 : :
13657 : : /* F1 is generated from a deduction-guide (13.3.1.8) and F2 is not */
13658 : : /* We distinguish between candidates from an explicit deduction guide and
13659 : : candidates built from a constructor based on DECL_ARTIFICIAL. */
13660 : 6483 : int art1 = DECL_ARTIFICIAL (cand1->fn);
13661 : 6483 : int art2 = DECL_ARTIFICIAL (cand2->fn);
13662 : 6483 : if (art1 != art2)
13663 : 1284 : return art2 - art1;
13664 : :
13665 : 5199 : if (art1)
13666 : : {
13667 : : /* Prefer the special copy guide over a declared copy/move
13668 : : constructor. */
13669 : 5196 : if (copy_guide_p (cand1->fn))
13670 : : return 1;
13671 : 74 : if (copy_guide_p (cand2->fn))
13672 : : return -1;
13673 : :
13674 : : /* Prefer a candidate generated from a non-template constructor. */
13675 : 26 : int tg1 = template_guide_p (cand1->fn);
13676 : 26 : int tg2 = template_guide_p (cand2->fn);
13677 : 26 : if (tg1 != tg2)
13678 : 6 : return tg2 - tg1;
13679 : : }
13680 : : }
13681 : :
13682 : : /* F1 is a constructor for a class D, F2 is a constructor for a base class B
13683 : : of D, and for all arguments the corresponding parameters of F1 and F2 have
13684 : : the same type (CWG 2273/2277). */
13685 : 15483615 : if (DECL_INHERITED_CTOR (cand1->fn) || DECL_INHERITED_CTOR (cand2->fn))
13686 : : {
13687 : 92 : tree base1 = DECL_CONTEXT (strip_inheriting_ctors (cand1->fn));
13688 : 92 : tree base2 = DECL_CONTEXT (strip_inheriting_ctors (cand2->fn));
13689 : :
13690 : 92 : bool used1 = false;
13691 : 92 : bool used2 = false;
13692 : 92 : if (base1 == base2)
13693 : : /* No difference. */;
13694 : 92 : else if (DERIVED_FROM_P (base1, base2))
13695 : : used1 = true;
13696 : 18 : else if (DERIVED_FROM_P (base2, base1))
13697 : : used2 = true;
13698 : :
13699 : 83 : if (int diff = used2 - used1)
13700 : : {
13701 : 110 : for (i = 0; i < len; ++i)
13702 : : {
13703 : 36 : conversion *t1 = cand1->convs[i + off1];
13704 : 36 : conversion *t2 = cand2->convs[i + off2];
13705 : 36 : if (!same_type_p (t1->type, t2->type))
13706 : : break;
13707 : : }
13708 : 83 : if (i == len)
13709 : : return diff;
13710 : : }
13711 : : }
13712 : :
13713 : : /* Check whether we can discard a builtin candidate, either because we
13714 : : have two identical ones or matching builtin and non-builtin candidates.
13715 : :
13716 : : (Pedantically in the latter case the builtin which matched the user
13717 : : function should not be added to the overload set, but we spot it here.
13718 : :
13719 : : [over.match.oper]
13720 : : ... the builtin candidates include ...
13721 : : - do not have the same parameter type list as any non-template
13722 : : non-member candidate. */
13723 : :
13724 : 5161184 : if (identifier_p (cand1->fn) || identifier_p (cand2->fn))
13725 : : {
13726 : 65 : for (i = 0; i < len; ++i)
13727 : 52 : if (!same_type_p (cand1->convs[i]->type,
13728 : : cand2->convs[i]->type))
13729 : : break;
13730 : 37 : if (i == cand1->num_convs)
13731 : : {
13732 : 13 : if (cand1->fn == cand2->fn)
13733 : : /* Two built-in candidates; arbitrarily pick one. */
13734 : : return 1;
13735 : 14387601 : else if (identifier_p (cand1->fn))
13736 : : /* cand1 is built-in; prefer cand2. */
13737 : : return -1;
13738 : : else
13739 : : /* cand2 is built-in; prefer cand1. */
13740 : : return 1;
13741 : : }
13742 : : }
13743 : :
13744 : : /* For candidates of a multi-versioned function, make the version with
13745 : : the highest priority win. This version will be checked for dispatching
13746 : : first. If this version can be inlined into the caller, the front-end
13747 : : will simply make a direct call to this function. */
13748 : :
13749 : 5161171 : if (TREE_CODE (cand1->fn) == FUNCTION_DECL
13750 : 5161144 : && DECL_FUNCTION_VERSIONED (cand1->fn)
13751 : 890 : && TREE_CODE (cand2->fn) == FUNCTION_DECL
13752 : 5162061 : && DECL_FUNCTION_VERSIONED (cand2->fn))
13753 : : {
13754 : 890 : tree f1 = TREE_TYPE (cand1->fn);
13755 : 890 : tree f2 = TREE_TYPE (cand2->fn);
13756 : 890 : tree p1 = TYPE_ARG_TYPES (f1);
13757 : 890 : tree p2 = TYPE_ARG_TYPES (f2);
13758 : :
13759 : : /* Check if cand1->fn and cand2->fn are versions of the same function. It
13760 : : is possible that cand1->fn and cand2->fn are function versions but of
13761 : : different functions. Check types to see if they are versions of the same
13762 : : function. */
13763 : 890 : if (compparms (p1, p2)
13764 : 890 : && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
13765 : : {
13766 : : /* Always make the version with the higher priority, more
13767 : : specialized, win. */
13768 : 890 : gcc_assert (targetm.compare_version_priority);
13769 : 890 : if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
13770 : : return 1;
13771 : : else
13772 : : return -1;
13773 : : }
13774 : : }
13775 : :
13776 : : /* If the two function declarations represent the same function (this can
13777 : : happen with declarations in multiple scopes and arg-dependent lookup),
13778 : : arbitrarily choose one. But first make sure the default args we're
13779 : : using match. */
13780 : 5160254 : if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
13781 : 10320535 : && equal_functions (cand1->fn, cand2->fn))
13782 : : {
13783 : 31 : tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
13784 : 31 : tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
13785 : :
13786 : 62 : gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
13787 : :
13788 : 44 : for (i = 0; i < len; ++i)
13789 : : {
13790 : : /* Don't crash if the fn is variadic. */
13791 : 13 : if (!parms1)
13792 : : break;
13793 : 13 : parms1 = TREE_CHAIN (parms1);
13794 : 13 : parms2 = TREE_CHAIN (parms2);
13795 : : }
13796 : :
13797 : 31 : if (off1)
13798 : 0 : parms1 = TREE_CHAIN (parms1);
13799 : 31 : else if (off2)
13800 : 0 : parms2 = TREE_CHAIN (parms2);
13801 : :
13802 : 59 : for (; parms1; ++i)
13803 : : {
13804 : 68 : if (!cp_tree_equal (TREE_PURPOSE (parms1),
13805 : 34 : TREE_PURPOSE (parms2)))
13806 : : {
13807 : 6 : if (warn)
13808 : : {
13809 : 3 : if (complain & tf_error)
13810 : : {
13811 : 3 : auto_diagnostic_group d;
13812 : 3 : if (permerror (input_location,
13813 : : "default argument mismatch in "
13814 : : "overload resolution"))
13815 : : {
13816 : 3 : inform (DECL_SOURCE_LOCATION (cand1->fn),
13817 : : " candidate 1: %q#F", cand1->fn);
13818 : 3 : inform (DECL_SOURCE_LOCATION (cand2->fn),
13819 : : " candidate 2: %q#F", cand2->fn);
13820 : : }
13821 : 3 : }
13822 : : else
13823 : : return 0;
13824 : : }
13825 : : else
13826 : 3 : add_warning (cand1, cand2);
13827 : : break;
13828 : : }
13829 : 28 : parms1 = TREE_CHAIN (parms1);
13830 : 28 : parms2 = TREE_CHAIN (parms2);
13831 : : }
13832 : :
13833 : 31 : return 1;
13834 : : }
13835 : :
13836 : 5160817 : tweak:
13837 : :
13838 : : /* Extension: If the worst conversion for one candidate is better than the
13839 : : worst conversion for the other, take the first. */
13840 : 5160817 : if (!pedantic && (complain & tf_warning_or_error))
13841 : : {
13842 : : conversion_rank rank1 = cr_identity, rank2 = cr_identity;
13843 : 12065203 : struct z_candidate *w = 0, *l = 0;
13844 : :
13845 : 12065203 : for (i = 0; i < len; ++i)
13846 : : {
13847 : 7100052 : if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
13848 : 4907397 : rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
13849 : 7100052 : if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
13850 : 4907413 : rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
13851 : : }
13852 : 4965151 : if (rank1 < rank2)
13853 : 32 : winner = 1, w = cand1, l = cand2;
13854 : 4965151 : if (rank1 > rank2)
13855 : : winner = -1, w = cand2, l = cand1;
13856 : 4965042 : if (winner)
13857 : : {
13858 : : /* Don't choose a deleted function over ambiguity. */
13859 : 141 : if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
13860 : : return 0;
13861 : 141 : if (warn)
13862 : : {
13863 : 6 : auto_diagnostic_group d;
13864 : 6 : if (pedwarn (input_location, 0,
13865 : : "ISO C++ says that these are ambiguous, even "
13866 : : "though the worst conversion for the first is "
13867 : : "better than the worst conversion for the second:"))
13868 : : {
13869 : 3 : print_z_candidate (input_location, N_("candidate 1:"), w);
13870 : 3 : print_z_candidate (input_location, N_("candidate 2:"), l);
13871 : : }
13872 : 6 : }
13873 : : else
13874 : 135 : add_warning (w, l);
13875 : 141 : return winner;
13876 : : }
13877 : : }
13878 : :
13879 : : gcc_assert (!winner);
13880 : : return 0;
13881 : : }
13882 : :
13883 : : /* Given a list of candidates for overloading, find the best one, if any.
13884 : : This algorithm has a worst case of O(2n) (winner is last), and a best
13885 : : case of O(n/2) (totally ambiguous); much better than a sorting
13886 : : algorithm. The candidates list is assumed to be sorted according
13887 : : to viability (via splice_viable). */
13888 : :
13889 : : static struct z_candidate *
13890 : 185617611 : tourney (struct z_candidate *candidates, tsubst_flags_t complain)
13891 : : {
13892 : 185617611 : struct z_candidate **champ = &candidates, **challenger;
13893 : 185617611 : int fate;
13894 : 185617611 : struct z_candidate *previous_worse_champ = nullptr;
13895 : :
13896 : : /* Walk through the list once, comparing each current champ to the next
13897 : : candidate, knocking out a candidate or two with each comparison. */
13898 : :
13899 : 242688223 : for (challenger = &candidates->next; *challenger && (*challenger)->viable; )
13900 : : {
13901 : 57095281 : fate = joust (*champ, *challenger, 0, complain);
13902 : 57095281 : if (fate == 1)
13903 : 37316258 : challenger = &(*challenger)->next;
13904 : 19779023 : else if (fate == -1)
13905 : : {
13906 : 14620047 : previous_worse_champ = *champ;
13907 : 14620047 : champ = challenger;
13908 : 14620047 : challenger = &(*challenger)->next;
13909 : : }
13910 : : else
13911 : : {
13912 : 5158976 : previous_worse_champ = nullptr;
13913 : 5158976 : champ = &(*challenger)->next;
13914 : 5158976 : if (!*champ || !(*champ)->viable
13915 : 5134401 : || (*champ)->viable < (*challenger)->viable)
13916 : : {
13917 : : champ = nullptr;
13918 : : break;
13919 : : }
13920 : 5134307 : challenger = &(*champ)->next;
13921 : : }
13922 : : }
13923 : :
13924 : : /* Make sure the champ is better than all the candidates it hasn't yet
13925 : : been compared to. */
13926 : :
13927 : 185617611 : if (champ)
13928 : 25092121 : for (challenger = &candidates;
13929 : 210685063 : challenger != champ;
13930 : 25092121 : challenger = &(*challenger)->next)
13931 : : {
13932 : 25094021 : if (*challenger == previous_worse_champ)
13933 : : /* We already know this candidate is worse than the champ. */
13934 : 13530870 : continue;
13935 : 11563151 : fate = joust (*champ, *challenger, 0, complain);
13936 : 11563151 : if (fate != 1)
13937 : : {
13938 : : champ = nullptr;
13939 : : break;
13940 : : }
13941 : : }
13942 : :
13943 : 185592942 : if (!champ)
13944 : 26569 : return nullptr;
13945 : :
13946 : : /* Move the champ to the front of the candidate list. */
13947 : :
13948 : 185591042 : if (champ != &candidates)
13949 : : {
13950 : 15296149 : z_candidate *saved_champ = *champ;
13951 : 15296149 : *champ = saved_champ->next;
13952 : 15296149 : saved_champ->next = candidates;
13953 : 15296149 : candidates = saved_champ;
13954 : : }
13955 : :
13956 : 185591042 : return candidates;
13957 : : }
13958 : :
13959 : : /* Returns nonzero if things of type FROM can be converted to TO. */
13960 : :
13961 : : bool
13962 : 3275681 : can_convert (tree to, tree from, tsubst_flags_t complain)
13963 : : {
13964 : 3275681 : tree arg = NULL_TREE;
13965 : : /* implicit_conversion only considers user-defined conversions
13966 : : if it has an expression for the call argument list. */
13967 : 3275681 : if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to))
13968 : 115 : arg = build_stub_object (from);
13969 : 3275681 : return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain);
13970 : : }
13971 : :
13972 : : /* Returns nonzero if things of type FROM can be converted to TO with a
13973 : : standard conversion. */
13974 : :
13975 : : bool
13976 : 252 : can_convert_standard (tree to, tree from, tsubst_flags_t complain)
13977 : : {
13978 : 252 : return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
13979 : : }
13980 : :
13981 : : /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
13982 : :
13983 : : bool
13984 : 4079711 : can_convert_arg (tree to, tree from, tree arg, int flags,
13985 : : tsubst_flags_t complain)
13986 : : {
13987 : 4079711 : conversion *t;
13988 : 4079711 : bool ok_p;
13989 : :
13990 : 4079711 : conversion_obstack_sentinel cos;
13991 : : /* We want to discard any access checks done for this test,
13992 : : as we might not be in the appropriate access context and
13993 : : we'll do the check again when we actually perform the
13994 : : conversion. */
13995 : 4079711 : push_deferring_access_checks (dk_deferred);
13996 : :
13997 : : /* Handle callers like check_local_shadow forgetting to
13998 : : convert_from_reference. */
13999 : 4079711 : if (TYPE_REF_P (from) && arg)
14000 : : {
14001 : 60 : arg = convert_from_reference (arg);
14002 : 60 : from = TREE_TYPE (arg);
14003 : : }
14004 : :
14005 : 4079711 : t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
14006 : : flags, complain);
14007 : 4079711 : ok_p = (t && !t->bad_p);
14008 : :
14009 : : /* Discard the access checks now. */
14010 : 4079711 : pop_deferring_access_checks ();
14011 : :
14012 : 8159422 : return ok_p;
14013 : 4079711 : }
14014 : :
14015 : : /* Like can_convert_arg, but allows dubious conversions as well. */
14016 : :
14017 : : bool
14018 : 120032008 : can_convert_arg_bad (tree to, tree from, tree arg, int flags,
14019 : : tsubst_flags_t complain)
14020 : : {
14021 : 120032008 : conversion *t;
14022 : :
14023 : 120032008 : conversion_obstack_sentinel cos;
14024 : : /* Try to perform the conversion. */
14025 : 120032008 : t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
14026 : : flags, complain);
14027 : :
14028 : 240064016 : return t != NULL;
14029 : 120032008 : }
14030 : :
14031 : : /* Return an IMPLICIT_CONV_EXPR from EXPR to TYPE with bits set from overload
14032 : : resolution FLAGS. */
14033 : :
14034 : : tree
14035 : 11818632 : build_implicit_conv_flags (tree type, tree expr, int flags)
14036 : : {
14037 : : /* In a template, we are only concerned about determining the
14038 : : type of non-dependent expressions, so we do not have to
14039 : : perform the actual conversion. But for initializers, we
14040 : : need to be able to perform it at instantiation
14041 : : (or instantiate_non_dependent_expr) time. */
14042 : 11818632 : expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
14043 : 11818632 : if (!(flags & LOOKUP_ONLYCONVERTING))
14044 : 3267245 : IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
14045 : 11818632 : if (flags & LOOKUP_NO_NARROWING)
14046 : 5878 : IMPLICIT_CONV_EXPR_BRACED_INIT (expr) = true;
14047 : 11818632 : return expr;
14048 : : }
14049 : :
14050 : : /* Convert EXPR to TYPE. Return the converted expression.
14051 : :
14052 : : Note that we allow bad conversions here because by the time we get to
14053 : : this point we are committed to doing the conversion. If we end up
14054 : : doing a bad conversion, convert_like will complain. */
14055 : :
14056 : : tree
14057 : 206321212 : perform_implicit_conversion_flags (tree type, tree expr,
14058 : : tsubst_flags_t complain, int flags)
14059 : : {
14060 : 206321212 : conversion *conv;
14061 : 206321212 : location_t loc = cp_expr_loc_or_input_loc (expr);
14062 : :
14063 : 206321212 : if (error_operand_p (expr))
14064 : 432 : return error_mark_node;
14065 : :
14066 : 206320780 : conversion_obstack_sentinel cos;
14067 : :
14068 : 206320780 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
14069 : : /*c_cast_p=*/false,
14070 : : flags, complain);
14071 : :
14072 : 206320780 : if (!conv)
14073 : : {
14074 : 170089 : if (complain & tf_error)
14075 : 470 : implicit_conversion_error (loc, type, expr);
14076 : 170089 : expr = error_mark_node;
14077 : : }
14078 : 206150691 : else if (processing_template_decl && conv->kind != ck_identity)
14079 : 11813941 : expr = build_implicit_conv_flags (type, expr, flags);
14080 : : else
14081 : : {
14082 : : /* Give a conversion call the same location as expr. */
14083 : 194336750 : iloc_sentinel il (loc);
14084 : 194336750 : expr = convert_like (conv, expr, complain);
14085 : 194336750 : }
14086 : :
14087 : 206320780 : return expr;
14088 : 206320780 : }
14089 : :
14090 : : tree
14091 : 25169619 : perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
14092 : : {
14093 : 25169619 : return perform_implicit_conversion_flags (type, expr, complain,
14094 : 25169619 : LOOKUP_IMPLICIT);
14095 : : }
14096 : :
14097 : : /* Convert EXPR to TYPE (as a direct-initialization) if that is
14098 : : permitted. If the conversion is valid, the converted expression is
14099 : : returned. Otherwise, NULL_TREE is returned, except in the case
14100 : : that TYPE is a class type; in that case, an error is issued. If
14101 : : C_CAST_P is true, then this direct-initialization is taking
14102 : : place as part of a static_cast being attempted as part of a C-style
14103 : : cast. */
14104 : :
14105 : : tree
14106 : 41920279 : perform_direct_initialization_if_possible (tree type,
14107 : : tree expr,
14108 : : bool c_cast_p,
14109 : : tsubst_flags_t complain)
14110 : : {
14111 : 41920279 : conversion *conv;
14112 : :
14113 : 41920279 : if (type == error_mark_node || error_operand_p (expr))
14114 : : return error_mark_node;
14115 : : /* [dcl.init]
14116 : :
14117 : : If the destination type is a (possibly cv-qualified) class type:
14118 : :
14119 : : -- If the initialization is direct-initialization ...,
14120 : : constructors are considered.
14121 : :
14122 : : -- If overload resolution is successful, the selected constructor
14123 : : is called to initialize the object, with the initializer expression
14124 : : or expression-list as its argument(s).
14125 : :
14126 : : -- Otherwise, if no constructor is viable, the destination type is
14127 : : a (possibly cv-qualified) aggregate class A, and the initializer is
14128 : : a parenthesized expression-list, the object is initialized as
14129 : : follows... */
14130 : 41920279 : if (CLASS_TYPE_P (type))
14131 : : {
14132 : 1677909 : releasing_vec args (make_tree_vector_single (expr));
14133 : 1677909 : expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
14134 : : &args, type, LOOKUP_NORMAL, complain);
14135 : 1677909 : return build_cplus_new (type, expr, complain);
14136 : 1677909 : }
14137 : :
14138 : 40242370 : conversion_obstack_sentinel cos;
14139 : :
14140 : 40242370 : conv = implicit_conversion (type, TREE_TYPE (expr), expr,
14141 : : c_cast_p,
14142 : : LOOKUP_NORMAL, complain);
14143 : 40242370 : if (!conv || conv->bad_p)
14144 : : expr = NULL_TREE;
14145 : 36212732 : else if (processing_template_decl && conv->kind != ck_identity)
14146 : : {
14147 : : /* In a template, we are only concerned about determining the
14148 : : type of non-dependent expressions, so we do not have to
14149 : : perform the actual conversion. But for initializers, we
14150 : : need to be able to perform it at instantiation
14151 : : (or instantiate_non_dependent_expr) time. */
14152 : 671828 : expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
14153 : 671828 : IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
14154 : : }
14155 : : else
14156 : 35540904 : expr = convert_like (conv, expr, NULL_TREE, 0,
14157 : : /*issue_conversion_warnings=*/false,
14158 : : c_cast_p, /*nested_p=*/false, complain);
14159 : :
14160 : 40242370 : return expr;
14161 : 40242370 : }
14162 : :
14163 : : /* When initializing a reference that lasts longer than a full-expression,
14164 : : this special rule applies:
14165 : :
14166 : : [class.temporary]
14167 : :
14168 : : The temporary to which the reference is bound or the temporary
14169 : : that is the complete object to which the reference is bound
14170 : : persists for the lifetime of the reference.
14171 : :
14172 : : The temporaries created during the evaluation of the expression
14173 : : initializing the reference, except the temporary to which the
14174 : : reference is bound, are destroyed at the end of the
14175 : : full-expression in which they are created.
14176 : :
14177 : : In that case, we store the converted expression into a new
14178 : : VAR_DECL in a new scope.
14179 : :
14180 : : However, we want to be careful not to create temporaries when
14181 : : they are not required. For example, given:
14182 : :
14183 : : struct B {};
14184 : : struct D : public B {};
14185 : : D f();
14186 : : const B& b = f();
14187 : :
14188 : : there is no need to copy the return value from "f"; we can just
14189 : : extend its lifetime. Similarly, given:
14190 : :
14191 : : struct S {};
14192 : : struct T { operator S(); };
14193 : : T t;
14194 : : const S& s = t;
14195 : :
14196 : : we can extend the lifetime of the return value of the conversion
14197 : : operator.
14198 : :
14199 : : The next several functions are involved in this lifetime extension. */
14200 : :
14201 : : /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
14202 : : reference is being bound to a temporary. Create and return a new
14203 : : VAR_DECL with the indicated TYPE; this variable will store the value to
14204 : : which the reference is bound. */
14205 : :
14206 : : tree
14207 : 15552 : make_temporary_var_for_ref_to_temp (tree decl, tree type)
14208 : : {
14209 : 15552 : tree var = create_temporary_var (type);
14210 : :
14211 : : /* Register the variable. */
14212 : 15552 : if (VAR_P (decl)
14213 : 15552 : && (TREE_STATIC (decl) || CP_DECL_THREAD_LOCAL_P (decl)))
14214 : : {
14215 : : /* Namespace-scope or local static; give it a mangled name. */
14216 : :
14217 : : /* If an initializer is visible to multiple translation units, those
14218 : : translation units must agree on the addresses of the
14219 : : temporaries. Therefore the temporaries must be given a consistent name
14220 : : and vague linkage. The mangled name of a temporary is the name of the
14221 : : non-temporary object in whose initializer they appear, prefixed with
14222 : : GR and suffixed with a sequence number mangled using the usual rules
14223 : : for a seq-id. Temporaries are numbered with a pre-order, depth-first,
14224 : : left-to-right walk of the complete initializer. */
14225 : 756 : copy_linkage (var, decl);
14226 : :
14227 : 756 : tree name = mangle_ref_init_variable (decl);
14228 : 756 : DECL_NAME (var) = name;
14229 : 756 : SET_DECL_ASSEMBLER_NAME (var, name);
14230 : :
14231 : : /* Set the context to make the variable mergeable in modules. */
14232 : 756 : DECL_CONTEXT (var) = current_scope ();
14233 : : }
14234 : : else
14235 : : /* Create a new cleanup level if necessary. */
14236 : 14796 : maybe_push_cleanup_level (type);
14237 : :
14238 : 15552 : return pushdecl (var);
14239 : : }
14240 : :
14241 : : static tree extend_temps_r (tree *, int *, void *);
14242 : :
14243 : : /* EXPR is the initializer for a variable DECL of reference or
14244 : : std::initializer_list type. Create, push and return a new VAR_DECL
14245 : : for the initializer so that it will live as long as DECL. Any
14246 : : cleanup for the new variable is returned through CLEANUP, and the
14247 : : code to initialize the new variable is returned through INITP. */
14248 : :
14249 : : static tree
14250 : 15552 : set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
14251 : : tree *initp, tree *cond_guard,
14252 : : void *walk_data)
14253 : : {
14254 : 15552 : tree init;
14255 : 15552 : tree type;
14256 : 15552 : tree var;
14257 : :
14258 : : /* Create the temporary variable. */
14259 : 15552 : type = TREE_TYPE (expr);
14260 : 15552 : var = make_temporary_var_for_ref_to_temp (decl, type);
14261 : 15552 : layout_decl (var, 0);
14262 : : /* If the rvalue is the result of a function call it will be
14263 : : a TARGET_EXPR. If it is some other construct (such as a
14264 : : member access expression where the underlying object is
14265 : : itself the result of a function call), turn it into a
14266 : : TARGET_EXPR here. It is important that EXPR be a
14267 : : TARGET_EXPR below since otherwise the INIT_EXPR will
14268 : : attempt to make a bitwise copy of EXPR to initialize
14269 : : VAR. */
14270 : 15552 : if (TREE_CODE (expr) != TARGET_EXPR)
14271 : 0 : expr = get_target_expr (expr);
14272 : : else
14273 : : {
14274 : 15552 : if (TREE_ADDRESSABLE (expr))
14275 : 15484 : TREE_ADDRESSABLE (var) = 1;
14276 : 15552 : if (DECL_MERGEABLE (TARGET_EXPR_SLOT (expr)))
14277 : 505 : DECL_MERGEABLE (var) = true;
14278 : : }
14279 : :
14280 : 15552 : if (TREE_CODE (decl) == FIELD_DECL
14281 : 15552 : && extra_warnings && !warning_suppressed_p (decl))
14282 : : {
14283 : 3 : warning (OPT_Wextra, "a temporary bound to %qD only persists "
14284 : : "until the constructor exits", decl);
14285 : 3 : suppress_warning (decl);
14286 : : }
14287 : :
14288 : : /* Recursively extend temps in this initializer. The recursion needs to come
14289 : : after creating the variable to conform to the mangling ABI, and before
14290 : : maybe_constant_init because the extension might change its result. */
14291 : 15552 : if (walk_data)
14292 : 4155 : cp_walk_tree (&TARGET_EXPR_INITIAL (expr), extend_temps_r,
14293 : : walk_data, nullptr);
14294 : : else
14295 : 11397 : TARGET_EXPR_INITIAL (expr)
14296 : 22794 : = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups,
14297 : : cond_guard);
14298 : :
14299 : : /* Any reference temp has a non-trivial initializer. */
14300 : 15552 : DECL_NONTRIVIALLY_INITIALIZED_P (var) = true;
14301 : :
14302 : : /* If the initializer is constant, put it in DECL_INITIAL so we get
14303 : : static initialization and use in constant expressions. */
14304 : 15552 : init = maybe_constant_init (expr, var, /*manifestly_const_eval=*/true);
14305 : : /* As in store_init_value. */
14306 : 15552 : init = cp_fully_fold (init);
14307 : 15552 : if (TREE_CONSTANT (init))
14308 : : {
14309 : 953 : if (literal_type_p (type)
14310 : 927 : && CP_TYPE_CONST_NON_VOLATILE_P (type)
14311 : 1546 : && !TYPE_HAS_MUTABLE_P (type))
14312 : : {
14313 : : /* 5.19 says that a constant expression can include an
14314 : : lvalue-rvalue conversion applied to "a glvalue of literal type
14315 : : that refers to a non-volatile temporary object initialized
14316 : : with a constant expression". Rather than try to communicate
14317 : : that this VAR_DECL is a temporary, just mark it constexpr. */
14318 : 590 : DECL_DECLARED_CONSTEXPR_P (var) = true;
14319 : 590 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
14320 : 590 : TREE_CONSTANT (var) = true;
14321 : 590 : TREE_READONLY (var) = true;
14322 : : }
14323 : 953 : DECL_INITIAL (var) = init;
14324 : 953 : init = NULL_TREE;
14325 : : }
14326 : : else
14327 : : /* Create the INIT_EXPR that will initialize the temporary
14328 : : variable. */
14329 : 14599 : init = split_nonconstant_init (var, expr);
14330 : 15552 : if (at_function_scope_p ())
14331 : : {
14332 : 14918 : add_decl_expr (var);
14333 : :
14334 : 14918 : if (TREE_STATIC (var))
14335 : 122 : init = add_stmt_to_compound (init, register_dtor_fn (var));
14336 : : else
14337 : : {
14338 : : /* ??? Instead of rebuilding the cleanup, we could replace the slot
14339 : : with var in TARGET_EXPR_CLEANUP (expr). */
14340 : 14796 : tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
14341 : 14796 : if (cleanup)
14342 : : {
14343 : 6092 : if (cond_guard && cleanup != error_mark_node)
14344 : : {
14345 : 23 : if (*cond_guard == NULL_TREE)
14346 : : {
14347 : 23 : *cond_guard = build_local_temp (boolean_type_node);
14348 : 23 : add_decl_expr (*cond_guard);
14349 : 23 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION,
14350 : : *cond_guard, NOP_EXPR,
14351 : : boolean_false_node,
14352 : : tf_warning_or_error);
14353 : 23 : finish_expr_stmt (set);
14354 : : }
14355 : 23 : cleanup = build3 (COND_EXPR, void_type_node,
14356 : : *cond_guard, cleanup, NULL_TREE);
14357 : : }
14358 : 6092 : if (flag_exceptions && TREE_CODE (TREE_TYPE (var)) != ARRAY_TYPE)
14359 : : {
14360 : : /* The normal cleanup for this extended variable isn't pushed
14361 : : until cp_finish_decl, so we need to retain a TARGET_EXPR
14362 : : to clean it up in case a later initializer throws
14363 : : (g++.dg/eh/ref-temp3.C).
14364 : :
14365 : : We don't do this for array temporaries because they have
14366 : : the array cleanup region from build_vec_init.
14367 : :
14368 : : Unlike maybe_push_temp_cleanup, we don't actually need a
14369 : : flag, but a TARGET_EXPR needs a TARGET_EXPR_SLOT.
14370 : : Perhaps this could use WITH_CLEANUP_EXPR instead, but
14371 : : gimplify.cc doesn't handle that, and front-end handling
14372 : : was removed in r8-1725 and r8-1818.
14373 : :
14374 : : Alternately it might be preferable to flatten an
14375 : : initialization with extended temps into a sequence of
14376 : : (non-full-expression) statements, so we could immediately
14377 : : push_cleanup here for only a single cleanup region, but we
14378 : : don't have a mechanism for that in the front-end, only the
14379 : : gimplifier. */
14380 : 5984 : tree targ = get_internal_target_expr (boolean_true_node);
14381 : 5984 : TARGET_EXPR_CLEANUP (targ) = cleanup;
14382 : 5984 : CLEANUP_EH_ONLY (targ) = true;
14383 : : /* Don't actually initialize the bool. */
14384 : 5984 : init = (!init ? void_node
14385 : 5958 : : convert_to_void (init, ICV_STATEMENT, tf_none));
14386 : 5984 : TARGET_EXPR_INITIAL (targ) = init;
14387 : 5984 : init = targ;
14388 : : }
14389 : 6092 : vec_safe_push (*cleanups, cleanup);
14390 : : }
14391 : : }
14392 : :
14393 : : /* We must be careful to destroy the temporary only
14394 : : after its initialization has taken place. If the
14395 : : initialization throws an exception, then the
14396 : : destructor should not be run. We cannot simply
14397 : : transform INIT into something like:
14398 : :
14399 : : (INIT, ({ CLEANUP_STMT; }))
14400 : :
14401 : : because emit_local_var always treats the
14402 : : initializer as a full-expression. Thus, the
14403 : : destructor would run too early; it would run at the
14404 : : end of initializing the reference variable, rather
14405 : : than at the end of the block enclosing the
14406 : : reference variable.
14407 : :
14408 : : The solution is to pass back a cleanup expression
14409 : : which the caller is responsible for attaching to
14410 : : the statement tree. */
14411 : : }
14412 : : else
14413 : : {
14414 : 634 : rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
14415 : 634 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
14416 : : {
14417 : 88 : if (CP_DECL_THREAD_LOCAL_P (var))
14418 : 36 : tls_aggregates = tree_cons (NULL_TREE, var,
14419 : : tls_aggregates);
14420 : : else
14421 : 52 : static_aggregates = tree_cons (NULL_TREE, var,
14422 : : static_aggregates);
14423 : : }
14424 : : else
14425 : : /* Check whether the dtor is callable. */
14426 : 546 : cxx_maybe_build_cleanup (var, tf_warning_or_error);
14427 : : }
14428 : : /* Avoid -Wunused-variable warning (c++/38958). */
14429 : 15552 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
14430 : 15552 : && VAR_P (decl))
14431 : 6147 : TREE_USED (decl) = DECL_READ_P (decl) = true;
14432 : :
14433 : 15552 : *initp = init;
14434 : 15552 : return var;
14435 : : }
14436 : :
14437 : : /* Convert EXPR to the indicated reference TYPE, in a way suitable for
14438 : : initializing a variable of that TYPE. */
14439 : :
14440 : : tree
14441 : 5412410 : initialize_reference (tree type, tree expr,
14442 : : int flags, tsubst_flags_t complain)
14443 : : {
14444 : 5412410 : conversion *conv;
14445 : 5412410 : location_t loc = cp_expr_loc_or_input_loc (expr);
14446 : :
14447 : 5412410 : if (type == error_mark_node || error_operand_p (expr))
14448 : : return error_mark_node;
14449 : :
14450 : 5412345 : conversion_obstack_sentinel cos;
14451 : :
14452 : 5412345 : conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
14453 : : flags, complain);
14454 : : /* If this conversion failed, we're in C++20, and we have something like
14455 : : A& a(b) where A is an aggregate, try again, this time as A& a{b}. */
14456 : 5412345 : if ((!conv || conv->bad_p)
14457 : 581 : && (flags & LOOKUP_AGGREGATE_PAREN_INIT))
14458 : : {
14459 : 4 : tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
14460 : 4 : CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
14461 : 4 : CONSTRUCTOR_IS_PAREN_INIT (e) = true;
14462 : 4 : conversion *c = reference_binding (type, TREE_TYPE (e), e,
14463 : : /*c_cast_p=*/false, flags, complain);
14464 : : /* If this worked, use it. */
14465 : 4 : if (c && !c->bad_p)
14466 : : expr = e, conv = c;
14467 : : }
14468 : 5412857 : if (!conv || conv->bad_p)
14469 : : {
14470 : 578 : if (complain & tf_error)
14471 : : {
14472 : 351 : if (conv)
14473 : 306 : convert_like (conv, expr, complain);
14474 : 45 : else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
14475 : 13 : && !TYPE_REF_IS_RVALUE (type)
14476 : 58 : && !lvalue_p (expr))
14477 : 6 : error_at (loc, "invalid initialization of non-const reference of "
14478 : : "type %qH from an rvalue of type %qI",
14479 : 6 : type, TREE_TYPE (expr));
14480 : : else
14481 : 39 : error_at (loc, "invalid initialization of reference of type "
14482 : : "%qH from expression of type %qI", type,
14483 : 39 : TREE_TYPE (expr));
14484 : : }
14485 : 578 : return error_mark_node;
14486 : : }
14487 : :
14488 : 5411767 : if (conv->kind == ck_ref_bind)
14489 : : /* Perform the conversion. */
14490 : 5411764 : expr = convert_like (conv, expr, complain);
14491 : 3 : else if (conv->kind == ck_ambig)
14492 : : /* We gave an error in build_user_type_conversion_1. */
14493 : 3 : expr = error_mark_node;
14494 : : else
14495 : 0 : gcc_unreachable ();
14496 : :
14497 : : return expr;
14498 : 5412345 : }
14499 : :
14500 : : /* Return true if T is std::pair<const T&, const T&>. */
14501 : :
14502 : : static bool
14503 : 480134 : std_pair_ref_ref_p (tree t)
14504 : : {
14505 : : /* First, check if we have std::pair. */
14506 : 45308 : if (!NON_UNION_CLASS_TYPE_P (t)
14507 : 524982 : || !CLASSTYPE_TEMPLATE_INSTANTIATION (t))
14508 : : return false;
14509 : 14014 : tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (t));
14510 : 14014 : if (!decl_in_std_namespace_p (tdecl))
14511 : : return false;
14512 : 10221 : tree name = DECL_NAME (tdecl);
14513 : 10221 : if (!name || !id_equal (name, "pair"))
14514 : : return false;
14515 : :
14516 : : /* Now see if the template arguments are both const T&. */
14517 : 309 : tree args = CLASSTYPE_TI_ARGS (t);
14518 : 309 : if (TREE_VEC_LENGTH (args) != 2)
14519 : : return false;
14520 : 369 : for (int i = 0; i < 2; i++)
14521 : 399 : if (!TYPE_REF_OBJ_P (TREE_VEC_ELT (args, i))
14522 : 399 : || !CP_TYPE_CONST_P (TREE_TYPE (TREE_VEC_ELT (args, i))))
14523 : 279 : return false;
14524 : :
14525 : : return true;
14526 : : }
14527 : :
14528 : : /* Return true if a class T has a reference member. */
14529 : :
14530 : : static bool
14531 : 216 : class_has_reference_member_p (tree t)
14532 : : {
14533 : 216 : for (tree fields = TYPE_FIELDS (t);
14534 : 3361 : fields;
14535 : 3145 : fields = DECL_CHAIN (fields))
14536 : 3208 : if (TREE_CODE (fields) == FIELD_DECL
14537 : 196 : && !DECL_ARTIFICIAL (fields)
14538 : 3375 : && TYPE_REF_P (TREE_TYPE (fields)))
14539 : : return true;
14540 : : return false;
14541 : : }
14542 : :
14543 : : /* A wrapper for the above suitable as a callback for dfs_walk_once. */
14544 : :
14545 : : static tree
14546 : 216 : class_has_reference_member_p_r (tree binfo, void *)
14547 : : {
14548 : 216 : return (class_has_reference_member_p (BINFO_TYPE (binfo))
14549 : 216 : ? integer_one_node : NULL_TREE);
14550 : : }
14551 : :
14552 : :
14553 : : /* Return true if T (either a class or a function) has been marked as
14554 : : not-dangling. */
14555 : :
14556 : : static bool
14557 : 1458 : no_dangling_p (tree t)
14558 : : {
14559 : 1458 : t = lookup_attribute ("no_dangling", TYPE_ATTRIBUTES (t));
14560 : 1458 : if (!t)
14561 : : return false;
14562 : :
14563 : 75 : t = TREE_VALUE (t);
14564 : 75 : if (!t)
14565 : : return true;
14566 : :
14567 : 51 : t = build_converted_constant_bool_expr (TREE_VALUE (t), tf_warning_or_error);
14568 : 51 : t = cxx_constant_value (t);
14569 : 51 : return t == boolean_true_node;
14570 : : }
14571 : :
14572 : : /* Return true if a class CTYPE is either std::reference_wrapper or
14573 : : std::ref_view, or a reference wrapper class. We consider a class
14574 : : a reference wrapper class if it has a reference member. We no
14575 : : longer check that it has a constructor taking the same reference type
14576 : : since that approach still generated too many false positives. */
14577 : :
14578 : : static bool
14579 : 446 : reference_like_class_p (tree ctype)
14580 : : {
14581 : 446 : if (!CLASS_TYPE_P (ctype))
14582 : : return false;
14583 : :
14584 : 314 : if (no_dangling_p (ctype))
14585 : : return true;
14586 : :
14587 : : /* Also accept a std::pair<const T&, const T&>. */
14588 : 290 : if (std_pair_ref_ref_p (ctype))
14589 : : return true;
14590 : :
14591 : 275 : tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (ctype));
14592 : 275 : if (decl_in_std_namespace_p (tdecl))
14593 : : {
14594 : 38 : tree name = DECL_NAME (tdecl);
14595 : 38 : if (name
14596 : 38 : && (id_equal (name, "reference_wrapper")
14597 : 26 : || id_equal (name, "span")
14598 : 11 : || id_equal (name, "ref_view")))
14599 : : return true;
14600 : : }
14601 : :
14602 : : /* Avoid warning if CTYPE looks like std::span: it has a T* member and
14603 : : a trivial destructor. For example,
14604 : :
14605 : : template<typename T>
14606 : : struct Span {
14607 : : T* data_;
14608 : : std::size len_;
14609 : : };
14610 : :
14611 : : is considered std::span-like. */
14612 : 248 : if (NON_UNION_CLASS_TYPE_P (ctype) && TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
14613 : 228 : for (tree field = next_aggregate_field (TYPE_FIELDS (ctype));
14614 : 367 : field; field = next_aggregate_field (DECL_CHAIN (field)))
14615 : 199 : if (TYPE_PTR_P (TREE_TYPE (field)))
14616 : : return true;
14617 : :
14618 : : /* Some classes, such as std::tuple, have the reference member in its
14619 : : (non-direct) base class. */
14620 : 188 : if (dfs_walk_once (TYPE_BINFO (ctype), class_has_reference_member_p_r,
14621 : : nullptr, nullptr))
14622 : : return true;
14623 : :
14624 : : return false;
14625 : : }
14626 : :
14627 : : /* Helper for maybe_warn_dangling_reference to find a problematic temporary
14628 : : in EXPR (as outlined in maybe_warn_dangling_reference), or NULL_TREE
14629 : : if none found. For instance:
14630 : :
14631 : : const S& s = S().self(); // S()
14632 : : const int& r = (42, f(1)); // temporary for passing 1 to f
14633 : : const int& t = b ? f(1) : f(2); // temporary for 1
14634 : : const int& u = b ? f(1) : f(g); // temporary for 1
14635 : : const int& v = b ? f(g) : f(2); // temporary for 2
14636 : : const int& w = b ? f(g) : f(g); // NULL_TREE
14637 : : const int& y = (f(1), 42); // NULL_TREE
14638 : : const int& z = f(f(1)); // temporary for 1
14639 : :
14640 : : EXPR is the initializer. If ARG_P is true, we're processing an argument
14641 : : to a function; the point is to distinguish between, for example,
14642 : :
14643 : : Ref::inner (&TARGET_EXPR <D.2839, F::foo (fm)>)
14644 : :
14645 : : where we shouldn't warn, and
14646 : :
14647 : : Ref::inner (&TARGET_EXPR <D.2908, F::foo (&TARGET_EXPR <...>)>)
14648 : :
14649 : : where we should warn (Ref is a reference_like_class_p so we see through
14650 : : it. */
14651 : :
14652 : : static tree
14653 : 3620 : do_warn_dangling_reference (tree expr, bool arg_p)
14654 : : {
14655 : 3620 : STRIP_NOPS (expr);
14656 : :
14657 : 3620 : if (arg_p && expr_represents_temporary_p (expr))
14658 : : {
14659 : : /* An attempt to reduce the number of -Wdangling-reference
14660 : : false positives concerning reference wrappers (c++/107532).
14661 : : When we encounter a reference_like_class_p, we don't warn
14662 : : just yet; instead, we keep recursing to see if there were
14663 : : any temporaries behind the reference-wrapper class. */
14664 : : tree e = expr;
14665 : 354 : while (handled_component_p (e))
14666 : 15 : e = TREE_OPERAND (e, 0);
14667 : 339 : tree type = TREE_TYPE (e);
14668 : : /* If the temporary represents a lambda, we don't really know
14669 : : what's going on here. */
14670 : 461 : if (!reference_like_class_p (type) && !LAMBDA_TYPE_P (type))
14671 : : return expr;
14672 : : }
14673 : :
14674 : 3388 : switch (TREE_CODE (expr))
14675 : : {
14676 : 1689 : case CALL_EXPR:
14677 : 1689 : {
14678 : 1689 : tree fndecl = cp_get_callee_fndecl_nofold (expr);
14679 : 1689 : if (!fndecl
14680 : 1689 : || warning_suppressed_p (fndecl, OPT_Wdangling_reference)
14681 : 1678 : || !warning_enabled_at (DECL_SOURCE_LOCATION (fndecl),
14682 : 1678 : OPT_Wdangling_reference)
14683 : : /* Don't emit a false positive for:
14684 : : std::vector<int> v = ...;
14685 : : std::vector<int>::const_iterator it = v.begin();
14686 : : const int &r = *it++;
14687 : : because R refers to one of the int elements of V, not to
14688 : : a temporary object. Member operator* may return a reference
14689 : : but probably not to one of its arguments. */
14690 : 1355 : || (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl)
14691 : 849 : && DECL_OVERLOADED_OPERATOR_P (fndecl)
14692 : 320 : && DECL_OVERLOADED_OPERATOR_IS (fndecl, INDIRECT_REF))
14693 : 2833 : || no_dangling_p (TREE_TYPE (fndecl)))
14694 : 569 : return NULL_TREE;
14695 : :
14696 : 1120 : tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
14697 : : /* If the function doesn't return a reference, don't warn. This
14698 : : can be e.g.
14699 : : const int& z = std::min({1, 2, 3, 4, 5, 6, 7});
14700 : : which doesn't dangle: std::min here returns an int.
14701 : :
14702 : : If the function returns a std::pair<const T&, const T&>, we
14703 : : warn, to detect e.g.
14704 : : std::pair<const int&, const int&> v = std::minmax(1, 2);
14705 : : which also creates a dangling reference, because std::minmax
14706 : : returns std::pair<const T&, const T&>(b, a). */
14707 : 1120 : if (!(TYPE_REF_OBJ_P (rettype) || reference_like_class_p (rettype)))
14708 : : return NULL_TREE;
14709 : :
14710 : : /* Here we're looking to see if any of the arguments is a temporary
14711 : : initializing a reference parameter. */
14712 : 1456 : for (int i = 0; i < call_expr_nargs (expr); ++i)
14713 : : {
14714 : 1142 : tree arg = CALL_EXPR_ARG (expr, i);
14715 : : /* Check that this argument initializes a reference, except for
14716 : : the argument initializing the object of a member function. */
14717 : 1142 : if (!DECL_IOBJ_MEMBER_FUNCTION_P (fndecl)
14718 : 1142 : && !TYPE_REF_P (TREE_TYPE (arg)))
14719 : 130 : continue;
14720 : 1012 : STRIP_NOPS (arg);
14721 : 1012 : if (TREE_CODE (arg) == ADDR_EXPR)
14722 : 615 : arg = TREE_OPERAND (arg, 0);
14723 : : /* Recurse to see if the argument is a temporary. It could also
14724 : : be another call taking a temporary and returning it and
14725 : : initializing this reference parameter. */
14726 : 1012 : if ((arg = do_warn_dangling_reference (arg, /*arg_p=*/true)))
14727 : : {
14728 : : /* If we know the temporary could not bind to the return type,
14729 : : don't warn. This is for scalars and empty classes only
14730 : : because for other classes we can't be sure we are not
14731 : : returning its sub-object. */
14732 : 277 : if ((SCALAR_TYPE_P (TREE_TYPE (arg))
14733 : 152 : || is_empty_class (TREE_TYPE (arg)))
14734 : 152 : && TYPE_REF_P (rettype)
14735 : 137 : && !reference_related_p (TREE_TYPE (rettype),
14736 : 137 : TREE_TYPE (arg)))
14737 : 21 : continue;
14738 : 256 : return arg;
14739 : : }
14740 : : /* Don't warn about member functions like:
14741 : : std::any a(...);
14742 : : S& s = a.emplace<S>({0}, 0);
14743 : : which construct a new object and return a reference to it, but
14744 : : we still want to detect:
14745 : : struct S { const S& self () { return *this; } };
14746 : : const S& s = S().self();
14747 : : where 's' dangles. If we've gotten here, the object this function
14748 : : is invoked on is not a temporary. */
14749 : 735 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl))
14750 : : break;
14751 : : }
14752 : : return NULL_TREE;
14753 : : }
14754 : 28 : case COMPOUND_EXPR:
14755 : 28 : return do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p);
14756 : 31 : case COND_EXPR:
14757 : 31 : if (tree t = do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p))
14758 : : return t;
14759 : 16 : return do_warn_dangling_reference (TREE_OPERAND (expr, 2), arg_p);
14760 : 0 : case PAREN_EXPR:
14761 : 0 : return do_warn_dangling_reference (TREE_OPERAND (expr, 0), arg_p);
14762 : 122 : case TARGET_EXPR:
14763 : 122 : return do_warn_dangling_reference (TARGET_EXPR_INITIAL (expr), arg_p);
14764 : : default:
14765 : : return NULL_TREE;
14766 : : }
14767 : : }
14768 : :
14769 : : /* Implement -Wdangling-reference, to detect cases like
14770 : :
14771 : : int n = 1;
14772 : : const int& r = std::max(n - 1, n + 1); // r is dangling
14773 : :
14774 : : This creates temporaries from the arguments, returns a reference to
14775 : : one of the temporaries, but both temporaries are destroyed at the end
14776 : : of the full expression.
14777 : :
14778 : : This works by checking if a reference is initialized with a function
14779 : : that returns a reference, and at least one parameter of the function
14780 : : is a reference that is bound to a temporary. It assumes that such a
14781 : : function actually returns one of its arguments.
14782 : :
14783 : : DECL is the reference being initialized, INIT is the initializer. */
14784 : :
14785 : : static void
14786 : 59126238 : maybe_warn_dangling_reference (const_tree decl, tree init)
14787 : : {
14788 : 59126238 : if (!warn_dangling_reference)
14789 : : return;
14790 : 482240 : tree type = TREE_TYPE (decl);
14791 : : /* Only warn if what we're initializing has type T&& or const T&, or
14792 : : std::pair<const T&, const T&>. (A non-const lvalue reference can't
14793 : : bind to a temporary.) */
14794 : 962084 : if (!((TYPE_REF_OBJ_P (type)
14795 : 5222 : && (TYPE_REF_IS_RVALUE (type)
14796 : 4890 : || CP_TYPE_CONST_P (TREE_TYPE (type))))
14797 : 479844 : || std_pair_ref_ref_p (type)))
14798 : : return;
14799 : : /* Don't suppress the diagnostic just because the call comes from
14800 : : a system header. If the DECL is not in a system header, or if
14801 : : -Wsystem-headers was provided, warn. */
14802 : 2411 : auto wsh
14803 : 2411 : = make_temp_override (global_dc->m_warn_system_headers,
14804 : 2411 : (!in_system_header_at (DECL_SOURCE_LOCATION (decl))
14805 : 2411 : || global_dc->m_warn_system_headers));
14806 : 2411 : if (tree call = do_warn_dangling_reference (init, /*arg_p=*/false))
14807 : : {
14808 : 211 : auto_diagnostic_group d;
14809 : 211 : if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wdangling_reference,
14810 : : "possibly dangling reference to a temporary"))
14811 : 211 : inform (EXPR_LOCATION (call), "%qT temporary created here",
14812 : 211 : TREE_TYPE (call));
14813 : 211 : }
14814 : 2411 : }
14815 : :
14816 : : /* If *P is an xvalue expression, prevent temporary lifetime extension if it
14817 : : gets used to initialize a reference. */
14818 : :
14819 : : static tree
14820 : 86 : prevent_lifetime_extension (tree t)
14821 : : {
14822 : 86 : tree *p = &t;
14823 : 86 : while (TREE_CODE (*p) == COMPOUND_EXPR)
14824 : 0 : p = &TREE_OPERAND (*p, 1);
14825 : 95 : while (handled_component_p (*p))
14826 : 9 : p = &TREE_OPERAND (*p, 0);
14827 : : /* Change a TARGET_EXPR from prvalue to xvalue. */
14828 : 86 : if (TREE_CODE (*p) == TARGET_EXPR)
14829 : 3 : *p = build2 (COMPOUND_EXPR, TREE_TYPE (*p), *p,
14830 : 3 : move (TARGET_EXPR_SLOT (*p)));
14831 : 86 : return t;
14832 : : }
14833 : :
14834 : : /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
14835 : : which is bound either to a reference or a std::initializer_list. */
14836 : :
14837 : : static tree
14838 : 741933 : extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups,
14839 : : tree *cond_guard)
14840 : : {
14841 : : /* CWG1299 (C++20): The temporary object to which the reference is bound or
14842 : : the temporary object that is the complete object of a subobject to which
14843 : : the reference is bound persists for the lifetime of the reference if the
14844 : : glvalue to which the reference is bound was obtained through one of the
14845 : : following:
14846 : : - a temporary materialization conversion ([conv.rval]),
14847 : : - ( expression ), where expression is one of these expressions,
14848 : : - subscripting ([expr.sub]) of an array operand, where that operand is one
14849 : : of these expressions,
14850 : : - a class member access ([expr.ref]) using the . operator where the left
14851 : : operand is one of these expressions and the right operand designates a
14852 : : non-static data member of non-reference type,
14853 : : - a pointer-to-member operation ([expr.mptr.oper]) using the .* operator
14854 : : where the left operand is one of these expressions and the right operand
14855 : : is a pointer to data member of non-reference type,
14856 : : - a const_cast ([expr.const.cast]), static_cast ([expr.static.cast]),
14857 : : dynamic_cast ([expr.dynamic.cast]), or reinterpret_cast
14858 : : ([expr.reinterpret.cast]) converting, without a user-defined conversion,
14859 : : a glvalue operand that is one of these expressions to a glvalue that
14860 : : refers to the object designated by the operand, or to its complete
14861 : : object or a subobject thereof,
14862 : : - a conditional expression ([expr.cond]) that is a glvalue where the
14863 : : second or third operand is one of these expressions, or
14864 : : - a comma expression ([expr.comma]) that is a glvalue where the right
14865 : : operand is one of these expressions. */
14866 : :
14867 : : /* FIXME several cases are still handled wrong (101572, 81420). */
14868 : :
14869 : 741933 : tree sub = init;
14870 : 741933 : tree *p;
14871 : 741933 : STRIP_NOPS (sub);
14872 : 741933 : if (TREE_CODE (sub) == COMPOUND_EXPR)
14873 : : {
14874 : 281 : TREE_OPERAND (sub, 1)
14875 : 281 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups,
14876 : : cond_guard);
14877 : 281 : return init;
14878 : : }
14879 : 741652 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14880 : 741652 : && TYPE_PTRDATAMEM_P (TREE_TYPE (tree_strip_nop_conversions
14881 : : (TREE_OPERAND (sub, 1)))))
14882 : : {
14883 : : /* A pointer-to-member operation. */
14884 : 6 : TREE_OPERAND (sub, 0)
14885 : 6 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 0), cleanups,
14886 : : cond_guard);
14887 : 6 : return init;
14888 : : }
14889 : 741646 : if (TREE_CODE (sub) == COND_EXPR)
14890 : : {
14891 : 23568 : tree cur_cond_guard = NULL_TREE;
14892 : 23568 : if (TREE_OPERAND (sub, 1))
14893 : 23568 : TREE_OPERAND (sub, 1)
14894 : 47136 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups,
14895 : : &cur_cond_guard);
14896 : 23568 : if (cur_cond_guard)
14897 : : {
14898 : 9 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard,
14899 : : NOP_EXPR, boolean_true_node,
14900 : : tf_warning_or_error);
14901 : 9 : TREE_OPERAND (sub, 1)
14902 : 18 : = cp_build_compound_expr (set, TREE_OPERAND (sub, 1),
14903 : : tf_warning_or_error);
14904 : : }
14905 : 23568 : cur_cond_guard = NULL_TREE;
14906 : 23568 : if (TREE_OPERAND (sub, 2))
14907 : 23568 : TREE_OPERAND (sub, 2)
14908 : 47136 : = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 2), cleanups,
14909 : : &cur_cond_guard);
14910 : 23568 : if (cur_cond_guard)
14911 : : {
14912 : 12 : tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard,
14913 : : NOP_EXPR, boolean_true_node,
14914 : : tf_warning_or_error);
14915 : 12 : TREE_OPERAND (sub, 2)
14916 : 24 : = cp_build_compound_expr (set, TREE_OPERAND (sub, 2),
14917 : : tf_warning_or_error);
14918 : : }
14919 : 23568 : return init;
14920 : : }
14921 : 718078 : if (TREE_CODE (sub) != ADDR_EXPR)
14922 : : return init;
14923 : : /* Deal with binding to a subobject. */
14924 : 315252 : for (p = &TREE_OPERAND (sub, 0);
14925 : 360458 : TREE_CODE (*p) == COMPONENT_REF || TREE_CODE (*p) == ARRAY_REF; )
14926 : 45206 : p = &TREE_OPERAND (*p, 0);
14927 : 315252 : if (TREE_CODE (*p) == TARGET_EXPR)
14928 : : {
14929 : 11397 : tree subinit = NULL_TREE;
14930 : 11397 : *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit,
14931 : : cond_guard, nullptr);
14932 : 11397 : recompute_tree_invariant_for_addr_expr (sub);
14933 : 11397 : if (init != sub)
14934 : 11379 : init = fold_convert (TREE_TYPE (init), sub);
14935 : 11397 : if (subinit)
14936 : 10541 : init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
14937 : : }
14938 : : return init;
14939 : : }
14940 : :
14941 : : /* Data for extend_temps_r, mostly matching the parameters of
14942 : : extend_ref_init_temps. */
14943 : :
14944 : : struct extend_temps_data
14945 : : {
14946 : : tree decl;
14947 : : tree init;
14948 : : vec<tree, va_gc> **cleanups;
14949 : : tree* cond_guard;
14950 : : hash_set<tree> *pset; // For avoiding redundant walk_tree.
14951 : : hash_map<tree, tree> *var_map; // For remapping extended temps.
14952 : : };
14953 : :
14954 : : /* Tree walk function for extend_all_temps. Generally parallel to
14955 : : extend_ref_init_temps_1, but adapted for walk_tree. */
14956 : :
14957 : : tree
14958 : 159667 : extend_temps_r (tree *tp, int *walk_subtrees, void *data)
14959 : : {
14960 : 159667 : extend_temps_data *d = (extend_temps_data *)data;
14961 : :
14962 : 159667 : if (TREE_CODE (*tp) == VAR_DECL)
14963 : : {
14964 : 20992 : if (tree *r = d->var_map->get (*tp))
14965 : 0 : *tp = *r;
14966 : 20992 : return NULL_TREE;
14967 : : }
14968 : :
14969 : 138664 : if (TYPE_P (*tp) || TREE_CODE (*tp) == CLEANUP_POINT_EXPR
14970 : 277338 : || d->pset->add (*tp))
14971 : : {
14972 : 12217 : *walk_subtrees = 0;
14973 : 12217 : return NULL_TREE;
14974 : : }
14975 : :
14976 : 126458 : if (TREE_CODE (*tp) == COND_EXPR)
14977 : : {
14978 : 8 : cp_walk_tree (&TREE_OPERAND (*tp, 0), extend_temps_r, d, nullptr);
14979 : :
14980 : 24 : auto walk_arm = [d](tree &op)
14981 : : {
14982 : 16 : tree cur_cond_guard = NULL_TREE;
14983 : 16 : auto ov = make_temp_override (d->cond_guard, &cur_cond_guard);
14984 : 16 : cp_walk_tree (&op, extend_temps_r, d, nullptr);
14985 : 16 : if (cur_cond_guard)
14986 : : {
14987 : 2 : tree set = build2 (MODIFY_EXPR, boolean_type_node,
14988 : : cur_cond_guard, boolean_true_node);
14989 : 2 : op = cp_build_compound_expr (set, op, tf_none);
14990 : : }
14991 : 24 : };
14992 : 8 : walk_arm (TREE_OPERAND (*tp, 1));
14993 : 8 : walk_arm (TREE_OPERAND (*tp, 2));
14994 : :
14995 : 8 : *walk_subtrees = 0;
14996 : 8 : return NULL_TREE;
14997 : : }
14998 : :
14999 : 126450 : tree *p = tp;
15000 : :
15001 : 126450 : if (TREE_CODE (*tp) == ADDR_EXPR)
15002 : 30851 : for (p = &TREE_OPERAND (*tp, 0);
15003 : 32669 : TREE_CODE (*p) == COMPONENT_REF || TREE_CODE (*p) == ARRAY_REF; )
15004 : 1818 : p = &TREE_OPERAND (*p, 0);
15005 : :
15006 : 126450 : if (TREE_CODE (*p) == TARGET_EXPR
15007 : : /* An eliding TARGET_EXPR isn't a temporary at all. */
15008 : 4528 : && !TARGET_EXPR_ELIDING_P (*p)
15009 : : /* A TARGET_EXPR with TARGET_EXPR_INTERNAL_P is an artificial variable
15010 : : used during initialization that need not be extended. */
15011 : 130903 : && !TARGET_EXPR_INTERNAL_P (*p))
15012 : : {
15013 : : /* A CLEANUP_EH_ONLY expr should also have TARGET_EXPR_INTERNAL_P. */
15014 : 4155 : gcc_checking_assert (!CLEANUP_EH_ONLY (*p));
15015 : :
15016 : 4155 : tree subinit = NULL_TREE;
15017 : 4155 : tree slot = TARGET_EXPR_SLOT (*p);
15018 : 4155 : *p = set_up_extended_ref_temp (d->decl, *p, d->cleanups, &subinit,
15019 : : d->cond_guard, d);
15020 : 4155 : if (TREE_CODE (*tp) == ADDR_EXPR)
15021 : 4141 : recompute_tree_invariant_for_addr_expr (*tp);
15022 : 4155 : if (subinit)
15023 : 4084 : *tp = cp_build_compound_expr (subinit, *tp, tf_none);
15024 : 4155 : d->var_map->put (slot, *p);
15025 : : }
15026 : :
15027 : : return NULL_TREE;
15028 : : }
15029 : :
15030 : : /* Extend all the temporaries in a for-range-initializer. */
15031 : :
15032 : : static tree
15033 : 19996 : extend_all_temps (tree decl, tree init, vec<tree, va_gc> **cleanups)
15034 : : {
15035 : 19996 : hash_set<tree> pset;
15036 : 19996 : hash_map<tree, tree> map;
15037 : 19996 : gcc_assert (!TREE_STATIC (decl));
15038 : 19996 : extend_temps_data d = { decl, init, cleanups, nullptr, &pset, &map };
15039 : 19996 : cp_walk_tree (&init, extend_temps_r, &d, nullptr);
15040 : 19996 : return init;
15041 : 19996 : }
15042 : :
15043 : : /* INIT is part of the initializer for DECL. If there are any
15044 : : reference or initializer lists being initialized, extend their
15045 : : lifetime to match that of DECL. */
15046 : :
15047 : : tree
15048 : 60886581 : extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups,
15049 : : tree *cond_guard)
15050 : : {
15051 : 60886581 : tree type = TREE_TYPE (init);
15052 : 60886581 : if (processing_template_decl)
15053 : : return init;
15054 : :
15055 : : /* P2718R0 - in C++23 for-range-initializer, extend all temps. */
15056 : 59146234 : if (DECL_NAME (decl) == for_range__identifier
15057 : 59146234 : && flag_range_for_ext_temps)
15058 : : {
15059 : 19996 : gcc_checking_assert (!cond_guard);
15060 : 19996 : return extend_all_temps (decl, init, cleanups);
15061 : : }
15062 : :
15063 : 59126238 : maybe_warn_dangling_reference (decl, init);
15064 : :
15065 : 59126238 : if (TYPE_REF_P (type))
15066 : 693947 : init = extend_ref_init_temps_1 (decl, init, cleanups, cond_guard);
15067 : : else
15068 : : {
15069 : 58432291 : tree ctor = init;
15070 : 58432291 : if (TREE_CODE (ctor) == TARGET_EXPR)
15071 : 1025069 : ctor = TARGET_EXPR_INITIAL (ctor);
15072 : 58432291 : if (TREE_CODE (ctor) == CONSTRUCTOR)
15073 : : {
15074 : : /* [dcl.init] When initializing an aggregate from a parenthesized list
15075 : : of values... a temporary object bound to a reference does not have
15076 : : its lifetime extended. */
15077 : 4552479 : if (CONSTRUCTOR_IS_PAREN_INIT (ctor))
15078 : : return init;
15079 : :
15080 : 4552198 : if (is_std_init_list (type))
15081 : : {
15082 : : /* The temporary array underlying a std::initializer_list
15083 : : is handled like a reference temporary. */
15084 : 563 : tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
15085 : 563 : array = extend_ref_init_temps_1 (decl, array, cleanups,
15086 : : cond_guard);
15087 : 563 : CONSTRUCTOR_ELT (ctor, 0)->value = array;
15088 : : }
15089 : : else
15090 : : {
15091 : 4551635 : unsigned i;
15092 : 4551635 : constructor_elt *p;
15093 : 4551635 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
15094 : 28011075 : FOR_EACH_VEC_SAFE_ELT (elts, i, p)
15095 : 23459440 : p->value = extend_ref_init_temps (decl, p->value, cleanups,
15096 : : cond_guard);
15097 : : }
15098 : 4552198 : recompute_constructor_flags (ctor);
15099 : 4552198 : if (decl_maybe_constant_var_p (decl) && TREE_CONSTANT (ctor))
15100 : 2060782 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
15101 : : }
15102 : : }
15103 : :
15104 : : return init;
15105 : : }
15106 : :
15107 : : /* Returns true iff an initializer for TYPE could contain temporaries that
15108 : : need to be extended because they are bound to references or
15109 : : std::initializer_list. */
15110 : :
15111 : : bool
15112 : 6264 : type_has_extended_temps (tree type)
15113 : : {
15114 : 6264 : type = strip_array_types (type);
15115 : 6264 : if (TYPE_REF_P (type))
15116 : : return true;
15117 : 6207 : if (CLASS_TYPE_P (type))
15118 : : {
15119 : 3313 : if (is_std_init_list (type))
15120 : : return true;
15121 : 3310 : for (tree f = next_aggregate_field (TYPE_FIELDS (type));
15122 : 7960 : f; f = next_aggregate_field (DECL_CHAIN (f)))
15123 : 4779 : if (type_has_extended_temps (TREE_TYPE (f)))
15124 : : return true;
15125 : : }
15126 : : return false;
15127 : : }
15128 : :
15129 : : /* Returns true iff TYPE is some variant of std::initializer_list. */
15130 : :
15131 : : bool
15132 : 145000031 : is_std_init_list (tree type)
15133 : : {
15134 : 145000031 : if (!TYPE_P (type))
15135 : : return false;
15136 : 145000008 : if (cxx_dialect == cxx98)
15137 : : return false;
15138 : : /* Look through typedefs. */
15139 : 144580956 : type = TYPE_MAIN_VARIANT (type);
15140 : 114086808 : return (CLASS_TYPE_P (type)
15141 : 113947427 : && CP_TYPE_CONTEXT (type) == std_node
15142 : 215510064 : && init_list_identifier == DECL_NAME (TYPE_NAME (type)));
15143 : : }
15144 : :
15145 : : /* Returns true iff DECL is a list constructor: i.e. a constructor which
15146 : : will accept an argument list of a single std::initializer_list<T>. */
15147 : :
15148 : : bool
15149 : 125388621 : is_list_ctor (tree decl)
15150 : : {
15151 : 125388621 : tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
15152 : 125388621 : tree arg;
15153 : :
15154 : 125388621 : if (!args || args == void_list_node)
15155 : : return false;
15156 : :
15157 : 101837784 : arg = non_reference (TREE_VALUE (args));
15158 : 101837784 : if (!is_std_init_list (arg))
15159 : : return false;
15160 : :
15161 : 2174054 : args = TREE_CHAIN (args);
15162 : :
15163 : 3693334 : if (args && args != void_list_node && !TREE_PURPOSE (args))
15164 : : /* There are more non-defaulted parms. */
15165 : : return false;
15166 : :
15167 : : return true;
15168 : : }
15169 : :
15170 : : /* We know that can_convert_arg_bad already said "no" when trying to convert
15171 : : FROM to TO with ARG and FLAGS. Try to figure out if it was because
15172 : : an explicit conversion function was skipped when looking for a way to
15173 : : perform the conversion. At this point we've already printed an error. */
15174 : :
15175 : : void
15176 : 1256 : maybe_show_nonconverting_candidate (tree to, tree from, tree arg, int flags)
15177 : : {
15178 : 1256 : if (!(flags & LOOKUP_ONLYCONVERTING))
15179 : 154 : return;
15180 : :
15181 : 1102 : conversion_obstack_sentinel cos;
15182 : 1102 : conversion *c = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
15183 : : flags & ~LOOKUP_ONLYCONVERTING, tf_none);
15184 : 1102 : if (c && !c->bad_p && c->user_conv_p)
15185 : : /* Ay, the conversion would have worked in direct-init context. */
15186 : 258 : for (; c; c = next_conversion (c))
15187 : 172 : if (c->kind == ck_user
15188 : 86 : && DECL_P (c->cand->fn)
15189 : 258 : && DECL_NONCONVERTING_P (c->cand->fn))
15190 : 86 : inform (DECL_SOURCE_LOCATION (c->cand->fn), "explicit conversion "
15191 : : "function was not considered");
15192 : 1102 : }
15193 : :
15194 : : /* We're converting EXPR to TYPE. If that conversion involves a conversion
15195 : : function and we're binding EXPR to a reference parameter of that function,
15196 : : return true. */
15197 : :
15198 : : bool
15199 : 171 : conv_binds_to_reference_parm_p (tree type, tree expr)
15200 : : {
15201 : 171 : conversion_obstack_sentinel cos;
15202 : 171 : conversion *c = implicit_conversion (type, TREE_TYPE (expr), expr,
15203 : : /*c_cast_p=*/false, LOOKUP_NORMAL,
15204 : : tf_none);
15205 : 171 : if (c && !c->bad_p && c->user_conv_p)
15206 : 117 : for (; c; c = next_conversion (c))
15207 : 81 : if (c->kind == ck_user)
15208 : 244 : for (z_candidate *cand = c->cand; cand; cand = cand->next)
15209 : 208 : if (cand->viable == 1)
15210 : 81 : for (size_t i = 0; i < cand->num_convs; ++i)
15211 : 45 : if (cand->convs[i]->kind == ck_ref_bind
15212 : 45 : && conv_get_original_expr (cand->convs[i]) == expr)
15213 : : return true;
15214 : :
15215 : : return false;
15216 : 171 : }
15217 : :
15218 : : #include "gt-cp-call.h"
|